]> 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 6f55c6ba5b98b07b111597877c5df0c257ffefd1..ef0ac6c2d8d83b779c2fb56fb498722319c621bc 100644 (file)
@@ -28,6 +28,7 @@
 #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"
@@ -48,7 +49,6 @@ typedef struct {
        EmpathyTpGroup        *group;
        McAccount             *account;
        TpChannel             *channel;
-       gchar                 *id;
        gboolean               listing_pending_messages;
        /* Queue of messages not signalled yet */
        GQueue                *messages_queue;
@@ -91,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,
@@ -100,12 +102,11 @@ tp_chat_invalidated_cb (TpProxy       *proxy,
 {
        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);
 
+       g_object_unref (priv->channel);
+       priv->channel = NULL;
 }
 
 static void
@@ -227,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
@@ -242,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 *
@@ -409,8 +418,9 @@ 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;
 
@@ -423,14 +433,28 @@ tp_chat_received_cb (TpChannel   *channel,
  
        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);
 }
 
@@ -440,8 +464,9 @@ 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;
 
@@ -450,14 +475,14 @@ tp_chat_sent_cb (TpChannel   *channel,
 
        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);
 }
 
@@ -533,10 +558,12 @@ 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->listing_pending_messages = FALSE;
 
@@ -569,16 +596,33 @@ tp_chat_list_pending_messages_cb (TpChannel       *channel,
 
                DEBUG ("Message pending: %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, "")) {
+                       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 (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 (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
@@ -797,20 +841,6 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
                      "handle_type", &handle_type,
                      NULL);
 
-       if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
-               GArray *handles;
-               gchar **names;
-
-               handles = g_array_new (FALSE, FALSE, sizeof (guint));
-               g_array_append_val (handles, handle);
-               tp_cli_connection_run_inspect_handles (connection, -1,
-                                                      handle_type, handles,
-                                                      &names, NULL, NULL);
-               priv->id = *names;
-               g_array_free (handles, TRUE);
-               g_free (names);
-       }
-
        if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
                priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
                                                                                priv->account,
@@ -896,7 +926,12 @@ tp_chat_dispose (GObject *object)
        priv->dispose_has_run = TRUE;
 
        if (priv->channel != NULL)
-               empathy_tp_chat_close (self);
+               {
+                       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);
@@ -919,6 +954,10 @@ tp_chat_dispose (GObject *object)
                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;
@@ -961,21 +1000,6 @@ 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);
-       }
-
-       if (priv->contact_monitor) {
-               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);
        g_queue_free (priv->messages_queue);
        g_queue_free (priv->pending_messages_queue);
 
@@ -1048,7 +1072,7 @@ tp_chat_set_property (GObject      *object,
 
        switch (param_id) {
        case PROP_CHANNEL:
-               priv->channel = g_object_ref (g_value_get_object (value));
+               priv->channel = g_value_dup_object (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -1182,6 +1206,7 @@ empathy_tp_chat_close (EmpathyTpChat *chat) {
           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);
 
@@ -1199,7 +1224,7 @@ empathy_tp_chat_get_id (EmpathyTpChat *chat)
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
        g_return_val_if_fail (priv->ready, NULL);
 
-       return priv->id;
+       return tp_channel_get_identifier (priv->channel);
 }
 
 EmpathyContact *