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>
23 #include "networkssettingspage.h"
30 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
33 connectedIcon = QIcon(":/22x22/actions/network-connect");
34 disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
37 setEnabled(false); // need a core connection!
39 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
40 connect(Client::instance(), SIGNAL(networkAdded(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
41 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
42 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
43 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
45 //connect(ui.networkList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
46 //connect(ui.serverList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
48 foreach(IdentityId id, Client::identityIds()) {
49 clientIdentityAdded(id);
53 void NetworksSettingsPage::save() {
58 void NetworksSettingsPage::load() {
60 foreach(NetworkId netid, Client::networkIds()) {
61 clientNetworkAdded(netid);
63 ui.networkList->setCurrentRow(0);
64 setChangedState(false);
67 void NetworksSettingsPage::reset() {
69 ui.networkList->clear();
73 foreach(Identity *identity, identities.values()) {
74 identity->deleteLater();
77 deletedIdentities.clear();
78 changedIdentities.clear();
79 ui.identityList->clear();
83 void NetworksSettingsPage::widgetHasChanged() {
84 bool changed = testHasChanged();
85 if(changed != hasChanged()) setChangedState(changed);
88 bool NetworksSettingsPage::testHasChanged() {
93 void NetworksSettingsPage::setWidgetStates() {
95 if(ui.networkList->selectedItems().count()) {
96 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
97 ui.detailsBox->setEnabled(true);
98 ui.renameNetwork->setEnabled(true);
99 ui.deleteNetwork->setEnabled(true);
100 ui.connectNow->setEnabled(true);
101 if(Client::network(id)->isConnected()) {
102 ui.connectNow->setIcon(disconnectedIcon);
103 ui.connectNow->setText(tr("Disconnect"));
105 ui.connectNow->setIcon(connectedIcon);
106 ui.connectNow->setText(tr("Connect"));
109 ui.renameNetwork->setEnabled(false);
110 ui.deleteNetwork->setEnabled(false);
111 ui.connectNow->setEnabled(false);
112 ui.detailsBox->setEnabled(false);
115 if(ui.serverList->selectedItems().count()) {
116 ui.editServer->setEnabled(true);
117 ui.deleteServer->setEnabled(true);
118 ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
119 ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
121 ui.editServer->setEnabled(false);
122 ui.deleteServer->setEnabled(false);
123 ui.upServer->setEnabled(false);
124 ui.downServer->setEnabled(false);
128 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
129 this->setEnabled(state);
138 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const {
139 for(int i = 0; i < ui.networkList->count(); i++) {
140 QListWidgetItem *item = ui.networkList->item(i);
141 if(item->data(Qt::UserRole).value<NetworkId>() == id) return item;
146 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
147 const Identity * identity = Client::identity(id);
148 connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
151 // default identity is always the first one!
152 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
154 QString name = identity->identityName();
155 for(int j = 0; j < ui.identityList->count(); j++) {
156 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
157 ui.identityList->insertItem(j, name, id.toInt());
163 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
168 void NetworksSettingsPage::clientIdentityUpdated() {
169 const Identity *identity = qobject_cast<const Identity *>(sender());
171 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
174 int row = ui.identityList->findData(identity->id().toInt());
176 qWarning() << "NetworksSettingsPage: Invalid identity to update!";
179 if(ui.identityList->itemText(row) != identity->identityName()) {
180 ui.identityList->setItemText(row, identity->identityName());
184 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id) {
185 ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
186 foreach(NetworkInfo info, networkInfos.values()) {
187 if(info.identity == id) info.identity = 1; // set to default
193 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
195 connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
198 void NetworksSettingsPage::clientNetworkUpdated() {
199 const Network *net = qobject_cast<const Network *>(sender());
201 qWarning() << "Update request for unknown network received!";
204 QListWidgetItem *item = networkItem(net->networkId());
206 item->setText(net->networkName());
207 if(net->isConnected()) {
208 item->setIcon(connectedIcon);
210 item->setIcon(disconnectedIcon);
215 void NetworksSettingsPage::insertNetwork(NetworkId id) {
216 NetworkInfo info = Client::network(id)->networkInfo();
217 networkInfos[id] = info;
218 QListWidgetItem *item = new QListWidgetItem(disconnectedIcon, info.networkName);
219 item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(id));
220 ui.networkList->addItem(item);
221 if(Client::network(id)->isConnected()) {
222 item->setIcon(connectedIcon);
224 item->setIcon(disconnectedIcon);
229 void NetworksSettingsPage::displayNetwork(NetworkId id, bool dontsave) {
230 NetworkInfo info = networkInfos[id];
231 ui.serverList->clear();
232 foreach(QVariantMap v, info.serverList) {
233 ui.serverList->addItem(QString("%1:%2").arg(v["Address"].toString()).arg(v["Port"].toUInt()));
238 /*** Network list ***/
240 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
241 if(ui.networkList->selectedItems().count()) {
242 NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
248 void NetworksSettingsPage::on_addNetwork_clicked() {
254 /**************************************************************************
256 *************************************************************************/
258 NetworkEditDlgNew::NetworkEditDlgNew(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist) {
263 setWindowTitle(tr("Add Network"));
264 on_networkEdit_textChanged(""); // disable ok button
265 } else ui.networkEdit->setText(old);
268 QString NetworkEditDlgNew::networkName() const {
269 return ui.networkEdit->text();
273 void NetworkEditDlgNew::on_networkEdit_textChanged(const QString &text) {
274 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
278 /**************************************************************************
280 *************************************************************************/