1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel IRC Development 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) any later version. *
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 <QSoftMenuBar>
24 #include "coreconnectdlg.h"
27 #include "clientsettings.h"
29 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool /*doAutoConnect*/) : QDialog(parent) {
32 setAttribute(Qt::WA_DeleteOnClose);
37 QMenu *menu = new QMenu(this);
38 newAccAction = new QAction(QIcon(":icon/new"), tr("New"), this);
39 delAccAction = new QAction(QIcon(":icon/trash"), tr("Delete"), this);
40 editAccAction = new QAction(QIcon(":icon/settings"), tr("Properties..."), this);
41 menu->addAction(newAccAction);
42 menu->addAction(delAccAction);
43 menu->addAction(editAccAction);
44 QSoftMenuBar::addMenuTo(this, menu);
45 QSoftMenuBar::setCancelEnabled(this, true);
46 ui.newAccount->setDefaultAction(newAccAction);
47 ui.delAccount->setDefaultAction(delAccAction);
48 ui.editAccount->setDefaultAction(editAccAction);
49 connect(newAccAction, SIGNAL(triggered()), this, SLOT(createAccount()));
50 connect(delAccAction, SIGNAL(triggered()), this, SLOT(removeAccount()));
51 connect(editAccAction, SIGNAL(triggered()), this, SLOT(editAccount()));
52 connect(ui.accountList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
53 connect(ui.doConnect, SIGNAL(clicked()), this, SLOT(doConnect()));
55 ui.accountList->setSelectionMode(QAbstractItemView::SingleSelection);
56 ui.accountList->setSortingEnabled(true);
59 ui.accountList->addItems(s.knownAccounts());
60 if(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
64 CoreConnectDlg::~CoreConnectDlg() {
65 //qDebug() << "destroy";
68 void CoreConnectDlg::setWidgetStates() {
69 editAccAction->setEnabled(ui.accountList->selectedItems().count());
70 delAccAction->setEnabled(ui.accountList->selectedItems().count());
71 ui.doConnect->setEnabled(ui.accountList->selectedItems().count());
74 void CoreConnectDlg::createAccount() {
78 void CoreConnectDlg::editAccount() {
79 if(!ui.accountList->selectedItems().count()) return;
80 QString acc = ui.accountList->selectedItems()[0]->text();
84 void CoreConnectDlg::editAccount(QString acc) {
85 EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
87 int res = dlg->exec();
88 if(res == QDialog::Accepted) {
90 ui.accountList->clear();
91 ui.accountList->addItems(s.knownAccounts());
92 QList<QListWidgetItem *> list = ui.accountList->findItems(dlg->accountName(), Qt::MatchExactly);
93 Q_ASSERT(list.count() == 1);
94 list[0]->setSelected(true);
100 void CoreConnectDlg::removeAccount() {
101 if(ui.accountList->selectedItems().count()) {
102 QListWidgetItem *item = ui.accountList->selectedItems()[0];
103 int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
104 "Note that this only affects your local account settings and will not remove "
105 "any data from the core.").arg(item->text()), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
106 if(res == QMessageBox::Yes) {
108 s.removeAccount(item->text());
109 item = ui.accountList->takeItem(ui.accountList->row(item));
116 void CoreConnectDlg::doConnect() {
117 if(!ui.accountList->selectedItems().count()) return;
118 QString acc = ui.accountList->selectedItems()[0]->text();
120 QVariantMap connInfo = s.value(acc, "AccountData").toMap();
121 connInfo["AccountName"] = acc;
123 progressDlg = new CoreConnectProgressDlg(this);
124 connect(progressDlg, SIGNAL(accepted()), this, SLOT(connectionSuccess()));
125 connect(progressDlg, SIGNAL(rejected()), this, SLOT(connectionFailure()));
126 progressDlg->showMaximized();
127 progressDlg->connectToCore(connInfo);
130 void CoreConnectDlg::connectionSuccess() {
131 if(progressDlg->isConnected()) {
132 progressDlg->deleteLater();
139 void CoreConnectDlg::connectionFailure() {
140 progressDlg->deleteLater();
141 Client::instance()->disconnectFromCore();
144 QVariant CoreConnectDlg::getCoreState() {
148 /****************************************************************************************************/
150 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
154 ui.accountEdit->setText(accountName());
155 if(accName.isEmpty()) {
156 ui.port->setValue(DEFAULT_PORT);
157 ui.accountEdit->setFocus();
159 ui.hostEdit->setFocus();
161 QVariantMap data = s.value(accName, "AccountData").toMap();
162 ui.hostEdit->setText(data["Host"].toString());
163 ui.port->setValue(data["Port"].toUInt());
164 ui.userEdit->setText(data["User"].toString());
165 //if(data.contains("Password")) {
166 ui.passwdEdit->setText(data["Password"].toString());
167 // ui.rememberPasswd->setChecked(true);
168 //} else ui.rememberPasswd->setChecked(false);
172 QString EditCoreAcctDlg::accountName() const {
176 void EditCoreAcctDlg::accept() {
178 if(ui.userEdit->text().isEmpty() || ui.hostEdit->text().isEmpty() || ui.accountEdit->text().isEmpty()) {
179 int res = QMessageBox::warning(this, tr("Missing information"),
180 tr("Please enter all required information or discard changes to return to account selection."),
181 QMessageBox::Discard|QMessageBox::Retry);
182 if(res != QMessageBox::Retry) reject();
185 if(ui.accountEdit->text() != accountName() && s.knownAccounts().contains(ui.accountEdit->text())) {
186 int res = QMessageBox::warning(this, tr("Non-unique account name"),
187 tr("Account names need to be unique. Please enter a different name or discard all changes to "
188 "return to account selection."),
189 QMessageBox::Discard|QMessageBox::Retry);
190 if(res != QMessageBox::Retry) reject();
191 ui.accountEdit->setSelection(0, ui.accountEdit->text().length());
192 ui.accountEdit->setFocus();
195 if(accountName() != ui.accountEdit->text()) {
196 s.removeAccount(accountName());
197 accName = ui.accountEdit->text();
200 accData["User"] = ui.userEdit->text();
201 accData["Host"] = ui.hostEdit->text();
202 accData["Port"] = ui.port->value();
203 accData["Password"] = ui.passwdEdit->text();
204 s.setValue(accountName(), "AccountData", accData);
208 /********************************************************************************************/
210 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
215 connectsuccess = false;
217 connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
218 connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
219 connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
220 connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
224 bool CoreConnectProgressDlg::isConnected() const {
225 return connectsuccess;
228 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
229 Client::instance()->connectToCore(connInfo);
233 void CoreConnectProgressDlg::coreConnected() {
234 connectsuccess = true;
238 void CoreConnectProgressDlg::coreConnectionError(QString err) {
239 QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Ok);
243 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
244 ui.connectionProgress->setMaximum(total);
245 ui.connectionProgress->setValue(partial);