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