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"
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(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
62 doConnect(); // shortcut for development
65 CoreConnectDlg::~CoreConnectDlg() {
66 //qDebug() << "destroy";
69 void CoreConnectDlg::setWidgetStates() {
70 editAccAction->setEnabled(ui.accountList->selectedItems().count());
71 delAccAction->setEnabled(ui.accountList->selectedItems().count());
72 ui.doConnect->setEnabled(ui.accountList->selectedItems().count());
75 void CoreConnectDlg::createAccount() {
79 void CoreConnectDlg::editAccount() {
80 if(!ui.accountList->selectedItems().count()) return;
81 QString acc = ui.accountList->selectedItems()[0]->text();
85 void CoreConnectDlg::editAccount(QString acc) {
86 EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
88 int res = dlg->exec();
89 if(res == QDialog::Accepted) {
91 ui.accountList->clear();
92 ui.accountList->addItems(s.knownAccounts());
93 QList<QListWidgetItem *> list = ui.accountList->findItems(dlg->accountName(), Qt::MatchExactly);
94 Q_ASSERT(list.count() == 1);
95 list[0]->setSelected(true);
101 void CoreConnectDlg::removeAccount() {
102 if(ui.accountList->selectedItems().count()) {
103 QListWidgetItem *item = ui.accountList->selectedItems()[0];
104 int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
105 "Note that this only affects your local account settings and will not remove "
106 "any data from the core.").arg(item->text()), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
107 if(res == QMessageBox::Yes) {
109 s.removeAccount(item->text());
110 item = ui.accountList->takeItem(ui.accountList->row(item));
117 void CoreConnectDlg::doConnect() {
118 if(!ui.accountList->selectedItems().count()) return;
119 QString acc = ui.accountList->selectedItems()[0]->text();
121 QVariantMap connInfo = s.value(acc, "AccountData").toMap();
122 connInfo["AccountName"] = acc;
124 progressDlg = new CoreConnectProgressDlg(this);
125 connect(progressDlg, SIGNAL(accepted()), this, SLOT(connectionSuccess()));
126 connect(progressDlg, SIGNAL(rejected()), this, SLOT(connectionFailure()));
127 progressDlg->showMaximized();
128 progressDlg->connectToCore(connInfo);
131 void CoreConnectDlg::connectionSuccess() {
132 if(progressDlg->isConnected()) {
133 progressDlg->deleteLater();
140 void CoreConnectDlg::connectionFailure() {
141 progressDlg->deleteLater();
142 Client::instance()->disconnectFromCore();
145 QVariant CoreConnectDlg::getCoreState() {
149 /****************************************************************************************************/
151 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
155 ui.accountEdit->setText(accountName());
156 if(accName.isEmpty()) {
157 ui.port->setValue(DEFAULT_PORT);
158 ui.accountEdit->setFocus();
160 ui.hostEdit->setFocus();
162 QVariantMap data = s.value(accName, "AccountData").toMap();
163 ui.hostEdit->setText(data["Host"].toString());
164 ui.port->setValue(data["Port"].toUInt());
165 ui.userEdit->setText(data["User"].toString());
166 //if(data.contains("Password")) {
167 ui.passwdEdit->setText(data["Password"].toString());
168 // ui.rememberPasswd->setChecked(true);
169 //} else ui.rememberPasswd->setChecked(false);
173 QString EditCoreAcctDlg::accountName() const {
177 void EditCoreAcctDlg::accept() {
179 if(ui.userEdit->text().isEmpty() || ui.hostEdit->text().isEmpty() || ui.accountEdit->text().isEmpty()) {
180 int res = QMessageBox::warning(this, tr("Missing information"),
181 tr("Please enter all required information or discard changes to return to account selection."),
182 QMessageBox::Discard|QMessageBox::Retry);
183 if(res != QMessageBox::Retry) reject();
186 if(ui.accountEdit->text() != accountName() && s.knownAccounts().contains(ui.accountEdit->text())) {
187 int res = QMessageBox::warning(this, tr("Non-unique account name"),
188 tr("Account names need to be unique. Please enter a different name or discard all changes to "
189 "return to account selection."),
190 QMessageBox::Discard|QMessageBox::Retry);
191 if(res != QMessageBox::Retry) reject();
192 ui.accountEdit->setSelection(0, ui.accountEdit->text().length());
193 ui.accountEdit->setFocus();
196 if(accountName() != ui.accountEdit->text()) {
197 s.removeAccount(accountName());
198 accName = ui.accountEdit->text();
201 accData["User"] = ui.userEdit->text();
202 accData["Host"] = ui.hostEdit->text();
203 accData["Port"] = ui.port->value();
204 accData["Password"] = ui.passwdEdit->text();
205 s.setValue(accountName(), "AccountData", accData);
209 /********************************************************************************************/
211 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
216 connectsuccess = false;
218 connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
219 connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
220 connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
221 connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
225 bool CoreConnectProgressDlg::isConnected() const {
226 return connectsuccess;
229 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
230 Client::instance()->connectToCore(connInfo);
234 void CoreConnectProgressDlg::coreConnected() {
235 connectsuccess = true;
239 void CoreConnectProgressDlg::coreConnectionError(QString err) {
240 QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Ok);
244 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
245 ui.connectionProgress->setMaximum(total);
246 ui.connectionProgress->setValue(partial);