1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "networkssettingspage.h"
25 #include <QHeaderView>
26 #include <QMessageBox>
33 #include "presetnetworks.h"
34 #include "settingspagedlg.h"
36 #include "widgethelpers.h"
41 #include "settingspages/identitiessettingspage.h"
43 NetworksSettingsPage::NetworksSettingsPage(QWidget* parent)
44 : SettingsPage(tr("IRC"), tr("Networks"), parent)
48 // hide SASL options for older cores
49 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
51 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
52 ui.saslExtInfo->hide();
54 ui.saslExtInfo->hide();
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"));
68 connectedIcon = icon::get("network-connect");
69 connectingIcon = icon::get("network-wired"); // FIXME network-connecting
70 disconnectedIcon = icon::get("network-disconnect");
73 infoIcon = icon::get("dialog-information");
74 warningIcon = icon::get("dialog-warning");
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);
82 ui.sendEncoding->model()->sort(0);
83 ui.recvEncoding->model()->sort(0);
84 ui.serverEncoding->model()->sort(0);
86 setEnabled(Client::isConnected()); // need a core connection!
89 connectToWidgetsChangedSignals({ui.identityList, ui.performEdit, ui.sasl,
90 ui.saslAccount, ui.saslPassword, ui.autoIdentify,
91 ui.autoIdentifyService, ui.autoIdentifyPassword, ui.useCustomEncodings,
92 ui.sendEncoding, ui.recvEncoding, ui.serverEncoding,
93 ui.autoReconnect, ui.reconnectInterval, ui.reconnectRetries,
94 ui.unlimitedRetries, ui.rejoinOnReconnect, ui.useCustomMessageRate,
95 ui.messageRateBurstSize, ui.messageRateDelay, ui.unlimitedMessageRate},
97 &NetworksSettingsPage::widgetHasChanged);
99 connect(Client::instance(), &Client::coreConnectionStateChanged, this, &NetworksSettingsPage::coreConnectionStateChanged);
100 connect(Client::instance(), &Client::networkCreated, this, &NetworksSettingsPage::clientNetworkAdded);
101 connect(Client::instance(), &Client::networkRemoved, this, &NetworksSettingsPage::clientNetworkRemoved);
102 connect(Client::instance(), &Client::identityCreated, this, &NetworksSettingsPage::clientIdentityAdded);
103 connect(Client::instance(), &Client::identityRemoved, this, &NetworksSettingsPage::clientIdentityRemoved);
105 foreach (IdentityId id, Client::identityIds()) {
106 clientIdentityAdded(id);
110 void NetworksSettingsPage::save()
114 saveToNetworkInfo(networkInfos[currentId]);
116 QList<NetworkInfo> toCreate, toUpdate;
117 QList<NetworkId> toRemove;
118 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
119 while (i != networkInfos.end()) {
120 NetworkId id = (*i).networkId;
123 // if(id == currentId) currentId = 0;
124 // QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
125 // if(items.count()) {
126 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
129 // i = networkInfos.erase(i);
133 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
139 foreach (NetworkId id, Client::networkIds()) {
140 if (!networkInfos.contains(id))
143 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
144 int ret = dlg.exec();
145 if (ret == QDialog::Rejected) {
146 // canceled -> reload everything to be safe
149 setChangedState(false);
153 void NetworksSettingsPage::load()
157 // Handle UI dependent on core feature flags here
158 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
159 // Custom rate limiting supported, allow toggling
160 ui.useCustomMessageRate->setEnabled(true);
161 // Reset tooltip to default.
162 ui.useCustomMessageRate->setToolTip(QString("%1").arg(tr("<p>Override default message rate limiting.</p>"
163 "<p><b>Setting limits too low may get you disconnected"
164 " from the server!</b></p>")));
165 // If changed, update the message below!
168 // Custom rate limiting not supported, disallow toggling
169 ui.useCustomMessageRate->setEnabled(false);
170 // Split up the message to allow re-using translations:
171 // [Original tool-tip]
172 // [Bold 'does not support feature' message]
173 // [Specific version needed and feature details]
174 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
175 .arg(tr("<p>Override default message rate limiting.</p>"
176 "<p><b>Setting limits too low may get you disconnected"
177 " from the server!</b></p>"),
178 tr("Your Quassel core does not support this feature"),
179 tr("You need a Quassel core v0.13.0 or newer in order to "
180 "modify message rate limits.")));
184 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
185 // backlog from the core.
189 // Reset network capability status in case no valid networks get selected (a rare situation)
190 resetNetworkCapStates();
192 foreach (NetworkId netid, Client::networkIds()) {
193 clientNetworkAdded(netid);
195 ui.networkList->setCurrentRow(0);
197 setChangedState(false);
200 void NetworksSettingsPage::reset()
203 ui.networkList->clear();
204 networkInfos.clear();
207 bool NetworksSettingsPage::aboutToSave()
210 saveToNetworkInfo(networkInfos[currentId]);
212 foreach (NetworkInfo info, networkInfos.values()) {
213 if (!info.serverList.count())
218 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
219 if (errors.contains(1))
220 error += tr("<li>All networks need at least one server defined</li>");
221 error += tr("</ul>");
222 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
226 void NetworksSettingsPage::widgetHasChanged()
228 if (_ignoreWidgetChanges)
230 bool changed = testHasChanged();
231 if (changed != hasChanged())
232 setChangedState(changed);
235 bool NetworksSettingsPage::testHasChanged()
237 if (currentId != 0) {
238 saveToNetworkInfo(networkInfos[currentId]);
240 if (Client::networkIds().count() != networkInfos.count())
242 foreach (NetworkId id, networkInfos.keys()) {
245 if (Client::network(id)->networkInfo() != networkInfos[id])
251 void NetworksSettingsPage::setWidgetStates()
254 if (ui.networkList->selectedItems().count()) {
255 ui.detailsBox->setEnabled(true);
256 ui.renameNetwork->setEnabled(true);
257 ui.deleteNetwork->setEnabled(true);
259 /* button disabled for now
260 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
261 const Network *net = id > 0 ? Client::network(id) : 0;
262 ui.connectNow->setEnabled(net);
263 // && (Client::network(id)->connectionState() == Network::Initialized
264 // || Client::network(id)->connectionState() == Network::Disconnected));
266 if(net->connectionState() == Network::Disconnected) {
267 ui.connectNow->setIcon(connectedIcon);
268 ui.connectNow->setText(tr("Connect"));
270 ui.connectNow->setIcon(disconnectedIcon);
271 ui.connectNow->setText(tr("Disconnect"));
274 ui.connectNow->setIcon(QIcon());
275 ui.connectNow->setText(tr("Apply first!"));
279 ui.renameNetwork->setEnabled(false);
280 ui.deleteNetwork->setEnabled(false);
281 // ui.connectNow->setEnabled(false);
282 ui.detailsBox->setEnabled(false);
285 if (ui.serverList->selectedItems().count()) {
286 ui.editServer->setEnabled(true);
287 ui.deleteServer->setEnabled(true);
288 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
289 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
292 ui.editServer->setEnabled(false);
293 ui.deleteServer->setEnabled(false);
294 ui.upServer->setEnabled(false);
295 ui.downServer->setEnabled(false);
299 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem* item)
301 if (!item && !(item = networkItem(id)))
303 const Network* net = Client::network(id);
304 if (!net || net->isInitialized())
305 item->setFlags(item->flags() | Qt::ItemIsEnabled);
307 item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
308 if (net && net->connectionState() == Network::Initialized) {
309 item->setIcon(connectedIcon);
311 else if (net && net->connectionState() != Network::Disconnected) {
312 item->setIcon(connectingIcon);
315 item->setIcon(disconnectedIcon);
319 // check if we already have another net of this name in the list, and replace it
320 QList<QListWidgetItem*> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
322 foreach (QListWidgetItem* i, items) {
323 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
325 continue; // only locally created nets should be replaced
326 if (oldid == currentId) {
329 ui.networkList->clearSelection();
331 int row = ui.networkList->row(i);
333 QListWidgetItem* olditem = ui.networkList->takeItem(row);
337 networkInfos.remove(oldid);
341 item->setText(net->networkName());
343 item->setSelected(true);
347 void NetworksSettingsPage::resetNetworkCapStates()
349 // Set the status to a blank (invalid) network ID, reseting all UI
350 setNetworkCapStates(NetworkId());
353 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
355 const Network* net = Client::network(id);
356 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
357 // Capability negotiation is supported, network exists.
358 // Check if the network is connected. Don't use net->isConnected() as that won't be true
359 // during capability negotiation when capabilities are added and removed.
360 if (net->connectionState() != Network::Disconnected) {
361 // Network exists and is connected, check available capabilities...
363 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
364 setSASLStatus(CapSupportStatus::MaybeSupported);
367 setSASLStatus(CapSupportStatus::MaybeUnsupported);
370 // Add additional capability-dependent interface updates here
373 // Network is disconnected
375 setSASLStatus(CapSupportStatus::Disconnected);
377 // Add additional capability-dependent interface updates here
381 // Capability negotiation is not supported and/or network doesn't exist.
382 // Don't assume anything and reset all capability-dependent interface elements to neutral.
384 setSASLStatus(CapSupportStatus::Unknown);
386 // Add additional capability-dependent interface updates here
390 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
392 this->setEnabled(state);
402 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
404 const Identity* identity = Client::identity(id);
405 connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
407 QString name = identity->identityName();
408 for (int j = 0; j < ui.identityList->count(); j++) {
409 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
410 ui.identityList->insertItem(j, name, id.toInt());
416 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
420 void NetworksSettingsPage::clientIdentityUpdated()
422 const auto* identity = qobject_cast<const Identity*>(sender());
424 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
427 int row = ui.identityList->findData(identity->id().toInt());
429 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
432 if (ui.identityList->itemText(row) != identity->identityName()) {
433 ui.identityList->setItemText(row, identity->identityName());
437 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
439 IdentityId defaultId = defaultIdentity();
441 saveToNetworkInfo(networkInfos[currentId]);
442 foreach (NetworkInfo info, networkInfos.values()) {
443 if (info.identity == id) {
444 if (info.networkId == currentId)
445 ui.identityList->setCurrentIndex(0);
446 info.identity = defaultId;
447 networkInfos[info.networkId] = info;
448 if (info.networkId > 0)
449 Client::updateNetwork(info);
452 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
456 QListWidgetItem* NetworksSettingsPage::networkItem(NetworkId id) const
458 for (int i = 0; i < ui.networkList->count(); i++) {
459 QListWidgetItem* item = ui.networkList->item(i);
460 if (item->data(Qt::UserRole).value<NetworkId>() == id)
466 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
469 // connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
470 connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
472 connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
473 connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
475 // Handle capability changes in case a server dis/connects with the settings window open.
476 connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
477 connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
480 void NetworksSettingsPage::clientNetworkUpdated()
482 const auto* net = qobject_cast<const Network*>(sender());
484 qWarning() << "Update request for unknown network received!";
487 networkInfos[net->networkId()] = net->networkInfo();
488 setItemState(net->networkId());
489 if (net->networkId() == currentId)
490 displayNetwork(net->networkId());
495 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
497 if (!networkInfos.contains(id))
501 NetworkInfo info = networkInfos.take(id);
502 QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
503 foreach (QListWidgetItem* item, items) {
504 if (item->data(Qt::UserRole).value<NetworkId>() == id)
505 delete ui.networkList->takeItem(ui.networkList->row(item));
511 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
514 const auto* net = qobject_cast<const Network*>(sender());
518 if(net->networkId() == currentId) {
519 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
522 setItemState(net->networkId());
523 if (net->networkId() == currentId) {
524 // Network is currently shown. Update the capability-dependent UI in case capabilities have
526 setNetworkCapStates(currentId);
531 void NetworksSettingsPage::networkConnectionError(const QString&) {}
533 QListWidgetItem* NetworksSettingsPage::insertNetwork(NetworkId id)
535 NetworkInfo info = Client::network(id)->networkInfo();
536 networkInfos[id] = info;
537 return insertNetwork(info);
540 QListWidgetItem* NetworksSettingsPage::insertNetwork(const NetworkInfo& info)
542 QListWidgetItem* item = nullptr;
543 QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
545 item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
547 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
548 // -> then we can be sure that this is the core-side replacement for the net we created
549 foreach (QListWidgetItem* i, items) {
550 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
557 item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
559 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
560 setItemState(info.networkId, item);
565 // Called when selecting 'Configure' from the buffer list
566 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
568 QListWidgetItem* item = networkItem(netId);
569 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
572 void NetworksSettingsPage::displayNetwork(NetworkId id)
574 _ignoreWidgetChanges = true;
576 NetworkInfo info = networkInfos[id];
579 // this is only needed when the core supports SASL EXTERNAL
580 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
582 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
585 _cid = new CertIdentity(*Client::identity(info.identity), this);
586 _cid->enableEditSsl(true);
587 connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
591 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
592 ui.serverList->clear();
593 foreach (Network::Server server, info.serverList) {
594 QListWidgetItem* item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
596 item->setIcon(icon::get("document-encrypt"));
597 ui.serverList->addItem(item);
600 // ui.randomServer->setChecked(info.useRandomServer);
601 // Update the capability-dependent UI in case capabilities have changed.
602 setNetworkCapStates(id);
603 ui.performEdit->setPlainText(info.perform.join("\n"));
604 ui.autoIdentify->setChecked(info.useAutoIdentify);
605 ui.autoIdentifyService->setText(info.autoIdentifyService);
606 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
607 ui.sasl->setChecked(info.useSasl);
608 ui.saslAccount->setText(info.saslAccount);
609 ui.saslPassword->setText(info.saslPassword);
610 if (info.codecForEncoding.isEmpty()) {
611 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
612 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
613 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
614 ui.useCustomEncodings->setChecked(false);
617 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
618 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
619 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
620 ui.useCustomEncodings->setChecked(true);
622 ui.autoReconnect->setChecked(info.useAutoReconnect);
623 ui.reconnectInterval->setValue(info.autoReconnectInterval);
624 ui.reconnectRetries->setValue(info.autoReconnectRetries);
625 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
626 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
627 // Custom rate limiting
628 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
629 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
630 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
631 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
632 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
633 // Convert milliseconds (integer) into seconds (double)
634 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
637 // just clear widgets
640 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
644 ui.identityList->setCurrentIndex(-1);
645 ui.serverList->clear();
646 ui.performEdit->clear();
647 ui.autoIdentifyService->clear();
648 ui.autoIdentifyPassword->clear();
649 ui.saslAccount->clear();
650 ui.saslPassword->clear();
653 _ignoreWidgetChanges = false;
657 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo& info)
659 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
660 // info.useRandomServer = ui.randomServer->isChecked();
661 info.perform = ui.performEdit->toPlainText().split("\n");
662 info.useAutoIdentify = ui.autoIdentify->isChecked();
663 info.autoIdentifyService = ui.autoIdentifyService->text();
664 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
665 info.useSasl = ui.sasl->isChecked();
666 info.saslAccount = ui.saslAccount->text();
667 info.saslPassword = ui.saslPassword->text();
668 if (!ui.useCustomEncodings->isChecked()) {
669 info.codecForEncoding.clear();
670 info.codecForDecoding.clear();
671 info.codecForServer.clear();
674 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
675 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
676 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
678 info.useAutoReconnect = ui.autoReconnect->isChecked();
679 info.autoReconnectInterval = ui.reconnectInterval->value();
680 info.autoReconnectRetries = ui.reconnectRetries->value();
681 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
682 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
683 // Custom rate limiting
684 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
685 info.messageRateBurstSize = ui.messageRateBurstSize->value();
686 // Convert seconds (double) into milliseconds (integer)
687 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
688 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
691 void NetworksSettingsPage::clientNetworkCapsUpdated()
693 // Grab the updated network
694 const auto* net = qobject_cast<const Network*>(sender());
696 qWarning() << "Update request for unknown network received!";
699 if (net->networkId() == currentId) {
700 // Network is currently shown. Update the capability-dependent UI in case capabilities have
702 setNetworkCapStates(currentId);
706 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
708 if (_saslStatusSelected != saslStatus) {
709 // Update the cached copy of SASL status used with the Details dialog
710 _saslStatusSelected = saslStatus;
712 // Update the user interface
713 switch (saslStatus) {
714 case CapSupportStatus::Unknown:
715 // There's no capability negotiation or network doesn't exist. Don't assume
717 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Could not check if supported by network")));
718 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
720 case CapSupportStatus::Disconnected:
721 // Disconnected from network, no way to check.
722 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Cannot check if supported when disconnected")));
723 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
725 case CapSupportStatus::MaybeUnsupported:
726 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
727 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Not currently supported by network")));
728 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
730 case CapSupportStatus::MaybeSupported:
731 // The network advertises support for SASL PLAIN. Encourage using it!
732 // Unfortunately we don't know for sure if it's desired or functional.
733 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
734 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
741 void NetworksSettingsPage::sslUpdated()
743 if (_cid && !_cid->sslKey().isNull()) {
744 ui.saslContents->setDisabled(true);
745 ui.saslExtInfo->setHidden(false);
748 ui.saslContents->setDisabled(false);
749 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
750 // state to indicate whether or not it's disabled. To workaround this, keep track of
751 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
753 if (!ui.sasl->isChecked()) {
754 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
755 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
756 // sasl is later checked.
757 ui.sasl->setChecked(true);
758 ui.sasl->setChecked(false);
760 ui.saslExtInfo->setHidden(true);
765 /*** Network list ***/
767 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
769 if (currentId != 0) {
770 saveToNetworkInfo(networkInfos[currentId]);
772 if (ui.networkList->selectedItems().count()) {
773 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
776 ui.serverList->setCurrentRow(0);
784 void NetworksSettingsPage::on_addNetwork_clicked()
786 QStringList existing;
787 for (int i = 0; i < ui.networkList->count(); i++)
788 existing << ui.networkList->item(i)->text();
789 NetworkAddDlg dlg(existing, this);
790 if (dlg.exec() == QDialog::Accepted) {
791 NetworkInfo info = dlg.networkInfo();
792 if (info.networkName.isEmpty())
793 return; // sanity check
796 for (id = 1; id <= networkInfos.count(); id++) {
798 if (!networkInfos.keys().contains(-id.toInt()))
803 info.identity = defaultIdentity();
804 networkInfos[id] = info;
805 QListWidgetItem* item = insertNetwork(info);
806 ui.networkList->setCurrentItem(item);
811 void NetworksSettingsPage::on_deleteNetwork_clicked()
813 if (ui.networkList->selectedItems().count()) {
814 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
816 = QMessageBox::question(this,
817 tr("Delete Network?"),
818 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?")
819 .arg(networkInfos[netid].networkName),
820 QMessageBox::Yes | QMessageBox::No,
822 if (ret == QMessageBox::Yes) {
824 networkInfos.remove(netid);
825 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
826 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow() + 1, ui.networkList->count() - 1));
833 void NetworksSettingsPage::on_renameNetwork_clicked()
835 if (!ui.networkList->selectedItems().count())
837 QString old = ui.networkList->selectedItems()[0]->text();
838 QStringList existing;
839 for (int i = 0; i < ui.networkList->count(); i++)
840 existing << ui.networkList->item(i)->text();
841 NetworkEditDlg dlg(old, existing, this);
842 if (dlg.exec() == QDialog::Accepted) {
843 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
844 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
845 networkInfos[netid].networkName = dlg.networkName();
851 void NetworksSettingsPage::on_connectNow_clicked() {
852 if(!ui.networkList->selectedItems().count()) return;
853 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
854 const Network *net = Client::network(id);
856 if(net->connectionState() == Network::Disconnected) net->requestConnect();
857 else net->requestDisconnect();
861 /*** Server list ***/
863 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
868 void NetworksSettingsPage::on_addServer_clicked()
872 ServerEditDlg dlg(Network::Server(), this);
873 if (dlg.exec() == QDialog::Accepted) {
874 networkInfos[currentId].serverList.append(dlg.serverData());
875 displayNetwork(currentId);
876 ui.serverList->setCurrentRow(ui.serverList->count() - 1);
881 void NetworksSettingsPage::on_editServer_clicked()
885 int cur = ui.serverList->currentRow();
886 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
887 if (dlg.exec() == QDialog::Accepted) {
888 networkInfos[currentId].serverList[cur] = dlg.serverData();
889 displayNetwork(currentId);
890 ui.serverList->setCurrentRow(cur);
895 void NetworksSettingsPage::on_deleteServer_clicked()
899 int cur = ui.serverList->currentRow();
900 networkInfos[currentId].serverList.removeAt(cur);
901 displayNetwork(currentId);
902 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count() - 1));
906 void NetworksSettingsPage::on_upServer_clicked()
908 int cur = ui.serverList->currentRow();
909 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
910 networkInfos[currentId].serverList.insert(cur - 1, server);
911 displayNetwork(currentId);
912 ui.serverList->setCurrentRow(cur - 1);
916 void NetworksSettingsPage::on_downServer_clicked()
918 int cur = ui.serverList->currentRow();
919 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
920 networkInfos[currentId].serverList.insert(cur + 1, server);
921 displayNetwork(currentId);
922 ui.serverList->setCurrentRow(cur + 1);
926 void NetworksSettingsPage::on_editIdentities_clicked()
928 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
932 void NetworksSettingsPage::on_saslStatusDetails_clicked()
934 if (ui.networkList->selectedItems().count()) {
935 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
936 QString& netName = networkInfos[netid].networkName;
938 // If these strings are visible, one of the status messages wasn't detected below.
939 QString saslStatusHeader = "[header unintentionally left blank]";
940 QString saslStatusExplanation = "[explanation unintentionally left blank]";
942 // If true, show a warning icon instead of an information icon
943 bool useWarningIcon = false;
945 // Determine which explanation to show
946 switch (_saslStatusSelected) {
947 case CapSupportStatus::Unknown:
948 saslStatusHeader = tr("Could not check if SASL supported by network");
949 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
950 "be due to unsaved changes or an older Quassel core. You "
951 "can still try using SASL.")
954 case CapSupportStatus::Disconnected:
955 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
956 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
957 "disconnected. Connect to the network, or try using SASL "
961 case CapSupportStatus::MaybeUnsupported:
962 saslStatusHeader = tr("SASL not currently supported by network");
963 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
964 "However, support might be added later on.")
966 useWarningIcon = true;
968 case CapSupportStatus::MaybeSupported:
969 saslStatusHeader = tr("SASL supported by network");
970 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
971 "should use SASL instead of NickServ identification.")
976 // Process this in advance for reusability below
977 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
978 const QString saslStatusMsgText = QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>")
979 .arg(saslStatusHeader,
980 saslStatusExplanation,
981 tr("SASL is a standardized way to log in and identify yourself to "
984 if (useWarningIcon) {
985 // Show as a warning dialog box
986 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
989 // Show as an information dialog box
990 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
995 IdentityId NetworksSettingsPage::defaultIdentity() const
997 IdentityId defaultId = 0;
998 QList<IdentityId> ids = Client::identityIds();
999 foreach (IdentityId id, ids) {
1000 if (defaultId == 0 || id < defaultId)
1006 /**************************************************************************
1008 *************************************************************************/
1010 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget* parent)
1012 , existing(std::move(exist))
1015 ui.useSSL->setIcon(icon::get("document-encrypt"));
1017 // Whenever useSSL is toggled, update the port number if not changed from the default
1018 connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1019 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1020 // If useSSL is later changed to be checked by default, change port's default value, too.
1022 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1023 // Synchronize requiring SSL with the use SSL checkbox
1024 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1025 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1028 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1029 ui.sslVerify->setEnabled(false);
1030 ui.sslVerify->setChecked(false);
1031 // Split up the message to allow re-using translations:
1032 // [Original tool-tip]
1033 // [Bold 'does not support feature' message]
1034 // [Specific version needed and feature details]
1035 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1036 .arg(ui.sslVerify->toolTip(),
1037 tr("Your Quassel core does not support this feature"),
1038 tr("You need a Quassel core v0.13.0 or newer in order to "
1039 "verify connection security.")));
1042 // read preset networks
1043 QStringList networks = PresetNetworks::names();
1044 foreach (QString s, existing)
1045 networks.removeAll(s);
1046 if (networks.count())
1047 ui.presetList->addItems(networks);
1049 ui.useManual->setChecked(true);
1050 ui.usePreset->setEnabled(false);
1052 connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1053 connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1054 connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1055 connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1059 NetworkInfo NetworkAddDlg::networkInfo() const
1061 if (ui.useManual->isChecked()) {
1063 info.networkName = ui.networkName->text().trimmed();
1064 info.serverList << Network::Server(ui.serverAddress->text().trimmed(),
1066 ui.serverPassword->text(),
1067 ui.useSSL->isChecked(),
1068 ui.sslVerify->isChecked());
1072 return PresetNetworks::networkInfo(ui.presetList->currentText());
1075 void NetworkAddDlg::setButtonStates()
1078 if (ui.usePreset->isChecked() && ui.presetList->count())
1080 else if (ui.useManual->isChecked()) {
1081 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1082 && !ui.serverAddress->text().isEmpty();
1084 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1087 void NetworkAddDlg::updateSslPort(bool isChecked)
1089 // "Use encrypted connection" was toggled, check the state...
1090 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1091 // Had been using the plain-text port, use the SSL default
1092 ui.port->setValue(Network::PORT_SSL);
1094 else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1095 // Had been using the SSL port, use the plain-text default
1096 ui.port->setValue(Network::PORT_PLAINTEXT);
1100 /**************************************************************************
1102 *************************************************************************/
1104 NetworkEditDlg::NetworkEditDlg(const QString& old, QStringList exist, QWidget* parent)
1106 , existing(std::move(exist))
1110 if (old.isEmpty()) {
1112 setWindowTitle(tr("Add Network"));
1113 on_networkEdit_textChanged(""); // disable ok button
1116 ui.networkEdit->setText(old);
1119 QString NetworkEditDlg::networkName() const
1121 return ui.networkEdit->text().trimmed();
1124 void NetworkEditDlg::on_networkEdit_textChanged(const QString& text)
1126 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1129 /**************************************************************************
1131 *************************************************************************/
1132 ServerEditDlg::ServerEditDlg(const Network::Server& server, QWidget* parent)
1136 ui.useSSL->setIcon(icon::get("document-encrypt"));
1137 ui.host->setText(server.host);
1138 ui.host->setFocus();
1139 ui.port->setValue(server.port);
1140 ui.password->setText(server.password);
1141 ui.useSSL->setChecked(server.useSsl);
1142 ui.sslVerify->setChecked(server.sslVerify);
1143 ui.sslVersion->setCurrentIndex(server.sslVersion);
1144 ui.useProxy->setChecked(server.useProxy);
1145 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1146 ui.proxyHost->setText(server.proxyHost);
1147 ui.proxyPort->setValue(server.proxyPort);
1148 ui.proxyUsername->setText(server.proxyUser);
1149 ui.proxyPassword->setText(server.proxyPass);
1151 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1152 // only if the core won't use autonegotiation to determine the best
1153 // protocol. When autonegotiation was introduced, it would have been
1154 // a good idea to use the CoreFeatures enum to accomplish this.
1155 // However, since multiple versions have been released since then, that
1156 // is no longer possible. Instead, we rely on the fact that the
1157 // Datastream protocol was introduced in the same version (0.10) as SSL
1158 // autonegotiation. Because of that, we can display the dropdown only
1159 // if the Legacy protocol is in use. If any other RemotePeer protocol
1160 // is in use, that means a newer protocol is in use and therefore the
1161 // core will use autonegotiation.
1162 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1164 ui.sslVersion->hide();
1167 // Whenever useSSL is toggled, update the port number if not changed from the default
1168 connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1169 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1170 // If useSSL is later changed to be checked by default, change port's default value, too.
1172 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1173 // Synchronize requiring SSL with the use SSL checkbox
1174 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1175 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1178 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1179 ui.sslVerify->setEnabled(false);
1180 ui.sslVerify->setChecked(false);
1181 // Split up the message to allow re-using translations:
1182 // [Original tool-tip]
1183 // [Bold 'does not support feature' message]
1184 // [Specific version needed and feature details]
1185 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1186 .arg(ui.sslVerify->toolTip(),
1187 tr("Your Quassel core does not support this feature"),
1188 tr("You need a Quassel core v0.13.0 or newer in order to "
1189 "verify connection security.")));
1192 on_host_textChanged();
1195 Network::Server ServerEditDlg::serverData() const
1197 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1198 server.sslVersion = ui.sslVersion->currentIndex();
1199 server.useProxy = ui.useProxy->isChecked();
1200 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1201 server.proxyHost = ui.proxyHost->text();
1202 server.proxyPort = ui.proxyPort->value();
1203 server.proxyUser = ui.proxyUsername->text();
1204 server.proxyPass = ui.proxyPassword->text();
1208 void ServerEditDlg::on_host_textChanged()
1210 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1213 void ServerEditDlg::updateSslPort(bool isChecked)
1215 // "Use encrypted connection" was toggled, check the state...
1216 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1217 // Had been using the plain-text port, use the SSL default
1218 ui.port->setValue(Network::PORT_SSL);
1220 else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1221 // Had been using the SSL port, use the plain-text default
1222 ui.port->setValue(Network::PORT_PLAINTEXT);
1226 /**************************************************************************
1228 *************************************************************************/
1230 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo>& toCreate,
1231 const QList<NetworkInfo>& toUpdate,
1232 const QList<NetworkId>& toRemove,
1238 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1241 ui.progressBar->setMaximum(numevents);
1242 ui.progressBar->setValue(0);
1244 connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1245 connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1247 foreach (NetworkId id, toRemove) {
1248 Client::removeNetwork(id);
1250 foreach (NetworkInfo info, toCreate) {
1251 Client::createNetwork(info);
1253 foreach (NetworkInfo info, toUpdate) {
1254 const Network* net = Client::network(info.networkId);
1256 qWarning() << "Invalid client network!";
1260 // FIXME this only checks for one changed item rather than all!
1261 connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1262 Client::updateNetwork(info);
1266 qWarning() << "Sync dialog called without stuff to change!";
1271 void SaveNetworksDlg::clientEvent()
1273 ui.progressBar->setValue(++rcvevents);
1274 if (rcvevents >= numevents)