OK button does no longer discard a settings dialog if settings are not valid.
[quassel.git] / src / qtui / coreconnectdlg.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 <QMessageBox>
23
24 #include "coreconnectdlg.h"
25
26 #include "clientsettings.h"
27 #include "clientsyncer.h"
28 #include "coreconfigwizard.h"
29
30 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect) : QDialog(parent) {
31   ui.setupUi(this);
32
33   clientSyncer = new ClientSyncer(this);
34   wizard = 0;
35
36   setAttribute(Qt::WA_DeleteOnClose);
37
38   doingAutoConnect = false;
39
40   ui.stackedWidget->setCurrentWidget(ui.accountPage);
41
42   CoreAccountSettings s;
43   AccountId lastacc = s.lastAccount();
44   autoConnectAccount = s.autoConnectAccount();
45   QListWidgetItem *currentItem = 0;
46   foreach(AccountId id, s.knownAccounts()) {
47     if(!id.isValid()) continue;
48     QVariantMap data = s.retrieveAccountData(id);
49     accounts[id] = data;
50     QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
51     item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
52     if(id == lastacc) currentItem = item;
53   }
54   if(currentItem) ui.accountList->setCurrentItem(currentItem);
55   else ui.accountList->setCurrentRow(0);
56
57   setAccountWidgetStates();
58
59   ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
60
61   connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
62   connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
63   connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
64   connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
65   connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
66   connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
67   connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
68   connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
69   connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
70   connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
71   connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
72   connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
73
74   connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
75   connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
76
77   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
78   connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
79
80   if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
81      && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
82     doingAutoConnect = true;
83     on_accountButtonBox_accepted();
84   }
85 }
86
87 CoreConnectDlg::~CoreConnectDlg() {
88   if(ui.accountList->selectedItems().count()) {
89     CoreAccountSettings s;
90     s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
91   }
92 }
93
94
95 /****************************************************
96  * Account Management
97  ***************************************************/
98
99 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
100   setAccountWidgetStates();
101 }
102
103 void CoreConnectDlg::setAccountWidgetStates() {
104   QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
105   ui.editAccount->setEnabled(selectedItems.count());
106   ui.deleteAccount->setEnabled(selectedItems.count());
107   ui.autoConnect->setEnabled(selectedItems.count());
108   if(selectedItems.count()) {
109     ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
110   }
111   ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
112 }
113
114 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
115   if(!state) {
116     autoConnectAccount = 0;
117   } else {
118     if(ui.accountList->selectedItems().count()) {
119       autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
120     } else {
121       qWarning() << "Checked auto connect without an enabled item!";  // should never happen!
122       autoConnectAccount = 0;
123     }
124   }
125   setAccountWidgetStates();
126 }
127
128 void CoreConnectDlg::on_addAccount_clicked() {
129   QStringList existing;
130   for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
131   CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
132   if(dlg.exec() == QDialog::Accepted) {
133     // find free ID
134     AccountId id = accounts.count() + 1;
135     for(AccountId i = 1; i <= accounts.count(); i++) {
136       if(!accounts.keys().contains(i)) {
137         id = i;
138         break;
139       }
140     }
141     QVariantMap data = dlg.accountData();
142     data["AccountId"] = QVariant::fromValue<AccountId>(id);
143     accounts[id] = data;
144     QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
145     item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
146     ui.accountList->setCurrentItem(item);
147   }
148 }
149
150 void CoreConnectDlg::on_editAccount_clicked() {
151   QStringList existing;
152   for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
153   AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
154   QVariantMap acct = accounts[id];
155   CoreAccountEditDlg dlg(id, acct, existing, this);
156   if(dlg.exec() == QDialog::Accepted) {
157     QVariantMap data = dlg.accountData();
158     ui.accountList->currentItem()->setText(data["AccountName"].toString());
159     accounts[id] = data;
160   }
161 }
162
163 void CoreConnectDlg::on_deleteAccount_clicked() {
164   AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
165   int ret = QMessageBox::question(this, tr("Remove Account Settings"),
166                                   tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
167                                   "Note: This will <em>not</em> remove or change any data on the Core itself!"),
168                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
169   if(ret == QMessageBox::Yes) {
170     int idx = ui.accountList->currentRow();
171     delete ui.accountList->takeItem(idx);
172     ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
173     accounts[id]["Delete"] = true;  // we only flag this here, actual deletion happens on accept!
174     setAccountWidgetStates();
175   }
176 }
177
178 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
179   Q_UNUSED(item);
180   on_accountButtonBox_accepted();
181 }
182
183 void CoreConnectDlg::on_accountButtonBox_accepted() {
184   // save accounts
185   CoreAccountSettings s;
186   foreach(QVariantMap acct, accounts.values()) {
187     AccountId id = acct["AccountId"].value<AccountId>();
188     if(acct.contains("Delete")) {
189       s.removeAccount(id);
190     } else {
191       s.storeAccountData(id, acct);
192     }
193   }
194   s.setAutoConnectAccount(autoConnectAccount);
195
196   ui.stackedWidget->setCurrentWidget(ui.loginPage);
197   account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
198   accountData = accounts[account];
199   s.setLastAccount(account);
200   connectToCore();
201 }
202
203 /*****************************************************
204  * Connecting to the Core
205  ****************************************************/
206
207 /*** Phase One: initializing the core connection ***/
208
209 void CoreConnectDlg::connectToCore() {
210   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
211   ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
212   ui.coreInfoLabel->setText("");
213   ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
214   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
215   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
216   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
217   disconnect(ui.loginButtonBox, 0, this, 0);
218   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
219
220   clientSyncer->connectToCore(accountData);
221 }
222
223 void CoreConnectDlg::initPhaseError(const QString &error) {
224   doingAutoConnect = false;
225   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
226   //ui.connectLabel->setBrush(QBrush("red"));
227   ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
228   ui.coreInfoLabel->setText(error);
229   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
230   ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
231   disconnect(ui.loginButtonBox, 0, this, 0);
232   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
233   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
234 }
235
236 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
237   ui.coreInfoLabel->setText(msg);
238 }
239
240 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
241   QString s;
242   QString host = accountData["Host"].toString();
243   switch(state) {
244     case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
245     case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
246     case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
247     case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
248     default: s = tr("Unknown connection state to %1"); break;
249   }
250   ui.connectLabel->setText(s);
251 }
252
253 void CoreConnectDlg::restartPhaseNull() {
254   doingAutoConnect = false;
255   ui.stackedWidget->setCurrentWidget(ui.accountPage);
256   clientSyncer->disconnectFromCore();
257 }
258
259 /*********************************************************
260  * Phase Two: Login
261  *********************************************************/
262
263 void CoreConnectDlg::startLogin() {
264   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
265   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
266   //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
267   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
268   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
269   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
270   if(!accountData["User"].toString().isEmpty()) {
271     ui.user->setText(accountData["User"].toString());
272     if(accountData["RememberPasswd"].toBool()) {
273       ui.password->setText(accountData["Password"].toString());
274       ui.rememberPasswd->setChecked(true);
275       ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
276     } else {
277       ui.rememberPasswd->setChecked(false);
278       ui.password->setFocus();
279     }
280   } else ui.user->setFocus();
281   disconnect(ui.loginButtonBox, 0, this, 0);
282   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
283   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
284   if(doingAutoConnect) doLogin();
285 }
286
287 void CoreConnectDlg::doLogin() {
288   QVariantMap loginData;
289   loginData["User"] = ui.user->text();
290   loginData["Password"] = ui.password->text();
291   loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
292   doLogin(loginData);
293 }
294
295 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
296   disconnect(ui.loginButtonBox, 0, this, 0);
297   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
298   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
299   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
300   ui.loginGroup->setTitle(tr("Logging in..."));
301   ui.user->setDisabled(true);
302   ui.password->setDisabled(true);
303   ui.rememberPasswd->setDisabled(true);
304   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
305   accountData["User"] = loginData["User"];
306   accountData["RememberPasswd"] = loginData["RememberPasswd"];
307   if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
308   else accountData.remove("Password");
309   ui.user->setText(loginData["User"].toString());
310   ui.password->setText(loginData["Password"].toString());
311   ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
312   CoreAccountSettings s;
313   s.storeAccountData(account, accountData);
314   clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
315 }
316
317 void CoreConnectDlg::setLoginWidgetStates() {
318   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
319 }
320
321 void CoreConnectDlg::loginFailed(const QString &error) {
322   if(wizard) {
323     wizard->reject();
324   }
325   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
326   ui.loginGroup->setTitle(tr("Login"));
327   ui.user->setEnabled(true);
328   ui.password->setEnabled(true);
329   ui.rememberPasswd->setEnabled(true);
330   ui.coreInfoLabel->setText(error);
331   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
332   ui.password->setFocus();
333   doingAutoConnect = false;
334 }
335
336 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
337   storageBackends = backends;
338   ui.loginStack->setCurrentWidget(ui.coreConfigPage);
339
340   //on_launchCoreConfigWizard_clicked();
341
342 }
343
344 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
345   Q_ASSERT(!wizard);
346   wizard = new CoreConfigWizard(storageBackends, this);
347   connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
348   connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
349   connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
350   connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
351   connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
352   connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
353   connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
354   connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
355   wizard->show();
356 }
357
358 void CoreConnectDlg::configWizardAccepted() {
359
360   wizard->deleteLater();
361   wizard = 0;
362 }
363
364 void CoreConnectDlg::configWizardRejected() {
365
366   wizard->deleteLater();
367   wizard = 0;
368   //exit(1); // FIXME
369 }
370
371
372 /************************************************************
373  * Phase Three: Syncing
374  ************************************************************/
375
376 void CoreConnectDlg::startSync() {
377   ui.sessionProgress->setRange(0, 1);
378   ui.sessionProgress->setValue(0);
379   ui.networksProgress->setRange(0, 1);
380   ui.networksProgress->setValue(0);
381   ui.channelsProgress->setRange(0, 1);
382   ui.channelsProgress->setValue(0);
383   ui.ircUsersProgress->setRange(0, 1);
384   ui.ircUsersProgress->setValue(0);
385
386   ui.stackedWidget->setCurrentWidget(ui.syncPage);
387   // clean up old page
388   ui.loginGroup->setTitle(tr("Login"));
389   ui.user->setEnabled(true);
390   ui.password->setEnabled(true);
391   ui.rememberPasswd->setEnabled(true);
392   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
393 }
394
395 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
396   ui.sessionProgress->setRange(0, max);
397   ui.sessionProgress->setValue(val);
398
399 }
400
401 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
402   if(max == 0) {
403     ui.networksProgress->setFormat("0/0");
404     ui.networksProgress->setRange(0, 1);
405     ui.networksProgress->setValue(1);
406   } else {
407     ui.networksProgress->setFormat("%v/%m");
408     ui.networksProgress->setRange(0, max);
409     ui.networksProgress->setValue(val);
410   }
411 }
412
413 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
414   if(max == 0) {
415     ui.channelsProgress->setFormat("0/0");
416     ui.channelsProgress->setRange(0, 1);
417     ui.channelsProgress->setValue(1);
418   } else {
419     ui.channelsProgress->setFormat("%v/%m");
420     ui.channelsProgress->setRange(0, max);
421     ui.channelsProgress->setValue(val);
422   }
423 }
424
425 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
426   if(max == 0) {
427     ui.ircUsersProgress->setFormat("0/0");
428     ui.ircUsersProgress->setRange(0, 1);
429     ui.ircUsersProgress->setValue(1);
430   } else {
431     if(val % 100) return;
432     ui.ircUsersProgress->setFormat("%v/%m");
433     ui.ircUsersProgress->setRange(0, max);
434     ui.ircUsersProgress->setValue(val);
435   }
436 }
437
438 void CoreConnectDlg::syncFinished() {
439   if(!wizard) accept();
440   else {
441     hide();
442     disconnect(wizard, 0, this, 0);
443     connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
444   }
445 }
446
447 /*****************************************************************************************
448  * CoreAccountEditDlg
449  *****************************************************************************************/
450
451 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent) : QDialog(parent) {
452   ui.setupUi(this);
453   existing = _existing;
454   account = acct;
455   if(id.isValid()) {
456     existing.removeAll(acct["AccountName"].toString());
457     ui.host->setText(acct["Host"].toString());
458     ui.port->setValue(acct["Port"].toUInt());
459     ui.useInternal->setChecked(acct["UseInternal"].toBool());
460     ui.accountName->setText(acct["AccountName"].toString());
461   } else {
462     setWindowTitle(tr("Add Core Account"));
463   }
464 }
465
466 QVariantMap CoreAccountEditDlg::accountData() {
467   account["AccountName"] = ui.accountName->text();
468   account["Host"] = ui.host->text();
469   account["Port"] = ui.port->value();
470   account["UseInternal"] = ui.useInternal->isChecked();
471   return account;
472 }
473
474 void CoreAccountEditDlg::setWidgetStates() {
475   bool ok = !ui.accountName->text().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
476   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
477 }
478
479 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
480   Q_UNUSED(text);
481   setWidgetStates();
482 }
483
484 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
485   Q_UNUSED(text);
486   setWidgetStates();
487 }
488
489 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {
490   Q_UNUSED(state);
491   setWidgetStates();
492 }