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