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