]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-conf.c
Move empathy-conf to libempathy-gtk. libempathy do not depend directly on gconf anymore.
[empathy.git] / libempathy-gtk / empathy-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 <libempathy/empathy-debug.h>
30
31 #include "empathy-conf.h"
32
33 #define DEBUG_DOMAIN "Config"
34
35 #define EMPATHY_CONF_ROOT       "/apps/empathy"
36 #define DESKTOP_INTERFACE_ROOT  "/desktop/gnome/interface"
37
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONF, EmpathyConfPriv))
39
40 typedef struct {
41         GConfClient *gconf_client;
42 } EmpathyConfPriv;
43
44 typedef struct {
45         EmpathyConf           *conf;
46         EmpathyConfNotifyFunc  func;
47         gpointer               user_data;
48 } EmpathyConfNotifyData;
49
50 static void conf_finalize (GObject *object);
51
52 G_DEFINE_TYPE (EmpathyConf, empathy_conf, G_TYPE_OBJECT);
53
54 static EmpathyConf *global_conf = NULL;
55
56 static void
57 empathy_conf_class_init (EmpathyConfClass *class)
58 {
59         GObjectClass *object_class;
60
61         object_class = G_OBJECT_CLASS (class);
62
63         object_class->finalize = conf_finalize;
64
65         g_type_class_add_private (object_class, sizeof (EmpathyConfPriv));
66 }
67
68 static void
69 empathy_conf_init (EmpathyConf *conf)
70 {
71         EmpathyConfPriv *priv;
72
73         priv = GET_PRIV (conf);
74
75         priv->gconf_client = gconf_client_get_default ();
76
77         gconf_client_add_dir (priv->gconf_client,
78                               EMPATHY_CONF_ROOT,
79                               GCONF_CLIENT_PRELOAD_ONELEVEL,
80                               NULL);
81         gconf_client_add_dir (priv->gconf_client,
82                               DESKTOP_INTERFACE_ROOT,
83                               GCONF_CLIENT_PRELOAD_NONE,
84                               NULL);
85 }
86
87 static void
88 conf_finalize (GObject *object)
89 {
90         EmpathyConfPriv *priv;
91
92         priv = GET_PRIV (object);
93
94         gconf_client_remove_dir (priv->gconf_client,
95                                  EMPATHY_CONF_ROOT,
96                                  NULL);
97         gconf_client_remove_dir (priv->gconf_client,
98                                  DESKTOP_INTERFACE_ROOT,
99                                  NULL);
100
101         g_object_unref (priv->gconf_client);
102
103         G_OBJECT_CLASS (empathy_conf_parent_class)->finalize (object);
104 }
105
106 EmpathyConf *
107 empathy_conf_get (void)
108 {
109         if (!global_conf) {
110                 global_conf = g_object_new (EMPATHY_TYPE_CONF, NULL);
111         }
112
113         return global_conf;
114 }
115
116 void
117 empathy_conf_shutdown (void)
118 {
119         if (global_conf) {
120                 g_object_unref (global_conf);
121                 global_conf = NULL;
122         }
123 }
124
125 gboolean
126 empathy_conf_set_int (EmpathyConf  *conf,
127                      const gchar *key,
128                      gint         value)
129 {
130         EmpathyConfPriv *priv;
131
132         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
133
134         empathy_debug (DEBUG_DOMAIN, "Setting int:'%s' to %d", key, value);
135
136         priv = GET_PRIV (conf);
137
138         return gconf_client_set_int (priv->gconf_client,
139                                      key,
140                                      value,
141                                      NULL);
142 }
143
144 gboolean
145 empathy_conf_get_int (EmpathyConf  *conf,
146                      const gchar *key,
147                      gint        *value)
148 {
149         EmpathyConfPriv *priv;
150         GError          *error = NULL;
151
152         *value = 0;
153
154         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
155         g_return_val_if_fail (value != NULL, FALSE);
156
157         priv = GET_PRIV (conf);
158
159         *value = gconf_client_get_int (priv->gconf_client,
160                                        key,
161                                        &error);
162
163         if (error) {
164                 g_error_free (error);
165                 return FALSE;
166         }
167
168         return TRUE;
169 }
170
171 gboolean
172 empathy_conf_set_bool (EmpathyConf  *conf,
173                       const gchar *key,
174                       gboolean     value)
175 {
176         EmpathyConfPriv *priv;
177
178         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
179
180         empathy_debug (DEBUG_DOMAIN, "Setting bool:'%s' to %d ---> %s",
181                       key, value, value ? "true" : "false");
182
183         priv = GET_PRIV (conf);
184
185         return gconf_client_set_bool (priv->gconf_client,
186                                       key,
187                                       value,
188                                       NULL);
189 }
190
191 gboolean
192 empathy_conf_get_bool (EmpathyConf  *conf,
193                       const gchar *key,
194                       gboolean    *value)
195 {
196         EmpathyConfPriv *priv;
197         GError          *error = NULL;
198
199         *value = FALSE;
200
201         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
202         g_return_val_if_fail (value != NULL, FALSE);
203
204         priv = GET_PRIV (conf);
205
206         *value = gconf_client_get_bool (priv->gconf_client,
207                                         key,
208                                         &error);
209
210         if (error) {
211                 g_error_free (error);
212                 return FALSE;
213         }
214
215         return TRUE;
216 }
217
218 gboolean
219 empathy_conf_set_string (EmpathyConf  *conf,
220                         const gchar *key,
221                         const gchar *value)
222 {
223         EmpathyConfPriv *priv;
224
225         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
226
227         empathy_debug (DEBUG_DOMAIN, "Setting string:'%s' to '%s'",
228                       key, value);
229
230         priv = GET_PRIV (conf);
231
232         return gconf_client_set_string (priv->gconf_client,
233                                         key,
234                                         value,
235                                         NULL);
236 }
237
238 gboolean
239 empathy_conf_get_string (EmpathyConf   *conf,
240                         const gchar  *key,
241                         gchar       **value)
242 {
243         EmpathyConfPriv *priv;
244         GError          *error = NULL;
245
246         *value = NULL;
247
248         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
249
250         priv = GET_PRIV (conf);
251
252         *value = gconf_client_get_string (priv->gconf_client,
253                                           key,
254                                           &error);
255
256         if (error) {
257                 g_error_free (error);
258                 return FALSE;
259         }
260
261         return TRUE;
262 }
263
264 gboolean
265 empathy_conf_set_string_list (EmpathyConf  *conf,
266                              const gchar *key,
267                              GSList      *value)
268 {
269         EmpathyConfPriv *priv;
270
271         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
272
273         priv = GET_PRIV (conf);
274
275         return gconf_client_set_list (priv->gconf_client,
276                                       key,
277                                       GCONF_VALUE_STRING,
278                                       value,
279                                       NULL);
280 }
281
282 gboolean
283 empathy_conf_get_string_list (EmpathyConf   *conf,
284                              const gchar  *key,
285                              GSList      **value)
286 {
287         EmpathyConfPriv *priv;
288         GError          *error = NULL;
289
290         *value = NULL;
291
292         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
293
294         priv = GET_PRIV (conf);
295
296         *value = gconf_client_get_list (priv->gconf_client,
297                                         key,
298                                         GCONF_VALUE_STRING,
299                                         &error);
300         if (error) {
301                 g_error_free (error);
302                 return FALSE;
303         }
304
305         return TRUE;
306 }
307
308 static void
309 conf_notify_data_free (EmpathyConfNotifyData *data)
310 {
311         g_object_unref (data->conf);
312         g_slice_free (EmpathyConfNotifyData, data);
313 }
314
315 static void
316 conf_notify_func (GConfClient *client,
317                   guint        id,
318                   GConfEntry  *entry,
319                   gpointer     user_data)
320 {
321         EmpathyConfNotifyData *data;
322
323         data = user_data;
324
325         data->func (data->conf,
326                     gconf_entry_get_key (entry),
327                     data->user_data);
328 }
329
330 guint
331 empathy_conf_notify_add (EmpathyConf           *conf,
332                         const gchar          *key,
333                         EmpathyConfNotifyFunc func,
334                         gpointer              user_data)
335 {
336         EmpathyConfPriv       *priv;
337         guint                  id;
338         EmpathyConfNotifyData *data;
339
340         g_return_val_if_fail (EMPATHY_IS_CONF (conf), 0);
341
342         priv = GET_PRIV (conf);
343
344         data = g_slice_new (EmpathyConfNotifyData);
345         data->func = func;
346         data->user_data = user_data;
347         data->conf = g_object_ref (conf);
348
349         id = gconf_client_notify_add (priv->gconf_client,
350                                       key,
351                                       conf_notify_func,
352                                       data,
353                                       (GFreeFunc) conf_notify_data_free,
354                                       NULL);
355
356         return id;
357 }
358
359 gboolean
360 empathy_conf_notify_remove (EmpathyConf *conf,
361                            guint       id)
362 {
363         EmpathyConfPriv *priv;
364
365         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
366
367         priv = GET_PRIV (conf);
368
369         gconf_client_notify_remove (priv->gconf_client, id);
370
371         return TRUE;
372 }
373