1 /***************************************************************************
2 * Copyright (C) 2005-2016 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"
35 #include "settingspages/identitiessettingspage.h"
37 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
38 : SettingsPage(tr("IRC"), tr("Networks"), parent)
45 // hide SASL options for older cores
46 if (!(Client::coreFeatures() & Quassel::SaslAuthentication))
48 if (!(Client::coreFeatures() & Quassel::SaslExternal))
49 ui.saslExtInfo->hide();
51 ui.saslExtInfo->hide();
55 ui.renameNetwork->setIcon(QIcon::fromTheme("edit-rename"));
56 ui.addNetwork->setIcon(QIcon::fromTheme("list-add"));
57 ui.deleteNetwork->setIcon(QIcon::fromTheme("edit-delete"));
58 ui.addServer->setIcon(QIcon::fromTheme("list-add"));
59 ui.deleteServer->setIcon(QIcon::fromTheme("edit-delete"));
60 ui.editServer->setIcon(QIcon::fromTheme("configure"));
61 ui.upServer->setIcon(QIcon::fromTheme("go-up"));
62 ui.downServer->setIcon(QIcon::fromTheme("go-down"));
63 ui.editIdentities->setIcon(QIcon::fromTheme("configure"));
65 _ignoreWidgetChanges = false;
67 connectedIcon = QIcon::fromTheme("network-connect");
68 connectingIcon = QIcon::fromTheme("network-wired"); // FIXME network-connecting
69 disconnectedIcon = QIcon::fromTheme("network-disconnect");
71 foreach(int mib, QTextCodec::availableMibs()) {
72 QByteArray codec = QTextCodec::codecForMib(mib)->name();
73 ui.sendEncoding->addItem(codec);
74 ui.recvEncoding->addItem(codec);
75 ui.serverEncoding->addItem(codec);
77 ui.sendEncoding->model()->sort(0);
78 ui.recvEncoding->model()->sort(0);
79 ui.serverEncoding->model()->sort(0);
81 setEnabled(Client::isConnected()); // need a core connection!
83 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
84 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
85 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
86 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
87 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
89 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
90 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
91 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
92 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
93 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
94 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
95 connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
96 connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
97 connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
98 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
99 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
100 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
101 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
102 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
103 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
104 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
105 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
106 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
108 // Core features can change during a reconnect. Always connect these here, delaying testing for
109 // the core feature flag in load().
110 connect(ui.useCustomMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
111 connect(ui.messageRateBurstSize, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
112 connect(ui.messageRateDelay, SIGNAL(valueChanged(double)), this, SLOT(widgetHasChanged()));
113 connect(ui.unlimitedMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
115 // Add additional widgets here
116 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
117 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
119 foreach(IdentityId id, Client::identityIds()) {
120 clientIdentityAdded(id);
125 void NetworksSettingsPage::save()
128 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
130 QList<NetworkInfo> toCreate, toUpdate;
131 QList<NetworkId> toRemove;
132 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
133 while (i != networkInfos.end()) {
134 NetworkId id = (*i).networkId;
137 //if(id == currentId) currentId = 0;
138 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
139 //if(items.count()) {
140 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
143 //i = networkInfos.erase(i);
147 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
153 foreach(NetworkId id, Client::networkIds()) {
154 if (!networkInfos.contains(id)) toRemove.append(id);
156 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
157 int ret = dlg.exec();
158 if (ret == QDialog::Rejected) {
159 // canceled -> reload everything to be safe
162 setChangedState(false);
167 void NetworksSettingsPage::load()
171 // Handle UI dependent on core feature flags here
172 if (Client::coreFeatures() & Quassel::CustomRateLimits) {
173 // Custom rate limiting supported, allow toggling
174 ui.useCustomMessageRate->setEnabled(true);
175 // Reset tooltip to default.
176 ui.useCustomMessageRate->setToolTip(QString("%1").arg(
177 tr("<p>Override default message rate limiting.</p>"
178 "<p><b>Setting limits too low may get you disconnected"
179 " from the server!</b></p>")));
180 // If changed, update the message below!
182 // Custom rate limiting not supported, disallow toggling
183 ui.useCustomMessageRate->setEnabled(false);
184 // Split up the message to allow re-using translations:
185 // [Original tool-tip]
186 // [Bold 'does not support feature' message]
187 // [Specific version needed and feature details]
188 ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
189 tr("<p>Override default message rate limiting.</p>"
190 "<p><b>Setting limits too low may get you disconnected"
191 " from the server!</b></p>"),
192 tr("Your Quassel core does not support this feature"),
193 tr("You need a Quassel core v0.13.0 or newer in order to "
194 "modify message rate limits.")));
198 // Hide the SASL EXTERNAL notice until a network's shown. Stops it from showing while loading
199 // backlog from the core.
203 foreach(NetworkId netid, Client::networkIds()) {
204 clientNetworkAdded(netid);
206 ui.networkList->setCurrentRow(0);
208 setChangedState(false);
212 void NetworksSettingsPage::reset()
215 ui.networkList->clear();
216 networkInfos.clear();
220 bool NetworksSettingsPage::aboutToSave()
222 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
224 foreach(NetworkInfo info, networkInfos.values()) {
225 if (!info.serverList.count()) errors.append(1);
227 if (!errors.count()) return true;
228 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
229 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
230 error += tr("</ul>");
231 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
236 void NetworksSettingsPage::widgetHasChanged()
238 if (_ignoreWidgetChanges) return;
239 bool changed = testHasChanged();
240 if (changed != hasChanged()) setChangedState(changed);
244 bool NetworksSettingsPage::testHasChanged()
246 if (currentId != 0) {
247 saveToNetworkInfo(networkInfos[currentId]);
249 if (Client::networkIds().count() != networkInfos.count()) return true;
250 foreach(NetworkId id, networkInfos.keys()) {
251 if (id < 0) return true;
252 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
258 void NetworksSettingsPage::setWidgetStates()
261 if (ui.networkList->selectedItems().count()) {
262 ui.detailsBox->setEnabled(true);
263 ui.renameNetwork->setEnabled(true);
264 ui.deleteNetwork->setEnabled(true);
266 /* button disabled for now
267 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
268 const Network *net = id > 0 ? Client::network(id) : 0;
269 ui.connectNow->setEnabled(net);
270 // && (Client::network(id)->connectionState() == Network::Initialized
271 // || Client::network(id)->connectionState() == Network::Disconnected));
273 if(net->connectionState() == Network::Disconnected) {
274 ui.connectNow->setIcon(connectedIcon);
275 ui.connectNow->setText(tr("Connect"));
277 ui.connectNow->setIcon(disconnectedIcon);
278 ui.connectNow->setText(tr("Disconnect"));
281 ui.connectNow->setIcon(QIcon());
282 ui.connectNow->setText(tr("Apply first!"));
286 ui.renameNetwork->setEnabled(false);
287 ui.deleteNetwork->setEnabled(false);
288 //ui.connectNow->setEnabled(false);
289 ui.detailsBox->setEnabled(false);
292 if (ui.serverList->selectedItems().count()) {
293 ui.editServer->setEnabled(true);
294 ui.deleteServer->setEnabled(true);
295 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
296 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
299 ui.editServer->setEnabled(false);
300 ui.deleteServer->setEnabled(false);
301 ui.upServer->setEnabled(false);
302 ui.downServer->setEnabled(false);
307 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
309 if (!item && !(item = networkItem(id))) return;
310 const Network *net = Client::network(id);
311 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
312 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
313 if (net && net->connectionState() == Network::Initialized) {
314 item->setIcon(connectedIcon);
316 else if (net && net->connectionState() != Network::Disconnected) {
317 item->setIcon(connectingIcon);
320 item->setIcon(disconnectedIcon);
324 // check if we already have another net of this name in the list, and replace it
325 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
327 foreach(QListWidgetItem *i, items) {
328 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
329 if (oldid > 0) continue; // only locally created nets should be replaced
330 if (oldid == currentId) {
333 ui.networkList->clearSelection();
335 int row = ui.networkList->row(i);
337 QListWidgetItem *olditem = ui.networkList->takeItem(row);
341 networkInfos.remove(oldid);
345 item->setText(net->networkName());
346 if (select) item->setSelected(true);
351 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
353 this->setEnabled(state);
364 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
366 const Identity *identity = Client::identity(id);
367 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
369 QString name = identity->identityName();
370 for (int j = 0; j < ui.identityList->count(); j++) {
371 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
372 ui.identityList->insertItem(j, name, id.toInt());
378 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
383 void NetworksSettingsPage::clientIdentityUpdated()
385 const Identity *identity = qobject_cast<const Identity *>(sender());
387 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
390 int row = ui.identityList->findData(identity->id().toInt());
392 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
395 if (ui.identityList->itemText(row) != identity->identityName()) {
396 ui.identityList->setItemText(row, identity->identityName());
401 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
403 IdentityId defaultId = defaultIdentity();
404 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
405 foreach(NetworkInfo info, networkInfos.values()) {
406 if (info.identity == id) {
407 if (info.networkId == currentId)
408 ui.identityList->setCurrentIndex(0);
409 info.identity = defaultId;
410 networkInfos[info.networkId] = info;
411 if (info.networkId > 0) Client::updateNetwork(info);
414 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
419 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
421 for (int i = 0; i < ui.networkList->count(); i++) {
422 QListWidgetItem *item = ui.networkList->item(i);
423 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
429 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
432 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
433 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
435 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
436 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
440 void NetworksSettingsPage::clientNetworkUpdated()
442 const Network *net = qobject_cast<const Network *>(sender());
444 qWarning() << "Update request for unknown network received!";
447 networkInfos[net->networkId()] = net->networkInfo();
448 setItemState(net->networkId());
449 if (net->networkId() == currentId) displayNetwork(net->networkId());
455 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
457 if (!networkInfos.contains(id)) return;
458 if (id == currentId) displayNetwork(0);
459 NetworkInfo info = networkInfos.take(id);
460 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
461 foreach(QListWidgetItem *item, items) {
462 if (item->data(Qt::UserRole).value<NetworkId>() == id)
463 delete ui.networkList->takeItem(ui.networkList->row(item));
470 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
473 const Network *net = qobject_cast<const Network *>(sender());
476 if(net->networkId() == currentId) {
477 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
480 setItemState(net->networkId());
485 void NetworksSettingsPage::networkConnectionError(const QString &)
490 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
492 NetworkInfo info = Client::network(id)->networkInfo();
493 networkInfos[id] = info;
494 return insertNetwork(info);
498 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
500 QListWidgetItem *item = 0;
501 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
502 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
504 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
505 // -> then we can be sure that this is the core-side replacement for the net we created
506 foreach(QListWidgetItem *i, items) {
507 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
508 if (id < 0) { item = i; break; }
510 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
512 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
513 setItemState(info.networkId, item);
519 void NetworksSettingsPage::displayNetwork(NetworkId id)
521 _ignoreWidgetChanges = true;
523 NetworkInfo info = networkInfos[id];
526 // this is only needed when the core supports SASL EXTERNAL
527 if (Client::coreFeatures() & Quassel::SaslExternal) {
529 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
532 _cid = new CertIdentity(*Client::identity(info.identity), this);
533 _cid->enableEditSsl(true);
534 connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
538 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
539 ui.serverList->clear();
540 foreach(Network::Server server, info.serverList) {
541 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
543 item->setIcon(QIcon::fromTheme("document-encrypt"));
544 ui.serverList->addItem(item);
547 //ui.randomServer->setChecked(info.useRandomServer);
548 ui.performEdit->setPlainText(info.perform.join("\n"));
549 ui.autoIdentify->setChecked(info.useAutoIdentify);
550 ui.autoIdentifyService->setText(info.autoIdentifyService);
551 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
552 ui.sasl->setChecked(info.useSasl);
553 ui.saslAccount->setText(info.saslAccount);
554 ui.saslPassword->setText(info.saslPassword);
555 if (info.codecForEncoding.isEmpty()) {
556 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
557 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
558 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
559 ui.useCustomEncodings->setChecked(false);
562 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
563 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
564 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
565 ui.useCustomEncodings->setChecked(true);
567 ui.autoReconnect->setChecked(info.useAutoReconnect);
568 ui.reconnectInterval->setValue(info.autoReconnectInterval);
569 ui.reconnectRetries->setValue(info.autoReconnectRetries);
570 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
571 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
572 // Custom rate limiting
573 ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
574 // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
575 // disabled, 'ui.messageRateDelayFrame' will remain disabled.
576 ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
577 ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
578 // Convert milliseconds (integer) into seconds (double)
579 ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
582 // just clear widgets
585 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
589 ui.identityList->setCurrentIndex(-1);
590 ui.serverList->clear();
591 ui.performEdit->clear();
592 ui.autoIdentifyService->clear();
593 ui.autoIdentifyPassword->clear();
594 ui.saslAccount->clear();
595 ui.saslPassword->clear();
598 _ignoreWidgetChanges = false;
603 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
605 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
606 //info.useRandomServer = ui.randomServer->isChecked();
607 info.perform = ui.performEdit->toPlainText().split("\n");
608 info.useAutoIdentify = ui.autoIdentify->isChecked();
609 info.autoIdentifyService = ui.autoIdentifyService->text();
610 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
611 info.useSasl = ui.sasl->isChecked();
612 info.saslAccount = ui.saslAccount->text();
613 info.saslPassword = ui.saslPassword->text();
614 if (!ui.useCustomEncodings->isChecked()) {
615 info.codecForEncoding.clear();
616 info.codecForDecoding.clear();
617 info.codecForServer.clear();
620 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
621 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
622 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
624 info.useAutoReconnect = ui.autoReconnect->isChecked();
625 info.autoReconnectInterval = ui.reconnectInterval->value();
626 info.autoReconnectRetries = ui.reconnectRetries->value();
627 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
628 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
629 // Custom rate limiting
630 info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
631 info.messageRateBurstSize = ui.messageRateBurstSize->value();
632 // Convert seconds (double) into milliseconds (integer)
633 info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
634 info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
639 void NetworksSettingsPage::sslUpdated()
641 if (_cid && !_cid->sslKey().isNull()) {
642 ui.saslAccount->setDisabled(true);
643 ui.saslAccountLabel->setDisabled(true);
644 ui.saslPassword->setDisabled(true);
645 ui.saslPasswordLabel->setDisabled(true);
646 ui.saslExtInfo->setHidden(false);
648 ui.saslAccount->setDisabled(false);
649 ui.saslAccountLabel->setDisabled(false);
650 ui.saslPassword->setDisabled(false);
651 ui.saslPasswordLabel->setDisabled(false);
652 ui.saslExtInfo->setHidden(true);
658 /*** Network list ***/
660 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
662 if (currentId != 0) {
663 saveToNetworkInfo(networkInfos[currentId]);
665 if (ui.networkList->selectedItems().count()) {
666 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
669 ui.serverList->setCurrentRow(0);
678 void NetworksSettingsPage::on_addNetwork_clicked()
680 QStringList existing;
681 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
682 NetworkAddDlg dlg(existing, this);
683 if (dlg.exec() == QDialog::Accepted) {
684 NetworkInfo info = dlg.networkInfo();
685 if (info.networkName.isEmpty())
686 return; // sanity check
689 for (id = 1; id <= networkInfos.count(); id++) {
691 if (!networkInfos.keys().contains(-id.toInt())) break;
695 info.identity = defaultIdentity();
696 networkInfos[id] = info;
697 QListWidgetItem *item = insertNetwork(info);
698 ui.networkList->setCurrentItem(item);
704 void NetworksSettingsPage::on_deleteNetwork_clicked()
706 if (ui.networkList->selectedItems().count()) {
707 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
708 int ret = QMessageBox::question(this, tr("Delete Network?"),
709 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
710 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
711 if (ret == QMessageBox::Yes) {
713 networkInfos.remove(netid);
714 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
715 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
723 void NetworksSettingsPage::on_renameNetwork_clicked()
725 if (!ui.networkList->selectedItems().count()) return;
726 QString old = ui.networkList->selectedItems()[0]->text();
727 QStringList existing;
728 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
729 NetworkEditDlg dlg(old, existing, this);
730 if (dlg.exec() == QDialog::Accepted) {
731 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
732 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
733 networkInfos[netid].networkName = dlg.networkName();
740 void NetworksSettingsPage::on_connectNow_clicked() {
741 if(!ui.networkList->selectedItems().count()) return;
742 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
743 const Network *net = Client::network(id);
745 if(net->connectionState() == Network::Disconnected) net->requestConnect();
746 else net->requestDisconnect();
750 /*** Server list ***/
752 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
758 void NetworksSettingsPage::on_addServer_clicked()
760 if (currentId == 0) return;
761 ServerEditDlg dlg(Network::Server(), this);
762 if (dlg.exec() == QDialog::Accepted) {
763 networkInfos[currentId].serverList.append(dlg.serverData());
764 displayNetwork(currentId);
765 ui.serverList->setCurrentRow(ui.serverList->count()-1);
771 void NetworksSettingsPage::on_editServer_clicked()
773 if (currentId == 0) return;
774 int cur = ui.serverList->currentRow();
775 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
776 if (dlg.exec() == QDialog::Accepted) {
777 networkInfos[currentId].serverList[cur] = dlg.serverData();
778 displayNetwork(currentId);
779 ui.serverList->setCurrentRow(cur);
785 void NetworksSettingsPage::on_deleteServer_clicked()
787 if (currentId == 0) return;
788 int cur = ui.serverList->currentRow();
789 networkInfos[currentId].serverList.removeAt(cur);
790 displayNetwork(currentId);
791 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
796 void NetworksSettingsPage::on_upServer_clicked()
798 int cur = ui.serverList->currentRow();
799 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
800 networkInfos[currentId].serverList.insert(cur-1, server);
801 displayNetwork(currentId);
802 ui.serverList->setCurrentRow(cur-1);
807 void NetworksSettingsPage::on_downServer_clicked()
809 int cur = ui.serverList->currentRow();
810 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
811 networkInfos[currentId].serverList.insert(cur+1, server);
812 displayNetwork(currentId);
813 ui.serverList->setCurrentRow(cur+1);
818 void NetworksSettingsPage::on_editIdentities_clicked()
820 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
825 IdentityId NetworksSettingsPage::defaultIdentity() const
827 IdentityId defaultId = 0;
828 QList<IdentityId> ids = Client::identityIds();
829 foreach(IdentityId id, ids) {
830 if (defaultId == 0 || id < defaultId)
837 /**************************************************************************
839 *************************************************************************/
841 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
844 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
846 // Whenever useSSL is toggled, update the port number if not changed from the default
847 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
848 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
849 // If useSSL is later changed to be checked by default, change port's default value, too.
851 if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
852 // Synchronize requiring SSL with the use SSL checkbox
853 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
854 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
856 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
857 ui.sslVerify->setEnabled(false);
858 ui.sslVerify->setChecked(false);
859 // Split up the message to allow re-using translations:
860 // [Original tool-tip]
861 // [Bold 'does not support feature' message]
862 // [Specific version needed and feature details]
863 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
864 ui.sslVerify->toolTip(),
865 tr("Your Quassel core does not support this feature"),
866 tr("You need a Quassel core v0.13.0 or newer in order to "
867 "verify connection security.")));
870 // read preset networks
871 QStringList networks = PresetNetworks::names();
872 foreach(QString s, existing)
873 networks.removeAll(s);
874 if (networks.count())
875 ui.presetList->addItems(networks);
877 ui.useManual->setChecked(true);
878 ui.usePreset->setEnabled(false);
880 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
881 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
886 NetworkInfo NetworkAddDlg::networkInfo() const
888 if (ui.useManual->isChecked()) {
890 info.networkName = ui.networkName->text().trimmed();
891 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
892 ui.serverPassword->text(), ui.useSSL->isChecked(),
893 ui.sslVerify->isChecked());
897 return PresetNetworks::networkInfo(ui.presetList->currentText());
901 void NetworkAddDlg::setButtonStates()
904 if (ui.usePreset->isChecked() && ui.presetList->count())
906 else if (ui.useManual->isChecked()) {
907 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
908 && !ui.serverAddress->text().isEmpty();
910 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
914 void NetworkAddDlg::updateSslPort(bool isChecked)
916 // "Use encrypted connection" was toggled, check the state...
917 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
918 // Had been using the plain-text port, use the SSL default
919 ui.port->setValue(Network::PORT_SSL);
920 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
921 // Had been using the SSL port, use the plain-text default
922 ui.port->setValue(Network::PORT_PLAINTEXT);
927 /**************************************************************************
929 *************************************************************************/
931 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
937 setWindowTitle(tr("Add Network"));
938 on_networkEdit_textChanged(""); // disable ok button
940 else ui.networkEdit->setText(old);
944 QString NetworkEditDlg::networkName() const
946 return ui.networkEdit->text().trimmed();
950 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
952 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
956 /**************************************************************************
958 *************************************************************************/
959 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
962 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
963 ui.host->setText(server.host);
965 ui.port->setValue(server.port);
966 ui.password->setText(server.password);
967 ui.useSSL->setChecked(server.useSsl);
968 ui.sslVerify->setChecked(server.sslVerify);
969 ui.sslVersion->setCurrentIndex(server.sslVersion);
970 ui.useProxy->setChecked(server.useProxy);
971 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
972 ui.proxyHost->setText(server.proxyHost);
973 ui.proxyPort->setValue(server.proxyPort);
974 ui.proxyUsername->setText(server.proxyUser);
975 ui.proxyPassword->setText(server.proxyPass);
977 // This is a dirty hack to display the core->IRC SSL protocol dropdown
978 // only if the core won't use autonegotiation to determine the best
979 // protocol. When autonegotiation was introduced, it would have been
980 // a good idea to use the CoreFeatures enum to accomplish this.
981 // However, since multiple versions have been released since then, that
982 // is no longer possible. Instead, we rely on the fact that the
983 // Datastream protocol was introduced in the same version (0.10) as SSL
984 // autonegotiation. Because of that, we can display the dropdown only
985 // if the Legacy protocol is in use. If any other RemotePeer protocol
986 // is in use, that means a newer protocol is in use and therefore the
987 // core will use autonegotiation.
988 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
990 ui.sslVersion->hide();
993 // Whenever useSSL is toggled, update the port number if not changed from the default
994 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
995 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
996 // If useSSL is later changed to be checked by default, change port's default value, too.
998 if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
999 // Synchronize requiring SSL with the use SSL checkbox
1000 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1001 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1003 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1004 ui.sslVerify->setEnabled(false);
1005 ui.sslVerify->setChecked(false);
1006 // Split up the message to allow re-using translations:
1007 // [Original tool-tip]
1008 // [Bold 'does not support feature' message]
1009 // [Specific version needed and feature details]
1010 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1011 ui.sslVerify->toolTip(),
1012 tr("Your Quassel core does not support this feature"),
1013 tr("You need a Quassel core v0.13.0 or newer in order to "
1014 "verify connection security.")));
1017 on_host_textChanged();
1021 Network::Server ServerEditDlg::serverData() const
1023 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1024 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1025 server.sslVersion = ui.sslVersion->currentIndex();
1026 server.useProxy = ui.useProxy->isChecked();
1027 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1028 server.proxyHost = ui.proxyHost->text();
1029 server.proxyPort = ui.proxyPort->value();
1030 server.proxyUser = ui.proxyUsername->text();
1031 server.proxyPass = ui.proxyPassword->text();
1036 void ServerEditDlg::on_host_textChanged()
1038 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1042 void ServerEditDlg::updateSslPort(bool isChecked)
1044 // "Use encrypted connection" was toggled, check the state...
1045 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1046 // Had been using the plain-text port, use the SSL default
1047 ui.port->setValue(Network::PORT_SSL);
1048 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1049 // Had been using the SSL port, use the plain-text default
1050 ui.port->setValue(Network::PORT_PLAINTEXT);
1055 /**************************************************************************
1057 *************************************************************************/
1059 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1063 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1066 ui.progressBar->setMaximum(numevents);
1067 ui.progressBar->setValue(0);
1069 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1070 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1072 foreach(NetworkId id, toRemove) {
1073 Client::removeNetwork(id);
1075 foreach(NetworkInfo info, toCreate) {
1076 Client::createNetwork(info);
1078 foreach(NetworkInfo info, toUpdate) {
1079 const Network *net = Client::network(info.networkId);
1081 qWarning() << "Invalid client network!";
1085 // FIXME this only checks for one changed item rather than all!
1086 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1087 Client::updateNetwork(info);
1091 qWarning() << "Sync dialog called without stuff to change!";
1097 void SaveNetworksDlg::clientEvent()
1099 ui.progressBar->setValue(++rcvevents);
1100 if (rcvevents >= numevents) accept();