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