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 EmpathyTpChatPriv *priv = GET_PRIV (chat);
106 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
108 g_object_unref (priv->channel);
109 g_object_unref (priv->tp_chan);
110 priv->channel = NULL;
111 priv->tp_chan = NULL;
113 g_signal_emit (chat, signals[DESTROY], 0);
117 tp_chat_async_cb (TpChannel *proxy,
120 GObject *weak_object)
123 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
124 user_data, error->message);
129 tp_chat_member_added_cb (EmpathyTpGroup *group,
130 EmpathyContact *contact,
131 EmpathyContact *actor,
133 const gchar *message,
136 g_signal_emit_by_name (chat, "members-changed",
137 contact, actor, reason, message,
142 tp_chat_member_removed_cb (EmpathyTpGroup *group,
143 EmpathyContact *contact,
144 EmpathyContact *actor,
146 const gchar *message,
149 g_signal_emit_by_name (chat, "members-changed",
150 contact, actor, reason, message,
154 tp_chat_local_pending_cb (EmpathyTpGroup *group,
155 EmpathyContact *contact,
156 EmpathyContact *actor,
158 const gchar *message,
161 g_signal_emit_by_name (chat, "pendings-changed",
162 contact, actor, reason, message,
167 tp_chat_add (EmpathyContactList *list,
168 EmpathyContact *contact,
169 const gchar *message)
171 EmpathyTpChatPriv *priv = GET_PRIV (list);
173 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
174 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
177 empathy_tp_group_add_member (priv->group, contact, message);
182 tp_chat_remove (EmpathyContactList *list,
183 EmpathyContact *contact,
184 const gchar *message)
186 EmpathyTpChatPriv *priv = GET_PRIV (list);
188 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
189 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
192 empathy_tp_group_remove_member (priv->group, contact, message);
197 tp_chat_get_members (EmpathyContactList *list)
199 EmpathyTpChatPriv *priv = GET_PRIV (list);
200 GList *members = NULL;
202 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
205 members = empathy_tp_group_get_members (priv->group);
207 members = g_list_prepend (members, g_object_ref (priv->user));
208 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
214 static EmpathyMessage *
215 tp_chat_build_message (EmpathyTpChat *chat,
219 const gchar *message_body)
221 EmpathyTpChatPriv *priv;
222 EmpathyMessage *message;
223 EmpathyContact *sender;
225 priv = GET_PRIV (chat);
227 if (from_handle == 0) {
228 sender = g_object_ref (priv->user);
230 sender = empathy_contact_factory_get_from_handle (priv->factory,
235 message = empathy_message_new (message_body);
236 empathy_message_set_type (message, type);
237 empathy_message_set_sender (message, sender);
238 empathy_message_set_receiver (message, priv->user);
239 empathy_message_set_timestamp (message, timestamp);
241 g_object_unref (sender);
247 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
248 GParamSpec *param_spec,
251 EmpathyTpChatPriv *priv = GET_PRIV (chat);
252 EmpathyMessage *message;
253 EmpathyContactReady ready;
254 EmpathyContact *sender;
255 gboolean removed = FALSE;
257 /* Emit all messages queued until we find a message with not
258 * ready sender. When leaving this loop, sender is the first not ready
259 * contact queued and removed tells if at least one message got removed
261 while (priv->message_queue) {
262 message = priv->message_queue->data;
263 sender = empathy_message_get_sender (message);
264 ready = empathy_contact_get_ready (sender);
266 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
270 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
271 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
272 priv->message_queue = g_slist_remove (priv->message_queue,
274 g_object_unref (message);
279 g_signal_handlers_disconnect_by_func (contact,
280 tp_chat_sender_ready_notify_cb,
283 if (priv->message_queue) {
284 g_signal_connect (sender, "notify::ready",
285 G_CALLBACK (tp_chat_sender_ready_notify_cb),
292 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
293 EmpathyMessage *message)
295 EmpathyTpChatPriv *priv = GET_PRIV (chat);
296 EmpathyContact *sender;
297 EmpathyContactReady ready;
299 if (priv->message_queue != NULL) {
300 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
301 priv->message_queue = g_slist_append (priv->message_queue,
302 g_object_ref (message));
306 sender = empathy_message_get_sender (message);
307 ready = empathy_contact_get_ready (sender);
308 if (ready & EMPATHY_CONTACT_READY_NAME) {
309 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
310 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
314 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
315 priv->message_queue = g_slist_append (priv->message_queue,
316 g_object_ref (message));
317 g_signal_connect (sender, "notify::ready",
318 G_CALLBACK (tp_chat_sender_ready_notify_cb),
323 tp_chat_received_cb (TpChannel *channel,
329 const gchar *message_body,
333 EmpathyTpChatPriv *priv = GET_PRIV (chat);
334 EmpathyMessage *message;
336 if (!priv->had_pending_messages) {
340 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
342 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
348 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
349 g_object_unref (message);
351 if (priv->acknowledge) {
354 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
355 g_array_append_val (message_ids, message_id);
356 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
360 "acknowledging received message",
363 g_array_free (message_ids, TRUE);
368 tp_chat_sent_cb (TpChannel *channel,
371 const gchar *message_body,
375 EmpathyMessage *message;
377 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
379 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
385 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
386 g_object_unref (message);
390 tp_chat_send_error_cb (TpChannel *channel,
394 const gchar *message_body,
398 EmpathyMessage *message;
400 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
401 message_body, error_code);
403 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
409 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
410 g_object_unref (message);
414 tp_chat_state_changed_cb (TpChannel *channel,
420 EmpathyTpChatPriv *priv = GET_PRIV (chat);
421 EmpathyContact *contact;
423 contact = empathy_contact_factory_get_from_handle (priv->factory,
427 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
428 empathy_contact_get_name (contact),
431 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
432 g_object_unref (contact);
436 tp_chat_list_pending_messages_cb (TpChannel *channel,
437 const GPtrArray *messages_list,
442 EmpathyTpChatPriv *priv = GET_PRIV (chat);
444 GArray *message_ids = NULL;
446 priv->had_pending_messages = TRUE;
449 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
454 if (priv->acknowledge) {
455 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
459 for (i = 0; i < messages_list->len; i++) {
460 EmpathyMessage *message;
461 GValueArray *message_struct;
462 const gchar *message_body;
469 message_struct = g_ptr_array_index (messages_list, i);
471 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
472 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
473 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
474 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
475 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
476 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
478 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
481 g_array_append_val (message_ids, message_id);
484 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
490 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
491 g_object_unref (message);
495 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
499 "acknowledging pending messages",
502 g_array_free (message_ids, TRUE);
507 tp_chat_property_flags_changed_cb (TpProxy *proxy,
508 const GPtrArray *properties,
512 EmpathyTpChatPriv *priv = GET_PRIV (chat);
515 if (!priv->had_properties_list || !properties) {
519 for (i = 0; i < properties->len; i++) {
520 GValueArray *prop_struct;
521 TpChatProperty *property;
525 prop_struct = g_ptr_array_index (properties, i);
526 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
527 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
529 for (j = 0; j < priv->properties->len; j++) {
530 property = g_ptr_array_index (priv->properties, j);
531 if (property->id == id) {
532 property->flags = flags;
533 empathy_debug (DEBUG_DOMAIN,
534 "property %s flags changed: %d",
535 property->name, property->flags);
543 tp_chat_properties_changed_cb (TpProxy *proxy,
544 const GPtrArray *properties,
548 EmpathyTpChatPriv *priv = GET_PRIV (chat);
551 if (!priv->had_properties_list || !properties) {
555 for (i = 0; i < properties->len; i++) {
556 GValueArray *prop_struct;
557 TpChatProperty *property;
561 prop_struct = g_ptr_array_index (properties, i);
562 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
563 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
565 for (j = 0; j < priv->properties->len; j++) {
566 property = g_ptr_array_index (priv->properties, j);
567 if (property->id == id) {
568 if (property->value) {
569 g_value_copy (src_value, property->value);
571 property->value = tp_g_value_slice_dup (src_value);
574 empathy_debug (DEBUG_DOMAIN, "property %s changed",
576 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
577 property->name, property->value);
585 tp_chat_get_properties_cb (TpProxy *proxy,
586 const GPtrArray *properties,
592 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
597 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
601 tp_chat_list_properties_cb (TpProxy *proxy,
602 const GPtrArray *properties,
607 EmpathyTpChatPriv *priv = GET_PRIV (chat);
611 priv->had_properties_list = TRUE;
614 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
619 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
620 priv->properties = g_ptr_array_sized_new (properties->len);
621 for (i = 0; i < properties->len; i++) {
622 GValueArray *prop_struct;
623 TpChatProperty *property;
625 prop_struct = g_ptr_array_index (properties, i);
626 property = g_slice_new0 (TpChatProperty);
627 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
628 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
629 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
631 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
632 property->name, property->id, property->flags);
633 g_ptr_array_add (priv->properties, property);
634 if (property->flags & TP_PROPERTY_FLAG_READ) {
635 g_array_append_val (ids, property->id);
639 tp_cli_properties_interface_call_get_properties (proxy, -1,
641 tp_chat_get_properties_cb,
645 g_array_free (ids, TRUE);
649 empathy_tp_chat_set_property (EmpathyTpChat *chat,
653 EmpathyTpChatPriv *priv = GET_PRIV (chat);
654 TpChatProperty *property;
657 g_return_if_fail (priv->ready);
659 for (i = 0; i < priv->properties->len; i++) {
660 property = g_ptr_array_index (priv->properties, i);
661 if (!tp_strdiff (property->name, name)) {
662 GPtrArray *properties;
665 GValue dest_value = {0, };
667 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
671 g_value_init (&id, G_TYPE_UINT);
672 g_value_init (&dest_value, G_TYPE_VALUE);
673 g_value_set_uint (&id, property->id);
674 g_value_set_boxed (&dest_value, value);
676 prop = g_value_array_new (2);
677 g_value_array_append (prop, &id);
678 g_value_array_append (prop, &dest_value);
680 properties = g_ptr_array_sized_new (1);
681 g_ptr_array_add (properties, prop);
683 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
684 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
686 (tp_cli_properties_interface_callback_for_set_properties)
688 "Seting property", NULL,
691 g_ptr_array_free (properties, TRUE);
692 g_value_array_free (prop);
700 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
702 EmpathyTpChatPriv *priv = GET_PRIV (chat);
704 empathy_debug (DEBUG_DOMAIN, "Channel ready");
707 if (tp_proxy_has_interface_by_id (priv->channel,
708 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
709 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
711 g_signal_connect (priv->group, "member-added",
712 G_CALLBACK (tp_chat_member_added_cb),
714 g_signal_connect (priv->group, "member-removed",
715 G_CALLBACK (tp_chat_member_removed_cb),
717 g_signal_connect (priv->group, "local-pending",
718 G_CALLBACK (tp_chat_local_pending_cb),
721 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
723 priv->tp_chan->handle);
724 g_object_notify (G_OBJECT (chat), "remote-contact");
727 if (tp_proxy_has_interface_by_id (priv->channel,
728 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
729 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
730 tp_chat_list_properties_cb,
733 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
734 tp_chat_properties_changed_cb,
736 G_OBJECT (chat), NULL);
737 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
738 tp_chat_property_flags_changed_cb,
740 G_OBJECT (chat), NULL);
743 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
745 tp_chat_list_pending_messages_cb,
749 tp_cli_channel_type_text_connect_to_received (priv->channel,
752 G_OBJECT (chat), NULL);
753 tp_cli_channel_type_text_connect_to_sent (priv->channel,
756 G_OBJECT (chat), NULL);
757 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
758 tp_chat_send_error_cb,
760 G_OBJECT (chat), NULL);
761 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
762 tp_chat_state_changed_cb,
764 G_OBJECT (chat), NULL);
765 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
766 tp_chat_state_changed_cb,
768 G_OBJECT (chat), NULL);
772 tp_chat_finalize (GObject *object)
774 EmpathyTpChatPriv *priv = GET_PRIV (object);
777 if (priv->acknowledge && priv->channel) {
778 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
779 tp_cli_channel_call_close (priv->channel, -1,
781 "closing channel", NULL,
786 g_signal_handlers_disconnect_by_func (priv->channel,
787 tp_chat_invalidated_cb,
789 g_object_unref (priv->channel);
792 g_object_unref (priv->tp_chan);
795 if (priv->properties) {
796 for (i = 0; i < priv->properties->len; i++) {
797 TpChatProperty *property;
799 property = g_ptr_array_index (priv->properties, i);
800 g_free (property->name);
801 if (property->value) {
802 tp_g_value_slice_free (property->value);
804 g_slice_free (TpChatProperty, property);
806 g_ptr_array_free (priv->properties, TRUE);
809 if (priv->remote_contact) {
810 g_object_unref (priv->remote_contact);
813 g_object_unref (priv->group);
816 g_object_unref (priv->factory);
817 g_object_unref (priv->user);
818 g_object_unref (priv->account);
819 g_object_unref (priv->mc);
822 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
826 tp_chat_constructor (GType type,
828 GObjectConstructParam *props)
831 EmpathyTpChatPriv *priv;
832 gboolean channel_ready;
834 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
836 priv = GET_PRIV (chat);
837 priv->factory = empathy_contact_factory_new ();
838 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
839 priv->mc = empathy_mission_control_new ();
841 g_signal_connect (priv->channel, "invalidated",
842 G_CALLBACK (tp_chat_invalidated_cb),
845 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
847 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
849 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
850 G_CALLBACK (tp_chat_channel_ready_cb),
858 tp_chat_get_property (GObject *object,
863 EmpathyTpChatPriv *priv = GET_PRIV (object);
867 g_value_set_object (value, priv->account);
870 g_value_set_object (value, priv->tp_chan);
873 g_value_set_object (value, priv->channel);
875 case PROP_ACKNOWLEDGE:
876 g_value_set_boolean (value, priv->acknowledge);
878 case PROP_REMOTE_CONTACT:
879 g_value_set_object (value, priv->remote_contact);
882 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
888 tp_chat_set_property (GObject *object,
893 EmpathyTpChatPriv *priv = GET_PRIV (object);
897 priv->account = g_object_ref (g_value_get_object (value));
900 priv->tp_chan = g_object_ref (g_value_get_object (value));
903 priv->channel = g_object_ref (g_value_get_object (value));
905 case PROP_ACKNOWLEDGE:
906 priv->acknowledge = g_value_get_boolean (value);
909 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
915 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
917 GObjectClass *object_class = G_OBJECT_CLASS (klass);
919 object_class->finalize = tp_chat_finalize;
920 object_class->constructor = tp_chat_constructor;
921 object_class->get_property = tp_chat_get_property;
922 object_class->set_property = tp_chat_set_property;
924 /* Construct properties */
925 g_object_class_install_property (object_class,
927 g_param_spec_object ("account",
929 "The account associated with the channel",
932 G_PARAM_CONSTRUCT_ONLY));
933 g_object_class_install_property (object_class,
935 g_param_spec_object ("tp-chan",
937 "The text channel for the chat",
940 G_PARAM_CONSTRUCT_ONLY));
941 g_object_class_install_property (object_class,
943 g_param_spec_object ("channel",
945 "The text channel for the chat",
948 G_PARAM_CONSTRUCT_ONLY));
949 g_object_class_install_property (object_class,
951 g_param_spec_boolean ("acknowledge",
952 "acknowledge messages",
953 "Wheter or not received messages should be acknowledged",
957 g_object_class_install_property (object_class,
959 g_param_spec_object ("remote-contact",
960 "The remote contact",
961 "The remote contact if there is no group iface on the channel",
962 EMPATHY_TYPE_CONTACT,
964 g_object_class_install_property (object_class,
966 g_param_spec_boolean ("ready",
967 "Is the object ready",
968 "This object is can't be used until this becomes true",
973 signals[MESSAGE_RECEIVED] =
974 g_signal_new ("message-received",
975 G_TYPE_FROM_CLASS (klass),
979 g_cclosure_marshal_VOID__OBJECT,
981 1, EMPATHY_TYPE_MESSAGE);
983 signals[SEND_ERROR] =
984 g_signal_new ("send-error",
985 G_TYPE_FROM_CLASS (klass),
989 _empathy_marshal_VOID__OBJECT_UINT,
991 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
993 signals[CHAT_STATE_CHANGED] =
994 g_signal_new ("chat-state-changed",
995 G_TYPE_FROM_CLASS (klass),
999 _empathy_marshal_VOID__OBJECT_UINT,
1001 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1003 signals[PROPERTY_CHANGED] =
1004 g_signal_new ("property-changed",
1005 G_TYPE_FROM_CLASS (klass),
1009 _empathy_marshal_VOID__STRING_BOXED,
1011 2, G_TYPE_STRING, G_TYPE_VALUE);
1014 g_signal_new ("destroy",
1015 G_TYPE_FROM_CLASS (klass),
1019 g_cclosure_marshal_VOID__VOID,
1023 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1027 empathy_tp_chat_init (EmpathyTpChat *chat)
1032 tp_chat_iface_init (EmpathyContactListIface *iface)
1034 iface->add = tp_chat_add;
1035 iface->remove = tp_chat_remove;
1036 iface->get_members = tp_chat_get_members;
1040 empathy_tp_chat_new (McAccount *account,
1042 gboolean acknowledge)
1044 EmpathyTpChat *chat;
1046 TpConnection *connection;
1050 mc = empathy_mission_control_new ();
1051 tp_conn = mission_control_get_connection (mc, account, NULL);
1052 connection = tp_conn_dup_connection (tp_conn);
1053 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1055 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1059 "acknowledge", acknowledge,
1062 g_object_unref (channel);
1063 g_object_unref (tp_conn);
1064 g_object_unref (connection);
1065 g_object_unref (mc);
1071 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1073 EmpathyTpChat *chat;
1078 const gchar *bus_name;
1081 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1083 mc = empathy_mission_control_new ();
1084 account = empathy_contact_get_account (contact);
1086 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1087 /* The account is not connected. */
1091 tp_conn = mission_control_get_connection (mc, account, NULL);
1092 g_return_val_if_fail (tp_conn != NULL, NULL);
1093 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1094 handle = empathy_contact_get_handle (contact);
1096 text_chan = tp_conn_new_channel (tp_get_bus (),
1099 TP_IFACE_CHANNEL_TYPE_TEXT,
1100 TP_HANDLE_TYPE_CONTACT,
1104 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1106 g_object_unref (tp_conn);
1107 g_object_unref (text_chan);
1108 g_object_unref (mc);
1114 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1116 EmpathyTpChatPriv *priv;
1118 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1120 priv = GET_PRIV (chat);
1122 return priv->tp_chan;
1126 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1128 EmpathyTpChatPriv *priv;
1130 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1132 priv = GET_PRIV (chat);
1134 return priv->account;
1138 empathy_tp_chat_send (EmpathyTpChat *chat,
1139 EmpathyMessage *message)
1141 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1142 const gchar *message_body;
1143 EmpathyMessageType message_type;
1145 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1146 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1147 g_return_if_fail (priv->ready);
1149 message_body = empathy_message_get_body (message);
1150 message_type = empathy_message_get_type (message);
1152 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1153 tp_cli_channel_type_text_call_send (priv->channel, -1,
1157 "sending message", NULL,
1162 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1163 TpChannelChatState state)
1165 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1167 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1168 g_return_if_fail (priv->ready);
1170 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1171 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1174 "setting chat state",
1180 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1182 EmpathyTpChatPriv *priv;
1184 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1186 priv = GET_PRIV (chat);
1189 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1196 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1198 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1200 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1202 return priv->remote_contact;
1206 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1208 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1210 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);