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>
25 #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(icon::get("edit-rename"));
59 ui.addNetwork->setIcon(icon::get("list-add"));
60 ui.deleteNetwork->setIcon(icon::get("edit-delete"));
61 ui.addServer->setIcon(icon::get("list-add"));
62 ui.deleteServer->setIcon(icon::get("edit-delete"));
63 ui.editServer->setIcon(icon::get("configure"));
64 ui.upServer->setIcon(icon::get("go-up"));
65 ui.downServer->setIcon(icon::get("go-down"));
66 ui.editIdentities->setIcon(icon::get("configure"));
68 _ignoreWidgetChanges = false;
70 connectedIcon = icon::get("network-connect");
71 connectingIcon = icon::get("network-wired"); // FIXME network-connecting
72 disconnectedIcon = icon::get("network-disconnect");
75 infoIcon = icon::get("dialog-information");
76 warningIcon = icon::get("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 // Reset network capability status in case no valid networks get selected (a rare situation)
211 resetNetworkCapStates();
213 foreach(NetworkId netid, Client::networkIds()) {
214 clientNetworkAdded(netid);
216 ui.networkList->setCurrentRow(0);
218 setChangedState(false);
222 void NetworksSettingsPage::reset()
225 ui.networkList->clear();
226 networkInfos.clear();
230 bool NetworksSettingsPage::aboutToSave()
232 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
234 foreach(NetworkInfo info, networkInfos.values()) {
235 if (!info.serverList.count()) errors.append(1);
237 if (!errors.count()) return true;
238 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
239 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
240 error += tr("</ul>");
241 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
246 void NetworksSettingsPage::widgetHasChanged()
248 if (_ignoreWidgetChanges) return;
249 bool changed = testHasChanged();
250 if (changed != hasChanged()) setChangedState(changed);
254 bool NetworksSettingsPage::testHasChanged()
256 if (currentId != 0) {
257 saveToNetworkInfo(networkInfos[currentId]);
259 if (Client::networkIds().count() != networkInfos.count()) return true;
260 foreach(NetworkId id, networkInfos.keys()) {
261 if (id < 0) return true;
262 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
268 void NetworksSettingsPage::setWidgetStates()
271 if (ui.networkList->selectedItems().count()) {
272 ui.detailsBox->setEnabled(true);
273 ui.renameNetwork->setEnabled(true);
274 ui.deleteNetwork->setEnabled(true);
276 /* button disabled for now
277 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
278 const Network *net = id > 0 ? Client::network(id) : 0;
279 ui.connectNow->setEnabled(net);
280 // && (Client::network(id)->connectionState() == Network::Initialized
281 // || Client::network(id)->connectionState() == Network::Disconnected));
283 if(net->connectionState() == Network::Disconnected) {
284 ui.connectNow->setIcon(connectedIcon);
285 ui.connectNow->setText(tr("Connect"));
287 ui.connectNow->setIcon(disconnectedIcon);
288 ui.connectNow->setText(tr("Disconnect"));
291 ui.connectNow->setIcon(QIcon());
292 ui.connectNow->setText(tr("Apply first!"));
296 ui.renameNetwork->setEnabled(false);
297 ui.deleteNetwork->setEnabled(false);
298 //ui.connectNow->setEnabled(false);
299 ui.detailsBox->setEnabled(false);
302 if (ui.serverList->selectedItems().count()) {
303 ui.editServer->setEnabled(true);
304 ui.deleteServer->setEnabled(true);
305 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
306 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
309 ui.editServer->setEnabled(false);
310 ui.deleteServer->setEnabled(false);
311 ui.upServer->setEnabled(false);
312 ui.downServer->setEnabled(false);
317 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
319 if (!item && !(item = networkItem(id))) return;
320 const Network *net = Client::network(id);
321 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
322 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
323 if (net && net->connectionState() == Network::Initialized) {
324 item->setIcon(connectedIcon);
326 else if (net && net->connectionState() != Network::Disconnected) {
327 item->setIcon(connectingIcon);
330 item->setIcon(disconnectedIcon);
334 // check if we already have another net of this name in the list, and replace it
335 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
337 foreach(QListWidgetItem *i, items) {
338 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
339 if (oldid > 0) continue; // only locally created nets should be replaced
340 if (oldid == currentId) {
343 ui.networkList->clearSelection();
345 int row = ui.networkList->row(i);
347 QListWidgetItem *olditem = ui.networkList->takeItem(row);
351 networkInfos.remove(oldid);
355 item->setText(net->networkName());
356 if (select) item->setSelected(true);
361 void NetworksSettingsPage::resetNetworkCapStates()
363 // Set the status to a blank (invalid) network ID, reseting all UI
364 setNetworkCapStates(NetworkId());
368 void NetworksSettingsPage::setNetworkCapStates(NetworkId id)
370 const Network *net = Client::network(id);
371 if (net && Client::isCoreFeatureEnabled(Quassel::Feature::CapNegotiation)) {
372 // Capability negotiation is supported, network exists.
373 // Check if the network is connected. Don't use net->isConnected() as that won't be true
374 // during capability negotiation when capabilities are added and removed.
375 if (net->connectionState() != Network::Disconnected) {
376 // Network exists and is connected, check available capabilities...
378 if (net->saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
379 setSASLStatus(CapSupportStatus::MaybeSupported);
381 setSASLStatus(CapSupportStatus::MaybeUnsupported);
384 // Add additional capability-dependent interface updates here
386 // Network is disconnected
388 setSASLStatus(CapSupportStatus::Disconnected);
390 // Add additional capability-dependent interface updates here
393 // Capability negotiation is not supported and/or network doesn't exist.
394 // Don't assume anything and reset all capability-dependent interface elements to neutral.
396 setSASLStatus(CapSupportStatus::Unknown);
398 // Add additional capability-dependent interface updates here
403 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
405 this->setEnabled(state);
416 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
418 const Identity *identity = Client::identity(id);
419 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
421 QString name = identity->identityName();
422 for (int j = 0; j < ui.identityList->count(); j++) {
423 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
424 ui.identityList->insertItem(j, name, id.toInt());
430 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
435 void NetworksSettingsPage::clientIdentityUpdated()
437 const Identity *identity = qobject_cast<const Identity *>(sender());
439 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
442 int row = ui.identityList->findData(identity->id().toInt());
444 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
447 if (ui.identityList->itemText(row) != identity->identityName()) {
448 ui.identityList->setItemText(row, identity->identityName());
453 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
455 IdentityId defaultId = defaultIdentity();
456 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
457 foreach(NetworkInfo info, networkInfos.values()) {
458 if (info.identity == id) {
459 if (info.networkId == currentId)
460 ui.identityList->setCurrentIndex(0);
461 info.identity = defaultId;
462 networkInfos[info.networkId] = info;
463 if (info.networkId > 0) Client::updateNetwork(info);
466 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
471 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
473 for (int i = 0; i < ui.networkList->count(); i++) {
474 QListWidgetItem *item = ui.networkList->item(i);
475 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
481 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
484 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
485 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
487 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
488 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
490 // Handle capability changes in case a server dis/connects with the settings window open.
491 connect(Client::network(id), SIGNAL(capAdded(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
492 connect(Client::network(id), SIGNAL(capRemoved(const QString &)), this, SLOT(clientNetworkCapsUpdated()));
496 void NetworksSettingsPage::clientNetworkUpdated()
498 const Network *net = qobject_cast<const Network *>(sender());
500 qWarning() << "Update request for unknown network received!";
503 networkInfos[net->networkId()] = net->networkInfo();
504 setItemState(net->networkId());
505 if (net->networkId() == currentId) displayNetwork(net->networkId());
511 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
513 if (!networkInfos.contains(id)) return;
514 if (id == currentId) displayNetwork(0);
515 NetworkInfo info = networkInfos.take(id);
516 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
517 foreach(QListWidgetItem *item, items) {
518 if (item->data(Qt::UserRole).value<NetworkId>() == id)
519 delete ui.networkList->takeItem(ui.networkList->row(item));
526 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
529 const Network *net = qobject_cast<const Network *>(sender());
532 if(net->networkId() == currentId) {
533 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
536 setItemState(net->networkId());
537 if (net->networkId() == currentId) {
538 // Network is currently shown. Update the capability-dependent UI in case capabilities have
540 setNetworkCapStates(currentId);
546 void NetworksSettingsPage::networkConnectionError(const QString &)
551 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
553 NetworkInfo info = Client::network(id)->networkInfo();
554 networkInfos[id] = info;
555 return insertNetwork(info);
559 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
561 QListWidgetItem *item = 0;
562 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
563 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
565 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
566 // -> then we can be sure that this is the core-side replacement for the net we created
567 foreach(QListWidgetItem *i, items) {
568 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
569 if (id < 0) { item = i; break; }
571 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
573 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
574 setItemState(info.networkId, item);
580 // Called when selecting 'Configure' from the buffer list
581 void NetworksSettingsPage::bufferList_Open(NetworkId netId)
583 QListWidgetItem *item = networkItem(netId);
584 ui.networkList->setCurrentItem(item, QItemSelectionModel::SelectCurrent);
588 void NetworksSettingsPage::displayNetwork(NetworkId id)
590 _ignoreWidgetChanges = true;
592 NetworkInfo info = networkInfos[id];
595 // this is only needed when the core supports SASL EXTERNAL
596 if (Client::isCoreFeatureEnabled(Quassel::Feature::SaslExternal)) {
598 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
601 _cid = new CertIdentity(*Client::identity(info.identity), this);
602 _cid->enableEditSsl(true);
603 connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
607 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
608 ui.serverList->clear();
609 foreach(Network::Server server, info.serverList) {
610 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
612 item->setIcon(icon::get("document-encrypt"));
613 ui.serverList->addItem(item);
616 //ui.randomServer->setChecked(info.useRandomServer);
617 // Update the capability-dependent UI in case capabilities have changed.
618 setNetworkCapStates(id);
619 ui.performEdit->setPlainText(info.perform.join("\n"));
620 ui.autoIdentify->setChecked(info.useAutoIdentify);
621 ui.autoIdentifyService->setText(info.autoIdentifyService);
622 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
623 ui.sasl->setChecked(info.useSasl);
624 ui.saslAccount->setText(info.saslAccount);
625 ui.saslPassword->setText(info.saslPassword);
626 if (info.codecForEncoding.isEmpty()) {
627 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
628 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
629 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
630 ui.useCustomEncodings->setChecked(false);
633 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
634 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
635 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
636 ui.useCustomEncodings->setChecked(true);
638 ui.autoReconnect->setChecked(info.useAutoReconnect);
639 ui.reconnectInterval->setValue(info.autoReconnectInterval);
640 ui.reconnectRetries->setValue(info.autoReconnectRetries);
641 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
642 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
643 // Custom rate limiting
644 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
645 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
646 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
647 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
648 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
649 // Convert milliseconds (integer) into seconds (double)
650 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
653 // just clear widgets
656 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
660 ui.identityList->setCurrentIndex(-1);
661 ui.serverList->clear();
662 ui.performEdit->clear();
663 ui.autoIdentifyService->clear();
664 ui.autoIdentifyPassword->clear();
665 ui.saslAccount->clear();
666 ui.saslPassword->clear();
669 _ignoreWidgetChanges = false;
674 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
676 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
677 //info.useRandomServer = ui.randomServer->isChecked();
678 info.perform = ui.performEdit->toPlainText().split("\n");
679 info.useAutoIdentify = ui.autoIdentify->isChecked();
680 info.autoIdentifyService = ui.autoIdentifyService->text();
681 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
682 info.useSasl = ui.sasl->isChecked();
683 info.saslAccount = ui.saslAccount->text();
684 info.saslPassword = ui.saslPassword->text();
685 if (!ui.useCustomEncodings->isChecked()) {
686 info.codecForEncoding.clear();
687 info.codecForDecoding.clear();
688 info.codecForServer.clear();
691 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
692 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
693 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
695 info.useAutoReconnect = ui.autoReconnect->isChecked();
696 info.autoReconnectInterval = ui.reconnectInterval->value();
697 info.autoReconnectRetries = ui.reconnectRetries->value();
698 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
699 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
700 // Custom rate limiting
701 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
702 info.messageRateBurstSize = ui.messageRateBurstSize->value();
703 // Convert seconds (double) into milliseconds (integer)
704 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
705 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
709 void NetworksSettingsPage::clientNetworkCapsUpdated()
711 // Grab the updated network
712 const Network *net = qobject_cast<const Network *>(sender());
714 qWarning() << "Update request for unknown network received!";
717 if (net->networkId() == currentId) {
718 // Network is currently shown. Update the capability-dependent UI in case capabilities have
720 setNetworkCapStates(currentId);
725 void NetworksSettingsPage::setSASLStatus(const CapSupportStatus saslStatus)
727 if (_saslStatusSelected != saslStatus) {
728 // Update the cached copy of SASL status used with the Details dialog
729 _saslStatusSelected = saslStatus;
731 // Update the user interface
732 switch (saslStatus) {
733 case CapSupportStatus::Unknown:
734 // There's no capability negotiation or network doesn't exist. Don't assume
736 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
737 tr("Could not check if supported by network")));
738 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
740 case CapSupportStatus::Disconnected:
741 // Disconnected from network, no way to check.
742 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
743 tr("Cannot check if supported when disconnected")));
744 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
746 case CapSupportStatus::MaybeUnsupported:
747 // The network doesn't advertise support for SASL PLAIN. Here be dragons.
748 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(
749 tr("Not currently supported by network")));
750 ui.saslStatusIcon->setPixmap(warningIcon.pixmap(16));
752 case CapSupportStatus::MaybeSupported:
753 // The network advertises support for SASL PLAIN. Encourage using it!
754 // Unfortunately we don't know for sure if it's desired or functional.
755 ui.saslStatusLabel->setText(QString("<i>%1</i>").arg(tr("Supported by network")));
756 ui.saslStatusIcon->setPixmap(infoIcon.pixmap(16));
764 void NetworksSettingsPage::sslUpdated()
766 if (_cid && !_cid->sslKey().isNull()) {
767 ui.saslContents->setDisabled(true);
768 ui.saslExtInfo->setHidden(false);
770 ui.saslContents->setDisabled(false);
771 // Directly re-enabling causes the widgets to ignore the parent "Use SASL Authentication"
772 // state to indicate whether or not it's disabled. To workaround this, keep track of
773 // whether or not "Use SASL Authentication" is enabled, then quickly uncheck/recheck the
775 if (!ui.sasl->isChecked()) {
776 // SASL is not enabled, uncheck/recheck the group box to re-disable saslContents.
777 // Leaving saslContents disabled doesn't work as that prevents it from re-enabling if
778 // sasl is later checked.
779 ui.sasl->setChecked(true);
780 ui.sasl->setChecked(false);
782 ui.saslExtInfo->setHidden(true);
788 /*** Network list ***/
790 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
792 if (currentId != 0) {
793 saveToNetworkInfo(networkInfos[currentId]);
795 if (ui.networkList->selectedItems().count()) {
796 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
799 ui.serverList->setCurrentRow(0);
808 void NetworksSettingsPage::on_addNetwork_clicked()
810 QStringList existing;
811 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
812 NetworkAddDlg dlg(existing, this);
813 if (dlg.exec() == QDialog::Accepted) {
814 NetworkInfo info = dlg.networkInfo();
815 if (info.networkName.isEmpty())
816 return; // sanity check
819 for (id = 1; id <= networkInfos.count(); id++) {
821 if (!networkInfos.keys().contains(-id.toInt())) break;
825 info.identity = defaultIdentity();
826 networkInfos[id] = info;
827 QListWidgetItem *item = insertNetwork(info);
828 ui.networkList->setCurrentItem(item);
834 void NetworksSettingsPage::on_deleteNetwork_clicked()
836 if (ui.networkList->selectedItems().count()) {
837 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
838 int ret = QMessageBox::question(this, tr("Delete Network?"),
839 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
840 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
841 if (ret == QMessageBox::Yes) {
843 networkInfos.remove(netid);
844 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
845 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
853 void NetworksSettingsPage::on_renameNetwork_clicked()
855 if (!ui.networkList->selectedItems().count()) return;
856 QString old = ui.networkList->selectedItems()[0]->text();
857 QStringList existing;
858 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
859 NetworkEditDlg dlg(old, existing, this);
860 if (dlg.exec() == QDialog::Accepted) {
861 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
862 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
863 networkInfos[netid].networkName = dlg.networkName();
870 void NetworksSettingsPage::on_connectNow_clicked() {
871 if(!ui.networkList->selectedItems().count()) return;
872 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
873 const Network *net = Client::network(id);
875 if(net->connectionState() == Network::Disconnected) net->requestConnect();
876 else net->requestDisconnect();
880 /*** Server list ***/
882 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
888 void NetworksSettingsPage::on_addServer_clicked()
890 if (currentId == 0) return;
891 ServerEditDlg dlg(Network::Server(), this);
892 if (dlg.exec() == QDialog::Accepted) {
893 networkInfos[currentId].serverList.append(dlg.serverData());
894 displayNetwork(currentId);
895 ui.serverList->setCurrentRow(ui.serverList->count()-1);
901 void NetworksSettingsPage::on_editServer_clicked()
903 if (currentId == 0) return;
904 int cur = ui.serverList->currentRow();
905 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
906 if (dlg.exec() == QDialog::Accepted) {
907 networkInfos[currentId].serverList[cur] = dlg.serverData();
908 displayNetwork(currentId);
909 ui.serverList->setCurrentRow(cur);
915 void NetworksSettingsPage::on_deleteServer_clicked()
917 if (currentId == 0) return;
918 int cur = ui.serverList->currentRow();
919 networkInfos[currentId].serverList.removeAt(cur);
920 displayNetwork(currentId);
921 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
926 void NetworksSettingsPage::on_upServer_clicked()
928 int cur = ui.serverList->currentRow();
929 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
930 networkInfos[currentId].serverList.insert(cur-1, server);
931 displayNetwork(currentId);
932 ui.serverList->setCurrentRow(cur-1);
937 void NetworksSettingsPage::on_downServer_clicked()
939 int cur = ui.serverList->currentRow();
940 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
941 networkInfos[currentId].serverList.insert(cur+1, server);
942 displayNetwork(currentId);
943 ui.serverList->setCurrentRow(cur+1);
948 void NetworksSettingsPage::on_editIdentities_clicked()
950 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
955 void NetworksSettingsPage::on_saslStatusDetails_clicked()
957 if (ui.networkList->selectedItems().count()) {
958 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
959 QString &netName = networkInfos[netid].networkName;
961 // If these strings are visible, one of the status messages wasn't detected below.
962 QString saslStatusHeader = "[header unintentionally left blank]";
963 QString saslStatusExplanation = "[explanation unintentionally left blank]";
965 // If true, show a warning icon instead of an information icon
966 bool useWarningIcon = false;
968 // Determine which explanation to show
969 switch (_saslStatusSelected) {
970 case CapSupportStatus::Unknown:
971 saslStatusHeader = tr("Could not check if SASL supported by network");
972 saslStatusExplanation = tr("Quassel could not check if \"%1\" supports SASL. This may "
973 "be due to unsaved changes or an older Quassel core. You "
974 "can still try using SASL.").arg(netName);
976 case CapSupportStatus::Disconnected:
977 saslStatusHeader = tr("Cannot check if SASL supported when disconnected");
978 saslStatusExplanation = tr("Quassel cannot check if \"%1\" supports SASL when "
979 "disconnected. Connect to the network, or try using SASL "
980 "anyways.").arg(netName);
982 case CapSupportStatus::MaybeUnsupported:
983 saslStatusHeader = tr("SASL not currently supported by network");
984 saslStatusExplanation = tr("The network \"%1\" does not currently support SASL. "
985 "However, support might be added later on.").arg(netName);
986 useWarningIcon = true;
988 case CapSupportStatus::MaybeSupported:
989 saslStatusHeader = tr("SASL supported by network");
990 saslStatusExplanation = tr("The network \"%1\" supports SASL. In most cases, you "
991 "should use SASL instead of NickServ identification."
996 // Process this in advance for reusability below
997 const QString saslStatusMsgTitle = tr("SASL support for \"%1\"").arg(netName);
998 const QString saslStatusMsgText =
999 QString("<p><b>%1</b></p></br><p>%2</p></br><p><i>%3</i></p>"
1000 ).arg(saslStatusHeader,
1001 saslStatusExplanation,
1002 tr("SASL is a standardized way to log in and identify yourself to "
1005 if (useWarningIcon) {
1006 // Show as a warning dialog box
1007 QMessageBox::warning(this, saslStatusMsgTitle, saslStatusMsgText);
1009 // Show as an information dialog box
1010 QMessageBox::information(this, saslStatusMsgTitle, saslStatusMsgText);
1016 IdentityId NetworksSettingsPage::defaultIdentity() const
1018 IdentityId defaultId = 0;
1019 QList<IdentityId> ids = Client::identityIds();
1020 foreach(IdentityId id, ids) {
1021 if (defaultId == 0 || id < defaultId)
1028 /**************************************************************************
1030 *************************************************************************/
1032 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
1035 ui.useSSL->setIcon(icon::get("document-encrypt"));
1037 // Whenever useSSL is toggled, update the port number if not changed from the default
1038 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1039 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1040 // If useSSL is later changed to be checked by default, change port's default value, too.
1042 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1043 // Synchronize requiring SSL with the use SSL checkbox
1044 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1045 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1047 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1048 ui.sslVerify->setEnabled(false);
1049 ui.sslVerify->setChecked(false);
1050 // Split up the message to allow re-using translations:
1051 // [Original tool-tip]
1052 // [Bold 'does not support feature' message]
1053 // [Specific version needed and feature details]
1054 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1055 ui.sslVerify->toolTip(),
1056 tr("Your Quassel core does not support this feature"),
1057 tr("You need a Quassel core v0.13.0 or newer in order to "
1058 "verify connection security.")));
1061 // read preset networks
1062 QStringList networks = PresetNetworks::names();
1063 foreach(QString s, existing)
1064 networks.removeAll(s);
1065 if (networks.count())
1066 ui.presetList->addItems(networks);
1068 ui.useManual->setChecked(true);
1069 ui.usePreset->setEnabled(false);
1071 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1072 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
1073 connect(ui.usePreset, SIGNAL(toggled(bool)), SLOT(setButtonStates()));
1074 connect(ui.useManual, SIGNAL(toggled(bool)), SLOT(setButtonStates()));
1079 NetworkInfo NetworkAddDlg::networkInfo() const
1081 if (ui.useManual->isChecked()) {
1083 info.networkName = ui.networkName->text().trimmed();
1084 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
1085 ui.serverPassword->text(), ui.useSSL->isChecked(),
1086 ui.sslVerify->isChecked());
1090 return PresetNetworks::networkInfo(ui.presetList->currentText());
1094 void NetworkAddDlg::setButtonStates()
1097 if (ui.usePreset->isChecked() && ui.presetList->count())
1099 else if (ui.useManual->isChecked()) {
1100 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
1101 && !ui.serverAddress->text().isEmpty();
1103 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
1107 void NetworkAddDlg::updateSslPort(bool isChecked)
1109 // "Use encrypted connection" was toggled, check the state...
1110 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1111 // Had been using the plain-text port, use the SSL default
1112 ui.port->setValue(Network::PORT_SSL);
1113 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1114 // Had been using the SSL port, use the plain-text default
1115 ui.port->setValue(Network::PORT_PLAINTEXT);
1120 /**************************************************************************
1122 *************************************************************************/
1124 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
1128 if (old.isEmpty()) {
1130 setWindowTitle(tr("Add Network"));
1131 on_networkEdit_textChanged(""); // disable ok button
1133 else ui.networkEdit->setText(old);
1137 QString NetworkEditDlg::networkName() const
1139 return ui.networkEdit->text().trimmed();
1143 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
1145 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
1149 /**************************************************************************
1151 *************************************************************************/
1152 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
1155 ui.useSSL->setIcon(icon::get("document-encrypt"));
1156 ui.host->setText(server.host);
1157 ui.host->setFocus();
1158 ui.port->setValue(server.port);
1159 ui.password->setText(server.password);
1160 ui.useSSL->setChecked(server.useSsl);
1161 ui.sslVerify->setChecked(server.sslVerify);
1162 ui.sslVersion->setCurrentIndex(server.sslVersion);
1163 ui.useProxy->setChecked(server.useProxy);
1164 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
1165 ui.proxyHost->setText(server.proxyHost);
1166 ui.proxyPort->setValue(server.proxyPort);
1167 ui.proxyUsername->setText(server.proxyUser);
1168 ui.proxyPassword->setText(server.proxyPass);
1170 // This is a dirty hack to display the core->IRC SSL protocol dropdown
1171 // only if the core won't use autonegotiation to determine the best
1172 // protocol. When autonegotiation was introduced, it would have been
1173 // a good idea to use the CoreFeatures enum to accomplish this.
1174 // However, since multiple versions have been released since then, that
1175 // is no longer possible. Instead, we rely on the fact that the
1176 // Datastream protocol was introduced in the same version (0.10) as SSL
1177 // autonegotiation. Because of that, we can display the dropdown only
1178 // if the Legacy protocol is in use. If any other RemotePeer protocol
1179 // is in use, that means a newer protocol is in use and therefore the
1180 // core will use autonegotiation.
1181 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
1183 ui.sslVersion->hide();
1186 // Whenever useSSL is toggled, update the port number if not changed from the default
1187 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
1188 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
1189 // If useSSL is later changed to be checked by default, change port's default value, too.
1191 if (Client::isCoreFeatureEnabled(Quassel::Feature::VerifyServerSSL)) {
1192 // Synchronize requiring SSL with the use SSL checkbox
1193 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1194 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1196 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1197 ui.sslVerify->setEnabled(false);
1198 ui.sslVerify->setChecked(false);
1199 // Split up the message to allow re-using translations:
1200 // [Original tool-tip]
1201 // [Bold 'does not support feature' message]
1202 // [Specific version needed and feature details]
1203 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1204 ui.sslVerify->toolTip(),
1205 tr("Your Quassel core does not support this feature"),
1206 tr("You need a Quassel core v0.13.0 or newer in order to "
1207 "verify connection security.")));
1210 on_host_textChanged();
1214 Network::Server ServerEditDlg::serverData() const
1216 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1217 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1218 server.sslVersion = ui.sslVersion->currentIndex();
1219 server.useProxy = ui.useProxy->isChecked();
1220 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1221 server.proxyHost = ui.proxyHost->text();
1222 server.proxyPort = ui.proxyPort->value();
1223 server.proxyUser = ui.proxyUsername->text();
1224 server.proxyPass = ui.proxyPassword->text();
1229 void ServerEditDlg::on_host_textChanged()
1231 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1235 void ServerEditDlg::updateSslPort(bool isChecked)
1237 // "Use encrypted connection" was toggled, check the state...
1238 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1239 // Had been using the plain-text port, use the SSL default
1240 ui.port->setValue(Network::PORT_SSL);
1241 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1242 // Had been using the SSL port, use the plain-text default
1243 ui.port->setValue(Network::PORT_PLAINTEXT);
1248 /**************************************************************************
1250 *************************************************************************/
1252 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1256 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1259 ui.progressBar->setMaximum(numevents);
1260 ui.progressBar->setValue(0);
1262 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1263 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1265 foreach(NetworkId id, toRemove) {
1266 Client::removeNetwork(id);
1268 foreach(NetworkInfo info, toCreate) {
1269 Client::createNetwork(info);
1271 foreach(NetworkInfo info, toUpdate) {
1272 const Network *net = Client::network(info.networkId);
1274 qWarning() << "Invalid client network!";
1278 // FIXME this only checks for one changed item rather than all!
1279 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1280 Client::updateNetwork(info);
1284 qWarning() << "Sync dialog called without stuff to change!";
1290 void SaveNetworksDlg::clientEvent()
1292 ui.progressBar->setValue(++rcvevents);
1293 if (rcvevents >= numevents) accept();