1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-debug.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
41 #define DEBUG_DOMAIN "TpChat"
43 struct _EmpathyTpChatPriv {
44 EmpathyContactFactory *factory;
46 EmpathyContact *initiator;
47 EmpathyTpGroup *group;
54 gboolean had_pending_messages;
55 GSList *message_queue;
56 gboolean had_properties_list;
57 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
68 static void empathy_tp_chat_init (EmpathyTpChat *chat);
69 static void tp_chat_iface_init (EmpathyContactListIface *iface);
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
95 tp_chat_invalidated_cb (TpProxy *proxy,
101 EmpathyTpChatPriv *priv = GET_PRIV (chat);
103 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
105 g_object_unref (priv->channel);
106 g_object_unref (priv->tp_chan);
107 priv->channel = NULL;
108 priv->tp_chan = NULL;
110 g_signal_emit (chat, signals[DESTROY], 0);
114 tp_chat_async_cb (TpChannel *proxy,
117 GObject *weak_object)
120 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
121 user_data, error->message);
126 tp_chat_member_added_cb (EmpathyTpGroup *group,
127 EmpathyContact *contact,
128 EmpathyContact *actor,
130 const gchar *message,
133 g_signal_emit_by_name (chat, "members-changed",
134 contact, actor, reason, message,
139 tp_chat_member_removed_cb (EmpathyTpGroup *group,
140 EmpathyContact *contact,
141 EmpathyContact *actor,
143 const gchar *message,
146 g_signal_emit_by_name (chat, "members-changed",
147 contact, actor, reason, message,
151 tp_chat_local_pending_cb (EmpathyTpGroup *group,
152 EmpathyContact *contact,
153 EmpathyContact *actor,
155 const gchar *message,
158 g_signal_emit_by_name (chat, "pendings-changed",
159 contact, actor, reason, message,
164 tp_chat_add (EmpathyContactList *list,
165 EmpathyContact *contact,
166 const gchar *message)
168 EmpathyTpChatPriv *priv = GET_PRIV (list);
170 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
171 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
174 empathy_tp_group_add_member (priv->group, contact, message);
179 tp_chat_remove (EmpathyContactList *list,
180 EmpathyContact *contact,
181 const gchar *message)
183 EmpathyTpChatPriv *priv = GET_PRIV (list);
185 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
186 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
189 empathy_tp_group_remove_member (priv->group, contact, message);
194 tp_chat_get_members (EmpathyContactList *list)
196 EmpathyTpChatPriv *priv = GET_PRIV (list);
197 GList *members = NULL;
199 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
202 members = empathy_tp_group_get_members (priv->group);
204 members = g_list_prepend (members, g_object_ref (priv->user));
205 members = g_list_prepend (members, g_object_ref (priv->initiator));
211 static EmpathyMessage *
212 tp_chat_build_message (EmpathyTpChat *chat,
216 const gchar *message_body)
218 EmpathyTpChatPriv *priv;
219 EmpathyMessage *message;
220 EmpathyContact *sender;
222 priv = GET_PRIV (chat);
224 if (from_handle == 0) {
225 sender = g_object_ref (priv->user);
227 sender = empathy_contact_factory_get_from_handle (priv->factory,
232 message = empathy_message_new (message_body);
233 empathy_message_set_type (message, type);
234 empathy_message_set_sender (message, sender);
235 empathy_message_set_receiver (message, priv->user);
236 empathy_message_set_timestamp (message, timestamp);
238 g_object_unref (sender);
244 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
245 GParamSpec *param_spec,
248 EmpathyTpChatPriv *priv = GET_PRIV (chat);
249 EmpathyMessage *message;
250 EmpathyContactReady ready;
251 EmpathyContact *sender;
252 gboolean removed = FALSE;
254 /* Emit all messages queued until we find a message with not
255 * ready sender. When leaving this loop, sender is the first not ready
256 * contact queued and removed tells if at least one message got removed
258 while (priv->message_queue) {
259 message = priv->message_queue->data;
260 sender = empathy_message_get_sender (message);
261 ready = empathy_contact_get_ready (sender);
263 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
267 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
268 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
269 priv->message_queue = g_slist_remove (priv->message_queue,
271 g_object_unref (message);
276 g_signal_handlers_disconnect_by_func (contact,
277 tp_chat_sender_ready_notify_cb,
280 if (priv->message_queue) {
281 g_signal_connect (sender, "notify::ready",
282 G_CALLBACK (tp_chat_sender_ready_notify_cb),
289 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
290 EmpathyMessage *message)
292 EmpathyTpChatPriv *priv = GET_PRIV (chat);
293 EmpathyContact *sender;
294 EmpathyContactReady ready;
296 if (priv->message_queue != NULL) {
297 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
298 priv->message_queue = g_slist_append (priv->message_queue,
299 g_object_ref (message));
303 sender = empathy_message_get_sender (message);
304 ready = empathy_contact_get_ready (sender);
305 if (ready & EMPATHY_CONTACT_READY_NAME) {
306 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
307 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
311 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
312 priv->message_queue = g_slist_append (priv->message_queue,
313 g_object_ref (message));
314 g_signal_connect (sender, "notify::ready",
315 G_CALLBACK (tp_chat_sender_ready_notify_cb),
320 tp_chat_received_cb (TpChannel *channel,
326 const gchar *message_body,
330 EmpathyTpChatPriv *priv = GET_PRIV (chat);
331 EmpathyMessage *message;
333 if (!priv->had_pending_messages) {
337 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
339 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
345 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
346 g_object_unref (message);
348 if (priv->acknowledge) {
351 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
352 g_array_append_val (message_ids, message_id);
353 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
357 "acknowledging pending messages",
360 g_array_free (message_ids, TRUE);
365 tp_chat_sent_cb (TpChannel *channel,
368 const gchar *message_body,
372 EmpathyMessage *message;
374 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
376 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
382 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
383 g_object_unref (message);
387 tp_chat_send_error_cb (TpChannel *channel,
391 const gchar *message_body,
395 EmpathyMessage *message;
397 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
398 message_body, error_code);
400 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
406 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
407 g_object_unref (message);
411 tp_chat_state_changed_cb (TpChannel *channel,
417 EmpathyTpChatPriv *priv = GET_PRIV (chat);
418 EmpathyContact *contact;
420 contact = empathy_contact_factory_get_from_handle (priv->factory,
424 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
425 empathy_contact_get_name (contact),
428 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
429 g_object_unref (contact);
433 tp_chat_list_pending_messages_cb (TpChannel *channel,
434 const GPtrArray *messages_list,
439 EmpathyTpChatPriv *priv = GET_PRIV (chat);
442 priv->had_pending_messages = TRUE;
445 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
450 for (i = 0; i < messages_list->len; i++) {
451 EmpathyMessage *message;
452 GValueArray *message_struct;
453 const gchar *message_body;
460 message_struct = g_ptr_array_index (messages_list, i);
462 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
463 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
464 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
465 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
466 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
467 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
469 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
471 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
477 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
478 g_object_unref (message);
483 tp_chat_property_flags_changed_cb (TpProxy *proxy,
484 const GPtrArray *properties,
488 EmpathyTpChatPriv *priv = GET_PRIV (chat);
491 if (!priv->had_properties_list || !properties) {
495 for (i = 0; i < properties->len; i++) {
496 GValueArray *prop_struct;
497 TpChatProperty *property;
501 prop_struct = g_ptr_array_index (properties, i);
502 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
503 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
505 for (j = 0; j < priv->properties->len; j++) {
506 property = g_ptr_array_index (priv->properties, j);
507 if (property->id == id) {
508 property->flags = flags;
509 empathy_debug (DEBUG_DOMAIN,
510 "property %s flags changed: %d",
511 property->name, property->flags);
519 tp_chat_properties_changed_cb (TpProxy *proxy,
520 const GPtrArray *properties,
524 EmpathyTpChatPriv *priv = GET_PRIV (chat);
527 if (!priv->had_properties_list || !properties) {
531 for (i = 0; i < properties->len; i++) {
532 GValueArray *prop_struct;
533 TpChatProperty *property;
537 prop_struct = g_ptr_array_index (properties, i);
538 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
539 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
541 for (j = 0; j < priv->properties->len; j++) {
542 property = g_ptr_array_index (priv->properties, j);
543 if (property->id == id) {
544 if (property->value) {
545 g_value_copy (src_value, property->value);
547 property->value = tp_g_value_slice_dup (src_value);
550 empathy_debug (DEBUG_DOMAIN, "property %s changed",
552 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
553 property->name, property->value);
561 tp_chat_get_properties_cb (TpProxy *proxy,
562 const GPtrArray *properties,
568 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
573 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
577 tp_chat_list_properties_cb (TpProxy *proxy,
578 const GPtrArray *properties,
583 EmpathyTpChatPriv *priv = GET_PRIV (chat);
587 priv->had_properties_list = TRUE;
590 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
595 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
596 priv->properties = g_ptr_array_sized_new (properties->len);
597 for (i = 0; i < properties->len; i++) {
598 GValueArray *prop_struct;
599 TpChatProperty *property;
601 prop_struct = g_ptr_array_index (properties, i);
602 property = g_slice_new0 (TpChatProperty);
603 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
604 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
605 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
607 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
608 property->name, property->id, property->flags);
609 g_ptr_array_add (priv->properties, property);
610 if (property->flags & TP_PROPERTY_FLAG_READ) {
611 g_array_append_val (ids, property->id);
615 tp_cli_properties_interface_call_get_properties (proxy, -1,
617 tp_chat_get_properties_cb,
621 g_array_free (ids, TRUE);
625 empathy_tp_chat_set_property (EmpathyTpChat *chat,
629 EmpathyTpChatPriv *priv = GET_PRIV (chat);
630 TpChatProperty *property;
633 for (i = 0; i < priv->properties->len; i++) {
634 property = g_ptr_array_index (priv->properties, i);
635 if (!tp_strdiff (property->name, name)) {
636 GPtrArray *properties;
639 GValue dest_value = {0, };
641 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
645 g_value_init (&id, G_TYPE_UINT);
646 g_value_init (&dest_value, G_TYPE_VALUE);
647 g_value_set_uint (&id, property->id);
648 g_value_set_boxed (&dest_value, value);
650 prop = g_value_array_new (2);
651 g_value_array_append (prop, &id);
652 g_value_array_append (prop, &dest_value);
654 properties = g_ptr_array_sized_new (1);
655 g_ptr_array_add (properties, prop);
657 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
658 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
660 (tp_cli_properties_interface_callback_for_set_properties)
662 "Seting property", NULL,
665 g_ptr_array_free (properties, TRUE);
666 g_value_array_free (prop);
674 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
676 EmpathyTpChatPriv *priv = GET_PRIV (chat);
678 empathy_debug (DEBUG_DOMAIN, "Channel ready");
680 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
682 tp_chat_list_pending_messages_cb,
685 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
686 tp_chat_list_properties_cb,
691 tp_cli_channel_type_text_connect_to_received (priv->channel,
694 G_OBJECT (chat), NULL);
695 tp_cli_channel_type_text_connect_to_sent (priv->channel,
698 G_OBJECT (chat), NULL);
699 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
700 tp_chat_send_error_cb,
702 G_OBJECT (chat), NULL);
703 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
704 tp_chat_state_changed_cb,
706 G_OBJECT (chat), NULL);
707 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
708 tp_chat_state_changed_cb,
710 G_OBJECT (chat), NULL);
711 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
712 tp_chat_properties_changed_cb,
714 G_OBJECT (chat), NULL);
715 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
716 tp_chat_property_flags_changed_cb,
718 G_OBJECT (chat), NULL);
724 tp_chat_finalize (GObject *object)
726 EmpathyTpChatPriv *priv = GET_PRIV (object);
729 if (priv->acknowledge && priv->channel) {
730 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
731 tp_cli_channel_call_close (priv->channel, -1,
733 "closing channel", NULL,
738 g_signal_handlers_disconnect_by_func (priv->channel,
739 tp_chat_invalidated_cb,
741 g_object_unref (priv->channel);
744 g_object_unref (priv->tp_chan);
747 if (priv->properties) {
748 for (i = 0; i < priv->properties->len; i++) {
749 TpChatProperty *property;
751 property = g_ptr_array_index (priv->properties, i);
752 g_free (property->name);
753 if (property->value) {
754 tp_g_value_slice_free (property->value);
756 g_slice_free (TpChatProperty, property);
758 g_ptr_array_free (priv->properties, TRUE);
761 if (priv->initiator) {
762 g_object_unref (priv->initiator);
765 g_object_unref (priv->group);
768 g_object_unref (priv->factory);
769 g_object_unref (priv->user);
770 g_object_unref (priv->account);
771 g_object_unref (priv->mc);
774 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
778 tp_chat_constructor (GType type,
780 GObjectConstructParam *props)
783 EmpathyTpChatPriv *priv;
784 gboolean channel_ready;
786 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
788 priv = GET_PRIV (chat);
789 priv->factory = empathy_contact_factory_new ();
790 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
791 priv->mc = empathy_mission_control_new ();
793 g_signal_connect (priv->channel, "invalidated",
794 G_CALLBACK (tp_chat_invalidated_cb),
797 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
799 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
801 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
802 G_CALLBACK (tp_chat_channel_ready_cb),
806 if (tp_proxy_has_interface_by_id (priv->channel,
807 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
808 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
810 g_signal_connect (priv->group, "member-added",
811 G_CALLBACK (tp_chat_member_added_cb),
813 g_signal_connect (priv->group, "member-removed",
814 G_CALLBACK (tp_chat_member_removed_cb),
816 g_signal_connect (priv->group, "local-pending",
817 G_CALLBACK (tp_chat_local_pending_cb),
820 priv->initiator = empathy_contact_factory_get_from_handle (priv->factory,
822 priv->tp_chan->handle);
829 tp_chat_get_property (GObject *object,
834 EmpathyTpChatPriv *priv = GET_PRIV (object);
838 g_value_set_object (value, priv->account);
841 g_value_set_object (value, priv->tp_chan);
844 g_value_set_object (value, priv->channel);
846 case PROP_ACKNOWLEDGE:
847 g_value_set_boolean (value, priv->acknowledge);
850 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
856 tp_chat_set_property (GObject *object,
861 EmpathyTpChatPriv *priv = GET_PRIV (object);
865 priv->account = g_object_ref (g_value_get_object (value));
868 priv->tp_chan = g_object_ref (g_value_get_object (value));
871 priv->channel = g_object_ref (g_value_get_object (value));
873 case PROP_ACKNOWLEDGE:
874 priv->acknowledge = g_value_get_boolean (value);
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
883 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
885 GObjectClass *object_class = G_OBJECT_CLASS (klass);
887 object_class->finalize = tp_chat_finalize;
888 object_class->constructor = tp_chat_constructor;
889 object_class->get_property = tp_chat_get_property;
890 object_class->set_property = tp_chat_set_property;
892 /* Construct properties */
893 g_object_class_install_property (object_class,
895 g_param_spec_object ("account",
897 "The account associated with the channel",
900 G_PARAM_CONSTRUCT_ONLY));
901 g_object_class_install_property (object_class,
903 g_param_spec_object ("tp-chan",
905 "The text channel for the chat",
908 G_PARAM_CONSTRUCT_ONLY));
910 g_object_class_install_property (object_class,
912 g_param_spec_object ("channel",
914 "The text channel for the chat",
917 G_PARAM_CONSTRUCT_ONLY));
919 g_object_class_install_property (object_class,
921 g_param_spec_boolean ("acknowledge",
922 "acknowledge messages",
923 "Wheter or not received messages should be acknowledged",
926 G_PARAM_CONSTRUCT_ONLY));
929 signals[MESSAGE_RECEIVED] =
930 g_signal_new ("message-received",
931 G_TYPE_FROM_CLASS (klass),
935 g_cclosure_marshal_VOID__OBJECT,
937 1, EMPATHY_TYPE_MESSAGE);
939 signals[SEND_ERROR] =
940 g_signal_new ("send-error",
941 G_TYPE_FROM_CLASS (klass),
945 _empathy_marshal_VOID__OBJECT_UINT,
947 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
949 signals[CHAT_STATE_CHANGED] =
950 g_signal_new ("chat-state-changed",
951 G_TYPE_FROM_CLASS (klass),
955 _empathy_marshal_VOID__OBJECT_UINT,
957 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
959 signals[PROPERTY_CHANGED] =
960 g_signal_new ("property-changed",
961 G_TYPE_FROM_CLASS (klass),
965 _empathy_marshal_VOID__STRING_BOXED,
967 2, G_TYPE_STRING, G_TYPE_VALUE);
970 g_signal_new ("destroy",
971 G_TYPE_FROM_CLASS (klass),
975 g_cclosure_marshal_VOID__VOID,
979 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
983 empathy_tp_chat_init (EmpathyTpChat *chat)
988 tp_chat_iface_init (EmpathyContactListIface *iface)
990 iface->add = tp_chat_add;
991 iface->remove = tp_chat_remove;
992 iface->get_members = tp_chat_get_members;
996 empathy_tp_chat_new (McAccount *account,
998 gboolean acknowledge)
1000 EmpathyTpChat *chat;
1002 TpConnection *connection;
1006 mc = empathy_mission_control_new ();
1007 tp_conn = mission_control_get_connection (mc, account, NULL);
1008 connection = tp_conn_dup_connection (tp_conn);
1009 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1011 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
1015 "acknowledge", acknowledge,
1018 g_object_unref (channel);
1019 g_object_unref (tp_conn);
1020 g_object_unref (connection);
1021 g_object_unref (mc);
1027 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1029 EmpathyTpChat *chat;
1034 const gchar *bus_name;
1037 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1039 mc = empathy_mission_control_new ();
1040 account = empathy_contact_get_account (contact);
1042 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1043 /* The account is not connected. */
1047 tp_conn = mission_control_get_connection (mc, account, NULL);
1048 g_return_val_if_fail (tp_conn != NULL, NULL);
1049 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1050 handle = empathy_contact_get_handle (contact);
1052 text_chan = tp_conn_new_channel (tp_get_bus (),
1055 TP_IFACE_CHANNEL_TYPE_TEXT,
1056 TP_HANDLE_TYPE_CONTACT,
1060 chat = empathy_tp_chat_new (account, text_chan, TRUE);
1062 g_object_unref (tp_conn);
1063 g_object_unref (text_chan);
1064 g_object_unref (mc);
1070 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1072 EmpathyTpChatPriv *priv;
1074 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1076 priv = GET_PRIV (chat);
1078 return priv->tp_chan;
1082 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1084 EmpathyTpChatPriv *priv;
1086 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1088 priv = GET_PRIV (chat);
1090 return priv->account;
1094 empathy_tp_chat_send (EmpathyTpChat *chat,
1095 EmpathyMessage *message)
1097 EmpathyTpChatPriv *priv;
1098 const gchar *message_body;
1099 EmpathyMessageType message_type;
1101 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1102 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1104 priv = GET_PRIV (chat);
1106 message_body = empathy_message_get_body (message);
1107 message_type = empathy_message_get_type (message);
1109 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1110 tp_cli_channel_type_text_call_send (priv->channel, -1,
1114 "sending message", NULL,
1119 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1120 TpChannelChatState state)
1122 EmpathyTpChatPriv *priv;
1124 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1126 priv = GET_PRIV (chat);
1128 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1129 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1132 "setting chat state",
1138 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1140 EmpathyTpChatPriv *priv;
1142 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1144 priv = GET_PRIV (chat);
1147 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);