]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
Remove repeated casts to EmpathyTpChat
[empathy.git] / libempathy / empathy-tp-chat.c
index 4b53b0611f60b65ec94c13cbaeb019cb8979c965..49870b41bd8e30dfdc9ecd9c20dea91e37b67a5a 100644 (file)
@@ -28,7 +28,9 @@
 #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-time.h"
@@ -39,6 +41,7 @@
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
 typedef struct {
+       gboolean               dispose_has_run;
        EmpathyContactFactory *factory;
        EmpathyContactMonitor *contact_monitor;
        EmpathyContact        *user;
@@ -47,9 +50,11 @@ typedef struct {
        McAccount             *account;
        TpChannel             *channel;
        gchar                 *id;
-       gboolean               acknowledge;
        gboolean               listing_pending_messages;
-       GSList                *message_queue;
+       /* 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;
@@ -68,7 +73,6 @@ static void tp_chat_iface_init         (EmpathyContactListIface *iface);
 enum {
        PROP_0,
        PROP_CHANNEL,
-       PROP_ACKNOWLEDGE,
        PROP_REMOTE_CONTACT,
        PROP_READY,
 };
@@ -88,6 +92,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,
@@ -95,8 +101,14 @@ tp_chat_invalidated_cb (TpProxy       *proxy,
                        gchar         *message,
                        EmpathyTpChat *chat)
 {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       g_object_unref (priv->channel);
+       priv->channel = NULL;
+
        DEBUG ("Channel invalidated: %s", message);
        g_signal_emit (chat, signals[DESTROY], 0);
+
 }
 
 static void
@@ -121,6 +133,9 @@ tp_chat_member_added_cb (EmpathyTpGroup *group,
        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,
@@ -161,6 +176,9 @@ tp_chat_member_removed_cb (EmpathyTpGroup *group,
        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,
@@ -196,6 +214,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);
@@ -207,13 +230,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
@@ -222,13 +249,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 *
@@ -258,11 +289,16 @@ tp_chat_get_monitor (EmpathyContactList *list)
 
        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,
@@ -287,6 +323,7 @@ tp_chat_build_message (EmpathyTpChat *chat,
        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);
 
@@ -309,8 +346,7 @@ tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
         * 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);
 
@@ -320,10 +356,9 @@ tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
                }
 
                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;
        }
 
@@ -334,7 +369,7 @@ tp_chat_sender_ready_notify_cb (EmpathyContact *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",
@@ -352,25 +387,25 @@ tp_chat_emit_or_queue_message (EmpathyTpChat  *chat,
        EmpathyContact      *sender;
        EmpathyContactReady  ready;
 
-       if (priv->message_queue != NULL) {
+       if (g_queue_get_length (priv->messages_queue) > 0) {
                DEBUG ("Message queue not empty");
-               priv->message_queue = g_slist_append (priv->message_queue,
-                                                     g_object_ref (message));
+               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) &&
            (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;
        }
 
        DEBUG ("Sender not ready");
-       priv->message_queue = g_slist_append (priv->message_queue, 
-                                             g_object_ref (message));
+       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);
@@ -385,40 +420,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->channel == NULL)
+               return;
+
        if (priv->listing_pending_messages) {
                return;
        }
  
        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
@@ -427,19 +466,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;
 
+       if (priv->channel == NULL)
+               return;
+
        DEBUG ("Message sent: %s", message_body);
 
-       message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+       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);
 }
 
@@ -453,10 +498,15 @@ tp_chat_send_error_cb (TpChannel   *channel,
                       GObject     *chat)
 {
        EmpathyMessage *message;
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+
+       if (priv->channel == NULL)
+               return;
 
        DEBUG ("Message sent error: %s (%d)", message_body, error_code);
 
        message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
+                                        0,
                                         message_type,
                                         timestamp,
                                         0,
@@ -491,6 +541,9 @@ 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);
@@ -507,24 +560,23 @@ 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->listing_pending_messages = FALSE;
 
+       if (priv->channel == NULL)
+               return;
+
        if (error) {
                DEBUG ("Error listing pending messages: %s", error->message);
                return;
        }
 
-       if (priv->acknowledge) {
-               message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
-                                                messages_list->len);
-       }
-
        for (i = 0; i < messages_list->len; i++) {
                EmpathyMessage *message;
                GValueArray    *message_struct;
@@ -546,29 +598,32 @@ tp_chat_list_pending_messages_cb (TpChannel       *channel,
 
                DEBUG ("Message pending: %s", message_body);
 
-               if (message_ids) {
-                       g_array_append_val (message_ids, message_id);
+               if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
+                   !tp_strdiff (message_body, "")) {
+                       DEBUG ("Empty message with NonTextContent, ignoring and acking.");
+
+                       if (empty_non_text_content_ids == NULL) {
+                               empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
+                       }
+
+                       g_array_append_val (empty_non_text_content_ids, message_id);
+                       continue;
                }
 
-               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);
        }
 }
 
@@ -581,6 +636,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;
        }
@@ -616,6 +674,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;
        }
@@ -674,6 +735,9 @@ tp_chat_list_properties_cb (TpProxy         *proxy,
        GArray            *ids;
        guint              i;
 
+       if (priv->channel == NULL)
+               return;
+
        priv->had_properties_list = TRUE;
 
        if (error) {
@@ -768,6 +832,9 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
        TpConnection      *connection;
        guint              handle, handle_type;
 
+       if (priv->channel == NULL)
+               return;
+
        DEBUG ("Channel ready");
 
        g_object_get (priv->channel,
@@ -863,6 +930,70 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
        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
 tp_chat_finalize (GObject *object)
 {
@@ -871,21 +1002,6 @@ tp_chat_finalize (GObject *object)
 
        DEBUG ("Finalize: %p", object);
 
-       if (priv->acknowledge && priv->channel) {
-               DEBUG ("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->properties) {
                for (i = 0; i < priv->properties->len; i++) {
                        TpChatProperty *property;
@@ -900,31 +1016,10 @@ 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->contact_monitor);
-       g_object_unref (priv->factory);
-       g_object_unref (priv->user);
-       g_object_unref (priv->account);
        g_free (priv->id);
-
-       if (priv->message_queue) {
-               EmpathyMessage *message;
-               EmpathyContact *contact;
-
-               message = priv->message_queue->data;
-               contact = empathy_message_get_sender (message);
-               g_signal_handlers_disconnect_by_func (contact,
-                                                     tp_chat_sender_ready_notify_cb,
-                                                     object);
-       }
-       g_slist_foreach (priv->message_queue, (GFunc) g_object_unref, NULL);
-       g_slist_free (priv->message_queue);
+       g_queue_free (priv->messages_queue);
+       g_queue_free (priv->pending_messages_queue);
 
        G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
@@ -942,7 +1037,7 @@ tp_chat_constructor (GType                  type,
 
        priv = GET_PRIV (chat);
        priv->account = empathy_channel_get_account (priv->channel);
-       priv->factory = empathy_contact_factory_new ();
+       priv->factory = empathy_contact_factory_dup_singleton ();
        priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
 
        g_signal_connect (priv->channel, "invalidated",
@@ -973,9 +1068,6 @@ tp_chat_get_property (GObject    *object,
        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;
@@ -998,10 +1090,7 @@ tp_chat_set_property (GObject      *object,
 
        switch (param_id) {
        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);
@@ -1014,6 +1103,7 @@ 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;
@@ -1027,13 +1117,6 @@ 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_object_class_install_property (object_class,
                                         PROP_REMOTE_CONTACT,
@@ -1111,7 +1194,9 @@ empathy_tp_chat_init (EmpathyTpChat *chat)
                EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
 
        chat->priv = priv;
-       priv->contact_monitor = empathy_contact_monitor_new_for_proxy (EMPATHY_CONTACT_LIST (chat));
+       priv->contact_monitor = NULL;
+       priv->messages_queue = g_queue_new ();
+       priv->pending_messages_queue = g_queue_new ();
 }
 
 static void
@@ -1126,11 +1211,29 @@ tp_chat_iface_init (EmpathyContactListIface *iface)
 EmpathyTpChat *
 empathy_tp_chat_new (TpChannel *channel)
 {
-       return g_object_new (EMPATHY_TYPE_TP_CHAT, 
+       return g_object_new (EMPATHY_TYPE_TP_CHAT,
                             "channel", channel,
                             NULL);
 }
 
+void
+empathy_tp_chat_close (EmpathyTpChat *chat) {
+       EmpathyTpChatPriv *priv = GET_PRIV (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);
+
+       tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
+               "closing channel", NULL, NULL);
+
+       g_object_unref (priv->channel);
+       priv->channel = NULL;
+
+       g_signal_emit (chat, signals[DESTROY], 0);
+}
+
 const gchar *
 empathy_tp_chat_get_id (EmpathyTpChat *chat)
 {
@@ -1192,38 +1295,6 @@ empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
        return priv->members_count;
 }
 
-void
-empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
-                                 gboolean      acknowledge)
-{
-       EmpathyTpChatPriv *priv = GET_PRIV (chat);
-
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-
-       priv->acknowledge = acknowledge;
-       g_object_notify (G_OBJECT (chat), "acknowledge");
-}
-
-void
-empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
-{
-       EmpathyTpChatPriv  *priv = GET_PRIV (chat);
-
-       g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
-       g_return_if_fail (priv->ready);
-
-       if (priv->listing_pending_messages) {
-               return;
-       }
-
-       priv->listing_pending_messages = TRUE;
-       tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
-                                                            FALSE,
-                                                            tp_chat_list_pending_messages_cb,
-                                                            NULL, NULL,
-                                                            G_OBJECT (chat));
-}
-
 void
 empathy_tp_chat_send (EmpathyTpChat *chat,
                      EmpathyMessage *message)
@@ -1267,3 +1338,84 @@ empathy_tp_chat_set_state (EmpathyTpChat      *chat,
                                                                 G_OBJECT (chat));
 }
 
+
+const GList *
+empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *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;
+
+       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;
+
+       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);
+}