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>
28 #include "gsettings.h"
30 #include "gdelayedsettingsbackend.h"
31 #include "gsettingsbackendinternal.h"
32 #include "gsettings-mapping.h"
33 #include "gsettingsschema-internal.h"
40 * @short_description: High-level API for application settings
42 * The #GSettings class provides a convenient API for storing and retrieving
43 * application settings.
45 * Reads and writes can be considered to be non-blocking. Reading
46 * settings with #GSettings is typically extremely fast: on
47 * approximately the same order of magnitude (but slower than) a
48 * #GHashTable lookup. Writing settings is also extremely fast in terms
49 * of time to return to your application, but can be extremely expensive
50 * for other threads and other processes. Many settings backends
51 * (including dconf) have lazy initialisation which means in the common
52 * case of the user using their computer without modifying any settings
53 * a lot of work can be avoided. For dconf, the D-Bus service doesn't
54 * even need to be started in this case. For this reason, you should
55 * only ever modify #GSettings keys in response to explicit user action.
56 * Particular care should be paid to ensure that modifications are not
57 * made during startup -- for example, when setting the initial value
58 * of preferences widgets. The built-in g_settings_bind() functionality
59 * is careful not to write settings in response to notify signals as a
60 * result of modifications that it makes to widgets.
62 * When creating a GSettings instance, you have to specify a schema
63 * that describes the keys in your settings and their types and default
64 * values, as well as some other information.
66 * Normally, a schema has as fixed path that determines where the settings
67 * are stored in the conceptual global tree of settings. However, schemas
68 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
69 * useful e.g. when the schema describes an 'account', and you want to be
70 * able to store a arbitrary number of accounts.
72 * Unlike other configuration systems (like GConf), GSettings does not
73 * restrict keys to basic types like strings and numbers. GSettings stores
74 * values as #GVariant, and allows any #GVariantType for keys. Key names
75 * are restricted to lowercase characters, numbers and '-'. Furthermore,
76 * the names must begin with a lowercase character, must not end
77 * with a '-', and must not contain consecutive dashes.
79 * Similar to GConf, the default values in GSettings schemas can be
80 * localized, but the localized values are stored in gettext catalogs
81 * and looked up with the domain that is specified in the
82 * <tag class="attribute">gettext-domain</tag> attribute of the
83 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
84 * elements and the category that is specified in the l10n attribute of the
85 * <tag class="starttag">key</tag> element.
87 * GSettings uses schemas in a compact binary form that is created
88 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
89 * utility. The input is a schema description in an XML format that can be
90 * described by the following DTD:
91 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
93 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
95 * At runtime, schemas are identified by their id (as specified
96 * in the <tag class="attribute">id</tag> attribute of the
97 * <tag class="starttag">schema</tag> element). The
98 * convention for schema ids is to use a dotted name, similar in
99 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
100 * if the settings are for a specific service that owns a D-Bus bus name,
101 * the D-Bus bus name and schema id should match. For schemas which deal
102 * with settings not associated with one named application, the id should
103 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
105 * In addition to #GVariant types, keys can have types that have enumerated
106 * types. These can be described by a <tag class="starttag">choice</tag>,
107 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
108 * <xref linkend="schema-enumerated"/>. The underlying type of
109 * such a key is string, but you can use g_settings_get_enum(),
110 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
111 * access the numeric values corresponding to the string value of enum
114 * <example id="schema-default-values"><title>Default values</title>
115 * <programlisting><![CDATA[
117 * <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
119 * <key name="greeting" type="s">
120 * <default l10n="messages">"Hello, earthlings"</default>
121 * <summary>A greeting</summary>
123 * Greeting of the invading martians
127 * <key name="box" type="(ii)">
128 * <default>(20,30)</default>
133 * ]]></programlisting></example>
135 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
136 * <programlisting><![CDATA[
139 * <enum id="org.gtk.Test.myenum">
140 * <value nick="first" value="1"/>
141 * <value nick="second" value="2"/>
144 * <flags id="org.gtk.Test.myflags">
145 * <value nick="flag1" value="1"/>
146 * <value nick="flag2" value="2"/>
147 * <value nick="flag3" value="4"/>
150 * <schema id="org.gtk.Test">
152 * <key name="key-with-range" type="i">
153 * <range min="1" max="100"/>
154 * <default>10</default>
157 * <key name="key-with-choices" type="s">
159 * <choice value='Elisabeth'/>
160 * <choice value='Annabeth'/>
161 * <choice value='Joe'/>
164 * <alias value='Anna' target='Annabeth'/>
165 * <alias value='Beth' target='Elisabeth'/>
167 * <default>'Joe'</default>
170 * <key name='enumerated-key' enum='org.gtk.Test.myenum'>
171 * <default>'first'</default>
174 * <key name='flags-key' flags='org.gtk.Test.myflags'>
175 * <default>["flag1",flag2"]</default>
179 * ]]></programlisting></example>
182 * <title>Vendor overrides</title>
184 * Default values are defined in the schemas that get installed by
185 * an application. Sometimes, it is necessary for a vendor or distributor
186 * to adjust these defaults. Since patching the XML source for the schema
187 * is inconvenient and error-prone,
188 * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
189 * so-called 'vendor override' files. These are keyfiles in the same
190 * directory as the XML schema sources which can override default values.
191 * The schema id serves as the group name in the key file, and the values
192 * are expected in serialized GVariant form, as in the following example:
193 * <informalexample><programlisting>
197 * </programlisting></informalexample>
200 * glib-compile-schemas expects schema files to have the extension
201 * <filename>.gschema.override</filename>
206 * <title>Binding</title>
208 * A very convenient feature of GSettings lets you bind #GObject properties
209 * directly to settings, using g_settings_bind(). Once a GObject property
210 * has been bound to a setting, changes on either side are automatically
211 * propagated to the other side. GSettings handles details like
212 * mapping between GObject and GVariant types, and preventing infinite
216 * This makes it very easy to hook up a preferences dialog to the
217 * underlying settings. To make this even more convenient, GSettings
218 * looks for a boolean property with the name "sensitivity" and
219 * automatically binds it to the writability of the bound setting.
220 * If this 'magic' gets in the way, it can be suppressed with the
221 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
226 struct _GSettingsPrivate
228 /* where the signals go... */
229 GMainContext
*main_context
;
231 GSettingsBackend
*backend
;
232 GSettingsSchema
*schema
;
235 GDelayedSettingsBackend
*delayed
;
251 SIGNAL_WRITABLE_CHANGE_EVENT
,
252 SIGNAL_WRITABLE_CHANGED
,
258 static guint g_settings_signals
[N_SIGNALS
];
260 G_DEFINE_TYPE (GSettings
, g_settings
, G_TYPE_OBJECT
)
264 g_settings_real_change_event (GSettings
*settings
,
271 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
273 for (i
= 0; i
< n_keys
; i
++)
274 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGED
],
275 keys
[i
], g_quark_to_string (keys
[i
]));
281 g_settings_real_writable_change_event (GSettings
*settings
,
284 const GQuark
*keys
= &key
;
289 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
291 for (i
= 0; i
< n_keys
; i
++)
292 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGED
],
293 keys
[i
], g_quark_to_string (keys
[i
]));
299 settings_backend_changed (GObject
*target
,
300 GSettingsBackend
*backend
,
304 GSettings
*settings
= G_SETTINGS (target
);
305 gboolean ignore_this
;
308 /* We used to assert here:
310 * settings->priv->backend == backend
312 * but it could be the case that a notification is queued for delivery
313 * while someone calls g_settings_delay() (which changes the backend).
315 * Since the delay backend would just pass that straight through
316 * anyway, it doesn't make sense to try to detect this case.
317 * Therefore, we just accept it.
320 for (i
= 0; key
[i
] == settings
->priv
->path
[i
]; i
++);
322 if (settings
->priv
->path
[i
] == '\0' &&
323 g_settings_schema_has_key (settings
->priv
->schema
, key
+ i
))
327 quark
= g_quark_from_string (key
+ i
);
328 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
329 0, &quark
, 1, &ignore_this
);
334 settings_backend_path_changed (GObject
*target
,
335 GSettingsBackend
*backend
,
339 GSettings
*settings
= G_SETTINGS (target
);
340 gboolean ignore_this
;
342 if (g_str_has_prefix (settings
->priv
->path
, path
))
343 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
344 0, NULL
, 0, &ignore_this
);
348 settings_backend_keys_changed (GObject
*target
,
349 GSettingsBackend
*backend
,
351 const gchar
* const *items
,
354 GSettings
*settings
= G_SETTINGS (target
);
355 gboolean ignore_this
;
358 for (i
= 0; settings
->priv
->path
[i
] &&
359 settings
->priv
->path
[i
] == path
[i
]; i
++);
366 for (j
= 0; items
[j
]; j
++)
368 const gchar
*item
= items
[j
];
371 for (k
= 0; item
[k
] == settings
->priv
->path
[i
+ k
]; k
++);
373 if (settings
->priv
->path
[i
+ k
] == '\0' &&
374 g_settings_schema_has_key (settings
->priv
->schema
, item
+ k
))
375 quarks
[l
++] = g_quark_from_string (item
+ k
);
377 /* "256 quarks ought to be enough for anybody!"
378 * If this bites you, I'm sorry. Please file a bug.
384 g_signal_emit (settings
, g_settings_signals
[SIGNAL_CHANGE_EVENT
],
385 0, quarks
, l
, &ignore_this
);
390 settings_backend_writable_changed (GObject
*target
,
391 GSettingsBackend
*backend
,
394 GSettings
*settings
= G_SETTINGS (target
);
395 gboolean ignore_this
;
398 for (i
= 0; key
[i
] == settings
->priv
->path
[i
]; i
++);
400 if (settings
->priv
->path
[i
] == '\0' &&
401 g_settings_schema_has_key (settings
->priv
->schema
, key
+ i
))
402 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
],
403 0, g_quark_from_string (key
+ i
), &ignore_this
);
407 settings_backend_path_writable_changed (GObject
*target
,
408 GSettingsBackend
*backend
,
411 GSettings
*settings
= G_SETTINGS (target
);
412 gboolean ignore_this
;
414 if (g_str_has_prefix (settings
->priv
->path
, path
))
415 g_signal_emit (settings
, g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
],
416 0, (GQuark
) 0, &ignore_this
);
419 /* Properties, Construction, Destruction {{{1 */
421 g_settings_set_property (GObject
*object
,
426 GSettings
*settings
= G_SETTINGS (object
);
432 GSettingsSchema
*schema
;
434 schema
= g_value_dup_boxed (value
);
436 /* we receive a set_property() call for "settings-schema" even
437 * if it was not specified (ie: with NULL value). ->schema
438 * could already be set at this point (ie: via "schema-id").
439 * check for NULL to avoid clobbering the existing value.
443 g_assert (settings
->priv
->schema
== NULL
);
444 settings
->priv
->schema
= schema
;
451 const gchar
*schema_id
;
453 schema_id
= g_value_get_string (value
);
455 /* we receive a set_property() call for both "schema" and
456 * "schema-id", even if they are not set. Hopefully only one of
459 if (schema_id
!= NULL
)
461 GSettingsSchemaSource
*default_source
;
463 g_assert (settings
->priv
->schema
== NULL
);
464 default_source
= g_settings_schema_source_get_default ();
466 if (default_source
== NULL
)
467 g_error ("No GSettings schemas are installed on the system");
469 settings
->priv
->schema
= g_settings_schema_source_lookup (default_source
, schema_id
, TRUE
);
471 if (settings
->priv
->schema
== NULL
)
472 g_error ("Settings schema '%s' is not installed\n", schema_id
);
478 settings
->priv
->path
= g_value_dup_string (value
);
482 settings
->priv
->backend
= g_value_dup_object (value
);
486 g_assert_not_reached ();
491 g_settings_get_property (GObject
*object
,
496 GSettings
*settings
= G_SETTINGS (object
);
501 g_value_set_boxed (value
, settings
->priv
->schema
);
505 g_value_set_string (value
, g_settings_schema_get_id (settings
->priv
->schema
));
509 g_value_set_object (value
, settings
->priv
->backend
);
513 g_value_set_string (value
, settings
->priv
->path
);
516 case PROP_HAS_UNAPPLIED
:
517 g_value_set_boolean (value
, g_settings_get_has_unapplied (settings
));
520 case PROP_DELAY_APPLY
:
521 g_value_set_boolean (value
, settings
->priv
->delayed
!= NULL
);
525 g_assert_not_reached ();
529 static const GSettingsListenerVTable listener_vtable
= {
530 settings_backend_changed
,
531 settings_backend_path_changed
,
532 settings_backend_keys_changed
,
533 settings_backend_writable_changed
,
534 settings_backend_path_writable_changed
538 g_settings_constructed (GObject
*object
)
540 GSettings
*settings
= G_SETTINGS (object
);
541 const gchar
*schema_path
;
543 schema_path
= g_settings_schema_get_path (settings
->priv
->schema
);
545 if (settings
->priv
->path
&& schema_path
&& strcmp (settings
->priv
->path
, schema_path
) != 0)
546 g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
547 g_settings_schema_get_id (settings
->priv
->schema
), settings
->priv
->path
, schema_path
);
549 if (settings
->priv
->path
== NULL
)
551 if (schema_path
== NULL
)
552 g_error ("attempting to create schema '%s' without a path",
553 g_settings_schema_get_id (settings
->priv
->schema
));
555 settings
->priv
->path
= g_strdup (schema_path
);
558 if (settings
->priv
->backend
== NULL
)
559 settings
->priv
->backend
= g_settings_backend_get_default ();
561 g_settings_backend_watch (settings
->priv
->backend
,
562 &listener_vtable
, G_OBJECT (settings
),
563 settings
->priv
->main_context
);
564 g_settings_backend_subscribe (settings
->priv
->backend
,
565 settings
->priv
->path
);
569 g_settings_finalize (GObject
*object
)
571 GSettings
*settings
= G_SETTINGS (object
);
573 g_settings_backend_unsubscribe (settings
->priv
->backend
,
574 settings
->priv
->path
);
575 g_main_context_unref (settings
->priv
->main_context
);
576 g_object_unref (settings
->priv
->backend
);
577 g_settings_schema_unref (settings
->priv
->schema
);
578 g_free (settings
->priv
->path
);
580 G_OBJECT_CLASS (g_settings_parent_class
)->finalize (object
);
584 g_settings_init (GSettings
*settings
)
586 settings
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (settings
,
590 settings
->priv
->main_context
= g_main_context_ref_thread_default ();
594 g_settings_class_init (GSettingsClass
*class)
596 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
598 class->writable_change_event
= g_settings_real_writable_change_event
;
599 class->change_event
= g_settings_real_change_event
;
601 object_class
->set_property
= g_settings_set_property
;
602 object_class
->get_property
= g_settings_get_property
;
603 object_class
->constructed
= g_settings_constructed
;
604 object_class
->finalize
= g_settings_finalize
;
606 g_type_class_add_private (object_class
, sizeof (GSettingsPrivate
));
609 * GSettings::changed:
610 * @settings: the object on which the signal was emitted
611 * @key: the name of the key that changed
613 * The "changed" signal is emitted when a key has potentially changed.
614 * You should call one of the g_settings_get() calls to check the new
617 * This signal supports detailed connections. You can connect to the
618 * detailed signal "changed::x" in order to only receive callbacks
619 * when key "x" changes.
621 g_settings_signals
[SIGNAL_CHANGED
] =
622 g_signal_new ("changed", G_TYPE_SETTINGS
,
623 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
624 G_STRUCT_OFFSET (GSettingsClass
, changed
),
625 NULL
, NULL
, g_cclosure_marshal_VOID__STRING
, G_TYPE_NONE
,
626 1, G_TYPE_STRING
| G_SIGNAL_TYPE_STATIC_SCOPE
);
629 * GSettings::change-event:
630 * @settings: the object on which the signal was emitted
631 * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
632 * an array of #GQuark<!-- -->s for the changed keys, or %NULL
633 * @n_keys: the length of the @keys array, or 0
635 * The "change-event" signal is emitted once per change event that
636 * affects this settings object. You should connect to this signal
637 * only if you are interested in viewing groups of changes before they
638 * are split out into multiple emissions of the "changed" signal.
639 * For most use cases it is more appropriate to use the "changed" signal.
641 * In the event that the change event applies to one or more specified
642 * keys, @keys will be an array of #GQuark of length @n_keys. In the
643 * event that the change event applies to the #GSettings object as a
644 * whole (ie: potentially every key has been changed) then @keys will
645 * be %NULL and @n_keys will be 0.
647 * The default handler for this signal invokes the "changed" signal
648 * for each affected key. If any other connected handler returns
649 * %TRUE then this default functionality will be suppressed.
651 * Returns: %TRUE to stop other handlers from being invoked for the
652 * event. FALSE to propagate the event further.
654 g_settings_signals
[SIGNAL_CHANGE_EVENT
] =
655 g_signal_new ("change-event", G_TYPE_SETTINGS
,
657 G_STRUCT_OFFSET (GSettingsClass
, change_event
),
658 g_signal_accumulator_true_handled
, NULL
,
660 G_TYPE_BOOLEAN
, 2, G_TYPE_POINTER
, G_TYPE_INT
);
663 * GSettings::writable-changed:
664 * @settings: the object on which the signal was emitted
667 * The "writable-changed" signal is emitted when the writability of a
668 * key has potentially changed. You should call
669 * g_settings_is_writable() in order to determine the new status.
671 * This signal supports detailed connections. You can connect to the
672 * detailed signal "writable-changed::x" in order to only receive
673 * callbacks when the writability of "x" changes.
675 g_settings_signals
[SIGNAL_WRITABLE_CHANGED
] =
676 g_signal_new ("writable-changed", G_TYPE_SETTINGS
,
677 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
678 G_STRUCT_OFFSET (GSettingsClass
, writable_changed
),
679 NULL
, NULL
, g_cclosure_marshal_VOID__STRING
, G_TYPE_NONE
,
680 1, G_TYPE_STRING
| G_SIGNAL_TYPE_STATIC_SCOPE
);
683 * GSettings::writable-change-event:
684 * @settings: the object on which the signal was emitted
685 * @key: the quark of the key, or 0
687 * The "writable-change-event" signal is emitted once per writability
688 * change event that affects this settings object. You should connect
689 * to this signal if you are interested in viewing groups of changes
690 * before they are split out into multiple emissions of the
691 * "writable-changed" signal. For most use cases it is more
692 * appropriate to use the "writable-changed" signal.
694 * In the event that the writability change applies only to a single
695 * key, @key will be set to the #GQuark for that key. In the event
696 * that the writability change affects the entire settings object,
699 * The default handler for this signal invokes the "writable-changed"
700 * and "changed" signals for each affected key. This is done because
701 * changes in writability might also imply changes in value (if for
702 * example, a new mandatory setting is introduced). If any other
703 * connected handler returns %TRUE then this default functionality
704 * will be suppressed.
706 * Returns: %TRUE to stop other handlers from being invoked for the
707 * event. FALSE to propagate the event further.
709 g_settings_signals
[SIGNAL_WRITABLE_CHANGE_EVENT
] =
710 g_signal_new ("writable-change-event", G_TYPE_SETTINGS
,
712 G_STRUCT_OFFSET (GSettingsClass
, writable_change_event
),
713 g_signal_accumulator_true_handled
, NULL
,
714 NULL
, G_TYPE_BOOLEAN
, 1, G_TYPE_UINT
);
719 * The name of the context that the settings are stored in.
721 g_object_class_install_property (object_class
, PROP_BACKEND
,
722 g_param_spec_object ("backend",
723 P_("GSettingsBackend"),
724 P_("The GSettingsBackend for this settings object"),
725 G_TYPE_SETTINGS_BACKEND
, G_PARAM_CONSTRUCT_ONLY
|
726 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
729 * GSettings:settings-schema:
731 * The #GSettingsSchema describing the types of keys for this
734 * Ideally, this property would be called 'schema'. #GSettingsSchema
735 * has only existed since version 2.32, however, and before then the
736 * 'schema' property was used to refer to the ID of the schema rather
737 * than the schema itself. Take care.
739 g_object_class_install_property (object_class
, PROP_SCHEMA
,
740 g_param_spec_boxed ("settings-schema",
742 P_("The GSettingsSchema for this settings object"),
743 G_TYPE_SETTINGS_SCHEMA
,
744 G_PARAM_CONSTRUCT_ONLY
|
745 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
750 * The name of the schema that describes the types of keys
751 * for this #GSettings object.
753 * The type of this property is *not* #GSettingsSchema.
754 * #GSettingsSchema has only existed since version 2.32 and
755 * unfortunately this name was used in previous versions to refer to
756 * the schema ID rather than the schema itself. Take care to use the
757 * 'settings-schema' property if you wish to pass in a
760 * Deprecated:2.32:Use the 'schema-id' property instead. In a future
761 * version, this property may instead refer to a #GSettingsSchema.
763 g_object_class_install_property (object_class
, PROP_SCHEMA_ID
,
764 g_param_spec_string ("schema",
766 P_("The name of the schema for this settings object"),
768 G_PARAM_CONSTRUCT_ONLY
|
769 G_PARAM_DEPRECATED
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
772 * GSettings:schema-id:
774 * The name of the schema that describes the types of keys
775 * for this #GSettings object.
777 g_object_class_install_property (object_class
, PROP_SCHEMA_ID
,
778 g_param_spec_string ("schema-id",
780 P_("The name of the schema for this settings object"),
782 G_PARAM_CONSTRUCT_ONLY
|
783 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
788 * The path within the backend where the settings are stored.
790 g_object_class_install_property (object_class
, PROP_PATH
,
791 g_param_spec_string ("path",
793 P_("The path within the backend where the settings are"),
795 G_PARAM_CONSTRUCT_ONLY
|
796 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
799 * GSettings:has-unapplied:
801 * If this property is %TRUE, the #GSettings object has outstanding
802 * changes that will be applied when g_settings_apply() is called.
804 g_object_class_install_property (object_class
, PROP_HAS_UNAPPLIED
,
805 g_param_spec_boolean ("has-unapplied",
806 P_("Has unapplied changes"),
807 P_("TRUE if there are outstanding changes to apply()"),
809 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
));
812 * GSettings:delay-apply:
814 * Whether the #GSettings object is in 'delay-apply' mode. See
815 * g_settings_delay() for details.
819 g_object_class_install_property (object_class
, PROP_DELAY_APPLY
,
820 g_param_spec_boolean ("delay-apply",
821 P_("Delay-apply mode"),
822 P_("Whether this settings object is in 'delay-apply' mode"),
824 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
));
827 /* Construction (new, new_with_path, etc.) {{{1 */
830 * @schema_id: the id of the schema
832 * Creates a new #GSettings object with the schema specified by
835 * Signals on the newly created #GSettings object will be dispatched
836 * via the thread-default #GMainContext in effect at the time of the
837 * call to g_settings_new(). The new #GSettings will hold a reference
838 * on the context. See g_main_context_push_thread_default().
840 * Returns: a new #GSettings object
845 g_settings_new (const gchar
*schema_id
)
847 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
849 return g_object_new (G_TYPE_SETTINGS
,
850 "schema-id", schema_id
,
855 * g_settings_new_with_path:
856 * @schema_id: the id of the schema
857 * @path: the path to use
859 * Creates a new #GSettings object with the relocatable schema specified
860 * by @schema_id and a given path.
862 * You only need to do this if you want to directly create a settings
863 * object with a schema that doesn't have a specified path of its own.
866 * It is a programmer error to call this function for a schema that
867 * has an explicitly specified path.
869 * Returns: a new #GSettings object
874 g_settings_new_with_path (const gchar
*schema_id
,
877 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
878 g_return_val_if_fail (path
!= NULL
, NULL
);
880 return g_object_new (G_TYPE_SETTINGS
,
881 "schema-id", schema_id
,
887 * g_settings_new_with_backend:
888 * @schema_id: the id of the schema
889 * @backend: the #GSettingsBackend to use
891 * Creates a new #GSettings object with the schema specified by
892 * @schema_id and a given #GSettingsBackend.
894 * Creating a #GSettings object with a different backend allows accessing
895 * settings from a database other than the usual one. For example, it may make
896 * sense to pass a backend corresponding to the "defaults" settings database on
897 * the system to get a settings object that modifies the system default
898 * settings instead of the settings for this user.
900 * Returns: a new #GSettings object
905 g_settings_new_with_backend (const gchar
*schema_id
,
906 GSettingsBackend
*backend
)
908 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
909 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend
), NULL
);
911 return g_object_new (G_TYPE_SETTINGS
,
912 "schema-id", schema_id
,
918 * g_settings_new_with_backend_and_path:
919 * @schema_id: the id of the schema
920 * @backend: the #GSettingsBackend to use
921 * @path: the path to use
923 * Creates a new #GSettings object with the schema specified by
924 * @schema_id and a given #GSettingsBackend and path.
926 * This is a mix of g_settings_new_with_backend() and
927 * g_settings_new_with_path().
929 * Returns: a new #GSettings object
934 g_settings_new_with_backend_and_path (const gchar
*schema_id
,
935 GSettingsBackend
*backend
,
938 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
939 g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend
), NULL
);
940 g_return_val_if_fail (path
!= NULL
, NULL
);
942 return g_object_new (G_TYPE_SETTINGS
,
943 "schema-id", schema_id
,
950 * g_settings_new_full:
951 * @schema: a #GSettingsSchema
952 * @backend: (allow-none): a #GSettingsBackend
953 * @path: (allow-none): the path to use
955 * Creates a new #GSettings object with a given schema, backend and
958 * It should be extremely rare that you ever want to use this function.
959 * It is made available for advanced use-cases (such as plugin systems
960 * that want to provide access to schemas loaded from custom locations,
963 * At the most basic level, a #GSettings object is a pure composition of
964 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
965 * backend, and a #GMainContext to which signals are dispatched.
967 * This constructor therefore gives you full control over constructing
968 * #GSettings instances. The first 4 parameters are given directly as
969 * @schema, @backend and @path, and the main context is taken from the
970 * thread-default (as per g_settings_new()).
972 * If @backend is %NULL then the default backend is used.
974 * If @path is %NULL then the path from the schema is used. It is an
975 * error f @path is %NULL and the schema has no path of its own or if
976 * @path is non-%NULL and not equal to the path that the schema does
979 * Returns: a new #GSettings object
984 g_settings_new_full (GSettingsSchema
*schema
,
985 GSettingsBackend
*backend
,
988 return g_object_new (G_TYPE_SETTINGS
,
989 "settings-schema", schema
,
995 /* Internal read/write utilities {{{1 */
997 g_settings_write_to_backend (GSettings
*settings
,
998 GSettingsSchemaKey
*key
,
1004 path
= g_strconcat (settings
->priv
->path
, key
->name
, NULL
);
1005 success
= g_settings_backend_write (settings
->priv
->backend
, path
, value
, NULL
);
1012 g_settings_read_from_backend (GSettings
*settings
,
1013 GSettingsSchemaKey
*key
)
1019 path
= g_strconcat (settings
->priv
->path
, key
->name
, NULL
);
1020 value
= g_settings_backend_read (settings
->priv
->backend
, path
, key
->type
, FALSE
);
1025 fixup
= g_settings_schema_key_range_fixup (key
, value
);
1026 g_variant_unref (value
);
1034 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1036 * g_settings_get_value:
1037 * @settings: a #GSettings object
1038 * @key: the key to get the value for
1040 * Gets the value that is stored in @settings for @key.
1042 * It is a programmer error to give a @key that isn't contained in the
1043 * schema for @settings.
1045 * Returns: a new #GVariant
1050 g_settings_get_value (GSettings
*settings
,
1053 GSettingsSchemaKey skey
;
1056 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
1057 g_return_val_if_fail (key
!= NULL
, NULL
);
1059 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1060 value
= g_settings_read_from_backend (settings
, &skey
);
1063 value
= g_settings_schema_key_get_translated_default (&skey
);
1066 value
= g_variant_ref (skey
.default_value
);
1068 g_settings_schema_key_clear (&skey
);
1074 * g_settings_get_enum:
1075 * @settings: a #GSettings object
1076 * @key: the key to get the value for
1078 * Gets the value that is stored in @settings for @key and converts it
1079 * to the enum value that it represents.
1081 * In order to use this function the type of the value must be a string
1082 * and it must be marked in the schema file as an enumerated type.
1084 * It is a programmer error to give a @key that isn't contained in the
1085 * schema for @settings or is not marked as an enumerated type.
1087 * If the value stored in the configuration database is not a valid
1088 * value for the enumerated type then this function will return the
1091 * Returns: the enum value
1096 g_settings_get_enum (GSettings
*settings
,
1099 GSettingsSchemaKey skey
;
1103 g_return_val_if_fail (G_IS_SETTINGS (settings
), -1);
1104 g_return_val_if_fail (key
!= NULL
, -1);
1106 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1110 g_critical ("g_settings_get_enum() called on key `%s' which is not "
1111 "associated with an enumerated type", skey
.name
);
1112 g_settings_schema_key_clear (&skey
);
1116 value
= g_settings_read_from_backend (settings
, &skey
);
1119 value
= g_settings_schema_key_get_translated_default (&skey
);
1122 value
= g_variant_ref (skey
.default_value
);
1124 result
= g_settings_schema_key_to_enum (&skey
, value
);
1125 g_settings_schema_key_clear (&skey
);
1126 g_variant_unref (value
);
1132 * g_settings_set_enum:
1133 * @settings: a #GSettings object
1134 * @key: a key, within @settings
1135 * @value: an enumerated value
1137 * Looks up the enumerated type nick for @value and writes it to @key,
1140 * It is a programmer error to give a @key that isn't contained in the
1141 * schema for @settings or is not marked as an enumerated type, or for
1142 * @value not to be a valid value for the named type.
1144 * After performing the write, accessing @key directly with
1145 * g_settings_get_string() will return the 'nick' associated with
1148 * Returns: %TRUE, if the set succeeds
1151 g_settings_set_enum (GSettings
*settings
,
1155 GSettingsSchemaKey skey
;
1159 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1160 g_return_val_if_fail (key
!= NULL
, FALSE
);
1162 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1166 g_critical ("g_settings_set_enum() called on key `%s' which is not "
1167 "associated with an enumerated type", skey
.name
);
1171 if (!(variant
= g_settings_schema_key_from_enum (&skey
, value
)))
1173 g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1174 "in schema `%s'. Doing nothing.", value
, skey
.name
,
1175 g_settings_schema_get_id (skey
.schema
));
1176 g_settings_schema_key_clear (&skey
);
1180 success
= g_settings_write_to_backend (settings
, &skey
, variant
);
1181 g_settings_schema_key_clear (&skey
);
1187 * g_settings_get_flags:
1188 * @settings: a #GSettings object
1189 * @key: the key to get the value for
1191 * Gets the value that is stored in @settings for @key and converts it
1192 * to the flags value that it represents.
1194 * In order to use this function the type of the value must be an array
1195 * of strings and it must be marked in the schema file as an flags type.
1197 * It is a programmer error to give a @key that isn't contained in the
1198 * schema for @settings or is not marked as a flags type.
1200 * If the value stored in the configuration database is not a valid
1201 * value for the flags type then this function will return the default
1204 * Returns: the flags value
1209 g_settings_get_flags (GSettings
*settings
,
1212 GSettingsSchemaKey skey
;
1216 g_return_val_if_fail (G_IS_SETTINGS (settings
), -1);
1217 g_return_val_if_fail (key
!= NULL
, -1);
1219 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1223 g_critical ("g_settings_get_flags() called on key `%s' which is not "
1224 "associated with a flags type", skey
.name
);
1225 g_settings_schema_key_clear (&skey
);
1229 value
= g_settings_read_from_backend (settings
, &skey
);
1232 value
= g_settings_schema_key_get_translated_default (&skey
);
1235 value
= g_variant_ref (skey
.default_value
);
1237 result
= g_settings_schema_key_to_flags (&skey
, value
);
1238 g_settings_schema_key_clear (&skey
);
1239 g_variant_unref (value
);
1245 * g_settings_set_flags:
1246 * @settings: a #GSettings object
1247 * @key: a key, within @settings
1248 * @value: a flags value
1250 * Looks up the flags type nicks for the bits specified by @value, puts
1251 * them in an array of strings and writes the array to @key, within
1254 * It is a programmer error to give a @key that isn't contained in the
1255 * schema for @settings or is not marked as a flags type, or for @value
1256 * to contain any bits that are not value for the named type.
1258 * After performing the write, accessing @key directly with
1259 * g_settings_get_strv() will return an array of 'nicks'; one for each
1262 * Returns: %TRUE, if the set succeeds
1265 g_settings_set_flags (GSettings
*settings
,
1269 GSettingsSchemaKey skey
;
1273 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1274 g_return_val_if_fail (key
!= NULL
, FALSE
);
1276 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1280 g_critical ("g_settings_set_flags() called on key `%s' which is not "
1281 "associated with a flags type", skey
.name
);
1285 if (!(variant
= g_settings_schema_key_from_flags (&skey
, value
)))
1287 g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1288 "for key `%s' in schema `%s'. Doing nothing.",
1289 value
, skey
.name
, g_settings_schema_get_id (skey
.schema
));
1290 g_settings_schema_key_clear (&skey
);
1294 success
= g_settings_write_to_backend (settings
, &skey
, variant
);
1295 g_settings_schema_key_clear (&skey
);
1301 * g_settings_set_value:
1302 * @settings: a #GSettings object
1303 * @key: the name of the key to set
1304 * @value: a #GVariant of the correct type
1306 * Sets @key in @settings to @value.
1308 * It is a programmer error to give a @key that isn't contained in the
1309 * schema for @settings or for @value to have the incorrect type, per
1312 * If @value is floating then this function consumes the reference.
1314 * Returns: %TRUE if setting the key succeeded,
1315 * %FALSE if the key was not writable
1320 g_settings_set_value (GSettings
*settings
,
1324 GSettingsSchemaKey skey
;
1327 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1328 g_return_val_if_fail (key
!= NULL
, FALSE
);
1330 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1332 if (!g_settings_schema_key_type_check (&skey
, value
))
1334 g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1336 g_settings_schema_get_id (settings
->priv
->schema
),
1337 g_variant_type_peek_string (skey
.type
),
1338 g_variant_get_type_string (value
));
1343 if (!g_settings_schema_key_range_check (&skey
, value
))
1345 g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1346 "is outside of valid range",
1348 g_settings_schema_get_id (settings
->priv
->schema
));
1353 success
= g_settings_write_to_backend (settings
, &skey
, value
);
1354 g_settings_schema_key_clear (&skey
);
1361 * @settings: a #GSettings object
1362 * @key: the key to get the value for
1363 * @format: a #GVariant format string
1364 * @...: arguments as per @format
1366 * Gets the value that is stored at @key in @settings.
1368 * A convenience function that combines g_settings_get_value() with
1371 * It is a programmer error to give a @key that isn't contained in the
1372 * schema for @settings or for the #GVariantType of @format to mismatch
1373 * the type given in the schema.
1378 g_settings_get (GSettings
*settings
,
1380 const gchar
*format
,
1386 value
= g_settings_get_value (settings
, key
);
1388 va_start (ap
, format
);
1389 g_variant_get_va (value
, format
, NULL
, &ap
);
1392 g_variant_unref (value
);
1397 * @settings: a #GSettings object
1398 * @key: the name of the key to set
1399 * @format: a #GVariant format string
1400 * @...: arguments as per @format
1402 * Sets @key in @settings to @value.
1404 * A convenience function that combines g_settings_set_value() with
1407 * It is a programmer error to give a @key that isn't contained in the
1408 * schema for @settings or for the #GVariantType of @format to mismatch
1409 * the type given in the schema.
1411 * Returns: %TRUE if setting the key succeeded,
1412 * %FALSE if the key was not writable
1417 g_settings_set (GSettings
*settings
,
1419 const gchar
*format
,
1425 va_start (ap
, format
);
1426 value
= g_variant_new_va (format
, NULL
, &ap
);
1429 return g_settings_set_value (settings
, key
, value
);
1433 * g_settings_get_mapped:
1434 * @settings: a #GSettings object
1435 * @key: the key to get the value for
1436 * @mapping: (scope call): the function to map the value in the
1437 * settings database to the value used by the application
1438 * @user_data: user data for @mapping
1440 * Gets the value that is stored at @key in @settings, subject to
1441 * application-level validation/mapping.
1443 * You should use this function when the application needs to perform
1444 * some processing on the value of the key (for example, parsing). The
1445 * @mapping function performs that processing. If the function
1446 * indicates that the processing was unsuccessful (due to a parse error,
1447 * for example) then the mapping is tried again with another value.
1449 * This allows a robust 'fall back to defaults' behaviour to be
1450 * implemented somewhat automatically.
1452 * The first value that is tried is the user's setting for the key. If
1453 * the mapping function fails to map this value, other values may be
1454 * tried in an unspecified order (system or site defaults, translated
1455 * schema default values, untranslated schema default values, etc).
1457 * If the mapping function fails for all possible values, one additional
1458 * attempt is made: the mapping function is called with a %NULL value.
1459 * If the mapping function still indicates failure at this point then
1460 * the application will be aborted.
1462 * The result parameter for the @mapping function is pointed to a
1463 * #gpointer which is initially set to %NULL. The same pointer is given
1464 * to each invocation of @mapping. The final value of that #gpointer is
1465 * what is returned by this function. %NULL is valid; it is returned
1466 * just as any other value would be.
1468 * Returns: (transfer full): the result, which may be %NULL
1471 g_settings_get_mapped (GSettings
*settings
,
1473 GSettingsGetMapping mapping
,
1476 gpointer result
= NULL
;
1477 GSettingsSchemaKey skey
;
1481 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
1482 g_return_val_if_fail (key
!= NULL
, NULL
);
1483 g_return_val_if_fail (mapping
!= NULL
, NULL
);
1485 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
1487 if ((value
= g_settings_read_from_backend (settings
, &skey
)))
1489 okay
= mapping (value
, &result
, user_data
);
1490 g_variant_unref (value
);
1491 if (okay
) goto okay
;
1494 if ((value
= g_settings_schema_key_get_translated_default (&skey
)))
1496 okay
= mapping (value
, &result
, user_data
);
1497 g_variant_unref (value
);
1498 if (okay
) goto okay
;
1501 if (mapping (skey
.default_value
, &result
, user_data
))
1504 if (!mapping (NULL
, &result
, user_data
))
1505 g_error ("The mapping function given to g_settings_get_mapped() for key "
1506 "`%s' in schema `%s' returned FALSE when given a NULL value.",
1507 key
, g_settings_schema_get_id (settings
->priv
->schema
));
1510 g_settings_schema_key_clear (&skey
);
1515 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1517 * g_settings_get_string:
1518 * @settings: a #GSettings object
1519 * @key: the key to get the value for
1521 * Gets the value that is stored at @key in @settings.
1523 * A convenience variant of g_settings_get() for strings.
1525 * It is a programmer error to give a @key that isn't specified as
1526 * having a string type in the schema for @settings.
1528 * Returns: a newly-allocated string
1533 g_settings_get_string (GSettings
*settings
,
1539 value
= g_settings_get_value (settings
, key
);
1540 result
= g_variant_dup_string (value
, NULL
);
1541 g_variant_unref (value
);
1547 * g_settings_set_string:
1548 * @settings: a #GSettings object
1549 * @key: the name of the key to set
1550 * @value: the value to set it to
1552 * Sets @key in @settings to @value.
1554 * A convenience variant of g_settings_set() for strings.
1556 * It is a programmer error to give a @key that isn't specified as
1557 * having a string type in the schema for @settings.
1559 * Returns: %TRUE if setting the key succeeded,
1560 * %FALSE if the key was not writable
1565 g_settings_set_string (GSettings
*settings
,
1569 return g_settings_set_value (settings
, key
, g_variant_new_string (value
));
1573 * g_settings_get_int:
1574 * @settings: a #GSettings object
1575 * @key: the key to get the value for
1577 * Gets the value that is stored at @key in @settings.
1579 * A convenience variant of g_settings_get() for 32-bit integers.
1581 * It is a programmer error to give a @key that isn't specified as
1582 * having a int32 type in the schema for @settings.
1584 * Returns: an integer
1589 g_settings_get_int (GSettings
*settings
,
1595 value
= g_settings_get_value (settings
, key
);
1596 result
= g_variant_get_int32 (value
);
1597 g_variant_unref (value
);
1603 * g_settings_set_int:
1604 * @settings: a #GSettings object
1605 * @key: the name of the key to set
1606 * @value: the value to set it to
1608 * Sets @key in @settings to @value.
1610 * A convenience variant of g_settings_set() for 32-bit integers.
1612 * It is a programmer error to give a @key that isn't specified as
1613 * having a int32 type in the schema for @settings.
1615 * Returns: %TRUE if setting the key succeeded,
1616 * %FALSE if the key was not writable
1621 g_settings_set_int (GSettings
*settings
,
1625 return g_settings_set_value (settings
, key
, g_variant_new_int32 (value
));
1629 * g_settings_get_uint:
1630 * @settings: a #GSettings object
1631 * @key: the key to get the value for
1633 * Gets the value that is stored at @key in @settings.
1635 * A convenience variant of g_settings_get() for 32-bit unsigned
1638 * It is a programmer error to give a @key that isn't specified as
1639 * having a uint32 type in the schema for @settings.
1641 * Returns: an unsigned integer
1646 g_settings_get_uint (GSettings
*settings
,
1652 value
= g_settings_get_value (settings
, key
);
1653 result
= g_variant_get_uint32 (value
);
1654 g_variant_unref (value
);
1660 * g_settings_set_uint:
1661 * @settings: a #GSettings object
1662 * @key: the name of the key to set
1663 * @value: the value to set it to
1665 * Sets @key in @settings to @value.
1667 * A convenience variant of g_settings_set() for 32-bit unsigned
1670 * It is a programmer error to give a @key that isn't specified as
1671 * having a uint32 type in the schema for @settings.
1673 * Returns: %TRUE if setting the key succeeded,
1674 * %FALSE if the key was not writable
1679 g_settings_set_uint (GSettings
*settings
,
1683 return g_settings_set_value (settings
, key
, g_variant_new_uint32 (value
));
1687 * g_settings_get_double:
1688 * @settings: a #GSettings object
1689 * @key: the key to get the value for
1691 * Gets the value that is stored at @key in @settings.
1693 * A convenience variant of g_settings_get() for doubles.
1695 * It is a programmer error to give a @key that isn't specified as
1696 * having a 'double' type in the schema for @settings.
1703 g_settings_get_double (GSettings
*settings
,
1709 value
= g_settings_get_value (settings
, key
);
1710 result
= g_variant_get_double (value
);
1711 g_variant_unref (value
);
1717 * g_settings_set_double:
1718 * @settings: a #GSettings object
1719 * @key: the name of the key to set
1720 * @value: the value to set it to
1722 * Sets @key in @settings to @value.
1724 * A convenience variant of g_settings_set() for doubles.
1726 * It is a programmer error to give a @key that isn't specified as
1727 * having a 'double' type in the schema for @settings.
1729 * Returns: %TRUE if setting the key succeeded,
1730 * %FALSE if the key was not writable
1735 g_settings_set_double (GSettings
*settings
,
1739 return g_settings_set_value (settings
, key
, g_variant_new_double (value
));
1743 * g_settings_get_boolean:
1744 * @settings: a #GSettings object
1745 * @key: the key to get the value for
1747 * Gets the value that is stored at @key in @settings.
1749 * A convenience variant of g_settings_get() for booleans.
1751 * It is a programmer error to give a @key that isn't specified as
1752 * having a boolean type in the schema for @settings.
1754 * Returns: a boolean
1759 g_settings_get_boolean (GSettings
*settings
,
1765 value
= g_settings_get_value (settings
, key
);
1766 result
= g_variant_get_boolean (value
);
1767 g_variant_unref (value
);
1773 * g_settings_set_boolean:
1774 * @settings: a #GSettings object
1775 * @key: the name of the key to set
1776 * @value: the value to set it to
1778 * Sets @key in @settings to @value.
1780 * A convenience variant of g_settings_set() for booleans.
1782 * It is a programmer error to give a @key that isn't specified as
1783 * having a boolean type in the schema for @settings.
1785 * Returns: %TRUE if setting the key succeeded,
1786 * %FALSE if the key was not writable
1791 g_settings_set_boolean (GSettings
*settings
,
1795 return g_settings_set_value (settings
, key
, g_variant_new_boolean (value
));
1799 * g_settings_get_strv:
1800 * @settings: a #GSettings object
1801 * @key: the key to get the value for
1803 * A convenience variant of g_settings_get() for string arrays.
1805 * It is a programmer error to give a @key that isn't specified as
1806 * having an array of strings type in the schema for @settings.
1808 * Returns: (array zero-terminated=1) (transfer full): a
1809 * newly-allocated, %NULL-terminated array of strings, the value that
1810 * is stored at @key in @settings.
1815 g_settings_get_strv (GSettings
*settings
,
1821 value
= g_settings_get_value (settings
, key
);
1822 result
= g_variant_dup_strv (value
, NULL
);
1823 g_variant_unref (value
);
1829 * g_settings_set_strv:
1830 * @settings: a #GSettings object
1831 * @key: the name of the key to set
1832 * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1834 * Sets @key in @settings to @value.
1836 * A convenience variant of g_settings_set() for string arrays. If
1837 * @value is %NULL, then @key is set to be the empty array.
1839 * It is a programmer error to give a @key that isn't specified as
1840 * having an array of strings type in the schema for @settings.
1842 * Returns: %TRUE if setting the key succeeded,
1843 * %FALSE if the key was not writable
1848 g_settings_set_strv (GSettings
*settings
,
1850 const gchar
* const *value
)
1855 array
= g_variant_new_strv (value
, -1);
1857 array
= g_variant_new_strv (NULL
, 0);
1859 return g_settings_set_value (settings
, key
, array
);
1862 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1865 * @settings: a #GSettings object
1867 * Changes the #GSettings object into 'delay-apply' mode. In this
1868 * mode, changes to @settings are not immediately propagated to the
1869 * backend, but kept locally until g_settings_apply() is called.
1874 g_settings_delay (GSettings
*settings
)
1876 g_return_if_fail (G_IS_SETTINGS (settings
));
1878 if (settings
->priv
->delayed
)
1881 settings
->priv
->delayed
=
1882 g_delayed_settings_backend_new (settings
->priv
->backend
,
1884 settings
->priv
->main_context
);
1885 g_settings_backend_unwatch (settings
->priv
->backend
, G_OBJECT (settings
));
1886 g_object_unref (settings
->priv
->backend
);
1888 settings
->priv
->backend
= G_SETTINGS_BACKEND (settings
->priv
->delayed
);
1889 g_settings_backend_watch (settings
->priv
->backend
,
1890 &listener_vtable
, G_OBJECT (settings
),
1891 settings
->priv
->main_context
);
1893 g_object_notify (G_OBJECT (settings
), "delay-apply");
1898 * @settings: a #GSettings instance
1900 * Applies any changes that have been made to the settings. This
1901 * function does nothing unless @settings is in 'delay-apply' mode;
1902 * see g_settings_delay(). In the normal case settings are always
1903 * applied immediately.
1906 g_settings_apply (GSettings
*settings
)
1908 if (settings
->priv
->delayed
)
1910 GDelayedSettingsBackend
*delayed
;
1912 delayed
= G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
);
1913 g_delayed_settings_backend_apply (delayed
);
1918 * g_settings_revert:
1919 * @settings: a #GSettings instance
1921 * Reverts all non-applied changes to the settings. This function
1922 * does nothing unless @settings is in 'delay-apply' mode; see
1923 * g_settings_delay(). In the normal case settings are always applied
1926 * Change notifications will be emitted for affected keys.
1929 g_settings_revert (GSettings
*settings
)
1931 if (settings
->priv
->delayed
)
1933 GDelayedSettingsBackend
*delayed
;
1935 delayed
= G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
);
1936 g_delayed_settings_backend_revert (delayed
);
1941 * g_settings_get_has_unapplied:
1942 * @settings: a #GSettings object
1944 * Returns whether the #GSettings object has any unapplied
1945 * changes. This can only be the case if it is in 'delayed-apply' mode.
1947 * Returns: %TRUE if @settings has unapplied changes
1952 g_settings_get_has_unapplied (GSettings
*settings
)
1954 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
1956 return settings
->priv
->delayed
&&
1957 g_delayed_settings_backend_get_has_unapplied (
1958 G_DELAYED_SETTINGS_BACKEND (settings
->priv
->backend
));
1961 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
1964 * @settings: a #GSettings object
1965 * @key: the name of a key
1967 * Resets @key to its default value.
1969 * This call resets the key, as much as possible, to its default value.
1970 * That might the value specified in the schema or the one set by the
1974 g_settings_reset (GSettings
*settings
,
1979 path
= g_strconcat (settings
->priv
->path
, key
, NULL
);
1980 g_settings_backend_reset (settings
->priv
->backend
, path
, NULL
);
1987 * Ensures that all pending operations for the given are complete for
1988 * the default backend.
1990 * Writes made to a #GSettings are handled asynchronously. For this
1991 * reason, it is very unlikely that the changes have it to disk by the
1992 * time g_settings_set() returns.
1994 * This call will block until all of the writes have made it to the
1995 * backend. Since the mainloop is not running, no change notifications
1996 * will be dispatched during this call (but some may be queued by the
1997 * time the call is done).
2000 g_settings_sync (void)
2002 g_settings_backend_sync_default ();
2006 * g_settings_is_writable:
2007 * @settings: a #GSettings object
2008 * @name: the name of a key
2010 * Finds out if a key can be written or not
2012 * Returns: %TRUE if the key @name is writable
2017 g_settings_is_writable (GSettings
*settings
,
2023 g_return_val_if_fail (G_IS_SETTINGS (settings
), FALSE
);
2025 path
= g_strconcat (settings
->priv
->path
, name
, NULL
);
2026 writable
= g_settings_backend_get_writable (settings
->priv
->backend
, path
);
2033 * g_settings_get_child:
2034 * @settings: a #GSettings object
2035 * @name: the name of the 'child' schema
2037 * Creates a 'child' settings object which has a base path of
2038 * <replaceable>base-path</replaceable>/@name, where
2039 * <replaceable>base-path</replaceable> is the base path of @settings.
2041 * The schema for the child settings object must have been declared
2042 * in the schema of @settings using a <tag class="starttag">child</tag> element.
2044 * Returns: (transfer full): a 'child' settings object
2049 g_settings_get_child (GSettings
*settings
,
2052 const gchar
*child_schema
;
2057 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
2059 child_name
= g_strconcat (name
, "/", NULL
);
2060 child_schema
= g_settings_schema_get_string (settings
->priv
->schema
,
2062 if (child_schema
== NULL
)
2063 g_error ("Schema '%s' has no child '%s'",
2064 g_settings_schema_get_id (settings
->priv
->schema
), name
);
2066 child_path
= g_strconcat (settings
->priv
->path
, child_name
, NULL
);
2067 child
= g_object_new (G_TYPE_SETTINGS
,
2068 "schema-id", child_schema
,
2071 g_free (child_path
);
2072 g_free (child_name
);
2078 * g_settings_list_keys:
2079 * @settings: a #GSettings object
2081 * Introspects the list of keys on @settings.
2083 * You should probably not be calling this function from "normal" code
2084 * (since you should already know what keys are in your schema). This
2085 * function is intended for introspection reasons.
2087 * You should free the return value with g_strfreev() when you are done
2090 * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2093 g_settings_list_keys (GSettings
*settings
)
2100 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
2101 strv
= g_new (gchar
*, n_keys
+ 1);
2102 for (i
= j
= 0; i
< n_keys
; i
++)
2104 const gchar
*key
= g_quark_to_string (keys
[i
]);
2106 if (!g_str_has_suffix (key
, "/"))
2107 strv
[j
++] = g_strdup (key
);
2115 * g_settings_list_children:
2116 * @settings: a #GSettings object
2118 * Gets the list of children on @settings.
2120 * The list is exactly the list of strings for which it is not an error
2121 * to call g_settings_get_child().
2123 * For GSettings objects that are lists, this value can change at any
2124 * time and you should connect to the "children-changed" signal to watch
2125 * for those changes. Note that there is a race condition here: you may
2126 * request a child after listing it only for it to have been destroyed
2127 * in the meantime. For this reason, g_settings_get_child() may return
2128 * %NULL even for a child that was listed by this function.
2130 * For GSettings objects that are not lists, you should probably not be
2131 * calling this function from "normal" code (since you should already
2132 * know what children are in your schema). This function may still be
2133 * useful there for introspection reasons, however.
2135 * You should free the return value with g_strfreev() when you are done
2138 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2141 g_settings_list_children (GSettings
*settings
)
2148 keys
= g_settings_schema_list (settings
->priv
->schema
, &n_keys
);
2149 strv
= g_new (gchar
*, n_keys
+ 1);
2150 for (i
= j
= 0; i
< n_keys
; i
++)
2152 const gchar
*key
= g_quark_to_string (keys
[i
]);
2154 if (g_str_has_suffix (key
, "/"))
2156 gint length
= strlen (key
);
2158 strv
[j
] = g_memdup (key
, length
);
2159 strv
[j
][length
- 1] = '\0';
2169 * g_settings_get_range:
2170 * @settings: a #GSettings
2171 * @key: the key to query the range of
2173 * Queries the range of a key.
2175 * This function will return a #GVariant that fully describes the range
2176 * of values that are valid for @key.
2178 * The type of #GVariant returned is <literal>(sv)</literal>. The
2179 * string describes the type of range restriction in effect. The type
2180 * and meaning of the value contained in the variant depends on the
2183 * If the string is <literal>'type'</literal> then the variant contains
2184 * an empty array. The element type of that empty array is the expected
2185 * type of value and all values of that type are valid.
2187 * If the string is <literal>'enum'</literal> then the variant contains
2188 * an array enumerating the possible values. Each item in the array is
2189 * a possible valid value and no other values are valid.
2191 * If the string is <literal>'flags'</literal> then the variant contains
2192 * an array. Each item in the array is a value that may appear zero or
2193 * one times in an array to be used as the value for this key. For
2194 * example, if the variant contained the array <literal>['x',
2195 * 'y']</literal> then the valid values for the key would be
2196 * <literal>[]</literal>, <literal>['x']</literal>,
2197 * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2198 * <literal>['y', 'x']</literal>.
2200 * Finally, if the string is <literal>'range'</literal> then the variant
2201 * contains a pair of like-typed values -- the minimum and maximum
2202 * permissible values for this key.
2204 * This information should not be used by normal programs. It is
2205 * considered to be a hint for introspection purposes. Normal programs
2206 * should already know what is permitted by their own schema. The
2207 * format may change in any way in the future -- but particularly, new
2208 * forms may be added to the possibilities described above.
2210 * It is a programmer error to give a @key that isn't contained in the
2211 * schema for @settings.
2213 * You should free the returned value with g_variant_unref() when it is
2216 * Returns: a #GVariant describing the range
2221 g_settings_get_range (GSettings
*settings
,
2224 GSettingsSchemaKey skey
;
2228 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
2232 range
= g_variant_new ("(**)", skey
.minimum
, skey
.maximum
);
2235 else if (skey
.strinfo
)
2237 range
= strinfo_enumerate (skey
.strinfo
, skey
.strinfo_length
);
2238 type
= skey
.is_flags
? "flags" : "enum";
2242 range
= g_variant_new_array (skey
.type
, NULL
, 0);
2246 g_settings_schema_key_clear (&skey
);
2248 return g_variant_ref_sink (g_variant_new ("(sv)", type
, range
));
2252 * g_settings_range_check:
2253 * @settings: a #GSettings
2254 * @key: the key to check
2255 * @value: the value to check
2257 * Checks if the given @value is of the correct type and within the
2258 * permitted range for @key.
2260 * This API is not intended to be used by normal programs -- they should
2261 * already know what is permitted by their own schemas. This API is
2262 * meant to be used by programs such as editors or commandline tools.
2264 * It is a programmer error to give a @key that isn't contained in the
2265 * schema for @settings.
2267 * Returns: %TRUE if @value is valid for @key
2272 g_settings_range_check (GSettings
*settings
,
2276 GSettingsSchemaKey skey
;
2279 g_settings_schema_key_init (&skey
, settings
->priv
->schema
, key
);
2280 good
= g_settings_schema_key_type_check (&skey
, value
) &&
2281 g_settings_schema_key_range_check (&skey
, value
);
2282 g_settings_schema_key_clear (&skey
);
2290 GSettingsSchemaKey key
;
2291 GSettings
*settings
;
2294 GSettingsBindGetMapping get_mapping
;
2295 GSettingsBindSetMapping set_mapping
;
2297 GDestroyNotify destroy
;
2299 guint writable_handler_id
;
2300 guint property_handler_id
;
2301 const GParamSpec
*property
;
2302 guint key_handler_id
;
2304 /* prevent recursion */
2309 g_settings_binding_free (gpointer data
)
2311 GSettingsBinding
*binding
= data
;
2313 g_assert (!binding
->running
);
2315 if (binding
->writable_handler_id
)
2316 g_signal_handler_disconnect (binding
->settings
,
2317 binding
->writable_handler_id
);
2319 if (binding
->key_handler_id
)
2320 g_signal_handler_disconnect (binding
->settings
,
2321 binding
->key_handler_id
);
2323 if (g_signal_handler_is_connected (binding
->object
,
2324 binding
->property_handler_id
))
2325 g_signal_handler_disconnect (binding
->object
,
2326 binding
->property_handler_id
);
2328 g_settings_schema_key_clear (&binding
->key
);
2330 if (binding
->destroy
)
2331 binding
->destroy (binding
->user_data
);
2333 g_object_unref (binding
->settings
);
2335 g_slice_free (GSettingsBinding
, binding
);
2339 g_settings_binding_quark (const char *property
)
2344 tmp
= g_strdup_printf ("gsettingsbinding-%s", property
);
2345 quark
= g_quark_from_string (tmp
);
2352 g_settings_binding_key_changed (GSettings
*settings
,
2356 GSettingsBinding
*binding
= user_data
;
2357 GValue value
= G_VALUE_INIT
;
2360 g_assert (settings
== binding
->settings
);
2361 g_assert (key
== binding
->key
.name
);
2363 if (binding
->running
)
2366 binding
->running
= TRUE
;
2368 g_value_init (&value
, binding
->property
->value_type
);
2370 variant
= g_settings_read_from_backend (binding
->settings
, &binding
->key
);
2371 if (variant
&& !binding
->get_mapping (&value
, variant
, binding
->user_data
))
2373 /* silently ignore errors in the user's config database */
2374 g_variant_unref (variant
);
2378 if (variant
== NULL
)
2380 variant
= g_settings_schema_key_get_translated_default (&binding
->key
);
2382 !binding
->get_mapping (&value
, variant
, binding
->user_data
))
2384 /* flag translation errors with a warning */
2385 g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2386 "was rejected by the binding mapping function",
2387 binding
->key
.unparsed
, binding
->key
.name
,
2388 g_settings_schema_get_id (binding
->key
.schema
));
2389 g_variant_unref (variant
);
2394 if (variant
== NULL
)
2396 variant
= g_variant_ref (binding
->key
.default_value
);
2397 if (!binding
->get_mapping (&value
, variant
, binding
->user_data
))
2398 g_error ("The schema default value for key `%s' in schema `%s' "
2399 "was rejected by the binding mapping function.",
2400 binding
->key
.name
, g_settings_schema_get_id (binding
->key
.schema
));
2403 g_object_set_property (binding
->object
, binding
->property
->name
, &value
);
2404 g_variant_unref (variant
);
2405 g_value_unset (&value
);
2407 binding
->running
= FALSE
;
2411 g_settings_binding_property_changed (GObject
*object
,
2412 const GParamSpec
*pspec
,
2415 GSettingsBinding
*binding
= user_data
;
2416 GValue value
= G_VALUE_INIT
;
2419 g_assert (object
== binding
->object
);
2420 g_assert (pspec
== binding
->property
);
2422 if (binding
->running
)
2425 binding
->running
= TRUE
;
2427 g_value_init (&value
, pspec
->value_type
);
2428 g_object_get_property (object
, pspec
->name
, &value
);
2429 if ((variant
= binding
->set_mapping (&value
, binding
->key
.type
,
2430 binding
->user_data
)))
2432 g_variant_take_ref (variant
);
2434 if (!g_settings_schema_key_type_check (&binding
->key
, variant
))
2436 g_critical ("binding mapping function for key `%s' returned "
2437 "GVariant of type `%s' when type `%s' was requested",
2438 binding
->key
.name
, g_variant_get_type_string (variant
),
2439 g_variant_type_dup_string (binding
->key
.type
));
2443 if (!g_settings_schema_key_range_check (&binding
->key
, variant
))
2445 g_critical ("GObject property `%s' on a `%s' object is out of "
2446 "schema-specified range for key `%s' of `%s': %s",
2447 binding
->property
->name
, g_type_name (binding
->property
->owner_type
),
2448 binding
->key
.name
, g_settings_schema_get_id (binding
->key
.schema
),
2449 g_variant_print (variant
, TRUE
));
2453 g_settings_write_to_backend (binding
->settings
, &binding
->key
, variant
);
2454 g_variant_unref (variant
);
2456 g_value_unset (&value
);
2458 binding
->running
= FALSE
;
2462 g_settings_bind_invert_boolean_get_mapping (GValue
*value
,
2466 g_value_set_boolean (value
, !g_variant_get_boolean (variant
));
2471 g_settings_bind_invert_boolean_set_mapping (const GValue
*value
,
2472 const GVariantType
*expected_type
,
2475 return g_variant_new_boolean (!g_value_get_boolean (value
));
2480 * @settings: a #GSettings object
2481 * @key: the key to bind
2482 * @object: (type GObject.Object): a #GObject
2483 * @property: the name of the property to bind
2484 * @flags: flags for the binding
2486 * Create a binding between the @key in the @settings object
2487 * and the property @property of @object.
2489 * The binding uses the default GIO mapping functions to map
2490 * between the settings and property values. These functions
2491 * handle booleans, numeric types and string types in a
2492 * straightforward way. Use g_settings_bind_with_mapping() if
2493 * you need a custom mapping, or map between types that are not
2494 * supported by the default mapping functions.
2496 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2497 * function also establishes a binding between the writability of
2498 * @key and the "sensitive" property of @object (if @object has
2499 * a boolean property by that name). See g_settings_bind_writable()
2500 * for more details about writable bindings.
2502 * Note that the lifecycle of the binding is tied to the object,
2503 * and that you can have only one binding per object property.
2504 * If you bind the same property twice on the same object, the second
2505 * binding overrides the first one.
2510 g_settings_bind (GSettings
*settings
,
2513 const gchar
*property
,
2514 GSettingsBindFlags flags
)
2516 GSettingsBindGetMapping get_mapping
= NULL
;
2517 GSettingsBindSetMapping set_mapping
= NULL
;
2519 if (flags
& G_SETTINGS_BIND_INVERT_BOOLEAN
)
2521 get_mapping
= g_settings_bind_invert_boolean_get_mapping
;
2522 set_mapping
= g_settings_bind_invert_boolean_set_mapping
;
2524 /* can't pass this flag to g_settings_bind_with_mapping() */
2525 flags
&= ~G_SETTINGS_BIND_INVERT_BOOLEAN
;
2528 g_settings_bind_with_mapping (settings
, key
, object
, property
, flags
,
2529 get_mapping
, set_mapping
, NULL
, NULL
);
2533 * g_settings_bind_with_mapping: (skip)
2534 * @settings: a #GSettings object
2535 * @key: the key to bind
2536 * @object: (type GObject.Object): a #GObject
2537 * @property: the name of the property to bind
2538 * @flags: flags for the binding
2539 * @get_mapping: a function that gets called to convert values
2540 * from @settings to @object, or %NULL to use the default GIO mapping
2541 * @set_mapping: a function that gets called to convert values
2542 * from @object to @settings, or %NULL to use the default GIO mapping
2543 * @user_data: data that gets passed to @get_mapping and @set_mapping
2544 * @destroy: #GDestroyNotify function for @user_data
2546 * Create a binding between the @key in the @settings object
2547 * and the property @property of @object.
2549 * The binding uses the provided mapping functions to map between
2550 * settings and property values.
2552 * Note that the lifecycle of the binding is tied to the object,
2553 * and that you can have only one binding per object property.
2554 * If you bind the same property twice on the same object, the second
2555 * binding overrides the first one.
2560 g_settings_bind_with_mapping (GSettings
*settings
,
2563 const gchar
*property
,
2564 GSettingsBindFlags flags
,
2565 GSettingsBindGetMapping get_mapping
,
2566 GSettingsBindSetMapping set_mapping
,
2568 GDestroyNotify destroy
)
2570 GSettingsBinding
*binding
;
2571 GObjectClass
*objectclass
;
2572 gchar
*detailed_signal
;
2573 GQuark binding_quark
;
2575 g_return_if_fail (G_IS_SETTINGS (settings
));
2576 g_return_if_fail (key
!= NULL
);
2577 g_return_if_fail (G_IS_OBJECT (object
));
2578 g_return_if_fail (property
!= NULL
);
2579 g_return_if_fail (~flags
& G_SETTINGS_BIND_INVERT_BOOLEAN
);
2581 objectclass
= G_OBJECT_GET_CLASS (object
);
2583 binding
= g_slice_new0 (GSettingsBinding
);
2584 g_settings_schema_key_init (&binding
->key
, settings
->priv
->schema
, key
);
2585 binding
->settings
= g_object_ref (settings
);
2586 binding
->object
= object
;
2587 binding
->property
= g_object_class_find_property (objectclass
, property
);
2588 binding
->user_data
= user_data
;
2589 binding
->destroy
= destroy
;
2590 binding
->get_mapping
= get_mapping
? get_mapping
: g_settings_get_mapping
;
2591 binding
->set_mapping
= set_mapping
? set_mapping
: g_settings_set_mapping
;
2593 if (!(flags
& (G_SETTINGS_BIND_GET
| G_SETTINGS_BIND_SET
)))
2594 flags
|= G_SETTINGS_BIND_GET
| G_SETTINGS_BIND_SET
;
2596 if (binding
->property
== NULL
)
2598 g_critical ("g_settings_bind: no property '%s' on class '%s'",
2599 property
, G_OBJECT_TYPE_NAME (object
));
2603 if ((flags
& G_SETTINGS_BIND_GET
) &&
2604 (binding
->property
->flags
& G_PARAM_WRITABLE
) == 0)
2606 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2607 "writable", property
, G_OBJECT_TYPE_NAME (object
));
2610 if ((flags
& G_SETTINGS_BIND_SET
) &&
2611 (binding
->property
->flags
& G_PARAM_READABLE
) == 0)
2613 g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2614 "readable", property
, G_OBJECT_TYPE_NAME (object
));
2618 if (get_mapping
== g_settings_bind_invert_boolean_get_mapping
)
2620 /* g_settings_bind_invert_boolean_get_mapping() is a private
2621 * function, so if we are here it means that g_settings_bind() was
2622 * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2624 * Ensure that both sides are boolean.
2627 if (binding
->property
->value_type
!= G_TYPE_BOOLEAN
)
2629 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2630 "was specified, but property `%s' on type `%s' has "
2631 "type `%s'", property
, G_OBJECT_TYPE_NAME (object
),
2632 g_type_name ((binding
->property
->value_type
)));
2636 if (!g_variant_type_equal (binding
->key
.type
, G_VARIANT_TYPE_BOOLEAN
))
2638 g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2639 "was specified, but key `%s' on schema `%s' has "
2640 "type `%s'", key
, g_settings_schema_get_id (settings
->priv
->schema
),
2641 g_variant_type_dup_string (binding
->key
.type
));
2647 else if (((get_mapping
== NULL
&& (flags
& G_SETTINGS_BIND_GET
)) ||
2648 (set_mapping
== NULL
&& (flags
& G_SETTINGS_BIND_SET
))) &&
2649 !g_settings_mapping_is_compatible (binding
->property
->value_type
,
2652 g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2653 "'%s' which is not compatible with type '%s' of key '%s' "
2654 "on schema '%s'", property
, G_OBJECT_TYPE_NAME (object
),
2655 g_type_name (binding
->property
->value_type
),
2656 g_variant_type_dup_string (binding
->key
.type
), key
,
2657 g_settings_schema_get_id (settings
->priv
->schema
));
2661 if ((flags
& G_SETTINGS_BIND_SET
) &&
2662 (~flags
& G_SETTINGS_BIND_NO_SENSITIVITY
))
2664 GParamSpec
*sensitive
;
2666 sensitive
= g_object_class_find_property (objectclass
, "sensitive");
2668 if (sensitive
&& sensitive
->value_type
== G_TYPE_BOOLEAN
&&
2669 (sensitive
->flags
& G_PARAM_WRITABLE
))
2670 g_settings_bind_writable (settings
, binding
->key
.name
, object
, "sensitive", FALSE
);
2673 if (flags
& G_SETTINGS_BIND_SET
)
2675 detailed_signal
= g_strdup_printf ("notify::%s", property
);
2676 binding
->property_handler_id
=
2677 g_signal_connect (object
, detailed_signal
,
2678 G_CALLBACK (g_settings_binding_property_changed
),
2680 g_free (detailed_signal
);
2682 if (~flags
& G_SETTINGS_BIND_GET
)
2683 g_settings_binding_property_changed (object
,
2688 if (flags
& G_SETTINGS_BIND_GET
)
2690 if (~flags
& G_SETTINGS_BIND_GET_NO_CHANGES
)
2692 detailed_signal
= g_strdup_printf ("changed::%s", key
);
2693 binding
->key_handler_id
=
2694 g_signal_connect (settings
, detailed_signal
,
2695 G_CALLBACK (g_settings_binding_key_changed
),
2697 g_free (detailed_signal
);
2700 g_settings_binding_key_changed (settings
, binding
->key
.name
, binding
);
2703 binding_quark
= g_settings_binding_quark (property
);
2704 g_object_set_qdata_full (object
, binding_quark
,
2705 binding
, g_settings_binding_free
);
2708 /* Writability binding {{{1 */
2711 GSettings
*settings
;
2714 const gchar
*property
;
2717 } GSettingsWritableBinding
;
2720 g_settings_writable_binding_free (gpointer data
)
2722 GSettingsWritableBinding
*binding
= data
;
2724 g_signal_handler_disconnect (binding
->settings
, binding
->handler_id
);
2725 g_object_unref (binding
->settings
);
2726 g_slice_free (GSettingsWritableBinding
, binding
);
2730 g_settings_binding_writable_changed (GSettings
*settings
,
2734 GSettingsWritableBinding
*binding
= user_data
;
2737 g_assert (settings
== binding
->settings
);
2738 g_assert (key
== binding
->key
);
2740 writable
= g_settings_is_writable (settings
, key
);
2742 if (binding
->inverted
)
2743 writable
= !writable
;
2745 g_object_set (binding
->object
, binding
->property
, writable
, NULL
);
2749 * g_settings_bind_writable:
2750 * @settings: a #GSettings object
2751 * @key: the key to bind
2752 * @object: (type GObject.Object):a #GObject
2753 * @property: the name of a boolean property to bind
2754 * @inverted: whether to 'invert' the value
2756 * Create a binding between the writability of @key in the
2757 * @settings object and the property @property of @object.
2758 * The property must be boolean; "sensitive" or "visible"
2759 * properties of widgets are the most likely candidates.
2761 * Writable bindings are always uni-directional; changes of the
2762 * writability of the setting will be propagated to the object
2763 * property, not the other way.
2765 * When the @inverted argument is %TRUE, the binding inverts the
2766 * value as it passes from the setting to the object, i.e. @property
2767 * will be set to %TRUE if the key is <emphasis>not</emphasis>
2770 * Note that the lifecycle of the binding is tied to the object,
2771 * and that you can have only one binding per object property.
2772 * If you bind the same property twice on the same object, the second
2773 * binding overrides the first one.
2778 g_settings_bind_writable (GSettings
*settings
,
2781 const gchar
*property
,
2784 GSettingsWritableBinding
*binding
;
2785 gchar
*detailed_signal
;
2788 g_return_if_fail (G_IS_SETTINGS (settings
));
2790 pspec
= g_object_class_find_property (G_OBJECT_GET_CLASS (object
), property
);
2793 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2794 property
, G_OBJECT_TYPE_NAME (object
));
2797 if ((pspec
->flags
& G_PARAM_WRITABLE
) == 0)
2799 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2800 property
, G_OBJECT_TYPE_NAME (object
));
2804 binding
= g_slice_new (GSettingsWritableBinding
);
2805 binding
->settings
= g_object_ref (settings
);
2806 binding
->object
= object
;
2807 binding
->key
= g_intern_string (key
);
2808 binding
->property
= g_intern_string (property
);
2809 binding
->inverted
= inverted
;
2811 detailed_signal
= g_strdup_printf ("writable-changed::%s", key
);
2812 binding
->handler_id
=
2813 g_signal_connect (settings
, detailed_signal
,
2814 G_CALLBACK (g_settings_binding_writable_changed
),
2816 g_free (detailed_signal
);
2818 g_object_set_qdata_full (object
, g_settings_binding_quark (property
),
2819 binding
, g_settings_writable_binding_free
);
2821 g_settings_binding_writable_changed (settings
, binding
->key
, binding
);
2825 * g_settings_unbind:
2826 * @object: the object
2827 * @property: the property whose binding is removed
2829 * Removes an existing binding for @property on @object.
2831 * Note that bindings are automatically removed when the
2832 * object is finalized, so it is rarely necessary to call this
2838 g_settings_unbind (gpointer object
,
2839 const gchar
*property
)
2841 GQuark binding_quark
;
2843 binding_quark
= g_settings_binding_quark (property
);
2844 g_object_set_qdata (object
, binding_quark
, NULL
);
2851 GObject parent_instance
;
2853 GSettingsSchemaKey key
;
2854 GSettings
*settings
;
2857 typedef GObjectClass GSettingsActionClass
;
2859 static GType
g_settings_action_get_type (void);
2860 static void g_settings_action_iface_init (GActionInterface
*iface
);
2861 G_DEFINE_TYPE_WITH_CODE (GSettingsAction
, g_settings_action
, G_TYPE_OBJECT
,
2862 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION
, g_settings_action_iface_init
))
2868 ACTION_PROP_PARAMETER_TYPE
,
2869 ACTION_PROP_ENABLED
,
2870 ACTION_PROP_STATE_TYPE
,
2874 static const gchar
*
2875 g_settings_action_get_name (GAction
*action
)
2877 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2879 return gsa
->key
.name
;
2882 static const GVariantType
*
2883 g_settings_action_get_parameter_type (GAction
*action
)
2885 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2886 const GVariantType
*type
;
2888 type
= g_variant_get_type (gsa
->key
.default_value
);
2889 if (g_variant_type_equal (type
, G_VARIANT_TYPE_BOOLEAN
))
2896 g_settings_action_get_enabled (GAction
*action
)
2898 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2900 return g_settings_is_writable (gsa
->settings
, gsa
->key
.name
);
2903 static const GVariantType
*
2904 g_settings_action_get_state_type (GAction
*action
)
2906 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2908 return g_variant_get_type (gsa
->key
.default_value
);
2912 g_settings_action_get_state (GAction
*action
)
2914 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2917 value
= g_settings_read_from_backend (gsa
->settings
, &gsa
->key
);
2920 value
= g_settings_schema_key_get_translated_default (&gsa
->key
);
2923 value
= g_variant_ref (gsa
->key
.default_value
);
2929 g_settings_action_get_state_hint (GAction
*action
)
2931 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2933 /* no point in reimplementing this... */
2934 return g_settings_get_range (gsa
->settings
, gsa
->key
.name
);
2938 g_settings_action_change_state (GAction
*action
,
2941 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2943 if (g_settings_schema_key_type_check (&gsa
->key
, value
) && g_settings_schema_key_range_check (&gsa
->key
, value
))
2944 g_settings_write_to_backend (gsa
->settings
, &gsa
->key
, value
);
2948 g_settings_action_activate (GAction
*action
,
2949 GVariant
*parameter
)
2951 GSettingsAction
*gsa
= (GSettingsAction
*) action
;
2953 if (g_variant_is_of_type (gsa
->key
.default_value
, G_VARIANT_TYPE_BOOLEAN
))
2957 if (parameter
!= NULL
)
2960 old
= g_settings_action_get_state (action
);
2961 parameter
= g_variant_new_boolean (!g_variant_get_boolean (old
));
2962 g_variant_unref (old
);
2965 g_action_change_state (action
, parameter
);
2969 g_settings_action_get_property (GObject
*object
, guint prop_id
,
2970 GValue
*value
, GParamSpec
*pspec
)
2972 GAction
*action
= G_ACTION (object
);
2976 case ACTION_PROP_NAME
:
2977 g_value_set_string (value
, g_settings_action_get_name (action
));
2980 case ACTION_PROP_PARAMETER_TYPE
:
2981 g_value_set_boxed (value
, g_settings_action_get_parameter_type (action
));
2984 case ACTION_PROP_ENABLED
:
2985 g_value_set_boolean (value
, g_settings_action_get_enabled (action
));
2988 case ACTION_PROP_STATE_TYPE
:
2989 g_value_set_boxed (value
, g_settings_action_get_state_type (action
));
2992 case ACTION_PROP_STATE
:
2993 g_value_set_variant (value
, g_settings_action_get_state (action
));
2997 g_assert_not_reached ();
3002 g_settings_action_finalize (GObject
*object
)
3004 GSettingsAction
*gsa
= (GSettingsAction
*) object
;
3006 g_signal_handlers_disconnect_by_data (gsa
->settings
, gsa
);
3007 g_object_unref (gsa
->settings
);
3009 G_OBJECT_CLASS (g_settings_action_parent_class
)
3010 ->finalize (object
);
3014 g_settings_action_init (GSettingsAction
*gsa
)
3019 g_settings_action_iface_init (GActionInterface
*iface
)
3021 iface
->get_name
= g_settings_action_get_name
;
3022 iface
->get_parameter_type
= g_settings_action_get_parameter_type
;
3023 iface
->get_enabled
= g_settings_action_get_enabled
;
3024 iface
->get_state_type
= g_settings_action_get_state_type
;
3025 iface
->get_state
= g_settings_action_get_state
;
3026 iface
->get_state_hint
= g_settings_action_get_state_hint
;
3027 iface
->change_state
= g_settings_action_change_state
;
3028 iface
->activate
= g_settings_action_activate
;
3032 g_settings_action_class_init (GSettingsActionClass
*class)
3034 class->get_property
= g_settings_action_get_property
;
3035 class->finalize
= g_settings_action_finalize
;
3037 g_object_class_override_property (class, ACTION_PROP_NAME
, "name");
3038 g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE
, "parameter-type");
3039 g_object_class_override_property (class, ACTION_PROP_ENABLED
, "enabled");
3040 g_object_class_override_property (class, ACTION_PROP_STATE_TYPE
, "state-type");
3041 g_object_class_override_property (class, ACTION_PROP_STATE
, "state");
3045 g_settings_action_changed (GSettings
*settings
,
3049 g_object_notify (user_data
, "state");
3053 g_settings_action_enabled_changed (GSettings
*settings
,
3057 g_object_notify (user_data
, "enabled");
3061 * g_settings_create_action:
3062 * @settings: a #GSettings
3063 * @key: the name of a key in @settings
3065 * Creates a #GAction corresponding to a given #GSettings key.
3067 * The action has the same name as the key.
3069 * The value of the key becomes the state of the action and the action
3070 * is enabled when the key is writable. Changing the state of the
3071 * action results in the key being written to. Changes to the value or
3072 * writability of the key cause appropriate change notifications to be
3073 * emitted for the action.
3075 * For boolean-valued keys, action activations take no parameter and
3076 * result in the toggling of the value. For all other types,
3077 * activations take the new value for the key (which must have the
3080 * Returns: (transfer full): a new #GAction
3085 g_settings_create_action (GSettings
*settings
,
3088 GSettingsAction
*gsa
;
3089 gchar
*detailed_signal
;
3091 g_return_val_if_fail (G_IS_SETTINGS (settings
), NULL
);
3092 g_return_val_if_fail (key
!= NULL
, NULL
);
3094 gsa
= g_object_new (g_settings_action_get_type (), NULL
);
3095 gsa
->settings
= g_object_ref (settings
);
3096 g_settings_schema_key_init (&gsa
->key
, settings
->priv
->schema
, key
);
3098 detailed_signal
= g_strdup_printf ("changed::%s", key
);
3099 g_signal_connect (settings
, detailed_signal
, G_CALLBACK (g_settings_action_changed
), gsa
);
3100 g_free (detailed_signal
);
3101 detailed_signal
= g_strdup_printf ("writable-changed::%s", key
);
3102 g_signal_connect (settings
, detailed_signal
, G_CALLBACK (g_settings_action_enabled_changed
), gsa
);
3103 g_free (detailed_signal
);
3105 return G_ACTION (gsa
);
3110 /* vim:set foldmethod=marker: */