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