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)
38 ui.editAccount->setIcon(SmallIcon("document-properties"));
39 ui.addAccount->setIcon(SmallIcon("list-add"));
40 ui.deleteAccount->setIcon(SmallIcon("list-remove"));
41 ui.connectIcon->setPixmap(BarIcon("network-disconnect"));
42 ui.secureConnection->setPixmap(SmallIcon("document-encrypt"));
44 if(Quassel::runMode() != Quassel::Monolithic) {
45 ui.useInternalCore->hide();
48 // make it look more native under Mac OS X:
49 setWindowFlags(Qt::Sheet);
51 clientSyncer = new ClientSyncer(this);
52 connect(this, SIGNAL(newClientSyncer(ClientSyncer *)), Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)));
53 emit newClientSyncer(clientSyncer); // announce the new client syncer via the client.
57 doingAutoConnect = false;
59 ui.stackedWidget->setCurrentWidget(ui.accountPage);
61 CoreAccountSettings s;
62 AccountId lastacc = s.lastAccount();
63 autoConnectAccount = s.autoConnectAccount();
64 QListWidgetItem *currentItem = 0;
65 foreach(AccountId id, s.knownAccounts()) {
66 if(!id.isValid()) continue;
67 QVariantMap data = s.retrieveAccountData(id);
68 data["AccountId"] = QVariant::fromValue<AccountId>(id);
70 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
71 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
72 if(id == lastacc) currentItem = item;
74 if(currentItem) ui.accountList->setCurrentItem(currentItem);
75 else ui.accountList->setCurrentRow(0);
77 setAccountWidgetStates();
79 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
80 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
82 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
83 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
84 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
85 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
86 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
87 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
88 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
89 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
90 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
91 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
92 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
94 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
95 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
97 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
98 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
100 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
101 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
102 doingAutoConnect = true;
103 on_accountButtonBox_accepted();
107 CoreConnectDlg::~CoreConnectDlg() {
108 if(ui.accountList->selectedItems().count()) {
109 CoreAccountSettings s;
110 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
115 /****************************************************
117 ***************************************************/
119 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
120 setAccountWidgetStates();
123 void CoreConnectDlg::setAccountWidgetStates() {
124 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
125 ui.editAccount->setEnabled(selectedItems.count());
126 ui.deleteAccount->setEnabled(selectedItems.count());
127 ui.autoConnect->setEnabled(selectedItems.count());
128 if(selectedItems.count()) {
129 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
131 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
134 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
136 autoConnectAccount = 0;
138 if(ui.accountList->selectedItems().count()) {
139 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
141 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
142 autoConnectAccount = 0;
145 setAccountWidgetStates();
148 void CoreConnectDlg::on_addAccount_clicked() {
149 QStringList existing;
150 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
151 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
152 if(dlg.exec() == QDialog::Accepted) {
154 AccountId id = accounts.count() + 1;
155 for(AccountId i = 1; i <= accounts.count(); i++) {
156 if(!accounts.keys().contains(i)) {
161 QVariantMap data = dlg.accountData();
162 data["AccountId"] = QVariant::fromValue<AccountId>(id);
164 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
165 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
166 ui.accountList->setCurrentItem(item);
170 void CoreConnectDlg::on_editAccount_clicked() {
171 QStringList existing;
172 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
173 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
174 QVariantMap acct = accounts[id];
175 CoreAccountEditDlg dlg(id, acct, existing, this);
176 if(dlg.exec() == QDialog::Accepted) {
177 QVariantMap data = dlg.accountData();
178 ui.accountList->currentItem()->setText(data["AccountName"].toString());
183 void CoreConnectDlg::on_deleteAccount_clicked() {
184 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
185 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
186 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
187 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
188 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
189 if(ret == QMessageBox::Yes) {
190 int idx = ui.accountList->currentRow();
191 delete ui.accountList->takeItem(idx);
192 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
193 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
194 setAccountWidgetStates();
198 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
200 on_accountButtonBox_accepted();
203 void CoreConnectDlg::on_accountButtonBox_accepted() {
205 CoreAccountSettings s;
206 foreach(QVariantMap acct, accounts.values()) {
207 AccountId id = acct["AccountId"].value<AccountId>();
208 if(acct.contains("Delete")) {
211 s.storeAccountData(id, acct);
214 s.setAutoConnectAccount(autoConnectAccount);
216 ui.stackedWidget->setCurrentWidget(ui.loginPage);
217 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
218 accountData = accounts[account];
219 s.setLastAccount(account);
223 void CoreConnectDlg::on_useInternalCore_clicked() {
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.accountName->setText(acct["AccountName"].toString());
469 ui.useSsl->setChecked(acct["useSsl"].toBool());
471 ui.useSsl->setChecked(false);
472 ui.useSsl->setEnabled(false);
474 ui.useProxy->setChecked(acct["useProxy"].toBool());
475 ui.proxyHost->setText(acct["proxyHost"].toString());
476 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
477 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
478 ui.proxyUser->setText(acct["proxyUser"].toString());
479 ui.proxyPassword->setText(acct["proxyPassword"].toString());
481 setWindowTitle(tr("Add Core Account"));
483 ui.useSsl->setChecked(false);
484 ui.useSsl->setEnabled(false);
489 QVariantMap CoreAccountEditDlg::accountData() {
490 account["AccountName"] = ui.accountName->text().trimmed();
491 account["Host"] = ui.host->text().trimmed();
492 account["Port"] = ui.port->value();
493 account["useSsl"] = ui.useSsl->isChecked();
494 account["useProxy"] = ui.useProxy->isChecked();
495 account["proxyHost"] = ui.proxyHost->text().trimmed();
496 account["proxyPort"] = ui.proxyPort->value();
497 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
498 account["proxyUser"] = ui.proxyUser->text().trimmed();
499 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
503 void CoreAccountEditDlg::setWidgetStates() {
504 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && !ui.host->text().isEmpty();
505 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
508 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
513 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {