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"
31 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect)
36 // make it look more native under Mac OS X:
37 setWindowFlags(Qt::Sheet);
39 clientSyncer = new ClientSyncer(this);
42 setAttribute(Qt::WA_DeleteOnClose);
44 doingAutoConnect = false;
46 ui.stackedWidget->setCurrentWidget(ui.accountPage);
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);
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;
61 if(currentItem) ui.accountList->setCurrentItem(currentItem);
62 else ui.accountList->setCurrentRow(0);
64 setAccountWidgetStates();
66 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
67 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
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()));
81 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
82 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
84 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
85 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
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();
94 CoreConnectDlg::~CoreConnectDlg() {
95 if(ui.accountList->selectedItems().count()) {
96 CoreAccountSettings s;
97 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
102 /****************************************************
104 ***************************************************/
106 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
107 setAccountWidgetStates();
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);
118 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
121 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
123 autoConnectAccount = 0;
125 if(ui.accountList->selectedItems().count()) {
126 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
128 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
129 autoConnectAccount = 0;
132 setAccountWidgetStates();
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) {
141 AccountId id = accounts.count() + 1;
142 for(AccountId i = 1; i <= accounts.count(); i++) {
143 if(!accounts.keys().contains(i)) {
148 QVariantMap data = dlg.accountData();
149 data["AccountId"] = QVariant::fromValue<AccountId>(id);
151 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
152 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
153 ui.accountList->setCurrentItem(item);
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());
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();
185 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
187 on_accountButtonBox_accepted();
190 void CoreConnectDlg::on_accountButtonBox_accepted() {
192 CoreAccountSettings s;
193 foreach(QVariantMap acct, accounts.values()) {
194 AccountId id = acct["AccountId"].value<AccountId>();
195 if(acct.contains("Delete")) {
198 s.storeAccountData(id, acct);
201 s.setAutoConnectAccount(autoConnectAccount);
203 ui.stackedWidget->setCurrentWidget(ui.loginPage);
204 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
205 accountData = accounts[account];
206 s.setLastAccount(account);
210 /*****************************************************
211 * Connecting to the Core
212 ****************************************************/
214 /*** Phase One: initializing the core connection ***/
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()));
228 clientSyncer->connectToCore(accountData);
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()));
245 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
246 ui.coreInfoLabel->setText(msg);
249 void CoreConnectDlg::encrypted(bool useSsl) {
251 ui.secureConnection->show();
253 ui.secureConnection->hide();
256 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
258 QString host = accountData["Host"].toString();
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;
266 ui.connectLabel->setText(s);
269 void CoreConnectDlg::restartPhaseNull() {
270 doingAutoConnect = false;
271 ui.stackedWidget->setCurrentWidget(ui.accountPage);
272 clientSyncer->disconnectFromCore();
275 /*********************************************************
277 *********************************************************/
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();
293 ui.rememberPasswd->setChecked(false);
294 ui.password->setFocus();
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();
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();
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());
333 void CoreConnectDlg::setLoginWidgetStates() {
334 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
337 void CoreConnectDlg::loginFailed(const QString &error) {
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;
352 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
353 storageBackends = backends;
354 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
356 //on_launchCoreConfigWizard_clicked();
360 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
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()));
374 void CoreConnectDlg::configWizardAccepted() {
376 wizard->deleteLater();
380 void CoreConnectDlg::configWizardRejected() {
382 wizard->deleteLater();
388 /************************************************************
389 * Phase Three: Syncing
390 ************************************************************/
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);
398 ui.stackedWidget->setCurrentWidget(ui.syncPage);
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);
407 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
408 ui.sessionProgress->setRange(0, max);
409 ui.sessionProgress->setValue(val);
413 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
415 ui.networksProgress->setFormat("0/0");
416 ui.networksProgress->setRange(0, 1);
417 ui.networksProgress->setValue(1);
419 ui.networksProgress->setFormat("%v/%m");
420 ui.networksProgress->setRange(0, max);
421 ui.networksProgress->setValue(val);
425 void CoreConnectDlg::syncFinished() {
426 if(!wizard) accept();
429 disconnect(wizard, 0, this, 0);
430 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
434 /*****************************************************************************************
436 *****************************************************************************************/
437 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
441 existing = _existing;
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());
451 ui.useSsl->setChecked(false);
452 ui.useSsl->setEnabled(false);
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());
461 setWindowTitle(tr("Add Core Account"));
463 ui.useSsl->setChecked(false);
464 ui.useSsl->setEnabled(false);
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();
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();
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);
497 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
502 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
507 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {