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