]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-contact.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-contact.c
index bad6ef470accf2c280492309e52686392224b20a..5383187c1b07bce2ef8327b86b8a9cf7c49f6d0d 100644 (file)
 
 #include <glib/gi18n-lib.h>
 
+#include <telepathy-glib/account-manager.h>
+#include <telepathy-glib/interfaces.h>
 #include <telepathy-glib/util.h>
 
+#include <telepathy-logger/log-manager.h>
+
+#include <folks/folks.h>
+#include <folks/folks-telepathy.h>
+
+#ifdef HAVE_GEOCLUE
+#include <geoclue/geoclue-geocode.h>
+#endif
+
 #include "empathy-contact.h"
-#include "empathy-account-manager.h"
+#include "empathy-individual-manager.h"
 #include "empathy-utils.h"
 #include "empathy-enum-types.h"
 #include "empathy-marshal.h"
+#include "empathy-location.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
 #include "empathy-debug.h"
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
 typedef struct {
   TpContact *tp_contact;
-  McAccount *account;
+  TpAccount *account;
+  FolksPersona *persona;
   gchar *id;
-  gchar *name;
+  gchar *alias;
   EmpathyAvatar *avatar;
   TpConnectionPresenceType presence;
-  gchar *presence_message;
   guint handle;
   EmpathyCapabilities capabilities;
   gboolean is_user;
@@ -52,8 +64,13 @@ typedef struct {
   /* Location is composed of string keys and GValues.
    * Example: a "city" key would have "Helsinki" as string GValue,
    *          a "latitude" would have 65.0 as double GValue.
+   *
+   * This is a super set of the location stored in TpContact as we can try add
+   * more fields by searching the address using geoclue.
    */
   GHashTable *location;
+  GHashTable *groups;
+  gchar **client_types;
 } EmpathyContactPriv;
 
 static void contact_finalize (GObject *object);
@@ -62,6 +79,25 @@ static void contact_get_property (GObject *object, guint param_id,
 static void contact_set_property (GObject *object, guint param_id,
     const GValue *value, GParamSpec *pspec);
 
+#ifdef HAVE_GEOCLUE
+static void update_geocode (EmpathyContact *contact);
+#endif
+
+static void empathy_contact_set_location (EmpathyContact *contact,
+    GHashTable *location);
+
+static void contact_set_client_types (EmpathyContact *contact,
+    const gchar * const *types);
+
+static void set_capabilities_from_tp_caps (EmpathyContact *self,
+    TpCapabilities *caps);
+
+static void contact_set_avatar (EmpathyContact *contact,
+    EmpathyAvatar *avatar);
+static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
+static gboolean contact_load_avatar_cache (EmpathyContact *contact,
+    const gchar *token);
+
 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
 
 enum
@@ -69,15 +105,17 @@ enum
   PROP_0,
   PROP_TP_CONTACT,
   PROP_ACCOUNT,
+  PROP_PERSONA,
   PROP_ID,
-  PROP_NAME,
+  PROP_ALIAS,
   PROP_AVATAR,
   PROP_PRESENCE,
   PROP_PRESENCE_MESSAGE,
   PROP_HANDLE,
   PROP_CAPABILITIES,
   PROP_IS_USER,
-  PROP_LOCATION
+  PROP_LOCATION,
+  PROP_CLIENT_TYPES
 };
 
 enum {
@@ -87,6 +125,9 @@ enum {
 
 static guint signals[LAST_SIGNAL];
 
+/* TpContact* -> EmpathyContact*, both borrowed ref */
+static GHashTable *contacts_table = NULL;
+
 static void
 tp_contact_notify_cb (TpContact *tp_contact,
                       GParamSpec *param,
@@ -96,7 +137,7 @@ tp_contact_notify_cb (TpContact *tp_contact,
 
   /* Forward property notifications */
   if (!tp_strdiff (param->name, "alias"))
-    g_object_notify (contact, "name");
+    g_object_notify (contact, "alias");
   else if (!tp_strdiff (param->name, "presence-type")) {
     TpConnectionPresenceType presence;
 
@@ -106,12 +147,41 @@ tp_contact_notify_cb (TpContact *tp_contact,
     priv->presence = presence;
     g_object_notify (contact, "presence");
   }
-  else if (!tp_strdiff (param->name, "presence-message"))
-    g_object_notify (contact, "presence-message");
   else if (!tp_strdiff (param->name, "identifier"))
     g_object_notify (contact, "id");
   else if (!tp_strdiff (param->name, "handle"))
     g_object_notify (contact, "handle");
+  else if (!tp_strdiff (param->name, "location"))
+    {
+      GHashTable *location;
+
+      location = tp_contact_get_location (tp_contact);
+      /* This will start a geoclue search to find the address if needed */
+      empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
+    }
+  else if (!tp_strdiff (param->name, "capabilities"))
+    {
+      set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
+          tp_contact_get_capabilities (tp_contact));
+    }
+  else if (!tp_strdiff (param->name, "avatar-file"))
+    {
+      contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
+    }
+  else if (!tp_strdiff (param->name, "client-types"))
+    {
+      contact_set_client_types (EMPATHY_CONTACT (contact),
+          tp_contact_get_client_types (tp_contact));
+    }
+}
+
+static void
+folks_persona_notify_cb (FolksPersona *folks_persona,
+                         GParamSpec *param,
+                         GObject *contact)
+{
+  if (!tp_strdiff (param->name, "presence-message"))
+    g_object_notify (contact, "presence-message");
 }
 
 static void
@@ -121,6 +191,7 @@ contact_dispose (GObject *object)
 
   if (priv->tp_contact)
     {
+      g_hash_table_remove (contacts_table, priv->tp_contact);
       g_signal_handlers_disconnect_by_func (priv->tp_contact,
           tp_contact_notify_cb, object);
       g_object_unref (priv->tp_contact);
@@ -131,9 +202,69 @@ contact_dispose (GObject *object)
     g_object_unref (priv->account);
   priv->account = NULL;
 
+  if (priv->persona)
+    {
+      g_signal_handlers_disconnect_by_func (priv->persona,
+          folks_persona_notify_cb, object);
+      g_object_unref (priv->persona);
+    }
+  priv->persona = NULL;
+
+  if (priv->avatar != NULL)
+    {
+      empathy_avatar_unref (priv->avatar);
+      priv->avatar = NULL;
+    }
+
+  if (priv->location != NULL)
+    {
+      g_hash_table_unref (priv->location);
+      priv->location = NULL;
+    }
+
   G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
 }
 
+static void
+contact_constructed (GObject *object)
+{
+  EmpathyContact *contact = (EmpathyContact *) object;
+  EmpathyContactPriv *priv = GET_PRIV (contact);
+  GHashTable *location;
+  TpHandle self_handle;
+  TpHandle handle;
+  const gchar * const *client_types;
+
+  if (priv->tp_contact == NULL)
+    return;
+
+  priv->presence = empathy_contact_get_presence (contact);
+
+  location = tp_contact_get_location (priv->tp_contact);
+  if (location != NULL)
+    empathy_contact_set_location (contact, location);
+
+  client_types = tp_contact_get_client_types (priv->tp_contact);
+  if (client_types != NULL)
+    contact_set_client_types (contact, client_types);
+
+  set_capabilities_from_tp_caps (contact,
+      tp_contact_get_capabilities (priv->tp_contact));
+
+  contact_set_avatar_from_tp_contact (contact);
+
+  /* Set is-user property. Note that it could still be the handle is
+   * different from the connection's self handle, in the case the handle
+   * comes from a group interface. */
+  self_handle = tp_connection_get_self_handle (
+      tp_contact_get_connection (priv->tp_contact));
+  handle = tp_contact_get_handle (priv->tp_contact);
+  empathy_contact_set_is_user (contact, self_handle == handle);
+
+  g_signal_connect (priv->tp_contact, "notify",
+    G_CALLBACK (tp_contact_notify_cb), contact);
+}
+
 static void
 empathy_contact_class_init (EmpathyContactClass *class)
 {
@@ -145,6 +276,7 @@ empathy_contact_class_init (EmpathyContactClass *class)
   object_class->dispose = contact_dispose;
   object_class->get_property = contact_get_property;
   object_class->set_property = contact_set_property;
+  object_class->constructed = contact_constructed;
 
   g_object_class_install_property (object_class,
       PROP_TP_CONTACT,
@@ -159,9 +291,17 @@ empathy_contact_class_init (EmpathyContactClass *class)
       g_param_spec_object ("account",
         "The account",
         "The account associated with the contact",
-        MC_TYPE_ACCOUNT,
+        TP_TYPE_ACCOUNT,
         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (object_class,
+      PROP_PERSONA,
+      g_param_spec_object ("persona",
+        "Persona",
+        "The FolksPersona associated with the contact",
+        FOLKS_TYPE_PERSONA,
+        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_property (object_class,
       PROP_ID,
       g_param_spec_string ("id",
@@ -171,10 +311,10 @@ empathy_contact_class_init (EmpathyContactClass *class)
         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (object_class,
-      PROP_NAME,
-      g_param_spec_string ("name",
-        "Contact Name",
-        "The name of the contact",
+      PROP_ALIAS,
+      g_param_spec_string ("alias",
+        "Contact alias",
+        "An alias for the contact",
         NULL,
         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
@@ -184,7 +324,7 @@ empathy_contact_class_init (EmpathyContactClass *class)
         "Avatar image",
         "The avatar image",
         EMPATHY_TYPE_AVATAR,
-        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (object_class,
       PROP_PRESENCE,
@@ -240,6 +380,14 @@ empathy_contact_class_init (EmpathyContactClass *class)
         G_TYPE_HASH_TABLE,
         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (object_class,
+      PROP_CLIENT_TYPES,
+      g_param_spec_boxed ("client-types",
+        "Contact client types",
+        "Client types of the contact",
+        G_TYPE_STRV,
+        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   signals[PRESENCE_CHANGED] =
     g_signal_new ("presence-changed",
                   G_TYPE_FROM_CLASS (class),
@@ -263,6 +411,8 @@ empathy_contact_init (EmpathyContact *contact)
   contact->priv = priv;
 
   priv->location = NULL;
+  priv->client_types = NULL;
+  priv->groups = NULL;
 }
 
 static void
@@ -274,34 +424,114 @@ contact_finalize (GObject *object)
 
   DEBUG ("finalize: %p", object);
 
-  g_free (priv->name);
+  if (priv->groups != NULL)
+    g_hash_table_destroy (priv->groups);
+  g_free (priv->alias);
   g_free (priv->id);
-  g_free (priv->presence_message);
+  g_strfreev (priv->client_types);
 
-  if (priv->avatar)
-      empathy_avatar_unref (priv->avatar);
+  G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
+}
 
-  if (priv->location != NULL)
-      g_hash_table_unref (priv->location);
+static void
+empathy_contact_set_capabilities (EmpathyContact *contact,
+                                  EmpathyCapabilities capabilities)
+{
+  EmpathyContactPriv *priv;
 
-  G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+  priv = GET_PRIV (contact);
+
+  if (priv->capabilities == capabilities)
+    return;
+
+  priv->capabilities = capabilities;
+
+  g_object_notify (G_OBJECT (contact), "capabilities");
 }
 
 static void
-set_tp_contact (EmpathyContact *contact,
-                TpContact *tp_contact)
+empathy_contact_set_id (EmpathyContact *contact,
+                        const gchar *id)
 {
-  EmpathyContactPriv *priv = GET_PRIV (contact);
+  EmpathyContactPriv *priv;
+
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+  g_return_if_fail (id != NULL);
+
+  priv = GET_PRIV (contact);
+
+  /* We temporally ref the contact because it could be destroyed
+   * during the signal emition */
+  g_object_ref (contact);
+  if (tp_strdiff (id, priv->id))
+    {
+      g_free (priv->id);
+      priv->id = g_strdup (id);
+
+      g_object_notify (G_OBJECT (contact), "id");
+      if (EMP_STR_EMPTY (priv->alias))
+          g_object_notify (G_OBJECT (contact), "alias");
+    }
+
+  g_object_unref (contact);
+}
+
+static void
+empathy_contact_set_presence (EmpathyContact *contact,
+                              TpConnectionPresenceType presence)
+{
+  EmpathyContactPriv *priv;
+  TpConnectionPresenceType old_presence;
+
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+  priv = GET_PRIV (contact);
 
-  if (tp_contact == NULL)
+  if (presence == priv->presence)
     return;
 
-  g_assert (priv->tp_contact == NULL);
-  priv->tp_contact = g_object_ref (tp_contact);
-  priv->presence = empathy_contact_get_presence (contact);
+  old_presence = priv->presence;
+  priv->presence = presence;
 
-  g_signal_connect (priv->tp_contact, "notify",
-    G_CALLBACK (tp_contact_notify_cb), contact);
+  g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
+
+  g_object_notify (G_OBJECT (contact), "presence");
+}
+
+static void
+empathy_contact_set_presence_message (EmpathyContact *contact,
+                                      const gchar *message)
+{
+  EmpathyContactPriv *priv = GET_PRIV (contact);
+
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+  if (priv->persona != NULL)
+    {
+      folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
+          message);
+    }
+}
+
+static void
+empathy_contact_set_handle (EmpathyContact *contact,
+                            guint handle)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+  priv = GET_PRIV (contact);
+
+  g_object_ref (contact);
+  if (handle != priv->handle)
+    {
+      priv->handle = handle;
+      g_object_notify (G_OBJECT (contact), "handle");
+    }
+  g_object_unref (contact);
 }
 
 static void
@@ -320,11 +550,14 @@ contact_get_property (GObject *object,
       case PROP_ACCOUNT:
         g_value_set_object (value, empathy_contact_get_account (contact));
         break;
+      case PROP_PERSONA:
+        g_value_set_object (value, empathy_contact_get_persona (contact));
+        break;
       case PROP_ID:
         g_value_set_string (value, empathy_contact_get_id (contact));
         break;
-      case PROP_NAME:
-        g_value_set_string (value, empathy_contact_get_name (contact));
+      case PROP_ALIAS:
+        g_value_set_string (value, empathy_contact_get_alias (contact));
         break;
       case PROP_AVATAR:
         g_value_set_boxed (value, empathy_contact_get_avatar (contact));
@@ -362,20 +595,20 @@ contact_set_property (GObject *object,
   switch (param_id)
     {
       case PROP_TP_CONTACT:
-        set_tp_contact (contact, g_value_get_object (value));
+        priv->tp_contact = g_value_dup_object (value);
         break;
       case PROP_ACCOUNT:
         g_assert (priv->account == NULL);
         priv->account = g_value_dup_object (value);
         break;
+      case PROP_PERSONA:
+        empathy_contact_set_persona (contact, g_value_get_object (value));
+        break;
       case PROP_ID:
         empathy_contact_set_id (contact, g_value_get_string (value));
         break;
-      case PROP_NAME:
-        empathy_contact_set_name (contact, g_value_get_string (value));
-        break;
-      case PROP_AVATAR:
-        empathy_contact_set_avatar (contact, g_value_get_boxed (value));
+      case PROP_ALIAS:
+        empathy_contact_set_alias (contact, g_value_get_string (value));
         break;
       case PROP_PRESENCE:
         empathy_contact_set_presence (contact, g_value_get_uint (value));
@@ -398,7 +631,7 @@ contact_set_property (GObject *object,
     };
 }
 
-EmpathyContact *
+static EmpathyContact *
 empathy_contact_new (TpContact *tp_contact)
 {
   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
@@ -409,20 +642,28 @@ empathy_contact_new (TpContact *tp_contact)
 }
 
 EmpathyContact *
-empathy_contact_new_for_log (McAccount *account,
-                             const gchar *id,
-                             const gchar *name,
-                             gboolean is_user)
+empathy_contact_from_tpl_contact (TpAccount *account,
+    TplEntity *tpl_entity)
 {
-  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
-  g_return_val_if_fail (id != NULL, NULL);
+  EmpathyContact *retval;
+  gboolean is_user;
 
-  return g_object_new (EMPATHY_TYPE_CONTACT,
+  g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
+
+  is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
+
+  retval = g_object_new (EMPATHY_TYPE_CONTACT,
+      "id", tpl_entity_get_alias (tpl_entity),
+      "alias", tpl_entity_get_identifier (tpl_entity),
       "account", account,
-      "id", id,
-      "name", name,
       "is-user", is_user,
       NULL);
+
+  if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
+    contact_load_avatar_cache (retval,
+        tpl_entity_get_avatar_token (tpl_entity));
+
+  return retval;
 }
 
 TpContact *
@@ -452,69 +693,108 @@ empathy_contact_get_id (EmpathyContact *contact)
   return priv->id;
 }
 
+const gchar *
+empathy_contact_get_alias (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv;
+  const gchar        *alias;
+
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+  priv = GET_PRIV (contact);
+
+  if (priv->tp_contact != NULL)
+    alias = tp_contact_get_alias (priv->tp_contact);
+  else
+    alias = priv->alias;
+
+  if (!EMP_STR_EMPTY (alias))
+    return alias;
+  else
+    return empathy_contact_get_id (contact);
+}
+
 void
-empathy_contact_set_id (EmpathyContact *contact,
-                        const gchar *id)
+empathy_contact_set_alias (EmpathyContact *contact,
+                          const gchar *alias)
 {
   EmpathyContactPriv *priv;
+  FolksPersona *persona;
 
   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-  g_return_if_fail (id != NULL);
 
   priv = GET_PRIV (contact);
 
-  /* We temporally ref the contact because it could be destroyed
-   * during the signal emition */
   g_object_ref (contact);
-  if (tp_strdiff (id, priv->id))
+
+  /* Set the alias on the persona if possible */
+  persona = empathy_contact_get_persona (contact);
+  if (persona != NULL && FOLKS_IS_ALIASABLE (persona))
     {
-      g_free (priv->id);
-      priv->id = g_strdup (id);
+      DEBUG ("Setting alias for contact %s to %s",
+          empathy_contact_get_id (contact), alias);
 
-      g_object_notify (G_OBJECT (contact), "id");
-      if (EMP_STR_EMPTY (priv->name))
-          g_object_notify (G_OBJECT (contact), "name");
+      folks_aliasable_set_alias (FOLKS_ALIASABLE (persona), alias);
+    }
+
+  if (tp_strdiff (alias, priv->alias))
+    {
+      g_free (priv->alias);
+      priv->alias = g_strdup (alias);
+      g_object_notify (G_OBJECT (contact), "alias");
     }
 
   g_object_unref (contact);
 }
 
-const gchar *
-empathy_contact_get_name (EmpathyContact *contact)
+static void
+groups_change_group_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-  EmpathyContactPriv *priv;
-
-  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
-
-  priv = GET_PRIV (contact);
-
-  if (priv->tp_contact != NULL)
-    return tp_contact_get_alias (priv->tp_contact);
-
-  if (EMP_STR_EMPTY (priv->name))
-      return empathy_contact_get_id (contact);
+  FolksGroupable *groupable = FOLKS_GROUPABLE (source);
+  GError *error = NULL;
 
-  return priv->name;
+  folks_groupable_change_group_finish (groupable, result, &error);
+  if (error != NULL)
+    {
+      g_warning ("failed to change group: %s", error->message);
+      g_clear_error (&error);
+    }
 }
 
 void
-empathy_contact_set_name (EmpathyContact *contact,
-                          const gchar *name)
+empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
+    gboolean is_member)
 {
   EmpathyContactPriv *priv;
+  FolksPersona *persona;
 
   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+  g_return_if_fail (group != NULL);
 
   priv = GET_PRIV (contact);
 
-  g_object_ref (contact);
-  if (tp_strdiff (name, priv->name))
+  /* Normally pass through the changes to the persona */
+  persona = empathy_contact_get_persona (contact);
+  if (persona != NULL)
     {
-      g_free (priv->name);
-      priv->name = g_strdup (name);
-      g_object_notify (G_OBJECT (contact), "name");
+      if (FOLKS_IS_GROUPABLE (persona))
+        folks_groupable_change_group (FOLKS_GROUPABLE (persona), group, is_member,
+          groups_change_group_cb, contact);
+      return;
     }
-  g_object_unref (contact);
+
+  /* If the persona doesn't exist yet, we have to cache the changes until it
+   * does */
+  if (priv->groups == NULL)
+    {
+      priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
+          NULL);
+    }
+
+  g_hash_table_insert (priv->groups, g_strdup (group),
+      GUINT_TO_POINTER (is_member));
 }
 
 EmpathyAvatar *
@@ -529,9 +809,9 @@ empathy_contact_get_avatar (EmpathyContact *contact)
   return priv->avatar;
 }
 
-void
-empathy_contact_set_avatar (EmpathyContact *contact,
-                            EmpathyAvatar *avatar)
+static void
+contact_set_avatar (EmpathyContact *contact,
+                    EmpathyAvatar *avatar)
 {
   EmpathyContactPriv *priv;
 
@@ -554,7 +834,7 @@ empathy_contact_set_avatar (EmpathyContact *contact,
   g_object_notify (G_OBJECT (contact), "avatar");
 }
 
-McAccount *
+TpAccount *
 empathy_contact_get_account (EmpathyContact *contact)
 {
   EmpathyContactPriv *priv;
@@ -565,20 +845,111 @@ empathy_contact_get_account (EmpathyContact *contact)
 
   if (priv->account == NULL && priv->tp_contact != NULL)
     {
-      EmpathyAccountManager *manager;
       TpConnection *connection;
 
       /* FIXME: This assume the account manager already exists */
-      manager = empathy_account_manager_dup_singleton ();
       connection = tp_contact_get_connection (priv->tp_contact);
-      priv->account = empathy_account_manager_get_account (manager, connection);
-      g_object_ref (priv->account);
-      g_object_unref (manager);
+      priv->account =
+        g_object_ref (empathy_get_account_for_connection (connection));
     }
 
   return priv->account;
 }
 
+FolksPersona *
+empathy_contact_get_persona (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+  priv = GET_PRIV (contact);
+
+  if (priv->persona == NULL && priv->tp_contact != NULL)
+    {
+      /* FIXME: This is disgustingly slow */
+      /* Query for the persona */
+      EmpathyIndividualManager *manager;
+      GList *individuals, *l;
+
+      manager = empathy_individual_manager_dup_singleton ();
+      individuals = empathy_individual_manager_get_members (manager);
+
+      for (l = individuals; l != NULL; l = l->next)
+        {
+          GList *personas, *j;
+          FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
+
+          personas = folks_individual_get_personas (individual);
+          for (j = personas; j != NULL; j = j->next)
+            {
+              TpfPersona *persona = j->data;
+
+              if (TPF_IS_PERSONA (persona))
+                {
+                  TpContact *tp_contact = tpf_persona_get_contact (persona);
+
+                  if (tp_contact == priv->tp_contact)
+                    {
+                      /* Found the right persona */
+                      empathy_contact_set_persona (contact,
+                          (FolksPersona *) persona);
+                      goto finished;
+                    }
+                }
+            }
+        }
+
+finished:
+      g_list_free (individuals);
+      g_object_unref (manager);
+    }
+
+  return priv->persona;
+}
+
+void
+empathy_contact_set_persona (EmpathyContact *contact,
+    FolksPersona *persona)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+  g_return_if_fail (TPF_IS_PERSONA (persona));
+
+  priv = GET_PRIV (contact);
+
+  if (persona == priv->persona)
+    return;
+
+  if (priv->persona != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (priv->persona,
+          folks_persona_notify_cb, contact);
+      g_object_unref (priv->persona);
+    }
+  priv->persona = g_object_ref (persona);
+
+  g_signal_connect (priv->persona, "notify",
+    G_CALLBACK (folks_persona_notify_cb), contact);
+
+  g_object_notify (G_OBJECT (contact), "persona");
+
+  /* Set the persona's alias, since ours could've been set using
+   * empathy_contact_set_alias() before we had a persona; this happens when
+   * adding a contact. */
+  if (priv->alias != NULL)
+    empathy_contact_set_alias (contact, priv->alias);
+
+  /* Set the persona's groups */
+  if (priv->groups != NULL)
+    {
+      folks_groupable_set_groups (FOLKS_GROUPABLE (persona), priv->groups);
+      g_hash_table_destroy (priv->groups);
+      priv->groups = NULL;
+    }
+}
+
 TpConnection *
 empathy_contact_get_connection (EmpathyContact *contact)
 {
@@ -610,28 +981,6 @@ empathy_contact_get_presence (EmpathyContact *contact)
   return priv->presence;
 }
 
-void
-empathy_contact_set_presence (EmpathyContact *contact,
-                              TpConnectionPresenceType presence)
-{
-  EmpathyContactPriv *priv;
-  TpConnectionPresenceType old_presence;
-
-  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-
-  priv = GET_PRIV (contact);
-
-  if (presence == priv->presence)
-    return;
-
-  old_presence = priv->presence;
-  priv->presence = presence;
-
-  g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
-
-  g_object_notify (G_OBJECT (contact), "presence");
-}
-
 const gchar *
 empathy_contact_get_presence_message (EmpathyContact *contact)
 {
@@ -641,27 +990,10 @@ empathy_contact_get_presence_message (EmpathyContact *contact)
 
   priv = GET_PRIV (contact);
 
-  if (priv->tp_contact != NULL)
-    return tp_contact_get_presence_message (priv->tp_contact);
-
-  return priv->presence_message;
-}
-
-void
-empathy_contact_set_presence_message (EmpathyContact *contact,
-                                      const gchar *message)
-{
-  EmpathyContactPriv *priv = GET_PRIV (contact);
-
-  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+  if (priv->persona != NULL)
+    return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
 
-  if (!tp_strdiff (message, priv->presence_message))
-    return;
-
-  g_free (priv->presence_message);
-  priv->presence_message = g_strdup (message);
-
-  g_object_notify (G_OBJECT (contact), "presence-message");
+  return NULL;
 }
 
 guint
@@ -679,25 +1011,6 @@ empathy_contact_get_handle (EmpathyContact *contact)
   return priv->handle;
 }
 
-void
-empathy_contact_set_handle (EmpathyContact *contact,
-                            guint handle)
-{
-  EmpathyContactPriv *priv;
-
-  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-
-  priv = GET_PRIV (contact);
-
-  g_object_ref (contact);
-  if (handle != priv->handle)
-    {
-      priv->handle = handle;
-      g_object_notify (G_OBJECT (contact), "handle");
-    }
-  g_object_unref (contact);
-}
-
 EmpathyCapabilities
 empathy_contact_get_capabilities (EmpathyContact *contact)
 {
@@ -710,24 +1023,6 @@ empathy_contact_get_capabilities (EmpathyContact *contact)
   return priv->capabilities;
 }
 
-void
-empathy_contact_set_capabilities (EmpathyContact *contact,
-                                  EmpathyCapabilities capabilities)
-{
-  EmpathyContactPriv *priv;
-
-  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-
-  priv = GET_PRIV (contact);
-
-  if (priv->capabilities == capabilities)
-    return;
-
-  priv->capabilities = capabilities;
-
-  g_object_notify (G_OBJECT (contact), "capabilities");
-}
-
 gboolean
 empathy_contact_is_user (EmpathyContact *contact)
 {
@@ -769,6 +1064,14 @@ empathy_contact_is_online (EmpathyContact *contact)
       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
         return FALSE;
+      /* Contacts without presence are considered online so we can display IRC
+       * contacts in rooms. */
+      case TP_CONNECTION_PRESENCE_TYPE_UNSET:
+      case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
+      case TP_CONNECTION_PRESENCE_TYPE_AWAY:
+      case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
+      case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
+      case TP_CONNECTION_PRESENCE_TYPE_BUSY:
       default:
         return TRUE;
     }
@@ -802,6 +1105,30 @@ empathy_contact_can_voip (EmpathyContact *contact)
       EMPATHY_CAPABILITIES_VIDEO);
 }
 
+gboolean
+empathy_contact_can_voip_audio (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+
+  priv = GET_PRIV (contact);
+
+  return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
+}
+
+gboolean
+empathy_contact_can_voip_video (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+
+  priv = GET_PRIV (contact);
+
+  return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
+}
+
 gboolean
 empathy_contact_can_send_files (EmpathyContact *contact)
 {
@@ -815,7 +1142,7 @@ empathy_contact_can_send_files (EmpathyContact *contact)
 }
 
 gboolean
-empathy_contact_can_use_stream_tube (EmpathyContact *contact)
+empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
 {
   EmpathyContactPriv *priv;
 
@@ -823,85 +1150,91 @@ empathy_contact_can_use_stream_tube (EmpathyContact *contact)
 
   priv = GET_PRIV (contact);
 
-  return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
+  return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
+}
+
+static gboolean
+contact_has_log (EmpathyContact *contact)
+{
+  TplLogManager *manager;
+  gboolean have_log;
+
+  manager = tpl_log_manager_dup_singleton ();
+  have_log = tpl_log_manager_exists (manager,
+      empathy_contact_get_account (contact), empathy_contact_get_id (contact),
+      FALSE);
+  g_object_unref (manager);
+
+  return have_log;
+}
+
+gboolean
+empathy_contact_can_do_action (EmpathyContact *self,
+    EmpathyActionType action_type)
+{
+  gboolean sensitivity = FALSE;
+
+  switch (action_type)
+    {
+      case EMPATHY_ACTION_CHAT:
+        sensitivity = TRUE;
+        break;
+      case EMPATHY_ACTION_AUDIO_CALL:
+        sensitivity = empathy_contact_can_voip_audio (self);
+        break;
+      case EMPATHY_ACTION_VIDEO_CALL:
+        sensitivity = empathy_contact_can_voip_video (self);
+        break;
+      case EMPATHY_ACTION_VIEW_LOGS:
+        sensitivity = contact_has_log (self);
+        break;
+      case EMPATHY_ACTION_SEND_FILE:
+        sensitivity = empathy_contact_can_send_files (self);
+        break;
+      case EMPATHY_ACTION_SHARE_MY_DESKTOP:
+        sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
+        break;
+      default:
+        g_assert_not_reached ();
+    }
+
+  return (sensitivity ? TRUE : FALSE);
 }
 
 static gchar *
 contact_get_avatar_filename (EmpathyContact *contact,
                              const gchar *token)
 {
-  McAccount *account;
+  TpAccount *account;
   gchar *avatar_path;
   gchar *avatar_file;
   gchar *token_escaped;
-  gchar *contact_escaped;
 
   if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
     return NULL;
 
-  contact_escaped = tp_escape_as_identifier (empathy_contact_get_id (contact));
   token_escaped = tp_escape_as_identifier (token);
   account = empathy_contact_get_account (contact);
 
-  /* FIXME: Do not use the account, but proto/cm instead */
   avatar_path = g_build_filename (g_get_user_cache_dir (),
-      PACKAGE_NAME,
+      "telepathy",
       "avatars",
-      mc_account_get_unique_name (account),
-      contact_escaped,
+      tp_account_get_connection_manager (account),
+      tp_account_get_protocol (account),
       NULL);
   g_mkdir_with_parents (avatar_path, 0700);
 
   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
 
-  g_free (contact_escaped);
   g_free (token_escaped);
   g_free (avatar_path);
 
   return avatar_file;
 }
 
-void
-empathy_contact_load_avatar_data (EmpathyContact *contact,
-                                  const guchar *data,
-                                  const gsize len,
-                                  const gchar *format,
-                                  const gchar *token)
-{
-  EmpathyAvatar *avatar;
-  gchar *filename;
-  GError *error = NULL;
-
-  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
-  g_return_if_fail (data != NULL);
-  g_return_if_fail (len > 0);
-  g_return_if_fail (format != NULL);
-  g_return_if_fail (!EMP_STR_EMPTY (token));
-
-  /* Load and set the avatar */
-  filename = contact_get_avatar_filename (contact, token);
-  avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
-      g_strdup (token), filename);
-  empathy_contact_set_avatar (contact, avatar);
-  empathy_avatar_unref (avatar);
-
-  /* Save to cache if not yet in it */
-  if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
-    {
-      if (!empathy_avatar_save_to_file (avatar, filename, &error))
-        {
-          DEBUG ("Failed to save avatar in cache: %s",
-            error ? error->message : "No error given");
-          g_clear_error (&error);
-        }
-      else
-          DEBUG ("Avatar saved to %s", filename);
-    }
-}
-
-gboolean
-empathy_contact_load_avatar_cache (EmpathyContact *contact,
-                                   const gchar *token)
+static gboolean
+contact_load_avatar_cache (EmpathyContact *contact,
+                           const gchar *token)
 {
   EmpathyAvatar *avatar = NULL;
   gchar *filename;
@@ -927,10 +1260,14 @@ empathy_contact_load_avatar_cache (EmpathyContact *contact,
   if (data)
     {
       DEBUG ("Avatar loaded from %s", filename);
-      avatar = empathy_avatar_new (data, len, NULL, g_strdup (token), filename);
-      empathy_contact_set_avatar (contact, avatar);
+      avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
+      contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
     }
+  else
+    {
+      g_free (filename);
+    }
 
   return data != NULL;
 }
@@ -955,11 +1292,10 @@ empathy_avatar_get_type (void)
  * @data: the avatar data
  * @len: the size of avatar data
  * @format: the mime type of the avatar image
- * @token: the token of the avatar
  * @filename: the filename where the avatar is stored in cache
  *
  * Create a #EmpathyAvatar from the provided data. This function takes the
- * ownership of @data, @format, @token and @filename.
+ * ownership of @data, @format and @filename.
  *
  * Returns: a new #EmpathyAvatar
  */
@@ -967,7 +1303,6 @@ EmpathyAvatar *
 empathy_avatar_new (guchar *data,
                     gsize len,
                     gchar *format,
-                    gchar *token,
                     gchar *filename)
 {
   EmpathyAvatar *avatar;
@@ -976,7 +1311,6 @@ empathy_avatar_new (guchar *data,
   avatar->data = data;
   avatar->len = len;
   avatar->format = format;
-  avatar->token = token;
   avatar->filename = filename;
   avatar->refcount = 1;
 
@@ -993,7 +1327,7 @@ empathy_avatar_unref (EmpathyAvatar *avatar)
     {
       g_free (avatar->data);
       g_free (avatar->format);
-      g_free (avatar->token);
+      g_free (avatar->filename);
       g_slice_free (EmpathyAvatar, avatar);
     }
 }
@@ -1023,7 +1357,8 @@ empathy_avatar_save_to_file (EmpathyAvatar *self,
                              const gchar *filename,
                              GError **error)
 {
-  return g_file_set_contents (filename, self->data, self->len, error);
+  return g_file_set_contents (filename, (const gchar *) self->data, self->len,
+      error);
 }
 
 /**
@@ -1065,9 +1400,9 @@ empathy_contact_get_location (EmpathyContact *contact)
  * Example: a "city" key would have "Helsinki" as string GValue,
  *          a "latitude" would have 65.0 as double GValue.
  */
-void
+static void
 empathy_contact_set_location (EmpathyContact *contact,
-                              GHashTable *location)
+    GHashTable *location)
 {
   EmpathyContactPriv *priv;
 
@@ -1080,9 +1415,37 @@ empathy_contact_set_location (EmpathyContact *contact,
     g_hash_table_unref (priv->location);
 
   priv->location = g_hash_table_ref (location);
+#ifdef HAVE_GEOCLUE
+  update_geocode (contact);
+#endif
   g_object_notify (G_OBJECT (contact), "location");
 }
 
+const gchar * const *
+empathy_contact_get_client_types (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
+
+  priv = GET_PRIV (contact);
+
+  return (const gchar * const *) priv->client_types;
+}
+
+static void
+contact_set_client_types (EmpathyContact *contact,
+    const gchar * const *client_types)
+{
+  EmpathyContactPriv *priv = GET_PRIV (contact);
+
+  if (priv->client_types != NULL)
+    g_strfreev (priv->client_types);
+
+  priv->client_types = g_strdupv ((gchar **) client_types);
+  g_object_notify (G_OBJECT (contact), "client-types");
+}
+
 /**
  * empathy_contact_equal:
  * @contact1: an #EmpathyContact
@@ -1119,3 +1482,505 @@ empathy_contact_equal (gconstpointer contact1,
   }
   return FALSE;
 }
+
+#ifdef HAVE_GEOCLUE
+#define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
+#define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
+
+/* This callback is called by geoclue when it found a position
+ * for the given address.  A position is necessary for a contact
+ * to show up on the map
+ */
+static void
+geocode_cb (GeoclueGeocode *geocode,
+    GeocluePositionFields fields,
+    double latitude,
+    double longitude,
+    double altitude,
+    GeoclueAccuracy *accuracy,
+    GError *error,
+    gpointer contact)
+{
+  EmpathyContactPriv *priv = GET_PRIV (contact);
+  GHashTable *new_location;
+
+  if (priv->location == NULL)
+    goto out;
+
+  if (error != NULL)
+    {
+      DEBUG ("Error geocoding location : %s", error->message);
+      goto out;
+    }
+
+  /* No need to change location if we didn't find the position */
+  if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
+    goto out;
+
+  if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
+    goto out;
+
+  new_location = tp_asv_new (
+      EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
+      EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
+      NULL);
+
+  DEBUG ("\t - Latitude: %f", latitude);
+  DEBUG ("\t - Longitude: %f", longitude);
+
+  /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
+   * the values we just set. */
+  tp_g_hash_table_update (new_location, priv->location,
+      (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
+
+  /* Set the altitude only if it wasn't defined before */
+  if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
+      g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
+    {
+      tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
+          altitude);
+      DEBUG ("\t - Altitude: %f", altitude);
+    }
+
+  /* Don't change the accuracy as we used an address to get this position */
+  g_hash_table_unref (priv->location);
+  priv->location = new_location;
+  g_object_notify (contact, "location");
+out:
+  g_object_unref (geocode);
+  g_object_unref (contact);
+}
+
+static gchar *
+get_dup_string (GHashTable *location,
+    gchar *key)
+{
+  GValue *value;
+
+  value = g_hash_table_lookup (location, key);
+  if (value != NULL)
+    return g_value_dup_string (value);
+
+  return NULL;
+}
+
+static void
+update_geocode (EmpathyContact *contact)
+{
+  static GeoclueGeocode *geocode;
+  gchar *str;
+  GHashTable *address;
+  GHashTable *location;
+
+  location = empathy_contact_get_location (contact);
+  if (location == NULL)
+    return;
+
+  /* No need to search for position if contact published it */
+  if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
+      g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
+    return;
+
+  if (geocode == NULL)
+    {
+      geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
+      g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
+    }
+  else
+    {
+      g_object_ref (geocode);
+    }
+
+  address = geoclue_address_details_new ();
+
+  str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
+      DEBUG ("\t - countrycode: %s", str);
+    }
+
+  str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
+      DEBUG ("\t - country: %s", str);
+    }
+
+  str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
+      DEBUG ("\t - postalcode: %s", str);
+    }
+
+  str = get_dup_string (location, EMPATHY_LOCATION_REGION);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
+      DEBUG ("\t - region: %s", str);
+    }
+
+  str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
+      DEBUG ("\t - locality: %s", str);
+    }
+
+  str = get_dup_string (location, EMPATHY_LOCATION_STREET);
+  if (str != NULL)
+    {
+      g_hash_table_insert (address,
+        g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
+      DEBUG ("\t - street: %s", str);
+    }
+
+  if (g_hash_table_size (address) > 0)
+    {
+      g_object_ref (contact);
+
+      geoclue_geocode_address_to_position_async (geocode, address,
+          geocode_cb, contact);
+    }
+
+  g_hash_table_unref (address);
+}
+#endif
+
+static EmpathyCapabilities
+tp_caps_to_capabilities (TpCapabilities *caps)
+{
+  EmpathyCapabilities capabilities = 0;
+  guint i;
+  GPtrArray *classes;
+
+  classes = tp_capabilities_get_channel_classes (caps);
+
+  for (i = 0; i < classes->len; i++)
+    {
+      GValueArray *class_struct;
+      GHashTable *fixed_prop;
+      GStrv allowed_prop;
+      TpHandleType handle_type;
+      const gchar *chan_type;
+
+      class_struct = g_ptr_array_index (classes, i);
+      tp_value_array_unpack (class_struct, 2,
+          &fixed_prop,
+          &allowed_prop);
+
+      handle_type = tp_asv_get_uint32 (fixed_prop,
+          TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
+      if (handle_type != TP_HANDLE_TYPE_CONTACT)
+        continue;
+
+      chan_type = tp_asv_get_string (fixed_prop,
+          TP_PROP_CHANNEL_CHANNEL_TYPE);
+
+      if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
+        {
+          capabilities |= EMPATHY_CAPABILITIES_FT;
+        }
+      else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
+        {
+          const gchar *service;
+
+          service = tp_asv_get_string (fixed_prop,
+              TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
+
+          if (!tp_strdiff (service, "rfb"))
+            capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
+        }
+      else if (!tp_strdiff (chan_type,
+        TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
+        {
+          guint j;
+
+          for (j = 0; allowed_prop[j] != NULL; j++)
+            {
+              if (!tp_strdiff (allowed_prop[j],
+                    TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
+                capabilities |= EMPATHY_CAPABILITIES_AUDIO;
+              else if (!tp_strdiff (allowed_prop[j],
+                    TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
+                capabilities |= EMPATHY_CAPABILITIES_VIDEO;
+            }
+        }
+    }
+
+  return capabilities;
+}
+
+static void
+set_capabilities_from_tp_caps (EmpathyContact *self,
+    TpCapabilities *caps)
+{
+  EmpathyCapabilities capabilities;
+
+  if (caps == NULL)
+    return;
+
+  capabilities = tp_caps_to_capabilities (caps);
+  empathy_contact_set_capabilities (self, capabilities);
+}
+
+static void
+contact_set_avatar_from_tp_contact (EmpathyContact *contact)
+{
+  EmpathyContactPriv *priv = GET_PRIV (contact);
+  const gchar *mime;
+  GFile *file;
+
+  mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
+  file = tp_contact_get_avatar_file (priv->tp_contact);
+
+  if (file != NULL)
+    {
+      EmpathyAvatar *avatar;
+      gchar *data;
+      gsize len;
+
+      g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
+      avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime),
+          g_file_get_path (file));
+      contact_set_avatar (contact, avatar);
+      empathy_avatar_unref (avatar);
+    }
+  else
+    {
+      contact_set_avatar (contact, NULL);
+    }
+}
+
+EmpathyContact *
+empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
+{
+  EmpathyContact *contact = NULL;
+
+  g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
+
+  if (contacts_table == NULL)
+    contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
+  else
+    contact = g_hash_table_lookup (contacts_table, tp_contact);
+
+  if (contact == NULL)
+    {
+      contact = empathy_contact_new (tp_contact);
+
+      /* The hash table does not keep any ref.
+       * contact keeps a ref to tp_contact, and is removed from the table in
+       * contact_dispose() */
+      g_hash_table_insert (contacts_table, tp_contact, contact);
+    }
+  else
+    {
+      g_object_ref (contact);
+    }
+
+  return contact;
+}
+
+static int
+presence_cmp_func (EmpathyContact *a,
+    EmpathyContact *b)
+{
+  FolksPresence *presence_a, *presence_b;
+
+  presence_a = FOLKS_PRESENCE (empathy_contact_get_persona (a));
+  presence_b = FOLKS_PRESENCE (empathy_contact_get_persona (b));
+
+  /* We negate the result because we're sorting in reverse order (i.e. such that
+   * the Personas with the highest presence are at the beginning of the list. */
+  return -folks_presence_typecmp (folks_presence_get_presence_type (presence_a),
+      folks_presence_get_presence_type (presence_b));
+}
+
+static gint
+voip_cmp_func (EmpathyContact *a,
+    EmpathyContact *b)
+{
+  gboolean has_audio_a, has_audio_b;
+  gboolean has_video_a, has_video_b;
+
+  has_audio_a = empathy_contact_can_voip_audio (a);
+  has_audio_b = empathy_contact_can_voip_audio (b);
+  has_video_a = empathy_contact_can_voip_video (a);
+  has_video_b = empathy_contact_can_voip_video (b);
+
+  /* First check video */
+  if (has_video_a == has_video_b)
+    {
+      /* Use audio to to break tie */
+      if (has_audio_a == has_audio_b)
+        return 0;
+      else if (has_audio_a)
+        return -1;
+      else
+        return 1;
+    }
+  else if (has_video_a)
+    return -1;
+  else
+    return 1;
+}
+
+static gint
+ft_cmp_func (EmpathyContact *a,
+    EmpathyContact *b)
+{
+  gboolean can_send_files_a, can_send_files_b;
+
+  can_send_files_a = empathy_contact_can_send_files (a);
+  can_send_files_b = empathy_contact_can_send_files (b);
+
+  if (can_send_files_a == can_send_files_b)
+    return 0;
+  else if (can_send_files_a)
+    return -1;
+  else
+    return 1;
+}
+
+static gint
+rfb_stream_tube_cmp_func (EmpathyContact *a,
+    EmpathyContact *b)
+{
+  gboolean rfb_a, rfb_b;
+
+  rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
+  rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
+
+  if (rfb_a == rfb_b)
+    return 0;
+  else if (rfb_a)
+    return -1;
+  else
+    return 1;
+}
+
+/* Sort by presence as with presence_cmp_func(), but if the two contacts have
+ * the same presence, prefer the one which can do both audio *and* video calls,
+ * over the one which can only do one of the two. */
+static int
+voip_sort_func (EmpathyContact *a, EmpathyContact *b)
+{
+  gint presence_sort = presence_cmp_func (a, b);
+
+  if (presence_sort != 0)
+    return presence_sort;
+
+  return voip_cmp_func (a, b);
+}
+
+/* Sort by presence as with presence_cmp_func() and then break ties using the
+ * most "capable" individual. So users will be able to pick more actions on
+ * the contact in the "Contact" menu of the chat window. */
+static gint
+chat_sort_func (EmpathyContact *a,
+    EmpathyContact *b)
+{
+  gint result;
+
+  result = presence_cmp_func (a, b);
+  if (result != 0)
+    return result;
+
+  /* Prefer individual supporting file transfer */
+  result = ft_cmp_func (a, b);
+  if (result != 0)
+    return result;
+
+  /* Check audio/video capabilities */
+  result = voip_cmp_func (a, b);
+  if (result != 0)
+    return result;
+
+  /* Check 'Share my destop' feature */
+  return rfb_stream_tube_cmp_func (a, b);
+}
+
+static GCompareFunc
+get_sort_func_for_action (EmpathyActionType action_type)
+{
+  switch (action_type)
+    {
+      case EMPATHY_ACTION_AUDIO_CALL:
+      case EMPATHY_ACTION_VIDEO_CALL:
+        return (GCompareFunc) voip_sort_func;
+      case EMPATHY_ACTION_CHAT:
+        return (GCompareFunc) chat_sort_func;
+      case EMPATHY_ACTION_VIEW_LOGS:
+      case EMPATHY_ACTION_SEND_FILE:
+      case EMPATHY_ACTION_SHARE_MY_DESKTOP:
+      default:
+        return (GCompareFunc) presence_cmp_func;
+    }
+}
+
+/**
+ * empathy_contact_dup_best_for_action:
+ * @individual: a #FolksIndividual
+ * @action_type: the type of action to be performed on the contact
+ *
+ * Chooses a #FolksPersona from the given @individual which is best-suited for
+ * the given @action_type. "Best-suited" is determined by choosing the persona
+ * with the highest presence out of all the personas which can perform the given
+ * @action_type (e.g. are capable of video calling).
+ *
+ * Return value: an #EmpathyContact for the best persona, or %NULL;
+ * unref with g_object_unref()
+ */
+EmpathyContact *
+empathy_contact_dup_best_for_action (FolksIndividual *individual,
+    EmpathyActionType action_type)
+{
+  GList *personas, *contacts, *l;
+  EmpathyContact *best_contact = NULL;
+
+  /* Build a list of EmpathyContacts that we can sort */
+  personas = folks_individual_get_personas (individual);
+  contacts = NULL;
+
+  for (l = personas; l != NULL; l = l->next)
+    {
+      TpContact *tp_contact;
+      EmpathyContact *contact;
+
+      if (!TPF_IS_PERSONA (l->data))
+        continue;
+
+      tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
+      contact = empathy_contact_dup_from_tp_contact (tp_contact);
+      empathy_contact_set_persona (contact, FOLKS_PERSONA (l->data));
+
+      /* Only choose the contact if they're actually capable of the specified
+       * action. */
+      if (!empathy_contact_can_do_action (contact, action_type))
+        {
+          g_object_unref (contact);
+          continue;
+        }
+
+      contacts = g_list_prepend (contacts, contact);
+    }
+
+  /* Sort the contacts by some heuristic based on the action type, then take
+   * the top contact. */
+  if (contacts != NULL)
+    {
+      contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
+      best_contact = g_object_ref (contacts->data);
+    }
+
+  g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
+  g_list_free (contacts);
+
+  return best_contact;
+}