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