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 tp_chat_add (EmpathyContactList *list,
162 EmpathyContact *contact,
163 const gchar *message)
165 EmpathyTpChat *self = (EmpathyTpChat *) list;
166 TpChannel *channel = (TpChannel *) self;
168 if (tp_proxy_has_interface_by_id (self,
169 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
172 GArray handles = {(gchar *) &handle, 1};
174 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
175 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
177 handle = empathy_contact_get_handle (contact);
178 tp_cli_channel_interface_group_call_add_members (channel,
179 -1, &handles, NULL, NULL, NULL, NULL, NULL);
181 else if (self->priv->can_upgrade_to_muc)
183 TpAccountChannelRequest *req;
185 const char *object_path;
186 GPtrArray channels = { (gpointer *) &object_path, 1 };
187 const char *invitees[2] = { NULL, };
189 invitees[0] = empathy_contact_get_id (contact);
190 object_path = tp_proxy_get_object_path (self);
193 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
194 TP_IFACE_CHANNEL_TYPE_TEXT,
195 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
197 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
198 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
199 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
200 G_TYPE_STRV, invitees,
201 /* FIXME: InvitationMessage ? */
204 req = tp_account_channel_request_new (self->priv->account, props,
205 TP_USER_ACTION_TIME_NOT_USER_ACTION);
207 /* Although this is a MUC, it's anonymous, so CreateChannel is
209 tp_account_channel_request_create_channel_async (req,
210 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
212 g_object_unref (req);
213 g_hash_table_unref (props);
217 g_warning ("Cannot add to this channel");
222 tp_chat_remove (EmpathyContactList *list,
223 EmpathyContact *contact,
224 const gchar *message)
226 EmpathyTpChat *self = (EmpathyTpChat *) list;
228 GArray handles = {(gchar *) &handle, 1};
230 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
231 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
233 handle = empathy_contact_get_handle (contact);
234 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
235 &handles, NULL, NULL, NULL, NULL, NULL);
239 tp_chat_get_members (EmpathyContactList *list)
241 EmpathyTpChat *self = (EmpathyTpChat *) list;
242 GList *members = NULL;
244 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
246 if (self->priv->members)
248 members = g_list_copy (self->priv->members);
249 g_list_foreach (members, (GFunc) g_object_ref, NULL);
253 members = g_list_prepend (members, g_object_ref (self->priv->user));
255 if (self->priv->remote_contact != NULL)
256 members = g_list_prepend (members,
257 g_object_ref (self->priv->remote_contact));
264 check_ready (EmpathyTpChat *self)
266 if (self->priv->ready_result == NULL)
269 if (g_queue_get_length (self->priv->messages_queue) > 0)
274 g_simple_async_result_complete (self->priv->ready_result);
275 tp_clear_object (&self->priv->ready_result);
279 tp_chat_emit_queued_messages (EmpathyTpChat *self)
281 EmpathyMessage *message;
283 /* Check if we can now emit some queued messages */
284 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL)
286 if (empathy_message_get_sender (message) == NULL)
289 DEBUG ("Queued message ready");
290 g_queue_pop_head (self->priv->messages_queue);
291 g_queue_push_tail (self->priv->pending_messages_queue, message);
292 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
299 tp_chat_got_sender_cb (TpConnection *connection,
300 EmpathyContact *contact,
305 EmpathyTpChat *self = (EmpathyTpChat *) chat;
309 DEBUG ("Error: %s", error->message);
310 /* Do not block the message queue, just drop this message */
311 g_queue_remove (self->priv->messages_queue, message);
315 empathy_message_set_sender (message, contact);
318 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
322 tp_chat_build_message (EmpathyTpChat *self,
326 EmpathyMessage *message;
329 message = empathy_message_new_from_tp_message (msg, incoming);
330 /* FIXME: this is actually a lie for incoming messages. */
331 empathy_message_set_receiver (message, self->priv->user);
333 g_queue_push_tail (self->priv->messages_queue, message);
335 sender = tp_signalled_message_get_sender (msg);
336 g_assert (sender != NULL);
338 if (tp_contact_get_handle (sender) == 0)
340 empathy_message_set_sender (message, self->priv->user);
341 tp_chat_emit_queued_messages (self);
345 TpConnection *connection = tp_channel_borrow_connection (
348 empathy_tp_contact_factory_get_from_handle (connection,
349 tp_contact_get_handle (sender),
350 tp_chat_got_sender_cb,
351 message, NULL, G_OBJECT (self));
356 handle_delivery_report (EmpathyTpChat *self,
359 TpDeliveryStatus delivery_status;
360 const GHashTable *header;
361 TpChannelTextSendError delivery_error;
364 const gchar *message_body = NULL;
365 const gchar *delivery_dbus_error;
366 const gchar *delivery_token = NULL;
368 header = tp_message_peek (message, 0);
372 delivery_token = tp_asv_get_string (header, "delivery-token");
373 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
379 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
381 DEBUG ("Accepted %s", delivery_token);
382 tp_chat_set_delivery_status (self, delivery_token,
383 EMPATHY_DELIVERY_STATUS_ACCEPTED);
386 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
388 DEBUG ("Delivered %s", delivery_token);
389 tp_chat_set_delivery_status (self, delivery_token,
390 EMPATHY_DELIVERY_STATUS_NONE);
393 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
394 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
399 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
401 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
403 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
405 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
406 * TpMessage. (fdo #35884) */
407 echo = tp_asv_get_boxed (header, "delivery-echo",
408 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
409 if (echo != NULL && echo->len >= 2)
411 const GHashTable *echo_body;
413 echo_body = g_ptr_array_index (echo, 1);
414 if (echo_body != NULL)
415 message_body = tp_asv_get_string (echo_body, "content");
418 tp_chat_set_delivery_status (self, delivery_token,
419 EMPATHY_DELIVERY_STATUS_NONE);
420 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
421 delivery_error, delivery_dbus_error);
424 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
425 message, NULL, NULL);
429 handle_incoming_message (EmpathyTpChat *self,
435 if (tp_message_is_delivery_report (message))
437 handle_delivery_report (self, message);
441 message_body = tp_message_to_text (message, NULL);
443 DEBUG ("Message %s (channel %s): %s",
444 pending ? "pending" : "received",
445 tp_proxy_get_object_path (self), message_body);
447 if (message_body == NULL)
449 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
451 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
452 message, NULL, NULL);
456 tp_chat_build_message (self, message, TRUE);
458 g_free (message_body);
462 message_received_cb (TpTextChannel *channel,
466 handle_incoming_message (self, message, FALSE);
470 find_pending_message_func (gconstpointer a,
473 EmpathyMessage *msg = (EmpathyMessage *) a;
474 TpMessage *message = (TpMessage *) b;
476 if (empathy_message_get_tp_message (msg) == message)
483 pending_message_removed_cb (TpTextChannel *channel,
489 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
490 find_pending_message_func);
495 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
497 g_object_unref (m->data);
498 g_queue_delete_link (self->priv->pending_messages_queue, m);
502 message_sent_cb (TpTextChannel *channel,
504 TpMessageSendingFlags flags,
510 message_body = tp_message_to_text (message, NULL);
512 DEBUG ("Message sent: %s", message_body);
514 tp_chat_build_message (self, message, FALSE);
516 g_free (message_body);
519 static TpChannelTextSendError
520 error_to_text_send_error (GError *error)
522 if (error->domain != TP_ERRORS)
523 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
527 case TP_ERROR_OFFLINE:
528 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
529 case TP_ERROR_INVALID_HANDLE:
530 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
531 case TP_ERROR_PERMISSION_DENIED:
532 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
533 case TP_ERROR_NOT_IMPLEMENTED:
534 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
537 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
541 message_send_cb (GObject *source,
542 GAsyncResult *result,
545 EmpathyTpChat *self = user_data;
546 TpTextChannel *channel = (TpTextChannel *) source;
548 GError *error = NULL;
550 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
552 DEBUG ("Error: %s", error->message);
554 /* FIXME: we should use the body of the message as first argument of the
555 * signal but can't easily get it as we just get a user_data pointer. Once
556 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
557 * to use the user_data pointer to pass the message and fix this. */
558 g_signal_emit (self, signals[SEND_ERROR], 0,
559 NULL, error_to_text_send_error (error), NULL);
561 g_error_free (error);
564 tp_chat_set_delivery_status (self, token,
565 EMPATHY_DELIVERY_STATUS_SENDING);
571 TpChannelChatState state;
575 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
576 EmpathyContact *contact,
581 TpChannelChatState state;
585 DEBUG ("Error: %s", error->message);
589 state = GPOINTER_TO_UINT (user_data);
590 DEBUG ("Chat state changed for %s (%d): %d",
591 empathy_contact_get_alias (contact),
592 empathy_contact_get_handle (contact), state);
594 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
598 tp_chat_state_changed_cb (TpChannel *channel,
600 TpChannelChatState state,
603 TpConnection *connection = tp_channel_borrow_connection (
606 empathy_tp_contact_factory_get_from_handle (connection, handle,
607 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
608 NULL, G_OBJECT (self));
612 list_pending_messages (EmpathyTpChat *self)
616 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
618 for (l = messages; l != NULL; l = g_list_next (l))
620 TpMessage *message = l->data;
622 handle_incoming_message (self, message, FALSE);
625 g_list_free (messages);
629 update_subject (EmpathyTpChat *self,
630 GHashTable *properties)
632 gboolean can_set, valid;
633 const gchar *subject;
635 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
637 self->priv->can_set_subject = can_set;
639 subject = tp_asv_get_string (properties, "Subject");
644 g_free (self->priv->subject);
645 self->priv->subject = g_strdup (subject);
647 /* If the actor is included with this update, use it;
648 * otherwise, clear it to avoid showing stale information.
649 * Why might it not be included? When you join an IRC channel,
650 * you get a pair of messages: first, the current topic; next,
651 * who set it, and when. Idle reports these in two separate
654 actor = tp_asv_get_string (properties, "Actor");
655 g_free (self->priv->subject_actor);
656 self->priv->subject_actor = g_strdup (actor);
658 g_object_notify (G_OBJECT (self), "subject");
661 /* TODO: track Timestamp. */
665 tp_chat_get_all_subject_cb (TpProxy *proxy,
666 GHashTable *properties,
668 gpointer user_data G_GNUC_UNUSED,
671 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
675 DEBUG ("Error fetching subject: %s", error->message);
679 self->priv->supports_subject = TRUE;
680 update_subject (self, properties);
684 update_title (EmpathyTpChat *self,
685 GHashTable *properties)
687 const gchar *title = tp_asv_get_string (properties, "Title");
691 if (tp_str_empty (title))
694 g_free (self->priv->title);
695 self->priv->title = g_strdup (title);
696 g_object_notify (G_OBJECT (self), "title");
701 tp_chat_get_all_room_config_cb (TpProxy *proxy,
702 GHashTable *properties,
704 gpointer user_data G_GNUC_UNUSED,
707 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
711 DEBUG ("Error fetching room config: %s", error->message);
715 update_title (self, properties);
719 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
720 const gchar *interface_name,
722 const gchar **invalidated,
726 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
728 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
729 update_subject (self, changed);
731 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
732 update_title (self, changed);
736 empathy_tp_chat_set_subject (EmpathyTpChat *self,
737 const gchar *subject)
739 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
740 subject, tp_chat_async_cb, "while setting subject", NULL,
745 empathy_tp_chat_get_title (EmpathyTpChat *self)
747 return self->priv->title;
751 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
753 return self->priv->supports_subject;
757 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
759 return self->priv->can_set_subject;
763 empathy_tp_chat_get_subject (EmpathyTpChat *self)
765 return self->priv->subject;
769 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
771 return self->priv->subject_actor;
775 tp_chat_dispose (GObject *object)
777 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
779 tp_clear_object (&self->priv->account);
780 tp_clear_object (&self->priv->remote_contact);
781 tp_clear_object (&self->priv->user);
783 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
784 g_queue_clear (self->priv->messages_queue);
786 g_queue_foreach (self->priv->pending_messages_queue,
787 (GFunc) g_object_unref, NULL);
788 g_queue_clear (self->priv->pending_messages_queue);
790 tp_clear_object (&self->priv->ready_result);
792 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
793 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
797 tp_chat_finalize (GObject *object)
799 EmpathyTpChat *self = (EmpathyTpChat *) object;
801 DEBUG ("Finalize: %p", object);
803 g_queue_free (self->priv->messages_queue);
804 g_queue_free (self->priv->pending_messages_queue);
805 g_hash_table_unref (self->priv->messages_being_sent);
807 g_free (self->priv->title);
808 g_free (self->priv->subject);
809 g_free (self->priv->subject_actor);
811 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
815 check_almost_ready (EmpathyTpChat *self)
817 TpChannel *channel = (TpChannel *) self;
819 if (self->priv->ready_result == NULL)
822 if (self->priv->user == NULL)
825 /* We need either the members (room) or the remote contact (private chat).
826 * If the chat is protected by a password we can't get these information so
827 * consider the chat as ready so it can be presented to the user. */
828 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
829 self->priv->remote_contact == NULL)
832 g_assert (tp_proxy_is_prepared (self,
833 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
835 tp_g_signal_connect_object (self, "message-received",
836 G_CALLBACK (message_received_cb), self, 0);
837 tp_g_signal_connect_object (self, "pending-message-removed",
838 G_CALLBACK (pending_message_removed_cb), self, 0);
840 list_pending_messages (self);
842 tp_g_signal_connect_object (self, "message-sent",
843 G_CALLBACK (message_sent_cb), self, 0);
845 tp_g_signal_connect_object (self, "chat-state-changed",
846 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
852 tp_chat_got_added_contacts_cb (TpConnection *connection,
854 EmpathyContact * const * contacts,
856 const TpHandle *failed,
861 EmpathyTpChat *self = (EmpathyTpChat *) chat;
863 const TpIntSet *members;
865 EmpathyContact *contact;
869 DEBUG ("Error: %s", error->message);
873 members = tp_channel_group_get_members ((TpChannel *) self);
874 for (i = 0; i < n_contacts; i++)
876 contact = contacts[i];
877 handle = empathy_contact_get_handle (contact);
879 /* Make sure the contact is still member */
880 if (tp_intset_is_member (members, handle))
882 self->priv->members = g_list_prepend (self->priv->members,
883 g_object_ref (contact));
884 g_signal_emit_by_name (chat, "members-changed",
885 contact, NULL, 0, NULL, TRUE);
889 check_almost_ready (EMPATHY_TP_CHAT (chat));
892 static EmpathyContact *
893 chat_lookup_contact (EmpathyTpChat *self,
899 for (l = self->priv->members; l; l = l->next)
901 EmpathyContact *c = l->data;
903 if (empathy_contact_get_handle (c) != handle)
908 /* Caller takes the reference. */
909 self->priv->members = g_list_delete_link (self->priv->members, l);
929 static ContactRenameData *
930 contact_rename_data_new (TpHandle handle,
932 const gchar* message)
934 ContactRenameData *data = g_new (ContactRenameData, 1);
935 data->old_handle = handle;
936 data->reason = reason;
937 data->message = g_strdup (message);
943 contact_rename_data_free (ContactRenameData* data)
945 g_free (data->message);
950 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
952 EmpathyContact * const * contacts,
954 const TpHandle *failed,
959 EmpathyTpChat *self = (EmpathyTpChat *) chat;
960 const TpIntSet *members;
962 EmpathyContact *old = NULL, *new = NULL;
963 ContactRenameData *rename_data = (ContactRenameData *) user_data;
967 DEBUG ("Error: %s", error->message);
971 /* renamed members can only be delivered one at a time */
972 g_warn_if_fail (n_contacts == 1);
976 members = tp_channel_group_get_members ((TpChannel *) self);
977 handle = empathy_contact_get_handle (new);
979 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
981 /* Make sure the contact is still member */
982 if (tp_intset_is_member (members, handle))
984 self->priv->members = g_list_prepend (self->priv->members,
989 g_signal_emit_by_name (self, "member-renamed",
990 old, new, rename_data->reason,
991 rename_data->message);
992 g_object_unref (old);
996 if (self->priv->user == old)
998 /* We change our nick */
999 tp_clear_object (&self->priv->user);
1000 self->priv->user = g_object_ref (new);
1001 g_object_notify (chat, "self-contact");
1004 check_almost_ready (self);
1009 tp_chat_group_members_changed_cb (TpChannel *channel,
1013 GArray *local_pending,
1014 GArray *remote_pending,
1017 EmpathyTpChat *self)
1019 EmpathyContact *contact;
1020 EmpathyContact *actor_contact = NULL;
1022 ContactRenameData *rename_data;
1023 TpHandle old_handle;
1024 TpConnection *connection = tp_channel_borrow_connection (
1025 (TpChannel *) self);
1027 /* Contact renamed */
1028 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
1030 /* there can only be a single 'added' and a single 'removed' handle */
1031 if (removed->len != 1 || added->len != 1)
1033 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1034 added->len, removed->len);
1038 old_handle = g_array_index (removed, guint, 0);
1040 rename_data = contact_rename_data_new (old_handle, reason, message);
1041 empathy_tp_contact_factory_get_from_handles (connection,
1042 added->len, (TpHandle *) added->data,
1043 tp_chat_got_renamed_contacts_cb,
1044 rename_data, (GDestroyNotify) contact_rename_data_free,
1051 actor_contact = chat_lookup_contact (self, actor, FALSE);
1052 if (actor_contact == NULL)
1054 /* FIXME: handle this a tad more gracefully: perhaps
1055 * the actor was a server op. We could use the
1056 * contact-ids detail of MembersChangedDetailed.
1058 DEBUG ("actor %u not a channel member", actor);
1062 /* Remove contacts that are not members anymore */
1063 for (i = 0; i < removed->len; i++)
1065 contact = chat_lookup_contact (self,
1066 g_array_index (removed, TpHandle, i), TRUE);
1068 if (contact != NULL)
1070 g_signal_emit_by_name (self, "members-changed", contact,
1071 actor_contact, reason, message,
1073 g_object_unref (contact);
1077 /* Request added contacts */
1080 empathy_tp_contact_factory_get_from_handles (connection,
1081 added->len, (TpHandle *) added->data,
1082 tp_chat_got_added_contacts_cb, NULL, NULL,
1086 if (actor_contact != NULL)
1087 g_object_unref (actor_contact);
1091 tp_chat_got_remote_contact_cb (TpConnection *connection,
1092 EmpathyContact *contact,
1093 const GError *error,
1097 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1101 DEBUG ("Error: %s", error->message);
1102 empathy_tp_chat_leave (self, "");
1106 self->priv->remote_contact = g_object_ref (contact);
1107 g_object_notify (chat, "remote-contact");
1109 check_almost_ready (self);
1113 tp_chat_got_self_contact_cb (TpConnection *connection,
1114 EmpathyContact *contact,
1115 const GError *error,
1119 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1123 DEBUG ("Error: %s", error->message);
1124 empathy_tp_chat_leave (self, "");
1128 self->priv->user = g_object_ref (contact);
1129 empathy_contact_set_is_user (self->priv->user, TRUE);
1130 g_object_notify (chat, "self-contact");
1131 check_almost_ready (self);
1135 tp_chat_get_property (GObject *object,
1140 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1145 g_value_set_object (value, self->priv->account);
1147 case PROP_SELF_CONTACT:
1148 g_value_set_object (value, self->priv->user);
1150 case PROP_REMOTE_CONTACT:
1151 g_value_set_object (value, self->priv->remote_contact);
1153 case PROP_N_MESSAGES_SENDING:
1154 g_value_set_uint (value,
1155 g_hash_table_size (self->priv->messages_being_sent));
1158 g_value_set_string (value,
1159 empathy_tp_chat_get_title (self));
1162 g_value_set_string (value,
1163 empathy_tp_chat_get_subject (self));
1166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1172 tp_chat_set_property (GObject *object,
1174 const GValue *value,
1177 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1182 self->priv->account = g_value_dup_object (value);
1185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1195 static const TpProxyFeature *
1196 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1198 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1199 static GQuark need[2] = {0, 0};
1201 if (G_LIKELY (features[0].name != 0))
1204 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1205 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1206 features[FEAT_READY].depends_on = need;
1207 features[FEAT_READY].prepare_async =
1208 tp_chat_prepare_ready_async;
1210 /* assert that the terminator at the end is there */
1211 g_assert (features[N_FEAT].name == 0);
1217 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1219 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1220 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1222 object_class->dispose = tp_chat_dispose;
1223 object_class->finalize = tp_chat_finalize;
1224 object_class->get_property = tp_chat_get_property;
1225 object_class->set_property = tp_chat_set_property;
1227 proxy_class->list_features = tp_chat_list_features;
1229 g_object_class_install_property (object_class, PROP_ACCOUNT,
1230 g_param_spec_object ("account", "TpAccount",
1231 "the account associated with the chat",
1234 G_PARAM_CONSTRUCT_ONLY |
1235 G_PARAM_STATIC_STRINGS));
1238 * EmpathyTpChat:self-contact:
1240 * Not to be confused with TpChannel:group-self-contact.
1242 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1243 g_param_spec_object ("self-contact", "The local contact",
1244 "The EmpathyContact for the local user on this channel",
1245 EMPATHY_TYPE_CONTACT,
1248 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1249 g_param_spec_object ("remote-contact", "The remote contact",
1250 "The remote contact if there is no group iface on the channel",
1251 EMPATHY_TYPE_CONTACT,
1254 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1255 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1256 "The number of messages being sent",
1260 g_object_class_install_property (object_class, PROP_TITLE,
1261 g_param_spec_string ("title", "Title",
1262 "A human-readable name for the room, if any",
1265 G_PARAM_STATIC_STRINGS));
1267 g_object_class_install_property (object_class, PROP_SUBJECT,
1268 g_param_spec_string ("subject", "Subject",
1269 "The room's current subject, if any",
1272 G_PARAM_STATIC_STRINGS));
1275 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1276 G_TYPE_FROM_CLASS (klass),
1280 g_cclosure_marshal_generic,
1282 1, EMPATHY_TYPE_MESSAGE);
1284 signals[SEND_ERROR] = g_signal_new ("send-error",
1285 G_TYPE_FROM_CLASS (klass),
1289 g_cclosure_marshal_generic,
1291 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1293 signals[CHAT_STATE_CHANGED] = g_signal_new ("chat-state-changed-empathy",
1294 G_TYPE_FROM_CLASS (klass),
1298 g_cclosure_marshal_generic,
1300 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1302 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1303 G_TYPE_FROM_CLASS (klass),
1307 g_cclosure_marshal_generic,
1309 1, EMPATHY_TYPE_MESSAGE);
1311 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1315 empathy_tp_chat_init (EmpathyTpChat *self)
1317 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1318 EmpathyTpChatPrivate);
1320 self->priv->messages_queue = g_queue_new ();
1321 self->priv->pending_messages_queue = g_queue_new ();
1322 self->priv->messages_being_sent = g_hash_table_new_full (
1323 g_str_hash, g_str_equal, g_free, NULL);
1327 tp_chat_iface_init (EmpathyContactListIface *iface)
1329 iface->add = tp_chat_add;
1330 iface->remove = tp_chat_remove;
1331 iface->get_members = tp_chat_get_members;
1335 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1338 const gchar *object_path,
1339 const GHashTable *immutable_properties)
1341 TpProxy *conn_proxy = (TpProxy *) conn;
1343 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1344 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1345 g_return_val_if_fail (immutable_properties != NULL, NULL);
1347 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1351 "dbus-daemon", conn_proxy->dbus_daemon,
1352 "bus-name", conn_proxy->bus_name,
1353 "object-path", object_path,
1354 "channel-properties", immutable_properties,
1359 empathy_tp_chat_get_id (EmpathyTpChat *self)
1363 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1365 id = tp_channel_get_identifier ((TpChannel *) self);
1366 if (!EMP_STR_EMPTY (id))
1368 else if (self->priv->remote_contact)
1369 return empathy_contact_get_id (self->priv->remote_contact);
1376 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1378 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1380 return self->priv->remote_contact;
1384 empathy_tp_chat_get_account (EmpathyTpChat *self)
1386 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1388 return self->priv->account;
1392 empathy_tp_chat_send (EmpathyTpChat *self,
1395 gchar *message_body;
1397 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1398 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1400 message_body = tp_message_to_text (message, NULL);
1402 DEBUG ("Sending message: %s", message_body);
1404 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1405 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1406 message_send_cb, self);
1408 g_free (message_body);
1412 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1414 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1416 return self->priv->pending_messages_queue->head;
1420 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1421 EmpathyMessage *message)
1425 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1427 if (!empathy_message_is_incoming (message))
1430 tp_msg = empathy_message_get_tp_message (message);
1431 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1432 tp_msg, NULL, NULL);
1436 * empathy_tp_chat_can_add_contact:
1438 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1439 * That is if this chat is a 1-to-1 channel that can be upgraded to
1440 * a MUC using the Conference interface or if the channel is a MUC.
1443 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1445 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1447 return self->priv->can_upgrade_to_muc ||
1448 tp_proxy_has_interface_by_id (self,
1449 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1453 tp_channel_leave_async_cb (GObject *source_object,
1457 GError *error = NULL;
1459 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1461 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1463 g_error_free (error);
1468 empathy_tp_chat_leave (EmpathyTpChat *self,
1469 const gchar *message)
1471 TpChannel *channel = (TpChannel *) self;
1473 DEBUG ("Leaving channel %s with message \"%s\"",
1474 tp_channel_get_identifier (channel), message);
1476 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1477 message, tp_channel_leave_async_cb, self);
1481 add_members_cb (TpChannel *proxy,
1482 const GError *error,
1484 GObject *weak_object)
1486 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1490 DEBUG ("Failed to join chat (%s): %s",
1491 tp_channel_get_identifier ((TpChannel *) self), error->message);
1496 empathy_tp_chat_join (EmpathyTpChat *self)
1498 TpHandle self_handle;
1501 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1503 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1504 g_array_append_val (members, self_handle);
1506 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1,
1507 members, "", add_members_cb, NULL, NULL, G_OBJECT (self));
1509 g_array_unref (members);
1513 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1516 TpHandle self_handle;
1518 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1521 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1522 if (self_handle == 0)
1525 return tp_channel_group_get_local_pending_info ((TpChannel *) self,
1526 self_handle, inviter, NULL, NULL);
1530 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1531 EmpathyContact *contact)
1533 return tp_channel_get_chat_state ((TpChannel *) self,
1534 empathy_contact_get_handle (contact));
1538 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1540 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1542 return self->priv->user;
1546 empathy_tp_chat_get_feature_ready (void)
1548 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1552 tp_chat_prepare_ready_async (TpProxy *proxy,
1553 const TpProxyFeature *feature,
1554 GAsyncReadyCallback callback,
1557 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1558 TpChannel *channel = (TpChannel *) proxy;
1559 TpConnection *connection;
1560 gboolean listen_for_dbus_properties_changed = FALSE;
1562 g_assert (self->priv->ready_result == NULL);
1563 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1564 callback, user_data, tp_chat_prepare_ready_async);
1566 connection = tp_channel_borrow_connection (channel);
1568 if (tp_proxy_has_interface_by_id (self,
1569 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1571 const TpIntSet *members;
1575 /* Get self contact from the group's self handle */
1576 handle = tp_channel_group_get_self_handle (channel);
1577 empathy_tp_contact_factory_get_from_handle (connection,
1578 handle, tp_chat_got_self_contact_cb,
1579 NULL, NULL, G_OBJECT (self));
1581 /* Get initial member contacts */
1582 members = tp_channel_group_get_members (channel);
1583 handles = tp_intset_to_array (members);
1584 empathy_tp_contact_factory_get_from_handles (connection,
1585 handles->len, (TpHandle *) handles->data,
1586 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1588 self->priv->can_upgrade_to_muc = FALSE;
1590 tp_g_signal_connect_object (self, "group-members-changed",
1591 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1595 TpCapabilities *caps;
1600 /* Get the self contact from the connection's self handle */
1601 handle = tp_connection_get_self_handle (connection);
1602 empathy_tp_contact_factory_get_from_handle (connection,
1603 handle, tp_chat_got_self_contact_cb,
1604 NULL, NULL, G_OBJECT (self));
1606 /* Get the remote contact */
1607 handle = tp_channel_get_handle (channel, NULL);
1608 empathy_tp_contact_factory_get_from_handle (connection,
1609 handle, tp_chat_got_remote_contact_cb,
1610 NULL, NULL, G_OBJECT (self));
1612 caps = tp_connection_get_capabilities (connection);
1613 g_assert (caps != NULL);
1615 classes = tp_capabilities_get_channel_classes (caps);
1617 for (i = 0; i < classes->len; i++)
1619 GValueArray *array = g_ptr_array_index (classes, i);
1620 const char **oprops = g_value_get_boxed (
1621 g_value_array_get_nth (array, 1));
1623 if (tp_strv_contains (oprops,
1624 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1626 self->priv->can_upgrade_to_muc = TRUE;
1632 if (tp_proxy_has_interface_by_id (self,
1633 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1635 tp_cli_dbus_properties_call_get_all (channel, -1,
1636 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1637 tp_chat_get_all_subject_cb,
1640 listen_for_dbus_properties_changed = TRUE;
1643 if (tp_proxy_has_interface_by_id (self,
1644 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1646 tp_cli_dbus_properties_call_get_all (channel, -1,
1647 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1648 tp_chat_get_all_room_config_cb,
1651 listen_for_dbus_properties_changed = TRUE;
1654 if (listen_for_dbus_properties_changed)
1656 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1657 tp_chat_dbus_properties_changed_cb,
1659 G_OBJECT (self), NULL);