-
Notifications
You must be signed in to change notification settings - Fork 1
/
accountpage.cpp
295 lines (235 loc) · 8.09 KB
/
accountpage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright Arnt Gulbrandsen, [email protected].
#include "accountpage.h"
// geteuid()
#include <unistd.h>
#include <sys/types.h>
// getpwuid()
#include <sys/types.h>
#include <pwd.h>
#include <QGridLayout>
#include <QFileDialog>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDateTime>
#include <QMessageBox>
#include <QProcess>
/*! \class AccountPage accountpage.h
This class makes sure we have the details we need about the
tarsnap account; at the end all we need is the key file, but
getting there may require more.
*/
/*! Constructs an empty wizard page with the filename preset if
possible.
*/
AccountPage::AccountPage( BackupWizard * parent )
: QWizardPage( parent ),
file( new QLineEdit( this ) ),
login( new QLineEdit( this ) ),
password( new QLineEdit( this ) ),
host( new QLineEdit( "", this ) ),
tarsnap( nullptr ),
makeKey( new QPushButton( tr( "&Make new key" ), this ) ),
processStatus( new QLabel( "", this ) ),
cacheDirectory( new QLineEdit( parent->initialCacheDirectory(),
this ) ),
complete(false)
{
setTitle( "Host Key" );
setSubTitle( "Make or select host key" );
connect( file, SIGNAL(textChanged(const QString &)),
this, SLOT(checkPathNames()) );
connect( cacheDirectory, SIGNAL(textChanged(const QString &)),
this, SLOT(checkPathNames()) );
if ( cacheDirectory->text().isEmpty() ) {
struct passwd * pw = getpwuid(geteuid());
if ( pw && pw->pw_uid )
cacheDirectory->setText( QString::fromUtf8( pw->pw_dir ) +
"/.cache/tarsnap" );
else
cacheDirectory->setText( "/var/lib/tarsnap/cache" );
}
login->setPlaceholderText( tr( "Email address" ) );
password->setPlaceholderText( tr( "tarsnap.com password" ) );
password->setEchoMode( QLineEdit::Password );
char buf[1024];
::gethostname( buf, 1023 );
buf[1023] = 0;
host->setText( QString::fromAscii( buf ) );
file->setText( parent->initialKeyFile() );
if ( file->text().isEmpty() ) { // have to have some default...
struct passwd * pw = getpwuid(geteuid());
if ( pw )
file->setText( QString::fromUtf8( pw->pw_dir ) + "/" +
host->text() + ".tarsnapkey" );
else
file->setText( "/tmp/" + host->text() + ".tarsnapkey" );
}
registerField( "keyFile", file );
registerField( "host", host );
registerField( "cacheDirectory", cacheDirectory );
connect( makeKey, SIGNAL(clicked()),
this, SLOT(act()) );
enableMakeKey();
connect( login, SIGNAL(textChanged(const QString &)),
this, SLOT(enableMakeKey()) );
connect( password, SIGNAL(textChanged(const QString &)),
this, SLOT(enableMakeKey()) );
QGridLayout * l = new QGridLayout( this );
l->addWidget( new QLabel( tr( "Key file" ), this ),
0, 0, 1, 1, Qt::AlignLeft );
l->addWidget( file,
0, 1, 1, 1 );
QPushButton * b = new QPushButton( tr( "..." ), this );
connect( b, SIGNAL(clicked()),
this, SLOT(browseForFile()) );
l->addWidget( b,
0, 2, 1, 1 );
QLabel * howto = new QLabel(
tr( "<html>"
"If you don't have a host key yet, you have to register at "
"<a href=https://tarsnap.com/register.cgi>tarsnap.com</a> and "
"choose a password. In that case, please follow "
"the link and register, and add enough funds to pay for the first "
"backup.<p>"
"The email address and password "
"are needed to create the host key, and the host name so you can "
"back up several hosts using one tarsnap account.<p>"
"The key is stored only in the file you specify. "
"The email address is stored on tarsnap.com. "
"The password is not stored anywere.</html>" ),
this );
howto->setWordWrap( true );
howto->setOpenExternalLinks( true );
l->addWidget( howto,
1, 1, 1, 1 );
l->addWidget( new QLabel( tr( "Email" ), this ),
2, 0, 1, 1, Qt::AlignLeft );
l->addWidget( login,
2, 1, 1, 1 );
l->addWidget( new QLabel( tr( "Password" ), this ),
3, 0, 1, 1, Qt::AlignLeft );
l->addWidget( password,
3, 1, 1, 1 );
l->addWidget( new QLabel( tr( "Backup host" ), this ),
4, 0, 1, 1, Qt::AlignLeft );
l->addWidget( host,
4, 1, 1, 1 );
QHBoxLayout * processLayout = new QHBoxLayout();
l->addLayout( processLayout,
5, 1, 1, 1, Qt::AlignLeft );
processLayout->addWidget( makeKey );
processLayout->addWidget( processStatus, 2 );
l->addWidget( new QLabel( tr( "<html>Tarsnap caches information about what "
"it has uploaded in a local directory."
"</html" ),
this ),
6, 1, 1, 2, Qt::AlignLeft );
l->addWidget( new QLabel( tr( "Cache" ), this ),
7, 0, 1, 1, Qt::AlignLeft );
l->addWidget( cacheDirectory,
7, 1, 1, 1 );
b = new QPushButton( tr( "..." ), this );
connect( b, SIGNAL(clicked()),
this, SLOT(browseForCache()) );
l->addWidget( b,
7, 2, 1, 1 );
}
bool AccountPage::isComplete() const
{
if ( cacheDirectory->text().isEmpty() )
return false;
QString name = file->text();
if ( name.isEmpty() )
return false;
QFileInfo f( name );
return f.exists() && f.isFile();
}
/*! Browses for for the local file name.
This assumes that there is a file to be selected. It's also legal
to select a nonexistent file name and create it with "make new
key", but I think the common case is to browse to find the key, so
this change uses the right UI terminology.
*/
void AccountPage::browseForFile()
{
QString result = QFileDialog::getOpenFileName( this,
tr( "Select key file" ),
file->text() );
if ( !result.isNull() )
file->setText( result );
emit completeChanged();
}
/*! Runs tarsnap-keygen and makes sure we'll provide feedback later.
*/
void AccountPage::act()
{
if( tarsnap )
delete tarsnap;
tarsnap = new QProcess( this );
tarsnap->setProcessChannelMode( QProcess::MergedChannels );
QStringList cli;
cli << "--keyfile" << file->text().toUtf8()
<< "--user" << login->text().toUtf8()
<< "--machine" << host->text().toUtf8();
connect( tarsnap, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(handleExit(int, QProcess::ExitStatus)) );
tarsnap->start( field("tarsnapPath").toString() + "/tarsnap-keygen", cli );
QByteArray pw( password->text().toUtf8() + "\n" );
tarsnap->write( pw.data(), pw.size() );
processStatus->setText( tr( "Running..." ) );
}
/*! Contemplates what may have happened to the tarsnap-keygen process. */
void AccountPage::handleExit(int code, QProcess::ExitStatus status)
{
if ( status == QProcess::NormalExit && code == 0 ) {
processStatus->setText( tr( "Key successfully made" ) );
makeKey->setEnabled( false );
emit completeChanged();
return;
}
processStatus->setText( tr( "tarsnap-keygen failed" ) );
QMessageBox::critical( wizard(),
tr( "Error running tarsnap-keygen" ),
tr( "Oddly, tarsnap-keygen did not finish "
"normally. It seems likely that something "
"may have failed. The command line that "
"failed was quite likely "
"<code>tarsnap-keygen %1</code>" )
.arg("--keyfile " + file->text() +
" --user " + login->text() +
" --machine " + host->text() ) );
}
/*! This helper makes sure to emit completeChanged() whenever the key
file's and cache directory's status change.
*/
void AccountPage::checkPathNames()
{
bool was = complete;
complete = isComplete();
// let's not casually overwrite a valuable key
enableMakeKey();
if ( was != complete )
emit completeChanged();
}
/*! This helper enables/disables the "make key" button depending on
whether the user has provided the necessary input.
*/
void AccountPage::enableMakeKey()
{
makeKey->setEnabled( !isComplete() &&
login->text().contains( "@" ) &&
!password->text().isEmpty() );
}
/*! Opens a dialog to browse for a location where Tarsnap can store
its cache.
*/
void AccountPage::browseForCache()
{
QString s =
QFileDialog::getExistingDirectory( this,
tr( "Select cache directory" ),
cacheDirectory->text() );
if ( !s.isNull() )
cacheDirectory->setText( s );
}