/srv/mercurial-server/detachablepurple: f2d7bbfe9f95: Added gene...

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


Changeset: f2d7bbfe9f9586e59da728b560e7b1767bd4bba7
Author:	 Gilles Bedel <gillux at cpw.pidgin.im>
Date:	 2012-05-15 01:53 +0000
Branch:	 cpw.gillux.detachablepurple
URL: http://hg.pidgin.im/srv/mercurial-server/detachablepurple/rev/f2d7bbfe9f95

Description:

Added generic functions to load any PurpleObject or array of PurpleObjects.
Also updated the accounts loading function to take advantage of this.
Renamed the pack_account() function to pack_pobject(), as this one was
actually already generic.

diffstat:

 libpurple/dbus/constructor.c |  92 ++++++++++++++++++++++++-------------------
 1 files changed, 51 insertions(+), 41 deletions(-)

diffs (125 lines):

diff --git a/libpurple/dbus/constructor.c b/libpurple/dbus/constructor.c
--- a/libpurple/dbus/constructor.c
+++ b/libpurple/dbus/constructor.c
@@ -262,35 +262,52 @@
 }
 
 static GVariant*
-pack_account(PurpleAccount *account)
+pack_pobject(gpointer obj)
 {
-	PurpleObject *pobj = PURPLE_OBJECT(account);
+	PurpleObject *pobj = PURPLE_OBJECT(obj);
 	return g_variant_new("(o at a(sv))",
 	                     purple_object_get_dbus_path(pobj),
 	                     pack_pobject_properties(pobj));
 }
 
-/* Unpacks a GVariant-packed account and creates local PurpleAccounts
- * that will act as proxy objects. */
+static gpointer
+load_pobject(GVariant *vpobject, const char *type_name)
+{
+	PurpleObject *pobject;
+	gpointer *obj;
+	GVariant *props;
+	const gchar *dbus_path;
+
+	g_variant_get(vpobject, "(o at a(sv))", &dbus_path, &props);
+
+	obj = purple_constructor_new_proxy_object(type_name, props);
+
+	pobject = PURPLE_OBJECT(obj);
+	purple_object_set_dbus_path(pobject, dbus_path);
+	purple_object_dbus_init(obj);
+
+	g_variant_unref(props);
+	g_free((gchar*)dbus_path);
+
+	return obj;
+}
+
 static void
-load_account(GVariant *account)
+load_pobjects(GVariant *box, const char *type_name)
 {
-	PurpleAccount *paccount;
-	GVariant *props;
-	gchar *dbus_path;
+	GVariantIter iter;
+	GVariant *array, *pobject;
+	gpointer obj;
 
-	g_variant_get(account, "(o at a(sv))", &dbus_path, &props);
+	g_variant_get(box, "(@a(oa(sv)))", &array);
 
-	paccount = purple_constructor_new_proxy_object("PurpleAccount", props);
-	g_variant_unref(props);
-
-	purple_object_set_dbus_path(PURPLE_OBJECT(paccount), dbus_path);
-	g_free(dbus_path);
-
-	purple_account_dbus_init(paccount);
-	/* This signal will add our new account in the account list. */
-	g_signal_emit_by_name(G_OBJECT(paccount), "new");
-	purple_object_set_synchronized(PURPLE_OBJECT(paccount), TRUE);	
+	g_variant_iter_init(&iter, array);
+	while ((pobject = g_variant_iter_next_value(&iter)) != NULL) {
+		obj = load_pobject(pobject, type_name);
+		purple_object_set_synchronized(PURPLE_OBJECT(obj), TRUE);
+		g_variant_unref(pobject);
+	}
+	g_variant_unref(array);
 }
 
 /**
@@ -308,34 +325,27 @@
 	g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
 	while (all) {
 		PurpleAccount *account = all->data;
-		g_variant_builder_add_value(&builder, pack_account(account));
+		g_variant_builder_add_value(&builder, pack_pobject(account));
 		all = all->next;
 	}
 	return g_variant_new("(@a(oa(sv)))", g_variant_builder_end(&builder));
 }
 
-/**
- * This is what a client calls when it wants to call the dbus method
- * GetAllAccounts. It unpacks the gdbus formatted data and creates proxy
- * PurpleAccounts.
- */
+static void
+purple_constructor_load_all_pobj(const char *method, const char *type)
+{
+	PurpleConstructor* con = purple_constructor_get_instance();
+	GVariant *ret;
+
+	ret = purple_object_dbus_call(PURPLE_OBJECT(con), method);
+	if (ret) {
+		load_pobjects(ret, type);
+		g_variant_unref(ret);
+	}
+}
+
 void
 purple_accounts_get_all_RPC(void)
 {
-	PurpleConstructor* con = purple_constructor_get_instance();
-	GVariant *ret, *accounts, *account;
-	GVariantIter iter;
-
-	/* Download all the accounts. */
-	ret = purple_object_dbus_call(PURPLE_OBJECT(con), "GetAllAccounts");
-	g_variant_get(ret, "(@a(oa(sv)))", &accounts);
-
-	/* Unpack all this data. */
-	g_variant_iter_init(&iter, accounts);
-	while ((account = g_variant_iter_next_value(&iter)) != NULL) {
-		load_account(account);
-		g_variant_unref(account);
-	}
-	purple_debug_info("dbus", "Loaded %d accounts.\n", g_variant_n_children(accounts));
-	g_variant_unref(accounts);
+	purple_constructor_load_all_pobj("GetAllAccounts", "PurpleAccount");
 }



More information about the Commits mailing list