2c182552c26d950384a95b6b00ece209628696ed
[quassel.git] / src / qtui / settingspages / networkssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QHeaderView>
22
23 #include "networkssettingspage.h"
24
25 #include "client.h"
26 #include "identity.h"
27 #include "network.h"
28
29
30 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent) : SettingsPage(tr("General"), tr("Networks"), parent) {
31   ui.setupUi(this);
32
33   connectedIcon = QIcon(":/22x22/actions/network-connect");
34   disconnectedIcon = QIcon(":/22x22/actions/network-disconnect");
35
36   currentId = 0;
37   setEnabled(false);  // need a core connection!
38   setWidgetStates();
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)));
44
45   //connect(ui.networkList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
46   //connect(ui.serverList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
47
48   foreach(IdentityId id, Client::identityIds()) {
49     clientIdentityAdded(id);
50   }
51 }
52
53 void NetworksSettingsPage::save() {
54
55
56 }
57
58 void NetworksSettingsPage::load() {
59   reset();
60   foreach(NetworkId netid, Client::networkIds()) {
61     clientNetworkAdded(netid);
62   }
63   ui.networkList->setCurrentRow(0);
64   changeState(false);
65 }
66
67 void NetworksSettingsPage::reset() {
68   currentId = 0;
69   ui.networkList->clear();
70   networkInfos.clear();
71
72   /*
73   foreach(Identity *identity, identities.values()) {
74     identity->deleteLater();
75   }
76   identities.clear();
77   deletedIdentities.clear();
78   changedIdentities.clear();
79   ui.identityList->clear();
80   */
81 }
82
83 void NetworksSettingsPage::widgetHasChanged() {
84   bool changed = testHasChanged();
85   if(changed != hasChanged()) changeState(changed);
86 }
87
88 bool NetworksSettingsPage::testHasChanged() {
89
90   return false;
91 }
92
93 void NetworksSettingsPage::setWidgetStates() {
94   // network list
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"));
104     } else {
105       ui.connectNow->setIcon(connectedIcon);
106       ui.connectNow->setText(tr("Connect"));
107     }
108   } else {
109     ui.renameNetwork->setEnabled(false);
110     ui.deleteNetwork->setEnabled(false);
111     ui.connectNow->setEnabled(false);
112     ui.detailsBox->setEnabled(false);
113   }
114   // network details
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);
120   } else {
121     ui.editServer->setEnabled(false);
122     ui.deleteServer->setEnabled(false);
123     ui.upServer->setEnabled(false);
124     ui.downServer->setEnabled(false);
125   }
126 }
127
128 void NetworksSettingsPage::coreConnectionStateChanged(bool state) {
129   this->setEnabled(state);
130   if(state) {
131     load();
132   } else {
133     // reset
134     //currentId = 0;
135   }
136 }
137
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;
142   }
143   return 0;
144 }
145
146 void NetworksSettingsPage::clientIdentityAdded(IdentityId id) {
147   const Identity * identity = Client::identity(id);
148   connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
149
150   if(id == 1) {
151     // default identity is always the first one!
152     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
153   } else {
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());
158         widgetHasChanged();
159         return;
160       }
161     }
162     // append
163     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
164     widgetHasChanged();
165   }
166 }
167
168 void NetworksSettingsPage::clientIdentityUpdated() {
169   const Identity *identity = qobject_cast<const Identity *>(sender());
170   if(!identity) {
171     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
172     return;
173   }
174   int row = ui.identityList->findData(identity->id().toInt());
175   if(row < 0) {
176     qWarning() << "NetworksSettingsPage: Invalid identity to update!";
177     return;
178   }
179   if(ui.identityList->itemText(row) != identity->identityName()) {
180     ui.identityList->setItemText(row, identity->identityName());
181   }
182 }
183
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
188   }
189   widgetHasChanged();
190 }
191
192
193 void NetworksSettingsPage::clientNetworkAdded(NetworkId id) {
194   insertNetwork(id);
195   connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
196 }
197
198 void NetworksSettingsPage::clientNetworkUpdated() {
199   const Network *net = qobject_cast<const Network *>(sender());
200   if(!net) {
201     qWarning() << "Update request for unknown network received!";
202     return;
203   }
204   QListWidgetItem *item = networkItem(net->networkId());
205   if(!item) return;
206   item->setText(net->networkName());
207   if(net->isConnected()) {
208     item->setIcon(connectedIcon);
209   } else {
210     item->setIcon(disconnectedIcon);
211   }
212 }
213
214
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);
223   } else {
224     item->setIcon(disconnectedIcon);
225   }
226   widgetHasChanged();
227 }
228
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()));
234   }
235
236 }
237
238 /*** Network list ***/
239
240 void NetworksSettingsPage::on_networkList_itemSelectionChanged() {
241   if(ui.networkList->selectedItems().count()) {
242     NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
243     displayNetwork(id);
244   }
245   setWidgetStates();
246 }
247
248
249
250
251
252
253
254