cpw.gillux.detachablepurple: f3531495: Factorized the GVariant to GValue conver...

gillux at soc.pidgin.im gillux at soc.pidgin.im
Mon May 14 23:27:17 EDT 2012


----------------------------------------------------------------------
Revision: f3531495f214a2ae83f9dfc05d2898f16662923e
Parent:   d4f8bcdb2b33fde33439f951b4a8ca16d0b1e49a
Author:   gillux at soc.pidgin.im
Date:     05/14/12 20:49:46
Branch:   im.pidgin.cpw.gillux.detachablepurple
URL: http://d.pidgin.im/viewmtn/revision/info/f3531495f214a2ae83f9dfc05d2898f16662923e

Changelog: 

Factorized the GVariant to GValue conversion code into a new function.

Changes against parent d4f8bcdb2b33fde33439f951b4a8ca16d0b1e49a

  patched  libpurple/dbus/constructor.c
  patched  libpurple/pobject.c
  patched  libpurple/pobject.h

-------------- next part --------------
============================================================
--- libpurple/pobject.c	8d6b246f8d84441d057ad38ec4f474ce2baf7433
+++ libpurple/pobject.c	bb04c1f7351ce1b648a9aeea7292dfe3d6e601b3
@@ -401,6 +401,26 @@ purple_object_get_synchronized(PurpleObj
 	return priv->dbus_synchronized;
 }
 
+void
+purple_dbus_gvariant_to_gvalue(GVariant *gvariant, GValue *gvalue,
+                               GType expected_type)
+{
+	GValue tmp = {0, };
+
+	g_dbus_gvariant_to_gvalue(gvariant, gvalue);
+	/* Convert path names into gobjects. */
+	if (G_TYPE_IS_OBJECT(expected_type)
+	    && G_VALUE_HOLDS_STRING(gvalue))
+	{
+		g_value_init(&tmp, expected_type);
+		g_value_transform(gvalue, &tmp);
+		g_value_unset(gvalue);
+		g_value_init(gvalue, expected_type);
+		g_value_copy(&tmp, gvalue);
+		g_value_unset(&tmp);
+	}
+}
+
 GVariant *
 purple_object_get_dbus_property(PurpleObject *pobj, const gchar *prop_name)
 {
@@ -449,8 +469,6 @@ purple_object_set_dbus_property(PurpleOb
 	GParamSpec *pspec;
 	GDBusPropertyInfo *prop_info;
 	GValue gval = {0, };
-	GValue gval2 = {0, };
-	gboolean is_sync;
 
 	/* Check if such a named property exists. */
 	pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(pobj)),
@@ -469,19 +487,10 @@ purple_object_set_dbus_property(PurpleOb
 		prop_value = g_variant_get_variant(prop_value);
 
 	/* Convert this GVariant to a GValue. */
-	g_dbus_gvariant_to_gvalue(prop_value, &gval);
-	g_value_init(&gval2, G_PARAM_SPEC_VALUE_TYPE(pspec));
-	if (G_TYPE_IS_OBJECT(G_PARAM_SPEC_VALUE_TYPE(pspec))
-	    && G_VALUE_HOLDS_STRING(&gval)) {
-		/* Convert dbus path names into gobjects. */
-		g_value_transform(&gval, &gval2);
-	} else {
-		/* Regular value. */
-		g_value_copy(&gval, &gval2);
-	}
+	purple_dbus_gvariant_to_gvalue(prop_value, &gval,
+	                               G_PARAM_SPEC_VALUE_TYPE(pspec));
+	g_object_set_property(G_OBJECT(pobj), prop_name, &gval);
 	g_value_unset(&gval);
-	g_object_set_property(G_OBJECT(pobj), prop_name, &gval2);
-	g_value_unset(&gval2);
 	return TRUE;
 }
 
============================================================
--- libpurple/pobject.h	a8aadb380ed9069aa0f06104774525e6b4829015
+++ libpurple/pobject.h	62de0fd0cb0699904559dcd306bf880b90ed369f
@@ -261,6 +261,20 @@ GClosure *purple_object_get_dbus_closure
                                          const char        *name);
 
 /**
+ * Transforms a D-Bus GVariant into a GObject GValue, doing a conversion
+ * from a D-Bus type (e.g. object path string) to an expected
+ * GType (e.g. respectively object reference), if necessary.
+ *
+ * @param gvariant A GVariant.
+ * @param gvalue Return location pointing to a zero-filled
+ *        (uninitialized) GValue.
+ * @param expected_type The high-level type you want your gvariant
+ *        to be enventually converted to.
+ */
+void purple_dbus_gvariant_to_gvalue(GVariant *gvariant, GValue *gvalue,
+                                    GType expected_type);
+
+/**
  * Gets a property exported on D-Bus. It's like directly getting the
  * property on the object, but it also check if the property is
  * exported on D-Bus and eventually converts object references to
============================================================
--- libpurple/dbus/constructor.c	c23b1c484d68df283c332481d25003b7da7fe34f
+++ libpurple/dbus/constructor.c	a08835df568bef5ff02cf80546f87006b0402fba
@@ -189,10 +189,13 @@ purple_constructor_new_proxy_object(gcha
 	const gchar *prop_name;
 	GVariant *prop_value;
 	gpointer object;
+	GObjectClass *class;
+	GParamSpec *pspec;
 
-	/* Retreive the type. */
+	/* Retreive the type and its class. */
 	type = g_type_from_name(type_name);
 	g_return_val_if_fail(type != 0, NULL);
+	class = g_type_class_ref(type);
 
 	/* Parse the given properties and build the parmas array. */
 	g_variant_iter_init(&iter, props);
@@ -201,16 +204,12 @@ purple_constructor_new_proxy_object(gcha
 	while ((prop = g_variant_iter_next_value(&iter)) != NULL && i < n_params) {
 		g_variant_get(prop, "(sv)", &prop_name, &prop_value);
 		params[i].name = prop_name;
-
-		/* Maybe convert object path names into object references. */
-		if (g_variant_is_of_type(prop_value, G_VARIANT_TYPE_OBJECT_PATH)) {
-			const gchar *path = g_variant_get_string(prop_value, NULL);
-			GObject *obj = purple_dbus_get_gobject_by_path(path);
-			g_value_init(&params[i].value, G_TYPE_OBJECT);
-			g_value_set_object(&params[i].value, obj);
-		} else {
-			g_dbus_gvariant_to_gvalue(prop_value, &params[i].value);
-		}
+		pspec = g_object_class_find_property(class, prop_name);
+		if (pspec)
+			purple_dbus_gvariant_to_gvalue(prop_value, &params[i].value,
+			                               G_PARAM_SPEC_VALUE_TYPE(pspec));
+		else
+			purple_debug_warning("dbus", "Ignored an unknown property named '%s' for object type '%s'.\n", prop_name, type_name);
 		i++;
 
 		g_variant_unref(prop_value);
@@ -226,6 +225,7 @@ purple_constructor_new_proxy_object(gcha
 		g_value_unset(&params[n_params].value);
 	}
 	g_free(params);
+	g_type_class_unref(class);
 
 	return object;
 }


More information about the Commits mailing list