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 disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
39 setEnabled(false); // need a core connection!
41 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
42 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
43 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
44 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
45 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
47 connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
48 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
49 //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
51 foreach(IdentityId id, Client::identityIds()) {
52 clientIdentityAdded(id);
56 void NetworksSettingsPage::save() {
57 if(currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
59 // First, remove the temporarily created networks
60 QList<NetworkInfo> toCreate, toUpdate;
61 QList<NetworkId> toRemove;
62 QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
63 while(i != networkInfos.end()) {
64 if((*i).networkId < 0) {
66 i = networkInfos.erase(i);
68 if((*i) != Client::network((*i).networkId)->networkInfo()) {
74 foreach(NetworkId id, Client::networkIds()) {
75 if(!networkInfos.contains(id)) toRemove.append(id);
77 SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
79 if(ret == QDialog::Rejected) {
80 // canceled -> reload everything to be safe
85 void NetworksSettingsPage::load() {
87 foreach(NetworkId netid, Client::networkIds()) {
88 clientNetworkAdded(netid);
90 ui.networkList->setCurrentRow(0);
91 setChangedState(false);
94 void NetworksSettingsPage::reset() {
96 ui.networkList->clear();
100 foreach(Identity *identity, identities.values()) {
101 identity->deleteLater();
104 deletedIdentities.clear();
105 changedIdentities.clear();
106 ui.identityList->clear();
110 bool NetworksSettingsPage::aboutToSave() {
112 return true; // FIXME
115 void NetworksSettingsPage::widgetHasChanged() {
116 bool changed = testHasChanged();
117 if(changed != hasChanged()) setChangedState(changed);
120 bool NetworksSettingsPage::testHasChanged() {
122 saveToNetworkInfo(networkInfos[currentId]);
124 if(Client::networkIds().count() != networkInfos.count()) return true;
125 foreach(NetworkId id, networkInfos.keys()) {
126 if(id < 0) return true;
127 if(Client::network(id)->networkInfo() != networkInfos[id]) return true;
132 void NetworksSettingsPage::setWidgetStates() {
134 if(ui.networkList->selectedItems().count()) {
135 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
136 ui.detailsBox->setEnabled(true);
137 ui.renameNetwork->setEnabled(true);
138 ui.deleteNetwork->setEnabled(true);
139 ui.connectNow->setEnabled(true);
140 if(Client::network(id) && Client::network(id)->isConnected()) {
141 ui.connectNow->setIcon(disconnectedIcon);
142 ui.connectNow->setText(tr("Disconnect"));
144 ui.connectNow->setIcon(connectedIcon);
145 ui.connectNow->setText(tr("Connect"));
148 ui.renameNetwork->setEnabled(false);
149 ui.deleteNetwork->setEnabled(false);
150 ui.connectNow->setEnabled(false);
151 ui.detailsBox->setEnabled(false);
154 if(ui.serverList->selectedItems().count()) {
155 ui.editServer->setEnabled(true);
156 ui.deleteServer->setEnabled(true);
157 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
158 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
160 ui.editServer->setEnabled(false);
161 ui.deleteServer->setEnabled(false);
162 ui.upServer->setEnabled(false);
163 ui.downServer->setEnabled(false);
167 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
168 this->setEnabled(state);
177 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
178 for(int i = 0; i < ui.networkList->count(); i++) {
179 QListWidgetItem *item = ui.networkList->item(i);
180 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
185 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
186 const Identity * identity = Client::identity(id);
187 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
190 // default identity is always the first one!
191 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
193 QString name = identity->identityName();
194 for(int j = 0; j < ui.identityList->count(); j++) {
195 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
196 ui.identityList->insertItem(j, name, id.toInt());
202 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
207 void NetworksSettingsPage::clientIdentityUpdated() {
208 const Identity *identity = qobject_cast<const Identity *>(sender());
210 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
213 int row = ui.identityList->findData(identity->id().toInt());
215 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
218 if(ui.identityList->itemText(row) != identity->identityName()) {
219 ui.identityList->setItemText(row, identity->identityName());
223 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
224 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
225 foreach(NetworkInfo info, networkInfos.values()) {
226 if(info.identity == id) info.identity = 1; // set to default
232 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
234 connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
237 void NetworksSettingsPage::clientNetworkUpdated() {
238 const Network *net = qobject_cast<const Network *>(sender());
240 qWarning() << "Update request for unknown network received!";
243 QListWidgetItem *item = networkItem(net->networkId());
245 item->setText(net->networkName());
246 if(net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
247 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
248 if(net->isConnected()) {
249 item->setIcon(connectedIcon);
251 item->setIcon(disconnectedIcon);
255 void NetworksSettingsPage::clientNetworkRemoved(NetworkId) {
259 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
260 NetworkInfo info = Client::network(id)->networkInfo();
261 networkInfos[id] = info;
262 return insertNetwork(info);
265 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
266 QListWidgetItem *item = new QListWidgetItem(disconnectedIcon, info.networkName);
267 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
268 ui.networkList->addItem(item);
269 const Network *net = Client::network(info.networkId);
270 if(net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
271 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
272 if(net && net->isConnected()) {
273 item->setIcon(connectedIcon);
275 item->setIcon(disconnectedIcon);
281 void NetworksSettingsPage::displayNetwork(NetworkId id, bool dontsave) {
283 NetworkInfo info = networkInfos[id];
284 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
285 ui.serverList->clear();
286 foreach(QVariantMap v, info.serverList) {
287 ui.serverList->addItem(QString("%1:%2").arg(v["Host"].toString()).arg(v["Port"].toUInt()));
291 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
292 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
294 /*** Network list ***/
296 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
298 saveToNetworkInfo(networkInfos[currentId]);
300 if(ui.networkList->selectedItems().count()) {
301 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
304 ui.serverList->setCurrentRow(0);
311 void NetworksSettingsPage::on_addNetwork_clicked() {
312 QStringList existing;
313 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
314 NetworkEditDlgNew dlg(QString(), existing, this);
315 if(dlg.exec() == QDialog::Accepted) {
317 for(id = 1; id <= networkInfos.count(); id++) {
319 if(!networkInfos.keys().contains(-id.toInt())) break;
324 info.networkName = dlg.networkName();
326 networkInfos[id] = info;
327 QListWidgetItem *item = insertNetwork(info);
328 ui.networkList->setCurrentItem(item);
333 void NetworksSettingsPage::on_deleteNetwork_clicked() {
334 if(ui.networkList->selectedItems().count()) {
335 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
336 int ret = QMessageBox::question(this, tr("Delete Network?"),
337 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?"
338 "<br><br><em>NOTE: Backlog deletion hasn't actually been implemented yet.</em>").arg(networkInfos[netid].networkName),
339 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
340 if(ret == QMessageBox::Yes) {
342 networkInfos.remove(netid); qDebug() << netid << networkInfos.count();
343 delete ui.networkList->selectedItems()[0];
344 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
351 void NetworksSettingsPage::on_renameNetwork_clicked() {
352 if(!ui.networkList->selectedItems().count()) return;
353 QString old = ui.networkList->selectedItems()[0]->text();
354 QStringList existing;
355 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
356 NetworkEditDlgNew dlg(old, existing, this);
357 if(dlg.exec() == QDialog::Accepted) {
358 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
359 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
360 networkInfos[netid].networkName = dlg.networkName();
365 /*** Server list ***/
367 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
371 void NetworksSettingsPage::on_addServer_clicked() {
372 if(currentId == 0) return;
373 ServerEditDlgNew dlg(QVariantMap(), this);
374 if(dlg.exec() == QDialog::Accepted) {
375 networkInfos[currentId].serverList.append(dlg.serverData());
376 displayNetwork(currentId);
377 ui.serverList->setCurrentRow(ui.serverList->count()-1);
383 void NetworksSettingsPage::on_editServer_clicked() {
384 if(currentId == 0) return;
385 int cur = ui.serverList->currentRow();
386 ServerEditDlgNew dlg(networkInfos[currentId].serverList[cur], this);
387 if(dlg.exec() == QDialog::Accepted) {
388 networkInfos[currentId].serverList[cur] = dlg.serverData();
389 displayNetwork(currentId);
390 ui.serverList->setCurrentRow(cur);
395 void NetworksSettingsPage::on_deleteServer_clicked() {
396 if(currentId == 0) return;
397 int cur = ui.serverList->currentRow();
398 networkInfos[currentId].serverList.removeAt(cur);
399 displayNetwork(currentId);
400 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
404 void NetworksSettingsPage::on_upServer_clicked() {
405 int cur = ui.serverList->currentRow();
406 QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
407 networkInfos[currentId].serverList.insert(cur-1, foo);
408 displayNetwork(currentId);
409 ui.serverList->setCurrentRow(cur-1);
413 void NetworksSettingsPage::on_downServer_clicked() {
414 int cur = ui.serverList->currentRow();
415 QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
416 networkInfos[currentId].serverList.insert(cur+1, foo);
417 displayNetwork(currentId);
418 ui.serverList->setCurrentRow(cur+1);
422 /**************************************************************************
424 *************************************************************************/
426 NetworkEditDlgNew::NetworkEditDlgNew(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
431 setWindowTitle(tr("Add Network"));
432 on_networkEdit_textChanged(""); // disable ok button
433 } else ui.networkEdit->setText(old);
436 QString NetworkEditDlgNew::networkName() const {
437 return ui.networkEdit->text();
441 void NetworkEditDlgNew::on_networkEdit_textChanged(const QString &text) {
442 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
446 /**************************************************************************
448 *************************************************************************/
450 ServerEditDlgNew::ServerEditDlgNew(const QVariantMap &serverData, QWidget *parent) : QDialog(parent) {
452 if(serverData.count()) {
453 ui.host->setText(serverData["Host"].toString());
454 ui.port->setValue(serverData["Port"].toUInt());
455 ui.password->setText(serverData["Password"].toString());
456 ui.useSSL->setChecked(serverData["UseSSL"].toBool());
458 ui.port->setValue(Global::defaultPort);
460 on_host_textChanged();
463 QVariantMap ServerEditDlgNew::serverData() const {
464 QVariantMap _serverData;
465 _serverData["Host"] = ui.host->text();
466 _serverData["Port"] = ui.port->value();
467 _serverData["Password"] = ui.password->text();
468 _serverData["UseSSL"] = ui.useSSL->isChecked();
472 void ServerEditDlgNew::on_host_textChanged() {
473 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
476 /**************************************************************************
478 *************************************************************************/
480 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)