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 "hostpreferences.h"
30 #include <KStandardDirs>
31 #include <KTitleWidget>
35 #include <QVBoxLayout>
37 HostPreferences::HostPreferences(const QString
&url
, QObject
*parent
)
41 walletSupportCheckBox(0),
42 m_showConfigAgain(true),
45 if (m_url
.endsWith('/')) // check case when user enters an ending slash -> remove it
46 m_url
.truncate(m_url
.length() - 1);
48 m_filename
= KStandardDirs::locateLocal("appdata", "hostpreferences.xml");
50 QFile
file(m_filename
);
52 if (!m_doc
.setContent(&file
)) {
53 kWarning(5010) << "Error reading " << m_filename
;
55 // no xml file found, create a new one
56 QDomDocument
domDocument("krdc");
57 QDomProcessingInstruction process
= domDocument
.createProcessingInstruction(
58 "xml", "version=\"1.0\" encoding=\"UTF-8\"");
59 domDocument
.appendChild(process
);
61 QDomElement root
= domDocument
.createElement("krdc");
62 root
.setAttribute("version", "1.0");
63 domDocument
.appendChild(root
);
65 if (!file
.open(QFile::WriteOnly
| QFile::Truncate
))
66 kWarning(5010) << "Error creating " << m_filename
;
68 QTextStream
out(&file
);
69 domDocument
.save(out
, 4);
73 m_doc
.setContent(&file
);
77 HostPreferences::~HostPreferences()
81 void HostPreferences::updateElement(const QString
&name
, const QString
&value
)
83 QDomElement oldElement
= m_element
.firstChildElement(name
);
85 if (oldElement
== QDomElement()) {
86 oldElement
= m_doc
.createElement(name
);
87 m_element
.appendChild(oldElement
);
90 QDomElement newElement
= m_doc
.createElement(name
);
91 QDomText newText
= m_doc
.createTextNode(value
);
92 newElement
.appendChild(newText
);
93 m_element
.replaceChild(newElement
, oldElement
);
96 bool HostPreferences::saveConfig()
98 saveProtocolSpecificConfig();
100 if (showAgainCheckBox
) // check if the checkbox has been created
101 setShowConfigAgain(showAgainCheckBox
->isChecked());
103 if (walletSupportCheckBox
)
104 setWalletSupport(walletSupportCheckBox
->isChecked());
106 updateElement("showConfigAgain", m_showConfigAgain
? "true" : "false");
107 updateElement("walletSupport", m_walletSupport
? "true" : "false");
109 QDomElement root
= m_doc
.documentElement();
110 QDomElement oldElement
= QDomElement();
111 for (QDomNode n
= root
.firstChild(); !n
.isNull(); n
= n
.nextSibling()) {
112 if (n
.toElement().hasAttribute("url") && n
.toElement().attribute("url") == m_url
) {
113 oldElement
= n
.toElement();
117 if (oldElement
== QDomElement()) // host not existing, create new one
118 m_doc
.appendChild(m_element
);
120 m_doc
.replaceChild(m_element
, oldElement
);
122 QFile
file(m_filename
);
123 if (!file
.open(QFile::WriteOnly
| QFile::Text
)) {
124 kWarning(5010) << "Cannot write " << m_filename
<< ". " << file
.errorString();
128 QTextStream
out(&file
);
133 bool HostPreferences::hostConfigured()
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") == m_url
) {
138 kDebug(5010) << "Found: " << m_url
;
139 m_element
= n
.toElement();
147 // host not configured yet, create a new host element
148 m_element
= m_doc
.createElement("host");
149 m_doc
.documentElement().appendChild(m_element
);
150 m_element
.setTagName("host");
151 m_element
.setAttribute("url", m_url
);
158 void HostPreferences::readConfig()
160 readProtocolSpecificConfig();
162 setShowConfigAgain(m_element
.firstChildElement("showConfigAgain").text() != "false");
163 if (m_element
.firstChildElement("walletSupport") != QDomElement())
164 setWalletSupport(m_element
.firstChildElement("walletSupport").text() != "false");
166 setWalletSupport(Settings::walletSupport());
169 void HostPreferences::setShowConfigAgain(bool show
)
171 m_showConfigAgain
= show
;
174 bool HostPreferences::showConfigAgain()
176 return m_showConfigAgain
;
179 void HostPreferences::setWalletSupport(bool walletSupport
)
181 m_walletSupport
= walletSupport
;
184 bool HostPreferences::walletSupport()
186 return m_walletSupport
;
189 KDialog
*HostPreferences::createDialog(QWidget
*widget
)
191 KDialog
*dialog
= new KDialog
;
192 dialog
->setCaption(i18n("Host Configuration"));
194 QWidget
*mainWidget
= new QWidget(dialog
);
195 QVBoxLayout
*layout
= new QVBoxLayout(mainWidget
);
197 KTitleWidget
*titleWidget
= new KTitleWidget(dialog
);
198 titleWidget
->setText(i18n("Host Configuration"));
199 titleWidget
->setPixmap(KIcon("krdc"));
200 layout
->addWidget(titleWidget
);
202 layout
->addWidget(widget
);
204 showAgainCheckBox
= new QCheckBox(mainWidget
);
205 showAgainCheckBox
->setText(i18n("Show this dialog again for this host"));
206 showAgainCheckBox
->setChecked(showConfigAgain());
208 walletSupportCheckBox
= new QCheckBox(mainWidget
);
209 walletSupportCheckBox
->setText(i18n("Remember password (KWallet)"));
210 walletSupportCheckBox
->setChecked(walletSupport());
212 layout
->addWidget(showAgainCheckBox
);
213 layout
->addWidget(walletSupportCheckBox
);
214 layout
->addStretch(1);
215 mainWidget
->setLayout(layout
);
217 dialog
->setMainWidget(mainWidget
);
222 #include "hostpreferences.moc"