]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
7915ff20b659396697317037fd0ed78399b860bc
[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         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 (EMPATHY_TP_CHAT (chat), ids);
446                 g_array_free (ids, TRUE);
447
448                 return;
449         }
450
451         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
452                                          message_id,
453                                          message_type,
454                                          timestamp,
455                                          from_handle,
456                                          message_body);
457
458         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (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         EmpathyTpChatPriv *priv = GET_PRIV (chat);
471         EmpathyMessage *message;
472
473         if (priv->channel == NULL)
474                 return;
475
476         DEBUG ("Message sent: %s", message_body);
477
478         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
479                                          0,
480                                          message_type,
481                                          timestamp,
482                                          0,
483                                          message_body);
484
485         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
486         g_object_unref (message);
487 }
488
489 static void
490 tp_chat_send_error_cb (TpChannel   *channel,
491                        guint        error_code,
492                        guint        timestamp,
493                        guint        message_type,
494                        const gchar *message_body,
495                        gpointer     user_data,
496                        GObject     *chat)
497 {
498         EmpathyMessage *message;
499         EmpathyTpChatPriv *priv = GET_PRIV (chat);
500
501         if (priv->channel == NULL)
502                 return;
503
504         DEBUG ("Message sent error: %s (%d)", message_body, error_code);
505
506         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
507                                          0,
508                                          message_type,
509                                          timestamp,
510                                          0,
511                                          message_body);
512
513         g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
514         g_object_unref (message);
515 }
516
517 static void
518 tp_chat_send_cb (TpChannel    *proxy,
519                  const GError *error,
520                  gpointer      user_data,
521                  GObject      *chat)
522 {
523         EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
524
525         if (error) {
526                 DEBUG ("Error: %s", error->message);
527                 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
528                                TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
529         }
530 }
531
532 static void
533 tp_chat_state_changed_cb (TpChannel *channel,
534                           guint      handle,
535                           guint      state,
536                           gpointer   user_data,
537                           GObject   *chat)
538 {
539         EmpathyTpChatPriv *priv = GET_PRIV (chat);
540         EmpathyContact    *contact;
541
542         if (priv->channel == NULL)
543                 return;
544
545         contact = empathy_contact_factory_get_from_handle (priv->factory,
546                                                            priv->account,
547                                                            handle);
548
549         DEBUG ("Chat state changed for %s (%d): %d",
550                 empathy_contact_get_name (contact), handle, state);
551
552         g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
553         g_object_unref (contact);
554 }
555
556 static void
557 tp_chat_list_pending_messages_cb (TpChannel       *channel,
558                                   const GPtrArray *messages_list,
559                                   const GError    *error,
560                                   gpointer         user_data,
561                                   GObject         *chat)
562 {
563         EmpathyTpChatPriv *priv = GET_PRIV (chat);
564         guint              i;
565         GArray            *empty_non_text_content_ids = NULL;
566
567         priv->listing_pending_messages = FALSE;
568
569         if (priv->channel == NULL)
570                 return;
571
572         if (error) {
573                 DEBUG ("Error listing pending messages: %s", error->message);
574                 return;
575         }
576
577         for (i = 0; i < messages_list->len; i++) {
578                 EmpathyMessage *message;
579                 GValueArray    *message_struct;
580                 const gchar    *message_body;
581                 guint           message_id;
582                 guint           timestamp;
583                 guint           from_handle;
584                 guint           message_type;
585                 guint           message_flags;
586
587                 message_struct = g_ptr_array_index (messages_list, i);
588
589                 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
590                 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
591                 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
592                 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
593                 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
594                 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
595
596                 DEBUG ("Message pending: %s", message_body);
597
598                 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
599                     !tp_strdiff (message_body, "")) {
600                         DEBUG ("Empty message with NonTextContent, ignoring and acking.");
601
602                         if (empty_non_text_content_ids == NULL) {
603                                 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
604                         }
605
606                         g_array_append_val (empty_non_text_content_ids, message_id);
607                         continue;
608                 }
609
610                 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
611                                                  message_id,
612                                                  message_type,
613                                                  timestamp,
614                                                  from_handle,
615                                                  message_body);
616
617                 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
618                 g_object_unref (message);
619         }
620
621         if (empty_non_text_content_ids != NULL) {
622                 acknowledge_messages (EMPATHY_TP_CHAT (chat), empty_non_text_content_ids);
623                 g_array_free (empty_non_text_content_ids, TRUE);
624         }
625 }
626
627 static void
628 tp_chat_property_flags_changed_cb (TpProxy         *proxy,
629                                    const GPtrArray *properties,
630                                    gpointer         user_data,
631                                    GObject         *chat)
632 {
633         EmpathyTpChatPriv *priv = GET_PRIV (chat);
634         guint              i, j;
635
636         if (priv->channel == NULL)
637                 return;
638
639         if (!priv->had_properties_list || !properties) {
640                 return;
641         }
642
643         for (i = 0; i < properties->len; i++) {
644                 GValueArray    *prop_struct;
645                 TpChatProperty *property;
646                 guint           id;
647                 guint           flags;
648
649                 prop_struct = g_ptr_array_index (properties, i);
650                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
651                 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
652
653                 for (j = 0; j < priv->properties->len; j++) {
654                         property = g_ptr_array_index (priv->properties, j);
655                         if (property->id == id) {
656                                 property->flags = flags;
657                                 DEBUG ("property %s flags changed: %d",
658                                         property->name, property->flags);
659                                 break;
660                         }
661                 }
662         }
663 }
664
665 static void
666 tp_chat_properties_changed_cb (TpProxy         *proxy,
667                                const GPtrArray *properties,
668                                gpointer         user_data,
669                                GObject         *chat)
670 {
671         EmpathyTpChatPriv *priv = GET_PRIV (chat);
672         guint              i, j;
673
674         if (priv->channel == NULL)
675                 return;
676
677         if (!priv->had_properties_list || !properties) {
678                 return;
679         }
680
681         for (i = 0; i < properties->len; i++) {
682                 GValueArray    *prop_struct;
683                 TpChatProperty *property;
684                 guint           id;
685                 GValue         *src_value;
686
687                 prop_struct = g_ptr_array_index (properties, i);
688                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
689                 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
690
691                 for (j = 0; j < priv->properties->len; j++) {
692                         property = g_ptr_array_index (priv->properties, j);
693                         if (property->id == id) {
694                                 if (property->value) {
695                                         g_value_copy (src_value, property->value);
696                                 } else {
697                                         property->value = tp_g_value_slice_dup (src_value);
698                                 }
699
700                                 DEBUG ("property %s changed", property->name);
701                                 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
702                                                property->name, property->value);
703                                 break;
704                         }
705                 }
706         }
707 }
708
709 static void
710 tp_chat_get_properties_cb (TpProxy         *proxy,
711                            const GPtrArray *properties,
712                            const GError    *error,
713                            gpointer         user_data,
714                            GObject         *chat)
715 {
716         if (error) {
717                 DEBUG ("Error getting properties: %s", error->message);
718                 return;
719         }
720
721         tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
722 }
723
724 static void
725 tp_chat_list_properties_cb (TpProxy         *proxy,
726                             const GPtrArray *properties,
727                             const GError    *error,
728                             gpointer         user_data,
729                             GObject         *chat)
730 {
731         EmpathyTpChatPriv *priv = GET_PRIV (chat);
732         GArray            *ids;
733         guint              i;
734
735         if (priv->channel == NULL)
736                 return;
737
738         priv->had_properties_list = TRUE;
739
740         if (error) {
741                 DEBUG ("Error listing properties: %s", error->message);
742                 return;
743         }
744
745         ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
746         priv->properties = g_ptr_array_sized_new (properties->len);
747         for (i = 0; i < properties->len; i++) {
748                 GValueArray    *prop_struct;
749                 TpChatProperty *property;
750
751                 prop_struct = g_ptr_array_index (properties, i);
752                 property = g_slice_new0 (TpChatProperty);
753                 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
754                 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
755                 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
756
757                 DEBUG ("Adding property name=%s id=%d flags=%d",
758                         property->name, property->id, property->flags);
759                 g_ptr_array_add (priv->properties, property);
760                 if (property->flags & TP_PROPERTY_FLAG_READ) {
761                         g_array_append_val (ids, property->id);
762                 }
763         }
764
765         tp_cli_properties_interface_call_get_properties (proxy, -1,
766                                                          ids,
767                                                          tp_chat_get_properties_cb,
768                                                          NULL, NULL,
769                                                          chat);
770
771         g_array_free (ids, TRUE);
772 }
773
774 void
775 empathy_tp_chat_set_property (EmpathyTpChat *chat,
776                               const gchar   *name,
777                               const GValue  *value)
778 {
779         EmpathyTpChatPriv *priv = GET_PRIV (chat);
780         TpChatProperty    *property;
781         guint              i;
782
783         g_return_if_fail (priv->ready);
784
785         for (i = 0; i < priv->properties->len; i++) {
786                 property = g_ptr_array_index (priv->properties, i);
787                 if (!tp_strdiff (property->name, name)) {
788                         GPtrArray   *properties;
789                         GValueArray *prop;
790                         GValue       id = {0, };
791                         GValue       dest_value = {0, };
792
793                         if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
794                                 break;
795                         }
796
797                         g_value_init (&id, G_TYPE_UINT);
798                         g_value_init (&dest_value, G_TYPE_VALUE);
799                         g_value_set_uint (&id, property->id);
800                         g_value_set_boxed (&dest_value, value);
801
802                         prop = g_value_array_new (2);
803                         g_value_array_append (prop, &id);
804                         g_value_array_append (prop, &dest_value);
805
806                         properties = g_ptr_array_sized_new (1);
807                         g_ptr_array_add (properties, prop);
808
809                         DEBUG ("Set property %s", name);
810                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
811                                                                          properties,
812                                                                          (tp_cli_properties_interface_callback_for_set_properties)
813                                                                          tp_chat_async_cb,
814                                                                          "Seting property", NULL,
815                                                                          G_OBJECT (chat));
816
817                         g_ptr_array_free (properties, TRUE);
818                         g_value_array_free (prop);
819
820                         break;
821                 }
822         }
823 }
824
825 static void
826 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
827 {
828         EmpathyTpChatPriv *priv = GET_PRIV (chat);
829         TpConnection      *connection;
830         guint              handle, handle_type;
831
832         if (priv->channel == NULL)
833                 return;
834
835         DEBUG ("Channel ready");
836
837         g_object_get (priv->channel,
838                       "connection", &connection,
839                       "handle", &handle,
840                       "handle_type", &handle_type,
841                       NULL);
842
843         if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
844                 GArray *handles;
845                 gchar **names;
846
847                 handles = g_array_new (FALSE, FALSE, sizeof (guint));
848                 g_array_append_val (handles, handle);
849                 tp_cli_connection_run_inspect_handles (connection, -1,
850                                                        handle_type, handles,
851                                                        &names, NULL, NULL);
852                 priv->id = *names;
853                 g_array_free (handles, TRUE);
854                 g_free (names);
855         }
856
857         if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
858                 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
859                                                                                 priv->account,
860                                                                                 handle);
861                 g_object_notify (G_OBJECT (chat), "remote-contact");
862         }
863
864         if (tp_proxy_has_interface_by_id (priv->channel,
865                                           TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
866                 priv->group = empathy_tp_group_new (priv->channel);
867
868                 g_signal_connect (priv->group, "member-added",
869                                   G_CALLBACK (tp_chat_member_added_cb),
870                                   chat);
871                 g_signal_connect (priv->group, "member-removed",
872                                   G_CALLBACK (tp_chat_member_removed_cb),
873                                   chat);
874                 g_signal_connect (priv->group, "local-pending",
875                                   G_CALLBACK (tp_chat_local_pending_cb),
876                                   chat);
877                 empathy_run_until_ready (priv->group);
878         } else {
879                 priv->members_count = 2;
880         }
881         
882         if (tp_proxy_has_interface_by_id (priv->channel,
883                                           TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
884                 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
885                                                                   tp_chat_list_properties_cb,
886                                                                   NULL, NULL,
887                                                                   G_OBJECT (chat));
888                 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
889                                                                            tp_chat_properties_changed_cb,
890                                                                            NULL, NULL,
891                                                                            G_OBJECT (chat), NULL);
892                 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
893                                                                                tp_chat_property_flags_changed_cb,
894                                                                                NULL, NULL,
895                                                                                G_OBJECT (chat), NULL);
896         }
897
898         priv->listing_pending_messages = TRUE;
899         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
900                                                              FALSE,
901                                                              tp_chat_list_pending_messages_cb,
902                                                              NULL, NULL,
903                                                              G_OBJECT (chat));
904
905         tp_cli_channel_type_text_connect_to_received (priv->channel,
906                                                       tp_chat_received_cb,
907                                                       NULL, NULL,
908                                                       G_OBJECT (chat), NULL);
909         tp_cli_channel_type_text_connect_to_sent (priv->channel,
910                                                   tp_chat_sent_cb,
911                                                   NULL, NULL,
912                                                   G_OBJECT (chat), NULL);
913         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
914                                                         tp_chat_send_error_cb,
915                                                         NULL, NULL,
916                                                         G_OBJECT (chat), NULL);
917         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
918                                                                            tp_chat_state_changed_cb,
919                                                                            NULL, NULL,
920                                                                            G_OBJECT (chat), NULL);
921         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
922                                                                            tp_chat_state_changed_cb,
923                                                                            NULL, NULL,
924                                                                            G_OBJECT (chat), NULL);
925
926         priv->ready = TRUE;
927         g_object_notify (G_OBJECT (chat), "ready");
928 }
929
930 static void
931 tp_chat_dispose (GObject *object)
932 {
933         EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
934         EmpathyTpChatPriv *priv = GET_PRIV (self);
935
936         if (priv->dispose_has_run)
937                 return;
938
939         priv->dispose_has_run = TRUE;
940
941         if (priv->channel != NULL)
942                 {
943                         g_signal_handlers_disconnect_by_func (priv->channel,
944                                 tp_chat_invalidated_cb, self);
945                         g_object_unref (priv->channel);
946                         priv->channel = NULL;
947                 }
948
949         if (priv->remote_contact != NULL)
950                 g_object_unref (priv->remote_contact);
951
952         priv->remote_contact = NULL;
953
954         if (priv->group != NULL)
955                 g_object_unref (priv->group);
956         priv->group = NULL;
957
958         if (priv->factory != NULL)
959                 g_object_unref (priv->factory);
960         priv->factory = NULL;
961
962         if (priv->user != NULL);
963                 g_object_unref (priv->user);
964         priv->user = NULL;
965
966         if (priv->account != NULL);
967                 g_object_unref (priv->account);
968         priv->account = NULL;
969
970         if (priv->contact_monitor)
971                 g_object_unref (priv->contact_monitor);
972         priv->contact_monitor = NULL;
973
974         if (!g_queue_is_empty (priv->messages_queue)) {
975                 EmpathyMessage *message;
976                 EmpathyContact *contact;
977
978                 message = g_queue_peek_head (priv->messages_queue);
979                 contact = empathy_message_get_sender (message);
980                 g_signal_handlers_disconnect_by_func (contact,
981                 tp_chat_sender_ready_notify_cb, object);
982         }
983
984         g_list_foreach (priv->messages_queue->head,
985                 (GFunc) g_object_unref, NULL);
986
987         g_list_foreach (priv->pending_messages_queue->head,
988                 (GFunc) g_object_unref, NULL);
989
990         if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
991                 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
992 }
993
994 static void
995 tp_chat_finalize (GObject *object)
996 {
997         EmpathyTpChatPriv *priv = GET_PRIV (object);
998         guint              i;
999
1000         DEBUG ("Finalize: %p", object);
1001
1002         if (priv->properties) {
1003                 for (i = 0; i < priv->properties->len; i++) {
1004                         TpChatProperty *property;
1005
1006                         property = g_ptr_array_index (priv->properties, i);
1007                         g_free (property->name);
1008                         if (property->value) {
1009                                 tp_g_value_slice_free (property->value);
1010                         }
1011                         g_slice_free (TpChatProperty, property);
1012                 }
1013                 g_ptr_array_free (priv->properties, TRUE);
1014         }
1015
1016
1017         g_free (priv->id);
1018         g_queue_free (priv->messages_queue);
1019         g_queue_free (priv->pending_messages_queue);
1020
1021         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
1022 }
1023
1024 static GObject *
1025 tp_chat_constructor (GType                  type,
1026                      guint                  n_props,
1027                      GObjectConstructParam *props)
1028 {
1029         GObject           *chat;
1030         EmpathyTpChatPriv *priv;
1031         gboolean           channel_ready;
1032
1033         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1034
1035         priv = GET_PRIV (chat);
1036         priv->account = empathy_channel_get_account (priv->channel);
1037         priv->factory = empathy_contact_factory_dup_singleton ();
1038         priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1039
1040         g_signal_connect (priv->channel, "invalidated",
1041                           G_CALLBACK (tp_chat_invalidated_cb),
1042                           chat);
1043
1044         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1045         if (channel_ready) {
1046                 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1047         } else {
1048                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1049                                           G_CALLBACK (tp_chat_channel_ready_cb),
1050                                           chat);
1051         }
1052
1053         return chat;
1054 }
1055
1056 static void
1057 tp_chat_get_property (GObject    *object,
1058                       guint       param_id,
1059                       GValue     *value,
1060                       GParamSpec *pspec)
1061 {
1062         EmpathyTpChatPriv *priv = GET_PRIV (object);
1063
1064         switch (param_id) {
1065         case PROP_CHANNEL:
1066                 g_value_set_object (value, priv->channel);
1067                 break;
1068         case PROP_REMOTE_CONTACT:
1069                 g_value_set_object (value, priv->remote_contact);
1070                 break;
1071         case PROP_READY:
1072                 g_value_set_boolean (value, priv->ready);
1073                 break;
1074         default:
1075                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1076                 break;
1077         };
1078 }
1079
1080 static void
1081 tp_chat_set_property (GObject      *object,
1082                       guint         param_id,
1083                       const GValue *value,
1084                       GParamSpec   *pspec)
1085 {
1086         EmpathyTpChatPriv *priv = GET_PRIV (object);
1087
1088         switch (param_id) {
1089         case PROP_CHANNEL:
1090                 priv->channel = g_value_dup_object (value);
1091                 break;
1092         default:
1093                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1094                 break;
1095         };
1096 }
1097
1098 static void
1099 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1100 {
1101         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1102
1103         object_class->dispose = tp_chat_dispose;
1104         object_class->finalize = tp_chat_finalize;
1105         object_class->constructor = tp_chat_constructor;
1106         object_class->get_property = tp_chat_get_property;
1107         object_class->set_property = tp_chat_set_property;
1108
1109         g_object_class_install_property (object_class,
1110                                          PROP_CHANNEL,
1111                                          g_param_spec_object ("channel",
1112                                                               "telepathy channel",
1113                                                               "The text channel for the chat",
1114                                                               TP_TYPE_CHANNEL,
1115                                                               G_PARAM_READWRITE |
1116                                                               G_PARAM_CONSTRUCT_ONLY));
1117
1118         g_object_class_install_property (object_class,
1119                                          PROP_REMOTE_CONTACT,
1120                                          g_param_spec_object ("remote-contact",
1121                                                               "The remote contact",
1122                                                               "The remote contact if there is no group iface on the channel",
1123                                                               EMPATHY_TYPE_CONTACT,
1124                                                               G_PARAM_READABLE));
1125         g_object_class_install_property (object_class,
1126                                          PROP_READY,
1127                                          g_param_spec_boolean ("ready",
1128                                                                "Is the object ready",
1129                                                                "This object can't be used until this becomes true",
1130                                                                FALSE,
1131                                                                G_PARAM_READABLE));
1132
1133         /* Signals */
1134         signals[MESSAGE_RECEIVED] =
1135                 g_signal_new ("message-received",
1136                               G_TYPE_FROM_CLASS (klass),
1137                               G_SIGNAL_RUN_LAST,
1138                               0,
1139                               NULL, NULL,
1140                               g_cclosure_marshal_VOID__OBJECT,
1141                               G_TYPE_NONE,
1142                               1, EMPATHY_TYPE_MESSAGE);
1143
1144         signals[SEND_ERROR] =
1145                 g_signal_new ("send-error",
1146                               G_TYPE_FROM_CLASS (klass),
1147                               G_SIGNAL_RUN_LAST,
1148                               0,
1149                               NULL, NULL,
1150                               _empathy_marshal_VOID__OBJECT_UINT,
1151                               G_TYPE_NONE,
1152                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1153
1154         signals[CHAT_STATE_CHANGED] =
1155                 g_signal_new ("chat-state-changed",
1156                               G_TYPE_FROM_CLASS (klass),
1157                               G_SIGNAL_RUN_LAST,
1158                               0,
1159                               NULL, NULL,
1160                               _empathy_marshal_VOID__OBJECT_UINT,
1161                               G_TYPE_NONE,
1162                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1163
1164         signals[PROPERTY_CHANGED] =
1165                 g_signal_new ("property-changed",
1166                               G_TYPE_FROM_CLASS (klass),
1167                               G_SIGNAL_RUN_LAST,
1168                               0,
1169                               NULL, NULL,
1170                               _empathy_marshal_VOID__STRING_BOXED,
1171                               G_TYPE_NONE,
1172                               2, G_TYPE_STRING, G_TYPE_VALUE);
1173
1174         signals[DESTROY] =
1175                 g_signal_new ("destroy",
1176                               G_TYPE_FROM_CLASS (klass),
1177                               G_SIGNAL_RUN_LAST,
1178                               0,
1179                               NULL, NULL,
1180                               g_cclosure_marshal_VOID__VOID,
1181                               G_TYPE_NONE,
1182                               0);
1183
1184         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1185 }
1186
1187 static void
1188 empathy_tp_chat_init (EmpathyTpChat *chat)
1189 {
1190         EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1191                 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1192
1193         chat->priv = priv;
1194         priv->contact_monitor = NULL;
1195         priv->messages_queue = g_queue_new ();
1196         priv->pending_messages_queue = g_queue_new ();
1197 }
1198
1199 static void
1200 tp_chat_iface_init (EmpathyContactListIface *iface)
1201 {
1202         iface->add         = tp_chat_add;
1203         iface->remove      = tp_chat_remove;
1204         iface->get_members = tp_chat_get_members;
1205         iface->get_monitor = tp_chat_get_monitor;
1206 }
1207
1208 EmpathyTpChat *
1209 empathy_tp_chat_new (TpChannel *channel)
1210 {
1211         return g_object_new (EMPATHY_TYPE_TP_CHAT,
1212                              "channel", channel,
1213                              NULL);
1214 }
1215
1216 void
1217 empathy_tp_chat_close (EmpathyTpChat *chat) {
1218         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1219
1220         /* If there are still messages left, it'll come back..
1221            We loose the ordering of sent messages though */
1222         g_signal_handlers_disconnect_by_func (priv->channel,
1223                 tp_chat_invalidated_cb, chat);
1224
1225         tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1226                 "closing channel", NULL, NULL);
1227
1228         g_object_unref (priv->channel);
1229         priv->channel = NULL;
1230
1231         g_signal_emit (chat, signals[DESTROY], 0);
1232 }
1233
1234 const gchar *
1235 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1236 {
1237         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1238
1239         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1240         g_return_val_if_fail (priv->ready, NULL);
1241
1242         return priv->id;
1243 }
1244
1245 EmpathyContact *
1246 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1247 {
1248         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1249
1250         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1251
1252         return priv->remote_contact;
1253 }
1254
1255 McAccount *
1256 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1257 {
1258         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1259
1260         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1261
1262         return priv->account;
1263 }
1264
1265 TpChannel *
1266 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1267 {
1268         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1269
1270         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1271
1272         return priv->channel;
1273 }
1274
1275 gboolean
1276 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1277 {
1278         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1279
1280         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1281
1282         return priv->ready;
1283 }
1284
1285 guint
1286 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1287 {
1288         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1289
1290         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1291
1292         return priv->members_count;
1293 }
1294
1295 void
1296 empathy_tp_chat_send (EmpathyTpChat *chat,
1297                       EmpathyMessage *message)
1298 {
1299         EmpathyTpChatPriv        *priv = GET_PRIV (chat);
1300         const gchar              *message_body;
1301         TpChannelTextMessageType  message_type;
1302
1303         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1304         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1305         g_return_if_fail (priv->ready);
1306
1307         message_body = empathy_message_get_body (message);
1308         message_type = empathy_message_get_tptype (message);
1309
1310         DEBUG ("Sending message: %s", message_body);
1311         tp_cli_channel_type_text_call_send (priv->channel, -1,
1312                                             message_type,
1313                                             message_body,
1314                                             tp_chat_send_cb,
1315                                             g_object_ref (message),
1316                                             (GDestroyNotify) g_object_unref,
1317                                             G_OBJECT (chat));
1318 }
1319
1320 void
1321 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
1322                            TpChannelChatState  state)
1323 {
1324         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1325
1326         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1327         g_return_if_fail (priv->ready);
1328
1329         DEBUG ("Set state: %d", state);
1330         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1331                                                                  state,
1332                                                                  tp_chat_async_cb,
1333                                                                  "setting chat state",
1334                                                                  NULL,
1335                                                                  G_OBJECT (chat));
1336 }
1337
1338
1339 const GList *
1340 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1341 {
1342         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1343
1344         return priv->pending_messages_queue->head;
1345 }
1346
1347 static void
1348 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1349         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1350
1351         tp_cli_channel_type_text_call_acknowledge_pending_messages (
1352                 priv->channel, -1, ids, tp_chat_async_cb,
1353                 "acknowledging received message", NULL, G_OBJECT (chat));
1354 }
1355
1356 void
1357 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1358                                      EmpathyMessage *message) {
1359         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1360         GArray *message_ids;
1361         GList *m;
1362         guint id;
1363
1364         if (empathy_message_get_sender (message) == priv->user)
1365                 goto out;
1366
1367         message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1368
1369         id = empathy_message_get_id (message);
1370         g_array_append_val (message_ids, id);
1371         acknowledge_messages (chat, message_ids);
1372         g_array_free (message_ids, TRUE);
1373
1374 out:
1375         m = g_queue_find (priv->pending_messages_queue, message);
1376         g_assert (m != NULL);
1377         g_queue_delete_link (priv->pending_messages_queue, m);
1378         g_object_unref (message);
1379 }
1380
1381 void
1382 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1383                                       const GList *messages) {
1384         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1385         /* Copy messages as the messges list (probably is) our own */
1386         GList *msgs = g_list_copy ((GList *) messages);
1387         GList *l;
1388         guint length;
1389         GArray *message_ids;
1390
1391         length = g_list_length ((GList *)messages);
1392
1393         if (length == 0)
1394                 return;
1395
1396         message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1397
1398         for (l = msgs; l != NULL; l = g_list_next (l)) {
1399                 GList *m;
1400
1401                 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1402
1403                 m = g_queue_find (priv->pending_messages_queue, message);
1404                 g_assert (m != NULL);
1405                 g_queue_delete_link (priv->pending_messages_queue, m);
1406
1407                 if (empathy_message_get_sender (message) != priv->user) {
1408                         guint id = empathy_message_get_id (message);
1409                         g_array_append_val (message_ids, id);
1410                 }
1411         }
1412
1413         if (message_ids->len > 0)
1414                 acknowledge_messages (chat, message_ids);
1415
1416         g_array_free (message_ids, TRUE);
1417         g_list_free (msgs);
1418 }