[gnome-settings-daemon/wip/xsettings-manager: 15/16] xsettings: switch to GVariant for value storage



commit 28e6fc5756e4383b6a64e321531954e8baff1fd3
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Mar 8 00:13:13 2012 -0500

    xsettings: switch to GVariant for value storage
    
    Drop the tagged union setup that we were using previously.

 plugins/xsettings/xsettings-common.c  |   50 ++++-----------------
 plugins/xsettings/xsettings-common.h  |   15 ++----
 plugins/xsettings/xsettings-manager.c |   81 +++++++++++++++++----------------
 3 files changed, 56 insertions(+), 90 deletions(-)
---
diff --git a/plugins/xsettings/xsettings-common.c b/plugins/xsettings/xsettings-common.c
index aa63410..647f1df 100644
--- a/plugins/xsettings/xsettings-common.c
+++ b/plugins/xsettings/xsettings-common.c
@@ -38,7 +38,7 @@ xsettings_setting_new (const gchar *name)
 
   result = g_slice_new (XSettingsSetting);
   result->name = g_strdup (name);
-  result->type = XSETTINGS_TYPE_INT;
+  result->value = NULL;
   result->last_change_serial = 0;
 
   return result;
@@ -46,58 +46,26 @@ xsettings_setting_new (const gchar *name)
 
 void
 xsettings_setting_set (XSettingsSetting *setting,
-                       XSettingsSetting *value)
+                       GVariant         *value)
 {
-  if (setting->type == XSETTINGS_TYPE_STRING)
-    g_free (setting->data.v_string);
+  if (setting->value)
+    g_variant_unref (setting->value);
 
-  setting->type = value->type;
-
-  switch (value->type)
-    {
-    case XSETTINGS_TYPE_INT:
-      setting->data.v_int = value->data.v_int;
-      break;
-    case XSETTINGS_TYPE_COLOR:
-      setting->data.v_color = value->data.v_color;
-      break;
-    case XSETTINGS_TYPE_STRING:
-      setting->data.v_string = g_strdup (value->data.v_string);
-      break;
-    }
+  setting->value = value ? g_variant_ref (value) : NULL;
 }
 
 gboolean
 xsettings_setting_equal (XSettingsSetting *setting,
-                         XSettingsSetting *value)
+                         GVariant         *value)
 {
-  if (setting->type != value->type)
-    return FALSE;
-
-  if (strcmp (setting->name, value->name) != 0)
-    return FALSE;
-
-  switch (setting->type)
-    {
-    case XSETTINGS_TYPE_INT:
-      return setting->data.v_int == value->data.v_int;
-    case XSETTINGS_TYPE_COLOR:
-      return (setting->data.v_color.red == value->data.v_color.red &&
-              setting->data.v_color.green == value->data.v_color.green &&
-              setting->data.v_color.blue == value->data.v_color.blue &&
-              setting->data.v_color.alpha == value->data.v_color.alpha);
-    case XSETTINGS_TYPE_STRING:
-      return strcmp (setting->data.v_string, value->data.v_string) == 0;
-    }
-
-  return FALSE;
+  return setting->value && g_variant_equal (setting->value, value);
 }
 
 void
 xsettings_setting_free (XSettingsSetting *setting)
 {
-  if (setting->type == XSETTINGS_TYPE_STRING)
-    g_free (setting->data.v_string);
+  if (setting->value)
+    g_variant_unref (setting->value);
 
   g_free (setting->name);
 
diff --git a/plugins/xsettings/xsettings-common.h b/plugins/xsettings/xsettings-common.h
index 1f46ab9..4f03d9e 100644
--- a/plugins/xsettings/xsettings-common.h
+++ b/plugins/xsettings/xsettings-common.h
@@ -23,6 +23,8 @@
 #ifndef XSETTINGS_COMMON_H
 #define XSETTINGS_COMMON_H
 
+#include <glib.h>
+
 typedef struct _XSettingsColor   XSettingsColor;
 typedef struct _XSettingsSetting XSettingsSetting;
 
@@ -44,23 +46,16 @@ struct _XSettingsColor
 struct _XSettingsSetting
 {
   char *name;
-  XSettingsType type;
-  
-  union {
-    int v_int;
-    char *v_string;
-    XSettingsColor v_color;
-  } data;
-
+  GVariant *value;
   unsigned long last_change_serial;
 };
 
 XSettingsSetting *xsettings_setting_new   (const gchar      *name);
 void              xsettings_setting_set   (XSettingsSetting *setting,
-                                           XSettingsSetting *value);
+                                           GVariant         *value);
 void              xsettings_setting_free  (XSettingsSetting *setting);
 gboolean          xsettings_setting_equal (XSettingsSetting *setting,
-                                           XSettingsSetting *value);
+                                           GVariant         *value);
 
 char xsettings_byte_order (void);
 
diff --git a/plugins/xsettings/xsettings-manager.c b/plugins/xsettings/xsettings-manager.c
index ce225ff..401f28d 100644
--- a/plugins/xsettings/xsettings-manager.c
+++ b/plugins/xsettings/xsettings-manager.c
@@ -203,10 +203,12 @@ xsettings_manager_delete_setting (XSettingsManager *manager,
 static void
 xsettings_manager_set_setting (XSettingsManager *manager,
                                const gchar      *name,
-                               XSettingsSetting *value)
+                               GVariant         *value)
 {
   XSettingsSetting *setting;
 
+  g_variant_ref_sink (value);
+
   setting = g_hash_table_lookup (manager->settings, name);
 
   if (setting == NULL)
@@ -221,6 +223,8 @@ xsettings_manager_set_setting (XSettingsManager *manager,
       xsettings_setting_set (setting, value);
       setting->last_change_serial = manager->serial;
     }
+
+  g_variant_unref (value);
 }
 
 void
@@ -228,13 +232,7 @@ xsettings_manager_set_int (XSettingsManager *manager,
 			   const char       *name,
 			   int               value)
 {
-  XSettingsSetting setting;
-
-  setting.name = (char *)name;
-  setting.type = XSETTINGS_TYPE_INT;
-  setting.data.v_int = value;
-
-  xsettings_manager_set_setting (manager, name, &setting);
+  xsettings_manager_set_setting (manager, name, g_variant_new_int32 (value));
 }
 
 void
@@ -242,13 +240,7 @@ xsettings_manager_set_string (XSettingsManager *manager,
 			      const char       *name,
 			      const char       *value)
 {
-  XSettingsSetting setting;
-
-  setting.name = (char *)name;
-  setting.type = XSETTINGS_TYPE_STRING;
-  setting.data.v_string = (char *)value;
-
-  xsettings_manager_set_setting (manager, name, &setting);
+  xsettings_manager_set_setting (manager, name, g_variant_new_string (value));
 }
 
 void
@@ -256,23 +248,41 @@ xsettings_manager_set_color (XSettingsManager *manager,
 			     const char       *name,
 			     XSettingsColor   *value)
 {
-  XSettingsSetting setting;
+  GVariant *tmp;
 
-  setting.name = (char *)name;
-  setting.type = XSETTINGS_TYPE_COLOR;
-  setting.data.v_color = *value;
+  tmp = g_variant_new ("(qqqq)", value->red, value->green, value->blue, value->alpha);
+  xsettings_manager_set_setting (manager, name, tmp);
+}
 
-  xsettings_manager_set_setting (manager, name, &setting);
+static gchar
+xsettings_get_typecode (GVariant *value)
+{
+  switch (g_variant_classify (value))
+    {
+    case G_VARIANT_CLASS_INT32:
+      return XSETTINGS_TYPE_INT;
+    case G_VARIANT_CLASS_STRING:
+      return XSETTINGS_TYPE_STRING;
+    case G_VARIANT_CLASS_TUPLE:
+      return XSETTINGS_TYPE_COLOR;
+    default:
+      g_assert_not_reached ();
+    }
 }
 
 static void
 setting_store (XSettingsSetting *setting,
                GString          *buffer)
 {
+  XSettingsType type;
+  GVariant *value;
   guint16 len16;
-  guint32 len32;
 
-  g_string_append_c (buffer, setting->type);
+  value = setting->value;
+
+  type = xsettings_get_typecode (value);
+
+  g_string_append_c (buffer, type);
   g_string_append_c (buffer, 0);
 
   len16 = strlen (setting->name);
@@ -284,30 +294,23 @@ setting_store (XSettingsSetting *setting,
 
   g_string_append_len (buffer, (gchar *) &setting->last_change_serial, 4);
 
-  switch (setting->type)
+  if (type == XSETTINGS_TYPE_STRING)
     {
-    case XSETTINGS_TYPE_INT:
-      g_string_append_len (buffer, (gchar *) &setting->data.v_int, 4);
-      break;
+      const gchar *string;
+      gsize stringlen;
+      guint32 len32;
 
-    case XSETTINGS_TYPE_STRING:
-      len32 = strlen (setting->data.v_string);
+      string = g_variant_get_string (value, &stringlen);
+      len32 = stringlen;
       g_string_append_len (buffer, (gchar *) &len32, 4);
-
-      g_string_append (buffer, setting->data.v_string);
+      g_string_append (buffer, string);
 
       while (buffer->len & 3)
         g_string_append_c (buffer, '\0');
-
-      break;
-
-    case XSETTINGS_TYPE_COLOR:
-      g_string_append_len (buffer, (gchar *) &setting->data.v_color.red, 2);
-      g_string_append_len (buffer, (gchar *) &setting->data.v_color.green, 2);
-      g_string_append_len (buffer, (gchar *) &setting->data.v_color.blue, 2);
-      g_string_append_len (buffer, (gchar *) &setting->data.v_color.alpha, 2);
-      break;
     }
+  else
+    /* GVariant format is the same as XSETTINGS format for the non-string types */
+    g_string_append_len (buffer, g_variant_get_data (value), g_variant_get_size (value));
 }
 
 void



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