Implement bash completion for gsettings
[platform/upstream/glib.git] / gio / gsettings.c
index eda09e2..b517237 100644 (file)
@@ -236,7 +236,6 @@ struct _GSettingsPrivate
   GSettingsBackend *backend;
   GSettingsSchema *schema;
   gchar *schema_name;
-  gchar *context;
   gchar *path;
 
   GDelayedSettingsBackend *delayed;
@@ -245,9 +244,8 @@ struct _GSettingsPrivate
 enum
 {
   PROP_0,
-  PROP_BACKEND,
   PROP_SCHEMA,
-  PROP_CONTEXT,
+  PROP_BACKEND,
   PROP_PATH,
   PROP_HAS_UNAPPLIED,
 };
@@ -446,8 +444,8 @@ g_settings_set_property (GObject      *object,
       settings->priv->path = g_value_dup_string (value);
       break;
 
-    case PROP_CONTEXT:
-      settings->priv->context = g_value_dup_string (value);
+    case PROP_BACKEND:
+      settings->priv->backend = g_value_dup_object (value);
       break;
 
     default:
@@ -501,7 +499,9 @@ g_settings_constructed (GObject *object)
       settings->priv->path = g_strdup (schema_path);
     }
 
-  settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
+  if (settings->priv->backend == NULL)
+    settings->priv->backend = g_settings_backend_get_default ();
+
   g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
                             settings->priv->main_context,
                             settings_backend_changed,
@@ -524,7 +524,6 @@ g_settings_finalize (GObject *object)
   g_object_unref (settings->priv->backend);
   g_object_unref (settings->priv->schema);
   g_free (settings->priv->schema_name);
-  g_free (settings->priv->context);
   g_free (settings->priv->path);
 }
 
@@ -668,11 +667,11 @@ g_settings_class_init (GSettingsClass *class)
    *
    * The name of the context that the settings are stored in.
    */
-  g_object_class_install_property (object_class, PROP_CONTEXT,
-    g_param_spec_string ("context",
-                         P_("Context name"),
-                         P_("The name of the context for this settings object"),
-                         "", G_PARAM_CONSTRUCT_ONLY |
+  g_object_class_install_property (object_class, PROP_BACKEND,
+    g_param_spec_object ("backend",
+                         P_("GSettingsBackend"),
+                         P_("The GSettingsBackend for this settings object"),
+                         G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /**
@@ -773,66 +772,63 @@ g_settings_new_with_path (const gchar *schema,
 }
 
 /**
- * g_settings_new_with_context:
+ * g_settings_new_with_backend:
  * @schema: the name of the schema
- * @context: the context to use
+ * @backend: the #GSettingsBackend to use
  * @returns: a new #GSettings object
  *
- * Creates a new #GSettings object with a given schema and context.
+ * Creates a new #GSettings object with a given schema and backend.
  *
- * Creating settings objects with a context allow accessing settings
+ * Creating settings objects with an different backend allows accessing settings
  * from a database other than the usual one.  For example, it may make
- * sense to specify "defaults" in order to get a settings object that
- * modifies the system default settings instead of the settings for this
- * user.
- *
- * It is a programmer error to call this function for an unsupported
- * context.  Use g_settings_supports_context() to determine if a context
- * is supported if you are unsure.
+ * sense to pass a backend corresponding to the "defaults" settings database on
+ * the system to get a settings object that modifies the system default
+ * settings instead of the settings for this user.
  *
  * Since: 2.26
  */
 GSettings *
-g_settings_new_with_context (const gchar *schema,
-                             const gchar *context)
+g_settings_new_with_backend (const gchar      *schema,
+                             GSettingsBackend *backend)
 {
   g_return_val_if_fail (schema != NULL, NULL);
-  g_return_val_if_fail (context != NULL, NULL);
+  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
 
   return g_object_new (G_TYPE_SETTINGS,
                        "schema", schema,
-                       "context", context,
+                       "backend", backend,
                        NULL);
 }
 
 /**
- * g_settings_new_with_context_and_path:
+ * g_settings_new_with_backend_and_path:
  * @schema: the name of the schema
+ * @backend: the #GSettingsBackend to use
  * @path: the path to use
  * @returns: a new #GSettings object
  *
- * Creates a new #GSettings object with a given schema, context and
+ * Creates a new #GSettings object with a given schema, backend and
  * path.
  *
- * This is a mix of g_settings_new_with_context() and
+ * This is a mix of g_settings_new_with_backend() and
  * g_settings_new_with_path().
  *
  * Since: 2.26
  */
 GSettings *
-g_settings_new_with_context_and_path (const gchar *schema,
-                                      const gchar *context,
-                                      const gchar *path)
+g_settings_new_with_backend_and_path (const gchar      *schema,
+                                      GSettingsBackend *backend,
+                                      const gchar      *path)
 {
   g_return_val_if_fail (schema != NULL, NULL);
-  g_return_val_if_fail (context != NULL, NULL);
+  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
   g_return_val_if_fail (path != NULL, NULL);
 
   return g_object_new (G_TYPE_SETTINGS,
                        "schema", schema,
-                        "context", context,
-                        "path", path,
-                        NULL);
+                       "backend", backend,
+                       "path", path,
+                       NULL);
 }
 
 /* Internal read/write utilities, enum conversion, validation {{{1 */
@@ -870,9 +866,8 @@ g_settings_get_key_info (GSettingsKeyInfo *info,
 
   info->default_value = g_variant_iter_next_value (iter);
   info->type = g_variant_get_type (info->default_value);
-  info->schema = settings->priv->schema;
-  info->settings = settings;
-  info->key = key;
+  info->settings = g_object_ref (settings);
+  info->key = g_intern_string (key);
 
   while (g_variant_iter_next (iter, "(y*)", &code, &data))
     {
@@ -918,6 +913,7 @@ g_settings_free_key_info (GSettingsKeyInfo *info)
     g_variant_unref (info->maximum);
 
   g_variant_unref (info->default_value);
+  g_object_unref (info->settings);
 }
 
 static gboolean
@@ -1058,7 +1054,7 @@ g_settings_get_translated_default (GSettingsKeyInfo *info)
     /* translation not requested for this key */
     return NULL;
 
-  domain = g_settings_schema_get_gettext_domain (info->schema);
+  domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
 
   if (info->lc_char == 't')
     translated = g_dcgettext (domain, info->unparsed, LC_TIME);
@@ -1139,8 +1135,8 @@ g_settings_from_enum (GSettingsKeyInfo *info,
  *
  * Gets the value that is stored in @settings for @key.
  *
- * It is a programmer error to give a @key that isn't valid for
- * @settings.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings.
  *
  * Since: 2.26
  */
@@ -1180,8 +1176,8 @@ g_settings_get_value (GSettings   *settings,
  * In order to use this function the type of the value must be a string
  * and it must be marked in the schema file as an enumerated type.
  *
- * It is a programmer error to give a @key that isn't valid for
- * @settings, or is not marked as an enumerated type in the schema.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings or is not marked as an enumerated type.
  *
  * If the value stored in the configuration database is not a valid
  * value for the enumerated type then this function will return the
@@ -1230,14 +1226,14 @@ g_settings_get_enum (GSettings   *settings,
  * @settings: a #GSettings object
  * @key: a key, within @settings
  * @value: an enumerated value
- * Returns: %TRUE, if the set succeeds
+ * @returns: %TRUE, if the set succeeds
  *
  * Looks up the enumerated type nick for @value and writes it to @key,
  * within @settings.
  *
- * It is a programmer error for @key to not be listed in the schema or
- * for it not to be tagged as an enumerated type, or for @value not to
- * be a valid value for the named type.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings or is not marked as an enumerated type, or for
+ * @value not to be a valid value for the named type.
  *
  * After performing the write, accessing @key directly
  * g_settings_get_string() will return the 'nick' associated with
@@ -1290,9 +1286,9 @@ g_settings_set_enum (GSettings   *settings,
  *
  * Sets @key in @settings to @value.
  *
- * It is a programmer error to give a @key that isn't valid for
- * @settings.  It is a programmer error to give a @value of the
- * incorrect type.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings or for @value to have the incorrect type, per
+ * the schema.
  *
  * If @value is floating then this function consumes the reference.
  *
@@ -1328,9 +1324,9 @@ g_settings_set_value (GSettings   *settings,
  * A convenience function that combines g_settings_get_value() with
  * g_variant_get().
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or a @format_string that doesn't match the type of @key according
- * to the schema of @settings.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings or for the #GVariantType of @format to mismatch
+ * the type given in the schema.
  *
  * Since: 2.26
  */
@@ -1366,9 +1362,9 @@ g_settings_get (GSettings   *settings,
  * A convenience function that combines g_settings_set_value() with
  * g_variant_new().
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or a @format that doesn't match the type of @key according
- * to the schema of @settings.
+ * It is a programmer error to give a @key that isn't contained in the
+ * schema for @settings or for the #GVariantType of @format to mismatch
+ * the type given in the schema.
  *
  * Since: 2.26
  */
@@ -1399,8 +1395,8 @@ g_settings_set (GSettings   *settings,
  *
  * A convenience variant of g_settings_get() for strings.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type string.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a string type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1430,8 +1426,8 @@ g_settings_get_string (GSettings   *settings,
  *
  * A convenience variant of g_settings_set() for strings.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type string.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a string type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1453,8 +1449,8 @@ g_settings_set_string (GSettings   *settings,
  *
  * A convenience variant of g_settings_get() for 32-bit integers.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type int32.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a int32 type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1484,8 +1480,8 @@ g_settings_get_int (GSettings   *settings,
  *
  * A convenience variant of g_settings_set() for 32-bit integers.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type int32.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a int32 type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1507,8 +1503,8 @@ g_settings_set_int (GSettings   *settings,
  *
  * A convenience variant of g_settings_get() for doubles.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type double.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a 'double' type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1538,8 +1534,8 @@ g_settings_get_double (GSettings   *settings,
  *
  * A convenience variant of g_settings_set() for doubles.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type double.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a 'double' type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1561,8 +1557,8 @@ g_settings_set_double (GSettings   *settings,
  *
  * A convenience variant of g_settings_get() for booleans.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type boolean.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a boolean type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1592,8 +1588,8 @@ g_settings_get_boolean (GSettings  *settings,
  *
  * A convenience variant of g_settings_set() for booleans.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type boolean.
+ * It is a programmer error to give a @key that isn't specified as
+ * having a boolean type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1615,8 +1611,8 @@ g_settings_set_boolean (GSettings  *settings,
  *
  * A convenience variant of g_settings_get() for string arrays.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type 'string array'.
+ * It is a programmer error to give a @key that isn't specified as
+ * having an array of strings type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1647,8 +1643,8 @@ g_settings_get_strv (GSettings   *settings,
  * A convenience variant of g_settings_set() for string arrays.  If
  * @value is %NULL, then @key is set to be the empty array.
  *
- * It is a programmer error to pass a @key that isn't valid for
- * @settings or is not of type 'string array'.
+ * It is a programmer error to give a @key that isn't specified as
+ * having an array of strings type in the schema for @settings.
  *
  * Since: 2.26
  */
@@ -1666,6 +1662,7 @@ g_settings_set_strv (GSettings           *settings,
 
   return g_settings_set_value (settings, key, array);
 }
+
 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
 /**
  * g_settings_delay:
@@ -1766,7 +1763,7 @@ g_settings_get_has_unapplied (GSettings *settings)
            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
 }
 
-/* Extra API (sync, get_child, is_writable) {{{1 */
+/* Extra API (sync, get_child, is_writable, list_keys) {{{1 */
 /**
  * g_settings_sync:
  * @context: the context to sync, or %NULL
@@ -1784,15 +1781,9 @@ g_settings_get_has_unapplied (GSettings *settings)
  * time the call is done).
  **/
 void
-g_settings_sync (const gchar *context)
+g_settings_sync (void)
 {
-  GSettingsBackend *backend;
-
-  if (context == NULL)
-    context = "";
-
-  backend = g_settings_backend_get_with_context (context);
-  g_settings_backend_sync (backend);
+  g_settings_backend_sync_default ();
 }
 
 /**
@@ -1865,10 +1856,41 @@ g_settings_get_child (GSettings   *settings,
   return child;
 }
 
+/**
+ * g_settings_list_keys:
+ * @settings: a #GSettings object
+ * Returns: a list of the keys on @settings
+ *
+ * Introspects the list of keys on @settings.
+ *
+ * You should probably not be calling this function from "normal" code
+ * (since you should already know what keys are in your schema).  This
+ * function is intended for introspection reasons.
+ *
+ * You should free the return value with g_free() when you are done with
+ * it.
+ */
+const gchar **
+g_settings_list_keys (GSettings *settings)
+{
+  const GQuark *keys;
+  const gchar **strv;
+  gint n_keys;
+  gint i;
+
+  keys = g_settings_schema_list (settings->priv->schema, &n_keys);
+  strv = g_new (const gchar *, n_keys + 1);
+  for (i = 0; i < n_keys; i++)
+    strv[i] = g_quark_to_string (keys[i]);
+  strv[i] = NULL;
+
+  return strv;
+}
+
 /* Binding {{{1 */
 typedef struct
 {
-  GSettings *settings;
+  GSettingsKeyInfo info;
   GObject *object;
 
   GSettingsBindGetMapping get_mapping;
@@ -1880,8 +1902,6 @@ typedef struct
   guint property_handler_id;
   const GParamSpec *property;
   guint key_handler_id;
-  GVariantType *type;
-  const gchar *key;
 
   /* prevent recursion */
   gboolean running;
@@ -1895,11 +1915,11 @@ g_settings_binding_free (gpointer data)
   g_assert (!binding->running);
 
   if (binding->writable_handler_id)
-    g_signal_handler_disconnect (binding->settings,
+    g_signal_handler_disconnect (binding->info.settings,
                                  binding->writable_handler_id);
 
   if (binding->key_handler_id)
-    g_signal_handler_disconnect (binding->settings,
+    g_signal_handler_disconnect (binding->info.settings,
                                  binding->key_handler_id);
 
   if (g_signal_handler_is_connected (binding->object,
@@ -1907,8 +1927,7 @@ g_settings_binding_free (gpointer data)
   g_signal_handler_disconnect (binding->object,
                                binding->property_handler_id);
 
-  g_variant_type_free (binding->type);
-  g_object_unref (binding->settings);
+  g_settings_free_key_info (&binding->info);
 
   if (binding->destroy)
     binding->destroy (binding->user_data);
@@ -1938,8 +1957,8 @@ g_settings_binding_key_changed (GSettings   *settings,
   GValue value = { 0, };
   GVariant *variant;
 
-  g_assert (settings == binding->settings);
-  g_assert (key == binding->key);
+  g_assert (settings == binding->info.settings);
+  g_assert (key == binding->info.key);
 
   if (binding->running)
     return;
@@ -1947,11 +1966,42 @@ g_settings_binding_key_changed (GSettings   *settings,
   binding->running = TRUE;
 
   g_value_init (&value, binding->property->value_type);
-  variant = g_settings_get_value (settings, key);
-  if (binding->get_mapping (&value, variant, binding->user_data))
-    g_object_set_property (binding->object,
-                           binding->property->name,
-                           &value);
+
+  variant = g_settings_read_from_backend (&binding->info);
+  if (variant && !binding->get_mapping (&value, variant, binding->user_data))
+    {
+      /* silently ignore errors in the user's config database */
+      g_variant_unref (variant);
+      variant = NULL;
+    }
+
+  if (variant == NULL)
+    {
+      variant = g_settings_get_translated_default (&binding->info);
+      if (variant &&
+          !binding->get_mapping (&value, variant, binding->user_data))
+        {
+          /* flag translation errors with a warning */
+          g_warning ("Translated default `%s' for key `%s' in schema `%s' "
+                     "was rejected by the binding mapping function",
+                     binding->info.unparsed, binding->info.key,
+                     binding->info.settings->priv->schema_name);
+          g_variant_unref (variant);
+          variant = NULL;
+        }
+    }
+
+  if (variant == NULL)
+    {
+      variant = g_variant_ref (binding->info.default_value);
+      if (!binding->get_mapping (&value, variant, binding->user_data))
+        g_error ("The schema default value for key `%s' in schema `%s' "
+                 "was rejected by the binding mapping function.",
+                 binding->info.key,
+                 binding->info.settings->priv->schema_name);
+    }
+
+  g_object_set_property (binding->object, binding->property->name, &value);
   g_variant_unref (variant);
   g_value_unset (&value);
 
@@ -1977,12 +2027,34 @@ g_settings_binding_property_changed (GObject          *object,
 
   g_value_init (&value, pspec->value_type);
   g_object_get_property (object, pspec->name, &value);
-  if ((variant = binding->set_mapping (&value, binding->type,
+  if ((variant = binding->set_mapping (&value, binding->info.type,
                                        binding->user_data)))
     {
-      g_settings_set_value (binding->settings,
-                            binding->key,
-                            g_variant_ref_sink (variant));
+      if (g_variant_is_floating (variant))
+        g_variant_ref_sink (variant);
+
+      if (!g_settings_type_check (&binding->info, variant))
+        {
+          g_critical ("binding mapping function for key `%s' returned "
+                      "GVariant of type `%s' when type `%s' was requested",
+                      binding->info.key, g_variant_get_type_string (variant),
+                      g_variant_type_dup_string (binding->info.type));
+          return;
+        }
+
+      if (!g_settings_range_check (&binding->info, variant))
+        {
+          g_critical ("GObject property `%s' on a `%s' object is out of "
+                      "schema-specified range for key `%s' of `%s': %s",
+                      binding->property->name,
+                      g_type_name (binding->property->owner_type),
+                      binding->info.key,
+                      binding->info.settings->priv->schema_name,
+                      g_variant_print (variant, TRUE));
+          return;
+        }
+
+      g_settings_write_to_backend (&binding->info, variant);
       g_variant_unref (variant);
     }
   g_value_unset (&value);
@@ -2080,9 +2152,8 @@ g_settings_bind_with_mapping (GSettings               *settings,
   objectclass = G_OBJECT_GET_CLASS (object);
 
   binding = g_slice_new0 (GSettingsBinding);
-  binding->settings = g_object_ref (settings);
+  g_settings_get_key_info (&binding->info, settings, key);
   binding->object = object;
-  binding->key = g_intern_string (key);
   binding->property = g_object_class_find_property (objectclass, property);
   binding->user_data = user_data;
   binding->destroy = destroy;
@@ -2099,47 +2170,31 @@ g_settings_bind_with_mapping (GSettings               *settings,
       return;
     }
 
-  if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
+  if ((flags & G_SETTINGS_BIND_GET) &&
+      (binding->property->flags & G_PARAM_WRITABLE) == 0)
     {
-      g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
-                  property, G_OBJECT_TYPE_NAME (object));
+      g_critical ("g_settings_bind: property '%s' on class '%s' is not "
+                  "writable", property, G_OBJECT_TYPE_NAME (object));
       return;
     }
-  if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
-    {
-      g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
-                  property, G_OBJECT_TYPE_NAME (object));
-      return;
-    }
-
-  {
-    GVariantIter *iter;
-    GVariant *value;
-
-    iter = g_settings_schema_get_value (settings->priv->schema, key);
-    value = g_variant_iter_next_value (iter);
-    binding->type = g_variant_type_copy (g_variant_get_type (value));
-    g_variant_iter_free (iter);
-    g_variant_unref (value);
-  }
-
-  if (binding->type == NULL)
+  if ((flags & G_SETTINGS_BIND_SET) &&
+      (binding->property->flags & G_PARAM_READABLE) == 0)
     {
-      g_critical ("g_settings_bind: no key '%s' on schema '%s'",
-                  key, settings->priv->schema_name);
+      g_critical ("g_settings_bind: property '%s' on class '%s' is not "
+                  "readable", property, G_OBJECT_TYPE_NAME (object));
       return;
     }
 
   if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
        (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
       !g_settings_mapping_is_compatible (binding->property->value_type,
-                                         binding->type))
+                                         binding->info.type))
     {
       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
                   "'%s' which is not compatible with type '%s' of key '%s' "
                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
                   g_type_name (binding->property->value_type),
-                  g_variant_type_dup_string (binding->type), key,
+                  g_variant_type_dup_string (binding->info.type), key,
                   settings->priv->schema_name);
       return;
     }
@@ -2153,7 +2208,7 @@ g_settings_bind_with_mapping (GSettings               *settings,
 
       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
           (sensitive->flags & G_PARAM_WRITABLE))
-        g_settings_bind_writable (settings, binding->key,
+        g_settings_bind_writable (settings, binding->info.key,
                                   object, "sensitive", FALSE);
     }
 
@@ -2184,7 +2239,7 @@ g_settings_bind_with_mapping (GSettings               *settings,
           g_free (detailed_signal);
         }
 
-      g_settings_binding_key_changed (settings, binding->key, binding);
+      g_settings_binding_key_changed (settings, binding->info.key, binding);
     }
 
   binding_quark = g_settings_binding_quark (property);