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);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
90 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
93 tp_chat_invalidated_cb (TpProxy *proxy,
99 DEBUG ("Channel invalidated: %s", message);
100 g_signal_emit (chat, signals[DESTROY], 0);
104 tp_chat_async_cb (TpChannel *proxy,
107 GObject *weak_object)
110 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
115 tp_chat_add (EmpathyContactList *list,
116 EmpathyContact *contact,
117 const gchar *message)
119 EmpathyTpChatPriv *priv = GET_PRIV (list);
121 GArray handles = {(gchar *) &handle, 1};
123 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
124 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
126 handle = empathy_contact_get_handle (contact);
127 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
134 tp_chat_remove (EmpathyContactList *list,
135 EmpathyContact *contact,
136 const gchar *message)
138 EmpathyTpChatPriv *priv = GET_PRIV (list);
140 GArray handles = {(gchar *) &handle, 1};
142 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
143 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
145 handle = empathy_contact_get_handle (contact);
146 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
153 tp_chat_get_members (EmpathyContactList *list)
155 EmpathyTpChatPriv *priv = GET_PRIV (list);
156 GList *members = NULL;
158 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
161 members = g_list_copy (priv->members);
162 g_list_foreach (members, (GFunc) g_object_ref, NULL);
164 members = g_list_prepend (members, g_object_ref (priv->user));
165 if (priv->remote_contact != NULL)
166 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
172 static EmpathyContactMonitor *
173 tp_chat_get_monitor (EmpathyContactList *list)
175 EmpathyTpChatPriv *priv;
177 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
179 priv = GET_PRIV (list);
181 if (priv->contact_monitor == NULL) {
182 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
185 return priv->contact_monitor;
189 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
191 EmpathyTpChatPriv *priv = GET_PRIV (chat);
192 EmpathyMessage *message;
194 /* Check if we can now emit some queued messages */
195 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
196 if (empathy_message_get_sender (message) == NULL) {
200 DEBUG ("Queued message ready");
201 g_queue_pop_head (priv->messages_queue);
202 g_queue_push_tail (priv->pending_messages_queue, message);
203 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
208 tp_chat_got_sender_cb (EmpathyTpContactFactory *factory,
209 EmpathyContact *contact,
214 EmpathyTpChatPriv *priv = GET_PRIV (chat);
217 DEBUG ("Error: %s", error->message);
218 /* Do not block the message queue, just drop this message */
219 g_queue_remove (priv->messages_queue, message);
221 empathy_message_set_sender (message, contact);
224 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
228 tp_chat_build_message (EmpathyTpChat *chat,
234 const gchar *message_body)
236 EmpathyTpChatPriv *priv;
237 EmpathyMessage *message;
239 priv = GET_PRIV (chat);
241 message = empathy_message_new (message_body);
242 empathy_message_set_tptype (message, type);
243 empathy_message_set_receiver (message, priv->user);
244 empathy_message_set_timestamp (message, timestamp);
245 empathy_message_set_id (message, id);
246 empathy_message_set_incoming (message, incoming);
248 g_queue_push_tail (priv->messages_queue, message);
250 if (from_handle == 0) {
251 empathy_message_set_sender (message, priv->user);
252 tp_chat_emit_queued_messages (chat);
254 empathy_tp_contact_factory_get_from_handle (priv->factory,
256 tp_chat_got_sender_cb,
257 message, NULL, G_OBJECT (chat));
262 tp_chat_received_cb (TpChannel *channel,
268 const gchar *message_body,
272 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
273 EmpathyTpChatPriv *priv = GET_PRIV (chat);
275 if (priv->channel == NULL)
278 if (priv->listing_pending_messages) {
282 DEBUG ("Message received: %s", message_body);
284 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
285 !tp_strdiff (message_body, "")) {
288 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
290 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
291 g_array_append_val (ids, message_id);
292 acknowledge_messages (chat, ids);
293 g_array_free (ids, TRUE);
298 tp_chat_build_message (chat,
308 tp_chat_sent_cb (TpChannel *channel,
311 const gchar *message_body,
315 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
316 EmpathyTpChatPriv *priv = GET_PRIV (chat);
318 if (priv->channel == NULL)
321 DEBUG ("Message sent: %s", message_body);
323 tp_chat_build_message (chat,
333 tp_chat_send_error_cb (TpChannel *channel,
337 const gchar *message_body,
341 EmpathyTpChatPriv *priv = GET_PRIV (chat);
343 if (priv->channel == NULL)
346 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
348 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
352 tp_chat_send_cb (TpChannel *proxy,
357 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
360 DEBUG ("Error: %s", error->message);
361 g_signal_emit (chat, signals[SEND_ERROR], 0,
362 empathy_message_get_body (message),
363 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
369 TpChannelChatState state;
373 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
374 EmpathyContact *contact,
379 TpChannelChatState state;
382 DEBUG ("Error: %s", error->message);
386 state = GPOINTER_TO_UINT (user_data);
387 DEBUG ("Chat state changed for %s (%d): %d",
388 empathy_contact_get_name (contact),
389 empathy_contact_get_handle (contact), state);
391 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
395 tp_chat_state_changed_cb (TpChannel *channel,
397 TpChannelChatState state,
401 EmpathyTpChatPriv *priv = GET_PRIV (chat);
403 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
404 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
409 tp_chat_list_pending_messages_cb (TpChannel *channel,
410 const GPtrArray *messages_list,
415 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
416 EmpathyTpChatPriv *priv = GET_PRIV (chat);
418 GArray *empty_non_text_content_ids = NULL;
420 priv->listing_pending_messages = FALSE;
422 if (priv->channel == NULL)
426 DEBUG ("Error listing pending messages: %s", error->message);
430 for (i = 0; i < messages_list->len; i++) {
431 GValueArray *message_struct;
432 const gchar *message_body;
439 message_struct = g_ptr_array_index (messages_list, i);
441 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
442 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
443 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
444 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
445 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
446 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
448 DEBUG ("Message pending: %s", message_body);
450 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
451 !tp_strdiff (message_body, "")) {
452 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
454 if (empty_non_text_content_ids == NULL) {
455 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
458 g_array_append_val (empty_non_text_content_ids, message_id);
462 tp_chat_build_message (chat,
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 EmpathyTpChatProperty *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 EmpathyTpChatProperty *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 EmpathyTpChatProperty *property;
601 prop_struct = g_ptr_array_index (properties, i);
602 property = g_slice_new0 (EmpathyTpChatProperty);
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 EmpathyTpChatProperty *property;
633 if (!priv->had_properties_list) {
637 for (i = 0; i < priv->properties->len; i++) {
638 property = g_ptr_array_index (priv->properties, i);
639 if (!tp_strdiff (property->name, name)) {
640 GPtrArray *properties;
643 GValue dest_value = {0, };
645 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
649 g_value_init (&id, G_TYPE_UINT);
650 g_value_init (&dest_value, G_TYPE_VALUE);
651 g_value_set_uint (&id, property->id);
652 g_value_set_boxed (&dest_value, value);
654 prop = g_value_array_new (2);
655 g_value_array_append (prop, &id);
656 g_value_array_append (prop, &dest_value);
658 properties = g_ptr_array_sized_new (1);
659 g_ptr_array_add (properties, prop);
661 DEBUG ("Set property %s", name);
662 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
664 (tp_cli_properties_interface_callback_for_set_properties)
666 "Seting property", NULL,
669 g_ptr_array_free (properties, TRUE);
670 g_value_array_free (prop);
677 EmpathyTpChatProperty *
678 empathy_tp_chat_get_property (EmpathyTpChat *chat,
681 EmpathyTpChatPriv *priv = GET_PRIV (chat);
682 EmpathyTpChatProperty *property;
685 if (!priv->had_properties_list) {
689 for (i = 0; i < priv->properties->len; i++) {
690 property = g_ptr_array_index (priv->properties, i);
691 if (!tp_strdiff (property->name, name)) {
700 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
702 EmpathyTpChatPriv *priv = GET_PRIV (chat);
704 return priv->properties;
708 tp_chat_dispose (GObject *object)
710 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
711 EmpathyTpChatPriv *priv = GET_PRIV (self);
713 if (priv->dispose_has_run)
716 priv->dispose_has_run = TRUE;
718 if (priv->channel != NULL) {
719 g_signal_handlers_disconnect_by_func (priv->channel,
720 tp_chat_invalidated_cb, self);
721 g_object_unref (priv->channel);
723 priv->channel = NULL;
725 if (priv->remote_contact != NULL)
726 g_object_unref (priv->remote_contact);
727 priv->remote_contact = NULL;
729 if (priv->factory != NULL)
730 g_object_unref (priv->factory);
731 priv->factory = NULL;
733 if (priv->user != NULL)
734 g_object_unref (priv->user);
737 if (priv->contact_monitor)
738 g_object_unref (priv->contact_monitor);
739 priv->contact_monitor = NULL;
741 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
742 g_queue_clear (priv->messages_queue);
744 g_queue_foreach (priv->pending_messages_queue,
745 (GFunc) g_object_unref, NULL);
746 g_queue_clear (priv->pending_messages_queue);
748 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
749 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
753 tp_chat_finalize (GObject *object)
755 EmpathyTpChatPriv *priv = GET_PRIV (object);
758 DEBUG ("Finalize: %p", object);
760 if (priv->properties) {
761 for (i = 0; i < priv->properties->len; i++) {
762 EmpathyTpChatProperty *property;
764 property = g_ptr_array_index (priv->properties, i);
765 g_free (property->name);
766 if (property->value) {
767 tp_g_value_slice_free (property->value);
769 g_slice_free (EmpathyTpChatProperty, property);
771 g_ptr_array_free (priv->properties, TRUE);
774 g_queue_free (priv->messages_queue);
775 g_queue_free (priv->pending_messages_queue);
777 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
781 tp_chat_check_if_ready (EmpathyTpChat *chat)
783 EmpathyTpChatPriv *priv = GET_PRIV (chat);
788 if (priv->user == NULL)
791 if (!priv->got_password_flags)
794 /* We need either the members (room) or the remote contact (private chat).
795 * If the chat is protected by a password we can't get these information so
796 * consider the chat as ready so it can be presented to the user. */
797 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
798 priv->remote_contact == NULL)
803 tp_cli_channel_type_text_connect_to_received (priv->channel,
806 G_OBJECT (chat), NULL);
807 priv->listing_pending_messages = TRUE;
808 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
810 tp_chat_list_pending_messages_cb,
814 tp_cli_channel_type_text_connect_to_sent (priv->channel,
817 G_OBJECT (chat), NULL);
818 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
819 tp_chat_send_error_cb,
821 G_OBJECT (chat), NULL);
822 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
823 tp_chat_state_changed_cb,
825 G_OBJECT (chat), NULL);
826 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
827 tp_chat_state_changed_cb,
829 G_OBJECT (chat), NULL);
831 g_object_notify (G_OBJECT (chat), "ready");
835 tp_chat_update_remote_contact (EmpathyTpChat *chat)
837 EmpathyTpChatPriv *priv = GET_PRIV (chat);
838 EmpathyContact *contact = NULL;
839 TpHandle self_handle;
840 TpHandleType handle_type;
843 /* If this is a named chatroom, never pretend it is a private chat */
844 tp_channel_get_handle (priv->channel, &handle_type);
845 if (handle_type == TP_HANDLE_TYPE_ROOM) {
849 /* This is an MSN-like chat where anyone can join the chat at anytime.
850 * If there is only one non-self contact member, we are in a private
851 * chat and we set the "remote-contact" property to that contact. If
852 * there are more, set the "remote-contact" property to NULL and the
853 * UI will display a contact list. */
854 self_handle = tp_channel_group_get_self_handle (priv->channel);
855 for (l = priv->members; l; l = l->next) {
856 /* Skip self contact if member */
857 if (empathy_contact_get_handle (l->data) == self_handle) {
861 /* We have more than one remote contact, break */
862 if (contact != NULL) {
867 /* If we didn't find yet a remote contact, keep this one */
871 if (priv->remote_contact == contact) {
875 DEBUG ("Changing remote contact from %p to %p",
876 priv->remote_contact, contact);
878 if (priv->remote_contact) {
879 g_object_unref (priv->remote_contact);
882 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
883 g_object_notify (G_OBJECT (chat), "remote-contact");
887 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
889 EmpathyContact * const * contacts,
891 const TpHandle *failed,
896 EmpathyTpChatPriv *priv = GET_PRIV (chat);
898 const TpIntSet *members;
900 EmpathyContact *contact;
903 DEBUG ("Error: %s", error->message);
907 members = tp_channel_group_get_members (priv->channel);
908 for (i = 0; i < n_contacts; i++) {
909 contact = contacts[i];
910 handle = empathy_contact_get_handle (contact);
912 /* Make sure the contact is still member */
913 if (tp_intset_is_member (members, handle)) {
914 priv->members = g_list_prepend (priv->members,
915 g_object_ref (contact));
916 g_signal_emit_by_name (chat, "members-changed",
917 contact, NULL, 0, NULL, TRUE);
921 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
922 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
925 static EmpathyContact *
926 chat_lookup_contact (EmpathyTpChat *chat,
930 EmpathyTpChatPriv *priv = GET_PRIV (chat);
933 for (l = priv->members; l; l = l->next) {
934 EmpathyContact *c = l->data;
936 if (empathy_contact_get_handle (c) != handle) {
941 /* Caller takes the reference. */
942 priv->members = g_list_delete_link (priv->members, l);
954 tp_chat_group_members_changed_cb (TpChannel *self,
958 GArray *local_pending,
959 GArray *remote_pending,
964 EmpathyTpChatPriv *priv = GET_PRIV (chat);
965 EmpathyContact *contact;
966 EmpathyContact *actor_contact = NULL;
970 actor_contact = chat_lookup_contact (chat, actor, FALSE);
971 if (actor_contact == NULL) {
972 /* FIXME: handle this a tad more gracefully: perhaps
973 * the actor was a server op. We could use the
974 * contact-ids detail of MembersChangedDetailed.
976 DEBUG ("actor %u not a channel member", actor);
980 /* Remove contacts that are not members anymore */
981 for (i = 0; i < removed->len; i++) {
982 contact = chat_lookup_contact (chat,
983 g_array_index (removed, TpHandle, i), TRUE);
985 if (contact != NULL) {
986 g_signal_emit_by_name (chat, "members-changed", contact,
987 actor_contact, reason, message,
989 g_object_unref (contact);
993 /* Request added contacts */
994 if (added->len > 0) {
995 empathy_tp_contact_factory_get_from_handles (priv->factory,
996 added->len, (TpHandle *) added->data,
997 tp_chat_got_added_contacts_cb, NULL, NULL,
1001 tp_chat_update_remote_contact (chat);
1003 if (actor_contact != NULL) {
1004 g_object_unref (actor_contact);
1009 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
1010 EmpathyContact *contact,
1011 const GError *error,
1015 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1018 DEBUG ("Error: %s", error->message);
1019 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1023 priv->remote_contact = g_object_ref (contact);
1024 g_object_notify (chat, "remote-contact");
1026 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1030 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
1031 EmpathyContact *contact,
1032 const GError *error,
1036 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1039 DEBUG ("Error: %s", error->message);
1040 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1044 priv->user = g_object_ref (contact);
1045 empathy_contact_set_is_user (priv->user, TRUE);
1046 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1050 got_password_flags_cb (TpChannel *proxy,
1051 guint password_flags,
1052 const GError *error,
1054 GObject *weak_object)
1056 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1057 EmpathyTpChatPriv *priv = GET_PRIV (self);
1059 priv->got_password_flags = TRUE;
1060 priv->password_flags = password_flags;
1062 tp_chat_check_if_ready (EMPATHY_TP_CHAT (self));
1066 tp_chat_constructor (GType type,
1068 GObjectConstructParam *props)
1071 EmpathyTpChatPriv *priv;
1072 TpConnection *connection;
1075 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1077 priv = GET_PRIV (chat);
1079 connection = tp_channel_borrow_connection (priv->channel);
1080 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1081 g_signal_connect (priv->channel, "invalidated",
1082 G_CALLBACK (tp_chat_invalidated_cb),
1085 if (tp_proxy_has_interface_by_id (priv->channel,
1086 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1087 const TpIntSet *members;
1090 /* Get self contact from the group's self handle */
1091 handle = tp_channel_group_get_self_handle (priv->channel);
1092 empathy_tp_contact_factory_get_from_handle (priv->factory,
1093 handle, tp_chat_got_self_contact_cb,
1096 /* Get initial member contacts */
1097 members = tp_channel_group_get_members (priv->channel);
1098 handles = tp_intset_to_array (members);
1099 empathy_tp_contact_factory_get_from_handles (priv->factory,
1100 handles->len, (TpHandle *) handles->data,
1101 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1103 g_signal_connect (priv->channel, "group-members-changed",
1104 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1106 /* Get the self contact from the connection's self handle */
1107 handle = tp_connection_get_self_handle (connection);
1108 empathy_tp_contact_factory_get_from_handle (priv->factory,
1109 handle, tp_chat_got_self_contact_cb,
1112 /* Get the remote contact */
1113 handle = tp_channel_get_handle (priv->channel, NULL);
1114 empathy_tp_contact_factory_get_from_handle (priv->factory,
1115 handle, tp_chat_got_remote_contact_cb,
1119 if (tp_proxy_has_interface_by_id (priv->channel,
1120 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1121 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1122 tp_chat_list_properties_cb,
1125 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1126 tp_chat_properties_changed_cb,
1128 G_OBJECT (chat), NULL);
1129 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1130 tp_chat_property_flags_changed_cb,
1132 G_OBJECT (chat), NULL);
1135 /* Check if the chat is password protected */
1136 if (tp_proxy_has_interface_by_id (priv->channel,
1137 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1138 priv->got_password_flags = FALSE;
1139 tp_cli_channel_interface_password_call_get_password_flags (priv->channel,
1140 -1, got_password_flags_cb, chat, NULL, chat);
1142 /* No Password interface, so no need to fetch the password flags */
1143 priv->got_password_flags = TRUE;
1150 tp_chat_get_property (GObject *object,
1155 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1156 EmpathyTpChatPriv *priv = GET_PRIV (object);
1160 g_value_set_object (value, priv->channel);
1162 case PROP_REMOTE_CONTACT:
1163 g_value_set_object (value, priv->remote_contact);
1166 g_value_set_boolean (value, priv->ready);
1168 case PROP_PASSWORD_NEEDED:
1169 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1178 tp_chat_set_property (GObject *object,
1180 const GValue *value,
1183 EmpathyTpChatPriv *priv = GET_PRIV (object);
1187 priv->channel = g_value_dup_object (value);
1190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1196 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1198 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1200 object_class->dispose = tp_chat_dispose;
1201 object_class->finalize = tp_chat_finalize;
1202 object_class->constructor = tp_chat_constructor;
1203 object_class->get_property = tp_chat_get_property;
1204 object_class->set_property = tp_chat_set_property;
1206 g_object_class_install_property (object_class,
1208 g_param_spec_object ("channel",
1209 "telepathy channel",
1210 "The text channel for the chat",
1213 G_PARAM_CONSTRUCT_ONLY));
1215 g_object_class_install_property (object_class,
1216 PROP_REMOTE_CONTACT,
1217 g_param_spec_object ("remote-contact",
1218 "The remote contact",
1219 "The remote contact if there is no group iface on the channel",
1220 EMPATHY_TYPE_CONTACT,
1223 g_object_class_install_property (object_class,
1225 g_param_spec_boolean ("ready",
1226 "Is the object ready",
1227 "This object can't be used until this becomes true",
1231 g_object_class_install_property (object_class,
1232 PROP_PASSWORD_NEEDED,
1233 g_param_spec_boolean ("password-needed",
1235 "TRUE if a password is needed to join the channel",
1240 signals[MESSAGE_RECEIVED] =
1241 g_signal_new ("message-received",
1242 G_TYPE_FROM_CLASS (klass),
1246 g_cclosure_marshal_VOID__OBJECT,
1248 1, EMPATHY_TYPE_MESSAGE);
1250 signals[SEND_ERROR] =
1251 g_signal_new ("send-error",
1252 G_TYPE_FROM_CLASS (klass),
1256 _empathy_marshal_VOID__STRING_UINT,
1258 2, G_TYPE_STRING, G_TYPE_UINT);
1260 signals[CHAT_STATE_CHANGED] =
1261 g_signal_new ("chat-state-changed",
1262 G_TYPE_FROM_CLASS (klass),
1266 _empathy_marshal_VOID__OBJECT_UINT,
1268 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1270 signals[PROPERTY_CHANGED] =
1271 g_signal_new ("property-changed",
1272 G_TYPE_FROM_CLASS (klass),
1276 _empathy_marshal_VOID__STRING_BOXED,
1278 2, G_TYPE_STRING, G_TYPE_VALUE);
1281 g_signal_new ("destroy",
1282 G_TYPE_FROM_CLASS (klass),
1286 g_cclosure_marshal_VOID__VOID,
1290 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1294 empathy_tp_chat_init (EmpathyTpChat *chat)
1296 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1297 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1300 priv->contact_monitor = NULL;
1301 priv->messages_queue = g_queue_new ();
1302 priv->pending_messages_queue = g_queue_new ();
1306 tp_chat_iface_init (EmpathyContactListIface *iface)
1308 iface->add = tp_chat_add;
1309 iface->remove = tp_chat_remove;
1310 iface->get_members = tp_chat_get_members;
1311 iface->get_monitor = tp_chat_get_monitor;
1315 empathy_tp_chat_new (TpChannel *channel)
1317 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1323 empathy_tp_chat_close (EmpathyTpChat *chat) {
1324 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1326 /* If there are still messages left, it'll come back..
1327 * We loose the ordering of sent messages though */
1328 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1329 "closing channel", NULL, NULL);
1333 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1335 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1339 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1341 id = tp_channel_get_identifier (priv->channel);
1342 if (!EMP_STR_EMPTY (id))
1344 else if (priv->remote_contact)
1345 return empathy_contact_get_id (priv->remote_contact);
1352 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1354 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1356 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1357 g_return_val_if_fail (priv->ready, NULL);
1359 return priv->remote_contact;
1363 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1365 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1367 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1369 return priv->channel;
1373 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1375 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1377 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1379 return tp_channel_borrow_connection (priv->channel);
1383 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1385 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1387 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1393 empathy_tp_chat_send (EmpathyTpChat *chat,
1394 EmpathyMessage *message)
1396 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1397 const gchar *message_body;
1398 TpChannelTextMessageType message_type;
1400 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1401 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1402 g_return_if_fail (priv->ready);
1404 message_body = empathy_message_get_body (message);
1405 message_type = empathy_message_get_tptype (message);
1407 DEBUG ("Sending message: %s", message_body);
1408 tp_cli_channel_type_text_call_send (priv->channel, -1,
1412 g_object_ref (message),
1413 (GDestroyNotify) g_object_unref,
1418 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1419 TpChannelChatState state)
1421 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1423 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1424 g_return_if_fail (priv->ready);
1426 if (tp_proxy_has_interface_by_id (priv->channel,
1427 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1428 DEBUG ("Set state: %d", state);
1429 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1432 "setting chat state",
1440 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1442 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1444 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1445 g_return_val_if_fail (priv->ready, NULL);
1447 return priv->pending_messages_queue->head;
1451 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1452 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1454 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1455 priv->channel, -1, ids, tp_chat_async_cb,
1456 "acknowledging received message", NULL, G_OBJECT (chat));
1460 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1461 EmpathyMessage *message) {
1462 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1463 GArray *message_ids;
1467 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1468 g_return_if_fail (priv->ready);
1470 if (!empathy_message_is_incoming (message))
1473 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1475 id = empathy_message_get_id (message);
1476 g_array_append_val (message_ids, id);
1477 acknowledge_messages (chat, message_ids);
1478 g_array_free (message_ids, TRUE);
1481 m = g_queue_find (priv->pending_messages_queue, message);
1482 g_assert (m != NULL);
1483 g_queue_delete_link (priv->pending_messages_queue, m);
1484 g_object_unref (message);
1488 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1489 const GList *messages) {
1490 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1491 /* Copy messages as the messges list (probably is) our own */
1492 GList *msgs = g_list_copy ((GList *) messages);
1495 GArray *message_ids;
1497 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1498 g_return_if_fail (priv->ready);
1500 length = g_list_length ((GList *) messages);
1505 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1507 for (l = msgs; l != NULL; l = g_list_next (l)) {
1510 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1512 m = g_queue_find (priv->pending_messages_queue, message);
1513 g_assert (m != NULL);
1514 g_queue_delete_link (priv->pending_messages_queue, m);
1516 if (empathy_message_is_incoming (message)) {
1517 guint id = empathy_message_get_id (message);
1518 g_array_append_val (message_ids, id);
1520 g_object_unref (message);
1523 if (message_ids->len > 0)
1524 acknowledge_messages (chat, message_ids);
1526 g_array_free (message_ids, TRUE);
1531 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1533 EmpathyTpChatPriv *priv = GET_PRIV (self);
1535 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1539 provide_password_cb (TpChannel *channel,
1541 const GError *error,
1543 GObject *weak_object)
1545 GSimpleAsyncResult *result = user_data;
1547 if (error != NULL) {
1548 g_simple_async_result_set_from_error (result, error);
1550 else if (!correct) {
1551 /* The current D-Bus API is a bit weird so re-use the
1552 * AuthenticationFailed error */
1553 g_simple_async_result_set_error (result, TP_ERRORS,
1554 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1557 g_simple_async_result_complete (result);
1558 g_object_unref (result);
1562 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1563 const gchar *password,
1564 GAsyncReadyCallback callback,
1567 EmpathyTpChatPriv *priv = GET_PRIV (self);
1568 GSimpleAsyncResult *result;
1570 result = g_simple_async_result_new (G_OBJECT (self),
1571 callback, user_data, empathy_tp_chat_provide_password_finish);
1573 tp_cli_channel_interface_password_call_provide_password (priv->channel, -1,
1574 password, provide_password_cb, result, NULL, G_OBJECT (self));
1578 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1579 GAsyncResult *result,
1582 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1586 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1587 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);