cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / qtui / settingspages / networkssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "networkssettingspage.h"
22
23 #include <utility>
24
25 #include <QHeaderView>
26 #include <QMessageBox>
27 #include <QTextCodec>
28
29 #include "client.h"
30 #include "icon.h"
31 #include "identity.h"
32 #include "network.h"
33 #include "presetnetworks.h"
34 #include "settingspagedlg.h"
35 #include "util.h"
36 #include "widgethelpers.h"
37
38 // IRCv3 capabilities
39 #include "irccap.h"
40
41 #include "settingspages/identitiessettingspage.h"
42
43 NetworksSettingsPage::NetworksSettingsPage(QWidget* parent)
44     : SettingsPage(tr("IRC"), tr("Networks"), parent)
45 {
46     ui.setupUi(this);
47
48     // hide SASL options for older cores
49     if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
50         ui.sasl->hide();
51     if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
52         ui.saslExtInfo->hide();
53
54     // set up icons
55     ui.renameNetwork->setIcon(icon::get("edit-rename"));
56     ui.addNetwork->setIcon(icon::get("list-add"));
57     ui.deleteNetwork->setIcon(icon::get("edit-delete"));
58     ui.addServer->setIcon(icon::get("list-add"));
59     ui.deleteServer->setIcon(icon::get("edit-delete"));
60     ui.editServer->setIcon(icon::get("configure"));
61     ui.upServer->setIcon(icon::get("go-up"));
62     ui.downServer->setIcon(icon::get("go-down"));
63     ui.editIdentities->setIcon(icon::get("configure"));
64
65     connectedIcon = icon::get("network-connect");
66     connectingIcon = icon::get("network-wired");  // FIXME network-connecting
67     disconnectedIcon = icon::get("network-disconnect");
68
69     // Status icons
70     infoIcon = icon::get({"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, reseting 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             if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
376                 setSASLStatus(CapSupportStatus::MaybeSupported);
377             }
378             else {
379                 setSASLStatus(CapSupportStatus::MaybeUnsupported);
380             }
381
382             // Add additional capability-dependent interface updates here
383         }
384         else {
385             // Network is disconnected
386             // [SASL]
387             setSASLStatus(CapSupportStatus::Disconnected);
388
389             // Add additional capability-dependent interface updates here
390         }
391     }
392     else {
393         // Capability negotiation is not supported and/or network doesn't exist.
394         // Don't assume anything and reset all capability-dependent interface elements to neutral.
395         // [SASL]
396         setSASLStatus(CapSupportStatus::Unknown);
397
398         // Add additional capability-dependent interface updates here
399     }
400 }
401
402 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
403 {
404     this->setEnabled(state);
405     if (state) {
406         load();
407     }
408     else {
409         // reset
410         // currentId = 0;
411     }
412 }
413
414 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
415 {
416     const Identity* identity = Client::identity(id);
417     connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
418
419     QString name = identity->identityName();
420     for (int j = 0; j < ui.identityList->count(); j++) {
421         if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
422             ui.identityList->insertItem(j, name, id.toInt());
423             widgetHasChanged();
424             return;
425         }
426     }
427     // append
428     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
429     widgetHasChanged();
430 }
431
432 void NetworksSettingsPage::clientIdentityUpdated()
433 {
434     const auto* identity = qobject_cast<const Identity*>(sender());
435     if (!identity) {
436         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
437         return;
438     }
439     int row = ui.identityList->findData(identity->id().toInt());
440     if (row < 0) {
441         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
442         return;
443     }
444     if (ui.identityList->itemText(row) != identity->identityName()) {
445         ui.identityList->setItemText(row, identity->identityName());
446     }
447 }
448
449 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
450 {
451     IdentityId defaultId = defaultIdentity();
452     if (currentId != 0)
453         saveToNetworkInfo(networkInfos[currentId]);
454     foreach (NetworkInfo info, networkInfos.values()) {
455         if (info.identity == id) {
456             if (info.networkId == currentId)
457                 ui.identityList->setCurrentIndex(0);
458             info.identity = defaultId;
459             networkInfos[info.networkId] = info;
460             if (info.networkId > 0)
461                 Client::updateNetwork(info);
462         }
463     }
464     ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
465     widgetHasChanged();
466 }
467
468 QListWidgetItem* NetworksSettingsPage::networkItem(NetworkId id) const
469 {
470     for (int i = 0; i < ui.networkList->count(); i++) {
471         QListWidgetItem* item = ui.networkList->item(i);
472         if (item->data(Qt::UserRole).value<NetworkId>() == id)
473             return item;
474     }
475     return nullptr;
476 }
477
478 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
479 {
480     insertNetwork(id);
481     // connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
482     connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
483
484     connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
485     connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
486
487     // Handle capability changes in case a server dis/connects with the settings window open.
488     connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
489     connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
490 }
491
492 void NetworksSettingsPage::clientNetworkUpdated()
493 {
494     const auto* net = qobject_cast<const Network*>(sender());
495     if (!net) {
496         qWarning() << "Update request for unknown network received!";
497         return;
498     }
499     networkInfos[net->networkId()] = net->networkInfo();
500     setItemState(net->networkId());
501     if (net->networkId() == currentId)
502         displayNetwork(net->networkId());
503     setWidgetStates();
504     widgetHasChanged();
505 }
506
507 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
508 {
509     if (!networkInfos.contains(id))
510         return;
511     if (id == currentId)
512         displayNetwork(0);
513     NetworkInfo info = networkInfos.take(id);
514     QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
515     foreach (QListWidgetItem* item, items) {
516         if (item->data(Qt::UserRole).value<NetworkId>() == id)
517             delete ui.networkList->takeItem(ui.networkList->row(item));
518     }
519     setWidgetStates();
520     widgetHasChanged();
521 }
522
523 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
524 {
525     Q_UNUSED(state);
526     const auto* net = qobject_cast<const Network*>(sender());
527     if (!net)
528         return;
529     /*
530     if(net->networkId() == currentId) {
531       ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
532     }
533     */
534     setItemState(net->networkId());
535     if (net->networkId() == currentId) {
536         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
537         // changed.
538         setNetworkCapStates(currentId);
539     }
540     setWidgetStates();
541 }
542
543 void NetworksSettingsPage::networkConnectionError(const QString&) {}
544
545 QListWidgetItem* NetworksSettingsPage::insertNetwork(NetworkId id)
546 {
547     NetworkInfo info = Client::network(id)->networkInfo();
548     networkInfos[id] = info;
549     return insertNetwork(info);
550 }
551
552 QListWidgetItem* NetworksSettingsPage::insertNetwork(const NetworkInfo& info)
553 {
554     QListWidgetItem* item = nullptr;
555     QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
556     if (!items.count())
557         item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
558     else {
559         // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
560         // -> then we can be sure that this is the core-side replacement for the net we created
561         foreach (QListWidgetItem* i, items) {
562             NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
563             if (id < 0) {
564                 item = i;
565                 break;
566             }
567         }
568         if (!item)
569             item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
570     }
571     item->setData(Qt::UserRole, QVariant::fromValue(info.networkId));
572     setItemState(info.networkId, item);
573     widgetHasChanged();
574     return item;
575 }
576
577 // Called when selecting 'Configure' from the buffer list
578 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
579 {
580     QListWidgetItem* item = networkItem(netId);
581     ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
582 }
583
584 void NetworksSettingsPage::displayNetwork(NetworkId id)
585 {
586     _ignoreWidgetChanges = true;
587     if (id != 0) {
588         NetworkInfo info = networkInfos[id];
589
590         // this is only needed when the core supports SASL EXTERNAL
591         if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
592             if (_cid) {
593                 // Clean up existing CertIdentity
594                 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
595                 delete _cid;
596                 _cid = nullptr;
597             }
598             auto *identity = Client::identity(info.identity);
599             if (identity) {
600                 // Connect new CertIdentity
601                 _cid = new CertIdentity(*identity, this);
602                 _cid->enableEditSsl(true);
603                 connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
604             }
605             else {
606                 qWarning() << "NetworksSettingsPage::displayNetwork can't find Identity for IdentityId:" << info.identity;
607             }
608         }
609
610         ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
611         ui.serverList->clear();
612         foreach (Network::Server server, info.serverList) {
613             QListWidgetItem* item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
614             if (server.useSsl)
615                 item->setIcon(icon::get("document-encrypt"));
616             ui.serverList->addItem(item);
617         }
618         // setItemState(id);
619         // ui.randomServer->setChecked(info.useRandomServer);
620         // Update the capability-dependent UI in case capabilities have changed.
621         setNetworkCapStates(id);
622         ui.performEdit->setPlainText(info.perform.join("\n"));
623         ui.autoIdentify->setChecked(info.useAutoIdentify);
624         ui.autoIdentifyService->setText(info.autoIdentifyService);
625         ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
626         ui.sasl->setChecked(info.useSasl);
627         ui.saslAccount->setText(info.saslAccount);
628         ui.saslPassword->setText(info.saslPassword);
629         if (info.codecForEncoding.isEmpty()) {
630             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
631             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
632             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
633             ui.useCustomEncodings->setChecked(false);
634         }
635         else {
636             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
637             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
638             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
639             ui.useCustomEncodings->setChecked(true);
640         }
641         ui.autoReconnect->setChecked(info.useAutoReconnect);
642         ui.reconnectInterval->setValue(info.autoReconnectInterval);
643         ui.reconnectRetries->setValue(info.autoReconnectRetries);
644         ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
645         ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
646         // Custom rate limiting
647         ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
648         // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
649         // disabled, 'ui.messageRateDelayFrame' will remain disabled.
650         ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
651         ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
652         // Convert milliseconds (integer) into seconds (double)
653         ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
654         // Skipped IRCv3 capabilities
655         ui.enableCapServerTime->setChecked(!info.skipCaps.contains(IrcCap::SERVER_TIME));
656     }
657     else {
658         // just clear widgets
659         if (_cid) {
660             disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
661             delete _cid;
662         }
663         ui.identityList->setCurrentIndex(-1);
664         ui.serverList->clear();
665         ui.performEdit->clear();
666         ui.autoIdentifyService->clear();
667         ui.autoIdentifyPassword->clear();
668         ui.saslAccount->clear();
669         ui.saslPassword->clear();
670         setWidgetStates();
671     }
672     _ignoreWidgetChanges = false;
673     currentId = id;
674 }
675
676 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo& info)
677 {
678     info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
679     // info.useRandomServer = ui.randomServer->isChecked();
680     info.perform = ui.performEdit->toPlainText().split("\n");
681     info.useAutoIdentify = ui.autoIdentify->isChecked();
682     info.autoIdentifyService = ui.autoIdentifyService->text();
683     info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
684     info.useSasl = ui.sasl->isChecked();
685     info.saslAccount = ui.saslAccount->text();
686     info.saslPassword = ui.saslPassword->text();
687     if (!ui.useCustomEncodings->isChecked()) {
688         info.codecForEncoding.clear();
689         info.codecForDecoding.clear();
690         info.codecForServer.clear();
691     }
692     else {
693         info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
694         info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
695         info.codecForServer = ui.serverEncoding->currentText().toLatin1();
696     }
697     info.useAutoReconnect = ui.autoReconnect->isChecked();
698     info.autoReconnectInterval = ui.reconnectInterval->value();
699     info.autoReconnectRetries = ui.reconnectRetries->value();
700     info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
701     info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
702     // Custom rate limiting
703     info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
704     info.messageRateBurstSize = ui.messageRateBurstSize->value();
705     // Convert seconds (double) into milliseconds (integer)
706     info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
707     info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
708     // Skipped IRCv3 capabilities
709     if (ui.enableCapServerTime->isChecked()) {
710         // Capability enabled, remove it from the skip list
711         info.skipCaps.removeAll(IrcCap::SERVER_TIME);
712     } else if (!info.skipCaps.contains(IrcCap::SERVER_TIME)) {
713         // Capability disabled and not in the skip list, add it
714         info.skipCaps.append(IrcCap::SERVER_TIME);
715     }
716 }
717
718 void NetworksSettingsPage::clientNetworkCapsUpdated()
719 {
720     // Grab the updated network
721     const auto* net = qobject_cast<const Network*>(sender());
722     if (!net) {
723         qWarning() << "Update request for unknown network received!";
724         return;
725     }
726     if (net->networkId() == currentId) {
727         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
728         // changed.
729         setNetworkCapStates(currentId);
730     }
731 }
732
733 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
734 {
735     if (_saslStatusSelected != saslStatus) {
736         // Update the cached copy of SASL status used with the Details dialog
737         _saslStatusSelected = saslStatus;
738
739         // Update the user interface
740         switch (saslStatus) {
741         case CapSupportStatus::Unknown:
742             // There's no capability negotiation or network doesn't exist.  Don't assume
743             // anything.
744             ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Could not check if supported by network")));
745             ui.saslStatusIcon->setPixmap(questionIcon.pixmap(16));
746             break;
747         case CapSupportStatus::Disconnected:
748             // Disconnected from network, no way to check.
749             ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Cannot check if supported when disconnected")));
750             ui.saslStatusIcon->setPixmap(questionIcon.pixmap(16));
751             break;
752         case CapSupportStatus::MaybeUnsupported:
753             // The network doesn't advertise support for SASL PLAIN.  Here be dragons.
754             ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Not currently supported by network")));
755             ui.saslStatusIcon->setPixmap(unavailableIcon.pixmap(16));
756             break;
757         case CapSupportStatus::MaybeSupported:
758             // The network advertises support for SASL PLAIN.  Encourage using it!
759             // Unfortunately we don't know for sure if it's desired or functional.
760             ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
761             ui.saslStatusIcon->setPixmap(successIcon.pixmap(16));
762             break;
763         }
764     }
765 }
766
767 void NetworksSettingsPage::sslUpdated()
768 {
769     if (_cid && !_cid->sslKey().isNull()) {
770         ui.saslContents->setDisabled(true);
771         ui.saslExtInfo->setHidden(false);
772     }
773     else {
774         ui.saslContents->setDisabled(false);
775         // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
776         // state to indicate whether or not it's disabled.  To workaround this, keep track of
777         // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
778         // group box.
779         if (!ui.sasl->isChecked()) {
780             // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
781             // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
782             // sasl is later checked.
783             ui.sasl->setChecked(true);
784             ui.sasl->setChecked(false);
785         }
786         ui.saslExtInfo->setHidden(true);
787     }
788 }
789
790 /*** Network list ***/
791
792 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
793 {
794     if (currentId != 0) {
795         saveToNetworkInfo(networkInfos[currentId]);
796     }
797     if (ui.networkList->selectedItems().count()) {
798         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
799         currentId = id;
800         displayNetwork(id);
801         ui.serverList->setCurrentRow(0);
802     }
803     else {
804         currentId = 0;
805     }
806     setWidgetStates();
807 }
808
809 void NetworksSettingsPage::on_addNetwork_clicked()
810 {
811     QStringList existing;
812     for (int i = 0; i < ui.networkList->count(); i++)
813         existing << ui.networkList->item(i)->text();
814     NetworkAddDlg dlg(existing, this);
815     if (dlg.exec() == QDialog::Accepted) {
816         NetworkInfo info = dlg.networkInfo();
817         if (info.networkName.isEmpty())
818             return;  // sanity check
819
820         NetworkId id;
821         for (id = 1; id <= networkInfos.count(); id++) {
822             widgetHasChanged();
823             if (!networkInfos.keys().contains(-id.toInt()))
824                 break;
825         }
826         id = -id.toInt();
827         info.networkId = id;
828         info.identity = defaultIdentity();
829         networkInfos[id] = info;
830         QListWidgetItem* item = insertNetwork(info);
831         ui.networkList->setCurrentItem(item);
832         setWidgetStates();
833     }
834 }
835
836 void NetworksSettingsPage::on_deleteNetwork_clicked()
837 {
838     if (ui.networkList->selectedItems().count()) {
839         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
840         int ret
841             = QMessageBox::question(this,
842                                     tr("Delete Network?"),
843                                     tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?")
844                                         .arg(networkInfos[netid].networkName),
845                                     QMessageBox::Yes | QMessageBox::No,
846                                     QMessageBox::No);
847         if (ret == QMessageBox::Yes) {
848             currentId = 0;
849             networkInfos.remove(netid);
850             delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
851             ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow() + 1, ui.networkList->count() - 1));
852             setWidgetStates();
853             widgetHasChanged();
854         }
855     }
856 }
857
858 void NetworksSettingsPage::on_renameNetwork_clicked()
859 {
860     if (!ui.networkList->selectedItems().count())
861         return;
862     QString old = ui.networkList->selectedItems()[0]->text();
863     QStringList existing;
864     for (int i = 0; i < ui.networkList->count(); i++)
865         existing << ui.networkList->item(i)->text();
866     NetworkEditDlg dlg(old, existing, this);
867     if (dlg.exec() == QDialog::Accepted) {
868         ui.networkList->selectedItems()[0]->setText(dlg.networkName());
869         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
870         networkInfos[netid].networkName = dlg.networkName();
871         widgetHasChanged();
872     }
873 }
874
875 /*
876 void NetworksSettingsPage::on_connectNow_clicked() {
877   if(!ui.networkList->selectedItems().count()) return;
878   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
879   const Network *net = Client::network(id);
880   if(!net) return;
881   if(net->connectionState() == Network::Disconnected) net->requestConnect();
882   else net->requestDisconnect();
883 }
884 */
885
886 /*** Server list ***/
887
888 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
889 {
890     setWidgetStates();
891 }
892
893 void NetworksSettingsPage::on_addServer_clicked()
894 {
895     if (currentId == 0)
896         return;
897     ServerEditDlg dlg(Network::Server(), this);
898     if (dlg.exec() == QDialog::Accepted) {
899         networkInfos[currentId].serverList.append(dlg.serverData());
900         displayNetwork(currentId);
901         ui.serverList->setCurrentRow(ui.serverList->count() - 1);
902         widgetHasChanged();
903     }
904 }
905
906 void NetworksSettingsPage::on_editServer_clicked()
907 {
908     if (currentId == 0)
909         return;
910     int cur = ui.serverList->currentRow();
911     ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
912     if (dlg.exec() == QDialog::Accepted) {
913         networkInfos[currentId].serverList[cur] = dlg.serverData();
914         displayNetwork(currentId);
915         ui.serverList->setCurrentRow(cur);
916         widgetHasChanged();
917     }
918 }
919
920 void NetworksSettingsPage::on_deleteServer_clicked()
921 {
922     if (currentId == 0)
923         return;
924     int cur = ui.serverList->currentRow();
925     networkInfos[currentId].serverList.removeAt(cur);
926     displayNetwork(currentId);
927     ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count() - 1));
928     widgetHasChanged();
929 }
930
931 void NetworksSettingsPage::on_upServer_clicked()
932 {
933     int cur = ui.serverList->currentRow();
934     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
935     networkInfos[currentId].serverList.insert(cur - 1, server);
936     displayNetwork(currentId);
937     ui.serverList->setCurrentRow(cur - 1);
938     widgetHasChanged();
939 }
940
941 void NetworksSettingsPage::on_downServer_clicked()
942 {
943     int cur = ui.serverList->currentRow();
944     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
945     networkInfos[currentId].serverList.insert(cur + 1, server);
946     displayNetwork(currentId);
947     ui.serverList->setCurrentRow(cur + 1);
948     widgetHasChanged();
949 }
950
951 void NetworksSettingsPage::on_editIdentities_clicked()
952 {
953     SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
954     dlg.exec();
955 }
956
957 void NetworksSettingsPage::on_saslStatusDetails_clicked()
958 {
959     if (ui.networkList->selectedItems().count()) {
960         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
961         QString& netName = networkInfos[netid].networkName;
962
963         // If these strings are visible, one of the status messages wasn't detected below.
964         QString saslStatusHeader = "[header unintentionally left blank]";
965         QString saslStatusExplanation = "[explanation unintentionally left blank]";
966
967         // If true, show a warning icon instead of an information icon
968         bool useWarningIcon = false;
969
970         // Determine which explanation to show
971         switch (_saslStatusSelected) {
972         case CapSupportStatus::Unknown:
973             saslStatusHeader = tr("Could not check if SASL supported by network");
974             saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL.  This may "
975                                        "be due to unsaved changes or an older Quassel core.  You "
976                                        "can still try using SASL.")
977                                         .arg(netName);
978             break;
979         case CapSupportStatus::Disconnected:
980             saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
981             saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
982                                        "disconnected.  Connect to the network, or try using SASL "
983                                        "anyways.")
984                                         .arg(netName);
985             break;
986         case CapSupportStatus::MaybeUnsupported:
987             saslStatusHeader = tr("SASL not currently supported by network");
988             saslStatusExplanation = tr("The network \"%1\" does not currently support SASL.  "
989                                        "However, support might be added later on.")
990                                         .arg(netName);
991             useWarningIcon = true;
992             break;
993         case CapSupportStatus::MaybeSupported:
994             saslStatusHeader = tr("SASL supported by network");
995             saslStatusExplanation = tr("The network \"%1\" supports SASL.  In most cases, you "
996                                        "should use SASL instead of NickServ identification.")
997                                         .arg(netName);
998             break;
999         }
1000
1001         // Process this in advance for reusability below
1002         const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
1003         const QString saslStatusMsgText = QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>")
1004                                               .arg(saslStatusHeader,
1005                                                    saslStatusExplanation,
1006                                                    tr("SASL is a standardized way to log in and identify yourself to "
1007                                                       "IRC servers."));
1008
1009         if (useWarningIcon) {
1010             // Show as a warning dialog box
1011             QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1012         }
1013         else {
1014             // Show as an information dialog box
1015             QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1016         }
1017     }
1018 }
1019
1020 void NetworksSettingsPage::on_enableCapsStatusDetails_clicked()
1021 {
1022     if (!Client::isConnected() || Client::isCoreFeatureEnabled(Quassel::Feature::SkipIrcCaps)) {
1023         // Either disconnected or IRCv3 capability skippping supported
1024
1025         // Try to get a list of currently enabled features
1026         QStringList sortedCapsEnabled;
1027         // Check if a network is selected
1028         if (ui.networkList->selectedItems().count()) {
1029             // Get the underlying Network from the selected network
1030             NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
1031             const Network* net = Client::network(netid);
1032             if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
1033                 // Capability negotiation is supported, network exists.
1034                 // If the network is disconnected, the list of enabled capabilities will be empty,
1035                 // no need to check for that specifically.
1036                 // Sorting isn't required, but it looks nicer.
1037                 sortedCapsEnabled = net->capsEnabled();
1038                 sortedCapsEnabled.sort();
1039             }
1040         }
1041
1042         // Try to explain IRCv3 network features in a friendly way, including showing the currently
1043         // enabled features if available
1044         auto messageText = QString("<p>%1</p></br><p>%2</p>")
1045                 .arg(tr("Quassel makes use of newer IRC features when supported by the IRC network."
1046                         "  If desired, you can disable unwanted or problematic features here."),
1047                      tr("The <a href=\"https://ircv3.net/irc/\">IRCv3 website</a> provides more "
1048                         "technical details on the IRCv3 capabilities powering these features."));
1049
1050         if (!sortedCapsEnabled.isEmpty()) {
1051             // Format the capabilities within <code></code> blocks
1052             auto formattedCaps = QString("<code>%1</code>")
1053                     .arg(sortedCapsEnabled.join("</code>, <code>"));
1054
1055             // Add the currently enabled capabilities to the list
1056             // This creates a new QString, but this code is not performance-critical.
1057             messageText = messageText.append(QString("<p><i>%1</i></p>").arg(
1058                                                  tr("Currently enabled IRCv3 capabilities for this "
1059                                                     "network: %1").arg(formattedCaps)));
1060         }
1061
1062         QMessageBox::information(this, tr("Configuring network features"), messageText);
1063     }
1064     else {
1065         // Core does not IRCv3 capability skipping, show warning
1066         QMessageBox::warning(this, tr("Configuring network features unsupported"),
1067                              QString("<p><b>%1</b></p></br><p>%2</p>")
1068                              .arg(tr("Your Quassel core is too old to configure IRCv3 network features"),
1069                                   tr("You need a Quassel core v0.14.0 or newer to control what network "
1070                                      "features Quassel will use.")));
1071     }
1072 }
1073
1074 void NetworksSettingsPage::on_enableCapsAdvanced_clicked()
1075 {
1076     if (currentId == 0)
1077         return;
1078
1079     CapsEditDlg dlg(networkInfos[currentId].skipCapsToString(), this);
1080     if (dlg.exec() == QDialog::Accepted) {
1081         networkInfos[currentId].skipCapsFromString(dlg.skipCapsString());
1082         displayNetwork(currentId);
1083         widgetHasChanged();
1084     }
1085 }
1086
1087 IdentityId NetworksSettingsPage::defaultIdentity() const
1088 {
1089     IdentityId defaultId = 0;
1090     QList<IdentityId> ids = Client::identityIds();
1091     foreach (IdentityId id, ids) {
1092         if (defaultId == 0 || id < defaultId)
1093             defaultId = id;
1094     }
1095     return defaultId;
1096 }
1097
1098 /**************************************************************************
1099  * NetworkAddDlg
1100  *************************************************************************/
1101
1102 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget* parent)
1103     : QDialog(parent)
1104     , existing(std::move(exist))
1105 {
1106     ui.setupUi(this);
1107     ui.useSSL->setIcon(icon::get("document-encrypt"));
1108
1109     // Whenever useSSL is toggled, update the port number if not changed from the default
1110     connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1111     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1112     // If useSSL is later changed to be checked by default, change port's default value, too.
1113
1114     if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1115         // Synchronize requiring SSL with the use SSL checkbox
1116         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1117         connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1118     }
1119     else {
1120         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1121         ui.sslVerify->setEnabled(false);
1122         ui.sslVerify->setChecked(false);
1123         // Split up the message to allow re-using translations:
1124         // [Original tool-tip]
1125         // [Bold 'does not support feature' message]
1126         // [Specific version needed and feature details]
1127         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1128                                      .arg(ui.sslVerify->toolTip(),
1129                                           tr("Your Quassel core does not support this feature"),
1130                                           tr("You need a Quassel core v0.13.0 or newer in order to "
1131                                              "verify connection security.")));
1132     }
1133
1134     // read preset networks
1135     QStringList networks = PresetNetworks::names();
1136     foreach (QString s, existing)
1137         networks.removeAll(s);
1138     if (networks.count())
1139         ui.presetList->addItems(networks);
1140     else {
1141         ui.useManual->setChecked(true);
1142         ui.usePreset->setEnabled(false);
1143     }
1144     connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1145     connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1146     connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1147     connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1148     setButtonStates();
1149 }
1150
1151 NetworkInfo NetworkAddDlg::networkInfo() const
1152 {
1153     if (ui.useManual->isChecked()) {
1154         NetworkInfo info;
1155         info.networkName = ui.networkName->text().trimmed();
1156         info.serverList << Network::Server(ui.serverAddress->text().trimmed(),
1157                                            ui.port->value(),
1158                                            ui.serverPassword->text(),
1159                                            ui.useSSL->isChecked(),
1160                                            ui.sslVerify->isChecked());
1161         return info;
1162     }
1163     else
1164         return PresetNetworks::networkInfo(ui.presetList->currentText());
1165 }
1166
1167 void NetworkAddDlg::setButtonStates()
1168 {
1169     bool ok = false;
1170     if (ui.usePreset->isChecked() && ui.presetList->count())
1171         ok = true;
1172     else if (ui.useManual->isChecked()) {
1173         ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1174              && !ui.serverAddress->text().isEmpty();
1175     }
1176     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1177 }
1178
1179 void NetworkAddDlg::updateSslPort(bool isChecked)
1180 {
1181     // "Use encrypted connection" was toggled, check the state...
1182     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1183         // Had been using the plain-text port, use the SSL default
1184         ui.port->setValue(Network::PORT_SSL);
1185     }
1186     else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1187         // Had been using the SSL port, use the plain-text default
1188         ui.port->setValue(Network::PORT_PLAINTEXT);
1189     }
1190 }
1191
1192 /**************************************************************************
1193  * NetworkEditDlg
1194  *************************************************************************/
1195
1196 NetworkEditDlg::NetworkEditDlg(const QString& old, QStringList exist, QWidget* parent)
1197     : QDialog(parent)
1198     , existing(std::move(exist))
1199 {
1200     ui.setupUi(this);
1201
1202     if (old.isEmpty()) {
1203         // new network
1204         setWindowTitle(tr("Add Network"));
1205         on_networkEdit_textChanged("");  // disable ok button
1206     }
1207     else
1208         ui.networkEdit->setText(old);
1209 }
1210
1211 QString NetworkEditDlg::networkName() const
1212 {
1213     return ui.networkEdit->text().trimmed();
1214 }
1215
1216 void NetworkEditDlg::on_networkEdit_textChanged(const QString& text)
1217 {
1218     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1219 }
1220
1221 /**************************************************************************
1222  * ServerEditDlg
1223  *************************************************************************/
1224 ServerEditDlg::ServerEditDlg(const Network::Server& server, QWidget* parent)
1225     : QDialog(parent)
1226 {
1227     ui.setupUi(this);
1228     ui.useSSL->setIcon(icon::get("document-encrypt"));
1229     ui.host->setText(server.host);
1230     ui.host->setFocus();
1231     ui.port->setValue(server.port);
1232     ui.password->setText(server.password);
1233     ui.useSSL->setChecked(server.useSsl);
1234     ui.sslVerify->setChecked(server.sslVerify);
1235     ui.sslVersion->setCurrentIndex(server.sslVersion);
1236     ui.useProxy->setChecked(server.useProxy);
1237     ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1238     ui.proxyHost->setText(server.proxyHost);
1239     ui.proxyPort->setValue(server.proxyPort);
1240     ui.proxyUsername->setText(server.proxyUser);
1241     ui.proxyPassword->setText(server.proxyPass);
1242
1243     // This is a dirty hack to display the core->IRC SSL protocol dropdown
1244     // only if the core won't use autonegotiation to determine the best
1245     // protocol.  When autonegotiation was introduced, it would have been
1246     // a good idea to use the CoreFeatures enum to accomplish this.
1247     // However, since multiple versions have been released since then, that
1248     // is no longer possible.  Instead, we rely on the fact that the
1249     // Datastream protocol was introduced in the same version (0.10) as SSL
1250     // autonegotiation.  Because of that, we can display the dropdown only
1251     // if the Legacy protocol is in use.  If any other RemotePeer protocol
1252     // is in use, that means a newer protocol is in use and therefore the
1253     // core will use autonegotiation.
1254     if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1255         ui.label_3->hide();
1256         ui.sslVersion->hide();
1257     }
1258
1259     // Whenever useSSL is toggled, update the port number if not changed from the default
1260     connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1261     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1262     // If useSSL is later changed to be checked by default, change port's default value, too.
1263
1264     if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1265         // Synchronize requiring SSL with the use SSL checkbox
1266         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1267         connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1268     }
1269     else {
1270         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1271         ui.sslVerify->setEnabled(false);
1272         ui.sslVerify->setChecked(false);
1273         // Split up the message to allow re-using translations:
1274         // [Original tool-tip]
1275         // [Bold 'does not support feature' message]
1276         // [Specific version needed and feature details]
1277         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1278                                      .arg(ui.sslVerify->toolTip(),
1279                                           tr("Your Quassel core does not support this feature"),
1280                                           tr("You need a Quassel core v0.13.0 or newer in order to "
1281                                              "verify connection security.")));
1282     }
1283
1284     on_host_textChanged();
1285 }
1286
1287 Network::Server ServerEditDlg::serverData() const
1288 {
1289     Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1290     server.sslVersion = ui.sslVersion->currentIndex();
1291     server.useProxy = ui.useProxy->isChecked();
1292     server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1293     server.proxyHost = ui.proxyHost->text();
1294     server.proxyPort = ui.proxyPort->value();
1295     server.proxyUser = ui.proxyUsername->text();
1296     server.proxyPass = ui.proxyPassword->text();
1297     return server;
1298 }
1299
1300 void ServerEditDlg::on_host_textChanged()
1301 {
1302     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1303 }
1304
1305 void ServerEditDlg::updateSslPort(bool isChecked)
1306 {
1307     // "Use encrypted connection" was toggled, check the state...
1308     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1309         // Had been using the plain-text port, use the SSL default
1310         ui.port->setValue(Network::PORT_SSL);
1311     }
1312     else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1313         // Had been using the SSL port, use the plain-text default
1314         ui.port->setValue(Network::PORT_PLAINTEXT);
1315     }
1316 }
1317
1318 /**************************************************************************
1319  * CapsEditDlg
1320  *************************************************************************/
1321
1322 CapsEditDlg::CapsEditDlg(const QString& oldSkipCapsString, QWidget* parent)
1323     : QDialog(parent)
1324     , oldSkipCapsString(oldSkipCapsString)
1325 {
1326     ui.setupUi(this);
1327
1328     // Connect to the reset button to reset the text
1329     // This provides an explicit way to "get back to defaults" in case someone changes settings to
1330     // experiment
1331     QPushButton* defaultsButton = ui.buttonBox->button(QDialogButtonBox::RestoreDefaults);
1332     connect(defaultsButton, &QPushButton::clicked, this, &CapsEditDlg::defaultSkipCaps);
1333
1334     if (oldSkipCapsString.isEmpty()) {
1335         // Disable Reset button
1336         on_skipCapsEdit_textChanged("");
1337     }
1338     else {
1339         ui.skipCapsEdit->setText(oldSkipCapsString);
1340     }
1341 }
1342
1343
1344 QString CapsEditDlg::skipCapsString() const
1345 {
1346     return ui.skipCapsEdit->text();
1347 }
1348
1349 void CapsEditDlg::defaultSkipCaps()
1350 {
1351     ui.skipCapsEdit->setText("");
1352 }
1353
1354 void CapsEditDlg::on_skipCapsEdit_textChanged(const QString& text)
1355 {
1356     ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setDisabled(text.isEmpty());
1357 }
1358
1359 /**************************************************************************
1360  * SaveNetworksDlg
1361  *************************************************************************/
1362
1363 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo>& toCreate,
1364                                  const QList<NetworkInfo>& toUpdate,
1365                                  const QList<NetworkId>& toRemove,
1366                                  QWidget* parent)
1367     : QDialog(parent)
1368 {
1369     ui.setupUi(this);
1370
1371     numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1372     rcvevents = 0;
1373     if (numevents) {
1374         ui.progressBar->setMaximum(numevents);
1375         ui.progressBar->setValue(0);
1376
1377         connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1378         connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1379
1380         foreach (NetworkId id, toRemove) {
1381             Client::removeNetwork(id);
1382         }
1383         foreach (NetworkInfo info, toCreate) {
1384             Client::createNetwork(info);
1385         }
1386         foreach (NetworkInfo info, toUpdate) {
1387             const Network* net = Client::network(info.networkId);
1388             if (!net) {
1389                 qWarning() << "Invalid client network!";
1390                 numevents--;
1391                 continue;
1392             }
1393             // FIXME this only checks for one changed item rather than all!
1394             connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1395             Client::updateNetwork(info);
1396         }
1397     }
1398     else {
1399         qWarning() << "Sync dialog called without stuff to change!";
1400         accept();
1401     }
1402 }
1403
1404 void SaveNetworksDlg::clientEvent()
1405 {
1406     ui.progressBar->setValue(++rcvevents);
1407     if (rcvevents >= numevents)
1408         accept();
1409 }