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 if (priv->remote_contact != NULL)
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,
211 EmpathyContact *contact,
216 EmpathyTpChatPriv *priv = GET_PRIV (chat);
219 DEBUG ("Error: %s", error->message);
220 /* Do not block the message queue, just drop this message */
221 g_queue_remove (priv->messages_queue, message);
223 empathy_message_set_sender (message, contact);
226 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
230 tp_chat_build_message (EmpathyTpChat *chat,
236 const gchar *message_body)
238 EmpathyTpChatPriv *priv;
239 EmpathyMessage *message;
241 priv = GET_PRIV (chat);
243 message = empathy_message_new (message_body);
244 empathy_message_set_tptype (message, type);
245 empathy_message_set_receiver (message, priv->user);
246 empathy_message_set_timestamp (message, timestamp);
247 empathy_message_set_id (message, id);
248 empathy_message_set_incoming (message, incoming);
250 g_queue_push_tail (priv->messages_queue, message);
252 if (from_handle == 0) {
253 empathy_message_set_sender (message, priv->user);
254 tp_chat_emit_queued_messages (chat);
256 empathy_tp_contact_factory_get_from_handle (priv->factory,
258 tp_chat_got_sender_cb,
259 message, NULL, G_OBJECT (chat));
264 tp_chat_received_cb (TpChannel *channel,
270 const gchar *message_body,
274 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
275 EmpathyTpChatPriv *priv = GET_PRIV (chat);
277 if (priv->channel == NULL)
280 if (priv->listing_pending_messages) {
284 DEBUG ("Message received: %s", message_body);
286 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
287 !tp_strdiff (message_body, "")) {
290 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
292 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
293 g_array_append_val (ids, message_id);
294 acknowledge_messages (chat, ids);
295 g_array_free (ids, TRUE);
300 tp_chat_build_message (chat,
310 tp_chat_sent_cb (TpChannel *channel,
313 const gchar *message_body,
317 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
318 EmpathyTpChatPriv *priv = GET_PRIV (chat);
320 if (priv->channel == NULL)
323 DEBUG ("Message sent: %s", message_body);
325 tp_chat_build_message (chat,
335 tp_chat_send_error_cb (TpChannel *channel,
339 const gchar *message_body,
343 EmpathyTpChatPriv *priv = GET_PRIV (chat);
345 if (priv->channel == NULL)
348 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
350 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
354 tp_chat_send_cb (TpChannel *proxy,
359 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
362 DEBUG ("Error: %s", error->message);
363 g_signal_emit (chat, signals[SEND_ERROR], 0,
364 empathy_message_get_body (message),
365 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
371 TpChannelChatState state;
375 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
376 EmpathyContact *contact,
381 TpChannelChatState state;
384 DEBUG ("Error: %s", error->message);
388 state = GPOINTER_TO_UINT (user_data);
389 DEBUG ("Chat state changed for %s (%d): %d",
390 empathy_contact_get_name (contact),
391 empathy_contact_get_handle (contact), state);
393 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
397 tp_chat_state_changed_cb (TpChannel *channel,
399 TpChannelChatState state,
403 EmpathyTpChatPriv *priv = GET_PRIV (chat);
405 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
406 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
411 tp_chat_list_pending_messages_cb (TpChannel *channel,
412 const GPtrArray *messages_list,
417 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
418 EmpathyTpChatPriv *priv = GET_PRIV (chat);
420 GArray *empty_non_text_content_ids = NULL;
422 priv->listing_pending_messages = FALSE;
424 if (priv->channel == NULL)
428 DEBUG ("Error listing pending messages: %s", error->message);
432 for (i = 0; i < messages_list->len; i++) {
433 GValueArray *message_struct;
434 const gchar *message_body;
441 message_struct = g_ptr_array_index (messages_list, i);
443 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
444 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
445 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
446 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
447 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
448 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
450 DEBUG ("Message pending: %s", message_body);
452 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
453 !tp_strdiff (message_body, "")) {
454 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
456 if (empty_non_text_content_ids == NULL) {
457 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
460 g_array_append_val (empty_non_text_content_ids, message_id);
464 tp_chat_build_message (chat,
473 if (empty_non_text_content_ids != NULL) {
474 acknowledge_messages (chat, empty_non_text_content_ids);
475 g_array_free (empty_non_text_content_ids, TRUE);
480 tp_chat_property_flags_changed_cb (TpProxy *proxy,
481 const GPtrArray *properties,
485 EmpathyTpChatPriv *priv = GET_PRIV (chat);
488 if (priv->channel == NULL)
491 if (!priv->had_properties_list || !properties) {
495 for (i = 0; i < properties->len; i++) {
496 GValueArray *prop_struct;
497 TpChatProperty *property;
501 prop_struct = g_ptr_array_index (properties, i);
502 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
503 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
505 for (j = 0; j < priv->properties->len; j++) {
506 property = g_ptr_array_index (priv->properties, j);
507 if (property->id == id) {
508 property->flags = flags;
509 DEBUG ("property %s flags changed: %d",
510 property->name, property->flags);
518 tp_chat_properties_changed_cb (TpProxy *proxy,
519 const GPtrArray *properties,
523 EmpathyTpChatPriv *priv = GET_PRIV (chat);
526 if (priv->channel == NULL)
529 if (!priv->had_properties_list || !properties) {
533 for (i = 0; i < properties->len; i++) {
534 GValueArray *prop_struct;
535 TpChatProperty *property;
539 prop_struct = g_ptr_array_index (properties, i);
540 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
541 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
543 for (j = 0; j < priv->properties->len; j++) {
544 property = g_ptr_array_index (priv->properties, j);
545 if (property->id == id) {
546 if (property->value) {
547 g_value_copy (src_value, property->value);
549 property->value = tp_g_value_slice_dup (src_value);
552 DEBUG ("property %s changed", property->name);
553 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
554 property->name, property->value);
562 tp_chat_get_properties_cb (TpProxy *proxy,
563 const GPtrArray *properties,
569 DEBUG ("Error getting properties: %s", error->message);
573 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
577 tp_chat_list_properties_cb (TpProxy *proxy,
578 const GPtrArray *properties,
583 EmpathyTpChatPriv *priv = GET_PRIV (chat);
587 if (priv->channel == NULL)
590 priv->had_properties_list = TRUE;
593 DEBUG ("Error listing properties: %s", error->message);
597 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
598 priv->properties = g_ptr_array_sized_new (properties->len);
599 for (i = 0; i < properties->len; i++) {
600 GValueArray *prop_struct;
601 TpChatProperty *property;
603 prop_struct = g_ptr_array_index (properties, i);
604 property = g_slice_new0 (TpChatProperty);
605 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
606 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
607 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
609 DEBUG ("Adding property name=%s id=%d flags=%d",
610 property->name, property->id, property->flags);
611 g_ptr_array_add (priv->properties, property);
612 if (property->flags & TP_PROPERTY_FLAG_READ) {
613 g_array_append_val (ids, property->id);
617 tp_cli_properties_interface_call_get_properties (proxy, -1,
619 tp_chat_get_properties_cb,
623 g_array_free (ids, TRUE);
627 empathy_tp_chat_set_property (EmpathyTpChat *chat,
631 EmpathyTpChatPriv *priv = GET_PRIV (chat);
632 TpChatProperty *property;
635 for (i = 0; i < priv->properties->len; i++) {
636 property = g_ptr_array_index (priv->properties, i);
637 if (!tp_strdiff (property->name, name)) {
638 GPtrArray *properties;
641 GValue dest_value = {0, };
643 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
647 g_value_init (&id, G_TYPE_UINT);
648 g_value_init (&dest_value, G_TYPE_VALUE);
649 g_value_set_uint (&id, property->id);
650 g_value_set_boxed (&dest_value, value);
652 prop = g_value_array_new (2);
653 g_value_array_append (prop, &id);
654 g_value_array_append (prop, &dest_value);
656 properties = g_ptr_array_sized_new (1);
657 g_ptr_array_add (properties, prop);
659 DEBUG ("Set property %s", name);
660 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
662 (tp_cli_properties_interface_callback_for_set_properties)
664 "Seting property", NULL,
667 g_ptr_array_free (properties, TRUE);
668 g_value_array_free (prop);
676 tp_chat_dispose (GObject *object)
678 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
679 EmpathyTpChatPriv *priv = GET_PRIV (self);
681 if (priv->dispose_has_run)
684 priv->dispose_has_run = TRUE;
686 if (priv->channel != NULL) {
687 g_signal_handlers_disconnect_by_func (priv->channel,
688 tp_chat_invalidated_cb, self);
689 g_object_unref (priv->channel);
691 priv->channel = NULL;
693 if (priv->remote_contact != NULL)
694 g_object_unref (priv->remote_contact);
695 priv->remote_contact = NULL;
697 if (priv->factory != NULL)
698 g_object_unref (priv->factory);
699 priv->factory = NULL;
701 if (priv->user != NULL)
702 g_object_unref (priv->user);
705 if (priv->contact_monitor)
706 g_object_unref (priv->contact_monitor);
707 priv->contact_monitor = NULL;
709 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
710 g_queue_clear (priv->messages_queue);
712 g_queue_foreach (priv->pending_messages_queue,
713 (GFunc) g_object_unref, NULL);
714 g_queue_clear (priv->pending_messages_queue);
716 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
717 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
721 tp_chat_finalize (GObject *object)
723 EmpathyTpChatPriv *priv = GET_PRIV (object);
726 DEBUG ("Finalize: %p", object);
728 if (priv->properties) {
729 for (i = 0; i < priv->properties->len; i++) {
730 TpChatProperty *property;
732 property = g_ptr_array_index (priv->properties, i);
733 g_free (property->name);
734 if (property->value) {
735 tp_g_value_slice_free (property->value);
737 g_slice_free (TpChatProperty, property);
739 g_ptr_array_free (priv->properties, TRUE);
742 g_queue_free (priv->messages_queue);
743 g_queue_free (priv->pending_messages_queue);
745 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
749 tp_chat_check_if_ready (EmpathyTpChat *chat)
751 EmpathyTpChatPriv *priv = GET_PRIV (chat);
753 if (priv->ready || priv->user == NULL ||
754 (priv->members == NULL && priv->remote_contact == NULL)) {
760 tp_cli_channel_type_text_connect_to_received (priv->channel,
763 G_OBJECT (chat), NULL);
764 priv->listing_pending_messages = TRUE;
765 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
767 tp_chat_list_pending_messages_cb,
771 tp_cli_channel_type_text_connect_to_sent (priv->channel,
774 G_OBJECT (chat), NULL);
775 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
776 tp_chat_send_error_cb,
778 G_OBJECT (chat), NULL);
779 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
780 tp_chat_state_changed_cb,
782 G_OBJECT (chat), NULL);
783 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
784 tp_chat_state_changed_cb,
786 G_OBJECT (chat), NULL);
788 g_object_notify (G_OBJECT (chat), "ready");
792 tp_chat_update_remote_contact (EmpathyTpChat *chat)
794 EmpathyTpChatPriv *priv = GET_PRIV (chat);
795 EmpathyContact *contact = NULL;
796 TpHandle self_handle;
797 TpHandleType handle_type;
800 /* If this is a named chatroom, never pretend it is a private chat */
801 tp_channel_get_handle (priv->channel, &handle_type);
802 if (handle_type == TP_HANDLE_TYPE_ROOM) {
806 /* This is an MSN-like chat where anyone can join the chat at anytime.
807 * If there is only one non-self contact member, we are in a private
808 * chat and we set the "remote-contact" property to that contact. If
809 * there are more, set the "remote-contact" property to NULL and the
810 * UI will display a contact list. */
811 self_handle = tp_channel_group_get_self_handle (priv->channel);
812 for (l = priv->members; l; l = l->next) {
813 /* Skip self contact if member */
814 if (empathy_contact_get_handle (l->data) == self_handle) {
818 /* We have more than one remote contact, break */
819 if (contact != NULL) {
824 /* If we didn't find yet a remote contact, keep this one */
828 if (priv->remote_contact == contact) {
832 DEBUG ("Changing remote contact from %p to %p",
833 priv->remote_contact, contact);
835 if (priv->remote_contact) {
836 g_object_unref (priv->remote_contact);
839 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
840 g_object_notify (G_OBJECT (chat), "remote-contact");
844 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
846 EmpathyContact * const * contacts,
848 const TpHandle *failed,
853 EmpathyTpChatPriv *priv = GET_PRIV (chat);
855 const TpIntSet *members;
857 EmpathyContact *contact;
860 DEBUG ("Error: %s", error->message);
864 members = tp_channel_group_get_members (priv->channel);
865 for (i = 0; i < n_contacts; i++) {
866 contact = contacts[i];
867 handle = empathy_contact_get_handle (contact);
869 /* Make sure the contact is still member */
870 if (tp_intset_is_member (members, handle)) {
871 priv->members = g_list_prepend (priv->members,
872 g_object_ref (contact));
873 g_signal_emit_by_name (chat, "members-changed",
874 contact, NULL, 0, NULL, TRUE);
878 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
879 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
882 static EmpathyContact *
883 chat_lookup_contact (EmpathyTpChat *chat,
887 EmpathyTpChatPriv *priv = GET_PRIV (chat);
890 for (l = priv->members; l; l = l->next) {
891 EmpathyContact *c = l->data;
893 if (empathy_contact_get_handle (c) != handle) {
898 /* Caller takes the reference. */
899 priv->members = g_list_delete_link (priv->members, l);
911 tp_chat_group_members_changed_cb (TpChannel *self,
915 GArray *local_pending,
916 GArray *remote_pending,
921 EmpathyTpChatPriv *priv = GET_PRIV (chat);
922 EmpathyContact *contact;
923 EmpathyContact *actor_contact = NULL;
927 actor_contact = chat_lookup_contact (chat, actor, FALSE);
928 if (actor_contact == NULL) {
929 /* FIXME: handle this a tad more gracefully: perhaps
930 * the actor was a server op. We could use the
931 * contact-ids detail of MembersChangedDetailed.
933 DEBUG ("actor %u not a channel member", actor);
937 /* Remove contacts that are not members anymore */
938 for (i = 0; i < removed->len; i++) {
939 contact = chat_lookup_contact (chat,
940 g_array_index (removed, TpHandle, i), TRUE);
942 if (contact != NULL) {
943 g_signal_emit_by_name (chat, "members-changed", contact,
944 actor_contact, reason, message,
946 g_object_unref (contact);
950 /* Request added contacts */
951 if (added->len > 0) {
952 empathy_tp_contact_factory_get_from_handles (priv->factory,
953 added->len, (TpHandle *) added->data,
954 tp_chat_got_added_contacts_cb, NULL, NULL,
958 tp_chat_update_remote_contact (chat);
960 if (actor_contact != NULL) {
961 g_object_unref (actor_contact);
966 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
967 EmpathyContact *contact,
972 EmpathyTpChatPriv *priv = GET_PRIV (chat);
975 DEBUG ("Error: %s", error->message);
976 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
980 priv->remote_contact = g_object_ref (contact);
981 g_object_notify (chat, "remote-contact");
983 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
987 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
988 EmpathyContact *contact,
993 EmpathyTpChatPriv *priv = GET_PRIV (chat);
996 DEBUG ("Error: %s", error->message);
997 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1001 priv->user = g_object_ref (contact);
1002 empathy_contact_set_is_user (priv->user, TRUE);
1003 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1007 tp_chat_constructor (GType type,
1009 GObjectConstructParam *props)
1012 EmpathyTpChatPriv *priv;
1013 TpConnection *connection;
1016 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1018 priv = GET_PRIV (chat);
1020 connection = tp_channel_borrow_connection (priv->channel);
1021 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1022 g_signal_connect (priv->channel, "invalidated",
1023 G_CALLBACK (tp_chat_invalidated_cb),
1026 if (tp_proxy_has_interface_by_id (priv->channel,
1027 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1028 const TpIntSet *members;
1031 /* Get self contact from the group's self handle */
1032 handle = tp_channel_group_get_self_handle (priv->channel);
1033 empathy_tp_contact_factory_get_from_handle (priv->factory,
1034 handle, tp_chat_got_self_contact_cb,
1037 /* Get initial member contacts */
1038 members = tp_channel_group_get_members (priv->channel);
1039 handles = tp_intset_to_array (members);
1040 empathy_tp_contact_factory_get_from_handles (priv->factory,
1041 handles->len, (TpHandle *) handles->data,
1042 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1044 g_signal_connect (priv->channel, "group-members-changed",
1045 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1047 /* Get the self contact from the connection's self handle */
1048 handle = tp_connection_get_self_handle (connection);
1049 empathy_tp_contact_factory_get_from_handle (priv->factory,
1050 handle, tp_chat_got_self_contact_cb,
1053 /* Get the remote contact */
1054 handle = tp_channel_get_handle (priv->channel, NULL);
1055 empathy_tp_contact_factory_get_from_handle (priv->factory,
1056 handle, tp_chat_got_remote_contact_cb,
1060 if (tp_proxy_has_interface_by_id (priv->channel,
1061 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1062 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1063 tp_chat_list_properties_cb,
1066 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1067 tp_chat_properties_changed_cb,
1069 G_OBJECT (chat), NULL);
1070 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1071 tp_chat_property_flags_changed_cb,
1073 G_OBJECT (chat), NULL);
1080 tp_chat_get_property (GObject *object,
1085 EmpathyTpChatPriv *priv = GET_PRIV (object);
1089 g_value_set_object (value, priv->channel);
1091 case PROP_REMOTE_CONTACT:
1092 g_value_set_object (value, priv->remote_contact);
1095 g_value_set_boolean (value, priv->ready);
1098 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1104 tp_chat_set_property (GObject *object,
1106 const GValue *value,
1109 EmpathyTpChatPriv *priv = GET_PRIV (object);
1113 priv->channel = g_value_dup_object (value);
1116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1122 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1124 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1126 object_class->dispose = tp_chat_dispose;
1127 object_class->finalize = tp_chat_finalize;
1128 object_class->constructor = tp_chat_constructor;
1129 object_class->get_property = tp_chat_get_property;
1130 object_class->set_property = tp_chat_set_property;
1132 g_object_class_install_property (object_class,
1134 g_param_spec_object ("channel",
1135 "telepathy channel",
1136 "The text channel for the chat",
1139 G_PARAM_CONSTRUCT_ONLY));
1141 g_object_class_install_property (object_class,
1142 PROP_REMOTE_CONTACT,
1143 g_param_spec_object ("remote-contact",
1144 "The remote contact",
1145 "The remote contact if there is no group iface on the channel",
1146 EMPATHY_TYPE_CONTACT,
1149 g_object_class_install_property (object_class,
1151 g_param_spec_boolean ("ready",
1152 "Is the object ready",
1153 "This object can't be used until this becomes true",
1158 signals[MESSAGE_RECEIVED] =
1159 g_signal_new ("message-received",
1160 G_TYPE_FROM_CLASS (klass),
1164 g_cclosure_marshal_VOID__OBJECT,
1166 1, EMPATHY_TYPE_MESSAGE);
1168 signals[SEND_ERROR] =
1169 g_signal_new ("send-error",
1170 G_TYPE_FROM_CLASS (klass),
1174 _empathy_marshal_VOID__STRING_UINT,
1176 2, G_TYPE_STRING, G_TYPE_UINT);
1178 signals[CHAT_STATE_CHANGED] =
1179 g_signal_new ("chat-state-changed",
1180 G_TYPE_FROM_CLASS (klass),
1184 _empathy_marshal_VOID__OBJECT_UINT,
1186 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1188 signals[PROPERTY_CHANGED] =
1189 g_signal_new ("property-changed",
1190 G_TYPE_FROM_CLASS (klass),
1194 _empathy_marshal_VOID__STRING_BOXED,
1196 2, G_TYPE_STRING, G_TYPE_VALUE);
1199 g_signal_new ("destroy",
1200 G_TYPE_FROM_CLASS (klass),
1204 g_cclosure_marshal_VOID__VOID,
1208 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1212 empathy_tp_chat_init (EmpathyTpChat *chat)
1214 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1215 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1218 priv->contact_monitor = NULL;
1219 priv->messages_queue = g_queue_new ();
1220 priv->pending_messages_queue = g_queue_new ();
1224 tp_chat_iface_init (EmpathyContactListIface *iface)
1226 iface->add = tp_chat_add;
1227 iface->remove = tp_chat_remove;
1228 iface->get_members = tp_chat_get_members;
1229 iface->get_monitor = tp_chat_get_monitor;
1233 empathy_tp_chat_new (TpChannel *channel)
1235 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1241 empathy_tp_chat_close (EmpathyTpChat *chat) {
1242 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1244 /* If there are still messages left, it'll come back..
1245 * We loose the ordering of sent messages though */
1246 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1247 "closing channel", NULL, NULL);
1251 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1253 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1257 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1259 id = tp_channel_get_identifier (priv->channel);
1260 if (!EMP_STR_EMPTY (id))
1262 else if (priv->remote_contact)
1263 return empathy_contact_get_id (priv->remote_contact);
1270 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1272 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1274 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1275 g_return_val_if_fail (priv->ready, NULL);
1277 return priv->remote_contact;
1281 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1283 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1285 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1287 return priv->channel;
1291 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1293 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1295 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1297 return tp_channel_borrow_connection (priv->channel);
1301 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1303 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1305 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1311 empathy_tp_chat_send (EmpathyTpChat *chat,
1312 EmpathyMessage *message)
1314 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1315 const gchar *message_body;
1316 TpChannelTextMessageType message_type;
1318 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1319 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1320 g_return_if_fail (priv->ready);
1322 message_body = empathy_message_get_body (message);
1323 message_type = empathy_message_get_tptype (message);
1325 DEBUG ("Sending message: %s", message_body);
1326 tp_cli_channel_type_text_call_send (priv->channel, -1,
1330 g_object_ref (message),
1331 (GDestroyNotify) g_object_unref,
1336 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1337 TpChannelChatState state)
1339 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1341 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1342 g_return_if_fail (priv->ready);
1344 if (tp_proxy_has_interface_by_id (priv->channel,
1345 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1346 DEBUG ("Set state: %d", state);
1347 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1350 "setting chat state",
1358 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1360 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1362 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1363 g_return_val_if_fail (priv->ready, NULL);
1365 return priv->pending_messages_queue->head;
1369 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1370 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1372 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1373 priv->channel, -1, ids, tp_chat_async_cb,
1374 "acknowledging received message", NULL, G_OBJECT (chat));
1378 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1379 EmpathyMessage *message) {
1380 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1381 GArray *message_ids;
1385 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1386 g_return_if_fail (priv->ready);
1388 if (!empathy_message_is_incoming (message))
1391 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1393 id = empathy_message_get_id (message);
1394 g_array_append_val (message_ids, id);
1395 acknowledge_messages (chat, message_ids);
1396 g_array_free (message_ids, TRUE);
1399 m = g_queue_find (priv->pending_messages_queue, message);
1400 g_assert (m != NULL);
1401 g_queue_delete_link (priv->pending_messages_queue, m);
1402 g_object_unref (message);
1406 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1407 const GList *messages) {
1408 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1409 /* Copy messages as the messges list (probably is) our own */
1410 GList *msgs = g_list_copy ((GList *) messages);
1413 GArray *message_ids;
1415 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1416 g_return_if_fail (priv->ready);
1418 length = g_list_length ((GList *) messages);
1423 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1425 for (l = msgs; l != NULL; l = g_list_next (l)) {
1428 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1430 m = g_queue_find (priv->pending_messages_queue, message);
1431 g_assert (m != NULL);
1432 g_queue_delete_link (priv->pending_messages_queue, m);
1434 if (empathy_message_is_incoming (message)) {
1435 guint id = empathy_message_get_id (message);
1436 g_array_append_val (message_ids, id);
1438 g_object_unref (message);
1441 if (message_ids->len > 0)
1442 acknowledge_messages (chat, message_ids);
1444 g_array_free (message_ids, TRUE);