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