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