[darcs-to-svn @ initial import]
[empathy.git] / libempathy / gossip-conf.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Richard Hult <richard@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include <gconf/gconf-client.h>
28
29 #include "gossip-conf.h"
30 #include "gossip-debug.h"
31
32 #define DEBUG_DOMAIN "Config"
33
34 #define GOSSIP_CONF_ROOT       "/apps/empathy"
35 #define DESKTOP_INTERFACE_ROOT "/desktop/gnome/interface"
36
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONF, GossipConfPriv))
38
39 typedef struct {
40         GConfClient *gconf_client;
41 } GossipConfPriv;
42
43 typedef struct {
44         GossipConf           *conf;
45         GossipConfNotifyFunc  func;
46         gpointer               user_data;
47 } GossipConfNotifyData;
48
49 static void conf_finalize (GObject *object);
50
51 G_DEFINE_TYPE (GossipConf, gossip_conf, G_TYPE_OBJECT);
52
53 static GossipConf *global_conf = NULL;
54
55 static void
56 gossip_conf_class_init (GossipConfClass *class)
57 {
58         GObjectClass *object_class;
59
60         object_class = G_OBJECT_CLASS (class);
61
62         object_class->finalize = conf_finalize;
63
64         g_type_class_add_private (object_class, sizeof (GossipConfPriv));
65 }
66
67 static void
68 gossip_conf_init (GossipConf *conf)
69 {
70         GossipConfPriv *priv;
71
72         priv = GET_PRIV (conf);
73
74         priv->gconf_client = gconf_client_get_default ();
75
76         gconf_client_add_dir (priv->gconf_client,
77                               GOSSIP_CONF_ROOT,
78                               GCONF_CLIENT_PRELOAD_ONELEVEL,
79                               NULL);
80         gconf_client_add_dir (priv->gconf_client,
81                               DESKTOP_INTERFACE_ROOT,
82                               GCONF_CLIENT_PRELOAD_NONE,
83                               NULL);
84 }
85
86 static void
87 conf_finalize (GObject *object)
88 {
89         GossipConfPriv *priv;
90
91         priv = GET_PRIV (object);
92
93         gconf_client_remove_dir (priv->gconf_client,
94                                  GOSSIP_CONF_ROOT,
95                                  NULL);
96         gconf_client_remove_dir (priv->gconf_client,
97                                  DESKTOP_INTERFACE_ROOT,
98                                  NULL);
99
100         g_object_unref (priv->gconf_client);
101
102         G_OBJECT_CLASS (gossip_conf_parent_class)->finalize (object);
103 }
104
105 GossipConf *
106 gossip_conf_get (void)
107 {
108         if (!global_conf) {
109                 global_conf = g_object_new (GOSSIP_TYPE_CONF, NULL);
110         }
111
112         return global_conf;
113 }
114
115 void
116 gossip_conf_shutdown (void)
117 {
118         if (global_conf) {
119                 g_object_unref (global_conf);
120                 global_conf = NULL;
121         }
122 }
123
124 gboolean
125 gossip_conf_set_int (GossipConf  *conf,
126                      const gchar *key,
127                      gint         value)
128 {
129         GossipConfPriv *priv;
130
131         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
132
133         gossip_debug (DEBUG_DOMAIN, "Setting int:'%s' to %d", key, value);
134
135         priv = GET_PRIV (conf);
136
137         return gconf_client_set_int (priv->gconf_client,
138                                      key,
139                                      value,
140                                      NULL);
141 }
142
143 gboolean
144 gossip_conf_get_int (GossipConf  *conf,
145                      const gchar *key,
146                      gint        *value)
147 {
148         GossipConfPriv *priv;
149         GError          *error = NULL;
150
151         *value = 0;
152
153         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
154         g_return_val_if_fail (value != NULL, FALSE);
155
156         priv = GET_PRIV (conf);
157
158         *value = gconf_client_get_int (priv->gconf_client,
159                                        key,
160                                        &error);
161
162         gossip_debug (DEBUG_DOMAIN, "Getting int:'%s' (=%d), error:'%s'",
163                       key, *value, error ? error->message : "None");
164
165         if (error) {
166                 g_error_free (error);
167                 return FALSE;
168         }
169
170         return TRUE;
171 }
172
173 gboolean
174 gossip_conf_set_bool (GossipConf  *conf,
175                       const gchar *key,
176                       gboolean     value)
177 {
178         GossipConfPriv *priv;
179
180         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
181
182         gossip_debug (DEBUG_DOMAIN, "Setting bool:'%s' to %d ---> %s",
183                       key, value, value ? "true" : "false");
184
185         priv = GET_PRIV (conf);
186
187         return gconf_client_set_bool (priv->gconf_client,
188                                       key,
189                                       value,
190                                       NULL);
191 }
192
193 gboolean
194 gossip_conf_get_bool (GossipConf  *conf,
195                       const gchar *key,
196                       gboolean    *value)
197 {
198         GossipConfPriv *priv;
199         GError          *error = NULL;
200
201         *value = FALSE;
202
203         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
204         g_return_val_if_fail (value != NULL, FALSE);
205
206         priv = GET_PRIV (conf);
207
208         *value = gconf_client_get_bool (priv->gconf_client,
209                                         key,
210                                         &error);
211
212         gossip_debug (DEBUG_DOMAIN, "Getting bool:'%s' (=%d ---> %s), error:'%s'",
213                       key, *value, *value ? "true" : "false",
214                       error ? error->message : "None");
215
216         if (error) {
217                 g_error_free (error);
218                 return FALSE;
219         }
220
221         return TRUE;
222 }
223
224 gboolean
225 gossip_conf_set_string (GossipConf  *conf,
226                         const gchar *key,
227                         const gchar *value)
228 {
229         GossipConfPriv *priv;
230
231         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
232
233         gossip_debug (DEBUG_DOMAIN, "Setting string:'%s' to '%s'",
234                       key, value);
235
236         priv = GET_PRIV (conf);
237
238         return gconf_client_set_string (priv->gconf_client,
239                                         key,
240                                         value,
241                                         NULL);
242 }
243
244 gboolean
245 gossip_conf_get_string (GossipConf   *conf,
246                         const gchar  *key,
247                         gchar       **value)
248 {
249         GossipConfPriv *priv;
250         GError          *error = NULL;
251
252         *value = NULL;
253
254         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
255
256         priv = GET_PRIV (conf);
257
258         *value = gconf_client_get_string (priv->gconf_client,
259                                           key,
260                                           &error);
261
262         gossip_debug (DEBUG_DOMAIN, "Getting string:'%s' (='%s'), error:'%s'",
263                       key, *value, error ? error->message : "None");
264
265         if (error) {
266                 g_error_free (error);
267                 return FALSE;
268         }
269
270         return TRUE;
271 }
272
273 gboolean
274 gossip_conf_set_string_list (GossipConf  *conf,
275                              const gchar *key,
276                              GSList      *value)
277 {
278         GossipConfPriv *priv;
279
280         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
281
282         priv = GET_PRIV (conf);
283
284         return gconf_client_set_list (priv->gconf_client,
285                                       key,
286                                       GCONF_VALUE_STRING,
287                                       value,
288                                       NULL);
289 }
290
291 gboolean
292 gossip_conf_get_string_list (GossipConf   *conf,
293                              const gchar  *key,
294                              GSList      **value)
295 {
296         GossipConfPriv *priv;
297         GError          *error = NULL;
298
299         *value = NULL;
300
301         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
302
303         priv = GET_PRIV (conf);
304
305         *value = gconf_client_get_list (priv->gconf_client,
306                                         key,
307                                         GCONF_VALUE_STRING,
308                                         &error);
309         if (error) {
310                 g_error_free (error);
311                 return FALSE;
312         }
313
314         return TRUE;
315 }
316
317 static void
318 conf_notify_data_free (GossipConfNotifyData *data)
319 {
320         g_object_unref (data->conf);
321         g_slice_free (GossipConfNotifyData, data);
322 }
323
324 static void
325 conf_notify_func (GConfClient *client,
326                   guint        id,
327                   GConfEntry  *entry,
328                   gpointer     user_data)
329 {
330         GossipConfNotifyData *data;
331
332         data = user_data;
333
334         data->func (data->conf,
335                     gconf_entry_get_key (entry),
336                     data->user_data);
337 }
338
339 guint
340 gossip_conf_notify_add (GossipConf           *conf,
341                         const gchar          *key,
342                         GossipConfNotifyFunc func,
343                         gpointer              user_data)
344 {
345         GossipConfPriv       *priv;
346         guint                  id;
347         GossipConfNotifyData *data;
348
349         g_return_val_if_fail (GOSSIP_IS_CONF (conf), 0);
350
351         priv = GET_PRIV (conf);
352
353         data = g_slice_new (GossipConfNotifyData);
354         data->func = func;
355         data->user_data = user_data;
356         data->conf = g_object_ref (conf);
357
358         id = gconf_client_notify_add (priv->gconf_client,
359                                       key,
360                                       conf_notify_func,
361                                       data,
362                                       (GFreeFunc) conf_notify_data_free,
363                                       NULL);
364
365         return id;
366 }
367
368 gboolean
369 gossip_conf_notify_remove (GossipConf *conf,
370                            guint       id)
371 {
372         GossipConfPriv *priv;
373
374         g_return_val_if_fail (GOSSIP_IS_CONF (conf), FALSE);
375
376         priv = GET_PRIV (conf);
377
378         gconf_client_notify_remove (priv->gconf_client, id);
379
380         return TRUE;
381 }
382