~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/tools/optionstree/optionstree.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *
19
19
 */
20
20
 
 
21
#include "optionstree.h"
 
22
 
21
23
#include <QDomElement>
22
24
#include <QDomDocument>
23
 
#include <QDebug>
24
 
 
25
 
#include "optionstree.h"
26
 
 
 
25
#include <QStringList>
 
26
 
 
27
#include "atomicxmlfile.h"
27
28
 
28
29
/**
29
30
 * Default constructor
47
48
 * \param name 'Path' to the option ("appearance.emoticons.useSmilies")
48
49
 * \return value of the option. Will be invalid if non-existant.
49
50
 */
50
 
QVariant OptionsTree::getOption(const QString& name)
 
51
QVariant OptionsTree::getOption(const QString& name) const
51
52
{
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));
56
57
        }
57
58
        return value;
58
59
}
59
60
 
60
61
/**
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.
 
66
 *
 
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
66
70
 */
67
71
void OptionsTree::setOption(const QString& name, const QVariant& value)
68
72
{
69
 
        if ( tree_.getValue(name) == value )
 
73
        const QVariant &prev = tree_.getValue(name);
 
74
        if ( prev == value ) {
70
75
                return;
 
76
        }
 
77
        if (!prev.isValid()) {
 
78
                emit optionAboutToBeInserted(name);
 
79
        }
71
80
        tree_.setValue(name, value);
 
81
        if (!prev.isValid()) {
 
82
                emit optionInserted(name);
 
83
        }
72
84
        emit optionChanged(name);
73
85
}
74
86
 
75
 
/**
76
 
 * TODO
 
87
 
 
88
/**
 
89
 * @brief returns true iff the node @a node is an internal node.
 
90
 */
 
91
bool OptionsTree::isInternalNode(const QString &node) const
 
92
{
 
93
        return tree_.isInternalNode(node);
 
94
}
 
95
 
 
96
/**
 
97
 * \brief Sets the comment of the named option.
 
98
 * \param name "Path" to the option
 
99
 * \param comment the comment to store
77
100
 */
78
101
void OptionsTree::setComment(const QString& name, const QString& comment)
79
102
{
81
104
}
82
105
 
83
106
/**
84
 
 * TODO
 
107
 * \brief Returns the comment of the specified option.
 
108
 * \param name "Path" to the option
85
109
 */
86
 
QString OptionsTree::getComment(const QString& name)
 
110
QString OptionsTree::getComment(const QString& name) const
87
111
{
88
112
        return tree_.getComment(name);
89
113
}
90
114
 
 
115
bool OptionsTree::removeOption(const QString &name, bool internal_nodes)
 
116
{
 
117
        emit optionAboutToBeRemoved(name);
 
118
        bool ok = tree_.remove(name, internal_nodes);
 
119
        emit optionRemoved(name);
 
120
        return ok;
 
121
}
 
122
 
91
123
 
92
124
/**
93
125
 * Names of every stored option
94
126
 * \return Names of options
95
127
 */
96
 
QStringList OptionsTree::allOptionNames()
 
128
QStringList OptionsTree::allOptionNames() const
97
129
{
98
130
        return tree_.nodeChildren();
99
131
}
101
133
/**
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
105
138
 */
106
139
QStringList OptionsTree::getChildOptionNames(const QString& parent, bool direct, bool internal_nodes) const
107
140
{
108
141
        return tree_.nodeChildren(parent,direct,internal_nodes);
109
142
}
110
143
 
 
144
bool OptionsTree::isValidName(const QString &name)
 
145
{
 
146
        foreach(QString part, name.split('.')) {
 
147
                if (!VariantTree::isValidNodeName(part)) return false;
 
148
        }
 
149
        return true;
 
150
}
 
151
 
 
152
 
 
153
QString OptionsTree::mapLookup(const QString &basename, const QVariant &key) const
 
154
{
 
155
        QStringList children = getChildOptionNames( basename, true, true);
 
156
        foreach (QString path, children) {
 
157
                if (getOption(path+".key") == key) {
 
158
                        return path;
 
159
                }
 
160
        }
 
161
        qWarning("Accessing missing key '%s' in option map '%s'", qPrintable(key.toString()), qPrintable(basename));
 
162
        return basename + "XXX";
 
163
}
 
164
 
 
165
QVariant OptionsTree::mapGet(const QString &basename, const QVariant &key, const QString &node) const {
 
166
        return getOption(mapLookup(basename, key) + "." + node);
 
167
}
 
168
 
 
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);
 
173
        } else {
 
174
                return def;
 
175
        }
 
176
}
 
177
 
 
178
 
 
179
QString OptionsTree::mapPut(const QString &basename, const QVariant &key)
 
180
{
 
181
        QStringList children = getChildOptionNames( basename, true, true);
 
182
        foreach (QString path, children) {
 
183
                if (getOption(path+".key") == key) {
 
184
                        return path;
 
185
                }
 
186
        }
 
187
        // FIXME performance?
 
188
        
 
189
        // allocate first unused index
 
190
        QString path;
 
191
        int i = 0;
 
192
        do {
 
193
                path = basename+".m"+QString::number(i);
 
194
                ++i;
 
195
        } while (children.contains(path));
 
196
        setOption(path + ".key", key);
 
197
        return path;    
 
198
}
 
199
 
 
200
void OptionsTree::mapPut(const QString &basename, const QVariant &key, const QString &node, const QVariant &value) {
 
201
        setOption(mapPut(basename, key) + "." + node, value);
 
202
}
 
203
 
 
204
QVariantList OptionsTree::mapKeyList(const QString &basename) const
 
205
{
 
206
        QVariantList ret;
 
207
        QStringList children = getChildOptionNames( basename, true, true);
 
208
        foreach (QString path, children) {
 
209
                ret << getOption(path+".key");
 
210
        }
 
211
        return ret;
 
212
}
 
213
 
 
214
 
 
215
 
111
216
/**
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
118
223
 */
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
120
225
{
121
226
        QDomDocument doc(configName);
122
227
 
127
232
        doc.appendChild(base);
128
233
        
129
234
        tree_.toXml(doc, base);
130
 
        QFile file(fileName);
131
 
        if(!file.open(QIODevice::WriteOnly))
132
 
        {
133
 
                //qWarning(qPrintable(tr(QString("Could not open options file for saving, %1").arg(fileName))));
 
235
        AtomicXmlFile f(fileName);
 
236
        if (!f.saveDocument(doc))
134
237
                return false;
135
 
        }
136
 
        QTextStream text;
137
 
        text.setDevice(&file);
138
 
        text.setCodec("UTF-8");
139
 
        text << doc.toString();
140
 
        file.close();
141
238
 
142
239
        return true;
143
240
}
152
249
 */
153
250
bool OptionsTree::loadOptions(const QString& fileName, const QString& configName, const QString& configNS,  const QString& configVersion)
154
251
{
155
 
        // Open the file
156
 
        QFile file(fileName);
157
 
        if(!file.open(QIODevice::ReadOnly)) {
158
 
                return false;
159
 
        }
160
 
        
161
 
        // Load document from file
 
252
        AtomicXmlFile f(fileName);
162
253
        QDomDocument doc;
163
 
        if(!doc.setContent(&file))
 
254
        if (!f.loadDocument(&doc))
164
255
                return false;
165
 
        file.close();
166
256
 
167
257
        return loadOptions(doc.documentElement(), configName, configVersion, configNS);
168
258
}
169
259
 
170
260
/**
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
175
266
 */
176
267
bool OptionsTree::loadOptions(const QDomElement& base, const QString& configName, const QString& configNS, const QString& configVersion)