2 * Copyright (C) 2007-2012 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <telepathy-glib/telepathy-glib.h>
27 #include <extensions/extensions.h>
29 #include "empathy-tp-chat.h"
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-contact-list.h"
32 #include "empathy-request-util.h"
33 #include "empathy-time.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
37 #include "empathy-debug.h"
39 struct _EmpathyTpChatPrivate
43 EmpathyContact *remote_contact;
45 /* Queue of messages not signalled yet */
46 GQueue *messages_queue;
47 /* Queue of messages signalled but not acked yet */
48 GQueue *pending_messages_queue;
51 gboolean supports_subject;
52 gboolean can_set_subject;
56 /* Room config (for now, we only track the title and don't support
60 gboolean can_upgrade_to_muc;
62 GHashTable *messages_being_sent;
64 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
65 GSimpleAsyncResult *ready_result;
68 static void tp_chat_iface_init (EmpathyContactListIface *iface);
76 PROP_N_MESSAGES_SENDING,
90 static guint signals[LAST_SIGNAL];
92 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
93 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
97 tp_chat_set_delivery_status (EmpathyTpChat *self,
99 EmpathyDeliveryStatus delivery_status)
101 TpDeliveryReportingSupportFlags flags =
102 tp_text_channel_get_delivery_reporting_support (
103 TP_TEXT_CHANNEL (self));
105 /* channel must support receiving failures and successes */
106 if (!tp_str_empty (token) &&
107 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
108 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
110 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
112 switch (delivery_status)
114 case EMPATHY_DELIVERY_STATUS_NONE:
115 g_hash_table_remove (self->priv->messages_being_sent,
120 g_hash_table_insert (self->priv->messages_being_sent,
122 GUINT_TO_POINTER (delivery_status));
126 g_object_notify (G_OBJECT (self), "n-messages-sending");
130 static void tp_chat_prepare_ready_async (TpProxy *proxy,
131 const TpProxyFeature *feature,
132 GAsyncReadyCallback callback,
136 tp_chat_async_cb (TpChannel *proxy,
139 GObject *weak_object)
142 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
146 create_conference_cb (GObject *source,
147 GAsyncResult *result,
150 GError *error = NULL;
152 if (!tp_account_channel_request_create_channel_finish (
153 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
155 DEBUG ("Failed to create conference channel: %s", error->message);
156 g_error_free (error);
161 empathy_tp_chat_add (EmpathyTpChat *self,
162 EmpathyContact *contact,
163 const gchar *message)
165 TpChannel *channel = (TpChannel *) self;
167 if (tp_proxy_has_interface_by_id (self,
168 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
171 GArray handles = {(gchar *) &handle, 1};
173 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
175 handle = empathy_contact_get_handle (contact);
176 tp_cli_channel_interface_group_call_add_members (channel,
177 -1, &handles, NULL, NULL, NULL, NULL, NULL);
179 else if (self->priv->can_upgrade_to_muc)
181 TpAccountChannelRequest *req;
183 const char *object_path;
184 GPtrArray channels = { (gpointer *) &object_path, 1 };
185 const char *invitees[2] = { NULL, };
187 invitees[0] = empathy_contact_get_id (contact);
188 object_path = tp_proxy_get_object_path (self);
191 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
192 TP_IFACE_CHANNEL_TYPE_TEXT,
193 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
195 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
196 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
197 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
198 G_TYPE_STRV, invitees,
199 /* FIXME: InvitationMessage ? */
202 req = tp_account_channel_request_new (self->priv->account, props,
203 TP_USER_ACTION_TIME_NOT_USER_ACTION);
205 /* Although this is a MUC, it's anonymous, so CreateChannel is
207 tp_account_channel_request_create_channel_async (req,
208 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
210 g_object_unref (req);
211 g_hash_table_unref (props);
215 g_warning ("Cannot add to this channel");
220 tp_chat_remove (EmpathyContactList *list,
221 EmpathyContact *contact,
222 const gchar *message)
224 EmpathyTpChat *self = (EmpathyTpChat *) list;
226 GArray handles = {(gchar *) &handle, 1};
228 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
229 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
231 handle = empathy_contact_get_handle (contact);
232 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
233 &handles, NULL, NULL, NULL, NULL, NULL);
237 tp_chat_get_members (EmpathyContactList *list)
239 EmpathyTpChat *self = (EmpathyTpChat *) list;
240 GList *members = NULL;
242 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
244 if (self->priv->members)
246 members = g_list_copy (self->priv->members);
247 g_list_foreach (members, (GFunc) g_object_ref, NULL);
251 members = g_list_prepend (members, g_object_ref (self->priv->user));
253 if (self->priv->remote_contact != NULL)
254 members = g_list_prepend (members,
255 g_object_ref (self->priv->remote_contact));
262 check_ready (EmpathyTpChat *self)
264 if (self->priv->ready_result == NULL)
267 if (g_queue_get_length (self->priv->messages_queue) > 0)
272 g_simple_async_result_complete (self->priv->ready_result);
273 tp_clear_object (&self->priv->ready_result);
277 tp_chat_emit_queued_messages (EmpathyTpChat *self)
279 EmpathyMessage *message;
281 /* Check if we can now emit some queued messages */
282 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL)
284 if (empathy_message_get_sender (message) == NULL)
287 DEBUG ("Queued message ready");
288 g_queue_pop_head (self->priv->messages_queue);
289 g_queue_push_tail (self->priv->pending_messages_queue, message);
290 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
297 tp_chat_got_sender_cb (TpConnection *connection,
298 EmpathyContact *contact,
303 EmpathyTpChat *self = (EmpathyTpChat *) chat;
307 DEBUG ("Error: %s", error->message);
308 /* Do not block the message queue, just drop this message */
309 g_queue_remove (self->priv->messages_queue, message);
313 empathy_message_set_sender (message, contact);
316 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
320 tp_chat_build_message (EmpathyTpChat *self,
324 EmpathyMessage *message;
327 message = empathy_message_new_from_tp_message (msg, incoming);
328 /* FIXME: this is actually a lie for incoming messages. */
329 empathy_message_set_receiver (message, self->priv->user);
331 g_queue_push_tail (self->priv->messages_queue, message);
333 sender = tp_signalled_message_get_sender (msg);
334 g_assert (sender != NULL);
336 if (tp_contact_get_handle (sender) == 0)
338 empathy_message_set_sender (message, self->priv->user);
339 tp_chat_emit_queued_messages (self);
343 TpConnection *connection = tp_channel_borrow_connection (
346 empathy_tp_contact_factory_get_from_handle (connection,
347 tp_contact_get_handle (sender),
348 tp_chat_got_sender_cb,
349 message, NULL, G_OBJECT (self));
354 handle_delivery_report (EmpathyTpChat *self,
357 TpDeliveryStatus delivery_status;
358 const GHashTable *header;
359 TpChannelTextSendError delivery_error;
362 const gchar *message_body = NULL;
363 const gchar *delivery_dbus_error;
364 const gchar *delivery_token = NULL;
366 header = tp_message_peek (message, 0);
370 delivery_token = tp_asv_get_string (header, "delivery-token");
371 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
377 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
379 DEBUG ("Accepted %s", delivery_token);
380 tp_chat_set_delivery_status (self, delivery_token,
381 EMPATHY_DELIVERY_STATUS_ACCEPTED);
384 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
386 DEBUG ("Delivered %s", delivery_token);
387 tp_chat_set_delivery_status (self, delivery_token,
388 EMPATHY_DELIVERY_STATUS_NONE);
391 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
392 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
397 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
399 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
401 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
403 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
404 * TpMessage. (fdo #35884) */
405 echo = tp_asv_get_boxed (header, "delivery-echo",
406 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
407 if (echo != NULL && echo->len >= 2)
409 const GHashTable *echo_body;
411 echo_body = g_ptr_array_index (echo, 1);
412 if (echo_body != NULL)
413 message_body = tp_asv_get_string (echo_body, "content");
416 tp_chat_set_delivery_status (self, delivery_token,
417 EMPATHY_DELIVERY_STATUS_NONE);
418 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
419 delivery_error, delivery_dbus_error);
422 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
423 message, NULL, NULL);
427 handle_incoming_message (EmpathyTpChat *self,
433 if (tp_message_is_delivery_report (message))
435 handle_delivery_report (self, message);
439 message_body = tp_message_to_text (message, NULL);
441 DEBUG ("Message %s (channel %s): %s",
442 pending ? "pending" : "received",
443 tp_proxy_get_object_path (self), message_body);
445 if (message_body == NULL)
447 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
449 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
450 message, NULL, NULL);
454 tp_chat_build_message (self, message, TRUE);
456 g_free (message_body);
460 message_received_cb (TpTextChannel *channel,
464 handle_incoming_message (self, message, FALSE);
468 find_pending_message_func (gconstpointer a,
471 EmpathyMessage *msg = (EmpathyMessage *) a;
472 TpMessage *message = (TpMessage *) b;
474 if (empathy_message_get_tp_message (msg) == message)
481 pending_message_removed_cb (TpTextChannel *channel,
487 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
488 find_pending_message_func);
493 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
495 g_object_unref (m->data);
496 g_queue_delete_link (self->priv->pending_messages_queue, m);
500 message_sent_cb (TpTextChannel *channel,
502 TpMessageSendingFlags flags,
508 message_body = tp_message_to_text (message, NULL);
510 DEBUG ("Message sent: %s", message_body);
512 tp_chat_build_message (self, message, FALSE);
514 g_free (message_body);
517 static TpChannelTextSendError
518 error_to_text_send_error (GError *error)
520 if (error->domain != TP_ERRORS)
521 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
525 case TP_ERROR_OFFLINE:
526 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
527 case TP_ERROR_INVALID_HANDLE:
528 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
529 case TP_ERROR_PERMISSION_DENIED:
530 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
531 case TP_ERROR_NOT_IMPLEMENTED:
532 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
535 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
539 message_send_cb (GObject *source,
540 GAsyncResult *result,
543 EmpathyTpChat *self = user_data;
544 TpTextChannel *channel = (TpTextChannel *) source;
546 GError *error = NULL;
548 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
550 DEBUG ("Error: %s", error->message);
552 /* FIXME: we should use the body of the message as first argument of the
553 * signal but can't easily get it as we just get a user_data pointer. Once
554 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
555 * to use the user_data pointer to pass the message and fix this. */
556 g_signal_emit (self, signals[SEND_ERROR], 0,
557 NULL, error_to_text_send_error (error), NULL);
559 g_error_free (error);
562 tp_chat_set_delivery_status (self, token,
563 EMPATHY_DELIVERY_STATUS_SENDING);
569 TpChannelChatState state;
573 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
574 EmpathyContact *contact,
579 TpChannelChatState state;
583 DEBUG ("Error: %s", error->message);
587 state = GPOINTER_TO_UINT (user_data);
588 DEBUG ("Chat state changed for %s (%d): %d",
589 empathy_contact_get_alias (contact),
590 empathy_contact_get_handle (contact), state);
592 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
596 tp_chat_state_changed_cb (TpChannel *channel,
598 TpChannelChatState state,
601 TpConnection *connection = tp_channel_borrow_connection (
604 empathy_tp_contact_factory_get_from_handle (connection, handle,
605 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
606 NULL, G_OBJECT (self));
610 list_pending_messages (EmpathyTpChat *self)
614 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
616 for (l = messages; l != NULL; l = g_list_next (l))
618 TpMessage *message = l->data;
620 handle_incoming_message (self, message, FALSE);
623 g_list_free (messages);
627 update_subject (EmpathyTpChat *self,
628 GHashTable *properties)
630 gboolean can_set, valid;
631 const gchar *subject;
633 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
635 self->priv->can_set_subject = can_set;
637 subject = tp_asv_get_string (properties, "Subject");
642 g_free (self->priv->subject);
643 self->priv->subject = g_strdup (subject);
645 /* If the actor is included with this update, use it;
646 * otherwise, clear it to avoid showing stale information.
647 * Why might it not be included? When you join an IRC channel,
648 * you get a pair of messages: first, the current topic; next,
649 * who set it, and when. Idle reports these in two separate
652 actor = tp_asv_get_string (properties, "Actor");
653 g_free (self->priv->subject_actor);
654 self->priv->subject_actor = g_strdup (actor);
656 g_object_notify (G_OBJECT (self), "subject");
659 /* TODO: track Timestamp. */
663 tp_chat_get_all_subject_cb (TpProxy *proxy,
664 GHashTable *properties,
666 gpointer user_data G_GNUC_UNUSED,
669 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
673 DEBUG ("Error fetching subject: %s", error->message);
677 self->priv->supports_subject = TRUE;
678 update_subject (self, properties);
682 update_title (EmpathyTpChat *self,
683 GHashTable *properties)
685 const gchar *title = tp_asv_get_string (properties, "Title");
689 if (tp_str_empty (title))
692 g_free (self->priv->title);
693 self->priv->title = g_strdup (title);
694 g_object_notify (G_OBJECT (self), "title");
699 tp_chat_get_all_room_config_cb (TpProxy *proxy,
700 GHashTable *properties,
702 gpointer user_data G_GNUC_UNUSED,
705 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
709 DEBUG ("Error fetching room config: %s", error->message);
713 update_title (self, properties);
717 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
718 const gchar *interface_name,
720 const gchar **invalidated,
724 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
726 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
727 update_subject (self, changed);
729 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
730 update_title (self, changed);
734 empathy_tp_chat_set_subject (EmpathyTpChat *self,
735 const gchar *subject)
737 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
738 subject, tp_chat_async_cb, "while setting subject", NULL,
743 empathy_tp_chat_get_title (EmpathyTpChat *self)
745 return self->priv->title;
749 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
751 return self->priv->supports_subject;
755 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
757 return self->priv->can_set_subject;
761 empathy_tp_chat_get_subject (EmpathyTpChat *self)
763 return self->priv->subject;
767 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
769 return self->priv->subject_actor;
773 tp_chat_dispose (GObject *object)
775 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
777 tp_clear_object (&self->priv->account);
778 tp_clear_object (&self->priv->remote_contact);
779 tp_clear_object (&self->priv->user);
781 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
782 g_queue_clear (self->priv->messages_queue);
784 g_queue_foreach (self->priv->pending_messages_queue,
785 (GFunc) g_object_unref, NULL);
786 g_queue_clear (self->priv->pending_messages_queue);
788 tp_clear_object (&self->priv->ready_result);
790 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
791 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
795 tp_chat_finalize (GObject *object)
797 EmpathyTpChat *self = (EmpathyTpChat *) object;
799 DEBUG ("Finalize: %p", object);
801 g_queue_free (self->priv->messages_queue);
802 g_queue_free (self->priv->pending_messages_queue);
803 g_hash_table_unref (self->priv->messages_being_sent);
805 g_free (self->priv->title);
806 g_free (self->priv->subject);
807 g_free (self->priv->subject_actor);
809 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
813 check_almost_ready (EmpathyTpChat *self)
815 TpChannel *channel = (TpChannel *) self;
817 if (self->priv->ready_result == NULL)
820 if (self->priv->user == NULL)
823 /* We need either the members (room) or the remote contact (private chat).
824 * If the chat is protected by a password we can't get these information so
825 * consider the chat as ready so it can be presented to the user. */
826 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
827 self->priv->remote_contact == NULL)
830 g_assert (tp_proxy_is_prepared (self,
831 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
833 tp_g_signal_connect_object (self, "message-received",
834 G_CALLBACK (message_received_cb), self, 0);
835 tp_g_signal_connect_object (self, "pending-message-removed",
836 G_CALLBACK (pending_message_removed_cb), self, 0);
838 list_pending_messages (self);
840 tp_g_signal_connect_object (self, "message-sent",
841 G_CALLBACK (message_sent_cb), self, 0);
843 tp_g_signal_connect_object (self, "chat-state-changed",
844 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
850 tp_chat_got_added_contacts_cb (TpConnection *connection,
852 EmpathyContact * const * contacts,
854 const TpHandle *failed,
859 EmpathyTpChat *self = (EmpathyTpChat *) chat;
861 const TpIntSet *members;
863 EmpathyContact *contact;
867 DEBUG ("Error: %s", error->message);
871 members = tp_channel_group_get_members ((TpChannel *) self);
872 for (i = 0; i < n_contacts; i++)
874 contact = contacts[i];
875 handle = empathy_contact_get_handle (contact);
877 /* Make sure the contact is still member */
878 if (tp_intset_is_member (members, handle))
880 self->priv->members = g_list_prepend (self->priv->members,
881 g_object_ref (contact));
882 g_signal_emit_by_name (chat, "members-changed",
883 contact, NULL, 0, NULL, TRUE);
887 check_almost_ready (EMPATHY_TP_CHAT (chat));
890 static EmpathyContact *
891 chat_lookup_contact (EmpathyTpChat *self,
897 for (l = self->priv->members; l; l = l->next)
899 EmpathyContact *c = l->data;
901 if (empathy_contact_get_handle (c) != handle)
906 /* Caller takes the reference. */
907 self->priv->members = g_list_delete_link (self->priv->members, l);
927 static ContactRenameData *
928 contact_rename_data_new (TpHandle handle,
930 const gchar* message)
932 ContactRenameData *data = g_new (ContactRenameData, 1);
933 data->old_handle = handle;
934 data->reason = reason;
935 data->message = g_strdup (message);
941 contact_rename_data_free (ContactRenameData* data)
943 g_free (data->message);
948 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
950 EmpathyContact * const * contacts,
952 const TpHandle *failed,
957 EmpathyTpChat *self = (EmpathyTpChat *) chat;
958 const TpIntSet *members;
960 EmpathyContact *old = NULL, *new = NULL;
961 ContactRenameData *rename_data = (ContactRenameData *) user_data;
965 DEBUG ("Error: %s", error->message);
969 /* renamed members can only be delivered one at a time */
970 g_warn_if_fail (n_contacts == 1);
974 members = tp_channel_group_get_members ((TpChannel *) self);
975 handle = empathy_contact_get_handle (new);
977 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
979 /* Make sure the contact is still member */
980 if (tp_intset_is_member (members, handle))
982 self->priv->members = g_list_prepend (self->priv->members,
987 g_signal_emit_by_name (self, "member-renamed",
988 old, new, rename_data->reason,
989 rename_data->message);
990 g_object_unref (old);
994 if (self->priv->user == old)
996 /* We change our nick */
997 tp_clear_object (&self->priv->user);
998 self->priv->user = g_object_ref (new);
999 g_object_notify (chat, "self-contact");
1002 check_almost_ready (self);
1007 tp_chat_group_members_changed_cb (TpChannel *channel,
1011 GArray *local_pending,
1012 GArray *remote_pending,
1015 EmpathyTpChat *self)
1017 EmpathyContact *contact;
1018 EmpathyContact *actor_contact = NULL;
1020 ContactRenameData *rename_data;
1021 TpHandle old_handle;
1022 TpConnection *connection = tp_channel_borrow_connection (
1023 (TpChannel *) self);
1025 /* Contact renamed */
1026 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
1028 /* there can only be a single 'added' and a single 'removed' handle */
1029 if (removed->len != 1 || added->len != 1)
1031 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1032 added->len, removed->len);
1036 old_handle = g_array_index (removed, guint, 0);
1038 rename_data = contact_rename_data_new (old_handle, reason, message);
1039 empathy_tp_contact_factory_get_from_handles (connection,
1040 added->len, (TpHandle *) added->data,
1041 tp_chat_got_renamed_contacts_cb,
1042 rename_data, (GDestroyNotify) contact_rename_data_free,
1049 actor_contact = chat_lookup_contact (self, actor, FALSE);
1050 if (actor_contact == NULL)
1052 /* FIXME: handle this a tad more gracefully: perhaps
1053 * the actor was a server op. We could use the
1054 * contact-ids detail of MembersChangedDetailed.
1056 DEBUG ("actor %u not a channel member", actor);
1060 /* Remove contacts that are not members anymore */
1061 for (i = 0; i < removed->len; i++)
1063 contact = chat_lookup_contact (self,
1064 g_array_index (removed, TpHandle, i), TRUE);
1066 if (contact != NULL)
1068 g_signal_emit_by_name (self, "members-changed", contact,
1069 actor_contact, reason, message,
1071 g_object_unref (contact);
1075 /* Request added contacts */
1078 empathy_tp_contact_factory_get_from_handles (connection,
1079 added->len, (TpHandle *) added->data,
1080 tp_chat_got_added_contacts_cb, NULL, NULL,
1084 if (actor_contact != NULL)
1085 g_object_unref (actor_contact);
1089 tp_chat_got_remote_contact_cb (TpConnection *connection,
1090 EmpathyContact *contact,
1091 const GError *error,
1095 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1099 DEBUG ("Error: %s", error->message);
1100 empathy_tp_chat_leave (self, "");
1104 self->priv->remote_contact = g_object_ref (contact);
1105 g_object_notify (chat, "remote-contact");
1107 check_almost_ready (self);
1111 tp_chat_got_self_contact_cb (TpConnection *connection,
1112 EmpathyContact *contact,
1113 const GError *error,
1117 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1121 DEBUG ("Error: %s", error->message);
1122 empathy_tp_chat_leave (self, "");
1126 self->priv->user = g_object_ref (contact);
1127 empathy_contact_set_is_user (self->priv->user, TRUE);
1128 g_object_notify (chat, "self-contact");
1129 check_almost_ready (self);
1133 tp_chat_get_property (GObject *object,
1138 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1143 g_value_set_object (value, self->priv->account);
1145 case PROP_SELF_CONTACT:
1146 g_value_set_object (value, self->priv->user);
1148 case PROP_REMOTE_CONTACT:
1149 g_value_set_object (value, self->priv->remote_contact);
1151 case PROP_N_MESSAGES_SENDING:
1152 g_value_set_uint (value,
1153 g_hash_table_size (self->priv->messages_being_sent));
1156 g_value_set_string (value,
1157 empathy_tp_chat_get_title (self));
1160 g_value_set_string (value,
1161 empathy_tp_chat_get_subject (self));
1164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1170 tp_chat_set_property (GObject *object,
1172 const GValue *value,
1175 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1180 self->priv->account = g_value_dup_object (value);
1183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1193 static const TpProxyFeature *
1194 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1196 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1197 static GQuark need[2] = {0, 0};
1199 if (G_LIKELY (features[0].name != 0))
1202 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1203 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1204 features[FEAT_READY].depends_on = need;
1205 features[FEAT_READY].prepare_async =
1206 tp_chat_prepare_ready_async;
1208 /* assert that the terminator at the end is there */
1209 g_assert (features[N_FEAT].name == 0);
1215 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1217 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1218 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1220 object_class->dispose = tp_chat_dispose;
1221 object_class->finalize = tp_chat_finalize;
1222 object_class->get_property = tp_chat_get_property;
1223 object_class->set_property = tp_chat_set_property;
1225 proxy_class->list_features = tp_chat_list_features;
1227 g_object_class_install_property (object_class, PROP_ACCOUNT,
1228 g_param_spec_object ("account", "TpAccount",
1229 "the account associated with the chat",
1232 G_PARAM_CONSTRUCT_ONLY |
1233 G_PARAM_STATIC_STRINGS));
1236 * EmpathyTpChat:self-contact:
1238 * Not to be confused with TpChannel:group-self-contact.
1240 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1241 g_param_spec_object ("self-contact", "The local contact",
1242 "The EmpathyContact for the local user on this channel",
1243 EMPATHY_TYPE_CONTACT,
1246 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1247 g_param_spec_object ("remote-contact", "The remote contact",
1248 "The remote contact if there is no group iface on the channel",
1249 EMPATHY_TYPE_CONTACT,
1252 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1253 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1254 "The number of messages being sent",
1258 g_object_class_install_property (object_class, PROP_TITLE,
1259 g_param_spec_string ("title", "Title",
1260 "A human-readable name for the room, if any",
1263 G_PARAM_STATIC_STRINGS));
1265 g_object_class_install_property (object_class, PROP_SUBJECT,
1266 g_param_spec_string ("subject", "Subject",
1267 "The room's current subject, if any",
1270 G_PARAM_STATIC_STRINGS));
1273 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1274 G_TYPE_FROM_CLASS (klass),
1278 g_cclosure_marshal_generic,
1280 1, EMPATHY_TYPE_MESSAGE);
1282 signals[SEND_ERROR] = g_signal_new ("send-error",
1283 G_TYPE_FROM_CLASS (klass),
1287 g_cclosure_marshal_generic,
1289 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1291 signals[CHAT_STATE_CHANGED] = g_signal_new ("chat-state-changed-empathy",
1292 G_TYPE_FROM_CLASS (klass),
1296 g_cclosure_marshal_generic,
1298 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1300 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1301 G_TYPE_FROM_CLASS (klass),
1305 g_cclosure_marshal_generic,
1307 1, EMPATHY_TYPE_MESSAGE);
1309 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1313 empathy_tp_chat_init (EmpathyTpChat *self)
1315 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1316 EmpathyTpChatPrivate);
1318 self->priv->messages_queue = g_queue_new ();
1319 self->priv->pending_messages_queue = g_queue_new ();
1320 self->priv->messages_being_sent = g_hash_table_new_full (
1321 g_str_hash, g_str_equal, g_free, NULL);
1325 tp_chat_iface_init (EmpathyContactListIface *iface)
1327 iface->remove = tp_chat_remove;
1328 iface->get_members = tp_chat_get_members;
1332 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1335 const gchar *object_path,
1336 const GHashTable *immutable_properties)
1338 TpProxy *conn_proxy = (TpProxy *) conn;
1340 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1341 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1342 g_return_val_if_fail (immutable_properties != NULL, NULL);
1344 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1348 "dbus-daemon", conn_proxy->dbus_daemon,
1349 "bus-name", conn_proxy->bus_name,
1350 "object-path", object_path,
1351 "channel-properties", immutable_properties,
1356 empathy_tp_chat_get_id (EmpathyTpChat *self)
1360 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1362 id = tp_channel_get_identifier ((TpChannel *) self);
1363 if (!EMP_STR_EMPTY (id))
1365 else if (self->priv->remote_contact)
1366 return empathy_contact_get_id (self->priv->remote_contact);
1373 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1375 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1377 return self->priv->remote_contact;
1381 empathy_tp_chat_get_account (EmpathyTpChat *self)
1383 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1385 return self->priv->account;
1389 empathy_tp_chat_send (EmpathyTpChat *self,
1392 gchar *message_body;
1394 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1395 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1397 message_body = tp_message_to_text (message, NULL);
1399 DEBUG ("Sending message: %s", message_body);
1401 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1402 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1403 message_send_cb, self);
1405 g_free (message_body);
1409 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1411 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1413 return self->priv->pending_messages_queue->head;
1417 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1418 EmpathyMessage *message)
1422 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1424 if (!empathy_message_is_incoming (message))
1427 tp_msg = empathy_message_get_tp_message (message);
1428 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1429 tp_msg, NULL, NULL);
1433 * empathy_tp_chat_can_add_contact:
1435 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1436 * That is if this chat is a 1-to-1 channel that can be upgraded to
1437 * a MUC using the Conference interface or if the channel is a MUC.
1440 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1442 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1444 return self->priv->can_upgrade_to_muc ||
1445 tp_proxy_has_interface_by_id (self,
1446 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1450 tp_channel_leave_async_cb (GObject *source_object,
1454 GError *error = NULL;
1456 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1458 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1460 g_error_free (error);
1465 empathy_tp_chat_leave (EmpathyTpChat *self,
1466 const gchar *message)
1468 TpChannel *channel = (TpChannel *) self;
1470 DEBUG ("Leaving channel %s with message \"%s\"",
1471 tp_channel_get_identifier (channel), message);
1473 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1474 message, tp_channel_leave_async_cb, self);
1478 add_members_cb (TpChannel *proxy,
1479 const GError *error,
1481 GObject *weak_object)
1483 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1487 DEBUG ("Failed to join chat (%s): %s",
1488 tp_channel_get_identifier ((TpChannel *) self), error->message);
1493 empathy_tp_chat_join (EmpathyTpChat *self)
1495 TpHandle self_handle;
1498 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1500 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1501 g_array_append_val (members, self_handle);
1503 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1,
1504 members, "", add_members_cb, NULL, NULL, G_OBJECT (self));
1506 g_array_unref (members);
1510 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1513 TpHandle self_handle;
1515 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1518 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1519 if (self_handle == 0)
1522 return tp_channel_group_get_local_pending_info ((TpChannel *) self,
1523 self_handle, inviter, NULL, NULL);
1527 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1528 EmpathyContact *contact)
1530 return tp_channel_get_chat_state ((TpChannel *) self,
1531 empathy_contact_get_handle (contact));
1535 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1537 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1539 return self->priv->user;
1543 empathy_tp_chat_get_feature_ready (void)
1545 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1549 tp_chat_prepare_ready_async (TpProxy *proxy,
1550 const TpProxyFeature *feature,
1551 GAsyncReadyCallback callback,
1554 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1555 TpChannel *channel = (TpChannel *) proxy;
1556 TpConnection *connection;
1557 gboolean listen_for_dbus_properties_changed = FALSE;
1559 g_assert (self->priv->ready_result == NULL);
1560 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1561 callback, user_data, tp_chat_prepare_ready_async);
1563 connection = tp_channel_borrow_connection (channel);
1565 if (tp_proxy_has_interface_by_id (self,
1566 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1568 const TpIntSet *members;
1572 /* Get self contact from the group's self handle */
1573 handle = tp_channel_group_get_self_handle (channel);
1574 empathy_tp_contact_factory_get_from_handle (connection,
1575 handle, tp_chat_got_self_contact_cb,
1576 NULL, NULL, G_OBJECT (self));
1578 /* Get initial member contacts */
1579 members = tp_channel_group_get_members (channel);
1580 handles = tp_intset_to_array (members);
1581 empathy_tp_contact_factory_get_from_handles (connection,
1582 handles->len, (TpHandle *) handles->data,
1583 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1585 self->priv->can_upgrade_to_muc = FALSE;
1587 tp_g_signal_connect_object (self, "group-members-changed",
1588 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1592 TpCapabilities *caps;
1597 /* Get the self contact from the connection's self handle */
1598 handle = tp_connection_get_self_handle (connection);
1599 empathy_tp_contact_factory_get_from_handle (connection,
1600 handle, tp_chat_got_self_contact_cb,
1601 NULL, NULL, G_OBJECT (self));
1603 /* Get the remote contact */
1604 handle = tp_channel_get_handle (channel, NULL);
1605 empathy_tp_contact_factory_get_from_handle (connection,
1606 handle, tp_chat_got_remote_contact_cb,
1607 NULL, NULL, G_OBJECT (self));
1609 caps = tp_connection_get_capabilities (connection);
1610 g_assert (caps != NULL);
1612 classes = tp_capabilities_get_channel_classes (caps);
1614 for (i = 0; i < classes->len; i++)
1616 GValueArray *array = g_ptr_array_index (classes, i);
1617 const char **oprops = g_value_get_boxed (
1618 g_value_array_get_nth (array, 1));
1620 if (tp_strv_contains (oprops,
1621 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1623 self->priv->can_upgrade_to_muc = TRUE;
1629 if (tp_proxy_has_interface_by_id (self,
1630 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1632 tp_cli_dbus_properties_call_get_all (channel, -1,
1633 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1634 tp_chat_get_all_subject_cb,
1637 listen_for_dbus_properties_changed = TRUE;
1640 if (tp_proxy_has_interface_by_id (self,
1641 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1643 tp_cli_dbus_properties_call_get_all (channel, -1,
1644 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1645 tp_chat_get_all_room_config_cb,
1648 listen_for_dbus_properties_changed = TRUE;
1651 if (listen_for_dbus_properties_changed)
1653 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1654 tp_chat_dbus_properties_changed_cb,
1656 G_OBJECT (self), NULL);