2 * Copyright © 2009, 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gsettings.h"
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
46 * @short_description: a high-level API for application settings
48 * The #GSettings class provides a convenient API for storing and retrieving
49 * application settings.
51 * When creating a GSettings instance, you have to specify a schema
52 * that describes the keys in your settings and their types and default
53 * values, as well as some other information.
55 * Normally, a schema has as fixed path that determines where the settings
56 * are stored in the conceptual global tree of settings. However, schemas
57 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
58 * useful e.g. when the schema describes an 'account', and you want to be
59 * able to store a arbitrary number of accounts.
61 * Unlike other configuration systems (like GConf), GSettings does not
62 * restrict keys to basic types like strings and numbers. GSettings stores
63 * values as #GVariant, and allows any #GVariantType for keys. Key names
64 * are restricted to lowercase characters, numbers and '-'. Furthermore,
65 * the names must begin with a lowercase character, must not end
66 * with a '-', and must not contain consecutive dashes. Key names can
67 * be up to 32 characters long.
69 * Similar to GConf, the default values in GSettings schemas can be
70 * localized, but the localized values are stored in gettext catalogs
71 * and looked up with the domain that is specified in the
72 * <tag class="attribute">gettext-domain</tag> attribute of the
73 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
74 * elements and the category that is specified in the l10n attribute of the
75 * <tag class="starttag">key</tag> element.
77 * GSettings uses schemas in a compact binary form that is created
78 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
79 * utility. The input is a schema description in an XML format that can be
80 * described by the following DTD:
82 * <!ELEMENT schemalist (schema|enum)* >
83 * <!ATTLIST schemalist gettext-domain #IMPLIED >
85 * <!ELEMENT schema (key|child)* >
86 * <!ATTLIST schema id CDATA #REQUIRED
88 * gettext-domain CDATA #IMPLIED >
90 * <!-- defines an enumerated type -->
91 * <!-- each value element maps a nick to a numeric value -->
92 * <!ELEMENT enum (value*) >
93 * <!ATTLIST enum id CDATA #REQUIRED >
94 * <!ELEMENT value EMPTY >
95 * <!-- nick must be at least 2 characters long -->
96 * <!-- value must be parsable as a 32-bit integer -->
97 * <!ELEMENT value nick #REQUIRED
100 * <!ELEMENT key (default|summary?|description?|range?|choices?|aliases?) >
101 * <!-- name can only contain lowercase letters, numbers and '-' -->
102 * <!-- type must be a GVariant type string -->
103 * <!-- enum must be the id of an enum that has been defined earlier -->
104 * <!-- exactly one of enum or type must be given -->
105 * <!ATTLIST key name CDATA #REQUIRED
106 * type CDATA #IMPLIED
107 * enum CDATA #IMPLIED >
109 * <!-- the default value is specified a a serialized GVariant,
110 * i.e. you have to include the quotes when specifying a string -->
111 * <!ELEMENT default (#PCDATA) >
112 * <!-- the presence of the l10n attribute marks a default value for
113 * translation, its value is the gettext category to use -->
114 * <!-- if context is present, it specifies msgctxt to use -->
115 * <!ATTLIST default l10n (messages|time) #IMPLIED
116 * context CDATA #IMPLIED >
118 * <!ELEMENT summary (#PCDATA) >
119 * <!ELEMENT description (#PCDATA) >
121 * <!-- range is only allowed for keys with numeric type -->
122 * <!ELEMENT range EMPTY >
123 * <!-- min and max must be parseable as values of the key type and min < max -->
124 * <!ATTLIST range min CDATA #REQUIRED
125 * max CDATA #REQUIRED >
127 * <!-- choices is only allowed for keys with string or string array type -->
128 * <!ELEMENT choices (choice+) >
129 * <!-- each choice element specifies one possible value -->
130 * <!ELEMENT choice EMPTY >
131 * <!ATTLIST choice value CDATA #REQUIRED >
133 * <!-- aliases is only allowed for keys with enumerated type or with choices -->
134 * <!ELEMENT aliases (alias+) >
135 * <!-- each alias element specifies an alias for one of the possible values -->
136 * <!ELEMENT alias EMPTY >
137 * <!ATTLIST alias value CDATA #REQUIRED
138 * target CDATA #REQUIRED >
140 * <!ELEMENT child EMPTY >
141 * <!ATTLIST child name CDATA #REQUIRED
142 * schema CDATA #REQUIRED >
146 * At runtime, schemas are identified by their id (as specified
147 * in the <tag class="attribute">id</tag> attribute of the
148 * <tag class="starttag">schema</tag> element). The
149 * convention for schema ids is to use a dotted name, similar in
150 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
152 * <example><title>Default values</title>
153 * <programlisting><![CDATA[
155 * <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
157 * <key name="greeting" type="s">
158 * <default l10n="messages">"Hello, earthlings"</default>
159 * <summary>A greeting</summary>
161 * Greeting of the invading martians
165 * <key name="box" type="(ii)">
166 * <default>(20,30)</default>
171 * ]]></programlisting></example>
173 * <example><title>Ranges, choices and enumerated types</title>
174 * <programlisting><![CDATA[
178 * <value nick="first" value="1"/>
179 * <value nick="second" value="2"/>
182 * <schema id="org.gtk.test">
184 * <key name="key-with-range" type="i">
185 * <range min="1" max="100"/>
186 * <default>10</default>
189 * <key name="key-with-choices" type="s">
191 * <choice value='Elisabeth'/>
192 * <choice value='Annabeth'/>
193 * <choice value='Joe'/>
196 * <alias value="Annabeth" alias="Anna"/>
197 * <alias value="Elisabeth" alias="Beth"/>
199 * <default>'Joe'</default>
202 * <key name="enumerated-key" enum="myenum">
203 * <default>'first'</default>
208 * ]]></programlisting></example>
211 * <title>Binding</title>
213 * A very convenient feature of GSettings lets you bind #GObject properties
214 * directly to settings, using g_settings_bind(). Once a GObject property
215 * has been bound to a setting, changes on either side are automatically
216 * propagated to the other side. GSettings handles details like
217 * mapping between GObject and GVariant types, and preventing infinite
221 * This makes it very easy to hook up a preferences dialog to the
222 * underlying settings. To make this even more convenient, GSettings
223 * looks for a boolean property with the name "sensitivity" and
224 * automatically binds it to the writability of the bound setting.
225 * If this 'magic' gets in the way, it can be suppressed with the
226 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
231 struct _GSettingsPrivate
233 /* where the signals go... */
234 GMainContext *main_context;
236 GSettingsBackend *backend;
237 GSettingsSchema *schema;
242 GDelayedSettingsBackend *delayed;
257 SIGNAL_WRITABLE_CHANGE_EVENT,
258 SIGNAL_WRITABLE_CHANGED,
264 static guint g_settings_signals[N_SIGNALS];
266 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
270 g_settings_real_change_event (GSettings *settings,
277 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
279 for (i = 0; i < n_keys; i++)
280 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
281 keys[i], g_quark_to_string (keys[i]));
287 g_settings_real_writable_change_event (GSettings *settings,
290 const GQuark *keys = &key;
295 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
297 for (i = 0; i < n_keys; i++)
299 const gchar *string = g_quark_to_string (keys[i]);
301 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
303 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
311 settings_backend_changed (GSettingsBackend *backend,
316 GSettings *settings = G_SETTINGS (target);
317 gboolean ignore_this;
320 g_assert (settings->priv->backend == backend);
322 for (i = 0; key[i] == settings->priv->path[i]; i++);
324 if (settings->priv->path[i] == '\0' &&
325 g_settings_schema_has_key (settings->priv->schema, key + i))
329 quark = g_quark_from_string (key + i);
330 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
331 0, &quark, 1, &ignore_this);
336 settings_backend_path_changed (GSettingsBackend *backend,
341 GSettings *settings = G_SETTINGS (target);
342 gboolean ignore_this;
344 g_assert (settings->priv->backend == backend);
346 if (g_str_has_prefix (settings->priv->path, path))
347 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
348 0, NULL, 0, &ignore_this);
352 settings_backend_keys_changed (GSettingsBackend *backend,
355 const gchar * const *items,
358 GSettings *settings = G_SETTINGS (target);
359 gboolean ignore_this;
362 g_assert (settings->priv->backend == backend);
364 for (i = 0; settings->priv->path[i] &&
365 settings->priv->path[i] == path[i]; i++);
372 for (j = 0; items[j]; j++)
374 const gchar *item = items[j];
377 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
379 if (settings->priv->path[i + k] == '\0' &&
380 g_settings_schema_has_key (settings->priv->schema, item + k))
381 quarks[l++] = g_quark_from_string (item + k);
383 /* "256 quarks ought to be enough for anybody!"
384 * If this bites you, I'm sorry. Please file a bug.
390 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
391 0, quarks, l, &ignore_this);
396 settings_backend_writable_changed (GSettingsBackend *backend,
400 GSettings *settings = G_SETTINGS (target);
401 gboolean ignore_this;
404 g_assert (settings->priv->backend == backend);
406 for (i = 0; key[i] == settings->priv->path[i]; i++);
408 if (settings->priv->path[i] == '\0' &&
409 g_settings_schema_has_key (settings->priv->schema, key + i))
410 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
411 0, g_quark_from_string (key + i), &ignore_this);
415 settings_backend_path_writable_changed (GSettingsBackend *backend,
419 GSettings *settings = G_SETTINGS (target);
420 gboolean ignore_this;
422 g_assert (settings->priv->backend == backend);
424 if (g_str_has_prefix (settings->priv->path, path))
425 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
426 0, (GQuark) 0, &ignore_this);
429 /* Properties, Construction, Destruction {{{1 */
431 g_settings_set_property (GObject *object,
436 GSettings *settings = G_SETTINGS (object);
441 g_assert (settings->priv->schema_name == NULL);
442 settings->priv->schema_name = g_value_dup_string (value);
446 settings->priv->path = g_value_dup_string (value);
450 settings->priv->context = g_value_dup_string (value);
454 g_assert_not_reached ();
459 g_settings_get_property (GObject *object,
464 GSettings *settings = G_SETTINGS (object);
469 g_value_set_object (value, settings->priv->schema);
472 case PROP_HAS_UNAPPLIED:
473 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
477 g_assert_not_reached ();
482 g_settings_constructed (GObject *object)
484 GSettings *settings = G_SETTINGS (object);
485 const gchar *schema_path;
487 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
488 schema_path = g_settings_schema_get_path (settings->priv->schema);
490 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
491 g_error ("settings object created with schema '%s' and path '%s', but "
492 "path '%s' is specified by schema",
493 settings->priv->schema_name, settings->priv->path, schema_path);
495 if (settings->priv->path == NULL)
497 if (schema_path == NULL)
498 g_error ("attempting to create schema '%s' without a path",
499 settings->priv->schema_name);
501 settings->priv->path = g_strdup (schema_path);
504 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
505 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
506 settings->priv->main_context,
507 settings_backend_changed,
508 settings_backend_path_changed,
509 settings_backend_keys_changed,
510 settings_backend_writable_changed,
511 settings_backend_path_writable_changed);
512 g_settings_backend_subscribe (settings->priv->backend,
513 settings->priv->path);
517 g_settings_finalize (GObject *object)
519 GSettings *settings = G_SETTINGS (object);
521 g_settings_backend_unsubscribe (settings->priv->backend,
522 settings->priv->path);
523 g_main_context_unref (settings->priv->main_context);
524 g_object_unref (settings->priv->backend);
525 g_object_unref (settings->priv->schema);
526 g_free (settings->priv->schema_name);
527 g_free (settings->priv->context);
528 g_free (settings->priv->path);
532 g_settings_init (GSettings *settings)
534 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
538 settings->priv->main_context = g_main_context_get_thread_default ();
540 if (settings->priv->main_context == NULL)
541 settings->priv->main_context = g_main_context_default ();
543 g_main_context_ref (settings->priv->main_context);
547 g_settings_class_init (GSettingsClass *class)
549 GObjectClass *object_class = G_OBJECT_CLASS (class);
551 class->writable_change_event = g_settings_real_writable_change_event;
552 class->change_event = g_settings_real_change_event;
554 object_class->set_property = g_settings_set_property;
555 object_class->get_property = g_settings_get_property;
556 object_class->constructed = g_settings_constructed;
557 object_class->finalize = g_settings_finalize;
559 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
562 * GSettings::changed:
563 * @settings: the object on which the signal was emitted
564 * @key: the name of the key that changed
566 * The "changed" signal is emitted when a key has potentially changed.
567 * You should call one of the g_settings_get() calls to check the new
570 * This signal supports detailed connections. You can connect to the
571 * detailed signal "changed::x" in order to only receive callbacks
572 * when key "x" changes.
574 g_settings_signals[SIGNAL_CHANGED] =
575 g_signal_new ("changed", G_TYPE_SETTINGS,
576 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
577 G_STRUCT_OFFSET (GSettingsClass, changed),
578 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
579 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
582 * GSettings::change-event:
583 * @settings: the object on which the signal was emitted
584 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
585 * @n_keys: the length of the @keys array, or 0
586 * @returns: %TRUE to stop other handlers from being invoked for the
587 * event. FALSE to propagate the event further.
589 * The "change-event" signal is emitted once per change event that
590 * affects this settings object. You should connect to this signal
591 * only if you are interested in viewing groups of changes before they
592 * are split out into multiple emissions of the "changed" signal.
593 * For most use cases it is more appropriate to use the "changed" signal.
595 * In the event that the change event applies to one or more specified
596 * keys, @keys will be an array of #GQuark of length @n_keys. In the
597 * event that the change event applies to the #GSettings object as a
598 * whole (ie: potentially every key has been changed) then @keys will
599 * be %NULL and @n_keys will be 0.
601 * The default handler for this signal invokes the "changed" signal
602 * for each affected key. If any other connected handler returns
603 * %TRUE then this default functionality will be supressed.
605 g_settings_signals[SIGNAL_CHANGE_EVENT] =
606 g_signal_new ("change-event", G_TYPE_SETTINGS,
608 G_STRUCT_OFFSET (GSettingsClass, change_event),
609 g_signal_accumulator_true_handled, NULL,
610 _gio_marshal_BOOL__POINTER_INT,
611 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
614 * GSettings::writable-changed:
615 * @settings: the object on which the signal was emitted
618 * The "writable-changed" signal is emitted when the writability of a
619 * key has potentially changed. You should call
620 * g_settings_is_writable() in order to determine the new status.
622 * This signal supports detailed connections. You can connect to the
623 * detailed signal "writable-changed::x" in order to only receive
624 * callbacks when the writability of "x" changes.
626 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
627 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
628 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
629 G_STRUCT_OFFSET (GSettingsClass, changed),
630 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
631 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
634 * GSettings::writable-change-event:
635 * @settings: the object on which the signal was emitted
636 * @key: the quark of the key, or 0
637 * @returns: %TRUE to stop other handlers from being invoked for the
638 * event. FALSE to propagate the event further.
640 * The "writable-change-event" signal is emitted once per writability
641 * change event that affects this settings object. You should connect
642 * to this signal if you are interested in viewing groups of changes
643 * before they are split out into multiple emissions of the
644 * "writable-changed" signal. For most use cases it is more
645 * appropriate to use the "writable-changed" signal.
647 * In the event that the writability change applies only to a single
648 * key, @key will be set to the #GQuark for that key. In the event
649 * that the writability change affects the entire settings object,
652 * The default handler for this signal invokes the "writable-changed"
653 * and "changed" signals for each affected key. This is done because
654 * changes in writability might also imply changes in value (if for
655 * example, a new mandatory setting is introduced). If any other
656 * connected handler returns %TRUE then this default functionality
659 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
660 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
662 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
663 g_signal_accumulator_true_handled, NULL,
664 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
669 * The name of the context that the settings are stored in.
671 g_object_class_install_property (object_class, PROP_CONTEXT,
672 g_param_spec_string ("context",
674 P_("The name of the context for this settings object"),
675 "", G_PARAM_CONSTRUCT_ONLY |
676 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 * The name of the schema that describes the types of keys
682 * for this #GSettings object.
684 g_object_class_install_property (object_class, PROP_SCHEMA,
685 g_param_spec_string ("schema",
687 P_("The name of the schema for this settings object"),
689 G_PARAM_CONSTRUCT_ONLY |
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * The path within the backend where the settings are stored.
697 g_object_class_install_property (object_class, PROP_PATH,
698 g_param_spec_string ("path",
700 P_("The path within the backend where the settings are"),
702 G_PARAM_CONSTRUCT_ONLY |
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
706 * GSettings:has-unapplied:
708 * If this property is %TRUE, the #GSettings object has outstanding
709 * changes that will be applied when g_settings_apply() is called.
711 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
712 g_param_spec_boolean ("has-unapplied",
713 P_("Has unapplied changes"),
714 P_("TRUE if there are outstanding changes to apply()"),
716 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
720 /* Construction (new, new_with_path, etc.) {{{1 */
723 * @schema: the name of the schema
724 * @returns: a new #GSettings object
726 * Creates a new #GSettings object with a given schema.
728 * Signals on the newly created #GSettings object will be dispatched
729 * via the thread-default #GMainContext in effect at the time of the
730 * call to g_settings_new(). The new #GSettings will hold a reference
731 * on the context. See g_main_context_push_thread_default().
736 g_settings_new (const gchar *schema)
738 g_return_val_if_fail (schema != NULL, NULL);
740 return g_object_new (G_TYPE_SETTINGS,
746 * g_settings_new_with_path:
747 * @schema: the name of the schema
748 * @path: the path to use
749 * @returns: a new #GSettings object
751 * Creates a new #GSettings object with a given schema and path.
753 * You only need to do this if you want to directly create a settings
754 * object with a schema that doesn't have a specified path of its own.
757 * It is a programmer error to call this function for a schema that
758 * has an explicitly specified path.
763 g_settings_new_with_path (const gchar *schema,
766 g_return_val_if_fail (schema != NULL, NULL);
767 g_return_val_if_fail (path != NULL, NULL);
769 return g_object_new (G_TYPE_SETTINGS,
776 * g_settings_new_with_context:
777 * @schema: the name of the schema
778 * @context: the context to use
779 * @returns: a new #GSettings object
781 * Creates a new #GSettings object with a given schema and context.
783 * Creating settings objects with a context allow accessing settings
784 * from a database other than the usual one. For example, it may make
785 * sense to specify "defaults" in order to get a settings object that
786 * modifies the system default settings instead of the settings for this
789 * It is a programmer error to call this function for an unsupported
790 * context. Use g_settings_supports_context() to determine if a context
791 * is supported if you are unsure.
796 g_settings_new_with_context (const gchar *schema,
797 const gchar *context)
799 g_return_val_if_fail (schema != NULL, NULL);
800 g_return_val_if_fail (context != NULL, NULL);
802 return g_object_new (G_TYPE_SETTINGS,
809 * g_settings_new_with_context_and_path:
810 * @schema: the name of the schema
811 * @path: the path to use
812 * @returns: a new #GSettings object
814 * Creates a new #GSettings object with a given schema, context and
817 * This is a mix of g_settings_new_with_context() and
818 * g_settings_new_with_path().
823 g_settings_new_with_context_and_path (const gchar *schema,
824 const gchar *context,
827 g_return_val_if_fail (schema != NULL, NULL);
828 g_return_val_if_fail (context != NULL, NULL);
829 g_return_val_if_fail (path != NULL, NULL);
831 return g_object_new (G_TYPE_SETTINGS,
838 /* Internal read/write utilities, enum conversion, validation {{{1 */
844 GSettingsSchema *schema;
847 const guint32 *strinfo;
848 gsize strinfo_length;
850 const gchar *unparsed;
853 const GVariantType *type;
854 GVariant *minimum, *maximum;
855 GVariant *default_value;
859 g_settings_get_key_info (GSettingsKeyInfo *info,
867 memset (info, 0, sizeof *info);
869 iter = g_settings_schema_get_value (settings->priv->schema, key);
871 info->default_value = g_variant_iter_next_value (iter);
872 info->type = g_variant_get_type (info->default_value);
873 info->schema = settings->priv->schema;
874 info->settings = settings;
877 while (g_variant_iter_next (iter, "(y*)", &code, &data))
882 /* translation requested */
883 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
887 /* enumerated types, ... */
888 info->is_enum = TRUE;
890 /* ..., choices, aliases */
891 info->strinfo = g_variant_get_fixed_array (data,
892 &info->strinfo_length,
897 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
901 g_warning ("unknown schema extension '%c'", code);
905 g_variant_unref (data);
908 g_variant_iter_free (iter);
912 g_settings_free_key_info (GSettingsKeyInfo *info)
915 g_variant_unref (info->minimum);
918 g_variant_unref (info->maximum);
920 g_variant_unref (info->default_value);
924 g_settings_write_to_backend (GSettingsKeyInfo *info,
930 path = g_strconcat (info->settings->priv->path, info->key, NULL);
931 success = g_settings_backend_write (info->settings->priv->backend,
939 g_settings_type_check (GSettingsKeyInfo *info,
942 g_return_val_if_fail (value != NULL, FALSE);
944 return g_variant_is_of_type (value, info->type);
948 g_settings_range_check (GSettingsKeyInfo *info,
951 if (info->minimum == NULL && info->strinfo == NULL)
954 if (g_variant_is_container (value))
960 g_variant_iter_init (&iter, value);
961 while (ok && (child = g_variant_iter_next_value (&iter)))
963 ok = g_settings_range_check (info, value);
964 g_variant_unref (child);
972 return g_variant_compare (info->minimum, value) <= 0 &&
973 g_variant_compare (value, info->maximum) <= 0;
976 return strinfo_is_string_valid (info->strinfo,
977 info->strinfo_length,
978 g_variant_get_string (value, NULL));
982 g_settings_range_fixup (GSettingsKeyInfo *info,
987 if (g_settings_range_check (info, value))
988 return g_variant_ref (value);
990 if (info->strinfo == NULL)
993 if (g_variant_is_container (value))
995 GVariantBuilder builder;
999 g_variant_builder_init (&builder, g_variant_get_type (value));
1001 while ((child = g_variant_iter_next_value (&iter)))
1005 fixed = g_settings_range_fixup (info, child);
1006 g_variant_unref (child);
1010 g_variant_builder_clear (&builder);
1014 g_variant_builder_add_value (&builder, fixed);
1015 g_variant_unref (fixed);
1018 return g_variant_ref_sink (g_variant_builder_end (&builder));
1021 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1022 g_variant_get_string (value, NULL));
1023 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1027 g_settings_read_from_backend (GSettingsKeyInfo *info)
1033 path = g_strconcat (info->settings->priv->path, info->key, NULL);
1034 value = g_settings_backend_read (info->settings->priv->backend,
1035 path, info->type, FALSE);
1040 fixup = g_settings_range_fixup (info, value);
1041 g_variant_unref (value);
1050 g_settings_get_translated_default (GSettingsKeyInfo *info)
1052 const gchar *translated;
1053 GError *error = NULL;
1054 const gchar *domain;
1057 if (info->lc_char == '\0')
1058 /* translation not requested for this key */
1061 domain = g_settings_schema_get_gettext_domain (info->schema);
1063 if (info->lc_char == 't')
1064 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1066 translated = g_dgettext (domain, info->unparsed);
1068 if (translated == info->unparsed)
1069 /* the default value was not translated */
1072 /* try to parse the translation of the unparsed default */
1073 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1077 g_warning ("Failed to parse translated string `%s' for "
1078 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1079 info->settings->priv->schema_name, error->message);
1080 g_warning ("Using untranslated default instead.");
1081 g_error_free (error);
1084 else if (!g_settings_range_check (info, value))
1086 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1087 "is outside of valid range", info->unparsed, info->key,
1088 info->settings->priv->schema_name);
1089 g_variant_unref (value);
1097 g_settings_to_enum (GSettingsKeyInfo *info,
1103 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1104 g_variant_get_string (value, NULL),
1107 /* 'value' can only come from the backend after being filtered for validity,
1108 * from the translation after being filtered for validity, or from the schema
1109 * itself (which the schema compiler checks for validity). If this assertion
1110 * fails then it's really a bug in GSettings or the schema compiler...
1112 g_assert (it_worked);
1118 g_settings_from_enum (GSettingsKeyInfo *info,
1121 const gchar *string;
1123 string = strinfo_string_from_enum (info->strinfo,
1124 info->strinfo_length,
1130 return g_variant_ref_sink (g_variant_new_string (string));
1133 /* Public Get/Set API {{{1 (get, get_value, set, set_value) */
1135 * g_settings_get_value:
1136 * @settings: a #GSettings object
1137 * @key: the key to get the value for
1138 * @returns: a new #GVariant
1140 * Gets the value that is stored in @settings for @key.
1142 * It is a programmer error to give a @key that isn't valid for
1148 g_settings_get_value (GSettings *settings,
1151 GSettingsKeyInfo info;
1154 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1155 g_return_val_if_fail (key != NULL, NULL);
1157 g_settings_get_key_info (&info, settings, key);
1158 value = g_settings_read_from_backend (&info);
1161 value = g_settings_get_translated_default (&info);
1164 value = g_variant_ref (info.default_value);
1166 g_settings_free_key_info (&info);
1172 * g_settings_get_enum:
1173 * @settings: a #GSettings object
1174 * @key: the key to get the value for
1175 * @returns: the enum value
1177 * Gets the value that is stored in @settings for @key and converts it
1178 * to the enum value that it represents.
1180 * In order to use this function the type of the value must be a string
1181 * and it must be marked in the schema file as an enumerated type.
1183 * It is a programmer error to give a @key that isn't valid for
1184 * @settings, or is not marked as an enumerated type in the schema.
1186 * If the value stored in the configuration database is not a valid
1187 * value for the enumerated type then this function will return the
1193 g_settings_get_enum (GSettings *settings,
1196 GSettingsKeyInfo info;
1200 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1201 g_return_val_if_fail (key != NULL, -1);
1203 g_settings_get_key_info (&info, settings, key);
1207 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1208 "associated with an enumerated type", info.key);
1209 g_settings_free_key_info (&info);
1213 value = g_settings_read_from_backend (&info);
1216 value = g_settings_get_translated_default (&info);
1219 value = g_variant_ref (info.default_value);
1221 result = g_settings_to_enum (&info, value);
1222 g_settings_free_key_info (&info);
1223 g_variant_unref (value);
1229 * g_settings_set_enum:
1230 * @settings: a #GSettings object
1231 * @key: a key, within @settings
1232 * @value: an enumerated value
1233 * Returns: %TRUE, if the set succeeds
1235 * Looks up the enumerated type nick for @value and writes it to @key,
1238 * It is a programmer error for @key to not be listed in the schema or
1239 * for it not to be tagged as an enumerated type, or for @value not to
1240 * be a valid value for the named type.
1242 * After performing the write, accessing @key directly
1243 * g_settings_get_string() will return the 'nick' associated with
1247 g_settings_set_enum (GSettings *settings,
1251 GSettingsKeyInfo info;
1255 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1256 g_return_val_if_fail (key != NULL, FALSE);
1258 g_settings_get_key_info (&info, settings, key);
1262 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1263 "associated with an enumerated type", info.key);
1267 if (!(variant = g_settings_from_enum (&info, value)))
1269 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1270 "in schema `%s'. Doing nothing.", value, info.key,
1271 info.settings->priv->schema_name);
1272 g_settings_free_key_info (&info);
1276 success = g_settings_write_to_backend (&info, variant);
1277 g_settings_free_key_info (&info);
1278 g_variant_unref (variant);
1284 * g_settings_set_value:
1285 * @settings: a #GSettings object
1286 * @key: the name of the key to set
1287 * @value: a #GVariant of the correct type
1288 * @returns: %TRUE if setting the key succeeded,
1289 * %FALSE if the key was not writable
1291 * Sets @key in @settings to @value.
1293 * It is a programmer error to give a @key that isn't valid for
1294 * @settings. It is a programmer error to give a @value of the
1297 * If @value is floating then this function consumes the reference.
1302 g_settings_set_value (GSettings *settings,
1306 GSettingsKeyInfo info;
1308 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1309 g_return_val_if_fail (key != NULL, FALSE);
1311 g_settings_get_key_info (&info, settings, key);
1312 g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1313 g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1314 g_settings_free_key_info (&info);
1316 return g_settings_write_to_backend (&info, value);
1321 * @settings: a #GSettings object
1322 * @key: the key to get the value for
1323 * @format: a #GVariant format string
1324 * @...: arguments as per @format
1326 * Gets the value that is stored at @key in @settings.
1328 * A convenience function that combines g_settings_get_value() with
1331 * It is a programmer error to pass a @key that isn't valid for
1332 * @settings or a @format_string that doesn't match the type of @key according
1333 * to the schema of @settings.
1338 g_settings_get (GSettings *settings,
1340 const gchar *format,
1346 value = g_settings_get_value (settings, key);
1348 va_start (ap, format);
1349 g_variant_get_va (value, format, NULL, &ap);
1352 g_variant_unref (value);
1357 * @settings: a #GSettings object
1358 * @key: the name of the key to set
1359 * @format: a #GVariant format string
1360 * @...: arguments as per @format
1361 * @returns: %TRUE if setting the key succeeded,
1362 * %FALSE if the key was not writable
1364 * Sets @key in @settings to @value.
1366 * A convenience function that combines g_settings_set_value() with
1369 * It is a programmer error to pass a @key that isn't valid for
1370 * @settings or a @format that doesn't match the type of @key according
1371 * to the schema of @settings.
1376 g_settings_set (GSettings *settings,
1378 const gchar *format,
1384 va_start (ap, format);
1385 value = g_variant_new_va (format, NULL, &ap);
1388 return g_settings_set_value (settings, key, value);
1391 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1393 * g_settings_get_string:
1394 * @settings: a #GSettings object
1395 * @key: the key to get the value for
1396 * @returns: a newly-allocated string
1398 * Gets the value that is stored at @key in @settings.
1400 * A convenience variant of g_settings_get() for strings.
1402 * It is a programmer error to pass a @key that isn't valid for
1403 * @settings or is not of type string.
1408 g_settings_get_string (GSettings *settings,
1414 value = g_settings_get_value (settings, key);
1415 result = g_variant_dup_string (value, NULL);
1416 g_variant_unref (value);
1422 * g_settings_set_string:
1423 * @settings: a #GSettings object
1424 * @key: the name of the key to set
1425 * @value: the value to set it to
1426 * @returns: %TRUE if setting the key succeeded,
1427 * %FALSE if the key was not writable
1429 * Sets @key in @settings to @value.
1431 * A convenience variant of g_settings_set() for strings.
1433 * It is a programmer error to pass a @key that isn't valid for
1434 * @settings or is not of type string.
1439 g_settings_set_string (GSettings *settings,
1443 return g_settings_set_value (settings, key, g_variant_new_string (value));
1447 * g_settings_get_int:
1448 * @settings: a #GSettings object
1449 * @key: the key to get the value for
1450 * @returns: an integer
1452 * Gets the value that is stored at @key in @settings.
1454 * A convenience variant of g_settings_get() for 32-bit integers.
1456 * It is a programmer error to pass a @key that isn't valid for
1457 * @settings or is not of type int32.
1462 g_settings_get_int (GSettings *settings,
1468 value = g_settings_get_value (settings, key);
1469 result = g_variant_get_int32 (value);
1470 g_variant_unref (value);
1476 * g_settings_set_int:
1477 * @settings: a #GSettings object
1478 * @key: the name of the key to set
1479 * @value: the value to set it to
1480 * @returns: %TRUE if setting the key succeeded,
1481 * %FALSE if the key was not writable
1483 * Sets @key in @settings to @value.
1485 * A convenience variant of g_settings_set() for 32-bit integers.
1487 * It is a programmer error to pass a @key that isn't valid for
1488 * @settings or is not of type int32.
1493 g_settings_set_int (GSettings *settings,
1497 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1501 * g_settings_get_double:
1502 * @settings: a #GSettings object
1503 * @key: the key to get the value for
1504 * @returns: a double
1506 * Gets the value that is stored at @key in @settings.
1508 * A convenience variant of g_settings_get() for doubles.
1510 * It is a programmer error to pass a @key that isn't valid for
1511 * @settings or is not of type double.
1516 g_settings_get_double (GSettings *settings,
1522 value = g_settings_get_value (settings, key);
1523 result = g_variant_get_double (value);
1524 g_variant_unref (value);
1530 * g_settings_set_double:
1531 * @settings: a #GSettings object
1532 * @key: the name of the key to set
1533 * @value: the value to set it to
1534 * @returns: %TRUE if setting the key succeeded,
1535 * %FALSE if the key was not writable
1537 * Sets @key in @settings to @value.
1539 * A convenience variant of g_settings_set() for doubles.
1541 * It is a programmer error to pass a @key that isn't valid for
1542 * @settings or is not of type double.
1547 g_settings_set_double (GSettings *settings,
1551 return g_settings_set_value (settings, key, g_variant_new_double (value));
1555 * g_settings_get_boolean:
1556 * @settings: a #GSettings object
1557 * @key: the key to get the value for
1558 * @returns: a boolean
1560 * Gets the value that is stored at @key in @settings.
1562 * A convenience variant of g_settings_get() for booleans.
1564 * It is a programmer error to pass a @key that isn't valid for
1565 * @settings or is not of type boolean.
1570 g_settings_get_boolean (GSettings *settings,
1576 value = g_settings_get_value (settings, key);
1577 result = g_variant_get_boolean (value);
1578 g_variant_unref (value);
1584 * g_settings_set_boolean:
1585 * @settings: a #GSettings object
1586 * @key: the name of the key to set
1587 * @value: the value to set it to
1588 * @returns: %TRUE if setting the key succeeded,
1589 * %FALSE if the key was not writable
1591 * Sets @key in @settings to @value.
1593 * A convenience variant of g_settings_set() for booleans.
1595 * It is a programmer error to pass a @key that isn't valid for
1596 * @settings or is not of type boolean.
1601 g_settings_set_boolean (GSettings *settings,
1605 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1609 * g_settings_get_strv:
1610 * @settings: a #GSettings object
1611 * @key: the key to get the value for
1612 * @returns: a newly-allocated, %NULL-terminated array of strings
1614 * Gets the value that is stored at @key in @settings.
1616 * A convenience variant of g_settings_get() for string arrays.
1618 * It is a programmer error to pass a @key that isn't valid for
1619 * @settings or is not of type 'string array'.
1624 g_settings_get_strv (GSettings *settings,
1630 value = g_settings_get_value (settings, key);
1631 result = g_variant_dup_strv (value, NULL);
1632 g_variant_unref (value);
1638 * g_settings_set_strv:
1639 * @settings: a #GSettings object
1640 * @key: the name of the key to set
1641 * @value: (allow-none): the value to set it to, or %NULL
1642 * @returns: %TRUE if setting the key succeeded,
1643 * %FALSE if the key was not writable
1645 * Sets @key in @settings to @value.
1647 * A convenience variant of g_settings_set() for string arrays. If
1648 * @value is %NULL, then @key is set to be the empty array.
1650 * It is a programmer error to pass a @key that isn't valid for
1651 * @settings or is not of type 'string array'.
1656 g_settings_set_strv (GSettings *settings,
1658 const gchar * const *value)
1663 array = g_variant_new_strv (value, -1);
1665 array = g_variant_new_strv (NULL, 0);
1667 return g_settings_set_value (settings, key, array);
1669 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1672 * @settings: a #GSettings object
1674 * Changes the #GSettings object into 'delay-apply' mode. In this
1675 * mode, changes to @settings are not immediately propagated to the
1676 * backend, but kept locally until g_settings_apply() is called.
1681 g_settings_delay (GSettings *settings)
1683 g_return_if_fail (G_IS_SETTINGS (settings));
1685 if (settings->priv->delayed)
1688 settings->priv->delayed =
1689 g_delayed_settings_backend_new (settings->priv->backend,
1691 settings->priv->main_context);
1692 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1693 g_object_unref (settings->priv->backend);
1695 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1696 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1697 settings->priv->main_context,
1698 settings_backend_changed,
1699 settings_backend_path_changed,
1700 settings_backend_keys_changed,
1701 settings_backend_writable_changed,
1702 settings_backend_path_writable_changed);
1707 * @settings: a #GSettings instance
1709 * Applies any changes that have been made to the settings. This
1710 * function does nothing unless @settings is in 'delay-apply' mode;
1711 * see g_settings_set_delay_apply(). In the normal case settings are
1712 * always applied immediately.
1715 g_settings_apply (GSettings *settings)
1717 if (settings->priv->delayed)
1719 GDelayedSettingsBackend *delayed;
1721 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1722 g_delayed_settings_backend_apply (delayed);
1727 * g_settings_revert:
1728 * @settings: a #GSettings instance
1730 * Reverts all non-applied changes to the settings. This function
1731 * does nothing unless @settings is in 'delay-apply' mode; see
1732 * g_settings_set_delay_apply(). In the normal case settings are
1733 * always applied immediately.
1735 * Change notifications will be emitted for affected keys.
1738 g_settings_revert (GSettings *settings)
1740 if (settings->priv->delayed)
1742 GDelayedSettingsBackend *delayed;
1744 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1745 g_delayed_settings_backend_revert (delayed);
1750 * g_settings_get_has_unapplied:
1751 * @settings: a #GSettings object
1752 * @returns: %TRUE if @settings has unapplied changes
1754 * Returns whether the #GSettings object has any unapplied
1755 * changes. This can only be the case if it is in 'delayed-apply' mode.
1760 g_settings_get_has_unapplied (GSettings *settings)
1762 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1764 return settings->priv->delayed &&
1765 g_delayed_settings_backend_get_has_unapplied (
1766 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1769 /* Extra API (sync, get_child, is_writable) {{{1 */
1772 * @context: the context to sync, or %NULL
1774 * Ensures that all pending operations for the given context are
1777 * Writes made to a #GSettings are handled asynchronously. For this
1778 * reason, it is very unlikely that the changes have it to disk by the
1779 * time g_settings_set() returns.
1781 * This call will block until all of the writes have made it to the
1782 * backend. Since the mainloop is not running, no change notifications
1783 * will be dispatched during this call (but some may be queued by the
1784 * time the call is done).
1787 g_settings_sync (const gchar *context)
1789 GSettingsBackend *backend;
1791 if (context == NULL)
1794 backend = g_settings_backend_get_with_context (context);
1795 g_settings_backend_sync (backend);
1799 * g_settings_is_writable:
1800 * @settings: a #GSettings object
1801 * @name: the name of a key
1802 * @returns: %TRUE if the key @name is writable
1804 * Finds out if a key can be written or not
1809 g_settings_is_writable (GSettings *settings,
1815 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1817 path = g_strconcat (settings->priv->path, name, NULL);
1818 writable = g_settings_backend_get_writable (settings->priv->backend, path);
1825 * g_settings_get_child:
1826 * @settings: a #GSettings object
1827 * @name: the name of the 'child' schema
1828 * @returns: a 'child' settings object
1830 * Creates a 'child' settings object which has a base path of
1831 * <replaceable>base-path</replaceable>/@name", where
1832 * <replaceable>base-path</replaceable> is the base path of @settings.
1834 * The schema for the child settings object must have been declared
1835 * in the schema of @settings using a <tag class="starttag">child</tag> element.
1840 g_settings_get_child (GSettings *settings,
1843 const gchar *child_schema;
1848 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1850 child_name = g_strconcat (name, "/", NULL);
1851 child_schema = g_settings_schema_get_string (settings->priv->schema,
1853 if (child_schema == NULL)
1854 g_error ("Schema '%s' has no child '%s'",
1855 settings->priv->schema_name, name);
1857 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1858 child = g_object_new (G_TYPE_SETTINGS,
1859 "schema", child_schema,
1862 g_free (child_path);
1863 g_free (child_name);
1871 GSettings *settings;
1874 GSettingsBindGetMapping get_mapping;
1875 GSettingsBindSetMapping set_mapping;
1877 GDestroyNotify destroy;
1879 guint writable_handler_id;
1880 guint property_handler_id;
1881 const GParamSpec *property;
1882 guint key_handler_id;
1886 /* prevent recursion */
1891 g_settings_binding_free (gpointer data)
1893 GSettingsBinding *binding = data;
1895 g_assert (!binding->running);
1897 if (binding->writable_handler_id)
1898 g_signal_handler_disconnect (binding->settings,
1899 binding->writable_handler_id);
1901 if (binding->key_handler_id)
1902 g_signal_handler_disconnect (binding->settings,
1903 binding->key_handler_id);
1905 if (g_signal_handler_is_connected (binding->object,
1906 binding->property_handler_id))
1907 g_signal_handler_disconnect (binding->object,
1908 binding->property_handler_id);
1910 g_variant_type_free (binding->type);
1911 g_object_unref (binding->settings);
1913 if (binding->destroy)
1914 binding->destroy (binding->user_data);
1916 g_slice_free (GSettingsBinding, binding);
1920 g_settings_binding_quark (const char *property)
1925 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1926 quark = g_quark_from_string (tmp);
1933 g_settings_binding_key_changed (GSettings *settings,
1937 GSettingsBinding *binding = user_data;
1938 GValue value = { 0, };
1941 g_assert (settings == binding->settings);
1942 g_assert (key == binding->key);
1944 if (binding->running)
1947 binding->running = TRUE;
1949 g_value_init (&value, binding->property->value_type);
1950 variant = g_settings_get_value (settings, key);
1951 if (binding->get_mapping (&value, variant, binding->user_data))
1952 g_object_set_property (binding->object,
1953 binding->property->name,
1955 g_variant_unref (variant);
1956 g_value_unset (&value);
1958 binding->running = FALSE;
1962 g_settings_binding_property_changed (GObject *object,
1963 const GParamSpec *pspec,
1966 GSettingsBinding *binding = user_data;
1967 GValue value = { 0, };
1970 g_assert (object == binding->object);
1971 g_assert (pspec == binding->property);
1973 if (binding->running)
1976 binding->running = TRUE;
1978 g_value_init (&value, pspec->value_type);
1979 g_object_get_property (object, pspec->name, &value);
1980 if ((variant = binding->set_mapping (&value, binding->type,
1981 binding->user_data)))
1983 g_settings_set_value (binding->settings,
1985 g_variant_ref_sink (variant));
1986 g_variant_unref (variant);
1988 g_value_unset (&value);
1990 binding->running = FALSE;
1995 * @settings: a #GSettings object
1996 * @key: the key to bind
1997 * @object: a #GObject
1998 * @property: the name of the property to bind
1999 * @flags: flags for the binding
2001 * Create a binding between the @key in the @settings object
2002 * and the property @property of @object.
2004 * The binding uses the default GIO mapping functions to map
2005 * between the settings and property values. These functions
2006 * handle booleans, numeric types and string types in a
2007 * straightforward way. Use g_settings_bind_with_mapping() if
2008 * you need a custom mapping, or map between types that are not
2009 * supported by the default mapping functions.
2011 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2012 * function also establishes a binding between the writability of
2013 * @key and the "sensitive" property of @object (if @object has
2014 * a boolean property by that name). See g_settings_bind_writable()
2015 * for more details about writable bindings.
2017 * Note that the lifecycle of the binding is tied to the object,
2018 * and that you can have only one binding per object property.
2019 * If you bind the same property twice on the same object, the second
2020 * binding overrides the first one.
2025 g_settings_bind (GSettings *settings,
2028 const gchar *property,
2029 GSettingsBindFlags flags)
2031 g_settings_bind_with_mapping (settings, key, object, property,
2032 flags, NULL, NULL, NULL, NULL);
2036 * g_settings_bind_with_mapping:
2037 * @settings: a #GSettings object
2038 * @key: the key to bind
2039 * @object: a #GObject
2040 * @property: the name of the property to bind
2041 * @flags: flags for the binding
2042 * @get_mapping: a function that gets called to convert values
2043 * from @settings to @object, or %NULL to use the default GIO mapping
2044 * @set_mapping: a function that gets called to convert values
2045 * from @object to @settings, or %NULL to use the default GIO mapping
2046 * @user_data: data that gets passed to @get_mapping and @set_mapping
2047 * @destroy: #GDestroyNotify function for @user_data
2049 * Create a binding between the @key in the @settings object
2050 * and the property @property of @object.
2052 * The binding uses the provided mapping functions to map between
2053 * settings and property values.
2055 * Note that the lifecycle of the binding is tied to the object,
2056 * and that you can have only one binding per object property.
2057 * If you bind the same property twice on the same object, the second
2058 * binding overrides the first one.
2063 g_settings_bind_with_mapping (GSettings *settings,
2066 const gchar *property,
2067 GSettingsBindFlags flags,
2068 GSettingsBindGetMapping get_mapping,
2069 GSettingsBindSetMapping set_mapping,
2071 GDestroyNotify destroy)
2073 GSettingsBinding *binding;
2074 GObjectClass *objectclass;
2075 gchar *detailed_signal;
2076 GQuark binding_quark;
2078 g_return_if_fail (G_IS_SETTINGS (settings));
2080 objectclass = G_OBJECT_GET_CLASS (object);
2082 binding = g_slice_new0 (GSettingsBinding);
2083 binding->settings = g_object_ref (settings);
2084 binding->object = object;
2085 binding->key = g_intern_string (key);
2086 binding->property = g_object_class_find_property (objectclass, property);
2087 binding->user_data = user_data;
2088 binding->destroy = destroy;
2089 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2090 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2092 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2093 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2095 if (binding->property == NULL)
2097 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2098 property, G_OBJECT_TYPE_NAME (object));
2102 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
2104 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
2105 property, G_OBJECT_TYPE_NAME (object));
2108 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
2110 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
2111 property, G_OBJECT_TYPE_NAME (object));
2119 iter = g_settings_schema_get_value (settings->priv->schema, key);
2120 value = g_variant_iter_next_value (iter);
2121 binding->type = g_variant_type_copy (g_variant_get_type (value));
2122 g_variant_iter_free (iter);
2123 g_variant_unref (value);
2126 if (binding->type == NULL)
2128 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
2129 key, settings->priv->schema_name);
2133 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2134 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2135 !g_settings_mapping_is_compatible (binding->property->value_type,
2138 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2139 "'%s' which is not compatible with type '%s' of key '%s' "
2140 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2141 g_type_name (binding->property->value_type),
2142 g_variant_type_dup_string (binding->type), key,
2143 settings->priv->schema_name);
2147 if ((flags & G_SETTINGS_BIND_SET) &&
2148 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2150 GParamSpec *sensitive;
2152 sensitive = g_object_class_find_property (objectclass, "sensitive");
2154 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2155 (sensitive->flags & G_PARAM_WRITABLE))
2156 g_settings_bind_writable (settings, binding->key,
2157 object, "sensitive", FALSE);
2160 if (flags & G_SETTINGS_BIND_SET)
2162 detailed_signal = g_strdup_printf ("notify::%s", property);
2163 binding->property_handler_id =
2164 g_signal_connect (object, detailed_signal,
2165 G_CALLBACK (g_settings_binding_property_changed),
2167 g_free (detailed_signal);
2169 if (~flags & G_SETTINGS_BIND_GET)
2170 g_settings_binding_property_changed (object,
2175 if (flags & G_SETTINGS_BIND_GET)
2177 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2179 detailed_signal = g_strdup_printf ("changed::%s", key);
2180 binding->key_handler_id =
2181 g_signal_connect (settings, detailed_signal,
2182 G_CALLBACK (g_settings_binding_key_changed),
2184 g_free (detailed_signal);
2187 g_settings_binding_key_changed (settings, binding->key, binding);
2190 binding_quark = g_settings_binding_quark (property);
2191 g_object_set_qdata_full (object, binding_quark,
2192 binding, g_settings_binding_free);
2195 /* Writability binding {{{1 */
2198 GSettings *settings;
2201 const gchar *property;
2204 } GSettingsWritableBinding;
2207 g_settings_writable_binding_free (gpointer data)
2209 GSettingsWritableBinding *binding = data;
2211 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2212 g_object_unref (binding->settings);
2213 g_slice_free (GSettingsWritableBinding, binding);
2217 g_settings_binding_writable_changed (GSettings *settings,
2221 GSettingsWritableBinding *binding = user_data;
2224 g_assert (settings == binding->settings);
2225 g_assert (key == binding->key);
2227 writable = g_settings_is_writable (settings, key);
2229 if (binding->inverted)
2230 writable = !writable;
2232 g_object_set (binding->object, binding->property, writable, NULL);
2236 * g_settings_bind_writable:
2237 * @settings: a #GSettings object
2238 * @key: the key to bind
2239 * @object: a #GObject
2240 * @property: the name of a boolean property to bind
2241 * @inverted: whether to 'invert' the value
2243 * Create a binding between the writability of @key in the
2244 * @settings object and the property @property of @object.
2245 * The property must be boolean; "sensitive" or "visible"
2246 * properties of widgets are the most likely candidates.
2248 * Writable bindings are always uni-directional; changes of the
2249 * writability of the setting will be propagated to the object
2250 * property, not the other way.
2252 * When the @inverted argument is %TRUE, the binding inverts the
2253 * value as it passes from the setting to the object, i.e. @property
2254 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2257 * Note that the lifecycle of the binding is tied to the object,
2258 * and that you can have only one binding per object property.
2259 * If you bind the same property twice on the same object, the second
2260 * binding overrides the first one.
2265 g_settings_bind_writable (GSettings *settings,
2268 const gchar *property,
2271 GSettingsWritableBinding *binding;
2272 gchar *detailed_signal;
2275 g_return_if_fail (G_IS_SETTINGS (settings));
2277 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2280 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2281 property, G_OBJECT_TYPE_NAME (object));
2284 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2286 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2287 property, G_OBJECT_TYPE_NAME (object));
2291 binding = g_slice_new (GSettingsWritableBinding);
2292 binding->settings = g_object_ref (settings);
2293 binding->object = object;
2294 binding->key = g_intern_string (key);
2295 binding->property = g_intern_string (property);
2296 binding->inverted = inverted;
2298 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2299 binding->handler_id =
2300 g_signal_connect (settings, detailed_signal,
2301 G_CALLBACK (g_settings_binding_writable_changed),
2303 g_free (detailed_signal);
2305 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2306 binding, g_settings_writable_binding_free);
2308 g_settings_binding_writable_changed (settings, binding->key, binding);
2312 * g_settings_unbind:
2313 * @object: the object
2314 * @property: the property whose binding is removed
2316 * Removes an existing binding for @property on @object.
2318 * Note that bindings are automatically removed when the
2319 * object is finalized, so it is rarely necessary to call this
2325 g_settings_unbind (gpointer object,
2326 const gchar *property)
2328 GQuark binding_quark;
2330 binding_quark = g_settings_binding_quark (property);
2331 g_object_set_qdata (object, binding_quark, NULL);
2335 #define __G_SETTINGS_C__
2336 #include "gioaliasdef.c"
2338 /* vim:set foldmethod=marker: */