1 | #include <config.h> |
---|
2 | #include <string.h> |
---|
3 | |
---|
4 | #include <glib.h> |
---|
5 | #include <gconf/gconf-client.h> |
---|
6 | #include <libgnomeui/gnome-client.h> |
---|
7 | |
---|
8 | #include "panel-gconf.h" |
---|
9 | |
---|
10 | #undef PANEL_GCONF_DEBUG |
---|
11 | |
---|
12 | static GConfClient *panel_gconf_client = NULL; |
---|
13 | static char *panel_gconf_profile = NULL; |
---|
14 | |
---|
15 | G_CONST_RETURN char * |
---|
16 | panel_gconf_get_profile (void) |
---|
17 | { |
---|
18 | return panel_gconf_profile; |
---|
19 | } |
---|
20 | |
---|
21 | /* |
---|
22 | * panel_gconf_sprintf: |
---|
23 | * @format: the format string. See sprintf() documentation. |
---|
24 | * @...: the arguments to be inserted. |
---|
25 | * |
---|
26 | * This is a version of sprintf using a static buffer which is |
---|
27 | * intended for use in generating the full gconf key for all panel |
---|
28 | * config keys. |
---|
29 | * Note, you should not free the return value from this function and |
---|
30 | * you should realize that the return value will get overwritten or |
---|
31 | * freed by a subsequent call to this function. |
---|
32 | * |
---|
33 | * Return Value: a pointer to the static string buffer. |
---|
34 | */ |
---|
35 | G_CONST_RETURN char * |
---|
36 | panel_gconf_sprintf (const char *format, |
---|
37 | ...) |
---|
38 | { |
---|
39 | static char *buffer = NULL; |
---|
40 | static int buflen = 128; |
---|
41 | va_list args; |
---|
42 | int len; |
---|
43 | |
---|
44 | if (!buffer) |
---|
45 | buffer = g_new (char, buflen); |
---|
46 | |
---|
47 | va_start (args, format); |
---|
48 | len = g_vsnprintf (buffer, buflen, format, args); |
---|
49 | |
---|
50 | if (len >= buflen) { |
---|
51 | int i; |
---|
52 | |
---|
53 | /* Round up length to the nearest power of 2 */ |
---|
54 | for (i = 0; len != 1; i++, len >>= 1); |
---|
55 | |
---|
56 | buflen = len << (i + 1); |
---|
57 | g_assert (buflen > 0); |
---|
58 | |
---|
59 | g_free (buffer); |
---|
60 | buffer = g_new (char, buflen); |
---|
61 | |
---|
62 | va_start (args, format); |
---|
63 | len = g_vsnprintf (buffer, buflen, format, args); |
---|
64 | |
---|
65 | g_assert (len < buflen); |
---|
66 | } |
---|
67 | |
---|
68 | va_end (args); |
---|
69 | |
---|
70 | return buffer; |
---|
71 | } |
---|
72 | |
---|
73 | G_CONST_RETURN char * |
---|
74 | panel_gconf_global_key (const char *key) |
---|
75 | { |
---|
76 | return panel_gconf_sprintf ("/apps/panel/global/%s", key); |
---|
77 | } |
---|
78 | |
---|
79 | G_CONST_RETURN char * |
---|
80 | panel_gconf_general_key (const char *profile, |
---|
81 | const char *key) |
---|
82 | { |
---|
83 | return panel_gconf_sprintf ( |
---|
84 | "/apps/panel/profiles/%s/general/%s", profile, key); |
---|
85 | } |
---|
86 | |
---|
87 | G_CONST_RETURN char * |
---|
88 | panel_gconf_full_key (PanelGConfKeyType type, |
---|
89 | const char *profile, |
---|
90 | const char *id, |
---|
91 | const char *key) |
---|
92 | { |
---|
93 | char *subdir = NULL; |
---|
94 | |
---|
95 | switch (type) { |
---|
96 | case PANEL_GCONF_PANELS: |
---|
97 | subdir = "panels"; |
---|
98 | break; |
---|
99 | case PANEL_GCONF_OBJECTS: |
---|
100 | subdir = "objects"; |
---|
101 | break; |
---|
102 | case PANEL_GCONF_APPLETS: |
---|
103 | subdir = "applets"; |
---|
104 | break; |
---|
105 | default: |
---|
106 | g_assert_not_reached (); |
---|
107 | break; |
---|
108 | } |
---|
109 | |
---|
110 | return panel_gconf_sprintf ( |
---|
111 | "/apps/panel/profiles/%s/%s/%s/%s", |
---|
112 | profile, subdir, id, key); |
---|
113 | } |
---|
114 | |
---|
115 | GConfClient * |
---|
116 | panel_gconf_get_client (void) |
---|
117 | { |
---|
118 | if (!panel_gconf_client) |
---|
119 | panel_gconf_client = gconf_client_get_default (); |
---|
120 | |
---|
121 | return panel_gconf_client; |
---|
122 | } |
---|
123 | |
---|
124 | GSList * |
---|
125 | panel_gconf_all_global_entries (void) |
---|
126 | { |
---|
127 | GSList *list; |
---|
128 | |
---|
129 | list = gconf_client_all_entries ( |
---|
130 | panel_gconf_get_client (), "/apps/panel/global", NULL); |
---|
131 | |
---|
132 | return list; |
---|
133 | } |
---|
134 | |
---|
135 | int |
---|
136 | panel_gconf_get_int (const char *key, |
---|
137 | int default_val) |
---|
138 | { |
---|
139 | GConfValue *value; |
---|
140 | GError *error = NULL; |
---|
141 | |
---|
142 | value = gconf_client_get (panel_gconf_get_client (), key, &error); |
---|
143 | |
---|
144 | if (value) { |
---|
145 | int retval; |
---|
146 | |
---|
147 | if (value->type == GCONF_VALUE_INT) |
---|
148 | retval = gconf_value_get_int (value); |
---|
149 | else |
---|
150 | retval = default_val; |
---|
151 | |
---|
152 | gconf_value_free (value); |
---|
153 | |
---|
154 | return retval; |
---|
155 | } |
---|
156 | |
---|
157 | return default_val; |
---|
158 | } |
---|
159 | |
---|
160 | gboolean |
---|
161 | panel_gconf_get_bool (const char *key, |
---|
162 | gboolean default_val) |
---|
163 | { |
---|
164 | GConfValue *value; |
---|
165 | GError *error = NULL; |
---|
166 | |
---|
167 | value = gconf_client_get (panel_gconf_get_client (), key, &error); |
---|
168 | |
---|
169 | if (value) { |
---|
170 | gboolean retval; |
---|
171 | |
---|
172 | if (value->type == GCONF_VALUE_BOOL) |
---|
173 | retval = gconf_value_get_bool (value); |
---|
174 | else |
---|
175 | retval = default_val; |
---|
176 | |
---|
177 | gconf_value_free (value); |
---|
178 | |
---|
179 | return retval; |
---|
180 | } |
---|
181 | |
---|
182 | return default_val; |
---|
183 | } |
---|
184 | |
---|
185 | char * |
---|
186 | panel_gconf_get_string (const char *key, |
---|
187 | const char *default_val) |
---|
188 | { |
---|
189 | GConfValue *value; |
---|
190 | GError *error = NULL; |
---|
191 | |
---|
192 | value = gconf_client_get (panel_gconf_get_client (), key, &error); |
---|
193 | |
---|
194 | if (value) { |
---|
195 | char *retval; |
---|
196 | |
---|
197 | if (value->type == GCONF_VALUE_STRING) |
---|
198 | retval = g_strdup (gconf_value_get_string (value)); |
---|
199 | else |
---|
200 | retval = g_strdup (default_val); |
---|
201 | |
---|
202 | gconf_value_free (value); |
---|
203 | |
---|
204 | return retval; |
---|
205 | } |
---|
206 | |
---|
207 | return g_strdup (default_val); |
---|
208 | } |
---|
209 | |
---|
210 | void |
---|
211 | panel_gconf_set_int (const char *key, |
---|
212 | int value) |
---|
213 | { |
---|
214 | gconf_client_set_int (panel_gconf_get_client (), key, value, NULL); |
---|
215 | } |
---|
216 | |
---|
217 | void |
---|
218 | panel_gconf_set_bool (const char *key, |
---|
219 | gboolean value) |
---|
220 | { |
---|
221 | gconf_client_set_bool (panel_gconf_get_client (), key, value, NULL); |
---|
222 | } |
---|
223 | |
---|
224 | void |
---|
225 | panel_gconf_set_string (const char *key, |
---|
226 | const char *value) |
---|
227 | { |
---|
228 | gconf_client_set_string (panel_gconf_get_client (), key, value, NULL); |
---|
229 | } |
---|
230 | |
---|
231 | guint |
---|
232 | panel_gconf_notify_add (const char *key, |
---|
233 | GConfClientNotifyFunc notify_func, |
---|
234 | gpointer user_data) |
---|
235 | { |
---|
236 | return gconf_client_notify_add ( |
---|
237 | panel_gconf_get_client (), key, notify_func, |
---|
238 | user_data, NULL, NULL); |
---|
239 | } |
---|
240 | |
---|
241 | static void |
---|
242 | panel_notify_object_dead (guint notify_id) |
---|
243 | { |
---|
244 | gconf_client_notify_remove (panel_gconf_get_client (), notify_id); |
---|
245 | } |
---|
246 | |
---|
247 | guint |
---|
248 | panel_gconf_notify_add_while_alive (const char *key, |
---|
249 | GConfClientNotifyFunc notify_func, |
---|
250 | GObject *alive_object) |
---|
251 | { |
---|
252 | guint notify_id; |
---|
253 | |
---|
254 | g_return_val_if_fail (G_IS_OBJECT (alive_object), 0); |
---|
255 | |
---|
256 | notify_id = panel_gconf_notify_add (key, notify_func, alive_object); |
---|
257 | |
---|
258 | if (notify_id > 0) { |
---|
259 | /* Add a weak reference to the object so that we can |
---|
260 | * remove the notification when the object's gone. |
---|
261 | */ |
---|
262 | g_object_weak_ref (alive_object, |
---|
263 | (GWeakNotify) panel_notify_object_dead, |
---|
264 | GUINT_TO_POINTER (notify_id)); |
---|
265 | } |
---|
266 | |
---|
267 | return notify_id; |
---|
268 | } |
---|
269 | |
---|
270 | void |
---|
271 | panel_gconf_add_dir (const char *key) |
---|
272 | { |
---|
273 | gconf_client_add_dir ( |
---|
274 | panel_gconf_get_client (), key, GCONF_CLIENT_PRELOAD_NONE, NULL); |
---|
275 | } |
---|
276 | |
---|
277 | void |
---|
278 | panel_gconf_clean_dir (GConfClient *client, |
---|
279 | const char *dir) |
---|
280 | { |
---|
281 | GSList *subdirs; |
---|
282 | GSList *entries; |
---|
283 | GSList *l; |
---|
284 | |
---|
285 | subdirs = gconf_client_all_dirs (client, dir, NULL); |
---|
286 | |
---|
287 | for (l = subdirs; l; l = l->next) { |
---|
288 | panel_gconf_clean_dir (client, (char *) l->data); |
---|
289 | g_free (l->data); |
---|
290 | } |
---|
291 | |
---|
292 | g_slist_free (subdirs); |
---|
293 | |
---|
294 | entries = gconf_client_all_entries (client, dir, NULL); |
---|
295 | |
---|
296 | for (l = entries; l; l = l->next) { |
---|
297 | GConfEntry *entry = l->data; |
---|
298 | |
---|
299 | gconf_client_unset (client, gconf_entry_get_key (entry), NULL); |
---|
300 | gconf_entry_free (entry); |
---|
301 | } |
---|
302 | |
---|
303 | g_slist_free (entries); |
---|
304 | |
---|
305 | gconf_client_unset (client, dir, NULL); |
---|
306 | } |
---|
307 | |
---|
308 | static void |
---|
309 | panel_gconf_associate_schemas_in_dir (GConfClient *client, |
---|
310 | const char *profile_dir, |
---|
311 | const char *schema_dir, |
---|
312 | GError **error) |
---|
313 | { |
---|
314 | GSList *list, *l; |
---|
315 | |
---|
316 | #ifdef PANEL_GCONF_DEBUG |
---|
317 | g_print ("associating schemas in %s to %s\n", schema_dir, profile_dir); |
---|
318 | #endif |
---|
319 | |
---|
320 | list = gconf_client_all_entries (client, schema_dir, error); |
---|
321 | |
---|
322 | g_return_if_fail (*error == NULL); |
---|
323 | |
---|
324 | for (l = list; l; l = l->next) { |
---|
325 | GConfEntry *entry = l->data; |
---|
326 | const char *key; |
---|
327 | char *tmp; |
---|
328 | |
---|
329 | tmp = g_path_get_basename (gconf_entry_get_key (entry)); |
---|
330 | |
---|
331 | key = panel_gconf_sprintf ("%s/%s", profile_dir, tmp); |
---|
332 | |
---|
333 | g_free (tmp); |
---|
334 | |
---|
335 | gconf_engine_associate_schema ( |
---|
336 | client->engine, key, gconf_entry_get_key (entry), error); |
---|
337 | |
---|
338 | gconf_entry_free (entry); |
---|
339 | |
---|
340 | if (*error) { |
---|
341 | g_slist_free (list); |
---|
342 | return; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | g_slist_free (list); |
---|
347 | |
---|
348 | list = gconf_client_all_dirs (client, schema_dir, error); |
---|
349 | |
---|
350 | for (l = list; l; l = l->next) { |
---|
351 | char *subdir = l->data; |
---|
352 | char *prefs_subdir; |
---|
353 | char *schema_subdir; |
---|
354 | char *tmp; |
---|
355 | |
---|
356 | tmp = g_path_get_basename (subdir); |
---|
357 | |
---|
358 | prefs_subdir = g_strdup_printf ("%s/%s", profile_dir, tmp); |
---|
359 | schema_subdir = g_strdup_printf ("%s/%s", schema_dir, tmp); |
---|
360 | |
---|
361 | panel_gconf_associate_schemas_in_dir ( |
---|
362 | client, prefs_subdir, schema_subdir, error); |
---|
363 | |
---|
364 | g_free (prefs_subdir); |
---|
365 | g_free (schema_subdir); |
---|
366 | g_free (subdir); |
---|
367 | g_free (tmp); |
---|
368 | |
---|
369 | if (*error) { |
---|
370 | g_slist_free (list); |
---|
371 | return; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | g_slist_free (list); |
---|
376 | } |
---|
377 | |
---|
378 | void |
---|
379 | panel_gconf_setup_profile (const char *profile) |
---|
380 | { |
---|
381 | GError *error = NULL; |
---|
382 | char *profile_dir; |
---|
383 | char *schema_dir; |
---|
384 | |
---|
385 | if (profile) |
---|
386 | panel_gconf_profile = g_strdup (profile); |
---|
387 | else |
---|
388 | panel_gconf_profile = g_strdup ("default"); |
---|
389 | |
---|
390 | profile_dir = g_strconcat ("/apps/panel/profiles/", panel_gconf_profile, NULL); |
---|
391 | |
---|
392 | if (gconf_client_dir_exists (panel_gconf_get_client (), profile_dir, NULL)) { |
---|
393 | g_free (profile_dir); |
---|
394 | return; |
---|
395 | } |
---|
396 | |
---|
397 | #ifdef PANEL_GCONF_DEBUG |
---|
398 | g_print ("%s does not exist\n", profile_dir); |
---|
399 | #endif |
---|
400 | |
---|
401 | /* |
---|
402 | * FIXME: work out what set of defaults we want to use |
---|
403 | */ |
---|
404 | schema_dir = g_strconcat ("/schemas/apps/panel/default_profiles/", "medium", NULL); |
---|
405 | |
---|
406 | panel_gconf_associate_schemas_in_dir ( |
---|
407 | panel_gconf_get_client (), profile_dir, schema_dir, &error); |
---|
408 | |
---|
409 | if (error != NULL) { |
---|
410 | g_warning ("gconf error: %s", error->message); |
---|
411 | g_clear_error (&error); |
---|
412 | } |
---|
413 | |
---|
414 | g_free (profile_dir); |
---|
415 | g_free (schema_dir); |
---|
416 | |
---|
417 | /* |
---|
418 | * FIXME: setup notifies |
---|
419 | */ |
---|
420 | } |
---|
421 | |
---|
422 | char * |
---|
423 | panel_gconf_load_default_config_for_screen (PanelGConfKeyType type, |
---|
424 | const char *profile, |
---|
425 | const char *id, |
---|
426 | int screen, |
---|
427 | GError **error) |
---|
428 | { |
---|
429 | const char *subdir = NULL; |
---|
430 | const char *schemas_dir; |
---|
431 | char *new_dir; |
---|
432 | char *new_id; |
---|
433 | |
---|
434 | switch (type) { |
---|
435 | case PANEL_GCONF_PANELS: |
---|
436 | subdir = "panels"; |
---|
437 | break; |
---|
438 | case PANEL_GCONF_OBJECTS: |
---|
439 | subdir = "objects"; |
---|
440 | break; |
---|
441 | case PANEL_GCONF_APPLETS: |
---|
442 | subdir = "applets"; |
---|
443 | break; |
---|
444 | default: |
---|
445 | g_assert_not_reached (); |
---|
446 | break; |
---|
447 | } |
---|
448 | |
---|
449 | schemas_dir = panel_gconf_sprintf ( |
---|
450 | "/schemas/apps/panel/default_profiles/medium/%s/%s", |
---|
451 | subdir, id); |
---|
452 | |
---|
453 | new_id = g_strdup_printf ("%s_screen%d", id, screen); |
---|
454 | |
---|
455 | new_dir = g_strdup_printf ("/apps/panel/profiles/%s/%s/%s", profile, subdir, new_id); |
---|
456 | |
---|
457 | panel_gconf_associate_schemas_in_dir ( |
---|
458 | panel_gconf_get_client (), new_dir, schemas_dir, error); |
---|
459 | |
---|
460 | g_free (new_dir); |
---|
461 | |
---|
462 | if (error && *error) { |
---|
463 | g_free (new_id); |
---|
464 | new_id = NULL; |
---|
465 | } |
---|
466 | |
---|
467 | return new_id; |
---|
468 | } |
---|