Merge pull request #2 from sandsmark/wii
[quassel.git] / src / qtui / simplenetworkeditor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "simplenetworkeditor.h"
22
23 #include "iconloader.h"
24
25 #include "networkssettingspage.h"
26
27 SimpleNetworkEditor::SimpleNetworkEditor(QWidget *parent)
28     : QWidget(parent)
29 {
30     ui.setupUi(this);
31
32     ui.addServer->setIcon(SmallIcon("list-add"));
33     ui.deleteServer->setIcon(SmallIcon("edit-delete"));
34     ui.editServer->setIcon(SmallIcon("configure"));
35     ui.upServer->setIcon(SmallIcon("go-up"));
36     ui.downServer->setIcon(SmallIcon("go-down"));
37
38     connect(ui.networkNameEdit, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
39     connect(ui.channelList, SIGNAL(textChanged()), this, SIGNAL(widgetHasChanged()));
40 }
41
42
43 void SimpleNetworkEditor::setWidgetStates()
44 {
45     if (ui.serverList->selectedItems().count()) {
46         ui.editServer->setEnabled(true);
47         ui.deleteServer->setEnabled(true);
48         ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
49         ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
50     }
51     else {
52         ui.editServer->setEnabled(false);
53         ui.deleteServer->setEnabled(false);
54         ui.upServer->setEnabled(false);
55         ui.downServer->setEnabled(false);
56     }
57 }
58
59
60 void SimpleNetworkEditor::displayNetworkInfo(const NetworkInfo &networkInfo)
61 {
62     _networkInfo = networkInfo;
63
64     ui.serverList->clear();
65     foreach(Network::Server server, _networkInfo.serverList) {
66         QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
67         if (server.useSsl)
68             item->setIcon(SmallIcon("document-encrypt"));
69         ui.serverList->addItem(item);
70     }
71
72     ui.networkNameEdit->setText(_networkInfo.networkName);
73     setWidgetStates();
74 }
75
76
77 void SimpleNetworkEditor::saveToNetworkInfo(NetworkInfo &networkInfo)
78 {
79     _networkInfo.networkName = ui.networkNameEdit->text();
80     networkInfo = _networkInfo;
81 }
82
83
84 QStringList SimpleNetworkEditor::defaultChannels() const
85 {
86     return ui.channelList->toPlainText().split("\n",  QString::SkipEmptyParts);
87 }
88
89
90 void SimpleNetworkEditor::setDefaultChannels(const QStringList &channels)
91 {
92     ui.channelList->setPlainText(channels.join("\n"));
93 }
94
95
96 void SimpleNetworkEditor::on_serverList_itemSelectionChanged()
97 {
98     setWidgetStates();
99 }
100
101
102 void SimpleNetworkEditor::on_addServer_clicked()
103 {
104     ServerEditDlg dlg(Network::Server(), this);
105     if (dlg.exec() == QDialog::Accepted) {
106         _networkInfo.serverList.append(dlg.serverData());
107         displayNetworkInfo(_networkInfo);
108         ui.serverList->setCurrentRow(ui.serverList->count() - 1);
109         emit widgetHasChanged();
110     }
111 }
112
113
114 void SimpleNetworkEditor::on_editServer_clicked()
115 {
116     int cur = ui.serverList->currentRow();
117     ServerEditDlg dlg(_networkInfo.serverList[cur], this);
118     if (dlg.exec() == QDialog::Accepted) {
119         _networkInfo.serverList[cur] = dlg.serverData();
120         displayNetworkInfo(_networkInfo);
121         ui.serverList->setCurrentRow(cur);
122         emit widgetHasChanged();
123     }
124 }
125
126
127 void SimpleNetworkEditor::on_deleteServer_clicked()
128 {
129     int cur = ui.serverList->currentRow();
130     _networkInfo.serverList.removeAt(cur);
131     displayNetworkInfo(_networkInfo);
132     ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count() - 1));
133     emit widgetHasChanged();
134 }
135
136
137 void SimpleNetworkEditor::on_upServer_clicked()
138 {
139     int cur = ui.serverList->currentRow();
140     Network::Server server = _networkInfo.serverList.takeAt(cur);
141     _networkInfo.serverList.insert(cur - 1, server);
142     displayNetworkInfo(_networkInfo);
143     ui.serverList->setCurrentRow(cur - 1);
144     emit widgetHasChanged();
145 }
146
147
148 void SimpleNetworkEditor::on_downServer_clicked()
149 {
150     int cur = ui.serverList->currentRow();
151     Network::Server server = _networkInfo.serverList.takeAt(cur);
152     _networkInfo.serverList.insert(cur + 1, server);
153     displayNetworkInfo(_networkInfo);
154     ui.serverList->setCurrentRow(cur + 1);
155     emit widgetHasChanged();
156 }