176e077c05f53dc4cdfc4f9908121b6bbd06a71b
[quassel.git] / src / qtopia / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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   if(!ui.accountList->count()) {
62     QVariantMap accData;
63     accData["User"] = "testuser";
64     accData["Host"] = "apollo.mindpool.net";
65     accData["Port"] = 4242;
66     accData["Password"] = "testuser";
67     s.setValue("Test Account", "AccountData", accData);
68     ui.accountList->addItems(s.knownAccounts());
69   }
70   // end test account
71   if(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
72   setWidgetStates();
73 #ifdef DEVELMODE
74   doConnect(); // shortcut for development
75 #endif
76 }
77
78 CoreConnectDlg::~CoreConnectDlg() {
79   //qDebug() << "destroy";
80 }
81
82 void CoreConnectDlg::setWidgetStates() {
83   editAccAction->setEnabled(ui.accountList->selectedItems().count());
84   delAccAction->setEnabled(ui.accountList->selectedItems().count());
85   ui.doConnect->setEnabled(ui.accountList->selectedItems().count());
86 }
87
88 void CoreConnectDlg::createAccount() {
89   editAccount("");
90 }
91
92 void CoreConnectDlg::editAccount() {
93   if(!ui.accountList->selectedItems().count()) return;
94   QString acc = ui.accountList->selectedItems()[0]->text();
95   editAccount(acc);
96 }
97
98 void CoreConnectDlg::editAccount(QString acc) {
99   EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
100   dlg->showMaximized();
101   int res = dlg->exec();
102   if(res == QDialog::Accepted) {
103     AccountSettings s;
104     ui.accountList->clear();
105     ui.accountList->addItems(s.knownAccounts());
106     QList<QListWidgetItem *> list = ui.accountList->findItems(dlg->accountName(), Qt::MatchExactly);
107     Q_ASSERT(list.count() == 1);
108     list[0]->setSelected(true);
109     setWidgetStates();
110   }
111   dlg->deleteLater();
112 }
113
114 void CoreConnectDlg::removeAccount() {
115   if(ui.accountList->selectedItems().count()) {
116     QListWidgetItem *item = ui.accountList->selectedItems()[0];
117     int res = QMessageBox::warning(this, tr("Delete account?"), tr("Do you really want to delete the data for the account '%1'?<br>"
118         "Note that this only affects your local account settings and will not remove "
119         "any data from the core.").arg(item->text()), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
120     if(res == QMessageBox::Yes) {
121       AccountSettings s;
122       s.removeAccount(item->text());
123       item = ui.accountList->takeItem(ui.accountList->row(item));
124       delete item;
125       setWidgetStates();
126     }
127   }
128 }
129
130 void CoreConnectDlg::doConnect() {
131   if(!ui.accountList->selectedItems().count()) return;
132   QString acc = ui.accountList->selectedItems()[0]->text();
133   AccountSettings s;
134   QVariantMap connInfo = s.value(acc, "AccountData").toMap();
135   connInfo["AccountName"] = acc;
136
137   progressDlg = new CoreConnectProgressDlg(this);
138   connect(progressDlg, SIGNAL(accepted()), this, SLOT(connectionSuccess()));
139   connect(progressDlg, SIGNAL(rejected()), this, SLOT(connectionFailure()));
140   progressDlg->showMaximized();
141   progressDlg->connectToCore(connInfo);
142 }
143
144 void CoreConnectDlg::connectionSuccess() {
145   if(progressDlg->isConnected()) {
146     progressDlg->deleteLater();
147     accept();
148   } else {
149     connectionFailure();
150   }
151 }
152
153 void CoreConnectDlg::connectionFailure() {
154   progressDlg->deleteLater();
155   Client::instance()->disconnectFromCore();
156 }
157
158 QVariant CoreConnectDlg::getCoreState() {
159   return coreState;
160 }
161
162 /****************************************************************************************************/
163
164 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
165   ui.setupUi(this);
166   setModal(true);
167
168   ui.accountEdit->setText(accountName());
169   if(accName.isEmpty()) {
170     ui.port->setValue(DEFAULT_PORT);
171     ui.accountEdit->setFocus();
172   } else {
173     ui.hostEdit->setFocus();
174     AccountSettings s;
175     QVariantMap data = s.value(accName, "AccountData").toMap();
176     ui.hostEdit->setText(data["Host"].toString());
177     ui.port->setValue(data["Port"].toUInt());
178     ui.userEdit->setText(data["User"].toString());
179     //if(data.contains("Password")) {
180       ui.passwdEdit->setText(data["Password"].toString());
181     //  ui.rememberPasswd->setChecked(true);
182     //} else ui.rememberPasswd->setChecked(false);
183   }
184 }
185
186 QString EditCoreAcctDlg::accountName() const {
187   return accName;
188 }
189
190 void EditCoreAcctDlg::accept() {
191   AccountSettings s;
192   if(ui.userEdit->text().isEmpty() || ui.hostEdit->text().isEmpty() || ui.accountEdit->text().isEmpty()) {
193     int res = QMessageBox::warning(this, tr("Missing information"),
194                                    tr("Please enter all required information or discard changes to return to account selection."),
195                                       QMessageBox::Discard|QMessageBox::Retry);
196     if(res != QMessageBox::Retry) reject();
197     return;
198   }
199   if(ui.accountEdit->text() != accountName() && s.knownAccounts().contains(ui.accountEdit->text())) {
200     int res = QMessageBox::warning(this, tr("Non-unique account name"),
201                                    tr("Account names need to be unique. Please enter a different name or discard all changes to "
202                                       "return to account selection."),
203                                       QMessageBox::Discard|QMessageBox::Retry);
204     if(res != QMessageBox::Retry) reject();
205     ui.accountEdit->setSelection(0, ui.accountEdit->text().length());
206     ui.accountEdit->setFocus();
207     return;
208   }
209   if(accountName() != ui.accountEdit->text()) {
210     s.removeAccount(accountName());
211     accName = ui.accountEdit->text();
212   }
213   QVariantMap accData;
214   accData["User"] = ui.userEdit->text();
215   accData["Host"] = ui.hostEdit->text();
216   accData["Port"] = ui.port->value();
217   accData["Password"] = ui.passwdEdit->text();
218   s.setValue(accountName(), "AccountData", accData);
219   QDialog::accept();
220 }
221
222 /********************************************************************************************/
223
224 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
225   ui.setupUi(this);
226
227   setModal(true);
228
229   connectsuccess = false;
230
231   connect(Client::instance(), SIGNAL(coreConnectionMsg(const QString &)), ui.connectionStatus, SLOT(setText(const QString &)));
232   connect(Client::instance(), SIGNAL(coreConnectionProgress(uint, uint)), this, SLOT(updateProgressBar(uint, uint)));
233   connect(Client::instance(), SIGNAL(coreConnectionError(QString)), this, SLOT(coreConnectionError(QString)));
234   connect(Client::instance(), SIGNAL(connected()), this, SLOT(coreConnected()));
235
236 }
237
238 bool CoreConnectProgressDlg::isConnected() const {
239   return connectsuccess;
240 }
241
242 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
243   Client::instance()->connectToCore(connInfo);
244
245 }
246
247 void CoreConnectProgressDlg::coreConnected() {
248   connectsuccess = true;
249   accept();
250 }
251
252 void CoreConnectProgressDlg::coreConnectionError(QString err) {
253   QMessageBox::warning(this, tr("Connection Error"), tr("<b>Could not connect to Quassel Core!</b><br>\n") + err, QMessageBox::Ok);
254   reject();
255 }
256
257 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
258   ui.connectionProgress->setMaximum(total);
259   ui.connectionProgress->setValue(partial);
260 }
261