diff options
author | Sjoerd Simons <sjoerd.simons@collabora.co.uk> | 2009-07-08 18:35:43 +0800 |
---|---|---|
committer | Sjoerd Simons <sjoerd.simons@collabora.co.uk> | 2009-07-08 23:17:42 +0800 |
commit | 96fad6a1159582a3e8792dd3308ce1fbeb178234 (patch) | |
tree | 8890b1c4f420b2053af80d586b0a112f772c1097 /libempathy/empathy-account.c | |
parent | e98ae9bde97e290c0f54593af1cb30905bb9055e (diff) | |
download | gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar.gz gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar.bz2 gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar.lz gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar.xz gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.tar.zst gsoc2013-empathy-96fad6a1159582a3e8792dd3308ce1fbeb178234.zip |
Initial port of Account and AccountManager to MC5
Diffstat (limited to 'libempathy/empathy-account.c')
-rw-r--r-- | libempathy/empathy-account.c | 368 |
1 files changed, 300 insertions, 68 deletions
diff --git a/libempathy/empathy-account.c b/libempathy/empathy-account.c index c6a2184f9..010990d7a 100644 --- a/libempathy/empathy-account.c +++ b/libempathy/empathy-account.c @@ -18,20 +18,25 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <telepathy-glib/enums.h> +#include <telepathy-glib/dbus.h> +#include <telepathy-glib/account.h> +#include <telepathy-glib/gtypes.h> +#include <telepathy-glib/util.h> #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT #include <libempathy/empathy-debug.h> #include "empathy-account.h" -#include "empathy-account-priv.h" #include "empathy-utils.h" #include "empathy-marshal.h" +#define UNIQUE_NAME_PREFIX "/org/freedesktop/Telepathy/Account/" + /* signals */ enum { STATUS_CHANGED, @@ -45,10 +50,12 @@ static guint signals[LAST_SIGNAL]; enum { PROP_ENABLED = 1, PROP_PRESENCE, + PROP_READY, PROP_CONNECTION_STATUS, PROP_CONNECTION_STATUS_REASON, PROP_CONNECTION, PROP_UNIQUE_NAME, + PROP_DBUS_DAEMON, PROP_DISPLAY_NAME }; @@ -69,19 +76,28 @@ struct _EmpathyAccountPriv TpConnectionPresenceType presence; gboolean enabled; + gboolean valid; + gboolean ready; /* Timestamp when the connection got connected in seconds since the epoch */ glong connect_time; - McAccount *mc_account; - McProfile *mc_profile; - gchar *cm_name; gchar *proto_name; gchar *icon_name; + + gchar *unique_name; + gchar *display_name; + TpDBusDaemon *dbus; + + TpAccount *account; + GHashTable *parameters; }; #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccount) +static void _empathy_account_set_connection (EmpathyAccount *account, + TpConnection *connection); + static void empathy_account_init (EmpathyAccount *obj) { @@ -96,6 +112,32 @@ empathy_account_init (EmpathyAccount *obj) } static void +empathy_account_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyAccount *account = EMPATHY_ACCOUNT (object); + EmpathyAccountPriv *priv = GET_PRIV (account); + + switch (prop_id) + { + case PROP_ENABLED: + empathy_account_set_enabled (account, g_value_get_boolean (value)); + break; + case PROP_UNIQUE_NAME: + priv->unique_name = g_value_dup_string (value); + break; + case PROP_DBUS_DAEMON: + priv->dbus = g_value_get_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void empathy_account_get_property (GObject *object, guint prop_id, GValue *value, @@ -109,6 +151,9 @@ empathy_account_get_property (GObject *object, case PROP_ENABLED: g_value_set_boolean (value, priv->enabled); break; + case PROP_READY: + g_value_set_boolean (value, priv->enabled); + break; case PROP_PRESENCE: g_value_set_uint (value, priv->presence); break; @@ -130,12 +175,208 @@ empathy_account_get_property (GObject *object, g_value_set_string (value, empathy_account_get_display_name (account)); break; + case PROP_DBUS_DAEMON: + g_value_set_string (value, + empathy_account_get_display_name (account)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } +static void +empathy_account_update (EmpathyAccount *account, GHashTable *properties) +{ + EmpathyAccountPriv *priv = GET_PRIV (account); + const gchar *conn_path; + GValueArray *arr; + TpConnectionStatus old_s = priv->status; + TpConnectionPresenceType old_p = priv->presence; + + if (g_hash_table_lookup (properties, "ConnectionStatus") != NULL) + priv->status = tp_asv_get_int32 (properties, "ConnectionStatus", NULL); + + if (g_hash_table_lookup (properties, "ConnectionStatusReason") != NULL) + priv->reason = tp_asv_get_int32 (properties, + "ConnectionStatusReason", NULL); + + if (g_hash_table_lookup (properties, "CurrentPresence") != NULL) + { + arr = tp_asv_get_boxed (properties, "CurrentPresence", + TP_STRUCT_TYPE_SIMPLE_PRESENCE); + priv->presence = g_value_get_uint (g_value_array_get_nth (arr, 0)); + } + + if (g_hash_table_lookup (properties, "DisplayName") != NULL) + priv->display_name = + g_strdup (tp_asv_get_string (properties, "DisplayName")); + + if (g_hash_table_lookup (properties, "Enabled") != NULL) + empathy_account_set_enabled (account, + tp_asv_get_boolean (properties, "Enabled", NULL)); + + if (g_hash_table_lookup (properties, "Valid") != NULL) + priv->valid = tp_asv_get_boolean (properties, "Valid", NULL); + + if (g_hash_table_lookup (properties, "Parameters") != NULL) + { + GHashTable *parameters; + + parameters = tp_asv_get_boxed (properties, "Parameters", + TP_HASH_TYPE_STRING_VARIANT_MAP); + + priv->parameters = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP, + parameters); + } + + if (!priv->ready) + { + priv->ready = TRUE; + g_object_notify (G_OBJECT (account), "ready"); + } + + if (priv->status != old_s) + { + if (priv->status == TP_CONNECTION_STATUS_CONNECTED) + { + GTimeVal val; + g_get_current_time (&val); + + priv->connect_time = val.tv_sec; + } + + g_signal_emit (account, signals[STATUS_CHANGED], 0, + old_s, priv->status, priv->reason); + + g_object_notify (G_OBJECT (account), "status"); + } + + if (priv->presence != old_p) + { + g_signal_emit (account, signals[PRESENCE_CHANGED], 0, + old_p, priv->presence); + g_object_notify (G_OBJECT (account), "presence"); + } + + if (g_hash_table_lookup (properties, "Connection") != NULL) + { + conn_path = tp_asv_get_object_path (properties, "Connection"); + + if (tp_strdiff (conn_path, "/") && priv->connection == NULL) + { + TpConnection *conn; + GError *error = NULL; + conn = tp_connection_new (priv->dbus, NULL, conn_path, &error); + + if (conn == NULL) + { + DEBUG ("Failed to create a new TpConnection: %s", + error->message); + g_error_free (error); + } + + _empathy_account_set_connection (account, conn); + } + } +} + +static void +empathy_account_properties_changed (TpAccount *proxy, + GHashTable *properties, + gpointer user_data, + GObject *weak_object) +{ + EmpathyAccount *account = EMPATHY_ACCOUNT (weak_object); + EmpathyAccountPriv *priv = GET_PRIV (account); + + if (!priv->ready) + return; + + empathy_account_update (account, properties); +} + +static void +empathy_account_got_all_cb (TpProxy *proxy, + GHashTable *properties, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + EmpathyAccount *account = EMPATHY_ACCOUNT (weak_object); + + if (error != NULL) + { + printf ("Unhappy\n"); + return; + } + + empathy_account_update (account, properties); +} + +static gboolean +empathy_account_parse_unique_name (const gchar *bus_name, + gchar **protocol, gchar **manager) +{ + const gchar *proto, *proto_end; + const gchar *cm, *cm_end; + + g_return_val_if_fail ( + g_str_has_prefix (bus_name, UNIQUE_NAME_PREFIX), FALSE); + + cm = bus_name + strlen (UNIQUE_NAME_PREFIX); + + for (cm_end = cm; *cm_end != '/' && *cm_end != '\0'; cm_end++) + /* pass */; + + if (*cm_end == '\0') + return FALSE; + + if (cm_end == '\0') + return FALSE; + + proto = cm_end + 1; + + for (proto_end = proto; *proto_end != '/' && *proto_end != '\0'; proto_end++) + /* pass */; + + if (*proto_end == '\0') + return FALSE; + + if (protocol != NULL) + *protocol = g_strndup (proto, proto_end - proto); + + if (manager != NULL) + *manager = g_strndup (cm, cm_end - cm); + + return TRUE; +} + +static void +empathy_account_constructed (GObject *object) +{ + EmpathyAccount *account = EMPATHY_ACCOUNT (object); + EmpathyAccountPriv *priv = GET_PRIV (account); + + priv->account = tp_account_new (priv->dbus, priv->unique_name, NULL); + + empathy_account_parse_unique_name (priv->unique_name, + &(priv->proto_name), &(priv->cm_name)); + + priv->icon_name = g_strdup_printf ("im-%s", priv->proto_name); + + tp_cli_account_connect_to_account_property_changed (priv->account, + empathy_account_properties_changed, + NULL, NULL, object, NULL); + + tp_cli_dbus_properties_call_get_all (priv->account, -1, + TP_IFACE_ACCOUNT, + empathy_account_got_all_cb, + NULL, + NULL, + G_OBJECT (account)); +} + static void empathy_account_dispose (GObject *object); static void empathy_account_finalize (GObject *object); @@ -147,15 +388,24 @@ empathy_account_class_init (EmpathyAccountClass *empathy_account_class) g_type_class_add_private (empathy_account_class, sizeof (EmpathyAccountPriv)); + object_class->set_property = empathy_account_set_property; object_class->get_property = empathy_account_get_property; object_class->dispose = empathy_account_dispose; object_class->finalize = empathy_account_finalize; + object_class->constructed = empathy_account_constructed; g_object_class_install_property (object_class, PROP_ENABLED, g_param_spec_boolean ("enabled", "Enabled", "Whether this account is enabled or not", FALSE, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, PROP_READY, + g_param_spec_boolean ("ready", + "Ready", + "Whether this account is ready to be used", + FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); g_object_class_install_property (object_class, PROP_PRESENCE, @@ -197,7 +447,14 @@ empathy_account_class_init (EmpathyAccountClass *empathy_account_class) "UniqueName", "The accounts unique name", NULL, - G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, PROP_DBUS_DAEMON, + g_param_spec_object ("dbus-daemon", + "dbus-daemon", + "The Tp Dbus daemon on which this account exists", + TP_TYPE_DBUS_DAEMON, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (object_class, PROP_DISPLAY_NAME, g_param_spec_string ("display-name", @@ -241,10 +498,6 @@ empathy_account_dispose (GObject *object) g_object_unref (priv->connection); priv->connection = NULL; - if (priv->mc_profile != NULL) - g_object_unref (priv->mc_profile); - priv->mc_profile = NULL; - /* release any references held by the object here */ if (G_OBJECT_CLASS (empathy_account_parent_class)->dispose != NULL) G_OBJECT_CLASS (empathy_account_parent_class)->dispose (object); @@ -258,6 +511,7 @@ empathy_account_finalize (GObject *object) g_free (priv->cm_name); g_free (priv->proto_name); g_free (priv->icon_name); + g_free (priv->display_name); /* free any data held directly by the object here */ if (G_OBJECT_CLASS (empathy_account_parent_class)->finalize != NULL) @@ -310,7 +564,7 @@ empathy_account_get_unique_name (EmpathyAccount *account) { EmpathyAccountPriv *priv = GET_PRIV (account); - return mc_account_get_unique_name (priv->mc_account); + return priv->unique_name; } /** @@ -324,15 +578,16 @@ empathy_account_get_display_name (EmpathyAccount *account) { EmpathyAccountPriv *priv = GET_PRIV (account); - return mc_account_get_display_name (priv->mc_account); + return priv->display_name; } gboolean empathy_account_is_valid (EmpathyAccount *account) { - EmpathyAccountPriv *priv = GET_PRIV (account); + //EmpathyAccountPriv *priv = GET_PRIV (account); - return mc_account_is_complete (priv->mc_account); + /* FIXME */ + return FALSE; } const gchar * @@ -359,14 +614,6 @@ empathy_account_get_icon_name (EmpathyAccount *account) return priv->icon_name; } -void -empathy_account_set_enabled (EmpathyAccount *account, gboolean enabled) -{ - EmpathyAccountPriv *priv = GET_PRIV (account); - - mc_account_set_enabled (priv->mc_account, enabled); -} - gboolean empathy_account_is_enabled (EmpathyAccount *account) { @@ -378,39 +625,33 @@ empathy_account_is_enabled (EmpathyAccount *account) void empathy_account_unset_param (EmpathyAccount *account, const gchar *param) { - EmpathyAccountPriv *priv = GET_PRIV (account); + //EmpathyAccountPriv *priv = GET_PRIV (account); - mc_account_unset_param (priv->mc_account, param); + //mc_account_unset_param (priv->mc_account, param); } -gchar * +const gchar * empathy_account_get_param_string (EmpathyAccount *account, const gchar *param) { EmpathyAccountPriv *priv = GET_PRIV (account); - gchar *value = NULL; - mc_account_get_param_string (priv->mc_account, param, &value); - return value; + return tp_asv_get_string (priv->parameters, param); } gint empathy_account_get_param_int (EmpathyAccount *account, const gchar *param) { EmpathyAccountPriv *priv = GET_PRIV (account); - int value; - mc_account_get_param_int (priv->mc_account, param, &value); - return value; + return tp_asv_get_int32 (priv->parameters, param, NULL); } gboolean empathy_account_get_param_boolean (EmpathyAccount *account, const gchar *param) { EmpathyAccountPriv *priv = GET_PRIV (account); - gboolean value; - mc_account_get_param_boolean (priv->mc_account, param, &value); - return value; + return tp_asv_get_boolean (priv->parameters, param, NULL); } void @@ -418,8 +659,8 @@ empathy_account_set_param_string (EmpathyAccount *account, const gchar *param, const gchar *value) { - EmpathyAccountPriv *priv = GET_PRIV (account); - mc_account_set_param_string (priv->mc_account, param, value); + //EmpathyAccountPriv *priv = GET_PRIV (account); + //mc_account_set_param_string (priv->mc_account, param, value); } void @@ -427,8 +668,8 @@ empathy_account_set_param_int (EmpathyAccount *account, const gchar *param, gint value) { - EmpathyAccountPriv *priv = GET_PRIV (account); - mc_account_set_param_int (priv->mc_account, param, value); + //EmpathyAccountPriv *priv = GET_PRIV (account); + //mc_account_set_param_int (priv->mc_account, param, value); } void @@ -436,18 +677,29 @@ empathy_account_set_param_boolean (EmpathyAccount *account, const gchar *param, gboolean value) { - EmpathyAccountPriv *priv = GET_PRIV (account); - mc_account_set_param_boolean (priv->mc_account, param, value); + //EmpathyAccountPriv *priv = GET_PRIV (account); + //mc_account_set_param_boolean (priv->mc_account, param, value); } void empathy_account_set_display_name (EmpathyAccount *account, const gchar *display_name) { - EmpathyAccountPriv *priv = GET_PRIV (account); - mc_account_set_display_name (priv->mc_account, display_name); + //EmpathyAccountPriv *priv = GET_PRIV (account); + //mc_account_set_display_name (priv->mc_account, display_name); } + +EmpathyAccount * +empathy_account_new (TpDBusDaemon *dbus, const gchar *unique_name) +{ + return EMPATHY_ACCOUNT (g_object_new (EMPATHY_TYPE_ACCOUNT, + "dbus-daemon", dbus, + "unique-name", unique_name, + NULL)); +} + +#if 0 EmpathyAccount * _empathy_account_new (McAccount *mc_account) { @@ -470,7 +722,6 @@ _empathy_account_new (McAccount *mc_account) priv->proto_name = g_strdup (mc_protocol_get_name (protocol)); priv->cm_name = g_strdup (mc_manager_get_unique_name (manager)); - priv->icon_name = g_strdup_printf ("im-%s", priv->proto_name); g_object_unref (protocol); g_object_unref (manager); @@ -493,30 +744,8 @@ _empathy_account_set_status (EmpathyAccount *account, priv->status = status; priv->presence = presence; - if (priv->status != old_s) - { - if (priv->status == TP_CONNECTION_STATUS_CONNECTED) - { - GTimeVal val; - g_get_current_time (&val); - - priv->connect_time = val.tv_sec; - } - - priv->reason = reason; - g_signal_emit (account, signals[STATUS_CHANGED], 0, - old_s, priv->status, reason); - - g_object_notify (G_OBJECT (account), "status"); - } - - if (priv->presence != old_p) - { - g_signal_emit (account, signals[PRESENCE_CHANGED], 0, - old_p, priv->presence); - g_object_notify (G_OBJECT (account), "presence"); - } } +#endif static void empathy_account_connection_ready_cb (TpConnection *connection, @@ -568,7 +797,7 @@ _empathy_account_connection_invalidated_cb (TpProxy *self, g_object_notify (G_OBJECT (account), "connection"); } -void +static void _empathy_account_set_connection (EmpathyAccount *account, TpConnection *connection) { @@ -599,6 +828,7 @@ _empathy_account_set_connection (EmpathyAccount *account, G_CALLBACK (_empathy_account_connection_invalidated_cb), account); + DEBUG ("Readying connection for %s", priv->unique_name); /* notify a change in the connection property when it's ready */ tp_connection_call_when_ready (priv->connection, empathy_account_connection_ready_cb, account); @@ -606,7 +836,7 @@ _empathy_account_set_connection (EmpathyAccount *account, } void -_empathy_account_set_enabled (EmpathyAccount *account, +empathy_account_set_enabled (EmpathyAccount *account, gboolean enabled) { EmpathyAccountPriv *priv = GET_PRIV (account); @@ -618,6 +848,7 @@ _empathy_account_set_enabled (EmpathyAccount *account, g_object_notify (G_OBJECT (account), "enabled"); } +#if 0 McAccount * _empathy_account_get_mc_account (EmpathyAccount *account) { @@ -625,3 +856,4 @@ _empathy_account_get_mc_account (EmpathyAccount *account) return priv->mc_account; } +#endif |