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()));
107 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
108 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
110 foreach(IdentityId id, Client::identityIds()) {
111 clientIdentityAdded(id);
116 void NetworksSettingsPage::save()
119 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
121 QList<NetworkInfo> toCreate, toUpdate;
122 QList<NetworkId> toRemove;
123 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
124 while (i != networkInfos.end()) {
125 NetworkId id = (*i).networkId;
128 //if(id == currentId) currentId = 0;
129 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
130 //if(items.count()) {
131 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
134 //i = networkInfos.erase(i);
138 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
144 foreach(NetworkId id, Client::networkIds()) {
145 if (!networkInfos.contains(id)) toRemove.append(id);
147 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
148 int ret = dlg.exec();
149 if (ret == QDialog::Rejected) {
150 // canceled -> reload everything to be safe
153 setChangedState(false);
158 void NetworksSettingsPage::load()
161 foreach(NetworkId netid, Client::networkIds()) {
162 clientNetworkAdded(netid);
164 ui.networkList->setCurrentRow(0);
165 setChangedState(false);
169 void NetworksSettingsPage::reset()
172 ui.networkList->clear();
173 networkInfos.clear();
177 bool NetworksSettingsPage::aboutToSave()
179 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
181 foreach(NetworkInfo info, networkInfos.values()) {
182 if (!info.serverList.count()) errors.append(1);
184 if (!errors.count()) return true;
185 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
186 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
187 error += tr("</ul>");
188 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
193 void NetworksSettingsPage::widgetHasChanged()
195 if (_ignoreWidgetChanges) return;
196 bool changed = testHasChanged();
197 if (changed != hasChanged()) setChangedState(changed);
201 bool NetworksSettingsPage::testHasChanged()
203 if (currentId != 0) {
204 saveToNetworkInfo(networkInfos[currentId]);
206 if (Client::networkIds().count() != networkInfos.count()) return true;
207 foreach(NetworkId id, networkInfos.keys()) {
208 if (id < 0) return true;
209 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
215 void NetworksSettingsPage::setWidgetStates()
218 if (ui.networkList->selectedItems().count()) {
219 ui.detailsBox->setEnabled(true);
220 ui.renameNetwork->setEnabled(true);
221 ui.deleteNetwork->setEnabled(true);
223 /* button disabled for now
224 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
225 const Network *net = id > 0 ? Client::network(id) : 0;
226 ui.connectNow->setEnabled(net);
227 // && (Client::network(id)->connectionState() == Network::Initialized
228 // || Client::network(id)->connectionState() == Network::Disconnected));
230 if(net->connectionState() == Network::Disconnected) {
231 ui.connectNow->setIcon(connectedIcon);
232 ui.connectNow->setText(tr("Connect"));
234 ui.connectNow->setIcon(disconnectedIcon);
235 ui.connectNow->setText(tr("Disconnect"));
238 ui.connectNow->setIcon(QIcon());
239 ui.connectNow->setText(tr("Apply first!"));
243 ui.renameNetwork->setEnabled(false);
244 ui.deleteNetwork->setEnabled(false);
245 //ui.connectNow->setEnabled(false);
246 ui.detailsBox->setEnabled(false);
249 if (ui.serverList->selectedItems().count()) {
250 ui.editServer->setEnabled(true);
251 ui.deleteServer->setEnabled(true);
252 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
253 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
256 ui.editServer->setEnabled(false);
257 ui.deleteServer->setEnabled(false);
258 ui.upServer->setEnabled(false);
259 ui.downServer->setEnabled(false);
264 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
266 if (!item && !(item = networkItem(id))) return;
267 const Network *net = Client::network(id);
268 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
269 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
270 if (net && net->connectionState() == Network::Initialized) {
271 item->setIcon(connectedIcon);
273 else if (net && net->connectionState() != Network::Disconnected) {
274 item->setIcon(connectingIcon);
277 item->setIcon(disconnectedIcon);
281 // check if we already have another net of this name in the list, and replace it
282 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
284 foreach(QListWidgetItem *i, items) {
285 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
286 if (oldid > 0) continue; // only locally created nets should be replaced
287 if (oldid == currentId) {
290 ui.networkList->clearSelection();
292 int row = ui.networkList->row(i);
294 QListWidgetItem *olditem = ui.networkList->takeItem(row);
298 networkInfos.remove(oldid);
302 item->setText(net->networkName());
303 if (select) item->setSelected(true);
308 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
310 this->setEnabled(state);
321 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
323 const Identity *identity = Client::identity(id);
324 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
326 QString name = identity->identityName();
327 for (int j = 0; j < ui.identityList->count(); j++) {
328 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
329 ui.identityList->insertItem(j, name, id.toInt());
335 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
340 void NetworksSettingsPage::clientIdentityUpdated()
342 const Identity *identity = qobject_cast<const Identity *>(sender());
344 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
347 int row = ui.identityList->findData(identity->id().toInt());
349 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
352 if (ui.identityList->itemText(row) != identity->identityName()) {
353 ui.identityList->setItemText(row, identity->identityName());
358 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
360 IdentityId defaultId = defaultIdentity();
361 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
362 foreach(NetworkInfo info, networkInfos.values()) {
363 if (info.identity == id) {
364 if (info.networkId == currentId)
365 ui.identityList->setCurrentIndex(0);
366 info.identity = defaultId;
367 networkInfos[info.networkId] = info;
368 if (info.networkId > 0) Client::updateNetwork(info);
371 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
376 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
378 for (int i = 0; i < ui.networkList->count(); i++) {
379 QListWidgetItem *item = ui.networkList->item(i);
380 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
386 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
389 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
390 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
392 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
393 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
397 void NetworksSettingsPage::clientNetworkUpdated()
399 const Network *net = qobject_cast<const Network *>(sender());
401 qWarning() << "Update request for unknown network received!";
404 networkInfos[net->networkId()] = net->networkInfo();
405 setItemState(net->networkId());
406 if (net->networkId() == currentId) displayNetwork(net->networkId());
412 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
414 if (!networkInfos.contains(id)) return;
415 if (id == currentId) displayNetwork(0);
416 NetworkInfo info = networkInfos.take(id);
417 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
418 foreach(QListWidgetItem *item, items) {
419 if (item->data(Qt::UserRole).value<NetworkId>() == id)
420 delete ui.networkList->takeItem(ui.networkList->row(item));
427 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
430 const Network *net = qobject_cast<const Network *>(sender());
433 if(net->networkId() == currentId) {
434 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
437 setItemState(net->networkId());
442 void NetworksSettingsPage::networkConnectionError(const QString &)
447 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
449 NetworkInfo info = Client::network(id)->networkInfo();
450 networkInfos[id] = info;
451 return insertNetwork(info);
455 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
457 QListWidgetItem *item = 0;
458 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
459 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
461 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
462 // -> then we can be sure that this is the core-side replacement for the net we created
463 foreach(QListWidgetItem *i, items) {
464 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
465 if (id < 0) { item = i; break; }
467 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
469 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
470 setItemState(info.networkId, item);
476 void NetworksSettingsPage::displayNetwork(NetworkId id)
478 _ignoreWidgetChanges = true;
480 NetworkInfo info = networkInfos[id];
483 // this is only needed when the core supports SASL EXTERNAL
484 if (Client::coreFeatures() & Quassel::SaslExternal) {
486 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
489 _cid = new CertIdentity(*Client::identity(info.identity), this);
490 _cid->enableEditSsl(true);
491 connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
495 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
496 ui.serverList->clear();
497 foreach(Network::Server server, info.serverList) {
498 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
500 item->setIcon(QIcon::fromTheme("document-encrypt"));
501 ui.serverList->addItem(item);
504 //ui.randomServer->setChecked(info.useRandomServer);
505 ui.performEdit->setPlainText(info.perform.join("\n"));
506 ui.autoIdentify->setChecked(info.useAutoIdentify);
507 ui.autoIdentifyService->setText(info.autoIdentifyService);
508 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
509 ui.sasl->setChecked(info.useSasl);
510 ui.saslAccount->setText(info.saslAccount);
511 ui.saslPassword->setText(info.saslPassword);
512 if (info.codecForEncoding.isEmpty()) {
513 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
514 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
515 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
516 ui.useCustomEncodings->setChecked(false);
519 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
520 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
521 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
522 ui.useCustomEncodings->setChecked(true);
524 ui.autoReconnect->setChecked(info.useAutoReconnect);
525 ui.reconnectInterval->setValue(info.autoReconnectInterval);
526 ui.reconnectRetries->setValue(info.autoReconnectRetries);
527 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
528 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
531 // just clear widgets
534 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
538 ui.identityList->setCurrentIndex(-1);
539 ui.serverList->clear();
540 ui.performEdit->clear();
541 ui.autoIdentifyService->clear();
542 ui.autoIdentifyPassword->clear();
543 ui.saslAccount->clear();
544 ui.saslPassword->clear();
547 _ignoreWidgetChanges = false;
552 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
554 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
555 //info.useRandomServer = ui.randomServer->isChecked();
556 info.perform = ui.performEdit->toPlainText().split("\n");
557 info.useAutoIdentify = ui.autoIdentify->isChecked();
558 info.autoIdentifyService = ui.autoIdentifyService->text();
559 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
560 info.useSasl = ui.sasl->isChecked();
561 info.saslAccount = ui.saslAccount->text();
562 info.saslPassword = ui.saslPassword->text();
563 if (!ui.useCustomEncodings->isChecked()) {
564 info.codecForEncoding.clear();
565 info.codecForDecoding.clear();
566 info.codecForServer.clear();
569 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
570 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
571 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
573 info.useAutoReconnect = ui.autoReconnect->isChecked();
574 info.autoReconnectInterval = ui.reconnectInterval->value();
575 info.autoReconnectRetries = ui.reconnectRetries->value();
576 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
577 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
582 void NetworksSettingsPage::sslUpdated()
584 if (_cid && !_cid->sslKey().isNull()) {
585 ui.saslAccount->setDisabled(true);
586 ui.saslAccountLabel->setDisabled(true);
587 ui.saslPassword->setDisabled(true);
588 ui.saslPasswordLabel->setDisabled(true);
589 ui.saslExtInfo->setHidden(false);
591 ui.saslAccount->setDisabled(false);
592 ui.saslAccountLabel->setDisabled(false);
593 ui.saslPassword->setDisabled(false);
594 ui.saslPasswordLabel->setDisabled(false);
595 ui.saslExtInfo->setHidden(true);
601 /*** Network list ***/
603 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
605 if (currentId != 0) {
606 saveToNetworkInfo(networkInfos[currentId]);
608 if (ui.networkList->selectedItems().count()) {
609 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
612 ui.serverList->setCurrentRow(0);
621 void NetworksSettingsPage::on_addNetwork_clicked()
623 QStringList existing;
624 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
625 NetworkAddDlg dlg(existing, this);
626 if (dlg.exec() == QDialog::Accepted) {
627 NetworkInfo info = dlg.networkInfo();
628 if (info.networkName.isEmpty())
629 return; // sanity check
632 for (id = 1; id <= networkInfos.count(); id++) {
634 if (!networkInfos.keys().contains(-id.toInt())) break;
638 info.identity = defaultIdentity();
639 networkInfos[id] = info;
640 QListWidgetItem *item = insertNetwork(info);
641 ui.networkList->setCurrentItem(item);
647 void NetworksSettingsPage::on_deleteNetwork_clicked()
649 if (ui.networkList->selectedItems().count()) {
650 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
651 int ret = QMessageBox::question(this, tr("Delete Network?"),
652 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
653 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
654 if (ret == QMessageBox::Yes) {
656 networkInfos.remove(netid);
657 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
658 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
666 void NetworksSettingsPage::on_renameNetwork_clicked()
668 if (!ui.networkList->selectedItems().count()) return;
669 QString old = ui.networkList->selectedItems()[0]->text();
670 QStringList existing;
671 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
672 NetworkEditDlg dlg(old, existing, this);
673 if (dlg.exec() == QDialog::Accepted) {
674 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
675 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
676 networkInfos[netid].networkName = dlg.networkName();
683 void NetworksSettingsPage::on_connectNow_clicked() {
684 if(!ui.networkList->selectedItems().count()) return;
685 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
686 const Network *net = Client::network(id);
688 if(net->connectionState() == Network::Disconnected) net->requestConnect();
689 else net->requestDisconnect();
693 /*** Server list ***/
695 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
701 void NetworksSettingsPage::on_addServer_clicked()
703 if (currentId == 0) return;
704 ServerEditDlg dlg(Network::Server(), this);
705 if (dlg.exec() == QDialog::Accepted) {
706 networkInfos[currentId].serverList.append(dlg.serverData());
707 displayNetwork(currentId);
708 ui.serverList->setCurrentRow(ui.serverList->count()-1);
714 void NetworksSettingsPage::on_editServer_clicked()
716 if (currentId == 0) return;
717 int cur = ui.serverList->currentRow();
718 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
719 if (dlg.exec() == QDialog::Accepted) {
720 networkInfos[currentId].serverList[cur] = dlg.serverData();
721 displayNetwork(currentId);
722 ui.serverList->setCurrentRow(cur);
728 void NetworksSettingsPage::on_deleteServer_clicked()
730 if (currentId == 0) return;
731 int cur = ui.serverList->currentRow();
732 networkInfos[currentId].serverList.removeAt(cur);
733 displayNetwork(currentId);
734 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
739 void NetworksSettingsPage::on_upServer_clicked()
741 int cur = ui.serverList->currentRow();
742 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
743 networkInfos[currentId].serverList.insert(cur-1, server);
744 displayNetwork(currentId);
745 ui.serverList->setCurrentRow(cur-1);
750 void NetworksSettingsPage::on_downServer_clicked()
752 int cur = ui.serverList->currentRow();
753 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
754 networkInfos[currentId].serverList.insert(cur+1, server);
755 displayNetwork(currentId);
756 ui.serverList->setCurrentRow(cur+1);
761 void NetworksSettingsPage::on_editIdentities_clicked()
763 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
768 IdentityId NetworksSettingsPage::defaultIdentity() const
770 IdentityId defaultId = 0;
771 QList<IdentityId> ids = Client::identityIds();
772 foreach(IdentityId id, ids) {
773 if (defaultId == 0 || id < defaultId)
780 /**************************************************************************
782 *************************************************************************/
784 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
787 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
789 // Whenever useSSL is toggled, update the port number if not changed from the default
790 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
791 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
792 // If useSSL is later changed to be checked by default, change port's default value, too.
794 if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
795 // Synchronize requiring SSL with the use SSL checkbox
796 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
797 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
799 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
800 ui.sslVerify->setEnabled(false);
801 ui.sslVerify->setChecked(false);
802 // Split up the message to allow re-using translations:
803 // [Original tool-tip]
804 // [Bold 'does not support feature' message]
805 // [Specific version needed and feature details]
806 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
807 ui.sslVerify->toolTip(),
808 tr("Your Quassel core does not support this feature"),
809 tr("You need a Quassel core v0.13.0 or newer in order to "
810 "verify connection security.")));
813 // read preset networks
814 QStringList networks = PresetNetworks::names();
815 foreach(QString s, existing)
816 networks.removeAll(s);
817 if (networks.count())
818 ui.presetList->addItems(networks);
820 ui.useManual->setChecked(true);
821 ui.usePreset->setEnabled(false);
823 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
824 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
829 NetworkInfo NetworkAddDlg::networkInfo() const
831 if (ui.useManual->isChecked()) {
833 info.networkName = ui.networkName->text().trimmed();
834 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
835 ui.serverPassword->text(), ui.useSSL->isChecked(),
836 ui.sslVerify->isChecked());
840 return PresetNetworks::networkInfo(ui.presetList->currentText());
844 void NetworkAddDlg::setButtonStates()
847 if (ui.usePreset->isChecked() && ui.presetList->count())
849 else if (ui.useManual->isChecked()) {
850 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
851 && !ui.serverAddress->text().isEmpty();
853 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
857 void NetworkAddDlg::updateSslPort(bool isChecked)
859 // "Use encrypted connection" was toggled, check the state...
860 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
861 // Had been using the plain-text port, use the SSL default
862 ui.port->setValue(Network::PORT_SSL);
863 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
864 // Had been using the SSL port, use the plain-text default
865 ui.port->setValue(Network::PORT_PLAINTEXT);
870 /**************************************************************************
872 *************************************************************************/
874 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
880 setWindowTitle(tr("Add Network"));
881 on_networkEdit_textChanged(""); // disable ok button
883 else ui.networkEdit->setText(old);
887 QString NetworkEditDlg::networkName() const
889 return ui.networkEdit->text().trimmed();
893 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
895 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
899 /**************************************************************************
901 *************************************************************************/
902 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
905 ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
906 ui.host->setText(server.host);
908 ui.port->setValue(server.port);
909 ui.password->setText(server.password);
910 ui.useSSL->setChecked(server.useSsl);
911 ui.sslVerify->setChecked(server.sslVerify);
912 ui.sslVersion->setCurrentIndex(server.sslVersion);
913 ui.useProxy->setChecked(server.useProxy);
914 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
915 ui.proxyHost->setText(server.proxyHost);
916 ui.proxyPort->setValue(server.proxyPort);
917 ui.proxyUsername->setText(server.proxyUser);
918 ui.proxyPassword->setText(server.proxyPass);
920 // This is a dirty hack to display the core->IRC SSL protocol dropdown
921 // only if the core won't use autonegotiation to determine the best
922 // protocol. When autonegotiation was introduced, it would have been
923 // a good idea to use the CoreFeatures enum to accomplish this.
924 // However, since multiple versions have been released since then, that
925 // is no longer possible. Instead, we rely on the fact that the
926 // Datastream protocol was introduced in the same version (0.10) as SSL
927 // autonegotiation. Because of that, we can display the dropdown only
928 // if the Legacy protocol is in use. If any other RemotePeer protocol
929 // is in use, that means a newer protocol is in use and therefore the
930 // core will use autonegotiation.
931 if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
933 ui.sslVersion->hide();
936 // Whenever useSSL is toggled, update the port number if not changed from the default
937 connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
938 // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
939 // If useSSL is later changed to be checked by default, change port's default value, too.
941 if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
942 // Synchronize requiring SSL with the use SSL checkbox
943 ui.sslVerify->setEnabled(ui.useSSL->isChecked());
944 connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
946 // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
947 ui.sslVerify->setEnabled(false);
948 ui.sslVerify->setChecked(false);
949 // Split up the message to allow re-using translations:
950 // [Original tool-tip]
951 // [Bold 'does not support feature' message]
952 // [Specific version needed and feature details]
953 ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
954 ui.sslVerify->toolTip(),
955 tr("Your Quassel core does not support this feature"),
956 tr("You need a Quassel core v0.13.0 or newer in order to "
957 "verify connection security.")));
960 on_host_textChanged();
964 Network::Server ServerEditDlg::serverData() const
966 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
967 ui.useSSL->isChecked(), ui.sslVerify->isChecked());
968 server.sslVersion = ui.sslVersion->currentIndex();
969 server.useProxy = ui.useProxy->isChecked();
970 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
971 server.proxyHost = ui.proxyHost->text();
972 server.proxyPort = ui.proxyPort->value();
973 server.proxyUser = ui.proxyUsername->text();
974 server.proxyPass = ui.proxyPassword->text();
979 void ServerEditDlg::on_host_textChanged()
981 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
985 void ServerEditDlg::updateSslPort(bool isChecked)
987 // "Use encrypted connection" was toggled, check the state...
988 if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
989 // Had been using the plain-text port, use the SSL default
990 ui.port->setValue(Network::PORT_SSL);
991 } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
992 // Had been using the SSL port, use the plain-text default
993 ui.port->setValue(Network::PORT_PLAINTEXT);
998 /**************************************************************************
1000 *************************************************************************/
1002 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1006 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1009 ui.progressBar->setMaximum(numevents);
1010 ui.progressBar->setValue(0);
1012 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1013 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1015 foreach(NetworkId id, toRemove) {
1016 Client::removeNetwork(id);
1018 foreach(NetworkInfo info, toCreate) {
1019 Client::createNetwork(info);
1021 foreach(NetworkInfo info, toUpdate) {
1022 const Network *net = Client::network(info.networkId);
1024 qWarning() << "Invalid client network!";
1028 // FIXME this only checks for one changed item rather than all!
1029 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1030 Client::updateNetwork(info);
1034 qWarning() << "Sync dialog called without stuff to change!";
1040 void SaveNetworksDlg::clientEvent()
1042 ui.progressBar->setValue(++rcvevents);
1043 if (rcvevents >= numevents) accept();