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"
39 #include "settingspages/identitiessettingspage.h"
41 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
42 : SettingsPage(tr("IRC"), tr("Networks"), parent)
46 // hide SASL options for older cores
47 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
49 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
50 ui.saslExtInfo->hide();
52 ui.saslExtInfo->hide();
56 ui.renameNetwork->setIcon(icon::get("edit-rename"));
57 ui.addNetwork->setIcon(icon::get("list-add"));
58 ui.deleteNetwork->setIcon(icon::get("edit-delete"));
59 ui.addServer->setIcon(icon::get("list-add"));
60 ui.deleteServer->setIcon(icon::get("edit-delete"));
61 ui.editServer->setIcon(icon::get("configure"));
62 ui.upServer->setIcon(icon::get("go-up"));
63 ui.downServer->setIcon(icon::get("go-down"));
64 ui.editIdentities->setIcon(icon::get("configure"));
66 _ignoreWidgetChanges = false;
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!
88 connect(Client::instance(), &Client::coreConnectionStateChanged, this, &NetworksSettingsPage::coreConnectionStateChanged);
89 connect(Client::instance(), &Client::networkCreated, this, &NetworksSettingsPage::clientNetworkAdded);
90 connect(Client::instance(), &Client::networkRemoved, this, &NetworksSettingsPage::clientNetworkRemoved);
91 connect(Client::instance(), &Client::identityCreated, this, &NetworksSettingsPage::clientIdentityAdded);
92 connect(Client::instance(), &Client::identityRemoved, this, &NetworksSettingsPage::clientIdentityRemoved);
94 connect(ui.identityList, selectOverload<int>(&QComboBox::currentIndexChanged), this, &NetworksSettingsPage::widgetHasChanged);
95 //connect(ui.randomServer, &QAbstractButton::clicked, this, &NetworksSettingsPage::widgetHasChanged);
96 connect(ui.performEdit, &QTextEdit::textChanged, this, &NetworksSettingsPage::widgetHasChanged);
97 connect(ui.sasl, &QGroupBox::clicked, this, &NetworksSettingsPage::widgetHasChanged);
98 connect(ui.saslAccount, &QLineEdit::textEdited, this, &NetworksSettingsPage::widgetHasChanged);
99 connect(ui.saslPassword, &QLineEdit::textEdited, this, &NetworksSettingsPage::widgetHasChanged);
100 connect(ui.autoIdentify, &QGroupBox::clicked, this, &NetworksSettingsPage::widgetHasChanged);
101 connect(ui.autoIdentifyService, &QLineEdit::textEdited, this, &NetworksSettingsPage::widgetHasChanged);
102 connect(ui.autoIdentifyPassword, &QLineEdit::textEdited, this, &NetworksSettingsPage::widgetHasChanged);
103 connect(ui.useCustomEncodings, &QGroupBox::clicked, this, &NetworksSettingsPage::widgetHasChanged);
104 connect(ui.sendEncoding, selectOverload<int>(&QComboBox::currentIndexChanged), this, &NetworksSettingsPage::widgetHasChanged);
105 connect(ui.recvEncoding, selectOverload<int>(&QComboBox::currentIndexChanged), this, &NetworksSettingsPage::widgetHasChanged);
106 connect(ui.serverEncoding, selectOverload<int>(&QComboBox::currentIndexChanged), this, &NetworksSettingsPage::widgetHasChanged);
107 connect(ui.autoReconnect, &QGroupBox::clicked, this, &NetworksSettingsPage::widgetHasChanged);
108 connect(ui.reconnectInterval, selectOverload<int>(&QSpinBox::valueChanged), this, &NetworksSettingsPage::widgetHasChanged);
109 connect(ui.reconnectRetries, selectOverload<int>(&QSpinBox::valueChanged), this, &NetworksSettingsPage::widgetHasChanged);
110 connect(ui.unlimitedRetries, &QAbstractButton::clicked, this, &NetworksSettingsPage::widgetHasChanged);
111 connect(ui.rejoinOnReconnect, &QAbstractButton::clicked, this, &NetworksSettingsPage::widgetHasChanged);
113 // Core features can change during a reconnect. Always connect these here, delaying testing for
114 // the core feature flag in load().
115 connect(ui.useCustomMessageRate, &QGroupBox::clicked, this, &NetworksSettingsPage::widgetHasChanged);
116 connect(ui.messageRateBurstSize, selectOverload<int>(&QSpinBox::valueChanged), this, &NetworksSettingsPage::widgetHasChanged);
117 connect(ui.messageRateDelay, selectOverload<double>(&QDoubleSpinBox::valueChanged), this, &NetworksSettingsPage::widgetHasChanged);
118 connect(ui.unlimitedMessageRate, &QAbstractButton::clicked, this, &NetworksSettingsPage::widgetHasChanged);
120 foreach(IdentityId id, Client::identityIds()) {
121 clientIdentityAdded(id);
126 void NetworksSettingsPage::save()
129 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
131 QList<NetworkInfo> toCreate, toUpdate;
132 QList<NetworkId> toRemove;
133 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
134 while (i != networkInfos.end()) {
135 NetworkId id = (*i).networkId;
138 //if(id == currentId) currentId = 0;
139 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
140 //if(items.count()) {
141 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
144 //i = networkInfos.erase(i);
148 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
154 foreach(NetworkId id, Client::networkIds()) {
155 if (!networkInfos.contains(id)) toRemove.append(id);
157 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
158 int ret = dlg.exec();
159 if (ret == QDialog::Rejected) {
160 // canceled -> reload everything to be safe
163 setChangedState(false);
168 void NetworksSettingsPage::load()
172 // Handle UI dependent on core feature flags here
173 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
174 // Custom rate limiting supported, allow toggling
175 ui.useCustomMessageRate->setEnabled(true);
176 // Reset tooltip to default.
177 ui.useCustomMessageRate->setToolTip(QString("%1").arg(
178 tr("<p>Override default message rate limiting.</p>"
179 "<p><b>Setting limits too low may get you disconnected"
180 " from the server!</b></p>")));
181 // If changed, update the message below!
183 // Custom rate limiting not supported, disallow toggling
184 ui.useCustomMessageRate->setEnabled(false);
185 // Split up the message to allow re-using translations:
186 // [Original tool-tip]
187 // [Bold 'does not support feature' message]
188 // [Specific version needed and feature details]
189 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
190 tr("<p>Override default message rate limiting.</p>"
191 "<p><b>Setting limits too low may get you disconnected"
192 " from the server!</b></p>"),
193 tr("Your Quassel core does not support this feature"),
194 tr("You need a Quassel core v0.13.0 or newer in order to "
195 "modify message rate limits.")));
199 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
200 // backlog from the core.
204 // Reset network capability status in case no valid networks get selected (a rare situation)
205 resetNetworkCapStates();
207 foreach(NetworkId netid, Client::networkIds()) {
208 clientNetworkAdded(netid);
210 ui.networkList->setCurrentRow(0);
212 setChangedState(false);
216 void NetworksSettingsPage::reset()
219 ui.networkList->clear();
220 networkInfos.clear();
224 bool NetworksSettingsPage::aboutToSave()
226 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
228 foreach(NetworkInfo info, networkInfos.values()) {
229 if (!info.serverList.count()) errors.append(1);
231 if (!errors.count()) return true;
232 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
233 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
234 error += tr("</ul>");
235 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
240 void NetworksSettingsPage::widgetHasChanged()
242 if (_ignoreWidgetChanges) return;
243 bool changed = testHasChanged();
244 if (changed != hasChanged()) setChangedState(changed);
248 bool NetworksSettingsPage::testHasChanged()
250 if (currentId != 0) {
251 saveToNetworkInfo(networkInfos[currentId]);
253 if (Client::networkIds().count() != networkInfos.count()) return true;
254 foreach(NetworkId id, networkInfos.keys()) {
255 if (id < 0) return true;
256 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
262 void NetworksSettingsPage::setWidgetStates()
265 if (ui.networkList->selectedItems().count()) {
266 ui.detailsBox->setEnabled(true);
267 ui.renameNetwork->setEnabled(true);
268 ui.deleteNetwork->setEnabled(true);
270 /* button disabled for now
271 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
272 const Network *net = id > 0 ? Client::network(id) : 0;
273 ui.connectNow->setEnabled(net);
274 // && (Client::network(id)->connectionState() == Network::Initialized
275 // || Client::network(id)->connectionState() == Network::Disconnected));
277 if(net->connectionState() == Network::Disconnected) {
278 ui.connectNow->setIcon(connectedIcon);
279 ui.connectNow->setText(tr("Connect"));
281 ui.connectNow->setIcon(disconnectedIcon);
282 ui.connectNow->setText(tr("Disconnect"));
285 ui.connectNow->setIcon(QIcon());
286 ui.connectNow->setText(tr("Apply first!"));
290 ui.renameNetwork->setEnabled(false);
291 ui.deleteNetwork->setEnabled(false);
292 //ui.connectNow->setEnabled(false);
293 ui.detailsBox->setEnabled(false);
296 if (ui.serverList->selectedItems().count()) {
297 ui.editServer->setEnabled(true);
298 ui.deleteServer->setEnabled(true);
299 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
300 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
303 ui.editServer->setEnabled(false);
304 ui.deleteServer->setEnabled(false);
305 ui.upServer->setEnabled(false);
306 ui.downServer->setEnabled(false);
311 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
313 if (!item && !(item = networkItem(id))) return;
314 const Network *net = Client::network(id);
315 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
316 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
317 if (net && net->connectionState() == Network::Initialized) {
318 item->setIcon(connectedIcon);
320 else if (net && net->connectionState() != Network::Disconnected) {
321 item->setIcon(connectingIcon);
324 item->setIcon(disconnectedIcon);
328 // check if we already have another net of this name in the list, and replace it
329 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
331 foreach(QListWidgetItem *i, items) {
332 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
333 if (oldid > 0) continue; // only locally created nets should be replaced
334 if (oldid == currentId) {
337 ui.networkList->clearSelection();
339 int row = ui.networkList->row(i);
341 QListWidgetItem *olditem = ui.networkList->takeItem(row);
345 networkInfos.remove(oldid);
349 item->setText(net->networkName());
350 if (select) item->setSelected(true);
355 void NetworksSettingsPage::resetNetworkCapStates()
357 // Set the status to a blank (invalid) network ID, reseting all UI
358 setNetworkCapStates(NetworkId());
362 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
364 const Network *net = Client::network(id);
365 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
366 // Capability negotiation is supported, network exists.
367 // Check if the network is connected. Don't use net->isConnected() as that won't be true
368 // during capability negotiation when capabilities are added and removed.
369 if (net->connectionState() != Network::Disconnected) {
370 // Network exists and is connected, check available capabilities...
372 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
373 setSASLStatus(CapSupportStatus::MaybeSupported);
375 setSASLStatus(CapSupportStatus::MaybeUnsupported);
378 // Add additional capability-dependent interface updates here
380 // Network is disconnected
382 setSASLStatus(CapSupportStatus::Disconnected);
384 // Add additional capability-dependent interface updates here
387 // Capability negotiation is not supported and/or network doesn't exist.
388 // Don't assume anything and reset all capability-dependent interface elements to neutral.
390 setSASLStatus(CapSupportStatus::Unknown);
392 // Add additional capability-dependent interface updates here
397 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
399 this->setEnabled(state);
410 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
412 const Identity *identity = Client::identity(id);
413 connect(identity, &SyncableObject::updatedRemotely, this, &NetworksSettingsPage::clientIdentityUpdated);
415 QString name = identity->identityName();
416 for (int j = 0; j < ui.identityList->count(); j++) {
417 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
418 ui.identityList->insertItem(j, name, id.toInt());
424 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
429 void NetworksSettingsPage::clientIdentityUpdated()
431 const auto *identity = qobject_cast<const Identity *>(sender());
433 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
436 int row = ui.identityList->findData(identity->id().toInt());
438 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
441 if (ui.identityList->itemText(row) != identity->identityName()) {
442 ui.identityList->setItemText(row, identity->identityName());
447 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
449 IdentityId defaultId = defaultIdentity();
450 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
451 foreach(NetworkInfo info, networkInfos.values()) {
452 if (info.identity == id) {
453 if (info.networkId == currentId)
454 ui.identityList->setCurrentIndex(0);
455 info.identity = defaultId;
456 networkInfos[info.networkId] = info;
457 if (info.networkId > 0) Client::updateNetwork(info);
460 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
465 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
467 for (int i = 0; i < ui.networkList->count(); i++) {
468 QListWidgetItem *item = ui.networkList->item(i);
469 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
475 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
478 //connect(Client::network(id), &Network::updatedRemotely, this, &NetworksSettingsPage::clientNetworkUpdated);
479 connect(Client::network(id), &Network::configChanged, this, &NetworksSettingsPage::clientNetworkUpdated);
481 connect(Client::network(id), &Network::connectionStateSet, this, &NetworksSettingsPage::networkConnectionStateChanged);
482 connect(Client::network(id), &Network::connectionError, this, &NetworksSettingsPage::networkConnectionError);
484 // Handle capability changes in case a server dis/connects with the settings window open.
485 connect(Client::network(id), &Network::capAdded, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
486 connect(Client::network(id), &Network::capRemoved, this, &NetworksSettingsPage::clientNetworkCapsUpdated);
490 void NetworksSettingsPage::clientNetworkUpdated()
492 const auto *net = qobject_cast<const Network *>(sender());
494 qWarning() << "Update request for unknown network received!";
497 networkInfos[net->networkId()] = net->networkInfo();
498 setItemState(net->networkId());
499 if (net->networkId() == currentId) displayNetwork(net->networkId());
505 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
507 if (!networkInfos.contains(id)) return;
508 if (id == currentId) displayNetwork(0);
509 NetworkInfo info = networkInfos.take(id);
510 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
511 foreach(QListWidgetItem *item, items) {
512 if (item->data(Qt::UserRole).value<NetworkId>() == id)
513 delete ui.networkList->takeItem(ui.networkList->row(item));
520 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
523 const auto *net = qobject_cast<const Network *>(sender());
526 if(net->networkId() == currentId) {
527 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
530 setItemState(net->networkId());
531 if (net->networkId() == currentId) {
532 // Network is currently shown. Update the capability-dependent UI in case capabilities have
534 setNetworkCapStates(currentId);
540 void NetworksSettingsPage::networkConnectionError(const QString &)
545 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
547 NetworkInfo info = Client::network(id)->networkInfo();
548 networkInfos[id] = info;
549 return insertNetwork(info);
553 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
555 QListWidgetItem *item = nullptr;
556 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
557 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
559 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
560 // -> then we can be sure that this is the core-side replacement for the net we created
561 foreach(QListWidgetItem *i, items) {
562 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
563 if (id < 0) { item = i; break; }
565 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
567 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
568 setItemState(info.networkId, item);
574 // Called when selecting 'Configure' from the buffer list
575 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
577 QListWidgetItem *item = networkItem(netId);
578 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
582 void NetworksSettingsPage::displayNetwork(NetworkId id)
584 _ignoreWidgetChanges = true;
586 NetworkInfo info = networkInfos[id];
589 // this is only needed when the core supports SASL EXTERNAL
590 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
592 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
595 _cid = new CertIdentity(*Client::identity(info.identity), this);
596 _cid->enableEditSsl(true);
597 connect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
601 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
602 ui.serverList->clear();
603 foreach(Network::Server server, info.serverList) {
604 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
606 item->setIcon(icon::get("document-encrypt"));
607 ui.serverList->addItem(item);
610 //ui.randomServer->setChecked(info.useRandomServer);
611 // Update the capability-dependent UI in case capabilities have changed.
612 setNetworkCapStates(id);
613 ui.performEdit->setPlainText(info.perform.join("\n"));
614 ui.autoIdentify->setChecked(info.useAutoIdentify);
615 ui.autoIdentifyService->setText(info.autoIdentifyService);
616 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
617 ui.sasl->setChecked(info.useSasl);
618 ui.saslAccount->setText(info.saslAccount);
619 ui.saslPassword->setText(info.saslPassword);
620 if (info.codecForEncoding.isEmpty()) {
621 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
622 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
623 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
624 ui.useCustomEncodings->setChecked(false);
627 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
628 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
629 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
630 ui.useCustomEncodings->setChecked(true);
632 ui.autoReconnect->setChecked(info.useAutoReconnect);
633 ui.reconnectInterval->setValue(info.autoReconnectInterval);
634 ui.reconnectRetries->setValue(info.autoReconnectRetries);
635 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
636 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
637 // Custom rate limiting
638 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
639 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
640 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
641 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
642 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
643 // Convert milliseconds (integer) into seconds (double)
644 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
647 // just clear widgets
650 disconnect(_cid, &CertIdentity::sslSettingsUpdated, this, &NetworksSettingsPage::sslUpdated);
654 ui.identityList->setCurrentIndex(-1);
655 ui.serverList->clear();
656 ui.performEdit->clear();
657 ui.autoIdentifyService->clear();
658 ui.autoIdentifyPassword->clear();
659 ui.saslAccount->clear();
660 ui.saslPassword->clear();
663 _ignoreWidgetChanges = false;
668 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
670 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
671 //info.useRandomServer = ui.randomServer->isChecked();
672 info.perform = ui.performEdit->toPlainText().split("\n");
673 info.useAutoIdentify = ui.autoIdentify->isChecked();
674 info.autoIdentifyService = ui.autoIdentifyService->text();
675 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
676 info.useSasl = ui.sasl->isChecked();
677 info.saslAccount = ui.saslAccount->text();
678 info.saslPassword = ui.saslPassword->text();
679 if (!ui.useCustomEncodings->isChecked()) {
680 info.codecForEncoding.clear();
681 info.codecForDecoding.clear();
682 info.codecForServer.clear();
685 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
686 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
687 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
689 info.useAutoReconnect = ui.autoReconnect->isChecked();
690 info.autoReconnectInterval = ui.reconnectInterval->value();
691 info.autoReconnectRetries = ui.reconnectRetries->value();
692 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
693 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
694 // Custom rate limiting
695 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
696 info.messageRateBurstSize = ui.messageRateBurstSize->value();
697 // Convert seconds (double) into milliseconds (integer)
698 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
699 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
703 void NetworksSettingsPage::clientNetworkCapsUpdated()
705 // Grab the updated network
706 const auto *net = qobject_cast<const Network *>(sender());
708 qWarning() << "Update request for unknown network received!";
711 if (net->networkId() == currentId) {
712 // Network is currently shown. Update the capability-dependent UI in case capabilities have
714 setNetworkCapStates(currentId);
719 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
721 if (_saslStatusSelected != saslStatus) {
722 // Update the cached copy of SASL status used with the Details dialog
723 _saslStatusSelected = saslStatus;
725 // Update the user interface
726 switch (saslStatus) {
727 case CapSupportStatus::Unknown:
728 // There's no capability negotiation or network doesn't exist. Don't assume
730 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
731 tr("Could not check if supported by network")));
732 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
734 case CapSupportStatus::Disconnected:
735 // Disconnected from network, no way to check.
736 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
737 tr("Cannot check if supported when disconnected")));
738 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
740 case CapSupportStatus::MaybeUnsupported:
741 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
742 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
743 tr("Not currently supported by network")));
744 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
746 case CapSupportStatus::MaybeSupported:
747 // The network advertises support for SASL PLAIN. Encourage using it!
748 // Unfortunately we don't know for sure if it's desired or functional.
749 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
750 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
758 void NetworksSettingsPage::sslUpdated()
760 if (_cid && !_cid->sslKey().isNull()) {
761 ui.saslContents->setDisabled(true);
762 ui.saslExtInfo->setHidden(false);
764 ui.saslContents->setDisabled(false);
765 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
766 // state to indicate whether or not it's disabled. To workaround this, keep track of
767 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
769 if (!ui.sasl->isChecked()) {
770 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
771 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
772 // sasl is later checked.
773 ui.sasl->setChecked(true);
774 ui.sasl->setChecked(false);
776 ui.saslExtInfo->setHidden(true);
782 /*** Network list ***/
784 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
786 if (currentId != 0) {
787 saveToNetworkInfo(networkInfos[currentId]);
789 if (ui.networkList->selectedItems().count()) {
790 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
793 ui.serverList->setCurrentRow(0);
802 void NetworksSettingsPage::on_addNetwork_clicked()
804 QStringList existing;
805 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
806 NetworkAddDlg dlg(existing, this);
807 if (dlg.exec() == QDialog::Accepted) {
808 NetworkInfo info = dlg.networkInfo();
809 if (info.networkName.isEmpty())
810 return; // sanity check
813 for (id = 1; id <= networkInfos.count(); id++) {
815 if (!networkInfos.keys().contains(-id.toInt())) break;
819 info.identity = defaultIdentity();
820 networkInfos[id] = info;
821 QListWidgetItem *item = insertNetwork(info);
822 ui.networkList->setCurrentItem(item);
828 void NetworksSettingsPage::on_deleteNetwork_clicked()
830 if (ui.networkList->selectedItems().count()) {
831 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
832 int ret = QMessageBox::question(this, tr("Delete Network?"),
833 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
834 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
835 if (ret == QMessageBox::Yes) {
837 networkInfos.remove(netid);
838 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
839 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
847 void NetworksSettingsPage::on_renameNetwork_clicked()
849 if (!ui.networkList->selectedItems().count()) return;
850 QString old = ui.networkList->selectedItems()[0]->text();
851 QStringList existing;
852 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
853 NetworkEditDlg dlg(old, existing, this);
854 if (dlg.exec() == QDialog::Accepted) {
855 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
856 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
857 networkInfos[netid].networkName = dlg.networkName();
864 void NetworksSettingsPage::on_connectNow_clicked() {
865 if(!ui.networkList->selectedItems().count()) return;
866 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
867 const Network *net = Client::network(id);
869 if(net->connectionState() == Network::Disconnected) net->requestConnect();
870 else net->requestDisconnect();
874 /*** Server list ***/
876 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
882 void NetworksSettingsPage::on_addServer_clicked()
884 if (currentId == 0) return;
885 ServerEditDlg dlg(Network::Server(), this);
886 if (dlg.exec() == QDialog::Accepted) {
887 networkInfos[currentId].serverList.append(dlg.serverData());
888 displayNetwork(currentId);
889 ui.serverList->setCurrentRow(ui.serverList->count()-1);
895 void NetworksSettingsPage::on_editServer_clicked()
897 if (currentId == 0) return;
898 int cur = ui.serverList->currentRow();
899 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
900 if (dlg.exec() == QDialog::Accepted) {
901 networkInfos[currentId].serverList[cur] = dlg.serverData();
902 displayNetwork(currentId);
903 ui.serverList->setCurrentRow(cur);
909 void NetworksSettingsPage::on_deleteServer_clicked()
911 if (currentId == 0) return;
912 int cur = ui.serverList->currentRow();
913 networkInfos[currentId].serverList.removeAt(cur);
914 displayNetwork(currentId);
915 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
920 void NetworksSettingsPage::on_upServer_clicked()
922 int cur = ui.serverList->currentRow();
923 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
924 networkInfos[currentId].serverList.insert(cur-1, server);
925 displayNetwork(currentId);
926 ui.serverList->setCurrentRow(cur-1);
931 void NetworksSettingsPage::on_downServer_clicked()
933 int cur = ui.serverList->currentRow();
934 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
935 networkInfos[currentId].serverList.insert(cur+1, server);
936 displayNetwork(currentId);
937 ui.serverList->setCurrentRow(cur+1);
942 void NetworksSettingsPage::on_editIdentities_clicked()
944 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
949 void NetworksSettingsPage::on_saslStatusDetails_clicked()
951 if (ui.networkList->selectedItems().count()) {
952 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
953 QString &netName = networkInfos[netid].networkName;
955 // If these strings are visible, one of the status messages wasn't detected below.
956 QString saslStatusHeader = "[header unintentionally left blank]";
957 QString saslStatusExplanation = "[explanation unintentionally left blank]";
959 // If true, show a warning icon instead of an information icon
960 bool useWarningIcon = false;
962 // Determine which explanation to show
963 switch (_saslStatusSelected) {
964 case CapSupportStatus::Unknown:
965 saslStatusHeader = tr("Could not check if SASL supported by network");
966 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
967 "be due to unsaved changes or an older Quassel core. You "
968 "can still try using SASL.").arg(netName);
970 case CapSupportStatus::Disconnected:
971 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
972 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
973 "disconnected. Connect to the network, or try using SASL "
974 "anyways.").arg(netName);
976 case CapSupportStatus::MaybeUnsupported:
977 saslStatusHeader = tr("SASL not currently supported by network");
978 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
979 "However, support might be added later on.").arg(netName);
980 useWarningIcon = true;
982 case CapSupportStatus::MaybeSupported:
983 saslStatusHeader = tr("SASL supported by network");
984 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
985 "should use SASL instead of NickServ identification."
990 // Process this in advance for reusability below
991 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
992 const QString saslStatusMsgText =
993 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
994 ).arg(saslStatusHeader,
995 saslStatusExplanation,
996 tr("SASL is a standardized way to log in and identify yourself to "
999 if (useWarningIcon) {
1000 // Show as a warning dialog box
1001 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1003 // Show as an information dialog box
1004 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1010 IdentityId NetworksSettingsPage::defaultIdentity() const
1012 IdentityId defaultId = 0;
1013 QList<IdentityId> ids = Client::identityIds();
1014 foreach(IdentityId id, ids) {
1015 if (defaultId == 0 || id < defaultId)
1022 /**************************************************************************
1024 *************************************************************************/
1026 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1029 ui.useSSL->setIcon(icon::get("document-encrypt"));
1031 // Whenever useSSL is toggled, update the port number if not changed from the default
1032 connect(ui.useSSL, &QAbstractButton::toggled, this, &NetworkAddDlg::updateSslPort);
1033 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1034 // If useSSL is later changed to be checked by default, change port's default value, too.
1036 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1037 // Synchronize requiring SSL with the use SSL checkbox
1038 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1039 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1041 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1042 ui.sslVerify->setEnabled(false);
1043 ui.sslVerify->setChecked(false);
1044 // Split up the message to allow re-using translations:
1045 // [Original tool-tip]
1046 // [Bold 'does not support feature' message]
1047 // [Specific version needed and feature details]
1048 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1049 ui.sslVerify->toolTip(),
1050 tr("Your Quassel core does not support this feature"),
1051 tr("You need a Quassel core v0.13.0 or newer in order to "
1052 "verify connection security.")));
1055 // read preset networks
1056 QStringList networks = PresetNetworks::names();
1057 foreach(QString s, existing)
1058 networks.removeAll(s);
1059 if (networks.count())
1060 ui.presetList->addItems(networks);
1062 ui.useManual->setChecked(true);
1063 ui.usePreset->setEnabled(false);
1065 connect(ui.networkName, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1066 connect(ui.serverAddress, &QLineEdit::textChanged, this, &NetworkAddDlg::setButtonStates);
1067 connect(ui.usePreset, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1068 connect(ui.useManual, &QRadioButton::toggled, this, &NetworkAddDlg::setButtonStates);
1073 NetworkInfo NetworkAddDlg::networkInfo() const
1075 if (ui.useManual->isChecked()) {
1077 info.networkName = ui.networkName->text().trimmed();
1078 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1079 ui.serverPassword->text(), ui.useSSL->isChecked(),
1080 ui.sslVerify->isChecked());
1084 return PresetNetworks::networkInfo(ui.presetList->currentText());
1088 void NetworkAddDlg::setButtonStates()
1091 if (ui.usePreset->isChecked() && ui.presetList->count())
1093 else if (ui.useManual->isChecked()) {
1094 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1095 && !ui.serverAddress->text().isEmpty();
1097 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1101 void NetworkAddDlg::updateSslPort(bool isChecked)
1103 // "Use encrypted connection" was toggled, check the state...
1104 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1105 // Had been using the plain-text port, use the SSL default
1106 ui.port->setValue(Network::PORT_SSL);
1107 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1108 // Had been using the SSL port, use the plain-text default
1109 ui.port->setValue(Network::PORT_PLAINTEXT);
1114 /**************************************************************************
1116 *************************************************************************/
1118 NetworkEditDlg::NetworkEditDlg(const QString &old, QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1122 if (old.isEmpty()) {
1124 setWindowTitle(tr("Add Network"));
1125 on_networkEdit_textChanged(""); // disable ok button
1127 else ui.networkEdit->setText(old);
1131 QString NetworkEditDlg::networkName() const
1133 return ui.networkEdit->text().trimmed();
1137 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1139 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1143 /**************************************************************************
1145 *************************************************************************/
1146 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1149 ui.useSSL->setIcon(icon::get("document-encrypt"));
1150 ui.host->setText(server.host);
1151 ui.host->setFocus();
1152 ui.port->setValue(server.port);
1153 ui.password->setText(server.password);
1154 ui.useSSL->setChecked(server.useSsl);
1155 ui.sslVerify->setChecked(server.sslVerify);
1156 ui.sslVersion->setCurrentIndex(server.sslVersion);
1157 ui.useProxy->setChecked(server.useProxy);
1158 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1159 ui.proxyHost->setText(server.proxyHost);
1160 ui.proxyPort->setValue(server.proxyPort);
1161 ui.proxyUsername->setText(server.proxyUser);
1162 ui.proxyPassword->setText(server.proxyPass);
1164 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1165 // only if the core won't use autonegotiation to determine the best
1166 // protocol. When autonegotiation was introduced, it would have been
1167 // a good idea to use the CoreFeatures enum to accomplish this.
1168 // However, since multiple versions have been released since then, that
1169 // is no longer possible. Instead, we rely on the fact that the
1170 // Datastream protocol was introduced in the same version (0.10) as SSL
1171 // autonegotiation. Because of that, we can display the dropdown only
1172 // if the Legacy protocol is in use. If any other RemotePeer protocol
1173 // is in use, that means a newer protocol is in use and therefore the
1174 // core will use autonegotiation.
1175 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1177 ui.sslVersion->hide();
1180 // Whenever useSSL is toggled, update the port number if not changed from the default
1181 connect(ui.useSSL, &QAbstractButton::toggled, this, &ServerEditDlg::updateSslPort);
1182 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1183 // If useSSL is later changed to be checked by default, change port's default value, too.
1185 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1186 // Synchronize requiring SSL with the use SSL checkbox
1187 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1188 connect(ui.useSSL, &QAbstractButton::toggled, ui.sslVerify, &QWidget::setEnabled);
1190 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1191 ui.sslVerify->setEnabled(false);
1192 ui.sslVerify->setChecked(false);
1193 // Split up the message to allow re-using translations:
1194 // [Original tool-tip]
1195 // [Bold 'does not support feature' message]
1196 // [Specific version needed and feature details]
1197 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1198 ui.sslVerify->toolTip(),
1199 tr("Your Quassel core does not support this feature"),
1200 tr("You need a Quassel core v0.13.0 or newer in order to "
1201 "verify connection security.")));
1204 on_host_textChanged();
1208 Network::Server ServerEditDlg::serverData() const
1210 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1211 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1212 server.sslVersion = ui.sslVersion->currentIndex();
1213 server.useProxy = ui.useProxy->isChecked();
1214 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1215 server.proxyHost = ui.proxyHost->text();
1216 server.proxyPort = ui.proxyPort->value();
1217 server.proxyUser = ui.proxyUsername->text();
1218 server.proxyPass = ui.proxyPassword->text();
1223 void ServerEditDlg::on_host_textChanged()
1225 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1229 void ServerEditDlg::updateSslPort(bool isChecked)
1231 // "Use encrypted connection" was toggled, check the state...
1232 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1233 // Had been using the plain-text port, use the SSL default
1234 ui.port->setValue(Network::PORT_SSL);
1235 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1236 // Had been using the SSL port, use the plain-text default
1237 ui.port->setValue(Network::PORT_PLAINTEXT);
1242 /**************************************************************************
1244 *************************************************************************/
1246 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1250 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1253 ui.progressBar->setMaximum(numevents);
1254 ui.progressBar->setValue(0);
1256 connect(Client::instance(), &Client::networkCreated, this, &SaveNetworksDlg::clientEvent);
1257 connect(Client::instance(), &Client::networkRemoved, this, &SaveNetworksDlg::clientEvent);
1259 foreach(NetworkId id, toRemove) {
1260 Client::removeNetwork(id);
1262 foreach(NetworkInfo info, toCreate) {
1263 Client::createNetwork(info);
1265 foreach(NetworkInfo info, toUpdate) {
1266 const Network *net = Client::network(info.networkId);
1268 qWarning() << "Invalid client network!";
1272 // FIXME this only checks for one changed item rather than all!
1273 connect(net, &SyncableObject::updatedRemotely, this, &SaveNetworksDlg::clientEvent);
1274 Client::updateNetwork(info);
1278 qWarning() << "Sync dialog called without stuff to change!";
1284 void SaveNetworksDlg::clientEvent()
1286 ui.progressBar->setValue(++rcvevents);
1287 if (rcvevents >= numevents) accept();