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>
27 #include "gsettings.h"
29 #include "gdelayedsettingsbackend.h"
30 #include "gsettingsbackendinternal.h"
31 #include "gsettings-mapping.h"
32 #include "gio-marshal.h"
33 #include "gsettingsschema.h"
41 * @short_description: a high-level API for application settings
43 * The #GSettings class provides a convenient API for storing and retrieving
44 * application settings.
46 * When creating a GSettings instance, you have to specify a schema
47 * that describes the keys in your settings and their types and default
48 * values, as well as some other information.
50 * Normally, a schema has as fixed path that determines where the settings
51 * are stored in the conceptual global tree of settings. However, schemas
52 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
53 * useful e.g. when the schema describes an 'account', and you want to be
54 * able to store a arbitrary number of accounts.
56 * Unlike other configuration systems (like GConf), GSettings does not
57 * restrict keys to basic types like strings and numbers. GSettings stores
58 * values as #GVariant, and allows any #GVariantType for keys. Key names
59 * are restricted to lowercase characters, numbers and '-'. Furthermore,
60 * the names must begin with a lowercase character, must not end
61 * with a '-', and must not contain consecutive dashes. Key names can
62 * be up to 32 characters long.
64 * Similar to GConf, the default values in GSettings schemas can be
65 * localized, but the localized values are stored in gettext catalogs
66 * and looked up with the domain that is specified in the gettext-domain
67 * attribute of the <tag>schemalist</tag> or <tag>schema</tag> elements
68 * and the category that is specified in the l10n attribute of the
69 * <tag>key</tag> element.
71 * GSettings uses schemas in a compact binary form that is created
72 * by the gschema-compile utility. The input is a schema description in
73 * an XML format that can be described by the following DTD:
75 * <!ELEMENT schemalist (schema*) >
76 * <!ATTLIST schemalist gettext-domain #IMPLIED >
78 * <!ELEMENT schema (key|child)* >
79 * <!ATTLIST schema id CDATA #REQUIRED
81 * gettext-domain CDATA #IMPLIED >
83 * <!ELEMENT key (default|summary?|description?|range?|choices?) >
84 * <!-- name can only contain lowercase letters, numbers and '-' -->
85 * <!-- type must be a GVariant type string -->
86 * <!ATTLIST key name CDATA #REQUIRED
87 * type CDATA #REQUIRED >
89 * <!-- the default value is specified a a serialized GVariant,
90 * i.e. you have to include the quotes when specifying a string -->
91 * <!ELEMENT default (#PCDATA) >
92 * <!-- the presence of the l10n attribute marks a default value for
93 * translation, its value is the gettext category to use -->
94 * <!-- if context is present, it specifies msgctxt to use -->
95 * <!ATTLIST default l10n (messages|time) #IMPLIED
96 * context CDATA #IMPLIED >
98 * <!ELEMENT summary (#PCDATA) >
99 * <!ELEMENT description (#PCDATA) >
101 * <!ELEMENT range (min,max) >
102 * <!ELEMENT min (#PCDATA) >
103 * <!ELEMENT max (#PCDATA) >
105 * <!ELEMENT choices (choice+) >
106 * <!ELEMENT choice (alias?) >
107 * <!ATTLIST choice value CDATA #REQUIRED >
108 * <!ELEMENT choice (alias?) >
109 * <!ELEMENT alias EMPTY >
110 * <!ATTLIST alias value CDATA #REQUIRED >
112 * <!ELEMENT child EMPTY >
113 * <!ATTLIST child name CDATA #REQUIRED
114 * schema CDATA #REQUIRED >
119 * <title>Binding</title>
121 * A very convenient feature of GSettings lets you bind #GObject properties
122 * directly to settings, using g_settings_bind(). Once a GObject property
123 * has been bound to a setting, changes on either side are automatically
124 * propagated to the other side. GSettings handles details like
125 * mapping between GObject and GVariant types, and preventing infinite
129 * This makes it very easy to hook up a preferences dialog to the
130 * underlying settings. To make this even more convenient, GSettings
131 * looks for a boolean property with the name "sensitivity" and
132 * automatically binds it to the writability of the bound setting.
133 * If this 'magic' gets in the way, it can be suppressed with the
134 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
139 struct _GSettingsPrivate
141 GSettingsBackend *backend;
142 GSettingsSchema *schema;
147 GDelayedSettingsBackend *delayed;
162 SIGNAL_WRITABLE_CHANGE_EVENT,
163 SIGNAL_WRITABLE_CHANGED,
169 static guint g_settings_signals[N_SIGNALS];
171 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
174 g_settings_real_change_event (GSettings *settings,
181 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
183 for (i = 0; i < n_keys; i++)
184 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
185 keys[i], g_quark_to_string (keys[i]));
191 g_settings_real_writable_change_event (GSettings *settings,
194 const GQuark *keys = &key;
199 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
201 for (i = 0; i < n_keys; i++)
203 const gchar *string = g_quark_to_string (keys[i]);
205 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
207 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
215 settings_backend_changed (GSettingsBackend *backend,
220 GSettings *settings = G_SETTINGS (user_data);
221 gboolean ignore_this;
224 g_assert (settings->priv->backend == backend);
226 for (i = 0; key[i] == settings->priv->path[i]; i++);
228 if (settings->priv->path[i] == '\0' &&
229 g_settings_schema_has_key (settings->priv->schema, key + i))
233 quark = g_quark_from_string (key + i);
234 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
235 0, &quark, 1, &ignore_this);
240 settings_backend_path_changed (GSettingsBackend *backend,
245 GSettings *settings = G_SETTINGS (user_data);
246 gboolean ignore_this;
248 g_assert (settings->priv->backend == backend);
250 if (g_str_has_prefix (settings->priv->path, path))
251 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
252 0, NULL, 0, &ignore_this);
256 settings_backend_keys_changed (GSettingsBackend *backend,
258 const gchar * const *items,
262 GSettings *settings = G_SETTINGS (user_data);
263 gboolean ignore_this;
266 g_assert (settings->priv->backend == backend);
268 for (i = 0; settings->priv->path[i] &&
269 settings->priv->path[i] == path[i]; i++);
276 for (j = 0; items[j]; j++)
278 const gchar *item = items[j];
281 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
283 if (settings->priv->path[i + k] == '\0' &&
284 g_settings_schema_has_key (settings->priv->schema, item + k))
285 quarks[l++] = g_quark_from_string (item + k);
287 /* "256 quarks ought to be enough for anybody!"
288 * If this bites you, I'm sorry. Please file a bug.
294 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
295 0, quarks, l, &ignore_this);
300 settings_backend_writable_changed (GSettingsBackend *backend,
304 GSettings *settings = G_SETTINGS (user_data);
305 gboolean ignore_this;
308 g_assert (settings->priv->backend == backend);
310 for (i = 0; key[i] == settings->priv->path[i]; i++);
312 if (settings->priv->path[i] == '\0' &&
313 g_settings_schema_has_key (settings->priv->schema, key + i))
314 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
315 0, g_quark_from_string (key + i), &ignore_this);
319 settings_backend_path_writable_changed (GSettingsBackend *backend,
323 GSettings *settings = G_SETTINGS (user_data);
324 gboolean ignore_this;
326 g_assert (settings->priv->backend == backend);
328 if (g_str_has_prefix (settings->priv->path, path))
329 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
330 0, (GQuark) 0, &ignore_this);
334 g_settings_constructed (GObject *object)
336 GSettings *settings = G_SETTINGS (object);
337 const gchar *schema_path;
339 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
340 schema_path = g_settings_schema_get_path (settings->priv->schema);
342 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
343 g_error ("settings object created with schema '%s' and path '%s', but "
344 "path '%s' is specified by schema",
345 settings->priv->schema_name, settings->priv->path, schema_path);
347 if (settings->priv->path == NULL)
349 if (schema_path == NULL)
350 g_error ("attempting to create schema '%s' without a path",
351 settings->priv->schema_name);
353 settings->priv->path = g_strdup (schema_path);
356 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
357 g_settings_backend_watch (settings->priv->backend,
358 settings_backend_changed,
359 settings_backend_path_changed,
360 settings_backend_keys_changed,
361 settings_backend_writable_changed,
362 settings_backend_path_writable_changed,
364 g_settings_backend_subscribe (settings->priv->backend,
365 settings->priv->path);
369 g_settings_init (GSettings *settings)
371 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
378 * @settings: a #GSettings object
380 * Changes the #GSettings object into 'delay-apply' mode. In this
381 * mode, changes to @settings are not immediately propagated to the
382 * backend, but kept locally until g_settings_apply() is called.
387 g_settings_delay (GSettings *settings)
389 if (settings->priv->delayed)
392 settings->priv->delayed =
393 g_delayed_settings_backend_new (settings->priv->backend, settings);
394 g_settings_backend_unwatch (settings->priv->backend, settings);
395 g_object_unref (settings->priv->backend);
397 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
398 g_settings_backend_watch (settings->priv->backend,
399 settings_backend_changed,
400 settings_backend_path_changed,
401 settings_backend_keys_changed,
402 settings_backend_writable_changed,
403 settings_backend_path_writable_changed,
409 * @settings: a #GSettings instance
411 * Applies any changes that have been made to the settings. This
412 * function does nothing unless @settings is in 'delay-apply' mode;
413 * see g_settings_set_delay_apply(). In the normal case settings are
414 * always applied immediately.
417 g_settings_apply (GSettings *settings)
419 if (settings->priv->delayed)
421 GDelayedSettingsBackend *delayed;
423 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
424 g_delayed_settings_backend_apply (delayed);
430 * @settings: a #GSettings instance
432 * Reverts all non-applied changes to the settings. This function
433 * does nothing unless @settings is in 'delay-apply' mode; see
434 * g_settings_set_delay_apply(). In the normal case settings are
435 * always applied immediately.
437 * Change notifications will be emitted for affected keys.
440 g_settings_revert (GSettings *settings)
442 if (settings->priv->delayed)
444 GDelayedSettingsBackend *delayed;
446 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
447 g_delayed_settings_backend_revert (delayed);
452 g_settings_set_property (GObject *object,
457 GSettings *settings = G_SETTINGS (object);
462 g_assert (settings->priv->schema_name == NULL);
463 settings->priv->schema_name = g_value_dup_string (value);
467 settings->priv->path = g_value_dup_string (value);
471 settings->priv->context = g_value_dup_string (value);
475 g_assert_not_reached ();
480 * g_settings_get_has_unapplied:
481 * @settings: a #GSettings object
482 * @returns: %TRUE if @settings has unapplied changes
484 * Returns whether the #GSettings object has any unapplied
485 * changes. This can only be the case if it is in 'delayed-apply' mode.
490 g_settings_get_has_unapplied (GSettings *settings)
492 return settings->priv->delayed &&
493 g_delayed_settings_backend_get_has_unapplied (
494 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
498 g_settings_get_property (GObject *object,
503 GSettings *settings = G_SETTINGS (object);
508 g_value_set_object (value, settings->priv->schema);
511 case PROP_HAS_UNAPPLIED:
512 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
516 g_assert_not_reached ();
521 g_settings_finalize (GObject *object)
523 GSettings *settings = G_SETTINGS (object);
525 g_settings_backend_unwatch (settings->priv->backend, settings);
526 g_settings_backend_unsubscribe (settings->priv->backend,
527 settings->priv->path);
528 g_object_unref (settings->priv->backend);
529 g_object_unref (settings->priv->schema);
530 g_free (settings->priv->schema_name);
531 g_free (settings->priv->path);
535 g_settings_class_init (GSettingsClass *class)
537 GObjectClass *object_class = G_OBJECT_CLASS (class);
539 class->writable_change_event = g_settings_real_writable_change_event;
540 class->change_event = g_settings_real_change_event;
542 object_class->set_property = g_settings_set_property;
543 object_class->get_property = g_settings_get_property;
544 object_class->constructed = g_settings_constructed;
545 object_class->finalize = g_settings_finalize;
547 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
550 * GSettings::changed:
551 * @settings: the object on which the signal was emitted
553 * The "changed" signal is emitted when a key has potentially changed.
554 * You should call one of the g_settings_get() calls to check the new
557 * This signal supports detailed connections. You can connect to the
558 * detailed signal "changed::x" in order to only receive callbacks
559 * when key "x" changes.
561 g_settings_signals[SIGNAL_CHANGED] =
562 g_signal_new ("changed", G_TYPE_SETTINGS,
563 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
564 G_STRUCT_OFFSET (GSettingsClass, changed),
565 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
566 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
569 * GSettings::change-event:
570 * @settings: the object on which the signal was emitted
571 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
572 * @n_keys: the length of the @keys array, or 0
573 * @returns: %TRUE to stop other handlers from being invoked for the
574 * event. FALSE to propagate the event further.
576 * The "change-event" signal is emitted once per change event that
577 * affects this settings object. You should connect to this signal
578 * only if you are interested in viewing groups of changes before they
579 * are split out into multiple emissions of the "changed" signal.
580 * For most use cases it is more appropriate to use the "changed" signal.
582 * In the event that the change event applies to one or more specified
583 * keys, @keys will be an array of #GQuark of length @n_keys. In the
584 * event that the change event applies to the #GSettings object as a
585 * whole (ie: potentially every key has been changed) then @keys will
586 * be %NULL and @n_keys will be 0.
588 * The default handler for this signal invokes the "changed" signal
589 * for each affected key. If any other connected handler returns
590 * %TRUE then this default functionality will be supressed.
592 g_settings_signals[SIGNAL_CHANGE_EVENT] =
593 g_signal_new ("change-event", G_TYPE_SETTINGS,
595 G_STRUCT_OFFSET (GSettingsClass, change_event),
596 g_signal_accumulator_true_handled, NULL,
597 _gio_marshal_BOOL__POINTER_INT,
598 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
601 * GSettings::writable-changed:
602 * @settings: the object on which the signal was emitted
605 * The "writable-changed" signal is emitted when the writability of a
606 * key has potentially changed. You should call
607 * g_settings_is_writable() in order to determine the new status.
609 * This signal supports detailed connections. You can connect to the
610 * detailed signal "writable-changed::x" in order to only receive
611 * callbacks when the writability of "x" changes.
613 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
614 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
615 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
616 G_STRUCT_OFFSET (GSettingsClass, changed),
617 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
618 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
621 * GSettings::writable-change-event:
622 * @settings: the object on which the signal was emitted
623 * @key: the quark of the key, or 0
624 * @returns: %TRUE to stop other handlers from being invoked for the
625 * event. FALSE to propagate the event further.
627 * The "writable-change-event" signal is emitted once per writability
628 * change event that affects this settings object. You should connect
629 * to this signal if you are interested in viewing groups of changes
630 * before they are split out into multiple emissions of the
631 * "writable-changed" signal. For most use cases it is more
632 * appropriate to use the "writable-changed" signal.
634 * In the event that the writability change applies only to a single
635 * key, @key will be set to the #GQuark for that key. In the event
636 * that the writability change affects the entire settings object,
639 * The default handler for this signal invokes the "writable-changed"
640 * and "changed" signals for each affected key. This is done because
641 * changes in writability might also imply changes in value (if for
642 * example, a new mandatory setting is introduced). If any other
643 * connected handler returns %TRUE then this default functionality
646 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
647 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
649 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
650 g_signal_accumulator_true_handled, NULL,
651 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
656 * The name of the context that the settings are stored in.
658 g_object_class_install_property (object_class, PROP_CONTEXT,
659 g_param_spec_string ("context",
661 P_("The name of the context for this settings object"),
662 "", G_PARAM_CONSTRUCT_ONLY |
663 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
668 * The name of the schema that describes the types of keys
669 * for this #GSettings object.
671 g_object_class_install_property (object_class, PROP_SCHEMA,
672 g_param_spec_string ("schema",
674 P_("The name of the schema for this settings object"),
676 G_PARAM_CONSTRUCT_ONLY |
677 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
682 * The path within the backend where the settings are stored.
684 g_object_class_install_property (object_class, PROP_PATH,
685 g_param_spec_string ("path",
687 P_("The path within the backend where the settings are"),
689 G_PARAM_CONSTRUCT_ONLY |
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693 * GSettings:has-unapplied:
695 * If this property is %TRUE, the #GSettings object has outstanding
696 * changes that will be applied when g_settings_apply() is called.
698 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
699 g_param_spec_boolean ("has-unapplied",
700 P_("Has unapplied changes"),
701 P_("TRUE if there are outstanding changes to apply()"),
703 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
708 * g_settings_get_value:
709 * @settings: a #GSettings object
710 * @key: the key to get the value for
711 * @returns: a new #GVariant
713 * Gets the value that is stored in @settings for @key.
715 * It is a programmer error to give a @key that isn't valid for
721 g_settings_get_value (GSettings *settings,
724 const gchar *unparsed = NULL;
725 GVariant *value, *options;
726 const GVariantType *type;
727 gchar lc_char = '\0';
731 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
733 if G_UNLIKELY (sval == NULL)
734 g_error ("schema '%s' does not contain a key named '%s'",
735 settings->priv->schema_name, key);
737 path = g_strconcat (settings->priv->path, key, NULL);
738 type = g_variant_get_type (sval);
739 value = g_settings_backend_read (settings->priv->backend, path, type);
746 GVariant *option_value;
748 g_variant_iter_init (&iter, options);
749 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
751 if (strcmp (option, "l10n") == 0)
752 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
754 g_warning ("unknown schema extension '%s'", option);
758 if (value && !g_variant_is_of_type (value, type))
760 g_variant_unref (value);
764 if (value == NULL && lc_char != '\0')
765 /* we will need to translate the schema default value */
767 const gchar *translated;
768 GError *error = NULL;
772 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
775 lc_category = LC_TIME;
777 lc_category = LC_MESSAGES;
779 translated = dcgettext (domain, unparsed, lc_category);
781 if (translated != unparsed)
782 /* it was translated, so we need to re-parse it */
784 value = g_variant_parse (g_variant_get_type (sval),
785 translated, NULL, NULL, &error);
789 g_warning ("Failed to parse translated string `%s' for "
790 "key `%s' in schema `%s': %s", unparsed, key,
791 settings->priv->schema_name, error->message);
792 g_warning ("Using untranslated default instead.");
793 g_error_free (error);
799 /* either translation failed or there was none to do.
800 * use the pre-compiled default.
802 value = g_variant_ref (sval);
804 g_variant_unref (sval);
810 * g_settings_set_value:
811 * @settings: a #GSettings object
812 * @key: the name of the key to set
813 * @value: a #GVariant of the correct type
814 * @returns: %TRUE if setting the key succeeded,
815 * %FALSE if the key was not writable
817 * Sets @key in @settings to @value.
819 * It is a programmer error to give a @key that isn't valid for
820 * @settings. It is a programmer error to give a @value of the
823 * If @value is floating then this function consumes the reference.
828 g_settings_set_value (GSettings *settings,
832 gboolean correct_type;
837 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
838 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
839 g_variant_unref (sval);
841 g_return_val_if_fail (correct_type, FALSE);
843 path = g_strconcat (settings->priv->path, key, NULL);
844 result = g_settings_backend_write (settings->priv->backend,
853 * @settings: a #GSettings object
854 * @key: the key to get the value for
855 * @format: a #GVariant format string
856 * @...: arguments as per @format
858 * Gets the value that is stored at @key in @settings.
860 * A convenience function that combines g_settings_get_value() with
863 * It is a programmer error to pass a @key that isn't valid for
864 * @settings or a @format_string that doesn't match the type of @key according
865 * to the schema of @settings.
870 g_settings_get (GSettings *settings,
878 value = g_settings_get_value (settings, key);
880 va_start (ap, format);
881 g_variant_get_va (value, format, NULL, &ap);
884 g_variant_unref (value);
889 * @settings: a #GSettings object
890 * @key: the name of the key to set
891 * @format: a #GVariant format string
892 * @...: arguments as per @format
893 * @returns: %TRUE if setting the key succeeded,
894 * %FALSE if the key was not writable
896 * Sets @key in @settings to @value.
898 * A convenience function that combines g_settings_set_value() with
901 * It is a programmer error to pass a @key that isn't valid for
902 * @settings or a @format that doesn't match the type of @key according
903 * to the schema of @settings.
908 g_settings_set (GSettings *settings,
916 va_start (ap, format);
917 value = g_variant_new_va (format, NULL, &ap);
920 return g_settings_set_value (settings, key, value);
924 * g_settings_is_writable:
925 * @settings: a #GSettings object
926 * @name: the name of a key
927 * @returns: %TRUE if the key @name is writable
929 * Finds out if a key can be written or not
934 g_settings_is_writable (GSettings *settings,
940 path = g_strconcat (settings->priv->path, name, NULL);
941 writable = g_settings_backend_get_writable (settings->priv->backend, path);
948 * g_settings_get_child:
949 * @settings: a #GSettings object
950 * @name: the name of the 'child' schema
951 * @returns: a 'child' settings object
953 * Creates a 'child' settings object which has a base path of
954 * <replaceable>base-path</replaceable>/@name", where
955 * <replaceable>base-path</replaceable> is the base path of @settings.
957 * The schema for the child settings object must have been declared
958 * in the schema of @settings using a <tag>child</tag> element.
963 g_settings_get_child (GSettings *settings,
966 GVariant *child_schema;
971 child_name = g_strconcat (name, "/", NULL);
972 child_schema = g_settings_schema_get_value (settings->priv->schema,
974 if (child_schema == NULL ||
975 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
976 g_error ("Schema '%s' has no child '%s'",
977 settings->priv->schema_name, name);
979 child_path = g_strconcat (settings->priv->path, child_name, NULL);
980 child = g_object_new (G_TYPE_SETTINGS,
981 "schema", g_variant_get_string (child_schema, NULL),
984 g_variant_unref (child_schema);
993 * @schema: the name of the schema
994 * @returns: a new #GSettings object
996 * Creates a new #GSettings object with a given schema.
1001 g_settings_new (const gchar *schema)
1003 return g_object_new (G_TYPE_SETTINGS,
1009 * g_settings_new_with_path:
1010 * @schema: the name of the schema
1011 * @path: the path to use
1012 * @returns: a new #GSettings object
1014 * Creates a new #GSettings object with a given schema and path.
1016 * You only need to do this if you want to directly create a settings
1017 * object with a schema that doesn't have a specified path of its own.
1018 * That's quite rare.
1020 * It is a programmer error to call this function for a schema that
1021 * has an explicitly specified path.
1026 g_settings_new_with_path (const gchar *schema,
1029 return g_object_new (G_TYPE_SETTINGS,
1036 * g_settings_new_with_context:
1037 * @schema: the name of the schema
1038 * @context: the context to use
1039 * @returns: a new #GSettings object
1041 * Creates a new #GSettings object with a given schema and context.
1043 * Creating settings objects with a context allow accessing settings
1044 * from a database other than the usual one. For example, it may make
1045 * sense to specify "defaults" in order to get a settings object that
1046 * modifies the system default settings instead of the settings for this
1049 * It is a programmer error to call this function for an unsupported
1050 * context. Use g_settings_supports_context() to determine if a context
1051 * is supported if you are unsure.
1056 g_settings_new_with_context (const gchar *schema,
1057 const gchar *context)
1059 return g_object_new (G_TYPE_SETTINGS,
1066 * g_settings_new_with_context_and_path:
1067 * @schema: the name of the schema
1068 * @path: the path to use
1069 * @returns: a new #GSettings object
1071 * Creates a new #GSettings object with a given schema, context and
1074 * This is a mix of g_settings_new_with_context() and
1075 * g_settings_new_with_path().
1080 g_settings_new_with_context_and_path (const gchar *schema,
1081 const gchar *context,
1084 return g_object_new (G_TYPE_SETTINGS,
1093 GSettings *settings;
1096 GSettingsBindGetMapping get_mapping;
1097 GSettingsBindSetMapping set_mapping;
1099 GDestroyNotify destroy;
1101 guint writable_handler_id;
1102 guint property_handler_id;
1103 const GParamSpec *property;
1104 guint key_handler_id;
1108 /* prevent recursion */
1113 g_settings_binding_free (gpointer data)
1115 GSettingsBinding *binding = data;
1117 g_assert (!binding->running);
1119 if (binding->writable_handler_id)
1120 g_signal_handler_disconnect (binding->settings,
1121 binding->writable_handler_id);
1123 if (binding->key_handler_id)
1124 g_signal_handler_disconnect (binding->settings,
1125 binding->key_handler_id);
1127 if (g_signal_handler_is_connected (binding->object,
1128 binding->property_handler_id))
1129 g_signal_handler_disconnect (binding->object,
1130 binding->property_handler_id);
1132 g_variant_type_free (binding->type);
1133 g_object_unref (binding->settings);
1135 if (binding->destroy)
1136 binding->destroy (binding->user_data);
1138 g_slice_free (GSettingsBinding, binding);
1142 g_settings_binding_quark (const char *property)
1147 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1148 quark = g_quark_from_string (tmp);
1155 g_settings_binding_key_changed (GSettings *settings,
1159 GSettingsBinding *binding = user_data;
1163 g_assert (settings == binding->settings);
1164 g_assert (key == binding->key);
1166 if (binding->running)
1169 binding->running = TRUE;
1171 g_value_init (&value, binding->property->value_type);
1172 variant = g_settings_get_value (settings, key);
1173 if (binding->get_mapping (&value, variant, binding->user_data))
1174 g_object_set_property (binding->object,
1175 binding->property->name,
1177 g_value_unset (&value);
1179 binding->running = FALSE;
1183 g_settings_binding_writable_changed (GSettings *settings,
1187 GSettingsBinding *binding = user_data;
1190 g_assert (settings == binding->settings);
1191 g_assert (key == binding->key);
1193 writable = g_settings_is_writable (settings, key);
1194 g_object_set (binding->object, "sensitive", writable, NULL);
1198 g_settings_binding_property_changed (GObject *object,
1199 const GParamSpec *pspec,
1202 GSettingsBinding *binding = user_data;
1206 g_assert (object == binding->object);
1207 g_assert (pspec == binding->property);
1209 if (binding->running)
1212 binding->running = TRUE;
1214 g_value_init (&value, pspec->value_type);
1215 g_object_get_property (object, pspec->name, &value);
1216 if ((variant = binding->set_mapping (&value, binding->type,
1217 binding->user_data)))
1219 g_settings_set_value (binding->settings,
1221 g_variant_ref_sink (variant));
1222 g_variant_unref (variant);
1224 g_value_unset (&value);
1226 binding->running = FALSE;
1231 * @settings: a #GSettings object
1232 * @key: the key to bind
1233 * @object: a #GObject
1234 * @property: the name of the property to bind
1235 * @flags: flags for the binding
1237 * Create a binding between the @key in the @settings object
1238 * and the property @property of @object.
1240 * The binding uses the default GIO mapping functions to map
1241 * between the settings and property values. These functions
1242 * handle booleans, numeric types and string types in a
1243 * straightforward way. Use g_settings_bind_with_mapping()
1244 * if you need a custom mapping, or map between types that
1245 * are not supported by the default mapping functions.
1247 * Note that the lifecycle of the binding is tied to the object,
1248 * and that you can have only one binding per object property.
1249 * If you bind the same property twice on the same object, the second
1250 * binding overrides the first one.
1255 g_settings_bind (GSettings *settings,
1258 const gchar *property,
1259 GSettingsBindFlags flags)
1261 g_settings_bind_with_mapping (settings, key, object, property,
1262 flags, NULL, NULL, NULL, NULL);
1266 * g_settings_bind_with_mapping:
1267 * @settings: a #GSettings object
1268 * @key: the key to bind
1269 * @object: a #GObject
1270 * @property: the name of the property to bind
1271 * @flags: flags for the binding
1272 * @get_mapping: a function that gets called to convert values
1273 * from @settings to @object, or %NULL to use the default GIO mapping
1274 * @set_mapping: a function that gets called to convert values
1275 * from @object to @settings, or %NULL to use the default GIO mapping
1276 * @user_data: data that gets passed to @get_mapping and @set_mapping
1277 * @destroy: #GDestroyNotify function for @user_data
1279 * Create a binding between the @key in the @settings object
1280 * and the property @property of @object.
1282 * The binding uses the provided mapping functions to map between
1283 * settings and property values.
1285 * Note that the lifecycle of the binding is tied to the object,
1286 * and that you can have only one binding per object property.
1287 * If you bind the same property twice on the same object, the second
1288 * binding overrides the first one.
1293 g_settings_bind_with_mapping (GSettings *settings,
1296 const gchar *property,
1297 GSettingsBindFlags flags,
1298 GSettingsBindGetMapping get_mapping,
1299 GSettingsBindSetMapping set_mapping,
1301 GDestroyNotify destroy)
1303 GSettingsBinding *binding;
1304 GObjectClass *objectclass;
1305 gboolean bind_sensitive;
1306 gchar *detailed_signal;
1307 GQuark binding_quark;
1309 objectclass = G_OBJECT_GET_CLASS (object);
1311 binding = g_slice_new0 (GSettingsBinding);
1312 binding->settings = g_object_ref (settings);
1313 binding->object = object;
1314 binding->key = g_intern_string (key);
1315 binding->property = g_object_class_find_property (objectclass, property);
1316 binding->user_data = user_data;
1317 binding->destroy = destroy;
1318 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1319 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1321 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1322 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1324 if (binding->property == NULL)
1326 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1327 property, G_OBJECT_TYPE_NAME (object));
1334 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1335 binding->type = g_variant_type_copy (g_variant_get_type (value));
1337 g_variant_unref (value);
1340 if (binding->type == NULL)
1342 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1343 key, settings->priv->schema_name);
1347 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1348 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1349 !g_settings_mapping_is_compatible (binding->property->value_type,
1352 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1353 "'%s' which is not compatible with type '%s' of key '%s'"
1354 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1355 g_type_name (binding->property->value_type),
1356 g_variant_type_dup_string (binding->type), key,
1357 settings->priv->schema_name);
1361 if ((flags & G_SETTINGS_BIND_SET) &&
1362 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1364 GParamSpec *sensitive;
1366 sensitive = g_object_class_find_property (objectclass, "sensitive");
1367 bind_sensitive = sensitive && sensitive->value_type == G_TYPE_BOOLEAN;
1370 bind_sensitive = FALSE;
1372 if (flags & G_SETTINGS_BIND_SET)
1374 detailed_signal = g_strdup_printf ("notify::%s", property);
1375 binding->property_handler_id =
1376 g_signal_connect (object, detailed_signal,
1377 G_CALLBACK (g_settings_binding_property_changed),
1379 g_free (detailed_signal);
1381 if (~flags & G_SETTINGS_BIND_GET)
1382 g_settings_binding_property_changed (object,
1387 if (flags & G_SETTINGS_BIND_GET)
1389 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1391 detailed_signal = g_strdup_printf ("changed::%s", key);
1392 binding->key_handler_id =
1393 g_signal_connect (settings, detailed_signal,
1394 G_CALLBACK (g_settings_binding_key_changed),
1396 g_free (detailed_signal);
1399 g_settings_binding_key_changed (settings, binding->key, binding);
1404 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1405 binding->writable_handler_id =
1406 g_signal_connect (settings, detailed_signal,
1407 G_CALLBACK (g_settings_binding_writable_changed),
1409 g_free (detailed_signal);
1411 g_settings_binding_writable_changed (settings, binding->key, binding);
1414 binding_quark = g_settings_binding_quark (property);
1415 g_object_set_qdata_full (object, binding_quark,
1416 binding, g_settings_binding_free);
1420 * g_settings_unbind:
1421 * @object: the object
1422 * @property: the property whose binding is removed
1424 * Removes an existing binding for @property on @object.
1426 * Note that bindings are automatically removed when the
1427 * object is finalized, so it is rarely necessary to call this
1433 g_settings_unbind (gpointer object,
1434 const gchar *property)
1436 GQuark binding_quark;
1438 binding_quark = g_settings_binding_quark (property);
1439 g_object_set_qdata (object, binding_quark, NULL);
1443 * g_settings_get_string:
1444 * @settings: a #GSettings object
1445 * @key: the key to get the value for
1446 * @returns: a newly-allocated string
1448 * Gets the value that is stored at @key in @settings.
1450 * A convenience variant of g_settings_get() for strings.
1452 * It is a programmer error to pass a @key that isn't valid for
1453 * @settings or is not of type string.
1458 g_settings_get_string (GSettings *settings,
1464 value = g_settings_get_value (settings, key);
1465 result = g_variant_dup_string (value, NULL);
1466 g_variant_unref (value);
1472 * g_settings_set_string:
1473 * @settings: a #GSettings object
1474 * @key: the name of the key to set
1475 * @value: the value to set it to
1476 * @returns: %TRUE if setting the key succeeded,
1477 * %FALSE if the key was not writable
1479 * Sets @key in @settings to @value.
1481 * A convenience variant of g_settings_set() for strings.
1483 * It is a programmer error to pass a @key that isn't valid for
1484 * @settings or is not of type string.
1489 g_settings_set_string (GSettings *settings,
1493 return g_settings_set_value (settings, key, g_variant_new_string (value));
1497 * g_settings_get_int:
1498 * @settings: a #GSettings object
1499 * @key: the key to get the value for
1500 * @returns: an integer
1502 * Gets the value that is stored at @key in @settings.
1504 * A convenience variant of g_settings_get() for 32-bit integers.
1506 * It is a programmer error to pass a @key that isn't valid for
1507 * @settings or is not of type int32.
1512 g_settings_get_int (GSettings *settings,
1518 value = g_settings_get_value (settings, key);
1519 result = g_variant_get_int32 (value);
1520 g_variant_unref (value);
1526 * g_settings_set_int:
1527 * @settings: a #GSettings object
1528 * @key: the name of the key to set
1529 * @value: the value to set it to
1530 * @returns: %TRUE if setting the key succeeded,
1531 * %FALSE if the key was not writable
1533 * Sets @key in @settings to @value.
1535 * A convenience variant of g_settings_set() for 32-bit integers.
1537 * It is a programmer error to pass a @key that isn't valid for
1538 * @settings or is not of type int32.
1543 g_settings_set_int (GSettings *settings,
1547 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1551 * g_settings_get_double:
1552 * @settings: a #GSettings object
1553 * @key: the key to get the value for
1554 * @returns: a double
1556 * Gets the value that is stored at @key in @settings.
1558 * A convenience variant of g_settings_get() for doubles.
1560 * It is a programmer error to pass a @key that isn't valid for
1561 * @settings or is not of type double.
1566 g_settings_get_double (GSettings *settings,
1572 value = g_settings_get_value (settings, key);
1573 result = g_variant_get_double (value);
1574 g_variant_unref (value);
1580 * g_settings_set_double:
1581 * @settings: a #GSettings object
1582 * @key: the name of the key to set
1583 * @value: the value to set it to
1584 * @returns: %TRUE if setting the key succeeded,
1585 * %FALSE if the key was not writable
1587 * Sets @key in @settings to @value.
1589 * A convenience variant of g_settings_set() for doubles.
1591 * It is a programmer error to pass a @key that isn't valid for
1592 * @settings or is not of type double.
1597 g_settings_set_double (GSettings *settings,
1601 return g_settings_set_value (settings, key, g_variant_new_double (value));
1605 * g_settings_get_boolean:
1606 * @settings: a #GSettings object
1607 * @key: the key to get the value for
1608 * @returns: a boolean
1610 * Gets the value that is stored at @key in @settings.
1612 * A convenience variant of g_settings_get() for booleans.
1614 * It is a programmer error to pass a @key that isn't valid for
1615 * @settings or is not of type boolean.
1620 g_settings_get_boolean (GSettings *settings,
1626 value = g_settings_get_value (settings, key);
1627 result = g_variant_get_boolean (value);
1628 g_variant_unref (value);
1634 * g_settings_set_boolean:
1635 * @settings: a #GSettings object
1636 * @key: the name of the key to set
1637 * @value: the value to set it to
1638 * @returns: %TRUE if setting the key succeeded,
1639 * %FALSE if the key was not writable
1641 * Sets @key in @settings to @value.
1643 * A convenience variant of g_settings_set() for booleans.
1645 * It is a programmer error to pass a @key that isn't valid for
1646 * @settings or is not of type boolean.
1651 g_settings_set_boolean (GSettings *settings,
1655 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1659 * g_settings_get_strv:
1660 * @settings: a #GSettings object
1661 * @key: the key to get the value for
1662 * @returns: a newly-allocated, %NULL-terminated array of strings
1664 * Gets the value that is stored at @key in @settings.
1666 * A convenience variant of g_settings_get() for string arrays.
1668 * It is a programmer error to pass a @key that isn't valid for
1669 * @settings or is not of type 'string array'.
1674 g_settings_get_strv (GSettings *settings,
1681 value = g_settings_get_value (settings, key);
1682 result = g_variant_dup_strv (value, length);
1683 g_variant_unref (value);
1689 * g_settings_set_strv:
1690 * @settings: a #GSettings object
1691 * @key: the name of the key to set
1692 * @value: the value to set it to
1693 * @returns: %TRUE if setting the key succeeded,
1694 * %FALSE if the key was not writable
1696 * Sets @key in @settings to @value.
1698 * A convenience variant of g_settings_set() for string arrays.
1700 * It is a programmer error to pass a @key that isn't valid for
1701 * @settings or is not of type 'string array'.
1706 g_settings_set_strv (GSettings *settings,
1708 const gchar * const *value,
1711 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1714 #define __G_SETTINGS_C__
1715 #include "gioaliasdef.c"