1 ===============================
2 gEDA Configuration System API
3 ===============================
6 :Status: Proposed Specification
9 This document can be processed with docutils <http://docutils.sf.net>
10 to generate HTML or LaTeX. However, the markup is designed to be
11 fully human-readable as-is.
17 2.1 Errors in C functions
18 2.2 Errors in Scheme functions
19 3 Configuration Contexts
20 3.1 Obtaining a Context
24 3.2 Loading configuration files
25 3.3 Saving configuration files
28 4 Configuration Parameters
31 4.3 Configuration inheritance
33 4.4.1 Getting configuration values
34 4.4.2 Setting configuration values
35 5 Configuration Events
44 This document outlines the API provided by libgeda for configuration
45 of both libgeda and applications based on it. The API is designed for
46 not only saving and loading static configuration data to and from
47 permanent storage, but also to allow on-line event-driven updates to
48 the configuration state. The API is designed to be equally usable
49 from C code and from Scheme extensions. It is heavily inspired by the
50 ``GKeyFile`` API from GLib, with a few changes and enhancements
59 Recoverable errors that occur in C functions are signalled to the
60 caller using the "GError" mechanism [GLIB]_.
62 - File errors (e.g. "Access denied" or "File not found") are indicated
63 using the ``G_IO_ERROR`` error domain. See the GIO manual for
64 information on error codes. Functions that may cause a
65 ``G_IO_ERROR`` are clearly noted as such.
67 - All other errors are indicated using the ``EDA_CONFIG_ERROR`` error
68 domain. The possible error codes are:
70 ``EDA_CONFIG_ERROR_UNKNOWN_ENCODING``
71 The text being parsed was in an unknown encoding.
73 ``EDA_CONFIG_ERROR_PARSE``
74 The configuration data was ill-formed.
76 ``EDA_CONFIG_ERROR_KEY_NOT_FOUND``
77 A requested configuration key was not found.
79 ``EDA_CONFIG_ERROR_GROUP_NOT_FOUND``
80 A requested configuration group was not found.
82 ``EDA_CONFIG_ERROR_INVALID_VALUE``
83 A configuration value could not be parsed into the requested
86 Errors in Scheme functions
87 --------------------------
89 All errors in Scheme functions are reported by raising Guile errors
90 [GUILE]_. There are two possible error keys that may arise:
92 - File errors (e.g. "Access denied" or "File not found") are indicated
93 with the ``system-error`` key.
95 - All other errors are indicated using the ``config-error`` key. The
96 ``data`` part of the error arguments is a list containing one of the
100 The text being parsed was in an unknown encoding.
103 The configuration data was ill-formed.
106 A requested configuration key was not found.
109 A requested configuration group was not found.
112 A configuration value could not be parsed into the requested
115 Configuration Contexts
116 ======================
118 A configuration parameter is always evaluated within a *configuration
119 context*. Each context is associated with a configuration file
120 (although the file does not necessarily need to exist).
122 Each configuration context may have a *parent context*. If, when
123 looking up a parameter, it has no value set in the selected context,
124 the parent context is checked, and so on.
126 Configuration contexts are reference counted. You can increment and
127 decrement their reference counters using ``g_object_ref()`` and
128 ``g_object_unref()`` [GOBJECT]_.
130 Three special contexts are always automatically defined: the `default
131 context`_, the `system context`_, and the `user context`_. The user
132 context is the default parent context for newly-created configuration
140 EdaConfig *eda_config_get_context_for_path (const gchar *path,
145 path-config-context path
147 Normally, you shouldn't create a configuration context directly; you
148 should obtain the configuration context associated with a ``path``.
149 If a context matching the path does not yet exist, it is created.
151 ``path-config-context`` looks for a configuration file named
152 ``geda.conf``. If ``path`` is not a directory, it is truncated, and
153 then a file named ``geda.conf`` is looked for in that directory. If
154 none is found, the parent directory is checked, and so on until a
155 configuration file is found or the filesystem root is reached. If no
156 configuration file was found, the returned context will be associated
157 with a ``geda.conf`` in the same directory as ``path``.
159 .. warning:: Do not assume that the configuration file associated with
160 the context returned by ``path-config-context`` is
161 located in the directory specified by ``path``.
163 The configuration context returned by
164 ``eda_config_get_context_for_path()`` is owned by the library, and
165 should not be disposed of with ``g_object_unref()``.
173 EdaConfig *eda_config_get_default_context ()
177 default-config-context
179 The default context is not associated with any physical path or
180 on-disk configuration file, and has no parent context. It contains
181 the default configuration used when no configuration file can be
182 loaded. Applications should normally populate the default context
183 with their built-in default configuration settings on start-up, before
184 loading any further configuration files.
186 The configuration context returned by
187 ``eda_config_get_default_context()`` is owned by the library, and
188 should not be disposed of with ``g_object_unref()``.
195 EdaConfig *eda_config_get_system_context ()
199 system-config-context
201 The system context is used for system-wide configuration. It is located:
203 1. By searching ``${XDG_CONFIG_DIRS}`` for a ``gEDA/geda-system.conf``
206 2. By checking ``${sysconfdir}/gEDA`` for a configuration file.
208 Its parent context is the `default context`_.
210 The configuration context returned by
211 ``eda_config_get_system_context()`` is owned by the library, and
212 should not be disposed of with ``g_object_unref()``.
219 EdaConfig *eda_config_get_user_context ()
225 The user context is used for user-specific configuration, and is
226 loaded from ``${XDG_CONFIG_HOME}/gEDA/geda-user.conf`` [XDGDIRS]_.
227 Its parent context is the `system context`_.
229 The configuration context returned by
230 ``eda_config_get_user_context()`` is owned by the library, and should
231 not be disposed of with ``g_object_unref()``.
233 Loading configuration files
234 ---------------------------
236 Other than the `default context`_, all configuration contexts are
237 associated with an on-disk configuration file.
241 const gchar *eda_config_get_filename (EdaConfig *cfg)
247 Return the filename of the configuration file associated with the
248 context ``cfg``. For some contexts (including the `default
249 context`_), this will return ``NULL`` (in C) or ``#f`` (in Scheme).
253 gboolean eda_config_load (EdaConfig *cfg, GError **err)
259 Attempt to load configuration parameters for the context ``cfg`` from
260 its associated file. This function may generate a ``GIOError`` (in
261 C) or a ``system-error`` (in Scheme).
265 gboolean eda_config_is_loaded (EdaConfig *cfg)
271 Determine whether the context ``cfg`` has been successfully loaded
274 Saving configuration files
275 --------------------------
279 gboolean eda_config_save (EdaConfig *cfg, GError *error)
285 Attempt to save configuration parameters for the context ``cfg`` to
286 its associated file. This function may generate a ``GIOError`` (in
287 C) or a ``system-error`` (in Scheme).
291 gboolean eda_config_is_changed (EdaConfig *cfg)
297 Determine whether the context ``cfg`` has been altered since it was
298 last synchronised with the on-disk version by loading or saving it.
305 EdaConfig *eda_config_get_parent (EdaConfig *cfg)
311 Return the parent context of the context ``cfg``, if it has one.
315 void eda_config_set_parent (EdaConfig *cfg, EdaConfig *parent)
319 set-config-parent! cfg parent
321 Sets ``parent`` as the parent context of ``cfg``. If ``parent`` is
322 NULL or ``#f``, sets ``cfg`` as having no parent context.
324 .. note:: Normally, application code should avoid using this function;
325 keeping to the default configuration inheritance structure
326 is recommended in order to ensure consistent behaviour of
327 all libgeda applications.
332 Some configuration parameters are dangerous; in particular, parameters
333 that may lead to arbitrary code execution need to be handled
334 carefully. Such settings might include:
336 - Preferred PDF reader
337 - Preferred web browser
338 - Search path for Scheme plugins
340 Configuration contexts can be flagged as being 'trusted'. This allows
341 code that needs to access such dangerous parameters to determine
342 whether the value has been obtained from a safe source.
344 By default, the `default context`_, `system context`_ and `user
345 context`_ are trusted, and all other contexts untrusted.
349 gboolean eda_config_is_trusted (EdaConfig *cfg)
355 Test whether ``cfg`` is a trusted configuration context.
359 void eda_config_set_trusted (EdaConfig *cfg, gboolean trusted)
363 set-config-trusted! cfg trusted
365 Set whether the configuration context ``cfg`` should be trusted as a
366 source for dangerous configuration parameters.
368 .. warning:: You should not set a configuration context as trusted
369 unless you are certain that it originated from a safe
370 source (e.g. by interacting with the user to verify it).
374 EdaConfig *eda_config_get_trusted_context (EdaConfig *cfg)
378 config-trusted-context cfg
380 If ``cfg`` is trusted, returns ``cfg``; otherwise, returns the first
381 parent context of the configuration context ``cfg`` that is a trusted
382 context. If no trusted context could be found, returns NULL or
385 Configuration Parameters
386 ========================
388 A gEDA/gaf *configuration parameter* consists of three components:
391 A UTF-8 string which identifies the general category in which the
392 parameter lies (e.g. which application).
395 A UTF-8 string which specifically identifies the parameter within
399 The value of the parameter. This is stored as a UTF-8 string, but
400 can be converted to a number of possible scalar and list types.
402 Groups, names and values are all case-sensitive.
409 gchar **eda_config_get_groups (EdaConfig *cfg, gsize *length)
415 Returns a list of all groups available in ``cfg`` and its parent
416 contexts. The value returned by ``eda_config_get_groups()`` is a
417 newly-allocated NULL-terminated array of strings. Use
418 ``g_strfreev()`` to free it [GLIB_]. The ``length`` argument is an
419 optional return location for the number of groups returned.
423 gboolean eda_config_has_group (EdaConfig *cfg, const gchar *group)
427 config-has-group? cfg group
429 Test whether ``cfg`` or its parent contexts contain the specified
437 gchar **eda_config_get_keys (EdaConfig *cfg, const gchar *group,
438 gsize *length, GError **error)
442 config-keys cfg group
444 Returns a list of all keys available in the specified ``group`` in
445 ``cfg`` and its parent contexts. The value returned by
446 ``eda_config_get_keys()`` is a newly-allocated NULL-terminated array
447 of strings. Use ``g_strfreev()`` to free it [GLIB_]. The ``length``
448 argument is an optional return location for the number of keys
449 returned. If neither ``cfg`` nor any of its parent contexts contain
450 ``group``, raises an ``EdaConfigError`` (in C) or a ``config-error``
455 gboolean eda_config_has_key (EdaConfig *cfg, const gchar *group,
456 const gchar *key, GError **err)
460 config-has-key? cfg group key
462 Test whether ``cfg`` or its parent contexts contains ``key`` in the
465 Configuration inheritance
466 -------------------------
470 gboolean eda_config_is_inherited (EdaConfig *cfg, const gchar *group,
471 const gchar *key, GError **err)
475 config-inherited? cfg group key
477 Tests whether the value of the configuration parameter with the given
478 ``group`` and ``key`` is specified in the context ``cfg``, or whether
479 it is inherited from a parent context of ``cfg``.
483 EdaConfig *eda_config_get_source (EdaConfig *cfg, const gchar *group,
484 const gchar *key, GError **err)
488 config-source group key
490 Returns the configuration context (either ``cfg`` or one of its parent
491 contexts) in which the configuration parameter with the given
492 ``group`` and ``key`` has a value specified.
498 Each value is stored as a UTF-8 string in the configuration file.
499 However, this string can be parsed as several different types. All of
500 the following types are supported:
505 - Double-precision floating point numbers
507 In addition, lists of all of the above are supported.
509 Getting configuration values
510 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512 In general, errors in obtaining a value using the C functions should
513 be checked for using a GError. The list functions take a return
514 location for the ``length`` of list (for
515 ``eda_config_get_string_list()`` this argument may be NULL, since the
516 returned array is NULL-terminated). Returned strings should be freed
517 using ``g_free()``, and returned lists should be freed using
518 ``g_free()`` or ``g_strfreev()`` as appropriate [GLIB]_.
522 gchar *eda_config_get_string (EdaConfig *cfg, const gchar *group, const gchar *key,
525 gboolean eda_config_get_boolean (EdaConfig *cfg, const gchar *group, const gchar *key,
528 gint eda_config_get_int (EdaConfig *cfg, const gchar *group, const gchar *key,
531 gdouble eda_config_get_double (EdaConfig *cfg, const gchar *group, const gchar *key,
534 gchar **eda_config_get_string_list (EdaConfig *cfg, const gchar *group, const gchar *key,
535 gsize *length, GError **err)
537 gboolean *eda_config_get_boolean_list (EdaConfig *cfg, const gchar *group, const gchar *key,
538 gsize *length, GError **err)
540 gint *eda_config_get_int_list (EdaConfig *cfg, const gchar *group, const gchar *key,
541 gsize *length, GError **err)
543 gdouble *eda_config_get_double_list (EdaConfig *cfg, const gchar *group, const gchar *key,
544 gsize *length, GError **err)
548 config-string cfg group key [default]
549 config-boolean cfg group key [default]
550 config-int cfg group key [default]
551 config-real cfg group key [default]
552 config-string-list cfg group key [default]
553 config-boolean-list cfg group key [default]
554 config-int-list cfg group key [default]
555 config-real-list cfg group key [default]
557 If a ``default`` argument is specified, it is returned in lieu of
558 raising a ``config-error`` exception if an error occurs.
560 All values returned by ``config-real`` and ``config-real-list`` are
561 real and inexact [GUILE]_, even if the on-disk representation would
562 permit an exact representation.
564 Setting configuration values
565 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
569 void eda_config_set_string (EdaConfig *cfg, const gchar *group, const gchar *key,
572 void eda_config_set_boolean (EdaConfig *cfg, const gchar *group, const gchar *key,
575 void eda_config_set_int (EdaConfig *cfg, const gchar *group, const gchar *key,
578 void eda_config_set_double (EdaConfig *cfg, const gchar *group, const gchar *key,
581 void eda_config_set_string_list (EdaConfig *cfg, const char *group, const gchar *key,
582 const gchar * const list[], gsize length)
584 void eda_config_set_boolean_list (EdaConfig *cfg, const gchar *group, const gchar *key,
585 gboolean list[], gsize length)
587 void eda_config_set_int_list (EdaConfig *cfg, const gchar *group, const gchar *key,
588 gint list[], gsize length)
590 void eda_config_set_double_list (EdaConfig *cfg, const gchar *group, const gchar *key,
591 gdouble list[], gsize length)
595 set-config! cfg group key value
597 The ``config-set!`` function infers the necessary type from the type
598 of its ``value`` argument.
603 When the value of a configuration parameter is altered, either by
604 loading a file or directly via the API, a *configuration event* is
605 generated. Handlers can be registered to be notified when a
606 configuration event occurs. Handlers can match on:
608 - A particular group and key;
610 - A particular group;
612 - or on all parameters.
614 The events are also emitted as a GObject "config-changed" signal.
619 Recall that configuration value look-ups "flow" from configuration
620 contexts to their parent contexts, i.e. up the configuration tree. In
621 a similar manner, configuration events flow *down* the configuration
622 tree, from the context where the parameter was altered to any child
623 contexts which inherit that parameter's value.
625 For example, consider a context ``parent`` which has child contexts
626 ``child1`` and ``child2``. In ``parent``, the parameter ``foo`` has
627 value ``bar``; in ``child1``, its value is ``baz``; and in ``child2``,
635 [foo=baz] child1 child2 [foo not set]
637 If the ``foo`` parameter is altered in ``parent``, then the resulting
638 event will be dispatched in ``parent`` and ``child2``.
643 In C, configuration event handlers must have the
644 ``EdaConfigEventHandlerFunc`` prototype::
646 void handler (EdaConfig *cfg, const gchar *group, const gchar *key, gpointer user_data)
648 In Scheme, a configuration event handler must be a closure that
649 accepts three arguments::
651 handler cfg group key
653 ``cfg`` is always the configuration context that received the event,
654 and the ``group`` and ``key`` identify the configuration parameter
655 that was changed. The ``user_data`` argument to the C handler is a
656 data structure provided by the API user at the time when the handler
659 Adding and removing handlers is quite straightforward:
663 void eda_config_event_add (EdaConfig *cfg, const gchar *group, const gchar *key, EdaConfigEventHandlerFunc func, gpointer user_data)
667 add-config-event! cfg func [group [key]]
669 Registers ``func`` to be called when the configuration parameter
670 identified by ``group`` and ``key`` is modified in the context
671 ``cfg``. If ``key`` is not specified (or NULL), matches on any
672 parameter in ``group``. If group is not specified (or NULL), matches
673 on any parameter. If an identical event handler was already
674 registered, does nothing.
678 void eda_config_event_remove (EdaConfig *cfg, const gchar *group, const gchar *key, EdaConfigEventHandlerFunc func, gpointer user_data)
682 remove-config-event! cfg func [group [key]]
684 Unregisters the event handler ``func`` from the context ``cfg``. The
685 ``group``, ``key``, and (in C) ``user_data`` parameters must match
686 those specified when the event handler was registered. If no matching
687 event handler was registered, does nothing.
691 void eda_config_event_remove_all (EdaConfig *cfg, EdaConfigEventHandlerFunc func, gpointer user_data)
695 remove-config-event! cfg func #t
697 Unregisters all occurrences of the event handler ``func`` from the
698 context ``cfg``, no matter what events ``func`` was set to trigger on.
699 In C, the ``user_data`` parameter must match that specified when the
700 event handler was registered.
706 It is sometimes desirable to delay configuration event notifications.
707 For example, this might be necessary if you are planning to change a
708 large number of configuration values at the same time.
712 void eda_config_event_freeze (EdaConfig *cfg)
713 void eda_config_event_thaw (EdaConfig *cfg)
715 When ``eda_config_event_freeze()`` is called on it, the context
716 ``cfg`` stops dispatching event notifications. Whenever an event
717 notification would normally be dispatched, it instead is added to an
718 internal queue. When ``eda_config_event_thaw()`` is called, any
719 queued events are dispatched. Multiple pairs of
720 ``eda_config_event_freeze()`` and ``eda_config_event_thaw()`` calls
725 delay-config-events cfg thunk
727 Call ``thunk``, delaying configuration event notifications until
728 ``thunk`` exits, either normally or non-locally.
734 .. [GLIB] The GNOME Project, "GLib Reference Manual".
735 <http://library.gnome.org/devel/glib/stable/>
737 .. [GOBJECT] The GNOME Project, "GObject Reference Manual".
738 <http://library.gnome.org/devel/glib/stable/>
740 .. [GUILE] Free Software Foundation, "GNU Guile Reference Manual", Version
742 <http://www.gnu.org/software/guile/manual/>
744 .. [XDGDIRS] W. Bastian, R. Lortie and L. Poettering, "XDG Base
745 Directory Specification", Version 0.7.
746 <http://www.freedesktop.org/wiki/Standards/basedir-spec>