]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
Updated Basque language
[empathy.git] / libempathy / empathy-tp-chat.c
index ac61392e033268df517d89bfe62e96a1c3728145..a64f06a2fbc151dc557de86a396156398d5e26c8 100644 (file)
@@ -26,6 +26,7 @@
 #include <telepathy-glib/channel.h>
 #include <telepathy-glib/dbus.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/interfaces.h>
 
 #include "empathy-tp-chat.h"
 #include "empathy-tp-contact-factory.h"
@@ -226,6 +227,7 @@ tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
 
 static void
 tp_chat_build_message (EmpathyTpChat *chat,
+                      gboolean       incoming,
                       guint          id,
                       guint          type,
                       guint          timestamp,
@@ -242,6 +244,8 @@ tp_chat_build_message (EmpathyTpChat *chat,
        empathy_message_set_receiver (message, priv->user);
        empathy_message_set_timestamp (message, timestamp);
        empathy_message_set_id (message, id);
+       empathy_message_set_incoming (message, incoming);
+
        g_queue_push_tail (priv->messages_queue, message);
 
        if (from_handle == 0) {
@@ -293,6 +297,7 @@ tp_chat_received_cb (TpChannel   *channel,
        }
 
        tp_chat_build_message (chat,
+                              TRUE,
                               message_id,
                               message_type,
                               timestamp,
@@ -317,6 +322,7 @@ tp_chat_sent_cb (TpChannel   *channel,
        DEBUG ("Message sent: %s", message_body);
 
        tp_chat_build_message (chat,
+                              FALSE,
                               0,
                               message_type,
                               timestamp,
@@ -338,14 +344,9 @@ tp_chat_send_error_cb (TpChannel   *channel,
        if (priv->channel == NULL)
                return;
 
-       DEBUG ("Message sent error: %s (%d)", message_body, error_code);
+       DEBUG ("Error sending '%s' (%d)", message_body, error_code);
 
-       tp_chat_build_message (EMPATHY_TP_CHAT (chat),
-                              0,
-                              message_type,
-                              timestamp,
-                              0,
-                              message_body);
+       g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
 }
 
 static void
@@ -358,7 +359,8 @@ tp_chat_send_cb (TpChannel    *proxy,
 
        if (error) {
                DEBUG ("Error: %s", error->message);
-               g_signal_emit (chat, signals[SEND_ERROR], 0, message,
+               g_signal_emit (chat, signals[SEND_ERROR], 0,
+                              empathy_message_get_body (message),
                               TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
        }
 }
@@ -459,6 +461,7 @@ tp_chat_list_pending_messages_cb (TpChannel       *channel,
                }
 
                tp_chat_build_message (chat,
+                                      TRUE,
                                       message_id,
                                       message_type,
                                       timestamp,
@@ -875,6 +878,34 @@ tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
        tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
 }
 
+static EmpathyContact *
+chat_lookup_contact (EmpathyTpChat *chat,
+                    TpHandle       handle,
+                    gboolean       remove)
+{
+       EmpathyTpChatPriv *priv = GET_PRIV (chat);
+       GList *l;
+
+       for (l = priv->members; l; l = l->next) {
+               EmpathyContact *c = l->data;
+
+               if (empathy_contact_get_handle (c) != handle) {
+                       continue;
+               }
+
+               if (remove) {
+                       /* Caller takes the reference. */
+                       priv->members = g_list_delete_link (priv->members, l);
+               } else {
+                       g_object_ref (c);
+               }
+
+               return c;
+       }
+
+       return NULL;
+}
+
 static void
 tp_chat_group_members_changed_cb (TpChannel     *self,
                                  gchar         *message,
@@ -888,23 +919,30 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
 {
        EmpathyTpChatPriv *priv = GET_PRIV (chat);
        EmpathyContact *contact;
-       TpHandle handle;
+       EmpathyContact *actor_contact = NULL;
        guint i;
-       GList *l;
+
+       if (actor != 0) {
+               actor_contact = chat_lookup_contact (chat, actor, FALSE);
+               if (actor_contact == NULL) {
+                       /* FIXME: handle this a tad more gracefully: perhaps
+                        * the actor was a server op. We could use the
+                        * contact-ids detail of MembersChangedDetailed.
+                        */
+                       DEBUG ("actor %u not a channel member", actor);
+               }
+       }
 
        /* Remove contacts that are not members anymore */
        for (i = 0; i < removed->len; i++) {
-               for (l = priv->members; l; l = l->next) {
-                       contact = l->data;
-                       handle = empathy_contact_get_handle (contact);
-                       if (handle == g_array_index (removed, TpHandle, i)) {
-                               priv->members = g_list_delete_link (priv->members, l);
-                               g_signal_emit_by_name (chat, "members-changed",
-                                                      contact, NULL, reason,
-                                                      message, FALSE);
-                               g_object_unref (contact);
-                               break;
-                       }
+               contact = chat_lookup_contact (chat,
+                       g_array_index (removed, TpHandle, i), TRUE);
+
+               if (contact != NULL) {
+                       g_signal_emit_by_name (chat, "members-changed", contact,
+                                              actor_contact, reason, message,
+                                              FALSE);
+                       g_object_unref (contact);
                }
        }
 
@@ -917,6 +955,10 @@ tp_chat_group_members_changed_cb (TpChannel     *self,
        }
 
        tp_chat_update_remote_contact (chat);
+
+       if (actor_contact != NULL) {
+               g_object_unref (actor_contact);
+       }
 }
 
 static void
@@ -1128,9 +1170,9 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             _empathy_marshal_VOID__OBJECT_UINT,
+                             _empathy_marshal_VOID__STRING_UINT,
                              G_TYPE_NONE,
-                             2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
+                             2, G_TYPE_STRING, G_TYPE_UINT);
 
        signals[CHAT_STATE_CHANGED] =
                g_signal_new ("chat-state-changed",
@@ -1289,13 +1331,16 @@ empathy_tp_chat_set_state (EmpathyTpChat      *chat,
        g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
        g_return_if_fail (priv->ready);
 
-       DEBUG ("Set state: %d", state);
-       tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
-                                                                state,
-                                                                tp_chat_async_cb,
-                                                                "setting chat state",
-                                                                NULL,
-                                                                G_OBJECT (chat));
+       if (tp_proxy_has_interface_by_id (priv->channel,
+                                         TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
+               DEBUG ("Set state: %d", state);
+               tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
+                                                                        state,
+                                                                        tp_chat_async_cb,
+                                                                        "setting chat state",
+                                                                        NULL,
+                                                                        G_OBJECT (chat));
+       }
 }
 
 
@@ -1330,7 +1375,7 @@ empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
        g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
        g_return_if_fail (priv->ready);
 
-       if (empathy_message_get_sender (message) == priv->user)
+       if (!empathy_message_is_incoming (message))
                goto out;
 
        message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
@@ -1376,7 +1421,7 @@ empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
                g_assert (m != NULL);
                g_queue_delete_link (priv->pending_messages_queue, m);
 
-               if (empathy_message_get_sender (message) != priv->user) {
+               if (empathy_message_is_incoming (message)) {
                        guint id = empathy_message_get_id (message);
                        g_array_append_val (message_ids, id);
                }