]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-dispatcher.c
Updated Polish translation
[empathy.git] / libempathy / empathy-dispatcher.c
index 40045cdb10215644e63dd2abc71522147d59be17..caf697cb68f419fec53e0f66db27e844630af5b1 100644 (file)
@@ -31,6 +31,8 @@
 #include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/enums.h>
 #include <telepathy-glib/connection.h>
+#include <telepathy-glib/channel-dispatcher.h>
+#include <telepathy-glib/channel-request.h>
 #include <telepathy-glib/util.h>
 #include <telepathy-glib/dbus.h>
 #include <telepathy-glib/proxy-subclass.h>
@@ -45,7 +47,6 @@
 #include "empathy-dispatcher.h"
 #include "empathy-handler.h"
 #include "empathy-utils.h"
-#include "empathy-tube-handler.h"
 #include "empathy-tp-contact-factory.h"
 #include "empathy-chatroom-manager.h"
 #include "empathy-utils.h"
@@ -63,7 +64,6 @@ typedef struct
   GHashTable *connections;
   GHashTable *outstanding_classes_requests;
   gpointer token;
-  GSList *tubes;
 
   /* channels which the dispatcher is listening "invalidated" */
   GList *channels;
@@ -76,6 +76,12 @@ typedef struct
   GList *handlers;
 
   GHashTable *request_channel_class_async_ids;
+  /* (TpAccount *) => gulong
+   * Signal handler ID of the "status-changed" signal */
+  GHashTable *status_changed_handlers;
+
+  TpChannelDispatcher *channel_dispatcher;
+  TpDBusDaemon *dbus;
 } EmpathyDispatcherPriv;
 
 static GList *
@@ -138,6 +144,8 @@ typedef struct
   EmpathyDispatcherRequestCb *cb;
   gpointer user_data;
   gpointer *request_data;
+
+  TpChannelRequest *channel_request;
 } DispatcherRequestData;
 
 typedef struct
@@ -154,16 +162,6 @@ typedef struct
   /* ObjectPath -> EmpathyDispatchOperations */
   GHashTable *dispatching_channels;
 
-  /* ObjectPath -> EmpathyDispatchOperations
-   *
-   * This holds channels which were announced with NewChannel while we have an
-   * outstanding channel request for a channel of this type. On the Requests
-   * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
-   * return before NewChannels is emitted, but there was no guarantee of the
-   * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
-   * held in limbo here until we know whether they were requested.
-   */
-  GHashTable *outstanding_channels;
   /* List of DispatcherRequestData */
   GList *outstanding_requests;
   /* List of requestable channel classes */
@@ -186,6 +184,11 @@ empathy_dispatcher_call_create_or_ensure_channel (
     EmpathyDispatcher *dispatcher,
     DispatcherRequestData *request_data);
 
+static void
+dispatcher_request_failed (EmpathyDispatcher *dispatcher,
+    DispatcherRequestData *request_data,
+    const GError *error);
+
 static DispatchData *
 new_dispatch_data (TpChannel *channel,
                    GObject *channel_wrapper)
@@ -254,9 +257,13 @@ free_dispatcher_request_data (DispatcherRequestData *r)
   if (r->request != NULL)
     g_hash_table_unref (r->request);
 
+
   if (r->pending_call != NULL)
     tp_proxy_pending_call_cancel (r->pending_call);
 
+  if (r->channel_request != NULL)
+    g_object_unref (r->channel_request);
+
   g_slice_free (DispatcherRequestData, r);
 }
 
@@ -271,9 +278,6 @@ new_connection_data (void)
   cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
       g_free, g_object_unref);
 
-  cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
-      g_free, NULL);
-
   return cd;
 }
 
@@ -336,28 +340,6 @@ dispatcher_connection_invalidated_cb (TpConnection *connection,
   g_hash_table_remove (priv->connections, connection);
 }
 
-static gboolean
-dispatcher_operation_can_start (EmpathyDispatcher *self,
-                                EmpathyDispatchOperation *operation,
-                                ConnectionData *cd)
-{
-  GList *l;
-  const gchar *channel_type =
-    empathy_dispatch_operation_get_channel_type (operation);
-
-  for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
-    {
-      DispatcherRequestData *d = (DispatcherRequestData *) l->data;
-
-      if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
-        {
-          return FALSE;
-        }
-    }
-
-  return TRUE;
-}
-
 static void
 dispatch_operation_flush_requests (EmpathyDispatcher *self,
                                    EmpathyDispatchOperation *operation,
@@ -402,7 +384,6 @@ dispatcher_channel_invalidated_cb (TpProxy *proxy,
   /* Channel went away... */
   EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpConnection *connection;
-  EmpathyDispatchOperation *operation;
   ConnectionData *cd;
   const gchar *object_path;
 
@@ -421,15 +402,6 @@ dispatcher_channel_invalidated_cb (TpProxy *proxy,
   g_hash_table_remove (cd->dispatching_channels, object_path);
 
   priv->channels = g_list_remove (priv->channels, proxy);
-
-  operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
-  if (operation != NULL)
-    {
-      GError error = { domain, code, message };
-      dispatch_operation_flush_requests (self, operation, &error, cd);
-      g_hash_table_remove (cd->outstanding_channels, object_path);
-      g_object_unref (operation);
-    }
 }
 
 static void
@@ -539,9 +511,6 @@ dispatcher_start_dispatching (EmpathyDispatcher *self,
 
   if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
     {
-      g_assert (g_hash_table_lookup (cd->outstanding_channels,
-        object_path) == NULL);
-
       g_hash_table_insert (cd->dispatching_channels,
         g_strdup (object_path), operation);
 
@@ -569,26 +538,6 @@ dispatcher_start_dispatching (EmpathyDispatcher *self,
     }
 }
 
-static void
-dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
-                                         ConnectionData *cd)
-{
-  GHashTableIter iter;
-  gpointer value;
-
-  g_hash_table_iter_init (&iter, cd->outstanding_channels);
-  while (g_hash_table_iter_next (&iter, NULL, &value))
-    {
-      EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
-
-      if (dispatcher_operation_can_start (self, operation, cd))
-        {
-          g_hash_table_iter_remove (&iter);
-          dispatcher_start_dispatching (self, operation, cd);
-        }
-    }
-}
-
 static void
 dispatcher_connection_new_channel (EmpathyDispatcher *self,
                                    TpConnection *connection,
@@ -597,7 +546,8 @@ dispatcher_connection_new_channel (EmpathyDispatcher *self,
                                    guint handle_type,
                                    guint handle,
                                    GHashTable *properties,
-                                   gboolean incoming)
+                                   gboolean incoming,
+                                   const GPtrArray *requests_satisfied)
 {
   EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpChannel         *channel;
@@ -618,31 +568,6 @@ dispatcher_connection_new_channel (EmpathyDispatcher *self,
 
   cd = g_hash_table_lookup (priv->connections, connection);
 
-  /* Don't bother with channels we have already dispatched or are dispatching
-   * currently. This can happen when NewChannel(s) is fired after
-   * RequestChannel/CreateChannel/EnsureChannel */
-  if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
-    return;
-
-  if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
-    return;
-
-  /* Should never occur, but just in case a CM fires spurious NewChannel(s)
-   * signals */
-  if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
-    return;
-
-  /* Only pick up non-requested text and file channels. For all other it
-   * doesn't make sense to handle it if we didn't request it. The same goes
-   * for channels we discovered by the Channels property or ListChannels */
-  if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
-        && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
-    {
-      DEBUG ("Ignoring incoming channel of type %s on %s",
-        channel_type, object_path);
-      return;
-    }
-
   for (i = 0 ; blacklist[i] != NULL; i++)
     {
       if (!tp_strdiff (channel_type, blacklist[i]))
@@ -653,7 +578,17 @@ dispatcher_connection_new_channel (EmpathyDispatcher *self,
         }
     }
 
-  DEBUG ("New channel of type %s on %s", channel_type, object_path);
+  DEBUG ("%s channel of type %s on %s", incoming ? "incoming" : "outgoing",
+    channel_type, object_path);
+
+  if ((operation = g_hash_table_lookup (cd->dispatching_channels,
+      object_path)) != NULL)
+    {
+      /* This operation was already being dispatched, assume we got the channel
+       * again because something asked for it and approve it right away */
+      empathy_dispatch_operation_approve (operation);
+      return;
+    }
 
   if (properties == NULL)
     channel = tp_channel_new (connection, object_path, channel_type,
@@ -673,21 +608,55 @@ dispatcher_connection_new_channel (EmpathyDispatcher *self,
 
   g_object_unref (channel);
 
-  if (incoming)
+  if (!incoming && requests_satisfied != NULL)
     {
-      /* Request could either be by us or by a remote party. If there are no
-       * outstanding requests for this channel type we can assume it's remote.
-       * Otherwise we wait untill they are all satisfied */
-      if (dispatcher_operation_can_start (self, operation, cd))
-        dispatcher_start_dispatching (self, operation, cd);
-      else
-        g_hash_table_insert (cd->outstanding_channels,
-          g_strdup (object_path), operation);
-    }
-  else
-    {
-      dispatcher_start_dispatching (self, operation, cd);
+      GList *l;
+      gboolean found = FALSE;
+
+      l = cd->outstanding_requests;
+
+      while (l != NULL)
+        {
+          DispatcherRequestData *d = (DispatcherRequestData *) l->data;
+          guint n;
+          const gchar *path;
+
+          l = g_list_next (l);
+          if (d->request == NULL)
+            continue;
+
+          if (d->operation != NULL)
+            continue;
+
+          path = tp_proxy_get_object_path (d->channel_request);
+          for (n = 0; n < requests_satisfied->len ; n++)
+            {
+              const gchar *p = g_ptr_array_index (requests_satisfied, n);
+              if (!tp_strdiff (p, path))
+                {
+                  DEBUG ("Channel satified request %s"
+                   "(already dispatched: %d)", p, found);
+                  if (!found)
+                    {
+                      d->operation = operation;
+                      found = TRUE;
+                      continue;
+                    }
+                  else
+                    {
+                      GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
+                        "Not yours!" };
+                      dispatcher_request_failed (dispatcher, d, &err);
+                    }
+                }
+            }
+        }
     }
+
+  if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
+    empathy_dispatch_operation_approve (operation);
+
+  dispatcher_start_dispatching (dispatcher, operation, cd);
 }
 
 static void
@@ -695,7 +664,8 @@ dispatcher_connection_new_channel_with_properties (
     EmpathyDispatcher *self,
     TpConnection *connection,
     const gchar *object_path,
-    GHashTable *properties)
+    GHashTable *properties,
+    const GPtrArray *requests_satisfied)
 {
   const gchar *channel_type;
   guint handle_type;
@@ -703,7 +673,6 @@ dispatcher_connection_new_channel_with_properties (
   gboolean requested;
   gboolean valid;
 
-
   channel_type = tp_asv_get_string (properties,
     TP_IFACE_CHANNEL ".ChannelType");
   if (channel_type == NULL)
@@ -739,7 +708,8 @@ dispatcher_connection_new_channel_with_properties (
     }
 
   dispatcher_connection_new_channel (self, connection,
-    object_path, channel_type, handle_type, handle, properties, !requested);
+    object_path, channel_type, handle_type, handle, properties, !requested,
+    requests_satisfied);
 }
 
 static void
@@ -752,7 +722,6 @@ dispatcher_connection_got_all (TpProxy *proxy,
   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
   EmpathyDispatcherPriv *priv = GET_PRIV (self);
   GPtrArray *requestable_channels;
-  GPtrArray *existing_channels;
 
   if (error) {
     DEBUG ("Error: %s", error->message);
@@ -797,32 +766,6 @@ dispatcher_connection_got_all (TpProxy *proxy,
 
       g_hash_table_remove (priv->outstanding_classes_requests, proxy);
     }
-
-  existing_channels = tp_asv_get_boxed (properties,
-      "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
-
-  if (existing_channels != NULL)
-    {
-      guint idx;
-
-      for (idx = 0; idx < existing_channels->len; idx++)
-        {
-          GValueArray *values = g_ptr_array_index (existing_channels, idx);
-          const gchar *object_path;
-          GHashTable *props;
-
-          object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
-          props = g_value_get_boxed (g_value_array_get_nth (values, 1));
-
-          if (tp_strdiff (tp_asv_get_string (props,
-                      TP_IFACE_CHANNEL ".ChannelType"),
-                  TP_IFACE_CHANNEL_TYPE_TEXT))
-            continue;
-
-          dispatcher_connection_new_channel_with_properties (self,
-              TP_CONNECTION (proxy), object_path, props);
-        }
-    }
 }
 
 static void
@@ -837,23 +780,21 @@ dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
 }
 
 static void
-dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
-    TpConnection *connection)
+connection_ready_cb (TpConnection *connection,
+    const GError *error,
+    gpointer user_data)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (self);
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
   GPtrArray   *capabilities;
   GType        cap_type;
   GValue       cap = {0, };
   const gchar *remove_ = NULL;
 
-  if (g_hash_table_lookup (priv->connections, connection) != NULL)
-    return;
-
-  g_hash_table_insert (priv->connections, g_object_ref (connection),
-    new_connection_data ());
-
-  g_signal_connect (connection, "invalidated",
-    G_CALLBACK (dispatcher_connection_invalidated_cb), self);
+  if (error != NULL)
+    {
+      DEBUG ("Error: %s", error->message);
+      goto out;
+    }
 
   if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
@@ -887,6 +828,29 @@ dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
 
   g_value_unset (&cap);
   g_ptr_array_free (capabilities, TRUE);
+out:
+  g_object_unref (self);
+}
+
+static void
+dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
+    TpConnection *connection)
+{
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
+
+  if (g_hash_table_lookup (priv->connections, connection) != NULL)
+    return;
+
+  g_hash_table_insert (priv->connections, g_object_ref (connection),
+    new_connection_data ());
+
+  g_signal_connect (connection, "invalidated",
+    G_CALLBACK (dispatcher_connection_invalidated_cb), self);
+
+  /* Ensure to keep the self object alive while the call_when_ready is
+   * running */
+  g_object_ref (self);
+  tp_connection_call_when_ready (connection, connection_ready_cb, self);
 }
 
 static void
@@ -981,6 +945,14 @@ dispatcher_dispose (GObject *object)
   g_hash_table_destroy (priv->connections);
   priv->connections = NULL;
 
+  if (priv->channel_dispatcher != NULL)
+    g_object_unref (priv->channel_dispatcher);
+  priv->channel_dispatcher = NULL;
+
+  if (priv->dbus != NULL)
+    g_object_unref (priv->dbus);
+  priv->dbus = NULL;
+
   G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
 }
 
@@ -992,6 +964,7 @@ dispatcher_finalize (GObject *object)
   GHashTableIter iter;
   gpointer connection;
   GList *list;
+  gpointer account, id;
 
   if (priv->request_channel_class_async_ids != NULL)
     {
@@ -1015,6 +988,13 @@ dispatcher_finalize (GObject *object)
       g_list_free (list);
     }
 
+  g_hash_table_iter_init (&iter, priv->status_changed_handlers);
+  while (g_hash_table_iter_next (&iter, &account, &id))
+    {
+      g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
+    }
+  g_hash_table_destroy (priv->status_changed_handlers);
+
   g_object_unref (priv->account_manager);
 
   g_hash_table_destroy (priv->outstanding_classes_requests);
@@ -1114,6 +1094,30 @@ empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
 }
 
+static void
+connect_account (EmpathyDispatcher *self,
+    TpAccount *account)
+{
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
+  TpConnection *conn = tp_account_get_connection (account);
+  gulong id;
+
+  id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
+        account));
+
+  if (id != 0)
+    return;
+
+  if (conn != NULL)
+    dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
+
+  id = g_signal_connect (account, "status-changed",
+      G_CALLBACK (dispatcher_status_changed_cb), self);
+
+  g_hash_table_insert (priv->status_changed_handlers, account,
+      GUINT_TO_POINTER (id));
+}
+
 static void
 account_manager_prepared_cb (GObject *source_object,
                              GAsyncResult *result,
@@ -1135,18 +1139,43 @@ account_manager_prepared_cb (GObject *source_object,
   for (l = accounts; l; l = l->next)
     {
       TpAccount *a = l->data;
-      TpConnection *conn = tp_account_get_connection (a);
 
-      if (conn != NULL)
-        dispatcher_status_changed_cb (a, 0, 0, 0, NULL, NULL, self);
-
-      empathy_signal_connect_weak (a, "status-changed",
-          G_CALLBACK (dispatcher_status_changed_cb),
-          G_OBJECT (self));
+      connect_account (self, a);
     }
   g_list_free (accounts);
 }
 
+static void
+account_prepare_cb (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  EmpathyDispatcher *self = user_data;
+  TpAccount *account = TP_ACCOUNT (source_object);
+  GError *error = NULL;
+
+  if (!tp_account_prepare_finish (account, result, &error))
+    {
+      DEBUG ("Failed to prepare account: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  connect_account (self, account);
+}
+
+static void
+account_validity_changed_cb (TpAccountManager *manager,
+    TpAccount *account,
+    gboolean valid,
+    gpointer user_data)
+{
+  if (!valid)
+    return;
+
+  tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
+}
+
 static void
 empathy_dispatcher_init (EmpathyDispatcher *self)
 {
@@ -1167,8 +1196,17 @@ empathy_dispatcher_init (EmpathyDispatcher *self)
   tp_account_manager_prepare_async (priv->account_manager, NULL,
       account_manager_prepared_cb, self);
 
+  empathy_signal_connect_weak (priv->account_manager,
+      "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
+      G_OBJECT (self));
+
   priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
     g_direct_equal);
+  priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
+      g_direct_equal);
+
+  priv->dbus = tp_dbus_daemon_dup (NULL);
+  priv->channel_dispatcher = tp_channel_dispatcher_new (priv->dbus);
 }
 
 EmpathyDispatcher *
@@ -1231,6 +1269,12 @@ dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
   EmpathyDispatchOperation *operation = NULL;
   ConnectionData *conn_data;
 
+  /* The DispatcherRequestData owns a ref on the self object. As the request
+   * data could be destroyed (when calling dispatcher_request_failed for
+   * example) we keep a ref on self to be sure it stays alive while we are
+   * executing this function. */
+  g_object_ref (self);
+
   conn_data = g_hash_table_lookup (priv->connections,
     request_data->connection);
 
@@ -1240,16 +1284,10 @@ dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
 
       dispatcher_request_failed (self, request_data, error);
 
-      goto out;
+      return;
     }
 
-  operation = g_hash_table_lookup (conn_data->outstanding_channels,
-    object_path);
-
-  if (operation != NULL)
-    g_hash_table_remove (conn_data->outstanding_channels, object_path);
-  else
-    operation = g_hash_table_lookup (conn_data->dispatching_channels,
+  operation = g_hash_table_lookup (conn_data->dispatching_channels,
         object_path);
 
   if (operation == NULL)
@@ -1311,10 +1349,6 @@ dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
           conn_data);
 
   g_object_unref (operation);
-
-out:
-  dispatcher_flush_outstanding_operations (request_data->dispatcher,
-    conn_data);
 }
 
 static void
@@ -1358,13 +1392,16 @@ dispatcher_request_channel (DispatcherRequestData *request_data)
     }
   else
     {
-      request_data->pending_call = tp_cli_connection_call_request_channel (
+      TpProxyPendingCall *call = tp_cli_connection_call_request_channel (
         request_data->connection, -1,
         request_data->channel_type,
         request_data->handle_type,
         request_data->handle,
         TRUE, dispatcher_request_channel_cb,
         request_data, NULL, NULL);
+
+      if (call != NULL)
+        request_data->pending_call = call;
     }
 }
 
@@ -1483,8 +1520,6 @@ dispatcher_request_handles_cb (TpConnection *connection,
         request_data);
 
       free_dispatcher_request_data (request_data);
-
-      dispatcher_flush_outstanding_operations (self, cd);
       return;
     }
 
@@ -1503,6 +1538,7 @@ empathy_dispatcher_join_muc (TpConnection *connection,
   DispatcherRequestData *request_data;
   ConnectionData *connection_data;
   const gchar *names[] = { roomname, NULL };
+  TpProxyPendingCall *call;
 
   g_return_if_fail (TP_IS_CONNECTION (connection));
   g_return_if_fail (!EMP_STR_EMPTY (roomname));
@@ -1511,58 +1547,147 @@ empathy_dispatcher_join_muc (TpConnection *connection,
   priv = GET_PRIV (self);
 
   connection_data = g_hash_table_lookup (priv->connections, connection);
+  g_assert (connection_data != NULL);
 
   /* Don't know the room handle yet */
   request_data  = new_dispatcher_request_data (self, connection,
     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
     NULL, callback, user_data);
+  request_data->should_ensure = TRUE;
 
   connection_data->outstanding_requests = g_list_prepend
     (connection_data->outstanding_requests, request_data);
 
-  request_data->pending_call = tp_cli_connection_call_request_handles (
+  call = tp_cli_connection_call_request_handles (
     connection, -1,
     TP_HANDLE_TYPE_ROOM, names,
     dispatcher_request_handles_cb, request_data, NULL, NULL);
 
+  if (call != NULL)
+    request_data->pending_call = call;
+
   g_object_unref (self);
 }
 
 static void
-dispatcher_create_channel_cb (TpConnection *connect,
-                              const gchar *object_path,
-                              GHashTable *properties,
-                              const GError *error,
-                              gpointer user_data,
-                              GObject *weak_object)
+dispatcher_channel_request_failed_cb (TpChannelRequest *request,
+  const gchar *error,
+  const gchar *message,
+  gpointer user_data,
+  GObject *weak_object)
 {
   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
   EmpathyDispatcher *self =
       EMPATHY_DISPATCHER (request_data->dispatcher);
+  GError *err = NULL;
 
   request_data->pending_call = NULL;
 
-  dispatcher_connection_new_requested_channel (self,
-    request_data, object_path, properties, error);
+  DEBUG ("Request failed: %s - %s %s",
+    tp_proxy_get_object_path (request),
+    error, message);
+
+  tp_proxy_dbus_error_to_gerror (TP_PROXY (request),
+    error, message, &err);
+
+  dispatcher_request_failed (self, request_data, err);
+
+  g_error_free (err);
 }
 
 static void
-dispatcher_ensure_channel_cb (TpConnection *connect,
-                              gboolean is_ours,
-                              const gchar *object_path,
-                              GHashTable *properties,
-                              const GError *error,
-                              gpointer user_data,
-                              GObject *weak_object)
+dispatcher_channel_request_succeeded_cb (TpChannelRequest *request,
+  gpointer user_data,
+  GObject *weak_object)
 {
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
+  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
+  ConnectionData *conn_data;
+
+  conn_data = g_hash_table_lookup (priv->connections,
+    request_data->connection);
+
+  DEBUG ("Request succeeded: %s", tp_proxy_get_object_path (request));
+
+  /* When success gets called the internal request should have been satisfied,
+   * if it's still in outstanding_requests and doesn't have an operation
+   * assigned to it, the channel got handled by someone else.. */
+
+  if (g_list_find (conn_data->outstanding_requests, request_data) == NULL)
+    return;
+
+  if (request_data->operation == NULL)
+    {
+      GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS, "Not yours!" };
+      dispatcher_request_failed (self, request_data, &err);
+    }
+}
+
+static void
+dispatcher_channel_request_proceed_cb (TpChannelRequest *request,
+  const GError *error,
+  gpointer user_data,
+  GObject *weak_object)
+{
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
-  EmpathyDispatcher *self =
-      EMPATHY_DISPATCHER (request_data->dispatcher);
 
   request_data->pending_call = NULL;
 
-  dispatcher_connection_new_requested_channel (self,
-    request_data, object_path, properties, error);
+  if (error != NULL)
+    dispatcher_request_failed (self, request_data, error);
+}
+
+static void
+dispatcher_create_channel_cb (TpChannelDispatcher *proxy,
+    const gchar *request_path,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
+{
+  DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (request_data->dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  TpChannelRequest *request;
+  GError *err = NULL;
+  TpProxyPendingCall *call;
+
+  request_data->pending_call = NULL;
+
+  if (error != NULL)
+    {
+      dispatcher_request_failed (self, request_data, error);
+      return;
+    }
+
+  request = tp_channel_request_new (priv->dbus, request_path, NULL, NULL);
+  request_data->channel_request = request;
+
+  if (tp_cli_channel_request_connect_to_failed (request,
+      dispatcher_channel_request_failed_cb, request_data,
+      NULL, G_OBJECT (self), &err) == NULL)
+    {
+      dispatcher_request_failed (self, request_data, err);
+      g_error_free (err);
+      return;
+    }
+
+  if (tp_cli_channel_request_connect_to_succeeded (request,
+      dispatcher_channel_request_succeeded_cb, request_data,
+      NULL, G_OBJECT (self), &err) == NULL)
+    {
+      dispatcher_request_failed (self, request_data, err);
+      g_error_free (err);
+      return;
+    }
+
+  call = tp_cli_channel_request_call_proceed (request,
+    -1, dispatcher_channel_request_proceed_cb,
+    request_data, NULL, G_OBJECT (self));
+
+  if (call != NULL)
+    request_data->pending_call = call;
 }
 
 static void
@@ -1570,24 +1695,53 @@ empathy_dispatcher_call_create_or_ensure_channel (
     EmpathyDispatcher *self,
     DispatcherRequestData *request_data)
 {
+  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  TpAccount *account;
+  const gchar *handler = "";
+  TpProxyPendingCall *call;
+
+  account = empathy_get_account_for_connection (request_data->connection);
+
+  g_assert (account != NULL);
+
+  if (request_data->cb)
+    handler = empathy_handler_get_busname (priv->handler);
+
   if (request_data->should_ensure)
     {
-      request_data->pending_call =
-          tp_cli_connection_interface_requests_call_ensure_channel (
-          request_data->connection, -1,
-          request_data->request, dispatcher_ensure_channel_cb,
-          request_data, NULL, NULL);
+      call = tp_cli_channel_dispatcher_call_ensure_channel (
+          priv->channel_dispatcher,
+          -1, tp_proxy_get_object_path (TP_PROXY (account)),
+          request_data->request, 0, handler,
+          dispatcher_create_channel_cb, request_data, NULL, NULL);
     }
   else
     {
-      request_data->pending_call =
-          tp_cli_connection_interface_requests_call_create_channel (
-          request_data->connection, -1,
-          request_data->request, dispatcher_create_channel_cb,
-          request_data, NULL, NULL);
+      call = tp_cli_channel_dispatcher_call_create_channel (
+          priv->channel_dispatcher,
+          -1, tp_proxy_get_object_path (TP_PROXY (account)),
+          request_data->request, 0, handler,
+          dispatcher_create_channel_cb, request_data, NULL,
+          G_OBJECT (dispatcher));
     }
+
+  if (call != NULL)
+    request_data->pending_call = call;
 }
 
+/**
+ * empathy_dispatcher_create_channel:
+ * @self: the EmpathyDispatcher
+ * @connection: the Connection to dispatch on
+ * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
+ * @callback: a callback for when the channel arrives (or NULL)
+ * @user_data: optional user data (or NULL)
+ *
+ * When calling this function, #EmpathyDispatcher takes ownership of your
+ * reference to @request. DO NOT unref or destroy @request. When the request is
+ * done, @request will be unreferenced. Take another reference if you want to
+ * keep it around.
+ */
 void
 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
                                    TpConnection *connection,
@@ -2008,7 +2162,7 @@ empathy_dispatcher_handle_channels (EmpathyHandler *handler,
       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
 
       dispatcher_connection_new_channel_with_properties (self,
-        connection, object_path, properties);
+        connection, object_path, properties, requests_satisfied);
     }
 
   return TRUE;