030af57463eae0bb0503e7dad3f5c440fc40e10d
[quassel.git] / src / qtui / coreconfigwizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QDebug>
22 #include <QAbstractButton>
23 #include <QFormLayout>
24 #include <QSpinBox>
25
26 #include "coreconfigwizard.h"
27 #include "iconloader.h"
28
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));
42
43   setStartId(IntroPage);
44   //setStartId(StorageSelectionPage);
45
46 #ifndef Q_WS_MAC
47   setWizardStyle(ModernStyle);
48 #endif
49
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);
57
58   setModal(true);
59
60   setWindowTitle(tr("Core Configuration Wizard"));
61   setPixmap(QWizard::LogoPixmap, DesktopIcon("quassel"));
62 }
63
64 QHash<QString, QVariant> CoreConfigWizard::backends() const {
65   return _backends;
66 }
67
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);
72
73   QVariantMap foo;
74   foo["AdminUser"] = field("adminUser.user").toString();
75   foo["AdminPasswd"] = field("adminUser.password").toString();
76   foo["Backend"] = backend;
77   foo["ConnectionProperties"] = properties;
78   emit setupCore(foo);
79 }
80
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);
90 }
91
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);
98   //restart();
99 }
100
101 void CoreConfigWizard::startOver() {
102   foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
103   setStartId(CoreConfigWizard::AdminUserPage);
104   restart();
105 }
106
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);
112 }
113
114 void CoreConfigWizard::syncFinished() {
115   // TODO: display identities and networks settings if appropriate!
116   // accept();
117 }
118
119 namespace CoreConfigWizardPages {
120
121 /*** Intro Page ***/
122
123 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent) {
124   ui.setupUi(this);
125   setTitle(tr("Introduction"));
126   //setSubTitle(tr("foobar"));
127   //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
128
129 }
130
131 int IntroPage::nextId() const {
132   return CoreConfigWizard::AdminUserPage;
133
134 }
135
136 /*** Admin User Page ***/
137
138 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent) {
139   ui.setupUi(this);
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."));
142
143   registerField("adminUser.user*", ui.user);
144   registerField("adminUser.password*", ui.password);
145   registerField("adminUser.password2*", ui.password2);
146   registerField("adminUser.rememberPasswd", ui.rememberPasswd);
147
148   //ui.user->setText("foo");
149   //ui.password->setText("foo");
150   //ui.password2->setText("foo");
151 }
152
153 int AdminUserPage::nextId() const {
154   return CoreConfigWizard::StorageSelectionPage;
155
156 }
157
158 bool AdminUserPage::isComplete() const {
159   bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
160   return ok;
161 }
162
163 /*** Storage Selection Page ***/
164
165 StorageSelectionPage::StorageSelectionPage(const QHash<QString, QVariant> &backends, QWidget *parent) : QWizardPage(parent) {
166   ui.setupUi(this);
167   _backends = backends;
168
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."));
171   setCommitPage(true);
172
173   registerField("storage.backend", ui.backendList);
174
175   foreach(QString key, _backends.keys()) {
176     ui.backendList->addItem(_backends[key].toMap()["DisplayName"].toString(), key);
177   }
178
179   on_backendList_currentIndexChanged();
180 }
181
182 int StorageSelectionPage::nextId() const {
183   return CoreConfigWizard::SyncPage;
184 }
185
186 QString StorageSelectionPage::selectedBackend() const {
187   return ui.backendList->currentText();
188 }
189
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()) {
198       case QVariant::Int:
199         {
200           QSpinBox *spinbox = qobject_cast<QSpinBox *>(widget);
201           Q_ASSERT(spinbox);
202           propertyIter.value() = QVariant(spinbox->value());
203         }
204         break;
205       default:
206         {
207           QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
208           Q_ASSERT(lineEdit);
209           propertyIter.value() = QVariant(lineEdit->text());
210         }
211       }
212       propertyIter++;
213     }
214   }
215   return properties;
216 }
217
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());
221
222   if(_connectionBox) {
223     layout()->removeWidget(_connectionBox);
224     _connectionBox->deleteLater();
225     _connectionBox = 0;
226   }
227
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;
233
234     QVariantMap::const_iterator propertyIter = properties.constBegin();
235     while(propertyIter != properties.constEnd()) {
236       QWidget *widget = 0;
237       switch(propertyIter.value().type()) {
238       case QVariant::Int:
239         {
240           QSpinBox *spinbox = new QSpinBox(propertyBox);
241           spinbox->setMaximum(64000);
242           spinbox->setValue(propertyIter.value().toInt());
243           widget = spinbox;
244         }
245         break;
246       default:
247         widget = new QLineEdit(propertyIter.value().toString(), propertyBox);
248       }
249       widget->setObjectName(propertyIter.key());
250       formlayout->addRow(propertyIter.key() + ":", widget);
251       propertyIter++;
252     }
253     propertyBox->setLayout(formlayout);
254     static_cast<QVBoxLayout *>(layout())->insertWidget(layout()->indexOf(ui.descriptionBox) + 1, propertyBox);
255     _connectionBox = propertyBox;
256   }
257 }
258
259 /*** Sync Page ***/
260
261 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent) {
262   ui.setupUi(this);
263   setTitle(tr("Storing Your Settings"));
264   setSubTitle(tr("Your settings are now stored in the core, and you will be logged in automatically."));
265 }
266
267 void SyncPage::initializePage() {
268   complete = false;
269   hasError = false;
270
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);
278 }
279
280 int SyncPage::nextId() const {
281   if(!hasError) return -1;
282   return CoreConfigWizard::SyncRelayPage;
283 }
284
285 bool SyncPage::isComplete() const {
286   return complete;
287 }
288
289 void SyncPage::setStatus(const QString &status) {
290   ui.status->setText(status);
291 }
292
293 void SyncPage::setError(bool e) {
294   hasError = e;
295 }
296
297 void SyncPage::setComplete(bool c) {
298   complete = c;
299   completeChanged();
300 }
301
302 /*** Sync Relay Page ***/
303
304 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent) {
305   mode = Success;
306 }
307
308 void SyncRelayPage::setMode(Mode m) {
309   mode = m;
310 }
311
312 /*
313 void SyncRelayPage::initializePage() {
314   return;
315   if(mode == Success) {
316     wizard()->accept();
317   } else {
318     emit startOver();
319   }
320 }
321 */
322
323 int SyncRelayPage::nextId() const {
324   emit startOver();
325   return 0;
326 }
327
328 };  /* namespace CoreConfigWizardPages */