1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include <QMessageBox>
23 #include <QNetworkProxy>
25 #include "coreconnectdlg.h"
27 #include "clientsettings.h"
28 #include "clientsyncer.h"
29 #include "coreconfigwizard.h"
30 #include "iconloader.h"
32 CoreConnectDlg::CoreConnectDlg(bool autoconnect, QWidget *parent)
36 ui.editAccount->setIcon(SmallIcon("document-properties"));
37 ui.addAccount->setIcon(SmallIcon("list-add"));
38 ui.deleteAccount->setIcon(SmallIcon("list-remove"));
39 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
40 ui.secureConnection->setPixmap(SmallIcon("document-encrypt"));
42 // make it look more native under Mac OS X:
43 setWindowFlags(Qt::Sheet);
45 clientSyncer = new ClientSyncer(this);
48 doingAutoConnect = false;
50 ui.stackedWidget->setCurrentWidget(ui.accountPage);
52 CoreAccountSettings s;
53 AccountId lastacc = s.lastAccount();
54 autoConnectAccount = s.autoConnectAccount();
55 QListWidgetItem *currentItem = 0;
56 foreach(AccountId id, s.knownAccounts()) {
57 if(!id.isValid()) continue;
58 QVariantMap data = s.retrieveAccountData(id);
59 data["AccountId"] = QVariant::fromValue<AccountId>(id);
61 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
62 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
63 if(id == lastacc) currentItem = item;
65 if(currentItem) ui.accountList->setCurrentItem(currentItem);
66 else ui.accountList->setCurrentRow(0);
68 setAccountWidgetStates();
70 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
71 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
73 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
74 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
75 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
76 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
77 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
78 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
79 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
80 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
81 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
82 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
83 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
85 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
86 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
88 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
89 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
91 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
92 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
93 doingAutoConnect = true;
94 on_accountButtonBox_accepted();
98 CoreConnectDlg::~CoreConnectDlg() {
99 if(ui.accountList->selectedItems().count()) {
100 CoreAccountSettings s;
101 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
106 /****************************************************
108 ***************************************************/
110 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
111 setAccountWidgetStates();
114 void CoreConnectDlg::setAccountWidgetStates() {
115 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
116 ui.editAccount->setEnabled(selectedItems.count());
117 ui.deleteAccount->setEnabled(selectedItems.count());
118 ui.autoConnect->setEnabled(selectedItems.count());
119 if(selectedItems.count()) {
120 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
122 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
125 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
127 autoConnectAccount = 0;
129 if(ui.accountList->selectedItems().count()) {
130 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
132 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
133 autoConnectAccount = 0;
136 setAccountWidgetStates();
139 void CoreConnectDlg::on_addAccount_clicked() {
140 QStringList existing;
141 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
142 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
143 if(dlg.exec() == QDialog::Accepted) {
145 AccountId id = accounts.count() + 1;
146 for(AccountId i = 1; i <= accounts.count(); i++) {
147 if(!accounts.keys().contains(i)) {
152 QVariantMap data = dlg.accountData();
153 data["AccountId"] = QVariant::fromValue<AccountId>(id);
155 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
156 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
157 ui.accountList->setCurrentItem(item);
161 void CoreConnectDlg::on_editAccount_clicked() {
162 QStringList existing;
163 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
164 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
165 QVariantMap acct = accounts[id];
166 CoreAccountEditDlg dlg(id, acct, existing, this);
167 if(dlg.exec() == QDialog::Accepted) {
168 QVariantMap data = dlg.accountData();
169 ui.accountList->currentItem()->setText(data["AccountName"].toString());
174 void CoreConnectDlg::on_deleteAccount_clicked() {
175 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
176 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
177 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
178 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
179 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
180 if(ret == QMessageBox::Yes) {
181 int idx = ui.accountList->currentRow();
182 delete ui.accountList->takeItem(idx);
183 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
184 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
185 setAccountWidgetStates();
189 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
191 on_accountButtonBox_accepted();
194 void CoreConnectDlg::on_accountButtonBox_accepted() {
196 CoreAccountSettings s;
197 foreach(QVariantMap acct, accounts.values()) {
198 AccountId id = acct["AccountId"].value<AccountId>();
199 if(acct.contains("Delete")) {
202 s.storeAccountData(id, acct);
205 s.setAutoConnectAccount(autoConnectAccount);
207 ui.stackedWidget->setCurrentWidget(ui.loginPage);
208 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
209 accountData = accounts[account];
210 s.setLastAccount(account);
214 void CoreConnectDlg::on_useInternalCore_clicked() {
215 clientSyncer->useInternalCore();
219 /*****************************************************
220 * Connecting to the Core
221 ****************************************************/
223 /*** Phase One: initializing the core connection ***/
225 void CoreConnectDlg::connectToCore() {
226 ui.secureConnection->hide();
227 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
228 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
229 ui.coreInfoLabel->setText("");
230 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
231 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
232 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
233 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
234 disconnect(ui.loginButtonBox, 0, this, 0);
235 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
237 clientSyncer->connectToCore(accountData);
240 void CoreConnectDlg::initPhaseError(const QString &error) {
241 doingAutoConnect = false;
242 ui.secureConnection->hide();
243 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
244 //ui.connectLabel->setBrush(QBrush("red"));
245 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
246 ui.coreInfoLabel->setText(error);
247 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
248 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
249 disconnect(ui.loginButtonBox, 0, this, 0);
250 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
251 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
254 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
255 ui.coreInfoLabel->setText(msg);
258 void CoreConnectDlg::encrypted(bool useSsl) {
260 ui.secureConnection->show();
262 ui.secureConnection->hide();
265 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
267 QString host = accountData["Host"].toString();
269 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
270 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
271 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
272 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
273 default: s = tr("Unknown connection state to %1"); break;
275 ui.connectLabel->setText(s);
278 void CoreConnectDlg::restartPhaseNull() {
279 doingAutoConnect = false;
280 ui.stackedWidget->setCurrentWidget(ui.accountPage);
281 clientSyncer->disconnectFromCore();
284 /*********************************************************
286 *********************************************************/
288 void CoreConnectDlg::startLogin() {
289 ui.connectIcon->setPixmap(BarIcon("network-connect"));
290 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
291 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
292 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
293 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
294 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
295 if(!accountData["User"].toString().isEmpty()) {
296 ui.user->setText(accountData["User"].toString());
297 if(accountData["RememberPasswd"].toBool()) {
298 ui.password->setText(accountData["Password"].toString());
299 ui.rememberPasswd->setChecked(true);
300 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
302 ui.rememberPasswd->setChecked(false);
303 ui.password->setFocus();
305 } else ui.user->setFocus();
306 disconnect(ui.loginButtonBox, 0, this, 0);
307 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
308 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
309 if(doingAutoConnect) doLogin();
312 void CoreConnectDlg::doLogin() {
313 QVariantMap loginData;
314 loginData["User"] = ui.user->text();
315 loginData["Password"] = ui.password->text();
316 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
320 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
321 disconnect(ui.loginButtonBox, 0, this, 0);
322 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
323 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
324 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
325 ui.loginGroup->setTitle(tr("Logging in..."));
326 ui.user->setDisabled(true);
327 ui.password->setDisabled(true);
328 ui.rememberPasswd->setDisabled(true);
329 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
330 accountData["User"] = loginData["User"];
331 accountData["RememberPasswd"] = loginData["RememberPasswd"];
332 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
333 else accountData.remove("Password");
334 ui.user->setText(loginData["User"].toString());
335 ui.password->setText(loginData["Password"].toString());
336 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
337 CoreAccountSettings s;
338 s.storeAccountData(account, accountData);
339 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
342 void CoreConnectDlg::setLoginWidgetStates() {
343 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
346 void CoreConnectDlg::loginFailed(const QString &error) {
350 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
351 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
352 ui.loginGroup->setTitle(tr("Login"));
353 ui.user->setEnabled(true);
354 ui.password->setEnabled(true);
355 ui.rememberPasswd->setEnabled(true);
356 ui.coreInfoLabel->setText(error);
357 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
358 ui.password->setFocus();
359 doingAutoConnect = false;
362 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
363 storageBackends = backends;
364 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
366 //on_launchCoreConfigWizard_clicked();
370 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
372 wizard = new CoreConfigWizard(storageBackends, this);
373 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
374 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
375 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
376 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
377 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
378 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
379 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
380 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
384 void CoreConnectDlg::configWizardAccepted() {
386 wizard->deleteLater();
390 void CoreConnectDlg::configWizardRejected() {
392 wizard->deleteLater();
398 /************************************************************
399 * Phase Three: Syncing
400 ************************************************************/
402 void CoreConnectDlg::startSync() {
403 ui.sessionProgress->setRange(0, 1);
404 ui.sessionProgress->setValue(0);
405 ui.networksProgress->setRange(0, 1);
406 ui.networksProgress->setValue(0);
408 ui.stackedWidget->setCurrentWidget(ui.syncPage);
410 ui.loginGroup->setTitle(tr("Login"));
411 ui.user->setEnabled(true);
412 ui.password->setEnabled(true);
413 ui.rememberPasswd->setEnabled(true);
414 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
417 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
418 ui.sessionProgress->setRange(0, max);
419 ui.sessionProgress->setValue(val);
423 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
425 ui.networksProgress->setFormat("0/0");
426 ui.networksProgress->setRange(0, 1);
427 ui.networksProgress->setValue(1);
429 ui.networksProgress->setFormat("%v/%m");
430 ui.networksProgress->setRange(0, max);
431 ui.networksProgress->setValue(val);
435 void CoreConnectDlg::syncFinished() {
436 if(!wizard) accept();
439 disconnect(wizard, 0, this, 0);
440 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
444 /*****************************************************************************************
446 *****************************************************************************************/
447 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
451 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
453 existing = _existing;
455 existing.removeAll(acct["AccountName"].toString());
456 ui.host->setText(acct["Host"].toString());
457 ui.port->setValue(acct["Port"].toUInt());
458 ui.useInternal->setChecked(acct["UseInternal"].toBool());
459 ui.accountName->setText(acct["AccountName"].toString());
461 ui.useSsl->setChecked(acct["useSsl"].toBool());
463 ui.useSsl->setChecked(false);
464 ui.useSsl->setEnabled(false);
466 ui.useProxy->setChecked(acct["useProxy"].toBool());
467 ui.proxyHost->setText(acct["proxyHost"].toString());
468 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
469 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
470 ui.proxyUser->setText(acct["proxyUser"].toString());
471 ui.proxyPassword->setText(acct["proxyPassword"].toString());
473 setWindowTitle(tr("Add Core Account"));
475 ui.useSsl->setChecked(false);
476 ui.useSsl->setEnabled(false);
481 // 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
482 ui.useInternal->setChecked(false);
483 ui.useInternal->hide();
484 ui.useRemote->hide();
485 ui.labelUseBuiltinCore->hide();
489 QVariantMap CoreAccountEditDlg::accountData() {
490 account["AccountName"] = ui.accountName->text().trimmed();
491 account["Host"] = ui.host->text().trimmed();
492 account["Port"] = ui.port->value();
493 account["UseInternal"] = ui.useInternal->isChecked();
494 account["useSsl"] = ui.useSsl->isChecked();
495 account["useProxy"] = ui.useProxy->isChecked();
496 account["proxyHost"] = ui.proxyHost->text().trimmed();
497 account["proxyPort"] = ui.proxyPort->value();
498 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
499 account["proxyUser"] = ui.proxyUser->text().trimmed();
500 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
504 void CoreAccountEditDlg::setWidgetStates() {
505 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
506 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
509 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
514 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
519 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {