/
AddCloudWizard.cpp
524 lines (447 loc) · 19.1 KB
/
AddCloudWizard.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Copyright (c) 2017 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "AddCloudWizard.h"
#include "MainWindow.h"
#include "Athlete.h"
#include "Context.h"
#include "Settings.h"
#include "Colors.h"
#include "CloudService.h"
#include <QMessageBox>
// WIZARD FLOW
//
// 01. Select Service Class (e.g. Activities, Measures, Calendar)
// 10. Select Cloud Service Type (via CloudServiceFactory)
// 20. Authenticate Account (URL+Key, OAUTH or User/Pass)
// 30. Settings (Folder,sync on startup, sync on import)
// 90. Finalise (Confirm complete and add)
//
// Main wizard
AddCloudWizard::AddCloudWizard(Context *context) : QWizard(context->mainWindow), context(context)
{
#ifdef Q_OS_MAC
setWizardStyle(QWizard::ModernStyle);
#endif
// delete when done
setWindowModality(Qt::NonModal); // avoid blocking WFAPI calls for kickr
setAttribute(Qt::WA_DeleteOnClose);
setMinimumWidth(600 *dpiXFactor);
setMinimumHeight(500 *dpiYFactor);
// title
setWindowTitle(tr("Add Cloud Wizard"));
setPage(01, new AddClass(this)); // done
setPage(10, new AddService(this)); // done
setPage(20, new AddAuth(this)); // done
setPage(30, new AddSettings(this)); // done
setPage(90, new AddFinish(this)); // done
cloudService = NULL; // not cloned yet
done = false;
}
/*----------------------------------------------------------------------
* Wizard Pages
*--------------------------------------------------------------------*/
//Select Cloud type
AddClass::AddClass(AddCloudWizard *parent) : QWizardPage(parent), wizard(parent)
{
setTitle(tr("Service Type"));
setSubTitle(tr("What type of Service are you adding an account for ?"));
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(int)), this, SLOT(clicked(int)));
// Activities
QCommandLinkButton *p = new QCommandLinkButton(tr("Activities"), tr("Sync activities with services like Today's Plan, Strava, Dropbox and Google Drive"), this);
p->setStyleSheet(QString("font-size: %1px;").arg(12 * dpiXFactor));
connect(p, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(p, CloudService::Activities);
layout->addWidget(p);
// Measures
p = new QCommandLinkButton(tr("Measures"), tr("Sync measures data such as weight, body fat, HRV and sleep."));
p->setStyleSheet(QString("font-size: %1px;").arg(12 * dpiXFactor));
connect(p, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(p, CloudService::Measures);
layout->addWidget(p);
// Activities
p = new QCommandLinkButton(tr("Calendar"), tr("Sync planned workouts to WebDAV and CalDAV calendars."));
p->setStyleSheet(QString("font-size: %1px;").arg(12 * dpiXFactor));
connect(p, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(p, CloudService::Calendar);
layout->addWidget(p);
setFinalPage(false);
}
void
AddClass::clicked(int t)
{
// reset -- particularly since we might get here from
// other pages hitting 'Back'
wizard->type = t;
initializePage();
wizard->next();
}
//Select Cloud type
AddService::AddService(AddCloudWizard *parent) : QWizardPage(parent), wizard(parent)
{
setTitle(tr("Account Type"));
setSubTitle(tr("Select the cloud service type"));
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
buttons=new QWidget(this);
buttons->setContentsMargins(0,0,0,0);
buttonlayout= new QVBoxLayout(buttons);
buttonlayout->setSpacing(0);
scrollarea=new QScrollArea(this);
scrollarea->setWidgetResizable(true);
scrollarea->setWidget(buttons);
mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(QString)), this, SLOT(clicked(QString)));
layout->addWidget(scrollarea);
setFinalPage(false);
}
void
AddService::initializePage()
{
// clear whatever we have, if anything
QLayoutItem *item = NULL;
while((item = buttonlayout->takeAt(0)) != NULL) {
if (item->widget()) delete item->widget();
delete item;
}
CloudServiceFactory &factory = CloudServiceFactory::instance();
foreach(CloudService *s, factory.services()) {
// only ones with the capability we need.
if (s->type() != wizard->type) continue;
QCommandLinkButton *p = new QCommandLinkButton(s->name(), s->description(), this);
p->setStyleSheet(QString("font-size: %1px;").arg(12 * dpiXFactor));
connect(p, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(p, s->name());
buttonlayout->addWidget(p);
}
}
void
AddService::clicked(QString p)
{
// reset -- particularly since we might get here from
// other pages hitting 'Back'
wizard->service = p;
const CloudService *s = CloudServiceFactory::instance().service(wizard->service);
if (wizard->cloudService) delete wizard->cloudService;
wizard->cloudService = const_cast<CloudService*>(s)->clone(wizard->context);
if (wizard->cloudService) {
// clear the settings we previously edited
wizard->settings.clear();
// get default or whatever
QString cname;
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::URL, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
if (value == "") {
// get the default value for the service
value = wizard->cloudService->settings.value(CloudService::CloudServiceSetting::DefaultURL, "");
}
wizard->settings.insert(cname, value);
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Key, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
wizard->settings.insert(cname, value);
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Username, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
wizard->settings.insert(cname, value);
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Password, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
wizard->settings.insert(cname, value);
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::OAuthToken, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
wizard->settings.insert(cname, value);
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Folder, "")) != "") {
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, cname, "").toString();
wizard->settings.insert(cname, value);
}
// generic settings
QString value = appsettings->cvalue(wizard->context->athlete->cyclist, wizard->cloudService->syncOnImportSettingName(), "false").toString();
wizard->settings.insert(wizard->cloudService->syncOnImportSettingName(), value);
value = appsettings->cvalue(wizard->context->athlete->cyclist, wizard->cloudService->syncOnStartupSettingName(), "false").toString();
wizard->settings.insert(wizard->cloudService->syncOnStartupSettingName(), value);
wizard->next();
}
}
//Select Cloud type
AddAuth::AddAuth(AddCloudWizard *parent) : QWizardPage(parent), wizard(parent)
{
setTitle(tr("Service Credentials "));
setSubTitle(tr("Credentials and authorisation"));
QFormLayout *layout = new QFormLayout;
//layout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
// labels
urlLabel = new QLabel(tr("URL"));
keyLabel = new QLabel(tr("Key (optional)"));
userLabel = new QLabel(tr("Username"));
passLabel = new QLabel(tr("Password"));
authLabel = new QLabel(tr("Authorise"));
tokenLabel = new QLabel(tr("Token"));
// input boxes
url = new QLineEdit(this);
url->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
key = new QLineEdit(this);
key->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
user = new QLineEdit(this);
user->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
pass = new QLineEdit(this);
pass->setEchoMode(QLineEdit::Password);
pass->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
auth = new QPushButton(tr("Authorise"), this);
auth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
token = new QLabel(this);
token->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
layout->addRow(urlLabel, url);
layout->addRow(keyLabel, key);
layout->addRow(userLabel, user);
layout->addRow(passLabel, pass);
layout->addRow(authLabel, auth);
layout->addRow(tokenLabel, token);
setLayout(layout);
setFinalPage(false);
}
void
AddAuth::initializePage()
{
setSubTitle(tr("Credentials and authorisation"));
// hide all the widgets
url->hide();
key->hide();
user->hide();
pass->hide();
auth->hide();
token->hide();
urlLabel->hide();
keyLabel->hide();
userLabel->hide();
passLabel->hide();
authLabel->hide();
tokenLabel->hide();
// clone to do next few steps!
setSubTitle(QString(tr("%1 Credentials and authorisation")).arg(wizard->cloudService->name()));
// show all the widgets relevant for this service and update the value from the
// settings we have collected (which will have been defaulted).
QString cname;
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::URL, "")) != "") {
url->show(); urlLabel->show();
url->setText(wizard->settings.value(cname, ""));
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Key, "")) != "") {
key->show(); keyLabel->show();
key->setText(wizard->settings.value(cname, ""));
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Username, "")) != "") {
user->show(); userLabel->show();
user->setText(wizard->settings.value(cname, ""));
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Password, "")) != "") {
pass->show(); passLabel->show();
pass->setText(wizard->settings.value(cname, ""));
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::OAuthToken, "")) != "") {
auth->show(); authLabel->show();
token->show(); tokenLabel->show();
token->setText(wizard->settings.value(cname, ""));
}
}
bool
AddAuth::validatePage()
{
// check the authorisation has been completed
QString cname;
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::URL, "")) != "") {
wizard->settings.insert(cname, url->text());
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Key, "")) != "") {
wizard->settings.insert(cname, key->text());
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Username, "")) != "") {
wizard->settings.insert(cname, user->text());
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Password, "")) != "") {
wizard->settings.insert(cname, pass->text());
}
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::OAuthToken, "")) != "") {
wizard->settings.insert(cname, token->text());
}
return true;
}
// Scan for Cloud port / usb etc
AddSettings::AddSettings(AddCloudWizard *parent) : QWizardPage(parent), wizard(parent)
{
setSubTitle(tr("Cloud Service Settings"));
QFormLayout *layout = new QFormLayout(this);
layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
setLayout(layout);
folderLabel = new QLabel(tr("Folder"));
folder = new QLineEdit(this);
folder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
browse = new QPushButton(tr("Browse"));
syncStartup = new QCheckBox(tr("Sync on startup"));
syncImport = new QCheckBox(tr("Sync on import"));
QHBoxLayout *flayout = new QHBoxLayout;
flayout->addWidget(folderLabel);
flayout->addWidget(folder);
flayout->addWidget(browse);
layout->addRow(flayout);
layout->addRow(syncStartup); // only makes sense if the service has a query api
layout->addRow(syncImport); // only makes sense if the service has an upload api
connect(browse, SIGNAL(clicked()), this, SLOT(browseFolder()));
}
void
AddSettings::initializePage()
{
setTitle(QString(tr("Service Settings")));
// hide everything first
folderLabel->hide();
folder->hide();
browse->hide();
syncStartup->hide();
syncImport->hide();
// if we need a folder then set that to show
QString cname;
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Folder, "")) != "") {
browse->show(); folder->show(); folderLabel->show();
folder->setText(wizard->settings.value(cname, ""));
}
if (wizard->cloudService->capabilities() & CloudService::Query) {
QString value = wizard->settings.value(wizard->cloudService->syncOnStartupSettingName(), "false");
syncStartup->setChecked(value == "true");
syncStartup->show();
}
if (wizard->cloudService->capabilities() & CloudService::Upload) {
QString value = wizard->settings.value(wizard->cloudService->syncOnImportSettingName(), "false");
syncImport->setChecked(value == "true");
syncImport->show();
}
}
bool
AddSettings::validatePage()
{
// check the authorisation has been completed
QString cname;
if ((cname=wizard->cloudService->settings.value(CloudService::CloudServiceSetting::Folder, "")) != "") {
wizard->settings.insert(cname, folder->text());
}
// generic settings, but applied on a per service basis
wizard->settings.insert(wizard->cloudService->syncOnImportSettingName(), syncImport->isChecked() ? "true" : "false");
wizard->settings.insert(wizard->cloudService->syncOnStartupSettingName(), syncImport->isChecked() ? "true" : "false");
return true;
}
void
AddSettings::browseFolder()
{
// get current edit..
QString path = folder->text();
QStringList errors;
// open the connection using the current token
if (!wizard->cloudService->open(errors)) {
QMessageBox err;
err.setText(tr("Connection Failed"));
err.setDetailedText(errors.join("\n\n"));
err.setIcon(QMessageBox::Warning);
err.exec();
}
// find the folder using the current settings
CloudServiceDialog dialog(this, wizard->cloudService, tr("Choose Athlete Directory"), path, true);
int ret = dialog.exec();
// did we actually select something?
if (ret == QDialog::Accepted) {
path = dialog.pathnameSelected();
folder->setText(path);
//XXX ack QString id = google_drive.GetFileId(path);
//XXX ack appsettings->setCValue(context->athlete->cyclist, GC_GOOGLE_DRIVE_FOLDER_ID, id);
}
}
// Final confirmation
AddFinish::AddFinish(AddCloudWizard *parent) : QWizardPage(parent), wizard(parent)
{
setTitle(tr("Done"));
setSubTitle(tr("Add Cloud Account"));
layout = new QFormLayout;
setLayout(layout);
}
void
AddFinish::initializePage()
{
// clear previous
while(layout->count() > 0) {
QLayoutItem *item = layout->takeAt(0);
if (item->widget()) delete item->widget();
delete item;
}
// add from wizard settings -- this is what we
// will now create.
QHashIterator<CloudService::CloudServiceSetting,QString> want(wizard->cloudService->settings);
want.toFront();
while(want.hasNext()) {
want.next();
QString label, value;
switch(want.key()) {
case CloudService::URL: label=tr("URL"); break;
case CloudService::Key: label=tr("Key"); break;
case CloudService::Username: label=tr("Username"); break;
case CloudService::Password: label=tr("Password"); break;
case CloudService::OAuthToken: label=tr("Token"); break;
case CloudService::Folder: label=tr("Folder"); break;
case CloudService::DefaultURL: break;
}
// no clue
if (label == "") continue;
// get value
value = wizard->settings.value(want.value(), "");
if (value == "") continue;
// ok, we have a setting
if (label==tr("Password")) layout->addRow(new QLabel(label), new QLabel (QString("*").repeated(value.length())));
else layout->addRow(new QLabel(label), new QLabel (value));
}
QString syncstartup = wizard->settings.value(wizard->cloudService->syncOnStartupSettingName(), "");
if (syncstartup != "") layout->addRow(new QLabel(tr("Sync on start")), new QLabel (syncstartup));
QString syncimport = wizard->settings.value(wizard->cloudService->syncOnImportSettingName(), "");
if (syncimport != "") layout->addRow(new QLabel(tr("Sync on import")), new QLabel (syncimport));
}
bool
AddFinish::validatePage()
{
// ok, last thing to do is write out the new settings
QHashIterator<CloudService::CloudServiceSetting,QString> want(wizard->cloudService->settings);
want.toFront();
while(want.hasNext()) {
want.next();
// get value
QString value = wizard->settings.value(want.value(), "");
if (value == "") continue;
// ok, we have a setting
appsettings->setCValue(wizard->context->athlete->cyclist, want.value(), value);
}
// generic settings
QString syncstartup = wizard->settings.value(wizard->cloudService->syncOnStartupSettingName(), "");
if (syncstartup != "") appsettings->setCValue(wizard->context->athlete->cyclist,
wizard->cloudService->syncOnStartupSettingName(),
syncstartup);
QString syncimport = wizard->settings.value(wizard->cloudService->syncOnImportSettingName(), "");
if (syncimport != "") appsettings->setCValue(wizard->context->athlete->cyclist,
wizard->cloudService->syncOnImportSettingName(),
syncimport);
return true;
}