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 <QFormLayout>
27 #include "coreconfigwizard.h"
28 #include "coreconnection.h"
32 CoreConfigWizard::CoreConfigWizard(CoreConnection *connection, const QList<QVariant> &backends, const QList<QVariant> &authenticators, QWidget *parent)
34 _connection(connection)
37 setAttribute(Qt::WA_DeleteOnClose);
39 foreach(const QVariant &v, backends)
40 _backends[v.toMap()["DisplayName"].toString()] = v;
42 foreach(const QVariant &v, authenticators)
43 _authenticators[v.toMap()["BackendId"].toString()] = v;
45 setPage(IntroPage, new CoreConfigWizardPages::IntroPage(this));
46 setPage(AdminUserPage, new CoreConfigWizardPages::AdminUserPage(this));
47 setPage(AuthenticationSelectionPage, new CoreConfigWizardPages::AuthenticationSelectionPage(_authenticators, this));
48 setPage(StorageSelectionPage, new CoreConfigWizardPages::StorageSelectionPage(_backends, this));
49 syncPage = new CoreConfigWizardPages::SyncPage(this);
50 connect(syncPage, SIGNAL(setupCore(const QString &, const QVariantMap &, const QString &, const QVariantMap &)),
51 SLOT(prepareCoreSetup(const QString &, const QVariantMap &, const QString &, const QVariantMap &)));
52 setPage(SyncPage, syncPage);
53 syncRelayPage = new CoreConfigWizardPages::SyncRelayPage(this);
54 connect(syncRelayPage, SIGNAL(startOver()), this, SLOT(startOver()));
55 setPage(SyncRelayPage, syncRelayPage);
56 //setPage(Page_StorageDetails, new StorageDetailsPage());
57 //setPage(Page_Conclusion, new ConclusionPage(storageProviders));
59 setStartId(IntroPage);
60 //setStartId(StorageSelectionPage);
63 setWizardStyle(ModernStyle);
66 setOption(HaveHelpButton, false);
67 setOption(NoBackButtonOnStartPage, true);
68 setOption(HaveNextButtonOnLastPage, false);
69 setOption(HaveFinishButtonOnEarlyPages, false);
70 setOption(NoCancelButton, true);
71 setOption(IndependentPages, true);
72 //setOption(ExtendedWatermarkPixmap, true);
76 setWindowTitle(tr("Core Configuration Wizard"));
77 setPixmap(QWizard::LogoPixmap, QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png")).pixmap(48));
79 connect(connection, SIGNAL(coreSetupSuccess()), SLOT(coreSetupSuccess()));
80 connect(connection, SIGNAL(coreSetupFailed(QString)), SLOT(coreSetupFailed(QString)));
81 //connect(connection, SIGNAL(loginSuccess()), SLOT(loginSuccess()));
82 connect(connection, SIGNAL(synchronized()), SLOT(syncFinished()));
83 connect(this, SIGNAL(rejected()), connection, SLOT(disconnectFromCore()));
87 QHash<QString, QVariant> CoreConfigWizard::backends() const
93 QHash<QString, QVariant> CoreConfigWizard::authenticators() const
95 return _authenticators;
99 void CoreConfigWizard::prepareCoreSetup(const QString &backend, const QVariantMap &properties, const QString &authenticator, const QVariantMap &authProperties)
101 // Prevent the user from changing any settings he already specified...
102 foreach(int idx, visitedPages())
103 page(idx)->setEnabled(false);
105 // FIXME? We need to be able to set up older cores that don't have auth backend support.
106 // So if the core doesn't support that feature, don't pass those parameters.
107 if (!(Client::coreFeatures() & Quassel::Authenticators)) {
108 coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties));
111 coreConnection()->setupCore(Protocol::SetupData(field("adminUser.user").toString(), field("adminUser.password").toString(), backend, properties, authenticator, authProperties));
116 void CoreConfigWizard::coreSetupSuccess()
118 syncPage->setStatus(tr("Your core has been successfully configured. Logging you in..."));
119 syncPage->setError(false);
120 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
121 coreConnection()->loginToCore(field("adminUser.user").toString(), field("adminUser.password").toString(), field("adminUser.rememberPasswd").toBool());
125 void CoreConfigWizard::coreSetupFailed(const QString &error)
127 syncPage->setStatus(tr("Core configuration failed:<br><b>%1</b><br>Press <em>Next</em> to start over.").arg(error));
128 syncPage->setError(true);
129 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
130 //foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
131 //setStartId(SyncPage);
136 void CoreConfigWizard::startOver()
138 foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
139 setStartId(CoreConfigWizard::AdminUserPage);
144 void CoreConfigWizard::loginSuccess()
146 syncPage->setStatus(tr("Your are now logged into your freshly configured Quassel Core!<br>"
147 "Please remember to configure your identities and networks now."));
148 syncPage->setComplete(true);
149 syncPage->setFinalPage(true);
153 void CoreConfigWizard::syncFinished()
159 namespace CoreConfigWizardPages {
162 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent)
165 setTitle(tr("Introduction"));
166 //setSubTitle(tr("foobar"));
167 //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
171 int IntroPage::nextId() const
173 return CoreConfigWizard::AdminUserPage;
177 /*** Admin User Page ***/
179 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent)
182 setTitle(tr("Create Admin User"));
183 setSubTitle(tr("First, we will create a user on the core. This first user will have administrator privileges."));
185 registerField("adminUser.user*", ui.user);
186 registerField("adminUser.password*", ui.password);
187 registerField("adminUser.password2*", ui.password2);
188 registerField("adminUser.rememberPasswd", ui.rememberPasswd);
190 //ui.user->setText("foo");
191 //ui.password->setText("foo");
192 //ui.password2->setText("foo");
196 int AdminUserPage::nextId() const
198 // If the core doesn't support auth backends, skip that page!
199 if (!(Client::coreFeatures() & Quassel::Authenticators)) {
200 return CoreConfigWizard::StorageSelectionPage;
203 return CoreConfigWizard::AuthenticationSelectionPage;
208 bool AdminUserPage::isComplete() const
210 bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
214 /*** Authentication Selection Page ***/
216 AuthenticationSelectionPage::AuthenticationSelectionPage(const QHash<QString, QVariant> &backends, QWidget *parent)
217 : QWizardPage(parent),
223 setTitle(tr("Select Authentication Backend"));
224 setSubTitle(tr("Please select a backend for Quassel Core to use for authenticating users."));
226 registerField("authentication.backend", ui.backendList);
228 foreach(QString key, _backends.keys()) {
229 ui.backendList->addItem(_backends[key].toMap()["DisplayName"].toString(), key);
232 on_backendList_currentIndexChanged();
236 int AuthenticationSelectionPage::nextId() const
238 return CoreConfigWizard::StorageSelectionPage;
242 QString AuthenticationSelectionPage::selectedBackend() const
244 return ui.backendList->currentText();
248 QVariantMap AuthenticationSelectionPage::authProperties() const
250 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
252 QVariantMap properties;
253 QStringList setupKeys = _backends[backend].toMap()["SetupKeys"].toStringList();
254 if (!setupKeys.isEmpty()) {
255 QVariantMap defaults = _backends[backend].toMap()["SetupDefaults"].toMap();
256 foreach(QString key, setupKeys) {
257 QWidget *widget = _connectionBox->findChild<QWidget *>(key);
259 if (defaults.contains(key)) {
262 switch (def.type()) {
265 QSpinBox *spinbox = qobject_cast<QSpinBox *>(widget);
267 def = QVariant(spinbox->value());
272 QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
274 def = QVariant(lineEdit->text());
277 properties[key] = def;
284 void AuthenticationSelectionPage::on_backendList_currentIndexChanged()
286 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
287 ui.description->setText(_backends[backend].toMap()["Description"].toString());
289 if (_connectionBox) {
290 layout()->removeWidget(_connectionBox);
291 _connectionBox->deleteLater();
295 QStringList setupKeys = _backends[backend].toMap()["SetupKeys"].toStringList();
296 if (!setupKeys.isEmpty()) {
297 QVariantMap defaults = _backends[backend].toMap()["SetupDefaults"].toMap();
298 QGroupBox *propertyBox = new QGroupBox(this);
299 propertyBox->setTitle(tr("Connection Properties"));
300 QFormLayout *formlayout = new QFormLayout;
302 foreach(QString key, setupKeys) {
305 if (defaults.contains(key)) {
308 switch (def.type()) {
311 QSpinBox *spinbox = new QSpinBox(propertyBox);
312 spinbox->setMaximum(64000);
313 spinbox->setValue(def.toInt());
319 QLineEdit *lineEdit = new QLineEdit(def.toString(), propertyBox);
320 if (key.toLower().contains("password")) {
321 lineEdit->setEchoMode(QLineEdit::Password);
326 widget->setObjectName(key);
327 formlayout->addRow(key + ":", widget);
329 propertyBox->setLayout(formlayout);
330 static_cast<QVBoxLayout *>(layout())->insertWidget(layout()->indexOf(ui.descriptionBox) + 1, propertyBox);
331 _connectionBox = propertyBox;
335 /*** Storage Selection Page ***/
337 StorageSelectionPage::StorageSelectionPage(const QHash<QString, QVariant> &backends, QWidget *parent)
338 : QWizardPage(parent),
344 setTitle(tr("Select Storage Backend"));
345 setSubTitle(tr("Please select a database backend for the Quassel Core storage to store the backlog and other data in."));
348 registerField("storage.backend", ui.backendList);
350 int defaultIndex = 0;
351 foreach(QString key, _backends.keys()) {
352 ui.backendList->addItem(_backends[key].toMap()["DisplayName"].toString(), key);
353 if (_backends[key].toMap()["IsDefault"].toBool()) {
354 defaultIndex = ui.backendList->count() - 1;
358 ui.backendList->setCurrentIndex(defaultIndex);
360 on_backendList_currentIndexChanged();
364 int StorageSelectionPage::nextId() const
366 return CoreConfigWizard::SyncPage;
370 QString StorageSelectionPage::selectedBackend() const
372 return ui.backendList->currentText();
376 QVariantMap StorageSelectionPage::connectionProperties() const
378 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
380 QVariantMap properties;
381 QStringList setupKeys = _backends[backend].toMap()["SetupKeys"].toStringList();
382 if (!setupKeys.isEmpty()) {
383 QVariantMap defaults = _backends[backend].toMap()["SetupDefaults"].toMap();
384 foreach(QString key, setupKeys) {
385 QWidget *widget = _connectionBox->findChild<QWidget *>(key);
387 if (defaults.contains(key)) {
390 switch (def.type()) {
393 QSpinBox *spinbox = qobject_cast<QSpinBox *>(widget);
395 def = QVariant(spinbox->value());
400 QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
402 def = QVariant(lineEdit->text());
405 properties[key] = def;
408 qDebug() << properties;
410 // QVariantMap properties = _backends[backend].toMap()["ConnectionProperties"].toMap();
411 // if(!properties.isEmpty() && _connectionBox) {
412 // QVariantMap::iterator propertyIter = properties.begin();
413 // while(propertyIter != properties.constEnd()) {
414 // QWidget *widget = _connectionBox->findChild<QWidget *>(propertyIter.key());
415 // switch(propertyIter.value().type()) {
416 // case QVariant::Int:
418 // QSpinBox *spinbox = qobject_cast<QSpinBox *>(widget);
419 // Q_ASSERT(spinbox);
420 // propertyIter.value() = QVariant(spinbox->value());
425 // QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
426 // Q_ASSERT(lineEdit);
427 // propertyIter.value() = QVariant(lineEdit->text());
437 void StorageSelectionPage::on_backendList_currentIndexChanged()
439 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
440 ui.description->setText(_backends[backend].toMap()["Description"].toString());
442 if (_connectionBox) {
443 layout()->removeWidget(_connectionBox);
444 _connectionBox->deleteLater();
448 QStringList setupKeys = _backends[backend].toMap()["SetupKeys"].toStringList();
449 if (!setupKeys.isEmpty()) {
450 QVariantMap defaults = _backends[backend].toMap()["SetupDefaults"].toMap();
451 QGroupBox *propertyBox = new QGroupBox(this);
452 propertyBox->setTitle(tr("Connection Properties"));
453 QFormLayout *formlayout = new QFormLayout;
455 foreach(QString key, setupKeys) {
458 if (defaults.contains(key)) {
461 switch (def.type()) {
464 QSpinBox *spinbox = new QSpinBox(propertyBox);
465 spinbox->setMaximum(64000);
466 spinbox->setValue(def.toInt());
472 QLineEdit *lineEdit = new QLineEdit(def.toString(), propertyBox);
473 if (key.toLower().contains("password")) {
474 lineEdit->setEchoMode(QLineEdit::Password);
479 widget->setObjectName(key);
480 formlayout->addRow(key + ":", widget);
482 propertyBox->setLayout(formlayout);
483 static_cast<QVBoxLayout *>(layout())->insertWidget(layout()->indexOf(ui.descriptionBox) + 1, propertyBox);
484 _connectionBox = propertyBox;
491 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent)
494 setTitle(tr("Storing Your Settings"));
495 setSubTitle(tr("Your settings are now stored in the core, and you will be logged in automatically."));
499 void SyncPage::initializePage()
504 // Fill in sync info about the storage layer.
505 StorageSelectionPage *storagePage = qobject_cast<StorageSelectionPage *>(wizard()->page(CoreConfigWizard::StorageSelectionPage));
506 QString backend = storagePage->selectedBackend();
507 QVariantMap properties = storagePage->connectionProperties();
508 Q_ASSERT(!backend.isEmpty());
509 ui.backend->setText(backend);
511 // Fill in sync info about the authentication layer.
512 AuthenticationSelectionPage *authPage = qobject_cast<AuthenticationSelectionPage *>(wizard()->page(CoreConfigWizard::AuthenticationSelectionPage));
513 QString authenticator = authPage->selectedBackend();
514 QVariantMap authProperties = authPage->authProperties();
515 Q_ASSERT(!authenticator.isEmpty());
516 ui.authenticator->setText(authenticator);
518 ui.user->setText(wizard()->field("adminUser.user").toString());
520 emit setupCore(backend, properties, authenticator, authProperties);
524 int SyncPage::nextId() const
526 if (!hasError) return -1;
527 return CoreConfigWizard::SyncRelayPage;
531 bool SyncPage::isComplete() const
537 void SyncPage::setStatus(const QString &status)
539 ui.status->setText(status);
543 void SyncPage::setError(bool e)
549 void SyncPage::setComplete(bool c)
556 /*** Sync Relay Page ***/
558 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent)
564 void SyncRelayPage::setMode(Mode m)
571 void SyncRelayPage::initializePage() {
573 if(mode == Success) {
581 int SyncRelayPage::nextId() const
586 }; /* namespace CoreConfigWizardPages */