settings: Reset Network SASL status on load
[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
25 #include "networkssettingspage.h"
26
27 #include "client.h"
28 #include "icon.h"
29 #include "identity.h"
30 #include "network.h"
31 #include "presetnetworks.h"
32 #include "settingspagedlg.h"
33 #include "util.h"
34
35 // IRCv3 capabilities
36 #include "irccap.h"
37
38 #include "settingspages/identitiessettingspage.h"
39
40 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
41     : SettingsPage(tr("IRC"), tr("Networks"), parent)
42 #ifdef HAVE_SSL
43       , _cid(0)
44 #endif
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     _ignoreWidgetChanges = false;
69
70     connectedIcon = icon::get("network-connect");
71     connectingIcon = icon::get("network-wired"); // FIXME network-connecting
72     disconnectedIcon = icon::get("network-disconnect");
73
74     // Status icons
75     infoIcon = icon::get("dialog-information");
76     warningIcon = icon::get("dialog-warning");
77
78     foreach(int mib, QTextCodec::availableMibs()) {
79         QByteArray codec = QTextCodec::codecForMib(mib)->name();
80         ui.sendEncoding->addItem(codec);
81         ui.recvEncoding->addItem(codec);
82         ui.serverEncoding->addItem(codec);
83     }
84     ui.sendEncoding->model()->sort(0);
85     ui.recvEncoding->model()->sort(0);
86     ui.serverEncoding->model()->sort(0);
87     currentId = 0;
88     setEnabled(Client::isConnected()); // need a core connection!
89     setWidgetStates();
90     connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
91     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
92     connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
93     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
94     connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
95
96     connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
97     //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98     connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
99     connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
100     connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
101     connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
102     connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
103     connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
104     connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
105     connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
106     connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
107     connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
108     connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
109     connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
110     connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
111     connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
112     connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
113     connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
114
115     // Core features can change during a reconnect.  Always connect these here, delaying testing for
116     // the core feature flag in load().
117     connect(ui.useCustomMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
118     connect(ui.messageRateBurstSize, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
119     connect(ui.messageRateDelay, SIGNAL(valueChanged(double)), this, SLOT(widgetHasChanged()));
120     connect(ui.unlimitedMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
121
122     // Add additional widgets here
123     //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
124     //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
125
126     foreach(IdentityId id, Client::identityIds()) {
127         clientIdentityAdded(id);
128     }
129 }
130
131
132 void NetworksSettingsPage::save()
133 {
134     setEnabled(false);
135     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
136
137     QList<NetworkInfo> toCreate, toUpdate;
138     QList<NetworkId> toRemove;
139     QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
140     while (i != networkInfos.end()) {
141         NetworkId id = (*i).networkId;
142         if (id < 0) {
143             toCreate.append(*i);
144             //if(id == currentId) currentId = 0;
145             //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
146             //if(items.count()) {
147             //  Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
148             //  delete items[0];
149             //}
150             //i = networkInfos.erase(i);
151             ++i;
152         }
153         else {
154             if ((*i) != Client::network((*i).networkId)->networkInfo()) {
155                 toUpdate.append(*i);
156             }
157             ++i;
158         }
159     }
160     foreach(NetworkId id, Client::networkIds()) {
161         if (!networkInfos.contains(id)) toRemove.append(id);
162     }
163     SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
164     int ret = dlg.exec();
165     if (ret == QDialog::Rejected) {
166         // canceled -> reload everything to be safe
167         load();
168     }
169     setChangedState(false);
170     setEnabled(true);
171 }
172
173
174 void NetworksSettingsPage::load()
175 {
176     reset();
177
178     // Handle UI dependent on core feature flags here
179     if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
180         // Custom rate limiting supported, allow toggling
181         ui.useCustomMessageRate->setEnabled(true);
182         // Reset tooltip to default.
183         ui.useCustomMessageRate->setToolTip(QString("%1").arg(
184                                           tr("<p>Override default message rate limiting.</p>"
185                                              "<p><b>Setting limits too low may get you disconnected"
186                                              " from the server!</b></p>")));
187         // If changed, update the message below!
188     } else {
189         // Custom rate limiting not supported, disallow toggling
190         ui.useCustomMessageRate->setEnabled(false);
191         // Split up the message to allow re-using translations:
192         // [Original tool-tip]
193         // [Bold 'does not support feature' message]
194         // [Specific version needed and feature details]
195         ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
196                                           tr("<p>Override default message rate limiting.</p>"
197                                              "<p><b>Setting limits too low may get you disconnected"
198                                              " from the server!</b></p>"),
199                                           tr("Your Quassel core does not support this feature"),
200                                           tr("You need a Quassel core v0.13.0 or newer in order to "
201                                           "modify message rate limits.")));
202     }
203
204 #ifdef HAVE_SSL
205     // Hide the SASL EXTERNAL notice until a network's shown.  Stops it from showing while loading
206     // backlog from the core.
207     sslUpdated();
208 #endif
209
210     // Reset network capability status in case no valid networks get selected (a rare situation)
211     resetNetworkCapStates();
212
213     foreach(NetworkId netid, Client::networkIds()) {
214         clientNetworkAdded(netid);
215     }
216     ui.networkList->setCurrentRow(0);
217
218     setChangedState(false);
219 }
220
221
222 void NetworksSettingsPage::reset()
223 {
224     currentId = 0;
225     ui.networkList->clear();
226     networkInfos.clear();
227 }
228
229
230 bool NetworksSettingsPage::aboutToSave()
231 {
232     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
233     QList<int> errors;
234     foreach(NetworkInfo info, networkInfos.values()) {
235         if (!info.serverList.count()) errors.append(1);
236     }
237     if (!errors.count()) return true;
238     QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
239     if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
240     error += tr("</ul>");
241     QMessageBox::warning(this, tr("Invalid Network Settings"), error);
242     return false;
243 }
244
245
246 void NetworksSettingsPage::widgetHasChanged()
247 {
248     if (_ignoreWidgetChanges) return;
249     bool changed = testHasChanged();
250     if (changed != hasChanged()) setChangedState(changed);
251 }
252
253
254 bool NetworksSettingsPage::testHasChanged()
255 {
256     if (currentId != 0) {
257         saveToNetworkInfo(networkInfos[currentId]);
258     }
259     if (Client::networkIds().count() != networkInfos.count()) return true;
260     foreach(NetworkId id, networkInfos.keys()) {
261         if (id < 0) return true;
262         if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
263     }
264     return false;
265 }
266
267
268 void NetworksSettingsPage::setWidgetStates()
269 {
270     // network list
271     if (ui.networkList->selectedItems().count()) {
272         ui.detailsBox->setEnabled(true);
273         ui.renameNetwork->setEnabled(true);
274         ui.deleteNetwork->setEnabled(true);
275
276         /* button disabled for now
277         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
278         const Network *net = id > 0 ? Client::network(id) : 0;
279         ui.connectNow->setEnabled(net);
280         //    && (Client::network(id)->connectionState() == Network::Initialized
281         //    || Client::network(id)->connectionState() == Network::Disconnected));
282         if(net) {
283           if(net->connectionState() == Network::Disconnected) {
284             ui.connectNow->setIcon(connectedIcon);
285             ui.connectNow->setText(tr("Connect"));
286           } else {
287             ui.connectNow->setIcon(disconnectedIcon);
288             ui.connectNow->setText(tr("Disconnect"));
289           }
290         } else {
291           ui.connectNow->setIcon(QIcon());
292           ui.connectNow->setText(tr("Apply first!"));
293         } */
294     }
295     else {
296         ui.renameNetwork->setEnabled(false);
297         ui.deleteNetwork->setEnabled(false);
298         //ui.connectNow->setEnabled(false);
299         ui.detailsBox->setEnabled(false);
300     }
301     // network details
302     if (ui.serverList->selectedItems().count()) {
303         ui.editServer->setEnabled(true);
304         ui.deleteServer->setEnabled(true);
305         ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
306         ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
307     }
308     else {
309         ui.editServer->setEnabled(false);
310         ui.deleteServer->setEnabled(false);
311         ui.upServer->setEnabled(false);
312         ui.downServer->setEnabled(false);
313     }
314 }
315
316
317 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
318 {
319     if (!item && !(item = networkItem(id))) return;
320     const Network *net = Client::network(id);
321     if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
322     else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
323     if (net && net->connectionState() == Network::Initialized) {
324         item->setIcon(connectedIcon);
325     }
326     else if (net && net->connectionState() != Network::Disconnected) {
327         item->setIcon(connectingIcon);
328     }
329     else {
330         item->setIcon(disconnectedIcon);
331     }
332     if (net) {
333         bool select = false;
334         // check if we already have another net of this name in the list, and replace it
335         QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
336         if (items.count()) {
337             foreach(QListWidgetItem *i, items) {
338                 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
339                 if (oldid > 0) continue;  // only locally created nets should be replaced
340                 if (oldid == currentId) {
341                     select = true;
342                     currentId = 0;
343                     ui.networkList->clearSelection();
344                 }
345                 int row = ui.networkList->row(i);
346                 if (row >= 0) {
347                     QListWidgetItem *olditem = ui.networkList->takeItem(row);
348                     Q_ASSERT(olditem);
349                     delete olditem;
350                 }
351                 networkInfos.remove(oldid);
352                 break;
353             }
354         }
355         item->setText(net->networkName());
356         if (select) item->setSelected(true);
357     }
358 }
359
360
361 void NetworksSettingsPage::resetNetworkCapStates()
362 {
363     // Set the status to a blank (invalid) network ID, reseting all UI
364     setNetworkCapStates(NetworkId());
365 }
366
367
368 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
369 {
370     const Network *net = Client::network(id);
371     if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
372         // Capability negotiation is supported, network exists.
373         // Check if the network is connected.  Don't use net->isConnected() as that won't be true
374         // during capability negotiation when capabilities are added and removed.
375         if (net->connectionState() != Network::Disconnected) {
376             // Network exists and is connected, check available capabilities...
377             // [SASL]
378             if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
379                 setSASLStatus(CapSupportStatus::MaybeSupported);
380             } else {
381                 setSASLStatus(CapSupportStatus::MaybeUnsupported);
382             }
383
384             // Add additional capability-dependent interface updates here
385         } else {
386             // Network is disconnected
387             // [SASL]
388             setSASLStatus(CapSupportStatus::Disconnected);
389
390             // Add additional capability-dependent interface updates here
391         }
392     } else {
393         // Capability negotiation is not supported and/or network doesn't exist.
394         // Don't assume anything and reset all capability-dependent interface elements to neutral.
395         // [SASL]
396         setSASLStatus(CapSupportStatus::Unknown);
397
398         // Add additional capability-dependent interface updates here
399     }
400 }
401
402
403 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
404 {
405     this->setEnabled(state);
406     if (state) {
407         load();
408     }
409     else {
410         // reset
411         //currentId = 0;
412     }
413 }
414
415
416 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
417 {
418     const Identity *identity = Client::identity(id);
419     connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
420
421     QString name = identity->identityName();
422     for (int j = 0; j < ui.identityList->count(); j++) {
423         if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
424             ui.identityList->insertItem(j, name, id.toInt());
425             widgetHasChanged();
426             return;
427         }
428     }
429     // append
430     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
431     widgetHasChanged();
432 }
433
434
435 void NetworksSettingsPage::clientIdentityUpdated()
436 {
437     const Identity *identity = qobject_cast<const Identity *>(sender());
438     if (!identity) {
439         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
440         return;
441     }
442     int row = ui.identityList->findData(identity->id().toInt());
443     if (row < 0) {
444         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
445         return;
446     }
447     if (ui.identityList->itemText(row) != identity->identityName()) {
448         ui.identityList->setItemText(row, identity->identityName());
449     }
450 }
451
452
453 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
454 {
455     IdentityId defaultId = defaultIdentity();
456     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
457     foreach(NetworkInfo info, networkInfos.values()) {
458         if (info.identity == id) {
459             if (info.networkId == currentId)
460                 ui.identityList->setCurrentIndex(0);
461             info.identity = defaultId;
462             networkInfos[info.networkId] = info;
463             if (info.networkId > 0) Client::updateNetwork(info);
464         }
465     }
466     ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
467     widgetHasChanged();
468 }
469
470
471 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
472 {
473     for (int i = 0; i < ui.networkList->count(); i++) {
474         QListWidgetItem *item = ui.networkList->item(i);
475         if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
476     }
477     return 0;
478 }
479
480
481 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
482 {
483     insertNetwork(id);
484     //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
485     connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
486
487     connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
488     connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
489
490     // Handle capability changes in case a server dis/connects with the settings window open.
491     connect(Client::network(id), SIGNAL(capAdded(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
492     connect(Client::network(id), SIGNAL(capRemoved(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
493 }
494
495
496 void NetworksSettingsPage::clientNetworkUpdated()
497 {
498     const Network *net = qobject_cast<const Network *>(sender());
499     if (!net) {
500         qWarning() << "Update request for unknown network received!";
501         return;
502     }
503     networkInfos[net->networkId()] = net->networkInfo();
504     setItemState(net->networkId());
505     if (net->networkId() == currentId) displayNetwork(net->networkId());
506     setWidgetStates();
507     widgetHasChanged();
508 }
509
510
511 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
512 {
513     if (!networkInfos.contains(id)) return;
514     if (id == currentId) displayNetwork(0);
515     NetworkInfo info = networkInfos.take(id);
516     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
517     foreach(QListWidgetItem *item, items) {
518         if (item->data(Qt::UserRole).value<NetworkId>() == id)
519             delete ui.networkList->takeItem(ui.networkList->row(item));
520     }
521     setWidgetStates();
522     widgetHasChanged();
523 }
524
525
526 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
527 {
528     Q_UNUSED(state);
529     const Network *net = qobject_cast<const Network *>(sender());
530     if (!net) return;
531     /*
532     if(net->networkId() == currentId) {
533       ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
534     }
535     */
536     setItemState(net->networkId());
537     if (net->networkId() == currentId) {
538         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
539         // changed.
540         setNetworkCapStates(currentId);
541     }
542     setWidgetStates();
543 }
544
545
546 void NetworksSettingsPage::networkConnectionError(const QString &)
547 {
548 }
549
550
551 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
552 {
553     NetworkInfo info = Client::network(id)->networkInfo();
554     networkInfos[id] = info;
555     return insertNetwork(info);
556 }
557
558
559 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
560 {
561     QListWidgetItem *item = 0;
562     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
563     if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
564     else {
565         // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
566         // -> then we can be sure that this is the core-side replacement for the net we created
567         foreach(QListWidgetItem *i, items) {
568             NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
569             if (id < 0) { item = i; break; }
570         }
571         if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
572     }
573     item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
574     setItemState(info.networkId, item);
575     widgetHasChanged();
576     return item;
577 }
578
579
580 // Called when selecting 'Configure' from the buffer list
581 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
582 {
583     QListWidgetItem *item = networkItem(netId);
584     ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
585 }
586
587
588 void NetworksSettingsPage::displayNetwork(NetworkId id)
589 {
590     _ignoreWidgetChanges = true;
591     if (id != 0) {
592         NetworkInfo info = networkInfos[id];
593
594 #ifdef HAVE_SSL
595         // this is only needed when the core supports SASL EXTERNAL
596         if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
597             if (_cid) {
598                 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
599                 delete _cid;
600             }
601             _cid = new CertIdentity(*Client::identity(info.identity), this);
602             _cid->enableEditSsl(true);
603             connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
604         }
605 #endif
606
607         ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
608         ui.serverList->clear();
609         foreach(Network::Server server, info.serverList) {
610             QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
611             if (server.useSsl)
612                 item->setIcon(icon::get("document-encrypt"));
613             ui.serverList->addItem(item);
614         }
615         //setItemState(id);
616         //ui.randomServer->setChecked(info.useRandomServer);
617         // Update the capability-dependent UI in case capabilities have changed.
618         setNetworkCapStates(id);
619         ui.performEdit->setPlainText(info.perform.join("\n"));
620         ui.autoIdentify->setChecked(info.useAutoIdentify);
621         ui.autoIdentifyService->setText(info.autoIdentifyService);
622         ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
623         ui.sasl->setChecked(info.useSasl);
624         ui.saslAccount->setText(info.saslAccount);
625         ui.saslPassword->setText(info.saslPassword);
626         if (info.codecForEncoding.isEmpty()) {
627             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
628             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
629             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
630             ui.useCustomEncodings->setChecked(false);
631         }
632         else {
633             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
634             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
635             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
636             ui.useCustomEncodings->setChecked(true);
637         }
638         ui.autoReconnect->setChecked(info.useAutoReconnect);
639         ui.reconnectInterval->setValue(info.autoReconnectInterval);
640         ui.reconnectRetries->setValue(info.autoReconnectRetries);
641         ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
642         ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
643         // Custom rate limiting
644         ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
645         // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
646         // disabled, 'ui.messageRateDelayFrame' will remain disabled.
647         ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
648         ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
649         // Convert milliseconds (integer) into seconds (double)
650         ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
651     }
652     else {
653         // just clear widgets
654 #ifdef HAVE_SSL
655         if (_cid) {
656             disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
657             delete _cid;
658         }
659 #endif
660         ui.identityList->setCurrentIndex(-1);
661         ui.serverList->clear();
662         ui.performEdit->clear();
663         ui.autoIdentifyService->clear();
664         ui.autoIdentifyPassword->clear();
665         ui.saslAccount->clear();
666         ui.saslPassword->clear();
667         setWidgetStates();
668     }
669     _ignoreWidgetChanges = false;
670     currentId = id;
671 }
672
673
674 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
675 {
676     info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
677     //info.useRandomServer = ui.randomServer->isChecked();
678     info.perform = ui.performEdit->toPlainText().split("\n");
679     info.useAutoIdentify = ui.autoIdentify->isChecked();
680     info.autoIdentifyService = ui.autoIdentifyService->text();
681     info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
682     info.useSasl = ui.sasl->isChecked();
683     info.saslAccount = ui.saslAccount->text();
684     info.saslPassword = ui.saslPassword->text();
685     if (!ui.useCustomEncodings->isChecked()) {
686         info.codecForEncoding.clear();
687         info.codecForDecoding.clear();
688         info.codecForServer.clear();
689     }
690     else {
691         info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
692         info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
693         info.codecForServer = ui.serverEncoding->currentText().toLatin1();
694     }
695     info.useAutoReconnect = ui.autoReconnect->isChecked();
696     info.autoReconnectInterval = ui.reconnectInterval->value();
697     info.autoReconnectRetries = ui.reconnectRetries->value();
698     info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
699     info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
700     // Custom rate limiting
701     info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
702     info.messageRateBurstSize = ui.messageRateBurstSize->value();
703     // Convert seconds (double) into milliseconds (integer)
704     info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
705     info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
706 }
707
708
709 void NetworksSettingsPage::clientNetworkCapsUpdated()
710 {
711     // Grab the updated network
712     const Network *net = qobject_cast<const Network *>(sender());
713     if (!net) {
714         qWarning() << "Update request for unknown network received!";
715         return;
716     }
717     if (net->networkId() == currentId) {
718         // Network is currently shown.  Update the capability-dependent UI in case capabilities have
719         // changed.
720         setNetworkCapStates(currentId);
721     }
722 }
723
724
725 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
726 {
727     if (_saslStatusSelected != saslStatus) {
728         // Update the cached copy of SASL status used with the Details dialog
729         _saslStatusSelected = saslStatus;
730
731         // Update the user interface
732         switch (saslStatus) {
733             case CapSupportStatus::Unknown:
734                 // There's no capability negotiation or network doesn't exist.  Don't assume
735                 // anything.
736                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
737                                             tr("Could not check if supported by network")));
738                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
739                 break;
740             case CapSupportStatus::Disconnected:
741                 // Disconnected from network, no way to check.
742                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
743                                                 tr("Cannot check if supported when disconnected")));
744                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
745                 break;
746             case CapSupportStatus::MaybeUnsupported:
747                 // The network doesn't advertise support for SASL PLAIN.  Here be dragons.
748                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
749                                                 tr("Not currently supported by network")));
750                 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
751                 break;
752             case CapSupportStatus::MaybeSupported:
753                 // The network advertises support for SASL PLAIN.  Encourage using it!
754                 // Unfortunately we don't know for sure if it's desired or functional.
755                 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
756                 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
757                 break;
758         }
759     }
760 }
761
762
763 #ifdef HAVE_SSL
764 void NetworksSettingsPage::sslUpdated()
765 {
766     if (_cid && !_cid->sslKey().isNull()) {
767         ui.saslContents->setDisabled(true);
768         ui.saslExtInfo->setHidden(false);
769     } else {
770         ui.saslContents->setDisabled(false);
771         // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
772         // state to indicate whether or not it's disabled.  To workaround this, keep track of
773         // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
774         // group box.
775         if (!ui.sasl->isChecked()) {
776             // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
777             // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
778             // sasl is later checked.
779             ui.sasl->setChecked(true);
780             ui.sasl->setChecked(false);
781         }
782         ui.saslExtInfo->setHidden(true);
783     }
784 }
785 #endif
786
787
788 /*** Network list ***/
789
790 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
791 {
792     if (currentId != 0) {
793         saveToNetworkInfo(networkInfos[currentId]);
794     }
795     if (ui.networkList->selectedItems().count()) {
796         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
797         currentId = id;
798         displayNetwork(id);
799         ui.serverList->setCurrentRow(0);
800     }
801     else {
802         currentId = 0;
803     }
804     setWidgetStates();
805 }
806
807
808 void NetworksSettingsPage::on_addNetwork_clicked()
809 {
810     QStringList existing;
811     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
812     NetworkAddDlg dlg(existing, this);
813     if (dlg.exec() == QDialog::Accepted) {
814         NetworkInfo info = dlg.networkInfo();
815         if (info.networkName.isEmpty())
816             return;  // sanity check
817
818         NetworkId id;
819         for (id = 1; id <= networkInfos.count(); id++) {
820             widgetHasChanged();
821             if (!networkInfos.keys().contains(-id.toInt())) break;
822         }
823         id = -id.toInt();
824         info.networkId = id;
825         info.identity = defaultIdentity();
826         networkInfos[id] = info;
827         QListWidgetItem *item = insertNetwork(info);
828         ui.networkList->setCurrentItem(item);
829         setWidgetStates();
830     }
831 }
832
833
834 void NetworksSettingsPage::on_deleteNetwork_clicked()
835 {
836     if (ui.networkList->selectedItems().count()) {
837         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
838         int ret = QMessageBox::question(this, tr("Delete Network?"),
839             tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
840             QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
841         if (ret == QMessageBox::Yes) {
842             currentId = 0;
843             networkInfos.remove(netid);
844             delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
845             ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
846             setWidgetStates();
847             widgetHasChanged();
848         }
849     }
850 }
851
852
853 void NetworksSettingsPage::on_renameNetwork_clicked()
854 {
855     if (!ui.networkList->selectedItems().count()) return;
856     QString old = ui.networkList->selectedItems()[0]->text();
857     QStringList existing;
858     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
859     NetworkEditDlg dlg(old, existing, this);
860     if (dlg.exec() == QDialog::Accepted) {
861         ui.networkList->selectedItems()[0]->setText(dlg.networkName());
862         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
863         networkInfos[netid].networkName = dlg.networkName();
864         widgetHasChanged();
865     }
866 }
867
868
869 /*
870 void NetworksSettingsPage::on_connectNow_clicked() {
871   if(!ui.networkList->selectedItems().count()) return;
872   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
873   const Network *net = Client::network(id);
874   if(!net) return;
875   if(net->connectionState() == Network::Disconnected) net->requestConnect();
876   else net->requestDisconnect();
877 }
878 */
879
880 /*** Server list ***/
881
882 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
883 {
884     setWidgetStates();
885 }
886
887
888 void NetworksSettingsPage::on_addServer_clicked()
889 {
890     if (currentId == 0) return;
891     ServerEditDlg dlg(Network::Server(), this);
892     if (dlg.exec() == QDialog::Accepted) {
893         networkInfos[currentId].serverList.append(dlg.serverData());
894         displayNetwork(currentId);
895         ui.serverList->setCurrentRow(ui.serverList->count()-1);
896         widgetHasChanged();
897     }
898 }
899
900
901 void NetworksSettingsPage::on_editServer_clicked()
902 {
903     if (currentId == 0) return;
904     int cur = ui.serverList->currentRow();
905     ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
906     if (dlg.exec() == QDialog::Accepted) {
907         networkInfos[currentId].serverList[cur] = dlg.serverData();
908         displayNetwork(currentId);
909         ui.serverList->setCurrentRow(cur);
910         widgetHasChanged();
911     }
912 }
913
914
915 void NetworksSettingsPage::on_deleteServer_clicked()
916 {
917     if (currentId == 0) return;
918     int cur = ui.serverList->currentRow();
919     networkInfos[currentId].serverList.removeAt(cur);
920     displayNetwork(currentId);
921     ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
922     widgetHasChanged();
923 }
924
925
926 void NetworksSettingsPage::on_upServer_clicked()
927 {
928     int cur = ui.serverList->currentRow();
929     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
930     networkInfos[currentId].serverList.insert(cur-1, server);
931     displayNetwork(currentId);
932     ui.serverList->setCurrentRow(cur-1);
933     widgetHasChanged();
934 }
935
936
937 void NetworksSettingsPage::on_downServer_clicked()
938 {
939     int cur = ui.serverList->currentRow();
940     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
941     networkInfos[currentId].serverList.insert(cur+1, server);
942     displayNetwork(currentId);
943     ui.serverList->setCurrentRow(cur+1);
944     widgetHasChanged();
945 }
946
947
948 void NetworksSettingsPage::on_editIdentities_clicked()
949 {
950     SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
951     dlg.exec();
952 }
953
954
955 void NetworksSettingsPage::on_saslStatusDetails_clicked()
956 {
957     if (ui.networkList->selectedItems().count()) {
958         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
959         QString &netName = networkInfos[netid].networkName;
960
961         // If these strings are visible, one of the status messages wasn't detected below.
962         QString saslStatusHeader = "[header unintentionally left blank]";
963         QString saslStatusExplanation = "[explanation unintentionally left blank]";
964
965         // If true, show a warning icon instead of an information icon
966         bool useWarningIcon = false;
967
968         // Determine which explanation to show
969         switch (_saslStatusSelected) {
970         case CapSupportStatus::Unknown:
971             saslStatusHeader = tr("Could not check if SASL supported by network");
972             saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL.  This may "
973                                        "be due to unsaved changes or an older Quassel core.  You "
974                                        "can still try using SASL.").arg(netName);
975             break;
976         case CapSupportStatus::Disconnected:
977             saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
978             saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
979                                        "disconnected.  Connect to the network, or try using SASL "
980                                        "anyways.").arg(netName);
981             break;
982         case CapSupportStatus::MaybeUnsupported:
983             saslStatusHeader = tr("SASL not currently supported by network");
984             saslStatusExplanation = tr("The network \"%1\" does not currently support SASL.  "
985                                        "However, support might be added later on.").arg(netName);
986             useWarningIcon = true;
987             break;
988         case CapSupportStatus::MaybeSupported:
989             saslStatusHeader = tr("SASL supported by network");
990             saslStatusExplanation = tr("The network \"%1\" supports SASL.  In most cases, you "
991                                        "should use SASL instead of NickServ identification."
992                                        ).arg(netName);
993             break;
994         }
995
996         // Process this in advance for reusability below
997         const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
998         const QString saslStatusMsgText =
999                 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
1000                         ).arg(saslStatusHeader,
1001                               saslStatusExplanation,
1002                               tr("SASL is a standardized way to log in and identify yourself to "
1003                                  "IRC servers."));
1004
1005         if (useWarningIcon) {
1006             // Show as a warning dialog box
1007             QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1008         } else {
1009             // Show as an information dialog box
1010             QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1011         }
1012     }
1013 }
1014
1015
1016 IdentityId NetworksSettingsPage::defaultIdentity() const
1017 {
1018     IdentityId defaultId = 0;
1019     QList<IdentityId> ids = Client::identityIds();
1020     foreach(IdentityId id, ids) {
1021         if (defaultId == 0 || id < defaultId)
1022             defaultId = id;
1023     }
1024     return defaultId;
1025 }
1026
1027
1028 /**************************************************************************
1029 * NetworkAddDlg
1030 *************************************************************************/
1031
1032 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
1033 {
1034     ui.setupUi(this);
1035     ui.useSSL->setIcon(icon::get("document-encrypt"));
1036
1037     // Whenever useSSL is toggled, update the port number if not changed from the default
1038     connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1039     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1040     // If useSSL is later changed to be checked by default, change port's default value, too.
1041
1042     if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1043         // Synchronize requiring SSL with the use SSL checkbox
1044         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1045         connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1046     } else {
1047         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1048         ui.sslVerify->setEnabled(false);
1049         ui.sslVerify->setChecked(false);
1050         // Split up the message to allow re-using translations:
1051         // [Original tool-tip]
1052         // [Bold 'does not support feature' message]
1053         // [Specific version needed and feature details]
1054         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1055                                        ui.sslVerify->toolTip(),
1056                                        tr("Your Quassel core does not support this feature"),
1057                                        tr("You need a Quassel core v0.13.0 or newer in order to "
1058                                           "verify connection security.")));
1059     }
1060
1061     // read preset networks
1062     QStringList networks = PresetNetworks::names();
1063     foreach(QString s, existing)
1064     networks.removeAll(s);
1065     if (networks.count())
1066         ui.presetList->addItems(networks);
1067     else {
1068         ui.useManual->setChecked(true);
1069         ui.usePreset->setEnabled(false);
1070     }
1071     connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1072     connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), 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, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(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 }