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,
235 TpChannelTextMessageFlags flags)
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 empathy_message_set_incoming (message, incoming);
248 empathy_message_set_flags (message, flags);
250 g_queue_push_tail (priv->messages_queue, message);
252 if (from_handle == 0) {
253 empathy_message_set_sender (message, priv->user);
254 tp_chat_emit_queued_messages (chat);
256 empathy_tp_contact_factory_get_from_handle (priv->factory,
258 tp_chat_got_sender_cb,
259 message, NULL, G_OBJECT (chat));
264 tp_chat_received_cb (TpChannel *channel,
270 const gchar *message_body,
274 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
275 EmpathyTpChatPriv *priv = GET_PRIV (chat);
277 if (priv->channel == NULL)
280 if (priv->listing_pending_messages) {
284 DEBUG ("Message received: %s", message_body);
286 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
287 !tp_strdiff (message_body, "")) {
290 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
292 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
293 g_array_append_val (ids, message_id);
294 acknowledge_messages (chat, ids);
295 g_array_free (ids, TRUE);
300 tp_chat_build_message (chat,
311 tp_chat_sent_cb (TpChannel *channel,
314 const gchar *message_body,
318 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
319 EmpathyTpChatPriv *priv = GET_PRIV (chat);
321 if (priv->channel == NULL)
324 DEBUG ("Message sent: %s", message_body);
326 tp_chat_build_message (chat,
337 tp_chat_send_error_cb (TpChannel *channel,
341 const gchar *message_body,
345 EmpathyTpChatPriv *priv = GET_PRIV (chat);
347 if (priv->channel == NULL)
350 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
352 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
356 tp_chat_send_cb (TpChannel *proxy,
361 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
364 DEBUG ("Error: %s", error->message);
365 g_signal_emit (chat, signals[SEND_ERROR], 0,
366 empathy_message_get_body (message),
367 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
373 TpChannelChatState state;
377 tp_chat_state_changed_got_contact_cb (EmpathyTpContactFactory *factory,
378 EmpathyContact *contact,
383 TpChannelChatState state;
386 DEBUG ("Error: %s", error->message);
390 state = GPOINTER_TO_UINT (user_data);
391 DEBUG ("Chat state changed for %s (%d): %d",
392 empathy_contact_get_name (contact),
393 empathy_contact_get_handle (contact), state);
395 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
399 tp_chat_state_changed_cb (TpChannel *channel,
401 TpChannelChatState state,
405 EmpathyTpChatPriv *priv = GET_PRIV (chat);
407 empathy_tp_contact_factory_get_from_handle (priv->factory, handle,
408 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
413 tp_chat_list_pending_messages_cb (TpChannel *channel,
414 const GPtrArray *messages_list,
419 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
420 EmpathyTpChatPriv *priv = GET_PRIV (chat);
422 GArray *empty_non_text_content_ids = NULL;
424 priv->listing_pending_messages = FALSE;
426 if (priv->channel == NULL)
430 DEBUG ("Error listing pending messages: %s", error->message);
434 for (i = 0; i < messages_list->len; i++) {
435 GValueArray *message_struct;
436 const gchar *message_body;
443 message_struct = g_ptr_array_index (messages_list, i);
445 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
446 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
447 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
448 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
449 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
450 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
452 DEBUG ("Message pending: %s", message_body);
454 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
455 !tp_strdiff (message_body, "")) {
456 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
458 if (empty_non_text_content_ids == NULL) {
459 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
462 g_array_append_val (empty_non_text_content_ids, message_id);
466 tp_chat_build_message (chat,
476 if (empty_non_text_content_ids != NULL) {
477 acknowledge_messages (chat, empty_non_text_content_ids);
478 g_array_free (empty_non_text_content_ids, TRUE);
483 tp_chat_property_flags_changed_cb (TpProxy *proxy,
484 const GPtrArray *properties,
488 EmpathyTpChatPriv *priv = GET_PRIV (chat);
491 if (priv->channel == NULL)
494 if (!priv->had_properties_list || !properties) {
498 for (i = 0; i < properties->len; i++) {
499 GValueArray *prop_struct;
500 EmpathyTpChatProperty *property;
504 prop_struct = g_ptr_array_index (properties, i);
505 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
506 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
508 for (j = 0; j < priv->properties->len; j++) {
509 property = g_ptr_array_index (priv->properties, j);
510 if (property->id == id) {
511 property->flags = flags;
512 DEBUG ("property %s flags changed: %d",
513 property->name, property->flags);
521 tp_chat_properties_changed_cb (TpProxy *proxy,
522 const GPtrArray *properties,
526 EmpathyTpChatPriv *priv = GET_PRIV (chat);
529 if (priv->channel == NULL)
532 if (!priv->had_properties_list || !properties) {
536 for (i = 0; i < properties->len; i++) {
537 GValueArray *prop_struct;
538 EmpathyTpChatProperty *property;
542 prop_struct = g_ptr_array_index (properties, i);
543 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
544 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
546 for (j = 0; j < priv->properties->len; j++) {
547 property = g_ptr_array_index (priv->properties, j);
548 if (property->id == id) {
549 if (property->value) {
550 g_value_copy (src_value, property->value);
552 property->value = tp_g_value_slice_dup (src_value);
555 DEBUG ("property %s changed", property->name);
556 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
557 property->name, property->value);
565 tp_chat_get_properties_cb (TpProxy *proxy,
566 const GPtrArray *properties,
572 DEBUG ("Error getting properties: %s", error->message);
576 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
580 tp_chat_list_properties_cb (TpProxy *proxy,
581 const GPtrArray *properties,
586 EmpathyTpChatPriv *priv = GET_PRIV (chat);
590 if (priv->channel == NULL)
593 priv->had_properties_list = TRUE;
596 DEBUG ("Error listing properties: %s", error->message);
600 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
601 priv->properties = g_ptr_array_sized_new (properties->len);
602 for (i = 0; i < properties->len; i++) {
603 GValueArray *prop_struct;
604 EmpathyTpChatProperty *property;
606 prop_struct = g_ptr_array_index (properties, i);
607 property = g_slice_new0 (EmpathyTpChatProperty);
608 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
609 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
610 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
612 DEBUG ("Adding property name=%s id=%d flags=%d",
613 property->name, property->id, property->flags);
614 g_ptr_array_add (priv->properties, property);
615 if (property->flags & TP_PROPERTY_FLAG_READ) {
616 g_array_append_val (ids, property->id);
620 tp_cli_properties_interface_call_get_properties (proxy, -1,
622 tp_chat_get_properties_cb,
626 g_array_free (ids, TRUE);
630 empathy_tp_chat_set_property (EmpathyTpChat *chat,
634 EmpathyTpChatPriv *priv = GET_PRIV (chat);
635 EmpathyTpChatProperty *property;
638 if (!priv->had_properties_list) {
642 for (i = 0; i < priv->properties->len; i++) {
643 property = g_ptr_array_index (priv->properties, i);
644 if (!tp_strdiff (property->name, name)) {
645 GPtrArray *properties;
648 GValue dest_value = {0, };
650 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
654 g_value_init (&id, G_TYPE_UINT);
655 g_value_init (&dest_value, G_TYPE_VALUE);
656 g_value_set_uint (&id, property->id);
657 g_value_set_boxed (&dest_value, value);
659 prop = g_value_array_new (2);
660 g_value_array_append (prop, &id);
661 g_value_array_append (prop, &dest_value);
663 properties = g_ptr_array_sized_new (1);
664 g_ptr_array_add (properties, prop);
666 DEBUG ("Set property %s", name);
667 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
669 (tp_cli_properties_interface_callback_for_set_properties)
671 "Seting property", NULL,
674 g_ptr_array_free (properties, TRUE);
675 g_value_array_free (prop);
682 EmpathyTpChatProperty *
683 empathy_tp_chat_get_property (EmpathyTpChat *chat,
686 EmpathyTpChatPriv *priv = GET_PRIV (chat);
687 EmpathyTpChatProperty *property;
690 if (!priv->had_properties_list) {
694 for (i = 0; i < priv->properties->len; i++) {
695 property = g_ptr_array_index (priv->properties, i);
696 if (!tp_strdiff (property->name, name)) {
705 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
707 EmpathyTpChatPriv *priv = GET_PRIV (chat);
709 return priv->properties;
713 tp_chat_dispose (GObject *object)
715 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
716 EmpathyTpChatPriv *priv = GET_PRIV (self);
718 if (priv->dispose_has_run)
721 priv->dispose_has_run = TRUE;
723 if (priv->channel != NULL) {
724 g_signal_handlers_disconnect_by_func (priv->channel,
725 tp_chat_invalidated_cb, self);
726 g_object_unref (priv->channel);
728 priv->channel = NULL;
730 if (priv->remote_contact != NULL)
731 g_object_unref (priv->remote_contact);
732 priv->remote_contact = NULL;
734 if (priv->factory != NULL)
735 g_object_unref (priv->factory);
736 priv->factory = NULL;
738 if (priv->user != NULL)
739 g_object_unref (priv->user);
742 if (priv->contact_monitor)
743 g_object_unref (priv->contact_monitor);
744 priv->contact_monitor = NULL;
746 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
747 g_queue_clear (priv->messages_queue);
749 g_queue_foreach (priv->pending_messages_queue,
750 (GFunc) g_object_unref, NULL);
751 g_queue_clear (priv->pending_messages_queue);
753 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
754 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
758 tp_chat_finalize (GObject *object)
760 EmpathyTpChatPriv *priv = GET_PRIV (object);
763 DEBUG ("Finalize: %p", object);
765 if (priv->properties) {
766 for (i = 0; i < priv->properties->len; i++) {
767 EmpathyTpChatProperty *property;
769 property = g_ptr_array_index (priv->properties, i);
770 g_free (property->name);
771 if (property->value) {
772 tp_g_value_slice_free (property->value);
774 g_slice_free (EmpathyTpChatProperty, property);
776 g_ptr_array_free (priv->properties, TRUE);
779 g_queue_free (priv->messages_queue);
780 g_queue_free (priv->pending_messages_queue);
782 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
786 tp_chat_check_if_ready (EmpathyTpChat *chat)
788 EmpathyTpChatPriv *priv = GET_PRIV (chat);
793 if (priv->user == NULL)
796 if (!priv->got_password_flags)
799 /* We need either the members (room) or the remote contact (private chat).
800 * If the chat is protected by a password we can't get these information so
801 * consider the chat as ready so it can be presented to the user. */
802 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
803 priv->remote_contact == NULL)
808 tp_cli_channel_type_text_connect_to_received (priv->channel,
811 G_OBJECT (chat), NULL);
812 priv->listing_pending_messages = TRUE;
813 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
815 tp_chat_list_pending_messages_cb,
819 tp_cli_channel_type_text_connect_to_sent (priv->channel,
822 G_OBJECT (chat), NULL);
823 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
824 tp_chat_send_error_cb,
826 G_OBJECT (chat), NULL);
827 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
828 tp_chat_state_changed_cb,
830 G_OBJECT (chat), NULL);
832 g_object_notify (G_OBJECT (chat), "ready");
836 tp_chat_update_remote_contact (EmpathyTpChat *chat)
838 EmpathyTpChatPriv *priv = GET_PRIV (chat);
839 EmpathyContact *contact = NULL;
840 TpHandle self_handle;
841 TpHandleType handle_type;
844 /* If this is a named chatroom, never pretend it is a private chat */
845 tp_channel_get_handle (priv->channel, &handle_type);
846 if (handle_type == TP_HANDLE_TYPE_ROOM) {
850 /* This is an MSN-like chat where anyone can join the chat at anytime.
851 * If there is only one non-self contact member, we are in a private
852 * chat and we set the "remote-contact" property to that contact. If
853 * there are more, set the "remote-contact" property to NULL and the
854 * UI will display a contact list. */
855 self_handle = tp_channel_group_get_self_handle (priv->channel);
856 for (l = priv->members; l; l = l->next) {
857 /* Skip self contact if member */
858 if (empathy_contact_get_handle (l->data) == self_handle) {
862 /* We have more than one remote contact, break */
863 if (contact != NULL) {
868 /* If we didn't find yet a remote contact, keep this one */
872 if (priv->remote_contact == contact) {
876 DEBUG ("Changing remote contact from %p to %p",
877 priv->remote_contact, contact);
879 if (priv->remote_contact) {
880 g_object_unref (priv->remote_contact);
883 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
884 g_object_notify (G_OBJECT (chat), "remote-contact");
888 tp_chat_got_added_contacts_cb (EmpathyTpContactFactory *factory,
890 EmpathyContact * const * contacts,
892 const TpHandle *failed,
897 EmpathyTpChatPriv *priv = GET_PRIV (chat);
899 const TpIntSet *members;
901 EmpathyContact *contact;
904 DEBUG ("Error: %s", error->message);
908 members = tp_channel_group_get_members (priv->channel);
909 for (i = 0; i < n_contacts; i++) {
910 contact = contacts[i];
911 handle = empathy_contact_get_handle (contact);
913 /* Make sure the contact is still member */
914 if (tp_intset_is_member (members, handle)) {
915 priv->members = g_list_prepend (priv->members,
916 g_object_ref (contact));
917 g_signal_emit_by_name (chat, "members-changed",
918 contact, NULL, 0, NULL, TRUE);
922 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
923 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
926 static EmpathyContact *
927 chat_lookup_contact (EmpathyTpChat *chat,
931 EmpathyTpChatPriv *priv = GET_PRIV (chat);
934 for (l = priv->members; l; l = l->next) {
935 EmpathyContact *c = l->data;
937 if (empathy_contact_get_handle (c) != handle) {
942 /* Caller takes the reference. */
943 priv->members = g_list_delete_link (priv->members, l);
961 static ContactRenameData *
962 contact_rename_data_new (TpHandle handle,
964 const gchar* message)
966 ContactRenameData *data = g_new (ContactRenameData, 1);
967 data->old_handle = handle;
968 data->reason = reason;
969 data->message = g_strdup (message);
975 contact_rename_data_free (ContactRenameData* data)
977 g_free (data->message);
982 tp_chat_got_renamed_contacts_cb (EmpathyTpContactFactory *factory,
984 EmpathyContact * const * contacts,
986 const TpHandle *failed,
991 EmpathyTpChatPriv *priv = GET_PRIV (chat);
992 const TpIntSet *members;
994 EmpathyContact *old = NULL, *new = NULL;
995 ContactRenameData *rename_data = (ContactRenameData *) user_data;
998 DEBUG ("Error: %s", error->message);
1002 /* renamed members can only be delivered one at a time */
1003 g_warn_if_fail (n_contacts == 1);
1007 members = tp_channel_group_get_members (priv->channel);
1008 handle = empathy_contact_get_handle (new);
1010 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1011 rename_data->old_handle, TRUE);
1013 /* Make sure the contact is still member */
1014 if (tp_intset_is_member (members, handle)) {
1015 priv->members = g_list_prepend (priv->members,
1016 g_object_ref (new));
1019 g_signal_emit_by_name (chat, "member-renamed",
1020 old, new, rename_data->reason,
1021 rename_data->message);
1022 g_object_unref (old);
1026 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1027 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1032 tp_chat_group_members_changed_cb (TpChannel *self,
1036 GArray *local_pending,
1037 GArray *remote_pending,
1040 EmpathyTpChat *chat)
1042 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1043 EmpathyContact *contact;
1044 EmpathyContact *actor_contact = NULL;
1046 ContactRenameData *rename_data;
1047 TpHandle old_handle;
1049 /* Contact renamed */
1050 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1051 /* there can only be a single 'added' and a single 'removed' handle */
1052 g_warn_if_fail (removed->len == 1);
1053 g_warn_if_fail (added->len == 1);
1055 old_handle = g_array_index (removed, guint, 0);
1057 rename_data = contact_rename_data_new (old_handle, reason, message);
1058 empathy_tp_contact_factory_get_from_handles (priv->factory,
1059 added->len, (TpHandle *) added->data,
1060 tp_chat_got_renamed_contacts_cb,
1061 rename_data, (GDestroyNotify) contact_rename_data_free,
1067 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1068 if (actor_contact == NULL) {
1069 /* FIXME: handle this a tad more gracefully: perhaps
1070 * the actor was a server op. We could use the
1071 * contact-ids detail of MembersChangedDetailed.
1073 DEBUG ("actor %u not a channel member", actor);
1077 /* Remove contacts that are not members anymore */
1078 for (i = 0; i < removed->len; i++) {
1079 contact = chat_lookup_contact (chat,
1080 g_array_index (removed, TpHandle, i), TRUE);
1082 if (contact != NULL) {
1083 g_signal_emit_by_name (chat, "members-changed", contact,
1084 actor_contact, reason, message,
1086 g_object_unref (contact);
1090 /* Request added contacts */
1091 if (added->len > 0) {
1092 empathy_tp_contact_factory_get_from_handles (priv->factory,
1093 added->len, (TpHandle *) added->data,
1094 tp_chat_got_added_contacts_cb, NULL, NULL,
1098 tp_chat_update_remote_contact (chat);
1100 if (actor_contact != NULL) {
1101 g_object_unref (actor_contact);
1106 tp_chat_got_remote_contact_cb (EmpathyTpContactFactory *factory,
1107 EmpathyContact *contact,
1108 const GError *error,
1112 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1115 DEBUG ("Error: %s", error->message);
1116 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1120 priv->remote_contact = g_object_ref (contact);
1121 g_object_notify (chat, "remote-contact");
1123 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1127 tp_chat_got_self_contact_cb (EmpathyTpContactFactory *factory,
1128 EmpathyContact *contact,
1129 const GError *error,
1133 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1136 DEBUG ("Error: %s", error->message);
1137 empathy_tp_chat_close (EMPATHY_TP_CHAT (chat));
1141 priv->user = g_object_ref (contact);
1142 empathy_contact_set_is_user (priv->user, TRUE);
1143 tp_chat_check_if_ready (EMPATHY_TP_CHAT (chat));
1147 password_flags_changed_cb (TpChannel *channel,
1151 GObject *weak_object)
1153 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1154 EmpathyTpChatPriv *priv = GET_PRIV (self);
1155 gboolean was_needed, needed;
1157 was_needed = empathy_tp_chat_password_needed (self);
1159 priv->password_flags |= added;
1160 priv->password_flags ^= removed;
1162 needed = empathy_tp_chat_password_needed (self);
1164 if (was_needed != needed)
1165 g_object_notify (G_OBJECT (self), "password-needed");
1169 got_password_flags_cb (TpChannel *proxy,
1170 guint password_flags,
1171 const GError *error,
1173 GObject *weak_object)
1175 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1176 EmpathyTpChatPriv *priv = GET_PRIV (self);
1178 priv->got_password_flags = TRUE;
1179 priv->password_flags = password_flags;
1181 tp_chat_check_if_ready (EMPATHY_TP_CHAT (self));
1185 tp_chat_constructor (GType type,
1187 GObjectConstructParam *props)
1190 EmpathyTpChatPriv *priv;
1191 TpConnection *connection;
1194 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1196 priv = GET_PRIV (chat);
1198 connection = tp_channel_borrow_connection (priv->channel);
1199 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
1200 g_signal_connect (priv->channel, "invalidated",
1201 G_CALLBACK (tp_chat_invalidated_cb),
1204 if (tp_proxy_has_interface_by_id (priv->channel,
1205 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1206 const TpIntSet *members;
1209 /* Get self contact from the group's self handle */
1210 handle = tp_channel_group_get_self_handle (priv->channel);
1211 empathy_tp_contact_factory_get_from_handle (priv->factory,
1212 handle, tp_chat_got_self_contact_cb,
1215 /* Get initial member contacts */
1216 members = tp_channel_group_get_members (priv->channel);
1217 handles = tp_intset_to_array (members);
1218 empathy_tp_contact_factory_get_from_handles (priv->factory,
1219 handles->len, (TpHandle *) handles->data,
1220 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1222 g_signal_connect (priv->channel, "group-members-changed",
1223 G_CALLBACK (tp_chat_group_members_changed_cb), chat);
1225 /* Get the self contact from the connection's self handle */
1226 handle = tp_connection_get_self_handle (connection);
1227 empathy_tp_contact_factory_get_from_handle (priv->factory,
1228 handle, tp_chat_got_self_contact_cb,
1231 /* Get the remote contact */
1232 handle = tp_channel_get_handle (priv->channel, NULL);
1233 empathy_tp_contact_factory_get_from_handle (priv->factory,
1234 handle, tp_chat_got_remote_contact_cb,
1238 if (tp_proxy_has_interface_by_id (priv->channel,
1239 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1240 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1241 tp_chat_list_properties_cb,
1244 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1245 tp_chat_properties_changed_cb,
1247 G_OBJECT (chat), NULL);
1248 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1249 tp_chat_property_flags_changed_cb,
1251 G_OBJECT (chat), NULL);
1254 /* Check if the chat is password protected */
1255 if (tp_proxy_has_interface_by_id (priv->channel,
1256 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1257 priv->got_password_flags = FALSE;
1259 tp_cli_channel_interface_password_connect_to_password_flags_changed
1260 (priv->channel, password_flags_changed_cb, chat, NULL,
1261 G_OBJECT (chat), NULL);
1263 tp_cli_channel_interface_password_call_get_password_flags
1264 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1266 /* No Password interface, so no need to fetch the password flags */
1267 priv->got_password_flags = TRUE;
1274 tp_chat_get_property (GObject *object,
1279 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1280 EmpathyTpChatPriv *priv = GET_PRIV (object);
1284 g_value_set_object (value, priv->channel);
1286 case PROP_REMOTE_CONTACT:
1287 g_value_set_object (value, priv->remote_contact);
1290 g_value_set_boolean (value, priv->ready);
1292 case PROP_PASSWORD_NEEDED:
1293 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1302 tp_chat_set_property (GObject *object,
1304 const GValue *value,
1307 EmpathyTpChatPriv *priv = GET_PRIV (object);
1311 priv->channel = g_value_dup_object (value);
1314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1320 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1322 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1324 object_class->dispose = tp_chat_dispose;
1325 object_class->finalize = tp_chat_finalize;
1326 object_class->constructor = tp_chat_constructor;
1327 object_class->get_property = tp_chat_get_property;
1328 object_class->set_property = tp_chat_set_property;
1330 g_object_class_install_property (object_class,
1332 g_param_spec_object ("channel",
1333 "telepathy channel",
1334 "The text channel for the chat",
1337 G_PARAM_CONSTRUCT_ONLY));
1339 g_object_class_install_property (object_class,
1340 PROP_REMOTE_CONTACT,
1341 g_param_spec_object ("remote-contact",
1342 "The remote contact",
1343 "The remote contact if there is no group iface on the channel",
1344 EMPATHY_TYPE_CONTACT,
1347 g_object_class_install_property (object_class,
1349 g_param_spec_boolean ("ready",
1350 "Is the object ready",
1351 "This object can't be used until this becomes true",
1355 g_object_class_install_property (object_class,
1356 PROP_PASSWORD_NEEDED,
1357 g_param_spec_boolean ("password-needed",
1359 "TRUE if a password is needed to join the channel",
1364 signals[MESSAGE_RECEIVED] =
1365 g_signal_new ("message-received",
1366 G_TYPE_FROM_CLASS (klass),
1370 g_cclosure_marshal_VOID__OBJECT,
1372 1, EMPATHY_TYPE_MESSAGE);
1374 signals[SEND_ERROR] =
1375 g_signal_new ("send-error",
1376 G_TYPE_FROM_CLASS (klass),
1380 _empathy_marshal_VOID__STRING_UINT,
1382 2, G_TYPE_STRING, G_TYPE_UINT);
1384 signals[CHAT_STATE_CHANGED] =
1385 g_signal_new ("chat-state-changed",
1386 G_TYPE_FROM_CLASS (klass),
1390 _empathy_marshal_VOID__OBJECT_UINT,
1392 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1394 signals[PROPERTY_CHANGED] =
1395 g_signal_new ("property-changed",
1396 G_TYPE_FROM_CLASS (klass),
1400 _empathy_marshal_VOID__STRING_BOXED,
1402 2, G_TYPE_STRING, G_TYPE_VALUE);
1405 g_signal_new ("destroy",
1406 G_TYPE_FROM_CLASS (klass),
1410 g_cclosure_marshal_VOID__VOID,
1414 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1418 empathy_tp_chat_init (EmpathyTpChat *chat)
1420 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1421 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1424 priv->contact_monitor = NULL;
1425 priv->messages_queue = g_queue_new ();
1426 priv->pending_messages_queue = g_queue_new ();
1430 tp_chat_iface_init (EmpathyContactListIface *iface)
1432 iface->add = tp_chat_add;
1433 iface->remove = tp_chat_remove;
1434 iface->get_members = tp_chat_get_members;
1435 iface->get_monitor = tp_chat_get_monitor;
1439 empathy_tp_chat_new (TpChannel *channel)
1441 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1447 empathy_tp_chat_close (EmpathyTpChat *chat) {
1448 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1450 /* If there are still messages left, it'll come back..
1451 * We loose the ordering of sent messages though */
1452 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1453 "closing channel", NULL, NULL);
1457 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1459 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1463 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1465 id = tp_channel_get_identifier (priv->channel);
1466 if (!EMP_STR_EMPTY (id))
1468 else if (priv->remote_contact)
1469 return empathy_contact_get_id (priv->remote_contact);
1476 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1478 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1480 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1481 g_return_val_if_fail (priv->ready, NULL);
1483 return priv->remote_contact;
1487 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1489 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1491 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1493 return priv->channel;
1497 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1499 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1501 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1503 return tp_channel_borrow_connection (priv->channel);
1507 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1509 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1511 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1517 empathy_tp_chat_send (EmpathyTpChat *chat,
1518 EmpathyMessage *message)
1520 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1521 const gchar *message_body;
1522 TpChannelTextMessageType message_type;
1524 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1525 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1526 g_return_if_fail (priv->ready);
1528 message_body = empathy_message_get_body (message);
1529 message_type = empathy_message_get_tptype (message);
1531 DEBUG ("Sending message: %s", message_body);
1532 tp_cli_channel_type_text_call_send (priv->channel, -1,
1536 g_object_ref (message),
1537 (GDestroyNotify) g_object_unref,
1542 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1543 TpChannelChatState state)
1545 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1547 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1548 g_return_if_fail (priv->ready);
1550 if (tp_proxy_has_interface_by_id (priv->channel,
1551 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1552 DEBUG ("Set state: %d", state);
1553 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1556 "setting chat state",
1564 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1566 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1568 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1569 g_return_val_if_fail (priv->ready, NULL);
1571 return priv->pending_messages_queue->head;
1575 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1576 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1578 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1579 priv->channel, -1, ids, tp_chat_async_cb,
1580 "acknowledging received message", NULL, G_OBJECT (chat));
1584 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1585 EmpathyMessage *message) {
1586 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1587 GArray *message_ids;
1591 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1592 g_return_if_fail (priv->ready);
1594 if (!empathy_message_is_incoming (message))
1597 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1599 id = empathy_message_get_id (message);
1600 g_array_append_val (message_ids, id);
1601 acknowledge_messages (chat, message_ids);
1602 g_array_free (message_ids, TRUE);
1605 m = g_queue_find (priv->pending_messages_queue, message);
1606 g_assert (m != NULL);
1607 g_queue_delete_link (priv->pending_messages_queue, m);
1608 g_object_unref (message);
1612 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1613 const GList *messages) {
1614 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1615 /* Copy messages as the messges list (probably is) our own */
1616 GList *msgs = g_list_copy ((GList *) messages);
1619 GArray *message_ids;
1621 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1622 g_return_if_fail (priv->ready);
1624 length = g_list_length ((GList *) messages);
1629 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1631 for (l = msgs; l != NULL; l = g_list_next (l)) {
1634 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1636 m = g_queue_find (priv->pending_messages_queue, message);
1637 g_assert (m != NULL);
1638 g_queue_delete_link (priv->pending_messages_queue, m);
1640 if (empathy_message_is_incoming (message)) {
1641 guint id = empathy_message_get_id (message);
1642 g_array_append_val (message_ids, id);
1644 g_object_unref (message);
1647 if (message_ids->len > 0)
1648 acknowledge_messages (chat, message_ids);
1650 g_array_free (message_ids, TRUE);
1655 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1657 EmpathyTpChatPriv *priv = GET_PRIV (self);
1659 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1663 provide_password_cb (TpChannel *channel,
1665 const GError *error,
1667 GObject *weak_object)
1669 GSimpleAsyncResult *result = user_data;
1671 if (error != NULL) {
1672 g_simple_async_result_set_from_error (result, error);
1674 else if (!correct) {
1675 /* The current D-Bus API is a bit weird so re-use the
1676 * AuthenticationFailed error */
1677 g_simple_async_result_set_error (result, TP_ERRORS,
1678 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1681 g_simple_async_result_complete (result);
1682 g_object_unref (result);
1686 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1687 const gchar *password,
1688 GAsyncReadyCallback callback,
1691 EmpathyTpChatPriv *priv = GET_PRIV (self);
1692 GSimpleAsyncResult *result;
1694 result = g_simple_async_result_new (G_OBJECT (self),
1695 callback, user_data,
1696 empathy_tp_chat_provide_password_finish);
1698 tp_cli_channel_interface_password_call_provide_password
1699 (priv->channel, -1, password, provide_password_cb, result,
1700 NULL, G_OBJECT (self));
1704 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1705 GAsyncResult *result,
1708 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1712 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1713 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);