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"
33 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
37 ui.renameNetwork->setIcon(SmallIcon("edit-rename"));
38 ui.addNetwork->setIcon(SmallIcon("list-add"));
39 ui.deleteNetwork->setIcon(SmallIcon("edit-delete"));
40 ui.addServer->setIcon(SmallIcon("list-add"));
41 ui.deleteServer->setIcon(SmallIcon("edit-delete"));
42 ui.editServer->setIcon(SmallIcon("configure"));
43 ui.upServer->setIcon(SmallIcon("go-up"));
44 ui.downServer->setIcon(SmallIcon("go-down"));
45 ui.editIdentities->setIcon(SmallIcon("configure"));
47 _ignoreWidgetChanges = false;
49 connectedIcon = SmallIcon("network-connect");
50 connectingIcon = SmallIcon("network-wired"); // FIXME network-connecting
51 disconnectedIcon = SmallIcon("network-disconnect");
53 foreach(int mib, QTextCodec::availableMibs()) {
54 QByteArray codec = QTextCodec::codecForMib(mib)->name();
55 ui.sendEncoding->addItem(codec);
56 ui.recvEncoding->addItem(codec);
57 ui.serverEncoding->addItem(codec);
59 ui.sendEncoding->model()->sort(0);
60 ui.recvEncoding->model()->sort(0);
61 ui.serverEncoding->model()->sort(0);
63 setEnabled(Client::isConnected()); // need a core connection!
65 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
66 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
67 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
68 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
69 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
71 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
72 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
73 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
74 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
75 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
76 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
77 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
78 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
79 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
80 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
81 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
82 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
83 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
84 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
85 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
86 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
87 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
89 foreach(IdentityId id, Client::identityIds()) {
90 clientIdentityAdded(id);
94 void NetworksSettingsPage::save() {
96 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
98 QList<NetworkInfo> toCreate, toUpdate;
99 QList<NetworkId> toRemove;
100 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
101 while(i != networkInfos.end()) {
102 NetworkId id = (*i).networkId;
105 //if(id == currentId) currentId = 0;
106 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
107 //if(items.count()) {
108 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
111 //i = networkInfos.erase(i);
114 if((*i) != Client::network((*i).networkId)->networkInfo()) {
120 foreach(NetworkId id, Client::networkIds()) {
121 if(!networkInfos.contains(id)) toRemove.append(id);
123 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
124 int ret = dlg.exec();
125 if(ret == QDialog::Rejected) {
126 // canceled -> reload everything to be safe
129 setChangedState(false);
133 void NetworksSettingsPage::load() {
135 foreach(NetworkId netid, Client::networkIds()) {
136 clientNetworkAdded(netid);
138 ui.networkList->setCurrentRow(0);
139 setChangedState(false);
142 void NetworksSettingsPage::reset() {
144 ui.networkList->clear();
145 networkInfos.clear();
149 bool NetworksSettingsPage::aboutToSave() {
150 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
152 foreach(NetworkInfo info, networkInfos.values()) {
153 if(!info.serverList.count()) errors.append(1);
155 if(!errors.count()) return true;
156 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
157 if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
158 error += tr("</ul>");
159 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
163 void NetworksSettingsPage::widgetHasChanged() {
164 if(_ignoreWidgetChanges) return;
165 bool changed = testHasChanged();
166 if(changed != hasChanged()) setChangedState(changed);
169 bool NetworksSettingsPage::testHasChanged() {
171 saveToNetworkInfo(networkInfos[currentId]);
173 if(Client::networkIds().count() != networkInfos.count()) return true;
174 foreach(NetworkId id, networkInfos.keys()) {
175 if(id < 0) return true;
176 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
181 void NetworksSettingsPage::setWidgetStates() {
183 if(ui.networkList->selectedItems().count()) {
184 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
185 const Network *net = 0;
186 if(id > 0) net = Client::network(id);
187 ui.detailsBox->setEnabled(true);
188 ui.renameNetwork->setEnabled(true);
189 ui.deleteNetwork->setEnabled(true);
190 /* button disabled for now
191 ui.connectNow->setEnabled(net);
192 // && (Client::network(id)->connectionState() == Network::Initialized
193 // || Client::network(id)->connectionState() == Network::Disconnected));
195 if(net->connectionState() == Network::Disconnected) {
196 ui.connectNow->setIcon(connectedIcon);
197 ui.connectNow->setText(tr("Connect"));
199 ui.connectNow->setIcon(disconnectedIcon);
200 ui.connectNow->setText(tr("Disconnect"));
203 ui.connectNow->setIcon(QIcon());
204 ui.connectNow->setText(tr("Apply first!"));
207 ui.renameNetwork->setEnabled(false);
208 ui.deleteNetwork->setEnabled(false);
209 //ui.connectNow->setEnabled(false);
210 ui.detailsBox->setEnabled(false);
213 if(ui.serverList->selectedItems().count()) {
214 ui.editServer->setEnabled(true);
215 ui.deleteServer->setEnabled(true);
216 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
217 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
219 ui.editServer->setEnabled(false);
220 ui.deleteServer->setEnabled(false);
221 ui.upServer->setEnabled(false);
222 ui.downServer->setEnabled(false);
226 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
227 if(!item && !(item = networkItem(id))) return;
228 const Network *net = Client::network(id);
229 if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
230 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
231 if(net && net->connectionState() == Network::Initialized) {
232 item->setIcon(connectedIcon);
233 } else if(net && net->connectionState() != Network::Disconnected) {
234 item->setIcon(connectingIcon);
236 item->setIcon(disconnectedIcon);
240 // check if we already have another net of this name in the list, and replace it
241 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
243 foreach(QListWidgetItem *i, items) {
244 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
245 if(oldid > 0) continue; // only locally created nets should be replaced
246 if(oldid == currentId) {
249 ui.networkList->clearSelection();
251 int row = ui.networkList->row(i);
253 QListWidgetItem *olditem = ui.networkList->takeItem(row);
257 networkInfos.remove(oldid);
261 item->setText(net->networkName());
262 if(select) item->setSelected(true);
266 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
267 this->setEnabled(state);
276 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
277 const Identity * identity = Client::identity(id);
278 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
281 // default identity is always the first one!
282 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
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());
298 void NetworksSettingsPage::clientIdentityUpdated() {
299 const Identity *identity = qobject_cast<const Identity *>(sender());
301 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
304 int row = ui.identityList->findData(identity->id().toInt());
306 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
309 if(ui.identityList->itemText(row) != identity->identityName()) {
310 ui.identityList->setItemText(row, identity->identityName());
314 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
315 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
316 //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
317 foreach(NetworkInfo info, networkInfos.values()) {
318 //qDebug() << info.networkName << info.networkId << info.identity;
319 if(info.identity == id) {
320 if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
321 info.identity = 1; // set to default
322 networkInfos[info.networkId] = info;
323 if(info.networkId > 0) Client::updateNetwork(info);
326 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
330 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
331 for(int i = 0; i < ui.networkList->count(); i++) {
332 QListWidgetItem *item = ui.networkList->item(i);
333 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
338 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
340 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
341 connect(Client::network(id), SIGNAL(identitySet(IdentityId)), this, SLOT(clientNetworkUpdated()));
342 connect(Client::network(id), SIGNAL(networkNameSet(const QString &)), this, SLOT(clientNetworkUpdated()));
343 connect(Client::network(id), SIGNAL(serverListSet(QVariantList)), this, SLOT(clientNetworkUpdated()));
344 connect(Client::network(id), SIGNAL(useRandomServerSet(bool)), this, SLOT(clientNetworkUpdated()));
345 connect(Client::network(id), SIGNAL(performSet(const QStringList &)), this, SLOT(clientNetworkUpdated()));
346 connect(Client::network(id), SIGNAL(useAutoIdentifySet(bool)), this, SLOT(clientNetworkUpdated()));
347 connect(Client::network(id), SIGNAL(autoIdentifyServiceSet(const QString &)), this, SLOT(clientNetworkUpdated()));
348 connect(Client::network(id), SIGNAL(autoIdentifyPasswordSet(const QString &)), this, SLOT(clientNetworkUpdated()));
349 connect(Client::network(id), SIGNAL(useAutoReconnectSet(bool)), this, SLOT(clientNetworkUpdated()));
350 connect(Client::network(id), SIGNAL(autoReconnectIntervalSet(quint32)), this, SLOT(clientNetworkUpdated()));
351 connect(Client::network(id), SIGNAL(autoReconnectRetriesSet(quint16)), this, SLOT(clientNetworkUpdated()));
352 connect(Client::network(id), SIGNAL(unlimitedReconnectRetriesSet(bool)), this, SLOT(clientNetworkUpdated()));
353 connect(Client::network(id), SIGNAL(rejoinChannelsSet(bool)), this, SLOT(clientNetworkUpdated()));
354 connect(Client::network(id), SIGNAL(codecForServerSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
355 connect(Client::network(id), SIGNAL(codecForEncodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
356 connect(Client::network(id), SIGNAL(codecForDecodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
358 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
359 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
362 void NetworksSettingsPage::clientNetworkUpdated() {
363 const Network *net = qobject_cast<const Network *>(sender());
365 qWarning() << "Update request for unknown network received!";
368 networkInfos[net->networkId()] = net->networkInfo();
369 setItemState(net->networkId());
370 if(net->networkId() == currentId) displayNetwork(net->networkId());
375 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
376 if(!networkInfos.contains(id)) return;
377 if(id == currentId) displayNetwork(0);
378 NetworkInfo info = networkInfos.take(id);
379 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
380 foreach(QListWidgetItem *item, items) {
381 if(item->data(Qt::UserRole).value<NetworkId>() == id)
382 delete ui.networkList->takeItem(ui.networkList->row(item));
388 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
390 const Network *net = qobject_cast<const Network *>(sender());
393 if(net->networkId() == currentId) {
394 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
397 setItemState(net->networkId());
401 void NetworksSettingsPage::networkConnectionError(const QString &) {
405 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
406 NetworkInfo info = Client::network(id)->networkInfo();
407 networkInfos[id] = info;
408 return insertNetwork(info);
411 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
412 QListWidgetItem *item = 0;
413 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
414 if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
416 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
417 // -> then we can be sure that this is the core-side replacement for the net we created
418 foreach(QListWidgetItem *i, items) {
419 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
420 if(id < 0) { item = i; break; }
422 if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
424 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
425 setItemState(info.networkId, item);
430 void NetworksSettingsPage::displayNetwork(NetworkId id) {
431 _ignoreWidgetChanges = true;
433 NetworkInfo info = networkInfos[id];
434 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
435 ui.serverList->clear();
436 foreach(Network::Server server, info.serverList) {
437 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
439 item->setIcon(SmallIcon("document-encrypt"));
440 ui.serverList->addItem(item);
443 //ui.randomServer->setChecked(info.useRandomServer);
444 ui.performEdit->setPlainText(info.perform.join("\n"));
445 ui.autoIdentify->setChecked(info.useAutoIdentify);
446 ui.autoIdentifyService->setText(info.autoIdentifyService);
447 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
448 if(info.codecForEncoding.isEmpty()) {
449 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
450 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
451 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
452 ui.useCustomEncodings->setChecked(false);
454 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
455 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
456 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
457 ui.useCustomEncodings->setChecked(true);
459 ui.autoReconnect->setChecked(info.useAutoReconnect);
460 ui.reconnectInterval->setValue(info.autoReconnectInterval);
461 ui.reconnectRetries->setValue(info.autoReconnectRetries);
462 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
463 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
465 // just clear widgets
466 ui.identityList->setCurrentIndex(-1);
467 ui.serverList->clear();
468 ui.performEdit->clear();
469 ui.autoIdentifyService->clear();
470 ui.autoIdentifyPassword->clear();
473 _ignoreWidgetChanges = false;
477 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
478 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
479 //info.useRandomServer = ui.randomServer->isChecked();
480 info.perform = ui.performEdit->toPlainText().split("\n");
481 info.useAutoIdentify = ui.autoIdentify->isChecked();
482 info.autoIdentifyService = ui.autoIdentifyService->text();
483 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
484 if(!ui.useCustomEncodings->isChecked()) {
485 info.codecForEncoding.clear();
486 info.codecForDecoding.clear();
487 info.codecForServer.clear();
489 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
490 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
491 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
493 info.useAutoReconnect = ui.autoReconnect->isChecked();
494 info.autoReconnectInterval = ui.reconnectInterval->value();
495 info.autoReconnectRetries = ui.reconnectRetries->value();
496 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
497 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
499 /*** Network list ***/
501 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
503 saveToNetworkInfo(networkInfos[currentId]);
505 if(ui.networkList->selectedItems().count()) {
506 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
509 ui.serverList->setCurrentRow(0);
516 void NetworksSettingsPage::on_addNetwork_clicked() {
517 QStringList existing;
518 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
519 NetworkAddDlg dlg(existing, this);
520 if(dlg.exec() == QDialog::Accepted) {
522 for(id = 1; id <= networkInfos.count(); id++) {
524 if(!networkInfos.keys().contains(-id.toInt())) break;
528 NetworkInfo info = dlg.networkInfo();
529 if(info.networkName.isEmpty())
530 return; // sanity check
535 info.useRandomServer = false;
536 info.useAutoReconnect = true;
537 info.autoReconnectInterval = 60;
538 info.autoReconnectRetries = 20;
539 info.unlimitedReconnectRetries = false;
540 info.useAutoIdentify = false;
541 info.autoIdentifyService = "NickServ";
542 info.rejoinChannels = true;
544 networkInfos[id] = info;
545 QListWidgetItem *item = insertNetwork(info);
546 ui.networkList->setCurrentItem(item);
551 void NetworksSettingsPage::on_deleteNetwork_clicked() {
552 if(ui.networkList->selectedItems().count()) {
553 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
554 int ret = QMessageBox::question(this, tr("Delete Network?"),
555 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
556 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
557 if(ret == QMessageBox::Yes) {
559 networkInfos.remove(netid);
560 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
561 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
568 void NetworksSettingsPage::on_renameNetwork_clicked() {
569 if(!ui.networkList->selectedItems().count()) return;
570 QString old = ui.networkList->selectedItems()[0]->text();
571 QStringList existing;
572 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
573 NetworkEditDlg dlg(old, existing, this);
574 if(dlg.exec() == QDialog::Accepted) {
575 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
576 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
577 networkInfos[netid].networkName = dlg.networkName();
583 void NetworksSettingsPage::on_connectNow_clicked() {
584 if(!ui.networkList->selectedItems().count()) return;
585 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
586 const Network *net = Client::network(id);
588 if(net->connectionState() == Network::Disconnected) net->requestConnect();
589 else net->requestDisconnect();
593 /*** Server list ***/
595 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
599 void NetworksSettingsPage::on_addServer_clicked() {
600 if(currentId == 0) return;
601 ServerEditDlg dlg(Network::Server(), this);
602 if(dlg.exec() == QDialog::Accepted) {
603 networkInfos[currentId].serverList.append(dlg.serverData());
604 displayNetwork(currentId);
605 ui.serverList->setCurrentRow(ui.serverList->count()-1);
610 void NetworksSettingsPage::on_editServer_clicked() {
611 if(currentId == 0) return;
612 int cur = ui.serverList->currentRow();
613 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
614 if(dlg.exec() == QDialog::Accepted) {
615 networkInfos[currentId].serverList[cur] = dlg.serverData();
616 displayNetwork(currentId);
617 ui.serverList->setCurrentRow(cur);
622 void NetworksSettingsPage::on_deleteServer_clicked() {
623 if(currentId == 0) return;
624 int cur = ui.serverList->currentRow();
625 networkInfos[currentId].serverList.removeAt(cur);
626 displayNetwork(currentId);
627 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
631 void NetworksSettingsPage::on_upServer_clicked() {
632 int cur = ui.serverList->currentRow();
633 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
634 networkInfos[currentId].serverList.insert(cur-1, server);
635 displayNetwork(currentId);
636 ui.serverList->setCurrentRow(cur-1);
640 void NetworksSettingsPage::on_downServer_clicked() {
641 int cur = ui.serverList->currentRow();
642 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
643 networkInfos[currentId].serverList.insert(cur+1, server);
644 displayNetwork(currentId);
645 ui.serverList->setCurrentRow(cur+1);
649 /**************************************************************************
651 *************************************************************************/
653 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
655 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
657 // read preset networks
658 networksFilePath = findDataFilePath("networks.ini");
659 if(!networksFilePath.isEmpty()) {
660 QSettings s(networksFilePath, QSettings::IniFormat);
661 QStringList networks = s.childGroups();
662 foreach(QString s, existing)
663 networks.removeAll(s);
664 if(!networks.isEmpty()) {
665 QMap<QString, QString> sorted;
666 foreach(QString net, networks)
667 sorted[net.toLower()] = net;
668 ui.presetList->addItems(sorted.values());
671 if(!ui.presetList->count()) {
672 ui.useManual->setChecked(true);
673 ui.usePreset->setEnabled(false);
675 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
676 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
680 NetworkInfo NetworkAddDlg::networkInfo() const {
683 if(ui.useManual->isChecked()) {
684 info.networkName = ui.networkName->text().trimmed();
685 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(), ui.serverPassword->text(), ui.useSSL->isChecked());
687 info.networkName = ui.presetList->currentText();
688 QSettings s(networksFilePath, QSettings::IniFormat);
689 s.beginGroup(info.networkName);
690 foreach(QString server, s.value("Servers").toStringList()) {
692 QStringList splitserver = server.split(':', QString::SkipEmptyParts);
693 if(splitserver.count() != 2) {
694 qWarning() << "Invalid server entry in networks.conf:" << server;
697 if(splitserver[1].at(0) == '+')
699 uint port = splitserver[1].toUInt();
701 qWarning() << "Invalid port entry in networks.conf:" << server;
704 info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl);
711 void NetworkAddDlg::setButtonStates() {
713 if(ui.usePreset->isChecked() && ui.presetList->count())
715 else if(ui.useManual->isChecked()) {
716 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
717 && !ui.serverAddress->text().isEmpty();
719 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
722 /**************************************************************************
724 *************************************************************************/
726 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
731 setWindowTitle(tr("Add Network"));
732 on_networkEdit_textChanged(""); // disable ok button
733 } else ui.networkEdit->setText(old);
736 QString NetworkEditDlg::networkName() const {
737 return ui.networkEdit->text().trimmed();
741 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
742 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
745 /**************************************************************************
747 *************************************************************************/
748 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent) {
750 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
751 ui.host->setText(server.host);
752 ui.port->setValue(server.port);
753 ui.password->setText(server.password);
754 ui.useSSL->setChecked(server.useSsl);
755 ui.sslVersion->setCurrentIndex(server.sslVersion);
756 ui.useProxy->setChecked(server.useProxy);
757 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
758 ui.proxyHost->setText(server.proxyHost);
759 ui.proxyPort->setValue(server.proxyPort);
760 ui.proxyUsername->setText(server.proxyUser);
761 ui.proxyPassword->setText(server.proxyPass);
762 on_host_textChanged();
765 Network::Server ServerEditDlg::serverData() const {
766 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked());
767 server.sslVersion = ui.sslVersion->currentIndex();
768 server.useProxy = ui.useProxy->isChecked();
769 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
770 server.proxyHost = ui.proxyHost->text();
771 server.proxyPort = ui.proxyPort->value();
772 server.proxyUser = ui.proxyUsername->text();
773 server.proxyPass = ui.proxyPassword->text();
777 void ServerEditDlg::on_host_textChanged() {
778 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
781 /**************************************************************************
783 *************************************************************************/
785 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
789 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
792 ui.progressBar->setMaximum(numevents);
793 ui.progressBar->setValue(0);
795 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
796 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
798 foreach(NetworkId id, toRemove) {
799 Client::removeNetwork(id);
801 foreach(NetworkInfo info, toCreate) {
802 Client::createNetwork(info);
804 foreach(NetworkInfo info, toUpdate) {
805 const Network *net = Client::network(info.networkId);
807 qWarning() << "Invalid client network!";
811 // FIXME this only checks for one changed item rather than all!
812 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
813 Client::updateNetwork(info);
816 qWarning() << "Sync dialog called without stuff to change!";
821 void SaveNetworksDlg::clientEvent() {
822 ui.progressBar->setValue(++rcvevents);
823 if(rcvevents >= numevents) accept();