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(networkAdded(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);
256 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id) {
257 NetworkInfo info = Client::network(id)->networkInfo();
258 networkInfos[id] = info;
259 return insertNetwork(info);
262 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info) {
263 QListWidgetItem *item = new QListWidgetItem(disconnectedIcon, info.networkName);
264 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
265 ui.networkList->addItem(item);
266 const Network *net = Client::network(info.networkId);
267 if(net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
268 else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
269 if(net && net->isConnected()) {
270 item->setIcon(connectedIcon);
272 item->setIcon(disconnectedIcon);
278 void NetworksSettingsPage::displayNetwork(NetworkId id, bool dontsave) {
280 NetworkInfo info = networkInfos[id];
281 ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
282 ui.serverList->clear();
283 foreach(QVariantMap v, info.serverList) {
284 ui.serverList->addItem(QString("%1:%2").arg(v["Host"].toString()).arg(v["Port"].toUInt()));
288 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info) {
289 info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
291 /*** Network list ***/
293 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
295 saveToNetworkInfo(networkInfos[currentId]);
297 if(ui.networkList->selectedItems().count()) {
298 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
301 ui.serverList->setCurrentRow(0);
308 void NetworksSettingsPage::on_addNetwork_clicked() {
309 QStringList existing;
310 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
311 NetworkEditDlgNew dlg(QString(), existing, this);
312 if(dlg.exec() == QDialog::Accepted) {
314 for(id = 1; id <= networkInfos.count(); id++) {
316 if(!networkInfos.keys().contains(-id.toInt())) break;
321 info.networkName = dlg.networkName();
323 networkInfos[id] = info;
324 QListWidgetItem *item = insertNetwork(info);
325 ui.networkList->setCurrentItem(item);
330 void NetworksSettingsPage::on_deleteNetwork_clicked() {
331 if(ui.networkList->selectedItems().count()) {
332 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
333 int ret = QMessageBox::question(this, tr("Delete Network?"),
334 tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?"
335 "<br><br><em>NOTE: Backlog deletion hasn't actually been implemented yet.</em>").arg(networkInfos[netid].networkName),
336 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
337 if(ret == QMessageBox::Yes) {
339 networkInfos.remove(netid); qDebug() << netid << networkInfos.count();
340 delete ui.networkList->selectedItems()[0];
341 ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
348 void NetworksSettingsPage::on_renameNetwork_clicked() {
349 if(!ui.networkList->selectedItems().count()) return;
350 QString old = ui.networkList->selectedItems()[0]->text();
351 QStringList existing;
352 for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
353 NetworkEditDlgNew dlg(old, existing, this);
354 if(dlg.exec() == QDialog::Accepted) {
355 ui.networkList->selectedItems()[0]->setText(dlg.networkName());
356 NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
357 networkInfos[netid].networkName = dlg.networkName();
362 /*** Server list ***/
364 void NetworksSettingsPage::on_serverList_itemSelectionChanged() {
368 void NetworksSettingsPage::on_addServer_clicked() {
369 if(currentId == 0) return;
370 ServerEditDlgNew dlg(QVariantMap(), this);
371 if(dlg.exec() == QDialog::Accepted) {
372 networkInfos[currentId].serverList.append(dlg.serverData());
373 displayNetwork(currentId);
374 ui.serverList->setCurrentRow(ui.serverList->count()-1);
380 void NetworksSettingsPage::on_editServer_clicked() {
381 if(currentId == 0) return;
382 int cur = ui.serverList->currentRow();
383 ServerEditDlgNew dlg(networkInfos[currentId].serverList[cur], this);
384 if(dlg.exec() == QDialog::Accepted) {
385 networkInfos[currentId].serverList[cur] = dlg.serverData();
386 displayNetwork(currentId);
387 ui.serverList->setCurrentRow(cur);
392 void NetworksSettingsPage::on_deleteServer_clicked() {
393 if(currentId == 0) return;
394 int cur = ui.serverList->currentRow();
395 networkInfos[currentId].serverList.removeAt(cur);
396 displayNetwork(currentId);
397 ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
401 void NetworksSettingsPage::on_upServer_clicked() {
402 int cur = ui.serverList->currentRow();
403 QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
404 networkInfos[currentId].serverList.insert(cur-1, foo);
405 displayNetwork(currentId);
406 ui.serverList->setCurrentRow(cur-1);
410 void NetworksSettingsPage::on_downServer_clicked() {
411 int cur = ui.serverList->currentRow();
412 QVariantMap foo = networkInfos[currentId].serverList.takeAt(cur);
413 networkInfos[currentId].serverList.insert(cur+1, foo);
414 displayNetwork(currentId);
415 ui.serverList->setCurrentRow(cur+1);
419 /**************************************************************************
421 *************************************************************************/
423 NetworkEditDlgNew::NetworkEditDlgNew(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
428 setWindowTitle(tr("Add Network"));
429 on_networkEdit_textChanged(""); // disable ok button
430 } else ui.networkEdit->setText(old);
433 QString NetworkEditDlgNew::networkName() const {
434 return ui.networkEdit->text();
438 void NetworkEditDlgNew::on_networkEdit_textChanged(const QString &text) {
439 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
443 /**************************************************************************
445 *************************************************************************/
447 ServerEditDlgNew::ServerEditDlgNew(const QVariantMap &serverData, QWidget *parent) : QDialog(parent) {
449 if(serverData.count()) {
450 ui.host->setText(serverData["Host"].toString());
451 ui.port->setValue(serverData["Port"].toUInt());
452 ui.password->setText(serverData["Password"].toString());
453 ui.useSSL->setChecked(serverData["UseSSL"].toBool());
455 ui.port->setValue(Global::defaultPort);
457 on_host_textChanged();
460 QVariantMap ServerEditDlgNew::serverData() const {
461 QVariantMap _serverData;
462 _serverData["Host"] = ui.host->text();
463 _serverData["Port"] = ui.port->value();
464 _serverData["Password"] = ui.password->text();
465 _serverData["UseSSL"] = ui.useSSL->isChecked();
469 void ServerEditDlgNew::on_host_textChanged() {
470 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().isEmpty());
473 /**************************************************************************
475 *************************************************************************/
477 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)