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