- Highlights are now determined in Client::checkForHighlight(Message &).
[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   ui.accountButtonBox->setFocus();
59   ui.accountButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
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
222   //connect(Client::instance(), SIGNAL(coreConnectionPhaseOne(const QVariantMap &)), this, SLOT(phaseOneFinished
223   clientSyncer->connectToCore(accountData);
224 }
225
226 void CoreConnectDlg::initPhaseError(const QString &error) {
227   doingAutoConnect = false;
228   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
229   //ui.connectLabel->setBrush(QBrush("red"));
230   ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
231   ui.coreInfoLabel->setText(error);
232   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
233   ui.loginButtonBox->button(QDialogButtonBox::Retry)->setDefault(true);
234   disconnect(ui.loginButtonBox, 0, this, 0);
235   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
236   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
237 }
238
239 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
240   ui.coreInfoLabel->setText(msg);
241 }
242
243 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
244   QString s;
245   QString host = accountData["Host"].toString();
246   switch(state) {
247     case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
248     case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
249     case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
250     case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
251     default: s = tr("Unknown connection state to %1"); break;
252   }
253   ui.connectLabel->setText(s);
254 }
255
256 void CoreConnectDlg::restartPhaseNull() {
257   doingAutoConnect = false;
258   ui.stackedWidget->setCurrentWidget(ui.accountPage);
259   clientSyncer->disconnectFromCore();
260 }
261
262 /*********************************************************
263  * Phase Two: Login
264  *********************************************************/
265
266 void CoreConnectDlg::startLogin() {
267   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
268   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
269   ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
270   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
271   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
272   if(!accountData["User"].toString().isEmpty()) {
273     ui.user->setText(accountData["User"].toString());
274     if(accountData["RememberPasswd"].toBool()) {
275       ui.password->setText(accountData["Password"].toString());
276       ui.rememberPasswd->setChecked(true);
277       ui.loginButtonBox->setFocus();
278     } else {
279       ui.rememberPasswd->setChecked(false);
280       ui.password->setFocus();
281     }
282   } else ui.user->setFocus();
283   disconnect(ui.loginButtonBox, 0, this, 0);
284   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
285   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
286   if(doingAutoConnect) doLogin();
287 }
288
289 void CoreConnectDlg::doLogin() {
290   QVariantMap loginData;
291   loginData["User"] = ui.user->text();
292   loginData["Password"] = ui.password->text();
293   loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
294   doLogin(loginData);
295 }
296
297 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
298   disconnect(ui.loginButtonBox, 0, this, 0);
299   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
300   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
301   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
302   ui.loginGroup->setTitle(tr("Logging in..."));
303   ui.user->setDisabled(true);
304   ui.password->setDisabled(true);
305   ui.rememberPasswd->setDisabled(true);
306   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
307   accountData["User"] = loginData["User"];
308   accountData["RememberPasswd"] = loginData["RememberPasswd"];
309   if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
310   else accountData.remove("Password");
311   ui.user->setText(loginData["User"].toString());
312   ui.password->setText(loginData["Password"].toString());
313   ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
314   CoreAccountSettings s;
315   s.storeAccountData(account, accountData);
316   clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
317 }
318
319 void CoreConnectDlg::setLoginWidgetStates() {
320   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
321 }
322
323 void CoreConnectDlg::loginFailed(const QString &error) {
324   if(wizard) {
325     wizard->reject();
326   }
327   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
328   ui.loginGroup->setTitle(tr("Login"));
329   ui.user->setEnabled(true);
330   ui.password->setEnabled(true);
331   ui.rememberPasswd->setEnabled(true);
332   ui.coreInfoLabel->setText(error);
333   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
334   ui.password->setFocus();
335   doingAutoConnect = false;
336 }
337
338 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
339   storageBackends = backends;
340   ui.loginStack->setCurrentWidget(ui.coreConfigPage);
341
342   //on_launchCoreConfigWizard_clicked();
343
344 }
345
346 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
347   Q_ASSERT(!wizard);
348   wizard = new CoreConfigWizard(storageBackends, this);
349   connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
350   connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
351   connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
352   connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
353   connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
354   connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
355   connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
356   connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
357   wizard->show();
358 }
359
360 void CoreConnectDlg::configWizardAccepted() {
361
362   wizard->deleteLater();
363   wizard = 0;
364 }
365
366 void CoreConnectDlg::configWizardRejected() {
367
368   wizard->deleteLater();
369   wizard = 0;
370   //exit(1); // FIXME
371 }
372
373
374 /************************************************************
375  * Phase Three: Syncing
376  ************************************************************/
377
378 void CoreConnectDlg::startSync() {
379   ui.sessionProgress->setRange(0, 1);
380   ui.sessionProgress->setValue(0);
381   ui.networksProgress->setRange(0, 1);
382   ui.networksProgress->setValue(0);
383   ui.channelsProgress->setRange(0, 1);
384   ui.channelsProgress->setValue(0);
385   ui.ircUsersProgress->setRange(0, 1);
386   ui.ircUsersProgress->setValue(0);
387
388   ui.stackedWidget->setCurrentWidget(ui.syncPage);
389   // clean up old page
390   ui.loginGroup->setTitle(tr("Login"));
391   ui.user->setEnabled(true);
392   ui.password->setEnabled(true);
393   ui.rememberPasswd->setEnabled(true);
394   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
395 }
396
397 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
398   ui.sessionProgress->setRange(0, max);
399   ui.sessionProgress->setValue(val);
400
401 }
402
403 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
404   if(max == 0) {
405     ui.networksProgress->setFormat("0/0");
406     ui.networksProgress->setRange(0, 1);
407     ui.networksProgress->setValue(1);
408   } else {
409     ui.networksProgress->setFormat("%v/%m");
410     ui.networksProgress->setRange(0, max);
411     ui.networksProgress->setValue(val);
412   }
413 }
414
415 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
416   if(max == 0) {
417     ui.channelsProgress->setFormat("0/0");
418     ui.channelsProgress->setRange(0, 1);
419     ui.channelsProgress->setValue(1);
420   } else {
421     ui.channelsProgress->setFormat("%v/%m");
422     ui.channelsProgress->setRange(0, max);
423     ui.channelsProgress->setValue(val);
424   }
425 }
426
427 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
428   if(max == 0) {
429     ui.ircUsersProgress->setFormat("0/0");
430     ui.ircUsersProgress->setRange(0, 1);
431     ui.ircUsersProgress->setValue(1);
432   } else {
433     if(val % 100) return;
434     ui.ircUsersProgress->setFormat("%v/%m");
435     ui.ircUsersProgress->setRange(0, max);
436     ui.ircUsersProgress->setValue(val);
437   }
438 }
439
440 void CoreConnectDlg::syncFinished() {
441   if(!wizard) accept();
442   else {
443     hide();
444     disconnect(wizard, 0, this, 0);
445     connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
446   }
447 }
448
449 /*****************************************************************************************
450  * CoreAccountEditDlg
451  *****************************************************************************************/
452
453 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent) : QDialog(parent) {
454   ui.setupUi(this);
455   existing = _existing;
456   account = acct;
457   if(id.isValid()) {
458     existing.removeAll(acct["AccountName"].toString());
459     ui.host->setText(acct["Host"].toString());
460     ui.port->setValue(acct["Port"].toUInt());
461     ui.useInternal->setChecked(acct["UseInternal"].toBool());
462     ui.accountName->setText(acct["AccountName"].toString());
463   } else {
464     setWindowTitle(tr("Add Core Account"));
465   }
466 }
467
468 QVariantMap CoreAccountEditDlg::accountData() {
469   account["AccountName"] = ui.accountName->text();
470   account["Host"] = ui.host->text();
471   account["Port"] = ui.port->value();
472   account["UseInternal"] = ui.useInternal->isChecked();
473   return account;
474 }
475
476 void CoreAccountEditDlg::setWidgetStates() {
477   bool ok = !ui.accountName->text().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
478   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
479 }
480
481 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
482   Q_UNUSED(text);
483   setWidgetStates();
484 }
485
486 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
487   Q_UNUSED(text);
488   setWidgetStates();
489 }
490
491 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {
492   Q_UNUSED(state);
493   setWidgetStates();
494 }