[glib/keyfilebackend] moar



commit 0ad4e53f7ea9c8d85bbd612a035b6db3ab8de665
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Jun 23 10:11:21 2010 -0400

    moar

 gio/gkeyfilesettingsbackend.c |  181 ++++++++++++++++++++---------------------
 1 files changed, 89 insertions(+), 92 deletions(-)
---
diff --git a/gio/gkeyfilesettingsbackend.c b/gio/gkeyfilesettingsbackend.c
index d19706a..70d06f6 100644
--- a/gio/gkeyfilesettingsbackend.c
+++ b/gio/gkeyfilesettingsbackend.c
@@ -64,6 +64,33 @@ G_DEFINE_TYPE (GKeyfileSettingsBackend,
                g_keyfile_settings_backend,
                G_TYPE_SETTINGS_BACKEND)
 
+static void
+g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
+{
+  gchar *dirname;
+  gchar *contents;
+  gsize  length;
+  GFile *file;
+
+  dirname = g_path_get_dirname (kfsb->file_path);
+  if (!g_file_test (dirname, G_FILE_TEST_IS_DIR))
+    g_mkdir_with_parents (dirname, 0700);
+  g_free (dirname);
+
+  contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
+
+  file = g_file_new_for_path (kfsb->file_path);
+  g_file_replace_contents (file, contents, length,
+                           NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION,
+                           NULL, NULL, NULL);
+  g_object_unref (file);
+
+  g_free (kfsb->checksum);
+  kfsb->checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256, contents, length);
+
+  g_free (contents);
+}
+
 static gboolean
 convert_path (GKeyfileSettingsBackend  *kfsb,
               const gchar              *key,
@@ -247,6 +274,7 @@ g_keyfile_settings_backend_write_many (GSettingsBackend *backend,
     return FALSE;
 
   g_tree_foreach (tree, g_keyfile_settings_backend_write_one, &data);
+  g_keyfile_settings_backend_keyfile_write (data.kfsb);
 
   g_settings_backend_changed_tree (backend, tree, origin_tag);
 
@@ -268,7 +296,10 @@ g_keyfile_settings_backend_write (GSettingsBackend *backend,
   success = set_to_keyfile (kfsb, key, value);
 
   if (success)
-    g_settings_backend_changed (backend, key, origin_tag);
+    {
+      g_settings_backend_changed (backend, key, origin_tag);
+      g_keyfile_settings_backend_keyfile_write (kfsb);
+    }
 
   return success;
 }
@@ -280,7 +311,8 @@ g_keyfile_settings_backend_reset_path (GSettingsBackend *backend,
 {
   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
 
-  set_to_keyfile (kfsb, path, NULL);
+  if (set_to_keyfile (kfsb, path, NULL))
+    g_keyfile_settings_backend_keyfile_write (kfsb);
 
   g_settings_backend_path_changed (backend, path, origin_tag);
 }
@@ -292,7 +324,8 @@ g_keyfile_settings_backend_reset (GSettingsBackend *backend,
 {
   GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (backend);
 
-  set_to_keyfile (kfsb, key, NULL);
+  if (set_to_keyfile (kfsb, key, NULL))
+    g_keyfile_settings_backend_keyfile_write (kfsb);
 
   g_settings_backend_changed (backend, key, origin_tag);
 }
@@ -314,33 +347,6 @@ g_keyfile_settings_backend_get_permission (GSettingsBackend *backend,
 }
 
 static void
-g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb)
-{
-  gchar *dirname;
-  gchar *contents;
-  gsize  length;
-  GFile *file;
-
-  dirname = g_path_get_dirname (kfsb->file_path);
-  if (!g_file_test (dirname, G_FILE_TEST_IS_DIR))
-    g_mkdir_with_parents (dirname, 0700);
-  g_free (dirname);
-
-  contents = g_key_file_to_data (kfsb->keyfile, &length, NULL);
-
-  file = g_file_new_for_path (kfsb->file_path);
-  g_file_replace_contents (file, contents, length,
-                           NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION,
-                           NULL, NULL, NULL);
-  g_object_unref (file);
-
-  g_free (kfsb->checksum);
-  kfsb->checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256, contents, length);
-
-  g_free (contents);
-}
-
-static void
 g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
 {
   gchar       *contents = NULL;
@@ -354,47 +360,48 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
   GList       *keys_l = NULL;
   GList       *l;
 
-  if (!g_file_get_contents (kf_backend->priv->file_path,
-                            &contents, &length, NULL))
+  if (!g_file_get_contents (kfsb->file_path, &contents, &length, NULL))
     {
       contents = g_strdup ("");
       length = 0;
     }
 
-  new_checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256, contents, length);
+  new_checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA256,
+                                                contents, length);
 
-  if (g_strcmp0 (kf_backend->priv->checksum, new_checksum) == 0)
+  if (g_strcmp0 (kfsb->checksum, new_checksum) == 0)
     {
       g_free (new_checksum);
+      g_free (contents);
       return;
     }
 
-  if (kf_backend->priv->checksum != NULL)
-    g_free (kf_backend->priv->checksum);
-  kf_backend->priv->checksum = new_checksum;
+  if (kfsb->checksum != NULL)
+    g_free (kfsb->checksum);
+  kfsb->checksum = new_checksum;
 
-  if (kf_backend->priv->keyfile != NULL)
-    g_key_file_free (kf_backend->priv->keyfile);
+  if (kfsb->keyfile != NULL)
+    g_key_file_free (kfsb->keyfile);
 
-  kf_backend->priv->keyfile = g_key_file_new ();
+  kfsb->keyfile = g_key_file_new ();
 
   /* we just silently ignore errors: there's not much we can do about them */
   if (length > 0)
-    g_key_file_load_from_data (kf_backend->priv->keyfile, contents, length,
+    g_key_file_load_from_data (kfsb->keyfile, contents, length,
                                G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
 
   loaded_keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
   changed_array = g_ptr_array_new_with_free_func (g_free);
 
   /* Load keys from the keyfile */
-  groups = g_key_file_get_groups (kf_backend->priv->keyfile, &groups_nb);
+  groups = g_key_file_get_groups (kfsb->keyfile, &groups_nb);
   for (i = 0; i < groups_nb; i++)
     {
       gchar **keys = NULL;
       gsize   keys_nb = 0;
       int     j;
 
-      keys = g_key_file_get_keys (kf_backend->priv->keyfile, groups[i], &keys_nb, NULL);
+      keys = g_key_file_get_keys (kfsb->keyfile, groups[i], &keys_nb, NULL);
       if (keys == NULL)
         continue;
 
@@ -405,7 +412,7 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
           GVariant *old_variant;
           GVariant *variant;
 
-          value = g_key_file_get_string (kf_backend->priv->keyfile, groups[i], keys[j], NULL);
+          value = g_key_file_get_string (kfsb->keyfile, groups[i], keys[j], NULL);
           if (value == NULL)
             continue;
 
@@ -418,15 +425,8 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
           full_key = g_strjoin ("", groups[i], keys[j], NULL),
           g_hash_table_insert (loaded_keys, full_key, GINT_TO_POINTER(TRUE));
 
-          old_variant = g_hash_table_lookup (kf_backend->priv->table, full_key);
-
           if (old_variant == NULL || !g_variant_equal (old_variant, variant))
-            {
-              g_ptr_array_add (changed_array, g_strdup (full_key));
-              g_hash_table_replace (kf_backend->priv->table,
-                                    g_strdup (full_key),
-                                    g_variant_ref_sink (variant));
-            }
+            g_ptr_array_add (changed_array, g_strdup (full_key));
           else
             g_variant_unref (variant);
         }
@@ -436,7 +436,7 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
   g_strfreev (groups);
 
   /* Remove keys that were in the hashtable but not in the keyfile */
-  keys_l = g_hash_table_get_keys (kf_backend->priv->table);
+  keys_l = g_hash_table_get_keys (kfsb->table);
   for (l = keys_l; l != NULL; l = l->next)
     {
       gchar *key = l->data;
@@ -445,7 +445,7 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
         continue;
 
       g_ptr_array_add (changed_array, g_strdup (key));
-      g_hash_table_remove (kf_backend->priv->table, key);
+      g_hash_table_remove (kfsb->table, key);
     }
   g_list_free (keys_l);
 
@@ -453,7 +453,7 @@ g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb)
     {
       /* the array has to be NULL-terminated */
       g_ptr_array_add (changed_array, NULL);
-      g_settings_backend_keys_changed (G_SETTINGS_BACKEND (kf_backend),
+      g_settings_backend_keys_changed (G_SETTINGS_BACKEND (kfsb),
                                        "",
                                        (const gchar **) changed_array->pdata,
                                        NULL);
@@ -511,7 +511,7 @@ g_keyfile_settings_backend_keyfile_changed (GFileMonitor      *monitor,
                                             GFileMonitorEvent  event_type,
                                             gpointer           user_data)
 {
-  GKeyfileSettingsBackend *kf_backend;
+  GKeyfileSettingsBackend *kfsb;
 
   if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
       event_type != G_FILE_MONITOR_EVENT_CREATED &&
@@ -519,7 +519,7 @@ g_keyfile_settings_backend_keyfile_changed (GFileMonitor      *monitor,
       event_type != G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED)
     return;
 
-  kf_backend = G_KEYFILE_SETTINGS_BACKEND (user_data);
+  kfsb = G_KEYFILE_SETTINGS_BACKEND (user_data);
 
   if (event_type == G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED)
     {
@@ -527,58 +527,55 @@ g_keyfile_settings_backend_keyfile_changed (GFileMonitor      *monitor,
 
       writable = g_keyfile_settings_backend_keyfile_writable (file);
 
-      if (kf_backend->priv->writable == writable)
+      if (kfsb->writable == writable)
         return;
 
-      kf_backend->priv->writable = writable;
+      kfsb->writable = writable;
       if (!writable)
         return;
       /* else: reload the file since it was possibly not readable before */
     }
 
-  g_keyfile_settings_backend_keyfile_reload (kf_backend);
+  g_keyfile_settings_backend_keyfile_reload (kfsb);
 }
 
 static void
 g_keyfile_settings_backend_finalize (GObject *object)
 {
-  GKeyfileSettingsBackend *kf_backend = G_KEYFILE_SETTINGS_BACKEND (object);
+  GKeyfileSettingsBackend *kfsb = G_KEYFILE_SETTINGS_BACKEND (object);
 
-  g_hash_table_unref (kf_backend->priv->table);
-  kf_backend->priv->table = NULL;
+  g_hash_table_unref (kfsb->table);
+  kfsb->table = NULL;
 
-  g_key_file_free (kf_backend->priv->keyfile);
-  kf_backend->priv->keyfile = NULL;
+  g_key_file_free (kfsb->keyfile);
+  kfsb->keyfile = NULL;
 
-  g_free (kf_backend->priv->file_path);
-  kf_backend->priv->file_path = NULL;
+  g_free (kfsb->file_path);
+  kfsb->file_path = NULL;
 
-  g_free (kf_backend->priv->checksum);
-  kf_backend->priv->checksum = NULL;
+  g_free (kfsb->checksum);
+  kfsb->checksum = NULL;
 
-  g_file_monitor_cancel (kf_backend->priv->monitor);
-  g_object_unref (kf_backend->priv->monitor);
-  kf_backend->priv->monitor = NULL;
+  g_file_monitor_cancel (kfsb->monitor);
+  g_object_unref (kfsb->monitor);
+  kfsb->monitor = NULL;
 
   G_OBJECT_CLASS (g_keyfile_settings_backend_parent_class)
     ->finalize (object);
 }
 
 static void
-g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kf_backend)
+g_keyfile_settings_backend_init (GKeyfileSettingsBackend *kfsb)
 {
-  kf_backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (kf_backend,
-                                                  G_TYPE_KEYFILE_SETTINGS_BACKEND,
-                                                  GKeyfileSettingsBackendPrivate);
-  kf_backend->priv->table =
+  kfsb->table =
     g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                            (GDestroyNotify) g_variant_unref);
 
-  kf_backend->priv->keyfile = NULL;
-  kf_backend->priv->writable = FALSE;
-  kf_backend->priv->file_path = NULL;
-  kf_backend->priv->checksum = NULL;
-  kf_backend->priv->monitor = NULL;
+  kfsb->keyfile = NULL;
+  kfsb->writable = FALSE;
+  kfsb->file_path = NULL;
+  kfsb->checksum = NULL;
+  kfsb->monitor = NULL;
 }
 
 static void
@@ -606,25 +603,25 @@ g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
 GSettingsBackend *
 g_keyfile_settings_backend_new (const gchar *filename)
 {
-  GKeyfileSettingsBackend *kf_backend;
+  GKeyfileSettingsBackend *kfsb;
   GFile *file;
 
-  kf_backend = g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND, NULL);
-  kf_backend->priv->file_path = g_strdup (filename);
+  kfsb = g_object_new (G_TYPE_KEYFILE_SETTINGS_BACKEND, NULL);
+  kfsb->priv->file_path = g_strdup (filename);
 
-  file = g_file_new_for_path (kf_backend->priv->file_path);
+  file = g_file_new_for_path (kfsb->priv->file_path);
 
-  kf_backend->priv->writable = g_keyfile_settings_backend_keyfile_writable (file);
+  kfsb->priv->writable = g_keyfile_settings_backend_keyfile_writable (file);
 
-  kf_backend->priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_SEND_MOVED, NULL, NULL);
-  g_signal_connect (kf_backend->priv->monitor, "changed",
-                    (GCallback)g_keyfile_settings_backend_keyfile_changed, kf_backend);
+  kfsb->priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_SEND_MOVED, NULL, NULL);
+  g_signal_connect (kfsb->priv->monitor, "changed",
+                    (GCallback)g_keyfile_settings_backend_keyfile_changed, kfsb);
 
   g_object_unref (file);
 
-  g_keyfile_settings_backend_keyfile_reload (kf_backend);
+  g_keyfile_settings_backend_keyfile_reload (kfsb);
 
-  return G_SETTINGS_BACKEND (kf_backend);
+  return G_SETTINGS_BACKEND (kfsb);
 }
 
 #define __G_KEYFILE_SETTINGS_BACKEND_C__



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