modernize: Use override instead of virtual
[quassel.git] / src / qtui / coreconfigwizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreconfigwizard.h"
22
23 #include <QDebug>
24 #include <QAbstractButton>
25 #include <QCoreApplication>
26 #include <QFormLayout>
27 #include <QSpinBox>
28
29 #include "client.h"
30 #include "coreconnection.h"
31 #include "icon.h"
32
33 namespace {
34
35 QGroupBox *createDescriptionBox(const QString &description)
36 {
37     auto box = new QGroupBox;
38     auto layout = new QVBoxLayout(box);
39     auto label = new QLabel(description, box);
40     label->setWordWrap(true);
41     layout->addWidget(label);
42     layout->setAlignment(label, Qt::AlignTop);
43     box->setTitle(QCoreApplication::translate("CoreConfigWizard", "Description"));
44     return box;
45 }
46
47
48 template<typename FieldInfo>
49 QGroupBox *createFieldBox(const QString &title, const std::vector<FieldInfo> &fieldInfos)
50 {
51     // Create a config UI based on the field types sent from the backend
52     // We make some assumptions here (like integer range and password field names) that may not
53     // hold true for future authenticator types - but the only way around it for now would be to
54     // provide specialized config widgets for those (which may be a good idea anyway, e.g. if we
55     // think about client-side translations...)
56
57     auto *fieldBox = new QGroupBox;
58     fieldBox->setTitle(title);
59     auto *formLayout = new QFormLayout;
60     fieldBox->setLayout(formLayout);
61
62     for (auto &&fieldInfo : fieldInfos) {
63         QWidget *widget {nullptr};
64         switch (std::get<2>(fieldInfo).type()) {
65             case QVariant::Int:
66                 widget = new QSpinBox(fieldBox);
67                 // Here we assume that int fields are always in 16 bit range, like ports
68                 static_cast<QSpinBox *>(widget)->setMinimum(0);
69                 static_cast<QSpinBox *>(widget)->setMaximum(65535);
70                 static_cast<QSpinBox *>(widget)->setValue(std::get<2>(fieldInfo).toInt());
71                 break;
72             case QVariant::String:
73                 widget = new QLineEdit(std::get<2>(fieldInfo).toString(), fieldBox);
74                 // Here we assume that fields named something with "password" are actual password inputs
75                 if (std::get<0>(fieldInfo).toLower().contains("password"))
76                     static_cast<QLineEdit *>(widget)->setEchoMode(QLineEdit::Password);
77                 break;
78             default:
79                 qWarning() << "Unsupported type for backend property" << std::get<0>(fieldInfo);
80         }
81         if (widget) {
82             widget->setObjectName(std::get<0>(fieldInfo));
83             formLayout->addRow(std::get<1>(fieldInfo) + ":", widget);
84         }
85     }
86     return fieldBox;
87 }
88
89
90 template<typename FieldInfo>
91 QVariantMap propertiesFromFieldWidgets(QGroupBox *fieldBox, const std::vector<FieldInfo> &fieldInfos)
92 {
93     QVariantMap properties;
94     if (!fieldBox)
95         return properties;
96
97     for (auto &&fieldInfo : fieldInfos) {
98         QString key = std::get<0>(fieldInfo);
99         QVariant value;
100         switch (std::get<2>(fieldInfo).type()) {
101             case QVariant::Int: {
102                 QSpinBox *spinBox = fieldBox->findChild<QSpinBox *>(key);
103                 if (spinBox)
104                     value = spinBox->value();
105                 else
106                     qWarning() << "Could not find child widget for field" << key;
107                 break;
108             }
109             case QVariant::String: {
110                 QLineEdit *lineEdit = fieldBox->findChild<QLineEdit *>(key);
111                 if (lineEdit)
112                     value = lineEdit->text();
113                 else
114                     qWarning() << "Could not find child widget for field" << key;
115                 break;
116             }
117             default:
118                 qWarning() << "Unsupported type for backend property" << key;
119         }
120         properties[key] = std::move(value);
121     }
122     return properties;
123 }
124
125 } // anon
126
127
128 CoreConfigWizard::CoreConfigWizard(CoreConnection *connection, const QVariantList &backendInfos, const QVariantList &authInfos, QWidget *parent)
129     : QWizard(parent),
130     _connection{connection}
131 {
132     setModal(true);
133     setAttribute(Qt::WA_DeleteOnClose);
134
135     setPage(IntroPage, new CoreConfigWizardPages::IntroPage(this));
136     setPage(AdminUserPage, new CoreConfigWizardPages::AdminUserPage(this));
137     setPage(AuthenticationSelectionPage, new CoreConfigWizardPages::AuthenticationSelectionPage(authInfos, this));
138     setPage(StorageSelectionPage, new CoreConfigWizardPages::StorageSelectionPage(backendInfos, this));
139     syncPage = new CoreConfigWizardPages::SyncPage(this);
140     connect(syncPage, SIGNAL(setupCore(const QString &, const QVariantMap &, const QString &, const QVariantMap &)),
141             SLOT(prepareCoreSetup(const QString &, const QVariantMap &, const QString &, const QVariantMap &)));
142     setPage(SyncPage, syncPage);
143     syncRelayPage = new CoreConfigWizardPages::SyncRelayPage(this);
144     connect(syncRelayPage, SIGNAL(startOver()), this, SLOT(startOver()));
145     setPage(SyncRelayPage, syncRelayPage);
146
147     setStartId(IntroPage);
148
149 #ifndef Q_OS_MAC
150     setWizardStyle(ModernStyle);
151 #endif
152
153     setOption(HaveHelpButton, false);
154     setOption(NoBackButtonOnStartPage, true);
155     setOption(HaveNextButtonOnLastPage, false);
156     setOption(HaveFinishButtonOnEarlyPages, false);
157     setOption(NoCancelButton, true);
158     setOption(IndependentPages, true);
159
160     setModal(true);
161
162     setWindowTitle(CoreConfigWizard::tr("Core Configuration Wizard"));
163     setPixmap(QWizard::LogoPixmap, icon::get("quassel").pixmap(48));
164
165     connect(connection, SIGNAL(coreSetupSuccess()), SLOT(coreSetupSuccess()));
166     connect(connection, SIGNAL(coreSetupFailed(QString)), SLOT(coreSetupFailed(QString)));
167     connect(connection, SIGNAL(synchronized()), SLOT(syncFinished()));
168     connect(this, SIGNAL(rejected()), connection, SLOT(disconnectFromCore()));
169
170
171     // Resize all pages to the size hint of the largest one, so the wizard is large enough
172     QSize maxSize;
173     for (int id : pageIds()) {
174         auto p = page(id);
175         p->adjustSize();
176         maxSize = maxSize.expandedTo(p->sizeHint());
177     }
178     for (int id : pageIds()) {
179         page(id)->setFixedSize(maxSize);
180     }
181 }
182
183
184 void CoreConfigWizard::prepareCoreSetup(const QString &backend, const QVariantMap &properties, const QString &authenticator, const QVariantMap &authProperties)
185 {
186     // Prevent the user from changing any settings he already specified...
187     for (auto &&idx : visitedPages())
188         page(idx)->setEnabled(false);
189
190     // FIXME? We need to be able to set up older cores that don't have auth backend support.
191     // So if the core doesn't support that feature, don't pass those parameters.
192     if (!Client::isCoreFeatureEnabled(Quassel::Feature::Authenticators)) {
193         coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties));
194     }
195     else {
196         coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties, authenticator, authProperties));
197     }
198 }
199
200
201 void CoreConfigWizard::coreSetupSuccess()
202 {
203     syncPage->setStatus(tr("Your core has been successfully configured. Logging you in..."));
204     syncPage->setError(false);
205     syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Success);
206     coreConnection()->loginToCore(field("adminUser.user").toString(), field("adminUser.password").toString(), field("adminUser.rememberPasswd").toBool());
207 }
208
209
210 void CoreConfigWizard::coreSetupFailed(const QString &error)
211 {
212     syncPage->setStatus(tr("Core configuration failed:<br><b>%1</b><br>Press <em>Next</em> to start over.").arg(error));
213     syncPage->setError(true);
214     syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
215     //foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
216     //setStartId(SyncPage);
217     //restart();
218 }
219
220
221 void CoreConfigWizard::startOver()
222 {
223     foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
224     setStartId(CoreConfigWizard::AdminUserPage);
225     restart();
226 }
227
228
229 void CoreConfigWizard::syncFinished()
230 {
231     accept();
232 }
233
234
235 namespace CoreConfigWizardPages {
236 /*** Intro Page ***/
237
238 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent)
239 {
240     ui.setupUi(this);
241     setTitle(tr("Introduction"));
242     //setSubTitle(tr("foobar"));
243     //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
244 }
245
246
247 int IntroPage::nextId() const
248 {
249     return CoreConfigWizard::AdminUserPage;
250 }
251
252
253 /*** Admin User Page ***/
254
255 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent)
256 {
257     ui.setupUi(this);
258     setTitle(tr("Create Admin User"));
259     setSubTitle(tr("First, we will create a user on the core. This first user will have administrator privileges."));
260
261     registerField("adminUser.user*", ui.user);
262     registerField("adminUser.password*", ui.password);
263     registerField("adminUser.password2*", ui.password2);
264     registerField("adminUser.rememberPasswd", ui.rememberPasswd);
265 }
266
267
268 int AdminUserPage::nextId() const
269 {
270     // If the core doesn't support auth backends, skip that page!
271     if (!Client::isCoreFeatureEnabled(Quassel::Feature::Authenticators)) {
272         return CoreConfigWizard::StorageSelectionPage;
273     }
274     else {
275         return CoreConfigWizard::AuthenticationSelectionPage;
276     }
277 }
278
279
280 bool AdminUserPage::isComplete() const
281 {
282     bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
283     return ok;
284 }
285
286 /*** Authentication Selection Page ***/
287
288 AuthenticationSelectionPage::AuthenticationSelectionPage(const QVariantList &authInfos, QWidget *parent)
289     : QWizardPage(parent)
290 {
291     ui.setupUi(this);
292
293     setTitle(tr("Select Authentication Backend"));
294     setSubTitle(tr("Please select a backend for Quassel Core to use for authenticating users."));
295
296     registerField("authentication.backend", ui.backendList);
297
298     for (auto &&authInfo : authInfos) {
299         auto props = authInfo.toMap();
300         // Extract field infos to avoid having to reparse the list
301         std::vector<FieldInfo> fields;
302         const auto &list = props["SetupData"].toList();
303         for (int i = 0; i + 2 < list.size(); i += 3) {
304             fields.emplace_back(std::make_tuple(list[i].toString(), list[i+1].toString(), list[i+2]));
305         }
306         props.remove("SetupData");
307
308         _authProperties.emplace_back(std::move(props));
309         _authFields.emplace_back(std::move(fields));
310
311         // Create widgets
312         ui.backendList->addItem(_authProperties.back()["DisplayName"].toString(), _authProperties.back()["BackendId"].toString());
313         ui.descriptionStack->addWidget(createDescriptionBox(_authProperties.back()["Description"].toString()));
314         ui.authSettingsStack->addWidget(createFieldBox(tr("Authentication Settings"), _authFields.back()));
315     }
316
317     // Do some trickery to make the page large enough
318     setSizePolicy({QSizePolicy::Fixed, QSizePolicy::Fixed});
319
320     QSizePolicy sp{QSizePolicy::MinimumExpanding, QSizePolicy::Fixed};
321     sp.setRetainSizeWhenHidden(true);
322     ui.descriptionStack->setSizePolicy(sp);
323     ui.authSettingsStack->setSizePolicy(sp);
324
325     ui.descriptionStack->adjustSize();
326     ui.authSettingsStack->adjustSize();
327
328     ui.backendList->setCurrentIndex(0);
329 }
330
331
332 int AuthenticationSelectionPage::nextId() const
333 {
334     return CoreConfigWizard::StorageSelectionPage;
335 }
336
337
338 QString AuthenticationSelectionPage::displayName() const
339 {
340     return ui.backendList->currentText();
341 }
342
343
344 QString AuthenticationSelectionPage::authenticator() const
345 {
346     return ui.backendList->currentData().toString();
347 }
348
349
350 QVariantMap AuthenticationSelectionPage::authProperties() const
351 {
352     return propertiesFromFieldWidgets(qobject_cast<QGroupBox *>(ui.authSettingsStack->currentWidget()),
353                                       _authFields[ui.backendList->currentIndex()]);
354 }
355
356
357 void AuthenticationSelectionPage::on_backendList_currentIndexChanged(int index)
358 {
359     ui.descriptionStack->setCurrentIndex(index);
360     ui.authSettingsStack->setCurrentIndex(index);
361     ui.authSettingsStack->setVisible(!_authFields[index].empty());
362 }
363
364 /*** Storage Selection Page ***/
365
366 StorageSelectionPage::StorageSelectionPage(const QVariantList &backendInfos, QWidget *parent)
367     : QWizardPage(parent)
368 {
369     ui.setupUi(this);
370
371     setTitle(tr("Select Storage Backend"));
372     setSubTitle(tr("Please select a storage backend for Quassel Core."));
373     setCommitPage(true);
374
375     registerField("storage.backend", ui.backendList);
376
377     int defaultIndex {0};  // Legacy cores send backend infos in arbitrary order
378
379     for (auto &&backendInfo : backendInfos) {
380         auto props = backendInfo.toMap();
381         // Extract field infos to avoid having to reparse the list
382         std::vector<FieldInfo> fields;
383
384         // Legacy cores (prior to 0.13) didn't send SetupData for storage backends; deal with this
385         if (!props.contains("SetupData")) {
386             const auto &defaultValues = props["SetupDefaults"].toMap();
387             for (auto &&key : props["SetupKeys"].toStringList()) {
388                 fields.emplace_back(std::make_tuple(key, key, defaultValues.value(key, QString{})));
389             }
390             if (props.value("IsDefault", false).toBool()) {
391                 defaultIndex = ui.backendList->count();
392             }
393         }
394         else {
395             const auto &list = props["SetupData"].toList();
396             for (int i = 0; i + 2 < list.size(); i += 3) {
397                 fields.emplace_back(std::make_tuple(list[i].toString(), list[i+1].toString(), list[i+2]));
398             }
399             props.remove("SetupData");
400         }
401         props.remove("SetupKeys");
402         props.remove("SetupDefaults");
403         // Legacy cores (prior to 0.13) don't send the BackendId property
404         if (!props.contains("BackendId"))
405             props["BackendId"] = props["DisplayName"];
406         _backendProperties.emplace_back(std::move(props));
407         _backendFields.emplace_back(std::move(fields));
408
409         // Create widgets
410         ui.backendList->addItem(_backendProperties.back()["DisplayName"].toString(), _backendProperties.back()["BackendId"].toString());
411         ui.descriptionStack->addWidget(createDescriptionBox(_backendProperties.back()["Description"].toString()));
412         ui.storageSettingsStack->addWidget(createFieldBox(tr("Storage Settings"), _backendFields.back()));
413     }
414
415     // Do some trickery to make the page large enough
416     setSizePolicy({QSizePolicy::Fixed, QSizePolicy::Fixed});
417
418     QSizePolicy sp{QSizePolicy::MinimumExpanding, QSizePolicy::Fixed};
419     sp.setRetainSizeWhenHidden(true);
420     ui.descriptionStack->setSizePolicy(sp);
421     ui.storageSettingsStack->setSizePolicy(sp);
422
423     ui.descriptionStack->adjustSize();
424     ui.storageSettingsStack->adjustSize();
425
426     ui.backendList->setCurrentIndex(defaultIndex);
427 }
428
429
430 int StorageSelectionPage::nextId() const
431 {
432     return CoreConfigWizard::SyncPage;
433 }
434
435
436 QString StorageSelectionPage::displayName() const
437 {
438     return ui.backendList->currentText();
439 }
440
441
442 QString StorageSelectionPage::backend() const
443 {
444     return ui.backendList->currentData().toString();
445 }
446
447
448 QVariantMap StorageSelectionPage::backendProperties() const
449 {
450     return propertiesFromFieldWidgets(qobject_cast<QGroupBox *>(ui.storageSettingsStack->currentWidget()),
451                                       _backendFields[ui.backendList->currentIndex()]);
452 }
453
454
455 void StorageSelectionPage::on_backendList_currentIndexChanged(int index)
456 {
457     ui.descriptionStack->setCurrentIndex(index);
458     ui.storageSettingsStack->setCurrentIndex(index);
459     ui.storageSettingsStack->setVisible(!_backendFields[index].empty());
460 }
461
462
463 /*** Sync Page ***/
464
465 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent)
466 {
467     ui.setupUi(this);
468     setTitle(tr("Storing Your Settings"));
469     setSubTitle(tr("Your settings are now being stored in the core, and you will be logged in automatically."));
470 }
471
472
473 void SyncPage::initializePage()
474 {
475     _complete = false;
476     _hasError = false;
477     emit completeChanged();
478
479     // Fill in sync info about the storage layer.
480     StorageSelectionPage *storagePage = qobject_cast<StorageSelectionPage *>(wizard()->page(CoreConfigWizard::StorageSelectionPage));
481     QString backend = storagePage->backend();
482     QVariantMap backendProperties = storagePage->backendProperties();
483     ui.backend->setText(storagePage->displayName());
484
485     // Fill in sync info about the authentication layer.
486     AuthenticationSelectionPage *authPage = qobject_cast<AuthenticationSelectionPage *>(wizard()->page(CoreConfigWizard::AuthenticationSelectionPage));
487     QString authenticator = authPage->authenticator();
488     QVariantMap authProperties = authPage->authProperties();
489     ui.authenticator->setText(authPage->displayName());
490
491     ui.user->setText(wizard()->field("adminUser.user").toString());
492
493     emit setupCore(backend, backendProperties, authenticator, authProperties);
494 }
495
496
497 int SyncPage::nextId() const
498 {
499     if (!_hasError)
500         return -1;
501     return CoreConfigWizard::SyncRelayPage;
502 }
503
504
505 bool SyncPage::isComplete() const
506 {
507     return _complete || _hasError;
508 }
509
510
511 void SyncPage::setStatus(const QString &status)
512 {
513     ui.status->setText(status);
514 }
515
516
517 void SyncPage::setError(bool e)
518 {
519     _hasError = e;
520     setFinalPage(!e);
521     emit completeChanged();
522 }
523
524
525 void SyncPage::setComplete(bool c)
526 {
527     _complete = c;
528     completeChanged();
529 }
530
531
532 /*** Sync Relay Page ***/
533
534 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent)
535 {
536     mode = Success;
537 }
538
539
540 void SyncRelayPage::setMode(Mode m)
541 {
542     mode = m;
543 }
544
545 int SyncRelayPage::nextId() const
546 {
547     emit startOver();
548     return 0;
549 }
550 }  /* namespace CoreConfigWizardPages */