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