Don't loop indefinitely while trying to teach manners to Qt
[quassel.git] / src / qtui / simplenetworkeditor.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 "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 void SimpleNetworkEditor::setWidgetStates() {
43   if(ui.serverList->selectedItems().count()) {
44     ui.editServer->setEnabled(true);
45     ui.deleteServer->setEnabled(true);
46     ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
47     ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
48   } else {
49     ui.editServer->setEnabled(false);
50     ui.deleteServer->setEnabled(false);
51     ui.upServer->setEnabled(false);
52     ui.downServer->setEnabled(false);
53   }
54 }
55
56 void SimpleNetworkEditor::displayNetworkInfo(const NetworkInfo &networkInfo) {
57   _networkInfo = networkInfo;
58
59   ui.serverList->clear();
60   foreach(Network::Server server, _networkInfo.serverList) {
61     QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
62     if(server.useSsl)
63       item->setIcon(SmallIcon("document-encrypt"));
64     ui.serverList->addItem(item);
65   }
66
67   ui.networkNameEdit->setText(_networkInfo.networkName);
68   setWidgetStates();
69 }
70
71 void SimpleNetworkEditor::saveToNetworkInfo(NetworkInfo &networkInfo) {
72   _networkInfo.networkName = ui.networkNameEdit->text();
73   networkInfo = _networkInfo;
74 }
75
76 QStringList SimpleNetworkEditor::defaultChannels() const {
77   return ui.channelList->toPlainText().split("\n",  QString::SkipEmptyParts);
78 }
79
80 void SimpleNetworkEditor::setDefaultChannels(const QStringList &channels) {
81   ui.channelList->setPlainText(channels.join("\n"));
82 }
83
84 void SimpleNetworkEditor::on_serverList_itemSelectionChanged() {
85   setWidgetStates();
86 }
87
88 void SimpleNetworkEditor::on_addServer_clicked() {
89   ServerEditDlg dlg(Network::Server(), this);
90   if(dlg.exec() == QDialog::Accepted) {
91     _networkInfo.serverList.append(dlg.serverData());
92     displayNetworkInfo(_networkInfo);
93     ui.serverList->setCurrentRow(ui.serverList->count() - 1);
94     emit widgetHasChanged();
95   }
96 }
97
98 void SimpleNetworkEditor::on_editServer_clicked() {
99   int cur = ui.serverList->currentRow();
100   ServerEditDlg dlg(_networkInfo.serverList[cur], this);
101   if(dlg.exec() == QDialog::Accepted) {
102     _networkInfo.serverList[cur] = dlg.serverData();
103     displayNetworkInfo(_networkInfo);
104     ui.serverList->setCurrentRow(cur);
105     emit widgetHasChanged();
106   }
107 }
108
109 void SimpleNetworkEditor::on_deleteServer_clicked() {
110   int cur = ui.serverList->currentRow();
111   _networkInfo.serverList.removeAt(cur);
112   displayNetworkInfo(_networkInfo);
113   ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count() - 1));
114   emit widgetHasChanged();
115 }
116
117 void SimpleNetworkEditor::on_upServer_clicked() {
118   int cur = ui.serverList->currentRow();
119   Network::Server server = _networkInfo.serverList.takeAt(cur);
120   _networkInfo.serverList.insert(cur - 1, server);
121   displayNetworkInfo(_networkInfo);
122   ui.serverList->setCurrentRow(cur - 1);
123   emit widgetHasChanged();
124 }
125
126 void SimpleNetworkEditor::on_downServer_clicked() {
127   int cur = ui.serverList->currentRow();
128   Network::Server server = _networkInfo.serverList.takeAt(cur);
129   _networkInfo.serverList.insert(cur + 1, server);
130   displayNetworkInfo(_networkInfo);
131   ui.serverList->setCurrentRow(cur + 1);
132   emit widgetHasChanged();
133 }