1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Mobility Components.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QStringList>
48 #include "gconfitem_p.h"
51 #include <gconf/gconf-value.h>
52 #include <gconf/gconf-client.h>
54 struct GConfItemPrivate {
59 static void notify_trampoline(GConfClient*, guint, GConfEntry *, gpointer);
62 #define withClient(c) for(GConfClient *c = (g_type_init(), gconf_client_get_default()); c; g_object_unref(c), c=NULL)
64 static QByteArray convertKey(QString key)
66 if (key.startsWith('/'))
69 qWarning() << "Using dot-separated key names with GConfItem is deprecated.";
70 qWarning() << "Please use" << '/' + key.replace('.', '/') << "instead of" << key;
71 return '/' + key.replace('.', '/').toUtf8();
75 static QString convertKey(const char *key)
77 return QString::fromUtf8(key);
80 static QVariant convertValue(GConfValue *src)
86 case GCONF_VALUE_INVALID:
87 return QVariant(QVariant::Invalid);
88 case GCONF_VALUE_BOOL:
89 return QVariant((bool)gconf_value_get_bool(src));
91 return QVariant(gconf_value_get_int(src));
92 case GCONF_VALUE_FLOAT:
93 return QVariant(gconf_value_get_float(src));
94 case GCONF_VALUE_STRING:
95 return QVariant(QString::fromUtf8(gconf_value_get_string(src)));
96 case GCONF_VALUE_LIST:
97 switch (gconf_value_get_list_type(src)) {
98 case GCONF_VALUE_STRING: {
100 for (GSList *elts = gconf_value_get_list(src); elts; elts = elts->next)
101 result.append(QString::fromUtf8(gconf_value_get_string((GConfValue *)elts->data)));
102 return QVariant(result);
105 QList<QVariant> result;
106 for (GSList *elts = gconf_value_get_list(src); elts; elts = elts->next)
107 result.append(convertValue((GConfValue *)elts->data));
108 return QVariant(result);
111 case GCONF_VALUE_SCHEMA:
118 static GConfValue *convertString(const QString &str)
120 GConfValue *v = gconf_value_new(GCONF_VALUE_STRING);
121 gconf_value_set_string(v, str.toUtf8().data());
125 static GConfValueType primitiveType(const QVariant &elt)
127 switch (elt.type()) {
128 case QVariant::String:
129 return GCONF_VALUE_STRING;
131 return GCONF_VALUE_INT;
132 case QVariant::Double:
133 return GCONF_VALUE_FLOAT;
135 return GCONF_VALUE_BOOL;
137 return GCONF_VALUE_INVALID;
141 static GConfValueType uniformType(const QList<QVariant> &list)
143 GConfValueType result = GCONF_VALUE_INVALID;
145 foreach(const QVariant &elt, list) {
146 GConfValueType elt_type = primitiveType(elt);
148 if (elt_type == GCONF_VALUE_INVALID)
149 return GCONF_VALUE_INVALID;
151 if (result == GCONF_VALUE_INVALID)
153 else if (result != elt_type)
154 return GCONF_VALUE_INVALID;
157 if (result == GCONF_VALUE_INVALID)
158 return GCONF_VALUE_STRING; // empty list.
163 static int convertValue(const QVariant &src, GConfValue **valp)
167 switch (src.type()) {
168 case QVariant::Invalid:
172 v = gconf_value_new(GCONF_VALUE_BOOL);
173 gconf_value_set_bool(v, src.toBool());
176 v = gconf_value_new(GCONF_VALUE_INT);
177 gconf_value_set_int(v, src.toInt());
179 case QVariant::Double:
180 v = gconf_value_new(GCONF_VALUE_FLOAT);
181 gconf_value_set_float(v, src.toDouble());
183 case QVariant::String:
184 v = convertString(src.toString());
186 case QVariant::StringList: {
188 v = gconf_value_new(GCONF_VALUE_LIST);
189 gconf_value_set_list_type(v, GCONF_VALUE_STRING);
190 foreach(const QString &str, src.toStringList())
191 elts = g_slist_prepend(elts, convertString(str));
192 gconf_value_set_list_nocopy(v, g_slist_reverse(elts));
195 case QVariant::List: {
196 GConfValueType elt_type = uniformType(src.toList());
197 if (elt_type == GCONF_VALUE_INVALID)
201 v = gconf_value_new(GCONF_VALUE_LIST);
202 gconf_value_set_list_type(v, elt_type);
203 foreach(const QVariant &elt, src.toList()) {
204 GConfValue *val = NULL;
205 convertValue(elt, &val); // guaranteed to succeed.
206 elts = g_slist_prepend(elts, val);
208 gconf_value_set_list_nocopy(v, g_slist_reverse(elts));
220 void GConfItemPrivate::notify_trampoline(GConfClient*,
225 GConfItem *item = (GConfItem *)data;
226 item->update_value(true);
229 void GConfItem::update_value(bool emit_signal)
234 GError *error = NULL;
235 QByteArray k = convertKey(priv->key);
236 GConfValue *v = gconf_client_get(client, k.data(), &error);
239 qWarning() << error->message;
241 new_value = priv->value;
243 new_value = convertValue(v);
249 if (new_value != priv->value) {
250 priv->value = new_value;
256 QString GConfItem::key() const
261 QVariant GConfItem::value() const
266 QVariant GConfItem::value(const QVariant &def) const
268 if (priv->value.isNull())
274 void GConfItem::set(const QVariant &val)
277 QByteArray k = convertKey(priv->key);
279 if (convertValue(val, &v)) {
280 GError *error = NULL;
283 gconf_client_set(client, k.data(), v, &error);
286 gconf_client_unset(client, k.data(), &error);
290 qWarning() << error->message;
292 } else if (priv->value != val) {
298 qWarning() << "Can't store a" << val.typeName();
302 void GConfItem::unset()
307 QList<QString> GConfItem::listDirs() const
309 QList<QString> children;
312 QByteArray k = convertKey(priv->key);
313 GSList *dirs = gconf_client_all_dirs(client, k.data(), NULL);
314 for (GSList *d = dirs; d; d = d->next) {
315 children.append(convertKey((char *)d->data));
324 QList<QString> GConfItem::listEntries() const
326 QList<QString> children;
329 QByteArray k = convertKey(priv->key);
330 GSList *entries = gconf_client_all_entries(client, k.data(), NULL);
331 for (GSList *e = entries; e; e = e->next) {
332 children.append(convertKey(((GConfEntry *)e->data)->key));
333 gconf_entry_free((GConfEntry *)e->data);
335 g_slist_free(entries);
341 GConfItem::GConfItem(const QString &key, QObject *parent)
344 priv = new GConfItemPrivate;
348 QByteArray k = convertKey(priv->key);
349 gconf_client_add_dir(client, k.data(), GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
350 priv->notify_id = gconf_client_notify_add(client, k.data(),
351 GConfItemPrivate::notify_trampoline, this,
356 GConfItem::~GConfItem()
359 QByteArray k = convertKey(priv->key);
360 gconf_client_notify_remove(client, priv->notify_id);
361 gconf_client_remove_dir(client, k.data(), NULL);