1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 <QHeaderView>
22 #include <QMessageBox>
26 #include "networkssettingspage.h"
32 #include "presetnetworks.h"
33 #include "settingspagedlg.h"
35 #include "widgethelpers.h"
40 #include "settingspages/identitiessettingspage.h"
42 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
43 : SettingsPage(tr("IRC"), tr("Networks"), parent)
47 // hide SASL options for older cores
48 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
50 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
51 ui.saslExtInfo->hide();
53 ui.saslExtInfo->hide();
57 ui.renameNetwork->setIcon(icon::get("edit-rename"));
58 ui.addNetwork->setIcon(icon::get("list-add"));
59 ui.deleteNetwork->setIcon(icon::get("edit-delete"));
60 ui.addServer->setIcon(icon::get("list-add"));
61 ui.deleteServer->setIcon(icon::get("edit-delete"));
62 ui.editServer->setIcon(icon::get("configure"));
63 ui.upServer->setIcon(icon::get("go-up"));
64 ui.downServer->setIcon(icon::get("go-down"));
65 ui.editIdentities->setIcon(icon::get("configure"));
67 connectedIcon = icon::get("network-connect");
68 connectingIcon = icon::get("network-wired"); // FIXME network-connecting
69 disconnectedIcon = icon::get("network-disconnect");
72 infoIcon = icon::get("dialog-information");
73 warningIcon = icon::get("dialog-warning");
75 foreach(int mib, QTextCodec::availableMibs()) {
76 QByteArray codec = QTextCodec::codecForMib(mib)->name();
77 ui.sendEncoding->addItem(codec);
78 ui.recvEncoding->addItem(codec);
79 ui.serverEncoding->addItem(codec);
81 ui.sendEncoding->model()->sort(0);
82 ui.recvEncoding->model()->sort(0);
83 ui.serverEncoding->model()->sort(0);
85 setEnabled(Client::isConnected()); // need a core connection!
88 connectToWidgetsChangedSignals({
95 ui.autoIdentifyService,
96 ui.autoIdentifyPassword,
97 ui.useCustomEncodings,
102 ui.reconnectInterval,
105 ui.rejoinOnReconnect,
106 ui.useCustomMessageRate,
107 ui.messageRateBurstSize,
109 ui.unlimitedMessageRate
110 }, this, &NetworksSettingsPage::widgetHasChanged);
112 connect(Client::instance(), &Client::coreConnectionStateChanged, this, &NetworksSettingsPage::coreConnectionStateChanged);
113 connect(Client::instance(), &Client::networkCreated, this, &NetworksSettingsPage::clientNetworkAdded);
114 connect(Client::instance(), &Client::networkRemoved, this, &NetworksSettingsPage::clientNetworkRemoved);
115 connect(Client::instance(), &Client::identityCreated, this, &NetworksSettingsPage::clientIdentityAdded);
116 connect(Client::instance(), &Client::identityRemoved, this, &NetworksSettingsPage::clientIdentityRemoved);
118 foreach(IdentityId id, Client::identityIds()) {
119 clientIdentityAdded(id);
124 void NetworksSettingsPage::save()
127 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
129 QList<NetworkInfo> toCreate, toUpdate;
130 QList<NetworkId> toRemove;
131 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
132 while (i != networkInfos.end()) {
133 NetworkId id = (*i).networkId;
136 //if(id == currentId) currentId = 0;
137 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
138 //if(items.count()) {
139 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
142 //i = networkInfos.erase(i);
146 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
152 foreach(NetworkId id, Client::networkIds()) {
153 if (!networkInfos.contains(id)) toRemove.append(id);
155 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
156 int ret = dlg.exec();
157 if (ret == QDialog::Rejected) {
158 // canceled -> reload everything to be safe
161 setChangedState(false);
166 void NetworksSettingsPage::load()
170 // Handle UI dependent on core feature flags here
171 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
172 // Custom rate limiting supported, allow toggling
173 ui.useCustomMessageRate->setEnabled(true);
174 // Reset tooltip to default.
175 ui.useCustomMessageRate->setToolTip(QString("%1").arg(
176 tr("<p>Override default message rate limiting.</p>"
177 "<p><b>Setting limits too low may get you disconnected"
178 " from the server!</b></p>")));
179 // If changed, update the message below!
181 // Custom rate limiting not supported, disallow toggling
182 ui.useCustomMessageRate->setEnabled(false);
183 // Split up the message to allow re-using translations:
184 // [Original tool-tip]
185 // [Bold 'does not support feature' message]
186 // [Specific version needed and feature details]
187 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
188 tr("<p>Override default message rate limiting.</p>"
189 "<p><b>Setting limits too low may get you disconnected"
190 " from the server!</b></p>"),
191 tr("Your Quassel core does not support this feature"),
192 tr("You need a Quassel core v0.13.0 or newer in order to "
193 "modify message rate limits.")));
197 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
198 // backlog from the core.
202 // Reset network capability status in case no valid networks get selected (a rare situation)
203 resetNetworkCapStates();
205 foreach(NetworkId netid, Client::networkIds()) {
206 clientNetworkAdded(netid);
208 ui.networkList->setCurrentRow(0);
210 setChangedState(false);
214 void NetworksSettingsPage::reset()
217 ui.networkList->clear();
218 networkInfos.clear();
222 bool NetworksSettingsPage::aboutToSave()
224 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
226 foreach(NetworkInfo info, networkInfos.values()) {
227 if (!info.serverList.count()) errors.append(1);
229 if (!errors.count()) return true;
230 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
231 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
232 error += tr("</ul>");
233 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
238 void NetworksSettingsPage::widgetHasChanged()
240 if (_ignoreWidgetChanges) return;
241 bool changed = testHasChanged();
242 if (changed != hasChanged()) setChangedState(changed);
246 bool NetworksSettingsPage::testHasChanged()
248 if (currentId != 0) {
249 saveToNetworkInfo(networkInfos[currentId]);
251 if (Client::networkIds().count() != networkInfos.count()) return true;
252 foreach(NetworkId id, networkInfos.keys()) {
253 if (id < 0) return true;
254 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
260 void NetworksSettingsPage::setWidgetStates()
263 if (ui.networkList->selectedItems().count()) {
264 ui.detailsBox->setEnabled(true);
265 ui.renameNetwork->setEnabled(true);
266 ui.deleteNetwork->setEnabled(true);
268 /* button disabled for now
269 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
270 const Network *net = id > 0 ? Client::network(id) : 0;
271 ui.connectNow->setEnabled(net);
272 // && (Client::network(id)->connectionState() == Network::Initialized
273 // || Client::network(id)->connectionState() == Network::Disconnected));
275 if(net->connectionState() == Network::Disconnected) {
276 ui.connectNow->setIcon(connectedIcon);
277 ui.connectNow->setText(tr("Connect"));
279 ui.connectNow->setIcon(disconnectedIcon);
280 ui.connectNow->setText(tr("Disconnect"));
283 ui.connectNow->setIcon(QIcon());
284 ui.connectNow->setText(tr("Apply first!"));
288 ui.renameNetwork->setEnabled(false);
289 ui.deleteNetwork->setEnabled(false);
290 //ui.connectNow->setEnabled(false);
291 ui.detailsBox->setEnabled(false);
294 if (ui.serverList->selectedItems().count()) {
295 ui.editServer->setEnabled(true);
296 ui.deleteServer->setEnabled(true);
297 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
298 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
301 ui.editServer->setEnabled(false);
302 ui.deleteServer->setEnabled(false);
303 ui.upServer->setEnabled(false);
304 ui.downServer->setEnabled(false);
309 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
311 if (!item && !(item = networkItem(id))) return;
312 const Network *net = Client::network(id);
313 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
314 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
315 if (net && net->connectionState() == Network::Initialized) {
316 item->setIcon(connectedIcon);
318 else if (net && net->connectionState() != Network::Disconnected) {
319 item->setIcon(connectingIcon);
322 item->setIcon(disconnectedIcon);
326 // check if we already have another net of this name in the list, and replace it
327 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
329 foreach(QListWidgetItem *i, items) {
330 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
331 if (oldid > 0) continue; // only locally created nets should be replaced
332 if (oldid == currentId) {
335 ui.networkList->clearSelection();
337 int row = ui.networkList->row(i);
339 QListWidgetItem *olditem = ui.networkList->takeItem(row);
343 networkInfos.remove(oldid);
347 item->setText(net->networkName());
348 if (select) item->setSelected(true);
353 void NetworksSettingsPage::resetNetworkCapStates()
355 // Set the status to a blank (invalid) network ID, reseting all UI
356 setNetworkCapStates(NetworkId());
360 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
362 const Network *net = Client::network(id);
363 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
364 // Capability negotiation is supported, network exists.
365 // Check if the network is connected. Don't use net->isConnected() as that won't be true
366 // during capability negotiation when capabilities are added and removed.
367 if (net->connectionState() != Network::Disconnected) {
368 // Network exists and is connected, check available capabilities...
370 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
371 setSASLStatus(CapSupportStatus::MaybeSupported);
373 setSASLStatus(CapSupportStatus::MaybeUnsupported);
376 // Add additional capability-dependent interface updates here
378 // Network is disconnected
380 setSASLStatus(CapSupportStatus::Disconnected);
382 // Add additional capability-dependent interface updates here
385 // Capability negotiation is not supported and/or network doesn't exist.
386 // Don't assume anything and reset all capability-dependent interface elements to neutral.
388 setSASLStatus(CapSupportStatus::Unknown);
390 // Add additional capability-dependent interface updates here
395 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
397 this->setEnabled(state);
408 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
410 const Identity *identity = Client::identity(id);
411 connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
413 QString name = identity->identityName();
414 for (int j = 0; j < ui.identityList->count(); j++) {
415 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
416 ui.identityList->insertItem(j, name, id.toInt());
422 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
427 void NetworksSettingsPage::clientIdentityUpdated()
429 const auto *identity = qobject_cast<const Identity *>(sender());
431 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
434 int row = ui.identityList->findData(identity->id().toInt());
436 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
439 if (ui.identityList->itemText(row) != identity->identityName()) {
440 ui.identityList->setItemText(row, identity->identityName());
445 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
447 IdentityId defaultId = defaultIdentity();
448 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
449 foreach(NetworkInfo info, networkInfos.values()) {
450 if (info.identity == id) {
451 if (info.networkId == currentId)
452 ui.identityList->setCurrentIndex(0);
453 info.identity = defaultId;
454 networkInfos[info.networkId] = info;
455 if (info.networkId > 0) Client::updateNetwork(info);
458 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
463 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
465 for (int i = 0; i < ui.networkList->count(); i++) {
466 QListWidgetItem *item = ui.networkList->item(i);
467 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
473 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
476 //connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
477 connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
479 connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
480 connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
482 // Handle capability changes in case a server dis/connects with the settings window open.
483 connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
484 connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
488 void NetworksSettingsPage::clientNetworkUpdated()
490 const auto *net = qobject_cast<const Network *>(sender());
492 qWarning() << "Update request for unknown network received!";
495 networkInfos[net->networkId()] = net->networkInfo();
496 setItemState(net->networkId());
497 if (net->networkId() == currentId) displayNetwork(net->networkId());
503 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
505 if (!networkInfos.contains(id)) return;
506 if (id == currentId) displayNetwork(0);
507 NetworkInfo info = networkInfos.take(id);
508 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
509 foreach(QListWidgetItem *item, items) {
510 if (item->data(Qt::UserRole).value<NetworkId>() == id)
511 delete ui.networkList->takeItem(ui.networkList->row(item));
518 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
521 const auto *net = qobject_cast<const Network *>(sender());
524 if(net->networkId() == currentId) {
525 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
528 setItemState(net->networkId());
529 if (net->networkId() == currentId) {
530 // Network is currently shown. Update the capability-dependent UI in case capabilities have
532 setNetworkCapStates(currentId);
538 void NetworksSettingsPage::networkConnectionError(const QString &)
543 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
545 NetworkInfo info = Client::network(id)->networkInfo();
546 networkInfos[id] = info;
547 return insertNetwork(info);
551 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
553 QListWidgetItem *item = nullptr;
554 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
555 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
557 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
558 // -> then we can be sure that this is the core-side replacement for the net we created
559 foreach(QListWidgetItem *i, items) {
560 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
561 if (id < 0) { item = i; break; }
563 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
565 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
566 setItemState(info.networkId, item);
572 // Called when selecting 'Configure' from the buffer list
573 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
575 QListWidgetItem *item = networkItem(netId);
576 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
580 void NetworksSettingsPage::displayNetwork(NetworkId id)
582 _ignoreWidgetChanges = true;
584 NetworkInfo info = networkInfos[id];
587 // this is only needed when the core supports SASL EXTERNAL
588 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
590 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
593 _cid = new CertIdentity(*Client::identity(info.identity), this);
594 _cid->enableEditSsl(true);
595 connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
599 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
600 ui.serverList->clear();
601 foreach(Network::Server server, info.serverList) {
602 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
604 item->setIcon(icon::get("document-encrypt"));
605 ui.serverList->addItem(item);
608 //ui.randomServer->setChecked(info.useRandomServer);
609 // Update the capability-dependent UI in case capabilities have changed.
610 setNetworkCapStates(id);
611 ui.performEdit->setPlainText(info.perform.join("\n"));
612 ui.autoIdentify->setChecked(info.useAutoIdentify);
613 ui.autoIdentifyService->setText(info.autoIdentifyService);
614 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
615 ui.sasl->setChecked(info.useSasl);
616 ui.saslAccount->setText(info.saslAccount);
617 ui.saslPassword->setText(info.saslPassword);
618 if (info.codecForEncoding.isEmpty()) {
619 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
620 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
621 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
622 ui.useCustomEncodings->setChecked(false);
625 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
626 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
627 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
628 ui.useCustomEncodings->setChecked(true);
630 ui.autoReconnect->setChecked(info.useAutoReconnect);
631 ui.reconnectInterval->setValue(info.autoReconnectInterval);
632 ui.reconnectRetries->setValue(info.autoReconnectRetries);
633 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
634 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
635 // Custom rate limiting
636 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
637 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
638 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
639 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
640 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
641 // Convert milliseconds (integer) into seconds (double)
642 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
645 // just clear widgets
648 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
652 ui.identityList->setCurrentIndex(-1);
653 ui.serverList->clear();
654 ui.performEdit->clear();
655 ui.autoIdentifyService->clear();
656 ui.autoIdentifyPassword->clear();
657 ui.saslAccount->clear();
658 ui.saslPassword->clear();
661 _ignoreWidgetChanges = false;
666 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
668 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
669 //info.useRandomServer = ui.randomServer->isChecked();
670 info.perform = ui.performEdit->toPlainText().split("\n");
671 info.useAutoIdentify = ui.autoIdentify->isChecked();
672 info.autoIdentifyService = ui.autoIdentifyService->text();
673 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
674 info.useSasl = ui.sasl->isChecked();
675 info.saslAccount = ui.saslAccount->text();
676 info.saslPassword = ui.saslPassword->text();
677 if (!ui.useCustomEncodings->isChecked()) {
678 info.codecForEncoding.clear();
679 info.codecForDecoding.clear();
680 info.codecForServer.clear();
683 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
684 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
685 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
687 info.useAutoReconnect = ui.autoReconnect->isChecked();
688 info.autoReconnectInterval = ui.reconnectInterval->value();
689 info.autoReconnectRetries = ui.reconnectRetries->value();
690 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
691 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
692 // Custom rate limiting
693 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
694 info.messageRateBurstSize = ui.messageRateBurstSize->value();
695 // Convert seconds (double) into milliseconds (integer)
696 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
697 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
701 void NetworksSettingsPage::clientNetworkCapsUpdated()
703 // Grab the updated network
704 const auto *net = qobject_cast<const Network *>(sender());
706 qWarning() << "Update request for unknown network received!";
709 if (net->networkId() == currentId) {
710 // Network is currently shown. Update the capability-dependent UI in case capabilities have
712 setNetworkCapStates(currentId);
717 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
719 if (_saslStatusSelected != saslStatus) {
720 // Update the cached copy of SASL status used with the Details dialog
721 _saslStatusSelected = saslStatus;
723 // Update the user interface
724 switch (saslStatus) {
725 case CapSupportStatus::Unknown:
726 // There's no capability negotiation or network doesn't exist. Don't assume
728 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
729 tr("Could not check if supported by network")));
730 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
732 case CapSupportStatus::Disconnected:
733 // Disconnected from network, no way to check.
734 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
735 tr("Cannot check if supported when disconnected")));
736 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
738 case CapSupportStatus::MaybeUnsupported:
739 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
740 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
741 tr("Not currently supported by network")));
742 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
744 case CapSupportStatus::MaybeSupported:
745 // The network advertises support for SASL PLAIN. Encourage using it!
746 // Unfortunately we don't know for sure if it's desired or functional.
747 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
748 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
756 void NetworksSettingsPage::sslUpdated()
758 if (_cid && !_cid->sslKey().isNull()) {
759 ui.saslContents->setDisabled(true);
760 ui.saslExtInfo->setHidden(false);
762 ui.saslContents->setDisabled(false);
763 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
764 // state to indicate whether or not it's disabled. To workaround this, keep track of
765 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
767 if (!ui.sasl->isChecked()) {
768 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
769 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
770 // sasl is later checked.
771 ui.sasl->setChecked(true);
772 ui.sasl->setChecked(false);
774 ui.saslExtInfo->setHidden(true);
780 /*** Network list ***/
782 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
784 if (currentId != 0) {
785 saveToNetworkInfo(networkInfos[currentId]);
787 if (ui.networkList->selectedItems().count()) {
788 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
791 ui.serverList->setCurrentRow(0);
800 void NetworksSettingsPage::on_addNetwork_clicked()
802 QStringList existing;
803 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
804 NetworkAddDlg dlg(existing, this);
805 if (dlg.exec() == QDialog::Accepted) {
806 NetworkInfo info = dlg.networkInfo();
807 if (info.networkName.isEmpty())
808 return; // sanity check
811 for (id = 1; id <= networkInfos.count(); id++) {
813 if (!networkInfos.keys().contains(-id.toInt())) break;
817 info.identity = defaultIdentity();
818 networkInfos[id] = info;
819 QListWidgetItem *item = insertNetwork(info);
820 ui.networkList->setCurrentItem(item);
826 void NetworksSettingsPage::on_deleteNetwork_clicked()
828 if (ui.networkList->selectedItems().count()) {
829 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
830 int ret = QMessageBox::question(this, tr("Delete Network?"),
831 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
832 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
833 if (ret == QMessageBox::Yes) {
835 networkInfos.remove(netid);
836 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
837 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
845 void NetworksSettingsPage::on_renameNetwork_clicked()
847 if (!ui.networkList->selectedItems().count()) return;
848 QString old = ui.networkList->selectedItems()[0]->text();
849 QStringList existing;
850 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
851 NetworkEditDlg dlg(old, existing, this);
852 if (dlg.exec() == QDialog::Accepted) {
853 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
854 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
855 networkInfos[netid].networkName = dlg.networkName();
862 void NetworksSettingsPage::on_connectNow_clicked() {
863 if(!ui.networkList->selectedItems().count()) return;
864 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
865 const Network *net = Client::network(id);
867 if(net->connectionState() == Network::Disconnected) net->requestConnect();
868 else net->requestDisconnect();
872 /*** Server list ***/
874 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
880 void NetworksSettingsPage::on_addServer_clicked()
882 if (currentId == 0) return;
883 ServerEditDlg dlg(Network::Server(), this);
884 if (dlg.exec() == QDialog::Accepted) {
885 networkInfos[currentId].serverList.append(dlg.serverData());
886 displayNetwork(currentId);
887 ui.serverList->setCurrentRow(ui.serverList->count()-1);
893 void NetworksSettingsPage::on_editServer_clicked()
895 if (currentId == 0) return;
896 int cur = ui.serverList->currentRow();
897 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
898 if (dlg.exec() == QDialog::Accepted) {
899 networkInfos[currentId].serverList[cur] = dlg.serverData();
900 displayNetwork(currentId);
901 ui.serverList->setCurrentRow(cur);
907 void NetworksSettingsPage::on_deleteServer_clicked()
909 if (currentId == 0) return;
910 int cur = ui.serverList->currentRow();
911 networkInfos[currentId].serverList.removeAt(cur);
912 displayNetwork(currentId);
913 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
918 void NetworksSettingsPage::on_upServer_clicked()
920 int cur = ui.serverList->currentRow();
921 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
922 networkInfos[currentId].serverList.insert(cur-1, server);
923 displayNetwork(currentId);
924 ui.serverList->setCurrentRow(cur-1);
929 void NetworksSettingsPage::on_downServer_clicked()
931 int cur = ui.serverList->currentRow();
932 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
933 networkInfos[currentId].serverList.insert(cur+1, server);
934 displayNetwork(currentId);
935 ui.serverList->setCurrentRow(cur+1);
940 void NetworksSettingsPage::on_editIdentities_clicked()
942 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
947 void NetworksSettingsPage::on_saslStatusDetails_clicked()
949 if (ui.networkList->selectedItems().count()) {
950 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
951 QString &netName = networkInfos[netid].networkName;
953 // If these strings are visible, one of the status messages wasn't detected below.
954 QString saslStatusHeader = "[header unintentionally left blank]";
955 QString saslStatusExplanation = "[explanation unintentionally left blank]";
957 // If true, show a warning icon instead of an information icon
958 bool useWarningIcon = false;
960 // Determine which explanation to show
961 switch (_saslStatusSelected) {
962 case CapSupportStatus::Unknown:
963 saslStatusHeader = tr("Could not check if SASL supported by network");
964 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
965 "be due to unsaved changes or an older Quassel core. You "
966 "can still try using SASL.").arg(netName);
968 case CapSupportStatus::Disconnected:
969 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
970 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
971 "disconnected. Connect to the network, or try using SASL "
972 "anyways.").arg(netName);
974 case CapSupportStatus::MaybeUnsupported:
975 saslStatusHeader = tr("SASL not currently supported by network");
976 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
977 "However, support might be added later on.").arg(netName);
978 useWarningIcon = true;
980 case CapSupportStatus::MaybeSupported:
981 saslStatusHeader = tr("SASL supported by network");
982 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
983 "should use SASL instead of NickServ identification."
988 // Process this in advance for reusability below
989 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
990 const QString saslStatusMsgText =
991 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
992 ).arg(saslStatusHeader,
993 saslStatusExplanation,
994 tr("SASL is a standardized way to log in and identify yourself to "
997 if (useWarningIcon) {
998 // Show as a warning dialog box
999 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1001 // Show as an information dialog box
1002 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1008 IdentityId NetworksSettingsPage::defaultIdentity() const
1010 IdentityId defaultId = 0;
1011 QList<IdentityId> ids = Client::identityIds();
1012 foreach(IdentityId id, ids) {
1013 if (defaultId == 0 || id < defaultId)
1020 /**************************************************************************
1022 *************************************************************************/
1024 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1027 ui.useSSL->setIcon(icon::get("document-encrypt"));
1029 // Whenever useSSL is toggled, update the port number if not changed from the default
1030 connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1031 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1032 // If useSSL is later changed to be checked by default, change port's default value, too.
1034 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1035 // Synchronize requiring SSL with the use SSL checkbox
1036 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1037 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1039 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1040 ui.sslVerify->setEnabled(false);
1041 ui.sslVerify->setChecked(false);
1042 // Split up the message to allow re-using translations:
1043 // [Original tool-tip]
1044 // [Bold 'does not support feature' message]
1045 // [Specific version needed and feature details]
1046 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1047 ui.sslVerify->toolTip(),
1048 tr("Your Quassel core does not support this feature"),
1049 tr("You need a Quassel core v0.13.0 or newer in order to "
1050 "verify connection security.")));
1053 // read preset networks
1054 QStringList networks = PresetNetworks::names();
1055 foreach(QString s, existing)
1056 networks.removeAll(s);
1057 if (networks.count())
1058 ui.presetList->addItems(networks);
1060 ui.useManual->setChecked(true);
1061 ui.usePreset->setEnabled(false);
1063 connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1064 connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1065 connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1066 connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1071 NetworkInfo NetworkAddDlg::networkInfo() const
1073 if (ui.useManual->isChecked()) {
1075 info.networkName = ui.networkName->text().trimmed();
1076 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1077 ui.serverPassword->text(), ui.useSSL->isChecked(),
1078 ui.sslVerify->isChecked());
1082 return PresetNetworks::networkInfo(ui.presetList->currentText());
1086 void NetworkAddDlg::setButtonStates()
1089 if (ui.usePreset->isChecked() && ui.presetList->count())
1091 else if (ui.useManual->isChecked()) {
1092 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1093 && !ui.serverAddress->text().isEmpty();
1095 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1099 void NetworkAddDlg::updateSslPort(bool isChecked)
1101 // "Use encrypted connection" was toggled, check the state...
1102 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1103 // Had been using the plain-text port, use the SSL default
1104 ui.port->setValue(Network::PORT_SSL);
1105 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1106 // Had been using the SSL port, use the plain-text default
1107 ui.port->setValue(Network::PORT_PLAINTEXT);
1112 /**************************************************************************
1114 *************************************************************************/
1116 NetworkEditDlg::NetworkEditDlg(const QString &old, QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1120 if (old.isEmpty()) {
1122 setWindowTitle(tr("Add Network"));
1123 on_networkEdit_textChanged(""); // disable ok button
1125 else ui.networkEdit->setText(old);
1129 QString NetworkEditDlg::networkName() const
1131 return ui.networkEdit->text().trimmed();
1135 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1137 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1141 /**************************************************************************
1143 *************************************************************************/
1144 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1147 ui.useSSL->setIcon(icon::get("document-encrypt"));
1148 ui.host->setText(server.host);
1149 ui.host->setFocus();
1150 ui.port->setValue(server.port);
1151 ui.password->setText(server.password);
1152 ui.useSSL->setChecked(server.useSsl);
1153 ui.sslVerify->setChecked(server.sslVerify);
1154 ui.sslVersion->setCurrentIndex(server.sslVersion);
1155 ui.useProxy->setChecked(server.useProxy);
1156 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1157 ui.proxyHost->setText(server.proxyHost);
1158 ui.proxyPort->setValue(server.proxyPort);
1159 ui.proxyUsername->setText(server.proxyUser);
1160 ui.proxyPassword->setText(server.proxyPass);
1162 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1163 // only if the core won't use autonegotiation to determine the best
1164 // protocol. When autonegotiation was introduced, it would have been
1165 // a good idea to use the CoreFeatures enum to accomplish this.
1166 // However, since multiple versions have been released since then, that
1167 // is no longer possible. Instead, we rely on the fact that the
1168 // Datastream protocol was introduced in the same version (0.10) as SSL
1169 // autonegotiation. Because of that, we can display the dropdown only
1170 // if the Legacy protocol is in use. If any other RemotePeer protocol
1171 // is in use, that means a newer protocol is in use and therefore the
1172 // core will use autonegotiation.
1173 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1175 ui.sslVersion->hide();
1178 // Whenever useSSL is toggled, update the port number if not changed from the default
1179 connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1180 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1181 // If useSSL is later changed to be checked by default, change port's default value, too.
1183 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1184 // Synchronize requiring SSL with the use SSL checkbox
1185 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1186 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1188 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1189 ui.sslVerify->setEnabled(false);
1190 ui.sslVerify->setChecked(false);
1191 // Split up the message to allow re-using translations:
1192 // [Original tool-tip]
1193 // [Bold 'does not support feature' message]
1194 // [Specific version needed and feature details]
1195 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1196 ui.sslVerify->toolTip(),
1197 tr("Your Quassel core does not support this feature"),
1198 tr("You need a Quassel core v0.13.0 or newer in order to "
1199 "verify connection security.")));
1202 on_host_textChanged();
1206 Network::Server ServerEditDlg::serverData() const
1208 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1209 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1210 server.sslVersion = ui.sslVersion->currentIndex();
1211 server.useProxy = ui.useProxy->isChecked();
1212 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1213 server.proxyHost = ui.proxyHost->text();
1214 server.proxyPort = ui.proxyPort->value();
1215 server.proxyUser = ui.proxyUsername->text();
1216 server.proxyPass = ui.proxyPassword->text();
1221 void ServerEditDlg::on_host_textChanged()
1223 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1227 void ServerEditDlg::updateSslPort(bool isChecked)
1229 // "Use encrypted connection" was toggled, check the state...
1230 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1231 // Had been using the plain-text port, use the SSL default
1232 ui.port->setValue(Network::PORT_SSL);
1233 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1234 // Had been using the SSL port, use the plain-text default
1235 ui.port->setValue(Network::PORT_PLAINTEXT);
1240 /**************************************************************************
1242 *************************************************************************/
1244 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1248 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1251 ui.progressBar->setMaximum(numevents);
1252 ui.progressBar->setValue(0);
1254 connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1255 connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1257 foreach(NetworkId id, toRemove) {
1258 Client::removeNetwork(id);
1260 foreach(NetworkInfo info, toCreate) {
1261 Client::createNetwork(info);
1263 foreach(NetworkInfo info, toUpdate) {
1264 const Network *net = Client::network(info.networkId);
1266 qWarning() << "Invalid client network!";
1270 // FIXME this only checks for one changed item rather than all!
1271 connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1272 Client::updateNetwork(info);
1276 qWarning() << "Sync dialog called without stuff to change!";
1282 void SaveNetworksDlg::clientEvent()
1284 ui.progressBar->setValue(++rcvevents);
1285 if (rcvevents >= numevents) accept();