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