aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Claessens <xclaesse@gmail.com>2010-05-10 18:34:42 +0800
committerXavier Claessens <xclaesse@gmail.com>2010-05-26 17:17:47 +0800
commit373413516c18200750da27f385da588da6c297cc (patch)
tree9eb054ba129dbd975c3d2ddf5ab1f99fb60f0bd8
parent84419982180bd444e611b874add261d053341e5f (diff)
downloadgsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar.gz
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar.bz2
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar.lz
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar.xz
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.tar.zst
gsoc2013-empathy-373413516c18200750da27f385da588da6c297cc.zip
Add empathy_contact_dup_from_tp_contact()
This API make sure to return a singleton EmpathyContact for any TpContact
-rw-r--r--libempathy/empathy-contact.c43
-rw-r--r--libempathy/empathy-contact.h2
2 files changed, 45 insertions, 0 deletions
diff --git a/libempathy/empathy-contact.c b/libempathy/empathy-contact.c
index 224ad6e49..56b7aa42d 100644
--- a/libempathy/empathy-contact.c
+++ b/libempathy/empathy-contact.c
@@ -111,6 +111,9 @@ enum {
static guint signals[LAST_SIGNAL];
+/* TpContact* -> EmpathyContact* */
+static GHashTable *contacts_table = NULL;
+
static void
tp_contact_notify_cb (TpContact *tp_contact,
GParamSpec *param,
@@ -162,6 +165,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);
@@ -340,6 +344,8 @@ set_tp_contact (EmpathyContact *contact,
{
EmpathyContactPriv *priv = GET_PRIV (contact);
GHashTable *location;
+ TpHandle self_handle;
+ TpHandle handle;
if (tp_contact == NULL)
return;
@@ -357,6 +363,14 @@ set_tp_contact (EmpathyContact *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 (tp_contact));
+ handle = tp_contact_get_handle (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);
}
@@ -1466,3 +1480,32 @@ contact_set_avatar_from_tp_contact (EmpathyContact *contact)
}
}
+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;
+}
+
diff --git a/libempathy/empathy-contact.h b/libempathy/empathy-contact.h
index 43f20957f..da463269e 100644
--- a/libempathy/empathy-contact.h
+++ b/libempathy/empathy-contact.h
@@ -131,6 +131,8 @@ GHashTable * empathy_contact_get_location (EmpathyContact *contact);
gboolean empathy_contact_equal (gconstpointer contact1,
gconstpointer contact2);
+EmpathyContact *empathy_contact_dup_from_tp_contact (TpContact *tp_contact);
+
G_END_DECLS
#endif /* __EMPATHY_CONTACT_H__ */