1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-contact-monitor.h"
33 #include "empathy-contact-list.h"
34 #include "empathy-marshal.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
43 gboolean dispose_has_run;
44 EmpathyTpContactFactory *factory;
45 EmpathyContactMonitor *contact_monitor;
47 EmpathyContact *remote_contact;
50 gboolean listing_pending_messages;
51 /* Queue of messages not signalled yet */
52 GQueue *messages_queue;
53 /* Queue of messages signalled but not acked yet */
54 GQueue *pending_messages_queue;
55 gboolean had_properties_list;
56 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void tp_chat_iface_init (EmpathyContactListIface *iface);
85 static guint signals[LAST_SIGNAL];
87 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
94 tp_chat_invalidated_cb (TpProxy *proxy,
100 EmpathyTpChatPriv *priv = GET_PRIV (chat);
102 DEBUG ("Channel invalidated: %s", message);
103 g_signal_emit (chat, signals[DESTROY], 0);
107 tp_chat_async_cb (TpChannel *proxy,
110 GObject *weak_object)
113 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
118 tp_chat_add (EmpathyContactList *list,
119 EmpathyContact *contact,
120 const gchar *message)
122 EmpathyTpChatPriv *priv = GET_PRIV (list);
124 GArray handles = {(gchar *) &handle, 1};
126 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
127 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
129 handle = empathy_contact_get_handle (contact);
130 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
137 tp_chat_remove (EmpathyContactList *list,
138 EmpathyContact *contact,
139 const gchar *message)
141 EmpathyTpChatPriv *priv = GET_PRIV (list);
143 GArray handles = {(gchar *) &handle, 1};
145 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
146 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
148 handle = empathy_contact_get_handle (contact);
149 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
156 tp_chat_get_members (EmpathyContactList *list)
158 EmpathyTpChatPriv *priv = GET_PRIV (list);
159 GList *members = NULL;
161 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
164 members = g_list_copy (priv->members);
165 g_list_foreach (members, (GFunc) g_object_ref, NULL);
167 members = g_list_prepend (members, g_object_ref (priv->user));
168 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
174 static EmpathyContactMonitor *
175 tp_chat_get_monitor (EmpathyContactList *list)
177 EmpathyTpChatPriv *priv;
179 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
181 priv = GET_PRIV (list);
183 if (priv->contact_monitor == NULL) {
184 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
187 return priv->contact_monitor;
191 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
193 EmpathyTpChatPriv *priv = GET_PRIV (chat);
194 EmpathyMessage *message;
196 /* Check if we can now emit some queued messages */
197 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
198 if (empathy_message_get_sender (message) == NULL) {
202 DEBUG ("Queued message ready");
203 g_queue_pop_head (priv->messages_queue);
204 g_queue_push_tail (priv->pending_messages_queue, message);
205 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
210 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
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,
235 const gchar *message_body)
237 EmpathyTpChatPriv *priv;
238 EmpathyMessage *message;
240 priv = GET_PRIV (chat);
242 message = empathy_message_new (message_body);
243 empathy_message_set_tptype (message, type);
244 empathy_message_set_receiver (message, priv->user);
245 empathy_message_set_timestamp (message, timestamp);
246 empathy_message_set_id (message, id);
247 g_queue_push_tail (priv->messages_queue, message);
249 if (from_handle == 0) {
250 empathy_message_set_sender (message, priv->user);
251 tp_chat_emit_queued_messages (chat);
253 empathy_tp_contact_factory_get_from_handle (priv->factory,
255 tp_chat_got_sender_cb,
256 message, NULL, G_OBJECT (chat));
261 tp_chat_received_cb (TpChannel *channel,
267 const gchar *message_body,
271 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
272 EmpathyTpChatPriv *priv = GET_PRIV (chat);
274 if (priv->channel == NULL)
277 if (priv->listing_pending_messages) {
281 DEBUG ("Message received: %s", message_body);
283 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
284 !tp_strdiff (message_body, "")) {
287 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
289 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
290 g_array_append_val (ids, message_id);
291 acknowledge_messages (chat, ids);
292 g_array_free (ids, TRUE);
297 tp_chat_build_message (chat,
306 tp_chat_sent_cb (TpChannel *channel,
309 const gchar *message_body,
313 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
314 EmpathyTpChatPriv *priv = GET_PRIV (chat);
316 if (priv->channel == NULL)
319 DEBUG ("Message sent: %s", message_body);
321 tp_chat_build_message (chat,
330 tp_chat_send_error_cb (TpChannel *channel,
334 const gchar *message_body,
338 EmpathyTpChatPriv *priv = GET_PRIV (chat);
340 if (priv->channel == NULL)
343 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
345 tp_chat_build_message (EMPATHY_TP_CHAT (chat),
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, message,
364 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
370 TpChannelChatState state;
374 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
375 EmpathyContact *contact,
380 TpChannelChatState state;
383 DEBUG ("Error: %s", error->message);
387 state = GPOINTER_TO_UINT (user_data);
388 DEBUG ("Chat state changed for %s (%d): %d",
389 empathy_contact_get_name (contact),
390 empathy_contact_get_handle (contact), state);
392 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
396 tp_chat_state_changed_cb (TpChannel *channel,
398 TpChannelChatState state,
402 EmpathyTpChatPriv *priv = GET_PRIV (chat);
404 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
405 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
410 tp_chat_list_pending_messages_cb (TpChannel *channel,
411 const GPtrArray *messages_list,
416 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
417 EmpathyTpChatPriv *priv = GET_PRIV (chat);
419 GArray *empty_non_text_content_ids = NULL;
421 priv->listing_pending_messages = FALSE;
423 if (priv->channel == NULL)
427 DEBUG ("Error listing pending messages: %s", error->message);
431 for (i = 0; i < messages_list->len; i++) {
432 GValueArray *message_struct;
433 const gchar *message_body;
440 message_struct = g_ptr_array_index (messages_list, i);
442 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
443 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
444 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
445 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
446 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
447 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
449 DEBUG ("Message pending: %s", message_body);
451 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
452 !tp_strdiff (message_body, "")) {
453 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
455 if (empty_non_text_content_ids == NULL) {
456 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
459 g_array_append_val (empty_non_text_content_ids, message_id);
463 tp_chat_build_message (chat,
471 if (empty_non_text_content_ids != NULL) {
472 acknowledge_messages (chat, empty_non_text_content_ids);
473 g_array_free (empty_non_text_content_ids, TRUE);
478 tp_chat_property_flags_changed_cb (TpProxy *proxy,
479 const GPtrArray *properties,
483 EmpathyTpChatPriv *priv = GET_PRIV (chat);
486 if (priv->channel == NULL)
489 if (!priv->had_properties_list || !properties) {
493 for (i = 0; i < properties->len; i++) {
494 GValueArray *prop_struct;
495 TpChatProperty *property;
499 prop_struct = g_ptr_array_index (properties, i);
500 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
501 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
503 for (j = 0; j < priv->properties->len; j++) {
504 property = g_ptr_array_index (priv->properties, j);
505 if (property->id == id) {
506 property->flags = flags;
507 DEBUG ("property %s flags changed: %d",
508 property->name, property->flags);
516 tp_chat_properties_changed_cb (TpProxy *proxy,
517 const GPtrArray *properties,
521 EmpathyTpChatPriv *priv = GET_PRIV (chat);
524 if (priv->channel == NULL)
527 if (!priv->had_properties_list || !properties) {
531 for (i = 0; i < properties->len; i++) {
532 GValueArray *prop_struct;
533 TpChatProperty *property;
537 prop_struct = g_ptr_array_index (properties, i);
538 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
539 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
541 for (j = 0; j < priv->properties->len; j++) {
542 property = g_ptr_array_index (priv->properties, j);
543 if (property->id == id) {
544 if (property->value) {
545 g_value_copy (src_value, property->value);
547 property->value = tp_g_value_slice_dup (src_value);
550 DEBUG ("property %s changed", property->name);
551 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
552 property->name, property->value);
560 tp_chat_get_properties_cb (TpProxy *proxy,
561 const GPtrArray *properties,
567 DEBUG ("Error getting properties: %s", error->message);
571 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
575 tp_chat_list_properties_cb (TpProxy *proxy,
576 const GPtrArray *properties,
581 EmpathyTpChatPriv *priv = GET_PRIV (chat);
585 if (priv->channel == NULL)
588 priv->had_properties_list = TRUE;
591 DEBUG ("Error listing properties: %s", error->message);
595 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
596 priv->properties = g_ptr_array_sized_new (properties->len);
597 for (i = 0; i < properties->len; i++) {
598 GValueArray *prop_struct;
599 TpChatProperty *property;
601 prop_struct = g_ptr_array_index (properties, i);
602 property = g_slice_new0 (TpChatProperty);
603 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
604 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
605 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
607 DEBUG ("Adding property name=%s id=%d flags=%d",
608 property->name, property->id, property->flags);
609 g_ptr_array_add (priv->properties, property);
610 if (property->flags & TP_PROPERTY_FLAG_READ) {
611 g_array_append_val (ids, property->id);
615 tp_cli_properties_interface_call_get_properties (proxy, -1,
617 tp_chat_get_properties_cb,
621 g_array_free (ids, TRUE);
625 empathy_tp_chat_set_property (EmpathyTpChat *chat,
629 EmpathyTpChatPriv *priv = GET_PRIV (chat);
630 TpChatProperty *property;
633 for (i = 0; i < priv->properties->len; i++) {
634 property = g_ptr_array_index (priv->properties, i);
635 if (!tp_strdiff (property->name, name)) {
636 GPtrArray *properties;
639 GValue dest_value = {0, };
641 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
645 g_value_init (&id, G_TYPE_UINT);
646 g_value_init (&dest_value, G_TYPE_VALUE);
647 g_value_set_uint (&id, property->id);
648 g_value_set_boxed (&dest_value, value);
650 prop = g_value_array_new (2);
651 g_value_array_append (prop, &id);
652 g_value_array_append (prop, &dest_value);
654 properties = g_ptr_array_sized_new (1);
655 g_ptr_array_add (properties, prop);
657 DEBUG ("Set property %s", name);
658 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
660 (tp_cli_properties_interface_callback_for_set_properties)
662 "Seting property", NULL,
665 g_ptr_array_free (properties, TRUE);
666 g_value_array_free (prop);
674 tp_chat_dispose (GObject *object)
676 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
677 EmpathyTpChatPriv *priv = GET_PRIV (self);
679 if (priv->dispose_has_run)
682 priv->dispose_has_run = TRUE;
684 if (priv->channel != NULL) {
685 g_signal_handlers_disconnect_by_func (priv->channel,
686 tp_chat_invalidated_cb, self);
687 g_object_unref (priv->channel);
689 priv->channel = NULL;
691 if (priv->remote_contact != NULL)
692 g_object_unref (priv->remote_contact);
693 priv->remote_contact = NULL;
695 if (priv->factory != NULL)
696 g_object_unref (priv->factory);
697 priv->factory = NULL;
699 if (priv->user != NULL);
700 g_object_unref (priv->user);
703 if (priv->contact_monitor)
704 g_object_unref (priv->contact_monitor);
705 priv->contact_monitor = NULL;
707 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
708 g_queue_clear (priv->messages_queue);
710 g_queue_foreach (priv->pending_messages_queue,
711 (GFunc) g_object_unref, NULL);
712 g_queue_clear (priv->pending_messages_queue);
714 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
715 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
719 tp_chat_finalize (GObject *object)
721 EmpathyTpChatPriv *priv = GET_PRIV (object);
724 DEBUG ("Finalize: %p", object);
726 if (priv->properties) {
727 for (i = 0; i < priv->properties->len; i++) {
728 TpChatProperty *property;
730 property = g_ptr_array_index (priv->properties, i);
731 g_free (property->name);
732 if (property->value) {
733 tp_g_value_slice_free (property->value);
735 g_slice_free (TpChatProperty, property);
737 g_ptr_array_free (priv->properties, TRUE);
740 g_queue_free (priv->messages_queue);
741 g_queue_free (priv->pending_messages_queue);
743 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
747 tp_chat_check_if_ready (EmpathyTpChat *chat)
749 EmpathyTpChatPriv *priv = GET_PRIV (chat);
751 if (priv->ready || priv->user == NULL ||
752 (priv->members == NULL && priv->remote_contact == NULL)) {
758 priv->listing_pending_messages = TRUE;
759 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
761 tp_chat_list_pending_messages_cb,
765 tp_cli_channel_type_text_connect_to_received (priv->channel,
768 G_OBJECT (chat), NULL);
769 tp_cli_channel_type_text_connect_to_sent (priv->channel,
772 G_OBJECT (chat), NULL);
773 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
774 tp_chat_send_error_cb,
776 G_OBJECT (chat), NULL);
777 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
778 tp_chat_state_changed_cb,
780 G_OBJECT (chat), NULL);
781 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
782 tp_chat_state_changed_cb,
784 G_OBJECT (chat), NULL);
786 g_object_notify (G_OBJECT (chat), "ready");
790 tp_chat_update_remote_contact (EmpathyTpChat *chat)
792 EmpathyTpChatPriv *priv = GET_PRIV (chat);
793 EmpathyContact *contact = NULL;
794 TpHandle self_handle;
795 TpHandleType handle_type;
798 /* If this is a named chatroom, never pretend it is a private chat */
799 tp_channel_get_handle (priv->channel, &handle_type);
800 if (handle_type == TP_HANDLE_TYPE_ROOM) {
804 /* This is an MSN-like chat where anyone can join the chat at anytime.
805 * If there is only one non-self contact member, we are in a private
806 * chat and we set the "remote-contact" property to that contact. If
807 * there are more, set the "remote-contact" property to NULL and the
808 * UI will display a contact list. */
809 self_handle = tp_channel_group_get_self_handle (priv->channel);
810 for (l = priv->members; l; l = l->next) {
811 /* Skip self contact if member */
812 if (empathy_contact_get_handle (l->data) == self_handle) {
816 /* We have more than one remote contact, break */
817 if (contact != NULL) {
822 /* If we didn't find yet a remote contact, keep this one */
826 if (priv->remote_contact == contact) {
830 DEBUG ("Changing remote contact from %p to %p",
831 priv->remote_contact, contact);
833 if (priv->remote_contact) {
834 g_object_unref (priv->remote_contact);
837 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
838 g_object_notify (G_OBJECT (chat), "remote-contact");
842 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
844 EmpathyContact * const * contacts,
846 const TpHandle *failed,
851 EmpathyTpChatPriv *priv = GET_PRIV (chat);
853 const TpIntSet *members;
855 EmpathyContact *contact;
858 DEBUG ("Error: %s", error->message);
862 members = tp_channel_group_get_members (priv->channel);
863 for (i = 0; i < n_contacts; i++) {
864 contact = contacts[i];
865 handle = empathy_contact_get_handle (contact);
867 /* Make sure the contact is still member */
868 if (tp_intset_is_member (members, handle)) {
869 priv->members = g_list_prepend (priv->members,
870 g_object_ref (contact));
871 g_signal_emit_by_name (chat, "members-changed",
872 contact, NULL, 0, NULL, FALSE);
876 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
877 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
881 tp_chat_group_members_changed_cb (TpChannel *self,
885 GArray *local_pending,
886 GArray *remote_pending,
891 EmpathyTpChatPriv *priv = GET_PRIV (chat);
892 EmpathyContact *contact;
897 /* Remove contacts that are not members anymore */
898 for (i = 0; i < removed->len; i++) {
899 for (l = priv->members; l; l = l->next) {
901 handle = empathy_contact_get_handle (contact);
902 if (handle == g_array_index (removed, TpHandle, i)) {
903 priv->members = g_list_delete_link (priv->members, l);
904 g_signal_emit_by_name (chat, "members-changed",
905 contact, NULL, reason,
907 g_object_unref (contact);
913 /* Request added contacts */
914 if (added->len > 0) {
915 empathy_tp_contact_factory_get_from_handles (priv->factory,
916 added->len, (TpHandle*) added->data,
917 tp_chat_got_added_contacts_cb, NULL, NULL,
921 tp_chat_update_remote_contact (chat);
925 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
926 EmpathyContact *contact,
931 EmpathyTpChatPriv *priv = GET_PRIV (chat);
934 DEBUG ("Error: %s", error->message);
935 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
939 priv->remote_contact = g_object_ref (contact);
940 g_object_notify (chat, "remote-contact");
942 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
946 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
947 EmpathyContact *contact,
952 EmpathyTpChatPriv *priv = GET_PRIV (chat);
955 DEBUG ("Error: %s", error->message);
956 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
960 priv->user = g_object_ref (contact);
961 empathy_contact_set_is_user (priv->user, TRUE);
962 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
966 tp_chat_get_self_handle_cb (TpConnection *connection,
967 TpHandle self_handle,
972 EmpathyTpChatPriv *priv = GET_PRIV (chat);
975 DEBUG ("Error: %s", error->message);
976 tp_cli_channel_call_close (priv->channel, -1,
977 NULL, NULL, NULL, NULL);
981 empathy_tp_contact_factory_get_from_handle (priv->factory,
982 self_handle, tp_chat_got_self_contact_cb,
987 tp_chat_constructor (GType type,
989 GObjectConstructParam *props)
992 EmpathyTpChatPriv *priv;
993 TpConnection *connection;
996 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
998 priv = GET_PRIV (chat);
1000 connection = tp_channel_borrow_connection (priv->channel);
1001 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1002 g_signal_connect (priv->channel, "invalidated",
1003 G_CALLBACK (tp_chat_invalidated_cb),
1006 if (tp_proxy_has_interface_by_id (priv->channel,
1007 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1008 const TpIntSet *members;
1011 /* Get self contact from the group's self handle */
1012 handle = tp_channel_group_get_self_handle (priv->channel);
1013 empathy_tp_contact_factory_get_from_handle (priv->factory,
1014 handle, tp_chat_got_self_contact_cb,
1017 /* Get initial member contacts */
1018 members = tp_channel_group_get_members (priv->channel);
1019 handles = tp_intset_to_array (members);
1020 empathy_tp_contact_factory_get_from_handles (priv->factory,
1021 handles->len, (TpHandle*) handles->data,
1022 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1024 g_signal_connect (priv->channel, "group-members-changed",
1025 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1027 /* Get the self contact from the connection's self handle */
1028 tp_cli_connection_call_get_self_handle (connection, -1,
1029 tp_chat_get_self_handle_cb, NULL, NULL, chat);
1031 /* Get the remote contact */
1032 handle = tp_channel_get_handle (priv->channel, NULL);
1033 empathy_tp_contact_factory_get_from_handle (priv->factory,
1034 handle, tp_chat_got_remote_contact_cb,
1038 if (tp_proxy_has_interface_by_id (priv->channel,
1039 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1040 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1041 tp_chat_list_properties_cb,
1044 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1045 tp_chat_properties_changed_cb,
1047 G_OBJECT (chat), NULL);
1048 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1049 tp_chat_property_flags_changed_cb,
1051 G_OBJECT (chat), NULL);
1058 tp_chat_get_property (GObject *object,
1063 EmpathyTpChatPriv *priv = GET_PRIV (object);
1067 g_value_set_object (value, priv->channel);
1069 case PROP_REMOTE_CONTACT:
1070 g_value_set_object (value, priv->remote_contact);
1073 g_value_set_boolean (value, priv->ready);
1076 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1082 tp_chat_set_property (GObject *object,
1084 const GValue *value,
1087 EmpathyTpChatPriv *priv = GET_PRIV (object);
1091 priv->channel = g_value_dup_object (value);
1094 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1100 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1102 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1104 object_class->dispose = tp_chat_dispose;
1105 object_class->finalize = tp_chat_finalize;
1106 object_class->constructor = tp_chat_constructor;
1107 object_class->get_property = tp_chat_get_property;
1108 object_class->set_property = tp_chat_set_property;
1110 g_object_class_install_property (object_class,
1112 g_param_spec_object ("channel",
1113 "telepathy channel",
1114 "The text channel for the chat",
1117 G_PARAM_CONSTRUCT_ONLY));
1119 g_object_class_install_property (object_class,
1120 PROP_REMOTE_CONTACT,
1121 g_param_spec_object ("remote-contact",
1122 "The remote contact",
1123 "The remote contact if there is no group iface on the channel",
1124 EMPATHY_TYPE_CONTACT,
1127 g_object_class_install_property (object_class,
1129 g_param_spec_boolean ("ready",
1130 "Is the object ready",
1131 "This object can't be used until this becomes true",
1136 signals[MESSAGE_RECEIVED] =
1137 g_signal_new ("message-received",
1138 G_TYPE_FROM_CLASS (klass),
1142 g_cclosure_marshal_VOID__OBJECT,
1144 1, EMPATHY_TYPE_MESSAGE);
1146 signals[SEND_ERROR] =
1147 g_signal_new ("send-error",
1148 G_TYPE_FROM_CLASS (klass),
1152 _empathy_marshal_VOID__OBJECT_UINT,
1154 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1156 signals[CHAT_STATE_CHANGED] =
1157 g_signal_new ("chat-state-changed",
1158 G_TYPE_FROM_CLASS (klass),
1162 _empathy_marshal_VOID__OBJECT_UINT,
1164 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1166 signals[PROPERTY_CHANGED] =
1167 g_signal_new ("property-changed",
1168 G_TYPE_FROM_CLASS (klass),
1172 _empathy_marshal_VOID__STRING_BOXED,
1174 2, G_TYPE_STRING, G_TYPE_VALUE);
1177 g_signal_new ("destroy",
1178 G_TYPE_FROM_CLASS (klass),
1182 g_cclosure_marshal_VOID__VOID,
1186 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1190 empathy_tp_chat_init (EmpathyTpChat *chat)
1192 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1193 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1196 priv->contact_monitor = NULL;
1197 priv->messages_queue = g_queue_new ();
1198 priv->pending_messages_queue = g_queue_new ();
1202 tp_chat_iface_init (EmpathyContactListIface *iface)
1204 iface->add = tp_chat_add;
1205 iface->remove = tp_chat_remove;
1206 iface->get_members = tp_chat_get_members;
1207 iface->get_monitor = tp_chat_get_monitor;
1211 empathy_tp_chat_new (TpChannel *channel)
1213 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1219 empathy_tp_chat_close (EmpathyTpChat *chat) {
1220 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1222 /* If there are still messages left, it'll come back..
1223 * We loose the ordering of sent messages though */
1224 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1225 "closing channel", NULL, NULL);
1229 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1231 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1233 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1235 return tp_channel_get_identifier (priv->channel);
1239 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1241 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1243 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1244 g_return_val_if_fail (priv->ready, NULL);
1246 return priv->remote_contact;
1250 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1252 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1254 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1256 return priv->channel;
1260 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1262 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1264 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1266 return tp_channel_borrow_connection (priv->channel);
1270 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1272 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1274 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1280 empathy_tp_chat_send (EmpathyTpChat *chat,
1281 EmpathyMessage *message)
1283 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1284 const gchar *message_body;
1285 TpChannelTextMessageType message_type;
1287 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1288 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1289 g_return_if_fail (priv->ready);
1291 message_body = empathy_message_get_body (message);
1292 message_type = empathy_message_get_tptype (message);
1294 DEBUG ("Sending message: %s", message_body);
1295 tp_cli_channel_type_text_call_send (priv->channel, -1,
1299 g_object_ref (message),
1300 (GDestroyNotify) g_object_unref,
1305 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1306 TpChannelChatState state)
1308 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1310 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1311 g_return_if_fail (priv->ready);
1313 DEBUG ("Set state: %d", state);
1314 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1317 "setting chat state",
1324 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1326 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1328 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1329 g_return_val_if_fail (priv->ready, NULL);
1331 return priv->pending_messages_queue->head;
1335 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1336 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1338 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1339 priv->channel, -1, ids, tp_chat_async_cb,
1340 "acknowledging received message", NULL, G_OBJECT (chat));
1344 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1345 EmpathyMessage *message) {
1346 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1347 GArray *message_ids;
1351 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1352 g_return_if_fail (priv->ready);
1354 if (empathy_message_get_sender (message) == priv->user)
1357 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1359 id = empathy_message_get_id (message);
1360 g_array_append_val (message_ids, id);
1361 acknowledge_messages (chat, message_ids);
1362 g_array_free (message_ids, TRUE);
1365 m = g_queue_find (priv->pending_messages_queue, message);
1366 g_assert (m != NULL);
1367 g_queue_delete_link (priv->pending_messages_queue, m);
1368 g_object_unref (message);
1372 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1373 const GList *messages) {
1374 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1375 /* Copy messages as the messges list (probably is) our own */
1376 GList *msgs = g_list_copy ((GList *) messages);
1379 GArray *message_ids;
1381 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1382 g_return_if_fail (priv->ready);
1384 length = g_list_length ((GList *)messages);
1389 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1391 for (l = msgs; l != NULL; l = g_list_next (l)) {
1394 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1396 m = g_queue_find (priv->pending_messages_queue, message);
1397 g_assert (m != NULL);
1398 g_queue_delete_link (priv->pending_messages_queue, m);
1400 if (empathy_message_get_sender (message) != priv->user) {
1401 guint id = empathy_message_get_id (message);
1402 g_array_append_val (message_ids, id);
1404 g_object_unref (message);
1407 if (message_ids->len > 0)
1408 acknowledge_messages (chat, message_ids);
1410 g_array_free (message_ids, TRUE);