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"
31 #include "monoapplication.h"
33 CoreConnectDlg::CoreConnectDlg(bool autoconnect, QWidget *parent)
37 ui.editAccount->setIcon(SmallIcon("document-properties"));
38 ui.addAccount->setIcon(SmallIcon("list-add"));
39 ui.deleteAccount->setIcon(SmallIcon("list-remove"));
40 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
41 ui.secureConnection->setPixmap(SmallIcon("document-encrypt"));
43 // make it look more native under Mac OS X:
44 setWindowFlags(Qt::Sheet);
46 clientSyncer = new ClientSyncer(this);
50 doingAutoConnect = false;
52 ui.stackedWidget->setCurrentWidget(ui.accountPage);
54 CoreAccountSettings s;
55 AccountId lastacc = s.lastAccount();
56 autoConnectAccount = s.autoConnectAccount();
57 QListWidgetItem *currentItem = 0;
58 foreach(AccountId id, s.knownAccounts()) {
59 if(!id.isValid()) continue;
60 QVariantMap data = s.retrieveAccountData(id);
61 data["AccountId"] = QVariant::fromValue<AccountId>(id);
63 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
64 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
65 if(id == lastacc) currentItem = item;
67 if(currentItem) ui.accountList->setCurrentItem(currentItem);
68 else ui.accountList->setCurrentRow(0);
70 setAccountWidgetStates();
72 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
73 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
75 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
76 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
77 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
78 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
79 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
80 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
81 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
82 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
83 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
84 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
85 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
87 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
88 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
90 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
91 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
93 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
94 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
95 doingAutoConnect = true;
96 on_accountButtonBox_accepted();
100 CoreConnectDlg::~CoreConnectDlg() {
101 if(ui.accountList->selectedItems().count()) {
102 CoreAccountSettings s;
103 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
108 /****************************************************
110 ***************************************************/
112 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
113 setAccountWidgetStates();
116 void CoreConnectDlg::setAccountWidgetStates() {
117 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
118 ui.editAccount->setEnabled(selectedItems.count());
119 ui.deleteAccount->setEnabled(selectedItems.count());
120 ui.autoConnect->setEnabled(selectedItems.count());
121 if(selectedItems.count()) {
122 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
124 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
127 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
129 autoConnectAccount = 0;
131 if(ui.accountList->selectedItems().count()) {
132 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
134 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
135 autoConnectAccount = 0;
138 setAccountWidgetStates();
141 void CoreConnectDlg::on_addAccount_clicked() {
142 QStringList existing;
143 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
144 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
145 if(dlg.exec() == QDialog::Accepted) {
147 AccountId id = accounts.count() + 1;
148 for(AccountId i = 1; i <= accounts.count(); i++) {
149 if(!accounts.keys().contains(i)) {
154 QVariantMap data = dlg.accountData();
155 data["AccountId"] = QVariant::fromValue<AccountId>(id);
157 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
158 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
159 ui.accountList->setCurrentItem(item);
163 void CoreConnectDlg::on_editAccount_clicked() {
164 QStringList existing;
165 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
166 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
167 QVariantMap acct = accounts[id];
168 CoreAccountEditDlg dlg(id, acct, existing, this);
169 if(dlg.exec() == QDialog::Accepted) {
170 QVariantMap data = dlg.accountData();
171 ui.accountList->currentItem()->setText(data["AccountName"].toString());
176 void CoreConnectDlg::on_deleteAccount_clicked() {
177 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
178 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
179 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
180 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
181 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
182 if(ret == QMessageBox::Yes) {
183 int idx = ui.accountList->currentRow();
184 delete ui.accountList->takeItem(idx);
185 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
186 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
187 setAccountWidgetStates();
191 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
193 on_accountButtonBox_accepted();
196 void CoreConnectDlg::on_accountButtonBox_accepted() {
198 CoreAccountSettings s;
199 foreach(QVariantMap acct, accounts.values()) {
200 AccountId id = acct["AccountId"].value<AccountId>();
201 if(acct.contains("Delete")) {
204 s.storeAccountData(id, acct);
207 s.setAutoConnectAccount(autoConnectAccount);
209 ui.stackedWidget->setCurrentWidget(ui.loginPage);
210 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
211 accountData = accounts[account];
212 s.setLastAccount(account);
216 void CoreConnectDlg::on_useInternalCore_clicked() {
217 // FIXME: this needs to be a qobject_cast - therefore MonolithicApplication needs to be a proper QObject... :/
218 MonolithicApplication *monoApp = static_cast<MonolithicApplication *>(QApplication::instance());
220 qDebug() << "starting core...";
221 monoApp->startInternalCore();
222 monoApp->connectClientSyncer(clientSyncer);
224 clientSyncer->useInternalCore();
228 /*****************************************************
229 * Connecting to the Core
230 ****************************************************/
232 /*** Phase One: initializing the core connection ***/
234 void CoreConnectDlg::connectToCore() {
235 ui.secureConnection->hide();
236 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
237 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
238 ui.coreInfoLabel->setText("");
239 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
240 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
241 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
242 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
243 disconnect(ui.loginButtonBox, 0, this, 0);
244 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
246 clientSyncer->connectToCore(accountData);
249 void CoreConnectDlg::initPhaseError(const QString &error) {
250 doingAutoConnect = false;
251 ui.secureConnection->hide();
252 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
253 //ui.connectLabel->setBrush(QBrush("red"));
254 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
255 ui.coreInfoLabel->setText(error);
256 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
257 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
258 disconnect(ui.loginButtonBox, 0, this, 0);
259 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
260 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
263 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
264 ui.coreInfoLabel->setText(msg);
267 void CoreConnectDlg::encrypted(bool useSsl) {
269 ui.secureConnection->show();
271 ui.secureConnection->hide();
274 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
276 QString host = accountData["Host"].toString();
278 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
279 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
280 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
281 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
282 default: s = tr("Unknown connection state to %1"); break;
284 ui.connectLabel->setText(s);
287 void CoreConnectDlg::restartPhaseNull() {
288 doingAutoConnect = false;
289 ui.stackedWidget->setCurrentWidget(ui.accountPage);
290 clientSyncer->disconnectFromCore();
293 /*********************************************************
295 *********************************************************/
297 void CoreConnectDlg::startLogin() {
298 ui.connectIcon->setPixmap(BarIcon("network-connect"));
299 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
300 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
301 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
302 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
303 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
304 if(!accountData["User"].toString().isEmpty()) {
305 ui.user->setText(accountData["User"].toString());
306 if(accountData["RememberPasswd"].toBool()) {
307 ui.password->setText(accountData["Password"].toString());
308 ui.rememberPasswd->setChecked(true);
309 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
311 ui.rememberPasswd->setChecked(false);
312 ui.password->setFocus();
314 } else ui.user->setFocus();
315 disconnect(ui.loginButtonBox, 0, this, 0);
316 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
317 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
318 if(doingAutoConnect) doLogin();
321 void CoreConnectDlg::doLogin() {
322 QVariantMap loginData;
323 loginData["User"] = ui.user->text();
324 loginData["Password"] = ui.password->text();
325 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
329 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
330 disconnect(ui.loginButtonBox, 0, this, 0);
331 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
332 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
333 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
334 ui.loginGroup->setTitle(tr("Logging in..."));
335 ui.user->setDisabled(true);
336 ui.password->setDisabled(true);
337 ui.rememberPasswd->setDisabled(true);
338 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
339 accountData["User"] = loginData["User"];
340 accountData["RememberPasswd"] = loginData["RememberPasswd"];
341 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
342 else accountData.remove("Password");
343 ui.user->setText(loginData["User"].toString());
344 ui.password->setText(loginData["Password"].toString());
345 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
346 CoreAccountSettings s;
347 s.storeAccountData(account, accountData);
348 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
351 void CoreConnectDlg::setLoginWidgetStates() {
352 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
355 void CoreConnectDlg::loginFailed(const QString &error) {
359 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
360 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
361 ui.loginGroup->setTitle(tr("Login"));
362 ui.user->setEnabled(true);
363 ui.password->setEnabled(true);
364 ui.rememberPasswd->setEnabled(true);
365 ui.coreInfoLabel->setText(error);
366 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
367 ui.password->setFocus();
368 doingAutoConnect = false;
371 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
372 storageBackends = backends;
373 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
375 //on_launchCoreConfigWizard_clicked();
379 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
381 wizard = new CoreConfigWizard(storageBackends, this);
382 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
383 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
384 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
385 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
386 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
387 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
388 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
389 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
393 void CoreConnectDlg::configWizardAccepted() {
395 wizard->deleteLater();
399 void CoreConnectDlg::configWizardRejected() {
401 wizard->deleteLater();
407 /************************************************************
408 * Phase Three: Syncing
409 ************************************************************/
411 void CoreConnectDlg::startSync() {
412 ui.sessionProgress->setRange(0, 1);
413 ui.sessionProgress->setValue(0);
414 ui.networksProgress->setRange(0, 1);
415 ui.networksProgress->setValue(0);
417 ui.stackedWidget->setCurrentWidget(ui.syncPage);
419 ui.loginGroup->setTitle(tr("Login"));
420 ui.user->setEnabled(true);
421 ui.password->setEnabled(true);
422 ui.rememberPasswd->setEnabled(true);
423 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
426 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
427 ui.sessionProgress->setRange(0, max);
428 ui.sessionProgress->setValue(val);
432 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
434 ui.networksProgress->setFormat("0/0");
435 ui.networksProgress->setRange(0, 1);
436 ui.networksProgress->setValue(1);
438 ui.networksProgress->setFormat("%v/%m");
439 ui.networksProgress->setRange(0, max);
440 ui.networksProgress->setValue(val);
444 void CoreConnectDlg::syncFinished() {
445 if(!wizard) accept();
448 disconnect(wizard, 0, this, 0);
449 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
453 /*****************************************************************************************
455 *****************************************************************************************/
456 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
460 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
462 existing = _existing;
464 existing.removeAll(acct["AccountName"].toString());
465 ui.host->setText(acct["Host"].toString());
466 ui.port->setValue(acct["Port"].toUInt());
467 ui.useInternal->setChecked(acct["UseInternal"].toBool());
468 ui.accountName->setText(acct["AccountName"].toString());
470 ui.useSsl->setChecked(acct["useSsl"].toBool());
472 ui.useSsl->setChecked(false);
473 ui.useSsl->setEnabled(false);
475 ui.useProxy->setChecked(acct["useProxy"].toBool());
476 ui.proxyHost->setText(acct["proxyHost"].toString());
477 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
478 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
479 ui.proxyUser->setText(acct["proxyUser"].toString());
480 ui.proxyPassword->setText(acct["proxyPassword"].toString());
482 setWindowTitle(tr("Add Core Account"));
484 ui.useSsl->setChecked(false);
485 ui.useSsl->setEnabled(false);
490 // 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
491 ui.useInternal->setChecked(false);
492 ui.useInternal->hide();
493 ui.useRemote->hide();
494 ui.labelUseBuiltinCore->hide();
498 QVariantMap CoreAccountEditDlg::accountData() {
499 account["AccountName"] = ui.accountName->text().trimmed();
500 account["Host"] = ui.host->text().trimmed();
501 account["Port"] = ui.port->value();
502 account["UseInternal"] = ui.useInternal->isChecked();
503 account["useSsl"] = ui.useSsl->isChecked();
504 account["useProxy"] = ui.useProxy->isChecked();
505 account["proxyHost"] = ui.proxyHost->text().trimmed();
506 account["proxyPort"] = ui.proxyPort->value();
507 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
508 account["proxyUser"] = ui.proxyUser->text().trimmed();
509 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
513 void CoreAccountEditDlg::setWidgetStates() {
514 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
515 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
518 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
523 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
528 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {