]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
a3282818f09935fe04928e23a12be9ae4c14b2a0
[empathy.git] / libempathy / empathy-tp-chat.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
29
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-contact-monitor.h"
33 #include "empathy-contact-list.h"
34 #include "empathy-marshal.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
42 typedef struct {
43         gboolean               dispose_has_run;
44         EmpathyTpContactFactory *factory;
45         EmpathyContactMonitor *contact_monitor;
46         EmpathyContact        *user;
47         EmpathyContact        *remote_contact;
48         GList                 *members;
49         TpChannel             *channel;
50         gboolean               listing_pending_messages;
51         /* Queue of messages not signalled yet */
52         GQueue                *messages_queue;
53         /* Queue of messages signalled but not acked yet */
54         GQueue                *pending_messages_queue;
55         gboolean               had_properties_list;
56         GPtrArray             *properties;
57         gboolean               ready;
58 } EmpathyTpChatPriv;
59
60 typedef struct {
61         gchar          *name;
62         guint           id;
63         TpPropertyFlags flags;
64         GValue         *value;
65 } TpChatProperty;
66
67 static void tp_chat_iface_init         (EmpathyContactListIface *iface);
68
69 enum {
70         PROP_0,
71         PROP_CHANNEL,
72         PROP_REMOTE_CONTACT,
73         PROP_READY,
74 };
75
76 enum {
77         MESSAGE_RECEIVED,
78         SEND_ERROR,
79         CHAT_STATE_CHANGED,
80         PROPERTY_CHANGED,
81         DESTROY,
82         LAST_SIGNAL
83 };
84
85 static guint signals[LAST_SIGNAL];
86
87 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
88                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
89                                                 tp_chat_iface_init));
90
91 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
92
93 static void
94 tp_chat_invalidated_cb (TpProxy       *proxy,
95                         guint          domain,
96                         gint           code,
97                         gchar         *message,
98                         EmpathyTpChat *chat)
99 {
100         DEBUG ("Channel invalidated: %s", message);
101         g_signal_emit (chat, signals[DESTROY], 0);
102 }
103
104 static void
105 tp_chat_async_cb (TpChannel *proxy,
106                   const GError *error,
107                   gpointer user_data,
108                   GObject *weak_object)
109 {
110         if (error) {
111                 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
112         }
113 }
114
115 static void
116 tp_chat_add (EmpathyContactList *list,
117              EmpathyContact     *contact,
118              const gchar        *message)
119 {
120         EmpathyTpChatPriv *priv = GET_PRIV (list);
121         TpHandle           handle;
122         GArray             handles = {(gchar *) &handle, 1};
123
124         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
125         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
126
127         handle = empathy_contact_get_handle (contact);
128         tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
129                                                          &handles, NULL,
130                                                          NULL, NULL, NULL,
131                                                          NULL);
132 }
133
134 static void
135 tp_chat_remove (EmpathyContactList *list,
136                 EmpathyContact     *contact,
137                 const gchar        *message)
138 {
139         EmpathyTpChatPriv *priv = GET_PRIV (list);
140         TpHandle           handle;
141         GArray             handles = {(gchar *) &handle, 1};
142
143         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
144         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
145
146         handle = empathy_contact_get_handle (contact);
147         tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
148                                                             &handles, NULL,
149                                                             NULL, NULL, NULL,
150                                                             NULL);
151 }
152
153 static GList *
154 tp_chat_get_members (EmpathyContactList *list)
155 {
156         EmpathyTpChatPriv *priv = GET_PRIV (list);
157         GList             *members = NULL;
158
159         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
160
161         if (priv->members) {
162                 members = g_list_copy (priv->members);
163                 g_list_foreach (members, (GFunc) g_object_ref, NULL);
164         } else {
165                 members = g_list_prepend (members, g_object_ref (priv->user));
166                 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
167         }
168
169         return members;
170 }
171
172 static EmpathyContactMonitor *
173 tp_chat_get_monitor (EmpathyContactList *list)
174 {
175         EmpathyTpChatPriv *priv;
176
177         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
178
179         priv = GET_PRIV (list);
180
181         if (priv->contact_monitor == NULL) {
182                 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
183         }
184
185         return priv->contact_monitor;
186 }
187
188 static void
189 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
190 {
191         EmpathyTpChatPriv *priv = GET_PRIV (chat);
192         EmpathyMessage    *message;
193
194         /* Check if we can now emit some queued messages */
195         while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
196                 if (empathy_message_get_sender (message) == NULL) {
197                         break;
198                 }
199
200                 DEBUG ("Queued message ready");
201                 g_queue_pop_head (priv->messages_queue);
202                 g_queue_push_tail (priv->pending_messages_queue, message);
203                 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
204         }
205 }
206
207 static void
208 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
209                        EmpathyContact          *contact,
210                        const GError            *error,
211                        gpointer                 message,
212                        GObject                 *chat)
213 {
214         EmpathyTpChatPriv *priv = GET_PRIV (chat);
215
216         if (error) {
217                 DEBUG ("Error: %s", error->message);
218                 /* Do not block the message queue, just drop this message */
219                 g_queue_remove (priv->messages_queue, message);
220         } else {
221                 empathy_message_set_sender (message, contact);
222         }
223
224         tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
225 }
226
227 static void
228 tp_chat_build_message (EmpathyTpChat *chat,
229                        guint          id,
230                        guint          type,
231                        guint          timestamp,
232                        guint          from_handle,
233                        const gchar   *message_body)
234 {
235         EmpathyTpChatPriv *priv;
236         EmpathyMessage    *message;
237
238         priv = GET_PRIV (chat);
239
240         message = empathy_message_new (message_body);
241         empathy_message_set_tptype (message, type);
242         empathy_message_set_receiver (message, priv->user);
243         empathy_message_set_timestamp (message, timestamp);
244         empathy_message_set_id (message, id);
245         g_queue_push_tail (priv->messages_queue, message);
246
247         if (from_handle == 0) {
248                 empathy_message_set_sender (message, priv->user);
249                 tp_chat_emit_queued_messages (chat);
250         } else {
251                 empathy_tp_contact_factory_get_from_handle (priv->factory,
252                         from_handle,
253                         tp_chat_got_sender_cb,
254                         message, NULL, G_OBJECT (chat));
255         }
256 }
257
258 static void
259 tp_chat_received_cb (TpChannel   *channel,
260                      guint        message_id,
261                      guint        timestamp,
262                      guint        from_handle,
263                      guint        message_type,
264                      guint        message_flags,
265                      const gchar *message_body,
266                      gpointer     user_data,
267                      GObject     *chat_)
268 {
269         EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
270         EmpathyTpChatPriv *priv = GET_PRIV (chat);
271
272         if (priv->channel == NULL)
273                 return;
274
275         if (priv->listing_pending_messages) {
276                 return;
277         }
278
279         DEBUG ("Message received: %s", message_body);
280
281         if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
282             !tp_strdiff (message_body, "")) {
283                 GArray *ids;
284
285                 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
286
287                 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
288                 g_array_append_val (ids, message_id);
289                 acknowledge_messages (chat, ids);
290                 g_array_free (ids, TRUE);
291
292                 return;
293         }
294
295         tp_chat_build_message (chat,
296                                message_id,
297                                message_type,
298                                timestamp,
299                                from_handle,
300                                message_body);
301 }
302
303 static void
304 tp_chat_sent_cb (TpChannel   *channel,
305                  guint        timestamp,
306                  guint        message_type,
307                  const gchar *message_body,
308                  gpointer     user_data,
309                  GObject     *chat_)
310 {
311         EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
312         EmpathyTpChatPriv *priv = GET_PRIV (chat);
313
314         if (priv->channel == NULL)
315                 return;
316
317         DEBUG ("Message sent: %s", message_body);
318
319         tp_chat_build_message (chat,
320                                0,
321                                message_type,
322                                timestamp,
323                                0,
324                                message_body);
325 }
326
327 static void
328 tp_chat_send_error_cb (TpChannel   *channel,
329                        guint        error_code,
330                        guint        timestamp,
331                        guint        message_type,
332                        const gchar *message_body,
333                        gpointer     user_data,
334                        GObject     *chat)
335 {
336         EmpathyTpChatPriv *priv = GET_PRIV (chat);
337
338         if (priv->channel == NULL)
339                 return;
340
341         DEBUG ("Message sent error: %s (%d)", message_body, error_code);
342
343         tp_chat_build_message (EMPATHY_TP_CHAT (chat),
344                                0,
345                                message_type,
346                                timestamp,
347                                0,
348                                message_body);
349 }
350
351 static void
352 tp_chat_send_cb (TpChannel    *proxy,
353                  const GError *error,
354                  gpointer      user_data,
355                  GObject      *chat)
356 {
357         EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
358
359         if (error) {
360                 DEBUG ("Error: %s", error->message);
361                 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
362                                TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
363         }
364 }
365
366 typedef struct {
367         EmpathyTpChat *chat;
368         TpChannelChatState state;
369 } StateChangedData;
370
371 static void
372 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
373                                       EmpathyContact          *contact,
374                                       const GError            *error,
375                                       gpointer                 user_data,
376                                       GObject                 *chat)
377 {
378         TpChannelChatState state;
379
380         if (error) {
381                 DEBUG ("Error: %s", error->message);
382                 return;
383         }
384
385         state = GPOINTER_TO_UINT (user_data);
386         DEBUG ("Chat state changed for %s (%d): %d",
387                 empathy_contact_get_name (contact),
388                 empathy_contact_get_handle (contact), state);
389
390         g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
391 }
392
393 static void
394 tp_chat_state_changed_cb (TpChannel *channel,
395                           TpHandle   handle,
396                           TpChannelChatState state,
397                           gpointer   user_data,
398                           GObject   *chat)
399 {
400         EmpathyTpChatPriv *priv = GET_PRIV (chat);
401
402         empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
403                 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
404                 NULL, chat);
405 }
406
407 static void
408 tp_chat_list_pending_messages_cb (TpChannel       *channel,
409                                   const GPtrArray *messages_list,
410                                   const GError    *error,
411                                   gpointer         user_data,
412                                   GObject         *chat_)
413 {
414         EmpathyTpChat     *chat = EMPATHY_TP_CHAT (chat_);
415         EmpathyTpChatPriv *priv = GET_PRIV (chat);
416         guint              i;
417         GArray            *empty_non_text_content_ids = NULL;
418
419         priv->listing_pending_messages = FALSE;
420
421         if (priv->channel == NULL)
422                 return;
423
424         if (error) {
425                 DEBUG ("Error listing pending messages: %s", error->message);
426                 return;
427         }
428
429         for (i = 0; i < messages_list->len; i++) {
430                 GValueArray    *message_struct;
431                 const gchar    *message_body;
432                 guint           message_id;
433                 guint           timestamp;
434                 guint           from_handle;
435                 guint           message_type;
436                 guint           message_flags;
437
438                 message_struct = g_ptr_array_index (messages_list, i);
439
440                 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
441                 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
442                 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
443                 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
444                 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
445                 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
446
447                 DEBUG ("Message pending: %s", message_body);
448
449                 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
450                     !tp_strdiff (message_body, "")) {
451                         DEBUG ("Empty message with NonTextContent, ignoring and acking.");
452
453                         if (empty_non_text_content_ids == NULL) {
454                                 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
455                         }
456
457                         g_array_append_val (empty_non_text_content_ids, message_id);
458                         continue;
459                 }
460
461                 tp_chat_build_message (chat,
462                                        message_id,
463                                        message_type,
464                                        timestamp,
465                                        from_handle,
466                                        message_body);
467         }
468
469         if (empty_non_text_content_ids != NULL) {
470                 acknowledge_messages (chat, empty_non_text_content_ids);
471                 g_array_free (empty_non_text_content_ids, TRUE);
472         }
473 }
474
475 static void
476 tp_chat_property_flags_changed_cb (TpProxy         *proxy,
477                                    const GPtrArray *properties,
478                                    gpointer         user_data,
479                                    GObject         *chat)
480 {
481         EmpathyTpChatPriv *priv = GET_PRIV (chat);
482         guint              i, j;
483
484         if (priv->channel == NULL)
485                 return;
486
487         if (!priv->had_properties_list || !properties) {
488                 return;
489         }
490
491         for (i = 0; i < properties->len; i++) {
492                 GValueArray    *prop_struct;
493                 TpChatProperty *property;
494                 guint           id;
495                 guint           flags;
496
497                 prop_struct = g_ptr_array_index (properties, i);
498                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
499                 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
500
501                 for (j = 0; j < priv->properties->len; j++) {
502                         property = g_ptr_array_index (priv->properties, j);
503                         if (property->id == id) {
504                                 property->flags = flags;
505                                 DEBUG ("property %s flags changed: %d",
506                                         property->name, property->flags);
507                                 break;
508                         }
509                 }
510         }
511 }
512
513 static void
514 tp_chat_properties_changed_cb (TpProxy         *proxy,
515                                const GPtrArray *properties,
516                                gpointer         user_data,
517                                GObject         *chat)
518 {
519         EmpathyTpChatPriv *priv = GET_PRIV (chat);
520         guint              i, j;
521
522         if (priv->channel == NULL)
523                 return;
524
525         if (!priv->had_properties_list || !properties) {
526                 return;
527         }
528
529         for (i = 0; i < properties->len; i++) {
530                 GValueArray    *prop_struct;
531                 TpChatProperty *property;
532                 guint           id;
533                 GValue         *src_value;
534
535                 prop_struct = g_ptr_array_index (properties, i);
536                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
537                 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
538
539                 for (j = 0; j < priv->properties->len; j++) {
540                         property = g_ptr_array_index (priv->properties, j);
541                         if (property->id == id) {
542                                 if (property->value) {
543                                         g_value_copy (src_value, property->value);
544                                 } else {
545                                         property->value = tp_g_value_slice_dup (src_value);
546                                 }
547
548                                 DEBUG ("property %s changed", property->name);
549                                 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
550                                                property->name, property->value);
551                                 break;
552                         }
553                 }
554         }
555 }
556
557 static void
558 tp_chat_get_properties_cb (TpProxy         *proxy,
559                            const GPtrArray *properties,
560                            const GError    *error,
561                            gpointer         user_data,
562                            GObject         *chat)
563 {
564         if (error) {
565                 DEBUG ("Error getting properties: %s", error->message);
566                 return;
567         }
568
569         tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
570 }
571
572 static void
573 tp_chat_list_properties_cb (TpProxy         *proxy,
574                             const GPtrArray *properties,
575                             const GError    *error,
576                             gpointer         user_data,
577                             GObject         *chat)
578 {
579         EmpathyTpChatPriv *priv = GET_PRIV (chat);
580         GArray            *ids;
581         guint              i;
582
583         if (priv->channel == NULL)
584                 return;
585
586         priv->had_properties_list = TRUE;
587
588         if (error) {
589                 DEBUG ("Error listing properties: %s", error->message);
590                 return;
591         }
592
593         ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
594         priv->properties = g_ptr_array_sized_new (properties->len);
595         for (i = 0; i < properties->len; i++) {
596                 GValueArray    *prop_struct;
597                 TpChatProperty *property;
598
599                 prop_struct = g_ptr_array_index (properties, i);
600                 property = g_slice_new0 (TpChatProperty);
601                 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
602                 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
603                 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
604
605                 DEBUG ("Adding property name=%s id=%d flags=%d",
606                         property->name, property->id, property->flags);
607                 g_ptr_array_add (priv->properties, property);
608                 if (property->flags & TP_PROPERTY_FLAG_READ) {
609                         g_array_append_val (ids, property->id);
610                 }
611         }
612
613         tp_cli_properties_interface_call_get_properties (proxy, -1,
614                                                          ids,
615                                                          tp_chat_get_properties_cb,
616                                                          NULL, NULL,
617                                                          chat);
618
619         g_array_free (ids, TRUE);
620 }
621
622 void
623 empathy_tp_chat_set_property (EmpathyTpChat *chat,
624                               const gchar   *name,
625                               const GValue  *value)
626 {
627         EmpathyTpChatPriv *priv = GET_PRIV (chat);
628         TpChatProperty    *property;
629         guint              i;
630
631         for (i = 0; i < priv->properties->len; i++) {
632                 property = g_ptr_array_index (priv->properties, i);
633                 if (!tp_strdiff (property->name, name)) {
634                         GPtrArray   *properties;
635                         GValueArray *prop;
636                         GValue       id = {0, };
637                         GValue       dest_value = {0, };
638
639                         if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
640                                 break;
641                         }
642
643                         g_value_init (&id, G_TYPE_UINT);
644                         g_value_init (&dest_value, G_TYPE_VALUE);
645                         g_value_set_uint (&id, property->id);
646                         g_value_set_boxed (&dest_value, value);
647
648                         prop = g_value_array_new (2);
649                         g_value_array_append (prop, &id);
650                         g_value_array_append (prop, &dest_value);
651
652                         properties = g_ptr_array_sized_new (1);
653                         g_ptr_array_add (properties, prop);
654
655                         DEBUG ("Set property %s", name);
656                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
657                                                                          properties,
658                                                                          (tp_cli_properties_interface_callback_for_set_properties)
659                                                                          tp_chat_async_cb,
660                                                                          "Seting property", NULL,
661                                                                          G_OBJECT (chat));
662
663                         g_ptr_array_free (properties, TRUE);
664                         g_value_array_free (prop);
665
666                         break;
667                 }
668         }
669 }
670
671 static void
672 tp_chat_dispose (GObject *object)
673 {
674         EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
675         EmpathyTpChatPriv *priv = GET_PRIV (self);
676
677         if (priv->dispose_has_run)
678                 return;
679
680         priv->dispose_has_run = TRUE;
681
682         if (priv->channel != NULL) {
683                 g_signal_handlers_disconnect_by_func (priv->channel,
684                         tp_chat_invalidated_cb, self);
685                 g_object_unref (priv->channel);
686         }
687         priv->channel = NULL;
688
689         if (priv->remote_contact != NULL)
690                 g_object_unref (priv->remote_contact);
691         priv->remote_contact = NULL;
692
693         if (priv->factory != NULL)
694                 g_object_unref (priv->factory);
695         priv->factory = NULL;
696
697         if (priv->user != NULL)
698                 g_object_unref (priv->user);
699         priv->user = NULL;
700
701         if (priv->contact_monitor)
702                 g_object_unref (priv->contact_monitor);
703         priv->contact_monitor = NULL;
704
705         g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
706         g_queue_clear (priv->messages_queue);
707
708         g_queue_foreach (priv->pending_messages_queue,
709                 (GFunc) g_object_unref, NULL);
710         g_queue_clear (priv->pending_messages_queue);
711
712         if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
713                 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
714 }
715
716 static void
717 tp_chat_finalize (GObject *object)
718 {
719         EmpathyTpChatPriv *priv = GET_PRIV (object);
720         guint              i;
721
722         DEBUG ("Finalize: %p", object);
723
724         if (priv->properties) {
725                 for (i = 0; i < priv->properties->len; i++) {
726                         TpChatProperty *property;
727
728                         property = g_ptr_array_index (priv->properties, i);
729                         g_free (property->name);
730                         if (property->value) {
731                                 tp_g_value_slice_free (property->value);
732                         }
733                         g_slice_free (TpChatProperty, property);
734                 }
735                 g_ptr_array_free (priv->properties, TRUE);
736         }
737
738         g_queue_free (priv->messages_queue);
739         g_queue_free (priv->pending_messages_queue);
740
741         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
742 }
743
744 static void
745 tp_chat_check_if_ready (EmpathyTpChat *chat)
746 {
747         EmpathyTpChatPriv *priv = GET_PRIV (chat);
748
749         if (priv->ready || priv->user == NULL ||
750             (priv->members == NULL && priv->remote_contact == NULL)) {
751                 return;
752         }
753
754         DEBUG ("Ready!");
755
756         tp_cli_channel_type_text_connect_to_received (priv->channel,
757                                                       tp_chat_received_cb,
758                                                       NULL, NULL,
759                                                       G_OBJECT (chat), NULL);
760         priv->listing_pending_messages = TRUE;
761         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
762                                                              FALSE,
763                                                              tp_chat_list_pending_messages_cb,
764                                                              NULL, NULL,
765                                                              G_OBJECT (chat));
766
767         tp_cli_channel_type_text_connect_to_sent (priv->channel,
768                                                   tp_chat_sent_cb,
769                                                   NULL, NULL,
770                                                   G_OBJECT (chat), NULL);
771         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
772                                                         tp_chat_send_error_cb,
773                                                         NULL, NULL,
774                                                         G_OBJECT (chat), NULL);
775         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
776                                                                            tp_chat_state_changed_cb,
777                                                                            NULL, NULL,
778                                                                            G_OBJECT (chat), NULL);
779         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
780                                                                            tp_chat_state_changed_cb,
781                                                                            NULL, NULL,
782                                                                            G_OBJECT (chat), NULL);
783         priv->ready = TRUE;
784         g_object_notify (G_OBJECT (chat), "ready");
785 }
786
787 static void
788 tp_chat_update_remote_contact (EmpathyTpChat *chat)
789 {
790         EmpathyTpChatPriv *priv = GET_PRIV (chat);
791         EmpathyContact *contact = NULL;
792         TpHandle self_handle;
793         TpHandleType handle_type;
794         GList *l;
795
796         /* If this is a named chatroom, never pretend it is a private chat */
797         tp_channel_get_handle (priv->channel, &handle_type);
798         if (handle_type == TP_HANDLE_TYPE_ROOM) {
799                 return;
800         }
801
802         /* This is an MSN-like chat where anyone can join the chat at anytime.
803          * If there is only one non-self contact member, we are in a private
804          * chat and we set the "remote-contact" property to that contact. If
805          * there are more, set the "remote-contact" property to NULL and the
806          * UI will display a contact list. */
807         self_handle = tp_channel_group_get_self_handle (priv->channel);
808         for (l = priv->members; l; l = l->next) {
809                 /* Skip self contact if member */
810                 if (empathy_contact_get_handle (l->data) == self_handle) {
811                         continue;
812                 }
813
814                 /* We have more than one remote contact, break */
815                 if (contact != NULL) {
816                         contact = NULL;
817                         break;
818                 }
819
820                 /* If we didn't find yet a remote contact, keep this one */
821                 contact = l->data;
822         }
823
824         if (priv->remote_contact == contact) {
825                 return;
826         }
827
828         DEBUG ("Changing remote contact from %p to %p",
829                 priv->remote_contact, contact);
830
831         if (priv->remote_contact) {
832                 g_object_unref (priv->remote_contact);
833         }
834
835         priv->remote_contact = contact ? g_object_ref (contact) : NULL;
836         g_object_notify (G_OBJECT (chat), "remote-contact");
837 }
838
839 static void
840 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
841                                guint                    n_contacts,
842                                EmpathyContact * const * contacts,
843                                guint                    n_failed,
844                                const TpHandle          *failed,
845                                const GError            *error,
846                                gpointer                 user_data,
847                                GObject                 *chat)
848 {
849         EmpathyTpChatPriv *priv = GET_PRIV (chat);
850         guint i;
851         const TpIntSet *members;
852         TpHandle handle;
853         EmpathyContact *contact;
854
855         if (error) {
856                 DEBUG ("Error: %s", error->message);
857                 return;
858         }
859
860         members = tp_channel_group_get_members (priv->channel);
861         for (i = 0; i < n_contacts; i++) {
862                 contact = contacts[i];
863                 handle = empathy_contact_get_handle (contact);
864
865                 /* Make sure the contact is still member */
866                 if (tp_intset_is_member (members, handle)) {
867                         priv->members = g_list_prepend (priv->members,
868                                 g_object_ref (contact));
869                         g_signal_emit_by_name (chat, "members-changed",
870                                                contact, NULL, 0, NULL, TRUE);
871                 }
872         }
873
874         tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
875         tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
876 }
877
878 static EmpathyContact *
879 chat_lookup_contact (EmpathyTpChat *chat,
880                      TpHandle       handle,
881                      gboolean       remove)
882 {
883         EmpathyTpChatPriv *priv = GET_PRIV (chat);
884         GList *l;
885
886         for (l = priv->members; l; l = l->next) {
887                 EmpathyContact *c = l->data;
888
889                 if (empathy_contact_get_handle (c) != handle) {
890                         continue;
891                 }
892
893                 if (remove) {
894                         /* Caller takes the reference. */
895                         priv->members = g_list_delete_link (priv->members, l);
896                 } else {
897                         g_object_ref (c);
898                 }
899
900                 return c;
901         }
902
903         return NULL;
904 }
905
906 static void
907 tp_chat_group_members_changed_cb (TpChannel     *self,
908                                   gchar         *message,
909                                   GArray        *added,
910                                   GArray        *removed,
911                                   GArray        *local_pending,
912                                   GArray        *remote_pending,
913                                   guint          actor,
914                                   guint          reason,
915                                   EmpathyTpChat *chat)
916 {
917         EmpathyTpChatPriv *priv = GET_PRIV (chat);
918         EmpathyContact *contact;
919         EmpathyContact *actor_contact = NULL;
920         guint i;
921
922         if (actor != 0) {
923                 actor_contact = chat_lookup_contact (chat, actor, FALSE);
924                 if (actor_contact == NULL) {
925                         /* FIXME: handle this a tad more gracefully: perhaps
926                          * the actor was a server op. We could use the
927                          * contact-ids detail of MembersChangedDetailed.
928                          */
929                         DEBUG ("actor %u not a channel member", actor);
930                 }
931         }
932
933         /* Remove contacts that are not members anymore */
934         for (i = 0; i < removed->len; i++) {
935                 contact = chat_lookup_contact (chat,
936                         g_array_index (removed, TpHandle, i), TRUE);
937
938                 if (contact != NULL) {
939                         g_signal_emit_by_name (chat, "members-changed", contact,
940                                                actor_contact, reason, message,
941                                                FALSE);
942                         g_object_unref (contact);
943                 }
944         }
945
946         /* Request added contacts */
947         if (added->len > 0) {
948                 empathy_tp_contact_factory_get_from_handles (priv->factory,
949                         added->len, (TpHandle *) added->data,
950                         tp_chat_got_added_contacts_cb, NULL, NULL,
951                         G_OBJECT (chat));
952         }
953
954         tp_chat_update_remote_contact (chat);
955
956         if (actor_contact != NULL) {
957                 g_object_unref (actor_contact);
958         }
959 }
960
961 static void
962 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
963                                EmpathyContact          *contact,
964                                const GError            *error,
965                                gpointer                 user_data,
966                                GObject                 *chat)
967 {
968         EmpathyTpChatPriv *priv = GET_PRIV (chat);
969
970         if (error) {
971                 DEBUG ("Error: %s", error->message);
972                 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
973                 return;
974         }
975
976         priv->remote_contact = g_object_ref (contact);
977         g_object_notify (chat, "remote-contact");
978
979         tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
980 }
981
982 static void
983 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
984                              EmpathyContact          *contact,
985                              const GError            *error,
986                              gpointer                 user_data,
987                              GObject                 *chat)
988 {
989         EmpathyTpChatPriv *priv = GET_PRIV (chat);
990
991         if (error) {
992                 DEBUG ("Error: %s", error->message);
993                 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
994                 return;
995         }
996
997         priv->user = g_object_ref (contact);
998         empathy_contact_set_is_user (priv->user, TRUE);
999         tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1000 }
1001
1002 static GObject *
1003 tp_chat_constructor (GType                  type,
1004                      guint                  n_props,
1005                      GObjectConstructParam *props)
1006 {
1007         GObject           *chat;
1008         EmpathyTpChatPriv *priv;
1009         TpConnection      *connection;
1010         TpHandle           handle;
1011
1012         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1013
1014         priv = GET_PRIV (chat);
1015
1016         connection = tp_channel_borrow_connection (priv->channel);
1017         priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1018         g_signal_connect (priv->channel, "invalidated",
1019                           G_CALLBACK (tp_chat_invalidated_cb),
1020                           chat);
1021
1022         if (tp_proxy_has_interface_by_id (priv->channel,
1023                                           TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1024                 const TpIntSet *members;
1025                 GArray *handles;
1026
1027                 /* Get self contact from the group's self handle */
1028                 handle = tp_channel_group_get_self_handle (priv->channel);
1029                 empathy_tp_contact_factory_get_from_handle (priv->factory,
1030                         handle, tp_chat_got_self_contact_cb,
1031                         NULL, NULL, chat);
1032
1033                 /* Get initial member contacts */
1034                 members = tp_channel_group_get_members (priv->channel);
1035                 handles = tp_intset_to_array (members);
1036                 empathy_tp_contact_factory_get_from_handles (priv->factory,
1037                         handles->len, (TpHandle *) handles->data,
1038                         tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1039
1040                 g_signal_connect (priv->channel, "group-members-changed",
1041                         G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1042         } else {
1043                 /* Get the self contact from the connection's self handle */
1044                 handle = tp_connection_get_self_handle (connection);
1045                 empathy_tp_contact_factory_get_from_handle (priv->factory,
1046                         handle, tp_chat_got_self_contact_cb,
1047                         NULL, NULL, chat);
1048
1049                 /* Get the remote contact */
1050                 handle = tp_channel_get_handle (priv->channel, NULL);
1051                 empathy_tp_contact_factory_get_from_handle (priv->factory,
1052                         handle, tp_chat_got_remote_contact_cb,
1053                         NULL, NULL, chat);
1054         }
1055
1056         if (tp_proxy_has_interface_by_id (priv->channel,
1057                                           TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1058                 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1059                                                                   tp_chat_list_properties_cb,
1060                                                                   NULL, NULL,
1061                                                                   G_OBJECT (chat));
1062                 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1063                                                                            tp_chat_properties_changed_cb,
1064                                                                            NULL, NULL,
1065                                                                            G_OBJECT (chat), NULL);
1066                 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1067                                                                                tp_chat_property_flags_changed_cb,
1068                                                                                NULL, NULL,
1069                                                                                G_OBJECT (chat), NULL);
1070         }
1071
1072         return chat;
1073 }
1074
1075 static void
1076 tp_chat_get_property (GObject    *object,
1077                       guint       param_id,
1078                       GValue     *value,
1079                       GParamSpec *pspec)
1080 {
1081         EmpathyTpChatPriv *priv = GET_PRIV (object);
1082
1083         switch (param_id) {
1084         case PROP_CHANNEL:
1085                 g_value_set_object (value, priv->channel);
1086                 break;
1087         case PROP_REMOTE_CONTACT:
1088                 g_value_set_object (value, priv->remote_contact);
1089                 break;
1090         case PROP_READY:
1091                 g_value_set_boolean (value, priv->ready);
1092                 break;
1093         default:
1094                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1095                 break;
1096         };
1097 }
1098
1099 static void
1100 tp_chat_set_property (GObject      *object,
1101                       guint         param_id,
1102                       const GValue *value,
1103                       GParamSpec   *pspec)
1104 {
1105         EmpathyTpChatPriv *priv = GET_PRIV (object);
1106
1107         switch (param_id) {
1108         case PROP_CHANNEL:
1109                 priv->channel = g_value_dup_object (value);
1110                 break;
1111         default:
1112                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1113                 break;
1114         };
1115 }
1116
1117 static void
1118 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1119 {
1120         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1121
1122         object_class->dispose = tp_chat_dispose;
1123         object_class->finalize = tp_chat_finalize;
1124         object_class->constructor = tp_chat_constructor;
1125         object_class->get_property = tp_chat_get_property;
1126         object_class->set_property = tp_chat_set_property;
1127
1128         g_object_class_install_property (object_class,
1129                                          PROP_CHANNEL,
1130                                          g_param_spec_object ("channel",
1131                                                               "telepathy channel",
1132                                                               "The text channel for the chat",
1133                                                               TP_TYPE_CHANNEL,
1134                                                               G_PARAM_READWRITE |
1135                                                               G_PARAM_CONSTRUCT_ONLY));
1136
1137         g_object_class_install_property (object_class,
1138                                          PROP_REMOTE_CONTACT,
1139                                          g_param_spec_object ("remote-contact",
1140                                                               "The remote contact",
1141                                                               "The remote contact if there is no group iface on the channel",
1142                                                               EMPATHY_TYPE_CONTACT,
1143                                                               G_PARAM_READABLE));
1144
1145         g_object_class_install_property (object_class,
1146                                          PROP_READY,
1147                                          g_param_spec_boolean ("ready",
1148                                                                "Is the object ready",
1149                                                                "This object can't be used until this becomes true",
1150                                                                FALSE,
1151                                                                G_PARAM_READABLE));
1152
1153         /* Signals */
1154         signals[MESSAGE_RECEIVED] =
1155                 g_signal_new ("message-received",
1156                               G_TYPE_FROM_CLASS (klass),
1157                               G_SIGNAL_RUN_LAST,
1158                               0,
1159                               NULL, NULL,
1160                               g_cclosure_marshal_VOID__OBJECT,
1161                               G_TYPE_NONE,
1162                               1, EMPATHY_TYPE_MESSAGE);
1163
1164         signals[SEND_ERROR] =
1165                 g_signal_new ("send-error",
1166                               G_TYPE_FROM_CLASS (klass),
1167                               G_SIGNAL_RUN_LAST,
1168                               0,
1169                               NULL, NULL,
1170                               _empathy_marshal_VOID__OBJECT_UINT,
1171                               G_TYPE_NONE,
1172                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1173
1174         signals[CHAT_STATE_CHANGED] =
1175                 g_signal_new ("chat-state-changed",
1176                               G_TYPE_FROM_CLASS (klass),
1177                               G_SIGNAL_RUN_LAST,
1178                               0,
1179                               NULL, NULL,
1180                               _empathy_marshal_VOID__OBJECT_UINT,
1181                               G_TYPE_NONE,
1182                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1183
1184         signals[PROPERTY_CHANGED] =
1185                 g_signal_new ("property-changed",
1186                               G_TYPE_FROM_CLASS (klass),
1187                               G_SIGNAL_RUN_LAST,
1188                               0,
1189                               NULL, NULL,
1190                               _empathy_marshal_VOID__STRING_BOXED,
1191                               G_TYPE_NONE,
1192                               2, G_TYPE_STRING, G_TYPE_VALUE);
1193
1194         signals[DESTROY] =
1195                 g_signal_new ("destroy",
1196                               G_TYPE_FROM_CLASS (klass),
1197                               G_SIGNAL_RUN_LAST,
1198                               0,
1199                               NULL, NULL,
1200                               g_cclosure_marshal_VOID__VOID,
1201                               G_TYPE_NONE,
1202                               0);
1203
1204         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1205 }
1206
1207 static void
1208 empathy_tp_chat_init (EmpathyTpChat *chat)
1209 {
1210         EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1211                 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1212
1213         chat->priv = priv;
1214         priv->contact_monitor = NULL;
1215         priv->messages_queue = g_queue_new ();
1216         priv->pending_messages_queue = g_queue_new ();
1217 }
1218
1219 static void
1220 tp_chat_iface_init (EmpathyContactListIface *iface)
1221 {
1222         iface->add         = tp_chat_add;
1223         iface->remove      = tp_chat_remove;
1224         iface->get_members = tp_chat_get_members;
1225         iface->get_monitor = tp_chat_get_monitor;
1226 }
1227
1228 EmpathyTpChat *
1229 empathy_tp_chat_new (TpChannel *channel)
1230 {
1231         return g_object_new (EMPATHY_TYPE_TP_CHAT,
1232                              "channel", channel,
1233                              NULL);
1234 }
1235
1236 void
1237 empathy_tp_chat_close (EmpathyTpChat *chat) {
1238         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1239
1240         /* If there are still messages left, it'll come back..
1241          * We loose the ordering of sent messages though */
1242         tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1243                 "closing channel", NULL, NULL);
1244 }
1245
1246 const gchar *
1247 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1248 {
1249         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1250
1251         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1252
1253         return tp_channel_get_identifier (priv->channel);
1254 }
1255
1256 EmpathyContact *
1257 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1258 {
1259         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1260
1261         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1262         g_return_val_if_fail (priv->ready, NULL);
1263
1264         return priv->remote_contact;
1265 }
1266
1267 TpChannel *
1268 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1269 {
1270         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1271
1272         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1273
1274         return priv->channel;
1275 }
1276
1277 TpConnection *
1278 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1279 {
1280         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1281
1282         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1283
1284         return tp_channel_borrow_connection (priv->channel);
1285 }
1286
1287 gboolean
1288 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1289 {
1290         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1291
1292         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1293
1294         return priv->ready;
1295 }
1296
1297 void
1298 empathy_tp_chat_send (EmpathyTpChat *chat,
1299                       EmpathyMessage *message)
1300 {
1301         EmpathyTpChatPriv        *priv = GET_PRIV (chat);
1302         const gchar              *message_body;
1303         TpChannelTextMessageType  message_type;
1304
1305         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1306         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1307         g_return_if_fail (priv->ready);
1308
1309         message_body = empathy_message_get_body (message);
1310         message_type = empathy_message_get_tptype (message);
1311
1312         DEBUG ("Sending message: %s", message_body);
1313         tp_cli_channel_type_text_call_send (priv->channel, -1,
1314                                             message_type,
1315                                             message_body,
1316                                             tp_chat_send_cb,
1317                                             g_object_ref (message),
1318                                             (GDestroyNotify) g_object_unref,
1319                                             G_OBJECT (chat));
1320 }
1321
1322 void
1323 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
1324                            TpChannelChatState  state)
1325 {
1326         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1327
1328         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1329         g_return_if_fail (priv->ready);
1330
1331         DEBUG ("Set state: %d", state);
1332         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1333                                                                  state,
1334                                                                  tp_chat_async_cb,
1335                                                                  "setting chat state",
1336                                                                  NULL,
1337                                                                  G_OBJECT (chat));
1338 }
1339
1340
1341 const GList *
1342 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1343 {
1344         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1345
1346         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1347         g_return_val_if_fail (priv->ready, NULL);
1348
1349         return priv->pending_messages_queue->head;
1350 }
1351
1352 static void
1353 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1354         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1355
1356         tp_cli_channel_type_text_call_acknowledge_pending_messages (
1357                 priv->channel, -1, ids, tp_chat_async_cb,
1358                 "acknowledging received message", NULL, G_OBJECT (chat));
1359 }
1360
1361 void
1362 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1363                                      EmpathyMessage *message) {
1364         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1365         GArray *message_ids;
1366         GList *m;
1367         guint id;
1368
1369         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1370         g_return_if_fail (priv->ready);
1371
1372         if (empathy_message_get_sender (message) == priv->user)
1373                 goto out;
1374
1375         message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1376
1377         id = empathy_message_get_id (message);
1378         g_array_append_val (message_ids, id);
1379         acknowledge_messages (chat, message_ids);
1380         g_array_free (message_ids, TRUE);
1381
1382 out:
1383         m = g_queue_find (priv->pending_messages_queue, message);
1384         g_assert (m != NULL);
1385         g_queue_delete_link (priv->pending_messages_queue, m);
1386         g_object_unref (message);
1387 }
1388
1389 void
1390 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1391                                       const GList *messages) {
1392         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1393         /* Copy messages as the messges list (probably is) our own */
1394         GList *msgs = g_list_copy ((GList *) messages);
1395         GList *l;
1396         guint length;
1397         GArray *message_ids;
1398
1399         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1400         g_return_if_fail (priv->ready);
1401
1402         length = g_list_length ((GList *) messages);
1403
1404         if (length == 0)
1405                 return;
1406
1407         message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1408
1409         for (l = msgs; l != NULL; l = g_list_next (l)) {
1410                 GList *m;
1411
1412                 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1413
1414                 m = g_queue_find (priv->pending_messages_queue, message);
1415                 g_assert (m != NULL);
1416                 g_queue_delete_link (priv->pending_messages_queue, m);
1417
1418                 if (empathy_message_get_sender (message) != priv->user) {
1419                         guint id = empathy_message_get_id (message);
1420                         g_array_append_val (message_ids, id);
1421                 }
1422                 g_object_unref (message);
1423         }
1424
1425         if (message_ids->len > 0)
1426                 acknowledge_messages (chat, message_ids);
1427
1428         g_array_free (message_ids, TRUE);
1429         g_list_free (msgs);
1430 }
1431