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