Networks can now be removed even when they're connected.
[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   connectingIcon = QIcon(":/22x22/actions/gear");
37   disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
38
39   currentId = 0;
40   setEnabled(Client::isConnected());  // need a core connection!
41   setWidgetStates();
42   connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
43   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
44   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
45   connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
46   connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
47
48   connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
49   //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
50   //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
51
52   foreach(IdentityId id, Client::identityIds()) {
53     clientIdentityAdded(id);
54   }
55 }
56
57 void NetworksSettingsPage::save() {
58   setEnabled(false);
59   if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
60
61   QList<NetworkInfo> toCreate, toUpdate;
62   QList<NetworkId> toRemove;
63   QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
64   while(i != networkInfos.end()) {
65     NetworkId id = (*i).networkId;
66     if(id < 0) {
67       toCreate.append(*i);
68       //if(id == currentId) currentId = 0;
69       //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
70       //if(items.count()) {
71       //  Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
72       //  delete items[0];
73       //}
74       //i = networkInfos.erase(i);
75       ++i;
76     } else {
77       if((*i) != Client::network((*i).networkId)->networkInfo()) {
78         toUpdate.append(*i);
79       }
80       ++i;
81     }
82   }
83   foreach(NetworkId id, Client::networkIds()) {
84     if(!networkInfos.contains(id)) toRemove.append(id);
85   }
86   SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
87   int ret = dlg.exec();
88   if(ret == QDialog::Rejected) {
89     // canceled -> reload everything to be safe
90     load();
91   }
92   setChangedState(false);
93   setEnabled(true);
94 }
95
96 void NetworksSettingsPage::load() {
97   reset();
98   foreach(NetworkId netid, Client::networkIds()) {
99     clientNetworkAdded(netid);
100   }
101   ui.networkList->setCurrentRow(0);
102   setChangedState(false);
103 }
104
105 void NetworksSettingsPage::reset() {
106   currentId = 0;
107   ui.networkList->clear();
108   networkInfos.clear();
109
110 }
111
112 bool NetworksSettingsPage::aboutToSave() {
113   if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
114   QList<int> errors;
115   foreach(NetworkInfo info, networkInfos.values()) {
116     if(!info.serverList.count()) errors.append(1);
117   }
118   if(!errors.count()) return true;
119   QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
120   if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
121   error += tr("</ul>");
122   QMessageBox::warning(this, tr("Invalid Network Settings"), error);
123   return false;
124 }
125
126 void NetworksSettingsPage::widgetHasChanged() {
127   bool changed = testHasChanged();
128   if(changed != hasChanged()) setChangedState(changed);
129 }
130
131 bool NetworksSettingsPage::testHasChanged() {
132   if(currentId != 0) {
133     saveToNetworkInfo(networkInfos[currentId]);
134   }
135   if(Client::networkIds().count() != networkInfos.count()) return true;
136   foreach(NetworkId id, networkInfos.keys()) {
137     if(id < 0) return true;
138     if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
139   }
140   return false;
141 }
142
143 void NetworksSettingsPage::setWidgetStates() {
144   // network list
145   if(ui.networkList->selectedItems().count()) {
146     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
147     ui.detailsBox->setEnabled(true);
148     ui.renameNetwork->setEnabled(true);
149     ui.deleteNetwork->setEnabled(true);
150     ui.connectNow->setEnabled(id > 0
151         && (Client::network(id)->connectionState() == Network::Initialized
152         || Client::network(id)->connectionState() == Network::Disconnected));
153     if(Client::network(id) && Client::network(id)->isConnected()) {
154       ui.connectNow->setIcon(disconnectedIcon);
155       ui.connectNow->setText(tr("Disconnect"));
156     } else {
157       ui.connectNow->setIcon(connectedIcon);
158       ui.connectNow->setText(tr("Connect"));
159     }
160   } else {
161     ui.renameNetwork->setEnabled(false);
162     ui.deleteNetwork->setEnabled(false);
163     ui.connectNow->setEnabled(false);
164     ui.detailsBox->setEnabled(false);
165   }
166   // network details
167   if(ui.serverList->selectedItems().count()) {
168     ui.editServer->setEnabled(true);
169     ui.deleteServer->setEnabled(true);
170     ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
171     ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
172   } else {
173     ui.editServer->setEnabled(false);
174     ui.deleteServer->setEnabled(false);
175     ui.upServer->setEnabled(false);
176     ui.downServer->setEnabled(false);
177   }
178 }
179
180 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
181   if(!item && !(item = networkItem(id))) return;
182   const Network *net = Client::network(id);
183   if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
184   else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
185   if(net && net->connectionState() == Network::Initialized) {
186     item->setIcon(connectedIcon);
187   } else if(net && net->connectionState() != Network::Disconnected) {
188     item->setIcon(connectingIcon);
189   } else {
190     item->setIcon(disconnectedIcon);
191   }
192   if(net) {
193     bool select = false;
194     // check if we already have another net of this name in the list, and replace it
195     QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
196     if(items.count()) {
197       foreach(QListWidgetItem *i, items) {
198         NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
199         if(oldid > 0) continue;  // only locally created nets should be replaced
200         if(oldid == currentId) select = true;
201         int row = ui.networkList->row(i);
202         if(row >= 0) delete ui.networkList->takeItem(row);
203         networkInfos.remove(oldid);
204         break;
205       }
206     }
207     item->setText(net->networkName());
208     if(select) item->setSelected(true);
209   }
210 }
211
212 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
213   this->setEnabled(state);
214   if(state) {
215     load();
216   } else {
217     // reset
218     //currentId = 0;
219   }
220 }
221
222 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
223   const Identity * identity = Client::identity(id);
224   connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
225
226   if(id == 1) {
227     // default identity is always the first one!
228     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
229   } else {
230     QString name = identity->identityName();
231     for(int j = 0; j < ui.identityList->count(); j++) {
232       if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
233         ui.identityList->insertItem(j, name, id.toInt());
234         widgetHasChanged();
235         return;
236       }
237     }
238     // append
239     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
240     widgetHasChanged();
241   }
242 }
243
244 void NetworksSettingsPage::clientIdentityUpdated() {
245   const Identity *identity = qobject_cast<const Identity *>(sender());
246   if(!identity) {
247     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
248     return;
249   }
250   int row = ui.identityList->findData(identity->id().toInt());
251   if(row < 0) {
252     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
253     return;
254   }
255   if(ui.identityList->itemText(row) != identity->identityName()) {
256     ui.identityList->setItemText(row, identity->identityName());
257   }
258 }
259
260 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
261   if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
262   //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
263   foreach(NetworkInfo info, networkInfos.values()) {
264     qDebug() << info.networkName << info.networkId << info.identity;
265     if(info.identity == id) {
266       if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
267       info.identity = 1; // set to default
268       networkInfos[info.networkId] = info;
269       if(info.networkId > 0) Client::updateNetwork(info);
270     }
271   }
272   ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
273   widgetHasChanged();
274 }
275
276 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
277   for(int i = 0; i < ui.networkList->count(); i++) { 
278     QListWidgetItem *item = ui.networkList->item(i);
279     if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
280   }
281   return 0;
282 }
283
284 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
285   insertNetwork(id);
286   connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
287   connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
288   connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
289 }
290
291 void NetworksSettingsPage::clientNetworkUpdated() {
292   const Network *net = qobject_cast<const Network *>(sender());
293   if(!net) {
294     qWarning() << "Update request for unknown network received!";
295     return;
296   }
297   networkInfos[net->networkId()] = net->networkInfo();
298   setItemState(net->networkId());
299   if(net->networkId() == currentId) displayNetwork(net->networkId());
300   setWidgetStates();
301   widgetHasChanged();
302 }
303
304 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
305   if(!networkInfos.contains(id)) return;
306   if(id == currentId) displayNetwork(0);
307   NetworkInfo info = networkInfos.take(id);
308   QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
309   if(items.count()) {
310     Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
311     delete ui.networkList->takeItem(ui.networkList->row(items[0]));
312   }
313   setWidgetStates();
314   widgetHasChanged();
315 }
316
317 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
318   const Network *net = qobject_cast<const Network *>(sender());
319   if(!net) return;
320   if(net->networkId() == currentId) {
321     ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
322   }
323   setItemState(net->networkId());
324 }
325
326 void NetworksSettingsPage::networkConnectionError(const QString &) {
327
328 }
329
330 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
331   NetworkInfo info = Client::network(id)->networkInfo();
332   networkInfos[id] = info;
333   return insertNetwork(info);
334 }
335
336 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
337   QListWidgetItem *item = 0;
338   QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
339   if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
340   else {
341     // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
342     // -> then we can be sure that this is the core-side replacement for the net we created
343     foreach(QListWidgetItem *i, items) {
344       NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
345       if(id < 0) { item = i; break; }
346     }
347     if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
348   }
349   item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
350   setItemState(info.networkId, item);
351   widgetHasChanged();
352   return item;
353 }
354
355 void NetworksSettingsPage::displayNetwork(NetworkId id) {
356   if(id != 0) {
357     NetworkInfo info = networkInfos[id];
358     ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
359     ui.serverList->clear();
360     foreach(QVariant v, info.serverList) {
361       ui.serverList->addItem(QString("%1:%2").arg(v.toMap()["Host"].toString()).arg(v.toMap()["Port"].toUInt()));
362     }
363     setItemState(id);
364   } else {
365     // just clear widgets
366     ui.identityList->setCurrentIndex(-1);
367     ui.serverList->clear();
368     ui.performEdit->clear();
369     setWidgetStates();
370   }
371   currentId = id;
372 }
373
374 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
375   info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
376 }
377 /*** Network list ***/
378
379 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
380   if(currentId != 0) {
381     saveToNetworkInfo(networkInfos[currentId]);
382   }
383   if(ui.networkList->selectedItems().count()) {
384     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
385     currentId = id;
386     displayNetwork(id);
387     ui.serverList->setCurrentRow(0);
388   } else {
389     currentId = 0;
390   }
391   setWidgetStates();
392 }
393
394 void NetworksSettingsPage::on_addNetwork_clicked() {
395   QStringList existing;
396   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
397   NetworkEditDlg dlg(QString(), existing, this);
398   if(dlg.exec() == QDialog::Accepted) {
399     NetworkId id;
400     for(id = 1; id <= networkInfos.count(); id++) {
401       widgetHasChanged();
402       if(!networkInfos.keys().contains(-id.toInt())) break;
403     }
404     id = -id.toInt();
405     NetworkInfo info;
406     info.networkId = id;
407     info.networkName = dlg.networkName();
408     info.identity = 1;
409     networkInfos[id] = info;
410     QListWidgetItem *item = insertNetwork(info);
411     ui.networkList->setCurrentItem(item);
412     setWidgetStates();
413   }
414 }
415
416 void NetworksSettingsPage::on_deleteNetwork_clicked() {
417   if(ui.networkList->selectedItems().count()) {
418     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
419     int ret = QMessageBox::question(this, tr("Delete Network?"),
420                                     tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
421                                     QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
422     if(ret == QMessageBox::Yes) {
423       currentId = 0;
424       networkInfos.remove(netid);
425       delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
426       ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
427       setWidgetStates();
428       widgetHasChanged();
429     }
430   }
431 }
432
433 void NetworksSettingsPage::on_renameNetwork_clicked() {
434   if(!ui.networkList->selectedItems().count()) return;
435   QString old = ui.networkList->selectedItems()[0]->text();
436   QStringList existing;
437   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
438   NetworkEditDlg dlg(old, existing, this);
439   if(dlg.exec() == QDialog::Accepted) {
440     ui.networkList->selectedItems()[0]->setText(dlg.networkName());
441     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
442     networkInfos[netid].networkName = dlg.networkName();
443     widgetHasChanged();
444   }
445 }
446
447 void NetworksSettingsPage::on_connectNow_clicked() {
448   if(!ui.networkList->selectedItems().count()) return;
449   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
450   const Network *net = Client::network(id);
451   if(!net) return;
452   if(!net->isConnected()) net->requestConnect();
453   else net->requestDisconnect();
454 }
455
456 /*** Server list ***/
457
458 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
459   setWidgetStates();
460 }
461
462 void NetworksSettingsPage::on_addServer_clicked() {
463   if(currentId == 0) return;
464   ServerEditDlg dlg(QVariantMap(), this);
465   if(dlg.exec() == QDialog::Accepted) {
466     networkInfos[currentId].serverList.append(dlg.serverData());
467     displayNetwork(currentId);
468     ui.serverList->setCurrentRow(ui.serverList->count()-1);
469     widgetHasChanged();
470   }
471
472 }
473
474 void NetworksSettingsPage::on_editServer_clicked() {
475   if(currentId == 0) return;
476   int cur = ui.serverList->currentRow();
477   ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
478   if(dlg.exec() == QDialog::Accepted) {
479     networkInfos[currentId].serverList[cur] = dlg.serverData();
480     displayNetwork(currentId);
481     ui.serverList->setCurrentRow(cur);
482     widgetHasChanged();
483   }
484 }
485
486 void NetworksSettingsPage::on_deleteServer_clicked() {
487   if(currentId == 0) return;
488   int cur = ui.serverList->currentRow();
489   networkInfos[currentId].serverList.removeAt(cur);
490   displayNetwork(currentId);
491   ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
492   widgetHasChanged();
493 }
494
495 void NetworksSettingsPage::on_upServer_clicked() {
496   int cur = ui.serverList->currentRow();
497   QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
498   networkInfos[currentId].serverList.insert(cur-1, foo);
499   displayNetwork(currentId);
500   ui.serverList->setCurrentRow(cur-1);
501   widgetHasChanged();
502 }
503
504 void NetworksSettingsPage::on_downServer_clicked() {
505   int cur = ui.serverList->currentRow();
506   QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
507   networkInfos[currentId].serverList.insert(cur+1, foo);
508   displayNetwork(currentId);
509   ui.serverList->setCurrentRow(cur+1);
510   widgetHasChanged();
511 }
512
513 /**************************************************************************
514  * NetworkEditDlg
515  *************************************************************************/
516
517 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
518   ui.setupUi(this);
519
520   if(old.isEmpty()) {
521     // new network
522     setWindowTitle(tr("Add Network"));
523     on_networkEdit_textChanged(""); // disable ok button
524   } else ui.networkEdit->setText(old);
525 }
526
527 QString NetworkEditDlg::networkName() const {
528   return ui.networkEdit->text();
529
530 }
531
532 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
533   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
534 }
535
536
537 /**************************************************************************
538  * ServerEditDlg
539  *************************************************************************/
540
541 ServerEditDlg::ServerEditDlg(const QVariant &_serverData, QWidget *parent) : QDialog(parent) {
542   ui.setupUi(this);
543   QVariantMap serverData = _serverData.toMap();
544   if(serverData.count()) {
545     ui.host->setText(serverData["Host"].toString());
546     ui.port->setValue(serverData["Port"].toUInt());
547     ui.password->setText(serverData["Password"].toString());
548     ui.useSSL->setChecked(serverData["UseSSL"].toBool());
549   } else {
550     ui.port->setValue(6667);
551   }
552   on_host_textChanged();
553 }
554
555 QVariant ServerEditDlg::serverData() const {
556   QVariantMap _serverData;
557   _serverData["Host"] = ui.host->text();
558   _serverData["Port"] = ui.port->value();
559   _serverData["Password"] = ui.password->text();
560   _serverData["UseSSL"] = ui.useSSL->isChecked();
561   return _serverData;
562 }
563
564 void ServerEditDlg::on_host_textChanged() {
565   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
566 }
567
568 /**************************************************************************
569  * SaveNetworksDlg
570  *************************************************************************/
571
572 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
573 {
574   ui.setupUi(this);
575
576   numevents = toCreate.count() + toUpdate.count() + toRemove.count();
577   rcvevents = 0;
578   if(numevents) {
579     ui.progressBar->setMaximum(numevents);
580     ui.progressBar->setValue(0);
581
582     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
583     connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
584
585     foreach(NetworkInfo info, toCreate) {
586       Client::createNetwork(info);
587     }
588     foreach(NetworkInfo info, toUpdate) {
589       const Network *net = Client::network(info.networkId);
590       if(!net) {
591         qWarning() << "Invalid client network!";
592         numevents--;
593         continue;
594       }
595       // FIXME this only checks for one changed item rather than all!
596       connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
597       Client::updateNetwork(info);
598     }
599     foreach(NetworkId id, toRemove) {
600       Client::removeNetwork(id);
601     }
602   } else {
603     qWarning() << "Sync dialog called without stuff to change!";
604     accept();
605   }
606 }
607
608 void SaveNetworksDlg::clientEvent() {
609   ui.progressBar->setValue(++rcvevents);
610   if(rcvevents >= numevents) accept();
611 }