+
+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;
+ const gchar *token;
+ GFile *file;
+
+ token = tp_contact_get_avatar_token (priv->tp_contact);
+ 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_strdup (token),
+ g_file_get_path (file));
+ empathy_contact_set_avatar (contact, avatar);
+ empathy_avatar_unref (avatar);
+ }
+ else
+ {
+ empathy_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;
+}
+