1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include <QAbstractButton>
23 #include <QCoreApplication>
24 #include <QFormLayout>
28 #include "coreconfigwizard.h"
29 #include "coreconnection.h"
35 QGroupBox *createDescriptionBox(const QString &description)
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"));
48 template<typename FieldInfo>
49 QGroupBox *createFieldBox(const QString &title, const std::vector<FieldInfo> &fieldInfos)
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...)
57 QGroupBox *fieldBox = new QGroupBox;
58 fieldBox->setTitle(title);
60 QFormLayout *formLayout = new QFormLayout(fieldBox);
61 for (auto &&fieldInfo : fieldInfos) {
62 QWidget *widget {nullptr};
63 switch (std::get<2>(fieldInfo).type()) {
65 widget = new QSpinBox(fieldBox);
66 // Here we assume that int fields are always in 16 bit range, like ports
67 static_cast<QSpinBox *>(widget)->setMinimum(0);
68 static_cast<QSpinBox *>(widget)->setMaximum(65535);
69 static_cast<QSpinBox *>(widget)->setValue(std::get<2>(fieldInfo).toInt());
71 case QVariant::String:
72 widget = new QLineEdit(std::get<2>(fieldInfo).toString(), fieldBox);
73 // Here we assume that fields named something with "password" are actual password inputs
74 if (std::get<0>(fieldInfo).toLower().contains("password"))
75 static_cast<QLineEdit *>(widget)->setEchoMode(QLineEdit::Password);
78 qWarning() << "Unsupported type for backend property" << std::get<0>(fieldInfo);
81 widget->setObjectName(std::get<0>(fieldInfo));
82 formLayout->addRow(std::get<1>(fieldInfo) + ":", widget);
89 template<typename FieldInfo>
90 QVariantMap propertiesFromFieldWidgets(QGroupBox *fieldBox, const std::vector<FieldInfo> &fieldInfos)
92 QVariantMap properties;
96 for (auto &&fieldInfo : fieldInfos) {
97 QString key = std::get<0>(fieldInfo);
99 switch (std::get<2>(fieldInfo).type()) {
100 case QVariant::Int: {
101 QSpinBox *spinBox = fieldBox->findChild<QSpinBox *>(key);
103 value = spinBox->value();
105 qWarning() << "Could not find child widget for field" << key;
108 case QVariant::String: {
109 QLineEdit *lineEdit = fieldBox->findChild<QLineEdit *>(key);
111 value = lineEdit->text();
113 qWarning() << "Could not find child widget for field" << key;
117 qWarning() << "Unsupported type for backend property" << key;
119 properties[key] = std::move(value);
127 CoreConfigWizard::CoreConfigWizard(CoreConnection *connection, const QVariantList &backendInfos, const QVariantList &authInfos, QWidget *parent)
129 _connection{connection}
132 setAttribute(Qt::WA_DeleteOnClose);
134 setPage(IntroPage, new CoreConfigWizardPages::IntroPage(this));
135 setPage(AdminUserPage, new CoreConfigWizardPages::AdminUserPage(this));
136 setPage(AuthenticationSelectionPage, new CoreConfigWizardPages::AuthenticationSelectionPage(authInfos, this));
137 setPage(StorageSelectionPage, new CoreConfigWizardPages::StorageSelectionPage(backendInfos, this));
138 syncPage = new CoreConfigWizardPages::SyncPage(this);
139 connect(syncPage, SIGNAL(setupCore(const QString &, const QVariantMap &, const QString &, const QVariantMap &)),
140 SLOT(prepareCoreSetup(const QString &, const QVariantMap &, const QString &, const QVariantMap &)));
141 setPage(SyncPage, syncPage);
142 syncRelayPage = new CoreConfigWizardPages::SyncRelayPage(this);
143 connect(syncRelayPage, SIGNAL(startOver()), this, SLOT(startOver()));
144 setPage(SyncRelayPage, syncRelayPage);
146 setStartId(IntroPage);
149 setWizardStyle(ModernStyle);
152 setOption(HaveHelpButton, false);
153 setOption(NoBackButtonOnStartPage, true);
154 setOption(HaveNextButtonOnLastPage, false);
155 setOption(HaveFinishButtonOnEarlyPages, false);
156 setOption(NoCancelButton, true);
157 setOption(IndependentPages, true);
161 setWindowTitle(tr("Core Configuration Wizard"));
162 setPixmap(QWizard::LogoPixmap, QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png")).pixmap(48));
164 connect(connection, SIGNAL(coreSetupSuccess()), SLOT(coreSetupSuccess()));
165 connect(connection, SIGNAL(coreSetupFailed(QString)), SLOT(coreSetupFailed(QString)));
166 connect(connection, SIGNAL(synchronized()), SLOT(syncFinished()));
167 connect(this, SIGNAL(rejected()), connection, SLOT(disconnectFromCore()));
170 // Resize all pages to the size hint of the largest one, so the wizard is large enough
172 for (int id : pageIds()) {
175 maxSize = maxSize.expandedTo(p->sizeHint());
177 for (int id : pageIds()) {
178 page(id)->setFixedSize(maxSize);
183 void CoreConfigWizard::prepareCoreSetup(const QString &backend, const QVariantMap &properties, const QString &authenticator, const QVariantMap &authProperties)
185 // Prevent the user from changing any settings he already specified...
186 for (auto &&idx : visitedPages())
187 page(idx)->setEnabled(false);
189 // FIXME? We need to be able to set up older cores that don't have auth backend support.
190 // So if the core doesn't support that feature, don't pass those parameters.
191 if (!(Client::coreFeatures() & Quassel::Authenticators)) {
192 coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties));
195 coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties, authenticator, authProperties));
200 void CoreConfigWizard::coreSetupSuccess()
202 syncPage->setStatus(tr("Your core has been successfully configured. Logging you in..."));
203 syncPage->setError(false);
204 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Success);
205 coreConnection()->loginToCore(field("adminUser.user").toString(), field("adminUser.password").toString(), field("adminUser.rememberPasswd").toBool());
209 void CoreConfigWizard::coreSetupFailed(const QString &error)
211 syncPage->setStatus(tr("Core configuration failed:<br><b>%1</b><br>Press <em>Next</em> to start over.").arg(error));
212 syncPage->setError(true);
213 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
214 //foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
215 //setStartId(SyncPage);
220 void CoreConfigWizard::startOver()
222 foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
223 setStartId(CoreConfigWizard::AdminUserPage);
228 void CoreConfigWizard::syncFinished()
234 namespace CoreConfigWizardPages {
237 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent)
240 setTitle(tr("Introduction"));
241 //setSubTitle(tr("foobar"));
242 //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
246 int IntroPage::nextId() const
248 return CoreConfigWizard::AdminUserPage;
252 /*** Admin User Page ***/
254 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent)
257 setTitle(tr("Create Admin User"));
258 setSubTitle(tr("First, we will create a user on the core. This first user will have administrator privileges."));
260 registerField("adminUser.user*", ui.user);
261 registerField("adminUser.password*", ui.password);
262 registerField("adminUser.password2*", ui.password2);
263 registerField("adminUser.rememberPasswd", ui.rememberPasswd);
267 int AdminUserPage::nextId() const
269 // If the core doesn't support auth backends, skip that page!
270 if (!(Client::coreFeatures() & Quassel::Authenticators)) {
271 return CoreConfigWizard::StorageSelectionPage;
274 return CoreConfigWizard::AuthenticationSelectionPage;
279 bool AdminUserPage::isComplete() const
281 bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
285 /*** Authentication Selection Page ***/
287 AuthenticationSelectionPage::AuthenticationSelectionPage(const QVariantList &authInfos, QWidget *parent)
288 : QWizardPage(parent)
292 setTitle(tr("Select Authentication Backend"));
293 setSubTitle(tr("Please select a backend for Quassel Core to use for authenticating users."));
295 registerField("authentication.backend", ui.backendList);
297 for (auto &&authInfo : authInfos) {
298 auto props = authInfo.toMap();
299 // Extract field infos to avoid having to reparse the list
300 std::vector<FieldInfo> fields;
301 const auto &list = props["SetupData"].toList();
302 for (int i = 0; i + 2 < list.size(); i += 3) {
303 fields.emplace_back(std::make_tuple(list[i].toString(), list[i+1].toString(), list[i+2]));
305 props.remove("SetupData");
307 _authProperties.emplace_back(std::move(props));
308 _authFields.emplace_back(std::move(fields));
311 ui.backendList->addItem(_authProperties.back()["DisplayName"].toString(), _authProperties.back()["BackendId"].toString());
312 ui.descriptionStack->addWidget(createDescriptionBox(_authProperties.back()["Description"].toString()));
313 ui.authSettingsStack->addWidget(createFieldBox(tr("Authentication Settings"), _authFields.back()));
316 // Do some trickery to make the page large enough
317 setSizePolicy({QSizePolicy::Fixed, QSizePolicy::Fixed});
319 QSizePolicy sp{QSizePolicy::MinimumExpanding, QSizePolicy::Fixed};
320 #if QT_VERSION >= 0x050200
321 sp.setRetainSizeWhenHidden(true);
323 ui.authSettingsStack->setVisible(true); // ugly hack that will show an empty box, but we'll deprecate Qt4 soon anyway
325 ui.descriptionStack->setSizePolicy(sp);
326 ui.authSettingsStack->setSizePolicy(sp);
328 ui.descriptionStack->adjustSize();
329 ui.authSettingsStack->adjustSize();
331 ui.backendList->setCurrentIndex(0);
335 int AuthenticationSelectionPage::nextId() const
337 return CoreConfigWizard::StorageSelectionPage;
341 QString AuthenticationSelectionPage::displayName() const
343 return ui.backendList->currentText();
347 QString AuthenticationSelectionPage::authenticator() const
349 #if QT_VERSION >= 0x050200
350 return ui.backendList->currentData().toString();
352 return ui.backendList->itemData(ui.backendList->currentIndex()).toString();
357 QVariantMap AuthenticationSelectionPage::authProperties() const
359 return propertiesFromFieldWidgets(qobject_cast<QGroupBox *>(ui.authSettingsStack->currentWidget()),
360 _authFields[ui.backendList->currentIndex()]);
364 void AuthenticationSelectionPage::on_backendList_currentIndexChanged(int index)
366 ui.descriptionStack->setCurrentIndex(index);
367 ui.authSettingsStack->setCurrentIndex(index);
368 ui.authSettingsStack->setVisible(!_authFields[index].empty());
371 /*** Storage Selection Page ***/
373 StorageSelectionPage::StorageSelectionPage(const QVariantList &backendInfos, QWidget *parent)
374 : QWizardPage(parent)
378 setTitle(tr("Select Storage Backend"));
379 setSubTitle(tr("Please select a storage backend for Quassel Core."));
382 registerField("storage.backend", ui.backendList);
384 int defaultIndex {0}; // Legacy cores send backend infos in arbitrary order
386 for (auto &&backendInfo : backendInfos) {
387 auto props = backendInfo.toMap();
388 // Extract field infos to avoid having to reparse the list
389 std::vector<FieldInfo> fields;
391 // Legacy cores (prior to 0.13) didn't send SetupData for storage backends; deal with this
392 if (!props.contains("SetupData")) {
393 const auto &defaultValues = props["SetupDefaults"].toMap();
394 for (auto &&key : props["SetupKeys"].toStringList()) {
395 fields.emplace_back(std::make_tuple(key, key, defaultValues.value(key, QString{})));
397 if (props.value("IsDefault", false).toBool()) {
398 defaultIndex = ui.backendList->count();
402 const auto &list = props["SetupData"].toList();
403 for (int i = 0; i + 2 < list.size(); i += 3) {
404 fields.emplace_back(std::make_tuple(list[i].toString(), list[i+1].toString(), list[i+2]));
406 props.remove("SetupData");
408 props.remove("SetupKeys");
409 props.remove("SetupDefaults");
410 // Legacy cores (prior to 0.13) don't send the BackendId property
411 if (!props.contains("BackendId"))
412 props["BackendId"] = props["DisplayName"];
413 _backendProperties.emplace_back(std::move(props));
414 _backendFields.emplace_back(std::move(fields));
417 ui.backendList->addItem(_backendProperties.back()["DisplayName"].toString(), _backendProperties.back()["BackendId"].toString());
418 ui.descriptionStack->addWidget(createDescriptionBox(_backendProperties.back()["Description"].toString()));
419 ui.storageSettingsStack->addWidget(createFieldBox(tr("Storage Settings"), _backendFields.back()));
422 // Do some trickery to make the page large enough
423 setSizePolicy({QSizePolicy::Fixed, QSizePolicy::Fixed});
425 QSizePolicy sp{QSizePolicy::MinimumExpanding, QSizePolicy::Fixed};
426 #if QT_VERSION >= 0x050200
427 sp.setRetainSizeWhenHidden(true);
429 ui.storageSettingsStack->setVisible(true); // ugly hack that will show an empty box, but we'll deprecate Qt4 soon anyway
431 ui.descriptionStack->setSizePolicy(sp);
432 ui.storageSettingsStack->setSizePolicy(sp);
434 ui.descriptionStack->adjustSize();
435 ui.storageSettingsStack->adjustSize();
437 ui.backendList->setCurrentIndex(defaultIndex);
441 int StorageSelectionPage::nextId() const
443 return CoreConfigWizard::SyncPage;
447 QString StorageSelectionPage::displayName() const
449 return ui.backendList->currentText();
453 QString StorageSelectionPage::backend() const
455 #if QT_VERSION >= 0x050200
456 return ui.backendList->currentData().toString();
458 return ui.backendList->itemData(ui.backendList->currentIndex()).toString();
463 QVariantMap StorageSelectionPage::backendProperties() const
465 return propertiesFromFieldWidgets(qobject_cast<QGroupBox *>(ui.storageSettingsStack->currentWidget()),
466 _backendFields[ui.backendList->currentIndex()]);
470 void StorageSelectionPage::on_backendList_currentIndexChanged(int index)
472 ui.descriptionStack->setCurrentIndex(index);
473 ui.storageSettingsStack->setCurrentIndex(index);
474 ui.storageSettingsStack->setVisible(!_backendFields[index].empty());
480 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent)
483 setTitle(tr("Storing Your Settings"));
484 setSubTitle(tr("Your settings are now being stored in the core, and you will be logged in automatically."));
488 void SyncPage::initializePage()
492 emit completeChanged();
494 // Fill in sync info about the storage layer.
495 StorageSelectionPage *storagePage = qobject_cast<StorageSelectionPage *>(wizard()->page(CoreConfigWizard::StorageSelectionPage));
496 QString backend = storagePage->backend();
497 QVariantMap backendProperties = storagePage->backendProperties();
498 ui.backend->setText(storagePage->displayName());
500 // Fill in sync info about the authentication layer.
501 AuthenticationSelectionPage *authPage = qobject_cast<AuthenticationSelectionPage *>(wizard()->page(CoreConfigWizard::AuthenticationSelectionPage));
502 QString authenticator = authPage->authenticator();
503 QVariantMap authProperties = authPage->authProperties();
504 ui.authenticator->setText(authPage->displayName());
506 ui.user->setText(wizard()->field("adminUser.user").toString());
508 emit setupCore(backend, backendProperties, authenticator, authProperties);
512 int SyncPage::nextId() const
516 return CoreConfigWizard::SyncRelayPage;
520 bool SyncPage::isComplete() const
522 return _complete || _hasError;
526 void SyncPage::setStatus(const QString &status)
528 ui.status->setText(status);
532 void SyncPage::setError(bool e)
536 emit completeChanged();
540 void SyncPage::setComplete(bool c)
547 /*** Sync Relay Page ***/
549 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent)
555 void SyncRelayPage::setMode(Mode m)
560 int SyncRelayPage::nextId() const
565 }; /* namespace CoreConfigWizardPages */