/srv/mercurial-server/detachablepurple: a0c20f80e205: Factorized...

Gilles Bedel gillux at cpw.pidgin.im
Fri Jun 15 22:01:35 EDT 2012


Changeset: a0c20f80e205c7354ce55351be4f283cdc3c0965
Author:	 Gilles Bedel <gillux at cpw.pidgin.im>
Date:	 2012-05-15 00:49 +0000
Branch:	 cpw.gillux.detachablepurple
URL: http://hg.pidgin.im/srv/mercurial-server/detachablepurple/rev/a0c20f80e205

Description:

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

diffstat:

 libpurple/dbus/constructor.c |  22 +++++++++++-----------
 libpurple/pobject.c          |  37 +++++++++++++++++++++++--------------
 libpurple/pobject.h          |  14 ++++++++++++++
 3 files changed, 48 insertions(+), 25 deletions(-)

diffs (135 lines):

diff --git a/libpurple/dbus/constructor.c b/libpurple/dbus/constructor.c
--- a/libpurple/dbus/constructor.c
+++ b/libpurple/dbus/constructor.c
@@ -189,10 +189,13 @@
 	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 @@
 	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 @@
 		g_value_unset(&params[n_params].value);
 	}
 	g_free(params);
+	g_type_class_unref(class);
 
 	return object;
 }
diff --git a/libpurple/pobject.c b/libpurple/pobject.c
--- a/libpurple/pobject.c
+++ b/libpurple/pobject.c
@@ -401,6 +401,26 @@
 	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 @@
 	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 @@
 		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;
 }
 
diff --git a/libpurple/pobject.h b/libpurple/pobject.h
--- a/libpurple/pobject.h
+++ b/libpurple/pobject.h
@@ -261,6 +261,20 @@
                                          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



More information about the Commits mailing list