File: settings.cpp

package info (click to toggle)
tagua 1.0~alpha2-15
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 8,028 kB
  • ctags: 7,178
  • sloc: cpp: 26,149; ansic: 13,039; makefile: 182; ruby: 87; sh: 39
file content (182 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2006-2007 Paolo Capriotti <paolo.capriotti@kdemail.net>

BSD License
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "settings.h"
#ifdef __SETTINGS_DEBUG
#include <QFile>
#include <QTextStream>
#endif

SettingConstRef::SettingConstRef(const QDomElement& element)
: m_element(element) {
  if (!m_element.isNull()) {
    if (m_element.firstChild().toText().isNull())
      m_element.appendChild(m_element.ownerDocument().createTextNode( QString() ));
  }
}

SettingRefBase::operator bool() const {
  return !element().isNull();
}

bool SettingRefBase::flag(const QString& attr, bool def) const {
  return element().attribute(attr, def ? "true" : "false") == "true";
}

SettingRef::SettingRef(const QDomElement& parent, const QString& key)
: m_parent(parent)
, m_key(key) { }

void SettingRef::remove() {
  m_parent.removeChild(element());
}

Settings::Settings(const QDomElement& node)
: m_node(node) { }

Settings::Settings(const Settings& other)
: m_node(other.node()) { }

Settings& Settings::operator=(const Settings& other) {
  m_node = other.node();
  return *this;
}

SettingRef Settings::operator[](const QString& key) {
  return SettingRef(node(), key);
}

SettingConstRef Settings::operator[](const QString& key) const {
  return SettingConstRef(node().firstChildElement(key));
}

SettingGroup Settings::group(const QString& name) const {
  return SettingGroup(node(), name);
}

SettingGroup Settings::operator()(const QString& name) const {
  return group(name);
}

SettingArray Settings::array(const QString& name) const {
  return SettingArray(node(), name);
}

SettingArray Settings::newArray(const QString& name) const {
  SettingArray res(node(), name);
  res.clear();
  return res;
}

void Settings::ensureExistence(QDomElement& node, QDomElement parent, const QString& name) {
  if (node.isNull()) {
    node = parent.firstChildElement(name);
    if (node.isNull()) {
      node = parent.ownerDocument().createElement(name);
      parent.appendChild(node);
    }
  }
  Q_ASSERT(!node.isNull());
}

bool Settings::flag(const QString& attr, bool def) const {
  //kDebug() << "get flag " << node().isNull() << " " << attr << " = "
    //<< node().attribute(attr, def ? "true" : "false");
  return node().attribute(attr, def ? "true" : "false") == "true";
}

void Settings::setFlag(const QString& attr, bool val) {
  node().setAttribute(attr, val ? "true" : "false");
}

SettingGroup::SettingGroup(const QDomElement& parent, const QString& name)
: m_name(name)
, m_parent(parent) { }

QDomElement SettingGroup::node() const {
  ensureExistence(const_cast<QDomElement&>(m_node), m_parent, m_name);
  Q_ASSERT(!m_node.isNull());
  return m_node;
}

void SettingGroup::remove() {
  node().parentNode().removeChild(node());
}

SettingArray::SettingArray(const QDomElement& node, const QString& element)
: m_node(node)
, m_element(element) {
  QDomNodeList elements = m_node.elementsByTagName(m_element);
  m_array.resize(elements.size());
  for (int i = 0; i < elements.size(); i++)
    m_array[i] = elements.item(i).toElement();
}

Settings SettingArray::get(int index) const {
  return m_array[index];
}

Settings SettingArray::insert(int index) {
  QDomElement el = node().ownerDocument().createElement(m_element);
  node().insertBefore(el, m_array[index].node());
  Settings res = el;
  m_array.insert(m_array.begin() + index, res);
  return res;
}

Settings SettingArray::append() {
  QDomElement el = node().ownerDocument().createElement(m_element);
  node().appendChild(el);
  Settings res = el;
  m_array.push_back(res);
  return res;
}

void SettingArray::clear() {
  QDomNodeList children = node().childNodes();
  while (children.size() > 0) {
    QDomElement e = children.item(0).toElement();
    if (!e.isNull() || e.tagName() == m_element)
      node().removeChild(e);
  }
}

#ifdef __SETTINGS_DEBUG
void Settings::dump() const {
  QFile outf;
  outf.open(stdout, QIODevice::WriteOnly);
  QTextStream out(&outf);

  if (node().isNull()) {
    out << "<NULL NODE />" << endl;
    return;
  }

  QDomDocument temp;
  temp.appendChild(temp.importNode(node(), true));
  out << temp.toString() << endl;
}
#endif