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"
28 #include "clientsettings.h"
29 #include "clientsyncer.h"
30 #include "coreconfigwizard.h"
31 #include "iconloader.h"
34 CoreConnectDlg::CoreConnectDlg(bool autoconnect, QWidget *parent)
39 ui.editAccount->setIcon(SmallIcon("document-properties"));
40 ui.addAccount->setIcon(SmallIcon("list-add"));
41 ui.deleteAccount->setIcon(SmallIcon("list-remove"));
42 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
43 ui.secureConnection->setPixmap(SmallIcon("document-encrypt"));
45 if(Quassel::runMode() != Quassel::Monolithic) {
46 ui.useInternalCore->hide();
49 // make it look more native under Mac OS X:
50 setWindowFlags(Qt::Sheet);
52 clientSyncer = new ClientSyncer(this);
53 connect(this, SIGNAL(newClientSyncer(ClientSyncer *)), Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)));
54 emit newClientSyncer(clientSyncer); // announce the new client syncer via the client.
58 doingAutoConnect = false;
60 ui.stackedWidget->setCurrentWidget(ui.accountPage);
62 CoreAccountSettings s;
63 AccountId lastacc = s.lastAccount();
64 autoConnectAccount = s.autoConnectAccount();
65 QListWidgetItem *currentItem = 0;
66 foreach(AccountId id, s.knownAccounts()) {
67 if(!id.isValid()) continue;
68 QVariantMap data = s.retrieveAccountData(id);
69 if(data.contains("InternalAccount") && data["InternalAccount"].toBool()) {
70 _internalAccountId = id;
73 data["AccountId"] = QVariant::fromValue<AccountId>(id);
75 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
76 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
77 if(id == lastacc) currentItem = item;
79 if(currentItem) ui.accountList->setCurrentItem(currentItem);
80 else ui.accountList->setCurrentRow(0);
82 setAccountWidgetStates();
84 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
85 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
87 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
88 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
89 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
90 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
91 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
92 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
93 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
94 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
95 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
96 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
97 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
99 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
100 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
102 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
103 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
105 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
106 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
107 doingAutoConnect = true;
108 on_accountButtonBox_accepted();
112 CoreConnectDlg::~CoreConnectDlg() {
113 if(ui.accountList->selectedItems().count()) {
114 CoreAccountSettings s;
115 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
120 /****************************************************
122 ***************************************************/
124 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
125 setAccountWidgetStates();
128 void CoreConnectDlg::setAccountWidgetStates() {
129 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
130 ui.editAccount->setEnabled(selectedItems.count());
131 ui.deleteAccount->setEnabled(selectedItems.count());
132 ui.autoConnect->setEnabled(selectedItems.count());
133 if(selectedItems.count()) {
134 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
136 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
139 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
141 autoConnectAccount = 0;
143 if(ui.accountList->selectedItems().count()) {
144 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
146 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
147 autoConnectAccount = 0;
150 setAccountWidgetStates();
153 void CoreConnectDlg::on_addAccount_clicked() {
154 QStringList existing;
155 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
156 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
157 if(dlg.exec() == QDialog::Accepted) {
158 AccountId id = findFreeAccountId();
159 QVariantMap data = dlg.accountData();
160 data["AccountId"] = QVariant::fromValue<AccountId>(id);
162 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
163 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
164 ui.accountList->setCurrentItem(item);
168 void CoreConnectDlg::on_editAccount_clicked() {
169 QStringList existing;
170 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
171 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
172 QVariantMap acct = accounts[id];
173 CoreAccountEditDlg dlg(id, acct, existing, this);
174 if(dlg.exec() == QDialog::Accepted) {
175 QVariantMap data = dlg.accountData();
176 ui.accountList->currentItem()->setText(data["AccountName"].toString());
181 void CoreConnectDlg::on_deleteAccount_clicked() {
182 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
183 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
184 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
185 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
186 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
187 if(ret == QMessageBox::Yes) {
188 int idx = ui.accountList->currentRow();
189 delete ui.accountList->takeItem(idx);
190 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
191 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
192 setAccountWidgetStates();
196 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
198 on_accountButtonBox_accepted();
201 void CoreConnectDlg::on_accountButtonBox_accepted() {
203 CoreAccountSettings s;
204 foreach(QVariantMap acct, accounts.values()) {
205 AccountId id = acct["AccountId"].value<AccountId>();
206 if(acct.contains("Delete")) {
209 s.storeAccountData(id, acct);
212 s.setAutoConnectAccount(autoConnectAccount);
214 ui.stackedWidget->setCurrentWidget(ui.loginPage);
215 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
216 accountData = accounts[account];
217 s.setLastAccount(account);
221 void CoreConnectDlg::on_useInternalCore_clicked() {
222 if(!_internalAccountId.isValid()) {
223 _internalAccountId = findFreeAccountId();
225 data["InternalAccount"] = true;
226 CoreAccountSettings accountSettings;
227 accountSettings.storeAccountData(_internalAccountId, data);
229 clientSyncer->useInternalCore(_internalAccountId);
233 /*****************************************************
234 * Connecting to the Core
235 ****************************************************/
237 /*** Phase One: initializing the core connection ***/
239 void CoreConnectDlg::connectToCore() {
240 ui.secureConnection->hide();
241 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
242 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
243 ui.coreInfoLabel->setText("");
244 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
245 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
246 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
247 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
248 disconnect(ui.loginButtonBox, 0, this, 0);
249 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
251 clientSyncer->connectToCore(accountData);
254 void CoreConnectDlg::initPhaseError(const QString &error) {
255 doingAutoConnect = false;
256 ui.secureConnection->hide();
257 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
258 //ui.connectLabel->setBrush(QBrush("red"));
259 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
260 ui.coreInfoLabel->setText(error);
261 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
262 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
263 disconnect(ui.loginButtonBox, 0, this, 0);
264 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
265 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
268 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
269 ui.coreInfoLabel->setText(msg);
272 void CoreConnectDlg::encrypted(bool useSsl) {
274 ui.secureConnection->show();
276 ui.secureConnection->hide();
279 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
281 QString host = accountData["Host"].toString();
283 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
284 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
285 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
286 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
287 default: s = tr("Unknown connection state to %1"); break;
289 ui.connectLabel->setText(s);
292 void CoreConnectDlg::restartPhaseNull() {
293 doingAutoConnect = false;
294 ui.stackedWidget->setCurrentWidget(ui.accountPage);
295 clientSyncer->disconnectFromCore();
298 /*********************************************************
300 *********************************************************/
302 void CoreConnectDlg::startLogin() {
303 ui.connectIcon->setPixmap(BarIcon("network-connect"));
304 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
305 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
306 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
307 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
308 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
309 if(!accountData["User"].toString().isEmpty()) {
310 ui.user->setText(accountData["User"].toString());
311 if(accountData["RememberPasswd"].toBool()) {
312 ui.password->setText(accountData["Password"].toString());
313 ui.rememberPasswd->setChecked(true);
314 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
316 ui.rememberPasswd->setChecked(false);
317 ui.password->setFocus();
319 } else ui.user->setFocus();
320 disconnect(ui.loginButtonBox, 0, this, 0);
321 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
322 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
323 if(doingAutoConnect) doLogin();
326 void CoreConnectDlg::doLogin() {
327 QVariantMap loginData;
328 loginData["User"] = ui.user->text();
329 loginData["Password"] = ui.password->text();
330 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
334 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
335 disconnect(ui.loginButtonBox, 0, this, 0);
336 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
337 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
338 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
339 ui.loginGroup->setTitle(tr("Logging in..."));
340 ui.user->setDisabled(true);
341 ui.password->setDisabled(true);
342 ui.rememberPasswd->setDisabled(true);
343 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
344 accountData["User"] = loginData["User"];
345 accountData["RememberPasswd"] = loginData["RememberPasswd"];
346 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
347 else accountData.remove("Password");
348 ui.user->setText(loginData["User"].toString());
349 ui.password->setText(loginData["Password"].toString());
350 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
351 CoreAccountSettings s;
352 s.storeAccountData(account, accountData);
353 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
356 void CoreConnectDlg::setLoginWidgetStates() {
357 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
360 void CoreConnectDlg::loginFailed(const QString &error) {
364 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
365 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
366 ui.loginGroup->setTitle(tr("Login"));
367 ui.user->setEnabled(true);
368 ui.password->setEnabled(true);
369 ui.rememberPasswd->setEnabled(true);
370 ui.coreInfoLabel->setText(error);
371 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
372 ui.password->setFocus();
373 doingAutoConnect = false;
376 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
377 storageBackends = backends;
378 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
380 //on_launchCoreConfigWizard_clicked();
384 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
386 wizard = new CoreConfigWizard(storageBackends, this);
387 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
388 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
389 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
390 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
391 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
392 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
393 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
394 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
398 void CoreConnectDlg::configWizardAccepted() {
400 wizard->deleteLater();
404 void CoreConnectDlg::configWizardRejected() {
406 wizard->deleteLater();
412 /************************************************************
413 * Phase Three: Syncing
414 ************************************************************/
416 void CoreConnectDlg::startSync() {
417 ui.sessionProgress->setRange(0, 1);
418 ui.sessionProgress->setValue(0);
419 ui.networksProgress->setRange(0, 1);
420 ui.networksProgress->setValue(0);
422 ui.stackedWidget->setCurrentWidget(ui.syncPage);
424 ui.loginGroup->setTitle(tr("Login"));
425 ui.user->setEnabled(true);
426 ui.password->setEnabled(true);
427 ui.rememberPasswd->setEnabled(true);
428 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
431 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
432 ui.sessionProgress->setRange(0, max);
433 ui.sessionProgress->setValue(val);
437 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
439 ui.networksProgress->setFormat("0/0");
440 ui.networksProgress->setRange(0, 1);
441 ui.networksProgress->setValue(1);
443 ui.networksProgress->setFormat("%v/%m");
444 ui.networksProgress->setRange(0, max);
445 ui.networksProgress->setValue(val);
449 void CoreConnectDlg::syncFinished() {
450 if(!wizard) accept();
453 disconnect(wizard, 0, this, 0);
454 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
458 AccountId CoreConnectDlg::findFreeAccountId() {
459 for(AccountId i = 1;; i++) {
460 if(!accounts.contains(i) && i != _internalAccountId)
465 /*****************************************************************************************
467 *****************************************************************************************/
468 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
472 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
474 existing = _existing;
478 existing.removeAll(acct["AccountName"].toString());
479 ui.host->setText(acct["Host"].toString());
480 ui.port->setValue(acct["Port"].toUInt());
481 ui.accountName->setText(acct["AccountName"].toString());
483 ui.useSsl->setChecked(acct["useSsl"].toBool());
485 ui.useSsl->setChecked(false);
486 ui.useSsl->setEnabled(false);
488 ui.useProxy->setChecked(acct["useProxy"].toBool());
489 ui.proxyHost->setText(acct["proxyHost"].toString());
490 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
491 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
492 ui.proxyUser->setText(acct["proxyUser"].toString());
493 ui.proxyPassword->setText(acct["proxyPassword"].toString());
495 setWindowTitle(tr("Add Core Account"));
497 ui.useSsl->setChecked(false);
498 ui.useSsl->setEnabled(false);
503 QVariantMap CoreAccountEditDlg::accountData() {
504 account["AccountName"] = ui.accountName->text().trimmed();
505 account["Host"] = ui.host->text().trimmed();
506 account["Port"] = ui.port->value();
507 account["useSsl"] = ui.useSsl->isChecked();
508 account["useProxy"] = ui.useProxy->isChecked();
509 account["proxyHost"] = ui.proxyHost->text().trimmed();
510 account["proxyPort"] = ui.proxyPort->value();
511 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
512 account["proxyUser"] = ui.proxyUser->text().trimmed();
513 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
517 void CoreAccountEditDlg::setWidgetStates() {
518 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && !ui.host->text().isEmpty();
519 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
522 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
527 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {