SVN_SILENT
[kdenetwork.git] / krdc / hostpreferences.cpp
blobf9aa42c890ec5402a618c4b4dbc35d3186163f28
1 /****************************************************************************
2 **
3 ** Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
4 **
5 ** This file is part of KDE.
6 **
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 "hostpreferences.h"
26 #include "settings.h"
28 #include <KDebug>
29 #include <KLocale>
30 #include <KStandardDirs>
31 #include <KTitleWidget>
33 #include <QCheckBox>
34 #include <QFile>
35 #include <QVBoxLayout>
37 HostPreferences::HostPreferences(const QString &url, QObject *parent)
38 : QObject(parent),
39 m_url(url),
40 m_showConfigAgain(true),
41 m_walletSupport(true)
43 if (m_url.endsWith('/')) // check case when user enters an ending slash -> remove it
44 m_url.truncate(m_url.length() - 1);
46 m_filename = KStandardDirs::locateLocal("appdata", "hostpreferences.xml");
48 QFile file(m_filename);
50 if (!m_doc.setContent(&file)) {
51 kWarning(5010) << "Error reading " << m_filename;
53 // no xml file found, create a new one
54 QDomDocument domDocument("krdc");
55 QDomProcessingInstruction process = domDocument.createProcessingInstruction(
56 "xml", "version=\"1.0\" encoding=\"UTF-8\"");
57 domDocument.appendChild(process);
59 QDomElement root = domDocument.createElement("krdc");
60 root.setAttribute("version", "1.0");
61 domDocument.appendChild(root);
63 if (!file.open(QFile::WriteOnly | QFile::Truncate))
64 kWarning(5010) << "Error creating " << m_filename;
66 QTextStream out(&file);
67 domDocument.save(out, 4);
69 file.close();
71 m_doc.setContent(&file);
75 HostPreferences::~HostPreferences()
79 void HostPreferences::updateElement(const QString &name, const QString &value)
81 QDomElement oldElement = m_element.firstChildElement(name);
83 if (oldElement == QDomElement()) {
84 oldElement = m_doc.createElement(name);
85 m_element.appendChild(oldElement);
88 QDomElement newElement = m_doc.createElement(name);
89 QDomText newText = m_doc.createTextNode(value);
90 newElement.appendChild(newText);
91 m_element.replaceChild(newElement, oldElement);
94 bool HostPreferences::saveConfig()
96 saveProtocolSpecificConfig();
98 if (showAgainCheckBox) // check if the checkbox has been created
99 setShowConfigAgain(showAgainCheckBox->isChecked());
101 if (walletSupportCheckBox)
102 setWalletSupport(walletSupportCheckBox->isChecked());
104 updateElement("showConfigAgain", m_showConfigAgain ? "true" : "false");
105 updateElement("walletSupport", m_walletSupport ? "true" : "false");
107 QDomElement root = m_doc.documentElement();
108 QDomElement oldElement = QDomElement();
109 for (QDomNode n = root.firstChild(); !n.isNull(); n = n.nextSibling()) {
110 if (n.toElement().hasAttribute("url") && n.toElement().attribute("url") == m_url) {
111 oldElement = n.toElement();
115 if (oldElement == QDomElement()) // host not existing, create new one
116 m_doc.appendChild(m_element);
117 else
118 m_doc.replaceChild(m_element, oldElement);
120 QFile file(m_filename);
121 if (!file.open(QFile::WriteOnly | QFile::Text)) {
122 kWarning(5010) << "Cannot write " << m_filename << ". " << file.errorString();
123 return false;
126 QTextStream out(&file);
127 m_doc.save(out, 4);
128 return true;
131 bool HostPreferences::hostConfigured()
133 QDomElement root = m_doc.documentElement();
134 for (QDomNode n = root.firstChild(); !n.isNull(); n = n.nextSibling()) {
135 if (n.toElement().hasAttribute("url") && n.toElement().attribute("url") == m_url) {
136 kDebug(5010) << "Found: " << m_url;
137 m_element = n.toElement();
139 readConfig();
141 return true;
145 // host not configured yet, create a new host element
146 m_element = m_doc.createElement("host");
147 m_doc.documentElement().appendChild(m_element);
148 m_element.setTagName("host");
149 m_element.setAttribute("url", m_url);
151 readConfig();
153 return false;
156 void HostPreferences::readConfig()
158 readProtocolSpecificConfig();
160 setShowConfigAgain(m_element.firstChildElement("showConfigAgain").text() != "false");
161 if (m_element.firstChildElement("walletSupport") != QDomElement())
162 setWalletSupport(m_element.firstChildElement("walletSupport").text() != "false");
163 else
164 setWalletSupport(Settings::walletSupport());
167 void HostPreferences::setShowConfigAgain(bool show)
169 m_showConfigAgain = show;
172 bool HostPreferences::showConfigAgain()
174 return m_showConfigAgain;
177 void HostPreferences::setWalletSupport(bool walletSupport)
179 m_walletSupport = walletSupport;
182 bool HostPreferences::walletSupport()
184 return m_walletSupport;
187 KDialog *HostPreferences::createDialog(QWidget *widget)
189 KDialog *dialog = new KDialog;
190 dialog->setCaption(i18n("Host Configuration"));
192 QWidget *mainWidget = new QWidget(dialog);
193 QVBoxLayout *layout = new QVBoxLayout(mainWidget);
195 KTitleWidget *titleWidget = new KTitleWidget(dialog);
196 titleWidget->setText(i18n("Host Configuration"));
197 titleWidget->setPixmap(KIcon("krdc"));
198 layout->addWidget(titleWidget);
200 layout->addWidget(widget);
202 showAgainCheckBox = new QCheckBox(mainWidget);
203 showAgainCheckBox->setText(i18n("Show this dialog again for this host"));
204 showAgainCheckBox->setChecked(showConfigAgain());
206 walletSupportCheckBox = new QCheckBox(mainWidget);
207 walletSupportCheckBox->setText(i18n("Remember password (KWallet)"));
208 walletSupportCheckBox->setChecked(walletSupport());
210 layout->addWidget(showAgainCheckBox);
211 layout->addWidget(walletSupportCheckBox);
212 layout->addStretch(1);
213 mainWidget->setLayout(layout);
215 dialog->setMainWidget(mainWidget);
217 return dialog;
220 #include "hostpreferences.moc"