1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-debug.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
41 #define DEBUG_DOMAIN "TpChat"
43 struct _EmpathyTpChatPriv {
44 EmpathyContactFactory *factory;
46 EmpathyContact *initiator;
47 EmpathyTpGroup *group;
54 gboolean had_pending_messages;
55 GSList *message_queue;
56 gboolean had_properties_list;
57 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
68 static void empathy_tp_chat_init (EmpathyTpChat *chat);
69 static void tp_chat_iface_init (EmpathyContactListIface *iface);
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
95 tp_chat_invalidated_cb (TpProxy *proxy,
101 EmpathyTpChatPriv *priv = GET_PRIV (chat);
103 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
105 g_object_unref (priv->channel);
106 g_object_unref (priv->tp_chan);
107 priv->channel = NULL;
108 priv->tp_chan = NULL;
110 g_signal_emit (chat, signals[DESTROY], 0);
114 tp_chat_async_cb (TpChannel *proxy,
117 GObject *weak_object)
120 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
121 user_data, error->message);
126 tp_chat_member_added_cb (EmpathyTpGroup *group,
127 EmpathyContact *contact,
128 EmpathyContact *actor,
130 const gchar *message,
133 g_signal_emit_by_name (chat, "members-changed",
134 contact, actor, reason, message,
139 tp_chat_member_removed_cb (EmpathyTpGroup *group,
140 EmpathyContact *contact,
141 EmpathyContact *actor,
143 const gchar *message,
146 g_signal_emit_by_name (chat, "members-changed",
147 contact, actor, reason, message,
151 tp_chat_local_pending_cb (EmpathyTpGroup *group,
152 EmpathyContact *contact,
153 EmpathyContact *actor,
155 const gchar *message,
158 g_signal_emit_by_name (chat, "pendings-changed",
159 contact, actor, reason, message,
164 tp_chat_add (EmpathyContactList *list,
165 EmpathyContact *contact,
166 const gchar *message)
168 EmpathyTpChatPriv *priv = GET_PRIV (list);
170 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
171 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
174 empathy_tp_group_add_member (priv->group, contact, message);
179 tp_chat_remove (EmpathyContactList *list,
180 EmpathyContact *contact,
181 const gchar *message)
183 EmpathyTpChatPriv *priv = GET_PRIV (list);
185 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
186 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
189 empathy_tp_group_remove_member (priv->group, contact, message);
194 tp_chat_get_members (EmpathyContactList *list)
196 EmpathyTpChatPriv *priv = GET_PRIV (list);
197 GList *members = NULL;
199 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
202 members = empathy_tp_group_get_members (priv->group);
204 members = g_list_prepend (members, g_object_ref (priv->user));
205 members = g_list_prepend (members, g_object_ref (priv->initiator));
211 static EmpathyMessage *
212 tp_chat_build_message (EmpathyTpChat *chat,
216 const gchar *message_body)
218 EmpathyTpChatPriv *priv;
219 EmpathyMessage *message;
220 EmpathyContact *sender;
222 priv = GET_PRIV (chat);
224 if (from_handle == 0) {
225 sender = g_object_ref (priv->user);
227 sender = empathy_contact_factory_get_from_handle (priv->factory,
232 message = empathy_message_new (message_body);
233 empathy_message_set_type (message, type);
234 empathy_message_set_sender (message, sender);
235 empathy_message_set_receiver (message, priv->user);
236 empathy_message_set_timestamp (message, timestamp);
238 g_object_unref (sender);
244 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
245 GParamSpec *param_spec,
248 EmpathyTpChatPriv *priv = GET_PRIV (chat);
249 EmpathyMessage *message;
250 EmpathyContactReady ready;
251 EmpathyContact *sender;
252 gboolean removed = FALSE;
254 /* Emit all messages queued until we find a message with not
255 * ready sender. When leaving this loop, sender is the first not ready
256 * contact queued and removed tells if at least one message got removed
258 while (priv->message_queue) {
259 message = priv->message_queue->data;
260 sender = empathy_message_get_sender (message);
261 ready = empathy_contact_get_ready (sender);
263 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
267 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
268 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
269 priv->message_queue = g_slist_remove (priv->message_queue,
271 g_object_unref (message);
276 g_signal_handlers_disconnect_by_func (contact,
277 tp_chat_sender_ready_notify_cb,
280 if (priv->message_queue) {
281 g_signal_connect (sender, "notify::ready",
282 G_CALLBACK (tp_chat_sender_ready_notify_cb),
289 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
290 EmpathyMessage *message)
292 EmpathyTpChatPriv *priv = GET_PRIV (chat);
293 EmpathyContact *sender;
294 EmpathyContactReady ready;
296 if (priv->message_queue != NULL) {
297 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
298 priv->message_queue = g_slist_append (priv->message_queue,
299 g_object_ref (message));
303 sender = empathy_message_get_sender (message);
304 ready = empathy_contact_get_ready (sender);
305 if (ready & EMPATHY_CONTACT_READY_NAME) {
306 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
307 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
311 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
312 priv->message_queue = g_slist_append (priv->message_queue,
313 g_object_ref (message));
314 g_signal_connect (sender, "notify::ready",
315 G_CALLBACK (tp_chat_sender_ready_notify_cb),
320 tp_chat_received_cb (TpChannel *channel,
326 const gchar *message_body,
330 EmpathyTpChatPriv *priv = GET_PRIV (chat);
331 EmpathyMessage *message;
333 if (!priv->had_pending_messages) {
337 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
339 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
345 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
346 g_object_unref (message);
348 if (priv->acknowledge) {
351 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
352 g_array_append_val (message_ids, message_id);
353 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
357 "acknowledging pending messages",
360 g_array_free (message_ids, TRUE);
365 tp_chat_sent_cb (TpChannel *channel,
368 const gchar *message_body,
372 EmpathyMessage *message;
374 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
376 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
382 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
383 g_object_unref (message);
387 tp_chat_send_error_cb (TpChannel *channel,
391 const gchar *message_body,
395 EmpathyMessage *message;
397 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
398 message_body, error_code);
400 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
406 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
407 g_object_unref (message);
411 tp_chat_state_changed_cb (TpChannel *channel,
417 EmpathyTpChatPriv *priv = GET_PRIV (chat);
418 EmpathyContact *contact;
420 contact = empathy_contact_factory_get_from_handle (priv->factory,
424 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
425 empathy_contact_get_name (contact),
428 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
429 g_object_unref (contact);
433 tp_chat_list_pending_messages_cb (TpChannel *channel,
434 const GPtrArray *messages_list,
439 EmpathyTpChatPriv *priv = GET_PRIV (chat);
442 priv->had_pending_messages = TRUE;
445 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
450 for (i = 0; i < messages_list->len; i++) {
451 EmpathyMessage *message;
452 GValueArray *message_struct;
453 const gchar *message_body;
460 message_struct = g_ptr_array_index (messages_list, i);
462 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
463 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
464 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
465 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
466 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
467 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
469 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
471 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
477 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
478 g_object_unref (message);
483 tp_chat_property_flags_changed_cb (TpProxy *proxy,
484 const GPtrArray *properties,
488 EmpathyTpChatPriv *priv = GET_PRIV (chat);
491 if (!priv->had_properties_list || !properties) {
495 for (i = 0; i < properties->len; i++) {
496 GValueArray *prop_struct;
497 TpChatProperty *property;
501 prop_struct = g_ptr_array_index (properties, i);
502 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
503 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
505 for (j = 0; j < priv->properties->len; j++) {
506 property = g_ptr_array_index (priv->properties, j);
507 if (property->id == id) {
508 property->flags = flags;
509 empathy_debug (DEBUG_DOMAIN,
510 "property %s flags changed: %d",
511 property->name, property->flags);
519 tp_chat_properties_changed_cb (TpProxy *proxy,
520 const GPtrArray *properties,
524 EmpathyTpChatPriv *priv = GET_PRIV (chat);
527 if (!priv->had_properties_list || !properties) {
531 for (i = 0; i < properties->len; i++) {
532 GValueArray *prop_struct;
533 TpChatProperty *property;
537 prop_struct = g_ptr_array_index (properties, i);
538 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
539 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
541 for (j = 0; j < priv->properties->len; j++) {
542 property = g_ptr_array_index (priv->properties, j);
543 if (property->id == id) {
544 if (property->value) {
545 g_value_copy (src_value, property->value);
547 property->value = tp_g_value_slice_dup (src_value);
550 empathy_debug (DEBUG_DOMAIN, "property %s changed",
552 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
553 property->name, property->value);
561 tp_chat_get_properties_cb (TpProxy *proxy,
562 const GPtrArray *properties,
568 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
573 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
577 tp_chat_list_properties_cb (TpProxy *proxy,
578 const GPtrArray *properties,
583 EmpathyTpChatPriv *priv = GET_PRIV (chat);
587 priv->had_properties_list = TRUE;
590 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
595 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
596 priv->properties = g_ptr_array_sized_new (properties->len);
597 for (i = 0; i < properties->len; i++) {
598 GValueArray *prop_struct;
599 TpChatProperty *property;
601 prop_struct = g_ptr_array_index (properties, i);
602 property = g_slice_new0 (TpChatProperty);
603 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
604 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
605 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
607 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
608 property->name, property->id, property->flags);
609 g_ptr_array_add (priv->properties, property);
610 if (property->flags & TP_PROPERTY_FLAG_READ) {
611 g_array_append_val (ids, property->id);
615 tp_cli_properties_interface_call_get_properties (proxy, -1,
617 tp_chat_get_properties_cb,
621 g_array_free (ids, TRUE);
625 empathy_tp_chat_set_property (EmpathyTpChat *chat,
629 EmpathyTpChatPriv *priv = GET_PRIV (chat);
630 TpChatProperty *property;
633 for (i = 0; i < priv->properties->len; i++) {
634 property = g_ptr_array_index (priv->properties, i);
635 if (!tp_strdiff (property->name, name)) {
636 GPtrArray *properties;
639 GValue dest_value = {0, };
641 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
645 g_value_init (&id, G_TYPE_UINT);
646 g_value_init (&dest_value, G_TYPE_VALUE);
647 g_value_set_uint (&id, property->id);
648 g_value_set_boxed (&dest_value, value);
650 prop = g_value_array_new (2);
651 g_value_array_append (prop, &id);
652 g_value_array_append (prop, &dest_value);
654 properties = g_ptr_array_sized_new (1);
655 g_ptr_array_add (properties, prop);
657 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
658 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
660 (tp_cli_properties_interface_callback_for_set_properties)
662 "Seting property", NULL,
665 g_ptr_array_free (properties, TRUE);
666 g_value_array_free (prop);
674 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
676 EmpathyTpChatPriv *priv = GET_PRIV (chat);
678 empathy_debug (DEBUG_DOMAIN, "Channel ready");
680 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
682 tp_chat_list_pending_messages_cb,
685 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
686 tp_chat_list_properties_cb,
691 tp_cli_channel_type_text_connect_to_received (priv->channel,
694 G_OBJECT (chat), NULL);
695 tp_cli_channel_type_text_connect_to_sent (priv->channel,
698 G_OBJECT (chat), NULL);
699 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
700 tp_chat_send_error_cb,
702 G_OBJECT (chat), NULL);
703 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
704 tp_chat_state_changed_cb,
706 G_OBJECT (chat), NULL);
707 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
708 tp_chat_state_changed_cb,
710 G_OBJECT (chat), NULL);
711 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
712 tp_chat_properties_changed_cb,
714 G_OBJECT (chat), NULL);
715 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
716 tp_chat_property_flags_changed_cb,
718 G_OBJECT (chat), NULL);
724 tp_chat_finalize (GObject *object)
726 EmpathyTpChatPriv *priv = GET_PRIV (object);
729 if (priv->acknowledge && priv->channel) {
730 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
731 tp_cli_channel_call_close (priv->channel, -1,
733 "closing channel", NULL,
738 g_signal_handlers_disconnect_by_func (priv->channel,
739 tp_chat_invalidated_cb,
741 g_object_unref (priv->channel);
744 g_object_unref (priv->tp_chan);
747 if (priv->properties) {
748 for (i = 0; i < priv->properties->len; i++) {
749 TpChatProperty *property;
751 property = g_ptr_array_index (priv->properties, i);
752 g_free (property->name);
753 if (property->value) {
754 tp_g_value_slice_free (property->value);
756 g_slice_free (TpChatProperty, property);
758 g_ptr_array_free (priv->properties, TRUE);
761 g_object_unref (priv->factory);
762 g_object_unref (priv->user);
763 g_object_unref (priv->account);
764 g_object_unref (priv->mc);
767 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
771 tp_chat_constructor (GType type,
773 GObjectConstructParam *props)
776 EmpathyTpChatPriv *priv;
777 gboolean channel_ready;
779 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
781 priv = GET_PRIV (chat);
782 priv->factory = empathy_contact_factory_new ();
783 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
784 priv->mc = empathy_mission_control_new ();
786 g_signal_connect (priv->channel, "invalidated",
787 G_CALLBACK (tp_chat_invalidated_cb),
790 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
792 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
794 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
795 G_CALLBACK (tp_chat_channel_ready_cb),
799 if (tp_proxy_has_interface_by_id (priv->channel,
800 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
801 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
803 g_signal_connect (priv->group, "member-added",
804 G_CALLBACK (tp_chat_member_added_cb),
806 g_signal_connect (priv->group, "member-removed",
807 G_CALLBACK (tp_chat_member_removed_cb),
809 g_signal_connect (priv->group, "local-pending",
810 G_CALLBACK (tp_chat_local_pending_cb),
813 priv->initiator = empathy_contact_factory_get_from_handle (priv->factory,
815 priv->tp_chan->handle);
822 tp_chat_get_property (GObject *object,
827 EmpathyTpChatPriv *priv = GET_PRIV (object);
831 g_value_set_object (value, priv->account);
834 g_value_set_object (value, priv->tp_chan);
837 g_value_set_object (value, priv->channel);
839 case PROP_ACKNOWLEDGE:
840 g_value_set_boolean (value, priv->acknowledge);
843 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
849 tp_chat_set_property (GObject *object,
854 EmpathyTpChatPriv *priv = GET_PRIV (object);
858 priv->account = g_object_ref (g_value_get_object (value));
861 priv->tp_chan = g_object_ref (g_value_get_object (value));
864 priv->channel = g_object_ref (g_value_get_object (value));
866 case PROP_ACKNOWLEDGE:
867 priv->acknowledge = g_value_get_boolean (value);
870 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
876 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
878 GObjectClass *object_class = G_OBJECT_CLASS (klass);
880 object_class->finalize = tp_chat_finalize;
881 object_class->constructor = tp_chat_constructor;
882 object_class->get_property = tp_chat_get_property;
883 object_class->set_property = tp_chat_set_property;
885 /* Construct properties */
886 g_object_class_install_property (object_class,
888 g_param_spec_object ("account",
890 "The account associated with the channel",
893 G_PARAM_CONSTRUCT_ONLY));
894 g_object_class_install_property (object_class,
896 g_param_spec_object ("tp-chan",
898 "The text channel for the chat",
901 G_PARAM_CONSTRUCT_ONLY));
903 g_object_class_install_property (object_class,
905 g_param_spec_object ("channel",
907 "The text channel for the chat",
910 G_PARAM_CONSTRUCT_ONLY));
912 g_object_class_install_property (object_class,
914 g_param_spec_boolean ("acknowledge",
915 "acknowledge messages",
916 "Wheter or not received messages should be acknowledged",
919 G_PARAM_CONSTRUCT_ONLY));
922 signals[MESSAGE_RECEIVED] =
923 g_signal_new ("message-received",
924 G_TYPE_FROM_CLASS (klass),
928 g_cclosure_marshal_VOID__OBJECT,
930 1, EMPATHY_TYPE_MESSAGE);
932 signals[SEND_ERROR] =
933 g_signal_new ("send-error",
934 G_TYPE_FROM_CLASS (klass),
938 _empathy_marshal_VOID__OBJECT_UINT,
940 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
942 signals[CHAT_STATE_CHANGED] =
943 g_signal_new ("chat-state-changed",
944 G_TYPE_FROM_CLASS (klass),
948 _empathy_marshal_VOID__OBJECT_UINT,
950 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
952 signals[PROPERTY_CHANGED] =
953 g_signal_new ("property-changed",
954 G_TYPE_FROM_CLASS (klass),
958 _empathy_marshal_VOID__STRING_BOXED,
960 2, G_TYPE_STRING, G_TYPE_VALUE);
963 g_signal_new ("destroy",
964 G_TYPE_FROM_CLASS (klass),
968 g_cclosure_marshal_VOID__VOID,
972 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
976 empathy_tp_chat_init (EmpathyTpChat *chat)
981 tp_chat_iface_init (EmpathyContactListIface *iface)
983 iface->add = tp_chat_add;
984 iface->remove = tp_chat_remove;
985 iface->get_members = tp_chat_get_members;
989 empathy_tp_chat_new (McAccount *account,
991 gboolean acknowledge)
995 TpConnection *connection;
999 mc = empathy_mission_control_new ();
1000 tp_conn = mission_control_get_connection (mc, account, NULL);
1001 connection = tp_conn_dup_connection (tp_conn);
1002 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1004 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1008 "acknowledge", acknowledge,
1011 g_object_unref (channel);
1012 g_object_unref (tp_conn);
1013 g_object_unref (connection);
1014 g_object_unref (mc);
1020 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1022 EmpathyTpChat *chat;
1027 const gchar *bus_name;
1030 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1032 mc = empathy_mission_control_new ();
1033 account = empathy_contact_get_account (contact);
1035 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1036 /* The account is not connected. */
1040 tp_conn = mission_control_get_connection (mc, account, NULL);
1041 g_return_val_if_fail (tp_conn != NULL, NULL);
1042 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1043 handle = empathy_contact_get_handle (contact);
1045 text_chan = tp_conn_new_channel (tp_get_bus (),
1048 TP_IFACE_CHANNEL_TYPE_TEXT,
1049 TP_HANDLE_TYPE_CONTACT,
1053 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1055 g_object_unref (tp_conn);
1056 g_object_unref (text_chan);
1057 g_object_unref (mc);
1063 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1065 EmpathyTpChatPriv *priv;
1067 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1069 priv = GET_PRIV (chat);
1071 return priv->tp_chan;
1075 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1077 EmpathyTpChatPriv *priv;
1079 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1081 priv = GET_PRIV (chat);
1083 return priv->account;
1087 empathy_tp_chat_send (EmpathyTpChat *chat,
1088 EmpathyMessage *message)
1090 EmpathyTpChatPriv *priv;
1091 const gchar *message_body;
1092 EmpathyMessageType message_type;
1094 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1095 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1097 priv = GET_PRIV (chat);
1099 message_body = empathy_message_get_body (message);
1100 message_type = empathy_message_get_type (message);
1102 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1103 tp_cli_channel_type_text_call_send (priv->channel, -1,
1107 "sending message", NULL,
1112 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1113 TpChannelChatState state)
1115 EmpathyTpChatPriv *priv;
1117 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1119 priv = GET_PRIV (chat);
1121 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1122 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1125 "setting chat state",
1131 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1133 EmpathyTpChatPriv *priv;
1135 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1137 priv = GET_PRIV (chat);
1140 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);