Debian Codegraph @

geanygendoc/src/ggd-options.c

raw
1 /*
2 *
3 * GeanyGenDoc, a Geany plugin to ease generation of source code documentation
4 * Copyright (C) 2010-2011 Colomban Wendling <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h" /* for the gettext domain */
23 #endif
24
25 #include "ggd-options.h"
26
27 #include <glib.h>
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31
32 /* This module is strongly inspired from Geany's Stash module with some design
33 * changes and a complete reimplementation.
34 * The major difference is the way proxies are managed. */
35
36
37 /* stolen from Geany */
38 #define foreach_array(array, type, item) \
39 for ((item) = ((type*)(gpointer)(array)->data); \
40 (item) < &((type*)(gpointer)(array)->data)[(array)->len]; \
41 (item)++)
42
43
44 /*
45 * GgdOptEntry:
46 * @type: The setting's type
47 * @key: The setting's key (both its name and its key in the underlying
48 * configuration file)
49 * @optvar: Pointer to the actual value
50 * @value_destroy: Function use to destroy the value, or %NULL
51 * @proxy: A #GObject to use as a proxy for the value
52 * @proxy_prop: Name of the proxy's property to read and write the value
53 * @destroy_hid: Signal handler identifier for the proxy's ::destroy signal, if
54 * @proxy is a #GtkObject.
55 *
56 * The structure that represents an option.
57 */
58 struct _GgdOptEntry
59 {
60 GType type;
61 gchar *key;
62 gpointer optvar;
63 GDestroyNotify value_destroy;
64 GObject *proxy;
65 gchar *proxy_prop;
66 gulong destroy_hid;
67 };
68
69 typedef struct _GgdOptEntry GgdOptEntry;
70
71 /* syncs an entry's proxy to the entry's value */
72 static void
73 ggd_opt_entry_sync_to_proxy (GgdOptEntry *entry)
74 {
75 if (entry->proxy) {
76 /*g_debug ("Syncing proxy for %s", entry->key);*/
77 g_object_set (entry->proxy, entry->proxy_prop, *(gpointer *)entry->optvar,
78 NULL);
79 }
80 }
81
82 /* syncs an entry's value to the entry's proxy value */
83 static void
84 ggd_opt_entry_sync_from_proxy (GgdOptEntry *entry)
85 {
86 if (entry->proxy) {
87 if (entry->value_destroy) entry->value_destroy (*(gpointer *)entry->optvar);
88 g_object_get (entry->proxy, entry->proxy_prop, entry->optvar, NULL);
89 }
90 }
91
92 /*
93 * ggd_opt_entry_set_proxy:
94 * @entry: A #GgdOptEntry
95 * @proxy: The proxy object
96 * @prop: The name of the proxy's property to sync with
97 *
98 * Sets and syncs the proxy of a #GgdOptEntry.
99 */
100 static void
101 ggd_opt_entry_set_proxy (GgdOptEntry *entry,
102 GObject *proxy,
103 const gchar *prop)
104 {
105 if (entry->proxy) {
106 if (entry->destroy_hid > 0l) {
107 g_signal_handler_disconnect (entry->proxy, entry->destroy_hid);
108 }
109 g_object_unref (entry->proxy);
110 }
111 g_free (entry->proxy_prop);
112 entry->proxy = (proxy) ? g_object_ref (proxy) : proxy;
113 entry->proxy_prop = g_strdup (prop);
114 entry->destroy_hid = 0l;
115 /* sync the proxy with the setting's current state */
116 ggd_opt_entry_sync_to_proxy (entry);
117 }
118
119 /* Frees an entry's allocated data */
120 static void
121 ggd_opt_entry_free_data (GgdOptEntry *entry,
122 gboolean free_opt)
123 {
124 if (entry) {
125 ggd_opt_entry_set_proxy (entry, NULL, NULL);
126 /* don't free the value to let it in a usable state, and it is consistent
127 * since the user allocated it */
128 if (free_opt && entry->value_destroy) {
129 entry->value_destroy (*(gpointer *)entry->optvar);
130 *(gpointer *)entry->optvar = NULL;
131 }
132 g_free (entry->key);
133 }
134 }
135
136
137 struct _GgdOptGroup
138 {
139 gchar *name;
140 GArray *prefs;
141 };
142
143 /**
144 * ggd_opt_group_new:
145 * @section: The name of the section for which this group is for
146 *
147 * Creates a new #GgdOptGroup.
148 *
149 * Returns: The newly created #GgdOptGroup. Free with ggd_opt_group_free().
150 */
151 GgdOptGroup *
152 ggd_opt_group_new (const gchar *section)
153 {
154 GgdOptGroup *group;
155
156 group = g_slice_alloc (sizeof *group);
157 group->name = g_strdup (section);
158 group->prefs = g_array_new (FALSE, FALSE, sizeof (GgdOptEntry));
159
160 return group;
161 }
162
163 /**
164 * ggd_opt_group_free:
165 * @group: A #GgdoptGroup
166 * @free_opts: Whether to free the allocated options or not
167 *
168 * Frees a #GgdOptGroup.
169 */
170 void
171 ggd_opt_group_free (GgdOptGroup *group,
172 gboolean free_opts)
173 {
174 if (group) {
175 GgdOptEntry *entry;
176
177 foreach_array (group->prefs, GgdOptEntry, entry) {
178 ggd_opt_entry_free_data (entry, free_opts);
179 }
180 g_array_free (group->prefs, TRUE);
181 g_free (group->name);
182 g_slice_free1 (sizeof *group, group);
183 }
184 }
185
186 /* adds an entry to a group */
187 static GgdOptEntry *
188 ggd_opt_group_add_entry (GgdOptGroup *group,
189 GType type,
190 const gchar *key,
191 gpointer optvar,
192 GDestroyNotify value_destroy)
193 {
194 GgdOptEntry entry;
195
196 entry.type = type;
197 entry.key = g_strdup (key);
198 entry.optvar = optvar;
199 entry.value_destroy = value_destroy;
200 entry.proxy = NULL;
201 entry.proxy_prop = NULL;
202
203 g_array_append_val (group->prefs, entry);
204
205 /* cppcheck-suppress memleak symbolName=entry.key */
206 return &g_array_index (group->prefs, GgdOptEntry, group->prefs->len -1);
207 }
208
209 /* looks up for an entry in a group */
210 static GgdOptEntry *
211 ggd_opt_group_lookup_entry (GgdOptGroup *group,
212 gpointer optvar)
213 {
214 GgdOptEntry *entry;
215
216 foreach_array (group->prefs, GgdOptEntry, entry) {
217 if (entry->optvar == optvar) {
218 return entry;
219 }
220 }
221
222 return NULL;
223 }
224
225 /* looks up an entry in a group from is proxy */
226 static GgdOptEntry *
227 ggd_opt_group_lookup_entry_from_proxy (GgdOptGroup *group,
228 GObject *proxy)
229 {
230 GgdOptEntry *entry;
231
232 foreach_array (group->prefs, GgdOptEntry, entry) {
233 if (entry->proxy == proxy) {
234 return entry;
235 }
236 }
237
238 return NULL;
239 }
240
241 /**
242 * ggd_opt_group_add_boolean:
243 * @group: A #GgdOptGroup
244 * @optvar: Pointer to setting's variable. It must be already set to a valid
245 * value.
246 * @key: The key name for this setting
247 *
248 * Adds a boolean setting to a #GgdOptGroup.
249 */
250 void
251 ggd_opt_group_add_boolean (GgdOptGroup *group,
252 gboolean *optvar,
253 const gchar *key)
254 {
255 ggd_opt_group_add_entry (group, G_TYPE_BOOLEAN, key, optvar, NULL);
256 }
257
258 /**
259 * ggd_opt_group_add_string:
260 * @group: A #GgdOptGroup
261 * @optvar: Pointer to setting's variable. It must be already set to a valid
262 * value allocated by the GLib's memory manager or to %NULL since it
263 * must can be freed.
264 * @key: The key name for this setting
265 *
266 * Adds a string setting to a #GgdOptGroup.
267 */
268 void
269 ggd_opt_group_add_string (GgdOptGroup *group,
270 gchar **optvar,
271 const gchar *key)
272 {
273 if (*optvar == NULL) {
274 *optvar = g_strdup ("");
275 }
276 ggd_opt_group_add_entry (group, G_TYPE_STRING, key, (gpointer)optvar, g_free);
277 }
278
279
280 /**
281 * ggd_opt_group_sync_to_proxies:
282 * @group: A #GgdOptGroup
283 *
284 * Syncs proxies' values of a #GgdOptGroup to their setting' value.
285 */
286 void
287 ggd_opt_group_sync_to_proxies (GgdOptGroup *group)
288 {
289 GgdOptEntry *entry;
290
291 foreach_array (group->prefs, GgdOptEntry, entry) {
292 ggd_opt_entry_sync_to_proxy (entry);
293 }
294 }
295
296 /**
297 * ggd_opt_group_sync_from_proxies:
298 * @group: A #GgdOptGroup
299 *
300 * Syncs settings of a #GgdOptGroup to their proxies' values.
301 */
302 void
303 ggd_opt_group_sync_from_proxies (GgdOptGroup *group)
304 {
305 GgdOptEntry *entry;
306
307 foreach_array (group->prefs, GgdOptEntry, entry) {
308 ggd_opt_entry_sync_from_proxy (entry);
309 }
310 }
311
312 /* set the proxy of a value
313 * see the doc of ggd_opt_group_set_proxy_full() that does the same but returns
314 * a boolean */
315 static GgdOptEntry *
316 ggd_opt_group_set_proxy_full_internal (GgdOptGroup *group,
317 gpointer optvar,
318 gboolean check_type,
319 GType type_check,
320 GObject *proxy,
321 const gchar *prop)
322 {
323 GgdOptEntry *entry;
324
325 entry = ggd_opt_group_lookup_entry (group, optvar);
326 if (! entry) {
327 g_warning (_("Unknown option"));
328 } else {
329 if (check_type) {
330 gboolean success = TRUE;
331 GValue val = {0};
332
333 g_value_init (&val, type_check);
334 g_object_get_property (proxy, prop, &val);
335 if (! (G_VALUE_HOLDS (&val, type_check) && entry->type == type_check)) {
336 g_critical (_("Invalid option or proxy: either the proxy's property or "
337 "the option type is incompatible."));
338 }
339 g_value_unset (&val);
340 if (! success) {
341 return FALSE;
342 }
343 }
344 ggd_opt_entry_set_proxy (entry, proxy, prop);
345 }
346
347 return entry;
348 }
349
350 /**
351 * ggd_opt_group_set_proxy_full:
352 * @group: A #GgdOptGroup
353 * @optvar: The setting's pointer, as passed when adding the setting
354 * @check_type: Whether to check the consistence of the setting's type and the
355 * proxy's property
356 * @type_check: The type that the setting and the proxy's property should have
357 * (only used if @check_type is %TRUE)
358 * @proxy: A #GObject to use as proxy for this setting
359 * @prop: The property name of the proxy (must be readable and/or writable,
360 * depending on whether the prosy are sunk from or to settings)
361 *
362 * This function sets the proxy object for a given setting.
363 * Proxy objects can be used to e.g. display and edit a setting.
364 * If you use a #GtkObject derivate as proxy (such as #GtkWidget<!-- -->s),
365 * consider using ggd_opt_group_set_proxy_gtkobject_full() in place of this
366 * function.
367 *
368 * <note><para>
369 * Setting the proxy sets its value to the setting's value, no need to sync it
370 * again with ggd_opt_group_sync_to_proxies().
371 * </para></note>
372 *
373 * Returns: %TRUE if the proxy was correctly set, or %FALSE if the setting
374 * doesn't exists in @group.
375 */
376 gboolean
377 ggd_opt_group_set_proxy_full (GgdOptGroup *group,
378 gpointer optvar,
379 gboolean check_type,
380 GType type_check,
381 GObject *proxy,
382 const gchar *prop)
383 {
384 return ggd_opt_group_set_proxy_full_internal (group, optvar,
385 check_type, type_check,
386 proxy, prop) != NULL;
387 }
388
389 /**
390 * ggd_opt_group_remove_proxy:
391 * @group: A #GgdOptGroup
392 * @optvar: The setting's pointer, as passed when adding the setting
393 *
394 * Removes the proxy of a given setting.
395 */
396 void
397 ggd_opt_group_remove_proxy (GgdOptGroup *group,
398 gpointer optvar)
399 {
400 ggd_opt_group_set_proxy_full_internal (group, optvar, FALSE, 0, NULL, NULL);
401 }
402
403 /* detaches a proxy */
404 static void
405 ggd_opt_group_remove_proxy_from_proxy (GgdOptGroup *group,
406 GObject *proxy)
407 {
408 GgdOptEntry *entry;
409
410 entry = ggd_opt_group_lookup_entry_from_proxy (group, proxy);
411 if (entry) {
412 ggd_opt_entry_set_proxy (entry, NULL, NULL);
413 }
414 }
415
416 /**
417 * ggd_opt_group_set_proxy_gtkobject_full:
418 * @group: A #GgdOptGroup
419 * @optvar: The setting's pointer, as passed when adding the setting
420 * @check_type: Whether to check the consistence of the setting's type and the
421 * proxy's property
422 * @type_check: The type that the setting and the proxy's property should have
423 * (only used if @check_type is %TRUE)
424 * @proxy: A #GtkObject to use as proxy for this setting
425 * @prop: The property name of the proxy (must be readable and/or writable,
426 * depending on whether the prosy are sunk from or to settings)
427 *
428 * This is very similar to ggd_opt_group_set_proxy_full() but adds a signal
429 * handler on object's ::destroy signal to release it when destroyed.
430 * This should be used when @proxy is a GtkObject derivate, unless you handle
431 * the case yourself (e.g. to sync when widgets gets destroyed).
432 *
433 * Returns: %TRUE if the proxy was correctly set, or %FALSE if the setting
434 * doesn't exists in @group.
435 */
436 gboolean
437 ggd_opt_group_set_proxy_gtkobject_full (GgdOptGroup *group,
438 gpointer optvar,
439 gboolean check_type,
440 GType type_check,
441 GtkObject *proxy,
442 const gchar *prop)
443 {
444 GgdOptEntry *entry;
445
446 entry = ggd_opt_group_set_proxy_full_internal (group, optvar,
447 check_type, type_check,
448 G_OBJECT (proxy), prop);
449 if (entry) {
450 entry->destroy_hid = g_signal_connect_swapped (
451 proxy, "destroy",
452 G_CALLBACK (ggd_opt_group_remove_proxy_from_proxy), group
453 );
454 }
455
456 return entry != NULL;
457 }
458
459 /*
460 * ggd_opt_group_manage_key_file:
461 * @group: A #GgdOptGroup
462 * @load: Whether to read (%TRUE) or write (%FALSE) the group to or from the
463 * given key file
464 * @key_file: A #GKeyFile
465 *
466 * Reads or writes a #GgdOptGroup from or to a #GKeyFile
467 */
468 static void
469 ggd_opt_group_manage_key_file (GgdOptGroup *group,
470 gboolean load,
471 GKeyFile *key_file)
472 {
473 GgdOptEntry *entry;
474
475 foreach_array (group->prefs, GgdOptEntry, entry) {
476 GError *err = NULL;
477
478 switch (entry->type) {
479 case G_TYPE_BOOLEAN: {
480 gboolean *setting = (gboolean *)entry->optvar;
481
482 if (load) {
483 gboolean v;
484
485 v = g_key_file_get_boolean (key_file, group->name, entry->key, &err);
486 if (! err) {
487 *setting = v;
488 }
489 } else {
490 g_key_file_set_boolean (key_file, group->name, entry->key, *setting);
491 }
492 break;
493 }
494
495 case G_TYPE_STRING: {
496 gchar **setting = (gchar **)entry->optvar;
497
498 if (load) {
499 gchar *str;
500
501 str = g_key_file_get_string (key_file, group->name, entry->key, &err);
502 if (! err) {
503 if (entry->value_destroy) entry->value_destroy (*setting);
504 *setting = str;
505 }
506 } else {
507 g_key_file_set_string (key_file, group->name, entry->key, *setting);
508 }
509 break;
510 }
511
512 default:
513 g_warning (_("Unknown value type for keyfile entry %s::%s"),
514 group->name, entry->key);
515 }
516 if (err) {
517 g_warning (_("Error retrieving keyfile entry %s::%s: %s"),
518 group->name, entry->key, err->message);
519 g_error_free (err);
520 } else {
521 if (load) {
522 ggd_opt_entry_sync_to_proxy (entry);
523 }
524 }
525 }
526 }
527
528 /**
529 * ggd_opt_group_load_from_key_file:
530 * @group: A #GgdOptGroup
531 * @key_file: A #GKeyFile from where load values
532 *
533 * Loads values of a #GgdOptGroup from a #GKeyFile.
534 */
535 void
536 ggd_opt_group_load_from_key_file (GgdOptGroup *group,
537 GKeyFile *key_file)
538 {
539 ggd_opt_group_manage_key_file (group, TRUE, key_file);
540 }
541
542 /**
543 * ggd_opt_group_load_from_file:
544 * @group: A #GgdOptGroup
545 * @filename: Name of the file from which load values in the GLib file names
546 * encoding
547 * @error: return location for or %NULL to ignore them
548 *
549 * Loads values of a #GgdOptGroup from a file.
550 *
551 * Returns: %TRUE on success, %FALSE otherwise
552 */
553 gboolean
554 ggd_opt_group_load_from_file (GgdOptGroup *group,
555 const gchar *filename,
556 GError **error)
557 {
558 gboolean success = FALSE;
559 GKeyFile *key_file;
560
561 key_file = g_key_file_new ();
562 if (g_key_file_load_from_file (key_file, filename, 0, error)) {
563 ggd_opt_group_load_from_key_file (group, key_file);
564 success = TRUE;
565 }
566 g_key_file_free (key_file);
567
568 return success;
569 }
570
571 /**
572 * ggd_opt_group_write_to_key_file:
573 * @group: A #GgdOptGroup
574 * @key_file: A #GKeyFile
575 *
576 * Writes the values of a #GgdOptGroup to a #GkeyFile.
577 */
578 void
579 ggd_opt_group_write_to_key_file (GgdOptGroup *group,
580 GKeyFile *key_file)
581 {
582 ggd_opt_group_manage_key_file (group, FALSE, key_file);
583 }
584
585 /**
586 * ggd_opt_group_write_to_file:
587 * @group: A #GgdOptGroup
588 * @filename: Name of the file in which save the values, in the GLib file names
589 * encoding
590 * @error: Return location for errors or %NULL to ignore them
591 *
592 * Writes a #GgdOptGroup to a file.
593 * It keeps everything in the file, overwriting only the group managed by the
594 * given #GgdOptGroup. This means that it is perfectly safe to use this function
595 * to save a group that is not alone in a key file.
596 *
597 * <note><para>The file must be both readable and writable.</para></note>
598 *
599 * Returns: %TRUE on success, %FALSE otherwise.
600 */
601 gboolean
602 ggd_opt_group_write_to_file (GgdOptGroup *group,
603 const gchar *filename,
604 GError **error)
605 {
606 gboolean success = FALSE;
607 GKeyFile *key_file;
608 gchar *data;
609 gsize data_length;
610
611 key_file = g_key_file_new ();
612 /* try to load the original file but blindly ignore errors because they are
613 * unlikely to be interesting (the file doesn't already exist, a syntax error
614 * because the file exists but is empty (yes, this throws a parse error),
615 * etc.) */
616 g_key_file_load_from_file (key_file, filename,
617 G_KEY_FILE_KEEP_COMMENTS |
618 G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
619 ggd_opt_group_write_to_key_file (group, key_file);
620 data = g_key_file_to_data (key_file, &data_length, error);
621 if (data) {
622 success = g_file_set_contents (filename, data, data_length, error);
623 g_free (data);
624 }
625 g_key_file_free (key_file);
626
627 return success;
628 }