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