]> git.0d.be Git - empathy.git/commitdiff
Make use of TP_CONTACT_FEATURE_AVATAR_DATA
authorXavier Claessens <xclaesse@gmail.com>
Fri, 7 May 2010 08:10:22 +0000 (10:10 +0200)
committerXavier Claessens <xclaesse@gmail.com>
Wed, 26 May 2010 09:17:28 +0000 (11:17 +0200)
Fixes bug #579812

libempathy/empathy-contact.c
libempathy/empathy-contact.h
libempathy/empathy-tp-contact-factory.c

index 1d544d8e80bd784c3333dc63d3da78fa64ae3fc2..224ad6e498914df2ca6977da56d557c2f295bcb1 100644 (file)
@@ -84,6 +84,8 @@ static void empathy_contact_set_location (EmpathyContact *contact,
 static void set_capabilities_from_tp_caps (EmpathyContact *self,
     TpCapabilities *caps);
 
+static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
+
 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
 
 enum
@@ -147,6 +149,10 @@ tp_contact_notify_cb (TpContact *tp_contact,
       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));
+    }
 }
 
 static void
@@ -349,6 +355,8 @@ set_tp_contact (EmpathyContact *contact,
   set_capabilities_from_tp_caps (contact,
       tp_contact_get_capabilities (tp_contact));
 
+  contact_set_avatar_from_tp_contact (contact);
+
   g_signal_connect (priv->tp_contact, "notify",
     G_CALLBACK (tp_contact_notify_cb), contact);
 }
@@ -957,44 +965,6 @@ contact_get_avatar_filename (EmpathyContact *contact,
   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)
@@ -1465,3 +1435,34 @@ set_capabilities_from_tp_caps (EmpathyContact *self,
   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);
+    }
+}
+
index 002930bd56b9e73adfc97fff29a20b2328c690f2..43f20957f58f4716e8a06c5b9a69be5027789f74 100644 (file)
@@ -110,10 +110,6 @@ gboolean empathy_contact_can_voip_audio (EmpathyContact *contact);
 gboolean empathy_contact_can_voip_video (EmpathyContact *contact);
 gboolean empathy_contact_can_send_files (EmpathyContact *contact);
 gboolean empathy_contact_can_use_stream_tube (EmpathyContact *contact);
-
-void empathy_contact_load_avatar_data (EmpathyContact *contact,
-    const guchar *data, const gsize len, const gchar *format,
-    const gchar *token);
 gboolean empathy_contact_load_avatar_cache (EmpathyContact *contact,
     const gchar *token);
 
index b533a06dc2a87abf0759d61ada1b167eb4e5d34c..187d9940d82560e80c3e7a9365490ec750337189 100644 (file)
@@ -51,27 +51,13 @@ enum {
 
 static TpContactFeature contact_features[] = {
        TP_CONTACT_FEATURE_ALIAS,
+       TP_CONTACT_FEATURE_AVATAR_TOKEN,
+       TP_CONTACT_FEATURE_AVATAR_DATA,
        TP_CONTACT_FEATURE_PRESENCE,
        TP_CONTACT_FEATURE_LOCATION,
        TP_CONTACT_FEATURE_CAPABILITIES,
 };
 
-static EmpathyContact *
-tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
-                                  guint                    handle)
-{
-       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-       GList                       *l;
-
-       for (l = priv->contacts; l; l = l->next) {
-               if (empathy_contact_get_handle (l->data) == handle) {
-                       return l->data;
-               }
-       }
-
-       return NULL;
-}
-
 static EmpathyContact *
 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
                                       TpContact               *tp_contact)
@@ -110,156 +96,6 @@ tp_contact_factory_set_aliases_cb (TpConnection *connection,
        }
 }
 
-static void
-tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
-                                       guint         handle,
-                                       const gchar  *token,
-                                       const GArray *avatar_data,
-                                       const gchar  *mime_type,
-                                       gpointer      user_data,
-                                       GObject      *tp_factory)
-{
-       EmpathyContact *contact;
-
-       contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
-                                                    handle);
-       if (!contact) {
-               return;
-       }
-
-       DEBUG ("Avatar retrieved for contact %s (%d)",
-               empathy_contact_get_id (contact),
-               handle);
-
-       empathy_contact_load_avatar_data (contact,
-                                         (guchar *) avatar_data->data,
-                                         avatar_data->len,
-                                         mime_type,
-                                         token);
-}
-
-static void
-tp_contact_factory_request_avatars_cb (TpConnection *connection,
-                                      const GError *error,
-                                      gpointer      user_data,
-                                      GObject      *tp_factory)
-{
-       if (error) {
-               DEBUG ("Error: %s", error->message);
-       }
-}
-
-static gboolean
-tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
-                                       guint                    handle,
-                                       const gchar             *token)
-{
-       EmpathyContact *contact;
-       EmpathyAvatar  *avatar;
-
-       contact = tp_contact_factory_find_by_handle (tp_factory, handle);
-       if (!contact) {
-               return TRUE;
-       }
-
-       /* Check if we have an avatar */
-       if (EMP_STR_EMPTY (token)) {
-               empathy_contact_set_avatar (contact, NULL);
-               return TRUE;
-       }
-
-       /* Check if the avatar changed */
-       avatar = empathy_contact_get_avatar (contact);
-       if (avatar && !tp_strdiff (avatar->token, token)) {
-               return TRUE;
-       }
-
-       /* The avatar changed, search the new one in the cache */
-       if (empathy_contact_load_avatar_cache (contact, token)) {
-               /* Got from cache, use it */
-               return TRUE;
-       }
-
-       /* Avatar is not up-to-date, we have to request it. */
-       return FALSE;
-}
-
-static void
-tp_contact_factory_got_known_avatar_tokens (TpConnection *connection,
-                                           GHashTable   *tokens,
-                                           const GError *error,
-                                           gpointer      user_data,
-                                           GObject      *weak_object)
-{
-       EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
-       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-       GArray *handles;
-       GHashTableIter iter;
-       gpointer key, value;
-
-       if (error) {
-               DEBUG ("Error: %s", error->message);
-               return;
-       }
-
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-
-       g_hash_table_iter_init (&iter, tokens);
-       while (g_hash_table_iter_next (&iter, &key, &value)) {
-               guint handle = GPOINTER_TO_UINT (key);
-               const gchar *token = value;
-
-               if (!tp_contact_factory_avatar_maybe_update (tp_factory,
-                                                            handle, token)) {
-                       g_array_append_val (handles, handle);
-               }
-       }
-
-       DEBUG ("Got %d tokens, need to request %d avatars",
-               g_hash_table_size (tokens), handles->len);
-
-       /* Request needed avatars */
-       if (handles->len > 0) {
-               tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
-                                                                         -1,
-                                                                         handles,
-                                                                         tp_contact_factory_request_avatars_cb,
-                                                                         NULL, NULL,
-                                                                         G_OBJECT (tp_factory));
-       }
-
-       g_array_free (handles, TRUE);
-}
-
-static void
-tp_contact_factory_avatar_updated_cb (TpConnection *connection,
-                                     guint         handle,
-                                     const gchar  *new_token,
-                                     gpointer      user_data,
-                                     GObject      *tp_factory)
-{
-       GArray *handles;
-
-       if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
-                                                   handle, new_token)) {
-               /* Avatar was cached, nothing to do */
-               return;
-       }
-
-       DEBUG ("Need to request avatar for token %s", new_token);
-
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, handle);
-
-       tp_cli_connection_interface_avatars_call_request_avatars (connection,
-                                                                 -1,
-                                                                 handles,
-                                                                 tp_contact_factory_request_avatars_cb,
-                                                                 NULL, NULL,
-                                                                 tp_factory);
-       g_array_free (handles, TRUE);
-}
-
 static void
 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
                                EmpathyContact          *contact)
@@ -267,7 +103,6 @@ tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
        EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
        TpHandle self_handle;
        TpHandle handle;
-       GArray handles = {(gchar *) &handle, 1};
 
        /* Keep a weak ref to that contact */
        g_object_weak_ref (G_OBJECT (contact),
@@ -287,15 +122,6 @@ tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
        handle = empathy_contact_get_handle (contact);
        empathy_contact_set_is_user (contact, self_handle == handle);
 
-       /* FIXME: This should be done by TpContact */
-       if (tp_proxy_has_interface_by_id (priv->connection,
-                       TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS)) {
-               tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
-                       priv->connection, -1, &handles,
-                       tp_contact_factory_got_known_avatar_tokens, NULL, NULL,
-                       G_OBJECT (tp_factory));
-       }
-
        DEBUG ("Contact added: %s (%d)",
                empathy_contact_get_id (contact),
                empathy_contact_get_handle (contact));
@@ -733,60 +559,12 @@ tp_contact_factory_finalize (GObject *object)
        G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
 }
 
-static void
-connection_ready_cb (TpConnection *connection,
-                               const GError *error,
-                               gpointer user_data)
-{
-       EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
-       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-
-       if (error != NULL)
-               goto out;
-
-       /* FIXME: This should be moved to TpContact */
-       tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
-                                                                      tp_contact_factory_avatar_updated_cb,
-                                                                      NULL, NULL,
-                                                                      G_OBJECT (tp_factory),
-                                                                      NULL);
-       tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
-                                                                        tp_contact_factory_avatar_retrieved_cb,
-                                                                        NULL, NULL,
-                                                                        G_OBJECT (tp_factory),
-                                                                        NULL);
-
-out:
-       g_object_unref (tp_factory);
-}
-
-static GObject *
-tp_contact_factory_constructor (GType                  type,
-                               guint                  n_props,
-                               GObjectConstructParam *props)
-{
-       GObject *tp_factory;
-       EmpathyTpContactFactoryPriv *priv;
-
-       tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
-       priv = GET_PRIV (tp_factory);
-
-       /* Ensure to keep the self object alive while the call_when_ready is
-        * running */
-       g_object_ref (tp_factory);
-       tp_connection_call_when_ready (priv->connection, connection_ready_cb,
-               tp_factory);
-
-       return tp_factory;
-}
-
 static void
 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
        object_class->finalize = tp_contact_factory_finalize;
-       object_class->constructor = tp_contact_factory_constructor;
        object_class->get_property = tp_contact_factory_get_property;
        object_class->set_property = tp_contact_factory_set_property;