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(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
89 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
90 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
91 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
92 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
94 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
95 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
96 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
97 connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98 connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
99 connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
100 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
101 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
102 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
103 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
104 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
105 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
106 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
107 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
108 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
109 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
110 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
111 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(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, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
116 connect(ui.messageRateBurstSize, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
117 connect(ui.messageRateDelay, SIGNAL(valueChanged(double)), this, SLOT(widgetHasChanged()));
118 connect(ui.unlimitedMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
120 // Add additional widgets here
121 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
122 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
124 foreach(IdentityId id, Client::identityIds()) {
125 clientIdentityAdded(id);
130 void NetworksSettingsPage::save()
133 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
135 QList<NetworkInfo> toCreate, toUpdate;
136 QList<NetworkId> toRemove;
137 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
138 while (i != networkInfos.end()) {
139 NetworkId id = (*i).networkId;
142 //if(id == currentId) currentId = 0;
143 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
144 //if(items.count()) {
145 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
148 //i = networkInfos.erase(i);
152 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
158 foreach(NetworkId id, Client::networkIds()) {
159 if (!networkInfos.contains(id)) toRemove.append(id);
161 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
162 int ret = dlg.exec();
163 if (ret == QDialog::Rejected) {
164 // canceled -> reload everything to be safe
167 setChangedState(false);
172 void NetworksSettingsPage::load()
176 // Handle UI dependent on core feature flags here
177 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
178 // Custom rate limiting supported, allow toggling
179 ui.useCustomMessageRate->setEnabled(true);
180 // Reset tooltip to default.
181 ui.useCustomMessageRate->setToolTip(QString("%1").arg(
182 tr("<p>Override default message rate limiting.</p>"
183 "<p><b>Setting limits too low may get you disconnected"
184 " from the server!</b></p>")));
185 // If changed, update the message below!
187 // Custom rate limiting not supported, disallow toggling
188 ui.useCustomMessageRate->setEnabled(false);
189 // Split up the message to allow re-using translations:
190 // [Original tool-tip]
191 // [Bold 'does not support feature' message]
192 // [Specific version needed and feature details]
193 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
194 tr("<p>Override default message rate limiting.</p>"
195 "<p><b>Setting limits too low may get you disconnected"
196 " from the server!</b></p>"),
197 tr("Your Quassel core does not support this feature"),
198 tr("You need a Quassel core v0.13.0 or newer in order to "
199 "modify message rate limits.")));
203 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
204 // backlog from the core.
208 // Reset network capability status in case no valid networks get selected (a rare situation)
209 resetNetworkCapStates();
211 foreach(NetworkId netid, Client::networkIds()) {
212 clientNetworkAdded(netid);
214 ui.networkList->setCurrentRow(0);
216 setChangedState(false);
220 void NetworksSettingsPage::reset()
223 ui.networkList->clear();
224 networkInfos.clear();
228 bool NetworksSettingsPage::aboutToSave()
230 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
232 foreach(NetworkInfo info, networkInfos.values()) {
233 if (!info.serverList.count()) errors.append(1);
235 if (!errors.count()) return true;
236 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
237 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
238 error += tr("</ul>");
239 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
244 void NetworksSettingsPage::widgetHasChanged()
246 if (_ignoreWidgetChanges) return;
247 bool changed = testHasChanged();
248 if (changed != hasChanged()) setChangedState(changed);
252 bool NetworksSettingsPage::testHasChanged()
254 if (currentId != 0) {
255 saveToNetworkInfo(networkInfos[currentId]);
257 if (Client::networkIds().count() != networkInfos.count()) return true;
258 foreach(NetworkId id, networkInfos.keys()) {
259 if (id < 0) return true;
260 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
266 void NetworksSettingsPage::setWidgetStates()
269 if (ui.networkList->selectedItems().count()) {
270 ui.detailsBox->setEnabled(true);
271 ui.renameNetwork->setEnabled(true);
272 ui.deleteNetwork->setEnabled(true);
274 /* button disabled for now
275 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
276 const Network *net = id > 0 ? Client::network(id) : 0;
277 ui.connectNow->setEnabled(net);
278 // && (Client::network(id)->connectionState() == Network::Initialized
279 // || Client::network(id)->connectionState() == Network::Disconnected));
281 if(net->connectionState() == Network::Disconnected) {
282 ui.connectNow->setIcon(connectedIcon);
283 ui.connectNow->setText(tr("Connect"));
285 ui.connectNow->setIcon(disconnectedIcon);
286 ui.connectNow->setText(tr("Disconnect"));
289 ui.connectNow->setIcon(QIcon());
290 ui.connectNow->setText(tr("Apply first!"));
294 ui.renameNetwork->setEnabled(false);
295 ui.deleteNetwork->setEnabled(false);
296 //ui.connectNow->setEnabled(false);
297 ui.detailsBox->setEnabled(false);
300 if (ui.serverList->selectedItems().count()) {
301 ui.editServer->setEnabled(true);
302 ui.deleteServer->setEnabled(true);
303 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
304 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
307 ui.editServer->setEnabled(false);
308 ui.deleteServer->setEnabled(false);
309 ui.upServer->setEnabled(false);
310 ui.downServer->setEnabled(false);
315 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
317 if (!item && !(item = networkItem(id))) return;
318 const Network *net = Client::network(id);
319 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
320 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
321 if (net && net->connectionState() == Network::Initialized) {
322 item->setIcon(connectedIcon);
324 else if (net && net->connectionState() != Network::Disconnected) {
325 item->setIcon(connectingIcon);
328 item->setIcon(disconnectedIcon);
332 // check if we already have another net of this name in the list, and replace it
333 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
335 foreach(QListWidgetItem *i, items) {
336 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
337 if (oldid > 0) continue; // only locally created nets should be replaced
338 if (oldid == currentId) {
341 ui.networkList->clearSelection();
343 int row = ui.networkList->row(i);
345 QListWidgetItem *olditem = ui.networkList->takeItem(row);
349 networkInfos.remove(oldid);
353 item->setText(net->networkName());
354 if (select) item->setSelected(true);
359 void NetworksSettingsPage::resetNetworkCapStates()
361 // Set the status to a blank (invalid) network ID, reseting all UI
362 setNetworkCapStates(NetworkId());
366 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
368 const Network *net = Client::network(id);
369 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
370 // Capability negotiation is supported, network exists.
371 // Check if the network is connected. Don't use net->isConnected() as that won't be true
372 // during capability negotiation when capabilities are added and removed.
373 if (net->connectionState() != Network::Disconnected) {
374 // Network exists and is connected, check available capabilities...
376 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
377 setSASLStatus(CapSupportStatus::MaybeSupported);
379 setSASLStatus(CapSupportStatus::MaybeUnsupported);
382 // Add additional capability-dependent interface updates here
384 // Network is disconnected
386 setSASLStatus(CapSupportStatus::Disconnected);
388 // Add additional capability-dependent interface updates here
391 // Capability negotiation is not supported and/or network doesn't exist.
392 // Don't assume anything and reset all capability-dependent interface elements to neutral.
394 setSASLStatus(CapSupportStatus::Unknown);
396 // Add additional capability-dependent interface updates here
401 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
403 this->setEnabled(state);
414 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
416 const Identity *identity = Client::identity(id);
417 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
419 QString name = identity->identityName();
420 for (int j = 0; j < ui.identityList->count(); j++) {
421 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
422 ui.identityList->insertItem(j, name, id.toInt());
428 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
433 void NetworksSettingsPage::clientIdentityUpdated()
435 const Identity *identity = qobject_cast<const Identity *>(sender());
437 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
440 int row = ui.identityList->findData(identity->id().toInt());
442 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
445 if (ui.identityList->itemText(row) != identity->identityName()) {
446 ui.identityList->setItemText(row, identity->identityName());
451 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
453 IdentityId defaultId = defaultIdentity();
454 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
455 foreach(NetworkInfo info, networkInfos.values()) {
456 if (info.identity == id) {
457 if (info.networkId == currentId)
458 ui.identityList->setCurrentIndex(0);
459 info.identity = defaultId;
460 networkInfos[info.networkId] = info;
461 if (info.networkId > 0) Client::updateNetwork(info);
464 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
469 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
471 for (int i = 0; i < ui.networkList->count(); i++) {
472 QListWidgetItem *item = ui.networkList->item(i);
473 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
479 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
482 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
483 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
485 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
486 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
488 // Handle capability changes in case a server dis/connects with the settings window open.
489 connect(Client::network(id), SIGNAL(capAdded(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
490 connect(Client::network(id), SIGNAL(capRemoved(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
494 void NetworksSettingsPage::clientNetworkUpdated()
496 const Network *net = qobject_cast<const Network *>(sender());
498 qWarning() << "Update request for unknown network received!";
501 networkInfos[net->networkId()] = net->networkInfo();
502 setItemState(net->networkId());
503 if (net->networkId() == currentId) displayNetwork(net->networkId());
509 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
511 if (!networkInfos.contains(id)) return;
512 if (id == currentId) displayNetwork(0);
513 NetworkInfo info = networkInfos.take(id);
514 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
515 foreach(QListWidgetItem *item, items) {
516 if (item->data(Qt::UserRole).value<NetworkId>() == id)
517 delete ui.networkList->takeItem(ui.networkList->row(item));
524 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
527 const Network *net = qobject_cast<const Network *>(sender());
530 if(net->networkId() == currentId) {
531 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
534 setItemState(net->networkId());
535 if (net->networkId() == currentId) {
536 // Network is currently shown. Update the capability-dependent UI in case capabilities have
538 setNetworkCapStates(currentId);
544 void NetworksSettingsPage::networkConnectionError(const QString &)
549 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
551 NetworkInfo info = Client::network(id)->networkInfo();
552 networkInfos[id] = info;
553 return insertNetwork(info);
557 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
559 QListWidgetItem *item = nullptr;
560 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
561 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
563 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
564 // -> then we can be sure that this is the core-side replacement for the net we created
565 foreach(QListWidgetItem *i, items) {
566 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
567 if (id < 0) { item = i; break; }
569 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
571 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
572 setItemState(info.networkId, item);
578 // Called when selecting 'Configure' from the buffer list
579 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
581 QListWidgetItem *item = networkItem(netId);
582 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
586 void NetworksSettingsPage::displayNetwork(NetworkId id)
588 _ignoreWidgetChanges = true;
590 NetworkInfo info = networkInfos[id];
593 // this is only needed when the core supports SASL EXTERNAL
594 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
596 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
599 _cid = new CertIdentity(*Client::identity(info.identity), this);
600 _cid->enableEditSsl(true);
601 connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
605 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
606 ui.serverList->clear();
607 foreach(Network::Server server, info.serverList) {
608 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
610 item->setIcon(icon::get("document-encrypt"));
611 ui.serverList->addItem(item);
614 //ui.randomServer->setChecked(info.useRandomServer);
615 // Update the capability-dependent UI in case capabilities have changed.
616 setNetworkCapStates(id);
617 ui.performEdit->setPlainText(info.perform.join("\n"));
618 ui.autoIdentify->setChecked(info.useAutoIdentify);
619 ui.autoIdentifyService->setText(info.autoIdentifyService);
620 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
621 ui.sasl->setChecked(info.useSasl);
622 ui.saslAccount->setText(info.saslAccount);
623 ui.saslPassword->setText(info.saslPassword);
624 if (info.codecForEncoding.isEmpty()) {
625 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
626 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
627 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
628 ui.useCustomEncodings->setChecked(false);
631 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
632 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
633 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
634 ui.useCustomEncodings->setChecked(true);
636 ui.autoReconnect->setChecked(info.useAutoReconnect);
637 ui.reconnectInterval->setValue(info.autoReconnectInterval);
638 ui.reconnectRetries->setValue(info.autoReconnectRetries);
639 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
640 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
641 // Custom rate limiting
642 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
643 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
644 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
645 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
646 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
647 // Convert milliseconds (integer) into seconds (double)
648 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
651 // just clear widgets
654 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
658 ui.identityList->setCurrentIndex(-1);
659 ui.serverList->clear();
660 ui.performEdit->clear();
661 ui.autoIdentifyService->clear();
662 ui.autoIdentifyPassword->clear();
663 ui.saslAccount->clear();
664 ui.saslPassword->clear();
667 _ignoreWidgetChanges = false;
672 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
674 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
675 //info.useRandomServer = ui.randomServer->isChecked();
676 info.perform = ui.performEdit->toPlainText().split("\n");
677 info.useAutoIdentify = ui.autoIdentify->isChecked();
678 info.autoIdentifyService = ui.autoIdentifyService->text();
679 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
680 info.useSasl = ui.sasl->isChecked();
681 info.saslAccount = ui.saslAccount->text();
682 info.saslPassword = ui.saslPassword->text();
683 if (!ui.useCustomEncodings->isChecked()) {
684 info.codecForEncoding.clear();
685 info.codecForDecoding.clear();
686 info.codecForServer.clear();
689 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
690 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
691 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
693 info.useAutoReconnect = ui.autoReconnect->isChecked();
694 info.autoReconnectInterval = ui.reconnectInterval->value();
695 info.autoReconnectRetries = ui.reconnectRetries->value();
696 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
697 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
698 // Custom rate limiting
699 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
700 info.messageRateBurstSize = ui.messageRateBurstSize->value();
701 // Convert seconds (double) into milliseconds (integer)
702 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
703 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
707 void NetworksSettingsPage::clientNetworkCapsUpdated()
709 // Grab the updated network
710 const Network *net = qobject_cast<const Network *>(sender());
712 qWarning() << "Update request for unknown network received!";
715 if (net->networkId() == currentId) {
716 // Network is currently shown. Update the capability-dependent UI in case capabilities have
718 setNetworkCapStates(currentId);
723 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
725 if (_saslStatusSelected != saslStatus) {
726 // Update the cached copy of SASL status used with the Details dialog
727 _saslStatusSelected = saslStatus;
729 // Update the user interface
730 switch (saslStatus) {
731 case CapSupportStatus::Unknown:
732 // There's no capability negotiation or network doesn't exist. Don't assume
734 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
735 tr("Could not check if supported by network")));
736 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
738 case CapSupportStatus::Disconnected:
739 // Disconnected from network, no way to check.
740 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
741 tr("Cannot check if supported when disconnected")));
742 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
744 case CapSupportStatus::MaybeUnsupported:
745 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
746 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
747 tr("Not currently supported by network")));
748 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
750 case CapSupportStatus::MaybeSupported:
751 // The network advertises support for SASL PLAIN. Encourage using it!
752 // Unfortunately we don't know for sure if it's desired or functional.
753 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
754 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
762 void NetworksSettingsPage::sslUpdated()
764 if (_cid && !_cid->sslKey().isNull()) {
765 ui.saslContents->setDisabled(true);
766 ui.saslExtInfo->setHidden(false);
768 ui.saslContents->setDisabled(false);
769 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
770 // state to indicate whether or not it's disabled. To workaround this, keep track of
771 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
773 if (!ui.sasl->isChecked()) {
774 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
775 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
776 // sasl is later checked.
777 ui.sasl->setChecked(true);
778 ui.sasl->setChecked(false);
780 ui.saslExtInfo->setHidden(true);
786 /*** Network list ***/
788 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
790 if (currentId != 0) {
791 saveToNetworkInfo(networkInfos[currentId]);
793 if (ui.networkList->selectedItems().count()) {
794 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
797 ui.serverList->setCurrentRow(0);
806 void NetworksSettingsPage::on_addNetwork_clicked()
808 QStringList existing;
809 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
810 NetworkAddDlg dlg(existing, this);
811 if (dlg.exec() == QDialog::Accepted) {
812 NetworkInfo info = dlg.networkInfo();
813 if (info.networkName.isEmpty())
814 return; // sanity check
817 for (id = 1; id <= networkInfos.count(); id++) {
819 if (!networkInfos.keys().contains(-id.toInt())) break;
823 info.identity = defaultIdentity();
824 networkInfos[id] = info;
825 QListWidgetItem *item = insertNetwork(info);
826 ui.networkList->setCurrentItem(item);
832 void NetworksSettingsPage::on_deleteNetwork_clicked()
834 if (ui.networkList->selectedItems().count()) {
835 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
836 int ret = QMessageBox::question(this, tr("Delete Network?"),
837 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
838 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
839 if (ret == QMessageBox::Yes) {
841 networkInfos.remove(netid);
842 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
843 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
851 void NetworksSettingsPage::on_renameNetwork_clicked()
853 if (!ui.networkList->selectedItems().count()) return;
854 QString old = ui.networkList->selectedItems()[0]->text();
855 QStringList existing;
856 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
857 NetworkEditDlg dlg(old, existing, this);
858 if (dlg.exec() == QDialog::Accepted) {
859 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
860 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
861 networkInfos[netid].networkName = dlg.networkName();
868 void NetworksSettingsPage::on_connectNow_clicked() {
869 if(!ui.networkList->selectedItems().count()) return;
870 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
871 const Network *net = Client::network(id);
873 if(net->connectionState() == Network::Disconnected) net->requestConnect();
874 else net->requestDisconnect();
878 /*** Server list ***/
880 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
886 void NetworksSettingsPage::on_addServer_clicked()
888 if (currentId == 0) return;
889 ServerEditDlg dlg(Network::Server(), this);
890 if (dlg.exec() == QDialog::Accepted) {
891 networkInfos[currentId].serverList.append(dlg.serverData());
892 displayNetwork(currentId);
893 ui.serverList->setCurrentRow(ui.serverList->count()-1);
899 void NetworksSettingsPage::on_editServer_clicked()
901 if (currentId == 0) return;
902 int cur = ui.serverList->currentRow();
903 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
904 if (dlg.exec() == QDialog::Accepted) {
905 networkInfos[currentId].serverList[cur] = dlg.serverData();
906 displayNetwork(currentId);
907 ui.serverList->setCurrentRow(cur);
913 void NetworksSettingsPage::on_deleteServer_clicked()
915 if (currentId == 0) return;
916 int cur = ui.serverList->currentRow();
917 networkInfos[currentId].serverList.removeAt(cur);
918 displayNetwork(currentId);
919 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
924 void NetworksSettingsPage::on_upServer_clicked()
926 int cur = ui.serverList->currentRow();
927 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
928 networkInfos[currentId].serverList.insert(cur-1, server);
929 displayNetwork(currentId);
930 ui.serverList->setCurrentRow(cur-1);
935 void NetworksSettingsPage::on_downServer_clicked()
937 int cur = ui.serverList->currentRow();
938 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
939 networkInfos[currentId].serverList.insert(cur+1, server);
940 displayNetwork(currentId);
941 ui.serverList->setCurrentRow(cur+1);
946 void NetworksSettingsPage::on_editIdentities_clicked()
948 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
953 void NetworksSettingsPage::on_saslStatusDetails_clicked()
955 if (ui.networkList->selectedItems().count()) {
956 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
957 QString &netName = networkInfos[netid].networkName;
959 // If these strings are visible, one of the status messages wasn't detected below.
960 QString saslStatusHeader = "[header unintentionally left blank]";
961 QString saslStatusExplanation = "[explanation unintentionally left blank]";
963 // If true, show a warning icon instead of an information icon
964 bool useWarningIcon = false;
966 // Determine which explanation to show
967 switch (_saslStatusSelected) {
968 case CapSupportStatus::Unknown:
969 saslStatusHeader = tr("Could not check if SASL supported by network");
970 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
971 "be due to unsaved changes or an older Quassel core. You "
972 "can still try using SASL.").arg(netName);
974 case CapSupportStatus::Disconnected:
975 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
976 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
977 "disconnected. Connect to the network, or try using SASL "
978 "anyways.").arg(netName);
980 case CapSupportStatus::MaybeUnsupported:
981 saslStatusHeader = tr("SASL not currently supported by network");
982 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
983 "However, support might be added later on.").arg(netName);
984 useWarningIcon = true;
986 case CapSupportStatus::MaybeSupported:
987 saslStatusHeader = tr("SASL supported by network");
988 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
989 "should use SASL instead of NickServ identification."
994 // Process this in advance for reusability below
995 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
996 const QString saslStatusMsgText =
997 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
998 ).arg(saslStatusHeader,
999 saslStatusExplanation,
1000 tr("SASL is a standardized way to log in and identify yourself to "
1003 if (useWarningIcon) {
1004 // Show as a warning dialog box
1005 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1007 // Show as an information dialog box
1008 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1014 IdentityId NetworksSettingsPage::defaultIdentity() const
1016 IdentityId defaultId = 0;
1017 QList<IdentityId> ids = Client::identityIds();
1018 foreach(IdentityId id, ids) {
1019 if (defaultId == 0 || id < defaultId)
1026 /**************************************************************************
1028 *************************************************************************/
1030 NetworkAddDlg::NetworkAddDlg(QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1033 ui.useSSL->setIcon(icon::get("document-encrypt"));
1035 // Whenever useSSL is toggled, update the port number if not changed from the default
1036 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1037 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1038 // If useSSL is later changed to be checked by default, change port's default value, too.
1040 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1041 // Synchronize requiring SSL with the use SSL checkbox
1042 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1043 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1045 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1046 ui.sslVerify->setEnabled(false);
1047 ui.sslVerify->setChecked(false);
1048 // Split up the message to allow re-using translations:
1049 // [Original tool-tip]
1050 // [Bold 'does not support feature' message]
1051 // [Specific version needed and feature details]
1052 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1053 ui.sslVerify->toolTip(),
1054 tr("Your Quassel core does not support this feature"),
1055 tr("You need a Quassel core v0.13.0 or newer in order to "
1056 "verify connection security.")));
1059 // read preset networks
1060 QStringList networks = PresetNetworks::names();
1061 foreach(QString s, existing)
1062 networks.removeAll(s);
1063 if (networks.count())
1064 ui.presetList->addItems(networks);
1066 ui.useManual->setChecked(true);
1067 ui.usePreset->setEnabled(false);
1069 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1070 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1071 connect(ui.usePreset, SIGNAL(toggled(bool)), SLOT(setButtonStates()));
1072 connect(ui.useManual, SIGNAL(toggled(bool)), SLOT(setButtonStates()));
1077 NetworkInfo NetworkAddDlg::networkInfo() const
1079 if (ui.useManual->isChecked()) {
1081 info.networkName = ui.networkName->text().trimmed();
1082 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1083 ui.serverPassword->text(), ui.useSSL->isChecked(),
1084 ui.sslVerify->isChecked());
1088 return PresetNetworks::networkInfo(ui.presetList->currentText());
1092 void NetworkAddDlg::setButtonStates()
1095 if (ui.usePreset->isChecked() && ui.presetList->count())
1097 else if (ui.useManual->isChecked()) {
1098 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1099 && !ui.serverAddress->text().isEmpty();
1101 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1105 void NetworkAddDlg::updateSslPort(bool isChecked)
1107 // "Use encrypted connection" was toggled, check the state...
1108 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1109 // Had been using the plain-text port, use the SSL default
1110 ui.port->setValue(Network::PORT_SSL);
1111 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1112 // Had been using the SSL port, use the plain-text default
1113 ui.port->setValue(Network::PORT_PLAINTEXT);
1118 /**************************************************************************
1120 *************************************************************************/
1122 NetworkEditDlg::NetworkEditDlg(const QString &old, QStringList exist, QWidget *parent) : QDialog(parent), existing(std::move(exist))
1126 if (old.isEmpty()) {
1128 setWindowTitle(tr("Add Network"));
1129 on_networkEdit_textChanged(""); // disable ok button
1131 else ui.networkEdit->setText(old);
1135 QString NetworkEditDlg::networkName() const
1137 return ui.networkEdit->text().trimmed();
1141 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1143 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1147 /**************************************************************************
1149 *************************************************************************/
1150 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1153 ui.useSSL->setIcon(icon::get("document-encrypt"));
1154 ui.host->setText(server.host);
1155 ui.host->setFocus();
1156 ui.port->setValue(server.port);
1157 ui.password->setText(server.password);
1158 ui.useSSL->setChecked(server.useSsl);
1159 ui.sslVerify->setChecked(server.sslVerify);
1160 ui.sslVersion->setCurrentIndex(server.sslVersion);
1161 ui.useProxy->setChecked(server.useProxy);
1162 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1163 ui.proxyHost->setText(server.proxyHost);
1164 ui.proxyPort->setValue(server.proxyPort);
1165 ui.proxyUsername->setText(server.proxyUser);
1166 ui.proxyPassword->setText(server.proxyPass);
1168 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1169 // only if the core won't use autonegotiation to determine the best
1170 // protocol. When autonegotiation was introduced, it would have been
1171 // a good idea to use the CoreFeatures enum to accomplish this.
1172 // However, since multiple versions have been released since then, that
1173 // is no longer possible. Instead, we rely on the fact that the
1174 // Datastream protocol was introduced in the same version (0.10) as SSL
1175 // autonegotiation. Because of that, we can display the dropdown only
1176 // if the Legacy protocol is in use. If any other RemotePeer protocol
1177 // is in use, that means a newer protocol is in use and therefore the
1178 // core will use autonegotiation.
1179 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1181 ui.sslVersion->hide();
1184 // Whenever useSSL is toggled, update the port number if not changed from the default
1185 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1186 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1187 // If useSSL is later changed to be checked by default, change port's default value, too.
1189 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1190 // Synchronize requiring SSL with the use SSL checkbox
1191 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1192 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1194 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1195 ui.sslVerify->setEnabled(false);
1196 ui.sslVerify->setChecked(false);
1197 // Split up the message to allow re-using translations:
1198 // [Original tool-tip]
1199 // [Bold 'does not support feature' message]
1200 // [Specific version needed and feature details]
1201 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1202 ui.sslVerify->toolTip(),
1203 tr("Your Quassel core does not support this feature"),
1204 tr("You need a Quassel core v0.13.0 or newer in order to "
1205 "verify connection security.")));
1208 on_host_textChanged();
1212 Network::Server ServerEditDlg::serverData() const
1214 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1215 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1216 server.sslVersion = ui.sslVersion->currentIndex();
1217 server.useProxy = ui.useProxy->isChecked();
1218 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1219 server.proxyHost = ui.proxyHost->text();
1220 server.proxyPort = ui.proxyPort->value();
1221 server.proxyUser = ui.proxyUsername->text();
1222 server.proxyPass = ui.proxyPassword->text();
1227 void ServerEditDlg::on_host_textChanged()
1229 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1233 void ServerEditDlg::updateSslPort(bool isChecked)
1235 // "Use encrypted connection" was toggled, check the state...
1236 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1237 // Had been using the plain-text port, use the SSL default
1238 ui.port->setValue(Network::PORT_SSL);
1239 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1240 // Had been using the SSL port, use the plain-text default
1241 ui.port->setValue(Network::PORT_PLAINTEXT);
1246 /**************************************************************************
1248 *************************************************************************/
1250 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1254 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1257 ui.progressBar->setMaximum(numevents);
1258 ui.progressBar->setValue(0);
1260 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1261 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1263 foreach(NetworkId id, toRemove) {
1264 Client::removeNetwork(id);
1266 foreach(NetworkInfo info, toCreate) {
1267 Client::createNetwork(info);
1269 foreach(NetworkInfo info, toUpdate) {
1270 const Network *net = Client::network(info.networkId);
1272 qWarning() << "Invalid client network!";
1276 // FIXME this only checks for one changed item rather than all!
1277 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1278 Client::updateNetwork(info);
1282 qWarning() << "Sync dialog called without stuff to change!";
1288 void SaveNetworksDlg::clientEvent()
1290 ui.progressBar->setValue(++rcvevents);
1291 if (rcvevents >= numevents) accept();