47
48
* \param name 'Path' to the option ("appearance.emoticons.useSmilies")
48
49
* \return value of the option. Will be invalid if non-existant.
50
QVariant OptionsTree::getOption(const QString& name)
51
QVariant OptionsTree::getOption(const QString& name) const
52
53
QVariant value=tree_.getValue(name);
53
54
if (value==VariantTree::missingValue) {
54
55
value=QVariant(QVariant::Invalid);
55
qDebug() << "Accessing missing option " << name;
56
qWarning("Accessing missing option %s", qPrintable(name));
61
* Sets the value of the named option. If the option or any parents in the
62
* hierachy do not exist, they are created. Emits the optionChanged signal
63
* if the value differs from the existing value.
62
* \brief Sets the value of the named option.
63
* If the option or any parents in the
64
* hierachy do not exist, they are created and optionAboutToBeInserted and
65
* optionInserted will be emited.
67
* Emits the optionChanged signal if the value differs from the existing value.
64
68
* \param name "Path" to the option
65
69
* \param value Value of the option
67
71
void OptionsTree::setOption(const QString& name, const QVariant& value)
69
if ( tree_.getValue(name) == value )
73
const QVariant &prev = tree_.getValue(name);
74
if ( prev == value ) {
77
if (!prev.isValid()) {
78
emit optionAboutToBeInserted(name);
71
80
tree_.setValue(name, value);
81
if (!prev.isValid()) {
82
emit optionInserted(name);
72
84
emit optionChanged(name);
89
* @brief returns true iff the node @a node is an internal node.
91
bool OptionsTree::isInternalNode(const QString &node) const
93
return tree_.isInternalNode(node);
97
* \brief Sets the comment of the named option.
98
* \param name "Path" to the option
99
* \param comment the comment to store
78
101
void OptionsTree::setComment(const QString& name, const QString& comment)
107
* \brief Returns the comment of the specified option.
108
* \param name "Path" to the option
86
QString OptionsTree::getComment(const QString& name)
110
QString OptionsTree::getComment(const QString& name) const
88
112
return tree_.getComment(name);
115
bool OptionsTree::removeOption(const QString &name, bool internal_nodes)
117
emit optionAboutToBeRemoved(name);
118
bool ok = tree_.remove(name, internal_nodes);
119
emit optionRemoved(name);
93
125
* Names of every stored option
94
126
* \return Names of options
96
QStringList OptionsTree::allOptionNames()
128
QStringList OptionsTree::allOptionNames() const
98
130
return tree_.nodeChildren();
102
134
* Names of all child options of the given option.
103
135
* \param direct return only the direct children
104
* \return Names of options
136
* \param internal_nodes include internal (non-final) nodes
137
* \return Full names of options
106
139
QStringList OptionsTree::getChildOptionNames(const QString& parent, bool direct, bool internal_nodes) const
108
141
return tree_.nodeChildren(parent,direct,internal_nodes);
144
bool OptionsTree::isValidName(const QString &name)
146
foreach(QString part, name.split('.')) {
147
if (!VariantTree::isValidNodeName(part)) return false;
153
QString OptionsTree::mapLookup(const QString &basename, const QVariant &key) const
155
QStringList children = getChildOptionNames( basename, true, true);
156
foreach (QString path, children) {
157
if (getOption(path+".key") == key) {
161
qWarning("Accessing missing key '%s' in option map '%s'", qPrintable(key.toString()), qPrintable(basename));
162
return basename + "XXX";
165
QVariant OptionsTree::mapGet(const QString &basename, const QVariant &key, const QString &node) const {
166
return getOption(mapLookup(basename, key) + "." + node);
169
QVariant OptionsTree::mapGet(const QString &basename, const QVariant &key, const QString &node, const QVariant &def) const {
170
QVariantList keys = mapKeyList(basename);
171
if (keys.contains(key)) {
172
return getOption(mapLookup(basename, key) + "." + node);
179
QString OptionsTree::mapPut(const QString &basename, const QVariant &key)
181
QStringList children = getChildOptionNames( basename, true, true);
182
foreach (QString path, children) {
183
if (getOption(path+".key") == key) {
187
// FIXME performance?
189
// allocate first unused index
193
path = basename+".m"+QString::number(i);
195
} while (children.contains(path));
196
setOption(path + ".key", key);
200
void OptionsTree::mapPut(const QString &basename, const QVariant &key, const QString &node, const QVariant &value) {
201
setOption(mapPut(basename, key) + "." + node, value);
204
QVariantList OptionsTree::mapKeyList(const QString &basename) const
207
QStringList children = getChildOptionNames( basename, true, true);
208
foreach (QString path, children) {
209
ret << getOption(path+".key");
112
217
* Saves all options to the specified file
113
218
* \param fileName Name of the file to which to save options
116
221
* \param configNS Namespace of the config format
117
222
* \return 'true' if the file saves, 'false' if it fails
119
bool OptionsTree::saveOptions(const QString& fileName, const QString& configName, const QString& configNS, const QString& configVersion)
224
bool OptionsTree::saveOptions(const QString& fileName, const QString& configName, const QString& configNS, const QString& configVersion) const
121
226
QDomDocument doc(configName);
153
250
bool OptionsTree::loadOptions(const QString& fileName, const QString& configName, const QString& configNS, const QString& configVersion)
156
QFile file(fileName);
157
if(!file.open(QIODevice::ReadOnly)) {
161
// Load document from file
252
AtomicXmlFile f(fileName);
162
253
QDomDocument doc;
163
if(!doc.setContent(&file))
254
if (!f.loadDocument(&doc))
167
257
return loadOptions(doc.documentElement(), configName, configVersion, configNS);
171
261
* Loads all options from an XML element
172
* \param element the element to read the options from
262
* \param base the element to read the options from
173
263
* \param configName Name of the root element to check for
264
* \param configNS Namespace of the config format
174
265
* \param configVersion If specified, the function will fail if the file version doesn't match
176
267
bool OptionsTree::loadOptions(const QDomElement& base, const QString& configName, const QString& configNS, const QString& configVersion)