]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
Make use of tp-glib helpers func for GValue
[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_copy (src_value, property->value);
454                                 } else {
455                                         property->value = tp_g_value_slice_dup (src_value);
456                                 }
457
458                                 empathy_debug (DEBUG_DOMAIN, "property %s changed",
459                                                property->name);
460                                 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
461                                                property->name, property->value);
462                                 break;
463                         }
464                 }
465         }
466 }
467
468 static void
469 tp_chat_get_properties_cb (TpProxy         *proxy,
470                            const GPtrArray *properties,
471                            const GError    *error,
472                            gpointer         user_data,
473                            GObject         *chat)
474 {
475         if (error) {
476                 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
477                                error->message);
478                 return;
479         }
480
481         tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
482 }
483
484 static void
485 tp_chat_list_properties_cb (TpProxy         *proxy,
486                             const GPtrArray *properties,
487                             const GError    *error,
488                             gpointer         user_data,
489                             GObject         *chat)
490 {
491         EmpathyTpChatPriv *priv = GET_PRIV (chat);
492         GArray            *ids;
493         guint              i;
494
495         priv->had_properties_list = TRUE;
496
497         if (error) {
498                 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
499                                error->message);
500                 return;
501         }
502
503         ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
504         priv->properties = g_ptr_array_sized_new (properties->len);
505         for (i = 0; i < properties->len; i++) {
506                 GValueArray    *prop_struct;
507                 TpChatProperty *property;
508
509                 prop_struct = g_ptr_array_index (properties, i);
510                 property = g_slice_new0 (TpChatProperty);
511                 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
512                 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
513                 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
514
515                 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
516                                property->name, property->id, property->flags);
517                 g_ptr_array_add (priv->properties, property);
518                 if (property->flags & TP_PROPERTY_FLAG_READ) {
519                         g_array_append_val (ids, property->id);
520                 }
521         }
522
523         tp_cli_properties_interface_call_get_properties (proxy, -1,
524                                                          ids,
525                                                          tp_chat_get_properties_cb,
526                                                          NULL, NULL,
527                                                          chat);
528
529         g_array_free (ids, TRUE);
530 }
531
532 void
533 empathy_tp_chat_set_property (EmpathyTpChat *chat,
534                               const gchar   *name,
535                               const GValue  *value)
536 {
537         EmpathyTpChatPriv *priv = GET_PRIV (chat);
538         TpChatProperty    *property;
539         guint              i;
540
541         for (i = 0; i < priv->properties->len; i++) {
542                 property = g_ptr_array_index (priv->properties, i);
543                 if (!tp_strdiff (property->name, name)) {
544                         GPtrArray   *properties;
545                         GValueArray *prop;
546                         GValue       id = {0, };
547                         GValue       dest_value = {0, };
548
549                         if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
550                                 break;
551                         }
552
553                         g_value_init (&id, G_TYPE_UINT);
554                         g_value_init (&dest_value, G_TYPE_VALUE);
555                         g_value_set_uint (&id, property->id);
556                         g_value_set_boxed (&dest_value, value);
557
558                         prop = g_value_array_new (2);
559                         g_value_array_append (prop, &id);
560                         g_value_array_append (prop, &dest_value);
561
562                         properties = g_ptr_array_sized_new (1);
563                         g_ptr_array_add (properties, prop);
564
565                         empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
566                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
567                                                                          properties,
568                                                                          (tp_cli_properties_interface_callback_for_set_properties)
569                                                                          tp_chat_async_cb,
570                                                                          "Seting property", NULL,
571                                                                          G_OBJECT (chat));
572
573                         g_ptr_array_free (properties, TRUE);
574                         g_value_array_free (prop);
575
576                         break;
577                 }
578         }
579 }
580
581 static gboolean
582 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
583 {
584         EmpathyTpChatPriv *priv = GET_PRIV (chat);
585
586         empathy_debug (DEBUG_DOMAIN, "Channel ready");
587
588         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
589                                                              priv->acknowledge,
590                                                              tp_chat_list_pending_messages_cb,
591                                                              NULL, NULL,
592                                                              G_OBJECT (chat));
593         tp_cli_properties_interface_call_list_properties (priv->channel, -1,
594                                                           tp_chat_list_properties_cb,
595                                                           NULL, NULL,
596                                                           G_OBJECT (chat));
597
598
599         tp_cli_channel_type_text_connect_to_received (priv->channel,
600                                                       tp_chat_received_cb,
601                                                       NULL, NULL,
602                                                       G_OBJECT (chat), NULL);
603         tp_cli_channel_type_text_connect_to_sent (priv->channel,
604                                                   tp_chat_sent_cb,
605                                                   NULL, NULL,
606                                                   G_OBJECT (chat), NULL);
607         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
608                                                         tp_chat_send_error_cb,
609                                                         NULL, NULL,
610                                                         G_OBJECT (chat), NULL);
611         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
612                                                                            tp_chat_state_changed_cb,
613                                                                            NULL, NULL,
614                                                                            G_OBJECT (chat), NULL);
615         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
616                                                                            tp_chat_state_changed_cb,
617                                                                            NULL, NULL,
618                                                                            G_OBJECT (chat), NULL);
619         tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
620                                                                    tp_chat_properties_changed_cb,
621                                                                    NULL, NULL,
622                                                                    G_OBJECT (chat), NULL);
623         tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
624                                                                        tp_chat_property_flags_changed_cb,
625                                                                        NULL, NULL,
626                                                                        G_OBJECT (chat), NULL);
627
628         return FALSE;
629 }
630
631 static void
632 tp_chat_finalize (GObject *object)
633 {
634         EmpathyTpChatPriv *priv = GET_PRIV (object);
635         guint              i;
636
637         if (priv->acknowledge && priv->channel) {
638                 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
639                 tp_cli_channel_call_close (priv->channel, -1,
640                                            tp_chat_async_cb,
641                                            "closing channel", NULL,
642                                            NULL);
643         }
644
645         if (priv->channel) {
646                 g_signal_handlers_disconnect_by_func (priv->channel,
647                                                       tp_chat_invalidated_cb,
648                                                       object);
649                 g_object_unref (priv->channel);
650         }
651         if (priv->tp_chan) {
652                 g_object_unref (priv->tp_chan);
653         }
654
655         if (priv->properties) {
656                 for (i = 0; i < priv->properties->len; i++) {
657                         TpChatProperty *property;
658
659                         property = g_ptr_array_index (priv->properties, i);
660                         g_free (property->name);
661                         if (property->value) {
662                                 tp_g_value_slice_free (property->value);
663                         }
664                         g_slice_free (TpChatProperty, property);
665                 }
666                 g_ptr_array_free (priv->properties, TRUE);
667         }
668
669         g_object_unref (priv->factory);
670         g_object_unref (priv->user);
671         g_object_unref (priv->account);
672         g_object_unref (priv->mc);
673         g_free (priv->id);
674
675         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
676 }
677
678 static GObject *
679 tp_chat_constructor (GType                  type,
680                      guint                  n_props,
681                      GObjectConstructParam *props)
682 {
683         GObject           *chat;
684         EmpathyTpChatPriv *priv;
685         gboolean           channel_ready;
686
687         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
688
689         priv = GET_PRIV (chat);
690         priv->factory = empathy_contact_factory_new ();
691         priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
692         priv->mc = empathy_mission_control_new ();
693
694         g_signal_connect (priv->channel, "invalidated",
695                           G_CALLBACK (tp_chat_invalidated_cb),
696                           chat);
697
698         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
699         if (channel_ready) {
700                 /* FIXME: We do that in a cb to let time to set the acknowledge
701                  * property, this property should be required for construct. */
702                 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
703         } else {
704                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
705                                           G_CALLBACK (tp_chat_channel_ready_cb),
706                                           chat);
707         }
708
709         return chat;
710 }
711
712 static void
713 tp_chat_get_property (GObject    *object,
714                       guint       param_id,
715                       GValue     *value,
716                       GParamSpec *pspec)
717 {
718         EmpathyTpChatPriv *priv = GET_PRIV (object);
719
720         switch (param_id) {
721         case PROP_ACCOUNT:
722                 g_value_set_object (value, priv->account);
723                 break;
724         case PROP_TP_CHAN:
725                 g_value_set_object (value, priv->tp_chan);
726                 break;
727         case PROP_CHANNEL:
728                 g_value_set_object (value, priv->channel);
729                 break;
730         case PROP_ACKNOWLEDGE:
731                 g_value_set_boolean (value, priv->acknowledge);
732                 break;
733         default:
734                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
735                 break;
736         };
737 }
738
739 static void
740 tp_chat_set_property (GObject      *object,
741                       guint         param_id,
742                       const GValue *value,
743                       GParamSpec   *pspec)
744 {
745         EmpathyTpChatPriv *priv = GET_PRIV (object);
746
747         switch (param_id) {
748         case PROP_ACCOUNT:
749                 priv->account = g_object_ref (g_value_get_object (value));
750                 break;
751         case PROP_TP_CHAN:
752                 priv->tp_chan = g_object_ref (g_value_get_object (value));
753                 break;
754         case PROP_CHANNEL:
755                 priv->channel = g_object_ref (g_value_get_object (value));
756                 break;
757         case PROP_ACKNOWLEDGE:
758                 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
759                                                  g_value_get_boolean (value));
760                 break;
761         default:
762                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
763                 break;
764         };
765 }
766
767 static void
768 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
769 {
770         GObjectClass *object_class = G_OBJECT_CLASS (klass);
771
772         object_class->finalize = tp_chat_finalize;
773         object_class->constructor = tp_chat_constructor;
774         object_class->get_property = tp_chat_get_property;
775         object_class->set_property = tp_chat_set_property;
776
777         /* Construct properties */
778         g_object_class_install_property (object_class,
779                                          PROP_ACCOUNT,
780                                          g_param_spec_object ("account",
781                                                               "channel Account",
782                                                               "The account associated with the channel",
783                                                               MC_TYPE_ACCOUNT,
784                                                               G_PARAM_READWRITE |
785                                                               G_PARAM_CONSTRUCT_ONLY));
786         g_object_class_install_property (object_class,
787                                          PROP_TP_CHAN,
788                                          g_param_spec_object ("tp-chan",
789                                                               "telepathy channel",
790                                                               "The text channel for the chat",
791                                                               TELEPATHY_CHAN_TYPE,
792                                                               G_PARAM_READWRITE |
793                                                               G_PARAM_CONSTRUCT_ONLY));
794
795         g_object_class_install_property (object_class,
796                                          PROP_CHANNEL,
797                                          g_param_spec_object ("channel",
798                                                               "telepathy channel",
799                                                               "The text channel for the chat",
800                                                               TP_TYPE_CHANNEL,
801                                                               G_PARAM_READWRITE |
802                                                               G_PARAM_CONSTRUCT_ONLY));
803
804         g_object_class_install_property (object_class,
805                                          PROP_ACKNOWLEDGE,
806                                          g_param_spec_boolean ("acknowledge",
807                                                                "acknowledge messages",
808                                                                "Wheter or not received messages should be acknowledged",
809                                                                FALSE,
810                                                                G_PARAM_READWRITE |
811                                                                G_PARAM_CONSTRUCT));
812
813         /* Signals */
814         signals[MESSAGE_RECEIVED] =
815                 g_signal_new ("message-received",
816                               G_TYPE_FROM_CLASS (klass),
817                               G_SIGNAL_RUN_LAST,
818                               0,
819                               NULL, NULL,
820                               g_cclosure_marshal_VOID__OBJECT,
821                               G_TYPE_NONE,
822                               1, EMPATHY_TYPE_MESSAGE);
823
824         signals[SEND_ERROR] =
825                 g_signal_new ("send-error",
826                               G_TYPE_FROM_CLASS (klass),
827                               G_SIGNAL_RUN_LAST,
828                               0,
829                               NULL, NULL,
830                               _empathy_marshal_VOID__OBJECT_UINT,
831                               G_TYPE_NONE,
832                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
833
834         signals[CHAT_STATE_CHANGED] =
835                 g_signal_new ("chat-state-changed",
836                               G_TYPE_FROM_CLASS (klass),
837                               G_SIGNAL_RUN_LAST,
838                               0,
839                               NULL, NULL,
840                               _empathy_marshal_VOID__OBJECT_UINT,
841                               G_TYPE_NONE,
842                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
843
844         signals[PROPERTY_CHANGED] =
845                 g_signal_new ("property-changed",
846                               G_TYPE_FROM_CLASS (klass),
847                               G_SIGNAL_RUN_LAST,
848                               0,
849                               NULL, NULL,
850                               _empathy_marshal_VOID__STRING_BOXED,
851                               G_TYPE_NONE,
852                               2, G_TYPE_STRING, G_TYPE_VALUE);
853
854         signals[DESTROY] =
855                 g_signal_new ("destroy",
856                               G_TYPE_FROM_CLASS (klass),
857                               G_SIGNAL_RUN_LAST,
858                               0,
859                               NULL, NULL,
860                               g_cclosure_marshal_VOID__VOID,
861                               G_TYPE_NONE,
862                               0);
863
864         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
865 }
866
867 static void
868 empathy_tp_chat_init (EmpathyTpChat *chat)
869 {
870 }
871
872 EmpathyTpChat *
873 empathy_tp_chat_new (McAccount *account,
874                      TpChan    *tp_chan)
875 {
876         EmpathyTpChat  *chat;
877         TpChannel      *channel;
878         TpConnection   *connection;
879         MissionControl *mc;
880         TpConn         *tp_conn;
881
882         mc = empathy_mission_control_new ();
883         tp_conn = mission_control_get_connection (mc, account, NULL);
884         connection = tp_conn_dup_connection (tp_conn);
885         channel = tp_chan_dup_channel (tp_chan, connection, NULL);
886
887         chat = g_object_new (EMPATHY_TYPE_TP_CHAT, 
888                              "account", account,
889                              "channel", channel,
890                              "tp-chan", tp_chan,
891                              NULL);
892
893         g_object_unref (channel);
894         g_object_unref (tp_conn);
895         g_object_unref (connection);
896         g_object_unref (mc);
897
898         return chat;
899 }
900
901 EmpathyTpChat *
902 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
903 {
904         EmpathyTpChat  *chat;
905         MissionControl *mc;
906         McAccount      *account;
907         TpConn         *tp_conn;
908         TpChan         *text_chan;
909         const gchar    *bus_name;
910         guint           handle;
911
912         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
913
914         mc = empathy_mission_control_new ();
915         account = empathy_contact_get_account (contact);
916
917         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
918                 /* The account is not connected. */
919                 return NULL;
920         }
921
922         tp_conn = mission_control_get_connection (mc, account, NULL);
923         g_return_val_if_fail (tp_conn != NULL, NULL);
924         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
925         handle = empathy_contact_get_handle (contact);
926
927         text_chan = tp_conn_new_channel (tp_get_bus (),
928                                          tp_conn,
929                                          bus_name,
930                                          TP_IFACE_CHANNEL_TYPE_TEXT,
931                                          TP_HANDLE_TYPE_CONTACT,
932                                          handle,
933                                          TRUE);
934
935         chat = empathy_tp_chat_new (account, text_chan);
936
937         g_object_unref (tp_conn);
938         g_object_unref (text_chan);
939         g_object_unref (mc);
940
941         return chat;
942 }
943
944 gboolean
945 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
946 {
947         EmpathyTpChatPriv *priv;
948
949         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
950
951         priv = GET_PRIV (chat);
952
953         return priv->acknowledge;
954 }
955
956 void
957 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
958                                  gboolean       acknowledge)
959 {
960         EmpathyTpChatPriv *priv;
961
962         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
963
964         priv = GET_PRIV (chat);
965
966         priv->acknowledge = acknowledge;
967         g_object_notify (G_OBJECT (chat), "acknowledge");
968 }
969
970 TpChan *
971 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
972 {
973         EmpathyTpChatPriv *priv;
974
975         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
976
977         priv = GET_PRIV (chat);
978
979         return priv->tp_chan;
980 }
981
982 McAccount *
983 empathy_tp_chat_get_account (EmpathyTpChat *chat)
984 {
985         EmpathyTpChatPriv *priv;
986
987         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
988
989         priv = GET_PRIV (chat);
990
991         return priv->account;
992 }
993
994 void
995 empathy_tp_chat_send (EmpathyTpChat *chat,
996                       EmpathyMessage *message)
997 {
998         EmpathyTpChatPriv  *priv;
999         const gchar        *message_body;
1000         EmpathyMessageType  message_type;
1001
1002         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1003         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1004
1005         priv = GET_PRIV (chat);
1006
1007         message_body = empathy_message_get_body (message);
1008         message_type = empathy_message_get_type (message);
1009
1010         empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1011         tp_cli_channel_type_text_call_send (priv->channel, -1,
1012                                             message_type,
1013                                             message_body,
1014                                             tp_chat_async_cb,
1015                                             "sending message", NULL,
1016                                             G_OBJECT (chat));
1017 }
1018
1019 void
1020 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
1021                            TpChannelChatState  state)
1022 {
1023         EmpathyTpChatPriv *priv;
1024
1025         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1026
1027         priv = GET_PRIV (chat);
1028
1029         empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1030         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1031                                                                  state,
1032                                                                  tp_chat_async_cb,
1033                                                                  "setting chat state",
1034                                                                  NULL,
1035                                                                  G_OBJECT (chat));
1036 }
1037
1038 const gchar *
1039 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1040 {
1041         EmpathyTpChatPriv *priv;
1042
1043         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1044
1045         priv = GET_PRIV (chat);
1046
1047         if (!priv->id) {
1048                 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1049         }
1050
1051         return priv->id;
1052 }
1053