1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <QAbstractButton>
23 #include <QFormLayout>
26 #include "coreconfigwizard.h"
27 #include "iconloader.h"
29 CoreConfigWizard::CoreConfigWizard(const QList<QVariant> &backends, QWidget *parent) : QWizard(parent) {
30 foreach(QVariant v, backends) _backends[v.toMap()["DisplayName"].toString()] = v;
31 setPage(IntroPage, new CoreConfigWizardPages::IntroPage(this));
32 setPage(AdminUserPage, new CoreConfigWizardPages::AdminUserPage(this));
33 setPage(StorageSelectionPage, new CoreConfigWizardPages::StorageSelectionPage(_backends, this));
34 syncPage = new CoreConfigWizardPages::SyncPage(this);
35 connect(syncPage, SIGNAL(setupCore(const QString &, const QVariantMap &)), this, SLOT(prepareCoreSetup(const QString &, const QVariantMap &)));
36 setPage(SyncPage, syncPage);
37 syncRelayPage = new CoreConfigWizardPages::SyncRelayPage(this);
38 connect(syncRelayPage, SIGNAL(startOver()), this, SLOT(startOver()));
39 setPage(SyncRelayPage, syncRelayPage);
40 //setPage(Page_StorageDetails, new StorageDetailsPage());
41 //setPage(Page_Conclusion, new ConclusionPage(storageProviders));
43 setStartId(IntroPage);
44 //setStartId(StorageSelectionPage);
47 setWizardStyle(ModernStyle);
50 setOption(HaveHelpButton, false);
51 setOption(NoBackButtonOnStartPage, true);
52 setOption(HaveNextButtonOnLastPage, false);
53 setOption(HaveFinishButtonOnEarlyPages, false);
54 setOption(NoCancelButton, true);
55 setOption(IndependentPages, true);
56 //setOption(ExtendedWatermarkPixmap, true);
60 setWindowTitle(tr("Core Configuration Wizard"));
61 setPixmap(QWizard::LogoPixmap, DesktopIcon("quassel"));
64 QHash<QString, QVariant> CoreConfigWizard::backends() const {
68 void CoreConfigWizard::prepareCoreSetup(const QString &backend, const QVariantMap &properties) {
69 // Prevent the user from changing any settings he already specified...
70 foreach(int idx, visitedPages())
71 page(idx)->setEnabled(false);
74 foo["AdminUser"] = field("adminUser.user").toString();
75 foo["AdminPasswd"] = field("adminUser.password").toString();
76 foo["Backend"] = backend;
77 foo["ConnectionProperties"] = properties;
81 void CoreConfigWizard::coreSetupSuccess() {
82 syncPage->setStatus(tr("Your core has been successfully configured. Logging you in..."));
83 syncPage->setError(false);
84 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
85 QVariantMap loginData;
86 loginData["User"] = field("adminUser.user");
87 loginData["Password"] = field("adminUser.password");
88 loginData["RememberPasswd"] = field("adminUser.rememberPasswd");
89 emit loginToCore(loginData);
92 void CoreConfigWizard::coreSetupFailed(const QString &error) {
93 syncPage->setStatus(tr("Core configuration failed:<br><b>%1</b><br>Press <em>Next</em> to start over.").arg(error));
94 syncPage->setError(true);
95 syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
96 //foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
97 //setStartId(SyncPage);
101 void CoreConfigWizard::startOver() {
102 foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
103 setStartId(CoreConfigWizard::AdminUserPage);
107 void CoreConfigWizard::loginSuccess() {
108 syncPage->setStatus(tr("Your are now logged into your freshly configured Quassel Core!<br>"
109 "Please remember to configure your identities and networks now."));
110 syncPage->setComplete(true);
111 syncPage->setFinalPage(true);
114 void CoreConfigWizard::syncFinished() {
115 // TODO: display identities and networks settings if appropriate!
119 namespace CoreConfigWizardPages {
123 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent) {
125 setTitle(tr("Introduction"));
126 //setSubTitle(tr("foobar"));
127 //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
131 int IntroPage::nextId() const {
132 return CoreConfigWizard::AdminUserPage;
136 /*** Admin User Page ***/
138 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent) {
140 setTitle(tr("Create Admin User"));
141 setSubTitle(tr("First, we will create a user on the core. This first user will have administrator privileges."));
143 registerField("adminUser.user*", ui.user);
144 registerField("adminUser.password*", ui.password);
145 registerField("adminUser.password2*", ui.password2);
146 registerField("adminUser.rememberPasswd", ui.rememberPasswd);
148 //ui.user->setText("foo");
149 //ui.password->setText("foo");
150 //ui.password2->setText("foo");
153 int AdminUserPage::nextId() const {
154 return CoreConfigWizard::StorageSelectionPage;
158 bool AdminUserPage::isComplete() const {
159 bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
163 /*** Storage Selection Page ***/
165 StorageSelectionPage::StorageSelectionPage(const QHash<QString, QVariant> &backends, QWidget *parent) : QWizardPage(parent) {
167 _backends = backends;
169 setTitle(tr("Select Storage Backend"));
170 setSubTitle(tr("Please select a database backend for the Quassel Core storage to store the backlog and other data in."));
173 registerField("storage.backend", ui.backendList);
175 foreach(QString key, _backends.keys()) {
176 ui.backendList->addItem(_backends[key].toMap()["DisplayName"].toString(), key);
179 on_backendList_currentIndexChanged();
182 int StorageSelectionPage::nextId() const {
183 return CoreConfigWizard::SyncPage;
186 QString StorageSelectionPage::selectedBackend() const {
187 return ui.backendList->currentText();
190 QVariantMap StorageSelectionPage::connectionProperties() const {
191 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
192 QVariantMap properties = _backends[backend].toMap()["ConnectionProperties"].toMap();
193 if(!properties.isEmpty() && _connectionBox) {
194 QVariantMap::iterator propertyIter = properties.begin();
195 while(propertyIter != properties.constEnd()) {
196 QWidget *widget = _connectionBox->findChild<QWidget *>(propertyIter.key());
197 switch(propertyIter.value().type()) {
200 QSpinBox *spinbox = qobject_cast<QSpinBox *>(widget);
202 propertyIter.value() = QVariant(spinbox->value());
207 QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
209 propertyIter.value() = QVariant(lineEdit->text());
218 void StorageSelectionPage::on_backendList_currentIndexChanged() {
219 QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
220 ui.description->setText(_backends[backend].toMap()["Description"].toString());
223 layout()->removeWidget(_connectionBox);
224 _connectionBox->deleteLater();
228 QVariantMap properties = _backends[backend].toMap()["ConnectionProperties"].toMap();
229 if(!properties.isEmpty()) {
230 QGroupBox *propertyBox = new QGroupBox(this);
231 propertyBox->setTitle(tr("Connection Properties"));
232 QFormLayout *formlayout = new QFormLayout;
234 QVariantMap::const_iterator propertyIter = properties.constBegin();
235 while(propertyIter != properties.constEnd()) {
237 switch(propertyIter.value().type()) {
240 QSpinBox *spinbox = new QSpinBox(propertyBox);
241 spinbox->setMaximum(64000);
242 spinbox->setValue(propertyIter.value().toInt());
247 widget = new QLineEdit(propertyIter.value().toString(), propertyBox);
249 widget->setObjectName(propertyIter.key());
250 formlayout->addRow(propertyIter.key() + ":", widget);
253 propertyBox->setLayout(formlayout);
254 static_cast<QVBoxLayout *>(layout())->insertWidget(layout()->indexOf(ui.descriptionBox) + 1, propertyBox);
255 _connectionBox = propertyBox;
261 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent) {
263 setTitle(tr("Storing Your Settings"));
264 setSubTitle(tr("Your settings are now stored in the core, and you will be logged in automatically."));
267 void SyncPage::initializePage() {
271 StorageSelectionPage *storagePage = qobject_cast<StorageSelectionPage *>(wizard()->page(CoreConfigWizard::StorageSelectionPage));
272 QString backend = storagePage->selectedBackend();
273 QVariantMap properties = storagePage->connectionProperties();
274 Q_ASSERT(!backend.isEmpty());
275 ui.user->setText(wizard()->field("adminUser.user").toString());
276 ui.backend->setText(backend);
277 emit setupCore(backend, properties);
280 int SyncPage::nextId() const {
281 if(!hasError) return -1;
282 return CoreConfigWizard::SyncRelayPage;
285 bool SyncPage::isComplete() const {
289 void SyncPage::setStatus(const QString &status) {
290 ui.status->setText(status);
293 void SyncPage::setError(bool e) {
297 void SyncPage::setComplete(bool c) {
302 /*** Sync Relay Page ***/
304 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent) {
308 void SyncRelayPage::setMode(Mode m) {
313 void SyncRelayPage::initializePage() {
315 if(mode == Success) {
323 int SyncRelayPage::nextId() const {
328 }; /* namespace CoreConfigWizardPages */