Pressing enter in the topic line now sets the channel topic.
[quassel.git] / src / qtopia / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QtGui>
22 #include <QSoftMenuBar>
23
24 #include "coreconnectdlg.h"
25 #include "client.h"
26 #include "clientsettings.h"
27 #include "global.h"
28
29 CoreConnectDlg::CoreConnectDlg(QWidget *parent, bool /*doAutoConnect*/) : QDialog(parent) {
30   ui.setupUi(this);
31
32   setAttribute(Qt::WA_DeleteOnClose);
33   setModal(true);
34
35   coreState = 0;
36
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()));
54
55   ui.accountList->setSelectionMode(QAbstractItemView::SingleSelection);
56   ui.accountList->setSortingEnabled(true);
57
58   AccountSettings s;
59   ui.accountList->addItems(s.knownAccounts());
60   // if empty, create a test account
61   /* disabled for the moment
62   if(!ui.accountList->count()) {
63     QVariantMap accData;
64     accData["User"] = "testuser";
65     accData["Host"] = "";
66     accData["Port"] = 4242;
67     accData["Password"] = "";
68     s.setValue("Test Account", "AccountData", accData);
69     ui.accountList->addItems(s.knownAccounts());
70   }
71   */
72   // end test account
73   if(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
74   setWidgetStates();
75 #ifdef DEVELMODE
76   doConnect(); // shortcut for development
77 #endif
78 }
79
80 CoreConnectDlg::~CoreConnectDlg() {
81   //qDebug() << "destroy";
82 }
83
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());
88 }
89
90 void CoreConnectDlg::createAccount() {
91   editAccount("");
92 }
93
94 void CoreConnectDlg::editAccount() {
95   if(!ui.accountList->selectedItems().count()) return;
96   QString acc = ui.accountList->selectedItems()[0]->text();
97   editAccount(acc);
98 }
99
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) {
105     AccountSettings s;
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);
111     setWidgetStates();
112   }
113   dlg->deleteLater();
114 }
115
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) {
123       AccountSettings s;
124       s.removeAccount(item->text());
125       item = ui.accountList->takeItem(ui.accountList->row(item));
126       delete item;
127       setWidgetStates();
128     }
129   }
130 }
131
132 void CoreConnectDlg::doConnect() {
133   if(!ui.accountList->selectedItems().count()) return;
134   QString acc = ui.accountList->selectedItems()[0]->text();
135   AccountSettings s;
136   QVariantMap connInfo = s.value(acc, "AccountData").toMap();
137   connInfo["AccountName"] = acc;
138
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);
144 }
145
146 void CoreConnectDlg::connectionSuccess() {
147   if(progressDlg->isConnected()) {
148     progressDlg->deleteLater();
149     accept();
150   } else {
151     connectionFailure();
152   }
153 }
154
155 void CoreConnectDlg::connectionFailure() {
156   progressDlg->deleteLater();
157   Client::instance()->disconnectFromCore();
158 }
159
160 QVariant CoreConnectDlg::getCoreState() {
161   return coreState;
162 }
163
164 /****************************************************************************************************/
165
166 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
167   ui.setupUi(this);
168   setModal(true);
169
170   ui.accountEdit->setText(accountName());
171   if(accName.isEmpty()) {
172     ui.port->setValue(Global::defaultPort);
173     ui.accountEdit->setFocus();
174   } else {
175     ui.hostEdit->setFocus();
176     AccountSettings s;
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);
185   }
186 }
187
188 QString EditCoreAcctDlg::accountName() const {
189   return accName;
190 }
191
192 void EditCoreAcctDlg::accept() {
193   AccountSettings s;
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();
199     return;
200   }
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();
209     return;
210   }
211   if(accountName() != ui.accountEdit->text()) {
212     s.removeAccount(accountName());
213     accName = ui.accountEdit->text();
214   }
215   QVariantMap accData;
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);
221   QDialog::accept();
222 }
223
224 /********************************************************************************************/
225
226 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
227   ui.setupUi(this);
228
229   setModal(true);
230
231   connectsuccess = false;
232
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()));
237
238 }
239
240 bool CoreConnectProgressDlg::isConnected() const {
241   return connectsuccess;
242 }
243
244 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
245   Client::instance()->connectToCore(connInfo);
246
247 }
248
249 void CoreConnectProgressDlg::coreConnected() {
250   connectsuccess = true;
251   accept();
252 }
253
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);
256   reject();
257 }
258
259 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
260   ui.connectionProgress->setMaximum(total);
261   ui.connectionProgress->setValue(partial);
262 }
263