2 Copyright 2006-2007 Paolo Capriotti <paolo.capriotti@kdemail.net>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifdef __SETTINGS_DEBUG
30 #include <QTextStream>
33 SettingConstRef::SettingConstRef(const QDomElement
& element
)
34 : m_element(element
) {
35 if (!m_element
.isNull()) {
36 if (m_element
.firstChild().toText().isNull())
37 m_element
.appendChild(m_element
.ownerDocument().createTextNode( QString() ));
41 SettingRefBase::operator bool() const {
42 return !element().isNull();
45 bool SettingRefBase::flag(const QString
& attr
, bool def
) const {
46 return element().attribute(attr
, def
? "true" : "false") == "true";
49 SettingRef::SettingRef(const QDomElement
& parent
, const QString
& key
)
53 void SettingRef::remove() {
54 m_parent
.removeChild(element());
57 Settings::Settings(const QDomElement
& node
)
60 Settings::Settings(const Settings
& other
)
61 : m_node(other
.node()) { }
63 Settings
& Settings::operator=(const Settings
& other
) {
64 m_node
= other
.node();
68 SettingRef
Settings::operator[](const QString
& key
) {
69 return SettingRef(node(), key
);
72 SettingConstRef
Settings::operator[](const QString
& key
) const {
73 return SettingConstRef(node().firstChildElement(key
));
76 SettingGroup
Settings::group(const QString
& name
) const {
77 return SettingGroup(node(), name
);
80 SettingGroup
Settings::operator()(const QString
& name
) const {
84 SettingArray
Settings::array(const QString
& name
) const {
85 return SettingArray(node(), name
);
88 SettingArray
Settings::newArray(const QString
& name
) const {
89 SettingArray
res(node(), name
);
94 void Settings::ensureExistence(QDomElement
& node
, QDomElement parent
, const QString
& name
) {
96 node
= parent
.firstChildElement(name
);
98 node
= parent
.ownerDocument().createElement(name
);
99 parent
.appendChild(node
);
102 Q_ASSERT(!node
.isNull());
105 bool Settings::flag(const QString
& attr
, bool def
) const {
106 //std::cout << "get flag " << node().isNull() << " " << attr << " = "
107 //<< node().attribute(attr, def ? "true" : "false") << std::endl;
108 return node().attribute(attr
, def
? "true" : "false") == "true";
111 void Settings::setFlag(const QString
& attr
, bool val
) {
112 node().setAttribute(attr
, val
? "true" : "false");
115 SettingGroup::SettingGroup(const QDomElement
& parent
, const QString
& name
)
117 , m_parent(parent
) { }
119 QDomElement
SettingGroup::node() const {
120 ensureExistence(const_cast<QDomElement
&>(m_node
), m_parent
, m_name
);
121 Q_ASSERT(!m_node
.isNull());
125 void SettingGroup::remove() {
126 node().parentNode().removeChild(node());
129 SettingArray::SettingArray(const QDomElement
& node
, const QString
& element
)
131 , m_element(element
) {
132 QDomNodeList elements
= m_node
.elementsByTagName(m_element
);
133 m_array
.resize(elements
.size());
134 for (int i
= 0; i
< elements
.size(); i
++)
135 m_array
[i
] = elements
.item(i
).toElement();
138 Settings
SettingArray::get(int index
) const {
139 return m_array
[index
];
142 Settings
SettingArray::insert(int index
) {
143 QDomElement el
= node().ownerDocument().createElement(m_element
);
144 node().insertBefore(el
, m_array
[index
].node());
146 m_array
.insert(m_array
.begin() + index
, res
);
150 Settings
SettingArray::append() {
151 QDomElement el
= node().ownerDocument().createElement(m_element
);
152 node().appendChild(el
);
154 m_array
.push_back(res
);
158 void SettingArray::clear() {
159 QDomNodeList children
= node().childNodes();
160 while (children
.size() > 0) {
161 QDomElement e
= children
.item(0).toElement();
162 if (!e
.isNull() || e
.tagName() == m_element
)
163 node().removeChild(e
);
167 #ifdef __SETTINGS_DEBUG
168 void Settings::dump() const {
170 outf
.open(stdout
, QIODevice::WriteOnly
);
171 QTextStream
out(&outf
);
173 if (node().isNull()) {
174 out
<< "<NULL NODE />" << endl
;
179 temp
.appendChild(temp
.importNode(node(), true));
180 out
<< temp
.toString() << endl
;