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