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"
29 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool autoconnect) : QDialog(parent) {
32 clientSyncer = new ClientSyncer(this);
34 setAttribute(Qt::WA_DeleteOnClose);
36 doingAutoConnect = false;
38 ui.stackedWidget->setCurrentWidget(ui.accountPage);
39 ui.accountButtonBox->setFocus();
41 CoreAccountSettings s;
42 QString lastacc = s.lastAccount();
43 autoConnectAccount = s.autoConnectAccount();
44 accounts = s.retrieveAllAccounts();
45 ui.accountList->addItems(accounts.keys());
46 QList<QListWidgetItem *> l = ui.accountList->findItems(lastacc, Qt::MatchExactly);
47 if(l.count()) ui.accountList->setCurrentItem(l[0]);
48 else ui.accountList->setCurrentRow(0);
50 setAccountWidgetStates();
52 connect(clientSyncer, SIGNAL(socketStateChanged(QAbstractSocket::SocketState)),this, SLOT(initPhaseSocketState(QAbstractSocket::SocketState)));
53 connect(clientSyncer, SIGNAL(connectionError(const QString &)), this, SLOT(initPhaseError(const QString &)));
54 connect(clientSyncer, SIGNAL(connectionMsg(const QString &)), this, SLOT(initPhaseMsg(const QString &)));
55 connect(clientSyncer, SIGNAL(startLogin()), this, SLOT(startLogin()));
56 connect(clientSyncer, SIGNAL(loginFailed(const QString &)), this, SLOT(loginFailed(const QString &)));
57 connect(clientSyncer, SIGNAL(loginSuccess()), this, SLOT(startSync()));
58 connect(clientSyncer, SIGNAL(sessionProgress(quint32, quint32)), this, SLOT(coreSessionProgress(quint32, quint32)));
59 connect(clientSyncer, SIGNAL(networksProgress(quint32, quint32)), this, SLOT(coreNetworksProgress(quint32, quint32)));
60 connect(clientSyncer, SIGNAL(channelsProgress(quint32, quint32)), this, SLOT(coreChannelsProgress(quint32, quint32)));
61 connect(clientSyncer, SIGNAL(ircUsersProgress(quint32, quint32)), this, SLOT(coreIrcUsersProgress(quint32, quint32)));
62 connect(clientSyncer, SIGNAL(syncFinished()), this, SLOT(accept()));
64 connect(ui.user, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
65 connect(ui.password, SIGNAL(textChanged(const QString &)), this, SLOT(setLoginWidgetStates()));
67 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
68 connect(ui.syncButtonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), this, SLOT(restartPhaseNull()));
70 if(autoconnect && ui.accountList->count() && !autoConnectAccount.isEmpty() && autoConnectAccount == ui.accountList->currentItem()->text()) {
71 doingAutoConnect = true;
72 on_accountButtonBox_accepted();
76 CoreConnectDlg::~CoreConnectDlg() {
77 if(ui.accountList->selectedItems().count()) {
78 CoreAccountSettings s;
79 s.setLastAccount(ui.accountList->selectedItems()[0]->text());
84 /****************************************************
86 ***************************************************/
88 void CoreConnectDlg::on_accountList_itemSelectionChanged() {
89 setAccountWidgetStates();
92 void CoreConnectDlg::setAccountWidgetStates() {
93 QList<QListWidgetItem *> selectedItems = ui.accountList->selectedItems();
94 ui.editAccount->setEnabled(selectedItems.count());
95 ui.deleteAccount->setEnabled(selectedItems.count());
96 ui.autoConnect->setEnabled(selectedItems.count());
97 if(selectedItems.count()) {
98 ui.autoConnect->setChecked(selectedItems[0]->text() == autoConnectAccount);
102 void CoreConnectDlg::on_autoConnect_clicked(bool state) {
104 autoConnectAccount = QString();
106 if(ui.accountList->selectedItems().count()) {
107 autoConnectAccount = ui.accountList->selectedItems()[0]->text();
109 qWarning() << "Checked auto connect without an enabled item!"; // should never happen!
110 autoConnectAccount = QString();
113 setAccountWidgetStates();
116 void CoreConnectDlg::on_addAccount_clicked() {
117 QStringList existing;
118 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
119 CoreAccountEditDlg dlg(QString(), QVariantMap(), existing, this);
120 if(dlg.exec() == QDialog::Accepted) {
121 accounts[dlg.accountName()] = dlg.accountData();
122 ui.accountList->addItem(dlg.accountName());
123 ui.accountList->setCurrentItem(ui.accountList->findItems(dlg.accountName(), Qt::MatchExactly)[0]);
127 void CoreConnectDlg::on_editAccount_clicked() {
128 QStringList existing;
129 for(int i = 0; i < ui.accountList->count(); i++) existing << ui.accountList->item(i)->text();
130 QString current = ui.accountList->currentItem()->text();
131 QVariantMap acct = accounts[current];
132 CoreAccountEditDlg dlg(current, acct, existing, this);
133 if(dlg.exec() == QDialog::Accepted) {
134 if(current != dlg.accountName()) {
135 if(autoConnectAccount == current) autoConnectAccount = dlg.accountName();
136 accounts.remove(current);
137 current = dlg.accountName();
138 ui.accountList->currentItem()->setText(current);
140 accounts[current] = dlg.accountData();
142 //ui.accountList->setCurrent
145 void CoreConnectDlg::on_deleteAccount_clicked() {
146 QString current = ui.accountList->currentItem()->text();
147 int ret = QMessageBox::question(this, tr("Remove Account Settings"),
148 tr("Do you really want to remove your local settings for this Quassel Core account?<br>"
149 "Note: This will <em>not</em> remove or change any data on the Core itself!"),
150 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
151 if(ret == QMessageBox::Yes) {
152 int idx = ui.accountList->currentRow();
153 delete ui.accountList->item(idx);
154 ui.accountList->setCurrentRow(qMin(idx, ui.accountList->count()));
155 accounts.remove(current);
159 void CoreConnectDlg::on_accountList_itemDoubleClicked(QListWidgetItem *item) {
161 on_accountButtonBox_accepted();
164 void CoreConnectDlg::on_accountButtonBox_accepted() {
166 CoreAccountSettings s;
167 s.storeAllAccounts(accounts);
168 s.setAutoConnectAccount(autoConnectAccount);
170 ui.stackedWidget->setCurrentWidget(ui.loginPage);
171 accountName = ui.accountList->currentItem()->text();
172 account = s.retrieveAccount(accountName);
173 s.setLastAccount(accountName);
177 /*****************************************************
178 * Connecting to the Core
179 ****************************************************/
181 /*** Phase One: initializing the core connection ***/
183 void CoreConnectDlg::connectToCore() {
184 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-disconnect")));
185 ui.connectLabel->setText(tr("Connect to %1").arg(account["Host"].toString()));
186 ui.coreInfoLabel->setText("");
187 ui.loginStack->setCurrentWidget(ui.loginEmptyPage);
188 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
189 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
190 disconnect(ui.loginButtonBox, 0, this, 0);
191 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
194 //connect(Client::instance(), SIGNAL(coreConnectionPhaseOne(const QVariantMap &)), this, SLOT(phaseOneFinished
195 clientSyncer->connectToCore(account);
198 void CoreConnectDlg::initPhaseError(const QString &error) {
199 doingAutoConnect = false;
200 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/status/dialog-error")));
201 //ui.connectLabel->setBrush(QBrush("red"));
202 ui.connectLabel->setText(tr("<div style=color:red;>Connection to %1 failed!</div>").arg(account["Host"].toString()));
203 ui.coreInfoLabel->setText(error);
204 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Retry|QDialogButtonBox::Cancel);
205 disconnect(ui.loginButtonBox, 0, this, 0);
206 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(restartPhaseNull()));
207 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
210 void CoreConnectDlg::initPhaseMsg(const QString &msg) {
211 ui.coreInfoLabel->setText(msg);
214 void CoreConnectDlg::initPhaseSocketState(QAbstractSocket::SocketState state) {
216 QString host = account["Host"].toString();
218 case QAbstractSocket::UnconnectedState: s = tr("Not connected to %1.").arg(host); break;
219 case QAbstractSocket::HostLookupState: s = tr("Looking up %1...").arg(host); break;
220 case QAbstractSocket::ConnectingState: s = tr("Connecting to %1...").arg(host); break;
221 case QAbstractSocket::ConnectedState: s = tr("Connected to %1").arg(host); break;
222 default: s = tr("Unknown connection state to %1"); break;
224 ui.connectLabel->setText(s);
227 void CoreConnectDlg::restartPhaseNull() {
228 doingAutoConnect = false;
229 ui.stackedWidget->setCurrentWidget(ui.accountPage);
230 clientSyncer->disconnectFromCore();
233 /*********************************************************
235 *********************************************************/
237 void CoreConnectDlg::startLogin() {
238 ui.connectIcon->setPixmap(QPixmap::fromImage(QImage(":/22x22/actions/network-connect")));
239 ui.loginStack->setCurrentWidget(ui.loginCredentialsPage);
240 ui.loginStack->setMinimumSize(ui.loginStack->sizeHint()); ui.loginStack->updateGeometry();
241 ui.loginButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
242 if(!account["User"].toString().isEmpty()) {
243 ui.user->setText(account["User"].toString());
244 if(account["RememberPasswd"].toBool()) {
245 ui.password->setText(account["Password"].toString());
246 ui.rememberPasswd->setChecked(true);
248 ui.rememberPasswd->setChecked(false);
249 ui.password->setFocus();
251 } else ui.user->setFocus();
252 disconnect(ui.loginButtonBox, 0, this, 0);
253 connect(ui.loginButtonBox, SIGNAL(accepted()), this, SLOT(doLogin()));
254 connect(ui.loginButtonBox, SIGNAL(rejected()), this, SLOT(restartPhaseNull()));
255 if(doingAutoConnect) doLogin();
258 void CoreConnectDlg::doLogin() {
259 ui.loginGroup->setTitle(tr("Logging in..."));
260 ui.user->setDisabled(true);
261 ui.password->setDisabled(true);
262 ui.rememberPasswd->setDisabled(true);
263 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(true);
264 account["User"] = ui.user->text();
265 account["RememberPasswd"] = ui.rememberPasswd->isChecked();
266 if(ui.rememberPasswd->isChecked()) account["Password"] = ui.password->text();
267 else account.remove("Password");
268 CoreAccountSettings s;
269 s.storeAccount(accountName, account);
270 clientSyncer->loginToCore(account["User"].toString(), account["Password"].toString());
273 void CoreConnectDlg::setLoginWidgetStates() {
274 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.user->text().isEmpty() || ui.password->text().isEmpty());
277 void CoreConnectDlg::loginFailed(const QString &error) {
278 ui.loginGroup->setTitle(tr("Login"));
279 ui.user->setEnabled(true);
280 ui.password->setEnabled(true);
281 ui.rememberPasswd->setEnabled(true);
282 ui.coreInfoLabel->setText(error);
283 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
284 ui.password->setFocus();
285 doingAutoConnect = false;
288 /************************************************************
289 * Phase Three: Syncing
290 ************************************************************/
292 void CoreConnectDlg::startSync() {
293 ui.sessionProgress->setRange(0, 1);
294 ui.sessionProgress->setValue(0);
295 ui.networksProgress->setRange(0, 1);
296 ui.networksProgress->setValue(0);
297 ui.channelsProgress->setRange(0, 1);
298 ui.channelsProgress->setValue(0);
299 ui.ircUsersProgress->setRange(0, 1);
300 ui.ircUsersProgress->setValue(0);
302 ui.stackedWidget->setCurrentWidget(ui.syncPage);
304 ui.loginGroup->setTitle(tr("Login"));
305 ui.user->setEnabled(true);
306 ui.password->setEnabled(true);
307 ui.rememberPasswd->setEnabled(true);
308 ui.loginButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
312 void CoreConnectDlg::coreSessionProgress(quint32 val, quint32 max) {
313 ui.sessionProgress->setRange(0, max);
314 ui.sessionProgress->setValue(val);
318 void CoreConnectDlg::coreNetworksProgress(quint32 val, quint32 max) {
320 ui.networksProgress->setFormat("0/0");
321 ui.networksProgress->setRange(0, 1);
322 ui.networksProgress->setValue(1);
324 ui.networksProgress->setFormat("%v/%m");
325 ui.networksProgress->setRange(0, max);
326 ui.networksProgress->setValue(val);
330 void CoreConnectDlg::coreChannelsProgress(quint32 val, quint32 max) {
332 ui.channelsProgress->setFormat("0/0");
333 ui.channelsProgress->setRange(0, 1);
334 ui.channelsProgress->setValue(1);
336 ui.channelsProgress->setFormat("%v/%m");
337 ui.channelsProgress->setRange(0, max);
338 ui.channelsProgress->setValue(val);
342 void CoreConnectDlg::coreIrcUsersProgress(quint32 val, quint32 max) {
344 ui.ircUsersProgress->setFormat("0/0");
345 ui.ircUsersProgress->setRange(0, 1);
346 ui.ircUsersProgress->setValue(1);
348 ui.ircUsersProgress->setFormat("%v/%m");
349 ui.ircUsersProgress->setRange(0, max);
350 ui.ircUsersProgress->setValue(val);
354 /*****************************************************************************************
356 *****************************************************************************************/
358 CoreAccountEditDlg::CoreAccountEditDlg(const QString &name, const QVariantMap &acct, const QStringList &_existing, QWidget *parent) : QDialog(parent) {
360 existing = _existing;
362 if(!name.isEmpty()) {
363 existing.removeAll(name);
364 ui.host->setText(acct["Host"].toString());
365 ui.port->setValue(acct["Port"].toUInt());
366 ui.useInternal->setChecked(acct["UseInternal"].toBool());
367 ui.accountName->setText(name);
369 setWindowTitle(tr("Add Core Account"));
373 QString CoreAccountEditDlg::accountName() const {
374 return ui.accountName->text();
377 QVariantMap CoreAccountEditDlg::accountData() {
378 account["Host"] = ui.host->text();
379 account["Port"] = ui.port->value();
380 account["UseInternal"] = ui.useInternal->isChecked();
384 void CoreAccountEditDlg::setWidgetStates() {
385 bool ok = !accountName().isEmpty() && !existing.contains(accountName()) && (ui.useInternal->isChecked() || !ui.host->text().isEmpty());
386 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
389 void CoreAccountEditDlg::on_host_textChanged(const QString &text) {
394 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
399 void CoreAccountEditDlg::on_useRemote_toggled(bool state) {