Temporarily disabled add/remove networks because of a data corruption issue.
[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(false);  // 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   // FIXME disable network creation/deletion because of the storage issue
180   ui.addNetwork->setEnabled(false);
181   ui.deleteNetwork->setEnabled(false);
182 }
183
184 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
185   if(!item) item = networkItem(id);
186   const Network *net = Client::network(id);
187   if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
188   else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
189   if(net && net->connectionState() == Network::Initialized) {
190     item->setIcon(connectedIcon);
191   } else if(net && net->connectionState() != Network::Disconnected) {
192     item->setIcon(connectingIcon);
193   } else {
194     item->setIcon(disconnectedIcon);
195   }
196   if(net) {
197     // check if we already have another net of this name in the list, and replace it
198     QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
199     if(items.count()) {
200       foreach(QListWidgetItem *i, items) {
201         NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
202         if(oldid > 0) continue;  // only locally created nets should be replaced
203         if(oldid == currentId) item->setSelected(true);
204         delete ui.networkList->takeItem(ui.networkList->row(i));
205         networkInfos.remove(oldid);
206         break;
207       }
208     }
209     item->setText(net->networkName());
210   }
211 }
212
213 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
214   this->setEnabled(state);
215   if(state) {
216     load();
217   } else {
218     // reset
219     //currentId = 0;
220   }
221 }
222
223 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
224   const Identity * identity = Client::identity(id);
225   connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
226
227   if(id == 1) {
228     // default identity is always the first one!
229     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
230   } else {
231     QString name = identity->identityName();
232     for(int j = 0; j < ui.identityList->count(); j++) {
233       if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
234         ui.identityList->insertItem(j, name, id.toInt());
235         widgetHasChanged();
236         return;
237       }
238     }
239     // append
240     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
241     widgetHasChanged();
242   }
243 }
244
245 void NetworksSettingsPage::clientIdentityUpdated() {
246   const Identity *identity = qobject_cast<const Identity *>(sender());
247   if(!identity) {
248     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
249     return;
250   }
251   int row = ui.identityList->findData(identity->id().toInt());
252   if(row < 0) {
253     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
254     return;
255   }
256   if(ui.identityList->itemText(row) != identity->identityName()) {
257     ui.identityList->setItemText(row, identity->identityName());
258   }
259 }
260
261 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
262   if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
263   //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
264   foreach(NetworkInfo info, networkInfos.values()) {
265     qDebug() << info.networkName << info.networkId << info.identity;
266     if(info.identity == id) {
267       if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
268       info.identity = 1; // set to default
269       networkInfos[info.networkId] = info;
270       if(info.networkId > 0) Client::updateNetwork(info);
271     }
272   }
273   ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
274   widgetHasChanged();
275 }
276
277 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
278   for(int i = 0; i < ui.networkList->count(); i++) { 
279     QListWidgetItem *item = ui.networkList->item(i);
280     if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
281   }
282   return 0;
283 }
284
285 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
286   insertNetwork(id);
287   connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
288   connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
289   connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
290 }
291
292 void NetworksSettingsPage::clientNetworkUpdated() {
293   const Network *net = qobject_cast<const Network *>(sender());
294   if(!net) {
295     qWarning() << "Update request for unknown network received!";
296     return;
297   }
298   networkInfos[net->networkId()] = net->networkInfo();
299   setItemState(net->networkId());
300   if(net->networkId() == currentId) displayNetwork(net->networkId());
301   setWidgetStates();
302   widgetHasChanged();
303 }
304
305 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
306   if(!networkInfos.contains(id)) return;
307   if(id == currentId) displayNetwork(0);
308   NetworkInfo info = networkInfos.take(id);
309   QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
310   if(items.count()) {
311     Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
312     delete ui.networkList->takeItem(ui.networkList->row(items[0]));
313   }
314   setWidgetStates();
315   widgetHasChanged();
316 }
317
318 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
319   const Network *net = qobject_cast<const Network *>(sender());
320   if(!net) return;
321   if(net->networkId() == currentId) {
322     ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
323   }
324   setItemState(net->networkId());
325 }
326
327 void NetworksSettingsPage::networkConnectionError(const QString &) {
328
329 }
330
331 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
332   NetworkInfo info = Client::network(id)->networkInfo();
333   networkInfos[id] = info;
334   return insertNetwork(info);
335 }
336
337 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
338   QListWidgetItem *item = 0;
339   QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
340   if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
341   else {
342     // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
343     // -> then we can be sure that this is the core-side replacement for the net we created
344     foreach(QListWidgetItem *i, items) {
345       NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
346       if(id < 0) { item = i; break; }
347     }
348     if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
349   }
350   item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
351   setItemState(info.networkId, item);
352   widgetHasChanged();
353   return item;
354 }
355
356 void NetworksSettingsPage::displayNetwork(NetworkId id) {
357   if(id != 0) {
358     NetworkInfo info = networkInfos[id];
359     ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
360     ui.serverList->clear();
361     foreach(QVariant v, info.serverList) {
362       ui.serverList->addItem(QString("%1:%2").arg(v.toMap()["Host"].toString()).arg(v.toMap()["Port"].toUInt()));
363     }
364     setItemState(id);
365   } else {
366     // just clear widgets
367     ui.identityList->setCurrentIndex(-1);
368     ui.serverList->clear();
369     ui.performEdit->clear();
370     setWidgetStates();
371   }
372   currentId = id;
373 }
374
375 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
376   info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
377 }
378 /*** Network list ***/
379
380 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
381   if(currentId != 0) {
382     saveToNetworkInfo(networkInfos[currentId]);
383   }
384   if(ui.networkList->selectedItems().count()) {
385     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
386     currentId = id;
387     displayNetwork(id);
388     ui.serverList->setCurrentRow(0);
389   } else {
390     currentId = 0;
391   }
392   setWidgetStates();
393 }
394
395 void NetworksSettingsPage::on_addNetwork_clicked() {
396   QStringList existing;
397   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
398   NetworkEditDlg dlg(QString(), existing, this);
399   if(dlg.exec() == QDialog::Accepted) {
400     NetworkId id;
401     for(id = 1; id <= networkInfos.count(); id++) {
402       widgetHasChanged();
403       if(!networkInfos.keys().contains(-id.toInt())) break;
404     }
405     id = -id.toInt();
406     NetworkInfo info;
407     info.networkId = id;
408     info.networkName = dlg.networkName();
409     info.identity = 1;
410     networkInfos[id] = info;
411     QListWidgetItem *item = insertNetwork(info);
412     ui.networkList->setCurrentItem(item);
413     setWidgetStates();
414   }
415 }
416
417 void NetworksSettingsPage::on_deleteNetwork_clicked() {
418   if(ui.networkList->selectedItems().count()) {
419     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
420     int ret = QMessageBox::question(this, tr("Delete Network?"),
421                                     tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?"
422                                        "<br><br><em>NOTE: Backlog deletion hasn't actually been implemented yet.</em>").arg(networkInfos[netid].networkName),
423                                     QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
424     if(ret == QMessageBox::Yes) {
425       currentId = 0;
426       networkInfos.remove(netid);
427       delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
428       ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
429       setWidgetStates();
430       widgetHasChanged();
431     }
432   }
433 }
434
435 void NetworksSettingsPage::on_renameNetwork_clicked() {
436   if(!ui.networkList->selectedItems().count()) return;
437   QString old = ui.networkList->selectedItems()[0]->text();
438   QStringList existing;
439   for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
440   NetworkEditDlg dlg(old, existing, this);
441   if(dlg.exec() == QDialog::Accepted) {
442     ui.networkList->selectedItems()[0]->setText(dlg.networkName());
443     NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
444     networkInfos[netid].networkName = dlg.networkName();
445     widgetHasChanged();
446   }
447 }
448
449 void NetworksSettingsPage::on_connectNow_clicked() {
450   if(!ui.networkList->selectedItems().count()) return;
451   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
452   const Network *net = Client::network(id);
453   if(!net) return;
454   if(!net->isConnected()) net->requestConnect();
455   else net->requestDisconnect();
456 }
457
458 /*** Server list ***/
459
460 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
461   setWidgetStates();
462 }
463
464 void NetworksSettingsPage::on_addServer_clicked() {
465   if(currentId == 0) return;
466   ServerEditDlg dlg(QVariantMap(), this);
467   if(dlg.exec() == QDialog::Accepted) {
468     networkInfos[currentId].serverList.append(dlg.serverData());
469     displayNetwork(currentId);
470     ui.serverList->setCurrentRow(ui.serverList->count()-1);
471     widgetHasChanged();
472   }
473
474 }
475
476 void NetworksSettingsPage::on_editServer_clicked() {
477   if(currentId == 0) return;
478   int cur = ui.serverList->currentRow();
479   ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
480   if(dlg.exec() == QDialog::Accepted) {
481     networkInfos[currentId].serverList[cur] = dlg.serverData();
482     displayNetwork(currentId);
483     ui.serverList->setCurrentRow(cur);
484     widgetHasChanged();
485   }
486 }
487
488 void NetworksSettingsPage::on_deleteServer_clicked() {
489   if(currentId == 0) return;
490   int cur = ui.serverList->currentRow();
491   networkInfos[currentId].serverList.removeAt(cur);
492   displayNetwork(currentId);
493   ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
494   widgetHasChanged();
495 }
496
497 void NetworksSettingsPage::on_upServer_clicked() {
498   int cur = ui.serverList->currentRow();
499   QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
500   networkInfos[currentId].serverList.insert(cur-1, foo);
501   displayNetwork(currentId);
502   ui.serverList->setCurrentRow(cur-1);
503   widgetHasChanged();
504 }
505
506 void NetworksSettingsPage::on_downServer_clicked() {
507   int cur = ui.serverList->currentRow();
508   QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
509   networkInfos[currentId].serverList.insert(cur+1, foo);
510   displayNetwork(currentId);
511   ui.serverList->setCurrentRow(cur+1);
512   widgetHasChanged();
513 }
514
515 /**************************************************************************
516  * NetworkEditDlg
517  *************************************************************************/
518
519 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
520   ui.setupUi(this);
521
522   if(old.isEmpty()) {
523     // new network
524     setWindowTitle(tr("Add Network"));
525     on_networkEdit_textChanged(""); // disable ok button
526   } else ui.networkEdit->setText(old);
527 }
528
529 QString NetworkEditDlg::networkName() const {
530   return ui.networkEdit->text();
531
532 }
533
534 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
535   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
536 }
537
538
539 /**************************************************************************
540  * ServerEditDlg
541  *************************************************************************/
542
543 ServerEditDlg::ServerEditDlg(const QVariant &_serverData, QWidget *parent) : QDialog(parent) {
544   ui.setupUi(this);
545   QVariantMap serverData = _serverData.toMap();
546   if(serverData.count()) {
547     ui.host->setText(serverData["Host"].toString());
548     ui.port->setValue(serverData["Port"].toUInt());
549     ui.password->setText(serverData["Password"].toString());
550     ui.useSSL->setChecked(serverData["UseSSL"].toBool());
551   } else {
552     ui.port->setValue(Global::defaultPort);
553   }
554   on_host_textChanged();
555 }
556
557 QVariant ServerEditDlg::serverData() const {
558   QVariantMap _serverData;
559   _serverData["Host"] = ui.host->text();
560   _serverData["Port"] = ui.port->value();
561   _serverData["Password"] = ui.password->text();
562   _serverData["UseSSL"] = ui.useSSL->isChecked();
563   return _serverData;
564 }
565
566 void ServerEditDlg::on_host_textChanged() {
567   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
568 }
569
570 /**************************************************************************
571  * SaveNetworksDlg
572  *************************************************************************/
573
574 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
575 {
576   ui.setupUi(this);
577
578   numevents = toCreate.count() + toUpdate.count() + toRemove.count();
579   rcvevents = 0;
580   if(numevents) {
581     ui.progressBar->setMaximum(numevents);
582     ui.progressBar->setValue(0);
583
584     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
585     connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
586
587     foreach(NetworkInfo info, toCreate) {
588       Client::createNetwork(info);
589     }
590     foreach(NetworkInfo info, toUpdate) {
591       const Network *net = Client::network(info.networkId);
592       if(!net) {
593         qWarning() << "Invalid client network!";
594         numevents--;
595         continue;
596       }
597       // FIXME this only checks for one changed item rather than all!
598       connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
599       Client::updateNetwork(info);
600     }
601     foreach(NetworkId id, toRemove) {
602       Client::removeNetwork(id);
603     }
604   } else {
605     qWarning() << "Sync dialog called without stuff to change!";
606     accept();
607   }
608 }
609
610 void SaveNetworksDlg::clientEvent() {
611   ui.progressBar->setValue(++rcvevents);
612   if(rcvevents >= numevents) accept();
613 }