GSettings: add g_settings_reset()
authorRyan Lortie <desrt@desrt.ca>
Thu, 22 Jul 2010 21:49:40 +0000 (17:49 -0400)
committerRyan Lortie <desrt@desrt.ca>
Thu, 22 Jul 2010 21:50:05 +0000 (17:50 -0400)
Resets a key to its default value.

docs/reference/gio/gio-sections.txt
gio/gdelayedsettingsbackend.c
gio/gio.symbols
gio/gmemorysettingsbackend.c
gio/gsettings.c
gio/gsettings.h

index 00105e6..d76bcf2 100644 (file)
@@ -2138,6 +2138,7 @@ g_settings_apply
 g_settings_revert
 g_settings_get_has_unapplied
 g_settings_get_child
+g_settings_reset
 
 <SUBSECTION Introspection>
 g_settings_list_schemas
index f08ac0e..fd166a1 100644 (file)
@@ -95,15 +95,29 @@ g_delayed_settings_backend_read (GSettingsBackend   *backend,
                                  gboolean            default_value)
 {
   GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
-  GVariant *result;
+  gpointer result = NULL;
 
-  if (!default_value &&
-      (result = g_tree_lookup (delayed->priv->delayed, key)))
-    return g_variant_ref (result);
+  if (!default_value)
+    {
+      g_static_mutex_lock (&delayed->priv->lock);
+      if (g_tree_lookup_extended (delayed->priv->delayed, key, NULL, &result))
+        {
+          /* NULL in the tree means we should consult the default value */
+          if (result != NULL)
+            g_variant_ref (result);
+          else
+            default_value = TRUE;
+        }
+      g_static_mutex_unlock (&delayed->priv->lock);
+    }
+
+  if (result == NULL)
+    result = g_settings_backend_read (delayed->priv->backend, key,
+                                      expected_type, default_value);
 
-  return g_settings_backend_read (delayed->priv->backend,
-                                  key, expected_type, default_value);
+  return result;
 }
+
 static gboolean
 g_delayed_settings_backend_write (GSettingsBackend *backend,
                                   const gchar      *key,
@@ -172,7 +186,16 @@ g_delayed_settings_backend_reset (GSettingsBackend *backend,
                                   const gchar      *key,
                                   gpointer          origin_tag)
 {
-  /* deal with this... */
+  GDelayedSettingsBackend *delayed = G_DELAYED_SETTINGS_BACKEND (backend);
+  gboolean was_empty;
+
+  g_static_mutex_lock (&delayed->priv->lock);
+  was_empty = g_tree_nnodes (delayed->priv->delayed) == 0;
+  g_tree_insert (delayed->priv->delayed, g_strdup (key), NULL);
+  g_static_mutex_unlock (&delayed->priv->lock);
+
+  if (was_empty)
+    g_delayed_settings_backend_notify_unapplied (delayed);
 }
 
 static void
index 11d0372..40383a7 100644 (file)
@@ -1464,6 +1464,7 @@ g_settings_new_with_backend
 g_settings_new_with_backend_and_path
 g_settings_new_with_path
 g_settings_revert
+g_settings_reset
 g_settings_set
 g_settings_set_value
 g_settings_unbind
index 26a0d79..a573027 100644 (file)
@@ -95,7 +95,10 @@ g_memory_settings_backend_write_one (gpointer key,
 {
   GMemorySettingsBackend *memory = data;
 
-  g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
+  if (value != NULL)
+    g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
+  else
+    g_hash_table_remove (memory->table, key);
 
   return FALSE;
 }
@@ -111,6 +114,20 @@ g_memory_settings_backend_write_keys (GSettingsBackend *backend,
   return TRUE;
 }
 
+static void
+g_memory_settings_backend_reset (GSettingsBackend *backend,
+                                 const gchar      *key,
+                                 gpointer          origin_tag)
+{
+  GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
+
+  if (g_hash_table_lookup (memory->table, key))
+    {
+      g_hash_table_remove (memory->table, key);
+      g_settings_backend_changed (backend, key, origin_tag);
+    }
+}
+
 static gboolean
 g_memory_settings_backend_get_writable (GSettingsBackend *backend,
                                         const gchar      *name)
@@ -152,6 +169,7 @@ g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)
   backend_class->read = g_memory_settings_backend_read;
   backend_class->write = g_memory_settings_backend_write;
   backend_class->write_keys = g_memory_settings_backend_write_keys;
+  backend_class->reset = g_memory_settings_backend_reset;
   backend_class->get_writable = g_memory_settings_backend_get_writable;
   backend_class->get_permission = g_memory_settings_backend_get_permission;
   object_class->finalize = g_memory_settings_backend_finalize;
index 5b72540..8636d66 100644 (file)
@@ -1961,7 +1961,29 @@ g_settings_get_has_unapplied (GSettings *settings)
            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
 }
 
-/* Extra API (sync, get_child, is_writable, list_items) {{{1 */
+/* Extra API (reset, sync, get_child, is_writable, list_items) {{{1 */
+/**
+ * g_settings_reset:
+ * @settings: a #GSettings object
+ * @key: the name of a key
+ *
+ * Resets @key to its default value.
+ *
+ * This call resets the key, as much as possible, to its default value.
+ * That might the value specified in the schema or the one set by the
+ * administrator.
+ **/
+void
+g_settings_reset (GSettings *settings,
+                  const gchar *key)
+{
+  gchar *path;
+
+  path = g_strconcat (settings->priv->path, key, NULL);
+  g_settings_backend_reset (settings->priv->backend, path, NULL);
+  g_free (path);
+}
+
 /**
  * g_settings_sync:
  *
index d70d7fa..799370e 100644 (file)
@@ -95,6 +95,8 @@ void                    g_settings_get                                  (GSettin
                                                                          const gchar        *key,
                                                                          const gchar        *format,
                                                                          ...);
+void                    g_settings_reset                                (GSettings          *settings,
+                                                                         const gchar        *key);
 
 gint                    g_settings_get_int                              (GSettings          *settings,
                                                                          const gchar        *key);