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);
230 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel);
234 /*****************************************************
235 * Connecting to the Core
236 ****************************************************/
238 /*** Phase One: initializing the core connection ***/
240 void CoreConnectDlg::connectToCore() {
241 ui.secureConnection->hide();
242 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
243 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
244 ui.coreInfoLabel->setText("");
245 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
246 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
247 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
248 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
249 disconnect(ui.loginButtonBox, 0, this, 0);
250 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
252 clientSyncer->connectToCore(accountData);
255 void CoreConnectDlg::initPhaseError(const QString &error) {
256 doingAutoConnect = false;
257 ui.secureConnection->hide();
258 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
259 //ui.connectLabel->setBrush(QBrush("red"));
260 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
261 ui.coreInfoLabel->setText(error);
262 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
263 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
264 disconnect(ui.loginButtonBox, 0, this, 0);
265 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
266 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
269 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
270 ui.coreInfoLabel->setText(msg);
273 void CoreConnectDlg::encrypted(bool useSsl) {
275 ui.secureConnection->show();
277 ui.secureConnection->hide();
280 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
282 QString host = accountData["Host"].toString();
284 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
285 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
286 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
287 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
288 default: s = tr("Unknown connection state to %1"); break;
290 ui.connectLabel->setText(s);
293 void CoreConnectDlg::restartPhaseNull() {
294 doingAutoConnect = false;
295 ui.stackedWidget->setCurrentWidget(ui.accountPage);
296 clientSyncer->disconnectFromCore();
299 /*********************************************************
301 *********************************************************/
303 void CoreConnectDlg::startLogin() {
304 ui.connectIcon->setPixmap(BarIcon("network-connect"));
305 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
306 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
307 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
308 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
309 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
310 if(!accountData["User"].toString().isEmpty()) {
311 ui.user->setText(accountData["User"].toString());
312 if(accountData["RememberPasswd"].toBool()) {
313 ui.password->setText(accountData["Password"].toString());
314 ui.rememberPasswd->setChecked(true);
315 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
317 ui.rememberPasswd->setChecked(false);
318 ui.password->setFocus();
320 } else ui.user->setFocus();
321 disconnect(ui.loginButtonBox, 0, this, 0);
322 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
323 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
324 if(doingAutoConnect) doLogin();
327 void CoreConnectDlg::doLogin() {
328 QVariantMap loginData;
329 loginData["User"] = ui.user->text();
330 loginData["Password"] = ui.password->text();
331 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
335 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
336 disconnect(ui.loginButtonBox, 0, this, 0);
337 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
338 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
339 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
340 ui.loginGroup->setTitle(tr("Logging in..."));
341 ui.user->setDisabled(true);
342 ui.password->setDisabled(true);
343 ui.rememberPasswd->setDisabled(true);
344 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
345 accountData["User"] = loginData["User"];
346 accountData["RememberPasswd"] = loginData["RememberPasswd"];
347 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
348 else accountData.remove("Password");
349 ui.user->setText(loginData["User"].toString());
350 ui.password->setText(loginData["Password"].toString());
351 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
352 CoreAccountSettings s;
353 s.storeAccountData(account, accountData);
354 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
357 void CoreConnectDlg::setLoginWidgetStates() {
358 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
361 void CoreConnectDlg::loginFailed(const QString &error) {
365 ui.connectIcon->setPixmap(BarIcon("dialog-error"));
366 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
367 ui.loginGroup->setTitle(tr("Login"));
368 ui.user->setEnabled(true);
369 ui.password->setEnabled(true);
370 ui.rememberPasswd->setEnabled(true);
371 ui.coreInfoLabel->setText(error);
372 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
373 ui.password->setFocus();
374 doingAutoConnect = false;
377 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
378 storageBackends = backends;
379 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
381 //on_launchCoreConfigWizard_clicked();
385 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
387 wizard = new CoreConfigWizard(storageBackends, this);
388 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
389 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
390 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
391 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
392 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
393 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
394 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
395 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
399 void CoreConnectDlg::configWizardAccepted() {
401 wizard->deleteLater();
405 void CoreConnectDlg::configWizardRejected() {
407 wizard->deleteLater();
413 /************************************************************
414 * Phase Three: Syncing
415 ************************************************************/
417 void CoreConnectDlg::startSync() {
418 ui.sessionProgress->setRange(0, 1);
419 ui.sessionProgress->setValue(0);
420 ui.networksProgress->setRange(0, 1);
421 ui.networksProgress->setValue(0);
423 ui.stackedWidget->setCurrentWidget(ui.syncPage);
425 ui.loginGroup->setTitle(tr("Login"));
426 ui.user->setEnabled(true);
427 ui.password->setEnabled(true);
428 ui.rememberPasswd->setEnabled(true);
429 if(ui.loginButtonBox->standardButtons() & QDialogButtonBox::Ok) // in mono mode we don't show an Ok Button
430 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
433 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
434 ui.sessionProgress->setRange(0, max);
435 ui.sessionProgress->setValue(val);
439 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
441 ui.networksProgress->setFormat("0/0");
442 ui.networksProgress->setRange(0, 1);
443 ui.networksProgress->setValue(1);
445 ui.networksProgress->setFormat("%v/%m");
446 ui.networksProgress->setRange(0, max);
447 ui.networksProgress->setValue(val);
451 void CoreConnectDlg::syncFinished() {
452 if(!wizard) accept();
455 disconnect(wizard, 0, this, 0);
456 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
460 AccountId CoreConnectDlg::findFreeAccountId() {
461 for(AccountId i = 1;; i++) {
462 if(!accounts.contains(i) && i != _internalAccountId)
467 /*****************************************************************************************
469 *****************************************************************************************/
470 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
474 ui.useSsl->setIcon(SmallIcon("document-encrypt"));
476 existing = _existing;
480 existing.removeAll(acct["AccountName"].toString());
481 ui.host->setText(acct["Host"].toString());
482 ui.port->setValue(acct["Port"].toUInt());
483 ui.accountName->setText(acct["AccountName"].toString());
485 ui.useSsl->setChecked(acct["useSsl"].toBool());
487 ui.useSsl->setChecked(false);
488 ui.useSsl->setEnabled(false);
490 ui.useProxy->setChecked(acct["useProxy"].toBool());
491 ui.proxyHost->setText(acct["proxyHost"].toString());
492 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
493 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
494 ui.proxyUser->setText(acct["proxyUser"].toString());
495 ui.proxyPassword->setText(acct["proxyPassword"].toString());
497 setWindowTitle(tr("Add Core Account"));
499 ui.useSsl->setChecked(false);
500 ui.useSsl->setEnabled(false);
505 QVariantMap CoreAccountEditDlg::accountData() {
506 account["AccountName"] = ui.accountName->text().trimmed();
507 account["Host"] = ui.host->text().trimmed();
508 account["Port"] = ui.port->value();
509 account["useSsl"] = ui.useSsl->isChecked();
510 account["useProxy"] = ui.useProxy->isChecked();
511 account["proxyHost"] = ui.proxyHost->text().trimmed();
512 account["proxyPort"] = ui.proxyPort->value();
513 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
514 account["proxyUser"] = ui.proxyUser->text().trimmed();
515 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
519 void CoreAccountEditDlg::setWidgetStates() {
520 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && !ui.host->text().isEmpty();
521 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
524 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
529 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {