cpw.gillux.detachablepurple: 7fc0ebb5: Make the PurpleObject loading function p...

gillux at soc.pidgin.im gillux at soc.pidgin.im
Sun May 20 13:21:39 EDT 2012


----------------------------------------------------------------------
Revision: 7fc0ebb5906f481978c6e77c0a216d7c7222aa70
Parent:   b056fb6709801b504b70976e5c476a56bdb13597
Author:   gillux at soc.pidgin.im
Date:     05/20/12 11:38:24
Branch:   im.pidgin.cpw.gillux.detachablepurple
URL: http://d.pidgin.im/viewmtn/revision/info/7fc0ebb5906f481978c6e77c0a216d7c7222aa70

Changelog: 

Make the PurpleObject loading function public, with some enhancements
that will allow the loading of cross-referenced objects (i.e. with
properties that references each other, like PurpleBlistNodes).

Changes against parent b056fb6709801b504b70976e5c476a56bdb13597

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

-------------- next part --------------
============================================================
--- libpurple/dbus/constructor.c	0c145ce9319a7ad9b79c6ff6ab6556cef4d630d9
+++ libpurple/dbus/constructor.c	b7c7943a63227c1a1410e3b8009be930b324b679
@@ -247,9 +247,83 @@ purple_constructor_pack_pobject(gpointer
 	                     purple_constructor_pack_pobject_props(pobj));
 }
 
-static gpointer
-load_pobject(GVariant *vpobject, GType type)
+static void
+purple_constructor_reload_pobj_objpath_props_cb(GVariant *vpobject,
+                                                gpointer  user_data)
 {
+	purple_constructor_reload_pobj_objpath_props(vpobject);
+}
+
+void
+purple_constructor_reload_pobjs_objpath_props(GVariant *pobjects)
+{
+	purple_constructor_foreach_pobjects
+	  (pobjects, purple_constructor_reload_pobj_objpath_props_cb, NULL);
+}
+
+void
+purple_constructor_reload_pobj_objpath_props(GVariant *vpobject)
+{
+	GVariantIter iter;
+	GVariant *props, *prop, *prop_value;
+	gchar *prop_name, *dbus_path;
+	gpointer obj;
+
+	g_variant_get(vpobject, "(o at a(sv))", &dbus_path, &props);
+	obj = purple_dbus_get_gobject_by_path(dbus_path);
+	g_return_if_fail(obj != NULL);
+
+	g_variant_iter_init(&iter, props);
+	while ((prop = g_variant_iter_next_value(&iter)) != NULL) {
+		g_variant_get(prop, "(sv)", &prop_name, &prop_value);
+		if (g_variant_is_of_type(prop_value, G_VARIANT_TYPE_OBJECT_PATH)) {
+			purple_object_set_dbus_property(PURPLE_OBJECT(obj),
+			                                prop_name, prop_value);
+		}
+		g_free(prop_name);
+		g_variant_unref(prop_value);
+		g_variant_unref(prop);
+	}
+	g_variant_unref(props);
+}
+
+static void
+purple_constructor_load_pobj_cb(GVariant *vpobject, gpointer user_data)
+{
+	purple_constructor_load_pobj(vpobject, *(GType*)user_data);
+}
+
+void
+purple_constructor_load_pobjs(GVariant *pobjects, GType type)
+{
+	purple_constructor_foreach_pobjects
+	  (pobjects, purple_constructor_load_pobj_cb, &type);
+}
+
+gpointer
+purple_constructor_load_pobj(GVariant *vpobject, GType type)
+{
+	gpointer obj = purple_constructor_load_pobj_unsync(vpobject, type);
+	purple_object_set_synchronized(PURPLE_OBJECT(obj), TRUE);
+	return obj;
+}
+
+static void
+purple_constructor_load_pobj_unsync_cb(GVariant *vpobject, gpointer user_data)
+{
+	purple_constructor_load_pobj_unsync(vpobject, *(GType*)user_data);
+}
+
+void
+purple_constructor_load_pobjs_unsync(GVariant *pobjects, GType type)
+{
+	purple_constructor_foreach_pobjects
+	  (pobjects, purple_constructor_load_pobj_unsync_cb, &type);
+}
+
+gpointer
+purple_constructor_load_pobj_unsync(GVariant *vpobject, GType type)
+{
 	PurpleObject *pobject;
 	gpointer *obj;
 	GVariant *props;
@@ -269,19 +343,19 @@ load_pobject(GVariant *vpobject, GType t
 	return obj;
 }
 
-static void
-load_pobjects(GVariant *box, GType type)
+void
+purple_constructor_foreach_pobjects(GVariant *box,
+                                    PurpleConstructorForeachFunc treatment,
+                                    gpointer user_data)
 {
 	GVariantIter iter;
 	GVariant *array, *pobject;
-	gpointer obj;
 
 	g_variant_get(box, "(@a(oa(sv)))", &array);
 
 	g_variant_iter_init(&iter, array);
 	while ((pobject = g_variant_iter_next_value(&iter)) != NULL) {
-		obj = load_pobject(pobject, type);
-		purple_object_set_synchronized(PURPLE_OBJECT(obj), TRUE);
+		treatment(pobject, user_data);
 		g_variant_unref(pobject);
 	}
 	g_variant_unref(array);
@@ -319,7 +393,8 @@ purple_constructor_load_all_pobj(const c
 	                              purple_constructor_interface_info.name,
 	                              method);
 	if (ret) {
-		load_pobjects(ret, type);
+		purple_constructor_foreach_pobjects
+		  (ret, purple_constructor_load_pobj_cb, &type);
 		g_variant_unref(ret);
 	}
 }
@@ -392,8 +467,8 @@ purple_account_new_RPC(const char *usern
 	ret = purple_object_dbus_call(PURPLE_OBJECT(con), "NewAccount",
 	                              username, protocol_id);
 	if (ret) {
-		account = PURPLE_ACCOUNT(load_pobject(ret, "PurpleAccount"));
-		purple_object_set_synchronized(PURPLE_OBJECT(account), TRUE);
+		account = PURPLE_ACCOUNT
+		       (purple_constructor_load_pobj(ret, PURPLE_TYPE_ACCOUNT));
 	}
 	return account;
 }
============================================================
--- libpurple/dbus/constructor.h	091181571026c112907fba635c79837a8d65e66f
+++ libpurple/dbus/constructor.h	39f24ed4cea780acc66489e57db1b2379f87ff8b
@@ -43,6 +43,16 @@ G_BEGIN_DECLS
 G_BEGIN_DECLS
 
 /**
+ * A function type suitable for purple_constructor_foreach_pobjects().
+ * 
+ * @param pobj An "(oa(sv))" type GVariant defining the object. See
+ *        purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @param user_data user_data passed to purple_constructor_foreach_pobjects().
+ */
+typedef void (*PurpleConstructorForeachFunc)(GVariant *pobj,
+                                             gpointer  user_data);
+
+/**
  * Creates the PurpleConstructor singleton.
  */
 void purple_constructor_do_init(void);
@@ -87,6 +97,85 @@ GVariant *purple_constructor_pack_pobjec
 GVariant *purple_constructor_pack_pobject(gpointer pobject);
 
 /**
+ * Call a given function on each object of an array of objects.
+ *
+ * @param pobjects A GVariant of packed PurpleObjects. Its type must be either
+ *        "a(oa(sv))" or "(a(oa(sv)))". See purple_constructor_load_pobj() for
+ *        an explanation of the "oa(sv)" part.
+ * @param treatment A function to call on each object in the pobjects array.
+ * @param user_data User data passed to the treatment function.
+ */
+void purple_constructor_foreach_pobjects(GVariant *pobjects,
+                                         PurpleConstructorForeachFunc treatment,
+                                         gpointer user_data);
+
+/**
+ * Unpacks a PurpleObject and instantiate a GObject of type type_name
+ * with the properties and D-Bus name as defined in pobject. Also runs
+ * the purple_object_dbus_init() function on this newly-created object.
+ *
+ * @param pobject An "(oa(sv))" GVariant defining a PurpleObject. "o" is the
+ *        object path name and a(sv) is an array of properties (s is the name
+ *        of the property, v is its value).
+ * @param type A GType of a class derived from PurpleObject.
+ * @returns A newly-created object of type type_name, or NULL on error.
+ */
+gpointer purple_constructor_load_pobj(GVariant *pobject, GType type);
+
+/**
+ * Do the same as purple_constructor_load_pobj(), except that it opers
+ * an array of objects.
+ *
+ * @param pobject An "a(oa(sv))" GVariant defining an array of PurpleObjects.
+ *        See purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @param type A GType of a class derived from PurpleObject.
+ */
+void purple_constructor_load_pobjs(GVariant *pobjects, GType type);
+
+/**
+ * Does the same as purple_constructor_load_pobj() but without marking the
+ * created object as "synchronized", so that further operations may be done
+ * on the object before it's D-Bus-ready.
+ *
+ * @param pobject An "(oa(sv))" GVariant defining a PurpleObject. See
+ *        purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @param type A GType of a class derived from PurpleObject.
+ * @returns A newly-created object of type type_name, or NULL on error.
+ */
+gpointer purple_constructor_load_pobj_unsync(GVariant *pobject, GType type);
+
+/**
+ * Do the same as purple_constructor_load_pobj_unsync(), except that it opers
+ * an array of objects.
+ *
+ * @param pobject An "a(oa(sv))" GVariant defining an array of PurpleObjects.
+ *        See purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @param type A GType of a class derived from PurpleObject.
+ */
+void purple_constructor_load_pobjs_unsync(GVariant *pobjects, GType type);
+
+/**
+ * This function expects the object defined in pobject to be already
+ * instantiated as a PurpleObject-derived class. Sets all the "o"
+ * GVariant-typed properties according to pobject.
+ *
+ * @param pobject An "(oa(sv))" GVariant defining a PurpleObject. See
+ *        purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @returns The already instantiated object, if found, or NULL.
+ */
+void purple_constructor_reload_pobj_objpath_props(GVariant *vpobject);
+
+/**
+ * Do the same as purple_constructor_reload_pobj_objpath_props(), except that
+ * it opers an array of objects.
+ *
+ * @param pobject An "a(oa(sv))" GVariant defining an array of PurpleObjects.
+ *        See purple_constructor_load_pobj() for an explanation of "oa(sv)".
+ * @param type A GType of a class derived from PurpleObject.
+ */
+void purple_constructor_reload_pobjs_objpath_props(GVariant *pobjects);
+
+/**
  * What's get actually called when you call NewAccount over dbus.
  */
 gboolean DBUS_purple_constructor_new_account(PurpleConstructor *con, gchar* username, gchar* protocol_id, gchar **account_path, GError** error);


More information about the Commits mailing list