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