1 /***************************************************************************
2 * Copyright (C) 2005-2020 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();
55 ui.renameNetwork->setIcon(icon::get("edit-rename"));
56 ui.addNetwork->setIcon(icon::get("list-add"));
57 ui.deleteNetwork->setIcon(icon::get("edit-delete"));
58 ui.addServer->setIcon(icon::get("list-add"));
59 ui.deleteServer->setIcon(icon::get("edit-delete"));
60 ui.editServer->setIcon(icon::get("configure"));
61 ui.upServer->setIcon(icon::get("go-up"));
62 ui.downServer->setIcon(icon::get("go-down"));
63 ui.editIdentities->setIcon(icon::get("configure"));
65 connectedIcon = icon::get("network-connect");
66 connectingIcon = icon::get("network-wired"); // FIXME network-connecting
67 disconnectedIcon = icon::get("network-disconnect");
70 infoIcon = icon::get("dialog-information");
71 warningIcon = icon::get("dialog-warning");
73 foreach (int mib, QTextCodec::availableMibs()) {
74 QByteArray codec = QTextCodec::codecForMib(mib)->name();
75 ui.sendEncoding->addItem(codec);
76 ui.recvEncoding->addItem(codec);
77 ui.serverEncoding->addItem(codec);
79 ui.sendEncoding->model()->sort(0);
80 ui.recvEncoding->model()->sort(0);
81 ui.serverEncoding->model()->sort(0);
83 setEnabled(Client::isConnected()); // need a core connection!
86 connectToWidgetsChangedSignals({ui.identityList, ui.performEdit, ui.sasl,
87 ui.saslAccount, ui.saslPassword, ui.autoIdentify,
88 ui.autoIdentifyService, ui.autoIdentifyPassword, ui.useCustomEncodings,
89 ui.sendEncoding, ui.recvEncoding, ui.serverEncoding,
90 ui.autoReconnect, ui.reconnectInterval, ui.reconnectRetries,
91 ui.unlimitedRetries, ui.rejoinOnReconnect, ui.useCustomMessageRate,
92 ui.messageRateBurstSize, ui.messageRateDelay, ui.unlimitedMessageRate},
94 &NetworksSettingsPage::widgetHasChanged);
96 connect(Client::instance(), &Client::coreConnectionStateChanged, this, &NetworksSettingsPage::coreConnectionStateChanged);
97 connect(Client::instance(), &Client::networkCreated, this, &NetworksSettingsPage::clientNetworkAdded);
98 connect(Client::instance(), &Client::networkRemoved, this, &NetworksSettingsPage::clientNetworkRemoved);
99 connect(Client::instance(), &Client::identityCreated, this, &NetworksSettingsPage::clientIdentityAdded);
100 connect(Client::instance(), &Client::identityRemoved, this, &NetworksSettingsPage::clientIdentityRemoved);
102 foreach (IdentityId id, Client::identityIds()) {
103 clientIdentityAdded(id);
107 void NetworksSettingsPage::save()
111 saveToNetworkInfo(networkInfos[currentId]);
113 QList<NetworkInfo> toCreate, toUpdate;
114 QList<NetworkId> toRemove;
115 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
116 while (i != networkInfos.end()) {
117 NetworkId id = (*i).networkId;
120 // if(id == currentId) currentId = 0;
121 // QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
122 // if(items.count()) {
123 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
126 // i = networkInfos.erase(i);
130 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
136 foreach (NetworkId id, Client::networkIds()) {
137 if (!networkInfos.contains(id))
140 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
141 int ret = dlg.exec();
142 if (ret == QDialog::Rejected) {
143 // canceled -> reload everything to be safe
146 setChangedState(false);
150 void NetworksSettingsPage::load()
154 // Handle UI dependent on core feature flags here
155 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
156 // Custom rate limiting supported, allow toggling
157 ui.useCustomMessageRate->setEnabled(true);
158 // Reset tooltip to default.
159 ui.useCustomMessageRate->setToolTip(QString("%1").arg(tr("<p>Override default message rate limiting.</p>"
160 "<p><b>Setting limits too low may get you disconnected"
161 " from the server!</b></p>")));
162 // If changed, update the message below!
165 // Custom rate limiting not supported, disallow toggling
166 ui.useCustomMessageRate->setEnabled(false);
167 // Split up the message to allow re-using translations:
168 // [Original tool-tip]
169 // [Bold 'does not support feature' message]
170 // [Specific version needed and feature details]
171 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
172 .arg(tr("<p>Override default message rate limiting.</p>"
173 "<p><b>Setting limits too low may get you disconnected"
174 " from the server!</b></p>"),
175 tr("Your Quassel core does not support this feature"),
176 tr("You need a Quassel core v0.13.0 or newer in order to "
177 "modify message rate limits.")));
180 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
181 // backlog from the core.
184 // Reset network capability status in case no valid networks get selected (a rare situation)
185 resetNetworkCapStates();
187 foreach (NetworkId netid, Client::networkIds()) {
188 clientNetworkAdded(netid);
190 ui.networkList->setCurrentRow(0);
192 setChangedState(false);
195 void NetworksSettingsPage::reset()
198 ui.networkList->clear();
199 networkInfos.clear();
202 bool NetworksSettingsPage::aboutToSave()
205 saveToNetworkInfo(networkInfos[currentId]);
207 foreach (NetworkInfo info, networkInfos.values()) {
208 if (!info.serverList.count())
213 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
214 if (errors.contains(1))
215 error += tr("<li>All networks need at least one server defined</li>");
216 error += tr("</ul>");
217 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
221 void NetworksSettingsPage::widgetHasChanged()
223 if (_ignoreWidgetChanges)
225 bool changed = testHasChanged();
226 if (changed != hasChanged())
227 setChangedState(changed);
230 bool NetworksSettingsPage::testHasChanged()
232 if (currentId != 0) {
233 saveToNetworkInfo(networkInfos[currentId]);
235 if (Client::networkIds().count() != networkInfos.count())
237 foreach (NetworkId id, networkInfos.keys()) {
240 if (Client::network(id)->networkInfo() != networkInfos[id])
246 void NetworksSettingsPage::setWidgetStates()
249 if (ui.networkList->selectedItems().count()) {
250 ui.detailsBox->setEnabled(true);
251 ui.renameNetwork->setEnabled(true);
252 ui.deleteNetwork->setEnabled(true);
254 /* button disabled for now
255 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
256 const Network *net = id > 0 ? Client::network(id) : 0;
257 ui.connectNow->setEnabled(net);
258 // && (Client::network(id)->connectionState() == Network::Initialized
259 // || Client::network(id)->connectionState() == Network::Disconnected));
261 if(net->connectionState() == Network::Disconnected) {
262 ui.connectNow->setIcon(connectedIcon);
263 ui.connectNow->setText(tr("Connect"));
265 ui.connectNow->setIcon(disconnectedIcon);
266 ui.connectNow->setText(tr("Disconnect"));
269 ui.connectNow->setIcon(QIcon());
270 ui.connectNow->setText(tr("Apply first!"));
274 ui.renameNetwork->setEnabled(false);
275 ui.deleteNetwork->setEnabled(false);
276 // ui.connectNow->setEnabled(false);
277 ui.detailsBox->setEnabled(false);
280 if (ui.serverList->selectedItems().count()) {
281 ui.editServer->setEnabled(true);
282 ui.deleteServer->setEnabled(true);
283 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
284 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
287 ui.editServer->setEnabled(false);
288 ui.deleteServer->setEnabled(false);
289 ui.upServer->setEnabled(false);
290 ui.downServer->setEnabled(false);
294 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem* item)
296 if (!item && !(item = networkItem(id)))
298 const Network* net = Client::network(id);
299 if (!net || net->isInitialized())
300 item->setFlags(item->flags() | Qt::ItemIsEnabled);
302 item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
303 if (net && net->connectionState() == Network::Initialized) {
304 item->setIcon(connectedIcon);
306 else if (net && net->connectionState() != Network::Disconnected) {
307 item->setIcon(connectingIcon);
310 item->setIcon(disconnectedIcon);
314 // check if we already have another net of this name in the list, and replace it
315 QList<QListWidgetItem*> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
317 foreach (QListWidgetItem* i, items) {
318 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
320 continue; // only locally created nets should be replaced
321 if (oldid == currentId) {
324 ui.networkList->clearSelection();
326 int row = ui.networkList->row(i);
328 QListWidgetItem* olditem = ui.networkList->takeItem(row);
332 networkInfos.remove(oldid);
336 item->setText(net->networkName());
338 item->setSelected(true);
342 void NetworksSettingsPage::resetNetworkCapStates()
344 // Set the status to a blank (invalid) network ID, reseting all UI
345 setNetworkCapStates(NetworkId());
348 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
350 const Network* net = Client::network(id);
351 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
352 // Capability negotiation is supported, network exists.
353 // Check if the network is connected. Don't use net->isConnected() as that won't be true
354 // during capability negotiation when capabilities are added and removed.
355 if (net->connectionState() != Network::Disconnected) {
356 // Network exists and is connected, check available capabilities...
358 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
359 setSASLStatus(CapSupportStatus::MaybeSupported);
362 setSASLStatus(CapSupportStatus::MaybeUnsupported);
365 // Add additional capability-dependent interface updates here
368 // Network is disconnected
370 setSASLStatus(CapSupportStatus::Disconnected);
372 // Add additional capability-dependent interface updates here
376 // Capability negotiation is not supported and/or network doesn't exist.
377 // Don't assume anything and reset all capability-dependent interface elements to neutral.
379 setSASLStatus(CapSupportStatus::Unknown);
381 // Add additional capability-dependent interface updates here
385 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
387 this->setEnabled(state);
397 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
399 const Identity* identity = Client::identity(id);
400 connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
402 QString name = identity->identityName();
403 for (int j = 0; j < ui.identityList->count(); j++) {
404 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
405 ui.identityList->insertItem(j, name, id.toInt());
411 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
415 void NetworksSettingsPage::clientIdentityUpdated()
417 const auto* identity = qobject_cast<const Identity*>(sender());
419 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
422 int row = ui.identityList->findData(identity->id().toInt());
424 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
427 if (ui.identityList->itemText(row) != identity->identityName()) {
428 ui.identityList->setItemText(row, identity->identityName());
432 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
434 IdentityId defaultId = defaultIdentity();
436 saveToNetworkInfo(networkInfos[currentId]);
437 foreach (NetworkInfo info, networkInfos.values()) {
438 if (info.identity == id) {
439 if (info.networkId == currentId)
440 ui.identityList->setCurrentIndex(0);
441 info.identity = defaultId;
442 networkInfos[info.networkId] = info;
443 if (info.networkId > 0)
444 Client::updateNetwork(info);
447 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
451 QListWidgetItem* NetworksSettingsPage::networkItem(NetworkId id) const
453 for (int i = 0; i < ui.networkList->count(); i++) {
454 QListWidgetItem* item = ui.networkList->item(i);
455 if (item->data(Qt::UserRole).value<NetworkId>() == id)
461 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
464 // connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
465 connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
467 connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
468 connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
470 // Handle capability changes in case a server dis/connects with the settings window open.
471 connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
472 connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
475 void NetworksSettingsPage::clientNetworkUpdated()
477 const auto* net = qobject_cast<const Network*>(sender());
479 qWarning() << "Update request for unknown network received!";
482 networkInfos[net->networkId()] = net->networkInfo();
483 setItemState(net->networkId());
484 if (net->networkId() == currentId)
485 displayNetwork(net->networkId());
490 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
492 if (!networkInfos.contains(id))
496 NetworkInfo info = networkInfos.take(id);
497 QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
498 foreach (QListWidgetItem* item, items) {
499 if (item->data(Qt::UserRole).value<NetworkId>() == id)
500 delete ui.networkList->takeItem(ui.networkList->row(item));
506 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
509 const auto* net = qobject_cast<const Network*>(sender());
513 if(net->networkId() == currentId) {
514 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
517 setItemState(net->networkId());
518 if (net->networkId() == currentId) {
519 // Network is currently shown. Update the capability-dependent UI in case capabilities have
521 setNetworkCapStates(currentId);
526 void NetworksSettingsPage::networkConnectionError(const QString&) {}
528 QListWidgetItem* NetworksSettingsPage::insertNetwork(NetworkId id)
530 NetworkInfo info = Client::network(id)->networkInfo();
531 networkInfos[id] = info;
532 return insertNetwork(info);
535 QListWidgetItem* NetworksSettingsPage::insertNetwork(const NetworkInfo& info)
537 QListWidgetItem* item = nullptr;
538 QList<QListWidgetItem*> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
540 item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
542 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
543 // -> then we can be sure that this is the core-side replacement for the net we created
544 foreach (QListWidgetItem* i, items) {
545 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
552 item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
554 item->setData(Qt::UserRole, QVariant::fromValue(info.networkId));
555 setItemState(info.networkId, item);
560 // Called when selecting 'Configure' from the buffer list
561 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
563 QListWidgetItem* item = networkItem(netId);
564 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
567 void NetworksSettingsPage::displayNetwork(NetworkId id)
569 _ignoreWidgetChanges = true;
571 NetworkInfo info = networkInfos[id];
573 // this is only needed when the core supports SASL EXTERNAL
574 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
576 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
579 _cid = new CertIdentity(*Client::identity(info.identity), this);
580 _cid->enableEditSsl(true);
581 connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
584 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
585 ui.serverList->clear();
586 foreach (Network::Server server, info.serverList) {
587 QListWidgetItem* item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
589 item->setIcon(icon::get("document-encrypt"));
590 ui.serverList->addItem(item);
593 // ui.randomServer->setChecked(info.useRandomServer);
594 // Update the capability-dependent UI in case capabilities have changed.
595 setNetworkCapStates(id);
596 ui.performEdit->setPlainText(info.perform.join("\n"));
597 ui.autoIdentify->setChecked(info.useAutoIdentify);
598 ui.autoIdentifyService->setText(info.autoIdentifyService);
599 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
600 ui.sasl->setChecked(info.useSasl);
601 ui.saslAccount->setText(info.saslAccount);
602 ui.saslPassword->setText(info.saslPassword);
603 if (info.codecForEncoding.isEmpty()) {
604 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
605 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
606 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
607 ui.useCustomEncodings->setChecked(false);
610 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
611 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
612 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
613 ui.useCustomEncodings->setChecked(true);
615 ui.autoReconnect->setChecked(info.useAutoReconnect);
616 ui.reconnectInterval->setValue(info.autoReconnectInterval);
617 ui.reconnectRetries->setValue(info.autoReconnectRetries);
618 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
619 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
620 // Custom rate limiting
621 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
622 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
623 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
624 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
625 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
626 // Convert milliseconds (integer) into seconds (double)
627 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
630 // just clear widgets
632 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
635 ui.identityList->setCurrentIndex(-1);
636 ui.serverList->clear();
637 ui.performEdit->clear();
638 ui.autoIdentifyService->clear();
639 ui.autoIdentifyPassword->clear();
640 ui.saslAccount->clear();
641 ui.saslPassword->clear();
644 _ignoreWidgetChanges = false;
648 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo& info)
650 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
651 // info.useRandomServer = ui.randomServer->isChecked();
652 info.perform = ui.performEdit->toPlainText().split("\n");
653 info.useAutoIdentify = ui.autoIdentify->isChecked();
654 info.autoIdentifyService = ui.autoIdentifyService->text();
655 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
656 info.useSasl = ui.sasl->isChecked();
657 info.saslAccount = ui.saslAccount->text();
658 info.saslPassword = ui.saslPassword->text();
659 if (!ui.useCustomEncodings->isChecked()) {
660 info.codecForEncoding.clear();
661 info.codecForDecoding.clear();
662 info.codecForServer.clear();
665 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
666 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
667 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
669 info.useAutoReconnect = ui.autoReconnect->isChecked();
670 info.autoReconnectInterval = ui.reconnectInterval->value();
671 info.autoReconnectRetries = ui.reconnectRetries->value();
672 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
673 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
674 // Custom rate limiting
675 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
676 info.messageRateBurstSize = ui.messageRateBurstSize->value();
677 // Convert seconds (double) into milliseconds (integer)
678 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
679 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
682 void NetworksSettingsPage::clientNetworkCapsUpdated()
684 // Grab the updated network
685 const auto* net = qobject_cast<const Network*>(sender());
687 qWarning() << "Update request for unknown network received!";
690 if (net->networkId() == currentId) {
691 // Network is currently shown. Update the capability-dependent UI in case capabilities have
693 setNetworkCapStates(currentId);
697 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
699 if (_saslStatusSelected != saslStatus) {
700 // Update the cached copy of SASL status used with the Details dialog
701 _saslStatusSelected = saslStatus;
703 // Update the user interface
704 switch (saslStatus) {
705 case CapSupportStatus::Unknown:
706 // There's no capability negotiation or network doesn't exist. Don't assume
708 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Could not check if supported by network")));
709 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
711 case CapSupportStatus::Disconnected:
712 // Disconnected from network, no way to check.
713 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Cannot check if supported when disconnected")));
714 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
716 case CapSupportStatus::MaybeUnsupported:
717 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
718 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Not currently supported by network")));
719 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
721 case CapSupportStatus::MaybeSupported:
722 // The network advertises support for SASL PLAIN. Encourage using it!
723 // Unfortunately we don't know for sure if it's desired or functional.
724 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
725 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
731 void NetworksSettingsPage::sslUpdated()
733 if (_cid && !_cid->sslKey().isNull()) {
734 ui.saslContents->setDisabled(true);
735 ui.saslExtInfo->setHidden(false);
738 ui.saslContents->setDisabled(false);
739 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
740 // state to indicate whether or not it's disabled. To workaround this, keep track of
741 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
743 if (!ui.sasl->isChecked()) {
744 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
745 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
746 // sasl is later checked.
747 ui.sasl->setChecked(true);
748 ui.sasl->setChecked(false);
750 ui.saslExtInfo->setHidden(true);
754 /*** Network list ***/
756 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
758 if (currentId != 0) {
759 saveToNetworkInfo(networkInfos[currentId]);
761 if (ui.networkList->selectedItems().count()) {
762 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
765 ui.serverList->setCurrentRow(0);
773 void NetworksSettingsPage::on_addNetwork_clicked()
775 QStringList existing;
776 for (int i = 0; i < ui.networkList->count(); i++)
777 existing << ui.networkList->item(i)->text();
778 NetworkAddDlg dlg(existing, this);
779 if (dlg.exec() == QDialog::Accepted) {
780 NetworkInfo info = dlg.networkInfo();
781 if (info.networkName.isEmpty())
782 return; // sanity check
785 for (id = 1; id <= networkInfos.count(); id++) {
787 if (!networkInfos.keys().contains(-id.toInt()))
792 info.identity = defaultIdentity();
793 networkInfos[id] = info;
794 QListWidgetItem* item = insertNetwork(info);
795 ui.networkList->setCurrentItem(item);
800 void NetworksSettingsPage::on_deleteNetwork_clicked()
802 if (ui.networkList->selectedItems().count()) {
803 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
805 = QMessageBox::question(this,
806 tr("Delete Network?"),
807 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?")
808 .arg(networkInfos[netid].networkName),
809 QMessageBox::Yes | QMessageBox::No,
811 if (ret == QMessageBox::Yes) {
813 networkInfos.remove(netid);
814 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
815 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow() + 1, ui.networkList->count() - 1));
822 void NetworksSettingsPage::on_renameNetwork_clicked()
824 if (!ui.networkList->selectedItems().count())
826 QString old = ui.networkList->selectedItems()[0]->text();
827 QStringList existing;
828 for (int i = 0; i < ui.networkList->count(); i++)
829 existing << ui.networkList->item(i)->text();
830 NetworkEditDlg dlg(old, existing, this);
831 if (dlg.exec() == QDialog::Accepted) {
832 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
833 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
834 networkInfos[netid].networkName = dlg.networkName();
840 void NetworksSettingsPage::on_connectNow_clicked() {
841 if(!ui.networkList->selectedItems().count()) return;
842 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
843 const Network *net = Client::network(id);
845 if(net->connectionState() == Network::Disconnected) net->requestConnect();
846 else net->requestDisconnect();
850 /*** Server list ***/
852 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
857 void NetworksSettingsPage::on_addServer_clicked()
861 ServerEditDlg dlg(Network::Server(), this);
862 if (dlg.exec() == QDialog::Accepted) {
863 networkInfos[currentId].serverList.append(dlg.serverData());
864 displayNetwork(currentId);
865 ui.serverList->setCurrentRow(ui.serverList->count() - 1);
870 void NetworksSettingsPage::on_editServer_clicked()
874 int cur = ui.serverList->currentRow();
875 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
876 if (dlg.exec() == QDialog::Accepted) {
877 networkInfos[currentId].serverList[cur] = dlg.serverData();
878 displayNetwork(currentId);
879 ui.serverList->setCurrentRow(cur);
884 void NetworksSettingsPage::on_deleteServer_clicked()
888 int cur = ui.serverList->currentRow();
889 networkInfos[currentId].serverList.removeAt(cur);
890 displayNetwork(currentId);
891 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count() - 1));
895 void NetworksSettingsPage::on_upServer_clicked()
897 int cur = ui.serverList->currentRow();
898 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
899 networkInfos[currentId].serverList.insert(cur - 1, server);
900 displayNetwork(currentId);
901 ui.serverList->setCurrentRow(cur - 1);
905 void NetworksSettingsPage::on_downServer_clicked()
907 int cur = ui.serverList->currentRow();
908 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
909 networkInfos[currentId].serverList.insert(cur + 1, server);
910 displayNetwork(currentId);
911 ui.serverList->setCurrentRow(cur + 1);
915 void NetworksSettingsPage::on_editIdentities_clicked()
917 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
921 void NetworksSettingsPage::on_saslStatusDetails_clicked()
923 if (ui.networkList->selectedItems().count()) {
924 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
925 QString& netName = networkInfos[netid].networkName;
927 // If these strings are visible, one of the status messages wasn't detected below.
928 QString saslStatusHeader = "[header unintentionally left blank]";
929 QString saslStatusExplanation = "[explanation unintentionally left blank]";
931 // If true, show a warning icon instead of an information icon
932 bool useWarningIcon = false;
934 // Determine which explanation to show
935 switch (_saslStatusSelected) {
936 case CapSupportStatus::Unknown:
937 saslStatusHeader = tr("Could not check if SASL supported by network");
938 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
939 "be due to unsaved changes or an older Quassel core. You "
940 "can still try using SASL.")
943 case CapSupportStatus::Disconnected:
944 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
945 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
946 "disconnected. Connect to the network, or try using SASL "
950 case CapSupportStatus::MaybeUnsupported:
951 saslStatusHeader = tr("SASL not currently supported by network");
952 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
953 "However, support might be added later on.")
955 useWarningIcon = true;
957 case CapSupportStatus::MaybeSupported:
958 saslStatusHeader = tr("SASL supported by network");
959 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
960 "should use SASL instead of NickServ identification.")
965 // Process this in advance for reusability below
966 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
967 const QString saslStatusMsgText = QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>")
968 .arg(saslStatusHeader,
969 saslStatusExplanation,
970 tr("SASL is a standardized way to log in and identify yourself to "
973 if (useWarningIcon) {
974 // Show as a warning dialog box
975 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
978 // Show as an information dialog box
979 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
984 IdentityId NetworksSettingsPage::defaultIdentity() const
986 IdentityId defaultId = 0;
987 QList<IdentityId> ids = Client::identityIds();
988 foreach (IdentityId id, ids) {
989 if (defaultId == 0 || id < defaultId)
995 /**************************************************************************
997 *************************************************************************/
999 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget* parent)
1001 , existing(std::move(exist))
1004 ui.useSSL->setIcon(icon::get("document-encrypt"));
1006 // Whenever useSSL is toggled, update the port number if not changed from the default
1007 connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1008 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1009 // If useSSL is later changed to be checked by default, change port's default value, too.
1011 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1012 // Synchronize requiring SSL with the use SSL checkbox
1013 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1014 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1017 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1018 ui.sslVerify->setEnabled(false);
1019 ui.sslVerify->setChecked(false);
1020 // Split up the message to allow re-using translations:
1021 // [Original tool-tip]
1022 // [Bold 'does not support feature' message]
1023 // [Specific version needed and feature details]
1024 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1025 .arg(ui.sslVerify->toolTip(),
1026 tr("Your Quassel core does not support this feature"),
1027 tr("You need a Quassel core v0.13.0 or newer in order to "
1028 "verify connection security.")));
1031 // read preset networks
1032 QStringList networks = PresetNetworks::names();
1033 foreach (QString s, existing)
1034 networks.removeAll(s);
1035 if (networks.count())
1036 ui.presetList->addItems(networks);
1038 ui.useManual->setChecked(true);
1039 ui.usePreset->setEnabled(false);
1041 connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1042 connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1043 connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1044 connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1048 NetworkInfo NetworkAddDlg::networkInfo() const
1050 if (ui.useManual->isChecked()) {
1052 info.networkName = ui.networkName->text().trimmed();
1053 info.serverList << Network::Server(ui.serverAddress->text().trimmed(),
1055 ui.serverPassword->text(),
1056 ui.useSSL->isChecked(),
1057 ui.sslVerify->isChecked());
1061 return PresetNetworks::networkInfo(ui.presetList->currentText());
1064 void NetworkAddDlg::setButtonStates()
1067 if (ui.usePreset->isChecked() && ui.presetList->count())
1069 else if (ui.useManual->isChecked()) {
1070 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1071 && !ui.serverAddress->text().isEmpty();
1073 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1076 void NetworkAddDlg::updateSslPort(bool isChecked)
1078 // "Use encrypted connection" was toggled, check the state...
1079 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1080 // Had been using the plain-text port, use the SSL default
1081 ui.port->setValue(Network::PORT_SSL);
1083 else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1084 // Had been using the SSL port, use the plain-text default
1085 ui.port->setValue(Network::PORT_PLAINTEXT);
1089 /**************************************************************************
1091 *************************************************************************/
1093 NetworkEditDlg::NetworkEditDlg(const QString& old, QStringList exist, QWidget* parent)
1095 , existing(std::move(exist))
1099 if (old.isEmpty()) {
1101 setWindowTitle(tr("Add Network"));
1102 on_networkEdit_textChanged(""); // disable ok button
1105 ui.networkEdit->setText(old);
1108 QString NetworkEditDlg::networkName() const
1110 return ui.networkEdit->text().trimmed();
1113 void NetworkEditDlg::on_networkEdit_textChanged(const QString& text)
1115 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1118 /**************************************************************************
1120 *************************************************************************/
1121 ServerEditDlg::ServerEditDlg(const Network::Server& server, QWidget* parent)
1125 ui.useSSL->setIcon(icon::get("document-encrypt"));
1126 ui.host->setText(server.host);
1127 ui.host->setFocus();
1128 ui.port->setValue(server.port);
1129 ui.password->setText(server.password);
1130 ui.useSSL->setChecked(server.useSsl);
1131 ui.sslVerify->setChecked(server.sslVerify);
1132 ui.sslVersion->setCurrentIndex(server.sslVersion);
1133 ui.useProxy->setChecked(server.useProxy);
1134 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1135 ui.proxyHost->setText(server.proxyHost);
1136 ui.proxyPort->setValue(server.proxyPort);
1137 ui.proxyUsername->setText(server.proxyUser);
1138 ui.proxyPassword->setText(server.proxyPass);
1140 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1141 // only if the core won't use autonegotiation to determine the best
1142 // protocol. When autonegotiation was introduced, it would have been
1143 // a good idea to use the CoreFeatures enum to accomplish this.
1144 // However, since multiple versions have been released since then, that
1145 // is no longer possible. Instead, we rely on the fact that the
1146 // Datastream protocol was introduced in the same version (0.10) as SSL
1147 // autonegotiation. Because of that, we can display the dropdown only
1148 // if the Legacy protocol is in use. If any other RemotePeer protocol
1149 // is in use, that means a newer protocol is in use and therefore the
1150 // core will use autonegotiation.
1151 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1153 ui.sslVersion->hide();
1156 // Whenever useSSL is toggled, update the port number if not changed from the default
1157 connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1158 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1159 // If useSSL is later changed to be checked by default, change port's default value, too.
1161 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1162 // Synchronize requiring SSL with the use SSL checkbox
1163 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1164 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1167 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1168 ui.sslVerify->setEnabled(false);
1169 ui.sslVerify->setChecked(false);
1170 // Split up the message to allow re-using translations:
1171 // [Original tool-tip]
1172 // [Bold 'does not support feature' message]
1173 // [Specific version needed and feature details]
1174 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3")
1175 .arg(ui.sslVerify->toolTip(),
1176 tr("Your Quassel core does not support this feature"),
1177 tr("You need a Quassel core v0.13.0 or newer in order to "
1178 "verify connection security.")));
1181 on_host_textChanged();
1184 Network::Server ServerEditDlg::serverData() const
1186 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1187 server.sslVersion = ui.sslVersion->currentIndex();
1188 server.useProxy = ui.useProxy->isChecked();
1189 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1190 server.proxyHost = ui.proxyHost->text();
1191 server.proxyPort = ui.proxyPort->value();
1192 server.proxyUser = ui.proxyUsername->text();
1193 server.proxyPass = ui.proxyPassword->text();
1197 void ServerEditDlg::on_host_textChanged()
1199 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1202 void ServerEditDlg::updateSslPort(bool isChecked)
1204 // "Use encrypted connection" was toggled, check the state...
1205 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1206 // Had been using the plain-text port, use the SSL default
1207 ui.port->setValue(Network::PORT_SSL);
1209 else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1210 // Had been using the SSL port, use the plain-text default
1211 ui.port->setValue(Network::PORT_PLAINTEXT);
1215 /**************************************************************************
1217 *************************************************************************/
1219 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo>& toCreate,
1220 const QList<NetworkInfo>& toUpdate,
1221 const QList<NetworkId>& toRemove,
1227 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1230 ui.progressBar->setMaximum(numevents);
1231 ui.progressBar->setValue(0);
1233 connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1234 connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1236 foreach (NetworkId id, toRemove) {
1237 Client::removeNetwork(id);
1239 foreach (NetworkInfo info, toCreate) {
1240 Client::createNetwork(info);
1242 foreach (NetworkInfo info, toUpdate) {
1243 const Network* net = Client::network(info.networkId);
1245 qWarning() << "Invalid client network!";
1249 // FIXME this only checks for one changed item rather than all!
1250 connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1251 Client::updateNetwork(info);
1255 qWarning() << "Sync dialog called without stuff to change!";
1260 void SaveNetworksDlg::clientEvent()
1262 ui.progressBar->setValue(++rcvevents);
1263 if (rcvevents >= numevents)