aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-contact.c
diff options
context:
space:
mode:
Diffstat (limited to 'libempathy/empathy-contact.c')
-rw-r--r--libempathy/empathy-contact.c96
1 files changed, 80 insertions, 16 deletions
diff --git a/libempathy/empathy-contact.c b/libempathy/empathy-contact.c
index 55bc40bdd..8cca70944 100644
--- a/libempathy/empathy-contact.c
+++ b/libempathy/empathy-contact.c
@@ -190,14 +190,12 @@ contact_dispose (GObject *object)
{
EmpathyContactPriv *priv = GET_PRIV (object);
- if (priv->tp_contact)
+ if (priv->tp_contact != NULL)
{
- 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);
}
- priv->tp_contact = NULL;
+ tp_clear_object (&priv->tp_contact);
if (priv->account)
g_object_unref (priv->account);
@@ -631,14 +629,48 @@ contact_set_property (GObject *object,
};
}
+static void
+remove_tp_contact (gpointer data,
+ GObject *object)
+{
+ g_hash_table_remove (contacts_table, data);
+}
+
static EmpathyContact *
empathy_contact_new (TpContact *tp_contact)
{
+ EmpathyContact *retval;
+
g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
- return g_object_new (EMPATHY_TYPE_CONTACT,
+ retval = g_object_new (EMPATHY_TYPE_CONTACT,
"tp-contact", tp_contact,
NULL);
+
+ g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
+
+ return retval;
+}
+
+typedef struct
+{
+ TplEntity *entity;
+ TpAccount *account;
+} FindContactData;
+
+static gboolean
+contact_is_tpl_entity (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ EmpathyContact *contact = value;
+ FindContactData *data = user_data;
+
+ return !tp_strdiff (empathy_contact_get_id (contact),
+ tpl_entity_get_identifier (data->entity)) &&
+ !tp_strdiff (tp_proxy_get_object_path (data->account),
+ tp_proxy_get_object_path (
+ empathy_contact_get_account (contact)));
}
EmpathyContact *
@@ -647,17 +679,49 @@ empathy_contact_from_tpl_contact (TpAccount *account,
{
EmpathyContact *retval;
gboolean is_user;
+ EmpathyContact *existing_contact = NULL;
g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
- is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
+ if (contacts_table != NULL)
+ {
+ FindContactData data;
- retval = g_object_new (EMPATHY_TYPE_CONTACT,
- "id", tpl_entity_get_identifier (tpl_entity),
- "alias", tpl_entity_get_alias (tpl_entity),
- "account", account,
- "is-user", is_user,
- NULL);
+ data.entity = tpl_entity;
+ data.account = account;
+
+ existing_contact = g_hash_table_find (contacts_table,
+ contact_is_tpl_entity, &data);
+ }
+
+ if (existing_contact != NULL)
+ {
+ EmpathyContactPriv *priv;
+
+ retval = g_object_new (EMPATHY_TYPE_CONTACT,
+ "tp-contact", empathy_contact_get_tp_contact (existing_contact),
+ NULL);
+
+ priv = GET_PRIV (retval);
+
+ /* contact_set_property() calls empathy_contact_set_alias(), which
+ * tries to set the alias on the FolksPersona, but we don't want to
+ * do that when creating an EmpathyContact from a TplEntity. So just
+ * set priv->alias instead of passing it to g_object_new() instead. */
+ g_free (priv->alias);
+ priv->alias = g_strdup (tpl_entity_get_alias (tpl_entity));
+ }
+ else
+ {
+ is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
+
+ retval = g_object_new (EMPATHY_TYPE_CONTACT,
+ "id", tpl_entity_get_identifier (tpl_entity),
+ "alias", tpl_entity_get_alias (tpl_entity),
+ "account", account,
+ "is-user", is_user,
+ NULL);
+ }
if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
contact_load_avatar_cache (retval,
@@ -697,16 +761,16 @@ const gchar *
empathy_contact_get_alias (EmpathyContact *contact)
{
EmpathyContactPriv *priv;
- const gchar *alias;
+ const gchar *alias = NULL;
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
+ if (!EMP_STR_EMPTY (priv->alias))
alias = priv->alias;
+ else if (priv->tp_contact != NULL)
+ alias = tp_contact_get_alias (priv->tp_contact);
if (!EMP_STR_EMPTY (alias))
return alias;