1 /***************************************************************************
2 * Copyright (C) 2005-2012 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"
28 #include "iconloader.h"
31 #include "settingspagedlg.h"
34 #include "settingspages/identitiessettingspage.h"
36 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
37 : SettingsPage(tr("IRC"), tr("Networks"), parent)
41 // hide SASL options for older cores
42 if (!(Client::coreFeatures() & Quassel::SaslAuthentication))
46 ui.renameNetwork->setIcon(SmallIcon("edit-rename"));
47 ui.addNetwork->setIcon(SmallIcon("list-add"));
48 ui.deleteNetwork->setIcon(SmallIcon("edit-delete"));
49 ui.addServer->setIcon(SmallIcon("list-add"));
50 ui.deleteServer->setIcon(SmallIcon("edit-delete"));
51 ui.editServer->setIcon(SmallIcon("configure"));
52 ui.upServer->setIcon(SmallIcon("go-up"));
53 ui.downServer->setIcon(SmallIcon("go-down"));
54 ui.editIdentities->setIcon(SmallIcon("configure"));
56 _ignoreWidgetChanges = false;
58 connectedIcon = SmallIcon("network-connect");
59 connectingIcon = SmallIcon("network-wired"); // FIXME network-connecting
60 disconnectedIcon = SmallIcon("network-disconnect");
62 foreach(int mib, QTextCodec::availableMibs()) {
63 QByteArray codec = QTextCodec::codecForMib(mib)->name();
64 ui.sendEncoding->addItem(codec);
65 ui.recvEncoding->addItem(codec);
66 ui.serverEncoding->addItem(codec);
68 ui.sendEncoding->model()->sort(0);
69 ui.recvEncoding->model()->sort(0);
70 ui.serverEncoding->model()->sort(0);
72 setEnabled(Client::isConnected()); // need a core connection!
74 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
75 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
76 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
77 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
78 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
80 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
81 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
82 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
83 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
84 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
85 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
86 connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
87 connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
88 connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
89 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
90 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
91 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
92 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
93 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
94 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
95 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
96 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
97 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
99 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
101 foreach(IdentityId id, Client::identityIds()) {
102 clientIdentityAdded(id);
107 void NetworksSettingsPage::save()
110 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
112 QList<NetworkInfo> toCreate, toUpdate;
113 QList<NetworkId> toRemove;
114 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
115 while (i != networkInfos.end()) {
116 NetworkId id = (*i).networkId;
119 //if(id == currentId) currentId = 0;
120 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
121 //if(items.count()) {
122 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
125 //i = networkInfos.erase(i);
129 if ((*i) != Client::network((*i).networkId)->networkInfo()) {
135 foreach(NetworkId id, Client::networkIds()) {
136 if (!networkInfos.contains(id)) toRemove.append(id);
138 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
139 int ret = dlg.exec();
140 if (ret == QDialog::Rejected) {
141 // canceled -> reload everything to be safe
144 setChangedState(false);
149 void NetworksSettingsPage::load()
152 foreach(NetworkId netid, Client::networkIds()) {
153 clientNetworkAdded(netid);
155 ui.networkList->setCurrentRow(0);
156 setChangedState(false);
160 void NetworksSettingsPage::reset()
163 ui.networkList->clear();
164 networkInfos.clear();
168 bool NetworksSettingsPage::aboutToSave()
170 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
172 foreach(NetworkInfo info, networkInfos.values()) {
173 if (!info.serverList.count()) errors.append(1);
175 if (!errors.count()) return true;
176 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
177 if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
178 error += tr("</ul>");
179 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
184 void NetworksSettingsPage::widgetHasChanged()
186 if (_ignoreWidgetChanges) return;
187 bool changed = testHasChanged();
188 if (changed != hasChanged()) setChangedState(changed);
192 bool NetworksSettingsPage::testHasChanged()
194 if (currentId != 0) {
195 saveToNetworkInfo(networkInfos[currentId]);
197 if (Client::networkIds().count() != networkInfos.count()) return true;
198 foreach(NetworkId id, networkInfos.keys()) {
199 if (id < 0) return true;
200 if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
206 void NetworksSettingsPage::setWidgetStates()
209 if (ui.networkList->selectedItems().count()) {
210 ui.detailsBox->setEnabled(true);
211 ui.renameNetwork->setEnabled(true);
212 ui.deleteNetwork->setEnabled(true);
214 /* button disabled for now
215 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
216 const Network *net = id > 0 ? Client::network(id) : 0;
217 ui.connectNow->setEnabled(net);
218 // && (Client::network(id)->connectionState() == Network::Initialized
219 // || Client::network(id)->connectionState() == Network::Disconnected));
221 if(net->connectionState() == Network::Disconnected) {
222 ui.connectNow->setIcon(connectedIcon);
223 ui.connectNow->setText(tr("Connect"));
225 ui.connectNow->setIcon(disconnectedIcon);
226 ui.connectNow->setText(tr("Disconnect"));
229 ui.connectNow->setIcon(QIcon());
230 ui.connectNow->setText(tr("Apply first!"));
234 ui.renameNetwork->setEnabled(false);
235 ui.deleteNetwork->setEnabled(false);
236 //ui.connectNow->setEnabled(false);
237 ui.detailsBox->setEnabled(false);
240 if (ui.serverList->selectedItems().count()) {
241 ui.editServer->setEnabled(true);
242 ui.deleteServer->setEnabled(true);
243 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
244 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
247 ui.editServer->setEnabled(false);
248 ui.deleteServer->setEnabled(false);
249 ui.upServer->setEnabled(false);
250 ui.downServer->setEnabled(false);
255 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
257 if (!item && !(item = networkItem(id))) return;
258 const Network *net = Client::network(id);
259 if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
260 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
261 if (net && net->connectionState() == Network::Initialized) {
262 item->setIcon(connectedIcon);
264 else if (net && net->connectionState() != Network::Disconnected) {
265 item->setIcon(connectingIcon);
268 item->setIcon(disconnectedIcon);
272 // check if we already have another net of this name in the list, and replace it
273 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
275 foreach(QListWidgetItem *i, items) {
276 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
277 if (oldid > 0) continue; // only locally created nets should be replaced
278 if (oldid == currentId) {
281 ui.networkList->clearSelection();
283 int row = ui.networkList->row(i);
285 QListWidgetItem *olditem = ui.networkList->takeItem(row);
289 networkInfos.remove(oldid);
293 item->setText(net->networkName());
294 if (select) item->setSelected(true);
299 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
301 this->setEnabled(state);
312 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
314 const Identity *identity = Client::identity(id);
315 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
317 QString name = identity->identityName();
318 for (int j = 0; j < ui.identityList->count(); j++) {
319 if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
320 ui.identityList->insertItem(j, name, id.toInt());
326 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
331 void NetworksSettingsPage::clientIdentityUpdated()
333 const Identity *identity = qobject_cast<const Identity *>(sender());
335 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
338 int row = ui.identityList->findData(identity->id().toInt());
340 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
343 if (ui.identityList->itemText(row) != identity->identityName()) {
344 ui.identityList->setItemText(row, identity->identityName());
349 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
351 IdentityId defaultId = defaultIdentity();
352 if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
353 foreach(NetworkInfo info, networkInfos.values()) {
354 if (info.identity == id) {
355 if (info.networkId == currentId)
356 ui.identityList->setCurrentIndex(0);
357 info.identity = defaultId;
358 networkInfos[info.networkId] = info;
359 if (info.networkId > 0) Client::updateNetwork(info);
362 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
367 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
369 for (int i = 0; i < ui.networkList->count(); i++) {
370 QListWidgetItem *item = ui.networkList->item(i);
371 if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
377 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
380 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
381 connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
383 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
384 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
388 void NetworksSettingsPage::clientNetworkUpdated()
390 const Network *net = qobject_cast<const Network *>(sender());
392 qWarning() << "Update request for unknown network received!";
395 networkInfos[net->networkId()] = net->networkInfo();
396 setItemState(net->networkId());
397 if (net->networkId() == currentId) displayNetwork(net->networkId());
403 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
405 if (!networkInfos.contains(id)) return;
406 if (id == currentId) displayNetwork(0);
407 NetworkInfo info = networkInfos.take(id);
408 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
409 foreach(QListWidgetItem *item, items) {
410 if (item->data(Qt::UserRole).value<NetworkId>() == id)
411 delete ui.networkList->takeItem(ui.networkList->row(item));
418 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
421 const Network *net = qobject_cast<const Network *>(sender());
424 if(net->networkId() == currentId) {
425 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
428 setItemState(net->networkId());
433 void NetworksSettingsPage::networkConnectionError(const QString &)
438 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
440 NetworkInfo info = Client::network(id)->networkInfo();
441 networkInfos[id] = info;
442 return insertNetwork(info);
446 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
448 QListWidgetItem *item = 0;
449 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
450 if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
452 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
453 // -> then we can be sure that this is the core-side replacement for the net we created
454 foreach(QListWidgetItem *i, items) {
455 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
456 if (id < 0) { item = i; break; }
458 if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
460 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
461 setItemState(info.networkId, item);
467 void NetworksSettingsPage::displayNetwork(NetworkId id)
469 _ignoreWidgetChanges = true;
471 NetworkInfo info = networkInfos[id];
472 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
473 ui.serverList->clear();
474 foreach(Network::Server server, info.serverList) {
475 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
477 item->setIcon(SmallIcon("document-encrypt"));
478 ui.serverList->addItem(item);
481 //ui.randomServer->setChecked(info.useRandomServer);
482 ui.performEdit->setPlainText(info.perform.join("\n"));
483 ui.autoIdentify->setChecked(info.useAutoIdentify);
484 ui.autoIdentifyService->setText(info.autoIdentifyService);
485 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
486 ui.sasl->setChecked(info.useSasl);
487 ui.saslAccount->setText(info.saslAccount);
488 ui.saslPassword->setText(info.saslPassword);
489 if (info.codecForEncoding.isEmpty()) {
490 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
491 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
492 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
493 ui.useCustomEncodings->setChecked(false);
496 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
497 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
498 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
499 ui.useCustomEncodings->setChecked(true);
501 ui.autoReconnect->setChecked(info.useAutoReconnect);
502 ui.reconnectInterval->setValue(info.autoReconnectInterval);
503 ui.reconnectRetries->setValue(info.autoReconnectRetries);
504 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
505 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
508 // just clear widgets
509 ui.identityList->setCurrentIndex(-1);
510 ui.serverList->clear();
511 ui.performEdit->clear();
512 ui.autoIdentifyService->clear();
513 ui.autoIdentifyPassword->clear();
514 ui.saslAccount->clear();
515 ui.saslPassword->clear();
518 _ignoreWidgetChanges = false;
523 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
525 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
526 //info.useRandomServer = ui.randomServer->isChecked();
527 info.perform = ui.performEdit->toPlainText().split("\n");
528 info.useAutoIdentify = ui.autoIdentify->isChecked();
529 info.autoIdentifyService = ui.autoIdentifyService->text();
530 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
531 info.useSasl = ui.sasl->isChecked();
532 info.saslAccount = ui.saslAccount->text();
533 info.saslPassword = ui.saslPassword->text();
534 if (!ui.useCustomEncodings->isChecked()) {
535 info.codecForEncoding.clear();
536 info.codecForDecoding.clear();
537 info.codecForServer.clear();
540 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
541 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
542 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
544 info.useAutoReconnect = ui.autoReconnect->isChecked();
545 info.autoReconnectInterval = ui.reconnectInterval->value();
546 info.autoReconnectRetries = ui.reconnectRetries->value();
547 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
548 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
552 /*** Network list ***/
554 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
556 if (currentId != 0) {
557 saveToNetworkInfo(networkInfos[currentId]);
559 if (ui.networkList->selectedItems().count()) {
560 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
563 ui.serverList->setCurrentRow(0);
572 void NetworksSettingsPage::on_addNetwork_clicked()
574 QStringList existing;
575 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
576 NetworkAddDlg dlg(existing, this);
577 if (dlg.exec() == QDialog::Accepted) {
578 NetworkInfo info = dlg.networkInfo();
579 if (info.networkName.isEmpty())
580 return; // sanity check
583 for (id = 1; id <= networkInfos.count(); id++) {
585 if (!networkInfos.keys().contains(-id.toInt())) break;
589 info.identity = defaultIdentity();
590 networkInfos[id] = info;
591 QListWidgetItem *item = insertNetwork(info);
592 ui.networkList->setCurrentItem(item);
598 void NetworksSettingsPage::on_deleteNetwork_clicked()
600 if (ui.networkList->selectedItems().count()) {
601 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
602 int ret = QMessageBox::question(this, tr("Delete Network?"),
603 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
604 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
605 if (ret == QMessageBox::Yes) {
607 networkInfos.remove(netid);
608 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
609 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
617 void NetworksSettingsPage::on_renameNetwork_clicked()
619 if (!ui.networkList->selectedItems().count()) return;
620 QString old = ui.networkList->selectedItems()[0]->text();
621 QStringList existing;
622 for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
623 NetworkEditDlg dlg(old, existing, this);
624 if (dlg.exec() == QDialog::Accepted) {
625 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
626 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
627 networkInfos[netid].networkName = dlg.networkName();
634 void NetworksSettingsPage::on_connectNow_clicked() {
635 if(!ui.networkList->selectedItems().count()) return;
636 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
637 const Network *net = Client::network(id);
639 if(net->connectionState() == Network::Disconnected) net->requestConnect();
640 else net->requestDisconnect();
644 /*** Server list ***/
646 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
652 void NetworksSettingsPage::on_addServer_clicked()
654 if (currentId == 0) return;
655 ServerEditDlg dlg(Network::Server(), this);
656 if (dlg.exec() == QDialog::Accepted) {
657 networkInfos[currentId].serverList.append(dlg.serverData());
658 displayNetwork(currentId);
659 ui.serverList->setCurrentRow(ui.serverList->count()-1);
665 void NetworksSettingsPage::on_editServer_clicked()
667 if (currentId == 0) return;
668 int cur = ui.serverList->currentRow();
669 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
670 if (dlg.exec() == QDialog::Accepted) {
671 networkInfos[currentId].serverList[cur] = dlg.serverData();
672 displayNetwork(currentId);
673 ui.serverList->setCurrentRow(cur);
679 void NetworksSettingsPage::on_deleteServer_clicked()
681 if (currentId == 0) return;
682 int cur = ui.serverList->currentRow();
683 networkInfos[currentId].serverList.removeAt(cur);
684 displayNetwork(currentId);
685 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
690 void NetworksSettingsPage::on_upServer_clicked()
692 int cur = ui.serverList->currentRow();
693 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
694 networkInfos[currentId].serverList.insert(cur-1, server);
695 displayNetwork(currentId);
696 ui.serverList->setCurrentRow(cur-1);
701 void NetworksSettingsPage::on_downServer_clicked()
703 int cur = ui.serverList->currentRow();
704 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
705 networkInfos[currentId].serverList.insert(cur+1, server);
706 displayNetwork(currentId);
707 ui.serverList->setCurrentRow(cur+1);
712 void NetworksSettingsPage::on_editIdentities_clicked()
714 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
719 IdentityId NetworksSettingsPage::defaultIdentity() const
721 IdentityId defaultId = 0;
722 QList<IdentityId> ids = Client::identityIds();
723 foreach(IdentityId id, ids) {
724 if (defaultId == 0 || id < defaultId)
731 /**************************************************************************
733 *************************************************************************/
735 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
738 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
740 // read preset networks
741 QStringList networks = Network::presetNetworks();
742 foreach(QString s, existing)
743 networks.removeAll(s);
744 if (networks.count())
745 ui.presetList->addItems(networks);
747 ui.useManual->setChecked(true);
748 ui.usePreset->setEnabled(false);
750 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
751 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
756 NetworkInfo NetworkAddDlg::networkInfo() const
758 if (ui.useManual->isChecked()) {
760 info.networkName = ui.networkName->text().trimmed();
761 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(), ui.serverPassword->text(), ui.useSSL->isChecked());
765 return Network::networkInfoFromPreset(ui.presetList->currentText());
769 void NetworkAddDlg::setButtonStates()
772 if (ui.usePreset->isChecked() && ui.presetList->count())
774 else if (ui.useManual->isChecked()) {
775 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
776 && !ui.serverAddress->text().isEmpty();
778 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
782 /**************************************************************************
784 *************************************************************************/
786 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
792 setWindowTitle(tr("Add Network"));
793 on_networkEdit_textChanged(""); // disable ok button
795 else ui.networkEdit->setText(old);
799 QString NetworkEditDlg::networkName() const
801 return ui.networkEdit->text().trimmed();
805 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
807 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
811 /**************************************************************************
813 *************************************************************************/
814 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
817 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
818 ui.host->setText(server.host);
819 ui.port->setValue(server.port);
820 ui.password->setText(server.password);
821 ui.useSSL->setChecked(server.useSsl);
822 ui.sslVersion->setCurrentIndex(server.sslVersion);
823 ui.useProxy->setChecked(server.useProxy);
824 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
825 ui.proxyHost->setText(server.proxyHost);
826 ui.proxyPort->setValue(server.proxyPort);
827 ui.proxyUsername->setText(server.proxyUser);
828 ui.proxyPassword->setText(server.proxyPass);
829 on_host_textChanged();
833 Network::Server ServerEditDlg::serverData() const
835 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked());
836 server.sslVersion = ui.sslVersion->currentIndex();
837 server.useProxy = ui.useProxy->isChecked();
838 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
839 server.proxyHost = ui.proxyHost->text();
840 server.proxyPort = ui.proxyPort->value();
841 server.proxyUser = ui.proxyUsername->text();
842 server.proxyPass = ui.proxyPassword->text();
847 void ServerEditDlg::on_host_textChanged()
849 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
853 /**************************************************************************
855 *************************************************************************/
857 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
861 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
864 ui.progressBar->setMaximum(numevents);
865 ui.progressBar->setValue(0);
867 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
868 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
870 foreach(NetworkId id, toRemove) {
871 Client::removeNetwork(id);
873 foreach(NetworkInfo info, toCreate) {
874 Client::createNetwork(info);
876 foreach(NetworkInfo info, toUpdate) {
877 const Network *net = Client::network(info.networkId);
879 qWarning() << "Invalid client network!";
883 // FIXME this only checks for one changed item rather than all!
884 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
885 Client::updateNetwork(info);
889 qWarning() << "Sync dialog called without stuff to change!";
895 void SaveNetworksDlg::clientEvent()
897 ui.progressBar->setValue(++rcvevents);
898 if (rcvevents >= numevents) accept();