]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
TpChat: don't crash with broken TpProperties implementations
[empathy.git] / libempathy / empathy-tp-chat.c
index 7877acb22e1d7e09d895fb037ac08833194bd1a3..9eaff95924fa34b6397fde09298119958fe3a805 100644 (file)
 
 #include "empathy-tp-chat.h"
 #include "empathy-tp-contact-factory.h"
-#include "empathy-contact-monitor.h"
 #include "empathy-contact-list.h"
-#include "empathy-dispatcher.h"
 #include "empathy-marshal.h"
+#include "empathy-request-util.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
 #include "empathy-debug.h"
 
-#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
-typedef struct {
-       gboolean               dispose_has_run;
-       EmpathyTpContactFactory *factory;
-       EmpathyContactMonitor *contact_monitor;
+struct _EmpathyTpChatPrivate {
+       TpAccount             *account;
        EmpathyContact        *user;
        EmpathyContact        *remote_contact;
        GList                 *members;
-       TpChannel             *channel;
-       gboolean               listing_pending_messages;
        /* Queue of messages not signalled yet */
        GQueue                *messages_queue;
        /* Queue of messages signalled but not acked yet */
        GQueue                *pending_messages_queue;
        gboolean               had_properties_list;
        GPtrArray             *properties;
-       TpChannelPasswordFlags password_flags;
-       /* TRUE if we fetched the password flag of the channel or if it's not needed
-        * (channel doesn't implement the Password interface) */
-       gboolean               got_password_flags;
-       gboolean               ready;
        gboolean               can_upgrade_to_muc;
-} EmpathyTpChatPriv;
+
+       GHashTable            *messages_being_sent;
+
+       /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
+       GSimpleAsyncResult    *ready_result;
+};
 
 static void tp_chat_iface_init         (EmpathyContactListIface *iface);
 
 enum {
        PROP_0,
-       PROP_CHANNEL,
+       PROP_ACCOUNT,
        PROP_REMOTE_CONTACT,
-       PROP_PASSWORD_NEEDED,
-       PROP_READY,
+       PROP_N_MESSAGES_SENDING,
 };
 
 enum {
@@ -78,29 +71,54 @@ enum {
        SEND_ERROR,
        CHAT_STATE_CHANGED,
        PROPERTY_CHANGED,
-       DESTROY,
+       MESSAGE_ACKNOWLEDGED,
        LAST_SIGNAL
 };
 
 static guint signals[LAST_SIGNAL];
 
-G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
+G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
                         G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
                                                tp_chat_iface_init));
 
-static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
-
 static void
-tp_chat_invalidated_cb (TpProxy       *proxy,
-                       guint          domain,
-                       gint           code,
-                       gchar         *message,
-                       EmpathyTpChat *chat)
+tp_chat_set_delivery_status (EmpathyTpChat         *self,
+                            const gchar           *token,
+                            EmpathyDeliveryStatus  delivery_status)
 {
-       DEBUG ("Channel invalidated: %s", message);
-       g_signal_emit (chat, signals[DESTROY], 0);
+       TpDeliveryReportingSupportFlags flags =
+               tp_text_channel_get_delivery_reporting_support (
+                       TP_TEXT_CHANNEL (self));
+
+       /* channel must support receiving failures and successes */
+       if (!tp_str_empty (token) &&
+           flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
+           flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
+
+               DEBUG ("Delivery status (%s) = %u", token, delivery_status);
+
+               switch (delivery_status) {
+                       case EMPATHY_DELIVERY_STATUS_NONE:
+                               g_hash_table_remove (self->priv->messages_being_sent,
+                                       token);
+                               break;
+
+                       default:
+                               g_hash_table_insert (self->priv->messages_being_sent,
+                                       g_strdup (token),
+                                       GUINT_TO_POINTER (delivery_status));
+                               break;
+               }
+
+               g_object_notify (G_OBJECT (self), "n-messages-sending");
+       }
 }
 
+static void tp_chat_prepare_ready_async (TpProxy *proxy,
+       const TpProxyFeature *feature,
+       GAsyncReadyCallback callback,
+       gpointer user_data);
+
 static void
 tp_chat_async_cb (TpChannel *proxy,
                  const GError *error,
@@ -112,14 +130,29 @@ tp_chat_async_cb (TpChannel *proxy,
        }
 }
 
+static void
+create_conference_cb (GObject *source,
+                     GAsyncResult *result,
+                     gpointer user_data)
+{
+       GError *error = NULL;
+
+       if (!tp_account_channel_request_create_channel_finish (
+                       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
+               DEBUG ("Failed to create conference channel: %s", error->message);
+               g_error_free (error);
+       }
+}
+
 static void
 tp_chat_add (EmpathyContactList *list,
             EmpathyContact     *contact,
             const gchar        *message)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       EmpathyTpChat *self = (EmpathyTpChat *) list;
+       TpChannel *channel = (TpChannel *) self;
 
-       if (tp_proxy_has_interface_by_id (priv->channel,
+       if (tp_proxy_has_interface_by_id (self,
                TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
                TpHandle           handle;
                GArray             handles = {(gchar *) &handle, 1};
@@ -128,41 +161,40 @@ tp_chat_add (EmpathyContactList *list,
                g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
                handle = empathy_contact_get_handle (contact);
-               tp_cli_channel_interface_group_call_add_members (priv->channel,
+               tp_cli_channel_interface_group_call_add_members (channel,
                        -1, &handles, NULL, NULL, NULL, NULL, NULL);
-       } else if (priv->can_upgrade_to_muc) {
-               EmpathyDispatcher *dispatcher;
-               TpConnection      *connection;
+       } else if (self->priv->can_upgrade_to_muc) {
+               TpAccountChannelRequest *req;
                GHashTable        *props;
                const char        *object_path;
                GPtrArray          channels = { (gpointer *) &object_path, 1 };
                const char        *invitees[2] = { NULL, };
 
-               dispatcher = empathy_dispatcher_dup_singleton ();
-               connection = tp_channel_borrow_connection (priv->channel);
-
                invitees[0] = empathy_contact_get_id (contact);
-               object_path = tp_proxy_get_object_path (priv->channel);
+               object_path = tp_proxy_get_object_path (self);
 
                props = tp_asv_new (
-                   TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
+                   TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
                        TP_IFACE_CHANNEL_TYPE_TEXT,
-                   TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT,
+                   TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
                        TP_HANDLE_TYPE_NONE,
-                   EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialChannels",
+                   TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
                        TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
-                   EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialInviteeIDs",
+                   TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
                        G_TYPE_STRV, invitees,
                    /* FIXME: InvitationMessage ? */
                    NULL);
 
+               req = tp_account_channel_request_new (self->priv->account, props,
+                       TP_USER_ACTION_TIME_NOT_USER_ACTION);
+
                /* Although this is a MUC, it's anonymous, so CreateChannel is
-                * valid.
-                * props now belongs to EmpathyDispatcher, don't free it */
-               empathy_dispatcher_create_channel (dispatcher, connection,
-                               props, NULL, NULL);
+                * valid. */
+               tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
+                       NULL, create_conference_cb, NULL);
 
-               g_object_unref (dispatcher);
+               g_object_unref (req);
+               g_hash_table_unref (props);
        } else {
                g_warning ("Cannot add to this channel");
        }
@@ -173,7 +205,7 @@ tp_chat_remove (EmpathyContactList *list,
                EmpathyContact     *contact,
                const gchar        *message)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       EmpathyTpChat *self = (EmpathyTpChat *) list;
        TpHandle           handle;
        GArray             handles = {(gchar *) &handle, 1};
 
@@ -181,7 +213,7 @@ tp_chat_remove (EmpathyContactList *list,
        g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
        handle = empathy_contact_get_handle (contact);
-       tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
+       tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
                                                            &handles, NULL,
                                                            NULL, NULL, NULL,
                                                            NULL);
@@ -190,220 +222,308 @@ tp_chat_remove (EmpathyContactList *list,
 static GList *
 tp_chat_get_members (EmpathyContactList *list)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       EmpathyTpChat *self = (EmpathyTpChat *) list;
        GList             *members = NULL;
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
 
-       if (priv->members) {
-               members = g_list_copy (priv->members);
+       if (self->priv->members) {
+               members = g_list_copy (self->priv->members);
                g_list_foreach (members, (GFunc) g_object_ref, NULL);
        } else {
-               members = g_list_prepend (members, g_object_ref (priv->user));
-               if (priv->remote_contact != NULL)
-                       members = g_list_prepend (members, g_object_ref (priv->remote_contact));
+               members = g_list_prepend (members, g_object_ref (self->priv->user));
+               if (self->priv->remote_contact != NULL)
+                       members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
        }
 
        return members;
 }
 
-static EmpathyContactMonitor *
-tp_chat_get_monitor (EmpathyContactList *list)
+static void
+check_ready (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv;
+       if (self->priv->ready_result == NULL)
+               return;
 
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
+       if (g_queue_get_length (self->priv->messages_queue) > 0)
+               return;
 
-       priv = GET_PRIV (list);
+       DEBUG ("Ready");
 
-       if (priv->contact_monitor == NULL) {
-               priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
-       }
-
-       return priv->contact_monitor;
+       g_simple_async_result_complete (self->priv->ready_result);
+       tp_clear_object (&self->priv->ready_result);
 }
 
 static void
-tp_chat_emit_queued_messages (EmpathyTpChat *chat)
+tp_chat_emit_queued_messages (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyMessage    *message;
 
        /* Check if we can now emit some queued messages */
-       while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
+       while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
                if (empathy_message_get_sender (message) == NULL) {
                        break;
                }
 
                DEBUG ("Queued message ready");
-               g_queue_pop_head (priv->messages_queue);
-               g_queue_push_tail (priv->pending_messages_queue, message);
-               g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
+               g_queue_pop_head (self->priv->messages_queue);
+               g_queue_push_tail (self->priv->pending_messages_queue, message);
+               g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
        }
+
+       check_ready (self);
 }
 
 static void
-tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
+tp_chat_got_sender_cb (TpConnection            *connection,
                       EmpathyContact          *contact,
                       const GError            *error,
                       gpointer                 message,
                       GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
 
        if (error) {
                DEBUG ("Error: %s", error->message);
                /* Do not block the message queue, just drop this message */
-               g_queue_remove (priv->messages_queue, message);
+               g_queue_remove (self->priv->messages_queue, message);
        } else {
                empathy_message_set_sender (message, contact);
        }
 
-       tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
+       tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
 }
 
 static void
-tp_chat_build_message (EmpathyTpChat *chat,
-                      gboolean       incoming,
-                      guint          id,
-                      guint          type,
-                      guint          timestamp,
-                      guint          from_handle,
-                      const gchar   *message_body,
-                      TpChannelTextMessageFlags flags)
+tp_chat_build_message (EmpathyTpChat *self,
+                      TpMessage     *msg,
+                      gboolean       incoming)
 {
-       EmpathyTpChatPriv *priv;
        EmpathyMessage    *message;
+       TpContact *sender;
 
-       priv = GET_PRIV (chat);
+       message = empathy_message_new_from_tp_message (msg, incoming);
+       /* FIXME: this is actually a lie for incoming messages. */
+       empathy_message_set_receiver (message, self->priv->user);
 
-       message = empathy_message_new (message_body);
-       empathy_message_set_tptype (message, type);
-       empathy_message_set_receiver (message, priv->user);
-       empathy_message_set_timestamp (message, timestamp);
-       empathy_message_set_id (message, id);
-       empathy_message_set_incoming (message, incoming);
-       empathy_message_set_flags (message, flags);
+       g_queue_push_tail (self->priv->messages_queue, message);
 
-       g_queue_push_tail (priv->messages_queue, message);
+       sender = tp_signalled_message_get_sender (msg);
+       g_assert (sender != NULL);
 
-       if (from_handle == 0) {
-               empathy_message_set_sender (message, priv->user);
-               tp_chat_emit_queued_messages (chat);
+       if (tp_contact_get_handle (sender) == 0) {
+               empathy_message_set_sender (message, self->priv->user);
+               tp_chat_emit_queued_messages (self);
        } else {
-               empathy_tp_contact_factory_get_from_handle (priv->factory,
-                       from_handle,
+               TpConnection *connection = tp_channel_borrow_connection (
+                       (TpChannel *) self);
+
+               empathy_tp_contact_factory_get_from_handle (connection,
+                       tp_contact_get_handle (sender),
                        tp_chat_got_sender_cb,
-                       message, NULL, G_OBJECT (chat));
+                       message, NULL, G_OBJECT (self));
        }
 }
 
 static void
-tp_chat_received_cb (TpChannel   *channel,
-                    guint        message_id,
-                    guint        timestamp,
-                    guint        from_handle,
-                    guint        message_type,
-                    guint        message_flags,
-                    const gchar *message_body,
-                    gpointer     user_data,
-                    GObject     *chat_)
+handle_delivery_report (EmpathyTpChat *self,
+               TpMessage *message)
 {
-       EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpDeliveryStatus delivery_status;
+       const GHashTable *header;
+       TpChannelTextSendError delivery_error;
+       gboolean valid;
+       GPtrArray *echo;
+       const gchar *message_body = NULL;
+       const gchar *delivery_dbus_error;
+       const gchar *delivery_token = NULL;
+
+       header = tp_message_peek (message, 0);
+       if (header == NULL)
+               goto out;
 
-       if (priv->channel == NULL)
-               return;
+       delivery_token = tp_asv_get_string (header, "delivery-token");
+       delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
+
+       if (!valid) {
+               goto out;
+       } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
+               DEBUG ("Accepted %s", delivery_token);
+               tp_chat_set_delivery_status (self, delivery_token,
+                       EMPATHY_DELIVERY_STATUS_ACCEPTED);
+               goto out;
+       } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
+               DEBUG ("Delivered %s", delivery_token);
+               tp_chat_set_delivery_status (self, delivery_token,
+                       EMPATHY_DELIVERY_STATUS_NONE);
+               goto out;
+       } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
+               goto out;
+       }
+
+       delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
+       if (!valid)
+               delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
+
+       delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
+
+       /* TODO: ideally we should use tp-glib API giving us the echoed message as a
+        * TpMessage. (fdo #35884) */
+       echo = tp_asv_get_boxed (header, "delivery-echo",
+               TP_ARRAY_TYPE_MESSAGE_PART_LIST);
+       if (echo != NULL && echo->len >= 2) {
+               const GHashTable *echo_body;
+
+               echo_body = g_ptr_array_index (echo, 1);
+               if (echo_body != NULL)
+                       message_body = tp_asv_get_string (echo_body, "content");
+       }
+
+       tp_chat_set_delivery_status (self, delivery_token,
+                       EMPATHY_DELIVERY_STATUS_NONE);
+       g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
+                       delivery_error, delivery_dbus_error);
+
+out:
+       tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
+               message, NULL, NULL);
+}
 
-       if (priv->listing_pending_messages) {
+static void
+handle_incoming_message (EmpathyTpChat *self,
+                        TpMessage *message,
+                        gboolean pending)
+{
+       gchar *message_body;
+
+       if (tp_message_is_delivery_report (message)) {
+               handle_delivery_report (self, message);
                return;
        }
 
-       DEBUG ("Message received: %s", message_body);
+       message_body = tp_message_to_text (message, NULL);
 
-       if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
-           !tp_strdiff (message_body, "")) {
-               GArray *ids;
+       DEBUG ("Message %s (channel %s): %s",
+               pending ? "pending" : "received",
+               tp_proxy_get_object_path (self), message_body);
 
+       if (message_body == NULL) {
                DEBUG ("Empty message with NonTextContent, ignoring and acking.");
 
-               ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
-               g_array_append_val (ids, message_id);
-               acknowledge_messages (chat, ids);
-               g_array_free (ids, TRUE);
-
+               tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
+                       message, NULL, NULL);
                return;
        }
 
-       tp_chat_build_message (chat,
-                              TRUE,
-                              message_id,
-                              message_type,
-                              timestamp,
-                              from_handle,
-                              message_body,
-                              message_flags);
+       tp_chat_build_message (self, message, TRUE);
+
+       g_free (message_body);
 }
 
 static void
-tp_chat_sent_cb (TpChannel   *channel,
-                guint        timestamp,
-                guint        message_type,
-                const gchar *message_body,
-                gpointer     user_data,
-                GObject     *chat_)
+message_received_cb (TpTextChannel   *channel,
+                    TpMessage *message,
+                    EmpathyTpChat *self)
 {
-       EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       handle_incoming_message (self, message, FALSE);
+}
 
-       if (priv->channel == NULL)
-               return;
+static gboolean
+find_pending_message_func (gconstpointer a,
+                          gconstpointer b)
+{
+       EmpathyMessage *msg = (EmpathyMessage *) a;
+       TpMessage *message = (TpMessage *) b;
 
-       DEBUG ("Message sent: %s", message_body);
+       if (empathy_message_get_tp_message (msg) == message)
+               return 0;
 
-       tp_chat_build_message (chat,
-                              FALSE,
-                              0,
-                              message_type,
-                              timestamp,
-                              0,
-                              message_body,
-                              0);
+       return -1;
 }
 
 static void
-tp_chat_send_error_cb (TpChannel   *channel,
-                      guint        error_code,
-                      guint        timestamp,
-                      guint        message_type,
-                      const gchar *message_body,
-                      gpointer     user_data,
-                      GObject     *chat)
+pending_message_removed_cb (TpTextChannel   *channel,
+                           TpMessage *message,
+                           EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       GList *m;
+
+       m = g_queue_find_custom (self->priv->pending_messages_queue, message,
+                                find_pending_message_func);
 
-       if (priv->channel == NULL)
+       if (m == NULL)
                return;
 
-       DEBUG ("Error sending '%s' (%d)", message_body, error_code);
+       g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
 
-       g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
+       g_object_unref (m->data);
+       g_queue_delete_link (self->priv->pending_messages_queue, m);
 }
 
 static void
-tp_chat_send_cb (TpChannel    *proxy,
-                const GError *error,
-                gpointer      user_data,
-                GObject      *chat)
+message_sent_cb (TpTextChannel   *channel,
+                TpMessage *message,
+                TpMessageSendingFlags flags,
+                gchar              *token,
+                EmpathyTpChat      *self)
 {
-       EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
+       gchar *message_body;
 
-       if (error) {
+       message_body = tp_message_to_text (message, NULL);
+
+       DEBUG ("Message sent: %s", message_body);
+
+       tp_chat_build_message (self, message, FALSE);
+
+       g_free (message_body);
+}
+
+static TpChannelTextSendError
+error_to_text_send_error (GError *error)
+{
+       if (error->domain != TP_ERRORS)
+               return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
+
+       switch (error->code) {
+               case TP_ERROR_OFFLINE:
+                       return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
+               case TP_ERROR_INVALID_HANDLE:
+                       return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
+               case TP_ERROR_PERMISSION_DENIED:
+                       return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
+               case TP_ERROR_NOT_IMPLEMENTED:
+                       return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
+       }
+
+       return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
+}
+
+static void
+message_send_cb (GObject *source,
+                GAsyncResult *result,
+                gpointer      user_data)
+{
+       EmpathyTpChat *self = user_data;
+       TpTextChannel *channel = (TpTextChannel *) source;
+       gchar *token = NULL;
+       GError *error = NULL;
+
+       if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
                DEBUG ("Error: %s", error->message);
-               g_signal_emit (chat, signals[SEND_ERROR], 0,
-                              empathy_message_get_body (message),
-                              TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
+
+               /* FIXME: we should use the body of the message as first argument of the
+                * signal but can't easily get it as we just get a user_data pointer. Once
+                * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
+                * to use the user_data pointer to pass the message and fix this. */
+               g_signal_emit (self, signals[SEND_ERROR], 0,
+                              NULL, error_to_text_send_error (error), NULL);
+
+               g_error_free (error);
        }
+
+       tp_chat_set_delivery_status (self, token,
+               EMPATHY_DELIVERY_STATUS_SENDING);
+       g_free (token);
 }
 
 typedef struct {
@@ -412,7 +532,7 @@ typedef struct {
 } StateChangedData;
 
 static void
-tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
+tp_chat_state_changed_got_contact_cb (TpConnection            *connection,
                                      EmpathyContact          *contact,
                                      const GError            *error,
                                      gpointer                 user_data,
@@ -427,7 +547,7 @@ tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
 
        state = GPOINTER_TO_UINT (user_data);
        DEBUG ("Chat state changed for %s (%d): %d",
-               empathy_contact_get_name (contact),
+               empathy_contact_get_alias (contact),
                empathy_contact_get_handle (contact), state);
 
        g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
@@ -437,84 +557,31 @@ static void
 tp_chat_state_changed_cb (TpChannel *channel,
                          TpHandle   handle,
                          TpChannelChatState state,
-                         gpointer   user_data,
-                         GObject   *chat)
+                         EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpConnection *connection = tp_channel_borrow_connection (
+               (TpChannel *) self);
 
-       empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
+       empathy_tp_contact_factory_get_from_handle (connection, handle,
                tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
-               NULL, chat);
+               NULL, G_OBJECT (self));
 }
 
 static void
-tp_chat_list_pending_messages_cb (TpChannel       *channel,
-                                 const GPtrArray *messages_list,
-                                 const GError    *error,
-                                 gpointer         user_data,
-                                 GObject         *chat_)
+list_pending_messages (EmpathyTpChat *self)
 {
-       EmpathyTpChat     *chat = EMPATHY_TP_CHAT (chat_);
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       guint              i;
-       GArray            *empty_non_text_content_ids = NULL;
-
-       priv->listing_pending_messages = FALSE;
-
-       if (priv->channel == NULL)
-               return;
-
-       if (error) {
-               DEBUG ("Error listing pending messages: %s", error->message);
-               return;
-       }
+       GList *messages, *l;
 
-       for (i = 0; i < messages_list->len; i++) {
-               GValueArray    *message_struct;
-               const gchar    *message_body;
-               guint           message_id;
-               guint           timestamp;
-               guint           from_handle;
-               guint           message_type;
-               guint           message_flags;
+       messages = tp_text_channel_get_pending_messages (
+               TP_TEXT_CHANNEL (self));
 
-               message_struct = g_ptr_array_index (messages_list, i);
+       for (l = messages; l != NULL; l = g_list_next (l)) {
+               TpMessage *message = l->data;
 
-               message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
-               timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
-               from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
-               message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
-               message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
-               message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
-
-               DEBUG ("Message pending: %s", message_body);
-
-               if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
-                   !tp_strdiff (message_body, "")) {
-                       DEBUG ("Empty message with NonTextContent, ignoring and acking.");
-
-                       if (empty_non_text_content_ids == NULL) {
-                               empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
-                       }
-
-                       g_array_append_val (empty_non_text_content_ids, message_id);
-                       continue;
-               }
-
-               tp_chat_build_message (chat,
-                                      TRUE,
-                                      message_id,
-                                      message_type,
-                                      timestamp,
-                                      from_handle,
-                                      message_body,
-                                      message_flags);
+               handle_incoming_message (self, message, FALSE);
        }
 
-       if (empty_non_text_content_ids != NULL) {
-               acknowledge_messages (chat, empty_non_text_content_ids);
-               g_array_free (empty_non_text_content_ids, TRUE);
-       }
+       g_list_free (messages);
 }
 
 static void
@@ -523,13 +590,10 @@ tp_chat_property_flags_changed_cb (TpProxy         *proxy,
                                   gpointer         user_data,
                                   GObject         *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
        guint              i, j;
 
-       if (priv->channel == NULL)
-               return;
-
-       if (!priv->had_properties_list || !properties) {
+       if (!self->priv->had_properties_list || !properties) {
                return;
        }
 
@@ -543,8 +607,8 @@ tp_chat_property_flags_changed_cb (TpProxy         *proxy,
                id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
                flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
 
-               for (j = 0; j < priv->properties->len; j++) {
-                       property = g_ptr_array_index (priv->properties, j);
+               for (j = 0; j < self->priv->properties->len; j++) {
+                       property = g_ptr_array_index (self->priv->properties, j);
                        if (property->id == id) {
                                property->flags = flags;
                                DEBUG ("property %s flags changed: %d",
@@ -561,13 +625,10 @@ tp_chat_properties_changed_cb (TpProxy         *proxy,
                               gpointer         user_data,
                               GObject         *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
        guint              i, j;
 
-       if (priv->channel == NULL)
-               return;
-
-       if (!priv->had_properties_list || !properties) {
+       if (!self->priv->had_properties_list || !properties) {
                return;
        }
 
@@ -581,8 +642,8 @@ tp_chat_properties_changed_cb (TpProxy         *proxy,
                id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
                src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
 
-               for (j = 0; j < priv->properties->len; j++) {
-                       property = g_ptr_array_index (priv->properties, j);
+               for (j = 0; j < self->priv->properties->len; j++) {
+                       property = g_ptr_array_index (self->priv->properties, j);
                        if (property->id == id) {
                                if (property->value) {
                                        g_value_copy (src_value, property->value);
@@ -621,22 +682,19 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
                            gpointer         user_data,
                            GObject         *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
        GArray            *ids;
        guint              i;
 
-       if (priv->channel == NULL)
-               return;
-
-       priv->had_properties_list = TRUE;
-
        if (error) {
                DEBUG ("Error listing properties: %s", error->message);
                return;
        }
 
+       self->priv->had_properties_list = TRUE;
+
        ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
-       priv->properties = g_ptr_array_sized_new (properties->len);
+       self->priv->properties = g_ptr_array_sized_new (properties->len);
        for (i = 0; i < properties->len; i++) {
                GValueArray           *prop_struct;
                EmpathyTpChatProperty *property;
@@ -649,7 +707,7 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
 
                DEBUG ("Adding property name=%s id=%d flags=%d",
                        property->name, property->id, property->flags);
-               g_ptr_array_add (priv->properties, property);
+               g_ptr_array_add (self->priv->properties, property);
                if (property->flags & TP_PROPERTY_FLAG_READ) {
                        g_array_append_val (ids, property->id);
                }
@@ -665,20 +723,19 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
 }
 
 void
-empathy_tp_chat_set_property (EmpathyTpChat *chat,
+empathy_tp_chat_set_property (EmpathyTpChat *self,
                              const gchar   *name,
                              const GValue  *value)
 {
-       EmpathyTpChatPriv     *priv = GET_PRIV (chat);
        EmpathyTpChatProperty *property;
        guint                  i;
 
-       if (!priv->had_properties_list) {
+       if (!self->priv->had_properties_list) {
                return;
        }
 
-       for (i = 0; i < priv->properties->len; i++) {
-               property = g_ptr_array_index (priv->properties, i);
+       for (i = 0; i < self->priv->properties->len; i++) {
+               property = g_ptr_array_index (self->priv->properties, i);
                if (!tp_strdiff (property->name, name)) {
                        GPtrArray   *properties;
                        GValueArray *prop;
@@ -702,12 +759,12 @@ empathy_tp_chat_set_property (EmpathyTpChat *chat,
                        g_ptr_array_add (properties, prop);
 
                        DEBUG ("Set property %s", name);
-                       tp_cli_properties_interface_call_set_properties (priv->channel, -1,
+                       tp_cli_properties_interface_call_set_properties (self, -1,
                                                                         properties,
                                                                         (tp_cli_properties_interface_callback_for_set_properties)
                                                                         tp_chat_async_cb,
                                                                         "Seting property", NULL,
-                                                                        G_OBJECT (chat));
+                                                                        G_OBJECT (self));
 
                        g_ptr_array_free (properties, TRUE);
                        g_value_array_free (prop);
@@ -718,19 +775,18 @@ empathy_tp_chat_set_property (EmpathyTpChat *chat,
 }
 
 EmpathyTpChatProperty *
-empathy_tp_chat_get_property (EmpathyTpChat *chat,
+empathy_tp_chat_get_property (EmpathyTpChat *self,
                              const gchar   *name)
 {
-       EmpathyTpChatPriv     *priv = GET_PRIV (chat);
        EmpathyTpChatProperty *property;
        guint                  i;
 
-       if (!priv->had_properties_list) {
+       if (!self->priv->had_properties_list) {
                return NULL;
        }
 
-       for (i = 0; i < priv->properties->len; i++) {
-               property = g_ptr_array_index (priv->properties, i);
+       for (i = 0; i < self->priv->properties->len; i++) {
+               property = g_ptr_array_index (self->priv->properties, i);
                if (!tp_strdiff (property->name, name)) {
                        return property;
                }
@@ -740,53 +796,28 @@ empathy_tp_chat_get_property (EmpathyTpChat *chat,
 }
 
 GPtrArray *
-empathy_tp_chat_get_properties (EmpathyTpChat *chat)
+empathy_tp_chat_get_properties (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       return priv->properties;
+       return self->priv->properties;
 }
 
 static void
 tp_chat_dispose (GObject *object)
 {
        EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
-
-       if (priv->dispose_has_run)
-               return;
 
-       priv->dispose_has_run = TRUE;
-
-       if (priv->channel != NULL) {
-               g_signal_handlers_disconnect_by_func (priv->channel,
-                       tp_chat_invalidated_cb, self);
-               g_object_unref (priv->channel);
-       }
-       priv->channel = NULL;
+       tp_clear_object (&self->priv->account);
+       tp_clear_object (&self->priv->remote_contact);
+       tp_clear_object (&self->priv->user);
 
-       if (priv->remote_contact != NULL)
-               g_object_unref (priv->remote_contact);
-       priv->remote_contact = NULL;
+       g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
+       g_queue_clear (self->priv->messages_queue);
 
-       if (priv->factory != NULL)
-               g_object_unref (priv->factory);
-       priv->factory = NULL;
-
-       if (priv->user != NULL)
-               g_object_unref (priv->user);
-       priv->user = NULL;
-
-       if (priv->contact_monitor)
-               g_object_unref (priv->contact_monitor);
-       priv->contact_monitor = NULL;
-
-       g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
-       g_queue_clear (priv->messages_queue);
-
-       g_queue_foreach (priv->pending_messages_queue,
+       g_queue_foreach (self->priv->pending_messages_queue,
                (GFunc) g_object_unref, NULL);
-       g_queue_clear (priv->pending_messages_queue);
+       g_queue_clear (self->priv->pending_messages_queue);
+
+       tp_clear_object (&self->priv->ready_result);
 
        if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
                G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
@@ -795,92 +826,80 @@ tp_chat_dispose (GObject *object)
 static void
 tp_chat_finalize (GObject *object)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (object);
+       EmpathyTpChat *self = (EmpathyTpChat *) object;
        guint              i;
 
        DEBUG ("Finalize: %p", object);
 
-       if (priv->properties) {
-               for (i = 0; i < priv->properties->len; i++) {
+       if (self->priv->properties) {
+               for (i = 0; i < self->priv->properties->len; i++) {
                        EmpathyTpChatProperty *property;
 
-                       property = g_ptr_array_index (priv->properties, i);
+                       property = g_ptr_array_index (self->priv->properties, i);
                        g_free (property->name);
                        if (property->value) {
                                tp_g_value_slice_free (property->value);
                        }
                        g_slice_free (EmpathyTpChatProperty, property);
                }
-               g_ptr_array_free (priv->properties, TRUE);
+               g_ptr_array_free (self->priv->properties, TRUE);
        }
 
-       g_queue_free (priv->messages_queue);
-       g_queue_free (priv->pending_messages_queue);
+       g_queue_free (self->priv->messages_queue);
+       g_queue_free (self->priv->pending_messages_queue);
+       g_hash_table_destroy (self->priv->messages_being_sent);
 
        G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
 
 static void
-tp_chat_check_if_ready (EmpathyTpChat *chat)
+check_almost_ready (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpChannel *channel = (TpChannel *) self;
 
-       if (priv->ready)
+       if (self->priv->ready_result == NULL)
                return;
 
-       if (priv->user == NULL)
-               return;
-
-       if (!priv->got_password_flags)
+       if (self->priv->user == NULL)
                return;
 
        /* We need either the members (room) or the remote contact (private chat).
         * If the chat is protected by a password we can't get these information so
         * consider the chat as ready so it can be presented to the user. */
-       if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
-           priv->remote_contact == NULL)
+       if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
+           self->priv->remote_contact == NULL)
                return;
 
-       DEBUG ("Ready!");
-
-       tp_cli_channel_type_text_connect_to_received (priv->channel,
-                                                     tp_chat_received_cb,
-                                                     NULL, NULL,
-                                                     G_OBJECT (chat), NULL);
-       priv->listing_pending_messages = TRUE;
-       tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
-                                                            FALSE,
-                                                            tp_chat_list_pending_messages_cb,
-                                                            NULL, NULL,
-                                                            G_OBJECT (chat));
-
-       tp_cli_channel_type_text_connect_to_sent (priv->channel,
-                                                 tp_chat_sent_cb,
-                                                 NULL, NULL,
-                                                 G_OBJECT (chat), NULL);
-       tp_cli_channel_type_text_connect_to_send_error (priv->channel,
-                                                       tp_chat_send_error_cb,
-                                                       NULL, NULL,
-                                                       G_OBJECT (chat), NULL);
-       tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
-                                                                          tp_chat_state_changed_cb,
-                                                                          NULL, NULL,
-                                                                          G_OBJECT (chat), NULL);
-       priv->ready = TRUE;
-       g_object_notify (G_OBJECT (chat), "ready");
+       g_assert (tp_proxy_is_prepared (self,
+               TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
+
+       tp_g_signal_connect_object (self, "message-received",
+               G_CALLBACK (message_received_cb), self, 0);
+       tp_g_signal_connect_object (self, "pending-message-removed",
+               G_CALLBACK (pending_message_removed_cb), self, 0);
+
+       list_pending_messages (self);
+
+       tp_g_signal_connect_object (self, "message-sent",
+               G_CALLBACK (message_sent_cb), self, 0);
+
+       tp_g_signal_connect_object (self, "chat-state-changed",
+               G_CALLBACK (tp_chat_state_changed_cb), self, 0);
+
+       check_ready (self);
 }
 
 static void
-tp_chat_update_remote_contact (EmpathyTpChat *chat)
+tp_chat_update_remote_contact (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpChannel *channel = (TpChannel *) self;
        EmpathyContact *contact = NULL;
        TpHandle self_handle;
        TpHandleType handle_type;
        GList *l;
 
        /* If this is a named chatroom, never pretend it is a private chat */
-       tp_channel_get_handle (priv->channel, &handle_type);
+       tp_channel_get_handle (channel, &handle_type);
        if (handle_type == TP_HANDLE_TYPE_ROOM) {
                return;
        }
@@ -888,8 +907,8 @@ tp_chat_update_remote_contact (EmpathyTpChat *chat)
        /* This is an MSN chat, but it's the new style where 1-1 chats don't
         * have the group interface. If it has the conference interface, then
         * it is indeed a MUC. */
-       if (tp_proxy_has_interface_by_id (priv->channel,
-                                         EMP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
+       if (tp_proxy_has_interface_by_id (self,
+                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
                return;
        }
 
@@ -898,8 +917,8 @@ tp_chat_update_remote_contact (EmpathyTpChat *chat)
         * chat and we set the "remote-contact" property to that contact. If
         * there are more, set the "remote-contact" property to NULL and the
         * UI will display a contact list. */
-       self_handle = tp_channel_group_get_self_handle (priv->channel);
-       for (l = priv->members; l; l = l->next) {
+       self_handle = tp_channel_group_get_self_handle (channel);
+       for (l = self->priv->members; l; l = l->next) {
                /* Skip self contact if member */
                if (empathy_contact_get_handle (l->data) == self_handle) {
                        continue;
@@ -915,23 +934,23 @@ tp_chat_update_remote_contact (EmpathyTpChat *chat)
                contact = l->data;
        }
 
-       if (priv->remote_contact == contact) {
+       if (self->priv->remote_contact == contact) {
                return;
        }
 
        DEBUG ("Changing remote contact from %p to %p",
-               priv->remote_contact, contact);
+               self->priv->remote_contact, contact);
 
-       if (priv->remote_contact) {
-               g_object_unref (priv->remote_contact);
+       if (self->priv->remote_contact) {
+               g_object_unref (self->priv->remote_contact);
        }
 
-       priv->remote_contact = contact ? g_object_ref (contact) : NULL;
-       g_object_notify (G_OBJECT (chat), "remote-contact");
+       self->priv->remote_contact = contact ? g_object_ref (contact) : NULL;
+       g_object_notify (G_OBJECT (self), "remote-contact");
 }
 
 static void
-tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
+tp_chat_got_added_contacts_cb (TpConnection            *connection,
                               guint                    n_contacts,
                               EmpathyContact * const * contacts,
                               guint                    n_failed,
@@ -940,7 +959,7 @@ tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
                               gpointer                 user_data,
                               GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
        guint i;
        const TpIntSet *members;
        TpHandle handle;
@@ -951,14 +970,14 @@ tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
                return;
        }
 
-       members = tp_channel_group_get_members (priv->channel);
+       members = tp_channel_group_get_members ((TpChannel *) self);
        for (i = 0; i < n_contacts; i++) {
                contact = contacts[i];
                handle = empathy_contact_get_handle (contact);
 
                /* Make sure the contact is still member */
                if (tp_intset_is_member (members, handle)) {
-                       priv->members = g_list_prepend (priv->members,
+                       self->priv->members = g_list_prepend (self->priv->members,
                                g_object_ref (contact));
                        g_signal_emit_by_name (chat, "members-changed",
                                               contact, NULL, 0, NULL, TRUE);
@@ -966,18 +985,17 @@ tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
        }
 
        tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
-       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+       check_almost_ready (EMPATHY_TP_CHAT (chat));
 }
 
 static EmpathyContact *
-chat_lookup_contact (EmpathyTpChat *chat,
+chat_lookup_contact (EmpathyTpChat *self,
                     TpHandle       handle,
                     gboolean       remove_)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
        GList *l;
 
-       for (l = priv->members; l; l = l->next) {
+       for (l = self->priv->members; l; l = l->next) {
                EmpathyContact *c = l->data;
 
                if (empathy_contact_get_handle (c) != handle) {
@@ -986,7 +1004,7 @@ chat_lookup_contact (EmpathyTpChat *chat,
 
                if (remove_) {
                        /* Caller takes the reference. */
-                       priv->members = g_list_delete_link (priv->members, l);
+                       self->priv->members = g_list_delete_link (self->priv->members, l);
                } else {
                        g_object_ref (c);
                }
@@ -1025,7 +1043,7 @@ contact_rename_data_free (ContactRenameData* data)
 }
 
 static void
-tp_chat_got_renamed_contacts_cb (EmpathyTpContactFactory *factory,
+tp_chat_got_renamed_contacts_cb (TpConnection            *connection,
                                  guint                    n_contacts,
                                  EmpathyContact * const * contacts,
                                  guint                    n_failed,
@@ -1034,7 +1052,7 @@ tp_chat_got_renamed_contacts_cb (EmpathyTpContactFactory *factory,
                                  gpointer                 user_data,
                                  GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
        const TpIntSet *members;
        TpHandle handle;
        EmpathyContact *old = NULL, *new = NULL;
@@ -1050,32 +1068,37 @@ tp_chat_got_renamed_contacts_cb (EmpathyTpContactFactory *factory,
 
        new = contacts[0];
 
-       members = tp_channel_group_get_members (priv->channel);
+       members = tp_channel_group_get_members ((TpChannel *) self);
        handle = empathy_contact_get_handle (new);
 
-       old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
-                                  rename_data->old_handle, TRUE);
+       old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
 
        /* Make sure the contact is still member */
        if (tp_intset_is_member (members, handle)) {
-               priv->members = g_list_prepend (priv->members,
+               self->priv->members = g_list_prepend (self->priv->members,
                        g_object_ref (new));
 
                if (old != NULL) {
-                       g_signal_emit_by_name (chat, "member-renamed",
+                       g_signal_emit_by_name (self, "member-renamed",
                                               old, new, rename_data->reason,
                                               rename_data->message);
                        g_object_unref (old);
                }
        }
 
-       tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
-       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+       if (self->priv->user == old) {
+               /* We change our nick */
+               tp_clear_object (&self->priv->user);
+               self->priv->user = g_object_ref (new);
+       }
+
+       tp_chat_update_remote_contact (self);
+       check_almost_ready (self);
 }
 
 
 static void
-tp_chat_group_members_changed_cb (TpChannel     *self,
+tp_chat_group_members_changed_cb (TpChannel     *channel,
                                  gchar         *message,
                                  GArray        *added,
                                  GArray        *removed,
@@ -1083,34 +1106,38 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
                                  GArray        *remote_pending,
                                  guint          actor,
                                  guint          reason,
-                                 EmpathyTpChat *chat)
+                                 EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyContact *contact;
        EmpathyContact *actor_contact = NULL;
        guint i;
        ContactRenameData *rename_data;
        TpHandle old_handle;
+       TpConnection *connection = tp_channel_borrow_connection (
+               (TpChannel *) self);
 
        /* Contact renamed */
        if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
                /* there can only be a single 'added' and a single 'removed' handle */
-               g_warn_if_fail (removed->len == 1);
-               g_warn_if_fail (added->len == 1);
+               if (removed->len != 1 || added->len != 1) {
+                       g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
+                               added->len, removed->len);
+                       return;
+               }
 
                old_handle = g_array_index (removed, guint, 0);
 
                rename_data = contact_rename_data_new (old_handle, reason, message);
-               empathy_tp_contact_factory_get_from_handles (priv->factory,
+               empathy_tp_contact_factory_get_from_handles (connection,
                        added->len, (TpHandle *) added->data,
                        tp_chat_got_renamed_contacts_cb,
                        rename_data, (GDestroyNotify) contact_rename_data_free,
-                       G_OBJECT (chat));
+                       G_OBJECT (self));
                return;
        }
 
        if (actor != 0) {
-               actor_contact = chat_lookup_contact (chat, actor, FALSE);
+               actor_contact = chat_lookup_contact (self, actor, FALSE);
                if (actor_contact == NULL) {
                        /* FIXME: handle this a tad more gracefully: perhaps
                         * the actor was a server op. We could use the
@@ -1122,11 +1149,11 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
 
        /* Remove contacts that are not members anymore */
        for (i = 0; i < removed->len; i++) {
-               contact = chat_lookup_contact (chat,
+               contact = chat_lookup_contact (self,
                        g_array_index (removed, TpHandle, i), TRUE);
 
                if (contact != NULL) {
-                       g_signal_emit_by_name (chat, "members-changed", contact,
+                       g_signal_emit_by_name (self, "members-changed", contact,
                                               actor_contact, reason, message,
                                               FALSE);
                        g_object_unref (contact);
@@ -1135,13 +1162,13 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
 
        /* Request added contacts */
        if (added->len > 0) {
-               empathy_tp_contact_factory_get_from_handles (priv->factory,
+               empathy_tp_contact_factory_get_from_handles (connection,
                        added->len, (TpHandle *) added->data,
                        tp_chat_got_added_contacts_cb, NULL, NULL,
-                       G_OBJECT (chat));
+                       G_OBJECT (self));
        }
 
-       tp_chat_update_remote_contact (chat);
+       tp_chat_update_remote_contact (self);
 
        if (actor_contact != NULL) {
                g_object_unref (actor_contact);
@@ -1149,195 +1176,44 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
 }
 
 static void
-tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
+tp_chat_got_remote_contact_cb (TpConnection            *connection,
                               EmpathyContact          *contact,
                               const GError            *error,
                               gpointer                 user_data,
                               GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
 
        if (error) {
                DEBUG ("Error: %s", error->message);
-               empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
+               empathy_tp_chat_leave (self, "");
                return;
        }
 
-       priv->remote_contact = g_object_ref (contact);
+       self->priv->remote_contact = g_object_ref (contact);
        g_object_notify (chat, "remote-contact");
 
-       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+       check_almost_ready (self);
 }
 
 static void
-tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
+tp_chat_got_self_contact_cb (TpConnection            *connection,
                             EmpathyContact          *contact,
                             const GError            *error,
                             gpointer                 user_data,
                             GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyTpChat *self = (EmpathyTpChat *) chat;
 
        if (error) {
                DEBUG ("Error: %s", error->message);
-               empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
+               empathy_tp_chat_leave (self, "");
                return;
        }
 
-       priv->user = g_object_ref (contact);
-       empathy_contact_set_is_user (priv->user, TRUE);
-       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
-}
-
-static void
-password_flags_changed_cb (TpChannel *channel,
-    guint added,
-    guint removed,
-    gpointer user_data,
-    GObject *weak_object)
-{
-       EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
-       gboolean was_needed, needed;
-
-       was_needed = empathy_tp_chat_password_needed (self);
-
-       priv->password_flags |= added;
-       priv->password_flags ^= removed;
-
-       needed = empathy_tp_chat_password_needed (self);
-
-       if (was_needed != needed)
-               g_object_notify (G_OBJECT (self), "password-needed");
-}
-
-static void
-got_password_flags_cb (TpChannel *proxy,
-                            guint password_flags,
-                            const GError *error,
-                            gpointer user_data,
-                            GObject *weak_object)
-{
-       EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
-
-       priv->got_password_flags = TRUE;
-       priv->password_flags = password_flags;
-
-       tp_chat_check_if_ready (EMPATHY_TP_CHAT (self));
-}
-
-static GObject *
-tp_chat_constructor (GType                  type,
-                    guint                  n_props,
-                    GObjectConstructParam *props)
-{
-       GObject           *chat;
-       EmpathyTpChatPriv *priv;
-       TpConnection      *connection;
-       TpHandle           handle;
-
-       chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
-
-       priv = GET_PRIV (chat);
-
-       connection = tp_channel_borrow_connection (priv->channel);
-       priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
-       g_signal_connect (priv->channel, "invalidated",
-                         G_CALLBACK (tp_chat_invalidated_cb),
-                         chat);
-
-       if (tp_proxy_has_interface_by_id (priv->channel,
-                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
-               const TpIntSet *members;
-               GArray *handles;
-
-               /* Get self contact from the group's self handle */
-               handle = tp_channel_group_get_self_handle (priv->channel);
-               empathy_tp_contact_factory_get_from_handle (priv->factory,
-                       handle, tp_chat_got_self_contact_cb,
-                       NULL, NULL, chat);
-
-               /* Get initial member contacts */
-               members = tp_channel_group_get_members (priv->channel);
-               handles = tp_intset_to_array (members);
-               empathy_tp_contact_factory_get_from_handles (priv->factory,
-                       handles->len, (TpHandle *) handles->data,
-                       tp_chat_got_added_contacts_cb, NULL, NULL, chat);
-
-               priv->can_upgrade_to_muc = FALSE;
-
-               g_signal_connect (priv->channel, "group-members-changed",
-                       G_CALLBACK (tp_chat_group_members_changed_cb), chat);
-       } else {
-               EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
-               GList *list, *ptr;
-
-               /* Get the self contact from the connection's self handle */
-               handle = tp_connection_get_self_handle (connection);
-               empathy_tp_contact_factory_get_from_handle (priv->factory,
-                       handle, tp_chat_got_self_contact_cb,
-                       NULL, NULL, chat);
-
-               /* Get the remote contact */
-               handle = tp_channel_get_handle (priv->channel, NULL);
-               empathy_tp_contact_factory_get_from_handle (priv->factory,
-                       handle, tp_chat_got_remote_contact_cb,
-                       NULL, NULL, chat);
-
-               list = empathy_dispatcher_find_requestable_channel_classes (
-                       dispatcher, connection,
-                       tp_channel_get_channel_type (priv->channel),
-                       TP_UNKNOWN_HANDLE_TYPE, NULL);
-
-               for (ptr = list; ptr; ptr = ptr->next) {
-                       GValueArray *array = ptr->data;
-                       const char **oprops = g_value_get_boxed (
-                               g_value_array_get_nth (array, 1));
-
-                       if (tp_strv_contains (oprops, EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialChannels")) {
-                               priv->can_upgrade_to_muc = TRUE;
-                               break;
-                       }
-               }
-
-               g_list_free (list);
-               g_object_unref (dispatcher);
-       }
-
-       if (tp_proxy_has_interface_by_id (priv->channel,
-                                         TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
-               tp_cli_properties_interface_call_list_properties (priv->channel, -1,
-                                                                 tp_chat_list_properties_cb,
-                                                                 NULL, NULL,
-                                                                 G_OBJECT (chat));
-               tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
-                                                                          tp_chat_properties_changed_cb,
-                                                                          NULL, NULL,
-                                                                          G_OBJECT (chat), NULL);
-               tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
-                                                                              tp_chat_property_flags_changed_cb,
-                                                                              NULL, NULL,
-                                                                              G_OBJECT (chat), NULL);
-       }
-
-       /* Check if the chat is password protected */
-       if (tp_proxy_has_interface_by_id (priv->channel,
-                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
-               priv->got_password_flags = FALSE;
-
-               tp_cli_channel_interface_password_connect_to_password_flags_changed
-                       (priv->channel, password_flags_changed_cb, chat, NULL,
-                        G_OBJECT (chat), NULL);
-
-               tp_cli_channel_interface_password_call_get_password_flags
-                       (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
-       } else {
-               /* No Password interface, so no need to fetch the password flags */
-               priv->got_password_flags = TRUE;
-       }
-
-       return chat;
+       self->priv->user = g_object_ref (contact);
+       empathy_contact_set_is_user (self->priv->user, TRUE);
+       check_almost_ready (self);
 }
 
 static void
@@ -1347,20 +1223,17 @@ tp_chat_get_property (GObject    *object,
                      GParamSpec *pspec)
 {
        EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
-       EmpathyTpChatPriv *priv = GET_PRIV (object);
 
        switch (param_id) {
-       case PROP_CHANNEL:
-               g_value_set_object (value, priv->channel);
+       case PROP_ACCOUNT:
+               g_value_set_object (value, self->priv->account);
                break;
        case PROP_REMOTE_CONTACT:
-               g_value_set_object (value, priv->remote_contact);
+               g_value_set_object (value, self->priv->remote_contact);
                break;
-       case PROP_READY:
-               g_value_set_boolean (value, priv->ready);
-               break;
-       case PROP_PASSWORD_NEEDED:
-               g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
+       case PROP_N_MESSAGES_SENDING:
+               g_value_set_uint (value,
+                       g_hash_table_size (self->priv->messages_being_sent));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -1374,11 +1247,11 @@ tp_chat_set_property (GObject      *object,
                      const GValue *value,
                      GParamSpec   *pspec)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (object);
+       EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
 
        switch (param_id) {
-       case PROP_CHANNEL:
-               priv->channel = g_value_dup_object (value);
+       case PROP_ACCOUNT:
+               self->priv->account = g_value_dup_object (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -1386,25 +1259,54 @@ tp_chat_set_property (GObject      *object,
        };
 }
 
+enum {
+       FEAT_READY,
+       N_FEAT
+};
+
+static const TpProxyFeature *
+tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
+{
+       static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
+  static GQuark need[2] = {0, 0};
+
+       if (G_LIKELY (features[0].name != 0))
+               return features;
+
+       features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
+       need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
+       features[FEAT_READY].depends_on = need;
+       features[FEAT_READY].prepare_async =
+               tp_chat_prepare_ready_async;
+
+       /* assert that the terminator at the end is there */
+       g_assert (features[N_FEAT].name == 0);
+
+       return features;
+}
+
 static void
 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
 
        object_class->dispose = tp_chat_dispose;
        object_class->finalize = tp_chat_finalize;
-       object_class->constructor = tp_chat_constructor;
        object_class->get_property = tp_chat_get_property;
        object_class->set_property = tp_chat_set_property;
 
+       proxy_class->list_features = tp_chat_list_features;
+
        g_object_class_install_property (object_class,
-                                        PROP_CHANNEL,
-                                        g_param_spec_object ("channel",
-                                                             "telepathy channel",
-                                                             "The text channel for the chat",
-                                                             TP_TYPE_CHANNEL,
+                                        PROP_ACCOUNT,
+                                        g_param_spec_object ("account",
+                                                             "TpAccount",
+                                                             "the account associated with the chat",
+                                                             TP_TYPE_ACCOUNT,
                                                              G_PARAM_READWRITE |
-                                                             G_PARAM_CONSTRUCT_ONLY));
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
 
        g_object_class_install_property (object_class,
                                         PROP_REMOTE_CONTACT,
@@ -1415,24 +1317,16 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                                                              G_PARAM_READABLE));
 
        g_object_class_install_property (object_class,
-                                        PROP_READY,
-                                        g_param_spec_boolean ("ready",
-                                                              "Is the object ready",
-                                                              "This object can't be used until this becomes true",
-                                                              FALSE,
-                                                              G_PARAM_READABLE));
-
-       g_object_class_install_property (object_class,
-                                        PROP_PASSWORD_NEEDED,
-                                        g_param_spec_boolean ("password-needed",
-                                                              "password needed",
-                                                              "TRUE if a password is needed to join the channel",
-                                                              FALSE,
-                                                              G_PARAM_READABLE));
+                                        PROP_N_MESSAGES_SENDING,
+                                        g_param_spec_uint ("n-messages-sending",
+                                                           "Num Messages Sending",
+                                                           "The number of messages being sent",
+                                                           0, G_MAXUINT, 0,
+                                                           G_PARAM_READABLE));
 
        /* Signals */
        signals[MESSAGE_RECEIVED] =
-               g_signal_new ("message-received",
+               g_signal_new ("message-received-empathy",
                              G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              0,
@@ -1447,12 +1341,12 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             _empathy_marshal_VOID__STRING_UINT,
+                             _empathy_marshal_VOID__STRING_UINT_STRING,
                              G_TYPE_NONE,
-                             2, G_TYPE_STRING, G_TYPE_UINT);
+                             3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
 
        signals[CHAT_STATE_CHANGED] =
-               g_signal_new ("chat-state-changed",
+               g_signal_new ("chat-state-changed-empathy",
                              G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              0,
@@ -1471,29 +1365,29 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                              G_TYPE_NONE,
                              2, G_TYPE_STRING, G_TYPE_VALUE);
 
-       signals[DESTROY] =
-               g_signal_new ("destroy",
+       signals[MESSAGE_ACKNOWLEDGED] =
+               g_signal_new ("message-acknowledged",
                              G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             g_cclosure_marshal_VOID__VOID,
+                             g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE,
-                             0);
+                             1, EMPATHY_TYPE_MESSAGE);
 
-       g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
+       g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
 }
 
 static void
-empathy_tp_chat_init (EmpathyTpChat *chat)
+empathy_tp_chat_init (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
-               EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
+       self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+               EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
 
-       chat->priv = priv;
-       priv->contact_monitor = NULL;
-       priv->messages_queue = g_queue_new ();
-       priv->pending_messages_queue = g_queue_new ();
+       self->priv->messages_queue = g_queue_new ();
+       self->priv->pending_messages_queue = g_queue_new ();
+       self->priv->messages_being_sent = g_hash_table_new_full (
+               g_str_hash, g_str_equal, g_free, NULL);
 }
 
 static void
@@ -1502,357 +1396,310 @@ tp_chat_iface_init (EmpathyContactListIface *iface)
        iface->add         = tp_chat_add;
        iface->remove      = tp_chat_remove;
        iface->get_members = tp_chat_get_members;
-       iface->get_monitor = tp_chat_get_monitor;
 }
 
 EmpathyTpChat *
-empathy_tp_chat_new (TpChannel *channel)
+empathy_tp_chat_new (
+                    TpSimpleClientFactory *factory,
+                    TpAccount *account,
+                    TpConnection *conn,
+                    const gchar *object_path,
+                    const GHashTable *immutable_properties)
 {
-       return g_object_new (EMPATHY_TYPE_TP_CHAT,
-                            "channel", channel,
-                            NULL);
-}
+       TpProxy *conn_proxy = (TpProxy *) conn;
 
-static void
-empathy_tp_chat_close (EmpathyTpChat *chat) {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
+       g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
+       g_return_val_if_fail (immutable_properties != NULL, NULL);
 
-       /* If there are still messages left, it'll come back..
-        * We loose the ordering of sent messages though */
-       tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
-               "closing channel", NULL, NULL);
+       return g_object_new (EMPATHY_TYPE_TP_CHAT,
+                            "factory", factory,
+                            "account", account,
+                            "connection", conn,
+                            "dbus-daemon", conn_proxy->dbus_daemon,
+                            "bus-name", conn_proxy->bus_name,
+                            "object-path", object_path,
+                            "channel-properties", immutable_properties,
+                            NULL);
 }
 
 const gchar *
-empathy_tp_chat_get_id (EmpathyTpChat *chat)
+empathy_tp_chat_get_id (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
        const gchar *id;
 
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
 
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
-
-       id = tp_channel_get_identifier (priv->channel);
+       id = tp_channel_get_identifier ((TpChannel *) self);
        if (!EMP_STR_EMPTY (id))
                return id;
-       else if (priv->remote_contact)
-               return empathy_contact_get_id (priv->remote_contact);
+       else if (self->priv->remote_contact)
+               return empathy_contact_get_id (self->priv->remote_contact);
        else
                return NULL;
 
 }
 
 EmpathyContact *
-empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
-       g_return_val_if_fail (priv->ready, NULL);
-
-       return priv->remote_contact;
-}
-
-TpChannel *
-empathy_tp_chat_get_channel (EmpathyTpChat *chat)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
-
-       return priv->channel;
-}
-
-TpConnection *
-empathy_tp_chat_get_connection (EmpathyTpChat *chat)
+empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
 
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
-
-       return tp_channel_borrow_connection (priv->channel);
+       return self->priv->remote_contact;
 }
 
-gboolean
-empathy_tp_chat_is_ready (EmpathyTpChat *chat)
+TpAccount *
+empathy_tp_chat_get_account (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
 
-       return priv->ready;
+       return self->priv->account;
 }
 
 void
-empathy_tp_chat_send (EmpathyTpChat *chat,
-                     EmpathyMessage *message)
+empathy_tp_chat_send (EmpathyTpChat *self,
+                     TpMessage *message)
 {
-       EmpathyTpChatPriv        *priv = GET_PRIV (chat);
-       const gchar              *message_body;
-       TpChannelTextMessageType  message_type;
+       gchar *message_body;
 
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-       g_return_if_fail (EMPATHY_IS_MESSAGE (message));
-       g_return_if_fail (priv->ready);
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
+       g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
 
-       message_body = empathy_message_get_body (message);
-       message_type = empathy_message_get_tptype (message);
+       message_body = tp_message_to_text (message, NULL);
 
        DEBUG ("Sending message: %s", message_body);
-       tp_cli_channel_type_text_call_send (priv->channel, -1,
-                                           message_type,
-                                           message_body,
-                                           tp_chat_send_cb,
-                                           g_object_ref (message),
-                                           (GDestroyNotify) g_object_unref,
-                                           G_OBJECT (chat));
-}
 
-void
-empathy_tp_chat_set_state (EmpathyTpChat      *chat,
-                          TpChannelChatState  state)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
+               message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
+               message_send_cb, self);
 
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-       g_return_if_fail (priv->ready);
-
-       if (tp_proxy_has_interface_by_id (priv->channel,
-                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
-               DEBUG ("Set state: %d", state);
-               tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
-                                                                        state,
-                                                                        tp_chat_async_cb,
-                                                                        "setting chat state",
-                                                                        NULL,
-                                                                        G_OBJECT (chat));
-       }
+       g_free (message_body);
 }
 
-
 const GList *
-empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
+empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
-       g_return_val_if_fail (priv->ready, NULL);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
 
-       return priv->pending_messages_queue->head;
-}
-
-static void
-acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       tp_cli_channel_type_text_call_acknowledge_pending_messages (
-               priv->channel, -1, ids, tp_chat_async_cb,
-               "acknowledging received message", NULL, G_OBJECT (chat));
+       return self->priv->pending_messages_queue->head;
 }
 
 void
-empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
+empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
                                     EmpathyMessage *message) {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       GArray *message_ids;
-       GList *m;
-       guint id;
+       TpMessage *tp_msg;
 
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-       g_return_if_fail (priv->ready);
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
 
        if (!empathy_message_is_incoming (message))
-               goto out;
-
-       message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
-
-       id = empathy_message_get_id (message);
-       g_array_append_val (message_ids, id);
-       acknowledge_messages (chat, message_ids);
-       g_array_free (message_ids, TRUE);
-
-out:
-       m = g_queue_find (priv->pending_messages_queue, message);
-       g_assert (m != NULL);
-       g_queue_delete_link (priv->pending_messages_queue, m);
-       g_object_unref (message);
-}
-
-void
-empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
-                                     const GSList *messages) {
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       /* Copy messages as the messges list (probably is) our own */
-       GSList *msgs = g_slist_copy ((GSList *) messages);
-       GSList *l;
-       guint length;
-       GArray *message_ids;
-
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-       g_return_if_fail (priv->ready);
-
-       length = g_slist_length ((GSList *) messages);
-
-       if (length == 0)
                return;
 
-       message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
+       tp_msg = empathy_message_get_tp_message (message);
+       tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
+                                          tp_msg, NULL, NULL);
+}
 
-       for (l = msgs; l != NULL; l = g_slist_next (l)) {
-               GList *m;
+/**
+ * empathy_tp_chat_can_add_contact:
+ *
+ * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
+ * That is if this chat is a 1-to-1 channel that can be upgraded to
+ * a MUC using the Conference interface or if the channel is a MUC.
+ */
+gboolean
+empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
+{
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
 
-               EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
+       return self->priv->can_upgrade_to_muc ||
+               tp_proxy_has_interface_by_id (self,
+                       TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
+}
 
-               m = g_queue_find (priv->pending_messages_queue, message);
-               g_assert (m != NULL);
-               g_queue_delete_link (priv->pending_messages_queue, m);
+static void
+tp_channel_leave_async_cb (GObject *source_object,
+        GAsyncResult *res,
+        gpointer user_data)
+{
+       GError *error = NULL;
 
-               if (empathy_message_is_incoming (message)) {
-                       guint id = empathy_message_get_id (message);
-                       g_array_append_val (message_ids, id);
-               }
-               g_object_unref (message);
+       if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
+               DEBUG ("Could not leave channel properly: (%s); closing the channel",
+                       error->message);
+               g_error_free (error);
        }
-
-       if (message_ids->len > 0)
-               acknowledge_messages (chat, message_ids);
-
-       g_array_free (message_ids, TRUE);
-       g_slist_free (msgs);
 }
 
 void
-empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
+empathy_tp_chat_leave (EmpathyTpChat *self,
+               const gchar *message)
 {
-  empathy_tp_chat_acknowledge_messages (chat,
-    (GSList *) empathy_tp_chat_get_pending_messages (chat));
-}
+       TpChannel *channel = (TpChannel *) self;
 
-gboolean
-empathy_tp_chat_password_needed (EmpathyTpChat *self)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
+       DEBUG ("Leaving channel %s with message \"%s\"",
+               tp_channel_get_identifier (channel), message);
 
-       return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
+       tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
+               message, tp_channel_leave_async_cb, self);
 }
 
 static void
-provide_password_cb (TpChannel *channel,
-                                     gboolean correct,
-                                     const GError *error,
-                                     gpointer user_data,
-                                     GObject *weak_object)
+add_members_cb (TpChannel *proxy,
+               const GError *error,
+               gpointer user_data,
+               GObject *weak_object)
 {
-       GSimpleAsyncResult *result = user_data;
+       EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
 
        if (error != NULL) {
-               g_simple_async_result_set_from_error (result, error);
+               DEBUG ("Failed to join chat (%s): %s",
+                       tp_channel_get_identifier ((TpChannel *) self), error->message);
        }
-       else if (!correct) {
-               /* The current D-Bus API is a bit weird so re-use the
-                * AuthenticationFailed error */
-               g_simple_async_result_set_error (result, TP_ERRORS,
-                                                TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
-       }
-
-       g_simple_async_result_complete (result);
-       g_object_unref (result);
 }
 
 void
-empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
-                                                    const gchar *password,
-                                                    GAsyncReadyCallback callback,
-                                                    gpointer user_data)
+empathy_tp_chat_join (EmpathyTpChat *self)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
-       GSimpleAsyncResult *result;
+       TpHandle self_handle;
+       GArray *members;
+
+       self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
+
+       members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
+       g_array_append_val (members, self_handle);
 
-       result = g_simple_async_result_new (G_OBJECT (self),
-                                           callback, user_data,
-                                           empathy_tp_chat_provide_password_finish);
+       tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
+               "", add_members_cb, NULL, NULL, G_OBJECT (self));
 
-       tp_cli_channel_interface_password_call_provide_password
-               (priv->channel, -1, password, provide_password_cb, result,
-                NULL, G_OBJECT (self));
+       g_array_free (members, TRUE);
 }
 
 gboolean
-empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
-                                                     GAsyncResult *result,
-                                                     GError **error)
+empathy_tp_chat_is_invited (EmpathyTpChat *self,
+                           TpHandle *inviter)
 {
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
-               error))
+       TpHandle self_handle;
+
+       if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
                return FALSE;
 
-       g_return_val_if_fail (g_simple_async_result_is_valid (result,
-                                                             G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
+       self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
+       if (self_handle == 0)
+               return FALSE;
 
-       return TRUE;
+       return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
+               inviter, NULL, NULL);
 }
 
-/**
- * empathy_tp_chat_can_add_contact:
- *
- * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
- * That is if this chat is a 1-to-1 channel that can be upgraded to
- * a MUC using the Conference interface or if the channel is a MUC.
- */
-gboolean
-empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
+TpChannelChatState
+empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
+                           EmpathyContact *contact)
 {
-       EmpathyTpChatPriv *priv;
+       return tp_channel_get_chat_state ((TpChannel *) self,
+               empathy_contact_get_handle (contact));
+}
 
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
+EmpathyContact *
+empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
+{
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
 
-       priv = GET_PRIV (self);
+       return self->priv->user;
+}
 
-       return priv->can_upgrade_to_muc ||
-               tp_proxy_has_interface_by_id (priv->channel,
-                       TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
+GQuark
+empathy_tp_chat_get_feature_ready (void)
+{
+       return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
 }
 
 static void
-leave_remove_members_cb (TpChannel *proxy,
-                        const GError *error,
-                        gpointer user_data,
-                        GObject *weak_object)
+tp_chat_prepare_ready_async (TpProxy *proxy,
+       const TpProxyFeature *feature,
+       GAsyncReadyCallback callback,
+       gpointer user_data)
 {
-       EmpathyTpChat *self = user_data;
+       EmpathyTpChat *self = (EmpathyTpChat *) proxy;
+       TpChannel *channel = (TpChannel *) proxy;
+       TpConnection *connection;
 
-       if (error == NULL)
-               return;
+       g_assert (self->priv->ready_result == NULL);
+       self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
+               callback, user_data, tp_chat_prepare_ready_async);
 
-       DEBUG ("RemoveMembers failed (%s); closing the channel", error->message);
-       empathy_tp_chat_close (self);
-}
+       connection = tp_channel_borrow_connection (channel);
 
-void
-empathy_tp_chat_leave (EmpathyTpChat *self)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (self);
-       TpHandle self_handle;
-       GArray *array;
+       if (tp_proxy_has_interface_by_id (self,
+                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
+               const TpIntSet *members;
+               GArray *handles;
+               TpHandle handle;
 
-       if (!tp_proxy_has_interface_by_id (priv->channel,
-               TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
-               empathy_tp_chat_close (self);
-               return;
-       }
+               /* Get self contact from the group's self handle */
+               handle = tp_channel_group_get_self_handle (channel);
+               empathy_tp_contact_factory_get_from_handle (connection,
+                       handle, tp_chat_got_self_contact_cb,
+                       NULL, NULL, G_OBJECT (self));
 
-       self_handle = tp_channel_group_get_self_handle (priv->channel);
-       if (self_handle == 0) {
-               /* we are not member of the channel */
-               empathy_tp_chat_close (self);
-               return;
-       }
+               /* Get initial member contacts */
+               members = tp_channel_group_get_members (channel);
+               handles = tp_intset_to_array (members);
+               empathy_tp_contact_factory_get_from_handles (connection,
+                       handles->len, (TpHandle *) handles->data,
+                       tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
 
-       array = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
-       g_array_insert_val (array, 0, self_handle);
+               self->priv->can_upgrade_to_muc = FALSE;
 
-       tp_cli_channel_interface_group_call_remove_members (priv->channel, -1, array,
-               "", leave_remove_members_cb, self, NULL, G_OBJECT (self));
+               tp_g_signal_connect_object (self, "group-members-changed",
+                       G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
+       } else {
+               TpCapabilities *caps;
+               GPtrArray *classes;
+               guint i;
+               TpHandle handle;
+
+               /* Get the self contact from the connection's self handle */
+               handle = tp_connection_get_self_handle (connection);
+               empathy_tp_contact_factory_get_from_handle (connection,
+                       handle, tp_chat_got_self_contact_cb,
+                       NULL, NULL, G_OBJECT (self));
 
-       g_array_free (array, TRUE);
+               /* Get the remote contact */
+               handle = tp_channel_get_handle (channel, NULL);
+               empathy_tp_contact_factory_get_from_handle (connection,
+                       handle, tp_chat_got_remote_contact_cb,
+                       NULL, NULL, G_OBJECT (self));
+
+               caps = tp_connection_get_capabilities (connection);
+               g_assert (caps != NULL);
+
+               classes = tp_capabilities_get_channel_classes (caps);
+
+               for (i = 0; i < classes->len; i++) {
+                       GValueArray *array = g_ptr_array_index (classes, i);
+                       const char **oprops = g_value_get_boxed (
+                               g_value_array_get_nth (array, 1));
+
+                       if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
+                               self->priv->can_upgrade_to_muc = TRUE;
+                               break;
+                       }
+               }
+       }
+
+       if (tp_proxy_has_interface_by_id (self,
+                                         TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
+               tp_cli_properties_interface_call_list_properties (channel, -1,
+                                                                 tp_chat_list_properties_cb,
+                                                                 NULL, NULL,
+                                                                 G_OBJECT (self));
+               tp_cli_properties_interface_connect_to_properties_changed (channel,
+                                                                          tp_chat_properties_changed_cb,
+                                                                          NULL, NULL,
+                                                                          G_OBJECT (self), NULL);
+               tp_cli_properties_interface_connect_to_property_flags_changed (channel,
+                                                                              tp_chat_property_flags_changed_cb,
+                                                                              NULL, NULL,
+                                                                              G_OBJECT (self), NULL);
+       }
 }