Renaming/moving some files. Apparently, I can't use a QStringListModel since
[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 "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(ui.accountList->count()) ui.accountList->item(0)->setSelected(true);
61   setWidgetStates();
62   doConnect(); // shortcut for development
63 }
64
65 CoreConnectDlg::~CoreConnectDlg() {
66   //qDebug() << "destroy";
67 }
68
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());
73 }
74
75 void CoreConnectDlg::createAccount() {
76   editAccount("");
77 }
78
79 void CoreConnectDlg::editAccount() {
80   if(!ui.accountList->selectedItems().count()) return;
81   QString acc = ui.accountList->selectedItems()[0]->text();
82   editAccount(acc);
83 }
84
85 void CoreConnectDlg::editAccount(QString acc) {
86   EditCoreAcctDlg *dlg = new EditCoreAcctDlg(acc, this);
87   dlg->showMaximized();
88   int res = dlg->exec();
89   if(res == QDialog::Accepted) {
90     AccountSettings s;
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);
96     setWidgetStates();
97   }
98   dlg->deleteLater();
99 }
100
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) {
108       AccountSettings s;
109       s.removeAccount(item->text());
110       item = ui.accountList->takeItem(ui.accountList->row(item));
111       delete item;
112       setWidgetStates();
113     }
114   }
115 }
116
117 void CoreConnectDlg::doConnect() {
118   if(!ui.accountList->selectedItems().count()) return;
119   QString acc = ui.accountList->selectedItems()[0]->text();
120   AccountSettings s;
121   QVariantMap connInfo = s.value(acc, "AccountData").toMap();
122   connInfo["AccountName"] = acc;
123
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);
129 }
130
131 void CoreConnectDlg::connectionSuccess() {
132   if(progressDlg->isConnected()) {
133     progressDlg->deleteLater();
134     accept();
135   } else {
136     connectionFailure();
137   }
138 }
139
140 void CoreConnectDlg::connectionFailure() {
141   progressDlg->deleteLater();
142   Client::instance()->disconnectFromCore();
143 }
144
145 QVariant CoreConnectDlg::getCoreState() {
146   return coreState;
147 }
148
149 /****************************************************************************************************/
150
151 EditCoreAcctDlg::EditCoreAcctDlg(QString accname, QDialog *parent) : QDialog(parent), accName(accname) {
152   ui.setupUi(this);
153   setModal(true);
154
155   ui.accountEdit->setText(accountName());
156   if(accName.isEmpty()) {
157     ui.port->setValue(DEFAULT_PORT);
158     ui.accountEdit->setFocus();
159   } else {
160     ui.hostEdit->setFocus();
161     AccountSettings s;
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);
170   }
171 }
172
173 QString EditCoreAcctDlg::accountName() const {
174   return accName;
175 }
176
177 void EditCoreAcctDlg::accept() {
178   AccountSettings s;
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();
184     return;
185   }
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();
194     return;
195   }
196   if(accountName() != ui.accountEdit->text()) {
197     s.removeAccount(accountName());
198     accName = ui.accountEdit->text();
199   }
200   QVariantMap accData;
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);
206   QDialog::accept();
207 }
208
209 /********************************************************************************************/
210
211 CoreConnectProgressDlg::CoreConnectProgressDlg(QDialog *parent) : QDialog(parent) {
212   ui.setupUi(this);
213
214   setModal(true);
215
216   connectsuccess = false;
217
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()));
222
223 }
224
225 bool CoreConnectProgressDlg::isConnected() const {
226   return connectsuccess;
227 }
228
229 void CoreConnectProgressDlg::connectToCore(QVariantMap connInfo) {
230   Client::instance()->connectToCore(connInfo);
231
232 }
233
234 void CoreConnectProgressDlg::coreConnected() {
235   connectsuccess = true;
236   accept();
237 }
238
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);
241   reject();
242 }
243
244 void CoreConnectProgressDlg::updateProgressBar(uint partial, uint total) {
245   ui.connectionProgress->setMaximum(total);
246   ui.connectionProgress->setValue(partial);
247 }
248