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;
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);
89 static guint signals[LAST_SIGNAL];
91 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
92 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
96 tp_chat_invalidated_cb (TpProxy *proxy,
102 EmpathyTpChatPriv *priv = GET_PRIV (chat);
104 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
106 g_object_unref (priv->channel);
107 g_object_unref (priv->tp_chan);
108 priv->channel = NULL;
109 priv->tp_chan = NULL;
111 g_signal_emit (chat, signals[DESTROY], 0);
115 tp_chat_async_cb (TpChannel *proxy,
118 GObject *weak_object)
121 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
122 user_data, error->message);
127 tp_chat_member_added_cb (EmpathyTpGroup *group,
128 EmpathyContact *contact,
129 EmpathyContact *actor,
131 const gchar *message,
134 g_signal_emit_by_name (chat, "members-changed",
135 contact, actor, reason, message,
140 tp_chat_member_removed_cb (EmpathyTpGroup *group,
141 EmpathyContact *contact,
142 EmpathyContact *actor,
144 const gchar *message,
147 g_signal_emit_by_name (chat, "members-changed",
148 contact, actor, reason, message,
152 tp_chat_local_pending_cb (EmpathyTpGroup *group,
153 EmpathyContact *contact,
154 EmpathyContact *actor,
156 const gchar *message,
159 g_signal_emit_by_name (chat, "pendings-changed",
160 contact, actor, reason, message,
165 tp_chat_add (EmpathyContactList *list,
166 EmpathyContact *contact,
167 const gchar *message)
169 EmpathyTpChatPriv *priv = GET_PRIV (list);
171 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
172 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
175 empathy_tp_group_add_member (priv->group, contact, message);
180 tp_chat_remove (EmpathyContactList *list,
181 EmpathyContact *contact,
182 const gchar *message)
184 EmpathyTpChatPriv *priv = GET_PRIV (list);
186 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
187 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
190 empathy_tp_group_remove_member (priv->group, contact, message);
195 tp_chat_get_members (EmpathyContactList *list)
197 EmpathyTpChatPriv *priv = GET_PRIV (list);
198 GList *members = NULL;
200 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
203 members = empathy_tp_group_get_members (priv->group);
205 members = g_list_prepend (members, g_object_ref (priv->user));
206 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
212 static EmpathyMessage *
213 tp_chat_build_message (EmpathyTpChat *chat,
217 const gchar *message_body)
219 EmpathyTpChatPriv *priv;
220 EmpathyMessage *message;
221 EmpathyContact *sender;
223 priv = GET_PRIV (chat);
225 if (from_handle == 0) {
226 sender = g_object_ref (priv->user);
228 sender = empathy_contact_factory_get_from_handle (priv->factory,
233 message = empathy_message_new (message_body);
234 empathy_message_set_type (message, type);
235 empathy_message_set_sender (message, sender);
236 empathy_message_set_receiver (message, priv->user);
237 empathy_message_set_timestamp (message, timestamp);
239 g_object_unref (sender);
245 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
246 GParamSpec *param_spec,
249 EmpathyTpChatPriv *priv = GET_PRIV (chat);
250 EmpathyMessage *message;
251 EmpathyContactReady ready;
252 EmpathyContact *sender;
253 gboolean removed = FALSE;
255 /* Emit all messages queued until we find a message with not
256 * ready sender. When leaving this loop, sender is the first not ready
257 * contact queued and removed tells if at least one message got removed
259 while (priv->message_queue) {
260 message = priv->message_queue->data;
261 sender = empathy_message_get_sender (message);
262 ready = empathy_contact_get_ready (sender);
264 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
268 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
269 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
270 priv->message_queue = g_slist_remove (priv->message_queue,
272 g_object_unref (message);
277 g_signal_handlers_disconnect_by_func (contact,
278 tp_chat_sender_ready_notify_cb,
281 if (priv->message_queue) {
282 g_signal_connect (sender, "notify::ready",
283 G_CALLBACK (tp_chat_sender_ready_notify_cb),
290 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
291 EmpathyMessage *message)
293 EmpathyTpChatPriv *priv = GET_PRIV (chat);
294 EmpathyContact *sender;
295 EmpathyContactReady ready;
297 if (priv->message_queue != NULL) {
298 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
299 priv->message_queue = g_slist_append (priv->message_queue,
300 g_object_ref (message));
304 sender = empathy_message_get_sender (message);
305 ready = empathy_contact_get_ready (sender);
306 if (ready & EMPATHY_CONTACT_READY_NAME) {
307 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
308 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
312 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
313 priv->message_queue = g_slist_append (priv->message_queue,
314 g_object_ref (message));
315 g_signal_connect (sender, "notify::ready",
316 G_CALLBACK (tp_chat_sender_ready_notify_cb),
321 tp_chat_received_cb (TpChannel *channel,
327 const gchar *message_body,
331 EmpathyTpChatPriv *priv = GET_PRIV (chat);
332 EmpathyMessage *message;
334 if (!priv->had_pending_messages) {
338 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
340 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
346 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
347 g_object_unref (message);
349 if (priv->acknowledge) {
352 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
353 g_array_append_val (message_ids, message_id);
354 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
358 "acknowledging received message",
361 g_array_free (message_ids, TRUE);
366 tp_chat_sent_cb (TpChannel *channel,
369 const gchar *message_body,
373 EmpathyMessage *message;
375 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
377 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
383 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
384 g_object_unref (message);
388 tp_chat_send_error_cb (TpChannel *channel,
392 const gchar *message_body,
396 EmpathyMessage *message;
398 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
399 message_body, error_code);
401 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
407 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
408 g_object_unref (message);
412 tp_chat_state_changed_cb (TpChannel *channel,
418 EmpathyTpChatPriv *priv = GET_PRIV (chat);
419 EmpathyContact *contact;
421 contact = empathy_contact_factory_get_from_handle (priv->factory,
425 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
426 empathy_contact_get_name (contact),
429 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
430 g_object_unref (contact);
434 tp_chat_list_pending_messages_cb (TpChannel *channel,
435 const GPtrArray *messages_list,
440 EmpathyTpChatPriv *priv = GET_PRIV (chat);
442 GArray *message_ids = NULL;
444 priv->had_pending_messages = TRUE;
447 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
452 if (priv->acknowledge) {
453 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
457 for (i = 0; i < messages_list->len; i++) {
458 EmpathyMessage *message;
459 GValueArray *message_struct;
460 const gchar *message_body;
467 message_struct = g_ptr_array_index (messages_list, i);
469 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
470 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
471 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
472 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
473 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
474 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
476 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
479 g_array_append_val (message_ids, message_id);
482 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
488 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
489 g_object_unref (message);
493 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
497 "acknowledging pending messages",
500 g_array_free (message_ids, TRUE);
505 tp_chat_property_flags_changed_cb (TpProxy *proxy,
506 const GPtrArray *properties,
510 EmpathyTpChatPriv *priv = GET_PRIV (chat);
513 if (!priv->had_properties_list || !properties) {
517 for (i = 0; i < properties->len; i++) {
518 GValueArray *prop_struct;
519 TpChatProperty *property;
523 prop_struct = g_ptr_array_index (properties, i);
524 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
525 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
527 for (j = 0; j < priv->properties->len; j++) {
528 property = g_ptr_array_index (priv->properties, j);
529 if (property->id == id) {
530 property->flags = flags;
531 empathy_debug (DEBUG_DOMAIN,
532 "property %s flags changed: %d",
533 property->name, property->flags);
541 tp_chat_properties_changed_cb (TpProxy *proxy,
542 const GPtrArray *properties,
546 EmpathyTpChatPriv *priv = GET_PRIV (chat);
549 if (!priv->had_properties_list || !properties) {
553 for (i = 0; i < properties->len; i++) {
554 GValueArray *prop_struct;
555 TpChatProperty *property;
559 prop_struct = g_ptr_array_index (properties, i);
560 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
561 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
563 for (j = 0; j < priv->properties->len; j++) {
564 property = g_ptr_array_index (priv->properties, j);
565 if (property->id == id) {
566 if (property->value) {
567 g_value_copy (src_value, property->value);
569 property->value = tp_g_value_slice_dup (src_value);
572 empathy_debug (DEBUG_DOMAIN, "property %s changed",
574 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
575 property->name, property->value);
583 tp_chat_get_properties_cb (TpProxy *proxy,
584 const GPtrArray *properties,
590 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
595 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
599 tp_chat_list_properties_cb (TpProxy *proxy,
600 const GPtrArray *properties,
605 EmpathyTpChatPriv *priv = GET_PRIV (chat);
609 priv->had_properties_list = TRUE;
612 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
617 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
618 priv->properties = g_ptr_array_sized_new (properties->len);
619 for (i = 0; i < properties->len; i++) {
620 GValueArray *prop_struct;
621 TpChatProperty *property;
623 prop_struct = g_ptr_array_index (properties, i);
624 property = g_slice_new0 (TpChatProperty);
625 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
626 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
627 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
629 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
630 property->name, property->id, property->flags);
631 g_ptr_array_add (priv->properties, property);
632 if (property->flags & TP_PROPERTY_FLAG_READ) {
633 g_array_append_val (ids, property->id);
637 tp_cli_properties_interface_call_get_properties (proxy, -1,
639 tp_chat_get_properties_cb,
643 g_array_free (ids, TRUE);
647 empathy_tp_chat_set_property (EmpathyTpChat *chat,
651 EmpathyTpChatPriv *priv = GET_PRIV (chat);
652 TpChatProperty *property;
655 for (i = 0; i < priv->properties->len; i++) {
656 property = g_ptr_array_index (priv->properties, i);
657 if (!tp_strdiff (property->name, name)) {
658 GPtrArray *properties;
661 GValue dest_value = {0, };
663 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
667 g_value_init (&id, G_TYPE_UINT);
668 g_value_init (&dest_value, G_TYPE_VALUE);
669 g_value_set_uint (&id, property->id);
670 g_value_set_boxed (&dest_value, value);
672 prop = g_value_array_new (2);
673 g_value_array_append (prop, &id);
674 g_value_array_append (prop, &dest_value);
676 properties = g_ptr_array_sized_new (1);
677 g_ptr_array_add (properties, prop);
679 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
680 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
682 (tp_cli_properties_interface_callback_for_set_properties)
684 "Seting property", NULL,
687 g_ptr_array_free (properties, TRUE);
688 g_value_array_free (prop);
696 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
698 EmpathyTpChatPriv *priv = GET_PRIV (chat);
700 empathy_debug (DEBUG_DOMAIN, "Channel ready");
702 if (tp_proxy_has_interface_by_id (priv->channel,
703 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
704 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
706 g_signal_connect (priv->group, "member-added",
707 G_CALLBACK (tp_chat_member_added_cb),
709 g_signal_connect (priv->group, "member-removed",
710 G_CALLBACK (tp_chat_member_removed_cb),
712 g_signal_connect (priv->group, "local-pending",
713 G_CALLBACK (tp_chat_local_pending_cb),
716 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
718 priv->tp_chan->handle);
719 g_object_notify (G_OBJECT (chat), "remote-contact");
722 if (tp_proxy_has_interface_by_id (priv->channel,
723 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
724 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
725 tp_chat_list_properties_cb,
728 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
729 tp_chat_properties_changed_cb,
731 G_OBJECT (chat), NULL);
732 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
733 tp_chat_property_flags_changed_cb,
735 G_OBJECT (chat), NULL);
738 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
740 tp_chat_list_pending_messages_cb,
744 tp_cli_channel_type_text_connect_to_received (priv->channel,
747 G_OBJECT (chat), NULL);
748 tp_cli_channel_type_text_connect_to_sent (priv->channel,
751 G_OBJECT (chat), NULL);
752 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
753 tp_chat_send_error_cb,
755 G_OBJECT (chat), NULL);
756 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
757 tp_chat_state_changed_cb,
759 G_OBJECT (chat), NULL);
760 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
761 tp_chat_state_changed_cb,
763 G_OBJECT (chat), NULL);
767 tp_chat_finalize (GObject *object)
769 EmpathyTpChatPriv *priv = GET_PRIV (object);
772 if (priv->acknowledge && priv->channel) {
773 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
774 tp_cli_channel_call_close (priv->channel, -1,
776 "closing channel", NULL,
781 g_signal_handlers_disconnect_by_func (priv->channel,
782 tp_chat_invalidated_cb,
784 g_object_unref (priv->channel);
787 g_object_unref (priv->tp_chan);
790 if (priv->properties) {
791 for (i = 0; i < priv->properties->len; i++) {
792 TpChatProperty *property;
794 property = g_ptr_array_index (priv->properties, i);
795 g_free (property->name);
796 if (property->value) {
797 tp_g_value_slice_free (property->value);
799 g_slice_free (TpChatProperty, property);
801 g_ptr_array_free (priv->properties, TRUE);
804 if (priv->remote_contact) {
805 g_object_unref (priv->remote_contact);
808 g_object_unref (priv->group);
811 g_object_unref (priv->factory);
812 g_object_unref (priv->user);
813 g_object_unref (priv->account);
814 g_object_unref (priv->mc);
817 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
821 tp_chat_constructor (GType type,
823 GObjectConstructParam *props)
826 EmpathyTpChatPriv *priv;
827 gboolean channel_ready;
829 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
831 priv = GET_PRIV (chat);
832 priv->factory = empathy_contact_factory_new ();
833 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
834 priv->mc = empathy_mission_control_new ();
836 g_signal_connect (priv->channel, "invalidated",
837 G_CALLBACK (tp_chat_invalidated_cb),
840 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
842 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
844 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
845 G_CALLBACK (tp_chat_channel_ready_cb),
853 tp_chat_get_property (GObject *object,
858 EmpathyTpChatPriv *priv = GET_PRIV (object);
862 g_value_set_object (value, priv->account);
865 g_value_set_object (value, priv->tp_chan);
868 g_value_set_object (value, priv->channel);
870 case PROP_ACKNOWLEDGE:
871 g_value_set_boolean (value, priv->acknowledge);
873 case PROP_REMOTE_CONTACT:
874 g_value_set_object (value, priv->remote_contact);
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 /* Construct properties */
920 g_object_class_install_property (object_class,
922 g_param_spec_object ("account",
924 "The account associated with the channel",
927 G_PARAM_CONSTRUCT_ONLY));
928 g_object_class_install_property (object_class,
930 g_param_spec_object ("tp-chan",
932 "The text channel for the chat",
935 G_PARAM_CONSTRUCT_ONLY));
936 g_object_class_install_property (object_class,
938 g_param_spec_object ("channel",
940 "The text channel for the chat",
943 G_PARAM_CONSTRUCT_ONLY));
944 g_object_class_install_property (object_class,
946 g_param_spec_boolean ("acknowledge",
947 "acknowledge messages",
948 "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,
961 signals[MESSAGE_RECEIVED] =
962 g_signal_new ("message-received",
963 G_TYPE_FROM_CLASS (klass),
967 g_cclosure_marshal_VOID__OBJECT,
969 1, EMPATHY_TYPE_MESSAGE);
971 signals[SEND_ERROR] =
972 g_signal_new ("send-error",
973 G_TYPE_FROM_CLASS (klass),
977 _empathy_marshal_VOID__OBJECT_UINT,
979 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
981 signals[CHAT_STATE_CHANGED] =
982 g_signal_new ("chat-state-changed",
983 G_TYPE_FROM_CLASS (klass),
987 _empathy_marshal_VOID__OBJECT_UINT,
989 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
991 signals[PROPERTY_CHANGED] =
992 g_signal_new ("property-changed",
993 G_TYPE_FROM_CLASS (klass),
997 _empathy_marshal_VOID__STRING_BOXED,
999 2, G_TYPE_STRING, G_TYPE_VALUE);
1002 g_signal_new ("destroy",
1003 G_TYPE_FROM_CLASS (klass),
1007 g_cclosure_marshal_VOID__VOID,
1011 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1015 empathy_tp_chat_init (EmpathyTpChat *chat)
1020 tp_chat_iface_init (EmpathyContactListIface *iface)
1022 iface->add = tp_chat_add;
1023 iface->remove = tp_chat_remove;
1024 iface->get_members = tp_chat_get_members;
1028 empathy_tp_chat_new (McAccount *account,
1030 gboolean acknowledge)
1032 EmpathyTpChat *chat;
1034 TpConnection *connection;
1038 mc = empathy_mission_control_new ();
1039 tp_conn = mission_control_get_connection (mc, account, NULL);
1040 connection = tp_conn_dup_connection (tp_conn);
1041 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1043 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1047 "acknowledge", acknowledge,
1050 g_object_unref (channel);
1051 g_object_unref (tp_conn);
1052 g_object_unref (connection);
1053 g_object_unref (mc);
1059 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1061 EmpathyTpChat *chat;
1066 const gchar *bus_name;
1069 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1071 mc = empathy_mission_control_new ();
1072 account = empathy_contact_get_account (contact);
1074 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1075 /* The account is not connected. */
1079 tp_conn = mission_control_get_connection (mc, account, NULL);
1080 g_return_val_if_fail (tp_conn != NULL, NULL);
1081 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1082 handle = empathy_contact_get_handle (contact);
1084 text_chan = tp_conn_new_channel (tp_get_bus (),
1087 TP_IFACE_CHANNEL_TYPE_TEXT,
1088 TP_HANDLE_TYPE_CONTACT,
1092 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1094 g_object_unref (tp_conn);
1095 g_object_unref (text_chan);
1096 g_object_unref (mc);
1102 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1104 EmpathyTpChatPriv *priv;
1106 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1108 priv = GET_PRIV (chat);
1110 return priv->tp_chan;
1114 empathy_tp_chat_get_account (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->account;
1126 empathy_tp_chat_send (EmpathyTpChat *chat,
1127 EmpathyMessage *message)
1129 EmpathyTpChatPriv *priv;
1130 const gchar *message_body;
1131 EmpathyMessageType message_type;
1133 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1134 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1136 priv = GET_PRIV (chat);
1138 message_body = empathy_message_get_body (message);
1139 message_type = empathy_message_get_type (message);
1141 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1142 tp_cli_channel_type_text_call_send (priv->channel, -1,
1146 "sending message", NULL,
1151 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1152 TpChannelChatState state)
1154 EmpathyTpChatPriv *priv;
1156 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1158 priv = GET_PRIV (chat);
1160 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1161 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1164 "setting chat state",
1170 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1172 EmpathyTpChatPriv *priv;
1174 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1176 priv = GET_PRIV (chat);
1179 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1186 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1188 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1190 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1192 return priv->remote_contact;