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-tp-contact-factory.h"
32 #include "empathy-contact-monitor.h"
33 #include "empathy-contact-list.h"
34 #include "empathy-marshal.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
43 gboolean dispose_has_run;
44 EmpathyTpContactFactory *factory;
45 EmpathyContactMonitor *contact_monitor;
47 EmpathyContact *remote_contact;
50 gboolean listing_pending_messages;
51 /* Queue of messages not signalled yet */
52 GQueue *messages_queue;
53 /* Queue of messages signalled but not acked yet */
54 GQueue *pending_messages_queue;
55 gboolean had_properties_list;
56 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void tp_chat_iface_init (EmpathyContactListIface *iface);
85 static guint signals[LAST_SIGNAL];
87 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
94 tp_chat_invalidated_cb (TpProxy *proxy,
100 EmpathyTpChatPriv *priv = GET_PRIV (chat);
102 DEBUG ("Channel invalidated: %s", message);
103 g_signal_emit (chat, signals[DESTROY], 0);
107 tp_chat_async_cb (TpChannel *proxy,
110 GObject *weak_object)
113 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
118 tp_chat_add (EmpathyContactList *list,
119 EmpathyContact *contact,
120 const gchar *message)
122 EmpathyTpChatPriv *priv = GET_PRIV (list);
124 GArray handles = {(gchar *) &handle, 1};
126 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
127 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
129 handle = empathy_contact_get_handle (contact);
130 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
137 tp_chat_remove (EmpathyContactList *list,
138 EmpathyContact *contact,
139 const gchar *message)
141 EmpathyTpChatPriv *priv = GET_PRIV (list);
143 GArray handles = {(gchar *) &handle, 1};
145 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
146 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
148 handle = empathy_contact_get_handle (contact);
149 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
156 tp_chat_get_members (EmpathyContactList *list)
158 EmpathyTpChatPriv *priv = GET_PRIV (list);
159 GList *members = NULL;
161 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
164 members = g_list_copy (priv->members);
165 g_list_foreach (members, (GFunc) g_object_ref, NULL);
167 members = g_list_prepend (members, g_object_ref (priv->user));
168 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
174 static EmpathyContactMonitor *
175 tp_chat_get_monitor (EmpathyContactList *list)
177 EmpathyTpChatPriv *priv;
179 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
181 priv = GET_PRIV (list);
183 if (priv->contact_monitor == NULL) {
184 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
187 return priv->contact_monitor;
191 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
193 EmpathyTpChatPriv *priv = GET_PRIV (chat);
194 EmpathyMessage *message;
196 /* Check if we can now emit some queued messages */
197 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
198 if (empathy_message_get_sender (message) == NULL) {
202 DEBUG ("Queued message ready");
203 g_queue_pop_head (priv->messages_queue);
204 g_queue_push_tail (priv->pending_messages_queue, message);
205 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
210 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
215 empathy_message_set_sender (message, contacts->data);
216 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
220 tp_chat_build_message (EmpathyTpChat *chat,
225 const gchar *message_body)
227 EmpathyTpChatPriv *priv;
228 EmpathyMessage *message;
230 priv = GET_PRIV (chat);
232 message = empathy_message_new (message_body);
233 empathy_message_set_tptype (message, type);
234 empathy_message_set_receiver (message, priv->user);
235 empathy_message_set_timestamp (message, timestamp);
236 empathy_message_set_id (message, id);
237 g_queue_push_tail (priv->messages_queue, message);
239 if (from_handle == 0) {
240 empathy_message_set_sender (message, priv->user);
241 tp_chat_emit_queued_messages (chat);
243 empathy_tp_contact_factory_get_from_handles (priv->factory,
245 tp_chat_got_sender_cb,
246 message, NULL, G_OBJECT (chat));
251 tp_chat_received_cb (TpChannel *channel,
257 const gchar *message_body,
261 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
262 EmpathyTpChatPriv *priv = GET_PRIV (chat);
264 if (priv->channel == NULL)
267 if (priv->listing_pending_messages) {
271 DEBUG ("Message received: %s", message_body);
273 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
274 !tp_strdiff (message_body, "")) {
277 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
279 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
280 g_array_append_val (ids, message_id);
281 acknowledge_messages (chat, ids);
282 g_array_free (ids, TRUE);
287 tp_chat_build_message (chat,
296 tp_chat_sent_cb (TpChannel *channel,
299 const gchar *message_body,
303 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
304 EmpathyTpChatPriv *priv = GET_PRIV (chat);
306 if (priv->channel == NULL)
309 DEBUG ("Message sent: %s", message_body);
311 tp_chat_build_message (chat,
320 tp_chat_send_error_cb (TpChannel *channel,
324 const gchar *message_body,
328 EmpathyTpChatPriv *priv = GET_PRIV (chat);
330 if (priv->channel == NULL)
333 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
335 tp_chat_build_message (EMPATHY_TP_CHAT (chat),
344 tp_chat_send_cb (TpChannel *proxy,
349 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
352 DEBUG ("Error: %s", error->message);
353 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
354 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
360 TpChannelChatState state;
364 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
369 EmpathyContact *contact = contacts->data;
370 TpChannelChatState state;
372 state = GPOINTER_TO_UINT (user_data);
373 DEBUG ("Chat state changed for %s (%d): %d",
374 empathy_contact_get_name (contact),
375 empathy_contact_get_handle (contact), state);
377 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
381 tp_chat_state_changed_cb (TpChannel *channel,
383 TpChannelChatState state,
387 EmpathyTpChatPriv *priv = GET_PRIV (chat);
389 empathy_tp_contact_factory_get_from_handles (priv->factory, 1, &handle,
390 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
395 tp_chat_list_pending_messages_cb (TpChannel *channel,
396 const GPtrArray *messages_list,
401 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
402 EmpathyTpChatPriv *priv = GET_PRIV (chat);
404 GArray *empty_non_text_content_ids = NULL;
406 priv->listing_pending_messages = FALSE;
408 if (priv->channel == NULL)
412 DEBUG ("Error listing pending messages: %s", error->message);
416 for (i = 0; i < messages_list->len; i++) {
417 GValueArray *message_struct;
418 const gchar *message_body;
425 message_struct = g_ptr_array_index (messages_list, i);
427 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
428 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
429 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
430 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
431 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
432 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
434 DEBUG ("Message pending: %s", message_body);
436 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
437 !tp_strdiff (message_body, "")) {
438 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
440 if (empty_non_text_content_ids == NULL) {
441 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
444 g_array_append_val (empty_non_text_content_ids, message_id);
448 tp_chat_build_message (chat,
456 if (empty_non_text_content_ids != NULL) {
457 acknowledge_messages (chat, empty_non_text_content_ids);
458 g_array_free (empty_non_text_content_ids, TRUE);
463 tp_chat_property_flags_changed_cb (TpProxy *proxy,
464 const GPtrArray *properties,
468 EmpathyTpChatPriv *priv = GET_PRIV (chat);
471 if (priv->channel == NULL)
474 if (!priv->had_properties_list || !properties) {
478 for (i = 0; i < properties->len; i++) {
479 GValueArray *prop_struct;
480 TpChatProperty *property;
484 prop_struct = g_ptr_array_index (properties, i);
485 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
486 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
488 for (j = 0; j < priv->properties->len; j++) {
489 property = g_ptr_array_index (priv->properties, j);
490 if (property->id == id) {
491 property->flags = flags;
492 DEBUG ("property %s flags changed: %d",
493 property->name, property->flags);
501 tp_chat_properties_changed_cb (TpProxy *proxy,
502 const GPtrArray *properties,
506 EmpathyTpChatPriv *priv = GET_PRIV (chat);
509 if (priv->channel == NULL)
512 if (!priv->had_properties_list || !properties) {
516 for (i = 0; i < properties->len; i++) {
517 GValueArray *prop_struct;
518 TpChatProperty *property;
522 prop_struct = g_ptr_array_index (properties, i);
523 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
524 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
526 for (j = 0; j < priv->properties->len; j++) {
527 property = g_ptr_array_index (priv->properties, j);
528 if (property->id == id) {
529 if (property->value) {
530 g_value_copy (src_value, property->value);
532 property->value = tp_g_value_slice_dup (src_value);
535 DEBUG ("property %s changed", property->name);
536 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
537 property->name, property->value);
545 tp_chat_get_properties_cb (TpProxy *proxy,
546 const GPtrArray *properties,
552 DEBUG ("Error getting properties: %s", error->message);
556 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
560 tp_chat_list_properties_cb (TpProxy *proxy,
561 const GPtrArray *properties,
566 EmpathyTpChatPriv *priv = GET_PRIV (chat);
570 if (priv->channel == NULL)
573 priv->had_properties_list = TRUE;
576 DEBUG ("Error listing properties: %s", error->message);
580 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
581 priv->properties = g_ptr_array_sized_new (properties->len);
582 for (i = 0; i < properties->len; i++) {
583 GValueArray *prop_struct;
584 TpChatProperty *property;
586 prop_struct = g_ptr_array_index (properties, i);
587 property = g_slice_new0 (TpChatProperty);
588 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
589 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
590 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
592 DEBUG ("Adding property name=%s id=%d flags=%d",
593 property->name, property->id, property->flags);
594 g_ptr_array_add (priv->properties, property);
595 if (property->flags & TP_PROPERTY_FLAG_READ) {
596 g_array_append_val (ids, property->id);
600 tp_cli_properties_interface_call_get_properties (proxy, -1,
602 tp_chat_get_properties_cb,
606 g_array_free (ids, TRUE);
610 empathy_tp_chat_set_property (EmpathyTpChat *chat,
614 EmpathyTpChatPriv *priv = GET_PRIV (chat);
615 TpChatProperty *property;
618 for (i = 0; i < priv->properties->len; i++) {
619 property = g_ptr_array_index (priv->properties, i);
620 if (!tp_strdiff (property->name, name)) {
621 GPtrArray *properties;
624 GValue dest_value = {0, };
626 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
630 g_value_init (&id, G_TYPE_UINT);
631 g_value_init (&dest_value, G_TYPE_VALUE);
632 g_value_set_uint (&id, property->id);
633 g_value_set_boxed (&dest_value, value);
635 prop = g_value_array_new (2);
636 g_value_array_append (prop, &id);
637 g_value_array_append (prop, &dest_value);
639 properties = g_ptr_array_sized_new (1);
640 g_ptr_array_add (properties, prop);
642 DEBUG ("Set property %s", name);
643 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
645 (tp_cli_properties_interface_callback_for_set_properties)
647 "Seting property", NULL,
650 g_ptr_array_free (properties, TRUE);
651 g_value_array_free (prop);
659 tp_chat_dispose (GObject *object)
661 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
662 EmpathyTpChatPriv *priv = GET_PRIV (self);
664 if (priv->dispose_has_run)
667 priv->dispose_has_run = TRUE;
669 if (priv->channel != NULL) {
670 g_signal_handlers_disconnect_by_func (priv->channel,
671 tp_chat_invalidated_cb, self);
672 g_object_unref (priv->channel);
674 priv->channel = NULL;
676 if (priv->remote_contact != NULL)
677 g_object_unref (priv->remote_contact);
678 priv->remote_contact = NULL;
680 if (priv->factory != NULL)
681 g_object_unref (priv->factory);
682 priv->factory = NULL;
684 if (priv->user != NULL);
685 g_object_unref (priv->user);
688 if (priv->contact_monitor)
689 g_object_unref (priv->contact_monitor);
690 priv->contact_monitor = NULL;
692 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
693 g_queue_clear (priv->messages_queue);
695 g_queue_foreach (priv->pending_messages_queue,
696 (GFunc) g_object_unref, NULL);
697 g_queue_clear (priv->pending_messages_queue);
699 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
700 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
704 tp_chat_finalize (GObject *object)
706 EmpathyTpChatPriv *priv = GET_PRIV (object);
709 DEBUG ("Finalize: %p", object);
711 if (priv->properties) {
712 for (i = 0; i < priv->properties->len; i++) {
713 TpChatProperty *property;
715 property = g_ptr_array_index (priv->properties, i);
716 g_free (property->name);
717 if (property->value) {
718 tp_g_value_slice_free (property->value);
720 g_slice_free (TpChatProperty, property);
722 g_ptr_array_free (priv->properties, TRUE);
725 g_queue_free (priv->messages_queue);
726 g_queue_free (priv->pending_messages_queue);
728 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
732 tp_chat_check_if_ready (EmpathyTpChat *chat)
734 EmpathyTpChatPriv *priv = GET_PRIV (chat);
736 if (priv->ready || priv->user == NULL ||
737 (priv->members == NULL && priv->remote_contact == NULL)) {
743 priv->listing_pending_messages = TRUE;
744 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
746 tp_chat_list_pending_messages_cb,
750 tp_cli_channel_type_text_connect_to_received (priv->channel,
753 G_OBJECT (chat), NULL);
754 tp_cli_channel_type_text_connect_to_sent (priv->channel,
757 G_OBJECT (chat), NULL);
758 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
759 tp_chat_send_error_cb,
761 G_OBJECT (chat), NULL);
762 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
763 tp_chat_state_changed_cb,
765 G_OBJECT (chat), NULL);
766 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
767 tp_chat_state_changed_cb,
769 G_OBJECT (chat), NULL);
771 g_object_notify (G_OBJECT (chat), "ready");
775 tp_chat_update_remote_contact (EmpathyTpChat *chat)
777 EmpathyTpChatPriv *priv = GET_PRIV (chat);
778 EmpathyContact *contact = NULL;
779 TpHandle self_handle;
780 TpHandleType handle_type;
783 /* If this is a named chatroom, never pretend it is a private chat */
784 tp_channel_get_handle (priv->channel, &handle_type);
785 if (handle_type == TP_HANDLE_TYPE_ROOM) {
789 /* This is an MSN-like chat where anyone can join the chat at anytime.
790 * If there is only one non-self contact member, we are in a private
791 * chat and we set the "remote-contact" property to that contact. If
792 * there are more, set the "remote-contact" property to NULL and the
793 * UI will display a contact list. */
794 self_handle = tp_channel_group_get_self_handle (priv->channel);
795 for (l = priv->members; l; l = l->next) {
796 /* Skip self contact if member */
797 if (empathy_contact_get_handle (l->data) == self_handle) {
801 /* We have more than one remote contact, break */
802 if (contact != NULL) {
807 /* If we didn't find yet a remote contact, keep this one */
811 if (priv->remote_contact == contact) {
815 DEBUG ("Changing remote contact from %p to %p",
816 priv->remote_contact, contact);
818 if (priv->remote_contact) {
819 g_object_unref (priv->remote_contact);
822 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
823 g_object_notify (G_OBJECT (chat), "remote-contact");
827 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
832 EmpathyTpChatPriv *priv = GET_PRIV (chat);
834 const TpIntSet *members;
836 EmpathyContact *contact;
838 members = tp_channel_group_get_members (priv->channel);
839 for (l = contacts; l; l = l->next) {
841 handle = empathy_contact_get_handle (contact);
843 /* Make sure the contact is still member */
844 if (tp_intset_is_member (members, handle)) {
845 priv->members = g_list_prepend (priv->members,
846 g_object_ref (contact));
847 g_signal_emit_by_name (chat, "members-changed",
848 contact, NULL, 0, NULL, FALSE);
852 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
853 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
857 tp_chat_group_members_changed_cb (TpChannel *self,
861 GArray *local_pending,
862 GArray *remote_pending,
867 EmpathyTpChatPriv *priv = GET_PRIV (chat);
868 EmpathyContact *contact;
873 /* Remove contacts that are not members anymore */
874 for (i = 0; i < removed->len; i++) {
875 for (l = priv->members; l; l = l->next) {
877 handle = empathy_contact_get_handle (contact);
878 if (handle == g_array_index (removed, TpHandle, i)) {
879 priv->members = g_list_delete_link (priv->members, l);
880 g_signal_emit_by_name (chat, "members-changed",
881 contact, NULL, reason,
883 g_object_unref (contact);
889 /* Request added contacts */
890 if (added->len > 0) {
891 empathy_tp_contact_factory_get_from_handles (priv->factory,
892 added->len, (TpHandle*) added->data,
893 tp_chat_got_added_contacts_cb, NULL, NULL,
897 tp_chat_update_remote_contact (chat);
901 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
906 EmpathyTpChatPriv *priv = GET_PRIV (chat);
908 priv->remote_contact = g_object_ref (contacts->data);
909 g_object_notify (chat, "remote-contact");
911 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
915 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
920 EmpathyTpChatPriv *priv = GET_PRIV (chat);
922 priv->user = g_object_ref (contacts->data);
923 empathy_contact_set_is_user (priv->user, TRUE);
924 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
928 tp_chat_get_self_handle_cb (TpConnection *connection,
929 TpHandle self_handle,
934 EmpathyTpChatPriv *priv = GET_PRIV (chat);
937 DEBUG ("Error: %s", error->message);
938 tp_cli_channel_call_close (priv->channel, -1,
939 NULL, NULL, NULL, NULL);
943 empathy_tp_contact_factory_get_from_handles (priv->factory,
945 tp_chat_got_self_contact_cb,
950 tp_chat_constructor (GType type,
952 GObjectConstructParam *props)
955 EmpathyTpChatPriv *priv;
956 TpConnection *connection;
959 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
961 priv = GET_PRIV (chat);
963 connection = tp_channel_borrow_connection (priv->channel);
964 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
965 g_signal_connect (priv->channel, "invalidated",
966 G_CALLBACK (tp_chat_invalidated_cb),
969 if (tp_proxy_has_interface_by_id (priv->channel,
970 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
971 const TpIntSet *members;
974 /* Get self contact from the group's self handle */
975 handle = tp_channel_group_get_self_handle (priv->channel);
976 empathy_tp_contact_factory_get_from_handles (priv->factory,
978 tp_chat_got_self_contact_cb,
981 /* Get initial member contacts */
982 members = tp_channel_group_get_members (priv->channel);
983 handles = tp_intset_to_array (members);
984 empathy_tp_contact_factory_get_from_handles (priv->factory,
985 handles->len, (TpHandle*) handles->data,
986 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
988 g_signal_connect (priv->channel, "group-members-changed",
989 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
991 /* Get the self contact from the connection's self handle */
992 tp_cli_connection_call_get_self_handle (connection, -1,
993 tp_chat_get_self_handle_cb, NULL, NULL, chat);
995 /* Get the remote contact */
996 handle = tp_channel_get_handle (priv->channel, NULL);
997 empathy_tp_contact_factory_get_from_handles (priv->factory,
998 1, &handle, tp_chat_got_remote_contact_cb,
1002 if (tp_proxy_has_interface_by_id (priv->channel,
1003 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1004 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1005 tp_chat_list_properties_cb,
1008 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1009 tp_chat_properties_changed_cb,
1011 G_OBJECT (chat), NULL);
1012 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1013 tp_chat_property_flags_changed_cb,
1015 G_OBJECT (chat), NULL);
1022 tp_chat_get_property (GObject *object,
1027 EmpathyTpChatPriv *priv = GET_PRIV (object);
1031 g_value_set_object (value, priv->channel);
1033 case PROP_REMOTE_CONTACT:
1034 g_value_set_object (value, priv->remote_contact);
1037 g_value_set_boolean (value, priv->ready);
1040 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1046 tp_chat_set_property (GObject *object,
1048 const GValue *value,
1051 EmpathyTpChatPriv *priv = GET_PRIV (object);
1055 priv->channel = g_value_dup_object (value);
1058 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1064 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1066 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1068 object_class->dispose = tp_chat_dispose;
1069 object_class->finalize = tp_chat_finalize;
1070 object_class->constructor = tp_chat_constructor;
1071 object_class->get_property = tp_chat_get_property;
1072 object_class->set_property = tp_chat_set_property;
1074 g_object_class_install_property (object_class,
1076 g_param_spec_object ("channel",
1077 "telepathy channel",
1078 "The text channel for the chat",
1081 G_PARAM_CONSTRUCT_ONLY));
1083 g_object_class_install_property (object_class,
1084 PROP_REMOTE_CONTACT,
1085 g_param_spec_object ("remote-contact",
1086 "The remote contact",
1087 "The remote contact if there is no group iface on the channel",
1088 EMPATHY_TYPE_CONTACT,
1091 g_object_class_install_property (object_class,
1093 g_param_spec_boolean ("ready",
1094 "Is the object ready",
1095 "This object can't be used until this becomes true",
1100 signals[MESSAGE_RECEIVED] =
1101 g_signal_new ("message-received",
1102 G_TYPE_FROM_CLASS (klass),
1106 g_cclosure_marshal_VOID__OBJECT,
1108 1, EMPATHY_TYPE_MESSAGE);
1110 signals[SEND_ERROR] =
1111 g_signal_new ("send-error",
1112 G_TYPE_FROM_CLASS (klass),
1116 _empathy_marshal_VOID__OBJECT_UINT,
1118 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1120 signals[CHAT_STATE_CHANGED] =
1121 g_signal_new ("chat-state-changed",
1122 G_TYPE_FROM_CLASS (klass),
1126 _empathy_marshal_VOID__OBJECT_UINT,
1128 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1130 signals[PROPERTY_CHANGED] =
1131 g_signal_new ("property-changed",
1132 G_TYPE_FROM_CLASS (klass),
1136 _empathy_marshal_VOID__STRING_BOXED,
1138 2, G_TYPE_STRING, G_TYPE_VALUE);
1141 g_signal_new ("destroy",
1142 G_TYPE_FROM_CLASS (klass),
1146 g_cclosure_marshal_VOID__VOID,
1150 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1154 empathy_tp_chat_init (EmpathyTpChat *chat)
1156 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1157 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1160 priv->contact_monitor = NULL;
1161 priv->messages_queue = g_queue_new ();
1162 priv->pending_messages_queue = g_queue_new ();
1166 tp_chat_iface_init (EmpathyContactListIface *iface)
1168 iface->add = tp_chat_add;
1169 iface->remove = tp_chat_remove;
1170 iface->get_members = tp_chat_get_members;
1171 iface->get_monitor = tp_chat_get_monitor;
1175 empathy_tp_chat_new (TpChannel *channel)
1177 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1183 empathy_tp_chat_close (EmpathyTpChat *chat) {
1184 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1186 /* If there are still messages left, it'll come back..
1187 * We loose the ordering of sent messages though */
1188 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1189 "closing channel", NULL, NULL);
1193 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1195 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1197 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1199 return tp_channel_get_identifier (priv->channel);
1203 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1205 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1207 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1208 g_return_val_if_fail (priv->ready, NULL);
1210 return priv->remote_contact;
1214 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1216 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1218 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1220 return priv->channel;
1224 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1226 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1228 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1230 return tp_channel_borrow_connection (priv->channel);
1234 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1236 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1238 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1244 empathy_tp_chat_send (EmpathyTpChat *chat,
1245 EmpathyMessage *message)
1247 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1248 const gchar *message_body;
1249 TpChannelTextMessageType message_type;
1251 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1252 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1253 g_return_if_fail (priv->ready);
1255 message_body = empathy_message_get_body (message);
1256 message_type = empathy_message_get_tptype (message);
1258 DEBUG ("Sending message: %s", message_body);
1259 tp_cli_channel_type_text_call_send (priv->channel, -1,
1263 g_object_ref (message),
1264 (GDestroyNotify) g_object_unref,
1269 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1270 TpChannelChatState state)
1272 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1274 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1275 g_return_if_fail (priv->ready);
1277 DEBUG ("Set state: %d", state);
1278 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1281 "setting chat state",
1288 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1290 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1292 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1293 g_return_val_if_fail (priv->ready, NULL);
1295 return priv->pending_messages_queue->head;
1299 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1300 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1302 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1303 priv->channel, -1, ids, tp_chat_async_cb,
1304 "acknowledging received message", NULL, G_OBJECT (chat));
1308 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1309 EmpathyMessage *message) {
1310 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1311 GArray *message_ids;
1315 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1316 g_return_if_fail (priv->ready);
1318 if (empathy_message_get_sender (message) == priv->user)
1321 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1323 id = empathy_message_get_id (message);
1324 g_array_append_val (message_ids, id);
1325 acknowledge_messages (chat, message_ids);
1326 g_array_free (message_ids, TRUE);
1329 m = g_queue_find (priv->pending_messages_queue, message);
1330 g_assert (m != NULL);
1331 g_queue_delete_link (priv->pending_messages_queue, m);
1332 g_object_unref (message);
1336 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1337 const GList *messages) {
1338 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1339 /* Copy messages as the messges list (probably is) our own */
1340 GList *msgs = g_list_copy ((GList *) messages);
1343 GArray *message_ids;
1345 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1346 g_return_if_fail (priv->ready);
1348 length = g_list_length ((GList *)messages);
1353 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1355 for (l = msgs; l != NULL; l = g_list_next (l)) {
1358 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1360 m = g_queue_find (priv->pending_messages_queue, message);
1361 g_assert (m != NULL);
1362 g_queue_delete_link (priv->pending_messages_queue, m);
1364 if (empathy_message_get_sender (message) != priv->user) {
1365 guint id = empathy_message_get_id (message);
1366 g_array_append_val (message_ids, id);
1368 g_object_unref (message);
1371 if (message_ids->len > 0)
1372 acknowledge_messages (chat, message_ids);
1374 g_array_free (message_ids, TRUE);