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