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