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