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,
235 const gchar *message_body)
237 EmpathyTpChatPriv *priv;
238 EmpathyMessage *message;
240 priv = GET_PRIV (chat);
242 message = empathy_message_new (message_body);
243 empathy_message_set_tptype (message, type);
244 empathy_message_set_receiver (message, priv->user);
245 empathy_message_set_timestamp (message, timestamp);
246 empathy_message_set_id (message, id);
247 empathy_message_set_incoming (message, incoming);
249 g_queue_push_tail (priv->messages_queue, message);
251 if (from_handle == 0) {
252 empathy_message_set_sender (message, priv->user);
253 tp_chat_emit_queued_messages (chat);
255 empathy_tp_contact_factory_get_from_handle (priv->factory,
257 tp_chat_got_sender_cb,
258 message, NULL, G_OBJECT (chat));
263 tp_chat_received_cb (TpChannel *channel,
269 const gchar *message_body,
273 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
274 EmpathyTpChatPriv *priv = GET_PRIV (chat);
276 if (priv->channel == NULL)
279 if (priv->listing_pending_messages) {
283 DEBUG ("Message received: %s", message_body);
285 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
286 !tp_strdiff (message_body, "")) {
289 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
291 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
292 g_array_append_val (ids, message_id);
293 acknowledge_messages (chat, ids);
294 g_array_free (ids, TRUE);
299 tp_chat_build_message (chat,
309 tp_chat_sent_cb (TpChannel *channel,
312 const gchar *message_body,
316 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
317 EmpathyTpChatPriv *priv = GET_PRIV (chat);
319 if (priv->channel == NULL)
322 DEBUG ("Message sent: %s", message_body);
324 tp_chat_build_message (chat,
334 tp_chat_send_error_cb (TpChannel *channel,
338 const gchar *message_body,
342 EmpathyTpChatPriv *priv = GET_PRIV (chat);
344 if (priv->channel == NULL)
347 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
349 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
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,
363 empathy_message_get_body (message),
364 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
370 TpChannelChatState state;
374 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
375 EmpathyContact *contact,
380 TpChannelChatState state;
383 DEBUG ("Error: %s", error->message);
387 state = GPOINTER_TO_UINT (user_data);
388 DEBUG ("Chat state changed for %s (%d): %d",
389 empathy_contact_get_name (contact),
390 empathy_contact_get_handle (contact), state);
392 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
396 tp_chat_state_changed_cb (TpChannel *channel,
398 TpChannelChatState state,
402 EmpathyTpChatPriv *priv = GET_PRIV (chat);
404 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
405 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
410 tp_chat_list_pending_messages_cb (TpChannel *channel,
411 const GPtrArray *messages_list,
416 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
417 EmpathyTpChatPriv *priv = GET_PRIV (chat);
419 GArray *empty_non_text_content_ids = NULL;
421 priv->listing_pending_messages = FALSE;
423 if (priv->channel == NULL)
427 DEBUG ("Error listing pending messages: %s", error->message);
431 for (i = 0; i < messages_list->len; i++) {
432 GValueArray *message_struct;
433 const gchar *message_body;
440 message_struct = g_ptr_array_index (messages_list, i);
442 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
443 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
444 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
445 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
446 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
447 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
449 DEBUG ("Message pending: %s", message_body);
451 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
452 !tp_strdiff (message_body, "")) {
453 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
455 if (empty_non_text_content_ids == NULL) {
456 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
459 g_array_append_val (empty_non_text_content_ids, message_id);
463 tp_chat_build_message (chat,
472 if (empty_non_text_content_ids != NULL) {
473 acknowledge_messages (chat, empty_non_text_content_ids);
474 g_array_free (empty_non_text_content_ids, TRUE);
479 tp_chat_property_flags_changed_cb (TpProxy *proxy,
480 const GPtrArray *properties,
484 EmpathyTpChatPriv *priv = GET_PRIV (chat);
487 if (priv->channel == NULL)
490 if (!priv->had_properties_list || !properties) {
494 for (i = 0; i < properties->len; i++) {
495 GValueArray *prop_struct;
496 TpChatProperty *property;
500 prop_struct = g_ptr_array_index (properties, i);
501 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
502 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
504 for (j = 0; j < priv->properties->len; j++) {
505 property = g_ptr_array_index (priv->properties, j);
506 if (property->id == id) {
507 property->flags = flags;
508 DEBUG ("property %s flags changed: %d",
509 property->name, property->flags);
517 tp_chat_properties_changed_cb (TpProxy *proxy,
518 const GPtrArray *properties,
522 EmpathyTpChatPriv *priv = GET_PRIV (chat);
525 if (priv->channel == NULL)
528 if (!priv->had_properties_list || !properties) {
532 for (i = 0; i < properties->len; i++) {
533 GValueArray *prop_struct;
534 TpChatProperty *property;
538 prop_struct = g_ptr_array_index (properties, i);
539 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
540 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
542 for (j = 0; j < priv->properties->len; j++) {
543 property = g_ptr_array_index (priv->properties, j);
544 if (property->id == id) {
545 if (property->value) {
546 g_value_copy (src_value, property->value);
548 property->value = tp_g_value_slice_dup (src_value);
551 DEBUG ("property %s changed", property->name);
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 DEBUG ("Error getting properties: %s", error->message);
572 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
576 tp_chat_list_properties_cb (TpProxy *proxy,
577 const GPtrArray *properties,
582 EmpathyTpChatPriv *priv = GET_PRIV (chat);
586 if (priv->channel == NULL)
589 priv->had_properties_list = TRUE;
592 DEBUG ("Error listing properties: %s", error->message);
596 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
597 priv->properties = g_ptr_array_sized_new (properties->len);
598 for (i = 0; i < properties->len; i++) {
599 GValueArray *prop_struct;
600 TpChatProperty *property;
602 prop_struct = g_ptr_array_index (properties, i);
603 property = g_slice_new0 (TpChatProperty);
604 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
605 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
606 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
608 DEBUG ("Adding property name=%s id=%d flags=%d",
609 property->name, property->id, property->flags);
610 g_ptr_array_add (priv->properties, property);
611 if (property->flags & TP_PROPERTY_FLAG_READ) {
612 g_array_append_val (ids, property->id);
616 tp_cli_properties_interface_call_get_properties (proxy, -1,
618 tp_chat_get_properties_cb,
622 g_array_free (ids, TRUE);
626 empathy_tp_chat_set_property (EmpathyTpChat *chat,
630 EmpathyTpChatPriv *priv = GET_PRIV (chat);
631 TpChatProperty *property;
634 for (i = 0; i < priv->properties->len; i++) {
635 property = g_ptr_array_index (priv->properties, i);
636 if (!tp_strdiff (property->name, name)) {
637 GPtrArray *properties;
640 GValue dest_value = {0, };
642 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
646 g_value_init (&id, G_TYPE_UINT);
647 g_value_init (&dest_value, G_TYPE_VALUE);
648 g_value_set_uint (&id, property->id);
649 g_value_set_boxed (&dest_value, value);
651 prop = g_value_array_new (2);
652 g_value_array_append (prop, &id);
653 g_value_array_append (prop, &dest_value);
655 properties = g_ptr_array_sized_new (1);
656 g_ptr_array_add (properties, prop);
658 DEBUG ("Set property %s", name);
659 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
661 (tp_cli_properties_interface_callback_for_set_properties)
663 "Seting property", NULL,
666 g_ptr_array_free (properties, TRUE);
667 g_value_array_free (prop);
675 tp_chat_dispose (GObject *object)
677 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
678 EmpathyTpChatPriv *priv = GET_PRIV (self);
680 if (priv->dispose_has_run)
683 priv->dispose_has_run = TRUE;
685 if (priv->channel != NULL) {
686 g_signal_handlers_disconnect_by_func (priv->channel,
687 tp_chat_invalidated_cb, self);
688 g_object_unref (priv->channel);
690 priv->channel = NULL;
692 if (priv->remote_contact != NULL)
693 g_object_unref (priv->remote_contact);
694 priv->remote_contact = NULL;
696 if (priv->factory != NULL)
697 g_object_unref (priv->factory);
698 priv->factory = NULL;
700 if (priv->user != NULL)
701 g_object_unref (priv->user);
704 if (priv->contact_monitor)
705 g_object_unref (priv->contact_monitor);
706 priv->contact_monitor = NULL;
708 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
709 g_queue_clear (priv->messages_queue);
711 g_queue_foreach (priv->pending_messages_queue,
712 (GFunc) g_object_unref, NULL);
713 g_queue_clear (priv->pending_messages_queue);
715 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
716 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
720 tp_chat_finalize (GObject *object)
722 EmpathyTpChatPriv *priv = GET_PRIV (object);
725 DEBUG ("Finalize: %p", object);
727 if (priv->properties) {
728 for (i = 0; i < priv->properties->len; i++) {
729 TpChatProperty *property;
731 property = g_ptr_array_index (priv->properties, i);
732 g_free (property->name);
733 if (property->value) {
734 tp_g_value_slice_free (property->value);
736 g_slice_free (TpChatProperty, property);
738 g_ptr_array_free (priv->properties, TRUE);
741 g_queue_free (priv->messages_queue);
742 g_queue_free (priv->pending_messages_queue);
744 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
748 tp_chat_check_if_ready (EmpathyTpChat *chat)
750 EmpathyTpChatPriv *priv = GET_PRIV (chat);
752 if (priv->ready || priv->user == NULL ||
753 (priv->members == NULL && priv->remote_contact == NULL)) {
759 tp_cli_channel_type_text_connect_to_received (priv->channel,
762 G_OBJECT (chat), NULL);
763 priv->listing_pending_messages = TRUE;
764 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
766 tp_chat_list_pending_messages_cb,
770 tp_cli_channel_type_text_connect_to_sent (priv->channel,
773 G_OBJECT (chat), NULL);
774 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
775 tp_chat_send_error_cb,
777 G_OBJECT (chat), NULL);
778 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
779 tp_chat_state_changed_cb,
781 G_OBJECT (chat), NULL);
782 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
783 tp_chat_state_changed_cb,
785 G_OBJECT (chat), NULL);
787 g_object_notify (G_OBJECT (chat), "ready");
791 tp_chat_update_remote_contact (EmpathyTpChat *chat)
793 EmpathyTpChatPriv *priv = GET_PRIV (chat);
794 EmpathyContact *contact = NULL;
795 TpHandle self_handle;
796 TpHandleType handle_type;
799 /* If this is a named chatroom, never pretend it is a private chat */
800 tp_channel_get_handle (priv->channel, &handle_type);
801 if (handle_type == TP_HANDLE_TYPE_ROOM) {
805 /* This is an MSN-like chat where anyone can join the chat at anytime.
806 * If there is only one non-self contact member, we are in a private
807 * chat and we set the "remote-contact" property to that contact. If
808 * there are more, set the "remote-contact" property to NULL and the
809 * UI will display a contact list. */
810 self_handle = tp_channel_group_get_self_handle (priv->channel);
811 for (l = priv->members; l; l = l->next) {
812 /* Skip self contact if member */
813 if (empathy_contact_get_handle (l->data) == self_handle) {
817 /* We have more than one remote contact, break */
818 if (contact != NULL) {
823 /* If we didn't find yet a remote contact, keep this one */
827 if (priv->remote_contact == contact) {
831 DEBUG ("Changing remote contact from %p to %p",
832 priv->remote_contact, contact);
834 if (priv->remote_contact) {
835 g_object_unref (priv->remote_contact);
838 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
839 g_object_notify (G_OBJECT (chat), "remote-contact");
843 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
845 EmpathyContact * const * contacts,
847 const TpHandle *failed,
852 EmpathyTpChatPriv *priv = GET_PRIV (chat);
854 const TpIntSet *members;
856 EmpathyContact *contact;
859 DEBUG ("Error: %s", error->message);
863 members = tp_channel_group_get_members (priv->channel);
864 for (i = 0; i < n_contacts; i++) {
865 contact = contacts[i];
866 handle = empathy_contact_get_handle (contact);
868 /* Make sure the contact is still member */
869 if (tp_intset_is_member (members, handle)) {
870 priv->members = g_list_prepend (priv->members,
871 g_object_ref (contact));
872 g_signal_emit_by_name (chat, "members-changed",
873 contact, NULL, 0, NULL, TRUE);
877 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
878 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
881 static EmpathyContact *
882 chat_lookup_contact (EmpathyTpChat *chat,
886 EmpathyTpChatPriv *priv = GET_PRIV (chat);
889 for (l = priv->members; l; l = l->next) {
890 EmpathyContact *c = l->data;
892 if (empathy_contact_get_handle (c) != handle) {
897 /* Caller takes the reference. */
898 priv->members = g_list_delete_link (priv->members, l);
910 tp_chat_group_members_changed_cb (TpChannel *self,
914 GArray *local_pending,
915 GArray *remote_pending,
920 EmpathyTpChatPriv *priv = GET_PRIV (chat);
921 EmpathyContact *contact;
922 EmpathyContact *actor_contact = NULL;
926 actor_contact = chat_lookup_contact (chat, actor, FALSE);
927 if (actor_contact == NULL) {
928 /* FIXME: handle this a tad more gracefully: perhaps
929 * the actor was a server op. We could use the
930 * contact-ids detail of MembersChangedDetailed.
932 DEBUG ("actor %u not a channel member", actor);
936 /* Remove contacts that are not members anymore */
937 for (i = 0; i < removed->len; i++) {
938 contact = chat_lookup_contact (chat,
939 g_array_index (removed, TpHandle, i), TRUE);
941 if (contact != NULL) {
942 g_signal_emit_by_name (chat, "members-changed", contact,
943 actor_contact, reason, message,
945 g_object_unref (contact);
949 /* Request added contacts */
950 if (added->len > 0) {
951 empathy_tp_contact_factory_get_from_handles (priv->factory,
952 added->len, (TpHandle *) added->data,
953 tp_chat_got_added_contacts_cb, NULL, NULL,
957 tp_chat_update_remote_contact (chat);
959 if (actor_contact != NULL) {
960 g_object_unref (actor_contact);
965 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
966 EmpathyContact *contact,
971 EmpathyTpChatPriv *priv = GET_PRIV (chat);
974 DEBUG ("Error: %s", error->message);
975 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
979 priv->remote_contact = g_object_ref (contact);
980 g_object_notify (chat, "remote-contact");
982 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
986 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
987 EmpathyContact *contact,
992 EmpathyTpChatPriv *priv = GET_PRIV (chat);
995 DEBUG ("Error: %s", error->message);
996 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1000 priv->user = g_object_ref (contact);
1001 empathy_contact_set_is_user (priv->user, TRUE);
1002 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1006 tp_chat_constructor (GType type,
1008 GObjectConstructParam *props)
1011 EmpathyTpChatPriv *priv;
1012 TpConnection *connection;
1015 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1017 priv = GET_PRIV (chat);
1019 connection = tp_channel_borrow_connection (priv->channel);
1020 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1021 g_signal_connect (priv->channel, "invalidated",
1022 G_CALLBACK (tp_chat_invalidated_cb),
1025 if (tp_proxy_has_interface_by_id (priv->channel,
1026 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1027 const TpIntSet *members;
1030 /* Get self contact from the group's self handle */
1031 handle = tp_channel_group_get_self_handle (priv->channel);
1032 empathy_tp_contact_factory_get_from_handle (priv->factory,
1033 handle, tp_chat_got_self_contact_cb,
1036 /* Get initial member contacts */
1037 members = tp_channel_group_get_members (priv->channel);
1038 handles = tp_intset_to_array (members);
1039 empathy_tp_contact_factory_get_from_handles (priv->factory,
1040 handles->len, (TpHandle *) handles->data,
1041 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1043 g_signal_connect (priv->channel, "group-members-changed",
1044 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1046 /* Get the self contact from the connection's self handle */
1047 handle = tp_connection_get_self_handle (connection);
1048 empathy_tp_contact_factory_get_from_handle (priv->factory,
1049 handle, tp_chat_got_self_contact_cb,
1052 /* Get the remote contact */
1053 handle = tp_channel_get_handle (priv->channel, NULL);
1054 empathy_tp_contact_factory_get_from_handle (priv->factory,
1055 handle, tp_chat_got_remote_contact_cb,
1059 if (tp_proxy_has_interface_by_id (priv->channel,
1060 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1061 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1062 tp_chat_list_properties_cb,
1065 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1066 tp_chat_properties_changed_cb,
1068 G_OBJECT (chat), NULL);
1069 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1070 tp_chat_property_flags_changed_cb,
1072 G_OBJECT (chat), NULL);
1079 tp_chat_get_property (GObject *object,
1084 EmpathyTpChatPriv *priv = GET_PRIV (object);
1088 g_value_set_object (value, priv->channel);
1090 case PROP_REMOTE_CONTACT:
1091 g_value_set_object (value, priv->remote_contact);
1094 g_value_set_boolean (value, priv->ready);
1097 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1103 tp_chat_set_property (GObject *object,
1105 const GValue *value,
1108 EmpathyTpChatPriv *priv = GET_PRIV (object);
1112 priv->channel = g_value_dup_object (value);
1115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1121 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1123 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1125 object_class->dispose = tp_chat_dispose;
1126 object_class->finalize = tp_chat_finalize;
1127 object_class->constructor = tp_chat_constructor;
1128 object_class->get_property = tp_chat_get_property;
1129 object_class->set_property = tp_chat_set_property;
1131 g_object_class_install_property (object_class,
1133 g_param_spec_object ("channel",
1134 "telepathy channel",
1135 "The text channel for the chat",
1138 G_PARAM_CONSTRUCT_ONLY));
1140 g_object_class_install_property (object_class,
1141 PROP_REMOTE_CONTACT,
1142 g_param_spec_object ("remote-contact",
1143 "The remote contact",
1144 "The remote contact if there is no group iface on the channel",
1145 EMPATHY_TYPE_CONTACT,
1148 g_object_class_install_property (object_class,
1150 g_param_spec_boolean ("ready",
1151 "Is the object ready",
1152 "This object can't be used until this becomes true",
1157 signals[MESSAGE_RECEIVED] =
1158 g_signal_new ("message-received",
1159 G_TYPE_FROM_CLASS (klass),
1163 g_cclosure_marshal_VOID__OBJECT,
1165 1, EMPATHY_TYPE_MESSAGE);
1167 signals[SEND_ERROR] =
1168 g_signal_new ("send-error",
1169 G_TYPE_FROM_CLASS (klass),
1173 _empathy_marshal_VOID__STRING_UINT,
1175 2, G_TYPE_STRING, G_TYPE_UINT);
1177 signals[CHAT_STATE_CHANGED] =
1178 g_signal_new ("chat-state-changed",
1179 G_TYPE_FROM_CLASS (klass),
1183 _empathy_marshal_VOID__OBJECT_UINT,
1185 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1187 signals[PROPERTY_CHANGED] =
1188 g_signal_new ("property-changed",
1189 G_TYPE_FROM_CLASS (klass),
1193 _empathy_marshal_VOID__STRING_BOXED,
1195 2, G_TYPE_STRING, G_TYPE_VALUE);
1198 g_signal_new ("destroy",
1199 G_TYPE_FROM_CLASS (klass),
1203 g_cclosure_marshal_VOID__VOID,
1207 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1211 empathy_tp_chat_init (EmpathyTpChat *chat)
1213 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1214 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1217 priv->contact_monitor = NULL;
1218 priv->messages_queue = g_queue_new ();
1219 priv->pending_messages_queue = g_queue_new ();
1223 tp_chat_iface_init (EmpathyContactListIface *iface)
1225 iface->add = tp_chat_add;
1226 iface->remove = tp_chat_remove;
1227 iface->get_members = tp_chat_get_members;
1228 iface->get_monitor = tp_chat_get_monitor;
1232 empathy_tp_chat_new (TpChannel *channel)
1234 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1240 empathy_tp_chat_close (EmpathyTpChat *chat) {
1241 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1243 /* If there are still messages left, it'll come back..
1244 * We loose the ordering of sent messages though */
1245 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1246 "closing channel", NULL, NULL);
1250 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1252 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1254 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1256 return tp_channel_get_identifier (priv->channel);
1260 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1262 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1264 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1265 g_return_val_if_fail (priv->ready, NULL);
1267 return priv->remote_contact;
1271 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1273 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1275 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1277 return priv->channel;
1281 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1283 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1285 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1287 return tp_channel_borrow_connection (priv->channel);
1291 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1293 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1295 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1301 empathy_tp_chat_send (EmpathyTpChat *chat,
1302 EmpathyMessage *message)
1304 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1305 const gchar *message_body;
1306 TpChannelTextMessageType message_type;
1308 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1309 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1310 g_return_if_fail (priv->ready);
1312 message_body = empathy_message_get_body (message);
1313 message_type = empathy_message_get_tptype (message);
1315 DEBUG ("Sending message: %s", message_body);
1316 tp_cli_channel_type_text_call_send (priv->channel, -1,
1320 g_object_ref (message),
1321 (GDestroyNotify) g_object_unref,
1326 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1327 TpChannelChatState state)
1329 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1331 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1332 g_return_if_fail (priv->ready);
1334 if (tp_proxy_has_interface_by_id (priv->channel,
1335 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1336 DEBUG ("Set state: %d", state);
1337 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1340 "setting chat state",
1348 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1350 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1352 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1353 g_return_val_if_fail (priv->ready, NULL);
1355 return priv->pending_messages_queue->head;
1359 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1360 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1362 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1363 priv->channel, -1, ids, tp_chat_async_cb,
1364 "acknowledging received message", NULL, G_OBJECT (chat));
1368 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1369 EmpathyMessage *message) {
1370 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1371 GArray *message_ids;
1375 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1376 g_return_if_fail (priv->ready);
1378 if (!empathy_message_is_incoming (message))
1381 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1383 id = empathy_message_get_id (message);
1384 g_array_append_val (message_ids, id);
1385 acknowledge_messages (chat, message_ids);
1386 g_array_free (message_ids, TRUE);
1389 m = g_queue_find (priv->pending_messages_queue, message);
1390 g_assert (m != NULL);
1391 g_queue_delete_link (priv->pending_messages_queue, m);
1392 g_object_unref (message);
1396 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1397 const GList *messages) {
1398 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1399 /* Copy messages as the messges list (probably is) our own */
1400 GList *msgs = g_list_copy ((GList *) messages);
1403 GArray *message_ids;
1405 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1406 g_return_if_fail (priv->ready);
1408 length = g_list_length ((GList *) messages);
1413 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1415 for (l = msgs; l != NULL; l = g_list_next (l)) {
1418 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1420 m = g_queue_find (priv->pending_messages_queue, message);
1421 g_assert (m != NULL);
1422 g_queue_delete_link (priv->pending_messages_queue, m);
1424 if (empathy_message_is_incoming (message)) {
1425 guint id = empathy_message_get_id (message);
1426 g_array_append_val (message_ids, id);
1428 g_object_unref (message);
1431 if (message_ids->len > 0)
1432 acknowledge_messages (chat, message_ids);
1434 g_array_free (message_ids, TRUE);