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