]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-contact-factory.c
Use tp-glib generated code for file transfers rather than extensions.
[empathy.git] / libempathy / empathy-tp-contact-factory.c
index fa7befc0206b7e9f4065be1d90ce3a1c5d56c793..d83ba12a8ce146203497f4e86d664272c96ddb09 100644 (file)
 
 #include <telepathy-glib/util.h>
 #include <telepathy-glib/connection.h>
+#include <telepathy-glib/gtypes.h>
 #include <libmissioncontrol/mission-control.h>
 
+#include <extensions/extensions.h>
+
 #include "empathy-tp-contact-factory.h"
 #include "empathy-utils.h"
+#include "empathy-account-manager.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
 #include "empathy-debug.h"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
 typedef struct {
-       MissionControl *mc;
+       EmpathyAccountManager *account_manager;
        McAccount      *account;
        TpConnection   *connection;
        gboolean        ready;
 
        GList          *contacts;
        EmpathyContact *user;
-       gpointer        token;
 
        gchar         **avatar_mime_types;
        guint           avatar_min_width;
@@ -50,6 +53,7 @@ typedef struct {
        guint           avatar_max_width;
        guint           avatar_max_height;
        guint           avatar_max_size;
+  gboolean        can_request_ft;
 } EmpathyTpContactFactoryPriv;
 
 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
@@ -67,20 +71,6 @@ enum {
        PROP_MAX_SIZE
 };
 
-/* Prototypes */
-static void tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
-                                                          const gchar **mime_types,
-                                                          guint         min_width,
-                                                          guint         min_height,
-                                                          guint         max_width,
-                                                          guint         max_height,
-                                                          guint         max_size,
-                                                          const GError *error,
-                                                          gpointer      user_data,
-                                                          GObject      *tp_factory);
-static void tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory);
-
-
 static EmpathyContact *
 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
                                   guint                    handle)
@@ -140,7 +130,7 @@ tp_contact_factory_presences_table_foreach (const gchar    *state_str,
                message_str = g_value_get_string (message);
        }
 
-       if (!G_STR_EMPTY (message_str)) {
+       if (!EMP_STR_EMPTY (message_str)) {
                empathy_contact_set_presence_message (contact, message_str);
        } else {
                empathy_contact_set_presence_message (contact, NULL);
@@ -394,7 +384,7 @@ tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
        }
 
        /* Check if we have an avatar */
-       if (G_STR_EMPTY (token)) {
+       if (EMP_STR_EMPTY (token)) {
                empathy_contact_set_avatar (contact, NULL);
                return TRUE;
        }
@@ -739,66 +729,6 @@ tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factor
                        tp_factory);
 }
 
-
-static void
-tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
-                                      guint         handle,
-                                      const GError *error,
-                                      gpointer      user_data,
-                                      GObject      *tp_factory)
-{
-       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-
-       if (error) {
-               DEBUG ("Failed to get self handles: %s", error->message);
-               return;
-       }
-
-       empathy_contact_set_handle (priv->user, handle);
-
-       /* Get avatar requirements for this connection */
-       tp_cli_connection_interface_avatars_call_get_avatar_requirements (
-               priv->connection,
-               -1,
-               tp_contact_factory_got_avatar_requirements_cb,
-               NULL, NULL,
-               tp_factory);
-}
-
-
-static void
-tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
-                                              const gchar **mime_types,
-                                              guint         min_width,
-                                              guint         min_height,
-                                              guint         max_width,
-                                              guint         max_height,
-                                              guint         max_size,
-                                              const GError *error,
-                                              gpointer      user_data,
-                                              GObject      *tp_factory)
-{
-       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-
-       if (error) {
-               DEBUG ("Failed to get avatar requirements: %s", error->message);
-               /* We'll just leave avatar_mime_types as NULL; the
-                * avatar-setting code can use this as a signal that you can't
-                * set avatars.
-                */
-       } else {
-               priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
-               priv->avatar_min_width = min_width;
-               priv->avatar_min_height = min_height;
-               priv->avatar_max_width = max_width;
-               priv->avatar_max_height = max_height;
-               priv->avatar_max_size = max_size;
-       }
-
-       tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
-}
-
-
 static void
 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
 {
@@ -853,12 +783,12 @@ tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
                handle = empathy_contact_get_handle (contact);
                id = empathy_contact_get_id (contact);
                if (handle == 0) {
-                       g_assert (!G_STR_EMPTY (id));
+                       g_assert (!EMP_STR_EMPTY (id));
                        g_array_append_val (handle_needed, id);
                        handle_needed_contacts = g_list_prepend (handle_needed_contacts,
                                                                 g_object_ref (contact));
                }
-               if (G_STR_EMPTY (id)) {
+               if (EMP_STR_EMPTY (id)) {
                        g_array_append_val (id_needed, handle);
                        id_needed_contacts = g_list_prepend (id_needed_contacts,
                                                             g_object_ref (contact));
@@ -890,6 +820,133 @@ tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
        g_array_free (id_needed, TRUE);
 }
 
+static void
+get_requestable_channel_classes_cb (TpProxy *connection,
+                                   const GValue *value,
+                                   const GError *error,
+                                   gpointer user_data,
+                                   GObject *weak_object)
+{
+       EmpathyTpContactFactory     *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
+       EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
+       GPtrArray                   *classes;
+       guint                        i;
+
+       if (error != NULL) {
+               DEBUG ("Error: %s", error->message);
+               tp_contact_factory_ready (self);
+               return;
+       }
+
+       classes = g_value_get_boxed (value);
+       for (i = 0; i < classes->len; i++) {
+               GValue class = {0,};
+               GValue *chan_type, *handle_type;
+               GHashTable *fixed_prop;
+               GList *l;
+
+               g_value_init (&class, TP_STRUCT_TYPE_REQUESTABLE_CHANNEL_CLASS);
+               g_value_set_static_boxed (&class, g_ptr_array_index (classes, i));
+
+               dbus_g_type_struct_get (&class,
+                                       0, &fixed_prop,
+                                       G_MAXUINT);
+
+               chan_type = g_hash_table_lookup (fixed_prop,
+                       TP_IFACE_CHANNEL ".ChannelType");
+               if (chan_type == NULL ||
+                   tp_strdiff (g_value_get_string (chan_type),
+                               TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
+                       continue;
+               }
+
+               handle_type = g_hash_table_lookup (fixed_prop,
+                       TP_IFACE_CHANNEL ".TargetHandleType");
+               if (handle_type == NULL ||
+                   g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT) {
+                       continue;
+               }
+
+               /* We can request file transfer channel to contacts. */
+               priv->can_request_ft = TRUE;
+
+               /* Update the capabilities of all contacts */
+               for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
+                       EmpathyContact *contact = l->data;
+                       EmpathyCapabilities caps;
+
+                       caps = empathy_contact_get_capabilities (contact);
+                       empathy_contact_set_capabilities (contact, caps |
+                               EMPATHY_CAPABILITIES_FT);
+               }
+               break;
+       }
+
+       tp_contact_factory_ready (self);
+}
+
+static void
+tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
+                                              const gchar **mime_types,
+                                              guint         min_width,
+                                              guint         min_height,
+                                              guint         max_width,
+                                              guint         max_height,
+                                              guint         max_size,
+                                              const GError *error,
+                                              gpointer      user_data,
+                                              GObject      *tp_factory)
+{
+       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
+
+       if (error) {
+               DEBUG ("Failed to get avatar requirements: %s", error->message);
+               /* We'll just leave avatar_mime_types as NULL; the
+                * avatar-setting code can use this as a signal that you can't
+                * set avatars.
+                */
+       } else {
+               priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
+               priv->avatar_min_width = min_width;
+               priv->avatar_min_height = min_height;
+               priv->avatar_max_width = max_width;
+               priv->avatar_max_height = max_height;
+               priv->avatar_max_size = max_size;
+       }
+
+       /* Can we request file transfer channels? */
+       tp_cli_dbus_properties_call_get (priv->connection, -1,
+               TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
+               "RequestableChannelClasses",
+               get_requestable_channel_classes_cb, NULL, NULL,
+               G_OBJECT (tp_factory));
+}
+
+static void
+tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
+                                      guint         handle,
+                                      const GError *error,
+                                      gpointer      user_data,
+                                      GObject      *tp_factory)
+{
+       EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
+
+       if (error) {
+               DEBUG ("Failed to get self handles: %s", error->message);
+               return;
+       }
+
+       empathy_contact_set_handle (priv->user, handle);
+
+       /* Get avatar requirements for this connection */
+       tp_cli_connection_interface_avatars_call_get_avatar_requirements (
+               priv->connection,
+               -1,
+               tp_contact_factory_got_avatar_requirements_cb,
+               NULL, NULL,
+               tp_factory);
+}
+
 static void
 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
 {
@@ -908,13 +965,15 @@ tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
 {
        EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
        gboolean                     connection_ready;
+       MissionControl              *mc;
 
        if (priv->connection) {
                /* We already have our connection object */
                return;
        }
 
-       priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
+       mc = empathy_mission_control_dup_singleton ();
+       priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
        if (!priv->connection) {
                return;
        }
@@ -932,24 +991,23 @@ tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
                                          G_CALLBACK (tp_contact_factory_connection_ready_cb),
                                          tp_factory);
        }
+
+       g_object_unref (mc);
 }
 
 static void
-tp_contact_factory_status_changed_cb (MissionControl           *mc,
-                                     TpConnectionStatus        status,
-                                     McPresence                presence,
-                                     TpConnectionStatusReason  reason,
-                                     const gchar              *unique_name,
-                                     EmpathyTpContactFactory  *tp_factory)
+tp_contact_factory_account_connection_cb (EmpathyAccountManager *account_manager,
+                                         McAccount *account,
+                                         TpConnectionStatusReason reason,
+                                         TpConnectionStatus current,
+                                         TpConnectionStatus previous,
+                                         EmpathyTpContactFactory  *tp_factory)
 {
        EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
-       McAccount                   *account;
 
-       account = mc_account_lookup (unique_name);
        if (account && empathy_account_equal (account, priv->account)) {
                tp_contact_factory_status_updated (tp_factory);
        }
-       g_object_unref (account);
 }
 
 static void
@@ -989,6 +1047,27 @@ empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
        return g_object_ref (priv->user);
 }
 
+static void
+contact_created (EmpathyTpContactFactory *self,
+                 EmpathyContact *contact)
+{
+  EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
+
+  if (priv->can_request_ft)
+    {
+      /* Set the FT capability */
+      /* FIXME: We should use the futur ContactCapabilities interface */
+      EmpathyCapabilities caps;
+
+      caps = empathy_contact_get_capabilities (contact);
+      caps |= EMPATHY_CAPABILITIES_FT;
+
+      empathy_contact_set_capabilities (contact, caps);
+    }
+
+  tp_contact_factory_add_contact (self, contact);
+}
+
 EmpathyContact *
 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
                                        const gchar             *id)
@@ -1010,7 +1089,7 @@ empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
                                "account", priv->account,
                                "id", id,
                                NULL);
-       tp_contact_factory_add_contact (tp_factory, contact);
+       contact_created (tp_factory, contact);
 
        if (priv->ready) {
                const gchar *contact_ids[] = {id, NULL};
@@ -1099,7 +1178,7 @@ empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory
                                        "account", priv->account,
                                        "handle", handle,
                                        NULL);
-               tp_contact_factory_add_contact (tp_factory, contact);
+               contact_created (tp_factory, contact);
                contacts = g_list_prepend (contacts, contact);
                new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
        }
@@ -1288,7 +1367,9 @@ tp_contact_factory_finalize (GObject *object)
        DEBUG ("Finalized: %p (%s)", object,
                mc_account_get_normalized_name (priv->account));
 
-       empathy_disconnect_account_status_changed (priv->token);
+       g_signal_handlers_disconnect_by_func (priv->account_manager,
+                                             tp_contact_factory_account_connection_cb,
+                                             object);
 
        for (l = priv->contacts; l; l = l->next) {
                g_object_weak_unref (G_OBJECT (l->data),
@@ -1297,7 +1378,7 @@ tp_contact_factory_finalize (GObject *object)
        }
 
        g_list_free (priv->contacts);
-       g_object_unref (priv->mc);
+       g_object_unref (priv->account_manager);
        g_object_unref (priv->account);
        g_object_unref (priv->user);
 
@@ -1440,10 +1521,13 @@ empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
                EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
 
        tp_factory->priv = priv;
-       priv->mc = empathy_mission_control_new ();
-       priv->token = empathy_connect_to_account_status_changed (priv->mc,
-                                                  G_CALLBACK (tp_contact_factory_status_changed_cb),
-                                                  tp_factory, NULL);
+       priv->account_manager = empathy_account_manager_dup_singleton ();
+
+       g_signal_connect (priv->account_manager, "account-connection-changed",
+                         G_CALLBACK (tp_contact_factory_account_connection_cb),
+                         tp_factory);
+
+       priv->can_request_ft = FALSE;
 }
 
 EmpathyTpContactFactory *