]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
Add empathy_tp_chat_get_connection() to direct access the channel's connection.
[empathy.git] / libempathy / empathy-tp-chat.c
index 50187ed4df8cbfe8329f505f3224268eb26ffede..b9b731384f5fa7be91dd3e6d8fec7e18feac003d 100644 (file)
 
 #include <telepathy-glib/channel.h>
 #include <telepathy-glib/dbus.h>
+#include <telepathy-glib/util.h>
 
 #include "empathy-tp-chat.h"
-#include "empathy-contact-factory.h"
+#include "empathy-tp-contact-factory.h"
+#include "empathy-contact-monitor.h"
+#include "empathy-contact-list.h"
 #include "empathy-marshal.h"
-#include "empathy-debug.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
-                      EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
-
-#define DEBUG_DOMAIN "TpChat"
+#define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
+#include "empathy-debug.h"
 
-struct _EmpathyTpChatPriv {
-       EmpathyContactFactory *factory;
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
+typedef struct {
+       gboolean               dispose_has_run;
+       EmpathyTpContactFactory *factory;
+       EmpathyContactMonitor *contact_monitor;
        EmpathyContact        *user;
-       McAccount             *account;
+       EmpathyContact        *remote_contact;
+       GList                 *members;
        TpChannel             *channel;
-       gchar                 *id;
-       MissionControl        *mc;
-       gboolean               acknowledge;
-       TpChan                *tp_chan;
-       gboolean               had_pending_messages;
-       GSList                *message_queue;
-};
-
-static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
-static void empathy_tp_chat_init       (EmpathyTpChat      *chat);
+       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;
+       gboolean               ready;
+} EmpathyTpChatPriv;
+
+typedef struct {
+       gchar          *name;
+       guint           id;
+       TpPropertyFlags flags;
+       GValue         *value;
+} TpChatProperty;
+
+static void tp_chat_iface_init         (EmpathyContactListIface *iface);
 
 enum {
        PROP_0,
-       PROP_ACCOUNT,
        PROP_CHANNEL,
-       PROP_ACKNOWLEDGE,
+       PROP_REMOTE_CONTACT,
+       PROP_READY,
 };
 
 enum {
        MESSAGE_RECEIVED,
        SEND_ERROR,
        CHAT_STATE_CHANGED,
+       PROPERTY_CHANGED,
        DESTROY,
        LAST_SIGNAL
 };
 
 static guint signals[LAST_SIGNAL];
 
-G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
+G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
+                        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 (EmpathyTpChat *chat)
+tp_chat_invalidated_cb (TpProxy       *proxy,
+                       guint          domain,
+                       gint           code,
+                       gchar         *message,
+                       EmpathyTpChat *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Channel invalidated");
+       DEBUG ("Channel invalidated: %s", message);
+       g_signal_emit (chat, signals[DESTROY], 0);
 
        g_object_unref (priv->channel);
        priv->channel = NULL;
-
-       g_signal_emit (chat, signals[DESTROY], 0);
 }
 
 static void
@@ -93,121 +113,144 @@ tp_chat_async_cb (TpChannel *proxy,
                  GObject *weak_object)
 {
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
-                              user_data, error->message);
+               DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
        }
 }
 
-static EmpathyMessage *
-tp_chat_build_message (EmpathyTpChat *chat,
-                      guint          type,
-                      guint          timestamp,
-                      guint          from_handle,
-                      const gchar   *message_body)
+static void
+tp_chat_add (EmpathyContactList *list,
+            EmpathyContact     *contact,
+            const gchar        *message)
 {
-       EmpathyTpChatPriv *priv;
-       EmpathyMessage    *message;
-       EmpathyContact    *sender;
+       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       TpHandle           handle;
+       GArray             handles = {(gchar *) &handle, 1};
 
-       priv = GET_PRIV (chat);
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
+       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
-       if (from_handle == 0) {
-               sender = g_object_ref (priv->user);
+       handle = empathy_contact_get_handle (contact);
+       tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
+                                                        &handles, NULL,
+                                                        NULL, NULL, NULL,
+                                                        NULL);
+}
+
+static void
+tp_chat_remove (EmpathyContactList *list,
+               EmpathyContact     *contact,
+               const gchar        *message)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       TpHandle           handle;
+       GArray             handles = {(gchar *) &handle, 1};
+
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (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,
+                                                           &handles, NULL,
+                                                           NULL, NULL, NULL,
+                                                           NULL);
+}
+
+static GList *
+tp_chat_get_members (EmpathyContactList *list)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (list);
+       GList             *members = NULL;
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
+
+       if (priv->members) {
+               members = g_list_copy (priv->members);
+               g_list_foreach (members, (GFunc) g_object_ref, NULL);
        } else {
-               sender = empathy_contact_factory_get_from_handle (priv->factory,
-                                                                 priv->account,
-                                                                 from_handle);
+               members = g_list_prepend (members, g_object_ref (priv->user));
+               members = g_list_prepend (members, g_object_ref (priv->remote_contact));
        }
 
-       message = empathy_message_new (message_body);
-       empathy_message_set_type (message, type);
-       empathy_message_set_sender (message, sender);
-       empathy_message_set_receiver (message, priv->user);
-       empathy_message_set_timestamp (message, timestamp);
+       return members;
+}
+
+static EmpathyContactMonitor *
+tp_chat_get_monitor (EmpathyContactList *list)
+{
+       EmpathyTpChatPriv *priv;
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
 
-       g_object_unref (sender);
+       priv = GET_PRIV (list);
 
-       return message;
+       if (priv->contact_monitor == NULL) {
+               priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
+       }
+
+       return priv->contact_monitor;
 }
 
 static void
-tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
-                               GParamSpec     *param_spec,
-                               EmpathyTpChat  *chat)
+tp_chat_emit_queued_messages (EmpathyTpChat *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyMessage    *message;
-       EmpathyContact    *sender;
-       gboolean           removed = FALSE;
-       const gchar       *name, *id;
-
-       /* Emit all messages queued until we find a message with not
-        * ready sender. When leaving this loop, sender is the first not ready
-        * contact queued and removed tells if at least one message got removed
-        * from the queue. */
-       while (priv->message_queue) {
-               message = priv->message_queue->data;
-               sender = empathy_message_get_sender (message);
-               name = empathy_contact_get_name (sender);
-               id = empathy_contact_get_id (sender);
-
-               if (!tp_strdiff (name, id)) {
+
+       /* Check if we can now emit some queued messages */
+       while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
+               if (empathy_message_get_sender (message) == NULL) {
                        break;
                }
 
-               empathy_debug (DEBUG_DOMAIN, "Queued message ready");
+               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);
-               priv->message_queue = g_slist_remove (priv->message_queue,
-                                                     message);
-               g_object_unref (message);
-               removed = TRUE;
        }
+}
 
-       if (removed) {
-               g_signal_handlers_disconnect_by_func (contact,
-                                                     tp_chat_sender_ready_notify_cb,
-                                                     chat);
-
-               if (priv->message_queue) {
-                       g_signal_connect (sender, "notify::name",
-                                         G_CALLBACK (tp_chat_sender_ready_notify_cb),
-                                         chat);
-               }
-       }
+static void
+tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
+                      GList                   *contacts,
+                      gpointer                 message,
+                      GObject                 *chat)
+{
+       empathy_message_set_sender (message, contacts->data);
+       tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
 }
 
 static void
-tp_chat_emit_or_queue_message (EmpathyTpChat  *chat,
-                              EmpathyMessage *message)
+tp_chat_build_message (EmpathyTpChat *chat,
+                      guint          id,
+                      guint          type,
+                      guint          timestamp,
+                      guint          from_handle,
+                      const gchar   *message_body)
 {
-       EmpathyTpChatPriv   *priv = GET_PRIV (chat);
-       EmpathyContact      *sender;
-       const gchar         *name, *id;
+       EmpathyTpChatPriv *priv;
+       EmpathyMessage    *message;
 
-       if (priv->message_queue != NULL) {
-               empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
-               priv->message_queue = g_slist_append (priv->message_queue,
-                                                     g_object_ref (message));
-               return;
-       }
+       priv = GET_PRIV (chat);
 
-       sender = empathy_message_get_sender (message);
-       name = empathy_contact_get_name (sender);
-       id = empathy_contact_get_id (sender);
-       if (tp_strdiff (name, id)) {
-               empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
-               g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
-               return;
-       }
+       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);
+       g_queue_push_tail (priv->messages_queue, message);
 
-       empathy_debug (DEBUG_DOMAIN, "Sender not ready");
-       priv->message_queue = g_slist_append (priv->message_queue, 
-                                             g_object_ref (message));
-       g_signal_connect (sender, "notify::name",
-                         G_CALLBACK (tp_chat_sender_ready_notify_cb),
-                         chat);
+       if (from_handle == 0) {
+               empathy_message_set_sender (message, priv->user);
+               tp_chat_emit_queued_messages (chat);
+       } else {
+               empathy_tp_contact_factory_get_from_handles (priv->factory,
+                       1, &from_handle,
+                       tp_chat_got_sender_cb,
+                       message, NULL, G_OBJECT (chat));
+       }
 }
 
+static void
 tp_chat_received_cb (TpChannel   *channel,
                     guint        message_id,
                     guint        timestamp,
@@ -216,40 +259,40 @@ tp_chat_received_cb (TpChannel   *channel,
                     guint        message_flags,
                     const gchar *message_body,
                     gpointer     user_data,
-                    GObject     *chat)
+                    GObject     *chat_)
 {
+       EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       EmpathyMessage    *message;
 
-       if (!priv->had_pending_messages) {
+       if (priv->channel == NULL)
+               return;
+
+       if (priv->listing_pending_messages) {
                return;
        }
  
-       empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
+       DEBUG ("Message received: %s", message_body);
 
-       message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
-                                        message_type,
-                                        timestamp,
-                                        from_handle,
-                                        message_body);
+       if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
+           !tp_strdiff (message_body, "")) {
+               GArray *ids;
 
-       tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
-       g_object_unref (message);
+               DEBUG ("Empty message with NonTextContent, ignoring and acking.");
 
-       if (priv->acknowledge) {
-               GArray *message_ids;
+               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);
 
-               message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
-               g_array_append_val (message_ids, message_id);
-               tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
-                                                                           -1,
-                                                                           message_ids,
-                                                                           tp_chat_async_cb,
-                                                                           "acknowledging pending messages",
-                                                                           NULL,
-                                                                           chat);
-               g_array_free (message_ids, TRUE);
+               return;
        }
+
+       tp_chat_build_message (chat,
+                              message_id,
+                              message_type,
+                              timestamp,
+                              from_handle,
+                              message_body);
 }
 
 static void
@@ -258,20 +301,22 @@ tp_chat_sent_cb (TpChannel   *channel,
                 guint        message_type,
                 const gchar *message_body,
                 gpointer     user_data,
-                GObject     *chat)
+                GObject     *chat_)
 {
-       EmpathyMessage *message;
+       EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
+       if (priv->channel == NULL)
+               return;
 
-       message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
-                                        message_type,
-                                        timestamp,
-                                        0,
-                                        message_body);
+       DEBUG ("Message sent: %s", message_body);
 
-       tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
-       g_object_unref (message);
+       tp_chat_build_message (chat,
+                              0,
+                              message_type,
+                              timestamp,
+                              0,
+                              message_body);
 }
 
 static void
@@ -283,41 +328,70 @@ tp_chat_send_error_cb (TpChannel   *channel,
                       gpointer     user_data,
                       GObject     *chat)
 {
-       EmpathyMessage *message;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
-                      message_body, error_code);
+       if (priv->channel == NULL)
+               return;
 
-       message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
-                                        message_type,
-                                        timestamp,
-                                        0,
-                                        message_body);
+       DEBUG ("Message sent error: %s (%d)", message_body, error_code);
 
-       g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
-       g_object_unref (message);
+       tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+                              0,
+                              message_type,
+                              timestamp,
+                              0,
+                              message_body);
+}
+
+static void
+tp_chat_send_cb (TpChannel    *proxy,
+                const GError *error,
+                gpointer      user_data,
+                GObject      *chat)
+{
+       EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
+
+       if (error) {
+               DEBUG ("Error: %s", error->message);
+               g_signal_emit (chat, signals[SEND_ERROR], 0, message,
+                              TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
+       }
+}
+
+typedef struct {
+       EmpathyTpChat *chat;
+       TpChannelChatState state;
+} StateChangedData;
+
+static void
+tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
+                                     GList                   *contacts,
+                                     gpointer                 user_data,
+                                     GObject                 *chat)
+{
+       EmpathyContact *contact = contacts->data;
+       TpChannelChatState state;
+
+       state = GPOINTER_TO_UINT (user_data);
+       DEBUG ("Chat state changed for %s (%d): %d",
+               empathy_contact_get_name (contact),
+               empathy_contact_get_handle (contact), state);
+
+       g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
 }
 
 static void
 tp_chat_state_changed_cb (TpChannel *channel,
-                         guint      handle,
-                         guint      state,
+                         TpHandle   handle,
+                         TpChannelChatState state,
                          gpointer   user_data,
                          GObject   *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       EmpathyContact    *contact;
 
-       contact = empathy_contact_factory_get_from_handle (priv->factory,
-                                                          priv->account,
-                                                          handle);
-
-       empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
-                     empathy_contact_get_name (contact),
-                     handle, state);
-
-       g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
-       g_object_unref (contact);
+       empathy_tp_contact_factory_get_from_handles (priv->factory, 1, &handle,
+               tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
+               NULL, chat);
 }
 
 static void
@@ -325,15 +399,24 @@ tp_chat_list_pending_messages_cb (TpChannel       *channel,
                                  const GPtrArray *messages_list,
                                  const GError    *error,
                                  gpointer         user_data,
-                                 GObject         *chat)
+                                 GObject         *chat_)
 {
+       EmpathyTpChat     *chat = EMPATHY_TP_CHAT (chat_);
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        guint              i;
+       GArray            *empty_non_text_content_ids = NULL;
 
-       priv->had_pending_messages = TRUE;
+       priv->listing_pending_messages = FALSE;
+
+       if (priv->channel == NULL)
+               return;
+
+       if (error) {
+               DEBUG ("Error listing pending messages: %s", error->message);
+               return;
+       }
 
        for (i = 0; i < messages_list->len; i++) {
-               EmpathyMessage *message;
                GValueArray    *message_struct;
                const gchar    *message_body;
                guint           message_id;
@@ -351,28 +434,318 @@ tp_chat_list_pending_messages_cb (TpChannel       *channel,
                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));
 
-               empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
+               DEBUG ("Message pending: %s", message_body);
 
-               message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
-                                                message_type,
-                                                timestamp,
-                                                from_handle,
-                                                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.");
 
-               g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
-               g_object_unref (message);
+                       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,
+                                      message_id,
+                                      message_type,
+                                      timestamp,
+                                      from_handle,
+                                      message_body);
+       }
+
+       if (empty_non_text_content_ids != NULL) {
+               acknowledge_messages (chat, empty_non_text_content_ids);
+               g_array_free (empty_non_text_content_ids, TRUE);
+       }
+}
+
+static void
+tp_chat_property_flags_changed_cb (TpProxy         *proxy,
+                                  const GPtrArray *properties,
+                                  gpointer         user_data,
+                                  GObject         *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       guint              i, j;
+
+       if (priv->channel == NULL)
+               return;
+
+       if (!priv->had_properties_list || !properties) {
+               return;
+       }
+
+       for (i = 0; i < properties->len; i++) {
+               GValueArray    *prop_struct;
+               TpChatProperty *property;
+               guint           id;
+               guint           flags;
+
+               prop_struct = g_ptr_array_index (properties, i);
+               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);
+                       if (property->id == id) {
+                               property->flags = flags;
+                               DEBUG ("property %s flags changed: %d",
+                                       property->name, property->flags);
+                               break;
+                       }
+               }
+       }
+}
+
+static void
+tp_chat_properties_changed_cb (TpProxy         *proxy,
+                              const GPtrArray *properties,
+                              gpointer         user_data,
+                              GObject         *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       guint              i, j;
+
+       if (priv->channel == NULL)
+               return;
+
+       if (!priv->had_properties_list || !properties) {
+               return;
+       }
+
+       for (i = 0; i < properties->len; i++) {
+               GValueArray    *prop_struct;
+               TpChatProperty *property;
+               guint           id;
+               GValue         *src_value;
+
+               prop_struct = g_ptr_array_index (properties, i);
+               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);
+                       if (property->id == id) {
+                               if (property->value) {
+                                       g_value_copy (src_value, property->value);
+                               } else {
+                                       property->value = tp_g_value_slice_dup (src_value);
+                               }
+
+                               DEBUG ("property %s changed", property->name);
+                               g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
+                                              property->name, property->value);
+                               break;
+                       }
+               }
+       }
+}
+
+static void
+tp_chat_get_properties_cb (TpProxy         *proxy,
+                          const GPtrArray *properties,
+                          const GError    *error,
+                          gpointer         user_data,
+                          GObject         *chat)
+{
+       if (error) {
+               DEBUG ("Error getting properties: %s", error->message);
+               return;
+       }
+
+       tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
+}
+
+static void
+tp_chat_list_properties_cb (TpProxy         *proxy,
+                           const GPtrArray *properties,
+                           const GError    *error,
+                           gpointer         user_data,
+                           GObject         *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (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;
+       }
+
+       ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
+       priv->properties = g_ptr_array_sized_new (properties->len);
+       for (i = 0; i < properties->len; i++) {
+               GValueArray    *prop_struct;
+               TpChatProperty *property;
+
+               prop_struct = g_ptr_array_index (properties, i);
+               property = g_slice_new0 (TpChatProperty);
+               property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
+               property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
+               property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
+
+               DEBUG ("Adding property name=%s id=%d flags=%d",
+                       property->name, property->id, property->flags);
+               g_ptr_array_add (priv->properties, property);
+               if (property->flags & TP_PROPERTY_FLAG_READ) {
+                       g_array_append_val (ids, property->id);
+               }
+       }
+
+       tp_cli_properties_interface_call_get_properties (proxy, -1,
+                                                        ids,
+                                                        tp_chat_get_properties_cb,
+                                                        NULL, NULL,
+                                                        chat);
+
+       g_array_free (ids, TRUE);
+}
+
+void
+empathy_tp_chat_set_property (EmpathyTpChat *chat,
+                             const gchar   *name,
+                             const GValue  *value)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpChatProperty    *property;
+       guint              i;
+
+       for (i = 0; i < priv->properties->len; i++) {
+               property = g_ptr_array_index (priv->properties, i);
+               if (!tp_strdiff (property->name, name)) {
+                       GPtrArray   *properties;
+                       GValueArray *prop;
+                       GValue       id = {0, };
+                       GValue       dest_value = {0, };
+
+                       if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
+                               break;
+                       }
+
+                       g_value_init (&id, G_TYPE_UINT);
+                       g_value_init (&dest_value, G_TYPE_VALUE);
+                       g_value_set_uint (&id, property->id);
+                       g_value_set_boxed (&dest_value, value);
+
+                       prop = g_value_array_new (2);
+                       g_value_array_append (prop, &id);
+                       g_value_array_append (prop, &dest_value);
+
+                       properties = g_ptr_array_sized_new (1);
+                       g_ptr_array_add (properties, prop);
+
+                       DEBUG ("Set property %s", name);
+                       tp_cli_properties_interface_call_set_properties (priv->channel, -1,
+                                                                        properties,
+                                                                        (tp_cli_properties_interface_callback_for_set_properties)
+                                                                        tp_chat_async_cb,
+                                                                        "Seting property", NULL,
+                                                                        G_OBJECT (chat));
+
+                       g_ptr_array_free (properties, TRUE);
+                       g_value_array_free (prop);
+
+                       break;
+               }
+       }
+}
+
+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;
+
+       if (priv->remote_contact != NULL)
+               g_object_unref (priv->remote_contact);
+       priv->remote_contact = NULL;
+
+       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,
+               (GFunc) g_object_unref, NULL);
+       g_queue_clear (priv->pending_messages_queue);
+
+       if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
+               G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
+}
+
+static void
+tp_chat_finalize (GObject *object)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (object);
+       guint              i;
+
+       DEBUG ("Finalize: %p", object);
+
+       if (priv->properties) {
+               for (i = 0; i < priv->properties->len; i++) {
+                       TpChatProperty *property;
+
+                       property = g_ptr_array_index (priv->properties, i);
+                       g_free (property->name);
+                       if (property->value) {
+                               tp_g_value_slice_free (property->value);
+                       }
+                       g_slice_free (TpChatProperty, property);
+               }
+               g_ptr_array_free (priv->properties, TRUE);
        }
+
+       g_queue_free (priv->messages_queue);
+       g_queue_free (priv->pending_messages_queue);
+
+       G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
 
-static gboolean
-tp_chat_channel_ready_cb (EmpathyTpChat *chat)
+static void
+tp_chat_check_if_ready (EmpathyTpChat *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Channel ready");
+       if (priv->ready || priv->user == NULL ||
+           (priv->members == NULL && priv->remote_contact == NULL)) {
+               return;
+       }
+
+       DEBUG ("Ready!");
 
+       priv->listing_pending_messages = TRUE;
        tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
-                                                            priv->acknowledge,
+                                                            FALSE,
                                                             tp_chat_list_pending_messages_cb,
                                                             NULL, NULL,
                                                             G_OBJECT (chat));
@@ -393,31 +766,187 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
                                                                           tp_chat_state_changed_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");
+}
 
-       return FALSE;
+static void
+tp_chat_update_remote_contact (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       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);
+       if (handle_type == TP_HANDLE_TYPE_ROOM) {
+               return;
+       }
+
+       /* This is an MSN-like chat where anyone can join the chat at anytime.
+        * If there is only one non-self contact member, we are in a private
+        * 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) {
+               /* Skip self contact if member */
+               if (empathy_contact_get_handle (l->data) == self_handle) {
+                       continue;
+               }
+
+               /* We have more than one remote contact, break */
+               if (contact != NULL) {
+                       contact = NULL;
+                       break;
+               }
+
+               /* If we didn't find yet a remote contact, keep this one */
+               contact = l->data;
+       }
+
+       if (priv->remote_contact == contact) {
+               return;
+       }
+
+       DEBUG ("Changing remote contact from %p to %p",
+               priv->remote_contact, contact);
+
+       if (priv->remote_contact) {
+               g_object_unref (priv->remote_contact);
+       }
+
+       priv->remote_contact = contact ? g_object_ref (contact) : NULL;
+       g_object_notify (G_OBJECT (chat), "remote-contact");
 }
 
 static void
-tp_chat_finalize (GObject *object)
+tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
+                              GList                   *contacts,
+                              gpointer                 user_data,
+                              GObject                 *chat)
 {
-       EmpathyTpChatPriv *priv = GET_PRIV (object);
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       GList *l;
+       const TpIntSet *members;
+       TpHandle handle;
+       EmpathyContact *contact;
+
+       members = tp_channel_group_get_members (priv->channel);
+       for (l = contacts; l; l = l->next) {
+               contact = l->data;
+               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,
+                               g_object_ref (contact));
+                       g_signal_emit_by_name (chat, "members-changed",
+                                              contact, NULL, 0, NULL, FALSE);
+               }
+       }
 
-       if (priv->acknowledge) {
-               empathy_debug (DEBUG_DOMAIN, "Closing channel...");
-               tp_cli_channel_call_close (priv->channel, -1,
-                                          tp_chat_async_cb,
-                                          "closing channel", NULL,
-                                          NULL);
+       tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
+       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+}
+
+static void
+tp_chat_group_members_changed_cb (TpChannel     *self,
+                                 gchar         *message,
+                                 GArray        *added,
+                                 GArray        *removed,
+                                 GArray        *local_pending,
+                                 GArray        *remote_pending,
+                                 guint          actor,
+                                 guint          reason,
+                                 EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       EmpathyContact *contact;
+       TpHandle handle;
+       guint i;
+       GList *l;
+
+       /* Remove contacts that are not members anymore */
+       for (i = 0; i < removed->len; i++) {
+               for (l = priv->members; l; l = l->next) {
+                       contact = l->data;
+                       handle = empathy_contact_get_handle (contact);
+                       if (handle == g_array_index (removed, TpHandle, i)) {
+                               priv->members = g_list_delete_link (priv->members, l);
+                               g_signal_emit_by_name (chat, "members-changed",
+                                                      contact, actor, reason,
+                                                      message, FALSE);
+                               g_object_unref (contact);
+                               break;
+                       }
+               }
        }
 
-       g_object_unref (priv->channel);
-       g_object_unref (priv->factory);
-       g_object_unref (priv->user);
-       g_object_unref (priv->account);
-       g_object_unref (priv->mc);
-       g_free (priv->id);
+       /* Request added contacts */
+       if (added->len > 0) {
+               empathy_tp_contact_factory_get_from_handles (priv->factory,
+                       added->len, (TpHandle*) added->data,
+                       tp_chat_got_added_contacts_cb, NULL, NULL,
+                       G_OBJECT (chat));
+       }
 
-       G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
+       tp_chat_update_remote_contact (chat);
+}
+
+static void
+tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
+                              GList *contacts,
+                              gpointer user_data,
+                              GObject *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       priv->remote_contact = g_object_ref (contacts->data);
+       g_object_notify (chat, "remote-contact");
+
+       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+}
+
+static void
+tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
+                            GList                   *contacts,
+                            gpointer                 user_data,
+                            GObject                 *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       priv->user = g_object_ref (contacts->data);
+       empathy_contact_set_is_user (priv->user, TRUE);
+       tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
+}
+
+static void
+tp_chat_get_self_handle_cb (TpConnection *connection,
+                           TpHandle self_handle,
+                           const GError *error,
+                           gpointer user_data,
+                           GObject *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       if (error) {
+               DEBUG ("Error: %s", error->message);
+               tp_cli_channel_call_close (priv->channel, -1,
+                                          NULL, NULL, NULL, NULL);
+               return;
+       }
+
+       empathy_tp_contact_factory_get_from_handles (priv->factory,
+               1, &self_handle,
+               tp_chat_got_self_contact_cb,
+               NULL, NULL, chat);
 }
 
 static GObject *
@@ -427,33 +956,67 @@ tp_chat_constructor (GType                  type,
 {
        GObject           *chat;
        EmpathyTpChatPriv *priv;
-       gboolean           channel_ready;
+       TpConnection      *connection;
+       TpHandle           handle;
 
        chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
 
        priv = GET_PRIV (chat);
-       priv->factory = empathy_contact_factory_new ();
-       priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
-       priv->mc = empathy_mission_control_new ();
 
+       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);
 
-       g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
-       if (channel_ready) {
-               /* FIXME: We do that in a cb to let time to set the acknowledge
-                * property, this property should be required for construct. */
-               g_idle_add ((GSourceFunc) tp_chat_channel_ready_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_handles (priv->factory,
+                       1, &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);
+
+               g_signal_connect_swapped (priv->channel, "group-members-changed",
+                       G_CALLBACK (tp_chat_group_members_changed_cb), chat);
        } else {
-               g_signal_connect_swapped (priv->channel, "notify::channel-ready",
-                                         G_CALLBACK (tp_chat_channel_ready_cb),
-                                         chat);
+               /* Get the self contact from the connection's self handle */
+               tp_cli_connection_call_get_self_handle (connection, -1,
+                       tp_chat_get_self_handle_cb, NULL, NULL, chat);
+
+               /* Get the remote contact */
+               handle = tp_channel_get_handle (priv->channel, NULL);
+               empathy_tp_contact_factory_get_from_handles (priv->factory,
+                       1, &handle, tp_chat_got_remote_contact_cb,
+                       NULL, NULL, chat);
        }
 
-       /* FIXME: We do that in a cb to let time to set the acknowledge
-        * property, this property should be required for construct. */
-       g_idle_add ((GSourceFunc) empathy_tp_chat_get_pendings, chat);
+       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);
+       }
 
        return chat;
 }
@@ -467,14 +1030,14 @@ tp_chat_get_property (GObject    *object,
        EmpathyTpChatPriv *priv = GET_PRIV (object);
 
        switch (param_id) {
-       case PROP_ACCOUNT:
-               g_value_set_object (value, priv->account);
-               break;
        case PROP_CHANNEL:
                g_value_set_object (value, priv->channel);
                break;
-       case PROP_ACKNOWLEDGE:
-               g_value_set_boolean (value, priv->acknowledge);
+       case PROP_REMOTE_CONTACT:
+               g_value_set_object (value, priv->remote_contact);
+               break;
+       case PROP_READY:
+               g_value_set_boolean (value, priv->ready);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -491,15 +1054,8 @@ tp_chat_set_property (GObject      *object,
        EmpathyTpChatPriv *priv = GET_PRIV (object);
 
        switch (param_id) {
-       case PROP_ACCOUNT:
-               priv->account = g_object_ref (g_value_get_object (value));
-               break;
        case PROP_CHANNEL:
-               priv->channel = g_object_ref (g_value_get_object (value));
-               break;
-       case PROP_ACKNOWLEDGE:
-               empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
-                                                g_value_get_boolean (value));
+               priv->channel = g_value_dup_object (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -512,20 +1068,12 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_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;
 
-       /* Construct properties */
-       g_object_class_install_property (object_class,
-                                        PROP_ACCOUNT,
-                                        g_param_spec_object ("account",
-                                                             "channel Account",
-                                                             "The account associated with the channel",
-                                                             MC_TYPE_ACCOUNT,
-                                                             G_PARAM_READWRITE |
-                                                             G_PARAM_CONSTRUCT_ONLY));
        g_object_class_install_property (object_class,
                                         PROP_CHANNEL,
                                         g_param_spec_object ("channel",
@@ -536,13 +1084,20 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                                                              G_PARAM_CONSTRUCT_ONLY));
 
        g_object_class_install_property (object_class,
-                                        PROP_ACKNOWLEDGE,
-                                        g_param_spec_boolean ("acknowledge",
-                                                              "acknowledge messages",
-                                                              "Wheter or not received messages should be acknowledged",
+                                        PROP_REMOTE_CONTACT,
+                                        g_param_spec_object ("remote-contact",
+                                                             "The remote contact",
+                                                             "The remote contact if there is no group iface on the channel",
+                                                             EMPATHY_TYPE_CONTACT,
+                                                             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_READWRITE |
-                                                              G_PARAM_CONSTRUCT));
+                                                              G_PARAM_READABLE));
 
        /* Signals */
        signals[MESSAGE_RECEIVED] =
@@ -575,6 +1130,16 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                              G_TYPE_NONE,
                              2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
 
+       signals[PROPERTY_CHANGED] =
+               g_signal_new ("property-changed",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             _empathy_marshal_VOID__STRING_BOXED,
+                             G_TYPE_NONE,
+                             2, G_TYPE_STRING, G_TYPE_VALUE);
+
        signals[DESTROY] =
                g_signal_new ("destroy",
                              G_TYPE_FROM_CLASS (klass),
@@ -591,151 +1156,123 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
 static void
 empathy_tp_chat_init (EmpathyTpChat *chat)
 {
-}
+       EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
+               EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
 
-EmpathyTpChat *
-empathy_tp_chat_new (McAccount *account,
-                    TpChan    *tp_chan)
-{
-       EmpathyTpChat  *chat;
-       TpChannel      *channel;
-       TpConnection   *connection;
-       MissionControl *mc;
-       TpConn         *tp_conn;
-
-       mc = empathy_mission_control_new ();
-       tp_conn = mission_control_get_connection (mc, account, NULL);
-       connection = tp_conn_dup_connection (tp_conn);
-       channel = tp_chan_dup_channel (tp_chan, connection, NULL);
-
-       chat = g_object_new (EMPATHY_TYPE_TP_CHAT, 
-                            "account", account,
-                            "channel", channel,
-                            NULL);
-
-       g_object_unref (channel);
-       g_object_unref (tp_conn);
-       g_object_unref (connection);
-       g_object_unref (mc);
+       chat->priv = priv;
+       priv->contact_monitor = NULL;
+       priv->messages_queue = g_queue_new ();
+       priv->pending_messages_queue = g_queue_new ();
+}
 
-       return chat;
+static void
+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_with_contact (EmpathyContact *contact)
+empathy_tp_chat_new (TpChannel *channel)
 {
-       EmpathyTpChat  *chat;
-       MissionControl *mc;
-       McAccount      *account;
-       TpConn         *tp_conn;
-       TpChan         *text_chan;
-       const gchar    *bus_name;
-       guint           handle;
-
-       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
-
-       mc = empathy_mission_control_new ();
-       account = empathy_contact_get_account (contact);
-
-       if (mission_control_get_connection_status (mc, account, NULL) != 0) {
-               /* The account is not connected. */
-               return NULL;
-       }
+       return g_object_new (EMPATHY_TYPE_TP_CHAT,
+                            "channel", channel,
+                            NULL);
+}
 
-       tp_conn = mission_control_get_connection (mc, account, NULL);
-       g_return_val_if_fail (tp_conn != NULL, NULL);
-       bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
-       handle = empathy_contact_get_handle (contact);
+void
+empathy_tp_chat_close (EmpathyTpChat *chat) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       text_chan = tp_conn_new_channel (tp_get_bus (),
-                                        tp_conn,
-                                        bus_name,
-                                        TP_IFACE_CHANNEL_TYPE_TEXT,
-                                        TP_HANDLE_TYPE_CONTACT,
-                                        handle,
-                                        TRUE);
+       /* If there are still messages left, it'll come back..
+          We loose the ordering of sent messages though */
+       g_signal_handlers_disconnect_by_func (priv->channel,
+               tp_chat_invalidated_cb, chat);
 
-       chat = empathy_tp_chat_new (account, text_chan);
+       tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
+               "closing channel", NULL, NULL);
 
-       g_object_unref (tp_conn);
-       g_object_unref (text_chan);
-       g_object_unref (mc);
+       g_object_unref (priv->channel);
+       priv->channel = NULL;
 
-       return chat;
+       g_signal_emit (chat, signals[DESTROY], 0);
 }
 
-gboolean
-empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
+const gchar *
+empathy_tp_chat_get_id (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
-
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       priv = GET_PRIV (chat);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
 
-       return priv->acknowledge;
+       return tp_channel_get_identifier (priv->channel);
 }
 
-void
-empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
-                                gboolean       acknowledge)
+EmpathyContact *
+empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
-
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       priv = GET_PRIV (chat);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+       g_return_val_if_fail (priv->ready, NULL);
 
-       priv->acknowledge = acknowledge;
-       g_object_notify (G_OBJECT (chat), "acknowledge");
+       return priv->remote_contact;
 }
 
 TpChannel *
 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
 
-       priv = GET_PRIV (chat);
-
        return priv->channel;
 }
 
-McAccount *
-empathy_tp_chat_get_account (EmpathyTpChat *chat)
+TpConnection *
+empathy_tp_chat_get_connection (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
 
-       priv = GET_PRIV (chat);
+       return tp_channel_borrow_connection (priv->channel);
+}
 
-       return priv->account;
+gboolean
+empathy_tp_chat_is_ready (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
+
+       return priv->ready;
 }
 
 void
 empathy_tp_chat_send (EmpathyTpChat *chat,
                      EmpathyMessage *message)
 {
-       EmpathyTpChatPriv  *priv;
-       const gchar        *message_body;
-       EmpathyMessageType  message_type;
+       EmpathyTpChatPriv        *priv = GET_PRIV (chat);
+       const gchar              *message_body;
+       TpChannelTextMessageType  message_type;
 
        g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
        g_return_if_fail (EMPATHY_IS_MESSAGE (message));
-
-       priv = GET_PRIV (chat);
+       g_return_if_fail (priv->ready);
 
        message_body = empathy_message_get_body (message);
-       message_type = empathy_message_get_type (message);
+       message_type = empathy_message_get_tptype (message);
 
-       empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
+       DEBUG ("Sending message: %s", message_body);
        tp_cli_channel_type_text_call_send (priv->channel, -1,
                                            message_type,
                                            message_body,
-                                           tp_chat_async_cb,
-                                           "sending message", NULL,
+                                           tp_chat_send_cb,
+                                           g_object_ref (message),
+                                           (GDestroyNotify) g_object_unref,
                                            G_OBJECT (chat));
 }
 
@@ -743,13 +1280,12 @@ void
 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
                           TpChannelChatState  state)
 {
-       EmpathyTpChatPriv *priv;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
        g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+       g_return_if_fail (priv->ready);
 
-       priv = GET_PRIV (chat);
-
-       empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
+       DEBUG ("Set state: %d", state);
        tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
                                                                 state,
                                                                 tp_chat_async_cb,
@@ -758,23 +1294,95 @@ empathy_tp_chat_set_state (EmpathyTpChat      *chat,
                                                                 G_OBJECT (chat));
 }
 
-const gchar *
-empathy_tp_chat_get_id (EmpathyTpChat *chat)
+
+const GList *
+empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+       g_return_val_if_fail (priv->ready, NULL);
 
-       priv = GET_PRIV (chat);
+       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));
+}
+
+void
+empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
+                                    EmpathyMessage *message) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       GArray *message_ids;
+       GList *m;
+       guint id;
+
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+       g_return_if_fail (priv->ready);
+
+       if (empathy_message_get_sender (message) == priv->user)
+               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 GList *messages) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       /* Copy messages as the messges list (probably is) our own */
+       GList *msgs = g_list_copy ((GList *) messages);
+       GList *l;
+       guint length;
+       GArray *message_ids;
+
+       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
+       g_return_if_fail (priv->ready);
+
+       length = g_list_length ((GList *)messages);
+
+       if (length == 0)
+               return;
 
-       if (!priv->id) {
-               TpChan *tp_chan;
+       message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
 
-               tp_chan = tp_chan_new_from_channel (priv->channel);
-               priv->id = empathy_inspect_channel (priv->account, tp_chan);
-               g_object_unref (tp_chan);
+       for (l = msgs; l != NULL; l = g_list_next (l)) {
+               GList *m;
+
+               EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
+
+               m = g_queue_find (priv->pending_messages_queue, message);
+               g_assert (m != NULL);
+               g_queue_delete_link (priv->pending_messages_queue, m);
+
+               if (empathy_message_get_sender (message) != priv->user) {
+                       guint id = empathy_message_get_id (message);
+                       g_array_append_val (message_ids, id);
+               }
+               g_object_unref (message);
        }
 
-       return priv->id;
+       if (message_ids->len > 0)
+               acknowledge_messages (chat, message_ids);
+
+       g_array_free (message_ids, TRUE);
+       g_list_free (msgs);
 }