1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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 <QSoftMenuBar>
24 #include "coreconnectdlg.h"
26 #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 empty, create a test account
61 /* disabled for the moment
62 if(!ui.accountList->count()) {
64 accData["User"] = "testuser";
66 accData["Port"] = 4242;
67 accData["Password"] = "";
68 s.setValue("Test Account", "AccountData", accData);
69 ui.accountList->addItems(s.knownAccounts());
73 if(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
76 doConnect(); // shortcut for development
80 CoreConnectDlg::~CoreConnectDlg() {
81 //qDebug() << "destroy";
84 void CoreConnectDlg::setWidgetStates() {
85 editAccAction->setEnabled(ui.accountList->selectedItems().count());
86 delAccAction->setEnabled(ui.accountList->selectedItems().count());
87 ui.doConnect->setEnabled(ui.accountList->selectedItems().count());
90 void CoreConnectDlg::createAccount() {
94 void CoreConnectDlg::editAccount() {
95 if(!ui.accountList->selectedItems().count()) return;
96 QString acc = ui.accountList->selectedItems()[0]->text();
100 void CoreConnectDlg::editAccount(QString acc) {
101 EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
102 dlg->showMaximized();
103 int res = dlg->exec();
104 if(res == QDialog::Accepted) {
106 ui.accountList->clear();
107 ui.accountList->addItems(s.knownAccounts());
108 QList<QListWidgetItem *> list = ui.accountList->findItems(dlg->accountName(), Qt::MatchExactly);
109 Q_ASSERT(list.count() == 1);
110 list[0]->setSelected(true);
116 void CoreConnectDlg::removeAccount() {
117 if(ui.accountList->selectedItems().count()) {
118 QListWidgetItem *item = ui.accountList->selectedItems()[0];
119 int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
120 "Note that this only affects your local account settings and will not remove "
121 "any data from the core.").arg(item->text()), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
122 if(res == QMessageBox::Yes) {
124 s.removeAccount(item->text());
125 item = ui.accountList->takeItem(ui.accountList->row(item));
132 void CoreConnectDlg::doConnect() {
133 if(!ui.accountList->selectedItems().count()) return;
134 QString acc = ui.accountList->selectedItems()[0]->text();
136 QVariantMap connInfo = s.value(acc, "AccountData").toMap();
137 connInfo["AccountName"] = acc;
139 progressDlg = new CoreConnectProgressDlg(this);
140 connect(progressDlg, SIGNAL(accepted()), this, SLOT(connectionSuccess()));
141 connect(progressDlg, SIGNAL(rejected()), this, SLOT(connectionFailure()));
142 progressDlg->showMaximized();
143 progressDlg->connectToCore(connInfo);
146 void CoreConnectDlg::connectionSuccess() {
147 if(progressDlg->isConnected()) {
148 progressDlg->deleteLater();
155 void CoreConnectDlg::connectionFailure() {
156 progressDlg->deleteLater();
157 Client::instance()->disconnectFromCore();
160 QVariant CoreConnectDlg::getCoreState() {
164 /****************************************************************************************************/
166 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
170 ui.accountEdit->setText(accountName());
171 if(accName.isEmpty()) {
172 ui.port->setValue(Global::defaultPort);
173 ui.accountEdit->setFocus();
175 ui.hostEdit->setFocus();
177 QVariantMap data = s.value(accName, "AccountData").toMap();
178 ui.hostEdit->setText(data["Host"].toString());
179 ui.port->setValue(data["Port"].toUInt());
180 ui.userEdit->setText(data["User"].toString());
181 //if(data.contains("Password")) {
182 ui.passwdEdit->setText(data["Password"].toString());
183 // ui.rememberPasswd->setChecked(true);
184 //} else ui.rememberPasswd->setChecked(false);
188 QString EditCoreAcctDlg::accountName() const {
192 void EditCoreAcctDlg::accept() {
194 if(ui.userEdit->text().isEmpty() || ui.hostEdit->text().isEmpty() || ui.accountEdit->text().isEmpty()) {
195 int res = QMessageBox::warning(this, tr("Missing information"),
196 tr("Please enter all required information or discard changes to return to account selection."),
197 QMessageBox::Discard|QMessageBox::Retry);
198 if(res != QMessageBox::Retry) reject();
201 if(ui.accountEdit->text() != accountName() && s.knownAccounts().contains(ui.accountEdit->text())) {
202 int res = QMessageBox::warning(this, tr("Non-unique account name"),
203 tr("Account names need to be unique. Please enter a different name or discard all changes to "
204 "return to account selection."),
205 QMessageBox::Discard|QMessageBox::Retry);
206 if(res != QMessageBox::Retry) reject();
207 ui.accountEdit->setSelection(0, ui.accountEdit->text().length());
208 ui.accountEdit->setFocus();
211 if(accountName() != ui.accountEdit->text()) {
212 s.removeAccount(accountName());
213 accName = ui.accountEdit->text();
216 accData["User"] = ui.userEdit->text();
217 accData["Host"] = ui.hostEdit->text();
218 accData["Port"] = ui.port->value();
219 accData["Password"] = ui.passwdEdit->text();
220 s.setValue(accountName(), "AccountData", accData);
224 /********************************************************************************************/
226 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
231 connectsuccess = false;
233 connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
234 connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
235 connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
236 connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
240 bool CoreConnectProgressDlg::isConnected() const {
241 return connectsuccess;
244 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
245 Client::instance()->connectToCore(connInfo);
249 void CoreConnectProgressDlg::coreConnected() {
250 connectsuccess = true;
254 void CoreConnectProgressDlg::coreConnectionError(QString err) {
255 QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Ok);
259 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
260 ui.connectionProgress->setMaximum(total);
261 ui.connectionProgress->setValue(partial);