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
552 * @key: the name of the key that changed
554 * The "changed" signal is emitted when a key has potentially changed.
555 * You should call one of the g_settings_get() calls to check the new
558 * This signal supports detailed connections. You can connect to the
559 * detailed signal "changed::x" in order to only receive callbacks
560 * when key "x" changes.
562 g_settings_signals[SIGNAL_CHANGED] =
563 g_signal_new ("changed", G_TYPE_SETTINGS,
564 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
565 G_STRUCT_OFFSET (GSettingsClass, changed),
566 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
567 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
570 * GSettings::change-event:
571 * @settings: the object on which the signal was emitted
572 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
573 * @n_keys: the length of the @keys array, or 0
574 * @returns: %TRUE to stop other handlers from being invoked for the
575 * event. FALSE to propagate the event further.
577 * The "change-event" signal is emitted once per change event that
578 * affects this settings object. You should connect to this signal
579 * only if you are interested in viewing groups of changes before they
580 * are split out into multiple emissions of the "changed" signal.
581 * For most use cases it is more appropriate to use the "changed" signal.
583 * In the event that the change event applies to one or more specified
584 * keys, @keys will be an array of #GQuark of length @n_keys. In the
585 * event that the change event applies to the #GSettings object as a
586 * whole (ie: potentially every key has been changed) then @keys will
587 * be %NULL and @n_keys will be 0.
589 * The default handler for this signal invokes the "changed" signal
590 * for each affected key. If any other connected handler returns
591 * %TRUE then this default functionality will be supressed.
593 g_settings_signals[SIGNAL_CHANGE_EVENT] =
594 g_signal_new ("change-event", G_TYPE_SETTINGS,
596 G_STRUCT_OFFSET (GSettingsClass, change_event),
597 g_signal_accumulator_true_handled, NULL,
598 _gio_marshal_BOOL__POINTER_INT,
599 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
602 * GSettings::writable-changed:
603 * @settings: the object on which the signal was emitted
606 * The "writable-changed" signal is emitted when the writability of a
607 * key has potentially changed. You should call
608 * g_settings_is_writable() in order to determine the new status.
610 * This signal supports detailed connections. You can connect to the
611 * detailed signal "writable-changed::x" in order to only receive
612 * callbacks when the writability of "x" changes.
614 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
615 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
616 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
617 G_STRUCT_OFFSET (GSettingsClass, changed),
618 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
619 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
622 * GSettings::writable-change-event:
623 * @settings: the object on which the signal was emitted
624 * @key: the quark of the key, or 0
625 * @returns: %TRUE to stop other handlers from being invoked for the
626 * event. FALSE to propagate the event further.
628 * The "writable-change-event" signal is emitted once per writability
629 * change event that affects this settings object. You should connect
630 * to this signal if you are interested in viewing groups of changes
631 * before they are split out into multiple emissions of the
632 * "writable-changed" signal. For most use cases it is more
633 * appropriate to use the "writable-changed" signal.
635 * In the event that the writability change applies only to a single
636 * key, @key will be set to the #GQuark for that key. In the event
637 * that the writability change affects the entire settings object,
640 * The default handler for this signal invokes the "writable-changed"
641 * and "changed" signals for each affected key. This is done because
642 * changes in writability might also imply changes in value (if for
643 * example, a new mandatory setting is introduced). If any other
644 * connected handler returns %TRUE then this default functionality
647 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
648 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
650 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
651 g_signal_accumulator_true_handled, NULL,
652 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
657 * The name of the context that the settings are stored in.
659 g_object_class_install_property (object_class, PROP_CONTEXT,
660 g_param_spec_string ("context",
662 P_("The name of the context for this settings object"),
663 "", G_PARAM_CONSTRUCT_ONLY |
664 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
669 * The name of the schema that describes the types of keys
670 * for this #GSettings object.
672 g_object_class_install_property (object_class, PROP_SCHEMA,
673 g_param_spec_string ("schema",
675 P_("The name of the schema for this settings object"),
677 G_PARAM_CONSTRUCT_ONLY |
678 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
683 * The path within the backend where the settings are stored.
685 g_object_class_install_property (object_class, PROP_PATH,
686 g_param_spec_string ("path",
688 P_("The path within the backend where the settings are"),
690 G_PARAM_CONSTRUCT_ONLY |
691 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 * GSettings:has-unapplied:
696 * If this property is %TRUE, the #GSettings object has outstanding
697 * changes that will be applied when g_settings_apply() is called.
699 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
700 g_param_spec_boolean ("has-unapplied",
701 P_("Has unapplied changes"),
702 P_("TRUE if there are outstanding changes to apply()"),
704 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
709 * g_settings_get_value:
710 * @settings: a #GSettings object
711 * @key: the key to get the value for
712 * @returns: a new #GVariant
714 * Gets the value that is stored in @settings for @key.
716 * It is a programmer error to give a @key that isn't valid for
722 g_settings_get_value (GSettings *settings,
725 const gchar *unparsed = NULL;
726 GVariant *value, *options;
727 const GVariantType *type;
728 gchar lc_char = '\0';
732 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
734 if G_UNLIKELY (sval == NULL)
735 g_error ("schema '%s' does not contain a key named '%s'",
736 settings->priv->schema_name, key);
738 path = g_strconcat (settings->priv->path, key, NULL);
739 type = g_variant_get_type (sval);
740 value = g_settings_backend_read (settings->priv->backend, path, type);
747 GVariant *option_value;
749 g_variant_iter_init (&iter, options);
750 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
752 if (strcmp (option, "l10n") == 0)
753 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
755 g_warning ("unknown schema extension '%s'", option);
759 if (value && !g_variant_is_of_type (value, type))
761 g_variant_unref (value);
765 if (value == NULL && lc_char != '\0')
766 /* we will need to translate the schema default value */
768 const gchar *translated;
769 GError *error = NULL;
773 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
776 lc_category = LC_TIME;
778 lc_category = LC_MESSAGES;
780 translated = dcgettext (domain, unparsed, lc_category);
782 if (translated != unparsed)
783 /* it was translated, so we need to re-parse it */
785 value = g_variant_parse (g_variant_get_type (sval),
786 translated, NULL, NULL, &error);
790 g_warning ("Failed to parse translated string `%s' for "
791 "key `%s' in schema `%s': %s", unparsed, key,
792 settings->priv->schema_name, error->message);
793 g_warning ("Using untranslated default instead.");
794 g_error_free (error);
800 /* either translation failed or there was none to do.
801 * use the pre-compiled default.
803 value = g_variant_ref (sval);
805 g_variant_unref (sval);
811 * g_settings_set_value:
812 * @settings: a #GSettings object
813 * @key: the name of the key to set
814 * @value: a #GVariant of the correct type
815 * @returns: %TRUE if setting the key succeeded,
816 * %FALSE if the key was not writable
818 * Sets @key in @settings to @value.
820 * It is a programmer error to give a @key that isn't valid for
821 * @settings. It is a programmer error to give a @value of the
824 * If @value is floating then this function consumes the reference.
829 g_settings_set_value (GSettings *settings,
833 gboolean correct_type;
838 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
839 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
840 g_variant_unref (sval);
842 g_return_val_if_fail (correct_type, FALSE);
844 path = g_strconcat (settings->priv->path, key, NULL);
845 result = g_settings_backend_write (settings->priv->backend,
854 * @settings: a #GSettings object
855 * @key: the key to get the value for
856 * @format: a #GVariant format string
857 * @...: arguments as per @format
859 * Gets the value that is stored at @key in @settings.
861 * A convenience function that combines g_settings_get_value() with
864 * It is a programmer error to pass a @key that isn't valid for
865 * @settings or a @format_string that doesn't match the type of @key according
866 * to the schema of @settings.
871 g_settings_get (GSettings *settings,
879 value = g_settings_get_value (settings, key);
881 va_start (ap, format);
882 g_variant_get_va (value, format, NULL, &ap);
885 g_variant_unref (value);
890 * @settings: a #GSettings object
891 * @key: the name of the key to set
892 * @format: a #GVariant format string
893 * @...: arguments as per @format
894 * @returns: %TRUE if setting the key succeeded,
895 * %FALSE if the key was not writable
897 * Sets @key in @settings to @value.
899 * A convenience function that combines g_settings_set_value() with
902 * It is a programmer error to pass a @key that isn't valid for
903 * @settings or a @format that doesn't match the type of @key according
904 * to the schema of @settings.
909 g_settings_set (GSettings *settings,
917 va_start (ap, format);
918 value = g_variant_new_va (format, NULL, &ap);
921 return g_settings_set_value (settings, key, value);
925 * g_settings_is_writable:
926 * @settings: a #GSettings object
927 * @name: the name of a key
928 * @returns: %TRUE if the key @name is writable
930 * Finds out if a key can be written or not
935 g_settings_is_writable (GSettings *settings,
941 path = g_strconcat (settings->priv->path, name, NULL);
942 writable = g_settings_backend_get_writable (settings->priv->backend, path);
949 * g_settings_get_child:
950 * @settings: a #GSettings object
951 * @name: the name of the 'child' schema
952 * @returns: a 'child' settings object
954 * Creates a 'child' settings object which has a base path of
955 * <replaceable>base-path</replaceable>/@name", where
956 * <replaceable>base-path</replaceable> is the base path of @settings.
958 * The schema for the child settings object must have been declared
959 * in the schema of @settings using a <tag>child</tag> element.
964 g_settings_get_child (GSettings *settings,
967 GVariant *child_schema;
972 child_name = g_strconcat (name, "/", NULL);
973 child_schema = g_settings_schema_get_value (settings->priv->schema,
975 if (child_schema == NULL ||
976 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
977 g_error ("Schema '%s' has no child '%s'",
978 settings->priv->schema_name, name);
980 child_path = g_strconcat (settings->priv->path, child_name, NULL);
981 child = g_object_new (G_TYPE_SETTINGS,
982 "schema", g_variant_get_string (child_schema, NULL),
985 g_variant_unref (child_schema);
994 * @schema: the name of the schema
995 * @returns: a new #GSettings object
997 * Creates a new #GSettings object with a given schema.
1002 g_settings_new (const gchar *schema)
1004 return g_object_new (G_TYPE_SETTINGS,
1010 * g_settings_new_with_path:
1011 * @schema: the name of the schema
1012 * @path: the path to use
1013 * @returns: a new #GSettings object
1015 * Creates a new #GSettings object with a given schema and path.
1017 * You only need to do this if you want to directly create a settings
1018 * object with a schema that doesn't have a specified path of its own.
1019 * That's quite rare.
1021 * It is a programmer error to call this function for a schema that
1022 * has an explicitly specified path.
1027 g_settings_new_with_path (const gchar *schema,
1030 return g_object_new (G_TYPE_SETTINGS,
1037 * g_settings_new_with_context:
1038 * @schema: the name of the schema
1039 * @context: the context to use
1040 * @returns: a new #GSettings object
1042 * Creates a new #GSettings object with a given schema and context.
1044 * Creating settings objects with a context allow accessing settings
1045 * from a database other than the usual one. For example, it may make
1046 * sense to specify "defaults" in order to get a settings object that
1047 * modifies the system default settings instead of the settings for this
1050 * It is a programmer error to call this function for an unsupported
1051 * context. Use g_settings_supports_context() to determine if a context
1052 * is supported if you are unsure.
1057 g_settings_new_with_context (const gchar *schema,
1058 const gchar *context)
1060 return g_object_new (G_TYPE_SETTINGS,
1067 * g_settings_new_with_context_and_path:
1068 * @schema: the name of the schema
1069 * @path: the path to use
1070 * @returns: a new #GSettings object
1072 * Creates a new #GSettings object with a given schema, context and
1075 * This is a mix of g_settings_new_with_context() and
1076 * g_settings_new_with_path().
1081 g_settings_new_with_context_and_path (const gchar *schema,
1082 const gchar *context,
1085 return g_object_new (G_TYPE_SETTINGS,
1094 GSettings *settings;
1097 GSettingsBindGetMapping get_mapping;
1098 GSettingsBindSetMapping set_mapping;
1100 GDestroyNotify destroy;
1102 guint writable_handler_id;
1103 guint property_handler_id;
1104 const GParamSpec *property;
1105 guint key_handler_id;
1109 /* prevent recursion */
1114 g_settings_binding_free (gpointer data)
1116 GSettingsBinding *binding = data;
1118 g_assert (!binding->running);
1120 if (binding->writable_handler_id)
1121 g_signal_handler_disconnect (binding->settings,
1122 binding->writable_handler_id);
1124 if (binding->key_handler_id)
1125 g_signal_handler_disconnect (binding->settings,
1126 binding->key_handler_id);
1128 if (g_signal_handler_is_connected (binding->object,
1129 binding->property_handler_id))
1130 g_signal_handler_disconnect (binding->object,
1131 binding->property_handler_id);
1133 g_variant_type_free (binding->type);
1134 g_object_unref (binding->settings);
1136 if (binding->destroy)
1137 binding->destroy (binding->user_data);
1139 g_slice_free (GSettingsBinding, binding);
1143 g_settings_binding_quark (const char *property)
1148 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1149 quark = g_quark_from_string (tmp);
1156 g_settings_binding_key_changed (GSettings *settings,
1160 GSettingsBinding *binding = user_data;
1164 g_assert (settings == binding->settings);
1165 g_assert (key == binding->key);
1167 if (binding->running)
1170 binding->running = TRUE;
1172 g_value_init (&value, binding->property->value_type);
1173 variant = g_settings_get_value (settings, key);
1174 if (binding->get_mapping (&value, variant, binding->user_data))
1175 g_object_set_property (binding->object,
1176 binding->property->name,
1178 g_value_unset (&value);
1180 binding->running = FALSE;
1184 g_settings_binding_property_changed (GObject *object,
1185 const GParamSpec *pspec,
1188 GSettingsBinding *binding = user_data;
1192 g_assert (object == binding->object);
1193 g_assert (pspec == binding->property);
1195 if (binding->running)
1198 binding->running = TRUE;
1200 g_value_init (&value, pspec->value_type);
1201 g_object_get_property (object, pspec->name, &value);
1202 if ((variant = binding->set_mapping (&value, binding->type,
1203 binding->user_data)))
1205 g_settings_set_value (binding->settings,
1207 g_variant_ref_sink (variant));
1208 g_variant_unref (variant);
1210 g_value_unset (&value);
1212 binding->running = FALSE;
1217 * @settings: a #GSettings object
1218 * @key: the key to bind
1219 * @object: a #GObject
1220 * @property: the name of the property to bind
1221 * @flags: flags for the binding
1223 * Create a binding between the @key in the @settings object
1224 * and the property @property of @object.
1226 * The binding uses the default GIO mapping functions to map
1227 * between the settings and property values. These functions
1228 * handle booleans, numeric types and string types in a
1229 * straightforward way. Use g_settings_bind_with_mapping()
1230 * if you need a custom mapping, or map between types that
1231 * are not supported by the default mapping functions.
1233 * Note that the lifecycle of the binding is tied to the object,
1234 * and that you can have only one binding per object property.
1235 * If you bind the same property twice on the same object, the second
1236 * binding overrides the first one.
1241 g_settings_bind (GSettings *settings,
1244 const gchar *property,
1245 GSettingsBindFlags flags)
1247 g_settings_bind_with_mapping (settings, key, object, property,
1248 flags, NULL, NULL, NULL, NULL);
1252 * g_settings_bind_with_mapping:
1253 * @settings: a #GSettings object
1254 * @key: the key to bind
1255 * @object: a #GObject
1256 * @property: the name of the property to bind
1257 * @flags: flags for the binding
1258 * @get_mapping: a function that gets called to convert values
1259 * from @settings to @object, or %NULL to use the default GIO mapping
1260 * @set_mapping: a function that gets called to convert values
1261 * from @object to @settings, or %NULL to use the default GIO mapping
1262 * @user_data: data that gets passed to @get_mapping and @set_mapping
1263 * @destroy: #GDestroyNotify function for @user_data
1265 * Create a binding between the @key in the @settings object
1266 * and the property @property of @object.
1268 * The binding uses the provided mapping functions to map between
1269 * settings and property values.
1271 * Note that the lifecycle of the binding is tied to the object,
1272 * and that you can have only one binding per object property.
1273 * If you bind the same property twice on the same object, the second
1274 * binding overrides the first one.
1279 g_settings_bind_with_mapping (GSettings *settings,
1282 const gchar *property,
1283 GSettingsBindFlags flags,
1284 GSettingsBindGetMapping get_mapping,
1285 GSettingsBindSetMapping set_mapping,
1287 GDestroyNotify destroy)
1289 GSettingsBinding *binding;
1290 GObjectClass *objectclass;
1291 gchar *detailed_signal;
1292 GQuark binding_quark;
1294 objectclass = G_OBJECT_GET_CLASS (object);
1296 binding = g_slice_new0 (GSettingsBinding);
1297 binding->settings = g_object_ref (settings);
1298 binding->object = object;
1299 binding->key = g_intern_string (key);
1300 binding->property = g_object_class_find_property (objectclass, property);
1301 binding->user_data = user_data;
1302 binding->destroy = destroy;
1303 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1304 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1306 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1307 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1309 if (binding->property == NULL)
1311 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1312 property, G_OBJECT_TYPE_NAME (object));
1319 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1320 binding->type = g_variant_type_copy (g_variant_get_type (value));
1322 g_variant_unref (value);
1325 if (binding->type == NULL)
1327 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1328 key, settings->priv->schema_name);
1332 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1333 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1334 !g_settings_mapping_is_compatible (binding->property->value_type,
1337 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1338 "'%s' which is not compatible with type '%s' of key '%s'"
1339 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1340 g_type_name (binding->property->value_type),
1341 g_variant_type_dup_string (binding->type), key,
1342 settings->priv->schema_name);
1346 if ((flags & G_SETTINGS_BIND_SET) &&
1347 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1349 GParamSpec *sensitive;
1351 sensitive = g_object_class_find_property (objectclass, "sensitive");
1353 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN)
1354 g_settings_bind_writable (settings, binding->key,
1355 object, "sensitive", FALSE);
1358 if (flags & G_SETTINGS_BIND_SET)
1360 detailed_signal = g_strdup_printf ("notify::%s", property);
1361 binding->property_handler_id =
1362 g_signal_connect (object, detailed_signal,
1363 G_CALLBACK (g_settings_binding_property_changed),
1365 g_free (detailed_signal);
1367 if (~flags & G_SETTINGS_BIND_GET)
1368 g_settings_binding_property_changed (object,
1373 if (flags & G_SETTINGS_BIND_GET)
1375 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1377 detailed_signal = g_strdup_printf ("changed::%s", key);
1378 binding->key_handler_id =
1379 g_signal_connect (settings, detailed_signal,
1380 G_CALLBACK (g_settings_binding_key_changed),
1382 g_free (detailed_signal);
1385 g_settings_binding_key_changed (settings, binding->key, binding);
1388 binding_quark = g_settings_binding_quark (property);
1389 g_object_set_qdata_full (object, binding_quark,
1390 binding, g_settings_binding_free);
1395 GSettings *settings;
1398 const gchar *property;
1401 } GSettingsWritableBinding;
1404 g_settings_writable_binding_free (gpointer data)
1406 GSettingsWritableBinding *binding = data;
1408 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1409 g_object_unref (binding->settings);
1413 g_settings_binding_writable_changed (GSettings *settings,
1417 GSettingsWritableBinding *binding = user_data;
1420 g_assert (settings == binding->settings);
1421 g_assert (key == binding->key);
1423 writable = g_settings_is_writable (settings, key);
1425 if (binding->inverted)
1426 writable = !writable;
1428 g_object_set (binding->object, binding->property, writable, NULL);
1433 g_settings_bind_writable (GSettings *settings,
1436 const gchar *property,
1439 GSettingsWritableBinding *binding;
1440 gchar *detailed_signal;
1442 binding = g_slice_new (GSettingsWritableBinding);
1443 binding->settings = g_object_ref (settings);
1444 binding->object = object;
1445 binding->property = g_intern_string (property);
1446 binding->inverted = inverted;
1448 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1449 binding->handler_id =
1450 g_signal_connect (settings, detailed_signal,
1451 G_CALLBACK (g_settings_binding_writable_changed),
1453 g_free (detailed_signal);
1455 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1456 binding, g_settings_writable_binding_free);
1458 g_settings_binding_writable_changed (settings, binding->key, binding);
1462 * g_settings_unbind:
1463 * @object: the object
1464 * @property: the property whose binding is removed
1466 * Removes an existing binding for @property on @object.
1468 * Note that bindings are automatically removed when the
1469 * object is finalized, so it is rarely necessary to call this
1475 g_settings_unbind (gpointer object,
1476 const gchar *property)
1478 GQuark binding_quark;
1480 binding_quark = g_settings_binding_quark (property);
1481 g_object_set_qdata (object, binding_quark, NULL);
1485 * g_settings_get_string:
1486 * @settings: a #GSettings object
1487 * @key: the key to get the value for
1488 * @returns: a newly-allocated string
1490 * Gets the value that is stored at @key in @settings.
1492 * A convenience variant of g_settings_get() for strings.
1494 * It is a programmer error to pass a @key that isn't valid for
1495 * @settings or is not of type string.
1500 g_settings_get_string (GSettings *settings,
1506 value = g_settings_get_value (settings, key);
1507 result = g_variant_dup_string (value, NULL);
1508 g_variant_unref (value);
1514 * g_settings_set_string:
1515 * @settings: a #GSettings object
1516 * @key: the name of the key to set
1517 * @value: the value to set it to
1518 * @returns: %TRUE if setting the key succeeded,
1519 * %FALSE if the key was not writable
1521 * Sets @key in @settings to @value.
1523 * A convenience variant of g_settings_set() for strings.
1525 * It is a programmer error to pass a @key that isn't valid for
1526 * @settings or is not of type string.
1531 g_settings_set_string (GSettings *settings,
1535 return g_settings_set_value (settings, key, g_variant_new_string (value));
1539 * g_settings_get_int:
1540 * @settings: a #GSettings object
1541 * @key: the key to get the value for
1542 * @returns: an integer
1544 * Gets the value that is stored at @key in @settings.
1546 * A convenience variant of g_settings_get() for 32-bit integers.
1548 * It is a programmer error to pass a @key that isn't valid for
1549 * @settings or is not of type int32.
1554 g_settings_get_int (GSettings *settings,
1560 value = g_settings_get_value (settings, key);
1561 result = g_variant_get_int32 (value);
1562 g_variant_unref (value);
1568 * g_settings_set_int:
1569 * @settings: a #GSettings object
1570 * @key: the name of the key to set
1571 * @value: the value to set it to
1572 * @returns: %TRUE if setting the key succeeded,
1573 * %FALSE if the key was not writable
1575 * Sets @key in @settings to @value.
1577 * A convenience variant of g_settings_set() for 32-bit integers.
1579 * It is a programmer error to pass a @key that isn't valid for
1580 * @settings or is not of type int32.
1585 g_settings_set_int (GSettings *settings,
1589 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1593 * g_settings_get_double:
1594 * @settings: a #GSettings object
1595 * @key: the key to get the value for
1596 * @returns: a double
1598 * Gets the value that is stored at @key in @settings.
1600 * A convenience variant of g_settings_get() for doubles.
1602 * It is a programmer error to pass a @key that isn't valid for
1603 * @settings or is not of type double.
1608 g_settings_get_double (GSettings *settings,
1614 value = g_settings_get_value (settings, key);
1615 result = g_variant_get_double (value);
1616 g_variant_unref (value);
1622 * g_settings_set_double:
1623 * @settings: a #GSettings object
1624 * @key: the name of the key to set
1625 * @value: the value to set it to
1626 * @returns: %TRUE if setting the key succeeded,
1627 * %FALSE if the key was not writable
1629 * Sets @key in @settings to @value.
1631 * A convenience variant of g_settings_set() for doubles.
1633 * It is a programmer error to pass a @key that isn't valid for
1634 * @settings or is not of type double.
1639 g_settings_set_double (GSettings *settings,
1643 return g_settings_set_value (settings, key, g_variant_new_double (value));
1647 * g_settings_get_boolean:
1648 * @settings: a #GSettings object
1649 * @key: the key to get the value for
1650 * @returns: a boolean
1652 * Gets the value that is stored at @key in @settings.
1654 * A convenience variant of g_settings_get() for booleans.
1656 * It is a programmer error to pass a @key that isn't valid for
1657 * @settings or is not of type boolean.
1662 g_settings_get_boolean (GSettings *settings,
1668 value = g_settings_get_value (settings, key);
1669 result = g_variant_get_boolean (value);
1670 g_variant_unref (value);
1676 * g_settings_set_boolean:
1677 * @settings: a #GSettings object
1678 * @key: the name of the key to set
1679 * @value: the value to set it to
1680 * @returns: %TRUE if setting the key succeeded,
1681 * %FALSE if the key was not writable
1683 * Sets @key in @settings to @value.
1685 * A convenience variant of g_settings_set() for booleans.
1687 * It is a programmer error to pass a @key that isn't valid for
1688 * @settings or is not of type boolean.
1693 g_settings_set_boolean (GSettings *settings,
1697 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1701 * g_settings_get_strv:
1702 * @settings: a #GSettings object
1703 * @key: the key to get the value for
1704 * @returns: a newly-allocated, %NULL-terminated array of strings
1706 * Gets the value that is stored at @key in @settings.
1708 * A convenience variant of g_settings_get() for string arrays.
1710 * It is a programmer error to pass a @key that isn't valid for
1711 * @settings or is not of type 'string array'.
1716 g_settings_get_strv (GSettings *settings,
1723 value = g_settings_get_value (settings, key);
1724 result = g_variant_dup_strv (value, length);
1725 g_variant_unref (value);
1731 * g_settings_set_strv:
1732 * @settings: a #GSettings object
1733 * @key: the name of the key to set
1734 * @value: the value to set it to
1735 * @returns: %TRUE if setting the key succeeded,
1736 * %FALSE if the key was not writable
1738 * Sets @key in @settings to @value.
1740 * A convenience variant of g_settings_set() for string arrays.
1742 * It is a programmer error to pass a @key that isn't valid for
1743 * @settings or is not of type 'string array'.
1748 g_settings_set_strv (GSettings *settings,
1750 const gchar * const *value,
1753 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1756 #define __G_SETTINGS_C__
1757 #include "gioaliasdef.c"