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>
29 #include <telepathy-glib/interfaces.h>
31 #include "empathy-tp-chat.h"
32 #include "empathy-tp-contact-factory.h"
33 #include "empathy-contact-monitor.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-marshal.h"
36 #include "empathy-time.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
44 gboolean dispose_has_run;
45 EmpathyTpContactFactory *factory;
46 EmpathyContactMonitor *contact_monitor;
48 EmpathyContact *remote_contact;
51 gboolean listing_pending_messages;
52 /* Queue of messages not signalled yet */
53 GQueue *messages_queue;
54 /* Queue of messages signalled but not acked yet */
55 GQueue *pending_messages_queue;
56 gboolean had_properties_list;
57 GPtrArray *properties;
64 TpPropertyFlags flags;
68 static void tp_chat_iface_init (EmpathyContactListIface *iface);
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
92 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
95 tp_chat_invalidated_cb (TpProxy *proxy,
101 DEBUG ("Channel invalidated: %s", message);
102 g_signal_emit (chat, signals[DESTROY], 0);
106 tp_chat_async_cb (TpChannel *proxy,
109 GObject *weak_object)
112 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
117 tp_chat_add (EmpathyContactList *list,
118 EmpathyContact *contact,
119 const gchar *message)
121 EmpathyTpChatPriv *priv = GET_PRIV (list);
123 GArray handles = {(gchar *) &handle, 1};
125 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
126 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
128 handle = empathy_contact_get_handle (contact);
129 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
136 tp_chat_remove (EmpathyContactList *list,
137 EmpathyContact *contact,
138 const gchar *message)
140 EmpathyTpChatPriv *priv = GET_PRIV (list);
142 GArray handles = {(gchar *) &handle, 1};
144 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
145 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
147 handle = empathy_contact_get_handle (contact);
148 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
155 tp_chat_get_members (EmpathyContactList *list)
157 EmpathyTpChatPriv *priv = GET_PRIV (list);
158 GList *members = NULL;
160 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
163 members = g_list_copy (priv->members);
164 g_list_foreach (members, (GFunc) g_object_ref, NULL);
166 members = g_list_prepend (members, g_object_ref (priv->user));
167 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
173 static EmpathyContactMonitor *
174 tp_chat_get_monitor (EmpathyContactList *list)
176 EmpathyTpChatPriv *priv;
178 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
180 priv = GET_PRIV (list);
182 if (priv->contact_monitor == NULL) {
183 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
186 return priv->contact_monitor;
190 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
192 EmpathyTpChatPriv *priv = GET_PRIV (chat);
193 EmpathyMessage *message;
195 /* Check if we can now emit some queued messages */
196 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
197 if (empathy_message_get_sender (message) == NULL) {
201 DEBUG ("Queued message ready");
202 g_queue_pop_head (priv->messages_queue);
203 g_queue_push_tail (priv->pending_messages_queue, message);
204 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
209 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
210 EmpathyContact *contact,
215 EmpathyTpChatPriv *priv = GET_PRIV (chat);
218 DEBUG ("Error: %s", error->message);
219 /* Do not block the message queue, just drop this message */
220 g_queue_remove (priv->messages_queue, message);
222 empathy_message_set_sender (message, contact);
225 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
229 tp_chat_build_message (EmpathyTpChat *chat,
234 const gchar *message_body)
236 EmpathyTpChatPriv *priv;
237 EmpathyMessage *message;
239 priv = GET_PRIV (chat);
241 message = empathy_message_new (message_body);
242 empathy_message_set_tptype (message, type);
243 empathy_message_set_receiver (message, priv->user);
244 empathy_message_set_timestamp (message, timestamp);
245 empathy_message_set_id (message, id);
246 g_queue_push_tail (priv->messages_queue, message);
248 if (from_handle == 0) {
249 empathy_message_set_sender (message, priv->user);
250 tp_chat_emit_queued_messages (chat);
252 empathy_tp_contact_factory_get_from_handle (priv->factory,
254 tp_chat_got_sender_cb,
255 message, NULL, G_OBJECT (chat));
260 tp_chat_received_cb (TpChannel *channel,
266 const gchar *message_body,
270 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
271 EmpathyTpChatPriv *priv = GET_PRIV (chat);
273 if (priv->channel == NULL)
276 if (priv->listing_pending_messages) {
280 DEBUG ("Message received: %s", message_body);
282 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
283 !tp_strdiff (message_body, "")) {
286 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
288 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
289 g_array_append_val (ids, message_id);
290 acknowledge_messages (chat, ids);
291 g_array_free (ids, TRUE);
296 tp_chat_build_message (chat,
305 tp_chat_sent_cb (TpChannel *channel,
308 const gchar *message_body,
312 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
313 EmpathyTpChatPriv *priv = GET_PRIV (chat);
315 if (priv->channel == NULL)
318 DEBUG ("Message sent: %s", message_body);
320 tp_chat_build_message (chat,
329 tp_chat_send_error_cb (TpChannel *channel,
333 const gchar *message_body,
337 EmpathyTpChatPriv *priv = GET_PRIV (chat);
339 if (priv->channel == NULL)
342 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
344 tp_chat_build_message (EMPATHY_TP_CHAT (chat),
353 tp_chat_send_cb (TpChannel *proxy,
358 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
361 DEBUG ("Error: %s", error->message);
362 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
363 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
369 TpChannelChatState state;
373 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
374 EmpathyContact *contact,
379 TpChannelChatState state;
382 DEBUG ("Error: %s", error->message);
386 state = GPOINTER_TO_UINT (user_data);
387 DEBUG ("Chat state changed for %s (%d): %d",
388 empathy_contact_get_name (contact),
389 empathy_contact_get_handle (contact), state);
391 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
395 tp_chat_state_changed_cb (TpChannel *channel,
397 TpChannelChatState state,
401 EmpathyTpChatPriv *priv = GET_PRIV (chat);
403 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
404 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
409 tp_chat_list_pending_messages_cb (TpChannel *channel,
410 const GPtrArray *messages_list,
415 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
416 EmpathyTpChatPriv *priv = GET_PRIV (chat);
418 GArray *empty_non_text_content_ids = NULL;
420 priv->listing_pending_messages = FALSE;
422 if (priv->channel == NULL)
426 DEBUG ("Error listing pending messages: %s", error->message);
430 for (i = 0; i < messages_list->len; i++) {
431 GValueArray *message_struct;
432 const gchar *message_body;
439 message_struct = g_ptr_array_index (messages_list, i);
441 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
442 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
443 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
444 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
445 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
446 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
448 DEBUG ("Message pending: %s", message_body);
450 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
451 !tp_strdiff (message_body, "")) {
452 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
454 if (empty_non_text_content_ids == NULL) {
455 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
458 g_array_append_val (empty_non_text_content_ids, message_id);
462 tp_chat_build_message (chat,
470 if (empty_non_text_content_ids != NULL) {
471 acknowledge_messages (chat, empty_non_text_content_ids);
472 g_array_free (empty_non_text_content_ids, TRUE);
477 tp_chat_property_flags_changed_cb (TpProxy *proxy,
478 const GPtrArray *properties,
482 EmpathyTpChatPriv *priv = GET_PRIV (chat);
485 if (priv->channel == NULL)
488 if (!priv->had_properties_list || !properties) {
492 for (i = 0; i < properties->len; i++) {
493 GValueArray *prop_struct;
494 TpChatProperty *property;
498 prop_struct = g_ptr_array_index (properties, i);
499 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
500 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
502 for (j = 0; j < priv->properties->len; j++) {
503 property = g_ptr_array_index (priv->properties, j);
504 if (property->id == id) {
505 property->flags = flags;
506 DEBUG ("property %s flags changed: %d",
507 property->name, property->flags);
515 tp_chat_properties_changed_cb (TpProxy *proxy,
516 const GPtrArray *properties,
520 EmpathyTpChatPriv *priv = GET_PRIV (chat);
523 if (priv->channel == NULL)
526 if (!priv->had_properties_list || !properties) {
530 for (i = 0; i < properties->len; i++) {
531 GValueArray *prop_struct;
532 TpChatProperty *property;
536 prop_struct = g_ptr_array_index (properties, i);
537 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
538 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
540 for (j = 0; j < priv->properties->len; j++) {
541 property = g_ptr_array_index (priv->properties, j);
542 if (property->id == id) {
543 if (property->value) {
544 g_value_copy (src_value, property->value);
546 property->value = tp_g_value_slice_dup (src_value);
549 DEBUG ("property %s changed", property->name);
550 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
551 property->name, property->value);
559 tp_chat_get_properties_cb (TpProxy *proxy,
560 const GPtrArray *properties,
566 DEBUG ("Error getting properties: %s", error->message);
570 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
574 tp_chat_list_properties_cb (TpProxy *proxy,
575 const GPtrArray *properties,
580 EmpathyTpChatPriv *priv = GET_PRIV (chat);
584 if (priv->channel == NULL)
587 priv->had_properties_list = TRUE;
590 DEBUG ("Error listing properties: %s", error->message);
594 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
595 priv->properties = g_ptr_array_sized_new (properties->len);
596 for (i = 0; i < properties->len; i++) {
597 GValueArray *prop_struct;
598 TpChatProperty *property;
600 prop_struct = g_ptr_array_index (properties, i);
601 property = g_slice_new0 (TpChatProperty);
602 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
603 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
604 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
606 DEBUG ("Adding property name=%s id=%d flags=%d",
607 property->name, property->id, property->flags);
608 g_ptr_array_add (priv->properties, property);
609 if (property->flags & TP_PROPERTY_FLAG_READ) {
610 g_array_append_val (ids, property->id);
614 tp_cli_properties_interface_call_get_properties (proxy, -1,
616 tp_chat_get_properties_cb,
620 g_array_free (ids, TRUE);
624 empathy_tp_chat_set_property (EmpathyTpChat *chat,
628 EmpathyTpChatPriv *priv = GET_PRIV (chat);
629 TpChatProperty *property;
632 for (i = 0; i < priv->properties->len; i++) {
633 property = g_ptr_array_index (priv->properties, i);
634 if (!tp_strdiff (property->name, name)) {
635 GPtrArray *properties;
638 GValue dest_value = {0, };
640 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
644 g_value_init (&id, G_TYPE_UINT);
645 g_value_init (&dest_value, G_TYPE_VALUE);
646 g_value_set_uint (&id, property->id);
647 g_value_set_boxed (&dest_value, value);
649 prop = g_value_array_new (2);
650 g_value_array_append (prop, &id);
651 g_value_array_append (prop, &dest_value);
653 properties = g_ptr_array_sized_new (1);
654 g_ptr_array_add (properties, prop);
656 DEBUG ("Set property %s", name);
657 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
659 (tp_cli_properties_interface_callback_for_set_properties)
661 "Seting property", NULL,
664 g_ptr_array_free (properties, TRUE);
665 g_value_array_free (prop);
673 tp_chat_dispose (GObject *object)
675 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
676 EmpathyTpChatPriv *priv = GET_PRIV (self);
678 if (priv->dispose_has_run)
681 priv->dispose_has_run = TRUE;
683 if (priv->channel != NULL) {
684 g_signal_handlers_disconnect_by_func (priv->channel,
685 tp_chat_invalidated_cb, self);
686 g_object_unref (priv->channel);
688 priv->channel = NULL;
690 if (priv->remote_contact != NULL)
691 g_object_unref (priv->remote_contact);
692 priv->remote_contact = NULL;
694 if (priv->factory != NULL)
695 g_object_unref (priv->factory);
696 priv->factory = NULL;
698 if (priv->user != NULL)
699 g_object_unref (priv->user);
702 if (priv->contact_monitor)
703 g_object_unref (priv->contact_monitor);
704 priv->contact_monitor = NULL;
706 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
707 g_queue_clear (priv->messages_queue);
709 g_queue_foreach (priv->pending_messages_queue,
710 (GFunc) g_object_unref, NULL);
711 g_queue_clear (priv->pending_messages_queue);
713 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
714 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
718 tp_chat_finalize (GObject *object)
720 EmpathyTpChatPriv *priv = GET_PRIV (object);
723 DEBUG ("Finalize: %p", object);
725 if (priv->properties) {
726 for (i = 0; i < priv->properties->len; i++) {
727 TpChatProperty *property;
729 property = g_ptr_array_index (priv->properties, i);
730 g_free (property->name);
731 if (property->value) {
732 tp_g_value_slice_free (property->value);
734 g_slice_free (TpChatProperty, property);
736 g_ptr_array_free (priv->properties, TRUE);
739 g_queue_free (priv->messages_queue);
740 g_queue_free (priv->pending_messages_queue);
742 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
746 tp_chat_check_if_ready (EmpathyTpChat *chat)
748 EmpathyTpChatPriv *priv = GET_PRIV (chat);
750 if (priv->ready || priv->user == NULL ||
751 (priv->members == NULL && priv->remote_contact == NULL)) {
757 tp_cli_channel_type_text_connect_to_received (priv->channel,
760 G_OBJECT (chat), NULL);
761 priv->listing_pending_messages = TRUE;
762 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
764 tp_chat_list_pending_messages_cb,
768 tp_cli_channel_type_text_connect_to_sent (priv->channel,
771 G_OBJECT (chat), NULL);
772 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
773 tp_chat_send_error_cb,
775 G_OBJECT (chat), NULL);
776 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
777 tp_chat_state_changed_cb,
779 G_OBJECT (chat), NULL);
780 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
781 tp_chat_state_changed_cb,
783 G_OBJECT (chat), NULL);
785 g_object_notify (G_OBJECT (chat), "ready");
789 tp_chat_update_remote_contact (EmpathyTpChat *chat)
791 EmpathyTpChatPriv *priv = GET_PRIV (chat);
792 EmpathyContact *contact = NULL;
793 TpHandle self_handle;
794 TpHandleType handle_type;
797 /* If this is a named chatroom, never pretend it is a private chat */
798 tp_channel_get_handle (priv->channel, &handle_type);
799 if (handle_type == TP_HANDLE_TYPE_ROOM) {
803 /* This is an MSN-like chat where anyone can join the chat at anytime.
804 * If there is only one non-self contact member, we are in a private
805 * chat and we set the "remote-contact" property to that contact. If
806 * there are more, set the "remote-contact" property to NULL and the
807 * UI will display a contact list. */
808 self_handle = tp_channel_group_get_self_handle (priv->channel);
809 for (l = priv->members; l; l = l->next) {
810 /* Skip self contact if member */
811 if (empathy_contact_get_handle (l->data) == self_handle) {
815 /* We have more than one remote contact, break */
816 if (contact != NULL) {
821 /* If we didn't find yet a remote contact, keep this one */
825 if (priv->remote_contact == contact) {
829 DEBUG ("Changing remote contact from %p to %p",
830 priv->remote_contact, contact);
832 if (priv->remote_contact) {
833 g_object_unref (priv->remote_contact);
836 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
837 g_object_notify (G_OBJECT (chat), "remote-contact");
841 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
843 EmpathyContact * const * contacts,
845 const TpHandle *failed,
850 EmpathyTpChatPriv *priv = GET_PRIV (chat);
852 const TpIntSet *members;
854 EmpathyContact *contact;
857 DEBUG ("Error: %s", error->message);
861 members = tp_channel_group_get_members (priv->channel);
862 for (i = 0; i < n_contacts; i++) {
863 contact = contacts[i];
864 handle = empathy_contact_get_handle (contact);
866 /* Make sure the contact is still member */
867 if (tp_intset_is_member (members, handle)) {
868 priv->members = g_list_prepend (priv->members,
869 g_object_ref (contact));
870 g_signal_emit_by_name (chat, "members-changed",
871 contact, NULL, 0, NULL, TRUE);
875 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
876 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
880 tp_chat_group_members_changed_cb (TpChannel *self,
884 GArray *local_pending,
885 GArray *remote_pending,
890 EmpathyTpChatPriv *priv = GET_PRIV (chat);
891 EmpathyContact *contact;
896 /* Remove contacts that are not members anymore */
897 for (i = 0; i < removed->len; i++) {
898 for (l = priv->members; l; l = l->next) {
900 handle = empathy_contact_get_handle (contact);
901 if (handle == g_array_index (removed, TpHandle, i)) {
902 priv->members = g_list_delete_link (priv->members, l);
903 g_signal_emit_by_name (chat, "members-changed",
904 contact, NULL, reason,
906 g_object_unref (contact);
912 /* Request added contacts */
913 if (added->len > 0) {
914 empathy_tp_contact_factory_get_from_handles (priv->factory,
915 added->len, (TpHandle *) added->data,
916 tp_chat_got_added_contacts_cb, NULL, NULL,
920 tp_chat_update_remote_contact (chat);
924 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
925 EmpathyContact *contact,
930 EmpathyTpChatPriv *priv = GET_PRIV (chat);
933 DEBUG ("Error: %s", error->message);
934 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
938 priv->remote_contact = g_object_ref (contact);
939 g_object_notify (chat, "remote-contact");
941 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
945 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
946 EmpathyContact *contact,
951 EmpathyTpChatPriv *priv = GET_PRIV (chat);
954 DEBUG ("Error: %s", error->message);
955 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
959 priv->user = g_object_ref (contact);
960 empathy_contact_set_is_user (priv->user, TRUE);
961 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
965 tp_chat_constructor (GType type,
967 GObjectConstructParam *props)
970 EmpathyTpChatPriv *priv;
971 TpConnection *connection;
974 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
976 priv = GET_PRIV (chat);
978 connection = tp_channel_borrow_connection (priv->channel);
979 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
980 g_signal_connect (priv->channel, "invalidated",
981 G_CALLBACK (tp_chat_invalidated_cb),
984 if (tp_proxy_has_interface_by_id (priv->channel,
985 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
986 const TpIntSet *members;
989 /* Get self contact from the group's self handle */
990 handle = tp_channel_group_get_self_handle (priv->channel);
991 empathy_tp_contact_factory_get_from_handle (priv->factory,
992 handle, tp_chat_got_self_contact_cb,
995 /* Get initial member contacts */
996 members = tp_channel_group_get_members (priv->channel);
997 handles = tp_intset_to_array (members);
998 empathy_tp_contact_factory_get_from_handles (priv->factory,
999 handles->len, (TpHandle *) handles->data,
1000 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1002 g_signal_connect (priv->channel, "group-members-changed",
1003 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1005 /* Get the self contact from the connection's self handle */
1006 handle = tp_connection_get_self_handle (connection);
1007 empathy_tp_contact_factory_get_from_handle (priv->factory,
1008 handle, tp_chat_got_self_contact_cb,
1011 /* Get the remote contact */
1012 handle = tp_channel_get_handle (priv->channel, NULL);
1013 empathy_tp_contact_factory_get_from_handle (priv->factory,
1014 handle, tp_chat_got_remote_contact_cb,
1018 if (tp_proxy_has_interface_by_id (priv->channel,
1019 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1020 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1021 tp_chat_list_properties_cb,
1024 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1025 tp_chat_properties_changed_cb,
1027 G_OBJECT (chat), NULL);
1028 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1029 tp_chat_property_flags_changed_cb,
1031 G_OBJECT (chat), NULL);
1038 tp_chat_get_property (GObject *object,
1043 EmpathyTpChatPriv *priv = GET_PRIV (object);
1047 g_value_set_object (value, priv->channel);
1049 case PROP_REMOTE_CONTACT:
1050 g_value_set_object (value, priv->remote_contact);
1053 g_value_set_boolean (value, priv->ready);
1056 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1062 tp_chat_set_property (GObject *object,
1064 const GValue *value,
1067 EmpathyTpChatPriv *priv = GET_PRIV (object);
1071 priv->channel = g_value_dup_object (value);
1074 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1080 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1082 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1084 object_class->dispose = tp_chat_dispose;
1085 object_class->finalize = tp_chat_finalize;
1086 object_class->constructor = tp_chat_constructor;
1087 object_class->get_property = tp_chat_get_property;
1088 object_class->set_property = tp_chat_set_property;
1090 g_object_class_install_property (object_class,
1092 g_param_spec_object ("channel",
1093 "telepathy channel",
1094 "The text channel for the chat",
1097 G_PARAM_CONSTRUCT_ONLY));
1099 g_object_class_install_property (object_class,
1100 PROP_REMOTE_CONTACT,
1101 g_param_spec_object ("remote-contact",
1102 "The remote contact",
1103 "The remote contact if there is no group iface on the channel",
1104 EMPATHY_TYPE_CONTACT,
1107 g_object_class_install_property (object_class,
1109 g_param_spec_boolean ("ready",
1110 "Is the object ready",
1111 "This object can't be used until this becomes true",
1116 signals[MESSAGE_RECEIVED] =
1117 g_signal_new ("message-received",
1118 G_TYPE_FROM_CLASS (klass),
1122 g_cclosure_marshal_VOID__OBJECT,
1124 1, EMPATHY_TYPE_MESSAGE);
1126 signals[SEND_ERROR] =
1127 g_signal_new ("send-error",
1128 G_TYPE_FROM_CLASS (klass),
1132 _empathy_marshal_VOID__OBJECT_UINT,
1134 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1136 signals[CHAT_STATE_CHANGED] =
1137 g_signal_new ("chat-state-changed",
1138 G_TYPE_FROM_CLASS (klass),
1142 _empathy_marshal_VOID__OBJECT_UINT,
1144 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1146 signals[PROPERTY_CHANGED] =
1147 g_signal_new ("property-changed",
1148 G_TYPE_FROM_CLASS (klass),
1152 _empathy_marshal_VOID__STRING_BOXED,
1154 2, G_TYPE_STRING, G_TYPE_VALUE);
1157 g_signal_new ("destroy",
1158 G_TYPE_FROM_CLASS (klass),
1162 g_cclosure_marshal_VOID__VOID,
1166 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1170 empathy_tp_chat_init (EmpathyTpChat *chat)
1172 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1173 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1176 priv->contact_monitor = NULL;
1177 priv->messages_queue = g_queue_new ();
1178 priv->pending_messages_queue = g_queue_new ();
1182 tp_chat_iface_init (EmpathyContactListIface *iface)
1184 iface->add = tp_chat_add;
1185 iface->remove = tp_chat_remove;
1186 iface->get_members = tp_chat_get_members;
1187 iface->get_monitor = tp_chat_get_monitor;
1191 empathy_tp_chat_new (TpChannel *channel)
1193 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1199 empathy_tp_chat_close (EmpathyTpChat *chat) {
1200 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1202 /* If there are still messages left, it'll come back..
1203 * We loose the ordering of sent messages though */
1204 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1205 "closing channel", NULL, NULL);
1209 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1211 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1213 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1215 return tp_channel_get_identifier (priv->channel);
1219 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1221 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1223 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1224 g_return_val_if_fail (priv->ready, NULL);
1226 return priv->remote_contact;
1230 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1232 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1234 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1236 return priv->channel;
1240 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1242 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1244 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1246 return tp_channel_borrow_connection (priv->channel);
1250 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1252 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1254 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1260 empathy_tp_chat_send (EmpathyTpChat *chat,
1261 EmpathyMessage *message)
1263 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1264 const gchar *message_body;
1265 TpChannelTextMessageType message_type;
1267 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1268 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1269 g_return_if_fail (priv->ready);
1271 message_body = empathy_message_get_body (message);
1272 message_type = empathy_message_get_tptype (message);
1274 DEBUG ("Sending message: %s", message_body);
1275 tp_cli_channel_type_text_call_send (priv->channel, -1,
1279 g_object_ref (message),
1280 (GDestroyNotify) g_object_unref,
1285 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1286 TpChannelChatState state)
1288 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1290 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1291 g_return_if_fail (priv->ready);
1293 DEBUG ("Set state: %d", state);
1294 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1297 "setting chat state",
1304 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1306 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1308 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1309 g_return_val_if_fail (priv->ready, NULL);
1311 return priv->pending_messages_queue->head;
1315 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1316 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1318 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1319 priv->channel, -1, ids, tp_chat_async_cb,
1320 "acknowledging received message", NULL, G_OBJECT (chat));
1324 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1325 EmpathyMessage *message) {
1326 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1327 GArray *message_ids;
1331 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1332 g_return_if_fail (priv->ready);
1334 if (empathy_message_get_sender (message) == priv->user)
1337 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1339 id = empathy_message_get_id (message);
1340 g_array_append_val (message_ids, id);
1341 acknowledge_messages (chat, message_ids);
1342 g_array_free (message_ids, TRUE);
1345 m = g_queue_find (priv->pending_messages_queue, message);
1346 g_assert (m != NULL);
1347 g_queue_delete_link (priv->pending_messages_queue, m);
1348 g_object_unref (message);
1352 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1353 const GList *messages) {
1354 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1355 /* Copy messages as the messges list (probably is) our own */
1356 GList *msgs = g_list_copy ((GList *) messages);
1359 GArray *message_ids;
1361 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1362 g_return_if_fail (priv->ready);
1364 length = g_list_length ((GList *) messages);
1369 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1371 for (l = msgs; l != NULL; l = g_list_next (l)) {
1374 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1376 m = g_queue_find (priv->pending_messages_queue, message);
1377 g_assert (m != NULL);
1378 g_queue_delete_link (priv->pending_messages_queue, m);
1380 if (empathy_message_get_sender (message) != priv->user) {
1381 guint id = empathy_message_get_id (message);
1382 g_array_append_val (message_ids, id);
1384 g_object_unref (message);
1387 if (message_ids->len > 0)
1388 acknowledge_messages (chat, message_ids);
1390 g_array_free (message_ids, TRUE);