]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-contact.c
Merge branch 'people-nearby-fake-group-613558'
[empathy.git] / libempathy / empathy-contact.c
index 03ce8d28a397681eea6d296514e23aa81abcee78..df7d04ecaf94c30e8f84ee306d3c19a5abeda676 100644 (file)
 
 #include <glib/gi18n-lib.h>
 
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
+#ifdef ENABEL_TPL
+#include <telepathy-logger/contact.h>
+#endif /* ENABLE_TPL */
 
 #include "empathy-contact.h"
-#include "empathy-account-manager.h"
 #include "empathy-utils.h"
 #include "empathy-enum-types.h"
 #include "empathy-marshal.h"
@@ -39,7 +42,7 @@
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
 typedef struct {
   TpContact *tp_contact;
-  McAccount *account;
+  TpAccount *account;
   gchar *id;
   gchar *name;
   EmpathyAvatar *avatar;
@@ -159,7 +162,7 @@ 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,
@@ -408,14 +411,41 @@ empathy_contact_new (TpContact *tp_contact)
       NULL);
 }
 
+#ifdef ENABLE_TPL
 EmpathyContact *
-empathy_contact_new_for_log (McAccount *account,
+empathy_contact_from_tpl_contact (TpAccount *account,
+    TplContact *tpl_contact)
+{
+  EmpathyContact *retval;
+  gboolean is_user;
+
+  g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
+
+  is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
+
+  retval = g_object_new (EMPATHY_TYPE_CONTACT,
+      "id", tpl_contact_get_alias (tpl_contact),
+      "name", tpl_contact_get_identifier (tpl_contact),
+      "account", account,
+      "is-user", is_user,
+      NULL);
+
+  if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
+    empathy_contact_load_avatar_cache (retval,
+        tpl_contact_get_avatar_token (tpl_contact));
+
+  return retval;
+}
+#endif /* ENABLE_TPL */
+
+EmpathyContact *
+empathy_contact_new_for_log (TpAccount *account,
                              const gchar *id,
                              const gchar *name,
                              gboolean is_user)
 {
-  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
   g_return_val_if_fail (id != NULL, NULL);
+  g_assert (account != NULL);
 
   return g_object_new (EMPATHY_TYPE_CONTACT,
       "account", account,
@@ -483,18 +513,21 @@ const gchar *
 empathy_contact_get_name (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)
-    return tp_contact_get_alias (priv->tp_contact);
-
-  if (EMP_STR_EMPTY (priv->name))
-      return empathy_contact_get_id (contact);
-
-  return priv->name;
+    alias = tp_contact_get_alias (priv->tp_contact);
+  else
+    alias = priv->name;
+
+  if (!EMP_STR_EMPTY (alias))
+    return alias;
+  else
+    return empathy_contact_get_id (contact);
 }
 
 void
@@ -554,7 +587,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,15 +598,12 @@ 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;
@@ -802,6 +832,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)
 {
@@ -830,32 +884,27 @@ static gchar *
 contact_get_avatar_filename (EmpathyContact *contact,
                              const gchar *token)
 {
-  EmpathyContactPriv *priv = GET_PRIV (contact);
-  McAccount *account;
+  TpAccount *account;
   gchar *avatar_path;
   gchar *avatar_file;
   gchar *token_escaped;
-  gchar *contact_escaped;
 
-  if (EMP_STR_EMPTY (priv->id))
+  if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
     return NULL;
 
-  contact_escaped = tp_escape_as_identifier (priv->id);
   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);
 
@@ -864,7 +913,7 @@ contact_get_avatar_filename (EmpathyContact *contact,
 
 void
 empathy_contact_load_avatar_data (EmpathyContact *contact,
-                                  const guchar  *data,
+                                  const guchar *data,
                                   const gsize len,
                                   const gchar *format,
                                   const gchar *token)
@@ -880,13 +929,13 @@ empathy_contact_load_avatar_data (EmpathyContact *contact,
   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));
+      g_strdup (token), filename);
   empathy_contact_set_avatar (contact, avatar);
   empathy_avatar_unref (avatar);
 
   /* Save to cache if not yet in it */
-  filename = contact_get_avatar_filename (contact, token);
   if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
     {
       if (!empathy_avatar_save_to_file (avatar, filename, &error))
@@ -898,7 +947,6 @@ empathy_contact_load_avatar_data (EmpathyContact *contact,
       else
           DEBUG ("Avatar saved to %s", filename);
     }
-  g_free (filename);
 }
 
 gboolean
@@ -929,12 +977,15 @@ 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));
+      avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
+          filename);
       empathy_contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
     }
-
-  g_free (filename);
+  else
+    {
+      g_free (filename);
+    }
 
   return data != NULL;
 }
@@ -954,11 +1005,25 @@ empathy_avatar_get_type (void)
   return type_id;
 }
 
+/**
+ * empathy_avatar_new:
+ * @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.
+ *
+ * Returns: a new #EmpathyAvatar
+ */
 EmpathyAvatar *
 empathy_avatar_new (guchar *data,
                     gsize len,
                     gchar *format,
-                    gchar *token)
+                    gchar *token,
+                    gchar *filename)
 {
   EmpathyAvatar *avatar;
 
@@ -967,6 +1032,7 @@ empathy_avatar_new (guchar *data,
   avatar->len = len;
   avatar->format = format;
   avatar->token = token;
+  avatar->filename = filename;
   avatar->refcount = 1;
 
   return avatar;
@@ -1012,7 +1078,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);
 }
 
 /**
@@ -1071,3 +1138,40 @@ empathy_contact_set_location (EmpathyContact *contact,
   priv->location = g_hash_table_ref (location);
   g_object_notify (G_OBJECT (contact), "location");
 }
+
+/**
+ * empathy_contact_equal:
+ * @contact1: an #EmpathyContact
+ * @contact2: an #EmpathyContact
+ *
+ * Returns FALSE if one of the contacts is NULL but the other is not.
+ * Otherwise returns TRUE if both pointer are equal or if they bith
+ * refer to the same id.
+ * It's only necessary to call this function if your contact objects
+ * come from logs where contacts are created dynamically and comparing
+ * pointers is not enough.
+ */
+gboolean
+empathy_contact_equal (gconstpointer contact1,
+                       gconstpointer contact2)
+{
+  EmpathyContact *c1;
+  EmpathyContact *c2;
+  const gchar *id1;
+  const gchar *id2;
+
+  if ((contact1 == NULL) != (contact2 == NULL)) {
+    return FALSE;
+  }
+  if (contact1 == contact2) {
+    return TRUE;
+  }
+  c1 = EMPATHY_CONTACT (contact1);
+  c2 = EMPATHY_CONTACT (contact2);
+  id1 = empathy_contact_get_id (c1);
+  id2 = empathy_contact_get_id (c2);
+  if (!tp_strdiff (id1, id2)) {
+    return TRUE;
+  }
+  return FALSE;
+}