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 /*****************************************************
215 * Connecting to the Core
216 ****************************************************/
218 /*** Phase One: initializing the core connection ***/
220 void CoreConnectDlg::connectToCore() {
221 ui.secureConnection->hide();
222 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
223 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
224 ui.coreInfoLabel->setText("");
225 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
226 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
227 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
228 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
229 disconnect(ui.loginButtonBox, 0, this, 0);
230 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
232 clientSyncer->connectToCore(accountData);
235 void CoreConnectDlg::initPhaseError(const QString &error) {
236 doingAutoConnect = false;
237 ui.secureConnection->hide();
238 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
239 //ui.connectLabel->setBrush(QBrush("red"));
240 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
241 ui.coreInfoLabel->setText(error);
242 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
243 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
244 disconnect(ui.loginButtonBox, 0, this, 0);
245 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
246 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
249 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
250 ui.coreInfoLabel->setText(msg);
253 void CoreConnectDlg::encrypted(bool useSsl) {
255 ui.secureConnection->show();
257 ui.secureConnection->hide();
260 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
262 QString host = accountData["Host"].toString();
264 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
265 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
266 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
267 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
268 default: s = tr("Unknown connection state to %1"); break;
270 ui.connectLabel->setText(s);
273 void CoreConnectDlg::restartPhaseNull() {
274 doingAutoConnect = false;
275 ui.stackedWidget->setCurrentWidget(ui.accountPage);
276 clientSyncer->disconnectFromCore();
279 /*********************************************************
281 *********************************************************/
283 void CoreConnectDlg::startLogin() {
284 ui.connectIcon->setPixmap(BarIcon("network-connect"));
285 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
286 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
287 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
288 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
289 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
290 if(!accountData["User"].toString().isEmpty()) {
291 ui.user->setText(accountData["User"].toString());
292 if(accountData["RememberPasswd"].toBool()) {
293 ui.password->setText(accountData["Password"].toString());
294 ui.rememberPasswd->setChecked(true);
295 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
297 ui.rememberPasswd->setChecked(false);
298 ui.password->setFocus();
300 } else ui.user->setFocus();
301 disconnect(ui.loginButtonBox, 0, this, 0);
302 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
303 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
304 if(doingAutoConnect) doLogin();
307 void CoreConnectDlg::doLogin() {
308 QVariantMap loginData;
309 loginData["User"] = ui.user->text();
310 loginData["Password"] = ui.password->text();
311 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
315 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
316 disconnect(ui.loginButtonBox, 0, this, 0);
317 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
318 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
319 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
320 ui.loginGroup->setTitle(tr("Logging in..."));
321 ui.user->setDisabled(true);
322 ui.password->setDisabled(true);
323 ui.rememberPasswd->setDisabled(true);
324 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
325 accountData["User"] = loginData["User"];
326 accountData["RememberPasswd"] = loginData["RememberPasswd"];
327 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
328 else accountData.remove("Password");
329 ui.user->setText(loginData["User"].toString());
330 ui.password->setText(loginData["Password"].toString());
331 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
332 CoreAccountSettings s;
333 s.storeAccountData(account, accountData);
334 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
337 void CoreConnectDlg::setLoginWidgetStates() {
338 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
341 void CoreConnectDlg::loginFailed(const QString &error) {
345 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
346 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
347 ui.loginGroup->setTitle(tr("Login"));
348 ui.user->setEnabled(true);
349 ui.password->setEnabled(true);
350 ui.rememberPasswd->setEnabled(true);
351 ui.coreInfoLabel->setText(error);
352 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
353 ui.password->setFocus();
354 doingAutoConnect = false;
357 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
358 storageBackends = backends;
359 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
361 //on_launchCoreConfigWizard_clicked();
365 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
367 wizard = new CoreConfigWizard(storageBackends, this);
368 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
369 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
370 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
371 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
372 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
373 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
374 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
375 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
379 void CoreConnectDlg::configWizardAccepted() {
381 wizard->deleteLater();
385 void CoreConnectDlg::configWizardRejected() {
387 wizard->deleteLater();
393 /************************************************************
394 * Phase Three: Syncing
395 ************************************************************/
397 void CoreConnectDlg::startSync() {
398 ui.sessionProgress->setRange(0, 1);
399 ui.sessionProgress->setValue(0);
400 ui.networksProgress->setRange(0, 1);
401 ui.networksProgress->setValue(0);
403 ui.stackedWidget->setCurrentWidget(ui.syncPage);
405 ui.loginGroup->setTitle(tr("Login"));
406 ui.user->setEnabled(true);
407 ui.password->setEnabled(true);
408 ui.rememberPasswd->setEnabled(true);
409 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
412 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
413 ui.sessionProgress->setRange(0, max);
414 ui.sessionProgress->setValue(val);
418 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
420 ui.networksProgress->setFormat("0/0");
421 ui.networksProgress->setRange(0, 1);
422 ui.networksProgress->setValue(1);
424 ui.networksProgress->setFormat("%v/%m");
425 ui.networksProgress->setRange(0, max);
426 ui.networksProgress->setValue(val);
430 void CoreConnectDlg::syncFinished() {
431 if(!wizard) accept();
434 disconnect(wizard, 0, this, 0);
435 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
439 /*****************************************************************************************
441 *****************************************************************************************/
442 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
446 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
448 existing = _existing;
450 existing.removeAll(acct["AccountName"].toString());
451 ui.host->setText(acct["Host"].toString());
452 ui.port->setValue(acct["Port"].toUInt());
453 ui.useInternal->setChecked(acct["UseInternal"].toBool());
454 ui.accountName->setText(acct["AccountName"].toString());
456 ui.useSsl->setChecked(acct["useSsl"].toBool());
458 ui.useSsl->setChecked(false);
459 ui.useSsl->setEnabled(false);
461 ui.useProxy->setChecked(acct["useProxy"].toBool());
462 ui.proxyHost->setText(acct["proxyHost"].toString());
463 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
464 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
465 ui.proxyUser->setText(acct["proxyUser"].toString());
466 ui.proxyPassword->setText(acct["proxyPassword"].toString());
468 setWindowTitle(tr("Add Core Account"));
470 ui.useSsl->setChecked(false);
471 ui.useSsl->setEnabled(false);
476 // 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
477 ui.useInternal->setChecked(false);
478 ui.useInternal->hide();
479 ui.useRemote->hide();
480 ui.labelUseBuiltinCore->hide();
484 QVariantMap CoreAccountEditDlg::accountData() {
485 account["AccountName"] = ui.accountName->text().trimmed();
486 account["Host"] = ui.host->text().trimmed();
487 account["Port"] = ui.port->value();
488 account["UseInternal"] = ui.useInternal->isChecked();
489 account["useSsl"] = ui.useSsl->isChecked();
490 account["useProxy"] = ui.useProxy->isChecked();
491 account["proxyHost"] = ui.proxyHost->text().trimmed();
492 account["proxyPort"] = ui.proxyPort->value();
493 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
494 account["proxyUser"] = ui.proxyUser->text().trimmed();
495 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
499 void CoreAccountEditDlg::setWidgetStates() {
500 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
501 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
504 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
509 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
514 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {