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