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