Separate in library the application
[googlelatitude] / liblocationmaemo5 / gconfitem.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Mobility Components.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QString>
43 #include <QStringList>
44 #include <QByteArray>
45 #include <QVariant>
46 #include <QtDebug>
47
48 #include "gconfitem_p.h"
49
50 #include <glib.h>
51 #include <gconf/gconf-value.h>
52 #include <gconf/gconf-client.h>
53
54 struct GConfItemPrivate {
55     QString key;
56     QVariant value;
57     guint notify_id;
58
59     static void notify_trampoline(GConfClient*, guint, GConfEntry *, gpointer);
60 };
61
62 #define withClient(c) for(GConfClient *c = (g_type_init(), gconf_client_get_default()); c; g_object_unref(c), c=NULL)
63
64 static QByteArray convertKey(QString key)
65 {
66     if (key.startsWith('/'))
67         return key.toUtf8();
68     else {
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();
72     }
73 }
74
75 static QString convertKey(const char *key)
76 {
77     return QString::fromUtf8(key);
78 }
79
80 static QVariant convertValue(GConfValue *src)
81 {
82     if (!src) {
83         return QVariant();
84     } else {
85         switch (src->type) {
86             case GCONF_VALUE_INVALID:
87                 return QVariant(QVariant::Invalid);
88             case GCONF_VALUE_BOOL:
89                 return QVariant((bool)gconf_value_get_bool(src));
90             case GCONF_VALUE_INT:
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: {
99                         QStringList result;
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);
103                     }
104                     default: {
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);
109                     }
110                 }
111             case GCONF_VALUE_SCHEMA:
112             default:
113                 return QVariant();
114         }
115     }
116 }
117
118 static GConfValue *convertString(const QString &str)
119 {
120     GConfValue *v = gconf_value_new(GCONF_VALUE_STRING);
121     gconf_value_set_string(v, str.toUtf8().data());
122     return v;
123 }
124
125 static GConfValueType primitiveType(const QVariant &elt)
126 {
127     switch (elt.type()) {
128         case QVariant::String:
129             return GCONF_VALUE_STRING;
130         case QVariant::Int:
131             return GCONF_VALUE_INT;
132         case QVariant::Double:
133             return GCONF_VALUE_FLOAT;
134         case QVariant::Bool:
135             return GCONF_VALUE_BOOL;
136         default:
137             return GCONF_VALUE_INVALID;
138     }
139 }
140
141 static GConfValueType uniformType(const QList<QVariant> &list)
142 {
143     GConfValueType result = GCONF_VALUE_INVALID;
144
145     foreach(const QVariant &elt, list) {
146         GConfValueType elt_type = primitiveType(elt);
147
148         if (elt_type == GCONF_VALUE_INVALID)
149             return GCONF_VALUE_INVALID;
150
151         if (result == GCONF_VALUE_INVALID)
152             result = elt_type;
153         else if (result != elt_type)
154             return GCONF_VALUE_INVALID;
155     }
156
157     if (result == GCONF_VALUE_INVALID)
158         return GCONF_VALUE_STRING;  // empty list.
159     else
160         return result;
161 }
162
163 static int convertValue(const QVariant &src, GConfValue **valp)
164 {
165     GConfValue *v;
166
167     switch (src.type()) {
168         case QVariant::Invalid:
169             v = NULL;
170             break;
171         case QVariant::Bool:
172             v = gconf_value_new(GCONF_VALUE_BOOL);
173             gconf_value_set_bool(v, src.toBool());
174             break;
175         case QVariant::Int:
176             v = gconf_value_new(GCONF_VALUE_INT);
177             gconf_value_set_int(v, src.toInt());
178             break;
179         case QVariant::Double:
180             v = gconf_value_new(GCONF_VALUE_FLOAT);
181             gconf_value_set_float(v, src.toDouble());
182             break;
183         case QVariant::String:
184             v = convertString(src.toString());
185             break;
186         case QVariant::StringList: {
187             GSList *elts = NULL;
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));
193             break;
194         }
195         case QVariant::List: {
196             GConfValueType elt_type = uniformType(src.toList());
197             if (elt_type == GCONF_VALUE_INVALID)
198                 v = NULL;
199             else {
200                 GSList *elts = NULL;
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);
207                 }
208                 gconf_value_set_list_nocopy(v, g_slist_reverse(elts));
209             }
210             break;
211         }
212         default:
213             return 0;
214     }
215
216     *valp = v;
217     return 1;
218 }
219
220 void GConfItemPrivate::notify_trampoline(GConfClient*,
221         guint,
222         GConfEntry *,
223         gpointer data)
224 {
225     GConfItem *item = (GConfItem *)data;
226     item->update_value(true);
227 }
228
229 void GConfItem::update_value(bool emit_signal)
230 {
231     QVariant new_value;
232
233     withClient(client) {
234         GError *error = NULL;
235         QByteArray k = convertKey(priv->key);
236         GConfValue *v = gconf_client_get(client, k.data(), &error);
237
238         if (error) {
239             qWarning() << error->message;
240             g_error_free(error);
241             new_value = priv->value;
242         } else {
243             new_value = convertValue(v);
244             if (v)
245                 gconf_value_free(v);
246         }
247     }
248
249     if (new_value != priv->value) {
250         priv->value = new_value;
251         if (emit_signal)
252             emit valueChanged();
253     }
254 }
255
256 QString GConfItem::key() const
257 {
258     return priv->key;
259 }
260
261 QVariant GConfItem::value() const
262 {
263     return priv->value;
264 }
265
266 QVariant GConfItem::value(const QVariant &def) const
267 {
268     if (priv->value.isNull())
269         return def;
270     else
271         return priv->value;
272 }
273
274 void GConfItem::set(const QVariant &val)
275 {
276     withClient(client) {
277         QByteArray k = convertKey(priv->key);
278         GConfValue *v;
279         if (convertValue(val, &v)) {
280             GError *error = NULL;
281
282             if (v) {
283                 gconf_client_set(client, k.data(), v, &error);
284                 gconf_value_free(v);
285             } else {
286                 gconf_client_unset(client, k.data(), &error);
287             }
288
289             if (error) {
290                 qWarning() << error->message;
291                 g_error_free(error);
292             } else if (priv->value != val) {
293                 priv->value = val;
294                 emit valueChanged();
295             }
296
297         } else
298             qWarning() << "Can't store a" << val.typeName();
299     }
300 }
301
302 void GConfItem::unset()
303 {
304     set(QVariant());
305 }
306
307 QList<QString> GConfItem::listDirs() const
308 {
309     QList<QString> children;
310
311     withClient(client) {
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));
316             g_free(d->data);
317         }
318         g_slist_free(dirs);
319     }
320
321     return children;
322 }
323
324 QList<QString> GConfItem::listEntries() const
325 {
326     QList<QString> children;
327
328     withClient(client) {
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);
334         }
335         g_slist_free(entries);
336     }
337
338     return children;
339 }
340
341 GConfItem::GConfItem(const QString &key, QObject *parent)
342         : QObject(parent)
343 {
344     priv = new GConfItemPrivate;
345     priv->key = key;
346     withClient(client) {
347         update_value(false);
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,
352                           NULL, NULL);
353     }
354 }
355
356 GConfItem::~GConfItem()
357 {
358     withClient(client) {
359         QByteArray k = convertKey(priv->key);
360         gconf_client_notify_remove(client, priv->notify_id);
361         gconf_client_remove_dir(client, k.data(), NULL);
362     }
363     delete priv;
364 }