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>
24 #include "networkssettingspage.h"
32 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
35 connectedIcon = QIcon(":/22x22/actions/network-connect");
36 connectingIcon = QIcon(":/22x22/actions/gear");
37 disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
40 setEnabled(false); // need a core connection!
42 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
43 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
44 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
45 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
46 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
48 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
49 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
50 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
52 foreach(IdentityId id, Client::identityIds()) {
53 clientIdentityAdded(id);
57 void NetworksSettingsPage::save() {
59 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
61 QList<NetworkInfo> toCreate, toUpdate;
62 QList<NetworkId> toRemove;
63 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
64 while(i != networkInfos.end()) {
65 NetworkId id = (*i).networkId;
68 //if(id == currentId) currentId = 0;
69 //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
71 // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
74 //i = networkInfos.erase(i);
77 if((*i) != Client::network((*i).networkId)->networkInfo()) {
83 foreach(NetworkId id, Client::networkIds()) {
84 if(!networkInfos.contains(id)) toRemove.append(id);
86 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
88 if(ret == QDialog::Rejected) {
89 // canceled -> reload everything to be safe
92 setChangedState(false);
96 void NetworksSettingsPage::load() {
98 foreach(NetworkId netid, Client::networkIds()) {
99 clientNetworkAdded(netid);
101 ui.networkList->setCurrentRow(0);
102 setChangedState(false);
105 void NetworksSettingsPage::reset() {
107 ui.networkList->clear();
108 networkInfos.clear();
112 bool NetworksSettingsPage::aboutToSave() {
113 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
115 foreach(NetworkInfo info, networkInfos.values()) {
116 if(!info.serverList.count()) errors.append(1);
118 if(!errors.count()) return true;
119 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
120 if(errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
121 error += tr("</ul>");
122 QMessageBox::warning(this, tr("Invalid Network Settings"), error);
126 void NetworksSettingsPage::widgetHasChanged() {
127 bool changed = testHasChanged();
128 if(changed != hasChanged()) setChangedState(changed);
131 bool NetworksSettingsPage::testHasChanged() {
133 saveToNetworkInfo(networkInfos[currentId]);
135 if(Client::networkIds().count() != networkInfos.count()) return true;
136 foreach(NetworkId id, networkInfos.keys()) {
137 if(id < 0) return true;
138 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
143 void NetworksSettingsPage::setWidgetStates() {
145 if(ui.networkList->selectedItems().count()) {
146 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
147 ui.detailsBox->setEnabled(true);
148 ui.renameNetwork->setEnabled(true);
149 ui.deleteNetwork->setEnabled(true);
150 ui.connectNow->setEnabled(id > 0
151 && (Client::network(id)->connectionState() == Network::Initialized
152 || Client::network(id)->connectionState() == Network::Disconnected));
153 if(Client::network(id) && Client::network(id)->isConnected()) {
154 ui.connectNow->setIcon(disconnectedIcon);
155 ui.connectNow->setText(tr("Disconnect"));
157 ui.connectNow->setIcon(connectedIcon);
158 ui.connectNow->setText(tr("Connect"));
161 ui.renameNetwork->setEnabled(false);
162 ui.deleteNetwork->setEnabled(false);
163 ui.connectNow->setEnabled(false);
164 ui.detailsBox->setEnabled(false);
167 if(ui.serverList->selectedItems().count()) {
168 ui.editServer->setEnabled(true);
169 ui.deleteServer->setEnabled(true);
170 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
171 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
173 ui.editServer->setEnabled(false);
174 ui.deleteServer->setEnabled(false);
175 ui.upServer->setEnabled(false);
176 ui.downServer->setEnabled(false);
180 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item) {
181 if(!item) item = networkItem(id);
182 const Network *net = Client::network(id);
183 if(!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
184 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
185 if(net && net->connectionState() == Network::Initialized) {
186 item->setIcon(connectedIcon);
187 } else if(net && net->connectionState() != Network::Disconnected) {
188 item->setIcon(connectingIcon);
190 item->setIcon(disconnectedIcon);
193 // check if we already have another net of this name in the list, and replace it
194 QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
196 foreach(QListWidgetItem *i, items) {
197 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
198 if(oldid > 0) continue; // only locally created nets should be replaced
199 if(oldid == currentId) item->setSelected(true);
200 delete ui.networkList->takeItem(ui.networkList->row(i));
201 networkInfos.remove(oldid);
205 item->setText(net->networkName());
209 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
210 this->setEnabled(state);
219 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
220 const Identity * identity = Client::identity(id);
221 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
224 // default identity is always the first one!
225 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
227 QString name = identity->identityName();
228 for(int j = 0; j < ui.identityList->count(); j++) {
229 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
230 ui.identityList->insertItem(j, name, id.toInt());
236 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
241 void NetworksSettingsPage::clientIdentityUpdated() {
242 const Identity *identity = qobject_cast<const Identity *>(sender());
244 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
247 int row = ui.identityList->findData(identity->id().toInt());
249 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
252 if(ui.identityList->itemText(row) != identity->identityName()) {
253 ui.identityList->setItemText(row, identity->identityName());
257 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
258 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
259 //ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
260 foreach(NetworkInfo info, networkInfos.values()) {
261 qDebug() << info.networkName << info.networkId << info.identity;
262 if(info.identity == id) {
263 if(info.networkId == currentId) ui.identityList->setCurrentIndex(0);
264 info.identity = 1; // set to default
265 networkInfos[info.networkId] = info;
266 if(info.networkId > 0) Client::updateNetwork(info);
269 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
273 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
274 for(int i = 0; i < ui.networkList->count(); i++) {
275 QListWidgetItem *item = ui.networkList->item(i);
276 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
281 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
283 connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
284 connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
285 connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
288 void NetworksSettingsPage::clientNetworkUpdated() {
289 const Network *net = qobject_cast<const Network *>(sender());
291 qWarning() << "Update request for unknown network received!";
294 networkInfos[net->networkId()] = net->networkInfo();
295 setItemState(net->networkId());
296 if(net->networkId() == currentId) displayNetwork(net->networkId());
301 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id) {
302 if(!networkInfos.contains(id)) return;
303 if(id == currentId) displayNetwork(0);
304 NetworkInfo info = networkInfos.take(id);
305 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
307 Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
308 delete ui.networkList->takeItem(ui.networkList->row(items[0]));
314 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state) {
315 const Network *net = qobject_cast<const Network *>(sender());
317 if(net->networkId() == currentId) {
318 ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
320 setItemState(net->networkId());
323 void NetworksSettingsPage::networkConnectionError(const QString &) {
327 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
328 NetworkInfo info = Client::network(id)->networkInfo();
329 networkInfos[id] = info;
330 return insertNetwork(info);
333 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
334 QListWidgetItem *item = 0;
335 QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
336 if(!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
338 // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
339 // -> then we can be sure that this is the core-side replacement for the net we created
340 foreach(QListWidgetItem *i, items) {
341 NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
342 if(id < 0) { item = i; break; }
344 if(!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
346 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
347 setItemState(info.networkId, item);
352 void NetworksSettingsPage::displayNetwork(NetworkId id) {
354 NetworkInfo info = networkInfos[id];
355 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
356 ui.serverList->clear();
357 foreach(QVariant v, info.serverList) {
358 ui.serverList->addItem(QString("%1:%2").arg(v.toMap()["Host"].toString()).arg(v.toMap()["Port"].toUInt()));
362 // just clear widgets
363 ui.identityList->setCurrentIndex(-1);
364 ui.serverList->clear();
365 ui.performEdit->clear();
371 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
372 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
374 /*** Network list ***/
376 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
378 saveToNetworkInfo(networkInfos[currentId]);
380 if(ui.networkList->selectedItems().count()) {
381 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
384 ui.serverList->setCurrentRow(0);
391 void NetworksSettingsPage::on_addNetwork_clicked() {
392 QStringList existing;
393 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
394 NetworkEditDlgNew dlg(QString(), existing, this);
395 if(dlg.exec() == QDialog::Accepted) {
397 for(id = 1; id <= networkInfos.count(); id++) {
399 if(!networkInfos.keys().contains(-id.toInt())) break;
404 info.networkName = dlg.networkName();
406 networkInfos[id] = info;
407 QListWidgetItem *item = insertNetwork(info);
408 ui.networkList->setCurrentItem(item);
413 void NetworksSettingsPage::on_deleteNetwork_clicked() {
414 if(ui.networkList->selectedItems().count()) {
415 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
416 int ret = QMessageBox::question(this, tr("Delete Network?"),
417 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?"
418 "<br><br><em>NOTE: Backlog deletion hasn't actually been implemented yet.</em>").arg(networkInfos[netid].networkName),
419 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
420 if(ret == QMessageBox::Yes) {
422 networkInfos.remove(netid);
423 delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
424 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
431 void NetworksSettingsPage::on_renameNetwork_clicked() {
432 if(!ui.networkList->selectedItems().count()) return;
433 QString old = ui.networkList->selectedItems()[0]->text();
434 QStringList existing;
435 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
436 NetworkEditDlgNew dlg(old, existing, this);
437 if(dlg.exec() == QDialog::Accepted) {
438 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
439 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
440 networkInfos[netid].networkName = dlg.networkName();
445 void NetworksSettingsPage::on_connectNow_clicked() {
446 if(!ui.networkList->selectedItems().count()) return;
447 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
448 const Network *net = Client::network(id);
450 if(!net->isConnected()) net->requestConnect();
451 else net->requestDisconnect();
454 /*** Server list ***/
456 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
460 void NetworksSettingsPage::on_addServer_clicked() {
461 if(currentId == 0) return;
462 ServerEditDlgNew dlg(QVariantMap(), this);
463 if(dlg.exec() == QDialog::Accepted) {
464 networkInfos[currentId].serverList.append(dlg.serverData());
465 displayNetwork(currentId);
466 ui.serverList->setCurrentRow(ui.serverList->count()-1);
472 void NetworksSettingsPage::on_editServer_clicked() {
473 if(currentId == 0) return;
474 int cur = ui.serverList->currentRow();
475 ServerEditDlgNew dlg(networkInfos[currentId].serverList[cur], this);
476 if(dlg.exec() == QDialog::Accepted) {
477 networkInfos[currentId].serverList[cur] = dlg.serverData();
478 displayNetwork(currentId);
479 ui.serverList->setCurrentRow(cur);
484 void NetworksSettingsPage::on_deleteServer_clicked() {
485 if(currentId == 0) return;
486 int cur = ui.serverList->currentRow();
487 networkInfos[currentId].serverList.removeAt(cur);
488 displayNetwork(currentId);
489 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
493 void NetworksSettingsPage::on_upServer_clicked() {
494 int cur = ui.serverList->currentRow();
495 QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
496 networkInfos[currentId].serverList.insert(cur-1, foo);
497 displayNetwork(currentId);
498 ui.serverList->setCurrentRow(cur-1);
502 void NetworksSettingsPage::on_downServer_clicked() {
503 int cur = ui.serverList->currentRow();
504 QVariant foo = networkInfos[currentId].serverList.takeAt(cur);
505 networkInfos[currentId].serverList.insert(cur+1, foo);
506 displayNetwork(currentId);
507 ui.serverList->setCurrentRow(cur+1);
511 /**************************************************************************
513 *************************************************************************/
515 NetworkEditDlgNew::NetworkEditDlgNew(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
520 setWindowTitle(tr("Add Network"));
521 on_networkEdit_textChanged(""); // disable ok button
522 } else ui.networkEdit->setText(old);
525 QString NetworkEditDlgNew::networkName() const {
526 return ui.networkEdit->text();
530 void NetworkEditDlgNew::on_networkEdit_textChanged(const QString &text) {
531 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
535 /**************************************************************************
537 *************************************************************************/
539 ServerEditDlgNew::ServerEditDlgNew(const QVariant &_serverData, QWidget *parent) : QDialog(parent) {
541 QVariantMap serverData = _serverData.toMap();
542 if(serverData.count()) {
543 ui.host->setText(serverData["Host"].toString());
544 ui.port->setValue(serverData["Port"].toUInt());
545 ui.password->setText(serverData["Password"].toString());
546 ui.useSSL->setChecked(serverData["UseSSL"].toBool());
548 ui.port->setValue(Global::defaultPort);
550 on_host_textChanged();
553 QVariant ServerEditDlgNew::serverData() const {
554 QVariantMap _serverData;
555 _serverData["Host"] = ui.host->text();
556 _serverData["Port"] = ui.port->value();
557 _serverData["Password"] = ui.password->text();
558 _serverData["UseSSL"] = ui.useSSL->isChecked();
562 void ServerEditDlgNew::on_host_textChanged() {
563 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
566 /**************************************************************************
568 *************************************************************************/
570 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
574 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
577 ui.progressBar->setMaximum(numevents);
578 ui.progressBar->setValue(0);
580 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
581 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
583 foreach(NetworkInfo info, toCreate) {
584 Client::createNetwork(info);
586 foreach(NetworkInfo info, toUpdate) {
587 const Network *net = Client::network(info.networkId);
589 qWarning() << "Invalid client network!";
593 // FIXME this only checks for one changed item rather than all!
594 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
595 Client::updateNetwork(info);
597 foreach(NetworkId id, toRemove) {
598 Client::removeNetwork(id);
601 qWarning() << "Sync dialog called without stuff to change!";
606 void SaveNetworksDlg::clientEvent() {
607 ui.progressBar->setValue(++rcvevents);
608 if(rcvevents >= numevents) accept();