1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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("Misc"), tr("Networks"), parent) {
41 ui.renameNetwork->setIcon(SmallIcon("edit-rename"));
42 ui.addNetwork->setIcon(SmallIcon("list-add"));
43 ui.deleteNetwork->setIcon(SmallIcon("edit-delete"));
44 ui.addServer->setIcon(SmallIcon("list-add"));
45 ui.deleteServer->setIcon(SmallIcon("edit-delete"));
46 ui.editServer->setIcon(SmallIcon("configure"));
47 ui.upServer->setIcon(SmallIcon("go-up"));
48 ui.downServer->setIcon(SmallIcon("go-down"));
49 ui.editIdentities->setIcon(SmallIcon("configure"));
51 _ignoreWidgetChanges = false;
53 connectedIcon = SmallIcon("network-connect");
54 connectingIcon = SmallIcon("network-wired"); // FIXME network-connecting
55 disconnectedIcon = SmallIcon("network-disconnect");
57 foreach(int mib, QTextCodec::availableMibs()) {
58 QByteArray codec = QTextCodec::codecForMib(mib)->name();
59 ui.sendEncoding->addItem(codec);
60 ui.recvEncoding->addItem(codec);
61 ui.serverEncoding->addItem(codec);
63 ui.sendEncoding->model()->sort(0);
64 ui.recvEncoding->model()->sort(0);
65 ui.serverEncoding->model()->sort(0);
67 setEnabled(Client::isConnected()); // need a core connection!
69 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
70 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
71 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
72 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
73 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
75 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
76 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
77 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
78 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
79 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
80 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
81 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
82 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
83 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
84 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
85 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
86 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
87 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
88 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
89 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
90 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
91 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
93 foreach(IdentityId id, Client::identityIds()) {
94 clientIdentityAdded(id);
98 void NetworksSettingsPage::save() {
100 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
102 QList<NetworkInfo> toCreate, toUpdate;
103 QList<NetworkId> toRemove;
104 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
105 while(i != networkInfos.end()) {
106 NetworkId id = (*i).networkId;
109 //if(id == currentId) currentId = 0;
110 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
111 //if(items.count()) {
112 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
115 //i = networkInfos.erase(i);
118 if((*i) != Client::network((*i).networkId)->networkInfo()) {
124 foreach(NetworkId id, Client::networkIds()) {
125 if(!networkInfos.contains(id)) toRemove.append(id);
127 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
128 int ret = dlg.exec();
129 if(ret == QDialog::Rejected) {
130 // canceled -> reload everything to be safe
133 setChangedState(false);
137 void NetworksSettingsPage::load() {
139 foreach(NetworkId netid, Client::networkIds()) {
140 clientNetworkAdded(netid);
142 ui.networkList->setCurrentRow(0);
143 setChangedState(false);
146 void NetworksSettingsPage::reset() {
148 ui.networkList->clear();
149 networkInfos.clear();
153 bool NetworksSettingsPage::aboutToSave() {
154 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
156 foreach(NetworkInfo info, networkInfos.values()) {
157 if(!info.serverList.count()) errors.append(1);
159 if(!errors.count()) return true;
160 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
161 if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
162 error += tr("</ul>");
163 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
167 void NetworksSettingsPage::widgetHasChanged() {
168 if(_ignoreWidgetChanges) return;
169 bool changed = testHasChanged();
170 if(changed != hasChanged()) setChangedState(changed);
173 bool NetworksSettingsPage::testHasChanged() {
175 saveToNetworkInfo(networkInfos[currentId]);
177 if(Client::networkIds().count() != networkInfos.count()) return true;
178 foreach(NetworkId id, networkInfos.keys()) {
179 if(id < 0) return true;
180 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
185 void NetworksSettingsPage::setWidgetStates() {
187 if(ui.networkList->selectedItems().count()) {
188 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
189 const Network *net = 0;
190 if(id > 0) net = Client::network(id);
191 ui.detailsBox->setEnabled(true);
192 ui.renameNetwork->setEnabled(true);
193 ui.deleteNetwork->setEnabled(true);
194 /* button disabled for now
195 ui.connectNow->setEnabled(net);
196 // && (Client::network(id)->connectionState() == Network::Initialized
197 // || Client::network(id)->connectionState() == Network::Disconnected));
199 if(net->connectionState() == Network::Disconnected) {
200 ui.connectNow->setIcon(connectedIcon);
201 ui.connectNow->setText(tr("Connect"));
203 ui.connectNow->setIcon(disconnectedIcon);
204 ui.connectNow->setText(tr("Disconnect"));
207 ui.connectNow->setIcon(QIcon());
208 ui.connectNow->setText(tr("Apply first!"));
211 ui.renameNetwork->setEnabled(false);
212 ui.deleteNetwork->setEnabled(false);
213 //ui.connectNow->setEnabled(false);
214 ui.detailsBox->setEnabled(false);
217 if(ui.serverList->selectedItems().count()) {
218 ui.editServer->setEnabled(true);
219 ui.deleteServer->setEnabled(true);
220 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
221 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
223 ui.editServer->setEnabled(false);
224 ui.deleteServer->setEnabled(false);
225 ui.upServer->setEnabled(false);
226 ui.downServer->setEnabled(false);
230 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
231 if(!item && !(item = networkItem(id))) return;
232 const Network *net = Client::network(id);
233 if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
234 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
235 if(net && net->connectionState() == Network::Initialized) {
236 item->setIcon(connectedIcon);
237 } else if(net && net->connectionState() != Network::Disconnected) {
238 item->setIcon(connectingIcon);
240 item->setIcon(disconnectedIcon);
244 // check if we already have another net of this name in the list, and replace it
245 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
247 foreach(QListWidgetItem *i, items) {
248 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
249 if(oldid > 0) continue; // only locally created nets should be replaced
250 if(oldid == currentId) {
253 ui.networkList->clearSelection();
255 int row = ui.networkList->row(i);
257 QListWidgetItem *olditem = ui.networkList->takeItem(row);
261 networkInfos.remove(oldid);
265 item->setText(net->networkName());
266 if(select) item->setSelected(true);
270 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
271 this->setEnabled(state);
280 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
281 const Identity * identity = Client::identity(id);
282 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
284 QString name = identity->identityName();
285 for(int j = 0; j < ui.identityList->count(); j++) {
286 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
287 ui.identityList->insertItem(j, name, id.toInt());
293 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
297 void NetworksSettingsPage::clientIdentityUpdated() {
298 const Identity *identity = qobject_cast<const Identity *>(sender());
300 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
303 int row = ui.identityList->findData(identity->id().toInt());
305 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
308 if(ui.identityList->itemText(row) != identity->identityName()) {
309 ui.identityList->setItemText(row, identity->identityName());
313 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
314 IdentityId defaultId = defaultIdentity();
315 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
316 foreach(NetworkInfo info, networkInfos.values()) {
317 if(info.identity == id) {
318 if(info.networkId == currentId)
319 ui.identityList->setCurrentIndex(0);
320 info.identity = defaultId;
321 networkInfos[info.networkId] = info;
322 if(info.networkId > 0) Client::updateNetwork(info);
325 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
329 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
330 for(int i = 0; i < ui.networkList->count(); i++) {
331 QListWidgetItem *item = ui.networkList->item(i);
332 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
337 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
339 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
340 connect(Client::network(id), SIGNAL(identitySet(IdentityId)), this, SLOT(clientNetworkUpdated()));
341 connect(Client::network(id), SIGNAL(networkNameSet(const QString &)), this, SLOT(clientNetworkUpdated()));
342 connect(Client::network(id), SIGNAL(serverListSet(QVariantList)), this, SLOT(clientNetworkUpdated()));
343 connect(Client::network(id), SIGNAL(useRandomServerSet(bool)), this, SLOT(clientNetworkUpdated()));
344 connect(Client::network(id), SIGNAL(performSet(const QStringList &)), this, SLOT(clientNetworkUpdated()));
345 connect(Client::network(id), SIGNAL(useAutoIdentifySet(bool)), this, SLOT(clientNetworkUpdated()));
346 connect(Client::network(id), SIGNAL(autoIdentifyServiceSet(const QString &)), this, SLOT(clientNetworkUpdated()));
347 connect(Client::network(id), SIGNAL(autoIdentifyPasswordSet(const QString &)), this, SLOT(clientNetworkUpdated()));
348 connect(Client::network(id), SIGNAL(useAutoReconnectSet(bool)), this, SLOT(clientNetworkUpdated()));
349 connect(Client::network(id), SIGNAL(autoReconnectIntervalSet(quint32)), this, SLOT(clientNetworkUpdated()));
350 connect(Client::network(id), SIGNAL(autoReconnectRetriesSet(quint16)), this, SLOT(clientNetworkUpdated()));
351 connect(Client::network(id), SIGNAL(unlimitedReconnectRetriesSet(bool)), this, SLOT(clientNetworkUpdated()));
352 connect(Client::network(id), SIGNAL(rejoinChannelsSet(bool)), this, SLOT(clientNetworkUpdated()));
353 connect(Client::network(id), SIGNAL(codecForServerSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
354 connect(Client::network(id), SIGNAL(codecForEncodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
355 connect(Client::network(id), SIGNAL(codecForDecodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
357 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
358 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
361 void NetworksSettingsPage::clientNetworkUpdated() {
362 const Network *net = qobject_cast<const Network *>(sender());
364 qWarning() << "Update request for unknown network received!";
367 networkInfos[net->networkId()] = net->networkInfo();
368 setItemState(net->networkId());
369 if(net->networkId() == currentId) displayNetwork(net->networkId());
374 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
375 if(!networkInfos.contains(id)) return;
376 if(id == currentId) displayNetwork(0);
377 NetworkInfo info = networkInfos.take(id);
378 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
379 foreach(QListWidgetItem *item, items) {
380 if(item->data(Qt::UserRole).value<NetworkId>() == id)
381 delete ui.networkList->takeItem(ui.networkList->row(item));
387 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
389 const Network *net = qobject_cast<const Network *>(sender());
392 if(net->networkId() == currentId) {
393 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
396 setItemState(net->networkId());
400 void NetworksSettingsPage::networkConnectionError(const QString &) {
404 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
405 NetworkInfo info = Client::network(id)->networkInfo();
406 networkInfos[id] = info;
407 return insertNetwork(info);
410 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
411 QListWidgetItem *item = 0;
412 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
413 if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
415 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
416 // -> then we can be sure that this is the core-side replacement for the net we created
417 foreach(QListWidgetItem *i, items) {
418 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
419 if(id < 0) { item = i; break; }
421 if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
423 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
424 setItemState(info.networkId, item);
429 void NetworksSettingsPage::displayNetwork(NetworkId id) {
430 _ignoreWidgetChanges = true;
432 NetworkInfo info = networkInfos[id];
433 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
434 ui.serverList->clear();
435 foreach(Network::Server server, info.serverList) {
436 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
438 item->setIcon(SmallIcon("document-encrypt"));
439 ui.serverList->addItem(item);
442 //ui.randomServer->setChecked(info.useRandomServer);
443 ui.performEdit->setPlainText(info.perform.join("\n"));
444 ui.autoIdentify->setChecked(info.useAutoIdentify);
445 ui.autoIdentifyService->setText(info.autoIdentifyService);
446 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
447 if(info.codecForEncoding.isEmpty()) {
448 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
449 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
450 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
451 ui.useCustomEncodings->setChecked(false);
453 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
454 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
455 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
456 ui.useCustomEncodings->setChecked(true);
458 ui.autoReconnect->setChecked(info.useAutoReconnect);
459 ui.reconnectInterval->setValue(info.autoReconnectInterval);
460 ui.reconnectRetries->setValue(info.autoReconnectRetries);
461 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
462 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
464 // just clear widgets
465 ui.identityList->setCurrentIndex(-1);
466 ui.serverList->clear();
467 ui.performEdit->clear();
468 ui.autoIdentifyService->clear();
469 ui.autoIdentifyPassword->clear();
472 _ignoreWidgetChanges = false;
476 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
477 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
478 //info.useRandomServer = ui.randomServer->isChecked();
479 info.perform = ui.performEdit->toPlainText().split("\n");
480 info.useAutoIdentify = ui.autoIdentify->isChecked();
481 info.autoIdentifyService = ui.autoIdentifyService->text();
482 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
483 if(!ui.useCustomEncodings->isChecked()) {
484 info.codecForEncoding.clear();
485 info.codecForDecoding.clear();
486 info.codecForServer.clear();
488 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
489 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
490 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
492 info.useAutoReconnect = ui.autoReconnect->isChecked();
493 info.autoReconnectInterval = ui.reconnectInterval->value();
494 info.autoReconnectRetries = ui.reconnectRetries->value();
495 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
496 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
498 /*** Network list ***/
500 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
502 saveToNetworkInfo(networkInfos[currentId]);
504 if(ui.networkList->selectedItems().count()) {
505 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
508 ui.serverList->setCurrentRow(0);
515 void NetworksSettingsPage::on_addNetwork_clicked() {
516 QStringList existing;
517 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
518 NetworkAddDlg dlg(existing, this);
519 if(dlg.exec() == QDialog::Accepted) {
520 NetworkInfo info = dlg.networkInfo();
521 if(info.networkName.isEmpty())
522 return; // sanity check
525 for(id = 1; id <= networkInfos.count(); id++) {
527 if(!networkInfos.keys().contains(-id.toInt())) break;
531 info.identity = defaultIdentity();
532 networkInfos[id] = info;
533 QListWidgetItem *item = insertNetwork(info);
534 ui.networkList->setCurrentItem(item);
539 void NetworksSettingsPage::on_deleteNetwork_clicked() {
540 if(ui.networkList->selectedItems().count()) {
541 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
542 int ret = QMessageBox::question(this, tr("Delete Network?"),
543 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
544 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
545 if(ret == QMessageBox::Yes) {
547 networkInfos.remove(netid);
548 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
549 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
556 void NetworksSettingsPage::on_renameNetwork_clicked() {
557 if(!ui.networkList->selectedItems().count()) return;
558 QString old = ui.networkList->selectedItems()[0]->text();
559 QStringList existing;
560 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
561 NetworkEditDlg dlg(old, existing, this);
562 if(dlg.exec() == QDialog::Accepted) {
563 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
564 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
565 networkInfos[netid].networkName = dlg.networkName();
571 void NetworksSettingsPage::on_connectNow_clicked() {
572 if(!ui.networkList->selectedItems().count()) return;
573 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
574 const Network *net = Client::network(id);
576 if(net->connectionState() == Network::Disconnected) net->requestConnect();
577 else net->requestDisconnect();
581 /*** Server list ***/
583 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
587 void NetworksSettingsPage::on_addServer_clicked() {
588 if(currentId == 0) return;
589 ServerEditDlg dlg(Network::Server(), this);
590 if(dlg.exec() == QDialog::Accepted) {
591 networkInfos[currentId].serverList.append(dlg.serverData());
592 displayNetwork(currentId);
593 ui.serverList->setCurrentRow(ui.serverList->count()-1);
598 void NetworksSettingsPage::on_editServer_clicked() {
599 if(currentId == 0) return;
600 int cur = ui.serverList->currentRow();
601 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
602 if(dlg.exec() == QDialog::Accepted) {
603 networkInfos[currentId].serverList[cur] = dlg.serverData();
604 displayNetwork(currentId);
605 ui.serverList->setCurrentRow(cur);
610 void NetworksSettingsPage::on_deleteServer_clicked() {
611 if(currentId == 0) return;
612 int cur = ui.serverList->currentRow();
613 networkInfos[currentId].serverList.removeAt(cur);
614 displayNetwork(currentId);
615 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
619 void NetworksSettingsPage::on_upServer_clicked() {
620 int cur = ui.serverList->currentRow();
621 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
622 networkInfos[currentId].serverList.insert(cur-1, server);
623 displayNetwork(currentId);
624 ui.serverList->setCurrentRow(cur-1);
628 void NetworksSettingsPage::on_downServer_clicked() {
629 int cur = ui.serverList->currentRow();
630 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
631 networkInfos[currentId].serverList.insert(cur+1, server);
632 displayNetwork(currentId);
633 ui.serverList->setCurrentRow(cur+1);
637 void NetworksSettingsPage::on_editIdentities_clicked() {
638 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
642 IdentityId NetworksSettingsPage::defaultIdentity() const {
643 IdentityId defaultId = 0;
644 QList<IdentityId> ids = Client::identityIds();
645 foreach(IdentityId id, ids) {
646 if(defaultId == 0 || id < defaultId)
652 /**************************************************************************
654 *************************************************************************/
656 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
658 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
660 // read preset networks
661 QStringList networks = Network::presetNetworks();
662 foreach(QString s, existing)
663 networks.removeAll(s);
665 ui.presetList->addItems(networks);
667 ui.useManual->setChecked(true);
668 ui.usePreset->setEnabled(false);
670 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
671 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
675 NetworkInfo NetworkAddDlg::networkInfo() const {
676 if(ui.useManual->isChecked()) {
678 info.networkName = ui.networkName->text().trimmed();
679 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(), ui.serverPassword->text(), ui.useSSL->isChecked());
682 return Network::networkInfoFromPreset(ui.presetList->currentText());
685 void NetworkAddDlg::setButtonStates() {
687 if(ui.usePreset->isChecked() && ui.presetList->count())
689 else if(ui.useManual->isChecked()) {
690 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
691 && !ui.serverAddress->text().isEmpty();
693 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
696 /**************************************************************************
698 *************************************************************************/
700 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
705 setWindowTitle(tr("Add Network"));
706 on_networkEdit_textChanged(""); // disable ok button
707 } else ui.networkEdit->setText(old);
710 QString NetworkEditDlg::networkName() const {
711 return ui.networkEdit->text().trimmed();
715 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
716 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
719 /**************************************************************************
721 *************************************************************************/
722 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent) {
724 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
725 ui.host->setText(server.host);
726 ui.port->setValue(server.port);
727 ui.password->setText(server.password);
728 ui.useSSL->setChecked(server.useSsl);
729 ui.sslVersion->setCurrentIndex(server.sslVersion);
730 ui.useProxy->setChecked(server.useProxy);
731 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
732 ui.proxyHost->setText(server.proxyHost);
733 ui.proxyPort->setValue(server.proxyPort);
734 ui.proxyUsername->setText(server.proxyUser);
735 ui.proxyPassword->setText(server.proxyPass);
736 on_host_textChanged();
739 Network::Server ServerEditDlg::serverData() const {
740 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked());
741 server.sslVersion = ui.sslVersion->currentIndex();
742 server.useProxy = ui.useProxy->isChecked();
743 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
744 server.proxyHost = ui.proxyHost->text();
745 server.proxyPort = ui.proxyPort->value();
746 server.proxyUser = ui.proxyUsername->text();
747 server.proxyPass = ui.proxyPassword->text();
751 void ServerEditDlg::on_host_textChanged() {
752 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
755 /**************************************************************************
757 *************************************************************************/
759 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
763 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
766 ui.progressBar->setMaximum(numevents);
767 ui.progressBar->setValue(0);
769 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
770 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
772 foreach(NetworkId id, toRemove) {
773 Client::removeNetwork(id);
775 foreach(NetworkInfo info, toCreate) {
776 Client::createNetwork(info);
778 foreach(NetworkInfo info, toUpdate) {
779 const Network *net = Client::network(info.networkId);
781 qWarning() << "Invalid client network!";
785 // FIXME this only checks for one changed item rather than all!
786 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
787 Client::updateNetwork(info);
790 qWarning() << "Sync dialog called without stuff to change!";
795 void SaveNetworksDlg::clientEvent() {
796 ui.progressBar->setValue(++rcvevents);
797 if(rcvevents >= numevents) accept();