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) : SettingsPage(tr("General"), tr("Networks"), parent) {
40 ui.renameNetwork->setIcon(SmallIcon("edit-rename"));
41 ui.addNetwork->setIcon(SmallIcon("list-add"));
42 ui.deleteNetwork->setIcon(SmallIcon("edit-delete"));
43 ui.addServer->setIcon(SmallIcon("list-add"));
44 ui.deleteServer->setIcon(SmallIcon("edit-delete"));
45 ui.editServer->setIcon(SmallIcon("configure"));
46 ui.upServer->setIcon(SmallIcon("go-up"));
47 ui.downServer->setIcon(SmallIcon("go-down"));
48 ui.editIdentities->setIcon(SmallIcon("configure"));
50 _ignoreWidgetChanges = false;
52 connectedIcon = SmallIcon("network-connect");
53 connectingIcon = SmallIcon("network-wired"); // FIXME network-connecting
54 disconnectedIcon = SmallIcon("network-disconnect");
56 foreach(int mib, QTextCodec::availableMibs()) {
57 QByteArray codec = QTextCodec::codecForMib(mib)->name();
58 ui.sendEncoding->addItem(codec);
59 ui.recvEncoding->addItem(codec);
60 ui.serverEncoding->addItem(codec);
62 ui.sendEncoding->model()->sort(0);
63 ui.recvEncoding->model()->sort(0);
64 ui.serverEncoding->model()->sort(0);
66 setEnabled(Client::isConnected()); // need a core connection!
68 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
69 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
70 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
71 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
72 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
74 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
75 //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
76 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
77 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
78 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
79 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
80 connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
81 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
82 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
83 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
84 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
85 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
86 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
87 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
88 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
89 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
90 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
92 foreach(IdentityId id, Client::identityIds()) {
93 clientIdentityAdded(id);
97 void NetworksSettingsPage::save() {
99 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
101 QList<NetworkInfo> toCreate, toUpdate;
102 QList<NetworkId> toRemove;
103 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
104 while(i != networkInfos.end()) {
105 NetworkId id = (*i).networkId;
108 //if(id == currentId) currentId = 0;
109 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
110 //if(items.count()) {
111 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
114 //i = networkInfos.erase(i);
117 if((*i) != Client::network((*i).networkId)->networkInfo()) {
123 foreach(NetworkId id, Client::networkIds()) {
124 if(!networkInfos.contains(id)) toRemove.append(id);
126 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
127 int ret = dlg.exec();
128 if(ret == QDialog::Rejected) {
129 // canceled -> reload everything to be safe
132 setChangedState(false);
136 void NetworksSettingsPage::load() {
138 foreach(NetworkId netid, Client::networkIds()) {
139 clientNetworkAdded(netid);
141 ui.networkList->setCurrentRow(0);
142 setChangedState(false);
145 void NetworksSettingsPage::reset() {
147 ui.networkList->clear();
148 networkInfos.clear();
152 bool NetworksSettingsPage::aboutToSave() {
153 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
155 foreach(NetworkInfo info, networkInfos.values()) {
156 if(!info.serverList.count()) errors.append(1);
158 if(!errors.count()) return true;
159 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
160 if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
161 error += tr("</ul>");
162 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
166 void NetworksSettingsPage::widgetHasChanged() {
167 if(_ignoreWidgetChanges) return;
168 bool changed = testHasChanged();
169 if(changed != hasChanged()) setChangedState(changed);
172 bool NetworksSettingsPage::testHasChanged() {
174 saveToNetworkInfo(networkInfos[currentId]);
176 if(Client::networkIds().count() != networkInfos.count()) return true;
177 foreach(NetworkId id, networkInfos.keys()) {
178 if(id < 0) return true;
179 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
184 void NetworksSettingsPage::setWidgetStates() {
186 if(ui.networkList->selectedItems().count()) {
187 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
188 const Network *net = 0;
189 if(id > 0) net = Client::network(id);
190 ui.detailsBox->setEnabled(true);
191 ui.renameNetwork->setEnabled(true);
192 ui.deleteNetwork->setEnabled(true);
193 /* button disabled for now
194 ui.connectNow->setEnabled(net);
195 // && (Client::network(id)->connectionState() == Network::Initialized
196 // || Client::network(id)->connectionState() == Network::Disconnected));
198 if(net->connectionState() == Network::Disconnected) {
199 ui.connectNow->setIcon(connectedIcon);
200 ui.connectNow->setText(tr("Connect"));
202 ui.connectNow->setIcon(disconnectedIcon);
203 ui.connectNow->setText(tr("Disconnect"));
206 ui.connectNow->setIcon(QIcon());
207 ui.connectNow->setText(tr("Apply first!"));
210 ui.renameNetwork->setEnabled(false);
211 ui.deleteNetwork->setEnabled(false);
212 //ui.connectNow->setEnabled(false);
213 ui.detailsBox->setEnabled(false);
216 if(ui.serverList->selectedItems().count()) {
217 ui.editServer->setEnabled(true);
218 ui.deleteServer->setEnabled(true);
219 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
220 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
222 ui.editServer->setEnabled(false);
223 ui.deleteServer->setEnabled(false);
224 ui.upServer->setEnabled(false);
225 ui.downServer->setEnabled(false);
229 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
230 if(!item && !(item = networkItem(id))) return;
231 const Network *net = Client::network(id);
232 if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
233 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
234 if(net && net->connectionState() == Network::Initialized) {
235 item->setIcon(connectedIcon);
236 } else if(net && net->connectionState() != Network::Disconnected) {
237 item->setIcon(connectingIcon);
239 item->setIcon(disconnectedIcon);
243 // check if we already have another net of this name in the list, and replace it
244 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
246 foreach(QListWidgetItem *i, items) {
247 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
248 if(oldid > 0) continue; // only locally created nets should be replaced
249 if(oldid == currentId) {
252 ui.networkList->clearSelection();
254 int row = ui.networkList->row(i);
256 QListWidgetItem *olditem = ui.networkList->takeItem(row);
260 networkInfos.remove(oldid);
264 item->setText(net->networkName());
265 if(select) item->setSelected(true);
269 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
270 this->setEnabled(state);
279 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
280 const Identity * identity = Client::identity(id);
281 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
284 // default identity is always the first one!
285 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
287 QString name = identity->identityName();
288 for(int j = 0; j < ui.identityList->count(); j++) {
289 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
290 ui.identityList->insertItem(j, name, id.toInt());
296 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
301 void NetworksSettingsPage::clientIdentityUpdated() {
302 const Identity *identity = qobject_cast<const Identity *>(sender());
304 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
307 int row = ui.identityList->findData(identity->id().toInt());
309 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
312 if(ui.identityList->itemText(row) != identity->identityName()) {
313 ui.identityList->setItemText(row, identity->identityName());
317 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
318 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
319 //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
320 foreach(NetworkInfo info, networkInfos.values()) {
321 //qDebug() << info.networkName << info.networkId << info.identity;
322 if(info.identity == id) {
323 if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
324 info.identity = 1; // set to default
325 networkInfos[info.networkId] = info;
326 if(info.networkId > 0) Client::updateNetwork(info);
329 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
333 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
334 for(int i = 0; i < ui.networkList->count(); i++) {
335 QListWidgetItem *item = ui.networkList->item(i);
336 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
341 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
343 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
344 connect(Client::network(id), SIGNAL(identitySet(IdentityId)), this, SLOT(clientNetworkUpdated()));
345 connect(Client::network(id), SIGNAL(networkNameSet(const QString &)), this, SLOT(clientNetworkUpdated()));
346 connect(Client::network(id), SIGNAL(serverListSet(QVariantList)), this, SLOT(clientNetworkUpdated()));
347 connect(Client::network(id), SIGNAL(useRandomServerSet(bool)), this, SLOT(clientNetworkUpdated()));
348 connect(Client::network(id), SIGNAL(performSet(const QStringList &)), this, SLOT(clientNetworkUpdated()));
349 connect(Client::network(id), SIGNAL(useAutoIdentifySet(bool)), this, SLOT(clientNetworkUpdated()));
350 connect(Client::network(id), SIGNAL(autoIdentifyServiceSet(const QString &)), this, SLOT(clientNetworkUpdated()));
351 connect(Client::network(id), SIGNAL(autoIdentifyPasswordSet(const QString &)), this, SLOT(clientNetworkUpdated()));
352 connect(Client::network(id), SIGNAL(useAutoReconnectSet(bool)), this, SLOT(clientNetworkUpdated()));
353 connect(Client::network(id), SIGNAL(autoReconnectIntervalSet(quint32)), this, SLOT(clientNetworkUpdated()));
354 connect(Client::network(id), SIGNAL(autoReconnectRetriesSet(quint16)), this, SLOT(clientNetworkUpdated()));
355 connect(Client::network(id), SIGNAL(unlimitedReconnectRetriesSet(bool)), this, SLOT(clientNetworkUpdated()));
356 connect(Client::network(id), SIGNAL(rejoinChannelsSet(bool)), this, SLOT(clientNetworkUpdated()));
357 connect(Client::network(id), SIGNAL(codecForServerSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
358 connect(Client::network(id), SIGNAL(codecForEncodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
359 connect(Client::network(id), SIGNAL(codecForDecodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
361 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
362 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
365 void NetworksSettingsPage::clientNetworkUpdated() {
366 const Network *net = qobject_cast<const Network *>(sender());
368 qWarning() << "Update request for unknown network received!";
371 networkInfos[net->networkId()] = net->networkInfo();
372 setItemState(net->networkId());
373 if(net->networkId() == currentId) displayNetwork(net->networkId());
378 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
379 if(!networkInfos.contains(id)) return;
380 if(id == currentId) displayNetwork(0);
381 NetworkInfo info = networkInfos.take(id);
382 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
383 foreach(QListWidgetItem *item, items) {
384 if(item->data(Qt::UserRole).value<NetworkId>() == id)
385 delete ui.networkList->takeItem(ui.networkList->row(item));
391 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
393 const Network *net = qobject_cast<const Network *>(sender());
396 if(net->networkId() == currentId) {
397 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
400 setItemState(net->networkId());
404 void NetworksSettingsPage::networkConnectionError(const QString &) {
408 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
409 NetworkInfo info = Client::network(id)->networkInfo();
410 networkInfos[id] = info;
411 return insertNetwork(info);
414 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
415 QListWidgetItem *item = 0;
416 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
417 if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
419 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
420 // -> then we can be sure that this is the core-side replacement for the net we created
421 foreach(QListWidgetItem *i, items) {
422 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
423 if(id < 0) { item = i; break; }
425 if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
427 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
428 setItemState(info.networkId, item);
433 void NetworksSettingsPage::displayNetwork(NetworkId id) {
434 _ignoreWidgetChanges = true;
436 NetworkInfo info = networkInfos[id];
437 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
438 ui.serverList->clear();
439 foreach(Network::Server server, info.serverList) {
440 QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
442 item->setIcon(SmallIcon("document-encrypt"));
443 ui.serverList->addItem(item);
446 //ui.randomServer->setChecked(info.useRandomServer);
447 ui.performEdit->setPlainText(info.perform.join("\n"));
448 ui.autoIdentify->setChecked(info.useAutoIdentify);
449 ui.autoIdentifyService->setText(info.autoIdentifyService);
450 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
451 if(info.codecForEncoding.isEmpty()) {
452 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
453 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
454 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
455 ui.useCustomEncodings->setChecked(false);
457 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
458 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
459 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
460 ui.useCustomEncodings->setChecked(true);
462 ui.autoReconnect->setChecked(info.useAutoReconnect);
463 ui.reconnectInterval->setValue(info.autoReconnectInterval);
464 ui.reconnectRetries->setValue(info.autoReconnectRetries);
465 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
466 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
468 // just clear widgets
469 ui.identityList->setCurrentIndex(-1);
470 ui.serverList->clear();
471 ui.performEdit->clear();
472 ui.autoIdentifyService->clear();
473 ui.autoIdentifyPassword->clear();
476 _ignoreWidgetChanges = false;
480 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
481 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
482 //info.useRandomServer = ui.randomServer->isChecked();
483 info.perform = ui.performEdit->toPlainText().split("\n");
484 info.useAutoIdentify = ui.autoIdentify->isChecked();
485 info.autoIdentifyService = ui.autoIdentifyService->text();
486 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
487 if(!ui.useCustomEncodings->isChecked()) {
488 info.codecForEncoding.clear();
489 info.codecForDecoding.clear();
490 info.codecForServer.clear();
492 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
493 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
494 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
496 info.useAutoReconnect = ui.autoReconnect->isChecked();
497 info.autoReconnectInterval = ui.reconnectInterval->value();
498 info.autoReconnectRetries = ui.reconnectRetries->value();
499 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
500 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
502 /*** Network list ***/
504 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
506 saveToNetworkInfo(networkInfos[currentId]);
508 if(ui.networkList->selectedItems().count()) {
509 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
512 ui.serverList->setCurrentRow(0);
519 void NetworksSettingsPage::on_addNetwork_clicked() {
520 QStringList existing;
521 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
522 NetworkAddDlg dlg(existing, this);
523 if(dlg.exec() == QDialog::Accepted) {
525 for(id = 1; id <= networkInfos.count(); id++) {
527 if(!networkInfos.keys().contains(-id.toInt())) break;
531 NetworkInfo info = dlg.networkInfo();
532 if(info.networkName.isEmpty())
533 return; // sanity check
538 info.useRandomServer = false;
539 info.useAutoReconnect = true;
540 info.autoReconnectInterval = 60;
541 info.autoReconnectRetries = 20;
542 info.unlimitedReconnectRetries = false;
543 info.useAutoIdentify = false;
544 info.autoIdentifyService = "NickServ";
545 info.rejoinChannels = true;
547 networkInfos[id] = info;
548 QListWidgetItem *item = insertNetwork(info);
549 ui.networkList->setCurrentItem(item);
554 void NetworksSettingsPage::on_deleteNetwork_clicked() {
555 if(ui.networkList->selectedItems().count()) {
556 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
557 int ret = QMessageBox::question(this, tr("Delete Network?"),
558 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
559 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
560 if(ret == QMessageBox::Yes) {
562 networkInfos.remove(netid);
563 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
564 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
571 void NetworksSettingsPage::on_renameNetwork_clicked() {
572 if(!ui.networkList->selectedItems().count()) return;
573 QString old = ui.networkList->selectedItems()[0]->text();
574 QStringList existing;
575 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
576 NetworkEditDlg dlg(old, existing, this);
577 if(dlg.exec() == QDialog::Accepted) {
578 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
579 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
580 networkInfos[netid].networkName = dlg.networkName();
586 void NetworksSettingsPage::on_connectNow_clicked() {
587 if(!ui.networkList->selectedItems().count()) return;
588 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
589 const Network *net = Client::network(id);
591 if(net->connectionState() == Network::Disconnected) net->requestConnect();
592 else net->requestDisconnect();
596 /*** Server list ***/
598 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
602 void NetworksSettingsPage::on_addServer_clicked() {
603 if(currentId == 0) return;
604 ServerEditDlg dlg(Network::Server(), this);
605 if(dlg.exec() == QDialog::Accepted) {
606 networkInfos[currentId].serverList.append(dlg.serverData());
607 displayNetwork(currentId);
608 ui.serverList->setCurrentRow(ui.serverList->count()-1);
613 void NetworksSettingsPage::on_editServer_clicked() {
614 if(currentId == 0) return;
615 int cur = ui.serverList->currentRow();
616 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
617 if(dlg.exec() == QDialog::Accepted) {
618 networkInfos[currentId].serverList[cur] = dlg.serverData();
619 displayNetwork(currentId);
620 ui.serverList->setCurrentRow(cur);
625 void NetworksSettingsPage::on_deleteServer_clicked() {
626 if(currentId == 0) return;
627 int cur = ui.serverList->currentRow();
628 networkInfos[currentId].serverList.removeAt(cur);
629 displayNetwork(currentId);
630 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
634 void NetworksSettingsPage::on_upServer_clicked() {
635 int cur = ui.serverList->currentRow();
636 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
637 networkInfos[currentId].serverList.insert(cur-1, server);
638 displayNetwork(currentId);
639 ui.serverList->setCurrentRow(cur-1);
643 void NetworksSettingsPage::on_downServer_clicked() {
644 int cur = ui.serverList->currentRow();
645 Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
646 networkInfos[currentId].serverList.insert(cur+1, server);
647 displayNetwork(currentId);
648 ui.serverList->setCurrentRow(cur+1);
652 void NetworksSettingsPage::on_editIdentities_clicked() {
653 SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
657 /**************************************************************************
659 *************************************************************************/
661 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
663 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
665 // read preset networks
666 networksFilePath = findDataFilePath("networks.ini");
667 if(!networksFilePath.isEmpty()) {
668 QSettings s(networksFilePath, QSettings::IniFormat);
669 QStringList networks = s.childGroups();
670 foreach(QString s, existing)
671 networks.removeAll(s);
672 if(!networks.isEmpty()) {
673 QMap<QString, QString> sorted;
674 foreach(QString net, networks)
675 sorted[net.toLower()] = net;
676 ui.presetList->addItems(sorted.values());
679 if(!ui.presetList->count()) {
680 ui.useManual->setChecked(true);
681 ui.usePreset->setEnabled(false);
683 connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
684 connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
688 NetworkInfo NetworkAddDlg::networkInfo() const {
691 if(ui.useManual->isChecked()) {
692 info.networkName = ui.networkName->text().trimmed();
693 info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(), ui.serverPassword->text(), ui.useSSL->isChecked());
695 info.networkName = ui.presetList->currentText();
696 QSettings s(networksFilePath, QSettings::IniFormat);
697 s.beginGroup(info.networkName);
698 foreach(QString server, s.value("Servers").toStringList()) {
700 QStringList splitserver = server.split(':', QString::SkipEmptyParts);
701 if(splitserver.count() != 2) {
702 qWarning() << "Invalid server entry in networks.conf:" << server;
705 if(splitserver[1].at(0) == '+')
707 uint port = splitserver[1].toUInt();
709 qWarning() << "Invalid port entry in networks.conf:" << server;
712 info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl);
719 void NetworkAddDlg::setButtonStates() {
721 if(ui.usePreset->isChecked() && ui.presetList->count())
723 else if(ui.useManual->isChecked()) {
724 ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
725 && !ui.serverAddress->text().isEmpty();
727 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
730 /**************************************************************************
732 *************************************************************************/
734 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
739 setWindowTitle(tr("Add Network"));
740 on_networkEdit_textChanged(""); // disable ok button
741 } else ui.networkEdit->setText(old);
744 QString NetworkEditDlg::networkName() const {
745 return ui.networkEdit->text().trimmed();
749 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
750 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
753 /**************************************************************************
755 *************************************************************************/
756 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent) {
758 ui.useSSL->setIcon(SmallIcon("document-encrypt"));
759 ui.host->setText(server.host);
760 ui.port->setValue(server.port);
761 ui.password->setText(server.password);
762 ui.useSSL->setChecked(server.useSsl);
763 ui.sslVersion->setCurrentIndex(server.sslVersion);
764 ui.useProxy->setChecked(server.useProxy);
765 ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
766 ui.proxyHost->setText(server.proxyHost);
767 ui.proxyPort->setValue(server.proxyPort);
768 ui.proxyUsername->setText(server.proxyUser);
769 ui.proxyPassword->setText(server.proxyPass);
770 on_host_textChanged();
773 Network::Server ServerEditDlg::serverData() const {
774 Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(), ui.useSSL->isChecked());
775 server.sslVersion = ui.sslVersion->currentIndex();
776 server.useProxy = ui.useProxy->isChecked();
777 server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
778 server.proxyHost = ui.proxyHost->text();
779 server.proxyPort = ui.proxyPort->value();
780 server.proxyUser = ui.proxyUsername->text();
781 server.proxyPass = ui.proxyPassword->text();
785 void ServerEditDlg::on_host_textChanged() {
786 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
789 /**************************************************************************
791 *************************************************************************/
793 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
797 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
800 ui.progressBar->setMaximum(numevents);
801 ui.progressBar->setValue(0);
803 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
804 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
806 foreach(NetworkId id, toRemove) {
807 Client::removeNetwork(id);
809 foreach(NetworkInfo info, toCreate) {
810 Client::createNetwork(info);
812 foreach(NetworkInfo info, toUpdate) {
813 const Network *net = Client::network(info.networkId);
815 qWarning() << "Invalid client network!";
819 // FIXME this only checks for one changed item rather than all!
820 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
821 Client::updateNetwork(info);
824 qWarning() << "Sync dialog called without stuff to change!";
829 void SaveNetworksDlg::clientEvent() {
830 ui.progressBar->setValue(++rcvevents);
831 if(rcvevents >= numevents) accept();