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 Client::registerClientSyncer(clientSyncer);
54 // connect(this, SIGNAL(newClientSyncer(ClientSyncer *)), Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)));
55 // emit newClientSyncer(clientSyncer); // announce the new client syncer via the client.
59 doingAutoConnect = false;
61 ui.stackedWidget->setCurrentWidget(ui.accountPage);
63 CoreAccountSettings s;
64 AccountId lastacc = s.lastAccount();
65 autoConnectAccount = s.autoConnectAccount();
66 QListWidgetItem *currentItem = 0;
67 foreach(AccountId id, s.knownAccounts()) {
68 if(!id.isValid()) continue;
69 QVariantMap data = s.retrieveAccountData(id);
70 if(data.contains("InternalAccount") && data["InternalAccount"].toBool()) {
71 _internalAccountId = id;
74 data["AccountId"] = QVariant::fromValue<AccountId>(id);
76 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
77 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
78 if(id == lastacc) currentItem = item;
80 if(currentItem) ui.accountList->setCurrentItem(currentItem);
81 else ui.accountList->setCurrentRow(0);
83 setAccountWidgetStates();
85 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
86 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
88 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
89 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
90 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
91 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
92 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
93 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
94 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
95 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
96 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
97 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
98 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
100 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
101 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
103 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
104 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
106 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
107 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
108 doingAutoConnect = true;
109 on_accountButtonBox_accepted();
113 CoreConnectDlg::~CoreConnectDlg() {
114 if(ui.accountList->selectedItems().count()) {
115 CoreAccountSettings s;
116 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
121 /****************************************************
123 ***************************************************/
125 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
126 setAccountWidgetStates();
129 void CoreConnectDlg::setAccountWidgetStates() {
130 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
131 ui.editAccount->setEnabled(selectedItems.count());
132 ui.deleteAccount->setEnabled(selectedItems.count());
133 ui.autoConnect->setEnabled(selectedItems.count());
134 if(selectedItems.count()) {
135 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
137 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
140 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
142 autoConnectAccount = 0;
144 if(ui.accountList->selectedItems().count()) {
145 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
147 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
148 autoConnectAccount = 0;
151 setAccountWidgetStates();
154 void CoreConnectDlg::on_addAccount_clicked() {
155 QStringList existing;
156 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
157 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
158 if(dlg.exec() == QDialog::Accepted) {
159 AccountId id = findFreeAccountId();
160 QVariantMap data = dlg.accountData();
161 data["AccountId"] = QVariant::fromValue<AccountId>(id);
163 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
164 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
165 ui.accountList->setCurrentItem(item);
169 void CoreConnectDlg::on_editAccount_clicked() {
170 QStringList existing;
171 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
172 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
173 QVariantMap acct = accounts[id];
174 CoreAccountEditDlg dlg(id, acct, existing, this);
175 if(dlg.exec() == QDialog::Accepted) {
176 QVariantMap data = dlg.accountData();
177 ui.accountList->currentItem()->setText(data["AccountName"].toString());
182 void CoreConnectDlg::on_deleteAccount_clicked() {
183 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
184 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
185 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
186 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
187 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
188 if(ret == QMessageBox::Yes) {
189 int idx = ui.accountList->currentRow();
190 delete ui.accountList->takeItem(idx);
191 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
192 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
193 setAccountWidgetStates();
197 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
199 on_accountButtonBox_accepted();
202 void CoreConnectDlg::on_accountButtonBox_accepted() {
204 CoreAccountSettings s;
205 foreach(QVariantMap acct, accounts.values()) {
206 AccountId id = acct["AccountId"].value<AccountId>();
207 if(acct.contains("Delete")) {
210 s.storeAccountData(id, acct);
213 s.setAutoConnectAccount(autoConnectAccount);
215 ui.stackedWidget->setCurrentWidget(ui.loginPage);
216 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
217 accountData = accounts[account];
218 s.setLastAccount(account);
222 void CoreConnectDlg::on_useInternalCore_clicked() {
223 clientSyncer->useInternalCore();
224 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel);
227 /*****************************************************
228 * Connecting to the Core
229 ****************************************************/
231 /*** Phase One: initializing the core connection ***/
233 void CoreConnectDlg::connectToCore() {
234 ui.secureConnection->hide();
235 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
236 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
237 ui.coreInfoLabel->setText("");
238 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
239 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
240 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
241 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
242 disconnect(ui.loginButtonBox, 0, this, 0);
243 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
245 clientSyncer->connectToCore(accountData);
248 void CoreConnectDlg::initPhaseError(const QString &error) {
249 doingAutoConnect = false;
250 ui.secureConnection->hide();
251 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
252 //ui.connectLabel->setBrush(QBrush("red"));
253 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
254 ui.coreInfoLabel->setText(error);
255 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
256 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
257 disconnect(ui.loginButtonBox, 0, this, 0);
258 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
259 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
262 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
263 ui.coreInfoLabel->setText(msg);
266 void CoreConnectDlg::encrypted(bool useSsl) {
268 ui.secureConnection->show();
270 ui.secureConnection->hide();
273 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
275 QString host = accountData["Host"].toString();
277 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
278 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
279 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
280 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
281 default: s = tr("Unknown connection state to %1"); break;
283 ui.connectLabel->setText(s);
286 void CoreConnectDlg::restartPhaseNull() {
287 doingAutoConnect = false;
288 ui.stackedWidget->setCurrentWidget(ui.accountPage);
289 clientSyncer->disconnectFromCore();
292 /*********************************************************
294 *********************************************************/
296 void CoreConnectDlg::startLogin() {
297 ui.connectIcon->setPixmap(BarIcon("network-connect"));
298 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
299 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
300 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
301 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
302 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
303 if(!accountData["User"].toString().isEmpty()) {
304 ui.user->setText(accountData["User"].toString());
305 if(accountData["RememberPasswd"].toBool()) {
306 ui.password->setText(accountData["Password"].toString());
307 ui.rememberPasswd->setChecked(true);
308 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
310 ui.rememberPasswd->setChecked(false);
311 ui.password->setFocus();
313 } else ui.user->setFocus();
314 disconnect(ui.loginButtonBox, 0, this, 0);
315 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
316 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
317 if(doingAutoConnect) doLogin();
320 void CoreConnectDlg::doLogin() {
321 QVariantMap loginData;
322 loginData["User"] = ui.user->text();
323 loginData["Password"] = ui.password->text();
324 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
328 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
329 disconnect(ui.loginButtonBox, 0, this, 0);
330 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
331 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
332 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
333 ui.loginGroup->setTitle(tr("Logging in..."));
334 ui.user->setDisabled(true);
335 ui.password->setDisabled(true);
336 ui.rememberPasswd->setDisabled(true);
337 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
338 accountData["User"] = loginData["User"];
339 accountData["RememberPasswd"] = loginData["RememberPasswd"];
340 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
341 else accountData.remove("Password");
342 ui.user->setText(loginData["User"].toString());
343 ui.password->setText(loginData["Password"].toString());
344 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
345 CoreAccountSettings s;
346 s.storeAccountData(account, accountData);
347 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
350 void CoreConnectDlg::setLoginWidgetStates() {
351 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
354 void CoreConnectDlg::loginFailed(const QString &error) {
358 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
359 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
360 ui.loginGroup->setTitle(tr("Login"));
361 ui.user->setEnabled(true);
362 ui.password->setEnabled(true);
363 ui.rememberPasswd->setEnabled(true);
364 ui.coreInfoLabel->setText(error);
365 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
366 ui.password->setFocus();
367 doingAutoConnect = false;
370 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
371 storageBackends = backends;
372 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
374 //on_launchCoreConfigWizard_clicked();
378 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
380 wizard = new CoreConfigWizard(storageBackends, this);
381 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
382 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
383 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
384 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
385 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
386 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
387 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
388 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
392 void CoreConnectDlg::configWizardAccepted() {
394 wizard->deleteLater();
398 void CoreConnectDlg::configWizardRejected() {
400 wizard->deleteLater();
406 /************************************************************
407 * Phase Three: Syncing
408 ************************************************************/
410 void CoreConnectDlg::startSync() {
411 ui.sessionProgress->setRange(0, 1);
412 ui.sessionProgress->setValue(0);
413 ui.networksProgress->setRange(0, 1);
414 ui.networksProgress->setValue(0);
416 ui.stackedWidget->setCurrentWidget(ui.syncPage);
418 ui.loginGroup->setTitle(tr("Login"));
419 ui.user->setEnabled(true);
420 ui.password->setEnabled(true);
421 ui.rememberPasswd->setEnabled(true);
422 if(ui.loginButtonBox->standardButtons() & QDialogButtonBox::Ok) // in mono mode we don't show an Ok Button
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 AccountId CoreConnectDlg::findFreeAccountId() {
454 for(AccountId i = 1;; i++) {
455 if(!accounts.contains(i) && i != _internalAccountId)
460 /*****************************************************************************************
462 *****************************************************************************************/
463 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
467 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
469 existing = _existing;
473 existing.removeAll(acct["AccountName"].toString());
474 ui.host->setText(acct["Host"].toString());
475 ui.port->setValue(acct["Port"].toUInt());
476 ui.accountName->setText(acct["AccountName"].toString());
478 ui.useSsl->setChecked(acct["useSsl"].toBool());
480 ui.useSsl->setChecked(false);
481 ui.useSsl->setEnabled(false);
483 ui.useProxy->setChecked(acct["useProxy"].toBool());
484 ui.proxyHost->setText(acct["proxyHost"].toString());
485 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
486 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
487 ui.proxyUser->setText(acct["proxyUser"].toString());
488 ui.proxyPassword->setText(acct["proxyPassword"].toString());
490 setWindowTitle(tr("Add Core Account"));
492 ui.useSsl->setChecked(false);
493 ui.useSsl->setEnabled(false);
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["useSsl"] = ui.useSsl->isChecked();
503 account["useProxy"] = ui.useProxy->isChecked();
504 account["proxyHost"] = ui.proxyHost->text().trimmed();
505 account["proxyPort"] = ui.proxyPort->value();
506 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
507 account["proxyUser"] = ui.proxyUser->text().trimmed();
508 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
512 void CoreAccountEditDlg::setWidgetStates() {
513 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && !ui.host->text().isEmpty();
514 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
517 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
522 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {