1 /****************************************************************************
3 ** Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
5 ** This file is part of KDE.
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; see the file COPYING. If not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA 02110-1301, USA.
22 ****************************************************************************/
24 #include "hostpreferenceslist.h"
27 #include "vnchostpreferences.h"
30 #include "rdphostpreferences.h"
36 #include <KMessageBox>
37 #include <KPushButton>
38 #include <KStandardDirs>
42 #include <QListWidget>
44 HostPreferencesList::HostPreferencesList(QWidget
*parent
)
47 hostList
= new QListWidget(this);
48 connect(hostList
, SIGNAL(itemSelectionChanged()), SLOT(selectionChanged()));
49 connect(hostList
, SIGNAL(itemDoubleClicked(QListWidgetItem
*)), SLOT(configureHost()));
51 configureButton
= new KPushButton(this);
52 configureButton
->setEnabled(false);
53 configureButton
->setText(i18n("Configure..."));
54 configureButton
->setIcon(KIcon("configure"));
55 connect(configureButton
, SIGNAL(clicked()), SLOT(configureHost()));
57 removeButton
= new KPushButton(this);
58 removeButton
->setEnabled(false);
59 removeButton
->setText(i18n("Remove"));
60 removeButton
->setIcon(KIcon("list-remove"));
61 connect(removeButton
, SIGNAL(clicked()), SLOT(removeHost()));
63 QVBoxLayout
*buttonLayout
= new QVBoxLayout
;
64 buttonLayout
->addWidget(configureButton
);
65 buttonLayout
->addWidget(removeButton
);
66 buttonLayout
->addStretch();
68 QHBoxLayout
*mainLayout
= new QHBoxLayout(this);
69 mainLayout
->addWidget(hostList
);
70 mainLayout
->addLayout(buttonLayout
);
72 setLayout(mainLayout
);
77 HostPreferencesList::~HostPreferencesList()
81 void HostPreferencesList::readConfig()
83 m_filename
= KStandardDirs::locateLocal("appdata", "hostpreferences.xml");
85 QFile
file(m_filename
);
87 if (!m_doc
.setContent(&file
)) {
88 kWarning(5010) << "Error reading " << m_filename
;
92 QDomElement root
= m_doc
.documentElement();
93 for (QDomNode n
= root
.firstChild(); !n
.isNull(); n
= n
.nextSibling()) {
94 if (n
.toElement().hasAttribute("url"))
95 hostList
->addItem(new QListWidgetItem(n
.toElement().attribute("url")));
99 void HostPreferencesList::saveSettings()
103 void HostPreferencesList::configureHost()
105 QList
<QListWidgetItem
*> selectedItems
= hostList
->selectedItems();
107 foreach(QListWidgetItem
*selectedItem
, selectedItems
) {
108 QString url
= selectedItem
->text();
110 kDebug(5010) << "Configure host: " << url
;
113 if (url
.startsWith("vnc", Qt::CaseInsensitive
))
114 VncHostPreferences
hostPreferences(url
, true, this);
118 if (url
.startsWith("rdp", Qt::CaseInsensitive
))
119 RdpHostPreferences
hostPreferences(url
, true, this);
122 KMessageBox::error(this,
123 i18n("The selected host cannot be handled."),
124 i18n("Unusable URL"));
128 void HostPreferencesList::removeHost()
130 QList
<QListWidgetItem
*> selectedItems
= hostList
->selectedItems();
132 foreach(QListWidgetItem
*selectedItem
, selectedItems
) {
133 kDebug(5010) << "Remove host: " << selectedItem
->text();
135 QDomElement root
= m_doc
.documentElement();
136 for (QDomNode n
= root
.firstChild(); !n
.isNull(); n
= n
.nextSibling()) {
137 if (n
.toElement().hasAttribute("url") && n
.toElement().attribute("url") == selectedItem
->text()) {
138 kDebug(5010) << "Found and remove now: " << selectedItem
->text();
142 delete(selectedItem
);
145 QFile
file(m_filename
);
146 if (!file
.open(QFile::WriteOnly
| QFile::Text
)) {
147 kWarning(5010) << "Cannot write " << m_filename
<< ". " << file
.errorString();
150 QTextStream
out(&file
);
153 hostList
->clearSelection();
156 void HostPreferencesList::selectionChanged()
158 bool enabled
= hostList
->selectedItems().isEmpty() ? false : true;
160 configureButton
->setEnabled(enabled
);
161 removeButton
->setEnabled(enabled
);
164 #include "hostpreferenceslist.moc"