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