Drop use of xinclude in GTestDBus docs
[platform/upstream/glib.git] / gio / gsettings.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 /* Prelude {{{1 */
21 #include "config.h"
22
23 #include <glib.h>
24 #include <glibintl.h>
25
26 #include "gsettings.h"
27
28 #include "gdelayedsettingsbackend.h"
29 #include "gsettingsbackendinternal.h"
30 #include "gsettings-mapping.h"
31 #include "gsettingsschema-internal.h"
32 #include "gaction.h"
33
34 #include "strinfo.c"
35
36 /**
37  * SECTION:gsettings
38  * @short_description: High-level API for application settings
39  * @include: gio/gio.h
40  *
41  * The #GSettings class provides a convenient API for storing and retrieving
42  * application settings.
43  *
44  * Reads and writes can be considered to be non-blocking.  Reading
45  * settings with #GSettings is typically extremely fast: on
46  * approximately the same order of magnitude (but slower than) a
47  * #GHashTable lookup.  Writing settings is also extremely fast in terms
48  * of time to return to your application, but can be extremely expensive
49  * for other threads and other processes.  Many settings backends
50  * (including dconf) have lazy initialisation which means in the common
51  * case of the user using their computer without modifying any settings
52  * a lot of work can be avoided.  For dconf, the D-Bus service doesn't
53  * even need to be started in this case.  For this reason, you should
54  * only ever modify #GSettings keys in response to explicit user action.
55  * Particular care should be paid to ensure that modifications are not
56  * made during startup -- for example, when setting the initial value
57  * of preferences widgets.  The built-in g_settings_bind() functionality
58  * is careful not to write settings in response to notify signals as a
59  * result of modifications that it makes to widgets.
60  *
61  * When creating a GSettings instance, you have to specify a schema
62  * that describes the keys in your settings and their types and default
63  * values, as well as some other information.
64  *
65  * Normally, a schema has as fixed path that determines where the settings
66  * are stored in the conceptual global tree of settings. However, schemas
67  * can also be 'relocatable', i.e. not equipped with a fixed path. This is
68  * useful e.g. when the schema describes an 'account', and you want to be
69  * able to store a arbitrary number of accounts.
70  *
71  * Paths must start with and end with a forward slash character ('/')
72  * and must not contain two sequential slash characters.  Paths should
73  * be chosen based on a domain name associated with the program or
74  * library to which the settings belong.  Examples of paths are
75  * "/org/gtk/settings/file-chooser/" and "/ca/desrt/dconf-editor/".
76  * Paths should not start with "/apps/", "/desktop/" or "/system/" as
77  * they often did in GConf.
78  *
79  * Unlike other configuration systems (like GConf), GSettings does not
80  * restrict keys to basic types like strings and numbers. GSettings stores
81  * values as #GVariant, and allows any #GVariantType for keys. Key names
82  * are restricted to lowercase characters, numbers and '-'. Furthermore,
83  * the names must begin with a lowercase character, must not end
84  * with a '-', and must not contain consecutive dashes.
85  *
86  * Similar to GConf, the default values in GSettings schemas can be
87  * localized, but the localized values are stored in gettext catalogs
88  * and looked up with the domain that is specified in the
89  * <tag class="attribute">gettext-domain</tag> attribute of the
90  * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
91  * elements and the category that is specified in the l10n attribute of the
92  * <tag class="starttag">key</tag> element.
93  *
94  * GSettings uses schemas in a compact binary form that is created
95  * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
96  * utility. The input is a schema description in an XML format.
97  *
98  * A DTD for the gschema XML format can be found here: <ulink url="https://git.gnome.org/browse/glib/tree/gio/gschema.dtd">gschema.dtd</ulink>
99  *
100  * The <link linkend="glib-compile-schemas">glib-compile-schemas</link>
101  * tool expects schema files to have the extension
102  * <filename>.gschema.xml</filename>
103  *
104  * At runtime, schemas are identified by their id (as specified
105  * in the <tag class="attribute">id</tag> attribute of the
106  * <tag class="starttag">schema</tag> element). The
107  * convention for schema ids is to use a dotted name, similar in
108  * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
109  * if the settings are for a specific service that owns a D-Bus bus name,
110  * the D-Bus bus name and schema id should match. For schemas which deal
111  * with settings not associated with one named application, the id should
112  * not use StudlyCaps, e.g. "org.gnome.font-rendering".
113  *
114  * In addition to #GVariant types, keys can have types that have enumerated
115  * types. These can be described by a <tag class="starttag">choice</tag>,
116  * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
117  * <xref linkend="schema-enumerated"/>. The underlying type of
118  * such a key is string, but you can use g_settings_get_enum(),
119  * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
120  * access the numeric values corresponding to the string value of enum
121  * and flags keys.
122  *
123  * An example for default value:
124  * |[
125  * <schemalist>
126  *   <schema id="org.gtk.Test" path="/org/gtk/Test/" gettext-domain="test">
127  *
128  *     <key name="greeting" type="s">
129  *       <default l10n="messages">"Hello, earthlings"</default>
130  *       <summary>A greeting</summary>
131  *       <description>
132  *         Greeting of the invading martians
133  *       </description>
134  *     </key>
135  *
136  *     <key name="box" type="(ii)">
137  *       <default>(20,30)</default>
138  *     </key>
139  *
140  *   </schema>
141  * </schemalist>
142  * ]|
143  *
144  * An example for ranges, choices and enumerated types:
145  * |[
146  * <schemalist>
147  *
148  *   <enum id="org.gtk.Test.myenum">
149  *     <value nick="first" value="1"/>
150  *     <value nick="second" value="2"/>
151  *   </enum>
152  *
153  *   <flags id="org.gtk.Test.myflags">
154  *     <value nick="flag1" value="1"/>
155  *     <value nick="flag2" value="2"/>
156  *     <value nick="flag3" value="4"/>
157  *   </flags>
158  *
159  *   <schema id="org.gtk.Test">
160  *
161  *     <key name="key-with-range" type="i">
162  *       <range min="1" max="100"/>
163  *       <default>10</default>
164  *     </key>
165  *
166  *     <key name="key-with-choices" type="s">
167  *       <choices>
168  *         <choice value='Elisabeth'/>
169  *         <choice value='Annabeth'/>
170  *         <choice value='Joe'/>
171  *       </choices>
172  *       <aliases>
173  *         <alias value='Anna' target='Annabeth'/>
174  *         <alias value='Beth' target='Elisabeth'/>
175  *       </aliases>
176  *       <default>'Joe'</default>
177  *     </key>
178  *
179  *     <key name='enumerated-key' enum='org.gtk.Test.myenum'>
180  *       <default>'first'</default>
181  *     </key>
182  *
183  *     <key name='flags-key' flags='org.gtk.Test.myflags'>
184  *       <default>["flag1",flag2"]</default>
185  *     </key>
186  *   </schema>
187  * </schemalist>
188  * ]|
189  *
190  * ## Vendor overrides
191  *
192  * Default values are defined in the schemas that get installed by
193  * an application. Sometimes, it is necessary for a vendor or distributor
194  * to adjust these defaults. Since patching the XML source for the schema
195  * is inconvenient and error-prone,
196  * <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
197  * so-called 'vendor override' files. These are keyfiles in the same
198  * directory as the XML schema sources which can override default values.
199  * The schema id serves as the group name in the key file, and the values
200  * are expected in serialized GVariant form, as in the following example:
201  * |[
202  *     [org.gtk.Example]
203  *     key1='string'
204  *     key2=1.5
205  * ]|
206  *
207  * glib-compile-schemas expects schema files to have the extension
208  * <filename>.gschema.override</filename>
209  *
210  * ## Binding
211  *
212  * A very convenient feature of GSettings lets you bind #GObject properties
213  * directly to settings, using g_settings_bind(). Once a GObject property
214  * has been bound to a setting, changes on either side are automatically
215  * propagated to the other side. GSettings handles details like mapping
216  * between GObject and GVariant types, and preventing infinite cycles.
217  *
218  * This makes it very easy to hook up a preferences dialog to the
219  * underlying settings. To make this even more convenient, GSettings
220  * looks for a boolean property with the name "sensitivity" and
221  * automatically binds it to the writability of the bound setting.
222  * If this 'magic' gets in the way, it can be suppressed with the
223  * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
224  */
225
226 struct _GSettingsPrivate
227 {
228   /* where the signals go... */
229   GMainContext *main_context;
230
231   GSettingsBackend *backend;
232   GSettingsSchema *schema;
233   gchar *path;
234
235   GDelayedSettingsBackend *delayed;
236 };
237
238 enum
239 {
240   PROP_0,
241   PROP_SCHEMA,
242   PROP_SCHEMA_ID,
243   PROP_BACKEND,
244   PROP_PATH,
245   PROP_HAS_UNAPPLIED,
246   PROP_DELAY_APPLY
247 };
248
249 enum
250 {
251   SIGNAL_WRITABLE_CHANGE_EVENT,
252   SIGNAL_WRITABLE_CHANGED,
253   SIGNAL_CHANGE_EVENT,
254   SIGNAL_CHANGED,
255   N_SIGNALS
256 };
257
258 static guint g_settings_signals[N_SIGNALS];
259
260 G_DEFINE_TYPE_WITH_PRIVATE (GSettings, g_settings, G_TYPE_OBJECT)
261
262 /* Signals {{{1 */
263 static gboolean
264 g_settings_real_change_event (GSettings    *settings,
265                               const GQuark *keys,
266                               gint          n_keys)
267 {
268   gint i;
269
270   if (keys == NULL)
271     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
272
273   for (i = 0; i < n_keys; i++)
274     {
275       const gchar *key = g_quark_to_string (keys[i]);
276
277       if (g_str_has_suffix (key, "/"))
278         continue;
279
280       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED], keys[i], key);
281     }
282
283   return FALSE;
284 }
285
286 static gboolean
287 g_settings_real_writable_change_event (GSettings *settings,
288                                        GQuark     key)
289 {
290   const GQuark *keys = &key;
291   gint n_keys = 1;
292   gint i;
293
294   if (key == 0)
295     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
296
297   for (i = 0; i < n_keys; i++)
298     {
299       const gchar *key = g_quark_to_string (keys[i]);
300
301       if (g_str_has_suffix (key, "/"))
302         continue;
303
304       g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED], keys[i], key);
305     }
306
307   return FALSE;
308 }
309
310 static void
311 settings_backend_changed (GObject             *target,
312                           GSettingsBackend    *backend,
313                           const gchar         *key,
314                           gpointer             origin_tag)
315 {
316   GSettings *settings = G_SETTINGS (target);
317   gboolean ignore_this;
318   gint i;
319
320   /* We used to assert here:
321    *
322    *   settings->priv->backend == backend
323    *
324    * but it could be the case that a notification is queued for delivery
325    * while someone calls g_settings_delay() (which changes the backend).
326    *
327    * Since the delay backend would just pass that straight through
328    * anyway, it doesn't make sense to try to detect this case.
329    * Therefore, we just accept it.
330    */
331
332   for (i = 0; key[i] == settings->priv->path[i]; i++);
333
334   if (settings->priv->path[i] == '\0' &&
335       g_settings_schema_has_key (settings->priv->schema, key + i))
336     {
337       GQuark quark;
338
339       quark = g_quark_from_string (key + i);
340       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
341                      0, &quark, 1, &ignore_this);
342     }
343 }
344
345 static void
346 settings_backend_path_changed (GObject          *target,
347                                GSettingsBackend *backend,
348                                const gchar      *path,
349                                gpointer          origin_tag)
350 {
351   GSettings *settings = G_SETTINGS (target);
352   gboolean ignore_this;
353
354   if (g_str_has_prefix (settings->priv->path, path))
355     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
356                    0, NULL, 0, &ignore_this);
357 }
358
359 static void
360 settings_backend_keys_changed (GObject             *target,
361                                GSettingsBackend    *backend,
362                                const gchar         *path,
363                                const gchar * const *items,
364                                gpointer             origin_tag)
365 {
366   GSettings *settings = G_SETTINGS (target);
367   gboolean ignore_this;
368   gint i;
369
370   for (i = 0; settings->priv->path[i] &&
371               settings->priv->path[i] == path[i]; i++);
372
373   if (path[i] == '\0')
374     {
375       GQuark quarks[256];
376       gint j, l = 0;
377
378       for (j = 0; items[j]; j++)
379          {
380            const gchar *item = items[j];
381            gint k;
382
383            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
384
385            if (settings->priv->path[i + k] == '\0' &&
386                g_settings_schema_has_key (settings->priv->schema, item + k))
387              quarks[l++] = g_quark_from_string (item + k);
388
389            /* "256 quarks ought to be enough for anybody!"
390             * If this bites you, I'm sorry.  Please file a bug.
391             */
392            g_assert (l < 256);
393          }
394
395       if (l > 0)
396         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
397                        0, quarks, l, &ignore_this);
398     }
399 }
400
401 static void
402 settings_backend_writable_changed (GObject          *target,
403                                    GSettingsBackend *backend,
404                                    const gchar      *key)
405 {
406   GSettings *settings = G_SETTINGS (target);
407   gboolean ignore_this;
408   gint i;
409
410   for (i = 0; key[i] == settings->priv->path[i]; i++);
411
412   if (settings->priv->path[i] == '\0' &&
413       g_settings_schema_has_key (settings->priv->schema, key + i))
414     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
415                    0, g_quark_from_string (key + i), &ignore_this);
416 }
417
418 static void
419 settings_backend_path_writable_changed (GObject          *target,
420                                         GSettingsBackend *backend,
421                                         const gchar      *path)
422 {
423   GSettings *settings = G_SETTINGS (target);
424   gboolean ignore_this;
425
426   if (g_str_has_prefix (settings->priv->path, path))
427     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
428                    0, (GQuark) 0, &ignore_this);
429 }
430
431 /* Properties, Construction, Destruction {{{1 */
432 static void
433 g_settings_set_property (GObject      *object,
434                          guint         prop_id,
435                          const GValue *value,
436                          GParamSpec   *pspec)
437 {
438   GSettings *settings = G_SETTINGS (object);
439
440   switch (prop_id)
441     {
442     case PROP_SCHEMA:
443       {
444         GSettingsSchema *schema;
445
446         schema = g_value_dup_boxed (value);
447
448         /* we receive a set_property() call for "settings-schema" even
449          * if it was not specified (ie: with NULL value).  ->schema
450          * could already be set at this point (ie: via "schema-id").
451          * check for NULL to avoid clobbering the existing value.
452          */
453         if (schema != NULL)
454           {
455             g_assert (settings->priv->schema == NULL);
456             settings->priv->schema = schema;
457           }
458       }
459       break;
460
461     case PROP_SCHEMA_ID:
462       {
463         const gchar *schema_id;
464
465         schema_id = g_value_get_string (value);
466
467         /* we receive a set_property() call for both "schema" and
468          * "schema-id", even if they are not set.  Hopefully only one of
469          * them is non-NULL.
470          */
471         if (schema_id != NULL)
472           {
473             GSettingsSchemaSource *default_source;
474
475             g_assert (settings->priv->schema == NULL);
476             default_source = g_settings_schema_source_get_default ();
477
478             if (default_source == NULL)
479               g_error ("No GSettings schemas are installed on the system");
480
481             settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);
482
483             if (settings->priv->schema == NULL)
484               g_error ("Settings schema '%s' is not installed\n", schema_id);
485           }
486       }
487       break;
488
489     case PROP_PATH:
490       settings->priv->path = g_value_dup_string (value);
491       break;
492
493     case PROP_BACKEND:
494       settings->priv->backend = g_value_dup_object (value);
495       break;
496
497     default:
498       g_assert_not_reached ();
499     }
500 }
501
502 static void
503 g_settings_get_property (GObject    *object,
504                          guint       prop_id,
505                          GValue     *value,
506                          GParamSpec *pspec)
507 {
508   GSettings *settings = G_SETTINGS (object);
509
510   switch (prop_id)
511     {
512     case PROP_SCHEMA:
513       g_value_set_boxed (value, settings->priv->schema);
514       break;
515
516      case PROP_SCHEMA_ID:
517       g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
518       break;
519
520      case PROP_BACKEND:
521       g_value_set_object (value, settings->priv->backend);
522       break;
523
524      case PROP_PATH:
525       g_value_set_string (value, settings->priv->path);
526       break;
527
528      case PROP_HAS_UNAPPLIED:
529       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
530       break;
531
532      case PROP_DELAY_APPLY:
533       g_value_set_boolean (value, settings->priv->delayed != NULL);
534       break;
535
536      default:
537       g_assert_not_reached ();
538     }
539 }
540
541 static const GSettingsListenerVTable listener_vtable = {
542   settings_backend_changed,
543   settings_backend_path_changed,
544   settings_backend_keys_changed,
545   settings_backend_writable_changed,
546   settings_backend_path_writable_changed
547 };
548
549 static void
550 g_settings_constructed (GObject *object)
551 {
552   GSettings *settings = G_SETTINGS (object);
553   const gchar *schema_path;
554
555   schema_path = g_settings_schema_get_path (settings->priv->schema);
556
557   if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
558     g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
559              g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
560
561   if (settings->priv->path == NULL)
562     {
563       if (schema_path == NULL)
564         g_error ("attempting to create schema '%s' without a path",
565                  g_settings_schema_get_id (settings->priv->schema));
566
567       settings->priv->path = g_strdup (schema_path);
568     }
569
570   if (settings->priv->backend == NULL)
571     settings->priv->backend = g_settings_backend_get_default ();
572
573   g_settings_backend_watch (settings->priv->backend,
574                             &listener_vtable, G_OBJECT (settings),
575                             settings->priv->main_context);
576   g_settings_backend_subscribe (settings->priv->backend,
577                                 settings->priv->path);
578 }
579
580 static void
581 g_settings_finalize (GObject *object)
582 {
583   GSettings *settings = G_SETTINGS (object);
584
585   g_settings_backend_unsubscribe (settings->priv->backend,
586                                   settings->priv->path);
587   g_main_context_unref (settings->priv->main_context);
588   g_object_unref (settings->priv->backend);
589   g_settings_schema_unref (settings->priv->schema);
590   g_free (settings->priv->path);
591
592   G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
593 }
594
595 static void
596 g_settings_init (GSettings *settings)
597 {
598   settings->priv = g_settings_get_instance_private (settings);
599   settings->priv->main_context = g_main_context_ref_thread_default ();
600 }
601
602 static void
603 g_settings_class_init (GSettingsClass *class)
604 {
605   GObjectClass *object_class = G_OBJECT_CLASS (class);
606
607   class->writable_change_event = g_settings_real_writable_change_event;
608   class->change_event = g_settings_real_change_event;
609
610   object_class->set_property = g_settings_set_property;
611   object_class->get_property = g_settings_get_property;
612   object_class->constructed = g_settings_constructed;
613   object_class->finalize = g_settings_finalize;
614
615   /**
616    * GSettings::changed:
617    * @settings: the object on which the signal was emitted
618    * @key: the name of the key that changed
619    *
620    * The "changed" signal is emitted when a key has potentially changed.
621    * You should call one of the g_settings_get() calls to check the new
622    * value.
623    *
624    * This signal supports detailed connections.  You can connect to the
625    * detailed signal "changed::x" in order to only receive callbacks
626    * when key "x" changes.
627    */
628   g_settings_signals[SIGNAL_CHANGED] =
629     g_signal_new ("changed", G_TYPE_SETTINGS,
630                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
631                   G_STRUCT_OFFSET (GSettingsClass, changed),
632                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
633                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
634
635   /**
636    * GSettings::change-event:
637    * @settings: the object on which the signal was emitted
638    * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
639    *        an array of #GQuarks for the changed keys, or %NULL
640    * @n_keys: the length of the @keys array, or 0
641    *
642    * The "change-event" signal is emitted once per change event that
643    * affects this settings object.  You should connect to this signal
644    * only if you are interested in viewing groups of changes before they
645    * are split out into multiple emissions of the "changed" signal.
646    * For most use cases it is more appropriate to use the "changed" signal.
647    *
648    * In the event that the change event applies to one or more specified
649    * keys, @keys will be an array of #GQuark of length @n_keys.  In the
650    * event that the change event applies to the #GSettings object as a
651    * whole (ie: potentially every key has been changed) then @keys will
652    * be %NULL and @n_keys will be 0.
653    *
654    * The default handler for this signal invokes the "changed" signal
655    * for each affected key.  If any other connected handler returns
656    * %TRUE then this default functionality will be suppressed.
657    *
658    * Returns: %TRUE to stop other handlers from being invoked for the
659    *          event. FALSE to propagate the event further.
660    */
661   g_settings_signals[SIGNAL_CHANGE_EVENT] =
662     g_signal_new ("change-event", G_TYPE_SETTINGS,
663                   G_SIGNAL_RUN_LAST,
664                   G_STRUCT_OFFSET (GSettingsClass, change_event),
665                   g_signal_accumulator_true_handled, NULL,
666                   NULL,
667                   G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
668
669   /**
670    * GSettings::writable-changed:
671    * @settings: the object on which the signal was emitted
672    * @key: the key
673    *
674    * The "writable-changed" signal is emitted when the writability of a
675    * key has potentially changed.  You should call
676    * g_settings_is_writable() in order to determine the new status.
677    *
678    * This signal supports detailed connections.  You can connect to the
679    * detailed signal "writable-changed::x" in order to only receive
680    * callbacks when the writability of "x" changes.
681    */
682   g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
683     g_signal_new ("writable-changed", G_TYPE_SETTINGS,
684                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
685                   G_STRUCT_OFFSET (GSettingsClass, writable_changed),
686                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
687                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
688
689   /**
690    * GSettings::writable-change-event:
691    * @settings: the object on which the signal was emitted
692    * @key: the quark of the key, or 0
693    *
694    * The "writable-change-event" signal is emitted once per writability
695    * change event that affects this settings object.  You should connect
696    * to this signal if you are interested in viewing groups of changes
697    * before they are split out into multiple emissions of the
698    * "writable-changed" signal.  For most use cases it is more
699    * appropriate to use the "writable-changed" signal.
700    *
701    * In the event that the writability change applies only to a single
702    * key, @key will be set to the #GQuark for that key.  In the event
703    * that the writability change affects the entire settings object,
704    * @key will be 0.
705    *
706    * The default handler for this signal invokes the "writable-changed"
707    * and "changed" signals for each affected key.  This is done because
708    * changes in writability might also imply changes in value (if for
709    * example, a new mandatory setting is introduced).  If any other
710    * connected handler returns %TRUE then this default functionality
711    * will be suppressed.
712    *
713    * Returns: %TRUE to stop other handlers from being invoked for the
714    *          event. FALSE to propagate the event further.
715    */
716   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
717     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
718                   G_SIGNAL_RUN_LAST,
719                   G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
720                   g_signal_accumulator_true_handled, NULL,
721                   NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
722
723   /**
724    * GSettings:context:
725    *
726    * The name of the context that the settings are stored in.
727    */
728   g_object_class_install_property (object_class, PROP_BACKEND,
729     g_param_spec_object ("backend",
730                          P_("GSettingsBackend"),
731                          P_("The GSettingsBackend for this settings object"),
732                          G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
733                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
734
735   /**
736    * GSettings:settings-schema:
737    *
738    * The #GSettingsSchema describing the types of keys for this
739    * #GSettings object.
740    *
741    * Ideally, this property would be called 'schema'.  #GSettingsSchema
742    * has only existed since version 2.32, however, and before then the
743    * 'schema' property was used to refer to the ID of the schema rather
744    * than the schema itself.  Take care.
745    */
746   g_object_class_install_property (object_class, PROP_SCHEMA,
747     g_param_spec_boxed ("settings-schema",
748                         P_("schema"),
749                         P_("The GSettingsSchema for this settings object"),
750                         G_TYPE_SETTINGS_SCHEMA,
751                         G_PARAM_CONSTRUCT_ONLY |
752                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
753
754   /**
755    * GSettings:schema:
756    *
757    * The name of the schema that describes the types of keys
758    * for this #GSettings object.
759    *
760    * The type of this property is *not* #GSettingsSchema.
761    * #GSettingsSchema has only existed since version 2.32 and
762    * unfortunately this name was used in previous versions to refer to
763    * the schema ID rather than the schema itself.  Take care to use the
764    * 'settings-schema' property if you wish to pass in a
765    * #GSettingsSchema.
766    *
767    * Deprecated:2.32:Use the 'schema-id' property instead.  In a future
768    * version, this property may instead refer to a #GSettingsSchema.
769    */
770   g_object_class_install_property (object_class, PROP_SCHEMA_ID,
771     g_param_spec_string ("schema",
772                          P_("Schema name"),
773                          P_("The name of the schema for this settings object"),
774                          NULL,
775                          G_PARAM_CONSTRUCT_ONLY |
776                          G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
777
778   /**
779    * GSettings:schema-id:
780    *
781    * The name of the schema that describes the types of keys
782    * for this #GSettings object.
783    */
784   g_object_class_install_property (object_class, PROP_SCHEMA_ID,
785     g_param_spec_string ("schema-id",
786                          P_("Schema name"),
787                          P_("The name of the schema for this settings object"),
788                          NULL,
789                          G_PARAM_CONSTRUCT_ONLY |
790                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
791
792    /**
793     * GSettings:path:
794     *
795     * The path within the backend where the settings are stored.
796     */
797    g_object_class_install_property (object_class, PROP_PATH,
798      g_param_spec_string ("path",
799                           P_("Base path"),
800                           P_("The path within the backend where the settings are"),
801                           NULL,
802                           G_PARAM_CONSTRUCT_ONLY |
803                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
804
805    /**
806     * GSettings:has-unapplied:
807     *
808     * If this property is %TRUE, the #GSettings object has outstanding
809     * changes that will be applied when g_settings_apply() is called.
810     */
811    g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
812      g_param_spec_boolean ("has-unapplied",
813                            P_("Has unapplied changes"),
814                            P_("TRUE if there are outstanding changes to apply()"),
815                            FALSE,
816                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
817
818    /**
819     * GSettings:delay-apply:
820     *
821     * Whether the #GSettings object is in 'delay-apply' mode. See
822     * g_settings_delay() for details.
823     *
824     * Since: 2.28
825     */
826    g_object_class_install_property (object_class, PROP_DELAY_APPLY,
827      g_param_spec_boolean ("delay-apply",
828                            P_("Delay-apply mode"),
829                            P_("Whether this settings object is in 'delay-apply' mode"),
830                            FALSE,
831                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
832 }
833
834 /* Construction (new, new_with_path, etc.) {{{1 */
835 /**
836  * g_settings_new:
837  * @schema_id: the id of the schema
838  *
839  * Creates a new #GSettings object with the schema specified by
840  * @schema_id.
841  *
842  * Signals on the newly created #GSettings object will be dispatched
843  * via the thread-default #GMainContext in effect at the time of the
844  * call to g_settings_new().  The new #GSettings will hold a reference
845  * on the context.  See g_main_context_push_thread_default().
846  *
847  * Returns: a new #GSettings object
848  *
849  * Since: 2.26
850  */
851 GSettings *
852 g_settings_new (const gchar *schema_id)
853 {
854   g_return_val_if_fail (schema_id != NULL, NULL);
855
856   return g_object_new (G_TYPE_SETTINGS,
857                        "schema-id", schema_id,
858                        NULL);
859 }
860
861 static gboolean
862 path_is_valid (const gchar *path)
863 {
864   if (!path)
865     return FALSE;
866
867   if (path[0] != '/')
868     return FALSE;
869
870   if (!g_str_has_suffix (path, "/"))
871     return FALSE;
872
873   return strstr (path, "//") == NULL;
874 }
875
876 /**
877  * g_settings_new_with_path:
878  * @schema_id: the id of the schema
879  * @path: the path to use
880  *
881  * Creates a new #GSettings object with the relocatable schema specified
882  * by @schema_id and a given path.
883  *
884  * You only need to do this if you want to directly create a settings
885  * object with a schema that doesn't have a specified path of its own.
886  * That's quite rare.
887  *
888  * It is a programmer error to call this function for a schema that
889  * has an explicitly specified path.
890  *
891  * It is a programmer error if @path is not a valid path.  A valid path
892  * begins and ends with '/' and does not contain two consecutive '/'
893  * characters.
894  *
895  * Returns: a new #GSettings object
896  *
897  * Since: 2.26
898  */
899 GSettings *
900 g_settings_new_with_path (const gchar *schema_id,
901                           const gchar *path)
902 {
903   g_return_val_if_fail (schema_id != NULL, NULL);
904   g_return_val_if_fail (path_is_valid (path), NULL);
905
906   return g_object_new (G_TYPE_SETTINGS,
907                        "schema-id", schema_id,
908                        "path", path,
909                        NULL);
910 }
911
912 /**
913  * g_settings_new_with_backend:
914  * @schema_id: the id of the schema
915  * @backend: the #GSettingsBackend to use
916  *
917  * Creates a new #GSettings object with the schema specified by
918  * @schema_id and a given #GSettingsBackend.
919  *
920  * Creating a #GSettings object with a different backend allows accessing
921  * settings from a database other than the usual one. For example, it may make
922  * sense to pass a backend corresponding to the "defaults" settings database on
923  * the system to get a settings object that modifies the system default
924  * settings instead of the settings for this user.
925  *
926  * Returns: a new #GSettings object
927  *
928  * Since: 2.26
929  */
930 GSettings *
931 g_settings_new_with_backend (const gchar      *schema_id,
932                              GSettingsBackend *backend)
933 {
934   g_return_val_if_fail (schema_id != NULL, NULL);
935   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
936
937   return g_object_new (G_TYPE_SETTINGS,
938                        "schema-id", schema_id,
939                        "backend", backend,
940                        NULL);
941 }
942
943 /**
944  * g_settings_new_with_backend_and_path:
945  * @schema_id: the id of the schema
946  * @backend: the #GSettingsBackend to use
947  * @path: the path to use
948  *
949  * Creates a new #GSettings object with the schema specified by
950  * @schema_id and a given #GSettingsBackend and path.
951  *
952  * This is a mix of g_settings_new_with_backend() and
953  * g_settings_new_with_path().
954  *
955  * Returns: a new #GSettings object
956  *
957  * Since: 2.26
958  */
959 GSettings *
960 g_settings_new_with_backend_and_path (const gchar      *schema_id,
961                                       GSettingsBackend *backend,
962                                       const gchar      *path)
963 {
964   g_return_val_if_fail (schema_id != NULL, NULL);
965   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
966   g_return_val_if_fail (path_is_valid (path), NULL);
967
968   return g_object_new (G_TYPE_SETTINGS,
969                        "schema-id", schema_id,
970                        "backend", backend,
971                        "path", path,
972                        NULL);
973 }
974
975 /**
976  * g_settings_new_full:
977  * @schema: a #GSettingsSchema
978  * @backend: (allow-none): a #GSettingsBackend
979  * @path: (allow-none): the path to use
980  *
981  * Creates a new #GSettings object with a given schema, backend and
982  * path.
983  *
984  * It should be extremely rare that you ever want to use this function.
985  * It is made available for advanced use-cases (such as plugin systems
986  * that want to provide access to schemas loaded from custom locations,
987  * etc).
988  *
989  * At the most basic level, a #GSettings object is a pure composition of
990  * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
991  * backend, and a #GMainContext to which signals are dispatched.
992  *
993  * This constructor therefore gives you full control over constructing
994  * #GSettings instances.  The first 4 parameters are given directly as
995  * @schema, @backend and @path, and the main context is taken from the
996  * thread-default (as per g_settings_new()).
997  *
998  * If @backend is %NULL then the default backend is used.
999  *
1000  * If @path is %NULL then the path from the schema is used.  It is an
1001  * error f @path is %NULL and the schema has no path of its own or if
1002  * @path is non-%NULL and not equal to the path that the schema does
1003  * have.
1004  *
1005  * Returns: a new #GSettings object
1006  *
1007  * Since: 2.32
1008  */
1009 GSettings *
1010 g_settings_new_full (GSettingsSchema  *schema,
1011                      GSettingsBackend *backend,
1012                      const gchar      *path)
1013 {
1014   g_return_val_if_fail (schema != NULL, NULL);
1015   g_return_val_if_fail (backend == NULL || G_IS_SETTINGS_BACKEND (backend), NULL);
1016   g_return_val_if_fail (path == NULL || path_is_valid (path), NULL);
1017
1018   return g_object_new (G_TYPE_SETTINGS,
1019                        "settings-schema", schema,
1020                        "backend", backend,
1021                        "path", path,
1022                        NULL);
1023 }
1024
1025 /* Internal read/write utilities {{{1 */
1026 static gboolean
1027 g_settings_write_to_backend (GSettings          *settings,
1028                              GSettingsSchemaKey *key,
1029                              GVariant           *value)
1030 {
1031   gboolean success;
1032   gchar *path;
1033
1034   path = g_strconcat (settings->priv->path, key->name, NULL);
1035   success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1036   g_free (path);
1037
1038   return success;
1039 }
1040
1041 static GVariant *
1042 g_settings_read_from_backend (GSettings          *settings,
1043                               GSettingsSchemaKey *key,
1044                               gboolean            user_value_only,
1045                               gboolean            default_value)
1046 {
1047   GVariant *value;
1048   GVariant *fixup;
1049   gchar *path;
1050
1051   path = g_strconcat (settings->priv->path, key->name, NULL);
1052   if (user_value_only)
1053     value = g_settings_backend_read_user_value (settings->priv->backend, path, key->type);
1054   else
1055     value = g_settings_backend_read (settings->priv->backend, path, key->type, default_value);
1056   g_free (path);
1057
1058   if (value != NULL)
1059     {
1060       fixup = g_settings_schema_key_range_fixup (key, value);
1061       g_variant_unref (value);
1062     }
1063   else
1064     fixup = NULL;
1065
1066   return fixup;
1067 }
1068
1069 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1070 /**
1071  * g_settings_get_value:
1072  * @settings: a #GSettings object
1073  * @key: the key to get the value for
1074  *
1075  * Gets the value that is stored in @settings for @key.
1076  *
1077  * It is a programmer error to give a @key that isn't contained in the
1078  * schema for @settings.
1079  *
1080  * Returns: a new #GVariant
1081  *
1082  * Since: 2.26
1083  */
1084 GVariant *
1085 g_settings_get_value (GSettings   *settings,
1086                       const gchar *key)
1087 {
1088   GSettingsSchemaKey skey;
1089   GVariant *value;
1090
1091   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1092   g_return_val_if_fail (key != NULL, NULL);
1093
1094   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1095   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1096
1097   if (value == NULL)
1098     value = g_settings_schema_key_get_translated_default (&skey);
1099
1100   if (value == NULL)
1101     value = g_variant_ref (skey.default_value);
1102
1103   g_settings_schema_key_clear (&skey);
1104
1105   return value;
1106 }
1107
1108 /**
1109  * g_settings_get_user_value:
1110  * @settings: a #GSettings object
1111  * @key: the key to get the user value for
1112  *
1113  * Checks the "user value" of a key, if there is one.
1114  *
1115  * The user value of a key is the last value that was set by the user.
1116  *
1117  * After calling g_settings_reset() this function should always return
1118  * %NULL (assuming something is not wrong with the system
1119  * configuration).
1120  *
1121  * It is possible that g_settings_get_value() will return a different
1122  * value than this function.  This can happen in the case that the user
1123  * set a value for a key that was subsequently locked down by the system
1124  * administrator -- this function will return the user's old value.
1125  *
1126  * This function may be useful for adding a "reset" option to a UI or
1127  * for providing indication that a particular value has been changed.
1128  *
1129  * It is a programmer error to give a @key that isn't contained in the
1130  * schema for @settings.
1131  *
1132  * Returns: (allow-none) (transfer full): the user's value, if set
1133  *
1134  * Since: 2.40
1135  **/
1136 GVariant *
1137 g_settings_get_user_value (GSettings   *settings,
1138                            const gchar *key)
1139 {
1140   GSettingsSchemaKey skey;
1141   GVariant *value;
1142
1143   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1144   g_return_val_if_fail (key != NULL, NULL);
1145
1146   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1147   value = g_settings_read_from_backend (settings, &skey, TRUE, FALSE);
1148   g_settings_schema_key_clear (&skey);
1149
1150   return value;
1151 }
1152
1153 /**
1154  * g_settings_get_default_value:
1155  * @settings: a #GSettings object
1156  * @key: the key to get the default value for
1157  *
1158  * Gets the "default value" of a key.
1159  *
1160  * This is the value that would be read if g_settings_reset() were to be
1161  * called on the key.
1162  *
1163  * Note that this may be a different value than returned by
1164  * g_settings_schema_key_get_default_value() if the system administrator
1165  * has provided a default value.
1166  *
1167  * Comparing the return values of g_settings_get_default_value() and
1168  * g_settings_get_value() is not sufficient for determining if a value
1169  * has been set because the user may have explicitly set the value to
1170  * something that happens to be equal to the default.  The difference
1171  * here is that if the default changes in the future, the user's key
1172  * will still be set.
1173  *
1174  * This function may be useful for adding an indication to a UI of what
1175  * the default value was before the user set it.
1176  *
1177  * It is a programmer error to give a @key that isn't contained in the
1178  * schema for @settings.
1179  *
1180  * Returns: (allow-none) (transfer full): the default value
1181  *
1182  * Since: 2.40
1183  **/
1184 GVariant *
1185 g_settings_get_default_value (GSettings   *settings,
1186                               const gchar *key)
1187 {
1188   GSettingsSchemaKey skey;
1189   GVariant *value;
1190
1191   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1192   g_return_val_if_fail (key != NULL, NULL);
1193
1194   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1195   value = g_settings_read_from_backend (settings, &skey, FALSE, TRUE);
1196
1197   if (value == NULL)
1198     value = g_settings_schema_key_get_translated_default (&skey);
1199
1200   if (value == NULL)
1201     value = g_variant_ref (skey.default_value);
1202
1203   g_settings_schema_key_clear (&skey);
1204
1205   return value;
1206 }
1207
1208 /**
1209  * g_settings_get_enum:
1210  * @settings: a #GSettings object
1211  * @key: the key to get the value for
1212  *
1213  * Gets the value that is stored in @settings for @key and converts it
1214  * to the enum value that it represents.
1215  *
1216  * In order to use this function the type of the value must be a string
1217  * and it must be marked in the schema file as an enumerated type.
1218  *
1219  * It is a programmer error to give a @key that isn't contained in the
1220  * schema for @settings or is not marked as an enumerated type.
1221  *
1222  * If the value stored in the configuration database is not a valid
1223  * value for the enumerated type then this function will return the
1224  * default value.
1225  *
1226  * Returns: the enum value
1227  *
1228  * Since: 2.26
1229  **/
1230 gint
1231 g_settings_get_enum (GSettings   *settings,
1232                      const gchar *key)
1233 {
1234   GSettingsSchemaKey skey;
1235   GVariant *value;
1236   gint result;
1237
1238   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1239   g_return_val_if_fail (key != NULL, -1);
1240
1241   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1242
1243   if (!skey.is_enum)
1244     {
1245       g_critical ("g_settings_get_enum() called on key '%s' which is not "
1246                   "associated with an enumerated type", skey.name);
1247       g_settings_schema_key_clear (&skey);
1248       return -1;
1249     }
1250
1251   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1252
1253   if (value == NULL)
1254     value = g_settings_schema_key_get_translated_default (&skey);
1255
1256   if (value == NULL)
1257     value = g_variant_ref (skey.default_value);
1258
1259   result = g_settings_schema_key_to_enum (&skey, value);
1260   g_settings_schema_key_clear (&skey);
1261   g_variant_unref (value);
1262
1263   return result;
1264 }
1265
1266 /**
1267  * g_settings_set_enum:
1268  * @settings: a #GSettings object
1269  * @key: a key, within @settings
1270  * @value: an enumerated value
1271  *
1272  * Looks up the enumerated type nick for @value and writes it to @key,
1273  * within @settings.
1274  *
1275  * It is a programmer error to give a @key that isn't contained in the
1276  * schema for @settings or is not marked as an enumerated type, or for
1277  * @value not to be a valid value for the named type.
1278  *
1279  * After performing the write, accessing @key directly with
1280  * g_settings_get_string() will return the 'nick' associated with
1281  * @value.
1282  *
1283  * Returns: %TRUE, if the set succeeds
1284  **/
1285 gboolean
1286 g_settings_set_enum (GSettings   *settings,
1287                      const gchar *key,
1288                      gint         value)
1289 {
1290   GSettingsSchemaKey skey;
1291   GVariant *variant;
1292   gboolean success;
1293
1294   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1295   g_return_val_if_fail (key != NULL, FALSE);
1296
1297   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1298
1299   if (!skey.is_enum)
1300     {
1301       g_critical ("g_settings_set_enum() called on key '%s' which is not "
1302                   "associated with an enumerated type", skey.name);
1303       return FALSE;
1304     }
1305
1306   if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1307     {
1308       g_critical ("g_settings_set_enum(): invalid enum value %d for key '%s' "
1309                   "in schema '%s'.  Doing nothing.", value, skey.name,
1310                   g_settings_schema_get_id (skey.schema));
1311       g_settings_schema_key_clear (&skey);
1312       return FALSE;
1313     }
1314
1315   success = g_settings_write_to_backend (settings, &skey, variant);
1316   g_settings_schema_key_clear (&skey);
1317
1318   return success;
1319 }
1320
1321 /**
1322  * g_settings_get_flags:
1323  * @settings: a #GSettings object
1324  * @key: the key to get the value for
1325  *
1326  * Gets the value that is stored in @settings for @key and converts it
1327  * to the flags value that it represents.
1328  *
1329  * In order to use this function the type of the value must be an array
1330  * of strings and it must be marked in the schema file as an flags type.
1331  *
1332  * It is a programmer error to give a @key that isn't contained in the
1333  * schema for @settings or is not marked as a flags type.
1334  *
1335  * If the value stored in the configuration database is not a valid
1336  * value for the flags type then this function will return the default
1337  * value.
1338  *
1339  * Returns: the flags value
1340  *
1341  * Since: 2.26
1342  **/
1343 guint
1344 g_settings_get_flags (GSettings   *settings,
1345                       const gchar *key)
1346 {
1347   GSettingsSchemaKey skey;
1348   GVariant *value;
1349   guint result;
1350
1351   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1352   g_return_val_if_fail (key != NULL, -1);
1353
1354   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1355
1356   if (!skey.is_flags)
1357     {
1358       g_critical ("g_settings_get_flags() called on key '%s' which is not "
1359                   "associated with a flags type", skey.name);
1360       g_settings_schema_key_clear (&skey);
1361       return -1;
1362     }
1363
1364   value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE);
1365
1366   if (value == NULL)
1367     value = g_settings_schema_key_get_translated_default (&skey);
1368
1369   if (value == NULL)
1370     value = g_variant_ref (skey.default_value);
1371
1372   result = g_settings_schema_key_to_flags (&skey, value);
1373   g_settings_schema_key_clear (&skey);
1374   g_variant_unref (value);
1375
1376   return result;
1377 }
1378
1379 /**
1380  * g_settings_set_flags:
1381  * @settings: a #GSettings object
1382  * @key: a key, within @settings
1383  * @value: a flags value
1384  *
1385  * Looks up the flags type nicks for the bits specified by @value, puts
1386  * them in an array of strings and writes the array to @key, within
1387  * @settings.
1388  *
1389  * It is a programmer error to give a @key that isn't contained in the
1390  * schema for @settings or is not marked as a flags type, or for @value
1391  * to contain any bits that are not value for the named type.
1392  *
1393  * After performing the write, accessing @key directly with
1394  * g_settings_get_strv() will return an array of 'nicks'; one for each
1395  * bit in @value.
1396  *
1397  * Returns: %TRUE, if the set succeeds
1398  **/
1399 gboolean
1400 g_settings_set_flags (GSettings   *settings,
1401                       const gchar *key,
1402                       guint        value)
1403 {
1404   GSettingsSchemaKey skey;
1405   GVariant *variant;
1406   gboolean success;
1407
1408   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1409   g_return_val_if_fail (key != NULL, FALSE);
1410
1411   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1412
1413   if (!skey.is_flags)
1414     {
1415       g_critical ("g_settings_set_flags() called on key '%s' which is not "
1416                   "associated with a flags type", skey.name);
1417       return FALSE;
1418     }
1419
1420   if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1421     {
1422       g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1423                   "for key '%s' in schema '%s'.  Doing nothing.",
1424                   value, skey.name, g_settings_schema_get_id (skey.schema));
1425       g_settings_schema_key_clear (&skey);
1426       return FALSE;
1427     }
1428
1429   success = g_settings_write_to_backend (settings, &skey, variant);
1430   g_settings_schema_key_clear (&skey);
1431
1432   return success;
1433 }
1434
1435 /**
1436  * g_settings_set_value:
1437  * @settings: a #GSettings object
1438  * @key: the name of the key to set
1439  * @value: a #GVariant of the correct type
1440  *
1441  * Sets @key in @settings to @value.
1442  *
1443  * It is a programmer error to give a @key that isn't contained in the
1444  * schema for @settings or for @value to have the incorrect type, per
1445  * the schema.
1446  *
1447  * If @value is floating then this function consumes the reference.
1448  *
1449  * Returns: %TRUE if setting the key succeeded,
1450  *     %FALSE if the key was not writable
1451  *
1452  * Since: 2.26
1453  **/
1454 gboolean
1455 g_settings_set_value (GSettings   *settings,
1456                       const gchar *key,
1457                       GVariant    *value)
1458 {
1459   GSettingsSchemaKey skey;
1460   gboolean success;
1461
1462   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1463   g_return_val_if_fail (key != NULL, FALSE);
1464
1465   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1466
1467   if (!g_settings_schema_key_type_check (&skey, value))
1468     {
1469       g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1470                   key,
1471                   g_settings_schema_get_id (settings->priv->schema),
1472                   g_variant_type_peek_string (skey.type),
1473                   g_variant_get_type_string (value));
1474
1475         return FALSE;
1476       }
1477
1478   if (!g_settings_schema_key_range_check (&skey, value))
1479     {
1480       g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1481                  "is outside of valid range",
1482                  key,
1483                  g_settings_schema_get_id (settings->priv->schema));
1484
1485         return FALSE;
1486     }
1487
1488   success = g_settings_write_to_backend (settings, &skey, value);
1489   g_settings_schema_key_clear (&skey);
1490
1491   return success;
1492 }
1493
1494 /**
1495  * g_settings_get:
1496  * @settings: a #GSettings object
1497  * @key: the key to get the value for
1498  * @format: a #GVariant format string
1499  * @...: arguments as per @format
1500  *
1501  * Gets the value that is stored at @key in @settings.
1502  *
1503  * A convenience function that combines g_settings_get_value() with
1504  * g_variant_get().
1505  *
1506  * It is a programmer error to give a @key that isn't contained in the
1507  * schema for @settings or for the #GVariantType of @format to mismatch
1508  * the type given in the schema.
1509  *
1510  * Since: 2.26
1511  */
1512 void
1513 g_settings_get (GSettings   *settings,
1514                 const gchar *key,
1515                 const gchar *format,
1516                 ...)
1517 {
1518   GVariant *value;
1519   va_list ap;
1520
1521   value = g_settings_get_value (settings, key);
1522
1523   if (strchr (format, '&'))
1524     {
1525       g_critical ("%s: the format string may not contain '&' (key '%s' from schema '%s'). "
1526                   "This call will probably stop working with a future version of glib.",
1527                   G_STRFUNC, key, g_settings_schema_get_id (settings->priv->schema));
1528     }
1529
1530   va_start (ap, format);
1531   g_variant_get_va (value, format, NULL, &ap);
1532   va_end (ap);
1533
1534   g_variant_unref (value);
1535 }
1536
1537 /**
1538  * g_settings_set:
1539  * @settings: a #GSettings object
1540  * @key: the name of the key to set
1541  * @format: a #GVariant format string
1542  * @...: arguments as per @format
1543  *
1544  * Sets @key in @settings to @value.
1545  *
1546  * A convenience function that combines g_settings_set_value() with
1547  * g_variant_new().
1548  *
1549  * It is a programmer error to give a @key that isn't contained in the
1550  * schema for @settings or for the #GVariantType of @format to mismatch
1551  * the type given in the schema.
1552  *
1553  * Returns: %TRUE if setting the key succeeded,
1554  *     %FALSE if the key was not writable
1555  *
1556  * Since: 2.26
1557  */
1558 gboolean
1559 g_settings_set (GSettings   *settings,
1560                 const gchar *key,
1561                 const gchar *format,
1562                 ...)
1563 {
1564   GVariant *value;
1565   va_list ap;
1566
1567   va_start (ap, format);
1568   value = g_variant_new_va (format, NULL, &ap);
1569   va_end (ap);
1570
1571   return g_settings_set_value (settings, key, value);
1572 }
1573
1574 /**
1575  * g_settings_get_mapped:
1576  * @settings: a #GSettings object
1577  * @key: the key to get the value for
1578  * @mapping: (scope call): the function to map the value in the
1579  *           settings database to the value used by the application
1580  * @user_data: user data for @mapping
1581  *
1582  * Gets the value that is stored at @key in @settings, subject to
1583  * application-level validation/mapping.
1584  *
1585  * You should use this function when the application needs to perform
1586  * some processing on the value of the key (for example, parsing).  The
1587  * @mapping function performs that processing.  If the function
1588  * indicates that the processing was unsuccessful (due to a parse error,
1589  * for example) then the mapping is tried again with another value.
1590  *
1591  * This allows a robust 'fall back to defaults' behaviour to be
1592  * implemented somewhat automatically.
1593  *
1594  * The first value that is tried is the user's setting for the key.  If
1595  * the mapping function fails to map this value, other values may be
1596  * tried in an unspecified order (system or site defaults, translated
1597  * schema default values, untranslated schema default values, etc).
1598  *
1599  * If the mapping function fails for all possible values, one additional
1600  * attempt is made: the mapping function is called with a %NULL value.
1601  * If the mapping function still indicates failure at this point then
1602  * the application will be aborted.
1603  *
1604  * The result parameter for the @mapping function is pointed to a
1605  * #gpointer which is initially set to %NULL.  The same pointer is given
1606  * to each invocation of @mapping.  The final value of that #gpointer is
1607  * what is returned by this function.  %NULL is valid; it is returned
1608  * just as any other value would be.
1609  *
1610  * Returns: (transfer full): the result, which may be %NULL
1611  **/
1612 gpointer
1613 g_settings_get_mapped (GSettings           *settings,
1614                        const gchar         *key,
1615                        GSettingsGetMapping  mapping,
1616                        gpointer             user_data)
1617 {
1618   gpointer result = NULL;
1619   GSettingsSchemaKey skey;
1620   GVariant *value;
1621   gboolean okay;
1622
1623   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1624   g_return_val_if_fail (key != NULL, NULL);
1625   g_return_val_if_fail (mapping != NULL, NULL);
1626
1627   g_settings_schema_key_init (&skey, settings->priv->schema, key);
1628
1629   if ((value = g_settings_read_from_backend (settings, &skey, FALSE, FALSE)))
1630     {
1631       okay = mapping (value, &result, user_data);
1632       g_variant_unref (value);
1633       if (okay) goto okay;
1634     }
1635
1636   if ((value = g_settings_schema_key_get_translated_default (&skey)))
1637     {
1638       okay = mapping (value, &result, user_data);
1639       g_variant_unref (value);
1640       if (okay) goto okay;
1641     }
1642
1643   if (mapping (skey.default_value, &result, user_data))
1644     goto okay;
1645
1646   if (!mapping (NULL, &result, user_data))
1647     g_error ("The mapping function given to g_settings_get_mapped() for key "
1648              "'%s' in schema '%s' returned FALSE when given a NULL value.",
1649              key, g_settings_schema_get_id (settings->priv->schema));
1650
1651  okay:
1652   g_settings_schema_key_clear (&skey);
1653
1654   return result;
1655 }
1656
1657 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1658 /**
1659  * g_settings_get_string:
1660  * @settings: a #GSettings object
1661  * @key: the key to get the value for
1662  *
1663  * Gets the value that is stored at @key in @settings.
1664  *
1665  * A convenience variant of g_settings_get() for strings.
1666  *
1667  * It is a programmer error to give a @key that isn't specified as
1668  * having a string type in the schema for @settings.
1669  *
1670  * Returns: a newly-allocated string
1671  *
1672  * Since: 2.26
1673  */
1674 gchar *
1675 g_settings_get_string (GSettings   *settings,
1676                        const gchar *key)
1677 {
1678   GVariant *value;
1679   gchar *result;
1680
1681   value = g_settings_get_value (settings, key);
1682   result = g_variant_dup_string (value, NULL);
1683   g_variant_unref (value);
1684
1685   return result;
1686 }
1687
1688 /**
1689  * g_settings_set_string:
1690  * @settings: a #GSettings object
1691  * @key: the name of the key to set
1692  * @value: the value to set it to
1693  *
1694  * Sets @key in @settings to @value.
1695  *
1696  * A convenience variant of g_settings_set() for strings.
1697  *
1698  * It is a programmer error to give a @key that isn't specified as
1699  * having a string type in the schema for @settings.
1700  *
1701  * Returns: %TRUE if setting the key succeeded,
1702  *     %FALSE if the key was not writable
1703  *
1704  * Since: 2.26
1705  */
1706 gboolean
1707 g_settings_set_string (GSettings   *settings,
1708                        const gchar *key,
1709                        const gchar *value)
1710 {
1711   return g_settings_set_value (settings, key, g_variant_new_string (value));
1712 }
1713
1714 /**
1715  * g_settings_get_int:
1716  * @settings: a #GSettings object
1717  * @key: the key to get the value for
1718  *
1719  * Gets the value that is stored at @key in @settings.
1720  *
1721  * A convenience variant of g_settings_get() for 32-bit integers.
1722  *
1723  * It is a programmer error to give a @key that isn't specified as
1724  * having a int32 type in the schema for @settings.
1725  *
1726  * Returns: an integer
1727  *
1728  * Since: 2.26
1729  */
1730 gint
1731 g_settings_get_int (GSettings   *settings,
1732                     const gchar *key)
1733 {
1734   GVariant *value;
1735   gint result;
1736
1737   value = g_settings_get_value (settings, key);
1738   result = g_variant_get_int32 (value);
1739   g_variant_unref (value);
1740
1741   return result;
1742 }
1743
1744 /**
1745  * g_settings_set_int:
1746  * @settings: a #GSettings object
1747  * @key: the name of the key to set
1748  * @value: the value to set it to
1749  *
1750  * Sets @key in @settings to @value.
1751  *
1752  * A convenience variant of g_settings_set() for 32-bit integers.
1753  *
1754  * It is a programmer error to give a @key that isn't specified as
1755  * having a int32 type in the schema for @settings.
1756  *
1757  * Returns: %TRUE if setting the key succeeded,
1758  *     %FALSE if the key was not writable
1759  *
1760  * Since: 2.26
1761  */
1762 gboolean
1763 g_settings_set_int (GSettings   *settings,
1764                     const gchar *key,
1765                     gint         value)
1766 {
1767   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1768 }
1769
1770 /**
1771  * g_settings_get_uint:
1772  * @settings: a #GSettings object
1773  * @key: the key to get the value for
1774  *
1775  * Gets the value that is stored at @key in @settings.
1776  *
1777  * A convenience variant of g_settings_get() for 32-bit unsigned
1778  * integers.
1779  *
1780  * It is a programmer error to give a @key that isn't specified as
1781  * having a uint32 type in the schema for @settings.
1782  *
1783  * Returns: an unsigned integer
1784  *
1785  * Since: 2.30
1786  */
1787 guint
1788 g_settings_get_uint (GSettings   *settings,
1789                      const gchar *key)
1790 {
1791   GVariant *value;
1792   guint result;
1793
1794   value = g_settings_get_value (settings, key);
1795   result = g_variant_get_uint32 (value);
1796   g_variant_unref (value);
1797
1798   return result;
1799 }
1800
1801 /**
1802  * g_settings_set_uint:
1803  * @settings: a #GSettings object
1804  * @key: the name of the key to set
1805  * @value: the value to set it to
1806  *
1807  * Sets @key in @settings to @value.
1808  *
1809  * A convenience variant of g_settings_set() for 32-bit unsigned
1810  * integers.
1811  *
1812  * It is a programmer error to give a @key that isn't specified as
1813  * having a uint32 type in the schema for @settings.
1814  *
1815  * Returns: %TRUE if setting the key succeeded,
1816  *     %FALSE if the key was not writable
1817  *
1818  * Since: 2.30
1819  */
1820 gboolean
1821 g_settings_set_uint (GSettings   *settings,
1822                      const gchar *key,
1823                      guint        value)
1824 {
1825   return g_settings_set_value (settings, key, g_variant_new_uint32 (value));
1826 }
1827
1828 /**
1829  * g_settings_get_double:
1830  * @settings: a #GSettings object
1831  * @key: the key to get the value for
1832  *
1833  * Gets the value that is stored at @key in @settings.
1834  *
1835  * A convenience variant of g_settings_get() for doubles.
1836  *
1837  * It is a programmer error to give a @key that isn't specified as
1838  * having a 'double' type in the schema for @settings.
1839  *
1840  * Returns: a double
1841  *
1842  * Since: 2.26
1843  */
1844 gdouble
1845 g_settings_get_double (GSettings   *settings,
1846                        const gchar *key)
1847 {
1848   GVariant *value;
1849   gdouble result;
1850
1851   value = g_settings_get_value (settings, key);
1852   result = g_variant_get_double (value);
1853   g_variant_unref (value);
1854
1855   return result;
1856 }
1857
1858 /**
1859  * g_settings_set_double:
1860  * @settings: a #GSettings object
1861  * @key: the name of the key to set
1862  * @value: the value to set it to
1863  *
1864  * Sets @key in @settings to @value.
1865  *
1866  * A convenience variant of g_settings_set() for doubles.
1867  *
1868  * It is a programmer error to give a @key that isn't specified as
1869  * having a 'double' type in the schema for @settings.
1870  *
1871  * Returns: %TRUE if setting the key succeeded,
1872  *     %FALSE if the key was not writable
1873  *
1874  * Since: 2.26
1875  */
1876 gboolean
1877 g_settings_set_double (GSettings   *settings,
1878                        const gchar *key,
1879                        gdouble      value)
1880 {
1881   return g_settings_set_value (settings, key, g_variant_new_double (value));
1882 }
1883
1884 /**
1885  * g_settings_get_boolean:
1886  * @settings: a #GSettings object
1887  * @key: the key to get the value for
1888  *
1889  * Gets the value that is stored at @key in @settings.
1890  *
1891  * A convenience variant of g_settings_get() for booleans.
1892  *
1893  * It is a programmer error to give a @key that isn't specified as
1894  * having a boolean type in the schema for @settings.
1895  *
1896  * Returns: a boolean
1897  *
1898  * Since: 2.26
1899  */
1900 gboolean
1901 g_settings_get_boolean (GSettings  *settings,
1902                        const gchar *key)
1903 {
1904   GVariant *value;
1905   gboolean result;
1906
1907   value = g_settings_get_value (settings, key);
1908   result = g_variant_get_boolean (value);
1909   g_variant_unref (value);
1910
1911   return result;
1912 }
1913
1914 /**
1915  * g_settings_set_boolean:
1916  * @settings: a #GSettings object
1917  * @key: the name of the key to set
1918  * @value: the value to set it to
1919  *
1920  * Sets @key in @settings to @value.
1921  *
1922  * A convenience variant of g_settings_set() for booleans.
1923  *
1924  * It is a programmer error to give a @key that isn't specified as
1925  * having a boolean type in the schema for @settings.
1926  *
1927  * Returns: %TRUE if setting the key succeeded,
1928  *     %FALSE if the key was not writable
1929  *
1930  * Since: 2.26
1931  */
1932 gboolean
1933 g_settings_set_boolean (GSettings  *settings,
1934                        const gchar *key,
1935                        gboolean     value)
1936 {
1937   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1938 }
1939
1940 /**
1941  * g_settings_get_strv:
1942  * @settings: a #GSettings object
1943  * @key: the key to get the value for
1944  *
1945  * A convenience variant of g_settings_get() for string arrays.
1946  *
1947  * It is a programmer error to give a @key that isn't specified as
1948  * having an array of strings type in the schema for @settings.
1949  *
1950  * Returns: (array zero-terminated=1) (transfer full): a
1951  * newly-allocated, %NULL-terminated array of strings, the value that
1952  * is stored at @key in @settings.
1953  *
1954  * Since: 2.26
1955  */
1956 gchar **
1957 g_settings_get_strv (GSettings   *settings,
1958                      const gchar *key)
1959 {
1960   GVariant *value;
1961   gchar **result;
1962
1963   value = g_settings_get_value (settings, key);
1964   result = g_variant_dup_strv (value, NULL);
1965   g_variant_unref (value);
1966
1967   return result;
1968 }
1969
1970 /**
1971  * g_settings_set_strv:
1972  * @settings: a #GSettings object
1973  * @key: the name of the key to set
1974  * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1975  *
1976  * Sets @key in @settings to @value.
1977  *
1978  * A convenience variant of g_settings_set() for string arrays.  If
1979  * @value is %NULL, then @key is set to be the empty array.
1980  *
1981  * It is a programmer error to give a @key that isn't specified as
1982  * having an array of strings type in the schema for @settings.
1983  *
1984  * Returns: %TRUE if setting the key succeeded,
1985  *     %FALSE if the key was not writable
1986  *
1987  * Since: 2.26
1988  */
1989 gboolean
1990 g_settings_set_strv (GSettings           *settings,
1991                      const gchar         *key,
1992                      const gchar * const *value)
1993 {
1994   GVariant *array;
1995
1996   if (value != NULL)
1997     array = g_variant_new_strv (value, -1);
1998   else
1999     array = g_variant_new_strv (NULL, 0);
2000
2001   return g_settings_set_value (settings, key, array);
2002 }
2003
2004 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
2005 /**
2006  * g_settings_delay:
2007  * @settings: a #GSettings object
2008  *
2009  * Changes the #GSettings object into 'delay-apply' mode. In this
2010  * mode, changes to @settings are not immediately propagated to the
2011  * backend, but kept locally until g_settings_apply() is called.
2012  *
2013  * Since: 2.26
2014  */
2015 void
2016 g_settings_delay (GSettings *settings)
2017 {
2018   g_return_if_fail (G_IS_SETTINGS (settings));
2019
2020   if (settings->priv->delayed)
2021     return;
2022
2023   settings->priv->delayed =
2024     g_delayed_settings_backend_new (settings->priv->backend,
2025                                     settings,
2026                                     settings->priv->main_context);
2027   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
2028   g_object_unref (settings->priv->backend);
2029
2030   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
2031   g_settings_backend_watch (settings->priv->backend,
2032                             &listener_vtable, G_OBJECT (settings),
2033                             settings->priv->main_context);
2034
2035   g_object_notify (G_OBJECT (settings), "delay-apply");
2036 }
2037
2038 /**
2039  * g_settings_apply:
2040  * @settings: a #GSettings instance
2041  *
2042  * Applies any changes that have been made to the settings.  This
2043  * function does nothing unless @settings is in 'delay-apply' mode;
2044  * see g_settings_delay().  In the normal case settings are always
2045  * applied immediately.
2046  **/
2047 void
2048 g_settings_apply (GSettings *settings)
2049 {
2050   if (settings->priv->delayed)
2051     {
2052       GDelayedSettingsBackend *delayed;
2053
2054       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2055       g_delayed_settings_backend_apply (delayed);
2056     }
2057 }
2058
2059 /**
2060  * g_settings_revert:
2061  * @settings: a #GSettings instance
2062  *
2063  * Reverts all non-applied changes to the settings.  This function
2064  * does nothing unless @settings is in 'delay-apply' mode; see
2065  * g_settings_delay().  In the normal case settings are always applied
2066  * immediately.
2067  *
2068  * Change notifications will be emitted for affected keys.
2069  **/
2070 void
2071 g_settings_revert (GSettings *settings)
2072 {
2073   if (settings->priv->delayed)
2074     {
2075       GDelayedSettingsBackend *delayed;
2076
2077       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2078       g_delayed_settings_backend_revert (delayed);
2079     }
2080 }
2081
2082 /**
2083  * g_settings_get_has_unapplied:
2084  * @settings: a #GSettings object
2085  *
2086  * Returns whether the #GSettings object has any unapplied
2087  * changes.  This can only be the case if it is in 'delayed-apply' mode.
2088  *
2089  * Returns: %TRUE if @settings has unapplied changes
2090  *
2091  * Since: 2.26
2092  */
2093 gboolean
2094 g_settings_get_has_unapplied (GSettings *settings)
2095 {
2096   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2097
2098   return settings->priv->delayed &&
2099          g_delayed_settings_backend_get_has_unapplied (
2100            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2101 }
2102
2103 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2104 /**
2105  * g_settings_reset:
2106  * @settings: a #GSettings object
2107  * @key: the name of a key
2108  *
2109  * Resets @key to its default value.
2110  *
2111  * This call resets the key, as much as possible, to its default value.
2112  * That might the value specified in the schema or the one set by the
2113  * administrator.
2114  **/
2115 void
2116 g_settings_reset (GSettings *settings,
2117                   const gchar *key)
2118 {
2119   gchar *path;
2120
2121   path = g_strconcat (settings->priv->path, key, NULL);
2122   g_settings_backend_reset (settings->priv->backend, path, NULL);
2123   g_free (path);
2124 }
2125
2126 /**
2127  * g_settings_sync:
2128  *
2129  * Ensures that all pending operations for the given are complete for
2130  * the default backend.
2131  *
2132  * Writes made to a #GSettings are handled asynchronously.  For this
2133  * reason, it is very unlikely that the changes have it to disk by the
2134  * time g_settings_set() returns.
2135  *
2136  * This call will block until all of the writes have made it to the
2137  * backend.  Since the mainloop is not running, no change notifications
2138  * will be dispatched during this call (but some may be queued by the
2139  * time the call is done).
2140  **/
2141 void
2142 g_settings_sync (void)
2143 {
2144   g_settings_backend_sync_default ();
2145 }
2146
2147 /**
2148  * g_settings_is_writable:
2149  * @settings: a #GSettings object
2150  * @name: the name of a key
2151  *
2152  * Finds out if a key can be written or not
2153  *
2154  * Returns: %TRUE if the key @name is writable
2155  *
2156  * Since: 2.26
2157  */
2158 gboolean
2159 g_settings_is_writable (GSettings   *settings,
2160                         const gchar *name)
2161 {
2162   gboolean writable;
2163   gchar *path;
2164
2165   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2166
2167   path = g_strconcat (settings->priv->path, name, NULL);
2168   writable = g_settings_backend_get_writable (settings->priv->backend, path);
2169   g_free (path);
2170
2171   return writable;
2172 }
2173
2174 /**
2175  * g_settings_get_child:
2176  * @settings: a #GSettings object
2177  * @name: the name of the 'child' schema
2178  *
2179  * Creates a 'child' settings object which has a base path of
2180  * <replaceable>base-path</replaceable>/@name, where
2181  * <replaceable>base-path</replaceable> is the base path of @settings.
2182  *
2183  * The schema for the child settings object must have been declared
2184  * in the schema of @settings using a <tag class="starttag">child</tag> element.
2185  *
2186  * Returns: (transfer full): a 'child' settings object
2187  *
2188  * Since: 2.26
2189  */
2190 GSettings *
2191 g_settings_get_child (GSettings   *settings,
2192                       const gchar *name)
2193 {
2194   const gchar *child_schema;
2195   gchar *child_path;
2196   gchar *child_name;
2197   GSettings *child;
2198
2199   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2200
2201   child_name = g_strconcat (name, "/", NULL);
2202   child_schema = g_settings_schema_get_string (settings->priv->schema,
2203                                                child_name);
2204   if (child_schema == NULL)
2205     g_error ("Schema '%s' has no child '%s'",
2206              g_settings_schema_get_id (settings->priv->schema), name);
2207
2208   child_path = g_strconcat (settings->priv->path, child_name, NULL);
2209   child = g_object_new (G_TYPE_SETTINGS,
2210                         "backend", settings->priv->backend,
2211                         "schema-id", child_schema,
2212                         "path", child_path,
2213                         NULL);
2214   g_free (child_path);
2215   g_free (child_name);
2216
2217   return child;
2218 }
2219
2220 /**
2221  * g_settings_list_keys:
2222  * @settings: a #GSettings object
2223  *
2224  * Introspects the list of keys on @settings.
2225  *
2226  * You should probably not be calling this function from "normal" code
2227  * (since you should already know what keys are in your schema).  This
2228  * function is intended for introspection reasons.
2229  *
2230  * You should free the return value with g_strfreev() when you are done
2231  * with it.
2232  *
2233  * Returns: (transfer full) (element-type utf8): a list of the keys on @settings
2234  */
2235 gchar **
2236 g_settings_list_keys (GSettings *settings)
2237 {
2238   const GQuark *keys;
2239   gchar **strv;
2240   gint n_keys;
2241   gint i, j;
2242
2243   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2244   strv = g_new (gchar *, n_keys + 1);
2245   for (i = j = 0; i < n_keys; i++)
2246     {
2247       const gchar *key = g_quark_to_string (keys[i]);
2248
2249       if (!g_str_has_suffix (key, "/"))
2250         strv[j++] = g_strdup (key);
2251     }
2252   strv[j] = NULL;
2253
2254   return strv;
2255 }
2256
2257 /**
2258  * g_settings_list_children:
2259  * @settings: a #GSettings object
2260  *
2261  * Gets the list of children on @settings.
2262  *
2263  * The list is exactly the list of strings for which it is not an error
2264  * to call g_settings_get_child().
2265  *
2266  * For GSettings objects that are lists, this value can change at any
2267  * time and you should connect to the "children-changed" signal to watch
2268  * for those changes.  Note that there is a race condition here: you may
2269  * request a child after listing it only for it to have been destroyed
2270  * in the meantime.  For this reason, g_settings_get_child() may return
2271  * %NULL even for a child that was listed by this function.
2272  *
2273  * For GSettings objects that are not lists, you should probably not be
2274  * calling this function from "normal" code (since you should already
2275  * know what children are in your schema).  This function may still be
2276  * useful there for introspection reasons, however.
2277  *
2278  * You should free the return value with g_strfreev() when you are done
2279  * with it.
2280  *
2281  * Returns: (transfer full) (element-type utf8): a list of the children on @settings
2282  */
2283 gchar **
2284 g_settings_list_children (GSettings *settings)
2285 {
2286   const GQuark *keys;
2287   gchar **strv;
2288   gint n_keys;
2289   gint i, j;
2290
2291   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2292   strv = g_new (gchar *, n_keys + 1);
2293   for (i = j = 0; i < n_keys; i++)
2294     {
2295       const gchar *key = g_quark_to_string (keys[i]);
2296
2297       if (g_str_has_suffix (key, "/"))
2298         {
2299           gint length = strlen (key);
2300
2301           strv[j] = g_memdup (key, length);
2302           strv[j][length - 1] = '\0';
2303           j++;
2304         }
2305     }
2306   strv[j] = NULL;
2307
2308   return strv;
2309 }
2310
2311 /**
2312  * g_settings_get_range:
2313  * @settings: a #GSettings
2314  * @key: the key to query the range of
2315  *
2316  * Queries the range of a key.
2317  *
2318  * Since: 2.28
2319  *
2320  * Deprecated:2.40:Use g_settings_schema_key_get_range() instead.
2321  **/
2322 GVariant *
2323 g_settings_get_range (GSettings   *settings,
2324                       const gchar *key)
2325 {
2326   GSettingsSchemaKey skey;
2327   GVariant *range;
2328
2329   g_settings_schema_key_init (&skey, settings->priv->schema, key);
2330   range = g_settings_schema_key_get_range (&skey);
2331   g_settings_schema_key_clear (&skey);
2332
2333   return range;
2334 }
2335
2336 /**
2337  * g_settings_range_check:
2338  * @settings: a #GSettings
2339  * @key: the key to check
2340  * @value: the value to check
2341  *
2342  * Checks if the given @value is of the correct type and within the
2343  * permitted range for @key.
2344  *
2345  * Returns: %TRUE if @value is valid for @key
2346  *
2347  * Since: 2.28
2348  *
2349  * Deprecated:2.40:Use g_settings_schema_key_range_check() instead.
2350  **/
2351 gboolean
2352 g_settings_range_check (GSettings   *settings,
2353                         const gchar *key,
2354                         GVariant    *value)
2355 {
2356   GSettingsSchemaKey skey;
2357   gboolean good;
2358
2359   g_settings_schema_key_init (&skey, settings->priv->schema, key);
2360   good = g_settings_schema_key_range_check (&skey, value);
2361   g_settings_schema_key_clear (&skey);
2362
2363   return good;
2364 }
2365
2366 /* Binding {{{1 */
2367 typedef struct
2368 {
2369   GSettingsSchemaKey key;
2370   GSettings *settings;
2371   GObject *object;
2372
2373   GSettingsBindGetMapping get_mapping;
2374   GSettingsBindSetMapping set_mapping;
2375   gpointer user_data;
2376   GDestroyNotify destroy;
2377
2378   guint writable_handler_id;
2379   guint property_handler_id;
2380   const GParamSpec *property;
2381   guint key_handler_id;
2382
2383   /* prevent recursion */
2384   gboolean running;
2385 } GSettingsBinding;
2386
2387 static void
2388 g_settings_binding_free (gpointer data)
2389 {
2390   GSettingsBinding *binding = data;
2391
2392   g_assert (!binding->running);
2393
2394   if (binding->writable_handler_id)
2395     g_signal_handler_disconnect (binding->settings,
2396                                  binding->writable_handler_id);
2397
2398   if (binding->key_handler_id)
2399     g_signal_handler_disconnect (binding->settings,
2400                                  binding->key_handler_id);
2401
2402   if (g_signal_handler_is_connected (binding->object,
2403                                      binding->property_handler_id))
2404   g_signal_handler_disconnect (binding->object,
2405                                binding->property_handler_id);
2406
2407   g_settings_schema_key_clear (&binding->key);
2408
2409   if (binding->destroy)
2410     binding->destroy (binding->user_data);
2411
2412   g_object_unref (binding->settings);
2413
2414   g_slice_free (GSettingsBinding, binding);
2415 }
2416
2417 static GQuark
2418 g_settings_binding_quark (const char *property)
2419 {
2420   GQuark quark;
2421   gchar *tmp;
2422
2423   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2424   quark = g_quark_from_string (tmp);
2425   g_free (tmp);
2426
2427   return quark;
2428 }
2429
2430 static void
2431 g_settings_binding_key_changed (GSettings   *settings,
2432                                 const gchar *key,
2433                                 gpointer     user_data)
2434 {
2435   GSettingsBinding *binding = user_data;
2436   GValue value = G_VALUE_INIT;
2437   GVariant *variant;
2438
2439   g_assert (settings == binding->settings);
2440   g_assert (key == binding->key.name);
2441
2442   if (binding->running)
2443     return;
2444
2445   binding->running = TRUE;
2446
2447   g_value_init (&value, binding->property->value_type);
2448
2449   variant = g_settings_read_from_backend (binding->settings, &binding->key, FALSE, FALSE);
2450   if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2451     {
2452       /* silently ignore errors in the user's config database */
2453       g_variant_unref (variant);
2454       variant = NULL;
2455     }
2456
2457   if (variant == NULL)
2458     {
2459       variant = g_settings_schema_key_get_translated_default (&binding->key);
2460       if (variant &&
2461           !binding->get_mapping (&value, variant, binding->user_data))
2462         {
2463           /* flag translation errors with a warning */
2464           g_warning ("Translated default '%s' for key '%s' in schema '%s' "
2465                      "was rejected by the binding mapping function",
2466                      binding->key.unparsed, binding->key.name,
2467                      g_settings_schema_get_id (binding->key.schema));
2468           g_variant_unref (variant);
2469           variant = NULL;
2470         }
2471     }
2472
2473   if (variant == NULL)
2474     {
2475       variant = g_variant_ref (binding->key.default_value);
2476       if (!binding->get_mapping (&value, variant, binding->user_data))
2477         g_error ("The schema default value for key '%s' in schema '%s' "
2478                  "was rejected by the binding mapping function.",
2479                  binding->key.name, g_settings_schema_get_id (binding->key.schema));
2480     }
2481
2482   g_object_set_property (binding->object, binding->property->name, &value);
2483   g_variant_unref (variant);
2484   g_value_unset (&value);
2485
2486   binding->running = FALSE;
2487 }
2488
2489 static void
2490 g_settings_binding_property_changed (GObject          *object,
2491                                      const GParamSpec *pspec,
2492                                      gpointer          user_data)
2493 {
2494   GSettingsBinding *binding = user_data;
2495   GValue value = G_VALUE_INIT;
2496   GVariant *variant;
2497
2498   g_assert (object == binding->object);
2499   g_assert (pspec == binding->property);
2500
2501   if (binding->running)
2502     return;
2503
2504   binding->running = TRUE;
2505
2506   g_value_init (&value, pspec->value_type);
2507   g_object_get_property (object, pspec->name, &value);
2508   if ((variant = binding->set_mapping (&value, binding->key.type,
2509                                        binding->user_data)))
2510     {
2511       g_variant_take_ref (variant);
2512
2513       if (!g_settings_schema_key_type_check (&binding->key, variant))
2514         {
2515           g_critical ("binding mapping function for key '%s' returned "
2516                       "GVariant of type '%s' when type '%s' was requested",
2517                       binding->key.name, g_variant_get_type_string (variant),
2518                       g_variant_type_dup_string (binding->key.type));
2519           return;
2520         }
2521
2522       if (!g_settings_schema_key_range_check (&binding->key, variant))
2523         {
2524           g_critical ("GObject property '%s' on a '%s' object is out of "
2525                       "schema-specified range for key '%s' of '%s': %s",
2526                       binding->property->name, g_type_name (binding->property->owner_type),
2527                       binding->key.name, g_settings_schema_get_id (binding->key.schema),
2528                       g_variant_print (variant, TRUE));
2529           return;
2530         }
2531
2532       g_settings_write_to_backend (binding->settings, &binding->key, variant);
2533       g_variant_unref (variant);
2534     }
2535   g_value_unset (&value);
2536
2537   binding->running = FALSE;
2538 }
2539
2540 static gboolean
2541 g_settings_bind_invert_boolean_get_mapping (GValue   *value,
2542                                             GVariant *variant,
2543                                             gpointer  user_data)
2544 {
2545   g_value_set_boolean (value, !g_variant_get_boolean (variant));
2546   return TRUE;
2547 }
2548
2549 static GVariant *
2550 g_settings_bind_invert_boolean_set_mapping (const GValue       *value,
2551                                             const GVariantType *expected_type,
2552                                             gpointer            user_data)
2553 {
2554   return g_variant_new_boolean (!g_value_get_boolean (value));
2555 }
2556
2557 /**
2558  * g_settings_bind:
2559  * @settings: a #GSettings object
2560  * @key: the key to bind
2561  * @object: (type GObject.Object): a #GObject
2562  * @property: the name of the property to bind
2563  * @flags: flags for the binding
2564  *
2565  * Create a binding between the @key in the @settings object
2566  * and the property @property of @object.
2567  *
2568  * The binding uses the default GIO mapping functions to map
2569  * between the settings and property values. These functions
2570  * handle booleans, numeric types and string types in a
2571  * straightforward way. Use g_settings_bind_with_mapping() if
2572  * you need a custom mapping, or map between types that are not
2573  * supported by the default mapping functions.
2574  *
2575  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2576  * function also establishes a binding between the writability of
2577  * @key and the "sensitive" property of @object (if @object has
2578  * a boolean property by that name). See g_settings_bind_writable()
2579  * for more details about writable bindings.
2580  *
2581  * Note that the lifecycle of the binding is tied to the object,
2582  * and that you can have only one binding per object property.
2583  * If you bind the same property twice on the same object, the second
2584  * binding overrides the first one.
2585  *
2586  * Since: 2.26
2587  */
2588 void
2589 g_settings_bind (GSettings          *settings,
2590                  const gchar        *key,
2591                  gpointer            object,
2592                  const gchar        *property,
2593                  GSettingsBindFlags  flags)
2594 {
2595   GSettingsBindGetMapping get_mapping = NULL;
2596   GSettingsBindSetMapping set_mapping = NULL;
2597
2598   if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2599     {
2600       get_mapping = g_settings_bind_invert_boolean_get_mapping;
2601       set_mapping = g_settings_bind_invert_boolean_set_mapping;
2602
2603       /* can't pass this flag to g_settings_bind_with_mapping() */
2604       flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2605     }
2606
2607   g_settings_bind_with_mapping (settings, key, object, property, flags,
2608                                 get_mapping, set_mapping, NULL, NULL);
2609 }
2610
2611 /**
2612  * g_settings_bind_with_mapping: (skip)
2613  * @settings: a #GSettings object
2614  * @key: the key to bind
2615  * @object: (type GObject.Object): a #GObject
2616  * @property: the name of the property to bind
2617  * @flags: flags for the binding
2618  * @get_mapping: a function that gets called to convert values
2619  *     from @settings to @object, or %NULL to use the default GIO mapping
2620  * @set_mapping: a function that gets called to convert values
2621  *     from @object to @settings, or %NULL to use the default GIO mapping
2622  * @user_data: data that gets passed to @get_mapping and @set_mapping
2623  * @destroy: #GDestroyNotify function for @user_data
2624  *
2625  * Create a binding between the @key in the @settings object
2626  * and the property @property of @object.
2627  *
2628  * The binding uses the provided mapping functions to map between
2629  * settings and property values.
2630  *
2631  * Note that the lifecycle of the binding is tied to the object,
2632  * and that you can have only one binding per object property.
2633  * If you bind the same property twice on the same object, the second
2634  * binding overrides the first one.
2635  *
2636  * Since: 2.26
2637  */
2638 void
2639 g_settings_bind_with_mapping (GSettings               *settings,
2640                               const gchar             *key,
2641                               gpointer                 object,
2642                               const gchar             *property,
2643                               GSettingsBindFlags       flags,
2644                               GSettingsBindGetMapping  get_mapping,
2645                               GSettingsBindSetMapping  set_mapping,
2646                               gpointer                 user_data,
2647                               GDestroyNotify           destroy)
2648 {
2649   GSettingsBinding *binding;
2650   GObjectClass *objectclass;
2651   gchar *detailed_signal;
2652   GQuark binding_quark;
2653
2654   g_return_if_fail (G_IS_SETTINGS (settings));
2655   g_return_if_fail (key != NULL);
2656   g_return_if_fail (G_IS_OBJECT (object));
2657   g_return_if_fail (property != NULL);
2658   g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2659
2660   objectclass = G_OBJECT_GET_CLASS (object);
2661
2662   binding = g_slice_new0 (GSettingsBinding);
2663   g_settings_schema_key_init (&binding->key, settings->priv->schema, key);
2664   binding->settings = g_object_ref (settings);
2665   binding->object = object;
2666   binding->property = g_object_class_find_property (objectclass, property);
2667   binding->user_data = user_data;
2668   binding->destroy = destroy;
2669   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2670   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2671
2672   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2673     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2674
2675   if (binding->property == NULL)
2676     {
2677       g_critical ("g_settings_bind: no property '%s' on class '%s'",
2678                   property, G_OBJECT_TYPE_NAME (object));
2679       return;
2680     }
2681
2682   if ((flags & G_SETTINGS_BIND_GET) &&
2683       (binding->property->flags & G_PARAM_WRITABLE) == 0)
2684     {
2685       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2686                   "writable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2687       return;
2688     }
2689   if ((flags & G_SETTINGS_BIND_SET) &&
2690       (binding->property->flags & G_PARAM_READABLE) == 0)
2691     {
2692       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2693                   "readable", binding->property->name, G_OBJECT_TYPE_NAME (object));
2694       return;
2695     }
2696
2697   if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2698     {
2699       /* g_settings_bind_invert_boolean_get_mapping() is a private
2700        * function, so if we are here it means that g_settings_bind() was
2701        * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2702        *
2703        * Ensure that both sides are boolean.
2704        */
2705
2706       if (binding->property->value_type != G_TYPE_BOOLEAN)
2707         {
2708           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2709                       "was specified, but property '%s' on type '%s' has "
2710                       "type '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2711                       g_type_name ((binding->property->value_type)));
2712           return;
2713         }
2714
2715       if (!g_variant_type_equal (binding->key.type, G_VARIANT_TYPE_BOOLEAN))
2716         {
2717           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2718                       "was specified, but key '%s' on schema '%s' has "
2719                       "type '%s'", key, g_settings_schema_get_id (settings->priv->schema),
2720                       g_variant_type_dup_string (binding->key.type));
2721           return;
2722         }
2723
2724     }
2725
2726   else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2727             (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2728            !g_settings_mapping_is_compatible (binding->property->value_type,
2729                                               binding->key.type))
2730     {
2731       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2732                   "'%s' which is not compatible with type '%s' of key '%s' "
2733                   "on schema '%s'", binding->property->name, G_OBJECT_TYPE_NAME (object),
2734                   g_type_name (binding->property->value_type),
2735                   g_variant_type_dup_string (binding->key.type), key,
2736                   g_settings_schema_get_id (settings->priv->schema));
2737       return;
2738     }
2739
2740   if ((flags & G_SETTINGS_BIND_SET) &&
2741       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2742     {
2743       GParamSpec *sensitive;
2744
2745       sensitive = g_object_class_find_property (objectclass, "sensitive");
2746
2747       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2748           (sensitive->flags & G_PARAM_WRITABLE))
2749         g_settings_bind_writable (settings, binding->key.name, object, "sensitive", FALSE);
2750     }
2751
2752   if (flags & G_SETTINGS_BIND_SET)
2753     {
2754       detailed_signal = g_strdup_printf ("notify::%s", binding->property->name);
2755       binding->property_handler_id =
2756         g_signal_connect (object, detailed_signal,
2757                           G_CALLBACK (g_settings_binding_property_changed),
2758                           binding);
2759       g_free (detailed_signal);
2760
2761       if (~flags & G_SETTINGS_BIND_GET)
2762         g_settings_binding_property_changed (object,
2763                                              binding->property,
2764                                              binding);
2765     }
2766
2767   if (flags & G_SETTINGS_BIND_GET)
2768     {
2769       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2770         {
2771           detailed_signal = g_strdup_printf ("changed::%s", key);
2772           binding->key_handler_id =
2773             g_signal_connect (settings, detailed_signal,
2774                               G_CALLBACK (g_settings_binding_key_changed),
2775                               binding);
2776           g_free (detailed_signal);
2777         }
2778
2779       g_settings_binding_key_changed (settings, binding->key.name, binding);
2780     }
2781
2782   binding_quark = g_settings_binding_quark (binding->property->name);
2783   g_object_set_qdata_full (object, binding_quark,
2784                            binding, g_settings_binding_free);
2785 }
2786
2787 /* Writability binding {{{1 */
2788 typedef struct
2789 {
2790   GSettings *settings;
2791   gpointer object;
2792   const gchar *key;
2793   const gchar *property;
2794   gboolean inverted;
2795   gulong handler_id;
2796 } GSettingsWritableBinding;
2797
2798 static void
2799 g_settings_writable_binding_free (gpointer data)
2800 {
2801   GSettingsWritableBinding *binding = data;
2802
2803   g_signal_handler_disconnect (binding->settings, binding->handler_id);
2804   g_object_unref (binding->settings);
2805   g_slice_free (GSettingsWritableBinding, binding);
2806 }
2807
2808 static void
2809 g_settings_binding_writable_changed (GSettings   *settings,
2810                                      const gchar *key,
2811                                      gpointer     user_data)
2812 {
2813   GSettingsWritableBinding *binding = user_data;
2814   gboolean writable;
2815
2816   g_assert (settings == binding->settings);
2817   g_assert (key == binding->key);
2818
2819   writable = g_settings_is_writable (settings, key);
2820
2821   if (binding->inverted)
2822     writable = !writable;
2823
2824   g_object_set (binding->object, binding->property, writable, NULL);
2825 }
2826
2827 /**
2828  * g_settings_bind_writable:
2829  * @settings: a #GSettings object
2830  * @key: the key to bind
2831  * @object: (type GObject.Object):a #GObject
2832  * @property: the name of a boolean property to bind
2833  * @inverted: whether to 'invert' the value
2834  *
2835  * Create a binding between the writability of @key in the
2836  * @settings object and the property @property of @object.
2837  * The property must be boolean; "sensitive" or "visible"
2838  * properties of widgets are the most likely candidates.
2839  *
2840  * Writable bindings are always uni-directional; changes of the
2841  * writability of the setting will be propagated to the object
2842  * property, not the other way.
2843  *
2844  * When the @inverted argument is %TRUE, the binding inverts the
2845  * value as it passes from the setting to the object, i.e. @property
2846  * will be set to %TRUE if the key is not writable.
2847  *
2848  * Note that the lifecycle of the binding is tied to the object,
2849  * and that you can have only one binding per object property.
2850  * If you bind the same property twice on the same object, the second
2851  * binding overrides the first one.
2852  *
2853  * Since: 2.26
2854  */
2855 void
2856 g_settings_bind_writable (GSettings   *settings,
2857                           const gchar *key,
2858                           gpointer     object,
2859                           const gchar *property,
2860                           gboolean     inverted)
2861 {
2862   GSettingsWritableBinding *binding;
2863   gchar *detailed_signal;
2864   GParamSpec *pspec;
2865
2866   g_return_if_fail (G_IS_SETTINGS (settings));
2867
2868   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2869   if (pspec == NULL)
2870     {
2871       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2872                   property, G_OBJECT_TYPE_NAME (object));
2873       return;
2874     }
2875   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2876     {
2877       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2878                   property, G_OBJECT_TYPE_NAME (object));
2879       return;
2880     }
2881
2882   binding = g_slice_new (GSettingsWritableBinding);
2883   binding->settings = g_object_ref (settings);
2884   binding->object = object;
2885   binding->key = g_intern_string (key);
2886   binding->property = g_intern_string (property);
2887   binding->inverted = inverted;
2888
2889   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2890   binding->handler_id =
2891     g_signal_connect (settings, detailed_signal,
2892                       G_CALLBACK (g_settings_binding_writable_changed),
2893                       binding);
2894   g_free (detailed_signal);
2895
2896   g_object_set_qdata_full (object, g_settings_binding_quark (property),
2897                            binding, g_settings_writable_binding_free);
2898
2899   g_settings_binding_writable_changed (settings, binding->key, binding);
2900 }
2901
2902 /**
2903  * g_settings_unbind:
2904  * @object: the object
2905  * @property: the property whose binding is removed
2906  *
2907  * Removes an existing binding for @property on @object.
2908  *
2909  * Note that bindings are automatically removed when the
2910  * object is finalized, so it is rarely necessary to call this
2911  * function.
2912  *
2913  * Since: 2.26
2914  */
2915 void
2916 g_settings_unbind (gpointer     object,
2917                    const gchar *property)
2918 {
2919   GQuark binding_quark;
2920
2921   binding_quark = g_settings_binding_quark (property);
2922   g_object_set_qdata (object, binding_quark, NULL);
2923 }
2924
2925 /* GAction {{{1 */
2926
2927 typedef struct
2928 {
2929   GObject parent_instance;
2930
2931   GSettingsSchemaKey key;
2932   GSettings *settings;
2933 } GSettingsAction;
2934
2935 typedef GObjectClass GSettingsActionClass;
2936
2937 static GType g_settings_action_get_type (void);
2938 static void g_settings_action_iface_init (GActionInterface *iface);
2939 G_DEFINE_TYPE_WITH_CODE (GSettingsAction, g_settings_action, G_TYPE_OBJECT,
2940                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_settings_action_iface_init))
2941
2942 enum
2943 {
2944   ACTION_PROP_0,
2945   ACTION_PROP_NAME,
2946   ACTION_PROP_PARAMETER_TYPE,
2947   ACTION_PROP_ENABLED,
2948   ACTION_PROP_STATE_TYPE,
2949   ACTION_PROP_STATE
2950 };
2951
2952 static const gchar *
2953 g_settings_action_get_name (GAction *action)
2954 {
2955   GSettingsAction *gsa = (GSettingsAction *) action;
2956
2957   return gsa->key.name;
2958 }
2959
2960 static const GVariantType *
2961 g_settings_action_get_parameter_type (GAction *action)
2962 {
2963   GSettingsAction *gsa = (GSettingsAction *) action;
2964   const GVariantType *type;
2965
2966   type = g_variant_get_type (gsa->key.default_value);
2967   if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
2968     type = NULL;
2969
2970   return type;
2971 }
2972
2973 static gboolean
2974 g_settings_action_get_enabled (GAction *action)
2975 {
2976   GSettingsAction *gsa = (GSettingsAction *) action;
2977
2978   return g_settings_is_writable (gsa->settings, gsa->key.name);
2979 }
2980
2981 static const GVariantType *
2982 g_settings_action_get_state_type (GAction *action)
2983 {
2984   GSettingsAction *gsa = (GSettingsAction *) action;
2985
2986   return g_variant_get_type (gsa->key.default_value);
2987 }
2988
2989 static GVariant *
2990 g_settings_action_get_state (GAction *action)
2991 {
2992   GSettingsAction *gsa = (GSettingsAction *) action;
2993   GVariant *value;
2994
2995   value = g_settings_read_from_backend (gsa->settings, &gsa->key, FALSE, FALSE);
2996
2997   if (value == NULL)
2998     value = g_settings_schema_key_get_translated_default (&gsa->key);
2999
3000   if (value == NULL)
3001     value = g_variant_ref (gsa->key.default_value);
3002
3003   return value;
3004 }
3005
3006 static GVariant *
3007 g_settings_action_get_state_hint (GAction *action)
3008 {
3009   GSettingsAction *gsa = (GSettingsAction *) action;
3010
3011   /* no point in reimplementing this... */
3012   return g_settings_schema_key_get_range (&gsa->key);
3013 }
3014
3015 static void
3016 g_settings_action_change_state (GAction  *action,
3017                                 GVariant *value)
3018 {
3019   GSettingsAction *gsa = (GSettingsAction *) action;
3020
3021   if (g_settings_schema_key_type_check (&gsa->key, value) && g_settings_schema_key_range_check (&gsa->key, value))
3022     g_settings_write_to_backend (gsa->settings, &gsa->key, value);
3023 }
3024
3025 static void
3026 g_settings_action_activate (GAction  *action,
3027                             GVariant *parameter)
3028 {
3029   GSettingsAction *gsa = (GSettingsAction *) action;
3030
3031   if (g_variant_is_of_type (gsa->key.default_value, G_VARIANT_TYPE_BOOLEAN))
3032     {
3033       GVariant *old;
3034
3035       if (parameter != NULL)
3036         return;
3037
3038       old = g_settings_action_get_state (action);
3039       parameter = g_variant_new_boolean (!g_variant_get_boolean (old));
3040       g_variant_unref (old);
3041     }
3042
3043   g_action_change_state (action, parameter);
3044 }
3045
3046 static void
3047 g_settings_action_get_property (GObject *object, guint prop_id,
3048                                 GValue *value, GParamSpec *pspec)
3049 {
3050   GAction *action = G_ACTION (object);
3051
3052   switch (prop_id)
3053     {
3054     case ACTION_PROP_NAME:
3055       g_value_set_string (value, g_settings_action_get_name (action));
3056       break;
3057
3058     case ACTION_PROP_PARAMETER_TYPE:
3059       g_value_set_boxed (value, g_settings_action_get_parameter_type (action));
3060       break;
3061
3062     case ACTION_PROP_ENABLED:
3063       g_value_set_boolean (value, g_settings_action_get_enabled (action));
3064       break;
3065
3066     case ACTION_PROP_STATE_TYPE:
3067       g_value_set_boxed (value, g_settings_action_get_state_type (action));
3068       break;
3069
3070     case ACTION_PROP_STATE:
3071       g_value_set_variant (value, g_settings_action_get_state (action));
3072       break;
3073
3074     default:
3075       g_assert_not_reached ();
3076     }
3077 }
3078
3079 static void
3080 g_settings_action_finalize (GObject *object)
3081 {
3082   GSettingsAction *gsa = (GSettingsAction *) object;
3083
3084   g_signal_handlers_disconnect_by_data (gsa->settings, gsa);
3085   g_object_unref (gsa->settings);
3086
3087   G_OBJECT_CLASS (g_settings_action_parent_class)
3088     ->finalize (object);
3089 }
3090
3091 static void
3092 g_settings_action_init (GSettingsAction *gsa)
3093 {
3094 }
3095
3096 static void
3097 g_settings_action_iface_init (GActionInterface *iface)
3098 {
3099   iface->get_name = g_settings_action_get_name;
3100   iface->get_parameter_type = g_settings_action_get_parameter_type;
3101   iface->get_enabled = g_settings_action_get_enabled;
3102   iface->get_state_type = g_settings_action_get_state_type;
3103   iface->get_state = g_settings_action_get_state;
3104   iface->get_state_hint = g_settings_action_get_state_hint;
3105   iface->change_state = g_settings_action_change_state;
3106   iface->activate = g_settings_action_activate;
3107 }
3108
3109 static void
3110 g_settings_action_class_init (GSettingsActionClass *class)
3111 {
3112   class->get_property = g_settings_action_get_property;
3113   class->finalize = g_settings_action_finalize;
3114
3115   g_object_class_override_property (class, ACTION_PROP_NAME, "name");
3116   g_object_class_override_property (class, ACTION_PROP_PARAMETER_TYPE, "parameter-type");
3117   g_object_class_override_property (class, ACTION_PROP_ENABLED, "enabled");
3118   g_object_class_override_property (class, ACTION_PROP_STATE_TYPE, "state-type");
3119   g_object_class_override_property (class, ACTION_PROP_STATE, "state");
3120 }
3121
3122 static void
3123 g_settings_action_changed (GSettings   *settings,
3124                            const gchar *key,
3125                            gpointer     user_data)
3126 {
3127   g_object_notify (user_data, "state");
3128 }
3129
3130 static void
3131 g_settings_action_enabled_changed (GSettings   *settings,
3132                                    const gchar *key,
3133                                    gpointer     user_data)
3134 {
3135   g_object_notify (user_data, "enabled");
3136 }
3137
3138 /**
3139  * g_settings_create_action:
3140  * @settings: a #GSettings
3141  * @key: the name of a key in @settings
3142  *
3143  * Creates a #GAction corresponding to a given #GSettings key.
3144  *
3145  * The action has the same name as the key.
3146  *
3147  * The value of the key becomes the state of the action and the action
3148  * is enabled when the key is writable.  Changing the state of the
3149  * action results in the key being written to.  Changes to the value or
3150  * writability of the key cause appropriate change notifications to be
3151  * emitted for the action.
3152  *
3153  * For boolean-valued keys, action activations take no parameter and
3154  * result in the toggling of the value.  For all other types,
3155  * activations take the new value for the key (which must have the
3156  * correct type).
3157  *
3158  * Returns: (transfer full): a new #GAction
3159  *
3160  * Since: 2.32
3161  **/
3162 GAction *
3163 g_settings_create_action (GSettings   *settings,
3164                           const gchar *key)
3165 {
3166   GSettingsAction *gsa;
3167   gchar *detailed_signal;
3168
3169   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
3170   g_return_val_if_fail (key != NULL, NULL);
3171
3172   gsa = g_object_new (g_settings_action_get_type (), NULL);
3173   gsa->settings = g_object_ref (settings);
3174   g_settings_schema_key_init (&gsa->key, settings->priv->schema, key);
3175
3176   detailed_signal = g_strdup_printf ("changed::%s", key);
3177   g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_changed), gsa);
3178   g_free (detailed_signal);
3179   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
3180   g_signal_connect (settings, detailed_signal, G_CALLBACK (g_settings_action_enabled_changed), gsa);
3181   g_free (detailed_signal);
3182
3183   return G_ACTION (gsa);
3184 }
3185
3186 /* Epilogue {{{1 */
3187
3188 /* vim:set foldmethod=marker: */