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 pending messages",
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);
443 priv->had_pending_messages = TRUE;
446 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
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);
472 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
478 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
479 g_object_unref (message);
484 tp_chat_property_flags_changed_cb (TpProxy *proxy,
485 const GPtrArray *properties,
489 EmpathyTpChatPriv *priv = GET_PRIV (chat);
492 if (!priv->had_properties_list || !properties) {
496 for (i = 0; i < properties->len; i++) {
497 GValueArray *prop_struct;
498 TpChatProperty *property;
502 prop_struct = g_ptr_array_index (properties, i);
503 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
504 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
506 for (j = 0; j < priv->properties->len; j++) {
507 property = g_ptr_array_index (priv->properties, j);
508 if (property->id == id) {
509 property->flags = flags;
510 empathy_debug (DEBUG_DOMAIN,
511 "property %s flags changed: %d",
512 property->name, property->flags);
520 tp_chat_properties_changed_cb (TpProxy *proxy,
521 const GPtrArray *properties,
525 EmpathyTpChatPriv *priv = GET_PRIV (chat);
528 if (!priv->had_properties_list || !properties) {
532 for (i = 0; i < properties->len; i++) {
533 GValueArray *prop_struct;
534 TpChatProperty *property;
538 prop_struct = g_ptr_array_index (properties, i);
539 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
540 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
542 for (j = 0; j < priv->properties->len; j++) {
543 property = g_ptr_array_index (priv->properties, j);
544 if (property->id == id) {
545 if (property->value) {
546 g_value_copy (src_value, property->value);
548 property->value = tp_g_value_slice_dup (src_value);
551 empathy_debug (DEBUG_DOMAIN, "property %s changed",
553 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
554 property->name, property->value);
562 tp_chat_get_properties_cb (TpProxy *proxy,
563 const GPtrArray *properties,
569 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
574 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
578 tp_chat_list_properties_cb (TpProxy *proxy,
579 const GPtrArray *properties,
584 EmpathyTpChatPriv *priv = GET_PRIV (chat);
588 priv->had_properties_list = TRUE;
591 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
596 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
597 priv->properties = g_ptr_array_sized_new (properties->len);
598 for (i = 0; i < properties->len; i++) {
599 GValueArray *prop_struct;
600 TpChatProperty *property;
602 prop_struct = g_ptr_array_index (properties, i);
603 property = g_slice_new0 (TpChatProperty);
604 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
605 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
606 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
608 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
609 property->name, property->id, property->flags);
610 g_ptr_array_add (priv->properties, property);
611 if (property->flags & TP_PROPERTY_FLAG_READ) {
612 g_array_append_val (ids, property->id);
616 tp_cli_properties_interface_call_get_properties (proxy, -1,
618 tp_chat_get_properties_cb,
622 g_array_free (ids, TRUE);
626 empathy_tp_chat_set_property (EmpathyTpChat *chat,
630 EmpathyTpChatPriv *priv = GET_PRIV (chat);
631 TpChatProperty *property;
634 for (i = 0; i < priv->properties->len; i++) {
635 property = g_ptr_array_index (priv->properties, i);
636 if (!tp_strdiff (property->name, name)) {
637 GPtrArray *properties;
640 GValue dest_value = {0, };
642 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
646 g_value_init (&id, G_TYPE_UINT);
647 g_value_init (&dest_value, G_TYPE_VALUE);
648 g_value_set_uint (&id, property->id);
649 g_value_set_boxed (&dest_value, value);
651 prop = g_value_array_new (2);
652 g_value_array_append (prop, &id);
653 g_value_array_append (prop, &dest_value);
655 properties = g_ptr_array_sized_new (1);
656 g_ptr_array_add (properties, prop);
658 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
659 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
661 (tp_cli_properties_interface_callback_for_set_properties)
663 "Seting property", NULL,
666 g_ptr_array_free (properties, TRUE);
667 g_value_array_free (prop);
675 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
677 EmpathyTpChatPriv *priv = GET_PRIV (chat);
679 empathy_debug (DEBUG_DOMAIN, "Channel ready");
681 if (tp_proxy_has_interface_by_id (priv->channel,
682 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
683 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
685 g_signal_connect (priv->group, "member-added",
686 G_CALLBACK (tp_chat_member_added_cb),
688 g_signal_connect (priv->group, "member-removed",
689 G_CALLBACK (tp_chat_member_removed_cb),
691 g_signal_connect (priv->group, "local-pending",
692 G_CALLBACK (tp_chat_local_pending_cb),
695 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
697 priv->tp_chan->handle);
698 g_object_notify (G_OBJECT (chat), "remote-contact");
701 if (tp_proxy_has_interface_by_id (priv->channel,
702 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
703 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
704 tp_chat_list_properties_cb,
707 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
708 tp_chat_properties_changed_cb,
710 G_OBJECT (chat), NULL);
711 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
712 tp_chat_property_flags_changed_cb,
714 G_OBJECT (chat), NULL);
717 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
719 tp_chat_list_pending_messages_cb,
723 tp_cli_channel_type_text_connect_to_received (priv->channel,
726 G_OBJECT (chat), NULL);
727 tp_cli_channel_type_text_connect_to_sent (priv->channel,
730 G_OBJECT (chat), NULL);
731 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
732 tp_chat_send_error_cb,
734 G_OBJECT (chat), NULL);
735 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
736 tp_chat_state_changed_cb,
738 G_OBJECT (chat), NULL);
739 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
740 tp_chat_state_changed_cb,
742 G_OBJECT (chat), NULL);
746 tp_chat_finalize (GObject *object)
748 EmpathyTpChatPriv *priv = GET_PRIV (object);
751 if (priv->acknowledge && priv->channel) {
752 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
753 tp_cli_channel_call_close (priv->channel, -1,
755 "closing channel", NULL,
760 g_signal_handlers_disconnect_by_func (priv->channel,
761 tp_chat_invalidated_cb,
763 g_object_unref (priv->channel);
766 g_object_unref (priv->tp_chan);
769 if (priv->properties) {
770 for (i = 0; i < priv->properties->len; i++) {
771 TpChatProperty *property;
773 property = g_ptr_array_index (priv->properties, i);
774 g_free (property->name);
775 if (property->value) {
776 tp_g_value_slice_free (property->value);
778 g_slice_free (TpChatProperty, property);
780 g_ptr_array_free (priv->properties, TRUE);
783 if (priv->remote_contact) {
784 g_object_unref (priv->remote_contact);
787 g_object_unref (priv->group);
790 g_object_unref (priv->factory);
791 g_object_unref (priv->user);
792 g_object_unref (priv->account);
793 g_object_unref (priv->mc);
796 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
800 tp_chat_constructor (GType type,
802 GObjectConstructParam *props)
805 EmpathyTpChatPriv *priv;
806 gboolean channel_ready;
808 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
810 priv = GET_PRIV (chat);
811 priv->factory = empathy_contact_factory_new ();
812 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
813 priv->mc = empathy_mission_control_new ();
815 g_signal_connect (priv->channel, "invalidated",
816 G_CALLBACK (tp_chat_invalidated_cb),
819 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
821 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
823 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
824 G_CALLBACK (tp_chat_channel_ready_cb),
832 tp_chat_get_property (GObject *object,
837 EmpathyTpChatPriv *priv = GET_PRIV (object);
841 g_value_set_object (value, priv->account);
844 g_value_set_object (value, priv->tp_chan);
847 g_value_set_object (value, priv->channel);
849 case PROP_ACKNOWLEDGE:
850 g_value_set_boolean (value, priv->acknowledge);
852 case PROP_REMOTE_CONTACT:
853 g_value_set_object (value, priv->remote_contact);
856 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
862 tp_chat_set_property (GObject *object,
867 EmpathyTpChatPriv *priv = GET_PRIV (object);
871 priv->account = g_object_ref (g_value_get_object (value));
874 priv->tp_chan = g_object_ref (g_value_get_object (value));
877 priv->channel = g_object_ref (g_value_get_object (value));
879 case PROP_ACKNOWLEDGE:
880 priv->acknowledge = g_value_get_boolean (value);
883 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
889 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
891 GObjectClass *object_class = G_OBJECT_CLASS (klass);
893 object_class->finalize = tp_chat_finalize;
894 object_class->constructor = tp_chat_constructor;
895 object_class->get_property = tp_chat_get_property;
896 object_class->set_property = tp_chat_set_property;
898 /* Construct properties */
899 g_object_class_install_property (object_class,
901 g_param_spec_object ("account",
903 "The account associated with the channel",
906 G_PARAM_CONSTRUCT_ONLY));
907 g_object_class_install_property (object_class,
909 g_param_spec_object ("tp-chan",
911 "The text channel for the chat",
914 G_PARAM_CONSTRUCT_ONLY));
915 g_object_class_install_property (object_class,
917 g_param_spec_object ("channel",
919 "The text channel for the chat",
922 G_PARAM_CONSTRUCT_ONLY));
923 g_object_class_install_property (object_class,
925 g_param_spec_boolean ("acknowledge",
926 "acknowledge messages",
927 "Wheter or not received messages should be acknowledged",
931 g_object_class_install_property (object_class,
933 g_param_spec_object ("remote-contact",
934 "The remote contact",
935 "The remote contact if there is no group iface on the channel",
936 EMPATHY_TYPE_CONTACT,
940 signals[MESSAGE_RECEIVED] =
941 g_signal_new ("message-received",
942 G_TYPE_FROM_CLASS (klass),
946 g_cclosure_marshal_VOID__OBJECT,
948 1, EMPATHY_TYPE_MESSAGE);
950 signals[SEND_ERROR] =
951 g_signal_new ("send-error",
952 G_TYPE_FROM_CLASS (klass),
956 _empathy_marshal_VOID__OBJECT_UINT,
958 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
960 signals[CHAT_STATE_CHANGED] =
961 g_signal_new ("chat-state-changed",
962 G_TYPE_FROM_CLASS (klass),
966 _empathy_marshal_VOID__OBJECT_UINT,
968 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
970 signals[PROPERTY_CHANGED] =
971 g_signal_new ("property-changed",
972 G_TYPE_FROM_CLASS (klass),
976 _empathy_marshal_VOID__STRING_BOXED,
978 2, G_TYPE_STRING, G_TYPE_VALUE);
981 g_signal_new ("destroy",
982 G_TYPE_FROM_CLASS (klass),
986 g_cclosure_marshal_VOID__VOID,
990 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
994 empathy_tp_chat_init (EmpathyTpChat *chat)
999 tp_chat_iface_init (EmpathyContactListIface *iface)
1001 iface->add = tp_chat_add;
1002 iface->remove = tp_chat_remove;
1003 iface->get_members = tp_chat_get_members;
1007 empathy_tp_chat_new (McAccount *account,
1009 gboolean acknowledge)
1011 EmpathyTpChat *chat;
1013 TpConnection *connection;
1017 mc = empathy_mission_control_new ();
1018 tp_conn = mission_control_get_connection (mc, account, NULL);
1019 connection = tp_conn_dup_connection (tp_conn);
1020 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1022 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1026 "acknowledge", acknowledge,
1029 g_object_unref (channel);
1030 g_object_unref (tp_conn);
1031 g_object_unref (connection);
1032 g_object_unref (mc);
1038 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1040 EmpathyTpChat *chat;
1045 const gchar *bus_name;
1048 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1050 mc = empathy_mission_control_new ();
1051 account = empathy_contact_get_account (contact);
1053 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1054 /* The account is not connected. */
1058 tp_conn = mission_control_get_connection (mc, account, NULL);
1059 g_return_val_if_fail (tp_conn != NULL, NULL);
1060 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1061 handle = empathy_contact_get_handle (contact);
1063 text_chan = tp_conn_new_channel (tp_get_bus (),
1066 TP_IFACE_CHANNEL_TYPE_TEXT,
1067 TP_HANDLE_TYPE_CONTACT,
1071 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1073 g_object_unref (tp_conn);
1074 g_object_unref (text_chan);
1075 g_object_unref (mc);
1081 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1083 EmpathyTpChatPriv *priv;
1085 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1087 priv = GET_PRIV (chat);
1089 return priv->tp_chan;
1093 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1095 EmpathyTpChatPriv *priv;
1097 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1099 priv = GET_PRIV (chat);
1101 return priv->account;
1105 empathy_tp_chat_send (EmpathyTpChat *chat,
1106 EmpathyMessage *message)
1108 EmpathyTpChatPriv *priv;
1109 const gchar *message_body;
1110 EmpathyMessageType message_type;
1112 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1113 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1115 priv = GET_PRIV (chat);
1117 message_body = empathy_message_get_body (message);
1118 message_type = empathy_message_get_type (message);
1120 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1121 tp_cli_channel_type_text_call_send (priv->channel, -1,
1125 "sending message", NULL,
1130 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1131 TpChannelChatState state)
1133 EmpathyTpChatPriv *priv;
1135 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1137 priv = GET_PRIV (chat);
1139 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1140 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1143 "setting chat state",
1149 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1151 EmpathyTpChatPriv *priv;
1153 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1155 priv = GET_PRIV (chat);
1158 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1165 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1167 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1169 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1171 return priv->remote_contact;