The WebPreviews are now controlled via a neat state machine
[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
24 #include "coreconfigwizard.h"
25 #include "iconloader.h"
26
27 CoreConfigWizard::CoreConfigWizard(const QList<QVariant> &backends, QWidget *parent) : QWizard(parent) {
28   foreach(QVariant v, backends) _backends[v.toMap()["DisplayName"].toString()] = v;
29   setPage(IntroPage, new CoreConfigWizardPages::IntroPage(this));
30   setPage(AdminUserPage, new CoreConfigWizardPages::AdminUserPage(this));
31   setPage(StorageSelectionPage, new CoreConfigWizardPages::StorageSelectionPage(_backends, this));
32   syncPage = new CoreConfigWizardPages::SyncPage(this);
33   connect(syncPage, SIGNAL(setupCore(const QString &)), this, SLOT(prepareCoreSetup(const QString &)));
34   setPage(SyncPage, syncPage);
35   syncRelayPage = new CoreConfigWizardPages::SyncRelayPage(this);
36   connect(syncRelayPage, SIGNAL(startOver()), this, SLOT(startOver()));
37   setPage(SyncRelayPage, syncRelayPage);
38   //setPage(Page_StorageDetails, new StorageDetailsPage());
39   //setPage(Page_Conclusion, new ConclusionPage(storageProviders));
40
41   setStartId(IntroPage);
42   //setStartId(StorageSelectionPage);
43
44 #ifndef Q_WS_MAC
45   setWizardStyle(ModernStyle);
46 #endif
47
48   setOption(HaveHelpButton, false);
49   setOption(NoBackButtonOnStartPage, true);
50   setOption(HaveNextButtonOnLastPage, false);
51   setOption(HaveFinishButtonOnEarlyPages, false);
52   setOption(NoCancelButton, true);
53   setOption(IndependentPages, true);
54   //setOption(ExtendedWatermarkPixmap, true);
55
56   setModal(true);
57
58   setWindowTitle(tr("Core Configuration Wizard"));
59   setPixmap(QWizard::LogoPixmap, DesktopIcon("quassel"));
60 }
61
62 QHash<QString, QVariant> CoreConfigWizard::backends() const {
63   return _backends;
64 }
65
66 void CoreConfigWizard::prepareCoreSetup(const QString &backend) {
67   // Prevent the user from changing any settings he already specified...
68   foreach(int idx, visitedPages()) page(idx)->setEnabled(false);
69   QVariantMap foo;
70   foo["AdminUser"] = field("adminUser.user").toString();
71   foo["AdminPasswd"] = field("adminUser.password").toString();
72   foo["Backend"] = backend;
73   emit setupCore(foo);
74 }
75
76 void CoreConfigWizard::coreSetupSuccess() {
77   syncPage->setStatus(tr("Your core has been successfully configured. Logging you in..."));
78   syncPage->setError(false);
79   syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
80   QVariantMap loginData;
81   loginData["User"] = field("adminUser.user");
82   loginData["Password"] = field("adminUser.password");
83   loginData["RememberPasswd"] = field("adminUser.rememberPasswd");
84   emit loginToCore(loginData);
85 }
86
87 void CoreConfigWizard::coreSetupFailed(const QString &error) {
88   syncPage->setStatus(tr("Core configuration failed:<br><b>%1</b><br>Press <em>Next</em> to start over.").arg(error));
89   syncPage->setError(true);
90   syncRelayPage->setMode(CoreConfigWizardPages::SyncRelayPage::Error);
91   //foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
92   //setStartId(SyncPage);
93   //restart();
94
95 }
96
97 void CoreConfigWizard::startOver() {
98   foreach(int idx, visitedPages()) page(idx)->setEnabled(true);
99   setStartId(CoreConfigWizard::AdminUserPage);
100   restart();
101 }
102
103 void CoreConfigWizard::loginSuccess() {
104   syncPage->setStatus(tr("Your are now logged into your freshly configured Quassel Core!<br>"
105                          "Please remember to configure your identities and networks now."));
106   syncPage->setComplete(true);
107   syncPage->setFinalPage(true);
108 }
109
110 void CoreConfigWizard::syncFinished() {
111   // TODO: display identities and networks settings if appropriate!
112   // accept();
113 }
114
115 namespace CoreConfigWizardPages {
116
117 /*** Intro Page ***/
118
119 IntroPage::IntroPage(QWidget *parent) : QWizardPage(parent) {
120   ui.setupUi(this);
121   setTitle(tr("Introduction"));
122   //setSubTitle(tr("foobar"));
123   //setPixmap(QWizard::WatermarkPixmap, QPixmap(":icons/quassel-icon.png"));
124
125 }
126
127 int IntroPage::nextId() const {
128   return CoreConfigWizard::AdminUserPage;
129
130 }
131
132 /*** Admin User Page ***/
133
134 AdminUserPage::AdminUserPage(QWidget *parent) : QWizardPage(parent) {
135   ui.setupUi(this);
136   setTitle(tr("Create Admin User"));
137   setSubTitle(tr("First, we will create a user on the core. This first user will have administrator privileges."));
138
139   registerField("adminUser.user*", ui.user);
140   registerField("adminUser.password*", ui.password);
141   registerField("adminUser.password2*", ui.password2);
142   registerField("adminUser.rememberPasswd", ui.rememberPasswd);
143
144   //ui.user->setText("foo");
145   //ui.password->setText("foo");
146   //ui.password2->setText("foo");
147 }
148
149 int AdminUserPage::nextId() const {
150   return CoreConfigWizard::StorageSelectionPage;
151
152 }
153
154 bool AdminUserPage::isComplete() const {
155   bool ok = !ui.user->text().isEmpty() && !ui.password->text().isEmpty() && ui.password->text() == ui.password2->text();
156   return ok;
157 }
158
159 /*** Storage Selection Page ***/
160
161 StorageSelectionPage::StorageSelectionPage(const QHash<QString, QVariant> &backends, QWidget *parent) : QWizardPage(parent) {
162   ui.setupUi(this);
163   _backends = backends;
164
165   setTitle(tr("Select Storage Backend"));
166   setSubTitle(tr("Please select a database backend for the Quassel Core storage to store the backlog and other data in."));
167   setCommitPage(true);
168
169   registerField("storage.backend", ui.backendList);
170
171   foreach(QString key, _backends.keys()) {
172     ui.backendList->addItem(_backends[key].toMap()["DisplayName"].toString(), key);
173   }
174
175   on_backendList_currentIndexChanged();
176 }
177
178 int StorageSelectionPage::nextId() const {
179   return CoreConfigWizard::SyncPage;
180 }
181
182 QString StorageSelectionPage::selectedBackend() const {
183   return ui.backendList->currentText();
184 }
185
186 void StorageSelectionPage::on_backendList_currentIndexChanged() {
187   QString backend = ui.backendList->itemData(ui.backendList->currentIndex()).toString();
188   ui.description->setText(_backends[backend].toMap()["Description"].toString());
189 }
190
191 /*** Sync Page ***/
192
193 SyncPage::SyncPage(QWidget *parent) : QWizardPage(parent) {
194   ui.setupUi(this);
195   setTitle(tr("Storing Your Settings"));
196   setSubTitle(tr("Your settings are now stored in the core, and you will be logged in automatically."));
197 }
198
199 void SyncPage::initializePage() {
200   complete = false;
201   hasError = false;
202   QString backend = qobject_cast<StorageSelectionPage *>(wizard()->page(CoreConfigWizard::StorageSelectionPage))->selectedBackend();
203   Q_ASSERT(!backend.isEmpty());
204   ui.user->setText(wizard()->field("adminUser.user").toString());
205   ui.backend->setText(backend);
206   emit setupCore(backend);
207 }
208
209 int SyncPage::nextId() const {
210   if(!hasError) return -1;
211   return CoreConfigWizard::SyncRelayPage;
212 }
213
214 bool SyncPage::isComplete() const {
215   return complete;
216 }
217
218 void SyncPage::setStatus(const QString &status) {
219   ui.status->setText(status);
220 }
221
222 void SyncPage::setError(bool e) {
223   hasError = e;
224 }
225
226 void SyncPage::setComplete(bool c) {
227   complete = c;
228   completeChanged();
229 }
230
231 /*** Sync Relay Page ***/
232
233 SyncRelayPage::SyncRelayPage(QWidget *parent) : QWizardPage(parent) {
234   mode = Success;
235 }
236
237 void SyncRelayPage::setMode(Mode m) {
238   mode = m;
239 }
240
241 /*
242 void SyncRelayPage::initializePage() {
243   return;
244   if(mode == Success) {
245     wizard()->accept();
246   } else {
247     emit startOver();
248   }
249 }
250 */
251
252 int SyncRelayPage::nextId() const {
253   emit startOver();
254   return 0;
255 }
256
257 };  /* namespace CoreConfigWizardPages */