Added getNetworkId(UserId user, const QString &network) to make the transition to...
[quassel.git] / src / qtopia / coreconnectdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development 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) any later version.                                   *
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 "global.h"
26 #include "client.h"
27 #include "clientsettings.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(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
61   setWidgetStates();
62 }
63
64 CoreConnectDlg::~CoreConnectDlg() {
65   //qDebug() << "destroy";
66 }
67
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());
72 }
73
74 void CoreConnectDlg::createAccount() {
75   editAccount("");
76 }
77
78 void CoreConnectDlg::editAccount() {
79   if(!ui.accountList->selectedItems().count()) return;
80   QString acc = ui.accountList->selectedItems()[0]->text();
81   editAccount(acc);
82 }
83
84 void CoreConnectDlg::editAccount(QString acc) {
85   EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
86   dlg->showMaximized();
87   int res = dlg->exec();
88   if(res == QDialog::Accepted) {
89     AccountSettings s;
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);
95     setWidgetStates();
96   }
97   dlg->deleteLater();
98 }
99
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) {
107       AccountSettings s;
108       s.removeAccount(item->text());
109       item = ui.accountList->takeItem(ui.accountList->row(item));
110       delete item;
111       setWidgetStates();
112     }
113   }
114 }
115
116 void CoreConnectDlg::doConnect() {
117   if(!ui.accountList->selectedItems().count()) return;
118   QString acc = ui.accountList->selectedItems()[0]->text();
119   AccountSettings s;
120   QVariantMap connInfo = s.value(acc, "AccountData").toMap();
121   connInfo["AccountName"] = acc;
122
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);
128 }
129
130 void CoreConnectDlg::connectionSuccess() {
131   if(progressDlg->isConnected()) {
132     progressDlg->deleteLater();
133     accept();
134   } else {
135     connectionFailure();
136   }
137 }
138
139 void CoreConnectDlg::connectionFailure() {
140   progressDlg->deleteLater();
141   Client::instance()->disconnectFromCore();
142 }
143
144 QVariant CoreConnectDlg::getCoreState() {
145   return coreState;
146 }
147
148 /****************************************************************************************************/
149
150 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
151   ui.setupUi(this);
152   setModal(true);
153
154   ui.accountEdit->setText(accountName());
155   if(accName.isEmpty()) {
156     ui.port->setValue(DEFAULT_PORT);
157     ui.accountEdit->setFocus();
158   } else {
159     ui.hostEdit->setFocus();
160     AccountSettings s;
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);
169   }
170 }
171
172 QString EditCoreAcctDlg::accountName() const {
173   return accName;
174 }
175
176 void EditCoreAcctDlg::accept() {
177   AccountSettings s;
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();
183     return;
184   }
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();
193     return;
194   }
195   if(accountName() != ui.accountEdit->text()) {
196     s.removeAccount(accountName());
197     accName = ui.accountEdit->text();
198   }
199   QVariantMap accData;
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);
205   QDialog::accept();
206 }
207
208 /********************************************************************************************/
209
210 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
211   ui.setupUi(this);
212
213   setModal(true);
214
215   connectsuccess = false;
216
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()));
221
222 }
223
224 bool CoreConnectProgressDlg::isConnected() const {
225   return connectsuccess;
226 }
227
228 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
229   Client::instance()->connectToCore(connInfo);
230
231 }
232
233 void CoreConnectProgressDlg::coreConnected() {
234   connectsuccess = true;
235   accept();
236 }
237
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);
240   reject();
241 }
242
243 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
244   ui.connectionProgress->setMaximum(total);
245   ui.connectionProgress->setValue(partial);
246 }
247