]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-conf.c
de.po: Updated German translation
[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-utils.h>
30 #include "empathy-conf.h"
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include <libempathy/empathy-debug.h>
34
35 #define EMPATHY_CONF_ROOT       "/apps/empathy"
36 #define DESKTOP_INTERFACE_ROOT  "/desktop/gnome/interface"
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyConf)
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 = G_TYPE_INSTANCE_GET_PRIVATE (conf,
71                 EMPATHY_TYPE_CONF, EmpathyConfPriv);
72
73         conf->priv = priv;
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         DEBUG ("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         DEBUG ("Setting bool:'%s' to %d ---> %s", key, value,
180                 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         DEBUG ("Setting string:'%s' to '%s'", key, value);
227
228         priv = GET_PRIV (conf);
229
230         return gconf_client_set_string (priv->gconf_client,
231                                         key,
232                                         value,
233                                         NULL);
234 }
235
236 gboolean
237 empathy_conf_get_string (EmpathyConf   *conf,
238                         const gchar  *key,
239                         gchar       **value)
240 {
241         EmpathyConfPriv *priv;
242         GError          *error = NULL;
243
244         *value = NULL;
245
246         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
247
248         priv = GET_PRIV (conf);
249
250         *value = gconf_client_get_string (priv->gconf_client,
251                                           key,
252                                           &error);
253
254         if (error) {
255                 g_error_free (error);
256                 return FALSE;
257         }
258
259         return TRUE;
260 }
261
262 gboolean
263 empathy_conf_set_string_list (EmpathyConf  *conf,
264                              const gchar *key,
265                              GSList      *value)
266 {
267         EmpathyConfPriv *priv;
268
269         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
270
271         priv = GET_PRIV (conf);
272
273         return gconf_client_set_list (priv->gconf_client,
274                                       key,
275                                       GCONF_VALUE_STRING,
276                                       value,
277                                       NULL);
278 }
279
280 gboolean
281 empathy_conf_get_string_list (EmpathyConf   *conf,
282                              const gchar  *key,
283                              GSList      **value)
284 {
285         EmpathyConfPriv *priv;
286         GError          *error = NULL;
287
288         *value = NULL;
289
290         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
291
292         priv = GET_PRIV (conf);
293
294         *value = gconf_client_get_list (priv->gconf_client,
295                                         key,
296                                         GCONF_VALUE_STRING,
297                                         &error);
298         if (error) {
299                 g_error_free (error);
300                 return FALSE;
301         }
302
303         return TRUE;
304 }
305
306 static void
307 conf_notify_data_free (EmpathyConfNotifyData *data)
308 {
309         g_object_unref (data->conf);
310         g_slice_free (EmpathyConfNotifyData, data);
311 }
312
313 static void
314 conf_notify_func (GConfClient *client,
315                   guint        id,
316                   GConfEntry  *entry,
317                   gpointer     user_data)
318 {
319         EmpathyConfNotifyData *data;
320
321         data = user_data;
322
323         data->func (data->conf,
324                     gconf_entry_get_key (entry),
325                     data->user_data);
326 }
327
328 guint
329 empathy_conf_notify_add (EmpathyConf           *conf,
330                         const gchar          *key,
331                         EmpathyConfNotifyFunc func,
332                         gpointer              user_data)
333 {
334         EmpathyConfPriv       *priv;
335         guint                  id;
336         EmpathyConfNotifyData *data;
337
338         g_return_val_if_fail (EMPATHY_IS_CONF (conf), 0);
339
340         priv = GET_PRIV (conf);
341
342         data = g_slice_new (EmpathyConfNotifyData);
343         data->func = func;
344         data->user_data = user_data;
345         data->conf = g_object_ref (conf);
346
347         id = gconf_client_notify_add (priv->gconf_client,
348                                       key,
349                                       conf_notify_func,
350                                       data,
351                                       (GFreeFunc) conf_notify_data_free,
352                                       NULL);
353
354         return id;
355 }
356
357 gboolean
358 empathy_conf_notify_remove (EmpathyConf *conf,
359                            guint       id)
360 {
361         EmpathyConfPriv *priv;
362
363         g_return_val_if_fail (EMPATHY_IS_CONF (conf), FALSE);
364
365         priv = GET_PRIV (conf);
366
367         gconf_client_notify_remove (priv->gconf_client, id);
368
369         return TRUE;
370 }
371