]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
Unref TpChat's channel after emitting destroy.
[empathy.git] / libempathy / empathy-tp-chat.c
index c3f456faf51991682b1c88a0a46172b432ccdbc7..ef0ac6c2d8d83b779c2fb56fb498722319c621bc 100644 (file)
 #include <telepathy-glib/util.h>
 
 #include "empathy-tp-chat.h"
+#include "empathy-tp-group.h"
 #include "empathy-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 {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
+typedef struct {
+       gboolean               dispose_has_run;
        EmpathyContactFactory *factory;
+       EmpathyContactMonitor *contact_monitor;
        EmpathyContact        *user;
        EmpathyContact        *remote_contact;
        EmpathyTpGroup        *group;
        McAccount             *account;
        TpChannel             *channel;
-       gchar                 *id;
-       MissionControl        *mc;
-       gboolean               acknowledge;
-       TpChan                *tp_chan;
-       gboolean               had_pending_messages;
-       GSList                *message_queue;
+       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;
+       guint                  members_count;
+} EmpathyTpChatPriv;
 
 typedef struct {
        gchar          *name;
@@ -64,17 +67,13 @@ typedef struct {
        GValue         *value;
 } TpChatProperty;
 
-static void empathy_tp_chat_class_init (EmpathyTpChatClass      *klass);
-static void empathy_tp_chat_init       (EmpathyTpChat           *chat);
 static void tp_chat_iface_init         (EmpathyContactListIface *iface);
 
 enum {
        PROP_0,
-       PROP_ACCOUNT,
-       PROP_TP_CHAN,
        PROP_CHANNEL,
-       PROP_ACKNOWLEDGE,
        PROP_REMOTE_CONTACT,
+       PROP_READY,
 };
 
 enum {
@@ -92,6 +91,8 @@ 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 (TpProxy       *proxy,
                        guint          domain,
@@ -101,14 +102,11 @@ tp_chat_invalidated_cb (TpProxy       *proxy,
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
+       DEBUG ("Channel invalidated: %s", message);
+       g_signal_emit (chat, signals[DESTROY], 0);
 
        g_object_unref (priv->channel);
-       g_object_unref (priv->tp_chan);
        priv->channel = NULL;
-       priv->tp_chan = NULL;
-
-       g_signal_emit (chat, signals[DESTROY], 0);
 }
 
 static void
@@ -118,8 +116,7 @@ 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);
        }
 }
 
@@ -131,9 +128,39 @@ tp_chat_member_added_cb (EmpathyTpGroup *group,
                         const gchar    *message,
                         EmpathyTpChat  *chat)
 {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       guint              handle_type = 0;
+
+       if (priv->channel == NULL)
+               return;
+
+       priv->members_count++;
        g_signal_emit_by_name (chat, "members-changed",
                               contact, actor, reason, message,
                               TRUE);
+
+       g_object_get (priv->channel, "handle-type", &handle_type, NULL);
+       if (handle_type == TP_HANDLE_TYPE_ROOM) {
+               return;
+       }
+
+       if (priv->members_count > 2 && priv->remote_contact) {
+               /* We now have more than 2 members, this is not a p2p chat
+                * anymore. Remove the remote-contact as it makes no sense, the
+                * EmpathyContactList interface must be used now. */
+               g_object_unref (priv->remote_contact);
+               priv->remote_contact = NULL;
+               g_object_notify (G_OBJECT (chat), "remote-contact");
+       }
+       if (priv->members_count <= 2 && !priv->remote_contact &&
+           !empathy_contact_is_user (contact)) {
+               /* This is a p2p chat, if it's not ourself that means this is
+                * the remote contact with who we are chatting. This is to
+                * avoid forcing the usage of the EmpathyContactList interface
+                * for p2p chats. */
+               priv->remote_contact = g_object_ref (contact);
+               g_object_notify (G_OBJECT (chat), "remote-contact");
+       }
 }
 
 static void
@@ -144,10 +171,39 @@ tp_chat_member_removed_cb (EmpathyTpGroup *group,
                           const gchar    *message,
                           EmpathyTpChat  *chat)
 {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       guint              handle_type = 0;
+
+       if (priv->channel == NULL)
+               return;
+
+       priv->members_count--;
        g_signal_emit_by_name (chat, "members-changed",
                               contact, actor, reason, message,
                               FALSE);
+
+       g_object_get (priv->channel, "handle-type", &handle_type, NULL);
+       if (handle_type == TP_HANDLE_TYPE_ROOM) {
+               return;
+       }
+
+       if (priv->members_count <= 2 && !priv->remote_contact) {
+               GList *members, *l;
+
+               /* We are not a MUC anymore, get the remote contact back */
+               members = empathy_tp_group_get_members (group);
+               for (l = members; l; l = l->next) {
+                       if (!empathy_contact_is_user (l->data)) {
+                               priv->remote_contact = g_object_ref (l->data);
+                               g_object_notify (G_OBJECT (chat), "remote-contact");
+                               break;
+                       }
+               }
+               g_list_foreach (members, (GFunc) g_object_unref, NULL);
+               g_list_free (members);
+       }
 }
+
 static void
 tp_chat_local_pending_cb  (EmpathyTpGroup *group,
                           EmpathyContact *contact,
@@ -156,6 +212,11 @@ tp_chat_local_pending_cb  (EmpathyTpGroup *group,
                           const gchar    *message,
                           EmpathyTpChat  *chat)
 {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       if (priv->channel == NULL)
+               return;
+
        g_signal_emit_by_name (chat, "pendings-changed",
                               contact, actor, reason, message,
                               TRUE);
@@ -167,13 +228,17 @@ tp_chat_add (EmpathyContactList *list,
             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));
 
-       if (priv->group) {
-               empathy_tp_group_add_member (priv->group, contact, message);
-       }
+       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
@@ -182,13 +247,17 @@ tp_chat_remove (EmpathyContactList *list,
                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));
 
-       if (priv->group) {
-               empathy_tp_group_remove_member (priv->group, contact, message);
-       }
+       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 *
@@ -209,8 +278,25 @@ tp_chat_get_members (EmpathyContactList *list)
        return members;
 }
 
+static EmpathyContactMonitor *
+tp_chat_get_monitor (EmpathyContactList *list)
+{
+       EmpathyTpChatPriv *priv;
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
+
+       priv = GET_PRIV (list);
+
+       if (priv->contact_monitor == NULL) {
+               priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
+       }
+
+       return priv->contact_monitor;
+}
+
 static EmpathyMessage *
 tp_chat_build_message (EmpathyTpChat *chat,
+                      guint          id,
                       guint          type,
                       guint          timestamp,
                       guint          from_handle,
@@ -231,10 +317,11 @@ tp_chat_build_message (EmpathyTpChat *chat,
        }
 
        message = empathy_message_new (message_body);
-       empathy_message_set_type (message, type);
+       empathy_message_set_tptype (message, type);
        empathy_message_set_sender (message, sender);
        empathy_message_set_receiver (message, priv->user);
        empathy_message_set_timestamp (message, timestamp);
+       empathy_message_set_id (message, id);
 
        g_object_unref (sender);
 
@@ -249,36 +336,40 @@ tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
        EmpathyTpChatPriv   *priv = GET_PRIV (chat);
        EmpathyMessage      *message;
        EmpathyContactReady  ready;
-       EmpathyContact      *sender;
+       EmpathyContact      *sender = NULL;
        gboolean             removed = FALSE;
 
        /* 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
+        * ready sender (in case of a MUC we could have more than one 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;
+       while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
                sender = empathy_message_get_sender (message);
                ready = empathy_contact_get_ready (sender);
 
-               if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
+               if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
+                   (ready & EMPATHY_CONTACT_READY_ID) == 0) {
                        break;
                }
 
-               empathy_debug (DEBUG_DOMAIN, "Queued message ready");
+               DEBUG ("Queued message ready");
+               message = 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) {
+               /* We removed at least one message from the queue, disconnect
+                * the ready signal from the previous contact */
                g_signal_handlers_disconnect_by_func (contact,
                                                      tp_chat_sender_ready_notify_cb,
                                                      chat);
 
-               if (priv->message_queue) {
+               if (g_queue_get_length (priv->messages_queue) > 0) {
+                       /* We still have queued message, connect the ready
+                        * signal on the new first message sender. */
                        g_signal_connect (sender, "notify::ready",
                                          G_CALLBACK (tp_chat_sender_ready_notify_cb),
                                          chat);
@@ -294,24 +385,25 @@ tp_chat_emit_or_queue_message (EmpathyTpChat  *chat,
        EmpathyContact      *sender;
        EmpathyContactReady  ready;
 
-       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));
+       if (g_queue_get_length (priv->messages_queue) > 0) {
+               DEBUG ("Message queue not empty");
+               g_queue_push_tail (priv->messages_queue, g_object_ref (message));
                return;
        }
 
+
        sender = empathy_message_get_sender (message);
        ready = empathy_contact_get_ready (sender);
-       if (ready & EMPATHY_CONTACT_READY_NAME) {
-               empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
+       if ((ready & EMPATHY_CONTACT_READY_NAME) &&
+           (ready & EMPATHY_CONTACT_READY_ID)) {
+               DEBUG ("Message queue empty and sender ready");
+               g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
                g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
                return;
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Sender not ready");
-       priv->message_queue = g_slist_append (priv->message_queue, 
-                                             g_object_ref (message));
+       DEBUG ("Sender not ready");
+       g_queue_push_tail (priv->messages_queue, g_object_ref (message));
        g_signal_connect (sender, "notify::ready",
                          G_CALLBACK (tp_chat_sender_ready_notify_cb),
                          chat);
@@ -326,40 +418,44 @@ 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),
+       if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
+           !tp_strdiff (message_body, "")) {
+               GArray *ids;
+
+               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);
+
+               return;
+       }
+
+       message = tp_chat_build_message (chat,
+                                        message_id,
                                         message_type,
                                         timestamp,
                                         from_handle,
                                         message_body);
 
-       tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
+       tp_chat_emit_or_queue_message (chat, message);
        g_object_unref (message);
-
-       if (priv->acknowledge) {
-               GArray *message_ids;
-
-               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 received message",
-                                                                           NULL,
-                                                                           chat);
-               g_array_free (message_ids, TRUE);
-       }
 }
 
 static void
@@ -368,19 +464,25 @@ tp_chat_sent_cb (TpChannel   *channel,
                 guint        message_type,
                 const gchar *message_body,
                 gpointer     user_data,
-                GObject     *chat)
+                GObject     *chat_)
 {
+       EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyMessage *message;
 
-       empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
+       if (priv->channel == NULL)
+               return;
 
-       message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+       DEBUG ("Message sent: %s", message_body);
+
+       message = tp_chat_build_message (chat,
+                                        0,
                                         message_type,
                                         timestamp,
                                         0,
                                         message_body);
 
-       tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
+       tp_chat_emit_or_queue_message (chat, message);
        g_object_unref (message);
 }
 
@@ -394,11 +496,15 @@ tp_chat_send_error_cb (TpChannel   *channel,
                       GObject     *chat)
 {
        EmpathyMessage *message;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       if (priv->channel == NULL)
+               return;
 
-       empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
-                      message_body, error_code);
+       DEBUG ("Message sent error: %s (%d)", message_body, error_code);
 
        message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+                                        0,
                                         message_type,
                                         timestamp,
                                         0,
@@ -408,6 +514,21 @@ tp_chat_send_error_cb (TpChannel   *channel,
        g_object_unref (message);
 }
 
+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);
+       }
+}
+
 static void
 tp_chat_state_changed_cb (TpChannel *channel,
                          guint      handle,
@@ -418,13 +539,15 @@ tp_chat_state_changed_cb (TpChannel *channel,
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyContact    *contact;
 
+       if (priv->channel == NULL)
+               return;
+
        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);
+       DEBUG ("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);
@@ -435,23 +558,21 @@ 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            *message_ids = NULL;
+       GArray            *empty_non_text_content_ids = NULL;
 
-       priv->had_pending_messages = TRUE;
+       priv->listing_pending_messages = FALSE;
 
-       if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
-                              error->message);
+       if (priv->channel == NULL)
                return;
-       }
 
-       if (priv->acknowledge) {
-               message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
-                                                messages_list->len);
+       if (error) {
+               DEBUG ("Error listing pending messages: %s", error->message);
+               return;
        }
 
        for (i = 0; i < messages_list->len; i++) {
@@ -473,31 +594,34 @@ 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);
+
+               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));
+                       }
 
-               if (message_ids) {
-                       g_array_append_val (message_ids, message_id);
+                       g_array_append_val (empty_non_text_content_ids, message_id);
+                       continue;
                }
 
-               message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+               message = tp_chat_build_message (chat,
+                                                message_id,
                                                 message_type,
                                                 timestamp,
                                                 from_handle,
                                                 message_body);
 
-               tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
+               tp_chat_emit_or_queue_message (chat, message);
                g_object_unref (message);
        }
 
-       if (message_ids) {
-               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);
+       if (empty_non_text_content_ids != NULL) {
+               acknowledge_messages (chat, empty_non_text_content_ids);
+               g_array_free (empty_non_text_content_ids, TRUE);
        }
 }
 
@@ -510,6 +634,9 @@ tp_chat_property_flags_changed_cb (TpProxy         *proxy,
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        guint              i, j;
 
+       if (priv->channel == NULL)
+               return;
+
        if (!priv->had_properties_list || !properties) {
                return;
        }
@@ -528,9 +655,8 @@ tp_chat_property_flags_changed_cb (TpProxy         *proxy,
                        property = g_ptr_array_index (priv->properties, j);
                        if (property->id == id) {
                                property->flags = flags;
-                               empathy_debug (DEBUG_DOMAIN,
-                                              "property %s flags changed: %d",
-                                              property->name, property->flags);
+                               DEBUG ("property %s flags changed: %d",
+                                       property->name, property->flags);
                                break;
                        }
                }
@@ -546,6 +672,9 @@ tp_chat_properties_changed_cb (TpProxy         *proxy,
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        guint              i, j;
 
+       if (priv->channel == NULL)
+               return;
+
        if (!priv->had_properties_list || !properties) {
                return;
        }
@@ -569,8 +698,7 @@ tp_chat_properties_changed_cb (TpProxy         *proxy,
                                        property->value = tp_g_value_slice_dup (src_value);
                                }
 
-                               empathy_debug (DEBUG_DOMAIN, "property %s changed",
-                                              property->name);
+                               DEBUG ("property %s changed", property->name);
                                g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
                                               property->name, property->value);
                                break;
@@ -587,8 +715,7 @@ tp_chat_get_properties_cb (TpProxy         *proxy,
                           GObject         *chat)
 {
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
-                              error->message);
+               DEBUG ("Error getting properties: %s", error->message);
                return;
        }
 
@@ -606,11 +733,13 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
        GArray            *ids;
        guint              i;
 
+       if (priv->channel == NULL)
+               return;
+
        priv->had_properties_list = TRUE;
 
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
-                              error->message);
+               DEBUG ("Error listing properties: %s", error->message);
                return;
        }
 
@@ -626,8 +755,8 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
                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));
 
-               empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
-                              property->name, property->id, property->flags);
+               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);
@@ -652,6 +781,8 @@ empathy_tp_chat_set_property (EmpathyTpChat *chat,
        TpChatProperty    *property;
        guint              i;
 
+       g_return_if_fail (priv->ready);
+
        for (i = 0; i < priv->properties->len; i++) {
                property = g_ptr_array_index (priv->properties, i);
                if (!tp_strdiff (property->name, name)) {
@@ -676,7 +807,7 @@ empathy_tp_chat_set_property (EmpathyTpChat *chat,
                        properties = g_ptr_array_sized_new (1);
                        g_ptr_array_add (properties, prop);
 
-                       empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
+                       DEBUG ("Set property %s", name);
                        tp_cli_properties_interface_call_set_properties (priv->channel, -1,
                                                                         properties,
                                                                         (tp_cli_properties_interface_callback_for_set_properties)
@@ -696,12 +827,30 @@ static void
 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       TpConnection      *connection;
+       guint              handle, handle_type;
+
+       if (priv->channel == NULL)
+               return;
 
-       empathy_debug (DEBUG_DOMAIN, "Channel ready");
+       DEBUG ("Channel ready");
+
+       g_object_get (priv->channel,
+                     "connection", &connection,
+                     "handle", &handle,
+                     "handle_type", &handle_type,
+                     NULL);
+
+       if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
+               priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
+                                                                               priv->account,
+                                                                               handle);
+               g_object_notify (G_OBJECT (chat), "remote-contact");
+       }
 
        if (tp_proxy_has_interface_by_id (priv->channel,
                                          TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
-               priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
+               priv->group = empathy_tp_group_new (priv->channel);
 
                g_signal_connect (priv->group, "member-added",
                                  G_CALLBACK (tp_chat_member_added_cb),
@@ -712,11 +861,9 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
                g_signal_connect (priv->group, "local-pending",
                                  G_CALLBACK (tp_chat_local_pending_cb),
                                  chat);
+               empathy_run_until_ready (priv->group);
        } else {
-               priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
-                                                                               priv->account,
-                                                                               priv->tp_chan->handle);
-               g_object_notify (G_OBJECT (chat), "remote-contact");
+               priv->members_count = 2;
        }
        
        if (tp_proxy_has_interface_by_id (priv->channel,
@@ -735,8 +882,9 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
                                                                               G_OBJECT (chat), NULL);
        }
 
+       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));
@@ -761,6 +909,73 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
                                                                           tp_chat_state_changed_cb,
                                                                           NULL, NULL,
                                                                           G_OBJECT (chat), NULL);
+
+       priv->ready = TRUE;
+       g_object_notify (G_OBJECT (chat), "ready");
+}
+
+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->group != NULL)
+               g_object_unref (priv->group);
+       priv->group = 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->account != NULL);
+               g_object_unref (priv->account);
+       priv->account = NULL;
+
+       if (priv->contact_monitor)
+               g_object_unref (priv->contact_monitor);
+       priv->contact_monitor = NULL;
+
+       if (!g_queue_is_empty (priv->messages_queue)) {
+               EmpathyMessage *message;
+               EmpathyContact *contact;
+
+               message = g_queue_peek_head (priv->messages_queue);
+               contact = empathy_message_get_sender (message);
+               g_signal_handlers_disconnect_by_func (contact,
+               tp_chat_sender_ready_notify_cb, object);
+       }
+
+       g_list_foreach (priv->messages_queue->head,
+               (GFunc) g_object_unref, NULL);
+
+       g_list_foreach (priv->pending_messages_queue->head,
+               (GFunc) g_object_unref, NULL);
+
+       if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
+               G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
 }
 
 static void
@@ -769,23 +984,7 @@ tp_chat_finalize (GObject *object)
        EmpathyTpChatPriv *priv = GET_PRIV (object);
        guint              i;
 
-       if (priv->acknowledge && priv->channel) {
-               empathy_debug (DEBUG_DOMAIN, "Closing channel...");
-               tp_cli_channel_call_close (priv->channel, -1,
-                                          tp_chat_async_cb,
-                                          "closing channel", NULL,
-                                          NULL);
-       }
-
-       if (priv->channel) {
-               g_signal_handlers_disconnect_by_func (priv->channel,
-                                                     tp_chat_invalidated_cb,
-                                                     object);
-               g_object_unref (priv->channel);
-       }
-       if (priv->tp_chan) {
-               g_object_unref (priv->tp_chan);
-       }
+       DEBUG ("Finalize: %p", object);
 
        if (priv->properties) {
                for (i = 0; i < priv->properties->len; i++) {
@@ -801,18 +1000,8 @@ tp_chat_finalize (GObject *object)
                g_ptr_array_free (priv->properties, TRUE);
        }
 
-       if (priv->remote_contact) {
-               g_object_unref (priv->remote_contact);
-       }
-       if (priv->group) {
-               g_object_unref (priv->group);
-       }
-
-       g_object_unref (priv->factory);
-       g_object_unref (priv->user);
-       g_object_unref (priv->account);
-       g_object_unref (priv->mc);
-       g_free (priv->id);
+       g_queue_free (priv->messages_queue);
+       g_queue_free (priv->pending_messages_queue);
 
        G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
@@ -829,9 +1018,9 @@ tp_chat_constructor (GType                  type,
        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->account = empathy_channel_get_account (priv->channel);
+       priv->factory = empathy_contact_factory_dup_singleton ();
        priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
-       priv->mc = empathy_mission_control_new ();
 
        g_signal_connect (priv->channel, "invalidated",
                          G_CALLBACK (tp_chat_invalidated_cb),
@@ -858,21 +1047,15 @@ 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_TP_CHAN:
-               g_value_set_object (value, priv->tp_chan);
-               break;
        case PROP_CHANNEL:
                g_value_set_object (value, priv->channel);
                break;
-       case PROP_ACKNOWLEDGE:
-               g_value_set_boolean (value, priv->acknowledge);
-               break;
        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);
                break;
@@ -888,17 +1071,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_TP_CHAN:
-               priv->tp_chan = 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:
-               priv->acknowledge = g_value_get_boolean (value);
+               priv->channel = g_value_dup_object (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -911,28 +1085,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_TP_CHAN,
-                                        g_param_spec_object ("tp-chan",
-                                                             "telepathy channel",
-                                                             "The text channel for the chat",
-                                                             TELEPATHY_CHAN_TYPE,
-                                                             G_PARAM_READWRITE |
-                                                             G_PARAM_CONSTRUCT_ONLY));
        g_object_class_install_property (object_class,
                                         PROP_CHANNEL,
                                         g_param_spec_object ("channel",
@@ -941,14 +1099,7 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                                                              TP_TYPE_CHANNEL,
                                                              G_PARAM_READWRITE |
                                                              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",
-                                                              FALSE,
-                                                              G_PARAM_READWRITE |
-                                                              G_PARAM_CONSTRUCT));
+
        g_object_class_install_property (object_class,
                                         PROP_REMOTE_CONTACT,
                                         g_param_spec_object ("remote-contact",
@@ -956,6 +1107,13 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                                                              "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_READABLE));
 
        /* Signals */
        signals[MESSAGE_RECEIVED] =
@@ -1014,6 +1172,13 @@ 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);
+
+       chat->priv = priv;
+       priv->contact_monitor = NULL;
+       priv->messages_queue = g_queue_new ();
+       priv->pending_messages_queue = g_queue_new ();
 }
 
 static void
@@ -1022,128 +1187,118 @@ 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 (McAccount *account,
-                    TpChan    *tp_chan,
-                    gboolean   acknowledge)
+empathy_tp_chat_new (TpChannel *channel)
 {
-       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,
+       return g_object_new (EMPATHY_TYPE_TP_CHAT,
                             "channel", channel,
-                            "tp-chan", tp_chan,
-                            "acknowledge", acknowledge,
                             NULL);
+}
 
-       g_object_unref (channel);
-       g_object_unref (tp_conn);
-       g_object_unref (connection);
-       g_object_unref (mc);
+void
+empathy_tp_chat_close (EmpathyTpChat *chat) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       return chat;
-}
+       /* 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);
 
-EmpathyTpChat *
-empathy_tp_chat_new_with_contact (EmpathyContact *contact)
-{
-       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;
-       }
+       tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
+               "closing channel", NULL, 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);
+       g_object_unref (priv->channel);
+       priv->channel = NULL;
 
-       text_chan = tp_conn_new_channel (tp_get_bus (),
-                                        tp_conn,
-                                        bus_name,
-                                        TP_IFACE_CHANNEL_TYPE_TEXT,
-                                        TP_HANDLE_TYPE_CONTACT,
-                                        handle,
-                                        TRUE);
+       g_signal_emit (chat, signals[DESTROY], 0);
+}
 
-       chat = empathy_tp_chat_new (account, text_chan, TRUE);
+const gchar *
+empathy_tp_chat_get_id (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       g_object_unref (tp_conn);
-       g_object_unref (text_chan);
-       g_object_unref (mc);
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+       g_return_val_if_fail (priv->ready, NULL);
 
-       return chat;
+       return tp_channel_get_identifier (priv->channel);
 }
 
-TpChan *
-empathy_tp_chat_get_channel (EmpathyTpChat *chat)
+EmpathyContact *
+empathy_tp_chat_get_remote_contact (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->tp_chan;
+       return priv->remote_contact;
 }
 
 McAccount *
 empathy_tp_chat_get_account (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv *priv;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
+
+       return priv->account;
+}
+
+TpChannel *
+empathy_tp_chat_get_channel (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
 
-       priv = GET_PRIV (chat);
+       return 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;
+}
+
+guint
+empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
+
+       return priv->members_count;
 }
 
 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));
 }
 
@@ -1151,13 +1306,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,
@@ -1166,29 +1320,84 @@ empathy_tp_chat_set_state (EmpathyTpChat      *chat,
                                                                 G_OBJECT (chat));
 }
 
-const gchar *
-empathy_tp_chat_get_id (EmpathyTpChat *chat)
-{
-       EmpathyTpChatPriv *priv;
 
-       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+const GList *
+empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       priv = GET_PRIV (chat);
+       return priv->pending_messages_queue->head;
+}
 
-       if (!priv->id) {
-               priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
-       }
+static void
+acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
 
-       return priv->id;
+       tp_cli_channel_type_text_call_acknowledge_pending_messages (
+               priv->channel, -1, ids, tp_chat_async_cb,
+               "acknowledging received message", NULL, G_OBJECT (chat));
 }
 
-EmpathyContact *
-empathy_tp_chat_get_remote_contact (EmpathyTpChat *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_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+       if (empathy_message_get_sender (message) == priv->user)
+               goto out;
 
-       return priv->remote_contact;
+       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;
+
+       length = g_list_length ((GList *)messages);
+
+       if (length == 0)
+               return;
+
+       message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
+
+       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);
+               }
+       }
+
+       if (message_ids->len > 0)
+               acknowledge_messages (chat, message_ids);
+
+       g_array_free (message_ids, TRUE);
+       g_list_free (msgs);
+}