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>
24 #include "coreconnectdlg.h"
26 #include "clientsettings.h"
27 #include "clientsyncer.h"
28 #include "coreconfigwizard.h"
30 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect) : QDialog(parent) {
33 clientSyncer = new ClientSyncer(this);
36 setAttribute(Qt::WA_DeleteOnClose);
38 doingAutoConnect = false;
40 ui.stackedWidget->setCurrentWidget(ui.accountPage);
41 ui.accountButtonBox->setFocus();
42 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
44 CoreAccountSettings s;
45 AccountId lastacc = s.lastAccount();
46 autoConnectAccount = s.autoConnectAccount();
47 QListWidgetItem *currentItem = 0;
48 foreach(AccountId id, s.knownAccounts()) {
49 if(!id.isValid()) continue;
50 QVariantMap data = s.retrieveAccountData(id);
52 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
53 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
54 if(id == lastacc) currentItem = item;
56 if(currentItem) ui.accountList->setCurrentItem(currentItem);
57 else ui.accountList->setCurrentRow(0);
59 setAccountWidgetStates();
61 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
62 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
63 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
64 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
65 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
66 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
67 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
68 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
69 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
70 connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
71 connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
72 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
74 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
75 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
77 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
78 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
80 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
81 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
82 doingAutoConnect = true;
83 on_accountButtonBox_accepted();
87 CoreConnectDlg::~CoreConnectDlg() {
88 if(ui.accountList->selectedItems().count()) {
89 CoreAccountSettings s;
90 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
95 /****************************************************
97 ***************************************************/
99 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
100 setAccountWidgetStates();
103 void CoreConnectDlg::setAccountWidgetStates() {
104 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
105 ui.editAccount->setEnabled(selectedItems.count());
106 ui.deleteAccount->setEnabled(selectedItems.count());
107 ui.autoConnect->setEnabled(selectedItems.count());
108 if(selectedItems.count()) {
109 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
111 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
114 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
116 autoConnectAccount = 0;
118 if(ui.accountList->selectedItems().count()) {
119 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
121 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
122 autoConnectAccount = 0;
125 setAccountWidgetStates();
128 void CoreConnectDlg::on_addAccount_clicked() {
129 QStringList existing;
130 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
131 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
132 if(dlg.exec() == QDialog::Accepted) {
134 AccountId id = accounts.count() + 1;
135 for(AccountId i = 1; i <= accounts.count(); i++) {
136 if(!accounts.keys().contains(i)) {
141 QVariantMap data = dlg.accountData();
142 data["AccountId"] = QVariant::fromValue<AccountId>(id);
144 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
145 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
146 ui.accountList->setCurrentItem(item);
150 void CoreConnectDlg::on_editAccount_clicked() {
151 QStringList existing;
152 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
153 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
154 QVariantMap acct = accounts[id];
155 CoreAccountEditDlg dlg(id, acct, existing, this);
156 if(dlg.exec() == QDialog::Accepted) {
157 QVariantMap data = dlg.accountData();
158 ui.accountList->currentItem()->setText(data["AccountName"].toString());
163 void CoreConnectDlg::on_deleteAccount_clicked() {
164 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
165 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
166 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
167 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
168 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
169 if(ret == QMessageBox::Yes) {
170 int idx = ui.accountList->currentRow();
171 delete ui.accountList->takeItem(idx);
172 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
173 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
174 setAccountWidgetStates();
178 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
180 on_accountButtonBox_accepted();
183 void CoreConnectDlg::on_accountButtonBox_accepted() {
185 CoreAccountSettings s;
186 foreach(QVariantMap acct, accounts.values()) {
187 AccountId id = acct["AccountId"].value<AccountId>();
188 if(acct.contains("Delete")) {
191 s.storeAccountData(id, acct);
194 s.setAutoConnectAccount(autoConnectAccount);
196 ui.stackedWidget->setCurrentWidget(ui.loginPage);
197 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
198 accountData = accounts[account];
199 s.setLastAccount(account);
203 /*****************************************************
204 * Connecting to the Core
205 ****************************************************/
207 /*** Phase One: initializing the core connection ***/
209 void CoreConnectDlg::connectToCore() {
210 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
211 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
212 ui.coreInfoLabel->setText("");
213 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
214 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
215 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
216 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
217 disconnect(ui.loginButtonBox, 0, this, 0);
218 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
221 //connect(Client::instance(), SIGNAL(coreConnectionPhaseOne(const QVariantMap &)), this, SLOT(phaseOneFinished
222 clientSyncer->connectToCore(accountData);
225 void CoreConnectDlg::initPhaseError(const QString &error) {
226 doingAutoConnect = false;
227 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
228 //ui.connectLabel->setBrush(QBrush("red"));
229 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
230 ui.coreInfoLabel->setText(error);
231 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
232 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setDefault(true);
233 disconnect(ui.loginButtonBox, 0, this, 0);
234 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
235 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
238 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
239 ui.coreInfoLabel->setText(msg);
242 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
244 QString host = accountData["Host"].toString();
246 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
247 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
248 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
249 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
250 default: s = tr("Unknown connection state to %1"); break;
252 ui.connectLabel->setText(s);
255 void CoreConnectDlg::restartPhaseNull() {
256 doingAutoConnect = false;
257 ui.stackedWidget->setCurrentWidget(ui.accountPage);
258 clientSyncer->disconnectFromCore();
261 /*********************************************************
263 *********************************************************/
265 void CoreConnectDlg::startLogin() {
266 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
267 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
268 ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
269 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
270 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
271 if(!accountData["User"].toString().isEmpty()) {
272 ui.user->setText(accountData["User"].toString());
273 if(accountData["RememberPasswd"].toBool()) {
274 ui.password->setText(accountData["Password"].toString());
275 ui.rememberPasswd->setChecked(true);
276 ui.loginButtonBox->setFocus();
278 ui.rememberPasswd->setChecked(false);
279 ui.password->setFocus();
281 } else ui.user->setFocus();
282 disconnect(ui.loginButtonBox, 0, this, 0);
283 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
284 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
285 if(doingAutoConnect) doLogin();
288 void CoreConnectDlg::doLogin() {
289 QVariantMap loginData;
290 loginData["User"] = ui.user->text();
291 loginData["Password"] = ui.password->text();
292 loginData["RememberPasswd"] = ui.rememberPasswd->isChecked();
296 void CoreConnectDlg::doLogin(const QVariantMap &loginData) {
297 disconnect(ui.loginButtonBox, 0, this, 0);
298 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
299 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
300 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
301 ui.loginGroup->setTitle(tr("Logging in..."));
302 ui.user->setDisabled(true);
303 ui.password->setDisabled(true);
304 ui.rememberPasswd->setDisabled(true);
305 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
306 accountData["User"] = loginData["User"];
307 accountData["RememberPasswd"] = loginData["RememberPasswd"];
308 if(loginData["RememberPasswd"].toBool()) accountData["Password"] = loginData["Password"];
309 else accountData.remove("Password");
310 ui.user->setText(loginData["User"].toString());
311 ui.password->setText(loginData["Password"].toString());
312 ui.rememberPasswd->setChecked(loginData["RememberPasswd"].toBool());
313 CoreAccountSettings s;
314 s.storeAccountData(account, accountData);
315 clientSyncer->loginToCore(loginData["User"].toString(), loginData["Password"].toString());
318 void CoreConnectDlg::setLoginWidgetStates() {
319 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
322 void CoreConnectDlg::loginFailed(const QString &error) {
326 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
327 ui.loginGroup->setTitle(tr("Login"));
328 ui.user->setEnabled(true);
329 ui.password->setEnabled(true);
330 ui.rememberPasswd->setEnabled(true);
331 ui.coreInfoLabel->setText(error);
332 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
333 ui.password->setFocus();
334 doingAutoConnect = false;
337 void CoreConnectDlg::startCoreConfig(const QVariantList &backends) {
338 storageBackends = backends;
339 ui.loginStack->setCurrentWidget(ui.coreConfigPage);
341 //on_launchCoreConfigWizard_clicked();
345 void CoreConnectDlg::on_launchCoreConfigWizard_clicked() {
347 wizard = new CoreConfigWizard(storageBackends, this);
348 connect(wizard, SIGNAL(setupCore(const QVariant &)), clientSyncer, SLOT(doCoreSetup(const QVariant &)));
349 connect(wizard, SIGNAL(loginToCore(const QVariantMap &)), this, SLOT(doLogin(const QVariantMap &)));
350 connect(clientSyncer, SIGNAL(coreSetupSuccess()), wizard, SLOT(coreSetupSuccess()));
351 connect(clientSyncer, SIGNAL(coreSetupFailed(const QString &)), wizard, SLOT(coreSetupFailed(const QString &)));
352 connect(wizard, SIGNAL(accepted()), this, SLOT(configWizardAccepted()));
353 connect(wizard, SIGNAL(rejected()), this, SLOT(configWizardRejected()));
354 connect(clientSyncer, SIGNAL(loginSuccess()), wizard, SLOT(loginSuccess()));
355 connect(clientSyncer, SIGNAL(syncFinished()), wizard, SLOT(syncFinished()));
359 void CoreConnectDlg::configWizardAccepted() {
361 wizard->deleteLater();
365 void CoreConnectDlg::configWizardRejected() {
367 wizard->deleteLater();
373 /************************************************************
374 * Phase Three: Syncing
375 ************************************************************/
377 void CoreConnectDlg::startSync() {
378 ui.sessionProgress->setRange(0, 1);
379 ui.sessionProgress->setValue(0);
380 ui.networksProgress->setRange(0, 1);
381 ui.networksProgress->setValue(0);
382 ui.channelsProgress->setRange(0, 1);
383 ui.channelsProgress->setValue(0);
384 ui.ircUsersProgress->setRange(0, 1);
385 ui.ircUsersProgress->setValue(0);
387 ui.stackedWidget->setCurrentWidget(ui.syncPage);
389 ui.loginGroup->setTitle(tr("Login"));
390 ui.user->setEnabled(true);
391 ui.password->setEnabled(true);
392 ui.rememberPasswd->setEnabled(true);
393 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
396 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
397 ui.sessionProgress->setRange(0, max);
398 ui.sessionProgress->setValue(val);
402 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
404 ui.networksProgress->setFormat("0/0");
405 ui.networksProgress->setRange(0, 1);
406 ui.networksProgress->setValue(1);
408 ui.networksProgress->setFormat("%v/%m");
409 ui.networksProgress->setRange(0, max);
410 ui.networksProgress->setValue(val);
414 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
416 ui.channelsProgress->setFormat("0/0");
417 ui.channelsProgress->setRange(0, 1);
418 ui.channelsProgress->setValue(1);
420 ui.channelsProgress->setFormat("%v/%m");
421 ui.channelsProgress->setRange(0, max);
422 ui.channelsProgress->setValue(val);
426 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
428 ui.ircUsersProgress->setFormat("0/0");
429 ui.ircUsersProgress->setRange(0, 1);
430 ui.ircUsersProgress->setValue(1);
432 if(val % 100) return;
433 ui.ircUsersProgress->setFormat("%v/%m");
434 ui.ircUsersProgress->setRange(0, max);
435 ui.ircUsersProgress->setValue(val);
439 void CoreConnectDlg::syncFinished() {
440 if(!wizard) accept();
443 disconnect(wizard, 0, this, 0);
444 connect(wizard, SIGNAL(finished(int)), this, SLOT(accept()));
448 /*****************************************************************************************
450 *****************************************************************************************/
452 CoreAccountEditDlg::CoreAccountEditDlg(AccountId id, const QVariantMap &acct, const QStringList &_existing, QWidget *parent) : QDialog(parent) {
454 existing = _existing;
457 existing.removeAll(acct["AccountName"].toString());
458 ui.host->setText(acct["Host"].toString());
459 ui.port->setValue(acct["Port"].toUInt());
460 ui.useInternal->setChecked(acct["UseInternal"].toBool());
461 ui.accountName->setText(acct["AccountName"].toString());
463 setWindowTitle(tr("Add Core Account"));
467 QVariantMap CoreAccountEditDlg::accountData() {
468 account["AccountName"] = ui.accountName->text();
469 account["Host"] = ui.host->text();
470 account["Port"] = ui.port->value();
471 account["UseInternal"] = ui.useInternal->isChecked();
475 void CoreAccountEditDlg::setWidgetStates() {
476 bool ok = !ui.accountName->text().isEmpty() && !existing.contains(ui.accountName->text()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
477 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
480 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
485 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
490 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {