This is the long-awaited monster commit, bringing you a redesigned core arch and...
[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
29 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect) : QDialog(parent) {
30   ui.setupUi(this);
31
32   clientSyncer = new ClientSyncer(this);
33
34   setAttribute(Qt::WA_DeleteOnClose);
35
36   doingAutoConnect = false;
37
38   ui.stackedWidget->setCurrentWidget(ui.accountPage);
39   ui.accountButtonBox->setFocus();
40
41   CoreAccountSettings s;
42   QString lastacc = s.lastAccount();
43   autoConnectAccount = s.autoConnectAccount();
44   accounts = s.retrieveAllAccounts();
45   ui.accountList->addItems(accounts.keys());
46   QList<QListWidgetItem *> l = ui.accountList->findItems(lastacc, Qt::MatchExactly);
47   if(l.count()) ui.accountList->setCurrentItem(l[0]);
48   else ui.accountList->setCurrentRow(0);
49
50   setAccountWidgetStates();
51
52   connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
53   connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
54   connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
55   connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
56   connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
57   connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
58   connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
59   connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
60   connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
61   connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
62   connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(accept()));
63
64   connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
65   connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
66
67   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
68   connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
69
70   if(autoconnect && ui.accountList->count() && !autoConnectAccount.isEmpty() && autoConnectAccount == ui.accountList->currentItem()->text()) {
71     doingAutoConnect = true;
72     on_accountButtonBox_accepted();
73   }
74 }
75
76 CoreConnectDlg::~CoreConnectDlg() {
77   if(ui.accountList->selectedItems().count()) {
78     CoreAccountSettings s;
79     s.setLastAccount(ui.accountList->selectedItems()[0]->text());
80   }
81 }
82
83
84 /****************************************************
85  * Account Management
86  ***************************************************/
87
88 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
89   setAccountWidgetStates();
90 }
91
92 void CoreConnectDlg::setAccountWidgetStates() {
93   QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
94   ui.editAccount->setEnabled(selectedItems.count());
95   ui.deleteAccount->setEnabled(selectedItems.count());
96   ui.autoConnect->setEnabled(selectedItems.count());
97   if(selectedItems.count()) {
98     ui.autoConnect->setChecked(selectedItems[0]->text() == autoConnectAccount);
99   }
100 }
101
102 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
103   if(!state) {
104     autoConnectAccount = QString();
105   } else {
106     if(ui.accountList->selectedItems().count()) {
107       autoConnectAccount = ui.accountList->selectedItems()[0]->text();
108     } else {
109       qWarning() << "Checked auto connect without an enabled item!";  // should never happen!
110       autoConnectAccount = QString();
111     }
112   }
113   setAccountWidgetStates();
114 }
115
116 void CoreConnectDlg::on_addAccount_clicked() {
117   QStringList existing;
118   for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
119   CoreAccountEditDlg dlg(QString(), QVariantMap(), existing, this);
120   if(dlg.exec() == QDialog::Accepted) {
121     accounts[dlg.accountName()] = dlg.accountData();
122     ui.accountList->addItem(dlg.accountName());
123     ui.accountList->setCurrentItem(ui.accountList->findItems(dlg.accountName(), Qt::MatchExactly)[0]);
124   }
125 }
126
127 void CoreConnectDlg::on_editAccount_clicked() {
128   QStringList existing;
129   for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
130   QString current = ui.accountList->currentItem()->text();
131   QVariantMap acct = accounts[current];
132   CoreAccountEditDlg dlg(current, acct, existing, this);
133   if(dlg.exec() == QDialog::Accepted) {
134     if(current != dlg.accountName()) {
135       if(autoConnectAccount == current) autoConnectAccount = dlg.accountName();
136       accounts.remove(current);
137       current = dlg.accountName();
138       ui.accountList->currentItem()->setText(current);
139     }
140     accounts[current] = dlg.accountData();
141   }
142   //ui.accountList->setCurrent
143 }
144
145 void CoreConnectDlg::on_deleteAccount_clicked() {
146   QString current = ui.accountList->currentItem()->text();
147   int ret = QMessageBox::question(this, tr("Remove Account Settings"),
148                                   tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
149                                   "Note: This will <em>not</em> remove or change any data on the Core itself!"),
150                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
151   if(ret == QMessageBox::Yes) {
152     int idx = ui.accountList->currentRow();
153     delete ui.accountList->item(idx);
154     ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()));
155     accounts.remove(current);
156   }
157 }
158
159 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
160   Q_UNUSED(item);
161   on_accountButtonBox_accepted();
162 }
163
164 void CoreConnectDlg::on_accountButtonBox_accepted() {
165   // save accounts
166   CoreAccountSettings s;
167   s.storeAllAccounts(accounts);
168   s.setAutoConnectAccount(autoConnectAccount);
169
170   ui.stackedWidget->setCurrentWidget(ui.loginPage);
171   accountName = ui.accountList->currentItem()->text();
172   account = s.retrieveAccount(accountName);
173   s.setLastAccount(accountName);
174   connectToCore();
175 }
176
177 /*****************************************************
178  * Connecting to the Core
179  ****************************************************/
180
181 /*** Phase One: initializing the core connection ***/
182
183 void CoreConnectDlg::connectToCore() {
184   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
185   ui.connectLabel->setText(tr("Connect to %1").arg(account["Host"].toString()));
186   ui.coreInfoLabel->setText("");
187   ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
188   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
189   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
190   disconnect(ui.loginButtonBox, 0, this, 0);
191   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
192
193
194   //connect(Client::instance(), SIGNAL(coreConnectionPhaseOne(const QVariantMap &)), this, SLOT(phaseOneFinished
195   clientSyncer->connectToCore(account);
196 }
197
198 void CoreConnectDlg::initPhaseError(const QString &error) {
199   doingAutoConnect = false;
200   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
201   //ui.connectLabel->setBrush(QBrush("red"));
202   ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(account["Host"].toString()));
203   ui.coreInfoLabel->setText(error);
204   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
205   disconnect(ui.loginButtonBox, 0, this, 0);
206   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
207   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
208 }
209
210 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
211   ui.coreInfoLabel->setText(msg);
212 }
213
214 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
215   QString s;
216   QString host = account["Host"].toString();
217   switch(state) {
218     case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
219     case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
220     case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
221     case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
222     default: s = tr("Unknown connection state to %1"); break;
223   }
224   ui.connectLabel->setText(s);
225 }
226
227 void CoreConnectDlg::restartPhaseNull() {
228   doingAutoConnect = false;
229   ui.stackedWidget->setCurrentWidget(ui.accountPage);
230   clientSyncer->disconnectFromCore();
231 }
232
233 /*********************************************************
234  * Phase Two: Login
235  *********************************************************/
236
237 void CoreConnectDlg::startLogin() {
238   ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
239   ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
240   ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
241   ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
242   if(!account["User"].toString().isEmpty()) {
243     ui.user->setText(account["User"].toString());
244     if(account["RememberPasswd"].toBool()) {
245       ui.password->setText(account["Password"].toString());
246       ui.rememberPasswd->setChecked(true);
247     } else {
248       ui.rememberPasswd->setChecked(false);
249       ui.password->setFocus();
250     }
251   } else ui.user->setFocus();
252   disconnect(ui.loginButtonBox, 0, this, 0);
253   connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
254   connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
255   if(doingAutoConnect) doLogin();
256 }
257
258 void CoreConnectDlg::doLogin() {
259   ui.loginGroup->setTitle(tr("Logging in..."));
260   ui.user->setDisabled(true);
261   ui.password->setDisabled(true);
262   ui.rememberPasswd->setDisabled(true);
263   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
264   account["User"] = ui.user->text();
265   account["RememberPasswd"] = ui.rememberPasswd->isChecked();
266   if(ui.rememberPasswd->isChecked()) account["Password"] = ui.password->text();
267   else account.remove("Password");
268   CoreAccountSettings s;
269   s.storeAccount(accountName, account);
270   clientSyncer->loginToCore(account["User"].toString(), account["Password"].toString());
271 }
272
273 void CoreConnectDlg::setLoginWidgetStates() {
274   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
275 }
276
277 void CoreConnectDlg::loginFailed(const QString &error) {
278   ui.loginGroup->setTitle(tr("Login"));
279   ui.user->setEnabled(true);
280   ui.password->setEnabled(true);
281   ui.rememberPasswd->setEnabled(true);
282   ui.coreInfoLabel->setText(error);
283   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
284   ui.password->setFocus();
285   doingAutoConnect = false;
286 }
287
288 /************************************************************
289  * Phase Three: Syncing
290  ************************************************************/
291
292 void CoreConnectDlg::startSync() {
293   ui.sessionProgress->setRange(0, 1);
294   ui.sessionProgress->setValue(0);
295   ui.networksProgress->setRange(0, 1);
296   ui.networksProgress->setValue(0);
297   ui.channelsProgress->setRange(0, 1);
298   ui.channelsProgress->setValue(0);
299   ui.ircUsersProgress->setRange(0, 1);
300   ui.ircUsersProgress->setValue(0);
301
302   ui.stackedWidget->setCurrentWidget(ui.syncPage);
303   // clean up old page
304   ui.loginGroup->setTitle(tr("Login"));
305   ui.user->setEnabled(true);
306   ui.password->setEnabled(true);
307   ui.rememberPasswd->setEnabled(true);
308   ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
309 }
310
311
312 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
313   ui.sessionProgress->setRange(0, max);
314   ui.sessionProgress->setValue(val);
315
316 }
317
318 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
319   if(max == 0) {
320     ui.networksProgress->setFormat("0/0");
321     ui.networksProgress->setRange(0, 1);
322     ui.networksProgress->setValue(1);
323   } else {
324     ui.networksProgress->setFormat("%v/%m");
325     ui.networksProgress->setRange(0, max);
326     ui.networksProgress->setValue(val);
327   }
328 }
329
330 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
331   if(max == 0) {
332     ui.channelsProgress->setFormat("0/0");
333     ui.channelsProgress->setRange(0, 1);
334     ui.channelsProgress->setValue(1);
335   } else {
336     ui.channelsProgress->setFormat("%v/%m");
337     ui.channelsProgress->setRange(0, max);
338     ui.channelsProgress->setValue(val);
339   }
340 }
341
342 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
343   if(max == 0) {
344     ui.ircUsersProgress->setFormat("0/0");
345     ui.ircUsersProgress->setRange(0, 1);
346     ui.ircUsersProgress->setValue(1);
347   } else {
348     ui.ircUsersProgress->setFormat("%v/%m");
349     ui.ircUsersProgress->setRange(0, max);
350     ui.ircUsersProgress->setValue(val);
351   }
352 }
353
354 /*****************************************************************************************
355  * CoreAccountEditDlg
356  *****************************************************************************************/
357
358 CoreAccountEditDlg::CoreAccountEditDlg(const QString &name, const QVariantMap &acct, const QStringList &_existing, QWidget *parent) : QDialog(parent) {
359   ui.setupUi(this);
360   existing = _existing;
361   account = acct;
362   if(!name.isEmpty()) {
363     existing.removeAll(name);
364     ui.host->setText(acct["Host"].toString());
365     ui.port->setValue(acct["Port"].toUInt());
366     ui.useInternal->setChecked(acct["UseInternal"].toBool());
367     ui.accountName->setText(name);
368   } else {
369     setWindowTitle(tr("Add Core Account"));
370   }
371 }
372
373 QString CoreAccountEditDlg::accountName() const {
374   return ui.accountName->text();
375 }
376
377 QVariantMap CoreAccountEditDlg::accountData() {
378   account["Host"] = ui.host->text();
379   account["Port"] = ui.port->value();
380   account["UseInternal"] = ui.useInternal->isChecked();
381   return account;
382 }
383
384 void CoreAccountEditDlg::setWidgetStates() {
385   bool ok = !accountName().isEmpty() && !existing.contains(accountName()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
386   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
387 }
388
389 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
390   Q_UNUSED(text);
391   setWidgetStates();
392 }
393
394 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
395   Q_UNUSED(text);
396   setWidgetStates();
397 }
398
399 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {
400   Q_UNUSED(state);
401   setWidgetStates();
402 }