]> 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 77b56bfc0951d07d154742e8b954f921fd6ec6e9..49870b41bd8e30dfdc9ecd9c20dea91e37b67a5a 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"
@@ -40,7 +41,7 @@
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
 typedef struct {
-       gboolean dispose_has_run;
+       gboolean               dispose_has_run;
        EmpathyContactFactory *factory;
        EmpathyContactMonitor *contact_monitor;
        EmpathyContact        *user;
@@ -58,7 +59,6 @@ typedef struct {
        GPtrArray             *properties;
        gboolean               ready;
        guint                  members_count;
-       guint                  invalidated_handler;
 } EmpathyTpChatPriv;
 
 typedef struct {
@@ -92,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,
@@ -100,11 +102,13 @@ tp_chat_invalidated_cb (TpProxy       *proxy,
                        EmpathyTpChat *chat)
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
-       DEBUG ("Channel invalidated: %s", message);
-       g_signal_emit (chat, signals[DESTROY], 0);
 
        g_object_unref (priv->channel);
        priv->channel = NULL;
+
+       DEBUG ("Channel invalidated: %s", message);
+       g_signal_emit (chat, signals[DESTROY], 0);
+
 }
 
 static void
@@ -226,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
@@ -241,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 *
@@ -408,8 +420,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;
 
@@ -422,14 +435,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);
 }
 
@@ -439,8 +466,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;
 
@@ -449,14 +477,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);
 }
 
@@ -532,10 +560,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;
 
@@ -568,16 +598,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
@@ -886,43 +933,65 @@ tp_chat_channel_ready_cb (EmpathyTpChat *chat)
 static void
 tp_chat_dispose (GObject *object)
 {
-  EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
-  EmpathyTpChatPriv *priv = GET_PRIV (self);
+       EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
+       EmpathyTpChatPriv *priv = GET_PRIV (self);
 
-  if (priv->dispose_has_run)
-    return;
+       if (priv->dispose_has_run)
+               return;
+
+       priv->dispose_has_run = TRUE;
 
-  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->channel != NULL)
-    empathy_tp_chat_close (self);
+       if (priv->factory != NULL)
+               g_object_unref (priv->factory);
+       priv->factory = NULL;
 
-  if (priv->remote_contact != NULL)
-    g_object_unref (priv->remote_contact);
-  priv->remote_contact = NULL;
+       if (priv->user != NULL);
+               g_object_unref (priv->user);
+       priv->user = NULL;
 
-  if (priv->group != NULL)
-    g_object_unref (priv->group);
-  priv->group = NULL;
+       if (priv->account != NULL);
+               g_object_unref (priv->account);
+       priv->account = NULL;
 
-  g_object_unref (priv->factory);
-  priv->factory = NULL;
+       if (priv->contact_monitor)
+               g_object_unref (priv->contact_monitor);
+       priv->contact_monitor = NULL;
 
-  g_object_unref (priv->user);
-  priv->user = NULL;
+       if (!g_queue_is_empty (priv->messages_queue)) {
+               EmpathyMessage *message;
+               EmpathyContact *contact;
 
-  g_object_unref (priv->account);
-  priv->account = NULL;
+               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_queue_free (priv->messages_queue);
+       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);
-  g_queue_free (priv->pending_messages_queue);
+       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);
+       if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
+               G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
 }
 
 static void
@@ -933,7 +1002,6 @@ tp_chat_finalize (GObject *object)
 
        DEBUG ("Finalize: %p", object);
 
-
        if (priv->properties) {
                for (i = 0; i < priv->properties->len; i++) {
                        TpChatProperty *property;
@@ -948,33 +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);
-       }
-
-       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);
-
-       if (g_queue_get_length (priv->messages_queue) > 0) {
-               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_queue_free (priv->messages_queue);
+       g_queue_free (priv->pending_messages_queue);
 
        G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
@@ -1045,7 +1090,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);
@@ -1172,20 +1217,21 @@ empathy_tp_chat_new (TpChannel *channel)
 }
 
 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);
-
-  g_signal_emit (chat, signals[DESTROY], 0);
-  priv->channel = NULL;
+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 *
@@ -1302,72 +1348,74 @@ empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
 }
 
 static void
-acknowledge_messages (EmpathyTpChat *chat, GArray *ids)
-{
-  EmpathyTpChatPriv *priv = GET_PRIV (chat);
+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));
+       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;
+                                    EmpathyMessage *message) {
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       GArray *message_ids;
+       GList *m;
+       guint id;
 
-  if (empathy_message_get_sender (message) == priv->user)
-    goto out;
+       if (empathy_message_get_sender (message) == priv->user)
+               goto out;
 
-  message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+       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);
+       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);
+       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;
-
-  GArray *message_ids;
-  message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
-
-  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);
+                                     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);
 }