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*) >
83 * <!ATTLIST schemalist gettext-domain #IMPLIED >
85 * <!ELEMENT schema (key|child)* >
86 * <!ATTLIST schema id CDATA #REQUIRED
88 * gettext-domain CDATA #IMPLIED >
90 * <!ELEMENT key (default|summary?|description?|range?|choices?|aliases?) >
91 * <!-- name can only contain lowercase letters, numbers and '-' -->
92 * <!-- type must be a GVariant type string -->
93 * <!ATTLIST key name CDATA #REQUIRED
94 * type CDATA #REQUIRED >
96 * <!-- the default value is specified a a serialized GVariant,
97 * i.e. you have to include the quotes when specifying a string -->
98 * <!ELEMENT default (#PCDATA) >
99 * <!-- the presence of the l10n attribute marks a default value for
100 * translation, its value is the gettext category to use -->
101 * <!-- if context is present, it specifies msgctxt to use -->
102 * <!ATTLIST default l10n (messages|time) #IMPLIED
103 * context CDATA #IMPLIED >
105 * <!ELEMENT summary (#PCDATA) >
106 * <!ELEMENT description (#PCDATA) >
108 * <!ELEMENT range EMPTY >
109 * <!ATTLIST range min CDATA #REQUIRED
110 * max CDATA #REQUIRED >
112 * <!ELEMENT choices (choice+) >
113 * <!ELEMENT choice EMPTY >
114 * <!ATTLIST choice value CDATA #REQUIRED >
115 * <!ELEMENT aliases (alias+) >
116 * <!ELEMENT alias EMPTY >
117 * <!ATTLIST alias value CDATA #REQUIRED >
119 * <!ELEMENT child EMPTY >
120 * <!ATTLIST child name CDATA #REQUIRED
121 * schema CDATA #REQUIRED >
125 * At runtime, schemas are identified by their id (as specified
126 * in the <tag class="attribute">id</tag> attribute of the
127 * <tag class="starttag">schema</tag> element). The
128 * convention for schema ids is to use a dotted name, similar in
129 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
132 * <title>Binding</title>
134 * A very convenient feature of GSettings lets you bind #GObject properties
135 * directly to settings, using g_settings_bind(). Once a GObject property
136 * has been bound to a setting, changes on either side are automatically
137 * propagated to the other side. GSettings handles details like
138 * mapping between GObject and GVariant types, and preventing infinite
142 * This makes it very easy to hook up a preferences dialog to the
143 * underlying settings. To make this even more convenient, GSettings
144 * looks for a boolean property with the name "sensitivity" and
145 * automatically binds it to the writability of the bound setting.
146 * If this 'magic' gets in the way, it can be suppressed with the
147 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
152 struct _GSettingsPrivate
154 /* where the signals go... */
155 GMainContext *main_context;
157 GSettingsBackend *backend;
158 GSettingsSchema *schema;
163 GDelayedSettingsBackend *delayed;
178 SIGNAL_WRITABLE_CHANGE_EVENT,
179 SIGNAL_WRITABLE_CHANGED,
185 static guint g_settings_signals[N_SIGNALS];
187 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
191 g_settings_real_change_event (GSettings *settings,
198 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
200 for (i = 0; i < n_keys; i++)
201 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
202 keys[i], g_quark_to_string (keys[i]));
208 g_settings_real_writable_change_event (GSettings *settings,
211 const GQuark *keys = &key;
216 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
218 for (i = 0; i < n_keys; i++)
220 const gchar *string = g_quark_to_string (keys[i]);
222 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
224 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
232 settings_backend_changed (GSettingsBackend *backend,
237 GSettings *settings = G_SETTINGS (target);
238 gboolean ignore_this;
241 g_assert (settings->priv->backend == backend);
243 for (i = 0; key[i] == settings->priv->path[i]; i++);
245 if (settings->priv->path[i] == '\0' &&
246 g_settings_schema_has_key (settings->priv->schema, key + i))
250 quark = g_quark_from_string (key + i);
251 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
252 0, &quark, 1, &ignore_this);
257 settings_backend_path_changed (GSettingsBackend *backend,
262 GSettings *settings = G_SETTINGS (target);
263 gboolean ignore_this;
265 g_assert (settings->priv->backend == backend);
267 if (g_str_has_prefix (settings->priv->path, path))
268 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
269 0, NULL, 0, &ignore_this);
273 settings_backend_keys_changed (GSettingsBackend *backend,
276 const gchar * const *items,
279 GSettings *settings = G_SETTINGS (target);
280 gboolean ignore_this;
283 g_assert (settings->priv->backend == backend);
285 for (i = 0; settings->priv->path[i] &&
286 settings->priv->path[i] == path[i]; i++);
293 for (j = 0; items[j]; j++)
295 const gchar *item = items[j];
298 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
300 if (settings->priv->path[i + k] == '\0' &&
301 g_settings_schema_has_key (settings->priv->schema, item + k))
302 quarks[l++] = g_quark_from_string (item + k);
304 /* "256 quarks ought to be enough for anybody!"
305 * If this bites you, I'm sorry. Please file a bug.
311 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
312 0, quarks, l, &ignore_this);
317 settings_backend_writable_changed (GSettingsBackend *backend,
321 GSettings *settings = G_SETTINGS (target);
322 gboolean ignore_this;
325 g_assert (settings->priv->backend == backend);
327 for (i = 0; key[i] == settings->priv->path[i]; i++);
329 if (settings->priv->path[i] == '\0' &&
330 g_settings_schema_has_key (settings->priv->schema, key + i))
331 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
332 0, g_quark_from_string (key + i), &ignore_this);
336 settings_backend_path_writable_changed (GSettingsBackend *backend,
340 GSettings *settings = G_SETTINGS (target);
341 gboolean ignore_this;
343 g_assert (settings->priv->backend == backend);
345 if (g_str_has_prefix (settings->priv->path, path))
346 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
347 0, (GQuark) 0, &ignore_this);
350 /* Properties, Construction, Destruction {{{1 */
352 g_settings_set_property (GObject *object,
357 GSettings *settings = G_SETTINGS (object);
362 g_assert (settings->priv->schema_name == NULL);
363 settings->priv->schema_name = g_value_dup_string (value);
367 settings->priv->path = g_value_dup_string (value);
371 settings->priv->context = g_value_dup_string (value);
375 g_assert_not_reached ();
380 g_settings_get_property (GObject *object,
385 GSettings *settings = G_SETTINGS (object);
390 g_value_set_object (value, settings->priv->schema);
393 case PROP_HAS_UNAPPLIED:
394 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
398 g_assert_not_reached ();
403 g_settings_constructed (GObject *object)
405 GSettings *settings = G_SETTINGS (object);
406 const gchar *schema_path;
408 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
409 schema_path = g_settings_schema_get_path (settings->priv->schema);
411 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
412 g_error ("settings object created with schema '%s' and path '%s', but "
413 "path '%s' is specified by schema",
414 settings->priv->schema_name, settings->priv->path, schema_path);
416 if (settings->priv->path == NULL)
418 if (schema_path == NULL)
419 g_error ("attempting to create schema '%s' without a path",
420 settings->priv->schema_name);
422 settings->priv->path = g_strdup (schema_path);
425 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
426 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
427 settings->priv->main_context,
428 settings_backend_changed,
429 settings_backend_path_changed,
430 settings_backend_keys_changed,
431 settings_backend_writable_changed,
432 settings_backend_path_writable_changed);
433 g_settings_backend_subscribe (settings->priv->backend,
434 settings->priv->path);
438 g_settings_finalize (GObject *object)
440 GSettings *settings = G_SETTINGS (object);
442 g_settings_backend_unsubscribe (settings->priv->backend,
443 settings->priv->path);
444 g_main_context_unref (settings->priv->main_context);
445 g_object_unref (settings->priv->backend);
446 g_object_unref (settings->priv->schema);
447 g_free (settings->priv->schema_name);
448 g_free (settings->priv->context);
449 g_free (settings->priv->path);
453 g_settings_init (GSettings *settings)
455 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
459 settings->priv->main_context = g_main_context_get_thread_default ();
461 if (settings->priv->main_context == NULL)
462 settings->priv->main_context = g_main_context_default ();
464 g_main_context_ref (settings->priv->main_context);
468 g_settings_class_init (GSettingsClass *class)
470 GObjectClass *object_class = G_OBJECT_CLASS (class);
472 class->writable_change_event = g_settings_real_writable_change_event;
473 class->change_event = g_settings_real_change_event;
475 object_class->set_property = g_settings_set_property;
476 object_class->get_property = g_settings_get_property;
477 object_class->constructed = g_settings_constructed;
478 object_class->finalize = g_settings_finalize;
480 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
483 * GSettings::changed:
484 * @settings: the object on which the signal was emitted
485 * @key: the name of the key that changed
487 * The "changed" signal is emitted when a key has potentially changed.
488 * You should call one of the g_settings_get() calls to check the new
491 * This signal supports detailed connections. You can connect to the
492 * detailed signal "changed::x" in order to only receive callbacks
493 * when key "x" changes.
495 g_settings_signals[SIGNAL_CHANGED] =
496 g_signal_new ("changed", G_TYPE_SETTINGS,
497 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
498 G_STRUCT_OFFSET (GSettingsClass, changed),
499 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
500 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
503 * GSettings::change-event:
504 * @settings: the object on which the signal was emitted
505 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
506 * @n_keys: the length of the @keys array, or 0
507 * @returns: %TRUE to stop other handlers from being invoked for the
508 * event. FALSE to propagate the event further.
510 * The "change-event" signal is emitted once per change event that
511 * affects this settings object. You should connect to this signal
512 * only if you are interested in viewing groups of changes before they
513 * are split out into multiple emissions of the "changed" signal.
514 * For most use cases it is more appropriate to use the "changed" signal.
516 * In the event that the change event applies to one or more specified
517 * keys, @keys will be an array of #GQuark of length @n_keys. In the
518 * event that the change event applies to the #GSettings object as a
519 * whole (ie: potentially every key has been changed) then @keys will
520 * be %NULL and @n_keys will be 0.
522 * The default handler for this signal invokes the "changed" signal
523 * for each affected key. If any other connected handler returns
524 * %TRUE then this default functionality will be supressed.
526 g_settings_signals[SIGNAL_CHANGE_EVENT] =
527 g_signal_new ("change-event", G_TYPE_SETTINGS,
529 G_STRUCT_OFFSET (GSettingsClass, change_event),
530 g_signal_accumulator_true_handled, NULL,
531 _gio_marshal_BOOL__POINTER_INT,
532 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
535 * GSettings::writable-changed:
536 * @settings: the object on which the signal was emitted
539 * The "writable-changed" signal is emitted when the writability of a
540 * key has potentially changed. You should call
541 * g_settings_is_writable() in order to determine the new status.
543 * This signal supports detailed connections. You can connect to the
544 * detailed signal "writable-changed::x" in order to only receive
545 * callbacks when the writability of "x" changes.
547 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
548 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
549 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
550 G_STRUCT_OFFSET (GSettingsClass, changed),
551 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
552 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
555 * GSettings::writable-change-event:
556 * @settings: the object on which the signal was emitted
557 * @key: the quark of the key, or 0
558 * @returns: %TRUE to stop other handlers from being invoked for the
559 * event. FALSE to propagate the event further.
561 * The "writable-change-event" signal is emitted once per writability
562 * change event that affects this settings object. You should connect
563 * to this signal if you are interested in viewing groups of changes
564 * before they are split out into multiple emissions of the
565 * "writable-changed" signal. For most use cases it is more
566 * appropriate to use the "writable-changed" signal.
568 * In the event that the writability change applies only to a single
569 * key, @key will be set to the #GQuark for that key. In the event
570 * that the writability change affects the entire settings object,
573 * The default handler for this signal invokes the "writable-changed"
574 * and "changed" signals for each affected key. This is done because
575 * changes in writability might also imply changes in value (if for
576 * example, a new mandatory setting is introduced). If any other
577 * connected handler returns %TRUE then this default functionality
580 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
581 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
583 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
584 g_signal_accumulator_true_handled, NULL,
585 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
590 * The name of the context that the settings are stored in.
592 g_object_class_install_property (object_class, PROP_CONTEXT,
593 g_param_spec_string ("context",
595 P_("The name of the context for this settings object"),
596 "", G_PARAM_CONSTRUCT_ONLY |
597 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
602 * The name of the schema that describes the types of keys
603 * for this #GSettings object.
605 g_object_class_install_property (object_class, PROP_SCHEMA,
606 g_param_spec_string ("schema",
608 P_("The name of the schema for this settings object"),
610 G_PARAM_CONSTRUCT_ONLY |
611 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 * The path within the backend where the settings are stored.
618 g_object_class_install_property (object_class, PROP_PATH,
619 g_param_spec_string ("path",
621 P_("The path within the backend where the settings are"),
623 G_PARAM_CONSTRUCT_ONLY |
624 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
627 * GSettings:has-unapplied:
629 * If this property is %TRUE, the #GSettings object has outstanding
630 * changes that will be applied when g_settings_apply() is called.
632 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
633 g_param_spec_boolean ("has-unapplied",
634 P_("Has unapplied changes"),
635 P_("TRUE if there are outstanding changes to apply()"),
637 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
641 /* Construction (new, new_with_path, etc.) {{{1 */
644 * @schema: the name of the schema
645 * @returns: a new #GSettings object
647 * Creates a new #GSettings object with a given schema.
649 * Signals on the newly created #GSettings object will be dispatched
650 * via the thread-default #GMainContext in effect at the time of the
651 * call to g_settings_new(). The new #GSettings will hold a reference
652 * on the context. See g_main_context_push_thread_default().
657 g_settings_new (const gchar *schema)
659 g_return_val_if_fail (schema != NULL, NULL);
661 return g_object_new (G_TYPE_SETTINGS,
667 * g_settings_new_with_path:
668 * @schema: the name of the schema
669 * @path: the path to use
670 * @returns: a new #GSettings object
672 * Creates a new #GSettings object with a given schema and path.
674 * You only need to do this if you want to directly create a settings
675 * object with a schema that doesn't have a specified path of its own.
678 * It is a programmer error to call this function for a schema that
679 * has an explicitly specified path.
684 g_settings_new_with_path (const gchar *schema,
687 g_return_val_if_fail (schema != NULL, NULL);
688 g_return_val_if_fail (path != NULL, NULL);
690 return g_object_new (G_TYPE_SETTINGS,
697 * g_settings_new_with_context:
698 * @schema: the name of the schema
699 * @context: the context to use
700 * @returns: a new #GSettings object
702 * Creates a new #GSettings object with a given schema and context.
704 * Creating settings objects with a context allow accessing settings
705 * from a database other than the usual one. For example, it may make
706 * sense to specify "defaults" in order to get a settings object that
707 * modifies the system default settings instead of the settings for this
710 * It is a programmer error to call this function for an unsupported
711 * context. Use g_settings_supports_context() to determine if a context
712 * is supported if you are unsure.
717 g_settings_new_with_context (const gchar *schema,
718 const gchar *context)
720 g_return_val_if_fail (schema != NULL, NULL);
721 g_return_val_if_fail (context != NULL, NULL);
723 return g_object_new (G_TYPE_SETTINGS,
730 * g_settings_new_with_context_and_path:
731 * @schema: the name of the schema
732 * @path: the path to use
733 * @returns: a new #GSettings object
735 * Creates a new #GSettings object with a given schema, context and
738 * This is a mix of g_settings_new_with_context() and
739 * g_settings_new_with_path().
744 g_settings_new_with_context_and_path (const gchar *schema,
745 const gchar *context,
748 g_return_val_if_fail (schema != NULL, NULL);
749 g_return_val_if_fail (context != NULL, NULL);
750 g_return_val_if_fail (path != NULL, NULL);
752 return g_object_new (G_TYPE_SETTINGS,
759 /* Internal read/write utilities, enum conversion, validation {{{1 */
765 GSettingsSchema *schema;
768 const guint32 *strinfo;
769 gsize strinfo_length;
771 const gchar *unparsed;
774 const GVariantType *type;
775 GVariant *minimum, *maximum;
776 GVariant *default_value;
780 g_settings_get_key_info (GSettingsKeyInfo *info,
788 memset (info, 0, sizeof *info);
790 iter = g_settings_schema_get_value (settings->priv->schema, key);
792 info->default_value = g_variant_iter_next_value (iter);
793 info->type = g_variant_get_type (info->default_value);
794 info->schema = settings->priv->schema;
795 info->settings = settings;
798 while (g_variant_iter_next (iter, "(y*)", &code, &data))
803 /* translation requeted */
804 g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
808 /* enumerated types, ... */
809 info->is_enum = TRUE;
811 /* ..., choices, aliases */
812 info->strinfo = g_variant_get_fixed_array (data,
813 &info->strinfo_length,
818 g_variant_get (data, "(**)", &info->minimum, &info->maximum);
822 g_warning ("unknown schema extension '%c'", code);
826 g_variant_unref (data);
829 g_variant_iter_free (iter);
833 g_settings_free_key_info (GSettingsKeyInfo *info)
836 g_variant_unref (info->minimum);
839 g_variant_unref (info->maximum);
841 g_variant_unref (info->default_value);
845 g_settings_write_to_backend (GSettingsKeyInfo *info,
851 path = g_strconcat (info->settings->priv->path, info->key, NULL);
852 success = g_settings_backend_write (info->settings->priv->backend,
860 g_settings_type_check (GSettingsKeyInfo *info,
863 g_return_val_if_fail (value != NULL, FALSE);
865 return g_variant_is_of_type (value, info->type);
869 g_settings_range_check (GSettingsKeyInfo *info,
872 if (info->minimum == NULL && info->strinfo == NULL)
875 if (g_variant_is_container (value))
881 g_variant_iter_init (&iter, value);
882 while (ok && (child = g_variant_iter_next_value (&iter)))
884 ok = g_settings_range_check (info, value);
885 g_variant_unref (child);
893 return g_variant_compare (info->minimum, value) <= 0 &&
894 g_variant_compare (value, info->maximum) <= 0;
897 return strinfo_is_string_valid (info->strinfo,
898 info->strinfo_length,
899 g_variant_get_string (value, NULL));
903 g_settings_range_fixup (GSettingsKeyInfo *info,
908 if (g_settings_range_check (info, value))
909 return g_variant_ref (value);
911 if (info->strinfo == NULL)
914 if (g_variant_is_container (value))
916 GVariantBuilder builder;
920 g_variant_builder_init (&builder, g_variant_get_type (value));
922 while ((child = g_variant_iter_next_value (&iter)))
926 fixed = g_settings_range_fixup (info, child);
927 g_variant_unref (child);
931 g_variant_builder_clear (&builder);
935 g_variant_builder_add_value (&builder, fixed);
936 g_variant_unref (fixed);
939 return g_variant_ref_sink (g_variant_builder_end (&builder));
942 target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
943 g_variant_get_string (value, NULL));
944 return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
948 g_settings_read_from_backend (GSettingsKeyInfo *info)
954 path = g_strconcat (info->settings->priv->path, info->key, NULL);
955 value = g_settings_backend_read (info->settings->priv->backend,
956 path, info->type, FALSE);
961 fixup = g_settings_range_fixup (info, value);
962 g_variant_unref (value);
971 g_settings_get_translated_default (GSettingsKeyInfo *info)
973 const gchar *translated;
974 GError *error = NULL;
978 if (info->lc_char == '\0')
979 /* translation not requested for this key */
982 domain = g_settings_schema_get_gettext_domain (info->schema);
984 if (info->lc_char == 't')
985 translated = g_dcgettext (domain, info->unparsed, LC_TIME);
987 translated = g_dgettext (domain, info->unparsed);
989 if (translated == info->unparsed)
990 /* the default value was not translated */
993 /* try to parse the translation of the unparsed default */
994 value = g_variant_parse (info->type, translated, NULL, NULL, &error);
998 g_warning ("Failed to parse translated string `%s' for "
999 "key `%s' in schema `%s': %s", info->unparsed, info->key,
1000 info->settings->priv->schema_name, error->message);
1001 g_warning ("Using untranslated default instead.");
1002 g_error_free (error);
1005 else if (!g_settings_range_check (info, value))
1007 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1008 "is outside of valid range", info->unparsed, info->key,
1009 info->settings->priv->schema_name);
1010 g_variant_unref (value);
1018 g_settings_to_enum (GSettingsKeyInfo *info,
1024 it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1025 g_variant_get_string (value, NULL),
1028 /* 'value' can only come from the backend after being filtered for validity,
1029 * from the translation after being filtered for validity, or from the schema
1030 * itself (which the schema compiler checks for validity). If this assertion
1031 * fails then it's really a bug in GSettings or the schema compiler...
1033 g_assert (it_worked);
1039 g_settings_from_enum (GSettingsKeyInfo *info,
1042 const gchar *string;
1044 string = strinfo_string_from_enum (info->strinfo,
1045 info->strinfo_length,
1051 return g_variant_ref_sink (g_variant_new_string (string));
1054 /* Public Get/Set API {{{1 (get, get_value, set, set_value) */
1056 * g_settings_get_value:
1057 * @settings: a #GSettings object
1058 * @key: the key to get the value for
1059 * @returns: a new #GVariant
1061 * Gets the value that is stored in @settings for @key.
1063 * It is a programmer error to give a @key that isn't valid for
1069 g_settings_get_value (GSettings *settings,
1072 GSettingsKeyInfo info;
1075 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1076 g_return_val_if_fail (key != NULL, NULL);
1078 g_settings_get_key_info (&info, settings, key);
1079 value = g_settings_read_from_backend (&info);
1082 value = g_settings_get_translated_default (&info);
1085 value = g_variant_ref (info.default_value);
1087 g_settings_free_key_info (&info);
1093 * g_settings_get_enum:
1094 * @settings: a #GSettings object
1095 * @key: the key to get the value for
1096 * @returns: the enum value
1098 * Gets the value that is stored in @settings for @key and converts it
1099 * to the enum value that it represents.
1101 * In order to use this function the type of the value must be a string
1102 * and it must be marked in the schema file as an enumerated type.
1104 * It is a programmer error to give a @key that isn't valid for
1105 * @settings, or is not marked as an enumerated type in the schema.
1107 * If the value stored in the configuration database is not a valid
1108 * value for the enumerated type then this function will return the
1114 g_settings_get_enum (GSettings *settings,
1117 GSettingsKeyInfo info;
1121 g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1122 g_return_val_if_fail (key != NULL, -1);
1124 g_settings_get_key_info (&info, settings, key);
1128 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1129 "associated with an enumerated type", info.key);
1130 g_settings_free_key_info (&info);
1134 value = g_settings_read_from_backend (&info);
1137 value = g_settings_get_translated_default (&info);
1140 value = g_variant_ref (info.default_value);
1142 result = g_settings_to_enum (&info, value);
1143 g_settings_free_key_info (&info);
1144 g_variant_unref (value);
1150 * g_settings_set_enum:
1151 * @settings: a #GSettings object
1152 * @key: a key, within @settings
1153 * @value: an enumerated value
1154 * Returns: %TRUE, if the set succeeds
1156 * Looks up the enumerated type nick for @value and writes it to @key,
1159 * It is a programmer error for @key to not be listed in the schema or
1160 * for it not to be tagged as an enumerated type, or for @value not to
1161 * be a valid value for the named type.
1163 * After performing the write, accessing @key directly
1164 * g_settings_get_string() will return the 'nick' associated with
1168 g_settings_set_enum (GSettings *settings,
1172 GSettingsKeyInfo info;
1176 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1177 g_return_val_if_fail (key != NULL, FALSE);
1179 g_settings_get_key_info (&info, settings, key);
1183 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1184 "associated with an enumerated type", info.key);
1188 if (!(variant = g_settings_from_enum (&info, value)))
1190 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1191 "in schema `%s'. Doing nothing.", value, info.key,
1192 info.settings->priv->schema_name);
1193 g_settings_free_key_info (&info);
1197 success = g_settings_write_to_backend (&info, variant);
1198 g_settings_free_key_info (&info);
1199 g_variant_unref (variant);
1205 * g_settings_set_value:
1206 * @settings: a #GSettings object
1207 * @key: the name of the key to set
1208 * @value: a #GVariant of the correct type
1209 * @returns: %TRUE if setting the key succeeded,
1210 * %FALSE if the key was not writable
1212 * Sets @key in @settings to @value.
1214 * It is a programmer error to give a @key that isn't valid for
1215 * @settings. It is a programmer error to give a @value of the
1218 * If @value is floating then this function consumes the reference.
1223 g_settings_set_value (GSettings *settings,
1227 GSettingsKeyInfo info;
1229 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1230 g_return_val_if_fail (key != NULL, FALSE);
1232 g_settings_get_key_info (&info, settings, key);
1233 g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1234 g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1235 g_settings_free_key_info (&info);
1237 return g_settings_write_to_backend (&info, value);
1242 * @settings: a #GSettings object
1243 * @key: the key to get the value for
1244 * @format: a #GVariant format string
1245 * @...: arguments as per @format
1247 * Gets the value that is stored at @key in @settings.
1249 * A convenience function that combines g_settings_get_value() with
1252 * It is a programmer error to pass a @key that isn't valid for
1253 * @settings or a @format_string that doesn't match the type of @key according
1254 * to the schema of @settings.
1259 g_settings_get (GSettings *settings,
1261 const gchar *format,
1267 value = g_settings_get_value (settings, key);
1269 va_start (ap, format);
1270 g_variant_get_va (value, format, NULL, &ap);
1273 g_variant_unref (value);
1278 * @settings: a #GSettings object
1279 * @key: the name of the key to set
1280 * @format: a #GVariant format string
1281 * @...: arguments as per @format
1282 * @returns: %TRUE if setting the key succeeded,
1283 * %FALSE if the key was not writable
1285 * Sets @key in @settings to @value.
1287 * A convenience function that combines g_settings_set_value() with
1290 * It is a programmer error to pass a @key that isn't valid for
1291 * @settings or a @format that doesn't match the type of @key according
1292 * to the schema of @settings.
1297 g_settings_set (GSettings *settings,
1299 const gchar *format,
1305 va_start (ap, format);
1306 value = g_variant_new_va (format, NULL, &ap);
1309 return g_settings_set_value (settings, key, value);
1312 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1314 * g_settings_get_string:
1315 * @settings: a #GSettings object
1316 * @key: the key to get the value for
1317 * @returns: a newly-allocated string
1319 * Gets the value that is stored at @key in @settings.
1321 * A convenience variant of g_settings_get() for strings.
1323 * It is a programmer error to pass a @key that isn't valid for
1324 * @settings or is not of type string.
1329 g_settings_get_string (GSettings *settings,
1335 value = g_settings_get_value (settings, key);
1336 result = g_variant_dup_string (value, NULL);
1337 g_variant_unref (value);
1343 * g_settings_set_string:
1344 * @settings: a #GSettings object
1345 * @key: the name of the key to set
1346 * @value: the value to set it to
1347 * @returns: %TRUE if setting the key succeeded,
1348 * %FALSE if the key was not writable
1350 * Sets @key in @settings to @value.
1352 * A convenience variant of g_settings_set() for strings.
1354 * It is a programmer error to pass a @key that isn't valid for
1355 * @settings or is not of type string.
1360 g_settings_set_string (GSettings *settings,
1364 return g_settings_set_value (settings, key, g_variant_new_string (value));
1368 * g_settings_get_int:
1369 * @settings: a #GSettings object
1370 * @key: the key to get the value for
1371 * @returns: an integer
1373 * Gets the value that is stored at @key in @settings.
1375 * A convenience variant of g_settings_get() for 32-bit integers.
1377 * It is a programmer error to pass a @key that isn't valid for
1378 * @settings or is not of type int32.
1383 g_settings_get_int (GSettings *settings,
1389 value = g_settings_get_value (settings, key);
1390 result = g_variant_get_int32 (value);
1391 g_variant_unref (value);
1397 * g_settings_set_int:
1398 * @settings: a #GSettings object
1399 * @key: the name of the key to set
1400 * @value: the value to set it to
1401 * @returns: %TRUE if setting the key succeeded,
1402 * %FALSE if the key was not writable
1404 * Sets @key in @settings to @value.
1406 * A convenience variant of g_settings_set() for 32-bit integers.
1408 * It is a programmer error to pass a @key that isn't valid for
1409 * @settings or is not of type int32.
1414 g_settings_set_int (GSettings *settings,
1418 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1422 * g_settings_get_double:
1423 * @settings: a #GSettings object
1424 * @key: the key to get the value for
1425 * @returns: a double
1427 * Gets the value that is stored at @key in @settings.
1429 * A convenience variant of g_settings_get() for doubles.
1431 * It is a programmer error to pass a @key that isn't valid for
1432 * @settings or is not of type double.
1437 g_settings_get_double (GSettings *settings,
1443 value = g_settings_get_value (settings, key);
1444 result = g_variant_get_double (value);
1445 g_variant_unref (value);
1451 * g_settings_set_double:
1452 * @settings: a #GSettings object
1453 * @key: the name of the key to set
1454 * @value: the value to set it to
1455 * @returns: %TRUE if setting the key succeeded,
1456 * %FALSE if the key was not writable
1458 * Sets @key in @settings to @value.
1460 * A convenience variant of g_settings_set() for doubles.
1462 * It is a programmer error to pass a @key that isn't valid for
1463 * @settings or is not of type double.
1468 g_settings_set_double (GSettings *settings,
1472 return g_settings_set_value (settings, key, g_variant_new_double (value));
1476 * g_settings_get_boolean:
1477 * @settings: a #GSettings object
1478 * @key: the key to get the value for
1479 * @returns: a boolean
1481 * Gets the value that is stored at @key in @settings.
1483 * A convenience variant of g_settings_get() for booleans.
1485 * It is a programmer error to pass a @key that isn't valid for
1486 * @settings or is not of type boolean.
1491 g_settings_get_boolean (GSettings *settings,
1497 value = g_settings_get_value (settings, key);
1498 result = g_variant_get_boolean (value);
1499 g_variant_unref (value);
1505 * g_settings_set_boolean:
1506 * @settings: a #GSettings object
1507 * @key: the name of the key to set
1508 * @value: the value to set it to
1509 * @returns: %TRUE if setting the key succeeded,
1510 * %FALSE if the key was not writable
1512 * Sets @key in @settings to @value.
1514 * A convenience variant of g_settings_set() for booleans.
1516 * It is a programmer error to pass a @key that isn't valid for
1517 * @settings or is not of type boolean.
1522 g_settings_set_boolean (GSettings *settings,
1526 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1530 * g_settings_get_strv:
1531 * @settings: a #GSettings object
1532 * @key: the key to get the value for
1533 * @returns: a newly-allocated, %NULL-terminated array of strings
1535 * Gets the value that is stored at @key in @settings.
1537 * A convenience variant of g_settings_get() for string arrays.
1539 * It is a programmer error to pass a @key that isn't valid for
1540 * @settings or is not of type 'string array'.
1545 g_settings_get_strv (GSettings *settings,
1551 value = g_settings_get_value (settings, key);
1552 result = g_variant_dup_strv (value, NULL);
1553 g_variant_unref (value);
1559 * g_settings_set_strv:
1560 * @settings: a #GSettings object
1561 * @key: the name of the key to set
1562 * @value: (allow-none): the value to set it to, or %NULL
1563 * @returns: %TRUE if setting the key succeeded,
1564 * %FALSE if the key was not writable
1566 * Sets @key in @settings to @value.
1568 * A convenience variant of g_settings_set() for string arrays. If
1569 * @value is %NULL, then @key is set to be the empty array.
1571 * It is a programmer error to pass a @key that isn't valid for
1572 * @settings or is not of type 'string array'.
1577 g_settings_set_strv (GSettings *settings,
1579 const gchar * const *value)
1584 array = g_variant_new_strv (value, -1);
1586 array = g_variant_new_strv (NULL, 0);
1588 return g_settings_set_value (settings, key, array);
1590 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1593 * @settings: a #GSettings object
1595 * Changes the #GSettings object into 'delay-apply' mode. In this
1596 * mode, changes to @settings are not immediately propagated to the
1597 * backend, but kept locally until g_settings_apply() is called.
1602 g_settings_delay (GSettings *settings)
1604 g_return_if_fail (G_IS_SETTINGS (settings));
1606 if (settings->priv->delayed)
1609 settings->priv->delayed =
1610 g_delayed_settings_backend_new (settings->priv->backend,
1612 settings->priv->main_context);
1613 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1614 g_object_unref (settings->priv->backend);
1616 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1617 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1618 settings->priv->main_context,
1619 settings_backend_changed,
1620 settings_backend_path_changed,
1621 settings_backend_keys_changed,
1622 settings_backend_writable_changed,
1623 settings_backend_path_writable_changed);
1628 * @settings: a #GSettings instance
1630 * Applies any changes that have been made to the settings. This
1631 * function does nothing unless @settings is in 'delay-apply' mode;
1632 * see g_settings_set_delay_apply(). In the normal case settings are
1633 * always applied immediately.
1636 g_settings_apply (GSettings *settings)
1638 if (settings->priv->delayed)
1640 GDelayedSettingsBackend *delayed;
1642 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1643 g_delayed_settings_backend_apply (delayed);
1648 * g_settings_revert:
1649 * @settings: a #GSettings instance
1651 * Reverts all non-applied changes to the settings. This function
1652 * does nothing unless @settings is in 'delay-apply' mode; see
1653 * g_settings_set_delay_apply(). In the normal case settings are
1654 * always applied immediately.
1656 * Change notifications will be emitted for affected keys.
1659 g_settings_revert (GSettings *settings)
1661 if (settings->priv->delayed)
1663 GDelayedSettingsBackend *delayed;
1665 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1666 g_delayed_settings_backend_revert (delayed);
1671 * g_settings_get_has_unapplied:
1672 * @settings: a #GSettings object
1673 * @returns: %TRUE if @settings has unapplied changes
1675 * Returns whether the #GSettings object has any unapplied
1676 * changes. This can only be the case if it is in 'delayed-apply' mode.
1681 g_settings_get_has_unapplied (GSettings *settings)
1683 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1685 return settings->priv->delayed &&
1686 g_delayed_settings_backend_get_has_unapplied (
1687 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1690 /* Extra API (sync, get_child, is_writable) {{{1 */
1693 * @context: the context to sync, or %NULL
1695 * Ensures that all pending operations for the given context are
1698 * Writes made to a #GSettings are handled asynchronously. For this
1699 * reason, it is very unlikely that the changes have it to disk by the
1700 * time g_settings_set() returns.
1702 * This call will block until all of the writes have made it to the
1703 * backend. Since the mainloop is not running, no change notifications
1704 * will be dispatched during this call (but some may be queued by the
1705 * time the call is done).
1708 g_settings_sync (const gchar *context)
1710 GSettingsBackend *backend;
1712 if (context == NULL)
1715 backend = g_settings_backend_get_with_context (context);
1716 g_settings_backend_sync (backend);
1720 * g_settings_is_writable:
1721 * @settings: a #GSettings object
1722 * @name: the name of a key
1723 * @returns: %TRUE if the key @name is writable
1725 * Finds out if a key can be written or not
1730 g_settings_is_writable (GSettings *settings,
1736 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1738 path = g_strconcat (settings->priv->path, name, NULL);
1739 writable = g_settings_backend_get_writable (settings->priv->backend, path);
1746 * g_settings_get_child:
1747 * @settings: a #GSettings object
1748 * @name: the name of the 'child' schema
1749 * @returns: a 'child' settings object
1751 * Creates a 'child' settings object which has a base path of
1752 * <replaceable>base-path</replaceable>/@name", where
1753 * <replaceable>base-path</replaceable> is the base path of @settings.
1755 * The schema for the child settings object must have been declared
1756 * in the schema of @settings using a <tag class="starttag">child</tag> element.
1761 g_settings_get_child (GSettings *settings,
1764 const gchar *child_schema;
1769 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1771 child_name = g_strconcat (name, "/", NULL);
1772 child_schema = g_settings_schema_get_string (settings->priv->schema,
1774 if (child_schema == NULL)
1775 g_error ("Schema '%s' has no child '%s'",
1776 settings->priv->schema_name, name);
1778 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1779 child = g_object_new (G_TYPE_SETTINGS,
1780 "schema", child_schema,
1783 g_free (child_path);
1784 g_free (child_name);
1792 GSettings *settings;
1795 GSettingsBindGetMapping get_mapping;
1796 GSettingsBindSetMapping set_mapping;
1798 GDestroyNotify destroy;
1800 guint writable_handler_id;
1801 guint property_handler_id;
1802 const GParamSpec *property;
1803 guint key_handler_id;
1807 /* prevent recursion */
1812 g_settings_binding_free (gpointer data)
1814 GSettingsBinding *binding = data;
1816 g_assert (!binding->running);
1818 if (binding->writable_handler_id)
1819 g_signal_handler_disconnect (binding->settings,
1820 binding->writable_handler_id);
1822 if (binding->key_handler_id)
1823 g_signal_handler_disconnect (binding->settings,
1824 binding->key_handler_id);
1826 if (g_signal_handler_is_connected (binding->object,
1827 binding->property_handler_id))
1828 g_signal_handler_disconnect (binding->object,
1829 binding->property_handler_id);
1831 g_variant_type_free (binding->type);
1832 g_object_unref (binding->settings);
1834 if (binding->destroy)
1835 binding->destroy (binding->user_data);
1837 g_slice_free (GSettingsBinding, binding);
1841 g_settings_binding_quark (const char *property)
1846 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1847 quark = g_quark_from_string (tmp);
1854 g_settings_binding_key_changed (GSettings *settings,
1858 GSettingsBinding *binding = user_data;
1859 GValue value = { 0, };
1862 g_assert (settings == binding->settings);
1863 g_assert (key == binding->key);
1865 if (binding->running)
1868 binding->running = TRUE;
1870 g_value_init (&value, binding->property->value_type);
1871 variant = g_settings_get_value (settings, key);
1872 if (binding->get_mapping (&value, variant, binding->user_data))
1873 g_object_set_property (binding->object,
1874 binding->property->name,
1876 g_variant_unref (variant);
1877 g_value_unset (&value);
1879 binding->running = FALSE;
1883 g_settings_binding_property_changed (GObject *object,
1884 const GParamSpec *pspec,
1887 GSettingsBinding *binding = user_data;
1888 GValue value = { 0, };
1891 g_assert (object == binding->object);
1892 g_assert (pspec == binding->property);
1894 if (binding->running)
1897 binding->running = TRUE;
1899 g_value_init (&value, pspec->value_type);
1900 g_object_get_property (object, pspec->name, &value);
1901 if ((variant = binding->set_mapping (&value, binding->type,
1902 binding->user_data)))
1904 g_settings_set_value (binding->settings,
1906 g_variant_ref_sink (variant));
1907 g_variant_unref (variant);
1909 g_value_unset (&value);
1911 binding->running = FALSE;
1916 * @settings: a #GSettings object
1917 * @key: the key to bind
1918 * @object: a #GObject
1919 * @property: the name of the property to bind
1920 * @flags: flags for the binding
1922 * Create a binding between the @key in the @settings object
1923 * and the property @property of @object.
1925 * The binding uses the default GIO mapping functions to map
1926 * between the settings and property values. These functions
1927 * handle booleans, numeric types and string types in a
1928 * straightforward way. Use g_settings_bind_with_mapping() if
1929 * you need a custom mapping, or map between types that are not
1930 * supported by the default mapping functions.
1932 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1933 * function also establishes a binding between the writability of
1934 * @key and the "sensitive" property of @object (if @object has
1935 * a boolean property by that name). See g_settings_bind_writable()
1936 * for more details about writable bindings.
1938 * Note that the lifecycle of the binding is tied to the object,
1939 * and that you can have only one binding per object property.
1940 * If you bind the same property twice on the same object, the second
1941 * binding overrides the first one.
1946 g_settings_bind (GSettings *settings,
1949 const gchar *property,
1950 GSettingsBindFlags flags)
1952 g_settings_bind_with_mapping (settings, key, object, property,
1953 flags, NULL, NULL, NULL, NULL);
1957 * g_settings_bind_with_mapping:
1958 * @settings: a #GSettings object
1959 * @key: the key to bind
1960 * @object: a #GObject
1961 * @property: the name of the property to bind
1962 * @flags: flags for the binding
1963 * @get_mapping: a function that gets called to convert values
1964 * from @settings to @object, or %NULL to use the default GIO mapping
1965 * @set_mapping: a function that gets called to convert values
1966 * from @object to @settings, or %NULL to use the default GIO mapping
1967 * @user_data: data that gets passed to @get_mapping and @set_mapping
1968 * @destroy: #GDestroyNotify function for @user_data
1970 * Create a binding between the @key in the @settings object
1971 * and the property @property of @object.
1973 * The binding uses the provided mapping functions to map between
1974 * settings and property values.
1976 * Note that the lifecycle of the binding is tied to the object,
1977 * and that you can have only one binding per object property.
1978 * If you bind the same property twice on the same object, the second
1979 * binding overrides the first one.
1984 g_settings_bind_with_mapping (GSettings *settings,
1987 const gchar *property,
1988 GSettingsBindFlags flags,
1989 GSettingsBindGetMapping get_mapping,
1990 GSettingsBindSetMapping set_mapping,
1992 GDestroyNotify destroy)
1994 GSettingsBinding *binding;
1995 GObjectClass *objectclass;
1996 gchar *detailed_signal;
1997 GQuark binding_quark;
1999 g_return_if_fail (G_IS_SETTINGS (settings));
2001 objectclass = G_OBJECT_GET_CLASS (object);
2003 binding = g_slice_new0 (GSettingsBinding);
2004 binding->settings = g_object_ref (settings);
2005 binding->object = object;
2006 binding->key = g_intern_string (key);
2007 binding->property = g_object_class_find_property (objectclass, property);
2008 binding->user_data = user_data;
2009 binding->destroy = destroy;
2010 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2011 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2013 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2014 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2016 if (binding->property == NULL)
2018 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2019 property, G_OBJECT_TYPE_NAME (object));
2023 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
2025 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
2026 property, G_OBJECT_TYPE_NAME (object));
2029 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
2031 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
2032 property, G_OBJECT_TYPE_NAME (object));
2040 iter = g_settings_schema_get_value (settings->priv->schema, key);
2041 value = g_variant_iter_next_value (iter);
2042 binding->type = g_variant_type_copy (g_variant_get_type (value));
2043 g_variant_iter_free (iter);
2044 g_variant_unref (value);
2047 if (binding->type == NULL)
2049 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
2050 key, settings->priv->schema_name);
2054 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2055 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2056 !g_settings_mapping_is_compatible (binding->property->value_type,
2059 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2060 "'%s' which is not compatible with type '%s' of key '%s' "
2061 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2062 g_type_name (binding->property->value_type),
2063 g_variant_type_dup_string (binding->type), key,
2064 settings->priv->schema_name);
2068 if ((flags & G_SETTINGS_BIND_SET) &&
2069 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2071 GParamSpec *sensitive;
2073 sensitive = g_object_class_find_property (objectclass, "sensitive");
2075 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2076 (sensitive->flags & G_PARAM_WRITABLE))
2077 g_settings_bind_writable (settings, binding->key,
2078 object, "sensitive", FALSE);
2081 if (flags & G_SETTINGS_BIND_SET)
2083 detailed_signal = g_strdup_printf ("notify::%s", property);
2084 binding->property_handler_id =
2085 g_signal_connect (object, detailed_signal,
2086 G_CALLBACK (g_settings_binding_property_changed),
2088 g_free (detailed_signal);
2090 if (~flags & G_SETTINGS_BIND_GET)
2091 g_settings_binding_property_changed (object,
2096 if (flags & G_SETTINGS_BIND_GET)
2098 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2100 detailed_signal = g_strdup_printf ("changed::%s", key);
2101 binding->key_handler_id =
2102 g_signal_connect (settings, detailed_signal,
2103 G_CALLBACK (g_settings_binding_key_changed),
2105 g_free (detailed_signal);
2108 g_settings_binding_key_changed (settings, binding->key, binding);
2111 binding_quark = g_settings_binding_quark (property);
2112 g_object_set_qdata_full (object, binding_quark,
2113 binding, g_settings_binding_free);
2116 /* Writability binding {{{1 */
2119 GSettings *settings;
2122 const gchar *property;
2125 } GSettingsWritableBinding;
2128 g_settings_writable_binding_free (gpointer data)
2130 GSettingsWritableBinding *binding = data;
2132 g_signal_handler_disconnect (binding->settings, binding->handler_id);
2133 g_object_unref (binding->settings);
2134 g_slice_free (GSettingsWritableBinding, binding);
2138 g_settings_binding_writable_changed (GSettings *settings,
2142 GSettingsWritableBinding *binding = user_data;
2145 g_assert (settings == binding->settings);
2146 g_assert (key == binding->key);
2148 writable = g_settings_is_writable (settings, key);
2150 if (binding->inverted)
2151 writable = !writable;
2153 g_object_set (binding->object, binding->property, writable, NULL);
2157 * g_settings_bind_writable:
2158 * @settings: a #GSettings object
2159 * @key: the key to bind
2160 * @object: a #GObject
2161 * @property: the name of a boolean property to bind
2162 * @inverted: whether to 'invert' the value
2164 * Create a binding between the writability of @key in the
2165 * @settings object and the property @property of @object.
2166 * The property must be boolean; "sensitive" or "visible"
2167 * properties of widgets are the most likely candidates.
2169 * Writable bindings are always uni-directional; changes of the
2170 * writability of the setting will be propagated to the object
2171 * property, not the other way.
2173 * When the @inverted argument is %TRUE, the binding inverts the
2174 * value as it passes from the setting to the object, i.e. @property
2175 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2178 * Note that the lifecycle of the binding is tied to the object,
2179 * and that you can have only one binding per object property.
2180 * If you bind the same property twice on the same object, the second
2181 * binding overrides the first one.
2186 g_settings_bind_writable (GSettings *settings,
2189 const gchar *property,
2192 GSettingsWritableBinding *binding;
2193 gchar *detailed_signal;
2196 g_return_if_fail (G_IS_SETTINGS (settings));
2198 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2201 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2202 property, G_OBJECT_TYPE_NAME (object));
2205 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2207 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2208 property, G_OBJECT_TYPE_NAME (object));
2212 binding = g_slice_new (GSettingsWritableBinding);
2213 binding->settings = g_object_ref (settings);
2214 binding->object = object;
2215 binding->key = g_intern_string (key);
2216 binding->property = g_intern_string (property);
2217 binding->inverted = inverted;
2219 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2220 binding->handler_id =
2221 g_signal_connect (settings, detailed_signal,
2222 G_CALLBACK (g_settings_binding_writable_changed),
2224 g_free (detailed_signal);
2226 g_object_set_qdata_full (object, g_settings_binding_quark (property),
2227 binding, g_settings_writable_binding_free);
2229 g_settings_binding_writable_changed (settings, binding->key, binding);
2233 * g_settings_unbind:
2234 * @object: the object
2235 * @property: the property whose binding is removed
2237 * Removes an existing binding for @property on @object.
2239 * Note that bindings are automatically removed when the
2240 * object is finalized, so it is rarely necessary to call this
2246 g_settings_unbind (gpointer object,
2247 const gchar *property)
2249 GQuark binding_quark;
2251 binding_quark = g_settings_binding_quark (property);
2252 g_object_set_qdata (object, binding_quark, NULL);
2256 #define __G_SETTINGS_C__
2257 #include "gioaliasdef.c"
2259 /* vim:set foldmethod=marker: */