Syncing my current state, Network settings still not fully functional, so don't use...
[quassel.git] / src / qtui / settingspages / networkssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QHeaderView>
22 #include <QMessageBox>
23
24 #include "networkssettingspage.h"
25
26 #include "client.h"
27 #include "global.h"
28 #include "identity.h"
29 #include "network.h"
30
31
32 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
33   ui.setupUi(this);
34
35   connectedIcon = QIcon(":/22x22/actions/network-connect");
36   disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
37
38   currentId = 0;
39   setEnabled(false);  // need a core connection!
40   setWidgetStates();
41   connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
42   connect(Client::instance(), SIGNAL(networkAdded(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
43   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
44   connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
45   connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
46
47   connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
48   //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
49   //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
50
51   foreach(IdentityId id, Client::identityIds()) {
52     clientIdentityAdded(id);
53   }
54 }
55
56 void NetworksSettingsPage::save() {
57   if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
58
59   // First, remove the temporarily created networks
60   QList<NetworkInfo> toCreate, toUpdate;
61   QList<NetworkId> toRemove;
62   QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
63   while(i != networkInfos.end()) {
64     if((*i).networkId < 0) {
65       toCreate.append(*i);
66       i = networkInfos.erase(i);
67     } else {
68       if((*i) != Client::network((*i).networkId)->networkInfo()) {
69         toUpdate.append(*i);
70       }
71       ++i;
72     }
73   }
74   foreach(NetworkId id, Client::networkIds()) {
75     if(!networkInfos.contains(id)) toRemove.append(id);
76   }
77   SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
78   int ret = dlg.exec();
79   if(ret == QDialog::Rejected) {
80     // canceled -> reload everything to be safe
81     load();
82   }
83 }
84
85 void NetworksSettingsPage::load() {
86   reset();
87   foreach(NetworkId netid, Client::networkIds()) {
88     clientNetworkAdded(netid);
89   }
90   ui.networkList->setCurrentRow(0);
91   setChangedState(false);
92 }
93
94 void NetworksSettingsPage::reset() {
95   currentId = 0;
96   ui.networkList->clear();
97   networkInfos.clear();
98
99   /*
100   foreach(Identity *identity, identities.values()) {
101     identity->deleteLater();
102   }
103   identities.clear();
104   deletedIdentities.clear();
105   changedIdentities.clear();
106   ui.identityList->clear();
107   */
108 }
109
110 bool NetworksSettingsPage::aboutToSave() {
111
112   return true; // FIXME
113 }
114
115 void NetworksSettingsPage::widgetHasChanged() {
116   bool changed = testHasChanged();
117   if(changed != hasChanged()) setChangedState(changed);
118 }
119
120 bool NetworksSettingsPage::testHasChanged() {
121   if(currentId != 0) {
122     saveToNetworkInfo(networkInfos[currentId]);
123   }
124   if(Client::networkIds().count() != networkInfos.count()) return true;
125   foreach(NetworkId id, networkInfos.keys()) {
126     if(id < 0) return true;
127     if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
128   }
129   return false;
130 }
131
132 void NetworksSettingsPage::setWidgetStates() {
133   // network list
134   if(ui.networkList->selectedItems().count()) {
135     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
136     ui.detailsBox->setEnabled(true);
137     ui.renameNetwork->setEnabled(true);
138     ui.deleteNetwork->setEnabled(true);
139     ui.connectNow->setEnabled(true);
140     if(Client::network(id) && Client::network(id)->isConnected()) {
141       ui.connectNow->setIcon(disconnectedIcon);
142       ui.connectNow->setText(tr("Disconnect"));
143     } else {
144       ui.connectNow->setIcon(connectedIcon);
145       ui.connectNow->setText(tr("Connect"));
146     }
147   } else {
148     ui.renameNetwork->setEnabled(false);
149     ui.deleteNetwork->setEnabled(false);
150     ui.connectNow->setEnabled(false);
151     ui.detailsBox->setEnabled(false);
152   }
153   // network details
154   if(ui.serverList->selectedItems().count()) {
155     ui.editServer->setEnabled(true);
156     ui.deleteServer->setEnabled(true);
157     ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
158     ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
159   } else {
160     ui.editServer->setEnabled(false);
161     ui.deleteServer->setEnabled(false);
162     ui.upServer->setEnabled(false);
163     ui.downServer->setEnabled(false);
164   }
165 }
166
167 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
168   this->setEnabled(state);
169   if(state) {
170     load();
171   } else {
172     // reset
173     //currentId = 0;
174   }
175 }
176
177 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
178   for(int i = 0; i < ui.networkList->count(); i++) {
179     QListWidgetItem *item = ui.networkList->item(i);
180     if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
181   }
182   return 0;
183 }
184
185 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
186   const Identity * identity = Client::identity(id);
187   connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
188
189   if(id == 1) {
190     // default identity is always the first one!
191     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
192   } else {
193     QString name = identity->identityName();
194     for(int j = 0; j < ui.identityList->count(); j++) {
195       if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
196         ui.identityList->insertItem(j, name, id.toInt());
197         widgetHasChanged();
198         return;
199       }
200     }
201     // append
202     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
203     widgetHasChanged();
204   }
205 }
206
207 void NetworksSettingsPage::clientIdentityUpdated() {
208   const Identity *identity = qobject_cast<const Identity *>(sender());
209   if(!identity) {
210     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
211     return;
212   }
213   int row = ui.identityList->findData(identity->id().toInt());
214   if(row < 0) {
215     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
216     return;
217   }
218   if(ui.identityList->itemText(row) != identity->identityName()) {
219     ui.identityList->setItemText(row, identity->identityName());
220   }
221 }
222
223 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
224   ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
225   foreach(NetworkInfo info, networkInfos.values()) {
226     if(info.identity == id) info.identity = 1; // set to default
227   }
228   widgetHasChanged();
229 }
230
231
232 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
233   insertNetwork(id);
234   connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
235 }
236
237 void NetworksSettingsPage::clientNetworkUpdated() {
238   const Network *net = qobject_cast<const Network *>(sender());
239   if(!net) {
240     qWarning() << "Update request for unknown network received!";
241     return;
242   }
243   QListWidgetItem *item = networkItem(net->networkId());
244   if(!item) return;
245   item->setText(net->networkName());
246   if(net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
247   else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
248   if(net->isConnected()) {
249     item->setIcon(connectedIcon);
250   } else {
251     item->setIcon(disconnectedIcon);
252   }
253 }
254
255
256 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
257   NetworkInfo info = Client::network(id)->networkInfo();
258   networkInfos[id] = info;
259   return insertNetwork(info);
260 }
261
262 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
263   QListWidgetItem *item = new QListWidgetItem(disconnectedIcon, info.networkName);
264   item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
265   ui.networkList->addItem(item);
266   const Network *net = Client::network(info.networkId);
267   if(net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
268   else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
269   if(net && net->isConnected()) {
270     item->setIcon(connectedIcon);
271   } else {
272     item->setIcon(disconnectedIcon);
273   }
274   widgetHasChanged();
275   return item;
276 }
277
278 void NetworksSettingsPage::displayNetwork(NetworkId id, bool dontsave) {
279   Q_UNUSED(dontsave);
280   NetworkInfo info = networkInfos[id];
281   ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
282   ui.serverList->clear();
283   foreach(QVariantMap v, info.serverList) {
284     ui.serverList->addItem(QString("%1:%2").arg(v["Host"].toString()).arg(v["Port"].toUInt()));
285   }
286 }
287
288 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
289   info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
290 }
291 /*** Network list ***/
292
293 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
294   if(currentId != 0) {
295     saveToNetworkInfo(networkInfos[currentId]);
296   }
297   if(ui.networkList->selectedItems().count()) {
298     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
299     currentId = id;
300     displayNetwork(id);
301     ui.serverList->setCurrentRow(0);
302   } else {
303     currentId = 0;
304   }
305   setWidgetStates();
306 }
307
308 void NetworksSettingsPage::on_addNetwork_clicked() {
309   QStringList existing;
310   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
311   NetworkEditDlgNew dlg(QString(), existing, this);
312   if(dlg.exec() == QDialog::Accepted) {
313     NetworkId id;
314     for(id = 1; id <= networkInfos.count(); id++) {
315       widgetHasChanged();
316       if(!networkInfos.keys().contains(-id.toInt())) break;
317     }
318     id = -id.toInt();
319     NetworkInfo info;
320     info.networkId = id;
321     info.networkName = dlg.networkName();
322     info.identity = 1;
323     networkInfos[id] = info;
324     QListWidgetItem *item = insertNetwork(info);
325     ui.networkList->setCurrentItem(item);
326     setWidgetStates();
327   }
328 }
329
330 void NetworksSettingsPage::on_deleteNetwork_clicked() {
331   if(ui.networkList->selectedItems().count()) {
332     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
333     int ret = QMessageBox::question(this, tr("Delete Network?"),
334                                     tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?"
335                                        "<br><br><em>NOTE: Backlog deletion hasn't actually been implemented yet.</em>").arg(networkInfos[netid].networkName),
336                                     QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
337     if(ret == QMessageBox::Yes) {
338       currentId = 0;
339       networkInfos.remove(netid); qDebug() << netid << networkInfos.count();
340       delete ui.networkList->selectedItems()[0];
341       ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
342       setWidgetStates();
343       widgetHasChanged();
344     }
345   }
346 }
347
348 void NetworksSettingsPage::on_renameNetwork_clicked() {
349   if(!ui.networkList->selectedItems().count()) return;
350   QString old = ui.networkList->selectedItems()[0]->text();
351   QStringList existing;
352   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
353   NetworkEditDlgNew dlg(old, existing, this);
354   if(dlg.exec() == QDialog::Accepted) {
355     ui.networkList->selectedItems()[0]->setText(dlg.networkName());
356     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
357     networkInfos[netid].networkName = dlg.networkName();
358     widgetHasChanged();
359   }
360 }
361
362 /*** Server list ***/
363
364 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
365   setWidgetStates();
366 }
367
368 void NetworksSettingsPage::on_addServer_clicked() {
369   if(currentId == 0) return;
370   ServerEditDlgNew dlg(QVariantMap(), this);
371   if(dlg.exec() == QDialog::Accepted) {
372     networkInfos[currentId].serverList.append(dlg.serverData());
373     displayNetwork(currentId);
374     ui.serverList->setCurrentRow(ui.serverList->count()-1);
375     widgetHasChanged();
376   }
377
378 }
379
380 void NetworksSettingsPage::on_editServer_clicked() {
381   if(currentId == 0) return;
382   int cur = ui.serverList->currentRow();
383   ServerEditDlgNew dlg(networkInfos[currentId].serverList[cur], this);
384   if(dlg.exec() == QDialog::Accepted) {
385     networkInfos[currentId].serverList[cur] = dlg.serverData();
386     displayNetwork(currentId);
387     ui.serverList->setCurrentRow(cur);
388     widgetHasChanged();
389   }
390 }
391
392 void NetworksSettingsPage::on_deleteServer_clicked() {
393   if(currentId == 0) return;
394   int cur = ui.serverList->currentRow();
395   networkInfos[currentId].serverList.removeAt(cur);
396   displayNetwork(currentId);
397   ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
398   widgetHasChanged();
399 }
400
401 void NetworksSettingsPage::on_upServer_clicked() {
402   int cur = ui.serverList->currentRow();
403   QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
404   networkInfos[currentId].serverList.insert(cur-1, foo);
405   displayNetwork(currentId);
406   ui.serverList->setCurrentRow(cur-1);
407   widgetHasChanged();
408 }
409
410 void NetworksSettingsPage::on_downServer_clicked() {
411   int cur = ui.serverList->currentRow();
412   QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
413   networkInfos[currentId].serverList.insert(cur+1, foo);
414   displayNetwork(currentId);
415   ui.serverList->setCurrentRow(cur+1);
416   widgetHasChanged();
417 }
418
419 /**************************************************************************
420  * NetworkEditDlg
421  *************************************************************************/
422
423 NetworkEditDlgNew::NetworkEditDlgNew(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
424   ui.setupUi(this);
425
426   if(old.isEmpty()) {
427     // new network
428     setWindowTitle(tr("Add Network"));
429     on_networkEdit_textChanged(""); // disable ok button
430   } else ui.networkEdit->setText(old);
431 }
432
433 QString NetworkEditDlgNew::networkName() const {
434   return ui.networkEdit->text();
435
436 }
437
438 void NetworkEditDlgNew::on_networkEdit_textChanged(const QString &text) {
439   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
440 }
441
442
443 /**************************************************************************
444  * ServerEditDlg
445  *************************************************************************/
446
447 ServerEditDlgNew::ServerEditDlgNew(const QVariantMap &serverData, QWidget *parent) : QDialog(parent) {
448   ui.setupUi(this);
449   if(serverData.count()) {
450     ui.host->setText(serverData["Host"].toString());
451     ui.port->setValue(serverData["Port"].toUInt());
452     ui.password->setText(serverData["Password"].toString());
453     ui.useSSL->setChecked(serverData["UseSSL"].toBool());
454   } else {
455     ui.port->setValue(Global::defaultPort);
456   }
457   on_host_textChanged();
458 }
459
460 QVariantMap ServerEditDlgNew::serverData() const {
461   QVariantMap _serverData;
462   _serverData["Host"] = ui.host->text();
463   _serverData["Port"] = ui.port->value();
464   _serverData["Password"] = ui.password->text();
465   _serverData["UseSSL"] = ui.useSSL->isChecked();
466   return _serverData;
467 }
468
469 void ServerEditDlgNew::on_host_textChanged() {
470   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
471 }
472
473 /**************************************************************************
474  * SaveNetworksDlg
475  *************************************************************************/
476
477 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
478 {
479   ui.setupUi(this);
480
481 }
482