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);
42 CoreAccountSettings s;
43 AccountId lastacc = s.lastAccount();
44 autoConnectAccount = s.autoConnectAccount();
45 QListWidgetItem *currentItem = 0;
46 foreach(AccountId id, s.knownAccounts()) {
47 if(!id.isValid()) continue;
48 QVariantMap data = s.retrieveAccountData(id);
50 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
51 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
52 if(id == lastacc) currentItem = item;
54 if(currentItem) ui.accountList->setCurrentItem(currentItem);
55 else ui.accountList->setCurrentRow(0);
57 setAccountWidgetStates();
59 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setFocus();
60 //ui.accountButtonBox->button(QDialogButtonBox::Ok)->setAutoDefault(true);
62 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
63 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
64 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
65 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
66 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
67 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
68 connect(clientSyncer, SIGNAL(startCoreSetup(const QVariantList &)), this, SLOT(startCoreConfig(const QVariantList &)));
69 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
70 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
71 connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
72 connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
73 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(syncFinished()));
75 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
76 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
78 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
79 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
81 if(autoconnect && ui.accountList->count() && autoConnectAccount.isValid()
82 && autoConnectAccount == ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>()) {
83 doingAutoConnect = true;
84 on_accountButtonBox_accepted();
88 CoreConnectDlg::~CoreConnectDlg() {
89 if(ui.accountList->selectedItems().count()) {
90 CoreAccountSettings s;
91 s.setLastAccount(ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>());
96 /****************************************************
98 ***************************************************/
100 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
101 setAccountWidgetStates();
104 void CoreConnectDlg::setAccountWidgetStates() {
105 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
106 ui.editAccount->setEnabled(selectedItems.count());
107 ui.deleteAccount->setEnabled(selectedItems.count());
108 ui.autoConnect->setEnabled(selectedItems.count());
109 if(selectedItems.count()) {
110 ui.autoConnect->setChecked(selectedItems[0]->data(Qt::UserRole).value<AccountId>() == autoConnectAccount);
112 ui.accountButtonBox->button(QDialogButtonBox::Ok)->setEnabled(ui.accountList->count());
115 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
117 autoConnectAccount = 0;
119 if(ui.accountList->selectedItems().count()) {
120 autoConnectAccount = ui.accountList->selectedItems()[0]->data(Qt::UserRole).value<AccountId>();
122 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
123 autoConnectAccount = 0;
126 setAccountWidgetStates();
129 void CoreConnectDlg::on_addAccount_clicked() {
130 QStringList existing;
131 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
132 CoreAccountEditDlg dlg(0, QVariantMap(), existing, this);
133 if(dlg.exec() == QDialog::Accepted) {
135 AccountId id = accounts.count() + 1;
136 for(AccountId i = 1; i <= accounts.count(); i++) {
137 if(!accounts.keys().contains(i)) {
142 QVariantMap data = dlg.accountData();
143 data["AccountId"] = QVariant::fromValue<AccountId>(id);
145 QListWidgetItem *item = new QListWidgetItem(data["AccountName"].toString(), ui.accountList);
146 item->setData(Qt::UserRole, QVariant::fromValue<AccountId>(id));
147 ui.accountList->setCurrentItem(item);
151 void CoreConnectDlg::on_editAccount_clicked() {
152 QStringList existing;
153 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
154 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
155 QVariantMap acct = accounts[id];
156 CoreAccountEditDlg dlg(id, acct, existing, this);
157 if(dlg.exec() == QDialog::Accepted) {
158 QVariantMap data = dlg.accountData();
159 ui.accountList->currentItem()->setText(data["AccountName"].toString());
164 void CoreConnectDlg::on_deleteAccount_clicked() {
165 AccountId id = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
166 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
167 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
168 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
169 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
170 if(ret == QMessageBox::Yes) {
171 int idx = ui.accountList->currentRow();
172 delete ui.accountList->takeItem(idx);
173 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()-1));
174 accounts[id]["Delete"] = true; // we only flag this here, actual deletion happens on accept!
175 setAccountWidgetStates();
179 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
181 on_accountButtonBox_accepted();
184 void CoreConnectDlg::on_accountButtonBox_accepted() {
186 CoreAccountSettings s;
187 foreach(QVariantMap acct, accounts.values()) {
188 AccountId id = acct["AccountId"].value<AccountId>();
189 if(acct.contains("Delete")) {
192 s.storeAccountData(id, acct);
195 s.setAutoConnectAccount(autoConnectAccount);
197 ui.stackedWidget->setCurrentWidget(ui.loginPage);
198 account = ui.accountList->currentItem()->data(Qt::UserRole).value<AccountId>();
199 accountData = accounts[account];
200 s.setLastAccount(account);
204 /*****************************************************
205 * Connecting to the Core
206 ****************************************************/
208 /*** Phase One: initializing the core connection ***/
210 void CoreConnectDlg::connectToCore() {
211 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
212 ui.connectLabel->setText(tr("Connect to %1").arg(accountData["Host"].toString()));
213 ui.coreInfoLabel->setText("");
214 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
215 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
216 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
217 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
218 disconnect(ui.loginButtonBox, 0, this, 0);
219 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
221 clientSyncer->connectToCore(accountData);
224 void CoreConnectDlg::initPhaseError(const QString &error) {
225 doingAutoConnect = false;
226 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
227 //ui.connectLabel->setBrush(QBrush("red"));
228 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(accountData["Host"].toString()));
229 ui.coreInfoLabel->setText(error);
230 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
231 ui.loginButtonBox->button(QDialogButtonBox::Retry)->setFocus();
232 disconnect(ui.loginButtonBox, 0, this, 0);
233 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
234 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
237 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
238 ui.coreInfoLabel->setText(msg);
241 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
243 QString host = accountData["Host"].toString();
245 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
246 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
247 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
248 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
249 default: s = tr("Unknown connection state to %1"); break;
251 ui.connectLabel->setText(s);
254 void CoreConnectDlg::restartPhaseNull() {
255 doingAutoConnect = false;
256 ui.stackedWidget->setCurrentWidget(ui.accountPage);
257 clientSyncer->disconnectFromCore();
260 /*********************************************************
262 *********************************************************/
264 void CoreConnectDlg::startLogin() {
265 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
266 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
267 //ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
268 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
269 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
270 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setFocus();
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->button(QDialogButtonBox::Ok)->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) {