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/telepathy-glib.h>
28 #include <extensions/extensions.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-request-util.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 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;
55 /* Room config (for now, we only track the title and don't support
59 gboolean can_upgrade_to_muc;
61 GHashTable *messages_being_sent;
63 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
64 GSimpleAsyncResult *ready_result;
67 static void tp_chat_iface_init (EmpathyContactListIface *iface);
73 PROP_N_MESSAGES_SENDING,
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
93 tp_chat_set_delivery_status (EmpathyTpChat *self,
95 EmpathyDeliveryStatus delivery_status)
97 TpDeliveryReportingSupportFlags flags =
98 tp_text_channel_get_delivery_reporting_support (
99 TP_TEXT_CHANNEL (self));
101 /* channel must support receiving failures and successes */
102 if (!tp_str_empty (token) &&
103 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
104 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
106 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
108 switch (delivery_status) {
109 case EMPATHY_DELIVERY_STATUS_NONE:
110 g_hash_table_remove (self->priv->messages_being_sent,
115 g_hash_table_insert (self->priv->messages_being_sent,
117 GUINT_TO_POINTER (delivery_status));
121 g_object_notify (G_OBJECT (self), "n-messages-sending");
125 static void tp_chat_prepare_ready_async (TpProxy *proxy,
126 const TpProxyFeature *feature,
127 GAsyncReadyCallback callback,
131 tp_chat_async_cb (TpChannel *proxy,
134 GObject *weak_object)
137 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
142 create_conference_cb (GObject *source,
143 GAsyncResult *result,
146 GError *error = NULL;
148 if (!tp_account_channel_request_create_channel_finish (
149 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
150 DEBUG ("Failed to create conference channel: %s", error->message);
151 g_error_free (error);
156 tp_chat_add (EmpathyContactList *list,
157 EmpathyContact *contact,
158 const gchar *message)
160 EmpathyTpChat *self = (EmpathyTpChat *) list;
161 TpChannel *channel = (TpChannel *) self;
163 if (tp_proxy_has_interface_by_id (self,
164 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
166 GArray handles = {(gchar *) &handle, 1};
168 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
169 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
171 handle = empathy_contact_get_handle (contact);
172 tp_cli_channel_interface_group_call_add_members (channel,
173 -1, &handles, NULL, NULL, NULL, NULL, NULL);
174 } else if (self->priv->can_upgrade_to_muc) {
175 TpAccountChannelRequest *req;
177 const char *object_path;
178 GPtrArray channels = { (gpointer *) &object_path, 1 };
179 const char *invitees[2] = { NULL, };
181 invitees[0] = empathy_contact_get_id (contact);
182 object_path = tp_proxy_get_object_path (self);
185 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
186 TP_IFACE_CHANNEL_TYPE_TEXT,
187 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
189 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
190 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
191 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
192 G_TYPE_STRV, invitees,
193 /* FIXME: InvitationMessage ? */
196 req = tp_account_channel_request_new (self->priv->account, props,
197 TP_USER_ACTION_TIME_NOT_USER_ACTION);
199 /* Although this is a MUC, it's anonymous, so CreateChannel is
201 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
202 NULL, create_conference_cb, NULL);
204 g_object_unref (req);
205 g_hash_table_unref (props);
207 g_warning ("Cannot add to this channel");
212 tp_chat_remove (EmpathyContactList *list,
213 EmpathyContact *contact,
214 const gchar *message)
216 EmpathyTpChat *self = (EmpathyTpChat *) list;
218 GArray handles = {(gchar *) &handle, 1};
220 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
221 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
223 handle = empathy_contact_get_handle (contact);
224 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
231 tp_chat_get_members (EmpathyContactList *list)
233 EmpathyTpChat *self = (EmpathyTpChat *) list;
234 GList *members = NULL;
236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
238 if (self->priv->members) {
239 members = g_list_copy (self->priv->members);
240 g_list_foreach (members, (GFunc) g_object_ref, NULL);
242 members = g_list_prepend (members, g_object_ref (self->priv->user));
243 if (self->priv->remote_contact != NULL)
244 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
251 check_ready (EmpathyTpChat *self)
253 if (self->priv->ready_result == NULL)
256 if (g_queue_get_length (self->priv->messages_queue) > 0)
261 g_simple_async_result_complete (self->priv->ready_result);
262 tp_clear_object (&self->priv->ready_result);
266 tp_chat_emit_queued_messages (EmpathyTpChat *self)
268 EmpathyMessage *message;
270 /* Check if we can now emit some queued messages */
271 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
272 if (empathy_message_get_sender (message) == NULL) {
276 DEBUG ("Queued message ready");
277 g_queue_pop_head (self->priv->messages_queue);
278 g_queue_push_tail (self->priv->pending_messages_queue, message);
279 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
286 tp_chat_got_sender_cb (TpConnection *connection,
287 EmpathyContact *contact,
292 EmpathyTpChat *self = (EmpathyTpChat *) chat;
295 DEBUG ("Error: %s", error->message);
296 /* Do not block the message queue, just drop this message */
297 g_queue_remove (self->priv->messages_queue, message);
299 empathy_message_set_sender (message, contact);
302 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
306 tp_chat_build_message (EmpathyTpChat *self,
310 EmpathyMessage *message;
313 message = empathy_message_new_from_tp_message (msg, incoming);
314 /* FIXME: this is actually a lie for incoming messages. */
315 empathy_message_set_receiver (message, self->priv->user);
317 g_queue_push_tail (self->priv->messages_queue, message);
319 sender = tp_signalled_message_get_sender (msg);
320 g_assert (sender != NULL);
322 if (tp_contact_get_handle (sender) == 0) {
323 empathy_message_set_sender (message, self->priv->user);
324 tp_chat_emit_queued_messages (self);
326 TpConnection *connection = tp_channel_borrow_connection (
329 empathy_tp_contact_factory_get_from_handle (connection,
330 tp_contact_get_handle (sender),
331 tp_chat_got_sender_cb,
332 message, NULL, G_OBJECT (self));
337 handle_delivery_report (EmpathyTpChat *self,
340 TpDeliveryStatus delivery_status;
341 const GHashTable *header;
342 TpChannelTextSendError delivery_error;
345 const gchar *message_body = NULL;
346 const gchar *delivery_dbus_error;
347 const gchar *delivery_token = NULL;
349 header = tp_message_peek (message, 0);
353 delivery_token = tp_asv_get_string (header, "delivery-token");
354 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
358 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
359 DEBUG ("Accepted %s", delivery_token);
360 tp_chat_set_delivery_status (self, delivery_token,
361 EMPATHY_DELIVERY_STATUS_ACCEPTED);
363 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
364 DEBUG ("Delivered %s", delivery_token);
365 tp_chat_set_delivery_status (self, delivery_token,
366 EMPATHY_DELIVERY_STATUS_NONE);
368 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
369 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED) {
373 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
375 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
377 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
379 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
380 * TpMessage. (fdo #35884) */
381 echo = tp_asv_get_boxed (header, "delivery-echo",
382 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
383 if (echo != NULL && echo->len >= 2) {
384 const GHashTable *echo_body;
386 echo_body = g_ptr_array_index (echo, 1);
387 if (echo_body != NULL)
388 message_body = tp_asv_get_string (echo_body, "content");
391 tp_chat_set_delivery_status (self, delivery_token,
392 EMPATHY_DELIVERY_STATUS_NONE);
393 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
394 delivery_error, delivery_dbus_error);
397 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
398 message, NULL, NULL);
402 handle_incoming_message (EmpathyTpChat *self,
408 if (tp_message_is_delivery_report (message)) {
409 handle_delivery_report (self, message);
413 message_body = tp_message_to_text (message, NULL);
415 DEBUG ("Message %s (channel %s): %s",
416 pending ? "pending" : "received",
417 tp_proxy_get_object_path (self), message_body);
419 if (message_body == NULL) {
420 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
422 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
423 message, NULL, NULL);
427 tp_chat_build_message (self, message, TRUE);
429 g_free (message_body);
433 message_received_cb (TpTextChannel *channel,
437 handle_incoming_message (self, message, FALSE);
441 find_pending_message_func (gconstpointer a,
444 EmpathyMessage *msg = (EmpathyMessage *) a;
445 TpMessage *message = (TpMessage *) b;
447 if (empathy_message_get_tp_message (msg) == message)
454 pending_message_removed_cb (TpTextChannel *channel,
460 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
461 find_pending_message_func);
466 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
468 g_object_unref (m->data);
469 g_queue_delete_link (self->priv->pending_messages_queue, m);
473 message_sent_cb (TpTextChannel *channel,
475 TpMessageSendingFlags flags,
481 message_body = tp_message_to_text (message, NULL);
483 DEBUG ("Message sent: %s", message_body);
485 tp_chat_build_message (self, message, FALSE);
487 g_free (message_body);
490 static TpChannelTextSendError
491 error_to_text_send_error (GError *error)
493 if (error->domain != TP_ERRORS)
494 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
496 switch (error->code) {
497 case TP_ERROR_OFFLINE:
498 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
499 case TP_ERROR_INVALID_HANDLE:
500 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
501 case TP_ERROR_PERMISSION_DENIED:
502 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
503 case TP_ERROR_NOT_IMPLEMENTED:
504 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
507 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
511 message_send_cb (GObject *source,
512 GAsyncResult *result,
515 EmpathyTpChat *self = user_data;
516 TpTextChannel *channel = (TpTextChannel *) source;
518 GError *error = NULL;
520 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
521 DEBUG ("Error: %s", error->message);
523 /* FIXME: we should use the body of the message as first argument of the
524 * signal but can't easily get it as we just get a user_data pointer. Once
525 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
526 * to use the user_data pointer to pass the message and fix this. */
527 g_signal_emit (self, signals[SEND_ERROR], 0,
528 NULL, error_to_text_send_error (error), NULL);
530 g_error_free (error);
533 tp_chat_set_delivery_status (self, token,
534 EMPATHY_DELIVERY_STATUS_SENDING);
540 TpChannelChatState state;
544 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
545 EmpathyContact *contact,
550 TpChannelChatState state;
553 DEBUG ("Error: %s", error->message);
557 state = GPOINTER_TO_UINT (user_data);
558 DEBUG ("Chat state changed for %s (%d): %d",
559 empathy_contact_get_alias (contact),
560 empathy_contact_get_handle (contact), state);
562 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
566 tp_chat_state_changed_cb (TpChannel *channel,
568 TpChannelChatState state,
571 TpConnection *connection = tp_channel_borrow_connection (
574 empathy_tp_contact_factory_get_from_handle (connection, handle,
575 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
576 NULL, G_OBJECT (self));
580 list_pending_messages (EmpathyTpChat *self)
584 messages = tp_text_channel_get_pending_messages (
585 TP_TEXT_CHANNEL (self));
587 for (l = messages; l != NULL; l = g_list_next (l)) {
588 TpMessage *message = l->data;
590 handle_incoming_message (self, message, FALSE);
593 g_list_free (messages);
597 update_subject (EmpathyTpChat *self,
598 GHashTable *properties)
600 EmpathyTpChatPrivate *priv = self->priv;
601 gboolean can_set, valid;
602 const gchar *subject;
604 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
606 priv->can_set_subject = can_set;
609 subject = tp_asv_get_string (properties, "Subject");
610 if (subject != NULL) {
611 g_free (priv->subject);
612 priv->subject = g_strdup (subject);
613 g_object_notify (G_OBJECT (self), "subject");
616 /* TODO: track Actor and Timestamp. */
620 tp_chat_get_all_subject_cb (TpProxy *proxy,
621 GHashTable *properties,
623 gpointer user_data G_GNUC_UNUSED,
626 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
627 EmpathyTpChatPrivate *priv = self->priv;
630 DEBUG ("Error fetching subject: %s", error->message);
634 priv->supports_subject = TRUE;
635 update_subject (self, properties);
639 update_title (EmpathyTpChat *self,
640 GHashTable *properties)
642 EmpathyTpChatPrivate *priv = self->priv;
643 const gchar *title = tp_asv_get_string (properties, "Title");
646 if (tp_str_empty (title)) {
650 g_free (priv->title);
651 priv->title = g_strdup (title);
652 g_object_notify (G_OBJECT (self), "title");
657 tp_chat_get_all_room_config_cb (TpProxy *proxy,
658 GHashTable *properties,
660 gpointer user_data G_GNUC_UNUSED,
663 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
666 DEBUG ("Error fetching room config: %s", error->message);
670 update_title (self, properties);
674 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
675 const gchar *interface_name,
677 const gchar **invalidated,
681 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
683 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT)) {
684 update_subject (self, changed);
687 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG)) {
688 update_title (self, changed);
693 empathy_tp_chat_set_subject (EmpathyTpChat *self,
694 const gchar *subject)
696 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
699 "while setting subject", NULL,
704 empathy_tp_chat_get_title (EmpathyTpChat *self)
706 EmpathyTpChatPrivate *priv = self->priv;
712 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
714 EmpathyTpChatPrivate *priv = self->priv;
716 return priv->supports_subject;
720 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
722 EmpathyTpChatPrivate *priv = self->priv;
724 return priv->can_set_subject;
728 empathy_tp_chat_get_subject (EmpathyTpChat *self)
730 EmpathyTpChatPrivate *priv = self->priv;
732 return priv->subject;
736 tp_chat_dispose (GObject *object)
738 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
740 tp_clear_object (&self->priv->account);
741 tp_clear_object (&self->priv->remote_contact);
742 tp_clear_object (&self->priv->user);
744 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
745 g_queue_clear (self->priv->messages_queue);
747 g_queue_foreach (self->priv->pending_messages_queue,
748 (GFunc) g_object_unref, NULL);
749 g_queue_clear (self->priv->pending_messages_queue);
751 tp_clear_object (&self->priv->ready_result);
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 EmpathyTpChat *self = (EmpathyTpChat *) object;
762 DEBUG ("Finalize: %p", object);
764 g_queue_free (self->priv->messages_queue);
765 g_queue_free (self->priv->pending_messages_queue);
766 g_hash_table_unref (self->priv->messages_being_sent);
768 g_free (self->priv->title);
769 g_free (self->priv->subject);
771 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
775 check_almost_ready (EmpathyTpChat *self)
777 TpChannel *channel = (TpChannel *) self;
779 if (self->priv->ready_result == NULL)
782 if (self->priv->user == NULL)
785 /* We need either the members (room) or the remote contact (private chat).
786 * If the chat is protected by a password we can't get these information so
787 * consider the chat as ready so it can be presented to the user. */
788 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
789 self->priv->remote_contact == NULL)
792 g_assert (tp_proxy_is_prepared (self,
793 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
795 tp_g_signal_connect_object (self, "message-received",
796 G_CALLBACK (message_received_cb), self, 0);
797 tp_g_signal_connect_object (self, "pending-message-removed",
798 G_CALLBACK (pending_message_removed_cb), self, 0);
800 list_pending_messages (self);
802 tp_g_signal_connect_object (self, "message-sent",
803 G_CALLBACK (message_sent_cb), self, 0);
805 tp_g_signal_connect_object (self, "chat-state-changed",
806 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
812 tp_chat_got_added_contacts_cb (TpConnection *connection,
814 EmpathyContact * const * contacts,
816 const TpHandle *failed,
821 EmpathyTpChat *self = (EmpathyTpChat *) chat;
823 const TpIntSet *members;
825 EmpathyContact *contact;
828 DEBUG ("Error: %s", error->message);
832 members = tp_channel_group_get_members ((TpChannel *) self);
833 for (i = 0; i < n_contacts; i++) {
834 contact = contacts[i];
835 handle = empathy_contact_get_handle (contact);
837 /* Make sure the contact is still member */
838 if (tp_intset_is_member (members, handle)) {
839 self->priv->members = g_list_prepend (self->priv->members,
840 g_object_ref (contact));
841 g_signal_emit_by_name (chat, "members-changed",
842 contact, NULL, 0, NULL, TRUE);
846 check_almost_ready (EMPATHY_TP_CHAT (chat));
849 static EmpathyContact *
850 chat_lookup_contact (EmpathyTpChat *self,
856 for (l = self->priv->members; l; l = l->next) {
857 EmpathyContact *c = l->data;
859 if (empathy_contact_get_handle (c) != handle) {
864 /* Caller takes the reference. */
865 self->priv->members = g_list_delete_link (self->priv->members, l);
883 static ContactRenameData *
884 contact_rename_data_new (TpHandle handle,
886 const gchar* message)
888 ContactRenameData *data = g_new (ContactRenameData, 1);
889 data->old_handle = handle;
890 data->reason = reason;
891 data->message = g_strdup (message);
897 contact_rename_data_free (ContactRenameData* data)
899 g_free (data->message);
904 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
906 EmpathyContact * const * contacts,
908 const TpHandle *failed,
913 EmpathyTpChat *self = (EmpathyTpChat *) chat;
914 const TpIntSet *members;
916 EmpathyContact *old = NULL, *new = NULL;
917 ContactRenameData *rename_data = (ContactRenameData *) user_data;
920 DEBUG ("Error: %s", error->message);
924 /* renamed members can only be delivered one at a time */
925 g_warn_if_fail (n_contacts == 1);
929 members = tp_channel_group_get_members ((TpChannel *) self);
930 handle = empathy_contact_get_handle (new);
932 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
934 /* Make sure the contact is still member */
935 if (tp_intset_is_member (members, handle)) {
936 self->priv->members = g_list_prepend (self->priv->members,
940 g_signal_emit_by_name (self, "member-renamed",
941 old, new, rename_data->reason,
942 rename_data->message);
943 g_object_unref (old);
947 if (self->priv->user == old) {
948 /* We change our nick */
949 tp_clear_object (&self->priv->user);
950 self->priv->user = g_object_ref (new);
953 check_almost_ready (self);
958 tp_chat_group_members_changed_cb (TpChannel *channel,
962 GArray *local_pending,
963 GArray *remote_pending,
968 EmpathyContact *contact;
969 EmpathyContact *actor_contact = NULL;
971 ContactRenameData *rename_data;
973 TpConnection *connection = tp_channel_borrow_connection (
976 /* Contact renamed */
977 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
978 /* there can only be a single 'added' and a single 'removed' handle */
979 if (removed->len != 1 || added->len != 1) {
980 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
981 added->len, removed->len);
985 old_handle = g_array_index (removed, guint, 0);
987 rename_data = contact_rename_data_new (old_handle, reason, message);
988 empathy_tp_contact_factory_get_from_handles (connection,
989 added->len, (TpHandle *) added->data,
990 tp_chat_got_renamed_contacts_cb,
991 rename_data, (GDestroyNotify) contact_rename_data_free,
997 actor_contact = chat_lookup_contact (self, actor, FALSE);
998 if (actor_contact == NULL) {
999 /* FIXME: handle this a tad more gracefully: perhaps
1000 * the actor was a server op. We could use the
1001 * contact-ids detail of MembersChangedDetailed.
1003 DEBUG ("actor %u not a channel member", actor);
1007 /* Remove contacts that are not members anymore */
1008 for (i = 0; i < removed->len; i++) {
1009 contact = chat_lookup_contact (self,
1010 g_array_index (removed, TpHandle, i), TRUE);
1012 if (contact != NULL) {
1013 g_signal_emit_by_name (self, "members-changed", contact,
1014 actor_contact, reason, message,
1016 g_object_unref (contact);
1020 /* Request added contacts */
1021 if (added->len > 0) {
1022 empathy_tp_contact_factory_get_from_handles (connection,
1023 added->len, (TpHandle *) added->data,
1024 tp_chat_got_added_contacts_cb, NULL, NULL,
1028 if (actor_contact != NULL) {
1029 g_object_unref (actor_contact);
1034 tp_chat_got_remote_contact_cb (TpConnection *connection,
1035 EmpathyContact *contact,
1036 const GError *error,
1040 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1043 DEBUG ("Error: %s", error->message);
1044 empathy_tp_chat_leave (self, "");
1048 self->priv->remote_contact = g_object_ref (contact);
1049 g_object_notify (chat, "remote-contact");
1051 check_almost_ready (self);
1055 tp_chat_got_self_contact_cb (TpConnection *connection,
1056 EmpathyContact *contact,
1057 const GError *error,
1061 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1064 DEBUG ("Error: %s", error->message);
1065 empathy_tp_chat_leave (self, "");
1069 self->priv->user = g_object_ref (contact);
1070 empathy_contact_set_is_user (self->priv->user, TRUE);
1071 check_almost_ready (self);
1075 tp_chat_get_property (GObject *object,
1080 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1084 g_value_set_object (value, self->priv->account);
1086 case PROP_REMOTE_CONTACT:
1087 g_value_set_object (value, self->priv->remote_contact);
1089 case PROP_N_MESSAGES_SENDING:
1090 g_value_set_uint (value,
1091 g_hash_table_size (self->priv->messages_being_sent));
1094 g_value_set_string (value,
1095 empathy_tp_chat_get_title (self));
1098 g_value_set_string (value,
1099 empathy_tp_chat_get_subject (self));
1102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1108 tp_chat_set_property (GObject *object,
1110 const GValue *value,
1113 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1117 self->priv->account = g_value_dup_object (value);
1120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1130 static const TpProxyFeature *
1131 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1133 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1134 static GQuark need[2] = {0, 0};
1136 if (G_LIKELY (features[0].name != 0))
1139 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1140 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1141 features[FEAT_READY].depends_on = need;
1142 features[FEAT_READY].prepare_async =
1143 tp_chat_prepare_ready_async;
1145 /* assert that the terminator at the end is there */
1146 g_assert (features[N_FEAT].name == 0);
1152 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1155 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1157 object_class->dispose = tp_chat_dispose;
1158 object_class->finalize = tp_chat_finalize;
1159 object_class->get_property = tp_chat_get_property;
1160 object_class->set_property = tp_chat_set_property;
1162 proxy_class->list_features = tp_chat_list_features;
1164 g_object_class_install_property (object_class,
1166 g_param_spec_object ("account",
1168 "the account associated with the chat",
1171 G_PARAM_CONSTRUCT_ONLY |
1172 G_PARAM_STATIC_STRINGS));
1174 g_object_class_install_property (object_class,
1175 PROP_REMOTE_CONTACT,
1176 g_param_spec_object ("remote-contact",
1177 "The remote contact",
1178 "The remote contact if there is no group iface on the channel",
1179 EMPATHY_TYPE_CONTACT,
1182 g_object_class_install_property (object_class,
1183 PROP_N_MESSAGES_SENDING,
1184 g_param_spec_uint ("n-messages-sending",
1185 "Num Messages Sending",
1186 "The number of messages being sent",
1190 g_object_class_install_property (object_class,
1192 g_param_spec_string ("title",
1194 "A human-readable name for the room, if any",
1197 G_PARAM_STATIC_STRINGS));
1199 g_object_class_install_property (object_class,
1201 g_param_spec_string ("subject",
1203 "The room's current subject, if any",
1206 G_PARAM_STATIC_STRINGS));
1209 signals[MESSAGE_RECEIVED] =
1210 g_signal_new ("message-received-empathy",
1211 G_TYPE_FROM_CLASS (klass),
1215 g_cclosure_marshal_generic,
1217 1, EMPATHY_TYPE_MESSAGE);
1219 signals[SEND_ERROR] =
1220 g_signal_new ("send-error",
1221 G_TYPE_FROM_CLASS (klass),
1225 g_cclosure_marshal_generic,
1227 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1229 signals[CHAT_STATE_CHANGED] =
1230 g_signal_new ("chat-state-changed-empathy",
1231 G_TYPE_FROM_CLASS (klass),
1235 g_cclosure_marshal_generic,
1237 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1239 signals[MESSAGE_ACKNOWLEDGED] =
1240 g_signal_new ("message-acknowledged",
1241 G_TYPE_FROM_CLASS (klass),
1245 g_cclosure_marshal_generic,
1247 1, EMPATHY_TYPE_MESSAGE);
1249 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1253 empathy_tp_chat_init (EmpathyTpChat *self)
1255 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1256 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1258 self->priv->messages_queue = g_queue_new ();
1259 self->priv->pending_messages_queue = g_queue_new ();
1260 self->priv->messages_being_sent = g_hash_table_new_full (
1261 g_str_hash, g_str_equal, g_free, NULL);
1265 tp_chat_iface_init (EmpathyContactListIface *iface)
1267 iface->add = tp_chat_add;
1268 iface->remove = tp_chat_remove;
1269 iface->get_members = tp_chat_get_members;
1273 empathy_tp_chat_new (
1274 TpSimpleClientFactory *factory,
1277 const gchar *object_path,
1278 const GHashTable *immutable_properties)
1280 TpProxy *conn_proxy = (TpProxy *) conn;
1282 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1283 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1284 g_return_val_if_fail (immutable_properties != NULL, NULL);
1286 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1290 "dbus-daemon", conn_proxy->dbus_daemon,
1291 "bus-name", conn_proxy->bus_name,
1292 "object-path", object_path,
1293 "channel-properties", immutable_properties,
1298 empathy_tp_chat_get_id (EmpathyTpChat *self)
1302 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1304 id = tp_channel_get_identifier ((TpChannel *) self);
1305 if (!EMP_STR_EMPTY (id))
1307 else if (self->priv->remote_contact)
1308 return empathy_contact_get_id (self->priv->remote_contact);
1315 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1317 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1319 return self->priv->remote_contact;
1323 empathy_tp_chat_get_account (EmpathyTpChat *self)
1325 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1327 return self->priv->account;
1331 empathy_tp_chat_send (EmpathyTpChat *self,
1334 gchar *message_body;
1336 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1337 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1339 message_body = tp_message_to_text (message, NULL);
1341 DEBUG ("Sending message: %s", message_body);
1343 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1344 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1345 message_send_cb, self);
1347 g_free (message_body);
1351 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1353 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1355 return self->priv->pending_messages_queue->head;
1359 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1360 EmpathyMessage *message) {
1363 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1365 if (!empathy_message_is_incoming (message))
1368 tp_msg = empathy_message_get_tp_message (message);
1369 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1370 tp_msg, NULL, NULL);
1374 * empathy_tp_chat_can_add_contact:
1376 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1377 * That is if this chat is a 1-to-1 channel that can be upgraded to
1378 * a MUC using the Conference interface or if the channel is a MUC.
1381 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1383 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1385 return self->priv->can_upgrade_to_muc ||
1386 tp_proxy_has_interface_by_id (self,
1387 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1391 tp_channel_leave_async_cb (GObject *source_object,
1395 GError *error = NULL;
1397 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1398 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1400 g_error_free (error);
1405 empathy_tp_chat_leave (EmpathyTpChat *self,
1406 const gchar *message)
1408 TpChannel *channel = (TpChannel *) self;
1410 DEBUG ("Leaving channel %s with message \"%s\"",
1411 tp_channel_get_identifier (channel), message);
1413 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1414 message, tp_channel_leave_async_cb, self);
1418 add_members_cb (TpChannel *proxy,
1419 const GError *error,
1421 GObject *weak_object)
1423 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1425 if (error != NULL) {
1426 DEBUG ("Failed to join chat (%s): %s",
1427 tp_channel_get_identifier ((TpChannel *) self), error->message);
1432 empathy_tp_chat_join (EmpathyTpChat *self)
1434 TpHandle self_handle;
1437 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1439 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1440 g_array_append_val (members, self_handle);
1442 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1443 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1445 g_array_unref (members);
1449 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1452 TpHandle self_handle;
1454 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1457 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1458 if (self_handle == 0)
1461 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1462 inviter, NULL, NULL);
1466 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1467 EmpathyContact *contact)
1469 return tp_channel_get_chat_state ((TpChannel *) self,
1470 empathy_contact_get_handle (contact));
1474 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1476 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1478 return self->priv->user;
1482 empathy_tp_chat_get_feature_ready (void)
1484 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1488 tp_chat_prepare_ready_async (TpProxy *proxy,
1489 const TpProxyFeature *feature,
1490 GAsyncReadyCallback callback,
1493 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1494 TpChannel *channel = (TpChannel *) proxy;
1495 TpConnection *connection;
1496 gboolean listen_for_dbus_properties_changed = FALSE;
1498 g_assert (self->priv->ready_result == NULL);
1499 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1500 callback, user_data, tp_chat_prepare_ready_async);
1502 connection = tp_channel_borrow_connection (channel);
1504 if (tp_proxy_has_interface_by_id (self,
1505 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1506 const TpIntSet *members;
1510 /* Get self contact from the group's self handle */
1511 handle = tp_channel_group_get_self_handle (channel);
1512 empathy_tp_contact_factory_get_from_handle (connection,
1513 handle, tp_chat_got_self_contact_cb,
1514 NULL, NULL, G_OBJECT (self));
1516 /* Get initial member contacts */
1517 members = tp_channel_group_get_members (channel);
1518 handles = tp_intset_to_array (members);
1519 empathy_tp_contact_factory_get_from_handles (connection,
1520 handles->len, (TpHandle *) handles->data,
1521 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1523 self->priv->can_upgrade_to_muc = FALSE;
1525 tp_g_signal_connect_object (self, "group-members-changed",
1526 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1528 TpCapabilities *caps;
1533 /* Get the self contact from the connection's self handle */
1534 handle = tp_connection_get_self_handle (connection);
1535 empathy_tp_contact_factory_get_from_handle (connection,
1536 handle, tp_chat_got_self_contact_cb,
1537 NULL, NULL, G_OBJECT (self));
1539 /* Get the remote contact */
1540 handle = tp_channel_get_handle (channel, NULL);
1541 empathy_tp_contact_factory_get_from_handle (connection,
1542 handle, tp_chat_got_remote_contact_cb,
1543 NULL, NULL, G_OBJECT (self));
1545 caps = tp_connection_get_capabilities (connection);
1546 g_assert (caps != NULL);
1548 classes = tp_capabilities_get_channel_classes (caps);
1550 for (i = 0; i < classes->len; i++) {
1551 GValueArray *array = g_ptr_array_index (classes, i);
1552 const char **oprops = g_value_get_boxed (
1553 g_value_array_get_nth (array, 1));
1555 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1556 self->priv->can_upgrade_to_muc = TRUE;
1562 if (tp_proxy_has_interface_by_id (self,
1563 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT)) {
1564 tp_cli_dbus_properties_call_get_all (channel, -1,
1565 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1566 tp_chat_get_all_subject_cb,
1569 listen_for_dbus_properties_changed = TRUE;
1572 if (tp_proxy_has_interface_by_id (self,
1573 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG)) {
1574 tp_cli_dbus_properties_call_get_all (channel, -1,
1575 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1576 tp_chat_get_all_room_config_cb,
1579 listen_for_dbus_properties_changed = TRUE;
1582 if (listen_for_dbus_properties_changed) {
1583 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1584 tp_chat_dbus_properties_changed_cb,
1586 G_OBJECT (self), NULL);