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;
61 static void tp_chat_iface_init (EmpathyContactListIface *iface);
79 static guint signals[LAST_SIGNAL];
81 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
82 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
85 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
88 tp_chat_invalidated_cb (TpProxy *proxy,
94 DEBUG ("Channel invalidated: %s", message);
95 g_signal_emit (chat, signals[DESTROY], 0);
99 tp_chat_async_cb (TpChannel *proxy,
102 GObject *weak_object)
105 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
110 tp_chat_add (EmpathyContactList *list,
111 EmpathyContact *contact,
112 const gchar *message)
114 EmpathyTpChatPriv *priv = GET_PRIV (list);
116 GArray handles = {(gchar *) &handle, 1};
118 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
119 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
121 handle = empathy_contact_get_handle (contact);
122 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
129 tp_chat_remove (EmpathyContactList *list,
130 EmpathyContact *contact,
131 const gchar *message)
133 EmpathyTpChatPriv *priv = GET_PRIV (list);
135 GArray handles = {(gchar *) &handle, 1};
137 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
138 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
140 handle = empathy_contact_get_handle (contact);
141 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
148 tp_chat_get_members (EmpathyContactList *list)
150 EmpathyTpChatPriv *priv = GET_PRIV (list);
151 GList *members = NULL;
153 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
156 members = g_list_copy (priv->members);
157 g_list_foreach (members, (GFunc) g_object_ref, NULL);
159 members = g_list_prepend (members, g_object_ref (priv->user));
160 if (priv->remote_contact != NULL)
161 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
167 static EmpathyContactMonitor *
168 tp_chat_get_monitor (EmpathyContactList *list)
170 EmpathyTpChatPriv *priv;
172 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
174 priv = GET_PRIV (list);
176 if (priv->contact_monitor == NULL) {
177 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
180 return priv->contact_monitor;
184 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
186 EmpathyTpChatPriv *priv = GET_PRIV (chat);
187 EmpathyMessage *message;
189 /* Check if we can now emit some queued messages */
190 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
191 if (empathy_message_get_sender (message) == NULL) {
195 DEBUG ("Queued message ready");
196 g_queue_pop_head (priv->messages_queue);
197 g_queue_push_tail (priv->pending_messages_queue, message);
198 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
203 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
204 EmpathyContact *contact,
209 EmpathyTpChatPriv *priv = GET_PRIV (chat);
212 DEBUG ("Error: %s", error->message);
213 /* Do not block the message queue, just drop this message */
214 g_queue_remove (priv->messages_queue, message);
216 empathy_message_set_sender (message, contact);
219 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
223 tp_chat_build_message (EmpathyTpChat *chat,
229 const gchar *message_body)
231 EmpathyTpChatPriv *priv;
232 EmpathyMessage *message;
234 priv = GET_PRIV (chat);
236 message = empathy_message_new (message_body);
237 empathy_message_set_tptype (message, type);
238 empathy_message_set_receiver (message, priv->user);
239 empathy_message_set_timestamp (message, timestamp);
240 empathy_message_set_id (message, id);
241 empathy_message_set_incoming (message, incoming);
243 g_queue_push_tail (priv->messages_queue, message);
245 if (from_handle == 0) {
246 empathy_message_set_sender (message, priv->user);
247 tp_chat_emit_queued_messages (chat);
249 empathy_tp_contact_factory_get_from_handle (priv->factory,
251 tp_chat_got_sender_cb,
252 message, NULL, G_OBJECT (chat));
257 tp_chat_received_cb (TpChannel *channel,
263 const gchar *message_body,
267 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
268 EmpathyTpChatPriv *priv = GET_PRIV (chat);
270 if (priv->channel == NULL)
273 if (priv->listing_pending_messages) {
277 DEBUG ("Message received: %s", message_body);
279 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
280 !tp_strdiff (message_body, "")) {
283 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
285 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
286 g_array_append_val (ids, message_id);
287 acknowledge_messages (chat, ids);
288 g_array_free (ids, TRUE);
293 tp_chat_build_message (chat,
303 tp_chat_sent_cb (TpChannel *channel,
306 const gchar *message_body,
310 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
311 EmpathyTpChatPriv *priv = GET_PRIV (chat);
313 if (priv->channel == NULL)
316 DEBUG ("Message sent: %s", message_body);
318 tp_chat_build_message (chat,
328 tp_chat_send_error_cb (TpChannel *channel,
332 const gchar *message_body,
336 EmpathyTpChatPriv *priv = GET_PRIV (chat);
338 if (priv->channel == NULL)
341 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
343 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
347 tp_chat_send_cb (TpChannel *proxy,
352 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
355 DEBUG ("Error: %s", error->message);
356 g_signal_emit (chat, signals[SEND_ERROR], 0,
357 empathy_message_get_body (message),
358 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
364 TpChannelChatState state;
368 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
369 EmpathyContact *contact,
374 TpChannelChatState state;
377 DEBUG ("Error: %s", error->message);
381 state = GPOINTER_TO_UINT (user_data);
382 DEBUG ("Chat state changed for %s (%d): %d",
383 empathy_contact_get_name (contact),
384 empathy_contact_get_handle (contact), state);
386 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
390 tp_chat_state_changed_cb (TpChannel *channel,
392 TpChannelChatState state,
396 EmpathyTpChatPriv *priv = GET_PRIV (chat);
398 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
399 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
404 tp_chat_list_pending_messages_cb (TpChannel *channel,
405 const GPtrArray *messages_list,
410 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
411 EmpathyTpChatPriv *priv = GET_PRIV (chat);
413 GArray *empty_non_text_content_ids = NULL;
415 priv->listing_pending_messages = FALSE;
417 if (priv->channel == NULL)
421 DEBUG ("Error listing pending messages: %s", error->message);
425 for (i = 0; i < messages_list->len; i++) {
426 GValueArray *message_struct;
427 const gchar *message_body;
434 message_struct = g_ptr_array_index (messages_list, i);
436 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
437 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
438 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
439 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
440 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
441 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
443 DEBUG ("Message pending: %s", message_body);
445 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
446 !tp_strdiff (message_body, "")) {
447 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
449 if (empty_non_text_content_ids == NULL) {
450 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
453 g_array_append_val (empty_non_text_content_ids, message_id);
457 tp_chat_build_message (chat,
466 if (empty_non_text_content_ids != NULL) {
467 acknowledge_messages (chat, empty_non_text_content_ids);
468 g_array_free (empty_non_text_content_ids, TRUE);
473 tp_chat_property_flags_changed_cb (TpProxy *proxy,
474 const GPtrArray *properties,
478 EmpathyTpChatPriv *priv = GET_PRIV (chat);
481 if (priv->channel == NULL)
484 if (!priv->had_properties_list || !properties) {
488 for (i = 0; i < properties->len; i++) {
489 GValueArray *prop_struct;
490 EmpathyTpChatProperty *property;
494 prop_struct = g_ptr_array_index (properties, i);
495 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
496 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
498 for (j = 0; j < priv->properties->len; j++) {
499 property = g_ptr_array_index (priv->properties, j);
500 if (property->id == id) {
501 property->flags = flags;
502 DEBUG ("property %s flags changed: %d",
503 property->name, property->flags);
511 tp_chat_properties_changed_cb (TpProxy *proxy,
512 const GPtrArray *properties,
516 EmpathyTpChatPriv *priv = GET_PRIV (chat);
519 if (priv->channel == NULL)
522 if (!priv->had_properties_list || !properties) {
526 for (i = 0; i < properties->len; i++) {
527 GValueArray *prop_struct;
528 EmpathyTpChatProperty *property;
532 prop_struct = g_ptr_array_index (properties, i);
533 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
534 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
536 for (j = 0; j < priv->properties->len; j++) {
537 property = g_ptr_array_index (priv->properties, j);
538 if (property->id == id) {
539 if (property->value) {
540 g_value_copy (src_value, property->value);
542 property->value = tp_g_value_slice_dup (src_value);
545 DEBUG ("property %s changed", property->name);
546 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
547 property->name, property->value);
555 tp_chat_get_properties_cb (TpProxy *proxy,
556 const GPtrArray *properties,
562 DEBUG ("Error getting properties: %s", error->message);
566 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
570 tp_chat_list_properties_cb (TpProxy *proxy,
571 const GPtrArray *properties,
576 EmpathyTpChatPriv *priv = GET_PRIV (chat);
580 if (priv->channel == NULL)
583 priv->had_properties_list = TRUE;
586 DEBUG ("Error listing properties: %s", error->message);
590 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
591 priv->properties = g_ptr_array_sized_new (properties->len);
592 for (i = 0; i < properties->len; i++) {
593 GValueArray *prop_struct;
594 EmpathyTpChatProperty *property;
596 prop_struct = g_ptr_array_index (properties, i);
597 property = g_slice_new0 (EmpathyTpChatProperty);
598 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
599 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
600 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
602 DEBUG ("Adding property name=%s id=%d flags=%d",
603 property->name, property->id, property->flags);
604 g_ptr_array_add (priv->properties, property);
605 if (property->flags & TP_PROPERTY_FLAG_READ) {
606 g_array_append_val (ids, property->id);
610 tp_cli_properties_interface_call_get_properties (proxy, -1,
612 tp_chat_get_properties_cb,
616 g_array_free (ids, TRUE);
620 empathy_tp_chat_set_property (EmpathyTpChat *chat,
624 EmpathyTpChatPriv *priv = GET_PRIV (chat);
625 EmpathyTpChatProperty *property;
628 if (!priv->had_properties_list) {
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);
672 EmpathyTpChatProperty *
673 empathy_tp_chat_get_property (EmpathyTpChat *chat,
676 EmpathyTpChatPriv *priv = GET_PRIV (chat);
677 EmpathyTpChatProperty *property;
680 if (!priv->had_properties_list) {
684 for (i = 0; i < priv->properties->len; i++) {
685 property = g_ptr_array_index (priv->properties, i);
686 if (!tp_strdiff (property->name, name)) {
695 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
697 EmpathyTpChatPriv *priv = GET_PRIV (chat);
699 return priv->properties;
703 tp_chat_dispose (GObject *object)
705 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
706 EmpathyTpChatPriv *priv = GET_PRIV (self);
708 if (priv->dispose_has_run)
711 priv->dispose_has_run = TRUE;
713 if (priv->channel != NULL) {
714 g_signal_handlers_disconnect_by_func (priv->channel,
715 tp_chat_invalidated_cb, self);
716 g_object_unref (priv->channel);
718 priv->channel = NULL;
720 if (priv->remote_contact != NULL)
721 g_object_unref (priv->remote_contact);
722 priv->remote_contact = NULL;
724 if (priv->factory != NULL)
725 g_object_unref (priv->factory);
726 priv->factory = NULL;
728 if (priv->user != NULL)
729 g_object_unref (priv->user);
732 if (priv->contact_monitor)
733 g_object_unref (priv->contact_monitor);
734 priv->contact_monitor = NULL;
736 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
737 g_queue_clear (priv->messages_queue);
739 g_queue_foreach (priv->pending_messages_queue,
740 (GFunc) g_object_unref, NULL);
741 g_queue_clear (priv->pending_messages_queue);
743 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
744 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
748 tp_chat_finalize (GObject *object)
750 EmpathyTpChatPriv *priv = GET_PRIV (object);
753 DEBUG ("Finalize: %p", object);
755 if (priv->properties) {
756 for (i = 0; i < priv->properties->len; i++) {
757 EmpathyTpChatProperty *property;
759 property = g_ptr_array_index (priv->properties, i);
760 g_free (property->name);
761 if (property->value) {
762 tp_g_value_slice_free (property->value);
764 g_slice_free (EmpathyTpChatProperty, property);
766 g_ptr_array_free (priv->properties, TRUE);
769 g_queue_free (priv->messages_queue);
770 g_queue_free (priv->pending_messages_queue);
772 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
776 tp_chat_check_if_ready (EmpathyTpChat *chat)
778 EmpathyTpChatPriv *priv = GET_PRIV (chat);
780 if (priv->ready || priv->user == NULL ||
781 (priv->members == NULL && priv->remote_contact == NULL)) {
787 tp_cli_channel_type_text_connect_to_received (priv->channel,
790 G_OBJECT (chat), NULL);
791 priv->listing_pending_messages = TRUE;
792 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
794 tp_chat_list_pending_messages_cb,
798 tp_cli_channel_type_text_connect_to_sent (priv->channel,
801 G_OBJECT (chat), NULL);
802 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
803 tp_chat_send_error_cb,
805 G_OBJECT (chat), NULL);
806 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
807 tp_chat_state_changed_cb,
809 G_OBJECT (chat), NULL);
810 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
811 tp_chat_state_changed_cb,
813 G_OBJECT (chat), NULL);
815 g_object_notify (G_OBJECT (chat), "ready");
819 tp_chat_update_remote_contact (EmpathyTpChat *chat)
821 EmpathyTpChatPriv *priv = GET_PRIV (chat);
822 EmpathyContact *contact = NULL;
823 TpHandle self_handle;
824 TpHandleType handle_type;
827 /* If this is a named chatroom, never pretend it is a private chat */
828 tp_channel_get_handle (priv->channel, &handle_type);
829 if (handle_type == TP_HANDLE_TYPE_ROOM) {
833 /* This is an MSN-like chat where anyone can join the chat at anytime.
834 * If there is only one non-self contact member, we are in a private
835 * chat and we set the "remote-contact" property to that contact. If
836 * there are more, set the "remote-contact" property to NULL and the
837 * UI will display a contact list. */
838 self_handle = tp_channel_group_get_self_handle (priv->channel);
839 for (l = priv->members; l; l = l->next) {
840 /* Skip self contact if member */
841 if (empathy_contact_get_handle (l->data) == self_handle) {
845 /* We have more than one remote contact, break */
846 if (contact != NULL) {
851 /* If we didn't find yet a remote contact, keep this one */
855 if (priv->remote_contact == contact) {
859 DEBUG ("Changing remote contact from %p to %p",
860 priv->remote_contact, contact);
862 if (priv->remote_contact) {
863 g_object_unref (priv->remote_contact);
866 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
867 g_object_notify (G_OBJECT (chat), "remote-contact");
871 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
873 EmpathyContact * const * contacts,
875 const TpHandle *failed,
880 EmpathyTpChatPriv *priv = GET_PRIV (chat);
882 const TpIntSet *members;
884 EmpathyContact *contact;
887 DEBUG ("Error: %s", error->message);
891 members = tp_channel_group_get_members (priv->channel);
892 for (i = 0; i < n_contacts; i++) {
893 contact = contacts[i];
894 handle = empathy_contact_get_handle (contact);
896 /* Make sure the contact is still member */
897 if (tp_intset_is_member (members, handle)) {
898 priv->members = g_list_prepend (priv->members,
899 g_object_ref (contact));
900 g_signal_emit_by_name (chat, "members-changed",
901 contact, NULL, 0, NULL, TRUE);
905 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
906 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
909 static EmpathyContact *
910 chat_lookup_contact (EmpathyTpChat *chat,
914 EmpathyTpChatPriv *priv = GET_PRIV (chat);
917 for (l = priv->members; l; l = l->next) {
918 EmpathyContact *c = l->data;
920 if (empathy_contact_get_handle (c) != handle) {
925 /* Caller takes the reference. */
926 priv->members = g_list_delete_link (priv->members, l);
938 tp_chat_group_members_changed_cb (TpChannel *self,
942 GArray *local_pending,
943 GArray *remote_pending,
948 EmpathyTpChatPriv *priv = GET_PRIV (chat);
949 EmpathyContact *contact;
950 EmpathyContact *actor_contact = NULL;
954 actor_contact = chat_lookup_contact (chat, actor, FALSE);
955 if (actor_contact == NULL) {
956 /* FIXME: handle this a tad more gracefully: perhaps
957 * the actor was a server op. We could use the
958 * contact-ids detail of MembersChangedDetailed.
960 DEBUG ("actor %u not a channel member", actor);
964 /* Remove contacts that are not members anymore */
965 for (i = 0; i < removed->len; i++) {
966 contact = chat_lookup_contact (chat,
967 g_array_index (removed, TpHandle, i), TRUE);
969 if (contact != NULL) {
970 g_signal_emit_by_name (chat, "members-changed", contact,
971 actor_contact, reason, message,
973 g_object_unref (contact);
977 /* Request added contacts */
978 if (added->len > 0) {
979 empathy_tp_contact_factory_get_from_handles (priv->factory,
980 added->len, (TpHandle *) added->data,
981 tp_chat_got_added_contacts_cb, NULL, NULL,
985 tp_chat_update_remote_contact (chat);
987 if (actor_contact != NULL) {
988 g_object_unref (actor_contact);
993 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
994 EmpathyContact *contact,
999 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1002 DEBUG ("Error: %s", error->message);
1003 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1007 priv->remote_contact = g_object_ref (contact);
1008 g_object_notify (chat, "remote-contact");
1010 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1014 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
1015 EmpathyContact *contact,
1016 const GError *error,
1020 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1023 DEBUG ("Error: %s", error->message);
1024 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1028 priv->user = g_object_ref (contact);
1029 empathy_contact_set_is_user (priv->user, TRUE);
1030 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1034 tp_chat_constructor (GType type,
1036 GObjectConstructParam *props)
1039 EmpathyTpChatPriv *priv;
1040 TpConnection *connection;
1043 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1045 priv = GET_PRIV (chat);
1047 connection = tp_channel_borrow_connection (priv->channel);
1048 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1049 g_signal_connect (priv->channel, "invalidated",
1050 G_CALLBACK (tp_chat_invalidated_cb),
1053 if (tp_proxy_has_interface_by_id (priv->channel,
1054 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1055 const TpIntSet *members;
1058 /* Get self contact from the group's self handle */
1059 handle = tp_channel_group_get_self_handle (priv->channel);
1060 empathy_tp_contact_factory_get_from_handle (priv->factory,
1061 handle, tp_chat_got_self_contact_cb,
1064 /* Get initial member contacts */
1065 members = tp_channel_group_get_members (priv->channel);
1066 handles = tp_intset_to_array (members);
1067 empathy_tp_contact_factory_get_from_handles (priv->factory,
1068 handles->len, (TpHandle *) handles->data,
1069 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1071 g_signal_connect (priv->channel, "group-members-changed",
1072 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1074 /* Get the self contact from the connection's self handle */
1075 handle = tp_connection_get_self_handle (connection);
1076 empathy_tp_contact_factory_get_from_handle (priv->factory,
1077 handle, tp_chat_got_self_contact_cb,
1080 /* Get the remote contact */
1081 handle = tp_channel_get_handle (priv->channel, NULL);
1082 empathy_tp_contact_factory_get_from_handle (priv->factory,
1083 handle, tp_chat_got_remote_contact_cb,
1087 if (tp_proxy_has_interface_by_id (priv->channel,
1088 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1089 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1090 tp_chat_list_properties_cb,
1093 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1094 tp_chat_properties_changed_cb,
1096 G_OBJECT (chat), NULL);
1097 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1098 tp_chat_property_flags_changed_cb,
1100 G_OBJECT (chat), NULL);
1107 tp_chat_get_property (GObject *object,
1112 EmpathyTpChatPriv *priv = GET_PRIV (object);
1116 g_value_set_object (value, priv->channel);
1118 case PROP_REMOTE_CONTACT:
1119 g_value_set_object (value, priv->remote_contact);
1122 g_value_set_boolean (value, priv->ready);
1125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1131 tp_chat_set_property (GObject *object,
1133 const GValue *value,
1136 EmpathyTpChatPriv *priv = GET_PRIV (object);
1140 priv->channel = g_value_dup_object (value);
1143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1149 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1151 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1153 object_class->dispose = tp_chat_dispose;
1154 object_class->finalize = tp_chat_finalize;
1155 object_class->constructor = tp_chat_constructor;
1156 object_class->get_property = tp_chat_get_property;
1157 object_class->set_property = tp_chat_set_property;
1159 g_object_class_install_property (object_class,
1161 g_param_spec_object ("channel",
1162 "telepathy channel",
1163 "The text channel for the chat",
1166 G_PARAM_CONSTRUCT_ONLY));
1168 g_object_class_install_property (object_class,
1169 PROP_REMOTE_CONTACT,
1170 g_param_spec_object ("remote-contact",
1171 "The remote contact",
1172 "The remote contact if there is no group iface on the channel",
1173 EMPATHY_TYPE_CONTACT,
1176 g_object_class_install_property (object_class,
1178 g_param_spec_boolean ("ready",
1179 "Is the object ready",
1180 "This object can't be used until this becomes true",
1185 signals[MESSAGE_RECEIVED] =
1186 g_signal_new ("message-received",
1187 G_TYPE_FROM_CLASS (klass),
1191 g_cclosure_marshal_VOID__OBJECT,
1193 1, EMPATHY_TYPE_MESSAGE);
1195 signals[SEND_ERROR] =
1196 g_signal_new ("send-error",
1197 G_TYPE_FROM_CLASS (klass),
1201 _empathy_marshal_VOID__STRING_UINT,
1203 2, G_TYPE_STRING, G_TYPE_UINT);
1205 signals[CHAT_STATE_CHANGED] =
1206 g_signal_new ("chat-state-changed",
1207 G_TYPE_FROM_CLASS (klass),
1211 _empathy_marshal_VOID__OBJECT_UINT,
1213 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1215 signals[PROPERTY_CHANGED] =
1216 g_signal_new ("property-changed",
1217 G_TYPE_FROM_CLASS (klass),
1221 _empathy_marshal_VOID__STRING_BOXED,
1223 2, G_TYPE_STRING, G_TYPE_VALUE);
1226 g_signal_new ("destroy",
1227 G_TYPE_FROM_CLASS (klass),
1231 g_cclosure_marshal_VOID__VOID,
1235 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1239 empathy_tp_chat_init (EmpathyTpChat *chat)
1241 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1242 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1245 priv->contact_monitor = NULL;
1246 priv->messages_queue = g_queue_new ();
1247 priv->pending_messages_queue = g_queue_new ();
1251 tp_chat_iface_init (EmpathyContactListIface *iface)
1253 iface->add = tp_chat_add;
1254 iface->remove = tp_chat_remove;
1255 iface->get_members = tp_chat_get_members;
1256 iface->get_monitor = tp_chat_get_monitor;
1260 empathy_tp_chat_new (TpChannel *channel)
1262 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1268 empathy_tp_chat_close (EmpathyTpChat *chat) {
1269 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1271 /* If there are still messages left, it'll come back..
1272 * We loose the ordering of sent messages though */
1273 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1274 "closing channel", NULL, NULL);
1278 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1280 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1284 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1286 id = tp_channel_get_identifier (priv->channel);
1287 if (!EMP_STR_EMPTY (id))
1289 else if (priv->remote_contact)
1290 return empathy_contact_get_id (priv->remote_contact);
1297 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1299 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1301 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1302 g_return_val_if_fail (priv->ready, NULL);
1304 return priv->remote_contact;
1308 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1310 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1312 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1314 return priv->channel;
1318 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1320 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1322 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1324 return tp_channel_borrow_connection (priv->channel);
1328 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1330 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1332 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1338 empathy_tp_chat_send (EmpathyTpChat *chat,
1339 EmpathyMessage *message)
1341 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1342 const gchar *message_body;
1343 TpChannelTextMessageType message_type;
1345 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1346 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1347 g_return_if_fail (priv->ready);
1349 message_body = empathy_message_get_body (message);
1350 message_type = empathy_message_get_tptype (message);
1352 DEBUG ("Sending message: %s", message_body);
1353 tp_cli_channel_type_text_call_send (priv->channel, -1,
1357 g_object_ref (message),
1358 (GDestroyNotify) g_object_unref,
1363 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1364 TpChannelChatState state)
1366 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1368 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1369 g_return_if_fail (priv->ready);
1371 if (tp_proxy_has_interface_by_id (priv->channel,
1372 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1373 DEBUG ("Set state: %d", state);
1374 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1377 "setting chat state",
1385 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1387 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1389 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1390 g_return_val_if_fail (priv->ready, NULL);
1392 return priv->pending_messages_queue->head;
1396 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1397 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1399 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1400 priv->channel, -1, ids, tp_chat_async_cb,
1401 "acknowledging received message", NULL, G_OBJECT (chat));
1405 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1406 EmpathyMessage *message) {
1407 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1408 GArray *message_ids;
1412 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1413 g_return_if_fail (priv->ready);
1415 if (!empathy_message_is_incoming (message))
1418 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1420 id = empathy_message_get_id (message);
1421 g_array_append_val (message_ids, id);
1422 acknowledge_messages (chat, message_ids);
1423 g_array_free (message_ids, TRUE);
1426 m = g_queue_find (priv->pending_messages_queue, message);
1427 g_assert (m != NULL);
1428 g_queue_delete_link (priv->pending_messages_queue, m);
1429 g_object_unref (message);
1433 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1434 const GList *messages) {
1435 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1436 /* Copy messages as the messges list (probably is) our own */
1437 GList *msgs = g_list_copy ((GList *) messages);
1440 GArray *message_ids;
1442 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1443 g_return_if_fail (priv->ready);
1445 length = g_list_length ((GList *) messages);
1450 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1452 for (l = msgs; l != NULL; l = g_list_next (l)) {
1455 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1457 m = g_queue_find (priv->pending_messages_queue, message);
1458 g_assert (m != NULL);
1459 g_queue_delete_link (priv->pending_messages_queue, m);
1461 if (empathy_message_is_incoming (message)) {
1462 guint id = empathy_message_get_id (message);
1463 g_array_append_val (message_ids, id);
1465 g_object_unref (message);
1468 if (message_ids->len > 0)
1469 acknowledge_messages (chat, message_ids);
1471 g_array_free (message_ids, TRUE);