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