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