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