1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
31 #include "empathy-tp-chat.h"
32 #include "empathy-tp-contact-factory.h"
33 #include "empathy-contact-monitor.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-marshal.h"
36 #include "empathy-time.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
44 gboolean dispose_has_run;
45 EmpathyTpContactFactory *factory;
46 EmpathyContactMonitor *contact_monitor;
48 EmpathyContact *remote_contact;
51 gboolean listing_pending_messages;
52 /* Queue of messages not signalled yet */
53 GQueue *messages_queue;
54 /* Queue of messages signalled but not acked yet */
55 GQueue *pending_messages_queue;
56 gboolean had_properties_list;
57 GPtrArray *properties;
64 TpPropertyFlags flags;
68 static void tp_chat_iface_init (EmpathyContactListIface *iface);
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
92 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
95 tp_chat_invalidated_cb (TpProxy *proxy,
101 DEBUG ("Channel invalidated: %s", message);
102 g_signal_emit (chat, signals[DESTROY], 0);
106 tp_chat_async_cb (TpChannel *proxy,
109 GObject *weak_object)
112 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
117 tp_chat_add (EmpathyContactList *list,
118 EmpathyContact *contact,
119 const gchar *message)
121 EmpathyTpChatPriv *priv = GET_PRIV (list);
123 GArray handles = {(gchar *) &handle, 1};
125 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
126 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
128 handle = empathy_contact_get_handle (contact);
129 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
136 tp_chat_remove (EmpathyContactList *list,
137 EmpathyContact *contact,
138 const gchar *message)
140 EmpathyTpChatPriv *priv = GET_PRIV (list);
142 GArray handles = {(gchar *) &handle, 1};
144 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
145 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
147 handle = empathy_contact_get_handle (contact);
148 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
155 tp_chat_get_members (EmpathyContactList *list)
157 EmpathyTpChatPriv *priv = GET_PRIV (list);
158 GList *members = NULL;
160 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
163 members = g_list_copy (priv->members);
164 g_list_foreach (members, (GFunc) g_object_ref, NULL);
166 members = g_list_prepend (members, g_object_ref (priv->user));
167 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
173 static EmpathyContactMonitor *
174 tp_chat_get_monitor (EmpathyContactList *list)
176 EmpathyTpChatPriv *priv;
178 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
180 priv = GET_PRIV (list);
182 if (priv->contact_monitor == NULL) {
183 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
186 return priv->contact_monitor;
190 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
192 EmpathyTpChatPriv *priv = GET_PRIV (chat);
193 EmpathyMessage *message;
195 /* Check if we can now emit some queued messages */
196 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
197 if (empathy_message_get_sender (message) == NULL) {
201 DEBUG ("Queued message ready");
202 g_queue_pop_head (priv->messages_queue);
203 g_queue_push_tail (priv->pending_messages_queue, message);
204 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
209 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
210 EmpathyContact *contact,
215 EmpathyTpChatPriv *priv = GET_PRIV (chat);
218 DEBUG ("Error: %s", error->message);
219 /* Do not block the message queue, just drop this message */
220 g_queue_remove (priv->messages_queue, message);
222 empathy_message_set_sender (message, contact);
225 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
229 tp_chat_build_message (EmpathyTpChat *chat,
234 const gchar *message_body)
236 EmpathyTpChatPriv *priv;
237 EmpathyMessage *message;
239 priv = GET_PRIV (chat);
241 message = empathy_message_new (message_body);
242 empathy_message_set_tptype (message, type);
243 empathy_message_set_receiver (message, priv->user);
244 empathy_message_set_timestamp (message, timestamp);
245 empathy_message_set_id (message, id);
246 g_queue_push_tail (priv->messages_queue, message);
248 if (from_handle == 0) {
249 empathy_message_set_sender (message, priv->user);
250 tp_chat_emit_queued_messages (chat);
252 empathy_tp_contact_factory_get_from_handle (priv->factory,
254 tp_chat_got_sender_cb,
255 message, NULL, G_OBJECT (chat));
260 tp_chat_received_cb (TpChannel *channel,
266 const gchar *message_body,
270 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
271 EmpathyTpChatPriv *priv = GET_PRIV (chat);
273 if (priv->channel == NULL)
276 if (priv->listing_pending_messages) {
280 DEBUG ("Message received: %s", message_body);
282 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
283 !tp_strdiff (message_body, "")) {
286 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
288 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
289 g_array_append_val (ids, message_id);
290 acknowledge_messages (chat, ids);
291 g_array_free (ids, TRUE);
296 tp_chat_build_message (chat,
305 tp_chat_sent_cb (TpChannel *channel,
308 const gchar *message_body,
312 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
313 EmpathyTpChatPriv *priv = GET_PRIV (chat);
315 if (priv->channel == NULL)
318 DEBUG ("Message sent: %s", message_body);
320 tp_chat_build_message (chat,
329 tp_chat_send_error_cb (TpChannel *channel,
333 const gchar *message_body,
337 EmpathyTpChatPriv *priv = GET_PRIV (chat);
339 if (priv->channel == NULL)
342 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
344 tp_chat_build_message (EMPATHY_TP_CHAT (chat),
353 tp_chat_send_cb (TpChannel *proxy,
358 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
361 DEBUG ("Error: %s", error->message);
362 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
363 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
369 TpChannelChatState state;
373 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
374 EmpathyContact *contact,
379 TpChannelChatState state;
382 DEBUG ("Error: %s", error->message);
386 state = GPOINTER_TO_UINT (user_data);
387 DEBUG ("Chat state changed for %s (%d): %d",
388 empathy_contact_get_name (contact),
389 empathy_contact_get_handle (contact), state);
391 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
395 tp_chat_state_changed_cb (TpChannel *channel,
397 TpChannelChatState state,
401 EmpathyTpChatPriv *priv = GET_PRIV (chat);
403 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
404 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
409 tp_chat_list_pending_messages_cb (TpChannel *channel,
410 const GPtrArray *messages_list,
415 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
416 EmpathyTpChatPriv *priv = GET_PRIV (chat);
418 GArray *empty_non_text_content_ids = NULL;
420 priv->listing_pending_messages = FALSE;
422 if (priv->channel == NULL)
426 DEBUG ("Error listing pending messages: %s", error->message);
430 for (i = 0; i < messages_list->len; i++) {
431 GValueArray *message_struct;
432 const gchar *message_body;
439 message_struct = g_ptr_array_index (messages_list, i);
441 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
442 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
443 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
444 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
445 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
446 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
448 DEBUG ("Message pending: %s", message_body);
450 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
451 !tp_strdiff (message_body, "")) {
452 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
454 if (empty_non_text_content_ids == NULL) {
455 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
458 g_array_append_val (empty_non_text_content_ids, message_id);
462 tp_chat_build_message (chat,
470 if (empty_non_text_content_ids != NULL) {
471 acknowledge_messages (chat, empty_non_text_content_ids);
472 g_array_free (empty_non_text_content_ids, TRUE);
477 tp_chat_property_flags_changed_cb (TpProxy *proxy,
478 const GPtrArray *properties,
482 EmpathyTpChatPriv *priv = GET_PRIV (chat);
485 if (priv->channel == NULL)
488 if (!priv->had_properties_list || !properties) {
492 for (i = 0; i < properties->len; i++) {
493 GValueArray *prop_struct;
494 TpChatProperty *property;
498 prop_struct = g_ptr_array_index (properties, i);
499 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
500 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
502 for (j = 0; j < priv->properties->len; j++) {
503 property = g_ptr_array_index (priv->properties, j);
504 if (property->id == id) {
505 property->flags = flags;
506 DEBUG ("property %s flags changed: %d",
507 property->name, property->flags);
515 tp_chat_properties_changed_cb (TpProxy *proxy,
516 const GPtrArray *properties,
520 EmpathyTpChatPriv *priv = GET_PRIV (chat);
523 if (priv->channel == NULL)
526 if (!priv->had_properties_list || !properties) {
530 for (i = 0; i < properties->len; i++) {
531 GValueArray *prop_struct;
532 TpChatProperty *property;
536 prop_struct = g_ptr_array_index (properties, i);
537 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
538 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
540 for (j = 0; j < priv->properties->len; j++) {
541 property = g_ptr_array_index (priv->properties, j);
542 if (property->id == id) {
543 if (property->value) {
544 g_value_copy (src_value, property->value);
546 property->value = tp_g_value_slice_dup (src_value);
549 DEBUG ("property %s changed", property->name);
550 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
551 property->name, property->value);
559 tp_chat_get_properties_cb (TpProxy *proxy,
560 const GPtrArray *properties,
566 DEBUG ("Error getting properties: %s", error->message);
570 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
574 tp_chat_list_properties_cb (TpProxy *proxy,
575 const GPtrArray *properties,
580 EmpathyTpChatPriv *priv = GET_PRIV (chat);
584 if (priv->channel == NULL)
587 priv->had_properties_list = TRUE;
590 DEBUG ("Error listing properties: %s", error->message);
594 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
595 priv->properties = g_ptr_array_sized_new (properties->len);
596 for (i = 0; i < properties->len; i++) {
597 GValueArray *prop_struct;
598 TpChatProperty *property;
600 prop_struct = g_ptr_array_index (properties, i);
601 property = g_slice_new0 (TpChatProperty);
602 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
603 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
604 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
606 DEBUG ("Adding property name=%s id=%d flags=%d",
607 property->name, property->id, property->flags);
608 g_ptr_array_add (priv->properties, property);
609 if (property->flags & TP_PROPERTY_FLAG_READ) {
610 g_array_append_val (ids, property->id);
614 tp_cli_properties_interface_call_get_properties (proxy, -1,
616 tp_chat_get_properties_cb,
620 g_array_free (ids, TRUE);
624 empathy_tp_chat_set_property (EmpathyTpChat *chat,
628 EmpathyTpChatPriv *priv = GET_PRIV (chat);
629 TpChatProperty *property;
632 for (i = 0; i < priv->properties->len; i++) {
633 property = g_ptr_array_index (priv->properties, i);
634 if (!tp_strdiff (property->name, name)) {
635 GPtrArray *properties;
638 GValue dest_value = {0, };
640 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
644 g_value_init (&id, G_TYPE_UINT);
645 g_value_init (&dest_value, G_TYPE_VALUE);
646 g_value_set_uint (&id, property->id);
647 g_value_set_boxed (&dest_value, value);
649 prop = g_value_array_new (2);
650 g_value_array_append (prop, &id);
651 g_value_array_append (prop, &dest_value);
653 properties = g_ptr_array_sized_new (1);
654 g_ptr_array_add (properties, prop);
656 DEBUG ("Set property %s", name);
657 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
659 (tp_cli_properties_interface_callback_for_set_properties)
661 "Seting property", NULL,
664 g_ptr_array_free (properties, TRUE);
665 g_value_array_free (prop);
673 tp_chat_dispose (GObject *object)
675 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
676 EmpathyTpChatPriv *priv = GET_PRIV (self);
678 if (priv->dispose_has_run)
681 priv->dispose_has_run = TRUE;
683 if (priv->channel != NULL) {
684 g_signal_handlers_disconnect_by_func (priv->channel,
685 tp_chat_invalidated_cb, self);
686 g_object_unref (priv->channel);
688 priv->channel = NULL;
690 if (priv->remote_contact != NULL)
691 g_object_unref (priv->remote_contact);
692 priv->remote_contact = NULL;
694 if (priv->factory != NULL)
695 g_object_unref (priv->factory);
696 priv->factory = NULL;
698 if (priv->user != NULL)
699 g_object_unref (priv->user);
702 if (priv->contact_monitor)
703 g_object_unref (priv->contact_monitor);
704 priv->contact_monitor = NULL;
706 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
707 g_queue_clear (priv->messages_queue);
709 g_queue_foreach (priv->pending_messages_queue,
710 (GFunc) g_object_unref, NULL);
711 g_queue_clear (priv->pending_messages_queue);
713 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
714 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
718 tp_chat_finalize (GObject *object)
720 EmpathyTpChatPriv *priv = GET_PRIV (object);
723 DEBUG ("Finalize: %p", object);
725 if (priv->properties) {
726 for (i = 0; i < priv->properties->len; i++) {
727 TpChatProperty *property;
729 property = g_ptr_array_index (priv->properties, i);
730 g_free (property->name);
731 if (property->value) {
732 tp_g_value_slice_free (property->value);
734 g_slice_free (TpChatProperty, property);
736 g_ptr_array_free (priv->properties, TRUE);
739 g_queue_free (priv->messages_queue);
740 g_queue_free (priv->pending_messages_queue);
742 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
746 tp_chat_check_if_ready (EmpathyTpChat *chat)
748 EmpathyTpChatPriv *priv = GET_PRIV (chat);
750 if (priv->ready || priv->user == NULL ||
751 (priv->members == NULL && priv->remote_contact == NULL)) {
757 tp_cli_channel_type_text_connect_to_received (priv->channel,
760 G_OBJECT (chat), NULL);
761 priv->listing_pending_messages = TRUE;
762 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
764 tp_chat_list_pending_messages_cb,
768 tp_cli_channel_type_text_connect_to_sent (priv->channel,
771 G_OBJECT (chat), NULL);
772 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
773 tp_chat_send_error_cb,
775 G_OBJECT (chat), NULL);
776 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
777 tp_chat_state_changed_cb,
779 G_OBJECT (chat), NULL);
780 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
781 tp_chat_state_changed_cb,
783 G_OBJECT (chat), NULL);
785 g_object_notify (G_OBJECT (chat), "ready");
789 tp_chat_update_remote_contact (EmpathyTpChat *chat)
791 EmpathyTpChatPriv *priv = GET_PRIV (chat);
792 EmpathyContact *contact = NULL;
793 TpHandle self_handle;
794 TpHandleType handle_type;
797 /* If this is a named chatroom, never pretend it is a private chat */
798 tp_channel_get_handle (priv->channel, &handle_type);
799 if (handle_type == TP_HANDLE_TYPE_ROOM) {
803 /* This is an MSN-like chat where anyone can join the chat at anytime.
804 * If there is only one non-self contact member, we are in a private
805 * chat and we set the "remote-contact" property to that contact. If
806 * there are more, set the "remote-contact" property to NULL and the
807 * UI will display a contact list. */
808 self_handle = tp_channel_group_get_self_handle (priv->channel);
809 for (l = priv->members; l; l = l->next) {
810 /* Skip self contact if member */
811 if (empathy_contact_get_handle (l->data) == self_handle) {
815 /* We have more than one remote contact, break */
816 if (contact != NULL) {
821 /* If we didn't find yet a remote contact, keep this one */
825 if (priv->remote_contact == contact) {
829 DEBUG ("Changing remote contact from %p to %p",
830 priv->remote_contact, contact);
832 if (priv->remote_contact) {
833 g_object_unref (priv->remote_contact);
836 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
837 g_object_notify (G_OBJECT (chat), "remote-contact");
841 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
843 EmpathyContact * const * contacts,
845 const TpHandle *failed,
850 EmpathyTpChatPriv *priv = GET_PRIV (chat);
852 const TpIntSet *members;
854 EmpathyContact *contact;
857 DEBUG ("Error: %s", error->message);
861 members = tp_channel_group_get_members (priv->channel);
862 for (i = 0; i < n_contacts; i++) {
863 contact = contacts[i];
864 handle = empathy_contact_get_handle (contact);
866 /* Make sure the contact is still member */
867 if (tp_intset_is_member (members, handle)) {
868 priv->members = g_list_prepend (priv->members,
869 g_object_ref (contact));
870 g_signal_emit_by_name (chat, "members-changed",
871 contact, NULL, 0, NULL, TRUE);
875 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
876 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
879 static EmpathyContact *
880 chat_lookup_contact (EmpathyTpChat *chat,
884 EmpathyTpChatPriv *priv = GET_PRIV (chat);
887 for (l = priv->members; l; l = l->next) {
888 EmpathyContact *c = l->data;
890 if (empathy_contact_get_handle (c) != handle) {
895 /* Caller takes the reference. */
896 priv->members = g_list_delete_link (priv->members, l);
908 tp_chat_group_members_changed_cb (TpChannel *self,
912 GArray *local_pending,
913 GArray *remote_pending,
918 EmpathyTpChatPriv *priv = GET_PRIV (chat);
919 EmpathyContact *contact;
920 EmpathyContact *actor_contact = NULL;
924 actor_contact = chat_lookup_contact (chat, actor, FALSE);
925 if (actor_contact == NULL) {
926 /* FIXME: handle this a tad more gracefully: perhaps
927 * the actor was a server op. We could use the
928 * contact-ids detail of MembersChangedDetailed.
930 DEBUG ("actor %u not a channel member", actor);
934 /* Remove contacts that are not members anymore */
935 for (i = 0; i < removed->len; i++) {
936 contact = chat_lookup_contact (chat,
937 g_array_index (removed, TpHandle, i), TRUE);
939 if (contact != NULL) {
940 g_signal_emit_by_name (chat, "members-changed", contact,
941 actor_contact, reason, message,
943 g_object_unref (contact);
947 /* Request added contacts */
948 if (added->len > 0) {
949 empathy_tp_contact_factory_get_from_handles (priv->factory,
950 added->len, (TpHandle *) added->data,
951 tp_chat_got_added_contacts_cb, NULL, NULL,
955 tp_chat_update_remote_contact (chat);
957 if (actor_contact != NULL) {
958 g_object_unref (actor_contact);
963 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
964 EmpathyContact *contact,
969 EmpathyTpChatPriv *priv = GET_PRIV (chat);
972 DEBUG ("Error: %s", error->message);
973 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
977 priv->remote_contact = g_object_ref (contact);
978 g_object_notify (chat, "remote-contact");
980 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
984 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
985 EmpathyContact *contact,
990 EmpathyTpChatPriv *priv = GET_PRIV (chat);
993 DEBUG ("Error: %s", error->message);
994 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
998 priv->user = g_object_ref (contact);
999 empathy_contact_set_is_user (priv->user, TRUE);
1000 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1004 tp_chat_constructor (GType type,
1006 GObjectConstructParam *props)
1009 EmpathyTpChatPriv *priv;
1010 TpConnection *connection;
1013 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1015 priv = GET_PRIV (chat);
1017 connection = tp_channel_borrow_connection (priv->channel);
1018 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1019 g_signal_connect (priv->channel, "invalidated",
1020 G_CALLBACK (tp_chat_invalidated_cb),
1023 if (tp_proxy_has_interface_by_id (priv->channel,
1024 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1025 const TpIntSet *members;
1028 /* Get self contact from the group's self handle */
1029 handle = tp_channel_group_get_self_handle (priv->channel);
1030 empathy_tp_contact_factory_get_from_handle (priv->factory,
1031 handle, tp_chat_got_self_contact_cb,
1034 /* Get initial member contacts */
1035 members = tp_channel_group_get_members (priv->channel);
1036 handles = tp_intset_to_array (members);
1037 empathy_tp_contact_factory_get_from_handles (priv->factory,
1038 handles->len, (TpHandle *) handles->data,
1039 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1041 g_signal_connect (priv->channel, "group-members-changed",
1042 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1044 /* Get the self contact from the connection's self handle */
1045 handle = tp_connection_get_self_handle (connection);
1046 empathy_tp_contact_factory_get_from_handle (priv->factory,
1047 handle, tp_chat_got_self_contact_cb,
1050 /* Get the remote contact */
1051 handle = tp_channel_get_handle (priv->channel, NULL);
1052 empathy_tp_contact_factory_get_from_handle (priv->factory,
1053 handle, tp_chat_got_remote_contact_cb,
1057 if (tp_proxy_has_interface_by_id (priv->channel,
1058 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1059 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1060 tp_chat_list_properties_cb,
1063 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1064 tp_chat_properties_changed_cb,
1066 G_OBJECT (chat), NULL);
1067 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1068 tp_chat_property_flags_changed_cb,
1070 G_OBJECT (chat), NULL);
1077 tp_chat_get_property (GObject *object,
1082 EmpathyTpChatPriv *priv = GET_PRIV (object);
1086 g_value_set_object (value, priv->channel);
1088 case PROP_REMOTE_CONTACT:
1089 g_value_set_object (value, priv->remote_contact);
1092 g_value_set_boolean (value, priv->ready);
1095 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1101 tp_chat_set_property (GObject *object,
1103 const GValue *value,
1106 EmpathyTpChatPriv *priv = GET_PRIV (object);
1110 priv->channel = g_value_dup_object (value);
1113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1119 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1121 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1123 object_class->dispose = tp_chat_dispose;
1124 object_class->finalize = tp_chat_finalize;
1125 object_class->constructor = tp_chat_constructor;
1126 object_class->get_property = tp_chat_get_property;
1127 object_class->set_property = tp_chat_set_property;
1129 g_object_class_install_property (object_class,
1131 g_param_spec_object ("channel",
1132 "telepathy channel",
1133 "The text channel for the chat",
1136 G_PARAM_CONSTRUCT_ONLY));
1138 g_object_class_install_property (object_class,
1139 PROP_REMOTE_CONTACT,
1140 g_param_spec_object ("remote-contact",
1141 "The remote contact",
1142 "The remote contact if there is no group iface on the channel",
1143 EMPATHY_TYPE_CONTACT,
1146 g_object_class_install_property (object_class,
1148 g_param_spec_boolean ("ready",
1149 "Is the object ready",
1150 "This object can't be used until this becomes true",
1155 signals[MESSAGE_RECEIVED] =
1156 g_signal_new ("message-received",
1157 G_TYPE_FROM_CLASS (klass),
1161 g_cclosure_marshal_VOID__OBJECT,
1163 1, EMPATHY_TYPE_MESSAGE);
1165 signals[SEND_ERROR] =
1166 g_signal_new ("send-error",
1167 G_TYPE_FROM_CLASS (klass),
1171 _empathy_marshal_VOID__OBJECT_UINT,
1173 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1175 signals[CHAT_STATE_CHANGED] =
1176 g_signal_new ("chat-state-changed",
1177 G_TYPE_FROM_CLASS (klass),
1181 _empathy_marshal_VOID__OBJECT_UINT,
1183 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1185 signals[PROPERTY_CHANGED] =
1186 g_signal_new ("property-changed",
1187 G_TYPE_FROM_CLASS (klass),
1191 _empathy_marshal_VOID__STRING_BOXED,
1193 2, G_TYPE_STRING, G_TYPE_VALUE);
1196 g_signal_new ("destroy",
1197 G_TYPE_FROM_CLASS (klass),
1201 g_cclosure_marshal_VOID__VOID,
1205 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1209 empathy_tp_chat_init (EmpathyTpChat *chat)
1211 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1212 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1215 priv->contact_monitor = NULL;
1216 priv->messages_queue = g_queue_new ();
1217 priv->pending_messages_queue = g_queue_new ();
1221 tp_chat_iface_init (EmpathyContactListIface *iface)
1223 iface->add = tp_chat_add;
1224 iface->remove = tp_chat_remove;
1225 iface->get_members = tp_chat_get_members;
1226 iface->get_monitor = tp_chat_get_monitor;
1230 empathy_tp_chat_new (TpChannel *channel)
1232 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1238 empathy_tp_chat_close (EmpathyTpChat *chat) {
1239 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1241 /* If there are still messages left, it'll come back..
1242 * We loose the ordering of sent messages though */
1243 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1244 "closing channel", NULL, NULL);
1248 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1250 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1252 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1254 return tp_channel_get_identifier (priv->channel);
1258 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1260 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1262 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1263 g_return_val_if_fail (priv->ready, NULL);
1265 return priv->remote_contact;
1269 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1271 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1273 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1275 return priv->channel;
1279 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1281 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1283 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1285 return tp_channel_borrow_connection (priv->channel);
1289 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1291 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1293 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1299 empathy_tp_chat_send (EmpathyTpChat *chat,
1300 EmpathyMessage *message)
1302 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1303 const gchar *message_body;
1304 TpChannelTextMessageType message_type;
1306 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1307 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1308 g_return_if_fail (priv->ready);
1310 message_body = empathy_message_get_body (message);
1311 message_type = empathy_message_get_tptype (message);
1313 DEBUG ("Sending message: %s", message_body);
1314 tp_cli_channel_type_text_call_send (priv->channel, -1,
1318 g_object_ref (message),
1319 (GDestroyNotify) g_object_unref,
1324 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1325 TpChannelChatState state)
1327 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1329 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1330 g_return_if_fail (priv->ready);
1332 if (tp_proxy_has_interface_by_id (priv->channel,
1333 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1334 DEBUG ("Set state: %d", state);
1335 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1338 "setting chat state",
1346 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1348 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1350 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1351 g_return_val_if_fail (priv->ready, NULL);
1353 return priv->pending_messages_queue->head;
1357 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1358 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1360 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1361 priv->channel, -1, ids, tp_chat_async_cb,
1362 "acknowledging received message", NULL, G_OBJECT (chat));
1366 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1367 EmpathyMessage *message) {
1368 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1369 GArray *message_ids;
1373 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1374 g_return_if_fail (priv->ready);
1376 if (empathy_message_get_sender (message) == priv->user)
1379 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1381 id = empathy_message_get_id (message);
1382 g_array_append_val (message_ids, id);
1383 acknowledge_messages (chat, message_ids);
1384 g_array_free (message_ids, TRUE);
1387 m = g_queue_find (priv->pending_messages_queue, message);
1388 g_assert (m != NULL);
1389 g_queue_delete_link (priv->pending_messages_queue, m);
1390 g_object_unref (message);
1394 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1395 const GList *messages) {
1396 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1397 /* Copy messages as the messges list (probably is) our own */
1398 GList *msgs = g_list_copy ((GList *) messages);
1401 GArray *message_ids;
1403 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1404 g_return_if_fail (priv->ready);
1406 length = g_list_length ((GList *) messages);
1411 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1413 for (l = msgs; l != NULL; l = g_list_next (l)) {
1416 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1418 m = g_queue_find (priv->pending_messages_queue, message);
1419 g_assert (m != NULL);
1420 g_queue_delete_link (priv->pending_messages_queue, m);
1422 if (empathy_message_get_sender (message) != priv->user) {
1423 guint id = empathy_message_get_id (message);
1424 g_array_append_val (message_ids, id);
1426 g_object_unref (message);
1429 if (message_ids->len > 0)
1430 acknowledge_messages (chat, message_ids);
1432 g_array_free (message_ids, TRUE);