uisupport: Provide helpers for dealing with widget changes
[quassel.git] / src / qtui / settingspages / networkssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QHeaderView>
22 #include <QMessageBox>
23 #include <QTextCodec>
24 #include <utility>
25
26 #include "networkssettingspage.h"
27
28 #include "client.h"
29 #include "icon.h"
30 #include "identity.h"
31 #include "network.h"
32 #include "presetnetworks.h"
33 #include "settingspagedlg.h"
34 #include "util.h"
35 #include "widgethelpers.h"
36
37 // IRCv3 capabilities
38 #include "irccap.h"
39
40 #include "settingspages/identitiessettingspage.h"
41
42 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
43     : SettingsPage(tr("IRC"), tr("Networks"), parent)
44 {
45     ui.setupUi(this);
46
47     // hide SASL options for older cores
48     if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
49         ui.sasl->hide();
50     if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
51         ui.saslExtInfo->hide();
52 #ifndef HAVE_SSL
53     ui.saslExtInfo->hide();
54 #endif
55
56     // set up icons
57     ui.renameNetwork->setIcon(icon::get("edit-rename"));
58     ui.addNetwork->setIcon(icon::get("list-add"));
59     ui.deleteNetwork->setIcon(icon::get("edit-delete"));
60     ui.addServer->setIcon(icon::get("list-add"));
61     ui.deleteServer->setIcon(icon::get("edit-delete"));
62     ui.editServer->setIcon(icon::get("configure"));
63     ui.upServer->setIcon(icon::get("go-up"));
64     ui.downServer->setIcon(icon::get("go-down"));
65     ui.editIdentities->setIcon(icon::get("configure"));
66
67     connectedIcon = icon::get("network-connect");
68     connectingIcon = icon::get("network-wired"); // FIXME network-connecting
69     disconnectedIcon = icon::get("network-disconnect");
70
71     // Status icons
72     infoIcon = icon::get("dialog-information");
73     warningIcon = icon::get("dialog-warning");
74
75     foreach(int mib, QTextCodec::availableMibs()) {
76         QByteArray codec = QTextCodec::codecForMib(mib)->name();
77         ui.sendEncoding->addItem(codec);
78         ui.recvEncoding->addItem(codec);
79         ui.serverEncoding->addItem(codec);
80     }
81     ui.sendEncoding->model()->sort(0);
82     ui.recvEncoding->model()->sort(0);
83     ui.serverEncoding->model()->sort(0);
84     currentId = 0;
85     setEnabled(Client::isConnected()); // need a core connection!
86     setWidgetStates();
87
88     connectToWidgetsChangedSignals({
89             ui.identityList,
90             ui.performEdit,
91             ui.sasl,
92             ui.saslAccount,
93             ui.saslPassword,
94             ui.autoIdentify,
95             ui.autoIdentifyService,
96             ui.autoIdentifyPassword,
97             ui.useCustomEncodings,
98             ui.sendEncoding,
99             ui.recvEncoding,
100             ui.serverEncoding,
101             ui.autoReconnect,
102             ui.reconnectInterval,
103             ui.reconnectRetries,
104             ui.unlimitedRetries,
105             ui.rejoinOnReconnect,
106             ui.useCustomMessageRate,
107             ui.messageRateBurstSize,
108             ui.messageRateDelay,
109             ui.unlimitedMessageRate
110     }, this, &NetworksSettingsPage::widgetHasChanged);
111
112     connect(Client::instance(), &Client::coreConnectionStateChanged, this, &NetworksSettingsPage::coreConnectionStateChanged);
113     connect(Client::instance(), &Client::networkCreated, this, &NetworksSettingsPage::clientNetworkAdded);
114     connect(Client::instance(), &Client::networkRemoved, this, &NetworksSettingsPage::clientNetworkRemoved);
115     connect(Client::instance(), &Client::identityCreated, this, &NetworksSettingsPage::clientIdentityAdded);
116     connect(Client::instance(), &Client::identityRemoved, this, &NetworksSettingsPage::clientIdentityRemoved);
117
118     foreach(IdentityId id, Client::identityIds()) {
119         clientIdentityAdded(id);
120     }
121 }
122
123
124 void NetworksSettingsPage::save()
125 {
126     setEnabled(false);
127     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
128
129     QList<NetworkInfo> toCreate, toUpdate;
130     QList<NetworkId> toRemove;
131     QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
132     while (i != networkInfos.end()) {
133         NetworkId id = (*i).networkId;
134         if (id < 0) {
135             toCreate.append(*i);
136             //if(id == currentId) currentId = 0;
137             //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
138             //if(items.count()) {
139             //  Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
140             //  delete items[0];
141             //}
142             //i = networkInfos.erase(i);
143             ++i;
144         }
145         else {
146             if ((*i) != Client::network((*i).networkId)->networkInfo()) {
147                 toUpdate.append(*i);
148             }
149             ++i;
150         }
151     }
152     foreach(NetworkId id, Client::networkIds()) {
153         if (!networkInfos.contains(id)) toRemove.append(id);
154     }
155     SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
156     int ret = dlg.exec();
157     if (ret == QDialog::Rejected) {
158         // canceled -> reload everything to be safe
159         load();
160     }
161     setChangedState(false);
162     setEnabled(true);
163 }
164
165
166 void NetworksSettingsPage::load()
167 {
168     reset();
169
170     // Handle UI dependent on core feature flags here
171     if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
172         // Custom rate limiting supported, allow toggling
173         ui.useCustomMessageRate->setEnabled(true);
174         // Reset tooltip to default.
175         ui.useCustomMessageRate->setToolTip(QString("%1").arg(
176                                           tr("<p>Override default message rate limiting.</p>"
177                                              "<p><b>Setting limits too low may get you disconnected"
178                                              " from the server!</b></p>")));
179         // If changed, update the message below!
180     } else {
181         // Custom rate limiting not supported, disallow toggling
182         ui.useCustomMessageRate->setEnabled(false);
183         // Split up the message to allow re-using translations:
184         // [Original tool-tip]
185         // [Bold 'does not support feature' message]
186         // [Specific version needed and feature details]
187         ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
188                                           tr("<p>Override default message rate limiting.</p>"
189                                              "<p><b>Setting limits too low may get you disconnected"
190                                              " from the server!</b></p>"),
191                                           tr("Your Quassel core does not support this feature"),
192                                           tr("You need a Quassel core v0.13.0 or newer in order to "
193                                           "modify message rate limits.")));
194     }
195
196 #ifdef HAVE_SSL
197     // Hide the SASL EXTERNAL notice until a network's shown.  Stops it from showing while loading
198     // backlog from the core.
199     sslUpdated();
200 #endif
201
202     // Reset network capability status in case no valid networks get selected (a rare situation)
203     resetNetworkCapStates();
204
205     foreach(NetworkId netid, Client::networkIds()) {
206         clientNetworkAdded(netid);
207     }
208     ui.networkList->setCurrentRow(0);
209
210     setChangedState(false);
211 }
212
213
214 void NetworksSettingsPage::reset()
215 {
216     currentId = 0;
217     ui.networkList->clear();
218     networkInfos.clear();
219 }
220
221
222 bool NetworksSettingsPage::aboutToSave()
223 {
224     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
225     QList<int> errors;
226     foreach(NetworkInfo info, networkInfos.values()) {
227         if (!info.serverList.count()) errors.append(1);
228     }
229     if (!errors.count()) return true;
230     QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
231     if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
232     error += tr("</ul>");
233     QMessageBox::warning(this, tr("Invalid Network Settings"), error);
234     return false;
235 }
236
237
238 void NetworksSettingsPage::widgetHasChanged()
239 {
240     if (_ignoreWidgetChanges) return;
241     bool changed = testHasChanged();
242     if (changed != hasChanged()) setChangedState(changed);
243 }
244
245
246 bool NetworksSettingsPage::testHasChanged()
247 {
248     if (currentId != 0) {
249         saveToNetworkInfo(networkInfos[currentId]);
250     }
251     if (Client::networkIds().count() != networkInfos.count()) return true;
252     foreach(NetworkId id, networkInfos.keys()) {
253         if (id < 0) return true;
254         if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
255     }
256     return false;
257 }
258
259
260 void NetworksSettingsPage::setWidgetStates()
261 {
262     // network list
263     if (ui.networkList->selectedItems().count()) {
264         ui.detailsBox->setEnabled(true);
265         ui.renameNetwork->setEnabled(true);
266         ui.deleteNetwork->setEnabled(true);
267
268         /* button disabled for now
269         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
270         const Network *net = id > 0 ? Client::network(id) : 0;
271         ui.connectNow->setEnabled(net);
272         //    && (Client::network(id)->connectionState() == Network::Initialized
273         //    || Client::network(id)->connectionState() == Network::Disconnected));
274         if(net) {
275           if(net->connectionState() == Network::Disconnected) {
276             ui.connectNow->setIcon(connectedIcon);
277             ui.connectNow->setText(tr("Connect"));
278           } else {
279             ui.connectNow->setIcon(disconnectedIcon);
280             ui.connectNow->setText(tr("Disconnect"));
281           }
282         } else {
283           ui.connectNow->setIcon(QIcon());
284           ui.connectNow->setText(tr("Apply first!"));
285         } */
286     }
287     else {
288         ui.renameNetwork->setEnabled(false);
289         ui.deleteNetwork->setEnabled(false);
290         //ui.connectNow->setEnabled(false);
291         ui.detailsBox->setEnabled(false);
292     }
293     // network details
294     if (ui.serverList->selectedItems().count()) {
295         ui.editServer->setEnabled(true);
296         ui.deleteServer->setEnabled(true);
297         ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
298         ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
299     }
300     else {
301         ui.editServer->setEnabled(false);
302         ui.deleteServer->setEnabled(false);
303         ui.upServer->setEnabled(false);
304         ui.downServer->setEnabled(false);
305     }
306 }
307
308
309 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
310 {
311     if (!item && !(item = networkItem(id))) return;
312     const Network *net = Client::network(id);
313     if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
314     else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
315     if (net && net->connectionState() == Network::Initialized) {
316         item->setIcon(connectedIcon);
317     }
318     else if (net && net->connectionState() != Network::Disconnected) {
319         item->setIcon(connectingIcon);
320     }
321     else {
322         item->setIcon(disconnectedIcon);
323     }
324     if (net) {
325         bool select = false;
326         // check if we already have another net of this name in the list, and replace it
327         QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
328         if (items.count()) {
329             foreach(QListWidgetItem *i, items) {
330                 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
331                 if (oldid > 0) continue;  // only locally created nets should be replaced
332                 if (oldid == currentId) {
333                     select = true;
334                     currentId = 0;
335                     ui.networkList->clearSelection();
336                 }
337                 int row = ui.networkList->row(i);
338                 if (row >= 0) {
339                     QListWidgetItem *olditem = ui.networkList->takeItem(row);
340                     Q_ASSERT(olditem);
341                     delete olditem;
342                 }
343                 networkInfos.remove(oldid);
344                 break;
345             }
346         }
347         item->setText(net->networkName());
348         if (select) item->setSelected(true);
349     }
350 }
351
352
353 void NetworksSettingsPage::resetNetworkCapStates()
354 {
355     // Set the status to a blank (invalid) network ID, reseting all UI
356     setNetworkCapStates(NetworkId());
357 }
358
359
360 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
361 {
362     const Network *net = Client::network(id);
363     if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
364         // Capability negotiation is supported, network exists.
365         // Check if the network is connected.  Don't use net->isConnected() as that won't be true
366         // during capability negotiation when capabilities are added and removed.
367         if (net->connectionState() != Network::Disconnected) {
368             // Network exists and is connected, check available capabilities...
369             // [SASL]
370             if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
371                 setSASLStatus(CapSupportStatus::MaybeSupported);
372             } else {
373                 setSASLStatus(CapSupportStatus::MaybeUnsupported);
374             }
375
376             // Add additional capability-dependent interface updates here
377         } else {
378             // Network is disconnected
379             // [SASL]
380             setSASLStatus(CapSupportStatus::Disconnected);
381
382             // Add additional capability-dependent interface updates here
383         }
384     } else {
385         // Capability negotiation is not supported and/or network doesn't exist.
386         // Don't assume anything and reset all capability-dependent interface elements to neutral.
387         // [SASL]
388         setSASLStatus(CapSupportStatus::Unknown);
389
390         // Add additional capability-dependent interface updates here
391     }
392 }
393
394
395 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
396 {
397     this->setEnabled(state);
398     if (state) {
399         load();
400     }
401     else {
402         // reset
403         //currentId = 0;
404     }
405 }
406
407
408 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
409 {
410     const Identity *identity = Client::identity(id);
411     connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
412
413     QString name = identity->identityName();
414     for (int j = 0; j < ui.identityList->count(); j++) {
415         if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
416             ui.identityList->insertItem(j, name, id.toInt());
417             widgetHasChanged();
418             return;
419         }
420     }
421     // append
422     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
423     widgetHasChanged();
424 }
425
426
427 void NetworksSettingsPage::clientIdentityUpdated()
428 {
429     const auto *identity = qobject_cast<const Identity *>(sender());
430     if (!identity) {
431         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
432         return;
433     }
434     int row = ui.identityList->findData(identity->id().toInt());
435     if (row < 0) {
436         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
437         return;
438     }
439     if (ui.identityList->itemText(row) != identity->identityName()) {
440         ui.identityList->setItemText(row, identity->identityName());
441     }
442 }
443
444
445 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
446 {
447     IdentityId defaultId = defaultIdentity();
448     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
449     foreach(NetworkInfo info, networkInfos.values()) {
450         if (info.identity == id) {
451             if (info.networkId == currentId)
452                 ui.identityList->setCurrentIndex(0);
453             info.identity = defaultId;
454             networkInfos[info.networkId] = info;
455             if (info.networkId > 0) Client::updateNetwork(info);
456         }
457     }
458     ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
459     widgetHasChanged();
460 }
461
462
463 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
464 {
465     for (int i = 0; i < ui.networkList->count(); i++) {
466         QListWidgetItem *item = ui.networkList->item(i);
467         if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
468     }
469     return nullptr;
470 }
471
472
473 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
474 {
475     insertNetwork(id);
476     //connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
477     connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
478
479     connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
480     connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
481
482     // Handle capability changes in case a server dis/connects with the settings window open.
483     connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
484     connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
485 }
486
487
488 void NetworksSettingsPage::clientNetworkUpdated()
489 {
490     const auto *net = qobject_cast<const Network *>(sender());
491     if (!net) {
492         qWarning() << "Update request for unknown network received!";
493         return;
494     }
495     networkInfos[net->networkId()] = net->networkInfo();
496     setItemState(net->networkId());
497     if (net->networkId() == currentId) displayNetwork(net->networkId());
498     setWidgetStates();
499     widgetHasChanged();
500 }
501
502
503 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
504 {
505     if (!networkInfos.contains(id)) return;
506     if (id == currentId) displayNetwork(0);
507     NetworkInfo info = networkInfos.take(id);
508     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
509     foreach(QListWidgetItem *item, items) {
510         if (item->data(Qt::UserRole).value<NetworkId>() == id)
511             delete ui.networkList->takeItem(ui.networkList->row(item));
512     }
513     setWidgetStates();
514     widgetHasChanged();
515 }
516
517
518 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
519 {
520     Q_UNUSED(state);
521     const auto *net = qobject_cast<const Network *>(sender());
522     if (!net) return;
523     /*
524     if(net->networkId() == currentId) {
525       ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
526     }
527     */
528     setItemState(net->networkId());
529     if (net->networkId() == currentId) {
530         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
531         // changed.
532         setNetworkCapStates(currentId);
533     }
534     setWidgetStates();
535 }
536
537
538 void NetworksSettingsPage::networkConnectionError(const QString &)
539 {
540 }
541
542
543 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
544 {
545     NetworkInfo info = Client::network(id)->networkInfo();
546     networkInfos[id] = info;
547     return insertNetwork(info);
548 }
549
550
551 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
552 {
553     QListWidgetItem *item = nullptr;
554     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
555     if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
556     else {
557         // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
558         // -> then we can be sure that this is the core-side replacement for the net we created
559         foreach(QListWidgetItem *i, items) {
560             NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
561             if (id < 0) { item = i; break; }
562         }
563         if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
564     }
565     item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
566     setItemState(info.networkId, item);
567     widgetHasChanged();
568     return item;
569 }
570
571
572 // Called when selecting 'Configure' from the buffer list
573 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
574 {
575     QListWidgetItem *item = networkItem(netId);
576     ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
577 }
578
579
580 void NetworksSettingsPage::displayNetwork(NetworkId id)
581 {
582     _ignoreWidgetChanges = true;
583     if (id != 0) {
584         NetworkInfo info = networkInfos[id];
585
586 #ifdef HAVE_SSL
587         // this is only needed when the core supports SASL EXTERNAL
588         if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
589             if (_cid) {
590                 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
591                 delete _cid;
592             }
593             _cid = new CertIdentity(*Client::identity(info.identity), this);
594             _cid->enableEditSsl(true);
595             connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
596         }
597 #endif
598
599         ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
600         ui.serverList->clear();
601         foreach(Network::Server server, info.serverList) {
602             QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
603             if (server.useSsl)
604                 item->setIcon(icon::get("document-encrypt"));
605             ui.serverList->addItem(item);
606         }
607         //setItemState(id);
608         //ui.randomServer->setChecked(info.useRandomServer);
609         // Update the capability-dependent UI in case capabilities have changed.
610         setNetworkCapStates(id);
611         ui.performEdit->setPlainText(info.perform.join("\n"));
612         ui.autoIdentify->setChecked(info.useAutoIdentify);
613         ui.autoIdentifyService->setText(info.autoIdentifyService);
614         ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
615         ui.sasl->setChecked(info.useSasl);
616         ui.saslAccount->setText(info.saslAccount);
617         ui.saslPassword->setText(info.saslPassword);
618         if (info.codecForEncoding.isEmpty()) {
619             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
620             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
621             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
622             ui.useCustomEncodings->setChecked(false);
623         }
624         else {
625             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
626             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
627             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
628             ui.useCustomEncodings->setChecked(true);
629         }
630         ui.autoReconnect->setChecked(info.useAutoReconnect);
631         ui.reconnectInterval->setValue(info.autoReconnectInterval);
632         ui.reconnectRetries->setValue(info.autoReconnectRetries);
633         ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
634         ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
635         // Custom rate limiting
636         ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
637         // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
638         // disabled, 'ui.messageRateDelayFrame' will remain disabled.
639         ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
640         ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
641         // Convert milliseconds (integer) into seconds (double)
642         ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
643     }
644     else {
645         // just clear widgets
646 #ifdef HAVE_SSL
647         if (_cid) {
648             disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
649             delete _cid;
650         }
651 #endif
652         ui.identityList->setCurrentIndex(-1);
653         ui.serverList->clear();
654         ui.performEdit->clear();
655         ui.autoIdentifyService->clear();
656         ui.autoIdentifyPassword->clear();
657         ui.saslAccount->clear();
658         ui.saslPassword->clear();
659         setWidgetStates();
660     }
661     _ignoreWidgetChanges = false;
662     currentId = id;
663 }
664
665
666 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
667 {
668     info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
669     //info.useRandomServer = ui.randomServer->isChecked();
670     info.perform = ui.performEdit->toPlainText().split("\n");
671     info.useAutoIdentify = ui.autoIdentify->isChecked();
672     info.autoIdentifyService = ui.autoIdentifyService->text();
673     info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
674     info.useSasl = ui.sasl->isChecked();
675     info.saslAccount = ui.saslAccount->text();
676     info.saslPassword = ui.saslPassword->text();
677     if (!ui.useCustomEncodings->isChecked()) {
678         info.codecForEncoding.clear();
679         info.codecForDecoding.clear();
680         info.codecForServer.clear();
681     }
682     else {
683         info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
684         info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
685         info.codecForServer = ui.serverEncoding->currentText().toLatin1();
686     }
687     info.useAutoReconnect = ui.autoReconnect->isChecked();
688     info.autoReconnectInterval = ui.reconnectInterval->value();
689     info.autoReconnectRetries = ui.reconnectRetries->value();
690     info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
691     info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
692     // Custom rate limiting
693     info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
694     info.messageRateBurstSize = ui.messageRateBurstSize->value();
695     // Convert seconds (double) into milliseconds (integer)
696     info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
697     info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
698 }
699
700
701 void NetworksSettingsPage::clientNetworkCapsUpdated()
702 {
703     // Grab the updated network
704     const auto *net = qobject_cast<const Network *>(sender());
705     if (!net) {
706         qWarning() << "Update request for unknown network received!";
707         return;
708     }
709     if (net->networkId() == currentId) {
710         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
711         // changed.
712         setNetworkCapStates(currentId);
713     }
714 }
715
716
717 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
718 {
719     if (_saslStatusSelected != saslStatus) {
720         // Update the cached copy of SASL status used with the Details dialog
721         _saslStatusSelected = saslStatus;
722
723         // Update the user interface
724         switch (saslStatus) {
725             case CapSupportStatus::Unknown:
726                 // There's no capability negotiation or network doesn't exist.  Don't assume
727                 // anything.
728                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
729                                             tr("Could not check if supported by network")));
730                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
731                 break;
732             case CapSupportStatus::Disconnected:
733                 // Disconnected from network, no way to check.
734                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
735                                                 tr("Cannot check if supported when disconnected")));
736                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
737                 break;
738             case CapSupportStatus::MaybeUnsupported:
739                 // The network doesn't advertise support for SASL PLAIN.  Here be dragons.
740                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
741                                                 tr("Not currently supported by network")));
742                 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
743                 break;
744             case CapSupportStatus::MaybeSupported:
745                 // The network advertises support for SASL PLAIN.  Encourage using it!
746                 // Unfortunately we don't know for sure if it's desired or functional.
747                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
748                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
749                 break;
750         }
751     }
752 }
753
754
755 #ifdef HAVE_SSL
756 void NetworksSettingsPage::sslUpdated()
757 {
758     if (_cid && !_cid->sslKey().isNull()) {
759         ui.saslContents->setDisabled(true);
760         ui.saslExtInfo->setHidden(false);
761     } else {
762         ui.saslContents->setDisabled(false);
763         // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
764         // state to indicate whether or not it's disabled.  To workaround this, keep track of
765         // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
766         // group box.
767         if (!ui.sasl->isChecked()) {
768             // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
769             // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
770             // sasl is later checked.
771             ui.sasl->setChecked(true);
772             ui.sasl->setChecked(false);
773         }
774         ui.saslExtInfo->setHidden(true);
775     }
776 }
777 #endif
778
779
780 /*** Network list ***/
781
782 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
783 {
784     if (currentId != 0) {
785         saveToNetworkInfo(networkInfos[currentId]);
786     }
787     if (ui.networkList->selectedItems().count()) {
788         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
789         currentId = id;
790         displayNetwork(id);
791         ui.serverList->setCurrentRow(0);
792     }
793     else {
794         currentId = 0;
795     }
796     setWidgetStates();
797 }
798
799
800 void NetworksSettingsPage::on_addNetwork_clicked()
801 {
802     QStringList existing;
803     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
804     NetworkAddDlg dlg(existing, this);
805     if (dlg.exec() == QDialog::Accepted) {
806         NetworkInfo info = dlg.networkInfo();
807         if (info.networkName.isEmpty())
808             return;  // sanity check
809
810         NetworkId id;
811         for (id = 1; id <= networkInfos.count(); id++) {
812             widgetHasChanged();
813             if (!networkInfos.keys().contains(-id.toInt())) break;
814         }
815         id = -id.toInt();
816         info.networkId = id;
817         info.identity = defaultIdentity();
818         networkInfos[id] = info;
819         QListWidgetItem *item = insertNetwork(info);
820         ui.networkList->setCurrentItem(item);
821         setWidgetStates();
822     }
823 }
824
825
826 void NetworksSettingsPage::on_deleteNetwork_clicked()
827 {
828     if (ui.networkList->selectedItems().count()) {
829         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
830         int ret = QMessageBox::question(this, tr("Delete Network?"),
831             tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
832             QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
833         if (ret == QMessageBox::Yes) {
834             currentId = 0;
835             networkInfos.remove(netid);
836             delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
837             ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
838             setWidgetStates();
839             widgetHasChanged();
840         }
841     }
842 }
843
844
845 void NetworksSettingsPage::on_renameNetwork_clicked()
846 {
847     if (!ui.networkList->selectedItems().count()) return;
848     QString old = ui.networkList->selectedItems()[0]->text();
849     QStringList existing;
850     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
851     NetworkEditDlg dlg(old, existing, this);
852     if (dlg.exec() == QDialog::Accepted) {
853         ui.networkList->selectedItems()[0]->setText(dlg.networkName());
854         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
855         networkInfos[netid].networkName = dlg.networkName();
856         widgetHasChanged();
857     }
858 }
859
860
861 /*
862 void NetworksSettingsPage::on_connectNow_clicked() {
863   if(!ui.networkList->selectedItems().count()) return;
864   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
865   const Network *net = Client::network(id);
866   if(!net) return;
867   if(net->connectionState() == Network::Disconnected) net->requestConnect();
868   else net->requestDisconnect();
869 }
870 */
871
872 /*** Server list ***/
873
874 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
875 {
876     setWidgetStates();
877 }
878
879
880 void NetworksSettingsPage::on_addServer_clicked()
881 {
882     if (currentId == 0) return;
883     ServerEditDlg dlg(Network::Server(), this);
884     if (dlg.exec() == QDialog::Accepted) {
885         networkInfos[currentId].serverList.append(dlg.serverData());
886         displayNetwork(currentId);
887         ui.serverList->setCurrentRow(ui.serverList->count()-1);
888         widgetHasChanged();
889     }
890 }
891
892
893 void NetworksSettingsPage::on_editServer_clicked()
894 {
895     if (currentId == 0) return;
896     int cur = ui.serverList->currentRow();
897     ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
898     if (dlg.exec() == QDialog::Accepted) {
899         networkInfos[currentId].serverList[cur] = dlg.serverData();
900         displayNetwork(currentId);
901         ui.serverList->setCurrentRow(cur);
902         widgetHasChanged();
903     }
904 }
905
906
907 void NetworksSettingsPage::on_deleteServer_clicked()
908 {
909     if (currentId == 0) return;
910     int cur = ui.serverList->currentRow();
911     networkInfos[currentId].serverList.removeAt(cur);
912     displayNetwork(currentId);
913     ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
914     widgetHasChanged();
915 }
916
917
918 void NetworksSettingsPage::on_upServer_clicked()
919 {
920     int cur = ui.serverList->currentRow();
921     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
922     networkInfos[currentId].serverList.insert(cur-1, server);
923     displayNetwork(currentId);
924     ui.serverList->setCurrentRow(cur-1);
925     widgetHasChanged();
926 }
927
928
929 void NetworksSettingsPage::on_downServer_clicked()
930 {
931     int cur = ui.serverList->currentRow();
932     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
933     networkInfos[currentId].serverList.insert(cur+1, server);
934     displayNetwork(currentId);
935     ui.serverList->setCurrentRow(cur+1);
936     widgetHasChanged();
937 }
938
939
940 void NetworksSettingsPage::on_editIdentities_clicked()
941 {
942     SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
943     dlg.exec();
944 }
945
946
947 void NetworksSettingsPage::on_saslStatusDetails_clicked()
948 {
949     if (ui.networkList->selectedItems().count()) {
950         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
951         QString &netName = networkInfos[netid].networkName;
952
953         // If these strings are visible, one of the status messages wasn't detected below.
954         QString saslStatusHeader = "[header unintentionally left blank]";
955         QString saslStatusExplanation = "[explanation unintentionally left blank]";
956
957         // If true, show a warning icon instead of an information icon
958         bool useWarningIcon = false;
959
960         // Determine which explanation to show
961         switch (_saslStatusSelected) {
962         case CapSupportStatus::Unknown:
963             saslStatusHeader = tr("Could not check if SASL supported by network");
964             saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL.  This may "
965                                        "be due to unsaved changes or an older Quassel core.  You "
966                                        "can still try using SASL.").arg(netName);
967             break;
968         case CapSupportStatus::Disconnected:
969             saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
970             saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
971                                        "disconnected.  Connect to the network, or try using SASL "
972                                        "anyways.").arg(netName);
973             break;
974         case CapSupportStatus::MaybeUnsupported:
975             saslStatusHeader = tr("SASL not currently supported by network");
976             saslStatusExplanation = tr("The network \"%1\" does not currently support SASL.  "
977                                        "However, support might be added later on.").arg(netName);
978             useWarningIcon = true;
979             break;
980         case CapSupportStatus::MaybeSupported:
981             saslStatusHeader = tr("SASL supported by network");
982             saslStatusExplanation = tr("The network \"%1\" supports SASL.  In most cases, you "
983                                        "should use SASL instead of NickServ identification."
984                                        ).arg(netName);
985             break;
986         }
987
988         // Process this in advance for reusability below
989         const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
990         const QString saslStatusMsgText =
991                 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
992                         ).arg(saslStatusHeader,
993                               saslStatusExplanation,
994                               tr("SASL is a standardized way to log in and identify yourself to "
995                                  "IRC servers."));
996
997         if (useWarningIcon) {
998             // Show as a warning dialog box
999             QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1000         } else {
1001             // Show as an information dialog box
1002             QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1003         }
1004     }
1005 }
1006
1007
1008 IdentityId NetworksSettingsPage::defaultIdentity() const
1009 {
1010     IdentityId defaultId = 0;
1011     QList<IdentityId> ids = Client::identityIds();
1012     foreach(IdentityId id, ids) {
1013         if (defaultId == 0 || id < defaultId)
1014             defaultId = id;
1015     }
1016     return defaultId;
1017 }
1018
1019
1020 /**************************************************************************
1021 * NetworkAddDlg
1022 *************************************************************************/
1023
1024 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1025 {
1026     ui.setupUi(this);
1027     ui.useSSL->setIcon(icon::get("document-encrypt"));
1028
1029     // Whenever useSSL is toggled, update the port number if not changed from the default
1030     connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1031     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1032     // If useSSL is later changed to be checked by default, change port's default value, too.
1033
1034     if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1035         // Synchronize requiring SSL with the use SSL checkbox
1036         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1037         connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1038     } else {
1039         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1040         ui.sslVerify->setEnabled(false);
1041         ui.sslVerify->setChecked(false);
1042         // Split up the message to allow re-using translations:
1043         // [Original tool-tip]
1044         // [Bold 'does not support feature' message]
1045         // [Specific version needed and feature details]
1046         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1047                                        ui.sslVerify->toolTip(),
1048                                        tr("Your Quassel core does not support this feature"),
1049                                        tr("You need a Quassel core v0.13.0 or newer in order to "
1050                                           "verify connection security.")));
1051     }
1052
1053     // read preset networks
1054     QStringList networks = PresetNetworks::names();
1055     foreach(QString s, existing)
1056     networks.removeAll(s);
1057     if (networks.count())
1058         ui.presetList->addItems(networks);
1059     else {
1060         ui.useManual->setChecked(true);
1061         ui.usePreset->setEnabled(false);
1062     }
1063     connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1064     connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1065     connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1066     connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1067     setButtonStates();
1068 }
1069
1070
1071 NetworkInfo NetworkAddDlg::networkInfo() const
1072 {
1073     if (ui.useManual->isChecked()) {
1074         NetworkInfo info;
1075         info.networkName = ui.networkName->text().trimmed();
1076         info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1077                                            ui.serverPassword->text(), ui.useSSL->isChecked(),
1078                                            ui.sslVerify->isChecked());
1079         return info;
1080     }
1081     else
1082         return PresetNetworks::networkInfo(ui.presetList->currentText());
1083 }
1084
1085
1086 void NetworkAddDlg::setButtonStates()
1087 {
1088     bool ok = false;
1089     if (ui.usePreset->isChecked() && ui.presetList->count())
1090         ok = true;
1091     else if (ui.useManual->isChecked()) {
1092         ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1093              && !ui.serverAddress->text().isEmpty();
1094     }
1095     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1096 }
1097
1098
1099 void NetworkAddDlg::updateSslPort(bool isChecked)
1100 {
1101     // "Use encrypted connection" was toggled, check the state...
1102     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1103         // Had been using the plain-text port, use the SSL default
1104         ui.port->setValue(Network::PORT_SSL);
1105     } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1106         // Had been using the SSL port, use the plain-text default
1107         ui.port->setValue(Network::PORT_PLAINTEXT);
1108     }
1109 }
1110
1111
1112 /**************************************************************************
1113  * NetworkEditDlg
1114  *************************************************************************/
1115
1116 NetworkEditDlg::NetworkEditDlg(const QString &old, QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1117 {
1118     ui.setupUi(this);
1119
1120     if (old.isEmpty()) {
1121         // new network
1122         setWindowTitle(tr("Add Network"));
1123         on_networkEdit_textChanged(""); // disable ok button
1124     }
1125     else ui.networkEdit->setText(old);
1126 }
1127
1128
1129 QString NetworkEditDlg::networkName() const
1130 {
1131     return ui.networkEdit->text().trimmed();
1132 }
1133
1134
1135 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1136 {
1137     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1138 }
1139
1140
1141 /**************************************************************************
1142  * ServerEditDlg
1143  *************************************************************************/
1144 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1145 {
1146     ui.setupUi(this);
1147     ui.useSSL->setIcon(icon::get("document-encrypt"));
1148     ui.host->setText(server.host);
1149     ui.host->setFocus();
1150     ui.port->setValue(server.port);
1151     ui.password->setText(server.password);
1152     ui.useSSL->setChecked(server.useSsl);
1153     ui.sslVerify->setChecked(server.sslVerify);
1154     ui.sslVersion->setCurrentIndex(server.sslVersion);
1155     ui.useProxy->setChecked(server.useProxy);
1156     ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1157     ui.proxyHost->setText(server.proxyHost);
1158     ui.proxyPort->setValue(server.proxyPort);
1159     ui.proxyUsername->setText(server.proxyUser);
1160     ui.proxyPassword->setText(server.proxyPass);
1161
1162     // This is a dirty hack to display the core->IRC SSL protocol dropdown
1163     // only if the core won't use autonegotiation to determine the best
1164     // protocol.  When autonegotiation was introduced, it would have been
1165     // a good idea to use the CoreFeatures enum to accomplish this.
1166     // However, since multiple versions have been released since then, that
1167     // is no longer possible.  Instead, we rely on the fact that the
1168     // Datastream protocol was introduced in the same version (0.10) as SSL
1169     // autonegotiation.  Because of that, we can display the dropdown only
1170     // if the Legacy protocol is in use.  If any other RemotePeer protocol
1171     // is in use, that means a newer protocol is in use and therefore the
1172     // core will use autonegotiation.
1173     if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1174         ui.label_3->hide();
1175         ui.sslVersion->hide();
1176     }
1177
1178     // Whenever useSSL is toggled, update the port number if not changed from the default
1179     connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1180     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1181     // If useSSL is later changed to be checked by default, change port's default value, too.
1182
1183     if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1184         // Synchronize requiring SSL with the use SSL checkbox
1185         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1186         connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1187     } else {
1188         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1189         ui.sslVerify->setEnabled(false);
1190         ui.sslVerify->setChecked(false);
1191         // Split up the message to allow re-using translations:
1192         // [Original tool-tip]
1193         // [Bold 'does not support feature' message]
1194         // [Specific version needed and feature details]
1195         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1196                                        ui.sslVerify->toolTip(),
1197                                        tr("Your Quassel core does not support this feature"),
1198                                        tr("You need a Quassel core v0.13.0 or newer in order to "
1199                                           "verify connection security.")));
1200     }
1201
1202     on_host_textChanged();
1203 }
1204
1205
1206 Network::Server ServerEditDlg::serverData() const
1207 {
1208     Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1209                            ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1210     server.sslVersion = ui.sslVersion->currentIndex();
1211     server.useProxy = ui.useProxy->isChecked();
1212     server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1213     server.proxyHost = ui.proxyHost->text();
1214     server.proxyPort = ui.proxyPort->value();
1215     server.proxyUser = ui.proxyUsername->text();
1216     server.proxyPass = ui.proxyPassword->text();
1217     return server;
1218 }
1219
1220
1221 void ServerEditDlg::on_host_textChanged()
1222 {
1223     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1224 }
1225
1226
1227 void ServerEditDlg::updateSslPort(bool isChecked)
1228 {
1229     // "Use encrypted connection" was toggled, check the state...
1230     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1231         // Had been using the plain-text port, use the SSL default
1232         ui.port->setValue(Network::PORT_SSL);
1233     } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1234         // Had been using the SSL port, use the plain-text default
1235         ui.port->setValue(Network::PORT_PLAINTEXT);
1236     }
1237 }
1238
1239
1240 /**************************************************************************
1241  * SaveNetworksDlg
1242  *************************************************************************/
1243
1244 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1245 {
1246     ui.setupUi(this);
1247
1248     numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1249     rcvevents = 0;
1250     if (numevents) {
1251         ui.progressBar->setMaximum(numevents);
1252         ui.progressBar->setValue(0);
1253
1254         connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1255         connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1256
1257         foreach(NetworkId id, toRemove) {
1258             Client::removeNetwork(id);
1259         }
1260         foreach(NetworkInfo info, toCreate) {
1261             Client::createNetwork(info);
1262         }
1263         foreach(NetworkInfo info, toUpdate) {
1264             const Network *net = Client::network(info.networkId);
1265             if (!net) {
1266                 qWarning() << "Invalid client network!";
1267                 numevents--;
1268                 continue;
1269             }
1270             // FIXME this only checks for one changed item rather than all!
1271             connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1272             Client::updateNetwork(info);
1273         }
1274     }
1275     else {
1276         qWarning() << "Sync dialog called without stuff to change!";
1277         accept();
1278     }
1279 }
1280
1281
1282 void SaveNetworksDlg::clientEvent()
1283 {
1284     ui.progressBar->setValue(++rcvevents);
1285     if (rcvevents >= numevents) accept();
1286 }