Who stole the ! from main.cpp?
[quassel.git] / src / qtui / serverlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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 "serverlist.h"
22 #include "identities.h"
23 #include "client.h"
24 #include "signalproxy.h"
25
26 /* NOTE: This dialog holds not only the server list, but also the identities.
27  *       This makes perfect sense given the fact that connections are initiated from
28  *       this dialog, and that the dialog exists during the lifetime of the program.
29  */
30
31 ServerListDlg::ServerListDlg(QWidget *parent) : QDialog(parent) {
32   ui.setupUi(this);
33
34   QSettings settings;
35   settings.beginGroup("GUI");
36   ui.showOnStartup->setChecked(settings.value("ShowServerListOnStartup", false).toBool());
37
38   updateNetworkTree();
39   connect(ui.networkTree, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
40   connect(Client::instance(), SIGNAL(sessionDataChanged(const QString &)), this, SLOT(updateNetworkTree()));
41
42   settings.endGroup();
43
44   Client::signalProxy()->attachSignal(this, SIGNAL(requestConnect(QString)));
45
46   // Autoconnect
47   /* Should not be the client's task... :-P
48   QStringList list;
49   QVariantMap networks = Client::retrieveSessionData("Networks").toMap();
50   foreach(QString net, networks.keys()) {
51     if(networks[net].toMap()["AutoConnect"].toBool()) {
52       list << net;
53     }
54   }
55   if(!list.isEmpty()) emit requestConnect(list);
56   */
57 }
58
59 ServerListDlg::~ServerListDlg() {
60
61 }
62
63 void ServerListDlg::updateNetworkTree() {
64   QVariantMap networks = Client::retrieveSessionData("Networks").toMap();
65   //QStringList headers;
66   //headers << "Network" << "Autoconnect";
67   ui.networkTree->clear();
68   //ui.networkTree->setHeaderLabels(headers);
69   ui.networkTree->setHeaderLabel(tr("Networks"));
70   QHash<QString, QTreeWidgetItem *> groups;
71   foreach(QString net, networks.keys()) {
72     QVariantMap s = networks[net].toMap();
73     QString gr = s["Group"].toString();
74     QTreeWidgetItem *item = 0;
75     if(gr.isEmpty()) {
76       item = new QTreeWidgetItem(ui.networkTree);
77     } else {
78       if(groups.contains(gr)) {
79         item = new QTreeWidgetItem(groups[gr]);
80       } else {
81         QTreeWidgetItem *newgr = new QTreeWidgetItem(ui.networkTree);
82         //ui.networkTree->addTopLevelItem(newgr);
83         newgr->setText(0, gr);
84         newgr->setFlags(newgr->flags() & ~Qt::ItemIsSelectable);
85         groups[gr] = newgr;
86         item = new QTreeWidgetItem(newgr);
87         newgr->setExpanded(true);
88         ui.networkTree->addTopLevelItem(newgr);
89         //ui.networkTree->expandItem(newgr); //<-- buggy Qt?
90       }
91     }
92     item->setText(0, net);
93     item->setToolTip(0, s["Description"].toString());
94     //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
95     //item->setCheckState(1, Qt::Unchecked);
96   }
97   ui.networkTree->sortItems(0, Qt::AscendingOrder);
98   updateButtons();
99 }
100
101 void ServerListDlg::updateButtons() {
102   QList<QTreeWidgetItem *> selected = ui.networkTree->selectedItems();
103   ui.editButton->setEnabled(selected.size() == 1);
104   ui.deleteButton->setEnabled(selected.size() >= 1);
105   ui.connectButton->setEnabled(selected.size() >= 1);
106
107 }
108
109 bool ServerListDlg::showOnStartup() {
110   return ui.showOnStartup->isChecked();
111 }
112
113 void ServerListDlg::on_addButton_clicked() {
114   NetworkEditDlg dlg(this, QVariantMap());
115   if(dlg.exec() == QDialog::Accepted) {
116     QVariantMap networks = Client::retrieveSessionData("Networks").toMap();
117     QVariantMap net = dlg.getNetwork();
118     networks[net["Name"].toString()] = net;
119     Client::storeSessionData("Networks", networks);
120     updateNetworkTree();
121   }
122 }
123
124 void ServerListDlg::on_editButton_clicked() {
125   QString curnet = ui.networkTree->currentItem()->text(0);
126   QVariantMap networks = Client::retrieveSessionData("Networks").toMap();
127   NetworkEditDlg dlg(this, networks[curnet].toMap());
128   if(dlg.exec() == QDialog::Accepted) {
129     QVariantMap net = dlg.getNetwork();
130     networks.remove(curnet);
131     networks[net["Name"].toString()] = net;
132     Client::storeSessionData("Networks", networks);
133     updateNetworkTree();
134   }
135 }
136
137 void ServerListDlg::on_deleteButton_clicked() {
138   if(QMessageBox::warning(this, tr("Remove Network?"), tr("Are you sure you want to delete the selected network(s)?"),
139                         QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
140     QVariantMap networks = Client::retrieveSessionData("Networks").toMap();
141     QList<QTreeWidgetItem *> sel = ui.networkTree->selectedItems();
142     foreach(QTreeWidgetItem *item, sel) {
143       networks.remove(item->text(0));
144     }
145     Client::storeSessionData("Networks", networks);
146     updateNetworkTree();
147   }
148 }
149
150 void ServerListDlg::editIdentities(bool end) {
151   IdentitiesDlg dlg(this);
152   if(dlg.exec() == QDialog::Accepted) {
153     /* Should now all be handled within the dialog class. Global settings rulez0rs. */
154     //identities = dlg.getIdentities();
155     //QMap<QString, QString> mapping = dlg.getNameMapping();
156     // add mapping here  <-- well, I don't fucking know anymore what I meant by this back in 2005...
157
158     //
159     //storeIdentities();
160     //storeNetworks();  // ? how to treat mapping and NOT save changes not yet applied to the server list?
161   }
162   else if(end) exit(0);
163 }
164
165 void ServerListDlg::on_showOnStartup_stateChanged(int) {
166   //QSettings s;
167   //s.setValue("GUI/ShowServerListOnStartup", ui.showOnStartup->isChecked());
168 }
169
170 void ServerListDlg::accept() {
171   QStringList nets;
172   QList<QTreeWidgetItem *> list = ui.networkTree->selectedItems();
173   foreach(QTreeWidgetItem *item, list) {
174     nets << item->text(0);
175   }
176   emit requestConnect(nets[0]); // FIXME
177   QDialog::accept();
178 }
179
180 /***************************************************************************/
181
182 NetworkEditDlg::NetworkEditDlg(QWidget *parent, QVariantMap _network) : QDialog(parent) {
183   ui.setupUi(this);
184   network = _network;
185   oldName = network["Name"].toString();
186
187   connect(ui.serverList, SIGNAL(itemSelectionChanged()), this, SLOT(updateServerButtons()));
188
189   QVariantMap identities = Client::retrieveSessionData("Identities").toMap();
190
191   ui.identityList->addItem(tr("Default Identity"));
192   foreach(QString id, identities.keys()) {
193     if(id != "Default") ui.identityList->addItem(id);
194   }
195   QStringList groups; groups << "";
196   QVariantMap nets = Client::retrieveSessionData("Networks").toMap();
197   foreach(QString net, nets.keys()) {
198     QString gr = nets[net].toMap()["Group"].toString();
199     if(!groups.contains(gr) && !gr.isEmpty()) {
200       groups.append(gr);
201     }
202   }
203   ui.networkGroup->addItems(groups);
204   if(network.size() == 0) network = createDefaultNetwork();
205
206   ui.networkName->setText(network["Name"].toString());
207   if(network["Group"].toString().isEmpty()) ui.networkGroup->setCurrentIndex(0);
208   else ui.networkGroup->setCurrentIndex(ui.networkGroup->findText(network["Group"].toString()));
209   if(network["Identity"].toString().isEmpty() || network["Identity"].toString() == "Default") ui.identityList->setCurrentIndex(0);
210   else ui.identityList->setCurrentIndex(ui.identityList->findText(network["Identity"].toString()));
211   ui.enableAutoConnect->setChecked(network["AutoConnect"].toBool());
212   updateWidgets();
213
214   on_networkName_textChanged(ui.networkName->text());
215   ui.networkName->setFocus();
216 }
217
218 QVariantMap NetworkEditDlg::createDefaultNetwork() {
219   QVariantMap net;
220
221   net["Name"] = QString();
222   net["Group"] = QString();
223   net["Identity"] = QString("Default");
224
225   return net;
226 }
227
228 void NetworkEditDlg::updateWidgets() {
229   ui.serverList->clear();
230   foreach(QVariant s, network["Servers"].toList()) {
231     QVariantMap server = s.toMap();
232     QString entry = QString("%1:%2").arg(server["Address"].toString()).arg(server["Port"].toInt());
233     QListWidgetItem *item = new QListWidgetItem(entry);
234     //if(server["Exclude"].toBool()) item->setCheckState(Qt::Checked);
235     ui.serverList->addItem(item);
236   }
237   ui.performEdit->clear();
238   ui.performEdit->setText( network["Perform"].toString() );
239   updateServerButtons();
240 }
241
242 void NetworkEditDlg::updateServerButtons() {
243   Q_ASSERT(ui.serverList->selectedItems().size() <= 1);
244   int curidx;
245   if(ui.serverList->selectedItems().isEmpty()) curidx = -1;
246   else curidx = ui.serverList->row(ui.serverList->selectedItems()[0]);
247   ui.editServer->setEnabled(curidx >= 0);
248   ui.deleteServer->setEnabled(curidx >= 0);
249   ui.upServer->setEnabled(curidx > 0);
250   ui.downServer->setEnabled(curidx >= 0 && curidx < ui.serverList->count() - 1);
251
252 }
253
254 void NetworkEditDlg::on_networkName_textChanged(QString text) {
255   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
256 }
257
258 void NetworkEditDlg::accept() {
259   QString reason = checkValidity();
260   if(reason.isEmpty()) {
261     network["Name"] = ui.networkName->text();
262     network["Description"] = ui.networkDesc->text();
263     /*if(ui.networkGroup->currentText() == "<none>") network["Group"] = "";
264     else */ network["Group"] = ui.networkGroup->currentText();
265     network["AutoConnect"] = ui.enableAutoConnect->isChecked();
266     network["Perform"] = ui.performEdit->toPlainText();
267     if(ui.identityList->currentIndex()) network["Identity"] = ui.identityList->currentText();
268     else network["Identity"] = "Default";
269     QDialog::accept();
270   } else {
271     QMessageBox::warning(this, tr("Invalid Network Settings!"),
272                          tr("<b>Your network settings are invalid!</b><br>%1").arg(reason));
273   }
274
275 }
276
277 QString NetworkEditDlg::checkValidity() {
278   QString r;
279   QVariantMap nets = Client::retrieveSessionData("Networks").toMap();
280   if(ui.networkName->text() != oldName && nets.keys().contains(ui.networkName->text())) {
281     r += tr(" Network name already exists.");
282   }
283   if(network["Servers"].toList().isEmpty()) {
284     r += tr(" You need to enter at least one server for this network.");
285   }
286   return r;
287 }
288
289 void NetworkEditDlg::on_addServer_clicked() {
290   ServerEditDlg dlg(this);
291   if(dlg.exec() == QDialog::Accepted) {
292     QList<QVariant> list = network["Servers"].toList();
293     list.append(dlg.getServer());
294     network["Servers"] = list;
295     updateWidgets();
296   }
297 }
298
299 void NetworkEditDlg::on_editServer_clicked() {
300   int idx = ui.serverList->currentRow();
301   ServerEditDlg dlg(this, network["Servers"].toList()[idx].toMap());
302   if(dlg.exec() == QDialog::Accepted) {
303     QList<QVariant> list = network["Servers"].toList();
304     list[idx] = dlg.getServer();
305     network["Servers"] = list;
306     updateWidgets();
307   }
308 }
309
310 void NetworkEditDlg::on_deleteServer_clicked() {
311   int idx = ui.serverList->currentRow();
312   QList<QVariant> list = network["Servers"].toList();
313   list.removeAt(idx);
314   network["Servers"] = list;
315   updateWidgets();
316   if(idx < ui.serverList->count()) ui.serverList->setCurrentRow(idx);
317   else if(ui.serverList->count()) ui.serverList->setCurrentRow(ui.serverList->count()-1);
318 }
319
320 void NetworkEditDlg::on_upServer_clicked() {
321   int idx = ui.serverList->currentRow();
322   QList<QVariant> list = network["Servers"].toList();
323   list.swap(idx, idx-1);
324   network["Servers"] = list;
325   updateWidgets();
326   ui.serverList->setCurrentRow(idx-1);
327 }
328
329 void NetworkEditDlg::on_downServer_clicked() {
330   int idx = ui.serverList->currentRow();
331   QList<QVariant> list = network["Servers"].toList();
332   list.swap(idx, idx+1);
333   network["Servers"] = list;
334   updateWidgets();
335   ui.serverList->setCurrentRow(idx+1);
336 }
337
338 void NetworkEditDlg::on_editIdentities_clicked() {
339   QString id;
340   if(ui.identityList->currentIndex() > 0) id = ui.identityList->currentText();
341   else id = "Default";
342   IdentitiesDlg dlg(this, id);
343   if(dlg.exec() == QDialog::Accepted) {
344     QVariantMap identities = Client::retrieveSessionData("Identities").toMap();
345     ui.identityList->clear();
346     ui.identityList->addItem(tr("Default Identity"));
347     foreach(QString i, identities.keys()) {
348       if(i != "Default") ui.identityList->addItem(i);
349     }
350     QMap<QString, QString> mapping = dlg.getNameMapping();
351     if(mapping.contains(id)) id = mapping[id];
352     else id = "Default";
353     if(id != "Default") ui.identityList->setCurrentIndex(ui.identityList->findText(id));
354     else ui.identityList->setCurrentIndex(0);
355     network["Identity"] = id;
356   }
357 }
358
359 /***************************************************************************/
360
361 ServerEditDlg::ServerEditDlg(QWidget *parent, QVariantMap server) : QDialog(parent) {
362   ui.setupUi(this);
363
364   if(!server.isEmpty()) {
365     ui.serverAddress->setText(server["Address"].toString());
366     ui.serverPort->setValue(server["Port"].toInt());
367   } else {
368     ui.serverAddress->setText(QString());
369     ui.serverPort->setValue(6667);
370   }
371   on_serverAddress_textChanged();
372 }
373
374 void ServerEditDlg::on_serverAddress_textChanged() {
375   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui.serverAddress->text().isEmpty());
376 }
377
378 QVariantMap ServerEditDlg::getServer() {
379   QVariantMap s;
380   s["Address"] = ui.serverAddress->text();
381   s["Port"] = ui.serverPort->text();
382   return s;
383 }
384
385
386 /***************************************************************************/