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-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
42 EmpathyContactFactory *factory;
44 EmpathyContact *remote_contact;
45 EmpathyTpGroup *group;
50 gboolean listing_pending_messages;
51 GSList *message_queue;
52 gboolean had_properties_list;
53 GPtrArray *properties;
61 TpPropertyFlags flags;
65 static void tp_chat_iface_init (EmpathyContactListIface *iface);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 tp_chat_invalidated_cb (TpProxy *proxy,
97 DEBUG ("Channel invalidated: %s", message);
98 g_signal_emit (chat, signals[DESTROY], 0);
102 tp_chat_async_cb (TpChannel *proxy,
105 GObject *weak_object)
108 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
113 tp_chat_member_added_cb (EmpathyTpGroup *group,
114 EmpathyContact *contact,
115 EmpathyContact *actor,
117 const gchar *message,
120 EmpathyTpChatPriv *priv = GET_PRIV (chat);
121 guint handle_type = 0;
123 priv->members_count++;
124 g_signal_emit_by_name (chat, "members-changed",
125 contact, actor, reason, message,
128 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
129 if (handle_type == TP_HANDLE_TYPE_ROOM) {
133 if (priv->members_count > 2 && priv->remote_contact) {
134 /* We now have more than 2 members, this is not a p2p chat
135 * anymore. Remove the remote-contact as it makes no sense, the
136 * EmpathyContactList interface must be used now. */
137 g_object_unref (priv->remote_contact);
138 priv->remote_contact = NULL;
139 g_object_notify (G_OBJECT (chat), "remote-contact");
141 if (priv->members_count <= 2 && !priv->remote_contact &&
142 !empathy_contact_is_user (contact)) {
143 /* This is a p2p chat, if it's not ourself that means this is
144 * the remote contact with who we are chatting. This is to
145 * avoid forcing the usage of the EmpathyContactList interface
147 priv->remote_contact = g_object_ref (contact);
148 g_object_notify (G_OBJECT (chat), "remote-contact");
153 tp_chat_member_removed_cb (EmpathyTpGroup *group,
154 EmpathyContact *contact,
155 EmpathyContact *actor,
157 const gchar *message,
160 EmpathyTpChatPriv *priv = GET_PRIV (chat);
161 guint handle_type = 0;
163 priv->members_count--;
164 g_signal_emit_by_name (chat, "members-changed",
165 contact, actor, reason, message,
168 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
169 if (handle_type == TP_HANDLE_TYPE_ROOM) {
173 if (priv->members_count <= 2 && !priv->remote_contact) {
176 /* We are not a MUC anymore, get the remote contact back */
177 members = empathy_tp_group_get_members (group);
178 for (l = members; l; l = l->next) {
179 if (!empathy_contact_is_user (l->data)) {
180 priv->remote_contact = g_object_ref (l->data);
181 g_object_notify (G_OBJECT (chat), "remote-contact");
185 g_list_foreach (members, (GFunc) g_object_unref, NULL);
186 g_list_free (members);
191 tp_chat_local_pending_cb (EmpathyTpGroup *group,
192 EmpathyContact *contact,
193 EmpathyContact *actor,
195 const gchar *message,
198 g_signal_emit_by_name (chat, "pendings-changed",
199 contact, actor, reason, message,
204 tp_chat_add (EmpathyContactList *list,
205 EmpathyContact *contact,
206 const gchar *message)
208 EmpathyTpChatPriv *priv = GET_PRIV (list);
210 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
211 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
214 empathy_tp_group_add_member (priv->group, contact, message);
219 tp_chat_remove (EmpathyContactList *list,
220 EmpathyContact *contact,
221 const gchar *message)
223 EmpathyTpChatPriv *priv = GET_PRIV (list);
225 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
226 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
229 empathy_tp_group_remove_member (priv->group, contact, message);
234 tp_chat_get_members (EmpathyContactList *list)
236 EmpathyTpChatPriv *priv = GET_PRIV (list);
237 GList *members = NULL;
239 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
242 members = empathy_tp_group_get_members (priv->group);
244 members = g_list_prepend (members, g_object_ref (priv->user));
245 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
251 static EmpathyMessage *
252 tp_chat_build_message (EmpathyTpChat *chat,
256 const gchar *message_body)
258 EmpathyTpChatPriv *priv;
259 EmpathyMessage *message;
260 EmpathyContact *sender;
262 priv = GET_PRIV (chat);
264 if (from_handle == 0) {
265 sender = g_object_ref (priv->user);
267 sender = empathy_contact_factory_get_from_handle (priv->factory,
272 message = empathy_message_new (message_body);
273 empathy_message_set_tptype (message, type);
274 empathy_message_set_sender (message, sender);
275 empathy_message_set_receiver (message, priv->user);
276 empathy_message_set_timestamp (message, timestamp);
278 g_object_unref (sender);
284 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
285 GParamSpec *param_spec,
288 EmpathyTpChatPriv *priv = GET_PRIV (chat);
289 EmpathyMessage *message;
290 EmpathyContactReady ready;
291 EmpathyContact *sender;
292 gboolean removed = FALSE;
294 /* Emit all messages queued until we find a message with not
295 * ready sender. When leaving this loop, sender is the first not ready
296 * contact queued and removed tells if at least one message got removed
298 while (priv->message_queue) {
299 message = priv->message_queue->data;
300 sender = empathy_message_get_sender (message);
301 ready = empathy_contact_get_ready (sender);
303 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
307 DEBUG ("Queued message ready");
308 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
309 priv->message_queue = g_slist_remove (priv->message_queue,
311 g_object_unref (message);
316 g_signal_handlers_disconnect_by_func (contact,
317 tp_chat_sender_ready_notify_cb,
320 if (priv->message_queue) {
321 g_signal_connect (sender, "notify::ready",
322 G_CALLBACK (tp_chat_sender_ready_notify_cb),
329 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
330 EmpathyMessage *message)
332 EmpathyTpChatPriv *priv = GET_PRIV (chat);
333 EmpathyContact *sender;
334 EmpathyContactReady ready;
336 if (priv->message_queue != NULL) {
337 DEBUG ("Message queue not empty");
338 priv->message_queue = g_slist_append (priv->message_queue,
339 g_object_ref (message));
343 sender = empathy_message_get_sender (message);
344 ready = empathy_contact_get_ready (sender);
345 if (ready & EMPATHY_CONTACT_READY_NAME) {
346 DEBUG ("Message queue empty and sender ready");
347 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
351 DEBUG ("Sender not ready");
352 priv->message_queue = g_slist_append (priv->message_queue,
353 g_object_ref (message));
354 g_signal_connect (sender, "notify::ready",
355 G_CALLBACK (tp_chat_sender_ready_notify_cb),
360 tp_chat_received_cb (TpChannel *channel,
366 const gchar *message_body,
370 EmpathyTpChatPriv *priv = GET_PRIV (chat);
371 EmpathyMessage *message;
373 if (priv->listing_pending_messages) {
377 DEBUG ("Message received: %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);
388 if (priv->acknowledge) {
391 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
392 g_array_append_val (message_ids, message_id);
393 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
397 "acknowledging received message",
400 g_array_free (message_ids, TRUE);
405 tp_chat_sent_cb (TpChannel *channel,
408 const gchar *message_body,
412 EmpathyMessage *message;
414 DEBUG ("Message sent: %s", message_body);
416 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
422 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
423 g_object_unref (message);
427 tp_chat_send_error_cb (TpChannel *channel,
431 const gchar *message_body,
435 EmpathyMessage *message;
437 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
439 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
445 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
446 g_object_unref (message);
450 tp_chat_state_changed_cb (TpChannel *channel,
456 EmpathyTpChatPriv *priv = GET_PRIV (chat);
457 EmpathyContact *contact;
459 contact = empathy_contact_factory_get_from_handle (priv->factory,
463 DEBUG ("Chat state changed for %s (%d): %d",
464 empathy_contact_get_name (contact), handle, state);
466 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
467 g_object_unref (contact);
471 tp_chat_list_pending_messages_cb (TpChannel *channel,
472 const GPtrArray *messages_list,
477 EmpathyTpChatPriv *priv = GET_PRIV (chat);
479 GArray *message_ids = NULL;
481 priv->listing_pending_messages = FALSE;
484 DEBUG ("Error listing pending messages: %s", error->message);
488 if (priv->acknowledge) {
489 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
493 for (i = 0; i < messages_list->len; i++) {
494 EmpathyMessage *message;
495 GValueArray *message_struct;
496 const gchar *message_body;
503 message_struct = g_ptr_array_index (messages_list, i);
505 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
506 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
507 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
508 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
509 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
510 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
512 DEBUG ("Message pending: %s", message_body);
515 g_array_append_val (message_ids, message_id);
518 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
524 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
525 g_object_unref (message);
529 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
533 "acknowledging pending messages",
536 g_array_free (message_ids, TRUE);
541 tp_chat_property_flags_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 flags = g_value_get_uint (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 property->flags = flags;
567 DEBUG ("property %s flags changed: %d",
568 property->name, property->flags);
576 tp_chat_properties_changed_cb (TpProxy *proxy,
577 const GPtrArray *properties,
581 EmpathyTpChatPriv *priv = GET_PRIV (chat);
584 if (!priv->had_properties_list || !properties) {
588 for (i = 0; i < properties->len; i++) {
589 GValueArray *prop_struct;
590 TpChatProperty *property;
594 prop_struct = g_ptr_array_index (properties, i);
595 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
596 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
598 for (j = 0; j < priv->properties->len; j++) {
599 property = g_ptr_array_index (priv->properties, j);
600 if (property->id == id) {
601 if (property->value) {
602 g_value_copy (src_value, property->value);
604 property->value = tp_g_value_slice_dup (src_value);
607 DEBUG ("property %s changed", property->name);
608 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
609 property->name, property->value);
617 tp_chat_get_properties_cb (TpProxy *proxy,
618 const GPtrArray *properties,
624 DEBUG ("Error getting properties: %s", error->message);
628 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
632 tp_chat_list_properties_cb (TpProxy *proxy,
633 const GPtrArray *properties,
638 EmpathyTpChatPriv *priv = GET_PRIV (chat);
642 priv->had_properties_list = TRUE;
645 DEBUG ("Error listing properties: %s", error->message);
649 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
650 priv->properties = g_ptr_array_sized_new (properties->len);
651 for (i = 0; i < properties->len; i++) {
652 GValueArray *prop_struct;
653 TpChatProperty *property;
655 prop_struct = g_ptr_array_index (properties, i);
656 property = g_slice_new0 (TpChatProperty);
657 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
658 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
659 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
661 DEBUG ("Adding property name=%s id=%d flags=%d",
662 property->name, property->id, property->flags);
663 g_ptr_array_add (priv->properties, property);
664 if (property->flags & TP_PROPERTY_FLAG_READ) {
665 g_array_append_val (ids, property->id);
669 tp_cli_properties_interface_call_get_properties (proxy, -1,
671 tp_chat_get_properties_cb,
675 g_array_free (ids, TRUE);
679 empathy_tp_chat_set_property (EmpathyTpChat *chat,
683 EmpathyTpChatPriv *priv = GET_PRIV (chat);
684 TpChatProperty *property;
687 g_return_if_fail (priv->ready);
689 for (i = 0; i < priv->properties->len; i++) {
690 property = g_ptr_array_index (priv->properties, i);
691 if (!tp_strdiff (property->name, name)) {
692 GPtrArray *properties;
695 GValue dest_value = {0, };
697 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
701 g_value_init (&id, G_TYPE_UINT);
702 g_value_init (&dest_value, G_TYPE_VALUE);
703 g_value_set_uint (&id, property->id);
704 g_value_set_boxed (&dest_value, value);
706 prop = g_value_array_new (2);
707 g_value_array_append (prop, &id);
708 g_value_array_append (prop, &dest_value);
710 properties = g_ptr_array_sized_new (1);
711 g_ptr_array_add (properties, prop);
713 DEBUG ("Set property %s", name);
714 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
716 (tp_cli_properties_interface_callback_for_set_properties)
718 "Seting property", NULL,
721 g_ptr_array_free (properties, TRUE);
722 g_value_array_free (prop);
730 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
732 EmpathyTpChatPriv *priv = GET_PRIV (chat);
733 TpConnection *connection;
734 guint handle, handle_type;
736 DEBUG ("Channel ready");
738 g_object_get (priv->channel,
739 "connection", &connection,
741 "handle_type", &handle_type,
744 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
748 handles = g_array_new (FALSE, FALSE, sizeof (guint));
749 g_array_append_val (handles, handle);
750 tp_cli_connection_run_inspect_handles (connection, -1,
751 handle_type, handles,
754 g_array_free (handles, TRUE);
758 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
759 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
762 g_object_notify (G_OBJECT (chat), "remote-contact");
765 if (tp_proxy_has_interface_by_id (priv->channel,
766 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
767 priv->group = empathy_tp_group_new (priv->channel);
769 g_signal_connect (priv->group, "member-added",
770 G_CALLBACK (tp_chat_member_added_cb),
772 g_signal_connect (priv->group, "member-removed",
773 G_CALLBACK (tp_chat_member_removed_cb),
775 g_signal_connect (priv->group, "local-pending",
776 G_CALLBACK (tp_chat_local_pending_cb),
778 empathy_run_until_ready (priv->group);
780 priv->members_count = 2;
783 if (tp_proxy_has_interface_by_id (priv->channel,
784 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
785 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
786 tp_chat_list_properties_cb,
789 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
790 tp_chat_properties_changed_cb,
792 G_OBJECT (chat), NULL);
793 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
794 tp_chat_property_flags_changed_cb,
796 G_OBJECT (chat), NULL);
799 priv->listing_pending_messages = TRUE;
800 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
802 tp_chat_list_pending_messages_cb,
806 tp_cli_channel_type_text_connect_to_received (priv->channel,
809 G_OBJECT (chat), NULL);
810 tp_cli_channel_type_text_connect_to_sent (priv->channel,
813 G_OBJECT (chat), NULL);
814 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
815 tp_chat_send_error_cb,
817 G_OBJECT (chat), NULL);
818 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
819 tp_chat_state_changed_cb,
821 G_OBJECT (chat), NULL);
822 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
823 tp_chat_state_changed_cb,
825 G_OBJECT (chat), NULL);
828 g_object_notify (G_OBJECT (chat), "ready");
832 tp_chat_finalize (GObject *object)
834 EmpathyTpChatPriv *priv = GET_PRIV (object);
837 DEBUG ("Finalize: %p", object);
839 if (priv->acknowledge && priv->channel) {
840 DEBUG ("Closing channel...");
841 tp_cli_channel_call_close (priv->channel, -1,
843 "closing channel", NULL,
848 g_signal_handlers_disconnect_by_func (priv->channel,
849 tp_chat_invalidated_cb,
851 g_object_unref (priv->channel);
854 if (priv->properties) {
855 for (i = 0; i < priv->properties->len; i++) {
856 TpChatProperty *property;
858 property = g_ptr_array_index (priv->properties, i);
859 g_free (property->name);
860 if (property->value) {
861 tp_g_value_slice_free (property->value);
863 g_slice_free (TpChatProperty, property);
865 g_ptr_array_free (priv->properties, TRUE);
868 if (priv->remote_contact) {
869 g_object_unref (priv->remote_contact);
872 g_object_unref (priv->group);
875 g_object_unref (priv->factory);
876 g_object_unref (priv->user);
877 g_object_unref (priv->account);
880 if (priv->message_queue) {
881 EmpathyMessage *message;
882 EmpathyContact *contact;
884 message = priv->message_queue->data;
885 contact = empathy_message_get_sender (message);
886 g_signal_handlers_disconnect_by_func (contact,
887 tp_chat_sender_ready_notify_cb,
890 g_slist_foreach (priv->message_queue, (GFunc) g_object_unref, NULL);
891 g_slist_free (priv->message_queue);
893 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
897 tp_chat_constructor (GType type,
899 GObjectConstructParam *props)
902 EmpathyTpChatPriv *priv;
903 gboolean channel_ready;
905 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
907 priv = GET_PRIV (chat);
908 priv->account = empathy_channel_get_account (priv->channel);
909 priv->factory = empathy_contact_factory_new ();
910 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
912 g_signal_connect (priv->channel, "invalidated",
913 G_CALLBACK (tp_chat_invalidated_cb),
916 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
918 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
920 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
921 G_CALLBACK (tp_chat_channel_ready_cb),
929 tp_chat_get_property (GObject *object,
934 EmpathyTpChatPriv *priv = GET_PRIV (object);
938 g_value_set_object (value, priv->channel);
940 case PROP_ACKNOWLEDGE:
941 g_value_set_boolean (value, priv->acknowledge);
943 case PROP_REMOTE_CONTACT:
944 g_value_set_object (value, priv->remote_contact);
947 g_value_set_boolean (value, priv->ready);
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
956 tp_chat_set_property (GObject *object,
961 EmpathyTpChatPriv *priv = GET_PRIV (object);
965 priv->channel = g_object_ref (g_value_get_object (value));
967 case PROP_ACKNOWLEDGE:
968 priv->acknowledge = g_value_get_boolean (value);
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
977 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
979 GObjectClass *object_class = G_OBJECT_CLASS (klass);
981 object_class->finalize = tp_chat_finalize;
982 object_class->constructor = tp_chat_constructor;
983 object_class->get_property = tp_chat_get_property;
984 object_class->set_property = tp_chat_set_property;
986 g_object_class_install_property (object_class,
988 g_param_spec_object ("channel",
990 "The text channel for the chat",
993 G_PARAM_CONSTRUCT_ONLY));
994 g_object_class_install_property (object_class,
996 g_param_spec_boolean ("acknowledge",
997 "acknowledge messages",
998 "Wheter or not received messages should be acknowledged",
1000 G_PARAM_READWRITE));
1002 g_object_class_install_property (object_class,
1003 PROP_REMOTE_CONTACT,
1004 g_param_spec_object ("remote-contact",
1005 "The remote contact",
1006 "The remote contact if there is no group iface on the channel",
1007 EMPATHY_TYPE_CONTACT,
1009 g_object_class_install_property (object_class,
1011 g_param_spec_boolean ("ready",
1012 "Is the object ready",
1013 "This object can't be used until this becomes true",
1018 signals[MESSAGE_RECEIVED] =
1019 g_signal_new ("message-received",
1020 G_TYPE_FROM_CLASS (klass),
1024 g_cclosure_marshal_VOID__OBJECT,
1026 1, EMPATHY_TYPE_MESSAGE);
1028 signals[SEND_ERROR] =
1029 g_signal_new ("send-error",
1030 G_TYPE_FROM_CLASS (klass),
1034 _empathy_marshal_VOID__OBJECT_UINT,
1036 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1038 signals[CHAT_STATE_CHANGED] =
1039 g_signal_new ("chat-state-changed",
1040 G_TYPE_FROM_CLASS (klass),
1044 _empathy_marshal_VOID__OBJECT_UINT,
1046 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1048 signals[PROPERTY_CHANGED] =
1049 g_signal_new ("property-changed",
1050 G_TYPE_FROM_CLASS (klass),
1054 _empathy_marshal_VOID__STRING_BOXED,
1056 2, G_TYPE_STRING, G_TYPE_VALUE);
1059 g_signal_new ("destroy",
1060 G_TYPE_FROM_CLASS (klass),
1064 g_cclosure_marshal_VOID__VOID,
1068 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1072 empathy_tp_chat_init (EmpathyTpChat *chat)
1074 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1075 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1081 tp_chat_iface_init (EmpathyContactListIface *iface)
1083 iface->add = tp_chat_add;
1084 iface->remove = tp_chat_remove;
1085 iface->get_members = tp_chat_get_members;
1089 empathy_tp_chat_new (TpChannel *channel)
1091 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1097 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1099 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1101 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1102 g_return_val_if_fail (priv->ready, NULL);
1108 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1110 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1112 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1114 return priv->remote_contact;
1118 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1120 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1122 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1124 return priv->account;
1128 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1130 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1132 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1134 return priv->channel;
1138 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1140 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1142 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1148 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1150 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1152 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1154 return priv->members_count;
1158 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
1159 gboolean acknowledge)
1161 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1163 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1165 priv->acknowledge = acknowledge;
1166 g_object_notify (G_OBJECT (chat), "acknowledge");
1170 empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
1172 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1174 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1175 g_return_if_fail (priv->ready);
1177 if (priv->listing_pending_messages) {
1181 priv->listing_pending_messages = TRUE;
1182 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
1184 tp_chat_list_pending_messages_cb,
1190 empathy_tp_chat_send (EmpathyTpChat *chat,
1191 EmpathyMessage *message)
1193 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1194 const gchar *message_body;
1195 TpChannelTextMessageType message_type;
1197 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1198 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1199 g_return_if_fail (priv->ready);
1201 message_body = empathy_message_get_body (message);
1202 message_type = empathy_message_get_tptype (message);
1204 DEBUG ("Sending message: %s", message_body);
1205 tp_cli_channel_type_text_call_send (priv->channel, -1,
1209 "sending message", NULL,
1214 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1215 TpChannelChatState state)
1217 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1219 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1220 g_return_if_fail (priv->ready);
1222 DEBUG ("Set state: %d", state);
1223 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1226 "setting chat state",