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"
31 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect)
36 // make it look more native under Mac OS X:
37 setWindowFlags(Qt::Sheet);
39 clientSyncer = new ClientSyncer(this);
42 setAttribute(Qt::WA_DeleteOnClose);
44 doingAutoConnect = false;
46 ui.stackedWidget->setCurrentWidget(ui.accountPage);
48 CoreAccountSettings s;
49 AccountId lastacc = s.lastAccount();
50 autoConnectAccount = s.autoConnectAccount();
51 QListWidgetItem *currentItem = 0;
52 foreach(AccountId id, s.knownAccounts()) {
53 if(!id.isValid()) continue;
54 QVariantMap data = s.retrieveAccountData(id);
55 data["AccountId"] = QVariant::fromValue<AccountId>(id);
57 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
58 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
59 if(id == lastacc) currentItem = item;
61 if(currentItem) ui.accountList->setCurrentItem(currentItem);
62 else ui.accountList->setCurrentRow(0);
64 setAccountWidgetStates();
66 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
67 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
69 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
70 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
71 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
72 connect(clientSyncer, SIGNAL(encrypted(bool)), this, SLOT(encrypted(bool)));
73 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
74 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
75 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
76 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
77 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
78 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
79 connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
80 connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
81 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
83 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
84 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
86 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
87 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
89 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
90 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
91 doingAutoConnect = true;
92 on_accountButtonBox_accepted();
96 CoreConnectDlg::~CoreConnectDlg() {
97 if(ui.accountList->selectedItems().count()) {
98 CoreAccountSettings s;
99 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
104 /****************************************************
106 ***************************************************/
108 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
109 setAccountWidgetStates();
112 void CoreConnectDlg::setAccountWidgetStates() {
113 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
114 ui.editAccount->setEnabled(selectedItems.count());
115 ui.deleteAccount->setEnabled(selectedItems.count());
116 ui.autoConnect->setEnabled(selectedItems.count());
117 if(selectedItems.count()) {
118 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
120 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
123 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
125 autoConnectAccount = 0;
127 if(ui.accountList->selectedItems().count()) {
128 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
130 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
131 autoConnectAccount = 0;
134 setAccountWidgetStates();
137 void CoreConnectDlg::on_addAccount_clicked() {
138 QStringList existing;
139 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
140 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
141 if(dlg.exec() == QDialog::Accepted) {
143 AccountId id = accounts.count() + 1;
144 for(AccountId i = 1; i <= accounts.count(); i++) {
145 if(!accounts.keys().contains(i)) {
150 QVariantMap data = dlg.accountData();
151 data["AccountId"] = QVariant::fromValue<AccountId>(id);
153 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
154 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
155 ui.accountList->setCurrentItem(item);
159 void CoreConnectDlg::on_editAccount_clicked() {
160 QStringList existing;
161 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
162 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
163 QVariantMap acct = accounts[id];
164 CoreAccountEditDlg dlg(id, acct, existing, this);
165 if(dlg.exec() == QDialog::Accepted) {
166 QVariantMap data = dlg.accountData();
167 ui.accountList->currentItem()->setText(data["AccountName"].toString());
172 void CoreConnectDlg::on_deleteAccount_clicked() {
173 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
174 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
175 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
176 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
177 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
178 if(ret == QMessageBox::Yes) {
179 int idx = ui.accountList->currentRow();
180 delete ui.accountList->takeItem(idx);
181 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
182 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
183 setAccountWidgetStates();
187 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
189 on_accountButtonBox_accepted();
192 void CoreConnectDlg::on_accountButtonBox_accepted() {
194 CoreAccountSettings s;
195 foreach(QVariantMap acct, accounts.values()) {
196 AccountId id = acct["AccountId"].value<AccountId>();
197 if(acct.contains("Delete")) {
200 s.storeAccountData(id, acct);
203 s.setAutoConnectAccount(autoConnectAccount);
205 ui.stackedWidget->setCurrentWidget(ui.loginPage);
206 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
207 accountData = accounts[account];
208 s.setLastAccount(account);
212 /*****************************************************
213 * Connecting to the Core
214 ****************************************************/
216 /*** Phase One: initializing the core connection ***/
218 void CoreConnectDlg::connectToCore() {
219 ui.secureConnection->hide();
220 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
221 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
222 ui.coreInfoLabel->setText("");
223 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
224 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
225 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
226 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
227 disconnect(ui.loginButtonBox, 0, this, 0);
228 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
230 clientSyncer->connectToCore(accountData);
233 void CoreConnectDlg::initPhaseError(const QString &error) {
234 doingAutoConnect = false;
235 ui.secureConnection->hide();
236 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
237 //ui.connectLabel->setBrush(QBrush("red"));
238 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
239 ui.coreInfoLabel->setText(error);
240 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
241 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
242 disconnect(ui.loginButtonBox, 0, this, 0);
243 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
244 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
247 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
248 ui.coreInfoLabel->setText(msg);
251 void CoreConnectDlg::encrypted(bool useSsl) {
253 ui.secureConnection->show();
255 ui.secureConnection->hide();
258 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
260 QString host = accountData["Host"].toString();
262 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
263 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
264 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
265 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
266 default: s = tr("Unknown connection state to %1"); break;
268 ui.connectLabel->setText(s);
271 void CoreConnectDlg::restartPhaseNull() {
272 doingAutoConnect = false;
273 ui.stackedWidget->setCurrentWidget(ui.accountPage);
274 clientSyncer->disconnectFromCore();
277 /*********************************************************
279 *********************************************************/
281 void CoreConnectDlg::startLogin() {
282 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
283 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
284 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
285 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
286 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
287 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
288 if(!accountData["User"].toString().isEmpty()) {
289 ui.user->setText(accountData["User"].toString());
290 if(accountData["RememberPasswd"].toBool()) {
291 ui.password->setText(accountData["Password"].toString());
292 ui.rememberPasswd->setChecked(true);
293 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
295 ui.rememberPasswd->setChecked(false);
296 ui.password->setFocus();
298 } else ui.user->setFocus();
299 disconnect(ui.loginButtonBox, 0, this, 0);
300 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
301 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
302 if(doingAutoConnect) doLogin();
305 void CoreConnectDlg::doLogin() {
306 QVariantMap loginData;
307 loginData["User"] = ui.user->text();
308 loginData["Password"] = ui.password->text();
309 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
313 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
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 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
318 ui.loginGroup->setTitle(tr("Logging in..."));
319 ui.user->setDisabled(true);
320 ui.password->setDisabled(true);
321 ui.rememberPasswd->setDisabled(true);
322 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
323 accountData["User"] = loginData["User"];
324 accountData["RememberPasswd"] = loginData["RememberPasswd"];
325 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
326 else accountData.remove("Password");
327 ui.user->setText(loginData["User"].toString());
328 ui.password->setText(loginData["Password"].toString());
329 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
330 CoreAccountSettings s;
331 s.storeAccountData(account, accountData);
332 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
335 void CoreConnectDlg::setLoginWidgetStates() {
336 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
339 void CoreConnectDlg::loginFailed(const QString &error) {
343 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
344 ui.loginGroup->setTitle(tr("Login"));
345 ui.user->setEnabled(true);
346 ui.password->setEnabled(true);
347 ui.rememberPasswd->setEnabled(true);
348 ui.coreInfoLabel->setText(error);
349 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
350 ui.password->setFocus();
351 doingAutoConnect = false;
354 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
355 storageBackends = backends;
356 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
358 //on_launchCoreConfigWizard_clicked();
362 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
364 wizard = new CoreConfigWizard(storageBackends, this);
365 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
366 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
367 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
368 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
369 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
370 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
371 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
372 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
376 void CoreConnectDlg::configWizardAccepted() {
378 wizard->deleteLater();
382 void CoreConnectDlg::configWizardRejected() {
384 wizard->deleteLater();
390 /************************************************************
391 * Phase Three: Syncing
392 ************************************************************/
394 void CoreConnectDlg::startSync() {
395 ui.sessionProgress->setRange(0, 1);
396 ui.sessionProgress->setValue(0);
397 ui.networksProgress->setRange(0, 1);
398 ui.networksProgress->setValue(0);
399 ui.channelsProgress->setRange(0, 1);
400 ui.channelsProgress->setValue(0);
401 ui.ircUsersProgress->setRange(0, 1);
402 ui.ircUsersProgress->setValue(0);
404 ui.stackedWidget->setCurrentWidget(ui.syncPage);
406 ui.loginGroup->setTitle(tr("Login"));
407 ui.user->setEnabled(true);
408 ui.password->setEnabled(true);
409 ui.rememberPasswd->setEnabled(true);
410 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
413 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
414 ui.sessionProgress->setRange(0, max);
415 ui.sessionProgress->setValue(val);
419 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
421 ui.networksProgress->setFormat("0/0");
422 ui.networksProgress->setRange(0, 1);
423 ui.networksProgress->setValue(1);
425 ui.networksProgress->setFormat("%v/%m");
426 ui.networksProgress->setRange(0, max);
427 ui.networksProgress->setValue(val);
431 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
433 ui.channelsProgress->setFormat("0/0");
434 ui.channelsProgress->setRange(0, 1);
435 ui.channelsProgress->setValue(1);
437 ui.channelsProgress->setFormat("%v/%m");
438 ui.channelsProgress->setRange(0, max);
439 ui.channelsProgress->setValue(val);
443 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
445 ui.ircUsersProgress->setFormat("0/0");
446 ui.ircUsersProgress->setRange(0, 1);
447 ui.ircUsersProgress->setValue(1);
449 if(val % 100) return;
450 ui.ircUsersProgress->setFormat("%v/%m");
451 ui.ircUsersProgress->setRange(0, max);
452 ui.ircUsersProgress->setValue(val);
456 void CoreConnectDlg::syncFinished() {
457 if(!wizard) accept();
460 disconnect(wizard, 0, this, 0);
461 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
465 /*****************************************************************************************
467 *****************************************************************************************/
468 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent)
472 existing = _existing;
474 existing.removeAll(acct["AccountName"].toString());
475 ui.host->setText(acct["Host"].toString());
476 ui.port->setValue(acct["Port"].toUInt());
477 ui.useInternal->setChecked(acct["UseInternal"].toBool());
478 ui.accountName->setText(acct["AccountName"].toString());
479 #ifndef QT_NO_OPENSSL
480 ui.useSsl->setChecked(acct["useSsl"].toBool());
482 ui.useSsl->setChecked(false);
483 ui.useSsl->setEnabled(false);
485 ui.useProxy->setChecked(acct["useProxy"].toBool());
486 ui.proxyHost->setText(acct["proxyHost"].toString());
487 ui.proxyPort->setValue(acct["proxyPort"].toUInt());
488 ui.proxyType->setCurrentIndex(acct["proxyType"].toInt() == QNetworkProxy::Socks5Proxy ? 0 : 1);
489 ui.proxyUser->setText(acct["proxyUser"].toString());
490 ui.proxyPassword->setText(acct["proxyPassword"].toString());
492 setWindowTitle(tr("Add Core Account"));
494 ui.useSsl->setChecked(false);
495 ui.useSsl->setEnabled(false);
500 QVariantMap CoreAccountEditDlg::accountData() {
501 account["AccountName"] = ui.accountName->text().trimmed();
502 account["Host"] = ui.host->text().trimmed();
503 account["Port"] = ui.port->value();
504 account["UseInternal"] = ui.useInternal->isChecked();
505 account["useSsl"] = ui.useSsl->isChecked();
506 account["useProxy"] = ui.useProxy->isChecked();
507 account["proxyHost"] = ui.proxyHost->text().trimmed();
508 account["proxyPort"] = ui.proxyPort->value();
509 account["proxyType"] = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
510 account["proxyUser"] = ui.proxyUser->text().trimmed();
511 account["proxyPassword"] = ui.proxyPassword->text().trimmed();
515 void CoreAccountEditDlg::setWidgetStates() {
516 bool ok = !ui.accountName->text().trimmed().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
517 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
520 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
525 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
530 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {