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