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;
58 TpChannelPasswordFlags password_flags;
59 /* TRUE if we fetched the password flag of the channel or if it's not needed
60 * (channel doesn't implement the Password interface) */
61 gboolean got_password_flags;
65 static void tp_chat_iface_init (EmpathyContactListIface *iface);
83 static guint signals[LAST_SIGNAL];
85 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
86 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
89 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
92 tp_chat_invalidated_cb (TpProxy *proxy,
98 DEBUG ("Channel invalidated: %s", message);
99 g_signal_emit (chat, signals[DESTROY], 0);
103 tp_chat_async_cb (TpChannel *proxy,
106 GObject *weak_object)
109 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
114 tp_chat_add (EmpathyContactList *list,
115 EmpathyContact *contact,
116 const gchar *message)
118 EmpathyTpChatPriv *priv = GET_PRIV (list);
120 GArray handles = {(gchar *) &handle, 1};
122 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
123 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
125 handle = empathy_contact_get_handle (contact);
126 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
133 tp_chat_remove (EmpathyContactList *list,
134 EmpathyContact *contact,
135 const gchar *message)
137 EmpathyTpChatPriv *priv = GET_PRIV (list);
139 GArray handles = {(gchar *) &handle, 1};
141 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
142 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
144 handle = empathy_contact_get_handle (contact);
145 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
152 tp_chat_get_members (EmpathyContactList *list)
154 EmpathyTpChatPriv *priv = GET_PRIV (list);
155 GList *members = NULL;
157 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
160 members = g_list_copy (priv->members);
161 g_list_foreach (members, (GFunc) g_object_ref, NULL);
163 members = g_list_prepend (members, g_object_ref (priv->user));
164 if (priv->remote_contact != NULL)
165 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
171 static EmpathyContactMonitor *
172 tp_chat_get_monitor (EmpathyContactList *list)
174 EmpathyTpChatPriv *priv;
176 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
178 priv = GET_PRIV (list);
180 if (priv->contact_monitor == NULL) {
181 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
184 return priv->contact_monitor;
188 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
190 EmpathyTpChatPriv *priv = GET_PRIV (chat);
191 EmpathyMessage *message;
193 /* Check if we can now emit some queued messages */
194 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
195 if (empathy_message_get_sender (message) == NULL) {
199 DEBUG ("Queued message ready");
200 g_queue_pop_head (priv->messages_queue);
201 g_queue_push_tail (priv->pending_messages_queue, message);
202 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
207 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
208 EmpathyContact *contact,
213 EmpathyTpChatPriv *priv = GET_PRIV (chat);
216 DEBUG ("Error: %s", error->message);
217 /* Do not block the message queue, just drop this message */
218 g_queue_remove (priv->messages_queue, message);
220 empathy_message_set_sender (message, contact);
223 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
227 tp_chat_build_message (EmpathyTpChat *chat,
233 const gchar *message_body)
235 EmpathyTpChatPriv *priv;
236 EmpathyMessage *message;
238 priv = GET_PRIV (chat);
240 message = empathy_message_new (message_body);
241 empathy_message_set_tptype (message, type);
242 empathy_message_set_receiver (message, priv->user);
243 empathy_message_set_timestamp (message, timestamp);
244 empathy_message_set_id (message, id);
245 empathy_message_set_incoming (message, incoming);
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,
307 tp_chat_sent_cb (TpChannel *channel,
310 const gchar *message_body,
314 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
315 EmpathyTpChatPriv *priv = GET_PRIV (chat);
317 if (priv->channel == NULL)
320 DEBUG ("Message sent: %s", message_body);
322 tp_chat_build_message (chat,
332 tp_chat_send_error_cb (TpChannel *channel,
336 const gchar *message_body,
340 EmpathyTpChatPriv *priv = GET_PRIV (chat);
342 if (priv->channel == NULL)
345 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
347 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
351 tp_chat_send_cb (TpChannel *proxy,
356 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
359 DEBUG ("Error: %s", error->message);
360 g_signal_emit (chat, signals[SEND_ERROR], 0,
361 empathy_message_get_body (message),
362 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
368 TpChannelChatState state;
372 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
373 EmpathyContact *contact,
378 TpChannelChatState state;
381 DEBUG ("Error: %s", error->message);
385 state = GPOINTER_TO_UINT (user_data);
386 DEBUG ("Chat state changed for %s (%d): %d",
387 empathy_contact_get_name (contact),
388 empathy_contact_get_handle (contact), state);
390 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
394 tp_chat_state_changed_cb (TpChannel *channel,
396 TpChannelChatState state,
400 EmpathyTpChatPriv *priv = GET_PRIV (chat);
402 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
403 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
408 tp_chat_list_pending_messages_cb (TpChannel *channel,
409 const GPtrArray *messages_list,
414 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
415 EmpathyTpChatPriv *priv = GET_PRIV (chat);
417 GArray *empty_non_text_content_ids = NULL;
419 priv->listing_pending_messages = FALSE;
421 if (priv->channel == NULL)
425 DEBUG ("Error listing pending messages: %s", error->message);
429 for (i = 0; i < messages_list->len; i++) {
430 GValueArray *message_struct;
431 const gchar *message_body;
438 message_struct = g_ptr_array_index (messages_list, i);
440 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
441 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
442 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
443 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
444 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
445 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
447 DEBUG ("Message pending: %s", message_body);
449 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
450 !tp_strdiff (message_body, "")) {
451 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
453 if (empty_non_text_content_ids == NULL) {
454 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
457 g_array_append_val (empty_non_text_content_ids, message_id);
461 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 EmpathyTpChatProperty *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 EmpathyTpChatProperty *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 EmpathyTpChatProperty *property;
600 prop_struct = g_ptr_array_index (properties, i);
601 property = g_slice_new0 (EmpathyTpChatProperty);
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 EmpathyTpChatProperty *property;
632 if (!priv->had_properties_list) {
636 for (i = 0; i < priv->properties->len; i++) {
637 property = g_ptr_array_index (priv->properties, i);
638 if (!tp_strdiff (property->name, name)) {
639 GPtrArray *properties;
642 GValue dest_value = {0, };
644 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
648 g_value_init (&id, G_TYPE_UINT);
649 g_value_init (&dest_value, G_TYPE_VALUE);
650 g_value_set_uint (&id, property->id);
651 g_value_set_boxed (&dest_value, value);
653 prop = g_value_array_new (2);
654 g_value_array_append (prop, &id);
655 g_value_array_append (prop, &dest_value);
657 properties = g_ptr_array_sized_new (1);
658 g_ptr_array_add (properties, prop);
660 DEBUG ("Set property %s", name);
661 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
663 (tp_cli_properties_interface_callback_for_set_properties)
665 "Seting property", NULL,
668 g_ptr_array_free (properties, TRUE);
669 g_value_array_free (prop);
676 EmpathyTpChatProperty *
677 empathy_tp_chat_get_property (EmpathyTpChat *chat,
680 EmpathyTpChatPriv *priv = GET_PRIV (chat);
681 EmpathyTpChatProperty *property;
684 if (!priv->had_properties_list) {
688 for (i = 0; i < priv->properties->len; i++) {
689 property = g_ptr_array_index (priv->properties, i);
690 if (!tp_strdiff (property->name, name)) {
699 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
701 EmpathyTpChatPriv *priv = GET_PRIV (chat);
703 return priv->properties;
707 tp_chat_dispose (GObject *object)
709 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
710 EmpathyTpChatPriv *priv = GET_PRIV (self);
712 if (priv->dispose_has_run)
715 priv->dispose_has_run = TRUE;
717 if (priv->channel != NULL) {
718 g_signal_handlers_disconnect_by_func (priv->channel,
719 tp_chat_invalidated_cb, self);
720 g_object_unref (priv->channel);
722 priv->channel = NULL;
724 if (priv->remote_contact != NULL)
725 g_object_unref (priv->remote_contact);
726 priv->remote_contact = NULL;
728 if (priv->factory != NULL)
729 g_object_unref (priv->factory);
730 priv->factory = NULL;
732 if (priv->user != NULL)
733 g_object_unref (priv->user);
736 if (priv->contact_monitor)
737 g_object_unref (priv->contact_monitor);
738 priv->contact_monitor = NULL;
740 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
741 g_queue_clear (priv->messages_queue);
743 g_queue_foreach (priv->pending_messages_queue,
744 (GFunc) g_object_unref, NULL);
745 g_queue_clear (priv->pending_messages_queue);
747 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
748 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
752 tp_chat_finalize (GObject *object)
754 EmpathyTpChatPriv *priv = GET_PRIV (object);
757 DEBUG ("Finalize: %p", object);
759 if (priv->properties) {
760 for (i = 0; i < priv->properties->len; i++) {
761 EmpathyTpChatProperty *property;
763 property = g_ptr_array_index (priv->properties, i);
764 g_free (property->name);
765 if (property->value) {
766 tp_g_value_slice_free (property->value);
768 g_slice_free (EmpathyTpChatProperty, property);
770 g_ptr_array_free (priv->properties, TRUE);
773 g_queue_free (priv->messages_queue);
774 g_queue_free (priv->pending_messages_queue);
776 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
780 tp_chat_check_if_ready (EmpathyTpChat *chat)
782 EmpathyTpChatPriv *priv = GET_PRIV (chat);
787 if (priv->user == NULL)
790 if (!priv->got_password_flags)
793 /* We need either the members (room) or the remote contact (private chat).
794 * If the chat is protected by a password we can't get these information so
795 * consider the chat as ready so it can be presented to the user. */
796 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
797 priv->remote_contact == NULL)
802 tp_cli_channel_type_text_connect_to_received (priv->channel,
805 G_OBJECT (chat), NULL);
806 priv->listing_pending_messages = TRUE;
807 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
809 tp_chat_list_pending_messages_cb,
813 tp_cli_channel_type_text_connect_to_sent (priv->channel,
816 G_OBJECT (chat), NULL);
817 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
818 tp_chat_send_error_cb,
820 G_OBJECT (chat), NULL);
821 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
822 tp_chat_state_changed_cb,
824 G_OBJECT (chat), NULL);
825 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
826 tp_chat_state_changed_cb,
828 G_OBJECT (chat), NULL);
830 g_object_notify (G_OBJECT (chat), "ready");
834 tp_chat_update_remote_contact (EmpathyTpChat *chat)
836 EmpathyTpChatPriv *priv = GET_PRIV (chat);
837 EmpathyContact *contact = NULL;
838 TpHandle self_handle;
839 TpHandleType handle_type;
842 /* If this is a named chatroom, never pretend it is a private chat */
843 tp_channel_get_handle (priv->channel, &handle_type);
844 if (handle_type == TP_HANDLE_TYPE_ROOM) {
848 /* This is an MSN-like chat where anyone can join the chat at anytime.
849 * If there is only one non-self contact member, we are in a private
850 * chat and we set the "remote-contact" property to that contact. If
851 * there are more, set the "remote-contact" property to NULL and the
852 * UI will display a contact list. */
853 self_handle = tp_channel_group_get_self_handle (priv->channel);
854 for (l = priv->members; l; l = l->next) {
855 /* Skip self contact if member */
856 if (empathy_contact_get_handle (l->data) == self_handle) {
860 /* We have more than one remote contact, break */
861 if (contact != NULL) {
866 /* If we didn't find yet a remote contact, keep this one */
870 if (priv->remote_contact == contact) {
874 DEBUG ("Changing remote contact from %p to %p",
875 priv->remote_contact, contact);
877 if (priv->remote_contact) {
878 g_object_unref (priv->remote_contact);
881 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
882 g_object_notify (G_OBJECT (chat), "remote-contact");
886 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
888 EmpathyContact * const * contacts,
890 const TpHandle *failed,
895 EmpathyTpChatPriv *priv = GET_PRIV (chat);
897 const TpIntSet *members;
899 EmpathyContact *contact;
902 DEBUG ("Error: %s", error->message);
906 members = tp_channel_group_get_members (priv->channel);
907 for (i = 0; i < n_contacts; i++) {
908 contact = contacts[i];
909 handle = empathy_contact_get_handle (contact);
911 /* Make sure the contact is still member */
912 if (tp_intset_is_member (members, handle)) {
913 priv->members = g_list_prepend (priv->members,
914 g_object_ref (contact));
915 g_signal_emit_by_name (chat, "members-changed",
916 contact, NULL, 0, NULL, TRUE);
920 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
921 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
924 static EmpathyContact *
925 chat_lookup_contact (EmpathyTpChat *chat,
929 EmpathyTpChatPriv *priv = GET_PRIV (chat);
932 for (l = priv->members; l; l = l->next) {
933 EmpathyContact *c = l->data;
935 if (empathy_contact_get_handle (c) != handle) {
940 /* Caller takes the reference. */
941 priv->members = g_list_delete_link (priv->members, l);
953 tp_chat_group_members_changed_cb (TpChannel *self,
957 GArray *local_pending,
958 GArray *remote_pending,
963 EmpathyTpChatPriv *priv = GET_PRIV (chat);
964 EmpathyContact *contact;
965 EmpathyContact *actor_contact = NULL;
969 actor_contact = chat_lookup_contact (chat, actor, FALSE);
970 if (actor_contact == NULL) {
971 /* FIXME: handle this a tad more gracefully: perhaps
972 * the actor was a server op. We could use the
973 * contact-ids detail of MembersChangedDetailed.
975 DEBUG ("actor %u not a channel member", actor);
979 /* Remove contacts that are not members anymore */
980 for (i = 0; i < removed->len; i++) {
981 contact = chat_lookup_contact (chat,
982 g_array_index (removed, TpHandle, i), TRUE);
984 if (contact != NULL) {
985 g_signal_emit_by_name (chat, "members-changed", contact,
986 actor_contact, reason, message,
988 g_object_unref (contact);
992 /* Request added contacts */
993 if (added->len > 0) {
994 empathy_tp_contact_factory_get_from_handles (priv->factory,
995 added->len, (TpHandle *) added->data,
996 tp_chat_got_added_contacts_cb, NULL, NULL,
1000 tp_chat_update_remote_contact (chat);
1002 if (actor_contact != NULL) {
1003 g_object_unref (actor_contact);
1008 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
1009 EmpathyContact *contact,
1010 const GError *error,
1014 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1017 DEBUG ("Error: %s", error->message);
1018 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1022 priv->remote_contact = g_object_ref (contact);
1023 g_object_notify (chat, "remote-contact");
1025 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1029 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
1030 EmpathyContact *contact,
1031 const GError *error,
1035 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1038 DEBUG ("Error: %s", error->message);
1039 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1043 priv->user = g_object_ref (contact);
1044 empathy_contact_set_is_user (priv->user, TRUE);
1045 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1049 got_password_flags_cb (TpChannel *proxy,
1050 guint password_flags,
1051 const GError *error,
1053 GObject *weak_object)
1055 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1056 EmpathyTpChatPriv *priv = GET_PRIV (self);
1058 priv->got_password_flags = TRUE;
1059 priv->password_flags = password_flags;
1061 tp_chat_check_if_ready (EMPATHY_TP_CHAT (self));
1065 tp_chat_constructor (GType type,
1067 GObjectConstructParam *props)
1070 EmpathyTpChatPriv *priv;
1071 TpConnection *connection;
1074 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1076 priv = GET_PRIV (chat);
1078 connection = tp_channel_borrow_connection (priv->channel);
1079 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1080 g_signal_connect (priv->channel, "invalidated",
1081 G_CALLBACK (tp_chat_invalidated_cb),
1084 if (tp_proxy_has_interface_by_id (priv->channel,
1085 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1086 const TpIntSet *members;
1089 /* Get self contact from the group's self handle */
1090 handle = tp_channel_group_get_self_handle (priv->channel);
1091 empathy_tp_contact_factory_get_from_handle (priv->factory,
1092 handle, tp_chat_got_self_contact_cb,
1095 /* Get initial member contacts */
1096 members = tp_channel_group_get_members (priv->channel);
1097 handles = tp_intset_to_array (members);
1098 empathy_tp_contact_factory_get_from_handles (priv->factory,
1099 handles->len, (TpHandle *) handles->data,
1100 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1102 g_signal_connect (priv->channel, "group-members-changed",
1103 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1105 /* Get the self contact from the connection's self handle */
1106 handle = tp_connection_get_self_handle (connection);
1107 empathy_tp_contact_factory_get_from_handle (priv->factory,
1108 handle, tp_chat_got_self_contact_cb,
1111 /* Get the remote contact */
1112 handle = tp_channel_get_handle (priv->channel, NULL);
1113 empathy_tp_contact_factory_get_from_handle (priv->factory,
1114 handle, tp_chat_got_remote_contact_cb,
1118 if (tp_proxy_has_interface_by_id (priv->channel,
1119 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1120 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1121 tp_chat_list_properties_cb,
1124 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1125 tp_chat_properties_changed_cb,
1127 G_OBJECT (chat), NULL);
1128 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1129 tp_chat_property_flags_changed_cb,
1131 G_OBJECT (chat), NULL);
1134 /* Check if the chat is password protected */
1135 if (tp_proxy_has_interface_by_id (priv->channel,
1136 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1137 priv->got_password_flags = FALSE;
1138 tp_cli_channel_interface_password_call_get_password_flags (priv->channel,
1139 -1, got_password_flags_cb, chat, NULL, chat);
1141 /* No Password interface, so no need to fetch the password flags */
1142 priv->got_password_flags = TRUE;
1149 tp_chat_get_property (GObject *object,
1154 EmpathyTpChatPriv *priv = GET_PRIV (object);
1158 g_value_set_object (value, priv->channel);
1160 case PROP_REMOTE_CONTACT:
1161 g_value_set_object (value, priv->remote_contact);
1164 g_value_set_boolean (value, priv->ready);
1167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1173 tp_chat_set_property (GObject *object,
1175 const GValue *value,
1178 EmpathyTpChatPriv *priv = GET_PRIV (object);
1182 priv->channel = g_value_dup_object (value);
1185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1191 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1193 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1195 object_class->dispose = tp_chat_dispose;
1196 object_class->finalize = tp_chat_finalize;
1197 object_class->constructor = tp_chat_constructor;
1198 object_class->get_property = tp_chat_get_property;
1199 object_class->set_property = tp_chat_set_property;
1201 g_object_class_install_property (object_class,
1203 g_param_spec_object ("channel",
1204 "telepathy channel",
1205 "The text channel for the chat",
1208 G_PARAM_CONSTRUCT_ONLY));
1210 g_object_class_install_property (object_class,
1211 PROP_REMOTE_CONTACT,
1212 g_param_spec_object ("remote-contact",
1213 "The remote contact",
1214 "The remote contact if there is no group iface on the channel",
1215 EMPATHY_TYPE_CONTACT,
1218 g_object_class_install_property (object_class,
1220 g_param_spec_boolean ("ready",
1221 "Is the object ready",
1222 "This object can't be used until this becomes true",
1227 signals[MESSAGE_RECEIVED] =
1228 g_signal_new ("message-received",
1229 G_TYPE_FROM_CLASS (klass),
1233 g_cclosure_marshal_VOID__OBJECT,
1235 1, EMPATHY_TYPE_MESSAGE);
1237 signals[SEND_ERROR] =
1238 g_signal_new ("send-error",
1239 G_TYPE_FROM_CLASS (klass),
1243 _empathy_marshal_VOID__STRING_UINT,
1245 2, G_TYPE_STRING, G_TYPE_UINT);
1247 signals[CHAT_STATE_CHANGED] =
1248 g_signal_new ("chat-state-changed",
1249 G_TYPE_FROM_CLASS (klass),
1253 _empathy_marshal_VOID__OBJECT_UINT,
1255 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1257 signals[PROPERTY_CHANGED] =
1258 g_signal_new ("property-changed",
1259 G_TYPE_FROM_CLASS (klass),
1263 _empathy_marshal_VOID__STRING_BOXED,
1265 2, G_TYPE_STRING, G_TYPE_VALUE);
1268 g_signal_new ("destroy",
1269 G_TYPE_FROM_CLASS (klass),
1273 g_cclosure_marshal_VOID__VOID,
1277 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1281 empathy_tp_chat_init (EmpathyTpChat *chat)
1283 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1284 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1287 priv->contact_monitor = NULL;
1288 priv->messages_queue = g_queue_new ();
1289 priv->pending_messages_queue = g_queue_new ();
1293 tp_chat_iface_init (EmpathyContactListIface *iface)
1295 iface->add = tp_chat_add;
1296 iface->remove = tp_chat_remove;
1297 iface->get_members = tp_chat_get_members;
1298 iface->get_monitor = tp_chat_get_monitor;
1302 empathy_tp_chat_new (TpChannel *channel)
1304 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1310 empathy_tp_chat_close (EmpathyTpChat *chat) {
1311 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1313 /* If there are still messages left, it'll come back..
1314 * We loose the ordering of sent messages though */
1315 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1316 "closing channel", NULL, NULL);
1320 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1322 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1326 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1328 id = tp_channel_get_identifier (priv->channel);
1329 if (!EMP_STR_EMPTY (id))
1331 else if (priv->remote_contact)
1332 return empathy_contact_get_id (priv->remote_contact);
1339 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1341 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1343 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1344 g_return_val_if_fail (priv->ready, NULL);
1346 return priv->remote_contact;
1350 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1352 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1354 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1356 return priv->channel;
1360 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1362 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1364 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1366 return tp_channel_borrow_connection (priv->channel);
1370 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1372 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1374 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1380 empathy_tp_chat_send (EmpathyTpChat *chat,
1381 EmpathyMessage *message)
1383 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1384 const gchar *message_body;
1385 TpChannelTextMessageType message_type;
1387 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1388 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1389 g_return_if_fail (priv->ready);
1391 message_body = empathy_message_get_body (message);
1392 message_type = empathy_message_get_tptype (message);
1394 DEBUG ("Sending message: %s", message_body);
1395 tp_cli_channel_type_text_call_send (priv->channel, -1,
1399 g_object_ref (message),
1400 (GDestroyNotify) g_object_unref,
1405 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1406 TpChannelChatState state)
1408 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1410 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1411 g_return_if_fail (priv->ready);
1413 if (tp_proxy_has_interface_by_id (priv->channel,
1414 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1415 DEBUG ("Set state: %d", state);
1416 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1419 "setting chat state",
1427 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1429 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1431 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1432 g_return_val_if_fail (priv->ready, NULL);
1434 return priv->pending_messages_queue->head;
1438 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1439 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1441 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1442 priv->channel, -1, ids, tp_chat_async_cb,
1443 "acknowledging received message", NULL, G_OBJECT (chat));
1447 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1448 EmpathyMessage *message) {
1449 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1450 GArray *message_ids;
1454 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1455 g_return_if_fail (priv->ready);
1457 if (!empathy_message_is_incoming (message))
1460 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1462 id = empathy_message_get_id (message);
1463 g_array_append_val (message_ids, id);
1464 acknowledge_messages (chat, message_ids);
1465 g_array_free (message_ids, TRUE);
1468 m = g_queue_find (priv->pending_messages_queue, message);
1469 g_assert (m != NULL);
1470 g_queue_delete_link (priv->pending_messages_queue, m);
1471 g_object_unref (message);
1475 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1476 const GList *messages) {
1477 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1478 /* Copy messages as the messges list (probably is) our own */
1479 GList *msgs = g_list_copy ((GList *) messages);
1482 GArray *message_ids;
1484 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1485 g_return_if_fail (priv->ready);
1487 length = g_list_length ((GList *) messages);
1492 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1494 for (l = msgs; l != NULL; l = g_list_next (l)) {
1497 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1499 m = g_queue_find (priv->pending_messages_queue, message);
1500 g_assert (m != NULL);
1501 g_queue_delete_link (priv->pending_messages_queue, m);
1503 if (empathy_message_is_incoming (message)) {
1504 guint id = empathy_message_get_id (message);
1505 g_array_append_val (message_ids, id);
1507 g_object_unref (message);
1510 if (message_ids->len > 0)
1511 acknowledge_messages (chat, message_ids);
1513 g_array_free (message_ids, TRUE);
1518 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1520 EmpathyTpChatPriv *priv = GET_PRIV (self);
1522 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1526 provide_password_cb (TpChannel *channel,
1528 const GError *error,
1530 GObject *weak_object)
1532 GSimpleAsyncResult *result = user_data;
1534 if (error != NULL) {
1535 g_simple_async_result_set_from_error (result, error);
1537 else if (!correct) {
1538 /* The current D-Bus API is a bit weird so re-use the
1539 * AuthenticationFailed error */
1540 g_simple_async_result_set_error (result, TP_ERRORS,
1541 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1544 g_simple_async_result_complete (result);
1545 g_object_unref (result);
1549 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1550 const gchar *password,
1551 GAsyncReadyCallback callback,
1554 EmpathyTpChatPriv *priv = GET_PRIV (self);
1555 GSimpleAsyncResult *result;
1557 result = g_simple_async_result_new (G_OBJECT (self),
1558 callback, user_data, empathy_tp_chat_provide_password_finish);
1560 tp_cli_channel_interface_password_call_provide_password (priv->channel, -1,
1561 password, provide_password_cb, result, NULL, G_OBJECT (self));
1565 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1566 GAsyncResult *result,
1569 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1573 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1574 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);