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