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 *remote_contact;
47 EmpathyTpGroup *group;
54 gboolean had_pending_messages;
55 GSList *message_queue;
56 gboolean had_properties_list;
57 GPtrArray *properties;
64 TpPropertyFlags flags;
68 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
69 static void empathy_tp_chat_init (EmpathyTpChat *chat);
70 static void tp_chat_iface_init (EmpathyContactListIface *iface);
91 static guint signals[LAST_SIGNAL];
93 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
94 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
98 tp_chat_invalidated_cb (TpProxy *proxy,
104 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
105 g_signal_emit (chat, signals[DESTROY], 0);
109 tp_chat_async_cb (TpChannel *proxy,
112 GObject *weak_object)
115 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
116 user_data, error->message);
121 tp_chat_member_added_cb (EmpathyTpGroup *group,
122 EmpathyContact *contact,
123 EmpathyContact *actor,
125 const gchar *message,
128 g_signal_emit_by_name (chat, "members-changed",
129 contact, actor, reason, message,
134 tp_chat_member_removed_cb (EmpathyTpGroup *group,
135 EmpathyContact *contact,
136 EmpathyContact *actor,
138 const gchar *message,
141 g_signal_emit_by_name (chat, "members-changed",
142 contact, actor, reason, message,
146 tp_chat_local_pending_cb (EmpathyTpGroup *group,
147 EmpathyContact *contact,
148 EmpathyContact *actor,
150 const gchar *message,
153 g_signal_emit_by_name (chat, "pendings-changed",
154 contact, actor, reason, message,
159 tp_chat_add (EmpathyContactList *list,
160 EmpathyContact *contact,
161 const gchar *message)
163 EmpathyTpChatPriv *priv = GET_PRIV (list);
165 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
166 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
169 empathy_tp_group_add_member (priv->group, contact, message);
174 tp_chat_remove (EmpathyContactList *list,
175 EmpathyContact *contact,
176 const gchar *message)
178 EmpathyTpChatPriv *priv = GET_PRIV (list);
180 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
181 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
184 empathy_tp_group_remove_member (priv->group, contact, message);
189 tp_chat_get_members (EmpathyContactList *list)
191 EmpathyTpChatPriv *priv = GET_PRIV (list);
192 GList *members = NULL;
194 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
197 members = empathy_tp_group_get_members (priv->group);
199 members = g_list_prepend (members, g_object_ref (priv->user));
200 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
206 static EmpathyMessage *
207 tp_chat_build_message (EmpathyTpChat *chat,
211 const gchar *message_body)
213 EmpathyTpChatPriv *priv;
214 EmpathyMessage *message;
215 EmpathyContact *sender;
217 priv = GET_PRIV (chat);
219 if (from_handle == 0) {
220 sender = g_object_ref (priv->user);
222 sender = empathy_contact_factory_get_from_handle (priv->factory,
227 message = empathy_message_new (message_body);
228 empathy_message_set_type (message, type);
229 empathy_message_set_sender (message, sender);
230 empathy_message_set_receiver (message, priv->user);
231 empathy_message_set_timestamp (message, timestamp);
233 g_object_unref (sender);
239 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
240 GParamSpec *param_spec,
243 EmpathyTpChatPriv *priv = GET_PRIV (chat);
244 EmpathyMessage *message;
245 EmpathyContactReady ready;
246 EmpathyContact *sender;
247 gboolean removed = FALSE;
249 /* Emit all messages queued until we find a message with not
250 * ready sender. When leaving this loop, sender is the first not ready
251 * contact queued and removed tells if at least one message got removed
253 while (priv->message_queue) {
254 message = priv->message_queue->data;
255 sender = empathy_message_get_sender (message);
256 ready = empathy_contact_get_ready (sender);
258 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
262 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
263 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
264 priv->message_queue = g_slist_remove (priv->message_queue,
266 g_object_unref (message);
271 g_signal_handlers_disconnect_by_func (contact,
272 tp_chat_sender_ready_notify_cb,
275 if (priv->message_queue) {
276 g_signal_connect (sender, "notify::ready",
277 G_CALLBACK (tp_chat_sender_ready_notify_cb),
284 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
285 EmpathyMessage *message)
287 EmpathyTpChatPriv *priv = GET_PRIV (chat);
288 EmpathyContact *sender;
289 EmpathyContactReady ready;
291 if (priv->message_queue != NULL) {
292 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
293 priv->message_queue = g_slist_append (priv->message_queue,
294 g_object_ref (message));
298 sender = empathy_message_get_sender (message);
299 ready = empathy_contact_get_ready (sender);
300 if (ready & EMPATHY_CONTACT_READY_NAME) {
301 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
302 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
306 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
307 priv->message_queue = g_slist_append (priv->message_queue,
308 g_object_ref (message));
309 g_signal_connect (sender, "notify::ready",
310 G_CALLBACK (tp_chat_sender_ready_notify_cb),
315 tp_chat_received_cb (TpChannel *channel,
321 const gchar *message_body,
325 EmpathyTpChatPriv *priv = GET_PRIV (chat);
326 EmpathyMessage *message;
328 if (!priv->had_pending_messages) {
332 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
334 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
340 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
341 g_object_unref (message);
343 if (priv->acknowledge) {
346 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
347 g_array_append_val (message_ids, message_id);
348 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
352 "acknowledging received message",
355 g_array_free (message_ids, TRUE);
360 tp_chat_sent_cb (TpChannel *channel,
363 const gchar *message_body,
367 EmpathyMessage *message;
369 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
371 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
377 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
378 g_object_unref (message);
382 tp_chat_send_error_cb (TpChannel *channel,
386 const gchar *message_body,
390 EmpathyMessage *message;
392 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
393 message_body, error_code);
395 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
401 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
402 g_object_unref (message);
406 tp_chat_state_changed_cb (TpChannel *channel,
412 EmpathyTpChatPriv *priv = GET_PRIV (chat);
413 EmpathyContact *contact;
415 contact = empathy_contact_factory_get_from_handle (priv->factory,
419 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
420 empathy_contact_get_name (contact),
423 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
424 g_object_unref (contact);
428 tp_chat_list_pending_messages_cb (TpChannel *channel,
429 const GPtrArray *messages_list,
434 EmpathyTpChatPriv *priv = GET_PRIV (chat);
436 GArray *message_ids = NULL;
438 priv->had_pending_messages = TRUE;
441 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
446 if (priv->acknowledge) {
447 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
451 for (i = 0; i < messages_list->len; i++) {
452 EmpathyMessage *message;
453 GValueArray *message_struct;
454 const gchar *message_body;
461 message_struct = g_ptr_array_index (messages_list, i);
463 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
464 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
465 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
466 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
467 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
468 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
470 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
473 g_array_append_val (message_ids, message_id);
476 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
482 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
483 g_object_unref (message);
487 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
491 "acknowledging pending messages",
494 g_array_free (message_ids, TRUE);
499 tp_chat_property_flags_changed_cb (TpProxy *proxy,
500 const GPtrArray *properties,
504 EmpathyTpChatPriv *priv = GET_PRIV (chat);
507 if (!priv->had_properties_list || !properties) {
511 for (i = 0; i < properties->len; i++) {
512 GValueArray *prop_struct;
513 TpChatProperty *property;
517 prop_struct = g_ptr_array_index (properties, i);
518 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
519 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
521 for (j = 0; j < priv->properties->len; j++) {
522 property = g_ptr_array_index (priv->properties, j);
523 if (property->id == id) {
524 property->flags = flags;
525 empathy_debug (DEBUG_DOMAIN,
526 "property %s flags changed: %d",
527 property->name, property->flags);
535 tp_chat_properties_changed_cb (TpProxy *proxy,
536 const GPtrArray *properties,
540 EmpathyTpChatPriv *priv = GET_PRIV (chat);
543 if (!priv->had_properties_list || !properties) {
547 for (i = 0; i < properties->len; i++) {
548 GValueArray *prop_struct;
549 TpChatProperty *property;
553 prop_struct = g_ptr_array_index (properties, i);
554 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
555 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
557 for (j = 0; j < priv->properties->len; j++) {
558 property = g_ptr_array_index (priv->properties, j);
559 if (property->id == id) {
560 if (property->value) {
561 g_value_copy (src_value, property->value);
563 property->value = tp_g_value_slice_dup (src_value);
566 empathy_debug (DEBUG_DOMAIN, "property %s changed",
568 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
569 property->name, property->value);
577 tp_chat_get_properties_cb (TpProxy *proxy,
578 const GPtrArray *properties,
584 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
589 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
593 tp_chat_list_properties_cb (TpProxy *proxy,
594 const GPtrArray *properties,
599 EmpathyTpChatPriv *priv = GET_PRIV (chat);
603 priv->had_properties_list = TRUE;
606 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
611 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
612 priv->properties = g_ptr_array_sized_new (properties->len);
613 for (i = 0; i < properties->len; i++) {
614 GValueArray *prop_struct;
615 TpChatProperty *property;
617 prop_struct = g_ptr_array_index (properties, i);
618 property = g_slice_new0 (TpChatProperty);
619 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
620 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
621 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
623 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
624 property->name, property->id, property->flags);
625 g_ptr_array_add (priv->properties, property);
626 if (property->flags & TP_PROPERTY_FLAG_READ) {
627 g_array_append_val (ids, property->id);
631 tp_cli_properties_interface_call_get_properties (proxy, -1,
633 tp_chat_get_properties_cb,
637 g_array_free (ids, TRUE);
641 empathy_tp_chat_set_property (EmpathyTpChat *chat,
645 EmpathyTpChatPriv *priv = GET_PRIV (chat);
646 TpChatProperty *property;
649 g_return_if_fail (priv->ready);
651 for (i = 0; i < priv->properties->len; i++) {
652 property = g_ptr_array_index (priv->properties, i);
653 if (!tp_strdiff (property->name, name)) {
654 GPtrArray *properties;
657 GValue dest_value = {0, };
659 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
663 g_value_init (&id, G_TYPE_UINT);
664 g_value_init (&dest_value, G_TYPE_VALUE);
665 g_value_set_uint (&id, property->id);
666 g_value_set_boxed (&dest_value, value);
668 prop = g_value_array_new (2);
669 g_value_array_append (prop, &id);
670 g_value_array_append (prop, &dest_value);
672 properties = g_ptr_array_sized_new (1);
673 g_ptr_array_add (properties, prop);
675 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
676 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
678 (tp_cli_properties_interface_callback_for_set_properties)
680 "Seting property", NULL,
683 g_ptr_array_free (properties, TRUE);
684 g_value_array_free (prop);
692 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
694 EmpathyTpChatPriv *priv = GET_PRIV (chat);
696 empathy_debug (DEBUG_DOMAIN, "Channel ready");
699 if (tp_proxy_has_interface_by_id (priv->channel,
700 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
701 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
703 g_signal_connect (priv->group, "member-added",
704 G_CALLBACK (tp_chat_member_added_cb),
706 g_signal_connect (priv->group, "member-removed",
707 G_CALLBACK (tp_chat_member_removed_cb),
709 g_signal_connect (priv->group, "local-pending",
710 G_CALLBACK (tp_chat_local_pending_cb),
713 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
715 priv->tp_chan->handle);
716 g_object_notify (G_OBJECT (chat), "remote-contact");
719 if (tp_proxy_has_interface_by_id (priv->channel,
720 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
721 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
722 tp_chat_list_properties_cb,
725 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
726 tp_chat_properties_changed_cb,
728 G_OBJECT (chat), NULL);
729 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
730 tp_chat_property_flags_changed_cb,
732 G_OBJECT (chat), NULL);
735 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
737 tp_chat_list_pending_messages_cb,
741 tp_cli_channel_type_text_connect_to_received (priv->channel,
744 G_OBJECT (chat), NULL);
745 tp_cli_channel_type_text_connect_to_sent (priv->channel,
748 G_OBJECT (chat), NULL);
749 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
750 tp_chat_send_error_cb,
752 G_OBJECT (chat), NULL);
753 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
754 tp_chat_state_changed_cb,
756 G_OBJECT (chat), NULL);
757 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
758 tp_chat_state_changed_cb,
760 G_OBJECT (chat), NULL);
764 tp_chat_finalize (GObject *object)
766 EmpathyTpChatPriv *priv = GET_PRIV (object);
769 if (priv->acknowledge && priv->channel) {
770 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
771 tp_cli_channel_call_close (priv->channel, -1,
773 "closing channel", NULL,
778 g_signal_handlers_disconnect_by_func (priv->channel,
779 tp_chat_invalidated_cb,
781 g_object_unref (priv->channel);
784 g_object_unref (priv->tp_chan);
787 if (priv->properties) {
788 for (i = 0; i < priv->properties->len; i++) {
789 TpChatProperty *property;
791 property = g_ptr_array_index (priv->properties, i);
792 g_free (property->name);
793 if (property->value) {
794 tp_g_value_slice_free (property->value);
796 g_slice_free (TpChatProperty, property);
798 g_ptr_array_free (priv->properties, TRUE);
801 if (priv->remote_contact) {
802 g_object_unref (priv->remote_contact);
805 g_object_unref (priv->group);
808 g_object_unref (priv->factory);
809 g_object_unref (priv->user);
810 g_object_unref (priv->account);
811 g_object_unref (priv->mc);
814 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
818 tp_chat_constructor (GType type,
820 GObjectConstructParam *props)
823 EmpathyTpChatPriv *priv;
824 gboolean channel_ready;
826 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
828 priv = GET_PRIV (chat);
829 priv->factory = empathy_contact_factory_new ();
830 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
831 priv->mc = empathy_mission_control_new ();
833 g_signal_connect (priv->channel, "invalidated",
834 G_CALLBACK (tp_chat_invalidated_cb),
837 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
839 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
841 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
842 G_CALLBACK (tp_chat_channel_ready_cb),
850 tp_chat_get_property (GObject *object,
855 EmpathyTpChatPriv *priv = GET_PRIV (object);
859 g_value_set_object (value, priv->account);
862 g_value_set_object (value, priv->tp_chan);
865 g_value_set_object (value, priv->channel);
867 case PROP_ACKNOWLEDGE:
868 g_value_set_boolean (value, priv->acknowledge);
870 case PROP_REMOTE_CONTACT:
871 g_value_set_object (value, priv->remote_contact);
874 g_value_set_boolean (value, priv->ready);
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
883 tp_chat_set_property (GObject *object,
888 EmpathyTpChatPriv *priv = GET_PRIV (object);
892 priv->account = g_object_ref (g_value_get_object (value));
895 priv->tp_chan = g_object_ref (g_value_get_object (value));
898 priv->channel = g_object_ref (g_value_get_object (value));
900 case PROP_ACKNOWLEDGE:
901 priv->acknowledge = g_value_get_boolean (value);
904 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
910 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
912 GObjectClass *object_class = G_OBJECT_CLASS (klass);
914 object_class->finalize = tp_chat_finalize;
915 object_class->constructor = tp_chat_constructor;
916 object_class->get_property = tp_chat_get_property;
917 object_class->set_property = tp_chat_set_property;
919 g_object_class_install_property (object_class,
921 g_param_spec_object ("account",
923 "The account associated with the channel",
926 G_PARAM_CONSTRUCT_ONLY));
927 g_object_class_install_property (object_class,
929 g_param_spec_object ("tp-chan",
931 "The text channel for the chat",
934 G_PARAM_CONSTRUCT_ONLY));
935 g_object_class_install_property (object_class,
937 g_param_spec_object ("channel",
939 "The text channel for the chat",
942 G_PARAM_CONSTRUCT_ONLY));
943 g_object_class_install_property (object_class,
945 g_param_spec_boolean ("acknowledge",
946 "acknowledge messages",
947 "Wheter or not received messages should be acknowledged",
952 g_object_class_install_property (object_class,
954 g_param_spec_object ("remote-contact",
955 "The remote contact",
956 "The remote contact if there is no group iface on the channel",
957 EMPATHY_TYPE_CONTACT,
959 g_object_class_install_property (object_class,
961 g_param_spec_boolean ("ready",
962 "Is the object ready",
963 "This object can't be used until this becomes true",
968 signals[MESSAGE_RECEIVED] =
969 g_signal_new ("message-received",
970 G_TYPE_FROM_CLASS (klass),
974 g_cclosure_marshal_VOID__OBJECT,
976 1, EMPATHY_TYPE_MESSAGE);
978 signals[SEND_ERROR] =
979 g_signal_new ("send-error",
980 G_TYPE_FROM_CLASS (klass),
984 _empathy_marshal_VOID__OBJECT_UINT,
986 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
988 signals[CHAT_STATE_CHANGED] =
989 g_signal_new ("chat-state-changed",
990 G_TYPE_FROM_CLASS (klass),
994 _empathy_marshal_VOID__OBJECT_UINT,
996 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
998 signals[PROPERTY_CHANGED] =
999 g_signal_new ("property-changed",
1000 G_TYPE_FROM_CLASS (klass),
1004 _empathy_marshal_VOID__STRING_BOXED,
1006 2, G_TYPE_STRING, G_TYPE_VALUE);
1009 g_signal_new ("destroy",
1010 G_TYPE_FROM_CLASS (klass),
1014 g_cclosure_marshal_VOID__VOID,
1018 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1022 empathy_tp_chat_init (EmpathyTpChat *chat)
1027 tp_chat_iface_init (EmpathyContactListIface *iface)
1029 iface->add = tp_chat_add;
1030 iface->remove = tp_chat_remove;
1031 iface->get_members = tp_chat_get_members;
1035 empathy_tp_chat_new (McAccount *account,
1037 gboolean acknowledge)
1039 EmpathyTpChat *chat;
1041 TpConnection *connection;
1044 mc = empathy_mission_control_new ();
1045 connection = mission_control_get_tpconnection (mc, account, NULL);
1046 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1048 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1052 "acknowledge", acknowledge,
1055 g_object_unref (channel);
1056 g_object_unref (connection);
1057 g_object_unref (mc);
1063 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1065 EmpathyTpChat *chat;
1070 const gchar *bus_name;
1073 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1075 mc = empathy_mission_control_new ();
1076 account = empathy_contact_get_account (contact);
1078 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1079 /* The account is not connected. */
1083 tp_conn = mission_control_get_connection (mc, account, NULL);
1084 g_return_val_if_fail (tp_conn != NULL, NULL);
1085 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1086 handle = empathy_contact_get_handle (contact);
1088 text_chan = tp_conn_new_channel (tp_get_bus (),
1091 TP_IFACE_CHANNEL_TYPE_TEXT,
1092 TP_HANDLE_TYPE_CONTACT,
1096 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1098 g_object_unref (tp_conn);
1099 g_object_unref (text_chan);
1100 g_object_unref (mc);
1106 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1108 EmpathyTpChatPriv *priv;
1110 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1112 priv = GET_PRIV (chat);
1114 return priv->tp_chan;
1118 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1120 EmpathyTpChatPriv *priv;
1122 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1124 priv = GET_PRIV (chat);
1126 return priv->account;
1130 empathy_tp_chat_send (EmpathyTpChat *chat,
1131 EmpathyMessage *message)
1133 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1134 const gchar *message_body;
1135 EmpathyMessageType message_type;
1137 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1138 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1139 g_return_if_fail (priv->ready);
1141 message_body = empathy_message_get_body (message);
1142 message_type = empathy_message_get_type (message);
1144 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1145 tp_cli_channel_type_text_call_send (priv->channel, -1,
1149 "sending message", NULL,
1154 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1155 TpChannelChatState state)
1157 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1159 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1160 g_return_if_fail (priv->ready);
1162 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1163 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1166 "setting chat state",
1172 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1174 EmpathyTpChatPriv *priv;
1176 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1178 priv = GET_PRIV (chat);
1181 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1188 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1190 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1192 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1194 return priv->remote_contact;
1198 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1200 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1202 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);