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(bool autoconnect, QWidget *parent)
36 // make it look more native under Mac OS X:
37 setWindowFlags(Qt::Sheet);
39 clientSyncer = new ClientSyncer(this);
42 doingAutoConnect = false;
44 ui.stackedWidget->setCurrentWidget(ui.accountPage);
46 CoreAccountSettings s;
47 AccountId lastacc = s.lastAccount();
48 autoConnectAccount = s.autoConnectAccount();
49 QListWidgetItem *currentItem = 0;
50 foreach(AccountId id, s.knownAccounts()) {
51 if(!id.isValid()) continue;
52 QVariantMap data = s.retrieveAccountData(id);
53 data["AccountId"] = QVariant::fromValue<AccountId>(id);
55 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
56 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
57 if(id == lastacc) currentItem = item;
59 if(currentItem) ui.accountList->setCurrentItem(currentItem);
60 else ui.accountList->setCurrentRow(0);
62 setAccountWidgetStates();
64 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
65 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
67 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
68 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
69 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
70 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
71 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
72 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
73 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
74 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
75 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
76 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
77 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
79 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
80 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
82 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
83 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
85 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
86 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
87 doingAutoConnect = true;
88 on_accountButtonBox_accepted();
92 CoreConnectDlg::~CoreConnectDlg() {
93 if(ui.accountList->selectedItems().count()) {
94 CoreAccountSettings s;
95 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
100 /****************************************************
102 ***************************************************/
104 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
105 setAccountWidgetStates();
108 void CoreConnectDlg::setAccountWidgetStates() {
109 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
110 ui.editAccount->setEnabled(selectedItems.count());
111 ui.deleteAccount->setEnabled(selectedItems.count());
112 ui.autoConnect->setEnabled(selectedItems.count());
113 if(selectedItems.count()) {
114 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
116 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
119 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
121 autoConnectAccount = 0;
123 if(ui.accountList->selectedItems().count()) {
124 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
126 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
127 autoConnectAccount = 0;
130 setAccountWidgetStates();
133 void CoreConnectDlg::on_addAccount_clicked() {
134 QStringList existing;
135 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
136 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
137 if(dlg.exec() == QDialog::Accepted) {
139 AccountId id = accounts.count() + 1;
140 for(AccountId i = 1; i <= accounts.count(); i++) {
141 if(!accounts.keys().contains(i)) {
146 QVariantMap data = dlg.accountData();
147 data["AccountId"] = QVariant::fromValue<AccountId>(id);
149 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
150 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
151 ui.accountList->setCurrentItem(item);
155 void CoreConnectDlg::on_editAccount_clicked() {
156 QStringList existing;
157 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
158 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
159 QVariantMap acct = accounts[id];
160 CoreAccountEditDlg dlg(id, acct, existing, this);
161 if(dlg.exec() == QDialog::Accepted) {
162 QVariantMap data = dlg.accountData();
163 ui.accountList->currentItem()->setText(data["AccountName"].toString());
168 void CoreConnectDlg::on_deleteAccount_clicked() {
169 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
170 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
171 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
172 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
173 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
174 if(ret == QMessageBox::Yes) {
175 int idx = ui.accountList->currentRow();
176 delete ui.accountList->takeItem(idx);
177 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
178 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
179 setAccountWidgetStates();
183 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
185 on_accountButtonBox_accepted();
188 void CoreConnectDlg::on_accountButtonBox_accepted() {
190 CoreAccountSettings s;
191 foreach(QVariantMap acct, accounts.values()) {
192 AccountId id = acct["AccountId"].value<AccountId>();
193 if(acct.contains("Delete")) {
196 s.storeAccountData(id, acct);
199 s.setAutoConnectAccount(autoConnectAccount);
201 ui.stackedWidget->setCurrentWidget(ui.loginPage);
202 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
203 accountData = accounts[account];
204 s.setLastAccount(account);
208 /*****************************************************
209 * Connecting to the Core
210 ****************************************************/
212 /*** Phase One: initializing the core connection ***/
214 void CoreConnectDlg::connectToCore() {
215 ui.secureConnection->hide();
216 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
217 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
218 ui.coreInfoLabel->setText("");
219 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
220 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
221 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
222 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
223 disconnect(ui.loginButtonBox, 0, this, 0);
224 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
226 clientSyncer->connectToCore(accountData);
229 void CoreConnectDlg::initPhaseError(const QString &error) {
230 doingAutoConnect = false;
231 ui.secureConnection->hide();
232 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
233 //ui.connectLabel->setBrush(QBrush("red"));
234 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
235 ui.coreInfoLabel->setText(error);
236 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
237 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
238 disconnect(ui.loginButtonBox, 0, this, 0);
239 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
240 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
243 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
244 ui.coreInfoLabel->setText(msg);
247 void CoreConnectDlg::encrypted(bool useSsl) {
249 ui.secureConnection->show();
251 ui.secureConnection->hide();
254 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
256 QString host = accountData["Host"].toString();
258 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
259 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
260 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
261 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
262 default: s = tr("Unknown connection state to %1"); break;
264 ui.connectLabel->setText(s);
267 void CoreConnectDlg::restartPhaseNull() {
268 doingAutoConnect = false;
269 ui.stackedWidget->setCurrentWidget(ui.accountPage);
270 clientSyncer->disconnectFromCore();
273 /*********************************************************
275 *********************************************************/
277 void CoreConnectDlg::startLogin() {
278 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
279 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
280 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
281 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
282 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
283 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
284 if(!accountData["User"].toString().isEmpty()) {
285 ui.user->setText(accountData["User"].toString());
286 if(accountData["RememberPasswd"].toBool()) {
287 ui.password->setText(accountData["Password"].toString());
288 ui.rememberPasswd->setChecked(true);
289 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
291 ui.rememberPasswd->setChecked(false);
292 ui.password->setFocus();
294 } else ui.user->setFocus();
295 disconnect(ui.loginButtonBox, 0, this, 0);
296 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
297 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
298 if(doingAutoConnect) doLogin();
301 void CoreConnectDlg::doLogin() {
302 QVariantMap loginData;
303 loginData["User"] = ui.user->text();
304 loginData["Password"] = ui.password->text();
305 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
309 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
310 disconnect(ui.loginButtonBox, 0, this, 0);
311 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
312 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
313 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
314 ui.loginGroup->setTitle(tr("Logging in..."));
315 ui.user->setDisabled(true);
316 ui.password->setDisabled(true);
317 ui.rememberPasswd->setDisabled(true);
318 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
319 accountData["User"] = loginData["User"];
320 accountData["RememberPasswd"] = loginData["RememberPasswd"];
321 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
322 else accountData.remove("Password");
323 ui.user->setText(loginData["User"].toString());
324 ui.password->setText(loginData["Password"].toString());
325 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
326 CoreAccountSettings s;
327 s.storeAccountData(account, accountData);
328 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
331 void CoreConnectDlg::setLoginWidgetStates() {
332 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
335 void CoreConnectDlg::loginFailed(const QString &error) {
339 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
340 ui.loginGroup->setTitle(tr("Login"));
341 ui.user->setEnabled(true);
342 ui.password->setEnabled(true);
343 ui.rememberPasswd->setEnabled(true);
344 ui.coreInfoLabel->setText(error);
345 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
346 ui.password->setFocus();
347 doingAutoConnect = false;
350 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
351 storageBackends = backends;
352 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
354 //on_launchCoreConfigWizard_clicked();
358 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
360 wizard = new CoreConfigWizard(storageBackends, this);
361 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
362 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
363 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
364 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
365 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
366 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
367 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
368 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
372 void CoreConnectDlg::configWizardAccepted() {
374 wizard->deleteLater();
378 void CoreConnectDlg::configWizardRejected() {
380 wizard->deleteLater();
386 /************************************************************
387 * Phase Three: Syncing
388 ************************************************************/
390 void CoreConnectDlg::startSync() {
391 ui.sessionProgress->setRange(0, 1);
392 ui.sessionProgress->setValue(0);
393 ui.networksProgress->setRange(0, 1);
394 ui.networksProgress->setValue(0);
396 ui.stackedWidget->setCurrentWidget(ui.syncPage);
398 ui.loginGroup->setTitle(tr("Login"));
399 ui.user->setEnabled(true);
400 ui.password->setEnabled(true);
401 ui.rememberPasswd->setEnabled(true);
402 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
405 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
406 ui.sessionProgress->setRange(0, max);
407 ui.sessionProgress->setValue(val);
411 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
413 ui.networksProgress->setFormat("0/0");
414 ui.networksProgress->setRange(0, 1);
415 ui.networksProgress->setValue(1);
417 ui.networksProgress->setFormat("%v/%m");
418 ui.networksProgress->setRange(0, max);
419 ui.networksProgress->setValue(val);
423 void CoreConnectDlg::syncFinished() {
424 if(!wizard) accept();
427 disconnect(wizard, 0, this, 0);
428 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
432 /*****************************************************************************************
434 *****************************************************************************************/
435 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
439 existing = _existing;
441 existing.removeAll(acct["AccountName"].toString());
442 ui.host->setText(acct["Host"].toString());
443 ui.port->setValue(acct["Port"].toUInt());
444 ui.useInternal->setChecked(acct["UseInternal"].toBool());
445 ui.accountName->setText(acct["AccountName"].toString());
447 ui.useSsl->setChecked(acct["useSsl"].toBool());
449 ui.useSsl->setChecked(false);
450 ui.useSsl->setEnabled(false);
452 ui.useProxy->setChecked(acct["useProxy"].toBool());
453 ui.proxyHost->setText(acct["proxyHost"].toString());
454 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
455 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
456 ui.proxyUser->setText(acct["proxyUser"].toString());
457 ui.proxyPassword->setText(acct["proxyPassword"].toString());
459 setWindowTitle(tr("Add Core Account"));
461 ui.useSsl->setChecked(false);
462 ui.useSsl->setEnabled(false);
467 // 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
468 ui.useInternal->setChecked(false);
469 ui.useInternal->hide();
470 ui.useRemote->hide();
471 ui.labelUseBuiltinCore->hide();
475 QVariantMap CoreAccountEditDlg::accountData() {
476 account["AccountName"] = ui.accountName->text().trimmed();
477 account["Host"] = ui.host->text().trimmed();
478 account["Port"] = ui.port->value();
479 account["UseInternal"] = ui.useInternal->isChecked();
480 account["useSsl"] = ui.useSsl->isChecked();
481 account["useProxy"] = ui.useProxy->isChecked();
482 account["proxyHost"] = ui.proxyHost->text().trimmed();
483 account["proxyPort"] = ui.proxyPort->value();
484 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
485 account["proxyUser"] = ui.proxyUser->text().trimmed();
486 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
490 void CoreAccountEditDlg::setWidgetStates() {
491 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
492 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
495 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
500 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
505 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {