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>
23 #include <QMessageBox>
26 #include "networkssettingspage.h"
31 #include "presetnetworks.h"
32 #include "settingspagedlg.h"
38 #include "settingspages/identitiessettingspage.h"
40 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
41 : SettingsPage(tr("IRC"), tr("Networks"), parent)
48 // hide SASL options for older cores
49 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslAuthentication))
51 if (!Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal))
52 ui.saslExtInfo->hide();
54 ui.saslExtInfo->hide();
58 ui.renameNetwork->setIcon(QIcon::fromTheme("edit-rename"));
59 ui.addNetwork->setIcon(QIcon::fromTheme("list-add"));
60 ui.deleteNetwork->setIcon(QIcon::fromTheme("edit-delete"));
61 ui.addServer->setIcon(QIcon::fromTheme("list-add"));
62 ui.deleteServer->setIcon(QIcon::fromTheme("edit-delete"));
63 ui.editServer->setIcon(QIcon::fromTheme("configure"));
64 ui.upServer->setIcon(QIcon::fromTheme("go-up"));
65 ui.downServer->setIcon(QIcon::fromTheme("go-down"));
66 ui.editIdentities->setIcon(QIcon::fromTheme("configure"));
68 _ignoreWidgetChanges = false;
70 connectedIcon = QIcon::fromTheme("network-connect");
71 connectingIcon = QIcon::fromTheme("network-wired"); // FIXME network-connecting
72 disconnectedIcon = QIcon::fromTheme("network-disconnect");
75 infoIcon = QIcon::fromTheme("dialog-information");
76 warningIcon = QIcon::fromTheme("dialog-warning");
78 foreach(int mib, QTextCodec::availableMibs()) {
79 QByteArray codec = QTextCodec::codecForMib(mib)->name();
80 ui.sendEncoding->addItem(codec);
81 ui.recvEncoding->addItem(codec);
82 ui.serverEncoding->addItem(codec);
84 ui.sendEncoding->model()->sort(0);
85 ui.recvEncoding->model()->sort(0);
86 ui.serverEncoding->model()->sort(0);
88 setEnabled(Client::isConnected()); // need a core connection!
90 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
91 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
92 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
93 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
94 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
96 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
97 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
99 connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
100 connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
101 connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
102 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
103 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
104 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
105 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
106 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
107 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
108 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
109 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
110 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
111 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
112 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
113 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
115 // Core features can change during a reconnect. Always connect these here, delaying testing for
116 // the core feature flag in load().
117 connect(ui.useCustomMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
118 connect(ui.messageRateBurstSize, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
119 connect(ui.messageRateDelay, SIGNAL(valueChanged(double)), this, SLOT(widgetHasChanged()));
120 connect(ui.unlimitedMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
122 // Add additional widgets here
123 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
124 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
126 foreach(IdentityId id, Client::identityIds()) {
127 clientIdentityAdded(id);
132 void NetworksSettingsPage::save()
135 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
137 QList<NetworkInfo> toCreate, toUpdate;
138 QList<NetworkId> toRemove;
139 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
140 while (i != networkInfos.end()) {
141 NetworkId id = (*i).networkId;
144 //if(id == currentId) currentId = 0;
145 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
146 //if(items.count()) {
147 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
150 //i = networkInfos.erase(i);
154 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
160 foreach(NetworkId id, Client::networkIds()) {
161 if (!networkInfos.contains(id)) toRemove.append(id);
163 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
164 int ret = dlg.exec();
165 if (ret == QDialog::Rejected) {
166 // canceled -> reload everything to be safe
169 setChangedState(false);
174 void NetworksSettingsPage::load()
178 // Handle UI dependent on core feature flags here
179 if (Client::isCoreFeatureEnabled(Quassel::Feature::CustomRateLimits)) {
180 // Custom rate limiting supported, allow toggling
181 ui.useCustomMessageRate->setEnabled(true);
182 // Reset tooltip to default.
183 ui.useCustomMessageRate->setToolTip(QString("%1").arg(
184 tr("<p>Override default message rate limiting.</p>"
185 "<p><b>Setting limits too low may get you disconnected"
186 " from the server!</b></p>")));
187 // If changed, update the message below!
189 // Custom rate limiting not supported, disallow toggling
190 ui.useCustomMessageRate->setEnabled(false);
191 // Split up the message to allow re-using translations:
192 // [Original tool-tip]
193 // [Bold 'does not support feature' message]
194 // [Specific version needed and feature details]
195 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
196 tr("<p>Override default message rate limiting.</p>"
197 "<p><b>Setting limits too low may get you disconnected"
198 " from the server!</b></p>"),
199 tr("Your Quassel core does not support this feature"),
200 tr("You need a Quassel core v0.13.0 or newer in order to "
201 "modify message rate limits.")));
205 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
206 // backlog from the core.
210 foreach(NetworkId netid, Client::networkIds()) {
211 clientNetworkAdded(netid);
213 ui.networkList->setCurrentRow(0);
215 setChangedState(false);
219 void NetworksSettingsPage::reset()
222 ui.networkList->clear();
223 networkInfos.clear();
227 bool NetworksSettingsPage::aboutToSave()
229 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
231 foreach(NetworkInfo info, networkInfos.values()) {
232 if (!info.serverList.count()) errors.append(1);
234 if (!errors.count()) return true;
235 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
236 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
237 error += tr("</ul>");
238 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
243 void NetworksSettingsPage::widgetHasChanged()
245 if (_ignoreWidgetChanges) return;
246 bool changed = testHasChanged();
247 if (changed != hasChanged()) setChangedState(changed);
251 bool NetworksSettingsPage::testHasChanged()
253 if (currentId != 0) {
254 saveToNetworkInfo(networkInfos[currentId]);
256 if (Client::networkIds().count() != networkInfos.count()) return true;
257 foreach(NetworkId id, networkInfos.keys()) {
258 if (id < 0) return true;
259 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
265 void NetworksSettingsPage::setWidgetStates()
268 if (ui.networkList->selectedItems().count()) {
269 ui.detailsBox->setEnabled(true);
270 ui.renameNetwork->setEnabled(true);
271 ui.deleteNetwork->setEnabled(true);
273 /* button disabled for now
274 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
275 const Network *net = id > 0 ? Client::network(id) : 0;
276 ui.connectNow->setEnabled(net);
277 // && (Client::network(id)->connectionState() == Network::Initialized
278 // || Client::network(id)->connectionState() == Network::Disconnected));
280 if(net->connectionState() == Network::Disconnected) {
281 ui.connectNow->setIcon(connectedIcon);
282 ui.connectNow->setText(tr("Connect"));
284 ui.connectNow->setIcon(disconnectedIcon);
285 ui.connectNow->setText(tr("Disconnect"));
288 ui.connectNow->setIcon(QIcon());
289 ui.connectNow->setText(tr("Apply first!"));
293 ui.renameNetwork->setEnabled(false);
294 ui.deleteNetwork->setEnabled(false);
295 //ui.connectNow->setEnabled(false);
296 ui.detailsBox->setEnabled(false);
299 if (ui.serverList->selectedItems().count()) {
300 ui.editServer->setEnabled(true);
301 ui.deleteServer->setEnabled(true);
302 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
303 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
306 ui.editServer->setEnabled(false);
307 ui.deleteServer->setEnabled(false);
308 ui.upServer->setEnabled(false);
309 ui.downServer->setEnabled(false);
314 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
316 if (!item && !(item = networkItem(id))) return;
317 const Network *net = Client::network(id);
318 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
319 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
320 if (net && net->connectionState() == Network::Initialized) {
321 item->setIcon(connectedIcon);
323 else if (net && net->connectionState() != Network::Disconnected) {
324 item->setIcon(connectingIcon);
327 item->setIcon(disconnectedIcon);
331 // check if we already have another net of this name in the list, and replace it
332 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
334 foreach(QListWidgetItem *i, items) {
335 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
336 if (oldid > 0) continue; // only locally created nets should be replaced
337 if (oldid == currentId) {
340 ui.networkList->clearSelection();
342 int row = ui.networkList->row(i);
344 QListWidgetItem *olditem = ui.networkList->takeItem(row);
348 networkInfos.remove(oldid);
352 item->setText(net->networkName());
353 if (select) item->setSelected(true);
358 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
360 const Network *net = Client::network(id);
361 if (Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation) && net) {
362 // Capability negotiation is supported, network exists.
363 // Check if the network is connected. Don't use net->isConnected() as that won't be true
364 // during capability negotiation when capabilities are added and removed.
365 if (net->connectionState() != Network::Disconnected) {
366 // Network exists and is connected, check available capabilities...
368 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
369 setSASLStatus(CapSupportStatus::MaybeSupported);
371 setSASLStatus(CapSupportStatus::MaybeUnsupported);
374 // Add additional capability-dependent interface updates here
376 // Network is disconnected
378 setSASLStatus(CapSupportStatus::Disconnected);
380 // Add additional capability-dependent interface updates here
383 // Capability negotiation is not supported and/or network doesn't exist.
384 // Don't assume anything and reset all capability-dependent interface elements to neutral.
386 setSASLStatus(CapSupportStatus::Unknown);
388 // Add additional capability-dependent interface updates here
393 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
395 this->setEnabled(state);
406 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
408 const Identity *identity = Client::identity(id);
409 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
411 QString name = identity->identityName();
412 for (int j = 0; j < ui.identityList->count(); j++) {
413 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
414 ui.identityList->insertItem(j, name, id.toInt());
420 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
425 void NetworksSettingsPage::clientIdentityUpdated()
427 const Identity *identity = qobject_cast<const Identity *>(sender());
429 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
432 int row = ui.identityList->findData(identity->id().toInt());
434 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
437 if (ui.identityList->itemText(row) != identity->identityName()) {
438 ui.identityList->setItemText(row, identity->identityName());
443 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
445 IdentityId defaultId = defaultIdentity();
446 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
447 foreach(NetworkInfo info, networkInfos.values()) {
448 if (info.identity == id) {
449 if (info.networkId == currentId)
450 ui.identityList->setCurrentIndex(0);
451 info.identity = defaultId;
452 networkInfos[info.networkId] = info;
453 if (info.networkId > 0) Client::updateNetwork(info);
456 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
461 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
463 for (int i = 0; i < ui.networkList->count(); i++) {
464 QListWidgetItem *item = ui.networkList->item(i);
465 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
471 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
474 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
475 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
477 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
478 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
480 // Handle capability changes in case a server dis/connects with the settings window open.
481 connect(Client::network(id), SIGNAL(capAdded(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
482 connect(Client::network(id), SIGNAL(capRemoved(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
486 void NetworksSettingsPage::clientNetworkUpdated()
488 const Network *net = qobject_cast<const Network *>(sender());
490 qWarning() << "Update request for unknown network received!";
493 networkInfos[net->networkId()] = net->networkInfo();
494 setItemState(net->networkId());
495 if (net->networkId() == currentId) displayNetwork(net->networkId());
501 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
503 if (!networkInfos.contains(id)) return;
504 if (id == currentId) displayNetwork(0);
505 NetworkInfo info = networkInfos.take(id);
506 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
507 foreach(QListWidgetItem *item, items) {
508 if (item->data(Qt::UserRole).value<NetworkId>() == id)
509 delete ui.networkList->takeItem(ui.networkList->row(item));
516 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
519 const Network *net = qobject_cast<const Network *>(sender());
522 if(net->networkId() == currentId) {
523 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
526 setItemState(net->networkId());
527 if (net->networkId() == currentId) {
528 // Network is currently shown. Update the capability-dependent UI in case capabilities have
530 setNetworkCapStates(currentId);
536 void NetworksSettingsPage::networkConnectionError(const QString &)
541 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
543 NetworkInfo info = Client::network(id)->networkInfo();
544 networkInfos[id] = info;
545 return insertNetwork(info);
549 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
551 QListWidgetItem *item = 0;
552 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
553 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
555 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
556 // -> then we can be sure that this is the core-side replacement for the net we created
557 foreach(QListWidgetItem *i, items) {
558 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
559 if (id < 0) { item = i; break; }
561 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
563 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
564 setItemState(info.networkId, item);
570 // Called when selecting 'Configure' from the buffer list
571 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
573 QListWidgetItem *item = networkItem(netId);
574 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
578 void NetworksSettingsPage::displayNetwork(NetworkId id)
580 _ignoreWidgetChanges = true;
582 NetworkInfo info = networkInfos[id];
585 // this is only needed when the core supports SASL EXTERNAL
586 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
588 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
591 _cid = new CertIdentity(*Client::identity(info.identity), this);
592 _cid->enableEditSsl(true);
593 connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
597 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
598 ui.serverList->clear();
599 foreach(Network::Server server, info.serverList) {
600 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
602 item->setIcon(QIcon::fromTheme("document-encrypt"));
603 ui.serverList->addItem(item);
606 //ui.randomServer->setChecked(info.useRandomServer);
607 // Update the capability-dependent UI in case capabilities have changed.
608 setNetworkCapStates(id);
609 ui.performEdit->setPlainText(info.perform.join("\n"));
610 ui.autoIdentify->setChecked(info.useAutoIdentify);
611 ui.autoIdentifyService->setText(info.autoIdentifyService);
612 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
613 ui.sasl->setChecked(info.useSasl);
614 ui.saslAccount->setText(info.saslAccount);
615 ui.saslPassword->setText(info.saslPassword);
616 if (info.codecForEncoding.isEmpty()) {
617 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
618 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
619 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
620 ui.useCustomEncodings->setChecked(false);
623 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
624 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
625 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
626 ui.useCustomEncodings->setChecked(true);
628 ui.autoReconnect->setChecked(info.useAutoReconnect);
629 ui.reconnectInterval->setValue(info.autoReconnectInterval);
630 ui.reconnectRetries->setValue(info.autoReconnectRetries);
631 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
632 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
633 // Custom rate limiting
634 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
635 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
636 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
637 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
638 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
639 // Convert milliseconds (integer) into seconds (double)
640 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
643 // just clear widgets
646 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
650 ui.identityList->setCurrentIndex(-1);
651 ui.serverList->clear();
652 ui.performEdit->clear();
653 ui.autoIdentifyService->clear();
654 ui.autoIdentifyPassword->clear();
655 ui.saslAccount->clear();
656 ui.saslPassword->clear();
659 _ignoreWidgetChanges = false;
664 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
666 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
667 //info.useRandomServer = ui.randomServer->isChecked();
668 info.perform = ui.performEdit->toPlainText().split("\n");
669 info.useAutoIdentify = ui.autoIdentify->isChecked();
670 info.autoIdentifyService = ui.autoIdentifyService->text();
671 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
672 info.useSasl = ui.sasl->isChecked();
673 info.saslAccount = ui.saslAccount->text();
674 info.saslPassword = ui.saslPassword->text();
675 if (!ui.useCustomEncodings->isChecked()) {
676 info.codecForEncoding.clear();
677 info.codecForDecoding.clear();
678 info.codecForServer.clear();
681 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
682 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
683 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
685 info.useAutoReconnect = ui.autoReconnect->isChecked();
686 info.autoReconnectInterval = ui.reconnectInterval->value();
687 info.autoReconnectRetries = ui.reconnectRetries->value();
688 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
689 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
690 // Custom rate limiting
691 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
692 info.messageRateBurstSize = ui.messageRateBurstSize->value();
693 // Convert seconds (double) into milliseconds (integer)
694 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
695 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
699 void NetworksSettingsPage::clientNetworkCapsUpdated()
701 // Grab the updated network
702 const Network *net = qobject_cast<const Network *>(sender());
704 qWarning() << "Update request for unknown network received!";
707 if (net->networkId() == currentId) {
708 // Network is currently shown. Update the capability-dependent UI in case capabilities have
710 setNetworkCapStates(currentId);
715 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
717 if (_saslStatusSelected != saslStatus) {
718 // Update the cached copy of SASL status used with the Details dialog
719 _saslStatusSelected = saslStatus;
721 // Update the user interface
722 switch (saslStatus) {
723 case CapSupportStatus::Unknown:
724 // There's no capability negotiation or network doesn't exist. Don't assume
726 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
727 tr("Could not check if supported by network")));
728 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
730 case CapSupportStatus::Disconnected:
731 // Disconnected from network, no way to check.
732 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
733 tr("Cannot check if supported when disconnected")));
734 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
736 case CapSupportStatus::MaybeUnsupported:
737 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
738 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
739 tr("Not currently supported by network")));
740 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
742 case CapSupportStatus::MaybeSupported:
743 // The network advertises support for SASL PLAIN. Encourage using it!
744 // Unfortunately we don't know for sure if it's desired or functional.
745 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
746 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
754 void NetworksSettingsPage::sslUpdated()
756 if (_cid && !_cid->sslKey().isNull()) {
757 ui.saslContents->setDisabled(true);
758 ui.saslExtInfo->setHidden(false);
760 ui.saslContents->setDisabled(false);
761 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
762 // state to indicate whether or not it's disabled. To workaround this, keep track of
763 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
765 if (!ui.sasl->isChecked()) {
766 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
767 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
768 // sasl is later checked.
769 ui.sasl->setChecked(true);
770 ui.sasl->setChecked(false);
772 ui.saslExtInfo->setHidden(true);
778 /*** Network list ***/
780 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
782 if (currentId != 0) {
783 saveToNetworkInfo(networkInfos[currentId]);
785 if (ui.networkList->selectedItems().count()) {
786 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
789 ui.serverList->setCurrentRow(0);
798 void NetworksSettingsPage::on_addNetwork_clicked()
800 QStringList existing;
801 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
802 NetworkAddDlg dlg(existing, this);
803 if (dlg.exec() == QDialog::Accepted) {
804 NetworkInfo info = dlg.networkInfo();
805 if (info.networkName.isEmpty())
806 return; // sanity check
809 for (id = 1; id <= networkInfos.count(); id++) {
811 if (!networkInfos.keys().contains(-id.toInt())) break;
815 info.identity = defaultIdentity();
816 networkInfos[id] = info;
817 QListWidgetItem *item = insertNetwork(info);
818 ui.networkList->setCurrentItem(item);
824 void NetworksSettingsPage::on_deleteNetwork_clicked()
826 if (ui.networkList->selectedItems().count()) {
827 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
828 int ret = QMessageBox::question(this, tr("Delete Network?"),
829 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
830 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
831 if (ret == QMessageBox::Yes) {
833 networkInfos.remove(netid);
834 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
835 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
843 void NetworksSettingsPage::on_renameNetwork_clicked()
845 if (!ui.networkList->selectedItems().count()) return;
846 QString old = ui.networkList->selectedItems()[0]->text();
847 QStringList existing;
848 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
849 NetworkEditDlg dlg(old, existing, this);
850 if (dlg.exec() == QDialog::Accepted) {
851 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
852 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
853 networkInfos[netid].networkName = dlg.networkName();
860 void NetworksSettingsPage::on_connectNow_clicked() {
861 if(!ui.networkList->selectedItems().count()) return;
862 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
863 const Network *net = Client::network(id);
865 if(net->connectionState() == Network::Disconnected) net->requestConnect();
866 else net->requestDisconnect();
870 /*** Server list ***/
872 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
878 void NetworksSettingsPage::on_addServer_clicked()
880 if (currentId == 0) return;
881 ServerEditDlg dlg(Network::Server(), this);
882 if (dlg.exec() == QDialog::Accepted) {
883 networkInfos[currentId].serverList.append(dlg.serverData());
884 displayNetwork(currentId);
885 ui.serverList->setCurrentRow(ui.serverList->count()-1);
891 void NetworksSettingsPage::on_editServer_clicked()
893 if (currentId == 0) return;
894 int cur = ui.serverList->currentRow();
895 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
896 if (dlg.exec() == QDialog::Accepted) {
897 networkInfos[currentId].serverList[cur] = dlg.serverData();
898 displayNetwork(currentId);
899 ui.serverList->setCurrentRow(cur);
905 void NetworksSettingsPage::on_deleteServer_clicked()
907 if (currentId == 0) return;
908 int cur = ui.serverList->currentRow();
909 networkInfos[currentId].serverList.removeAt(cur);
910 displayNetwork(currentId);
911 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
916 void NetworksSettingsPage::on_upServer_clicked()
918 int cur = ui.serverList->currentRow();
919 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
920 networkInfos[currentId].serverList.insert(cur-1, server);
921 displayNetwork(currentId);
922 ui.serverList->setCurrentRow(cur-1);
927 void NetworksSettingsPage::on_downServer_clicked()
929 int cur = ui.serverList->currentRow();
930 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
931 networkInfos[currentId].serverList.insert(cur+1, server);
932 displayNetwork(currentId);
933 ui.serverList->setCurrentRow(cur+1);
938 void NetworksSettingsPage::on_editIdentities_clicked()
940 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
945 void NetworksSettingsPage::on_saslStatusDetails_clicked()
947 if (ui.networkList->selectedItems().count()) {
948 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
949 QString &netName = networkInfos[netid].networkName;
951 // If these strings are visible, one of the status messages wasn't detected below.
952 QString saslStatusHeader = "[header unintentionally left blank]";
953 QString saslStatusExplanation = "[explanation unintentionally left blank]";
955 // If true, show a warning icon instead of an information icon
956 bool useWarningIcon = false;
958 // Determine which explanation to show
959 switch (_saslStatusSelected) {
960 case CapSupportStatus::Unknown:
961 saslStatusHeader = tr("Could not check if SASL supported by network");
962 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
963 "be due to unsaved changes or an older Quassel core. You "
964 "can still try using SASL.").arg(netName);
966 case CapSupportStatus::Disconnected:
967 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
968 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
969 "disconnected. Connect to the network, or try using SASL "
970 "anyways.").arg(netName);
972 case CapSupportStatus::MaybeUnsupported:
973 saslStatusHeader = tr("SASL not currently supported by network");
974 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
975 "However, support might be added later on.").arg(netName);
976 useWarningIcon = true;
978 case CapSupportStatus::MaybeSupported:
979 saslStatusHeader = tr("SASL supported by network");
980 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
981 "should use SASL instead of NickServ identification."
986 // Process this in advance for reusability below
987 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
988 const QString saslStatusMsgText =
989 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
990 ).arg(saslStatusHeader,
991 saslStatusExplanation,
992 tr("SASL is a standardized way to log in and identify yourself to "
995 if (useWarningIcon) {
996 // Show as a warning dialog box
997 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
999 // Show as an information dialog box
1000 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1006 IdentityId NetworksSettingsPage::defaultIdentity() const
1008 IdentityId defaultId = 0;
1009 QList<IdentityId> ids = Client::identityIds();
1010 foreach(IdentityId id, ids) {
1011 if (defaultId == 0 || id < defaultId)
1018 /**************************************************************************
1020 *************************************************************************/
1022 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
1025 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
1027 // Whenever useSSL is toggled, update the port number if not changed from the default
1028 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1029 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1030 // If useSSL is later changed to be checked by default, change port's default value, too.
1032 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1033 // Synchronize requiring SSL with the use SSL checkbox
1034 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1035 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1037 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1038 ui.sslVerify->setEnabled(false);
1039 ui.sslVerify->setChecked(false);
1040 // Split up the message to allow re-using translations:
1041 // [Original tool-tip]
1042 // [Bold 'does not support feature' message]
1043 // [Specific version needed and feature details]
1044 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1045 ui.sslVerify->toolTip(),
1046 tr("Your Quassel core does not support this feature"),
1047 tr("You need a Quassel core v0.13.0 or newer in order to "
1048 "verify connection security.")));
1051 // read preset networks
1052 QStringList networks = PresetNetworks::names();
1053 foreach(QString s, existing)
1054 networks.removeAll(s);
1055 if (networks.count())
1056 ui.presetList->addItems(networks);
1058 ui.useManual->setChecked(true);
1059 ui.usePreset->setEnabled(false);
1061 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1062 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1067 NetworkInfo NetworkAddDlg::networkInfo() const
1069 if (ui.useManual->isChecked()) {
1071 info.networkName = ui.networkName->text().trimmed();
1072 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1073 ui.serverPassword->text(), ui.useSSL->isChecked(),
1074 ui.sslVerify->isChecked());
1078 return PresetNetworks::networkInfo(ui.presetList->currentText());
1082 void NetworkAddDlg::setButtonStates()
1085 if (ui.usePreset->isChecked() && ui.presetList->count())
1087 else if (ui.useManual->isChecked()) {
1088 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1089 && !ui.serverAddress->text().isEmpty();
1091 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1095 void NetworkAddDlg::updateSslPort(bool isChecked)
1097 // "Use encrypted connection" was toggled, check the state...
1098 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1099 // Had been using the plain-text port, use the SSL default
1100 ui.port->setValue(Network::PORT_SSL);
1101 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1102 // Had been using the SSL port, use the plain-text default
1103 ui.port->setValue(Network::PORT_PLAINTEXT);
1108 /**************************************************************************
1110 *************************************************************************/
1112 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
1116 if (old.isEmpty()) {
1118 setWindowTitle(tr("Add Network"));
1119 on_networkEdit_textChanged(""); // disable ok button
1121 else ui.networkEdit->setText(old);
1125 QString NetworkEditDlg::networkName() const
1127 return ui.networkEdit->text().trimmed();
1131 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1133 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1137 /**************************************************************************
1139 *************************************************************************/
1140 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1143 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
1144 ui.host->setText(server.host);
1145 ui.host->setFocus();
1146 ui.port->setValue(server.port);
1147 ui.password->setText(server.password);
1148 ui.useSSL->setChecked(server.useSsl);
1149 ui.sslVerify->setChecked(server.sslVerify);
1150 ui.sslVersion->setCurrentIndex(server.sslVersion);
1151 ui.useProxy->setChecked(server.useProxy);
1152 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1153 ui.proxyHost->setText(server.proxyHost);
1154 ui.proxyPort->setValue(server.proxyPort);
1155 ui.proxyUsername->setText(server.proxyUser);
1156 ui.proxyPassword->setText(server.proxyPass);
1158 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1159 // only if the core won't use autonegotiation to determine the best
1160 // protocol. When autonegotiation was introduced, it would have been
1161 // a good idea to use the CoreFeatures enum to accomplish this.
1162 // However, since multiple versions have been released since then, that
1163 // is no longer possible. Instead, we rely on the fact that the
1164 // Datastream protocol was introduced in the same version (0.10) as SSL
1165 // autonegotiation. Because of that, we can display the dropdown only
1166 // if the Legacy protocol is in use. If any other RemotePeer protocol
1167 // is in use, that means a newer protocol is in use and therefore the
1168 // core will use autonegotiation.
1169 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1171 ui.sslVersion->hide();
1174 // Whenever useSSL is toggled, update the port number if not changed from the default
1175 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1176 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1177 // If useSSL is later changed to be checked by default, change port's default value, too.
1179 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1180 // Synchronize requiring SSL with the use SSL checkbox
1181 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1182 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1184 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1185 ui.sslVerify->setEnabled(false);
1186 ui.sslVerify->setChecked(false);
1187 // Split up the message to allow re-using translations:
1188 // [Original tool-tip]
1189 // [Bold 'does not support feature' message]
1190 // [Specific version needed and feature details]
1191 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1192 ui.sslVerify->toolTip(),
1193 tr("Your Quassel core does not support this feature"),
1194 tr("You need a Quassel core v0.13.0 or newer in order to "
1195 "verify connection security.")));
1198 on_host_textChanged();
1202 Network::Server ServerEditDlg::serverData() const
1204 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1205 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1206 server.sslVersion = ui.sslVersion->currentIndex();
1207 server.useProxy = ui.useProxy->isChecked();
1208 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1209 server.proxyHost = ui.proxyHost->text();
1210 server.proxyPort = ui.proxyPort->value();
1211 server.proxyUser = ui.proxyUsername->text();
1212 server.proxyPass = ui.proxyPassword->text();
1217 void ServerEditDlg::on_host_textChanged()
1219 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1223 void ServerEditDlg::updateSslPort(bool isChecked)
1225 // "Use encrypted connection" was toggled, check the state...
1226 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1227 // Had been using the plain-text port, use the SSL default
1228 ui.port->setValue(Network::PORT_SSL);
1229 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1230 // Had been using the SSL port, use the plain-text default
1231 ui.port->setValue(Network::PORT_PLAINTEXT);
1236 /**************************************************************************
1238 *************************************************************************/
1240 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1244 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1247 ui.progressBar->setMaximum(numevents);
1248 ui.progressBar->setValue(0);
1250 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1251 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1253 foreach(NetworkId id, toRemove) {
1254 Client::removeNetwork(id);
1256 foreach(NetworkInfo info, toCreate) {
1257 Client::createNetwork(info);
1259 foreach(NetworkInfo info, toUpdate) {
1260 const Network *net = Client::network(info.networkId);
1262 qWarning() << "Invalid client network!";
1266 // FIXME this only checks for one changed item rather than all!
1267 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1268 Client::updateNetwork(info);
1272 qWarning() << "Sync dialog called without stuff to change!";
1278 void SaveNetworksDlg::clientEvent()
1280 ui.progressBar->setValue(++rcvevents);
1281 if (rcvevents >= numevents) accept();