1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
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"
32 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
34 _ignoreWidgetChanges = false;
36 connectedIcon = QIcon(":/22x22/actions/network-connect");
37 connectingIcon = QIcon(":/22x22/actions/gear");
38 disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
40 foreach(int mib, QTextCodec::availableMibs()) {
41 QByteArray codec = QTextCodec::codecForMib(mib)->name();
42 ui.sendEncoding->addItem(codec);
43 ui.recvEncoding->addItem(codec);
44 ui.serverEncoding->addItem(codec);
46 ui.sendEncoding->model()->sort(0);
47 ui.recvEncoding->model()->sort(0);
48 ui.serverEncoding->model()->sort(0);
50 setEnabled(Client::isConnected()); // need a core connection!
52 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
53 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
54 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
55 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
56 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
58 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
59 connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
60 connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
61 connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62 connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
63 connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
64 connect(ui.useDefaultEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65 connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
66 connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
67 connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
68 connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69 connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
70 connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
71 connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
72 connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
73 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
74 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
76 foreach(IdentityId id, Client::identityIds()) {
77 clientIdentityAdded(id);
81 void NetworksSettingsPage::save() {
83 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
85 QList<NetworkInfo> toCreate, toUpdate;
86 QList<NetworkId> toRemove;
87 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
88 while(i != networkInfos.end()) {
89 NetworkId id = (*i).networkId;
92 //if(id == currentId) currentId = 0;
93 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
95 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
98 //i = networkInfos.erase(i);
101 if((*i) != Client::network((*i).networkId)->networkInfo()) {
107 foreach(NetworkId id, Client::networkIds()) {
108 if(!networkInfos.contains(id)) toRemove.append(id);
110 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
111 int ret = dlg.exec();
112 if(ret == QDialog::Rejected) {
113 // canceled -> reload everything to be safe
116 setChangedState(false);
120 void NetworksSettingsPage::load() {
122 foreach(NetworkId netid, Client::networkIds()) {
123 clientNetworkAdded(netid);
125 ui.networkList->setCurrentRow(0);
126 setChangedState(false);
129 void NetworksSettingsPage::reset() {
131 ui.networkList->clear();
132 networkInfos.clear();
136 bool NetworksSettingsPage::aboutToSave() {
137 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
139 foreach(NetworkInfo info, networkInfos.values()) {
140 if(!info.serverList.count()) errors.append(1);
142 if(!errors.count()) return true;
143 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
144 if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
145 error += tr("</ul>");
146 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
150 void NetworksSettingsPage::widgetHasChanged() {
151 if(_ignoreWidgetChanges) return;
152 bool changed = testHasChanged();
153 if(changed != hasChanged()) setChangedState(changed);
156 bool NetworksSettingsPage::testHasChanged() {
158 saveToNetworkInfo(networkInfos[currentId]);
160 if(Client::networkIds().count() != networkInfos.count()) return true;
161 foreach(NetworkId id, networkInfos.keys()) {
162 if(id < 0) return true;
163 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
168 void NetworksSettingsPage::setWidgetStates() {
170 if(ui.networkList->selectedItems().count()) {
171 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
172 const Network *net = 0;
173 if(id > 0) net = Client::network(id);
174 ui.detailsBox->setEnabled(true);
175 ui.renameNetwork->setEnabled(true);
176 ui.deleteNetwork->setEnabled(true);
177 ui.connectNow->setEnabled(net);
178 // && (Client::network(id)->connectionState() == Network::Initialized
179 // || Client::network(id)->connectionState() == Network::Disconnected));
181 if(net->connectionState() == Network::Disconnected) {
182 ui.connectNow->setIcon(connectedIcon);
183 ui.connectNow->setText(tr("Connect"));
185 ui.connectNow->setIcon(disconnectedIcon);
186 ui.connectNow->setText(tr("Disconnect"));
189 ui.connectNow->setIcon(QIcon());
190 ui.connectNow->setText(tr("Apply first!"));
193 ui.renameNetwork->setEnabled(false);
194 ui.deleteNetwork->setEnabled(false);
195 ui.connectNow->setEnabled(false);
196 ui.detailsBox->setEnabled(false);
199 if(ui.serverList->selectedItems().count()) {
200 ui.editServer->setEnabled(true);
201 ui.deleteServer->setEnabled(true);
202 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
203 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
205 ui.editServer->setEnabled(false);
206 ui.deleteServer->setEnabled(false);
207 ui.upServer->setEnabled(false);
208 ui.downServer->setEnabled(false);
212 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
213 if(!item && !(item = networkItem(id))) return;
214 const Network *net = Client::network(id);
215 if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
216 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
217 if(net && net->connectionState() == Network::Initialized) {
218 item->setIcon(connectedIcon);
219 } else if(net && net->connectionState() != Network::Disconnected) {
220 item->setIcon(connectingIcon);
222 item->setIcon(disconnectedIcon);
226 // check if we already have another net of this name in the list, and replace it
227 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
229 foreach(QListWidgetItem *i, items) {
230 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
231 if(oldid > 0) continue; // only locally created nets should be replaced
232 if(oldid == currentId) {
235 ui.networkList->clearSelection();
237 int row = ui.networkList->row(i);
239 QListWidgetItem *olditem = ui.networkList->takeItem(row);
243 networkInfos.remove(oldid);
247 item->setText(net->networkName());
248 if(select) item->setSelected(true);
252 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
253 this->setEnabled(state);
262 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
263 const Identity * identity = Client::identity(id);
264 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
267 // default identity is always the first one!
268 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
270 QString name = identity->identityName();
271 for(int j = 0; j < ui.identityList->count(); j++) {
272 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
273 ui.identityList->insertItem(j, name, id.toInt());
279 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
284 void NetworksSettingsPage::clientIdentityUpdated() {
285 const Identity *identity = qobject_cast<const Identity *>(sender());
287 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
290 int row = ui.identityList->findData(identity->id().toInt());
292 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
295 if(ui.identityList->itemText(row) != identity->identityName()) {
296 ui.identityList->setItemText(row, identity->identityName());
300 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
301 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
302 //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
303 foreach(NetworkInfo info, networkInfos.values()) {
304 //qDebug() << info.networkName << info.networkId << info.identity;
305 if(info.identity == id) {
306 if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
307 info.identity = 1; // set to default
308 networkInfos[info.networkId] = info;
309 if(info.networkId > 0) Client::updateNetwork(info);
312 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
316 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
317 for(int i = 0; i < ui.networkList->count(); i++) {
318 QListWidgetItem *item = ui.networkList->item(i);
319 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
324 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
326 //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
327 connect(Client::network(id), SIGNAL(identitySet(IdentityId)), this, SLOT(clientNetworkUpdated()));
328 connect(Client::network(id), SIGNAL(networkNameSet(const QString &)), this, SLOT(clientNetworkUpdated()));
329 connect(Client::network(id), SIGNAL(serverListSet(QVariantList)), this, SLOT(clientNetworkUpdated()));
330 connect(Client::network(id), SIGNAL(useRandomServerSet(bool)), this, SLOT(clientNetworkUpdated()));
331 connect(Client::network(id), SIGNAL(performSet(const QStringList &)), this, SLOT(clientNetworkUpdated()));
332 connect(Client::network(id), SIGNAL(useAutoIdentifySet(bool)), this, SLOT(clientNetworkUpdated()));
333 connect(Client::network(id), SIGNAL(autoIdentifyServiceSet(const QString &)), this, SLOT(clientNetworkUpdated()));
334 connect(Client::network(id), SIGNAL(autoIdentifyPasswordSet(const QString &)), this, SLOT(clientNetworkUpdated()));
335 connect(Client::network(id), SIGNAL(useAutoReconnectSet(bool)), this, SLOT(clientNetworkUpdated()));
336 connect(Client::network(id), SIGNAL(autoReconnectIntervalSet(quint32)), this, SLOT(clientNetworkUpdated()));
337 connect(Client::network(id), SIGNAL(autoReconnectRetriesSet(quint16)), this, SLOT(clientNetworkUpdated()));
338 connect(Client::network(id), SIGNAL(unlimitedReconnectRetriesSet(bool)), this, SLOT(clientNetworkUpdated()));
339 connect(Client::network(id), SIGNAL(rejoinChannelsSet(bool)), this, SLOT(clientNetworkUpdated()));
340 connect(Client::network(id), SIGNAL(codecForServerSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
341 connect(Client::network(id), SIGNAL(codecForEncodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
342 connect(Client::network(id), SIGNAL(codecForDecodingSet(const QByteArray &)), this, SLOT(clientNetworkUpdated()));
344 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
345 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
348 void NetworksSettingsPage::clientNetworkUpdated() {
349 const Network *net = qobject_cast<const Network *>(sender());
351 qWarning() << "Update request for unknown network received!";
354 networkInfos[net->networkId()] = net->networkInfo();
355 setItemState(net->networkId());
356 if(net->networkId() == currentId) displayNetwork(net->networkId());
361 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
362 if(!networkInfos.contains(id)) return;
363 if(id == currentId) displayNetwork(0);
364 NetworkInfo info = networkInfos.take(id);
365 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
366 foreach(QListWidgetItem *item, items) {
367 if(item->data(Qt::UserRole).value<NetworkId>() == id)
368 delete ui.networkList->takeItem(ui.networkList->row(item));
374 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
375 const Network *net = qobject_cast<const Network *>(sender());
377 if(net->networkId() == currentId) {
378 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
380 setItemState(net->networkId());
384 void NetworksSettingsPage::networkConnectionError(const QString &) {
388 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
389 NetworkInfo info = Client::network(id)->networkInfo();
390 networkInfos[id] = info;
391 return insertNetwork(info);
394 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
395 QListWidgetItem *item = 0;
396 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
397 if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
399 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
400 // -> then we can be sure that this is the core-side replacement for the net we created
401 foreach(QListWidgetItem *i, items) {
402 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
403 if(id < 0) { item = i; break; }
405 if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
407 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
408 setItemState(info.networkId, item);
413 void NetworksSettingsPage::displayNetwork(NetworkId id) {
414 _ignoreWidgetChanges = true;
416 NetworkInfo info = networkInfos[id];
417 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
418 ui.serverList->clear();
419 foreach(QVariant v, info.serverList) {
420 ui.serverList->addItem(QString("%1:%2").arg(v.toMap()["Host"].toString()).arg(v.toMap()["Port"].toUInt()));
423 ui.randomServer->setChecked(info.useRandomServer);
424 ui.performEdit->setPlainText(info.perform.join("\n"));
425 ui.autoIdentify->setChecked(info.useAutoIdentify);
426 ui.autoIdentifyService->setText(info.autoIdentifyService);
427 ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
428 if(info.codecForEncoding.isEmpty()) {
429 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
430 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
431 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
432 ui.useDefaultEncodings->setChecked(true);
434 ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
435 ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
436 ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
437 ui.useDefaultEncodings->setChecked(false);
439 ui.autoReconnect->setChecked(info.useAutoReconnect);
440 ui.reconnectInterval->setValue(info.autoReconnectInterval);
441 ui.reconnectRetries->setValue(info.autoReconnectRetries);
442 ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
443 ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
445 // just clear widgets
446 ui.identityList->setCurrentIndex(-1);
447 ui.serverList->clear();
448 ui.performEdit->clear();
449 ui.autoIdentifyService->clear();
450 ui.autoIdentifyPassword->clear();
453 _ignoreWidgetChanges = false;
457 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
458 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
459 info.useRandomServer = ui.randomServer->isChecked();
460 info.perform = ui.performEdit->toPlainText().split("\n");
461 info.useAutoIdentify = ui.autoIdentify->isChecked();
462 info.autoIdentifyService = ui.autoIdentifyService->text();
463 info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
464 if(ui.useDefaultEncodings->isChecked()) {
465 info.codecForEncoding.clear();
466 info.codecForDecoding.clear();
467 info.codecForServer.clear();
469 info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
470 info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
471 info.codecForServer = ui.serverEncoding->currentText().toLatin1();
473 info.useAutoReconnect = ui.autoReconnect->isChecked();
474 info.autoReconnectInterval = ui.reconnectInterval->value();
475 info.autoReconnectRetries = ui.reconnectRetries->value();
476 info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
477 info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
479 /*** Network list ***/
481 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
483 saveToNetworkInfo(networkInfos[currentId]);
485 if(ui.networkList->selectedItems().count()) {
486 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
489 ui.serverList->setCurrentRow(0);
496 void NetworksSettingsPage::on_addNetwork_clicked() {
497 QStringList existing;
498 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
499 NetworkEditDlg dlg(QString(), existing, this);
500 if(dlg.exec() == QDialog::Accepted) {
502 for(id = 1; id <= networkInfos.count(); id++) {
504 if(!networkInfos.keys().contains(-id.toInt())) break;
509 info.networkName = dlg.networkName();
513 info.useRandomServer = false;
514 info.useAutoReconnect = true;
515 info.autoReconnectInterval = 60;
516 info.autoReconnectRetries = 20;
517 info.unlimitedReconnectRetries = false;
518 info.useAutoIdentify = false;
519 info.autoIdentifyService = "NickServ";
520 info.rejoinChannels = true;
522 networkInfos[id] = info;
523 QListWidgetItem *item = insertNetwork(info);
524 ui.networkList->setCurrentItem(item);
529 void NetworksSettingsPage::on_deleteNetwork_clicked() {
530 if(ui.networkList->selectedItems().count()) {
531 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
532 int ret = QMessageBox::question(this, tr("Delete Network?"),
533 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
534 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
535 if(ret == QMessageBox::Yes) {
537 networkInfos.remove(netid);
538 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
539 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
546 void NetworksSettingsPage::on_renameNetwork_clicked() {
547 if(!ui.networkList->selectedItems().count()) return;
548 QString old = ui.networkList->selectedItems()[0]->text();
549 QStringList existing;
550 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
551 NetworkEditDlg dlg(old, existing, this);
552 if(dlg.exec() == QDialog::Accepted) {
553 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
554 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
555 networkInfos[netid].networkName = dlg.networkName();
560 void NetworksSettingsPage::on_connectNow_clicked() {
561 if(!ui.networkList->selectedItems().count()) return;
562 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
563 const Network *net = Client::network(id);
565 if(net->connectionState() == Network::Disconnected) net->requestConnect();
566 else net->requestDisconnect();
569 /*** Server list ***/
571 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
575 void NetworksSettingsPage::on_addServer_clicked() {
576 if(currentId == 0) return;
577 ServerEditDlg dlg(QVariantMap(), this);
578 if(dlg.exec() == QDialog::Accepted) {
579 networkInfos[currentId].serverList.append(dlg.serverData());
580 displayNetwork(currentId);
581 ui.serverList->setCurrentRow(ui.serverList->count()-1);
587 void NetworksSettingsPage::on_editServer_clicked() {
588 if(currentId == 0) return;
589 int cur = ui.serverList->currentRow();
590 ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
591 if(dlg.exec() == QDialog::Accepted) {
592 networkInfos[currentId].serverList[cur] = dlg.serverData();
593 displayNetwork(currentId);
594 ui.serverList->setCurrentRow(cur);
599 void NetworksSettingsPage::on_deleteServer_clicked() {
600 if(currentId == 0) return;
601 int cur = ui.serverList->currentRow();
602 networkInfos[currentId].serverList.removeAt(cur);
603 displayNetwork(currentId);
604 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
608 void NetworksSettingsPage::on_upServer_clicked() {
609 int cur = ui.serverList->currentRow();
610 QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
611 networkInfos[currentId].serverList.insert(cur-1, foo);
612 displayNetwork(currentId);
613 ui.serverList->setCurrentRow(cur-1);
617 void NetworksSettingsPage::on_downServer_clicked() {
618 int cur = ui.serverList->currentRow();
619 QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
620 networkInfos[currentId].serverList.insert(cur+1, foo);
621 displayNetwork(currentId);
622 ui.serverList->setCurrentRow(cur+1);
626 /**************************************************************************
628 *************************************************************************/
630 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
635 setWindowTitle(tr("Add Network"));
636 on_networkEdit_textChanged(""); // disable ok button
637 } else ui.networkEdit->setText(old);
640 QString NetworkEditDlg::networkName() const {
641 return ui.networkEdit->text();
645 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text) {
646 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
650 /**************************************************************************
652 *************************************************************************/
654 ServerEditDlg::ServerEditDlg(const QVariant &_serverData, QWidget *parent) : QDialog(parent) {
656 QVariantMap serverData = _serverData.toMap();
657 if(serverData.count()) {
658 ui.host->setText(serverData["Host"].toString());
659 ui.port->setValue(serverData["Port"].toUInt());
660 ui.password->setText(serverData["Password"].toString());
661 ui.useSSL->setChecked(serverData["UseSSL"].toBool());
663 ui.port->setValue(6667);
665 on_host_textChanged();
668 QVariant ServerEditDlg::serverData() const {
669 QVariantMap _serverData;
670 _serverData["Host"] = ui.host->text().trimmed();
671 _serverData["Port"] = ui.port->value();
672 _serverData["Password"] = ui.password->text();
673 _serverData["UseSSL"] = ui.useSSL->isChecked();
677 void ServerEditDlg::on_host_textChanged() {
678 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
681 /**************************************************************************
683 *************************************************************************/
685 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
689 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
692 ui.progressBar->setMaximum(numevents);
693 ui.progressBar->setValue(0);
695 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
696 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
698 foreach(NetworkId id, toRemove) {
699 Client::removeNetwork(id);
701 foreach(NetworkInfo info, toCreate) {
702 Client::createNetwork(info);
704 foreach(NetworkInfo info, toUpdate) {
705 const Network *net = Client::network(info.networkId);
707 qWarning() << "Invalid client network!";
711 // FIXME this only checks for one changed item rather than all!
712 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
713 Client::updateNetwork(info);
716 qWarning() << "Sync dialog called without stuff to change!";
721 void SaveNetworksDlg::clientEvent() {
722 ui.progressBar->setValue(++rcvevents);
723 if(rcvevents >= numevents) accept();