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