]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
Add a 'ready' property telling if the object is ready to use.
[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-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-debug.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
37
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39                        EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
40
41 #define DEBUG_DOMAIN "TpChat"
42
43 struct _EmpathyTpChatPriv {
44         EmpathyContactFactory *factory;
45         EmpathyContact        *user;
46         EmpathyContact        *remote_contact;
47         EmpathyTpGroup        *group;
48         McAccount             *account;
49         TpChannel             *channel;
50         gchar                 *id;
51         MissionControl        *mc;
52         gboolean               acknowledge;
53         TpChan                *tp_chan;
54         gboolean               had_pending_messages;
55         GSList                *message_queue;
56         gboolean               had_properties_list;
57         GPtrArray             *properties;
58         gboolean               ready;
59 };
60
61 typedef struct {
62         gchar          *name;
63         guint           id;
64         TpPropertyFlags flags;
65         GValue         *value;
66 } TpChatProperty;
67
68 static void empathy_tp_chat_class_init (EmpathyTpChatClass      *klass);
69 static void empathy_tp_chat_init       (EmpathyTpChat           *chat);
70 static void tp_chat_iface_init         (EmpathyContactListIface *iface);
71
72 enum {
73         PROP_0,
74         PROP_ACCOUNT,
75         PROP_TP_CHAN,
76         PROP_CHANNEL,
77         PROP_ACKNOWLEDGE,
78         PROP_REMOTE_CONTACT,
79         PROP_READY,
80 };
81
82 enum {
83         MESSAGE_RECEIVED,
84         SEND_ERROR,
85         CHAT_STATE_CHANGED,
86         PROPERTY_CHANGED,
87         DESTROY,
88         LAST_SIGNAL
89 };
90
91 static guint signals[LAST_SIGNAL];
92
93 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
94                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
95                                                 tp_chat_iface_init));
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         empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
107
108         g_object_unref (priv->channel);
109         g_object_unref (priv->tp_chan);
110         priv->channel = NULL;
111         priv->tp_chan = NULL;
112
113         g_signal_emit (chat, signals[DESTROY], 0);
114 }
115
116 static void
117 tp_chat_async_cb (TpChannel *proxy,
118                   const GError *error,
119                   gpointer user_data,
120                   GObject *weak_object)
121 {
122         if (error) {
123                 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
124                                user_data, error->message);
125         }
126 }
127
128 static void
129 tp_chat_member_added_cb (EmpathyTpGroup *group,
130                          EmpathyContact *contact,
131                          EmpathyContact *actor,
132                          guint           reason,
133                          const gchar    *message,
134                          EmpathyTpChat  *chat)
135 {
136         g_signal_emit_by_name (chat, "members-changed",
137                                contact, actor, reason, message,
138                                TRUE);
139 }
140
141 static void
142 tp_chat_member_removed_cb (EmpathyTpGroup *group,
143                            EmpathyContact *contact,
144                            EmpathyContact *actor,
145                            guint           reason,
146                            const gchar    *message,
147                            EmpathyTpChat  *chat)
148 {
149         g_signal_emit_by_name (chat, "members-changed",
150                                contact, actor, reason, message,
151                                FALSE);
152 }
153 static void
154 tp_chat_local_pending_cb  (EmpathyTpGroup *group,
155                            EmpathyContact *contact,
156                            EmpathyContact *actor,
157                            guint           reason,
158                            const gchar    *message,
159                            EmpathyTpChat  *chat)
160 {
161         g_signal_emit_by_name (chat, "pendings-changed",
162                                contact, actor, reason, message,
163                                TRUE);
164 }
165
166 static void
167 tp_chat_add (EmpathyContactList *list,
168              EmpathyContact     *contact,
169              const gchar        *message)
170 {
171         EmpathyTpChatPriv *priv = GET_PRIV (list);
172
173         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
174         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
175
176         if (priv->group) {
177                 empathy_tp_group_add_member (priv->group, contact, message);
178         }
179 }
180
181 static void
182 tp_chat_remove (EmpathyContactList *list,
183                 EmpathyContact     *contact,
184                 const gchar        *message)
185 {
186         EmpathyTpChatPriv *priv = GET_PRIV (list);
187
188         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
189         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
190
191         if (priv->group) {
192                 empathy_tp_group_remove_member (priv->group, contact, message);
193         }
194 }
195
196 static GList *
197 tp_chat_get_members (EmpathyContactList *list)
198 {
199         EmpathyTpChatPriv *priv = GET_PRIV (list);
200         GList             *members = NULL;
201
202         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
203
204         if (priv->group) {
205                 members = empathy_tp_group_get_members (priv->group);
206         } else {
207                 members = g_list_prepend (members, g_object_ref (priv->user));
208                 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
209         }
210
211         return members;
212 }
213
214 static EmpathyMessage *
215 tp_chat_build_message (EmpathyTpChat *chat,
216                        guint          type,
217                        guint          timestamp,
218                        guint          from_handle,
219                        const gchar   *message_body)
220 {
221         EmpathyTpChatPriv *priv;
222         EmpathyMessage    *message;
223         EmpathyContact    *sender;
224
225         priv = GET_PRIV (chat);
226
227         if (from_handle == 0) {
228                 sender = g_object_ref (priv->user);
229         } else {
230                 sender = empathy_contact_factory_get_from_handle (priv->factory,
231                                                                   priv->account,
232                                                                   from_handle);
233         }
234
235         message = empathy_message_new (message_body);
236         empathy_message_set_type (message, type);
237         empathy_message_set_sender (message, sender);
238         empathy_message_set_receiver (message, priv->user);
239         empathy_message_set_timestamp (message, timestamp);
240
241         g_object_unref (sender);
242
243         return message;
244 }
245
246 static void
247 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
248                                 GParamSpec     *param_spec,
249                                 EmpathyTpChat  *chat)
250 {
251         EmpathyTpChatPriv   *priv = GET_PRIV (chat);
252         EmpathyMessage      *message;
253         EmpathyContactReady  ready;
254         EmpathyContact      *sender;
255         gboolean             removed = FALSE;
256
257         /* Emit all messages queued until we find a message with not
258          * ready sender. When leaving this loop, sender is the first not ready
259          * contact queued and removed tells if at least one message got removed
260          * from the queue. */
261         while (priv->message_queue) {
262                 message = priv->message_queue->data;
263                 sender = empathy_message_get_sender (message);
264                 ready = empathy_contact_get_ready (sender);
265
266                 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
267                         break;
268                 }
269
270                 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
271                 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
272                 priv->message_queue = g_slist_remove (priv->message_queue,
273                                                       message);
274                 g_object_unref (message);
275                 removed = TRUE;
276         }
277
278         if (removed) {
279                 g_signal_handlers_disconnect_by_func (contact,
280                                                       tp_chat_sender_ready_notify_cb,
281                                                       chat);
282
283                 if (priv->message_queue) {
284                         g_signal_connect (sender, "notify::ready",
285                                           G_CALLBACK (tp_chat_sender_ready_notify_cb),
286                                           chat);
287                 }
288         }
289 }
290
291 static void
292 tp_chat_emit_or_queue_message (EmpathyTpChat  *chat,
293                                EmpathyMessage *message)
294 {
295         EmpathyTpChatPriv   *priv = GET_PRIV (chat);
296         EmpathyContact      *sender;
297         EmpathyContactReady  ready;
298
299         if (priv->message_queue != NULL) {
300                 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
301                 priv->message_queue = g_slist_append (priv->message_queue,
302                                                       g_object_ref (message));
303                 return;
304         }
305
306         sender = empathy_message_get_sender (message);
307         ready = empathy_contact_get_ready (sender);
308         if (ready & EMPATHY_CONTACT_READY_NAME) {
309                 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
310                 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
311                 return;
312         }
313
314         empathy_debug (DEBUG_DOMAIN, "Sender not ready");
315         priv->message_queue = g_slist_append (priv->message_queue, 
316                                               g_object_ref (message));
317         g_signal_connect (sender, "notify::ready",
318                           G_CALLBACK (tp_chat_sender_ready_notify_cb),
319                           chat);
320 }
321
322 static void
323 tp_chat_received_cb (TpChannel   *channel,
324                      guint        message_id,
325                      guint        timestamp,
326                      guint        from_handle,
327                      guint        message_type,
328                      guint        message_flags,
329                      const gchar *message_body,
330                      gpointer     user_data,
331                      GObject     *chat)
332 {
333         EmpathyTpChatPriv *priv = GET_PRIV (chat);
334         EmpathyMessage    *message;
335
336         if (!priv->had_pending_messages) {
337                 return;
338         }
339  
340         empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
341
342         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
343                                          message_type,
344                                          timestamp,
345                                          from_handle,
346                                          message_body);
347
348         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
349         g_object_unref (message);
350
351         if (priv->acknowledge) {
352                 GArray *message_ids;
353
354                 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
355                 g_array_append_val (message_ids, message_id);
356                 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
357                                                                             -1,
358                                                                             message_ids,
359                                                                             tp_chat_async_cb,
360                                                                             "acknowledging received message",
361                                                                             NULL,
362                                                                             chat);
363                 g_array_free (message_ids, TRUE);
364         }
365 }
366
367 static void
368 tp_chat_sent_cb (TpChannel   *channel,
369                  guint        timestamp,
370                  guint        message_type,
371                  const gchar *message_body,
372                  gpointer     user_data,
373                  GObject     *chat)
374 {
375         EmpathyMessage *message;
376
377         empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
378
379         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
380                                          message_type,
381                                          timestamp,
382                                          0,
383                                          message_body);
384
385         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
386         g_object_unref (message);
387 }
388
389 static void
390 tp_chat_send_error_cb (TpChannel   *channel,
391                        guint        error_code,
392                        guint        timestamp,
393                        guint        message_type,
394                        const gchar *message_body,
395                        gpointer     user_data,
396                        GObject     *chat)
397 {
398         EmpathyMessage *message;
399
400         empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
401                        message_body, error_code);
402
403         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
404                                          message_type,
405                                          timestamp,
406                                          0,
407                                          message_body);
408
409         g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
410         g_object_unref (message);
411 }
412
413 static void
414 tp_chat_state_changed_cb (TpChannel *channel,
415                           guint      handle,
416                           guint      state,
417                           gpointer   user_data,
418                           GObject   *chat)
419 {
420         EmpathyTpChatPriv *priv = GET_PRIV (chat);
421         EmpathyContact    *contact;
422
423         contact = empathy_contact_factory_get_from_handle (priv->factory,
424                                                            priv->account,
425                                                            handle);
426
427         empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
428                       empathy_contact_get_name (contact),
429                       handle, state);
430
431         g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
432         g_object_unref (contact);
433 }
434
435 static void
436 tp_chat_list_pending_messages_cb (TpChannel       *channel,
437                                   const GPtrArray *messages_list,
438                                   const GError    *error,
439                                   gpointer         user_data,
440                                   GObject         *chat)
441 {
442         EmpathyTpChatPriv *priv = GET_PRIV (chat);
443         guint              i;
444         GArray            *message_ids = NULL;
445
446         priv->had_pending_messages = TRUE;
447
448         if (error) {
449                 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
450                                error->message);
451                 return;
452         }
453
454         if (priv->acknowledge) {
455                 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
456                                                  messages_list->len);
457         }
458
459         for (i = 0; i < messages_list->len; i++) {
460                 EmpathyMessage *message;
461                 GValueArray    *message_struct;
462                 const gchar    *message_body;
463                 guint           message_id;
464                 guint           timestamp;
465                 guint           from_handle;
466                 guint           message_type;
467                 guint           message_flags;
468
469                 message_struct = g_ptr_array_index (messages_list, i);
470
471                 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
472                 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
473                 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
474                 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
475                 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
476                 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
477
478                 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
479
480                 if (message_ids) {
481                         g_array_append_val (message_ids, message_id);
482                 }
483
484                 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
485                                                  message_type,
486                                                  timestamp,
487                                                  from_handle,
488                                                  message_body);
489
490                 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
491                 g_object_unref (message);
492         }
493
494         if (message_ids) {
495                 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
496                                                                             -1,
497                                                                             message_ids,
498                                                                             tp_chat_async_cb,
499                                                                             "acknowledging pending messages",
500                                                                             NULL,
501                                                                             chat);
502                 g_array_free (message_ids, TRUE);
503         }
504 }
505
506 static void
507 tp_chat_property_flags_changed_cb (TpProxy         *proxy,
508                                    const GPtrArray *properties,
509                                    gpointer         user_data,
510                                    GObject         *chat)
511 {
512         EmpathyTpChatPriv *priv = GET_PRIV (chat);
513         guint              i, j;
514
515         if (!priv->had_properties_list || !properties) {
516                 return;
517         }
518
519         for (i = 0; i < properties->len; i++) {
520                 GValueArray    *prop_struct;
521                 TpChatProperty *property;
522                 guint           id;
523                 guint           flags;
524
525                 prop_struct = g_ptr_array_index (properties, i);
526                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
527                 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
528
529                 for (j = 0; j < priv->properties->len; j++) {
530                         property = g_ptr_array_index (priv->properties, j);
531                         if (property->id == id) {
532                                 property->flags = flags;
533                                 empathy_debug (DEBUG_DOMAIN,
534                                                "property %s flags changed: %d",
535                                                property->name, property->flags);
536                                 break;
537                         }
538                 }
539         }
540 }
541
542 static void
543 tp_chat_properties_changed_cb (TpProxy         *proxy,
544                                const GPtrArray *properties,
545                                gpointer         user_data,
546                                GObject         *chat)
547 {
548         EmpathyTpChatPriv *priv = GET_PRIV (chat);
549         guint              i, j;
550
551         if (!priv->had_properties_list || !properties) {
552                 return;
553         }
554
555         for (i = 0; i < properties->len; i++) {
556                 GValueArray    *prop_struct;
557                 TpChatProperty *property;
558                 guint           id;
559                 GValue         *src_value;
560
561                 prop_struct = g_ptr_array_index (properties, i);
562                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
563                 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
564
565                 for (j = 0; j < priv->properties->len; j++) {
566                         property = g_ptr_array_index (priv->properties, j);
567                         if (property->id == id) {
568                                 if (property->value) {
569                                         g_value_copy (src_value, property->value);
570                                 } else {
571                                         property->value = tp_g_value_slice_dup (src_value);
572                                 }
573
574                                 empathy_debug (DEBUG_DOMAIN, "property %s changed",
575                                                property->name);
576                                 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
577                                                property->name, property->value);
578                                 break;
579                         }
580                 }
581         }
582 }
583
584 static void
585 tp_chat_get_properties_cb (TpProxy         *proxy,
586                            const GPtrArray *properties,
587                            const GError    *error,
588                            gpointer         user_data,
589                            GObject         *chat)
590 {
591         if (error) {
592                 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
593                                error->message);
594                 return;
595         }
596
597         tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
598 }
599
600 static void
601 tp_chat_list_properties_cb (TpProxy         *proxy,
602                             const GPtrArray *properties,
603                             const GError    *error,
604                             gpointer         user_data,
605                             GObject         *chat)
606 {
607         EmpathyTpChatPriv *priv = GET_PRIV (chat);
608         GArray            *ids;
609         guint              i;
610
611         priv->had_properties_list = TRUE;
612
613         if (error) {
614                 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
615                                error->message);
616                 return;
617         }
618
619         ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
620         priv->properties = g_ptr_array_sized_new (properties->len);
621         for (i = 0; i < properties->len; i++) {
622                 GValueArray    *prop_struct;
623                 TpChatProperty *property;
624
625                 prop_struct = g_ptr_array_index (properties, i);
626                 property = g_slice_new0 (TpChatProperty);
627                 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
628                 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
629                 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
630
631                 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
632                                property->name, property->id, property->flags);
633                 g_ptr_array_add (priv->properties, property);
634                 if (property->flags & TP_PROPERTY_FLAG_READ) {
635                         g_array_append_val (ids, property->id);
636                 }
637         }
638
639         tp_cli_properties_interface_call_get_properties (proxy, -1,
640                                                          ids,
641                                                          tp_chat_get_properties_cb,
642                                                          NULL, NULL,
643                                                          chat);
644
645         g_array_free (ids, TRUE);
646 }
647
648 void
649 empathy_tp_chat_set_property (EmpathyTpChat *chat,
650                               const gchar   *name,
651                               const GValue  *value)
652 {
653         EmpathyTpChatPriv *priv = GET_PRIV (chat);
654         TpChatProperty    *property;
655         guint              i;
656
657         g_return_if_fail (priv->ready);
658
659         for (i = 0; i < priv->properties->len; i++) {
660                 property = g_ptr_array_index (priv->properties, i);
661                 if (!tp_strdiff (property->name, name)) {
662                         GPtrArray   *properties;
663                         GValueArray *prop;
664                         GValue       id = {0, };
665                         GValue       dest_value = {0, };
666
667                         if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
668                                 break;
669                         }
670
671                         g_value_init (&id, G_TYPE_UINT);
672                         g_value_init (&dest_value, G_TYPE_VALUE);
673                         g_value_set_uint (&id, property->id);
674                         g_value_set_boxed (&dest_value, value);
675
676                         prop = g_value_array_new (2);
677                         g_value_array_append (prop, &id);
678                         g_value_array_append (prop, &dest_value);
679
680                         properties = g_ptr_array_sized_new (1);
681                         g_ptr_array_add (properties, prop);
682
683                         empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
684                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
685                                                                          properties,
686                                                                          (tp_cli_properties_interface_callback_for_set_properties)
687                                                                          tp_chat_async_cb,
688                                                                          "Seting property", NULL,
689                                                                          G_OBJECT (chat));
690
691                         g_ptr_array_free (properties, TRUE);
692                         g_value_array_free (prop);
693
694                         break;
695                 }
696         }
697 }
698
699 static void
700 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
701 {
702         EmpathyTpChatPriv *priv = GET_PRIV (chat);
703
704         empathy_debug (DEBUG_DOMAIN, "Channel ready");
705
706         priv->ready = TRUE;
707         if (tp_proxy_has_interface_by_id (priv->channel,
708                                           TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
709                 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
710
711                 g_signal_connect (priv->group, "member-added",
712                                   G_CALLBACK (tp_chat_member_added_cb),
713                                   chat);
714                 g_signal_connect (priv->group, "member-removed",
715                                   G_CALLBACK (tp_chat_member_removed_cb),
716                                   chat);
717                 g_signal_connect (priv->group, "local-pending",
718                                   G_CALLBACK (tp_chat_local_pending_cb),
719                                   chat);
720         } else {
721                 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
722                                                                                 priv->account,
723                                                                                 priv->tp_chan->handle);
724                 g_object_notify (G_OBJECT (chat), "remote-contact");
725         }
726         
727         if (tp_proxy_has_interface_by_id (priv->channel,
728                                           TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
729                 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
730                                                                   tp_chat_list_properties_cb,
731                                                                   NULL, NULL,
732                                                                   G_OBJECT (chat));
733                 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
734                                                                            tp_chat_properties_changed_cb,
735                                                                            NULL, NULL,
736                                                                            G_OBJECT (chat), NULL);
737                 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
738                                                                                tp_chat_property_flags_changed_cb,
739                                                                                NULL, NULL,
740                                                                                G_OBJECT (chat), NULL);
741         }
742
743         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
744                                                              priv->acknowledge,
745                                                              tp_chat_list_pending_messages_cb,
746                                                              NULL, NULL,
747                                                              G_OBJECT (chat));
748
749         tp_cli_channel_type_text_connect_to_received (priv->channel,
750                                                       tp_chat_received_cb,
751                                                       NULL, NULL,
752                                                       G_OBJECT (chat), NULL);
753         tp_cli_channel_type_text_connect_to_sent (priv->channel,
754                                                   tp_chat_sent_cb,
755                                                   NULL, NULL,
756                                                   G_OBJECT (chat), NULL);
757         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
758                                                         tp_chat_send_error_cb,
759                                                         NULL, NULL,
760                                                         G_OBJECT (chat), NULL);
761         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
762                                                                            tp_chat_state_changed_cb,
763                                                                            NULL, NULL,
764                                                                            G_OBJECT (chat), NULL);
765         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
766                                                                            tp_chat_state_changed_cb,
767                                                                            NULL, NULL,
768                                                                            G_OBJECT (chat), NULL);
769 }
770
771 static void
772 tp_chat_finalize (GObject *object)
773 {
774         EmpathyTpChatPriv *priv = GET_PRIV (object);
775         guint              i;
776
777         if (priv->acknowledge && priv->channel) {
778                 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
779                 tp_cli_channel_call_close (priv->channel, -1,
780                                            tp_chat_async_cb,
781                                            "closing channel", NULL,
782                                            NULL);
783         }
784
785         if (priv->channel) {
786                 g_signal_handlers_disconnect_by_func (priv->channel,
787                                                       tp_chat_invalidated_cb,
788                                                       object);
789                 g_object_unref (priv->channel);
790         }
791         if (priv->tp_chan) {
792                 g_object_unref (priv->tp_chan);
793         }
794
795         if (priv->properties) {
796                 for (i = 0; i < priv->properties->len; i++) {
797                         TpChatProperty *property;
798
799                         property = g_ptr_array_index (priv->properties, i);
800                         g_free (property->name);
801                         if (property->value) {
802                                 tp_g_value_slice_free (property->value);
803                         }
804                         g_slice_free (TpChatProperty, property);
805                 }
806                 g_ptr_array_free (priv->properties, TRUE);
807         }
808
809         if (priv->remote_contact) {
810                 g_object_unref (priv->remote_contact);
811         }
812         if (priv->group) {
813                 g_object_unref (priv->group);
814         }
815
816         g_object_unref (priv->factory);
817         g_object_unref (priv->user);
818         g_object_unref (priv->account);
819         g_object_unref (priv->mc);
820         g_free (priv->id);
821
822         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
823 }
824
825 static GObject *
826 tp_chat_constructor (GType                  type,
827                      guint                  n_props,
828                      GObjectConstructParam *props)
829 {
830         GObject           *chat;
831         EmpathyTpChatPriv *priv;
832         gboolean           channel_ready;
833
834         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
835
836         priv = GET_PRIV (chat);
837         priv->factory = empathy_contact_factory_new ();
838         priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
839         priv->mc = empathy_mission_control_new ();
840
841         g_signal_connect (priv->channel, "invalidated",
842                           G_CALLBACK (tp_chat_invalidated_cb),
843                           chat);
844
845         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
846         if (channel_ready) {
847                 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
848         } else {
849                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
850                                           G_CALLBACK (tp_chat_channel_ready_cb),
851                                           chat);
852         }
853
854         return chat;
855 }
856
857 static void
858 tp_chat_get_property (GObject    *object,
859                       guint       param_id,
860                       GValue     *value,
861                       GParamSpec *pspec)
862 {
863         EmpathyTpChatPriv *priv = GET_PRIV (object);
864
865         switch (param_id) {
866         case PROP_ACCOUNT:
867                 g_value_set_object (value, priv->account);
868                 break;
869         case PROP_TP_CHAN:
870                 g_value_set_object (value, priv->tp_chan);
871                 break;
872         case PROP_CHANNEL:
873                 g_value_set_object (value, priv->channel);
874                 break;
875         case PROP_ACKNOWLEDGE:
876                 g_value_set_boolean (value, priv->acknowledge);
877                 break;
878         case PROP_REMOTE_CONTACT:
879                 g_value_set_object (value, priv->remote_contact);
880                 break;
881         default:
882                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
883                 break;
884         };
885 }
886
887 static void
888 tp_chat_set_property (GObject      *object,
889                       guint         param_id,
890                       const GValue *value,
891                       GParamSpec   *pspec)
892 {
893         EmpathyTpChatPriv *priv = GET_PRIV (object);
894
895         switch (param_id) {
896         case PROP_ACCOUNT:
897                 priv->account = g_object_ref (g_value_get_object (value));
898                 break;
899         case PROP_TP_CHAN:
900                 priv->tp_chan = g_object_ref (g_value_get_object (value));
901                 break;
902         case PROP_CHANNEL:
903                 priv->channel = g_object_ref (g_value_get_object (value));
904                 break;
905         case PROP_ACKNOWLEDGE:
906                 priv->acknowledge = g_value_get_boolean (value);
907                 break;
908         default:
909                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
910                 break;
911         };
912 }
913
914 static void
915 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
916 {
917         GObjectClass *object_class = G_OBJECT_CLASS (klass);
918
919         object_class->finalize = tp_chat_finalize;
920         object_class->constructor = tp_chat_constructor;
921         object_class->get_property = tp_chat_get_property;
922         object_class->set_property = tp_chat_set_property;
923
924         /* Construct properties */
925         g_object_class_install_property (object_class,
926                                          PROP_ACCOUNT,
927                                          g_param_spec_object ("account",
928                                                               "channel Account",
929                                                               "The account associated with the channel",
930                                                               MC_TYPE_ACCOUNT,
931                                                               G_PARAM_READWRITE |
932                                                               G_PARAM_CONSTRUCT_ONLY));
933         g_object_class_install_property (object_class,
934                                          PROP_TP_CHAN,
935                                          g_param_spec_object ("tp-chan",
936                                                               "telepathy channel",
937                                                               "The text channel for the chat",
938                                                               TELEPATHY_CHAN_TYPE,
939                                                               G_PARAM_READWRITE |
940                                                               G_PARAM_CONSTRUCT_ONLY));
941         g_object_class_install_property (object_class,
942                                          PROP_CHANNEL,
943                                          g_param_spec_object ("channel",
944                                                               "telepathy channel",
945                                                               "The text channel for the chat",
946                                                               TP_TYPE_CHANNEL,
947                                                               G_PARAM_READWRITE |
948                                                               G_PARAM_CONSTRUCT_ONLY));
949         g_object_class_install_property (object_class,
950                                          PROP_ACKNOWLEDGE,
951                                          g_param_spec_boolean ("acknowledge",
952                                                                "acknowledge messages",
953                                                                "Wheter or not received messages should be acknowledged",
954                                                                FALSE,
955                                                                G_PARAM_READWRITE |
956                                                                G_PARAM_CONSTRUCT));
957         g_object_class_install_property (object_class,
958                                          PROP_REMOTE_CONTACT,
959                                          g_param_spec_object ("remote-contact",
960                                                               "The remote contact",
961                                                               "The remote contact if there is no group iface on the channel",
962                                                               EMPATHY_TYPE_CONTACT,
963                                                               G_PARAM_READABLE));
964         g_object_class_install_property (object_class,
965                                          PROP_READY,
966                                          g_param_spec_boolean ("ready",
967                                                                "Is the object ready",
968                                                                "This object is can't be used until this becomes true",
969                                                                FALSE,
970                                                                G_PARAM_READABLE));
971
972         /* Signals */
973         signals[MESSAGE_RECEIVED] =
974                 g_signal_new ("message-received",
975                               G_TYPE_FROM_CLASS (klass),
976                               G_SIGNAL_RUN_LAST,
977                               0,
978                               NULL, NULL,
979                               g_cclosure_marshal_VOID__OBJECT,
980                               G_TYPE_NONE,
981                               1, EMPATHY_TYPE_MESSAGE);
982
983         signals[SEND_ERROR] =
984                 g_signal_new ("send-error",
985                               G_TYPE_FROM_CLASS (klass),
986                               G_SIGNAL_RUN_LAST,
987                               0,
988                               NULL, NULL,
989                               _empathy_marshal_VOID__OBJECT_UINT,
990                               G_TYPE_NONE,
991                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
992
993         signals[CHAT_STATE_CHANGED] =
994                 g_signal_new ("chat-state-changed",
995                               G_TYPE_FROM_CLASS (klass),
996                               G_SIGNAL_RUN_LAST,
997                               0,
998                               NULL, NULL,
999                               _empathy_marshal_VOID__OBJECT_UINT,
1000                               G_TYPE_NONE,
1001                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1002
1003         signals[PROPERTY_CHANGED] =
1004                 g_signal_new ("property-changed",
1005                               G_TYPE_FROM_CLASS (klass),
1006                               G_SIGNAL_RUN_LAST,
1007                               0,
1008                               NULL, NULL,
1009                               _empathy_marshal_VOID__STRING_BOXED,
1010                               G_TYPE_NONE,
1011                               2, G_TYPE_STRING, G_TYPE_VALUE);
1012
1013         signals[DESTROY] =
1014                 g_signal_new ("destroy",
1015                               G_TYPE_FROM_CLASS (klass),
1016                               G_SIGNAL_RUN_LAST,
1017                               0,
1018                               NULL, NULL,
1019                               g_cclosure_marshal_VOID__VOID,
1020                               G_TYPE_NONE,
1021                               0);
1022
1023         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1024 }
1025
1026 static void
1027 empathy_tp_chat_init (EmpathyTpChat *chat)
1028 {
1029 }
1030
1031 static void
1032 tp_chat_iface_init (EmpathyContactListIface *iface)
1033 {
1034         iface->add         = tp_chat_add;
1035         iface->remove      = tp_chat_remove;
1036         iface->get_members = tp_chat_get_members;
1037 }
1038
1039 EmpathyTpChat *
1040 empathy_tp_chat_new (McAccount *account,
1041                      TpChan    *tp_chan,
1042                      gboolean   acknowledge)
1043 {
1044         EmpathyTpChat  *chat;
1045         TpChannel      *channel;
1046         TpConnection   *connection;
1047         MissionControl *mc;
1048         TpConn         *tp_conn;
1049
1050         mc = empathy_mission_control_new ();
1051         tp_conn = mission_control_get_connection (mc, account, NULL);
1052         connection = tp_conn_dup_connection (tp_conn);
1053         channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1054
1055         chat = g_object_new (EMPATHY_TYPE_TP_CHAT, 
1056                              "account", account,
1057                              "channel", channel,
1058                              "tp-chan", tp_chan,
1059                              "acknowledge", acknowledge,
1060                              NULL);
1061
1062         g_object_unref (channel);
1063         g_object_unref (tp_conn);
1064         g_object_unref (connection);
1065         g_object_unref (mc);
1066
1067         return chat;
1068 }
1069
1070 EmpathyTpChat *
1071 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1072 {
1073         EmpathyTpChat  *chat;
1074         MissionControl *mc;
1075         McAccount      *account;
1076         TpConn         *tp_conn;
1077         TpChan         *text_chan;
1078         const gchar    *bus_name;
1079         guint           handle;
1080
1081         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1082
1083         mc = empathy_mission_control_new ();
1084         account = empathy_contact_get_account (contact);
1085
1086         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1087                 /* The account is not connected. */
1088                 return NULL;
1089         }
1090
1091         tp_conn = mission_control_get_connection (mc, account, NULL);
1092         g_return_val_if_fail (tp_conn != NULL, NULL);
1093         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1094         handle = empathy_contact_get_handle (contact);
1095
1096         text_chan = tp_conn_new_channel (tp_get_bus (),
1097                                          tp_conn,
1098                                          bus_name,
1099                                          TP_IFACE_CHANNEL_TYPE_TEXT,
1100                                          TP_HANDLE_TYPE_CONTACT,
1101                                          handle,
1102                                          TRUE);
1103
1104         chat = empathy_tp_chat_new (account, text_chan, TRUE);
1105
1106         g_object_unref (tp_conn);
1107         g_object_unref (text_chan);
1108         g_object_unref (mc);
1109
1110         return chat;
1111 }
1112
1113 TpChan *
1114 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1115 {
1116         EmpathyTpChatPriv *priv;
1117
1118         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1119
1120         priv = GET_PRIV (chat);
1121
1122         return priv->tp_chan;
1123 }
1124
1125 McAccount *
1126 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1127 {
1128         EmpathyTpChatPriv *priv;
1129
1130         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1131
1132         priv = GET_PRIV (chat);
1133
1134         return priv->account;
1135 }
1136
1137 void
1138 empathy_tp_chat_send (EmpathyTpChat *chat,
1139                       EmpathyMessage *message)
1140 {
1141         EmpathyTpChatPriv  *priv = GET_PRIV (chat);
1142         const gchar        *message_body;
1143         EmpathyMessageType  message_type;
1144
1145         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1146         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1147         g_return_if_fail (priv->ready);
1148
1149         message_body = empathy_message_get_body (message);
1150         message_type = empathy_message_get_type (message);
1151
1152         empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1153         tp_cli_channel_type_text_call_send (priv->channel, -1,
1154                                             message_type,
1155                                             message_body,
1156                                             tp_chat_async_cb,
1157                                             "sending message", NULL,
1158                                             G_OBJECT (chat));
1159 }
1160
1161 void
1162 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
1163                            TpChannelChatState  state)
1164 {
1165         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1166
1167         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1168         g_return_if_fail (priv->ready);
1169
1170         empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1171         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1172                                                                  state,
1173                                                                  tp_chat_async_cb,
1174                                                                  "setting chat state",
1175                                                                  NULL,
1176                                                                  G_OBJECT (chat));
1177 }
1178
1179 const gchar *
1180 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1181 {
1182         EmpathyTpChatPriv *priv;
1183
1184         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1185
1186         priv = GET_PRIV (chat);
1187
1188         if (!priv->id) {
1189                 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1190         }
1191
1192         return priv->id;
1193 }
1194
1195 EmpathyContact *
1196 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1197 {
1198         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1199
1200         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1201
1202         return priv->remote_contact;
1203 }
1204
1205 gboolean
1206 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1207 {
1208         EmpathyTpChatPriv *priv = GET_PRIV (chat);
1209
1210         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1211
1212         return priv->ready;
1213 }
1214