]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-dispatcher.c
Updated Polish translation
[empathy.git] / libempathy / empathy-dispatcher.c
index 528320295bf48e290119c4fa1e3664d5a5e3a5dc..caf697cb68f419fec53e0f66db27e844630af5b1 100644 (file)
 
 #include <glib/gi18n-lib.h>
 
+#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>
@@ -44,8 +47,6 @@
 #include "empathy-dispatcher.h"
 #include "empathy-handler.h"
 #include "empathy-utils.h"
-#include "empathy-tube-handler.h"
-#include "empathy-account-manager.h"
 #include "empathy-tp-contact-factory.h"
 #include "empathy-chatroom-manager.h"
 #include "empathy-utils.h"
@@ -58,12 +59,11 @@ typedef struct
 {
   gboolean dispose_has_run;
 
-  EmpathyAccountManager *account_manager;
+  TpAccountManager *account_manager;
   /* connection to connection data mapping */
   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 *
@@ -131,12 +137,15 @@ typedef struct
   guint handle_type;
   guint handle;
   EmpathyContact *contact;
+  TpProxyPendingCall *pending_call;
 
   /* Properties to pass to the channel when requesting it */
   GHashTable *request;
   EmpathyDispatcherRequestCb *cb;
   gpointer user_data;
   gpointer *request_data;
+
+  TpChannelRequest *channel_request;
 } DispatcherRequestData;
 
 typedef struct
@@ -153,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 */
@@ -185,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)
@@ -208,7 +212,7 @@ free_dispatch_data (DispatchData *data)
 }
 
 static DispatcherRequestData *
-new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
+new_dispatcher_request_data (EmpathyDispatcher *self,
                              TpConnection *connection,
                              const gchar *channel_type,
                              guint handle_type,
@@ -220,7 +224,7 @@ new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
 {
   DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
 
-  result->dispatcher = g_object_ref (dispatcher);
+  result->dispatcher = g_object_ref (self);
   result->connection = connection;
 
   result->should_ensure = FALSE;
@@ -253,6 +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);
 }
 
@@ -267,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;
 }
 
@@ -277,10 +285,10 @@ static void
 free_connection_data (ConnectionData *cd)
 {
   GList *l;
+  guint i;
 
   g_hash_table_destroy (cd->dispatched_channels);
   g_hash_table_destroy (cd->dispatching_channels);
-  int i;
 
   for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
     {
@@ -299,7 +307,7 @@ free_connection_data (ConnectionData *cd)
 static void
 free_find_channel_request (FindChannelRequest *r)
 {
-  int idx;
+  guint idx;
   char *str;
 
   g_object_unref (r->dispatcher);
@@ -324,38 +332,16 @@ dispatcher_connection_invalidated_cb (TpConnection *connection,
                                       guint domain,
                                       gint code,
                                       gchar *message,
-                                      EmpathyDispatcher *dispatcher)
+                                      EmpathyDispatcher *self)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
 
   DEBUG ("Error: %s", message);
   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 *dispatcher,
+dispatch_operation_flush_requests (EmpathyDispatcher *self,
                                    EmpathyDispatchOperation *operation,
                                    GError *error,
                                    ConnectionData *cd)
@@ -393,12 +379,11 @@ dispatcher_channel_invalidated_cb (TpProxy *proxy,
                                    guint domain,
                                    gint code,
                                    gchar *message,
-                                   EmpathyDispatcher *dispatcher)
+                                   EmpathyDispatcher *self)
 {
   /* Channel went away... */
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpConnection *connection;
-  EmpathyDispatchOperation *operation;
   ConnectionData *cd;
   const gchar *object_path;
 
@@ -417,34 +402,25 @@ 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 (dispatcher, operation, &error, cd);
-      g_hash_table_remove (cd->outstanding_channels, object_path);
-      g_object_unref (operation);
-    }
 }
 
 static void
 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
-                                EmpathyDispatcher *dispatcher)
+                                EmpathyDispatcher *self)
 {
   g_assert (empathy_dispatch_operation_is_incoming (operation));
   DEBUG ("Send of for dispatching: %s",
     empathy_dispatch_operation_get_object_path (operation));
-  g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
+  g_signal_emit (self, signals[DISPATCH], 0, operation);
 }
 
 static void
 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
-                               EmpathyDispatcher *dispatcher)
+                               EmpathyDispatcher *self)
 {
   /* Our job is done, remove the dispatch operation and mark the channel as
    * dispatched */
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpConnection *connection;
   ConnectionData *cd;
   const gchar *object_path;
@@ -471,23 +447,23 @@ dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
 
 static void
 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
-                             EmpathyDispatcher *dispatcher)
+                             EmpathyDispatcher *self)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpConnection *connection;
   ConnectionData *cd;
   EmpathyDispatchOperationState status;
 
   g_signal_connect (operation, "approved",
-    G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
+    G_CALLBACK (dispatch_operation_approved_cb), self);
 
   g_signal_connect (operation, "claimed",
-    G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
+    G_CALLBACK (dispatch_operation_claimed_cb), self);
 
   /* Signal the observers */
   DEBUG ("Send to observers: %s",
     empathy_dispatch_operation_get_object_path (operation));
-  g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
+  g_signal_emit (self, signals[OBSERVE], 0, operation);
 
   empathy_dispatch_operation_start (operation);
 
@@ -497,9 +473,9 @@ dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
   g_assert (cd != NULL);
 
   g_object_ref (operation);
-  g_object_ref (dispatcher);
+  g_object_ref (self);
 
-  dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
+  dispatch_operation_flush_requests (self, operation, NULL, cd);
   status = empathy_dispatch_operation_get_status (operation);
   g_object_unref (operation);
 
@@ -510,17 +486,17 @@ dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
     {
       DEBUG ("Send to approvers: %s",
         empathy_dispatch_operation_get_object_path (operation));
-      g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
+      g_signal_emit (self, signals[APPROVE], 0, operation);
     }
   else
     {
       g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
       DEBUG ("Send of for dispatching: %s",
         empathy_dispatch_operation_get_object_path (operation));
-      g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
+      g_signal_emit (self, signals[DISPATCH], 0, operation);
     }
 
-  g_object_unref (dispatcher);
+  g_object_unref (self);
 }
 
 static void
@@ -535,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);
 
@@ -545,10 +518,10 @@ dispatcher_start_dispatching (EmpathyDispatcher *self,
         {
           case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
             g_signal_connect (operation, "ready",
-              G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
+              G_CALLBACK (dispatch_operation_ready_cb), self);
             break;
           case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
-            dispatch_operation_ready_cb (operation, dispatcher);
+            dispatch_operation_ready_cb (operation, self);
             break;
           default:
             g_assert_not_reached ();
@@ -566,36 +539,17 @@ 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 (dispatcher, operation, cd);
-        }
-    }
-}
-
-static void
-dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
+dispatcher_connection_new_channel (EmpathyDispatcher *self,
                                    TpConnection *connection,
                                    const gchar *object_path,
                                    const gchar *channel_type,
                                    guint handle_type,
                                    guint handle,
                                    GHashTable *properties,
-                                   gboolean incoming)
+                                   gboolean incoming,
+                                   const GPtrArray *requests_satisfied)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   TpChannel         *channel;
   ConnectionData *cd;
   EmpathyDispatchOperation *operation;
@@ -610,35 +564,10 @@ dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
     NULL
   };
 
-  dispatcher_init_connection_if_needed (dispatcher, connection);
+  dispatcher_init_connection_if_needed (self, connection);
 
   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]))
@@ -649,7 +578,17 @@ dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
         }
     }
 
-  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,
@@ -660,7 +599,7 @@ dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
 
   g_signal_connect (channel, "invalidated",
     G_CALLBACK (dispatcher_channel_invalidated_cb),
-    dispatcher);
+    self);
 
   priv->channels = g_list_prepend (priv->channels, channel);
 
@@ -669,29 +608,64 @@ dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
 
   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 (dispatcher, operation, cd))
-        dispatcher_start_dispatching (dispatcher, operation, cd);
-      else
-        g_hash_table_insert (cd->outstanding_channels,
-          g_strdup (object_path), operation);
-    }
-  else
-    {
-      dispatcher_start_dispatching (dispatcher, 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
 dispatcher_connection_new_channel_with_properties (
-    EmpathyDispatcher *dispatcher,
+    EmpathyDispatcher *self,
     TpConnection *connection,
     const gchar *object_path,
-    GHashTable *properties)
+    GHashTable *properties,
+    const GPtrArray *requests_satisfied)
 {
   const gchar *channel_type;
   guint handle_type;
@@ -699,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)
@@ -734,8 +707,9 @@ dispatcher_connection_new_channel_with_properties (
       requested = FALSE;
     }
 
-  dispatcher_connection_new_channel (dispatcher, connection,
-    object_path, channel_type, handle_type, handle, properties, !requested);
+  dispatcher_connection_new_channel (self, connection,
+    object_path, channel_type, handle_type, handle, properties, !requested,
+    requests_satisfied);
 }
 
 static void
@@ -745,8 +719,8 @@ dispatcher_connection_got_all (TpProxy *proxy,
                                gpointer user_data,
                                GObject *object)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   GPtrArray *requestable_channels;
 
   if (error) {
@@ -779,7 +753,7 @@ dispatcher_connection_got_all (TpProxy *proxy,
         {
           request = l->data;
 
-          retval = empathy_dispatcher_find_channel_classes (dispatcher,
+          retval = empathy_dispatcher_find_channel_classes (self,
               TP_CONNECTION (proxy), request->channel_type,
               request->handle_type, request->properties);
           request->callback (retval, request->user_data);
@@ -799,30 +773,28 @@ dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
                                                  const GPtrArray *capabilities,
                                                  const GError    *error,
                                                  gpointer         user_data,
-                                                 GObject         *dispatcher)
+                                                 GObject         *self)
 {
   if (error)
     DEBUG ("Error: %s", error->message);
 }
 
 static void
-dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
-    TpConnection *connection)
+connection_ready_cb (TpConnection *connection,
+    const GError *error,
+    gpointer user_data)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  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;
+  const gchar *remove_ = NULL;
 
-  g_hash_table_insert (priv->connections, g_object_ref (connection),
-    new_connection_data ());
-
-  g_signal_connect (connection, "invalidated",
-    G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
+  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))
@@ -830,7 +802,7 @@ dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
       tp_cli_dbus_properties_call_get_all (connection, -1,
         TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
         dispatcher_connection_got_all,
-        NULL, NULL, G_OBJECT (dispatcher));
+        NULL, NULL, G_OBJECT (self));
     }
 
   /* Advertise VoIP capabilities */
@@ -844,24 +816,56 @@ dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
-           TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
+           TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
+           TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
+           G_MAXUINT);
   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
 
   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
-    connection, -1, capabilities, &remove,
+    connection, -1, capabilities, &remove_,
     dispatcher_connection_advertise_capabilities_cb,
-    NULL, NULL, G_OBJECT (dispatcher));
+    NULL, NULL, G_OBJECT (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
-dispatcher_new_connection_cb (EmpathyAccountManager *manager,
-                              TpConnection *connection,
-                              EmpathyDispatcher *dispatcher)
+dispatcher_status_changed_cb (TpAccount *account,
+                              guint old_status,
+                              guint new_status,
+                              guint reason,
+                              gchar *dbus_error_name,
+                              GHashTable *details,
+                              EmpathyDispatcher *self)
 {
-  dispatcher_init_connection_if_needed (dispatcher, connection);
+  TpConnection *conn = tp_account_get_connection (account);
+
+  if (conn != NULL)
+    dispatcher_init_connection_if_needed (self, conn);
 }
 
 static void
@@ -912,6 +916,8 @@ static void
 dispatcher_dispose (GObject *object)
 {
   EmpathyDispatcherPriv *priv = GET_PRIV (object);
+  GHashTableIter iter;
+  gpointer connection;
   GList *l;
 
   if (priv->dispose_has_run)
@@ -929,6 +935,24 @@ dispatcher_dispose (GObject *object)
     g_object_unref (priv->handler);
   priv->handler = NULL;
 
+  g_hash_table_iter_init (&iter, priv->connections);
+  while (g_hash_table_iter_next (&iter, &connection, NULL))
+    {
+      g_signal_handlers_disconnect_by_func (connection,
+          dispatcher_connection_invalidated_cb, 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);
 }
 
@@ -940,6 +964,7 @@ dispatcher_finalize (GObject *object)
   GHashTableIter iter;
   gpointer connection;
   GList *list;
+  gpointer account, id;
 
   if (priv->request_channel_class_async_ids != NULL)
     {
@@ -948,9 +973,6 @@ dispatcher_finalize (GObject *object)
       g_hash_table_destroy (priv->request_channel_class_async_ids);
     }
 
-  g_signal_handlers_disconnect_by_func (priv->account_manager,
-      dispatcher_new_connection_cb, object);
-
   for (l = priv->channels; l; l = l->next)
     {
       g_signal_handlers_disconnect_by_func (l->data,
@@ -959,13 +981,6 @@ dispatcher_finalize (GObject *object)
 
   g_list_free (priv->channels);
 
-  g_hash_table_iter_init (&iter, priv->connections);
-  while (g_hash_table_iter_next (&iter, &connection, NULL))
-    {
-      g_signal_handlers_disconnect_by_func (connection,
-          dispatcher_connection_invalidated_cb, object);
-    }
-
   g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
   while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
     {
@@ -973,9 +988,15 @@ 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->connections);
   g_hash_table_destroy (priv->outstanding_classes_requests);
 }
 
@@ -985,8 +1006,8 @@ dispatcher_set_property (GObject *object,
   const GValue *value,
   GParamSpec *pspec)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
 
   switch (property_id)
     {
@@ -1005,8 +1026,8 @@ dispatcher_get_property (GObject *object,
   GValue *value,
   GParamSpec *pspec)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
 
   switch (property_id)
     {
@@ -1074,18 +1095,95 @@ empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
 }
 
 static void
-empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
+connect_account (EmpathyDispatcher *self,
+    TpAccount *account)
 {
-  GList *connections, *l;
-  EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
-    EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
+  TpConnection *conn = tp_account_get_connection (account);
+  gulong id;
 
-  dispatcher->priv = priv;
-  priv->account_manager = empathy_account_manager_dup_singleton ();
+  id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
+        account));
 
-  g_signal_connect (priv->account_manager, "new-connection",
-    G_CALLBACK (dispatcher_new_connection_cb),
-    dispatcher);
+  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,
+                             gpointer user_data)
+{
+  GList *accounts, *l;
+  EmpathyDispatcher *self = user_data;
+  TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
+  GError *error = NULL;
+
+  if (!tp_account_manager_prepare_finish (account_manager, result, &error))
+    {
+      DEBUG ("Failed to prepare account manager: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  accounts = tp_account_manager_get_valid_accounts (account_manager);
+  for (l = accounts; l; l = l->next)
+    {
+      TpAccount *a = l->data;
+
+      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)
+{
+  EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+    EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
+
+  self->priv = priv;
+  priv->account_manager = tp_account_manager_dup ();
 
   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
     g_object_unref, (GDestroyNotify) free_connection_data);
@@ -1095,16 +1193,20 @@ empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
 
   priv->channels = NULL;
 
-  connections = empathy_account_manager_dup_connections (priv->account_manager);
-  for (l = connections; l; l = l->next)
-    {
-      dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
-      g_object_unref (l->data);
-    }
-  g_list_free (connections);
+  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 *
@@ -1112,10 +1214,10 @@ empathy_dispatcher_new (const gchar *name,
   GPtrArray *filters,
   GStrv capabilities)
 {
-  g_assert (dispatcher == NULL);
   EmpathyHandler *handler;
   EmpathyDispatcher *ret;
 
+  g_assert (dispatcher == NULL);
   handler = empathy_handler_new (name, filters, capabilities);
 
   ret = EMPATHY_DISPATCHER (
@@ -1134,11 +1236,11 @@ empathy_dispatcher_dup_singleton (void)
 }
 
 static void
-dispatcher_request_failed (EmpathyDispatcher *dispatcher,
+dispatcher_request_failed (EmpathyDispatcher *self,
                            DispatcherRequestData *request_data,
                            const GError *error)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   ConnectionData *conn_data;
 
   conn_data = g_hash_table_lookup (priv->connections,
@@ -1157,16 +1259,22 @@ dispatcher_request_failed (EmpathyDispatcher *dispatcher,
 }
 
 static void
-dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
-                                             DispatcherRequestData *request_data,
-                                             const gchar *object_path,
-                                             GHashTable *properties,
-                                             const GError *error)
+dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
+  DispatcherRequestData *request_data,
+  const gchar *object_path,
+  GHashTable *properties,
+  const GError *error)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (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);
 
@@ -1174,18 +1282,12 @@ dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
     {
       DEBUG ("Channel request failed: %s", error->message);
 
-      dispatcher_request_failed (dispatcher, request_data, error);
+      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)
@@ -1247,10 +1349,6 @@ dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
           conn_data);
 
   g_object_unref (operation);
-
-out:
-  dispatcher_flush_outstanding_operations (request_data->dispatcher,
-    conn_data);
 }
 
 static void
@@ -1260,10 +1358,13 @@ dispatcher_request_channel_cb (TpConnection *connection,
                                gpointer user_data,
                                GObject *weak_object)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
+  EmpathyDispatcher *self =
+      EMPATHY_DISPATCHER (request_data->dispatcher);
 
-  dispatcher_connection_new_requested_channel (dispatcher,
+  request_data->pending_call = NULL;
+
+  dispatcher_connection_new_requested_channel (self,
     request_data, object_path, NULL, error);
 }
 
@@ -1291,12 +1392,16 @@ dispatcher_request_channel (DispatcherRequestData *request_data)
     }
   else
     {
-      tp_cli_connection_call_request_channel (request_data->connection, -1,
+      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, G_OBJECT (request_data->dispatcher));
+        request_data, NULL, NULL);
+
+      if (call != NULL)
+        request_data->pending_call = call;
     }
 }
 
@@ -1305,7 +1410,7 @@ empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
                                       EmpathyDispatcherRequestCb *callback,
                                       gpointer user_data)
 {
-  EmpathyDispatcher *dispatcher;
+  EmpathyDispatcher *self;
   EmpathyDispatcherPriv *priv;
   TpConnection *connection;
   ConnectionData *connection_data;
@@ -1313,14 +1418,14 @@ empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
 
   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
-  dispatcher = empathy_dispatcher_dup_singleton ();
-  priv = GET_PRIV (dispatcher);
+  self = empathy_dispatcher_dup_singleton ();
+  priv = GET_PRIV (self);
 
   connection = empathy_contact_get_connection (contact);
   connection_data = g_hash_table_lookup (priv->connections, connection);
 
   /* The contact handle might not be known yet */
-  request_data = new_dispatcher_request_data (dispatcher, connection,
+  request_data = new_dispatcher_request_data (self, connection,
     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
     empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
   request_data->should_ensure = TRUE;
@@ -1330,7 +1435,7 @@ empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
 
   dispatcher_request_channel (request_data);
 
-  g_object_unref (dispatcher);
+  g_object_unref (self);
 }
 
 typedef struct
@@ -1370,17 +1475,17 @@ empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
                                          EmpathyDispatcherRequestCb *callback,
                                          gpointer user_data)
 {
-  EmpathyDispatcher *dispatcher;
+  EmpathyDispatcher *self;
   EmpathyTpContactFactory *factory;
   ChatWithContactIdData *data;
 
   g_return_if_fail (TP_IS_CONNECTION (connection));
   g_return_if_fail (!EMP_STR_EMPTY (contact_id));
 
-  dispatcher = empathy_dispatcher_dup_singleton ();
+  self = empathy_dispatcher_dup_singleton ();
   factory = empathy_tp_contact_factory_dup_singleton (connection);
   data = g_slice_new0 (ChatWithContactIdData);
-  data->dispatcher = dispatcher;
+  data->dispatcher = self;
   data->callback = callback;
   data->user_data = user_data;
   empathy_tp_contact_factory_get_from_id (factory, contact_id,
@@ -1398,10 +1503,12 @@ dispatcher_request_handles_cb (TpConnection *connection,
 {
   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
 
+  request_data->pending_call = NULL;
+
   if (error != NULL)
     {
-      EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
-      EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+      EmpathyDispatcher *self = request_data->dispatcher;
+      EmpathyDispatcherPriv *priv = GET_PRIV (self);
       ConnectionData *cd;
 
       cd = g_hash_table_lookup (priv->connections, request_data->connection);
@@ -1413,8 +1520,6 @@ dispatcher_request_handles_cb (TpConnection *connection,
         request_data);
 
       free_dispatcher_request_data (request_data);
-
-      dispatcher_flush_outstanding_operations (dispatcher, cd);
       return;
     }
 
@@ -1428,96 +1533,223 @@ empathy_dispatcher_join_muc (TpConnection *connection,
                              EmpathyDispatcherRequestCb *callback,
                              gpointer user_data)
 {
-  EmpathyDispatcher *dispatcher;
+  EmpathyDispatcher *self;
   EmpathyDispatcherPriv *priv;
   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));
 
-  dispatcher = empathy_dispatcher_dup_singleton ();
-  priv = GET_PRIV (dispatcher);
+  self = empathy_dispatcher_dup_singleton ();
+  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 (dispatcher, connection,
+  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);
 
-  tp_cli_connection_call_request_handles (connection, -1,
+  call = tp_cli_connection_call_request_handles (
+    connection, -1,
     TP_HANDLE_TYPE_ROOM, names,
-    dispatcher_request_handles_cb, request_data, NULL,
-    G_OBJECT (dispatcher));
+    dispatcher_request_handles_cb, request_data, NULL, NULL);
+
+  if (call != NULL)
+    request_data->pending_call = call;
 
-  g_object_unref (dispatcher);
+  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)
-{
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (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 (dispatcher,
-    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)
-{
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (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;
+
+  request_data->pending_call = NULL;
+
+  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));
 
-  dispatcher_connection_new_requested_channel (dispatcher,
-    request_data, object_path, properties, error);
+  if (call != NULL)
+    request_data->pending_call = call;
 }
 
 static void
 empathy_dispatcher_call_create_or_ensure_channel (
-    EmpathyDispatcher *dispatcher,
+    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)
     {
-      tp_cli_connection_interface_requests_call_ensure_channel (
-          request_data->connection, -1,
-          request_data->request, dispatcher_ensure_channel_cb,
-          request_data, NULL, G_OBJECT (request_data->dispatcher));
+      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
     {
-      tp_cli_connection_interface_requests_call_create_channel (
-          request_data->connection, -1,
-          request_data->request, dispatcher_create_channel_cb,
-          request_data, NULL, G_OBJECT (request_data->dispatcher));
+      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 *dispatcher,
+empathy_dispatcher_create_channel (EmpathyDispatcher *self,
                                    TpConnection *connection,
                                    GHashTable *request,
                                    EmpathyDispatcherRequestCb *callback,
                                    gpointer user_data)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   ConnectionData *connection_data;
   DispatcherRequestData *request_data;
   const gchar *channel_type;
@@ -1525,7 +1757,7 @@ empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
   guint handle;
   gboolean valid;
 
-  g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
+  g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
   g_return_if_fail (TP_IS_CONNECTION (connection));
   g_return_if_fail (request != NULL);
 
@@ -1541,14 +1773,14 @@ empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
 
   handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
 
-  request_data  = new_dispatcher_request_data (dispatcher, connection,
+  request_data  = new_dispatcher_request_data (self, connection,
     channel_type, handle_type, handle, request,
     NULL, callback, user_data);
 
   connection_data->outstanding_requests = g_list_prepend
     (connection_data->outstanding_requests, request_data);
 
-  empathy_dispatcher_call_create_or_ensure_channel (dispatcher, request_data);
+  empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
 }
 
 static gboolean
@@ -1582,7 +1814,7 @@ channel_class_matches (GValueArray *class,
   if (fixed_properties != NULL)
     {
       gpointer h_key, h_val;
-      int idx;
+      guint idx;
       GHashTableIter iter;
       gboolean found;
 
@@ -1633,17 +1865,17 @@ channel_class_matches (GValueArray *class,
 }
 
 static GList *
-empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher,
+empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
                                          TpConnection *connection,
                                          const gchar *channel_type,
                                          guint handle_type,
                                          GArray *fixed_properties)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   GValueArray *class;
   GPtrArray *classes;
   GList *matching_classes;
-  int i;
+  guint i;
   ConnectionData *cd;
 
   g_return_val_if_fail (channel_type != NULL, NULL);
@@ -1773,7 +2005,7 @@ setup_varargs (va_list var_args,
  */
 GList *
 empathy_dispatcher_find_requestable_channel_classes
-                                 (EmpathyDispatcher *dispatcher,
+                                 (EmpathyDispatcher *self,
                                   TpConnection *connection,
                                   const gchar *channel_type,
                                   guint handle_type,
@@ -1784,15 +2016,15 @@ empathy_dispatcher_find_requestable_channel_classes
   GArray *properties;
   EmpathyDispatcherPriv *priv;
   GList *retval;
-  int idx;
+  guint idx;
   char *str;
 
-  g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL);
+  g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
   g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
   g_return_val_if_fail (channel_type != NULL, NULL);
   g_return_val_if_fail (handle_type != 0, NULL);
 
-  priv = GET_PRIV (dispatcher);
+  priv = GET_PRIV (self);
 
   va_start (var_args, first_property_name);
 
@@ -1800,7 +2032,7 @@ empathy_dispatcher_find_requestable_channel_classes
 
   va_end (var_args);
 
-  retval = empathy_dispatcher_find_channel_classes (dispatcher, connection,
+  retval = empathy_dispatcher_find_channel_classes (self, connection,
     channel_type, handle_type, properties);
 
   if (properties != NULL)
@@ -1835,7 +2067,7 @@ empathy_dispatcher_find_requestable_channel_classes
  */
 void
 empathy_dispatcher_find_requestable_channel_classes_async
-                                 (EmpathyDispatcher *dispatcher,
+                                 (EmpathyDispatcher *self,
                                   TpConnection *connection,
                                   const gchar *channel_type,
                                   guint handle_type,
@@ -1850,12 +2082,12 @@ empathy_dispatcher_find_requestable_channel_classes_async
   EmpathyDispatcherPriv *priv;
   guint source_id;
 
-  g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
+  g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
   g_return_if_fail (TP_IS_CONNECTION (connection));
   g_return_if_fail (channel_type != NULL);
   g_return_if_fail (handle_type != 0);
 
-  priv = GET_PRIV (dispatcher);
+  priv = GET_PRIV (self);
 
   va_start (var_args, first_property_name);
 
@@ -1865,7 +2097,7 @@ empathy_dispatcher_find_requestable_channel_classes_async
 
   /* append another request for this connection */
   request = g_slice_new0 (FindChannelRequest);
-  request->dispatcher = g_object_ref (dispatcher);
+  request->dispatcher = g_object_ref (self);
   request->channel_type = g_strdup (channel_type);
   request->handle_type = handle_type;
   request->connection = connection;
@@ -1883,8 +2115,8 @@ static GList *
 empathy_dispatcher_get_channels (EmpathyHandler *handler,
   gpointer user_data)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
 
   return priv->channels;
 }
@@ -1900,18 +2132,19 @@ empathy_dispatcher_handle_channels (EmpathyHandler *handler,
     gpointer user_data,
     GError **error)
 {
-  EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
-  int i;
-  EmpathyAccount *account;
+  EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
+  guint i;
+  TpAccount *account;
   TpConnection *connection;
 
-  account = empathy_account_manager_ensure_account (priv->account_manager,
+  /* FIXME: should probably find out whether the account manager is prepared
+   * before ensuring. See bug #600111. */
+  account = tp_account_manager_ensure_account (priv->account_manager,
     account_path);
   g_assert (account != NULL);
 
-  connection = empathy_account_get_connection_for_path (account,
-      connection_path);
+  connection = tp_account_ensure_connection (account, connection_path);
   if (connection == NULL)
     {
       g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
@@ -1928,8 +2161,8 @@ empathy_dispatcher_handle_channels (EmpathyHandler *handler,
       object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
 
-      dispatcher_connection_new_channel_with_properties (dispatcher,
-        connection, object_path, properties);
+      dispatcher_connection_new_channel_with_properties (self,
+        connection, object_path, properties, requests_satisfied);
     }
 
   return TRUE;
@@ -1937,12 +2170,12 @@ empathy_dispatcher_handle_channels (EmpathyHandler *handler,
 
 
 EmpathyHandler *
-empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
+empathy_dispatcher_add_handler (EmpathyDispatcher *self,
     const gchar *name,
     GPtrArray *filters,
     GStrv capabilities)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   EmpathyHandler *handler;
 
   handler = empathy_handler_new (name, filters, capabilities);
@@ -1952,17 +2185,16 @@ empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
    * handler will always report all dispatched channels even if they came from
    * a different Handler */
   empathy_handler_set_handle_channels_func (handler,
-    empathy_dispatcher_handle_channels,
-    dispatcher);
+    empathy_dispatcher_handle_channels, self);
 
   return handler;
 }
 
 void
-empathy_dispatcher_remove_handler (EmpathyDispatcher *dispatcher,
+empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
   EmpathyHandler *handler)
 {
-  EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
+  EmpathyDispatcherPriv *priv = GET_PRIV (self);
   GList *h;
 
   h = g_list_find (priv->handlers, handler);