Debian Codegraph @

src/publishsubscribe/gconfitem.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #if !defined(QT_NO_GCONFLAYER)
41
42 #include "gconfitem_p.h"
43
44 #include <QtCore/qdebug.h>
45 #include <QtCore/qstringlist.h>
46
47 #include <glib.h>
48 #include <gconf/gconf-value.h>
49 #include <gconf/gconf-client.h>
50
51 QT_BEGIN_NAMESPACE
52
53 struct GConfItemPrivate
54 {
55 QString key;
56 QVariant value;
57 bool monitor;
58 guint notify_id;
59
60 static void notify_trampoline(GConfClient *, guint, GConfEntry *, gpointer);
61 };
62
63 #define withClient(c) for (GConfClient *c = (g_type_init(), gconf_client_get_default()); c; g_object_unref(c), c = NULL)
64
65 static QByteArray convertKey(QString key)
66 {
67 if (key.startsWith(QString::fromLatin1("/"))) {
68 return key.toUtf8();
69 } else {
70 qWarning() << "Using dot-separated key names with GConfItem is deprecated.";
71 qWarning() << "Please use" << QString::fromLatin1("/") + key.replace(QString::fromLatin1("."), QString::fromLatin1("/")) << "instead of" << key;
72 return QString(QString::fromLatin1("/") + key.replace(QString::fromLatin1("."), QString::fromLatin1("/"))).toUtf8();
73 }
74 }
75
76 static QString convertKey(const char *key)
77 {
78 return QString::fromUtf8(key);
79 }
80
81 static QVariant convertValue(GConfValue *src)
82 {
83 if (!src) {
84 return QVariant();
85 } else {
86 switch (src->type) {
87 case GCONF_VALUE_INVALID:
88 return QVariant(QVariant::Invalid);
89 case GCONF_VALUE_BOOL:
90 return QVariant((bool)gconf_value_get_bool(src));
91 case GCONF_VALUE_INT:
92 return QVariant(gconf_value_get_int(src));
93 case GCONF_VALUE_FLOAT:
94 return QVariant(gconf_value_get_float(src));
95 case GCONF_VALUE_STRING:
96 return QVariant(QString::fromUtf8(gconf_value_get_string(src)));
97 case GCONF_VALUE_LIST:
98 switch (gconf_value_get_list_type(src)) {
99 case GCONF_VALUE_STRING: {
100 QStringList result;
101 for (GSList *elts = gconf_value_get_list(src); elts; elts = elts->next)
102 result.append(QString::fromUtf8(gconf_value_get_string((GConfValue *)elts->data)));
103 return QVariant(result);
104 }
105 default: {
106 QList<QVariant> result;
107 for (GSList *elts = gconf_value_get_list(src); elts; elts = elts->next)
108 result.append(convertValue((GConfValue *)elts->data));
109 return QVariant(result);
110 }
111 }
112 case GCONF_VALUE_SCHEMA:
113 default:
114 return QVariant();
115 }
116 }
117 }
118
119 static GConfValue *convertString(const QString &str)
120 {
121 GConfValue *v = gconf_value_new (GCONF_VALUE_STRING);
122 gconf_value_set_string (v, str.toUtf8().data());
123 return v;
124 }
125
126 static GConfValueType primitiveType (const QVariant &elt)
127 {
128 switch(elt.type()) {
129 case QVariant::String:
130 return GCONF_VALUE_STRING;
131 case QVariant::Int:
132 return GCONF_VALUE_INT;
133 case QVariant::Double:
134 return GCONF_VALUE_FLOAT;
135 case QVariant::Bool:
136 return GCONF_VALUE_BOOL;
137 default:
138 return GCONF_VALUE_INVALID;
139 }
140 }
141
142 static GConfValueType uniformType(const QList<QVariant> &list)
143 {
144 GConfValueType result = GCONF_VALUE_INVALID;
145
146 foreach (const QVariant &elt, list) {
147 GConfValueType elt_type = primitiveType (elt);
148
149 if (elt_type == GCONF_VALUE_INVALID)
150 return GCONF_VALUE_INVALID;
151
152 if (result == GCONF_VALUE_INVALID)
153 result = elt_type;
154 else if (result != elt_type)
155 return GCONF_VALUE_INVALID;
156 }
157
158 if (result == GCONF_VALUE_INVALID)
159 return GCONF_VALUE_STRING; // empty list.
160 else
161 return result;
162 }
163
164 static int convertValue(const QVariant &src, GConfValue **valp)
165 {
166 GConfValue *v;
167
168 switch(src.type()) {
169 case QVariant::Invalid:
170 v = NULL;
171 break;
172 case QVariant::Bool:
173 v = gconf_value_new (GCONF_VALUE_BOOL);
174 gconf_value_set_bool (v, src.toBool());
175 break;
176 case QVariant::Int:
177 v = gconf_value_new (GCONF_VALUE_INT);
178 gconf_value_set_int (v, src.toInt());
179 break;
180 case QVariant::Double:
181 v = gconf_value_new (GCONF_VALUE_FLOAT);
182 gconf_value_set_float (v, src.toDouble());
183 break;
184 case QVariant::String:
185 v = convertString(src.toString());
186 break;
187 case QVariant::StringList: {
188 GSList *elts = NULL;
189 v = gconf_value_new(GCONF_VALUE_LIST);
190 gconf_value_set_list_type(v, GCONF_VALUE_STRING);
191 foreach (const QString &str, src.toStringList())
192 elts = g_slist_prepend(elts, convertString(str));
193 gconf_value_set_list_nocopy(v, g_slist_reverse(elts));
194 break;
195 }
196 case QVariant::List: {
197 GConfValueType elt_type = uniformType(src.toList());
198 if (elt_type == GCONF_VALUE_INVALID) {
199 v = NULL;
200 } else {
201 GSList *elts = NULL;
202 v = gconf_value_new(GCONF_VALUE_LIST);
203 gconf_value_set_list_type(v, elt_type);
204 foreach (const QVariant &elt, src.toList()) {
205 GConfValue *val = NULL;
206 convertValue(elt, &val); // guaranteed to succeed.
207 elts = g_slist_prepend(elts, val);
208 }
209 gconf_value_set_list_nocopy(v, g_slist_reverse(elts));
210 }
211 break;
212 }
213 default:
214 return 0;
215 }
216
217 *valp = v;
218 return 1;
219 }
220
221 void GConfItemPrivate::notify_trampoline(GConfClient *, guint, GConfEntry *entry, gpointer data)
222 {
223 GConfItem *item = (GConfItem *)data;
224 item->update_value(true, QString::fromUtf8(entry->key), convertValue(entry->value));
225 }
226
227 /*!
228 \class GConfItem
229 \brief GConfItem is a simple C++ wrapper for GConf.
230 \internal
231
232 Creating a GConfItem instance gives you access to a single GConf
233 key. You can get and set its value, and connect to its
234 valueChanged() signal to be notified about changes.
235
236 The value of a GConf key is returned to you as a QVariant, and you
237 pass in a QVariant when setting the value. GConfItem converts
238 between a QVariant and GConf values as needed, and according to the
239 following rules:
240
241 - A QVariant of type QVariant::Invalid denotes an unset GConf key.
242
243 - QVariant::Int, QVariant::Double, QVariant::Bool are converted to
244 and from the obvious equivalents.
245
246 - QVariant::String is converted to/from a GConf string and always
247 uses the UTF-8 encoding. No other encoding is supported.
248
249 - QVariant::StringList is converted to a list of UTF-8 strings.
250
251 - QVariant::List (which denotes a QList<QVariant>) is converted
252 to/from a GConf list. All elements of such a list must have the
253 same type, and that type must be one of QVariant::Int,
254 QVariant::Double, QVariant::Bool, or QVariant::String. (A list of
255 strings is returned as a QVariant::StringList, however, when you
256 get it back.)
257
258 - Any other QVariant or GConf value is essentially ignored.
259
260 \warning GConfItem is as thread-safe as GConf.
261 */
262
263 /*!
264 \internal
265 \fn void GConfItem::subtreeChanged(const QString &key, const QVariant &value)
266
267 This signal is emitted whenever some \a key in subtree of this item has changed to \a value.
268 */
269
270 /*!
271 \internal
272 \fn void GConfItem::valueChanged()
273
274 This signal is emitted whenever the value of this item has changed.
275 */
276
277 /*!
278 \internal
279 Constructs a GConfItem object with the \a parent to access the GConf \a key.
280
281 Note that key names should follow the normal GConf conventions like "/myapp/settings/first".
282 */
283 GConfItem::GConfItem(const QString &key, bool monitor, QObject *parent)
284 : QObject (parent)
285 {
286 priv = new GConfItemPrivate;
287 priv->key = key;
288 priv->monitor = monitor;
289 withClient(client) {
290 update_value(false, QString(), QVariant());
291 if (priv->monitor) {
292 QByteArray k = convertKey(priv->key);
293 gconf_client_add_dir (client, k.data(), GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
294 priv->notify_id = gconf_client_notify_add (client, k.data(), GConfItemPrivate::notify_trampoline, this, NULL, NULL);
295 }
296 }
297 }
298
299 /*!
300 \internal
301 Destroys the object.
302 */
303 GConfItem::~GConfItem()
304 {
305 withClient(client) {
306 QByteArray k = convertKey(priv->key);
307 if (priv->monitor) {
308 gconf_client_notify_remove (client, priv->notify_id);
309 gconf_client_remove_dir (client, k.data(), NULL);
310 }
311 }
312 delete priv;
313 }
314
315 /*!
316 \internal
317 Returns the key of this item, as given to the constructor.
318 */
319 QString GConfItem::key() const
320 {
321 return priv->key;
322 }
323
324 /*!
325 \internal
326 Returns the current value of this item, as a QVariant.
327 */
328 QVariant GConfItem::value() const
329 {
330 return priv->value;
331 }
332
333 /*!
334 \internal
335 Returns the current value of this item, or \a def if there is no value for it.
336 */
337 QVariant GConfItem::value(const QVariant &def) const
338 {
339 if (priv->value.isNull())
340 return def;
341 else
342 return priv->value;
343 }
344
345 /*!
346 \internal
347 Set the value of this item to \a val. If \a val can not be represented in GConf, or GConf
348 refuses to accept it for other reasons, the current value is not changed and nothing happens.
349
350 When the new value is different from the old value, the changedValue() signal is emitted on
351 this GConfItem as part of calling set(), but other GConfItem:s for the same key do only receive
352 a notification once the main loop runs.
353 */
354 void GConfItem::set(const QVariant &val)
355 {
356 withClient(client) {
357 QByteArray k = convertKey(priv->key);
358 GConfValue *v;
359 if (convertValue(val, &v)) {
360 GError *error = NULL;
361
362 if (v) {
363 gconf_client_set(client, k.data(), v, &error);
364 gconf_value_free(v);
365 } else {
366 gconf_client_unset(client, k.data(), &error);
367 }
368
369 if (error) {
370 qWarning() << error->message;
371 g_error_free(error);
372 } else if (priv->value != val) {
373 priv->value = val;
374 emit valueChanged();
375 }
376
377 } else {
378 qWarning() << "Can't store a" << val.typeName();
379 }
380 }
381 }
382
383 /*!
384 \internal
385 Unset this item. This is equivalent to set an invalid or null value.
386 */
387 void GConfItem::unset()
388 {
389 set(QVariant());
390 }
391
392 /*!
393 \internal
394 Unset this item and all its sub items recursively.
395 */
396 void GConfItem::recursiveUnset()
397 {
398 withClient(client) {
399 QByteArray k = convertKey(priv->key);
400 GError *error = NULL;
401
402 GConfUnsetFlags flags;
403 gconf_client_recursive_unset(client, k.data(), flags, &error);
404
405 if (error) {
406 qWarning() << error->message;
407 g_error_free(error);
408 } else {
409 priv->value = QVariant();
410 emit valueChanged();
411 }
412 }
413 }
414
415 /*!
416 \internal
417 Return a list of the directories below this item. The returned strings are absolute key names
418 like "/myapp/settings".
419
420 A directory is a key that has children. The same key might also have a value, but that is
421 confusing and best avoided.
422 */
423 QList<QString> GConfItem::listDirs() const
424 {
425 QList<QString> children;
426
427 withClient(client) {
428 QByteArray k = convertKey(priv->key);
429 GSList *dirs = gconf_client_all_dirs(client, k.data(), NULL);
430 for (GSList *d = dirs; d; d = d->next) {
431 children.append(convertKey((char *)d->data));
432 g_free (d->data);
433 }
434 g_slist_free (dirs);
435 }
436
437 return children;
438 }
439
440 /*!
441 \internal
442 Return a list of entries below this item. The returned strings are absolute key names like
443 "/myapp/settings/first".
444
445 A entry is a key that has a value. The same key might also have children, but that is confusing
446 and is best avoided.
447 */
448 QList<QString> GConfItem::listEntries() const
449 {
450 QList<QString> children;
451
452 withClient(client) {
453 QByteArray k = convertKey(priv->key);
454 GSList *entries = gconf_client_all_entries(client, k.data(), NULL);
455 for (GSList *e = entries; e; e = e->next) {
456 children.append(convertKey(((GConfEntry *)e->data)->key));
457 gconf_entry_free ((GConfEntry *)e->data);
458 }
459 g_slist_free (entries);
460 }
461
462 return children;
463 }
464
465 /*!
466 \internal
467 */
468 void GConfItem::update_value(bool emit_signal, const QString &key, const QVariant &value)
469 {
470 QVariant new_value;
471
472 if (emit_signal)
473 emit subtreeChanged(key, value);
474
475 withClient(client) {
476 GError *error = NULL;
477 QByteArray k = convertKey(priv->key);
478 GConfValue *v = gconf_client_get(client, k.data(), &error);
479
480 if (error) {
481 qWarning() << error->message;
482 g_error_free (error);
483 new_value = priv->value;
484 } else {
485 new_value = convertValue(v);
486 if (v)
487 gconf_value_free(v);
488 }
489 }
490
491 if (new_value != priv->value) {
492 priv->value = new_value;
493 if (emit_signal)
494 emit valueChanged();
495 }
496 }
497
498 QT_END_NAMESPACE
499
500 #endif // QT_NO_GCONFLAYER