[glib] GSettings: add g_settings_reset()



commit aed440815ec5c87f2439d723b60b8255578afdff
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jul 22 17:49:40 2010 -0400

    GSettings: add g_settings_reset()
    
    Resets a key to its default value.

 docs/reference/gio/gio-sections.txt |    1 +
 gio/gdelayedsettingsbackend.c       |   37 ++++++++++++++++++++++++++++------
 gio/gio.symbols                     |    1 +
 gio/gmemorysettingsbackend.c        |   20 ++++++++++++++++++-
 gio/gsettings.c                     |   24 +++++++++++++++++++++-
 gio/gsettings.h                     |    2 +
 6 files changed, 76 insertions(+), 9 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 00105e6..d76bcf2 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -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
diff --git a/gio/gdelayedsettingsbackend.c b/gio/gdelayedsettingsbackend.c
index f08ac0e..fd166a1 100644
--- a/gio/gdelayedsettingsbackend.c
+++ b/gio/gdelayedsettingsbackend.c
@@ -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
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 11d0372..40383a7 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -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
diff --git a/gio/gmemorysettingsbackend.c b/gio/gmemorysettingsbackend.c
index 26a0d79..a573027 100644
--- a/gio/gmemorysettingsbackend.c
+++ b/gio/gmemorysettingsbackend.c
@@ -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;
diff --git a/gio/gsettings.c b/gio/gsettings.c
index 5b72540..8636d66 100644
--- a/gio/gsettings.c
+++ b/gio/gsettings.c
@@ -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:
  *
diff --git a/gio/gsettings.h b/gio/gsettings.h
index d70d7fa..799370e 100644
--- a/gio/gsettings.h
+++ b/gio/gsettings.h
@@ -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);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]