soc.2009.telepathy: 3ea74520: Copy parameters to AccountManager on mod...

sttwister at soc.pidgin.im sttwister at soc.pidgin.im
Mon Jul 20 17:15:38 EDT 2009


-----------------------------------------------------------------
Revision: 3ea745207f2f9b475b512dfa3303e6503ff25131
Ancestor: 0c7eaf54b16010da40a7bdad96e7268718cf0f6b
Author: sttwister at soc.pidgin.im
Date: 2009-07-20T21:12:31
Branch: im.pidgin.soc.2009.telepathy
URL: http://d.pidgin.im/viewmtn/revision/info/3ea745207f2f9b475b512dfa3303e6503ff25131

Modified files:
        libpurple/protocols/telepathy/telepathy.c
        libpurple/protocols/telepathy/telepathy_account.c
        libpurple/protocols/telepathy/telepathy_account.h
        libpurple/protocols/telepathy/telepathy_utils.h

ChangeLog: 

Copy parameters to AccountManager on modifying or destroying accounts

-------------- next part --------------
============================================================
--- libpurple/protocols/telepathy/telepathy.c	35409b192423a2371870081158b779f5df0bd5c4
+++ libpurple/protocols/telepathy/telepathy.c	6b10c21a53cef831e34b4fa9965a2aaf177d8665
@@ -53,14 +53,6 @@ static TpAccountManager *account_Manager
 static gchar *module_path;
 static TpAccountManager *account_Manager;
 
-typedef struct
-{
-	TpConnectionManager *cm;
-	TpConnectionManagerProtocol *protocol;
-	PurplePlugin *plugin;
-
-} telepathy_data;
-
 static gboolean
 telepathy_plugin_load(PurplePlugin *plugin)
 {
@@ -205,8 +197,6 @@ telepathy_login(PurpleAccount *acct)
 static void
 telepathy_login(PurpleAccount *acct)
 {
-	purple_debug_info("telepathy", "Object path: %s\n",
-			purple_account_get_string(acct, "objpath", NULL));
 	PurpleConnection *gc = purple_account_get_connection(acct);
 
 	PurplePlugin* plugin = gc->prpl;
============================================================
--- libpurple/protocols/telepathy/telepathy_account.c	19d119018cffc8dc50eb0f2694b9a6066f9dec83
+++ libpurple/protocols/telepathy/telepathy_account.c	824be8dcba67f59dcab01ab1feab5e498d189f7d
@@ -23,13 +23,128 @@
 #include "telepathy_utils.h"
 
 #include <telepathy-glib/account.h>
+#include <telepathy-glib/connection-manager.h>
 #include <telepathy-glib/dbus.h>
 #include <telepathy-glib/interfaces.h>
+#include <telepathy-glib/util.h>
 
 #include "account.h"
 #include "debug.h"
+#include "../../../pidgin/gtkaccount.h"
 
 static void
+update_parameters_cb (TpAccount *proxy,
+                      const gchar **out_Reconnect_Required,
+                      const GError *error,
+                      gpointer user_data,
+                      GObject *weak_object)
+{
+	if (error != NULL)
+	{
+		purple_debug_error("telepathy", "UpdateParameters error: %s\n",
+				error->message);
+		return;
+	}
+
+	purple_debug_info("telepathy", "UpdateParameters succeeded!\n");
+}
+
+static void
+save_account_parameters (telepathy_account *account_data,
+                         TpConnectionManagerParam *params)
+{
+	PurpleAccount *account = account_data->account;
+
+	int i;
+	
+	GHashTable *params_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
+			NULL, (GDestroyNotify) tp_g_value_slice_free);
+
+	GPtrArray *unset = g_ptr_array_new();
+
+	/* Loop over all parameters */
+	for (i = 0; params[i].name != NULL; ++i)
+	{
+		gchar *name = params[i].name;
+		const gchar *signature = params[i].dbus_signature;
+
+		/* Some protocols might not require username or password, so check them before */
+		if (g_strcmp0(name, "account") == 0 && account->username != NULL)
+			tp_asv_set_string(params_hash, "account", account->username);
+		if (g_strcmp0(name, "password") == 0 && account->password != NULL)
+			tp_asv_set_string(params_hash, "password", account->password);
+
+		/* Account and password are handled in particular */
+		if (g_strcmp0(name, "account") != 0 && g_strcmp0(name, "password"))
+		{
+			/* Check the type of the parameter and update the hash table,
+			 * or add it to the unset array if it's default
+			 */
+			if (g_strcmp0(signature, "s") == 0)
+			{
+				if (g_strcmp0(purple_account_get_string(account, name, ""), ""))
+					tp_asv_set_string(params_hash, name,
+							purple_account_get_string(account, name, ""));
+				else
+					g_ptr_array_add(unset, name);
+			}
+			else if (g_strcmp0(signature, "n") == 0)
+			{
+				if (purple_account_get_int(account, name, 0) != 0)
+					tp_asv_set_int32(params_hash, name,
+							purple_account_get_int(account, name, 0));
+				else
+					g_ptr_array_add(unset, name);
+			}
+			else if (g_strcmp0(signature, "i") == 0)
+			{
+				if (purple_account_get_int(account, name, 0) != 0)
+					tp_asv_set_int32(params_hash, name,
+							purple_account_get_int(account, name, 0));
+				else
+					g_ptr_array_add(unset, name);
+			}
+			else if (g_strcmp0(signature, "u") == 0)
+			{
+				if (purple_account_get_int(account, name, 0) != 0)
+					tp_asv_set_uint32(params_hash, name,
+							purple_account_get_int(account, name, 0));
+				else
+					g_ptr_array_add(unset, name);
+			}
+			else if (g_strcmp0(signature, "q") == 0)
+			{
+				if (purple_account_get_int(account, name, 0) != 0)
+					tp_asv_set_uint32(params_hash, name,
+							purple_account_get_int(account, name, 0));
+				else
+					g_ptr_array_add(unset, name);
+			}
+			else if (g_strcmp0(signature, "b") == 0)
+			{
+				tp_asv_set_boolean(params_hash, name,
+						purple_account_get_bool(account, name, FALSE));
+			}
+			else
+				purple_debug_warning("telepathy", "Unknown signature \"%s\" for"
+						" \"%s\"\n", signature, name);
+		}
+	}
+
+	g_ptr_array_add(unset, NULL);
+
+	/* Upload the new parameters to AccountManager */
+	tp_cli_account_call_update_parameters(account_data->tp_account, -1,
+			params_hash, (const gchar **)unset->pdata,
+			update_parameters_cb, account_data,
+			NULL, NULL);
+
+	g_ptr_array_free(unset, TRUE);
+
+	g_hash_table_destroy(params_hash);
+}
+
+static void
 set_account_parameters (PurpleAccount *account,
                         GHashTable *parameters)
 {
@@ -87,7 +202,7 @@ get_account_properties_cb (TpProxy *prox
                            gpointer user_data,
                            GObject *weak_object)
 {
-	gchar *obj_Path = user_data;
+	telepathy_account *account_data = user_data;
 
 	GHashTable *parameters;
 	const gchar *display_name;
@@ -118,13 +233,16 @@ get_account_properties_cb (TpProxy *prox
 	/* Parse the object path to find the connection manager and the protocol.
 	 * The object path looks like "/org/freedesktop/Telepathy/Account/cm/proto/acct"
 	 */
-	tokens = g_strsplit(obj_Path, "/", 8);
+	tokens = g_strsplit(account_data->obj_Path, "/", 8);
 
 	cm = tokens[5];
 	proto = tokens[6];
 
 	protocol_id = g_strdup_printf("%s-%s-%s", TELEPATHY_ID, cm, proto);
 	
+	account_data->cm = g_strdup(cm);
+	account_data->protocol = g_strdup(proto);
+
 	g_strfreev(tokens);
 
 	/* Check if the account already exists in purple-land. If not, we need to manually
@@ -147,12 +265,71 @@ get_account_properties_cb (TpProxy *prox
 				display_name);
 	}
 
-	purple_account_set_string(account, "objpath", obj_Path);
+	account_data->account = account;
 
+	purple_account_set_string(account, "objpath", account_data->obj_Path);
+	purple_account_set_int(account, "tp_account_data", (int)account_data);
+
 	/* Sync the parameters with PurpleAccount's parameters */
 	set_account_parameters(account, parameters);
 }
 
+static void
+account_modified_cb (PurpleAccount *account,
+                     gpointer user_data)
+{
+	telepathy_account *account_data = (telepathy_account*)purple_account_get_int(
+			account, "tp_account_data", 0);
+
+	PurplePlugin *plugin;
+	telepathy_data *data;
+
+	if (account_data == NULL)
+	{
+		/* This doesn't seem to be a prpl-telepathy account, so there's nothing to do */
+		return;
+	}
+
+	/* We need to find the plugin of this account in order to have access to the
+	 * Connection Manager proxy and the protocol parameters.
+	 */
+	plugin = purple_find_prpl(g_strdup_printf("%s-%s-%s", TELEPATHY_ID, 
+			account_data->cm, account_data->protocol));
+
+	if (plugin == NULL)
+	{
+		purple_debug_info("telepathy", "There's no plugin for modifiying PurpleAccount\n");
+		return;
+	}
+
+	data = plugin->extra;
+
+	/* Save the parameters in AccountManager */
+	save_account_parameters(account_data, data->protocol->params);
+}
+
+static void
+account_destroying_cb (PurpleAccount *account,
+                       gpointer user_data)
+{
+	telepathy_account *account_data;
+
+	/* Save the changes to AccountManager and destroy the alocated struct */
+	account_modified_cb(account, user_data);
+	
+	account_data = (telepathy_account*)purple_account_get_int(
+			account, "tp_account_data", 0);
+
+	if (account_data != NULL)
+	{
+		g_free(account_data->obj_Path);
+		g_free(account_data->cm);
+		g_free(account_data->protocol);
+
+		g_free(account_data);
+	}
+}
+
 void
 get_valid_accounts_cb (TpProxy *proxy,
                        const GValue *out_Value,
@@ -191,6 +368,7 @@ get_valid_accounts_cb (TpProxy *proxy,
 	{
 		gchar *obj_Path = g_ptr_array_index(accounts, i);
 		TpAccount *account;
+		telepathy_account *account_data;
 
 		purple_debug_info("telepathy", "  %s\n", obj_Path);
 
@@ -205,9 +383,29 @@ get_valid_accounts_cb (TpProxy *proxy,
 			continue;
 		}
 
+		account_data = g_new0(telepathy_account, 1);
+
+		account_data->tp_account = account;
+		account_data->obj_Path = g_strdup(obj_Path);
+
 		/* Get all properties and sync the accounts with libpurple */
 		tp_cli_dbus_properties_call_get_all(account, -1, TP_IFACE_ACCOUNT,
-				get_account_properties_cb, g_strdup(obj_Path), g_free, NULL);
+				get_account_properties_cb, account_data, NULL, NULL);
+
+		/* FIXME: Is purple_accounts_get_handle() the right one to pass as the handle?
+		 * I honestly have no idea, seems to fail with a NULL :|
+		 *
+		 * FIXME: account-modified is Pidgin-dependent
+		 */
+		purple_signal_connect(pidgin_account_get_handle(), "account-modified",
+				purple_accounts_get_handle(),
+				PURPLE_CALLBACK(account_modified_cb),
+				NULL);
+
+		purple_signal_connect(purple_accounts_get_handle(), "account-destroying",
+				purple_accounts_get_handle(),
+				PURPLE_CALLBACK(account_destroying_cb),
+				NULL);
 	}
 
 	if (daemon)
============================================================
--- libpurple/protocols/telepathy/telepathy_account.h	baaca98396b3cb552a1936c8cce21f744e37e610
+++ libpurple/protocols/telepathy/telepathy_account.h	211e13ff966bb57b74bacf6b9b8f4e14ebeaf124
@@ -21,8 +21,20 @@
 #ifndef _TELEPATHY_ACCOUNT_H_
 #define _TELEPATHY_ACCOUNT_H_
 
-#include <telepathy-glib/proxy.h>
+#include <telepathy-glib/account.h>
 
+#include "account.h"
+
+typedef struct
+{
+	TpAccount *tp_account;
+	PurpleAccount *account;	
+
+	gchar *obj_Path;
+	gchar *cm, *protocol;
+
+} telepathy_account;
+
 void
 get_valid_accounts_cb (TpProxy *proxy,
                        const GValue *out_Value,
============================================================
--- libpurple/protocols/telepathy/telepathy_utils.h	e1261c63457926242df522730e260bb28e708e97
+++ libpurple/protocols/telepathy/telepathy_utils.h	8526714ef9541247c521454c04f3844e72df9ede
@@ -23,6 +23,8 @@
 
 #include <glib.h>
 
+#include <telepathy-glib/connection-manager.h>
+
 #include "internal.h"
 
 #include "status.h"
@@ -32,6 +34,14 @@ typedef struct
 
 typedef struct
 {
+	TpConnectionManager *cm;
+	TpConnectionManagerProtocol *protocol;
+	PurplePlugin *plugin;
+
+} telepathy_data;
+
+typedef struct
+{
 	const gchar *telepathy_name;
 	const gchar *dbus_type;
 	const gchar *human_name;


More information about the Commits mailing list