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