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-request-util.h"
32 #include "empathy-time.h"
33 #include "empathy-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
36 #include "empathy-debug.h"
38 struct _EmpathyTpChatPrivate
42 EmpathyContact *remote_contact;
44 /* Queue of messages not signalled yet */
45 GQueue *messages_queue;
46 /* Queue of messages signalled but not acked yet */
47 GQueue *pending_messages_queue;
50 gboolean supports_subject;
51 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;
73 PROP_N_MESSAGES_SENDING,
87 static guint signals[LAST_SIGNAL];
89 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
92 tp_chat_set_delivery_status (EmpathyTpChat *self,
94 EmpathyDeliveryStatus delivery_status)
96 TpDeliveryReportingSupportFlags flags =
97 tp_text_channel_get_delivery_reporting_support (
98 TP_TEXT_CHANNEL (self));
100 /* channel must support receiving failures and successes */
101 if (!tp_str_empty (token) &&
102 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
103 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
105 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
107 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);
141 create_conference_cb (GObject *source,
142 GAsyncResult *result,
145 GError *error = NULL;
147 if (!tp_account_channel_request_create_channel_finish (
148 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
150 DEBUG ("Failed to create conference channel: %s", error->message);
151 g_error_free (error);
156 empathy_tp_chat_add (EmpathyTpChat *self,
157 EmpathyContact *contact,
158 const gchar *message)
160 TpChannel *channel = (TpChannel *) self;
162 if (tp_proxy_has_interface_by_id (self,
163 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
166 GArray handles = {(gchar *) &handle, 1};
168 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
170 handle = empathy_contact_get_handle (contact);
171 tp_cli_channel_interface_group_call_add_members (channel,
172 -1, &handles, NULL, NULL, NULL, NULL, NULL);
174 else if (self->priv->can_upgrade_to_muc)
176 TpAccountChannelRequest *req;
178 const char *object_path;
179 GPtrArray channels = { (gpointer *) &object_path, 1 };
180 const char *invitees[2] = { NULL, };
182 invitees[0] = empathy_contact_get_id (contact);
183 object_path = tp_proxy_get_object_path (self);
186 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
187 TP_IFACE_CHANNEL_TYPE_TEXT,
188 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
190 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
191 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
192 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
193 G_TYPE_STRV, invitees,
194 /* FIXME: InvitationMessage ? */
197 req = tp_account_channel_request_new (self->priv->account, props,
198 TP_USER_ACTION_TIME_NOT_USER_ACTION);
200 /* Although this is a MUC, it's anonymous, so CreateChannel is
202 tp_account_channel_request_create_channel_async (req,
203 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
205 g_object_unref (req);
206 g_hash_table_unref (props);
210 g_warning ("Cannot add to this channel");
215 empathy_tp_chat_get_members (EmpathyTpChat *self)
217 GList *members = NULL;
219 if (self->priv->members)
221 members = g_list_copy (self->priv->members);
222 g_list_foreach (members, (GFunc) g_object_ref, NULL);
226 members = g_list_prepend (members, g_object_ref (self->priv->user));
228 if (self->priv->remote_contact != NULL)
229 members = g_list_prepend (members,
230 g_object_ref (self->priv->remote_contact));
237 check_ready (EmpathyTpChat *self)
239 if (self->priv->ready_result == NULL)
242 if (g_queue_get_length (self->priv->messages_queue) > 0)
247 g_simple_async_result_complete (self->priv->ready_result);
248 tp_clear_object (&self->priv->ready_result);
252 tp_chat_emit_queued_messages (EmpathyTpChat *self)
254 EmpathyMessage *message;
256 /* Check if we can now emit some queued messages */
257 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL)
259 if (empathy_message_get_sender (message) == NULL)
262 DEBUG ("Queued message ready");
263 g_queue_pop_head (self->priv->messages_queue);
264 g_queue_push_tail (self->priv->pending_messages_queue, message);
265 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
272 tp_chat_got_sender_cb (TpConnection *connection,
273 EmpathyContact *contact,
278 EmpathyTpChat *self = (EmpathyTpChat *) chat;
282 DEBUG ("Error: %s", error->message);
283 /* Do not block the message queue, just drop this message */
284 g_queue_remove (self->priv->messages_queue, message);
288 empathy_message_set_sender (message, contact);
291 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
295 tp_chat_build_message (EmpathyTpChat *self,
299 EmpathyMessage *message;
302 message = empathy_message_new_from_tp_message (msg, incoming);
303 /* FIXME: this is actually a lie for incoming messages. */
304 empathy_message_set_receiver (message, self->priv->user);
306 g_queue_push_tail (self->priv->messages_queue, message);
308 sender = tp_signalled_message_get_sender (msg);
309 g_assert (sender != NULL);
311 if (tp_contact_get_handle (sender) == 0)
313 empathy_message_set_sender (message, self->priv->user);
314 tp_chat_emit_queued_messages (self);
318 TpConnection *connection = tp_channel_borrow_connection (
321 empathy_tp_contact_factory_get_from_handle (connection,
322 tp_contact_get_handle (sender),
323 tp_chat_got_sender_cb,
324 message, NULL, G_OBJECT (self));
329 handle_delivery_report (EmpathyTpChat *self,
332 TpDeliveryStatus delivery_status;
333 const GHashTable *header;
334 TpChannelTextSendError delivery_error;
337 const gchar *message_body = NULL;
338 const gchar *delivery_dbus_error;
339 const gchar *delivery_token = NULL;
341 header = tp_message_peek (message, 0);
345 delivery_token = tp_asv_get_string (header, "delivery-token");
346 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
352 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
354 DEBUG ("Accepted %s", delivery_token);
355 tp_chat_set_delivery_status (self, delivery_token,
356 EMPATHY_DELIVERY_STATUS_ACCEPTED);
359 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
361 DEBUG ("Delivered %s", delivery_token);
362 tp_chat_set_delivery_status (self, delivery_token,
363 EMPATHY_DELIVERY_STATUS_NONE);
366 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
367 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
372 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
374 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
376 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
378 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
379 * TpMessage. (fdo #35884) */
380 echo = tp_asv_get_boxed (header, "delivery-echo",
381 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
382 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))
410 handle_delivery_report (self, message);
414 message_body = tp_message_to_text (message, NULL);
416 DEBUG ("Message %s (channel %s): %s",
417 pending ? "pending" : "received",
418 tp_proxy_get_object_path (self), message_body);
420 if (message_body == NULL)
422 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
424 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
425 message, NULL, NULL);
429 tp_chat_build_message (self, message, TRUE);
431 g_free (message_body);
435 message_received_cb (TpTextChannel *channel,
439 handle_incoming_message (self, message, FALSE);
443 find_pending_message_func (gconstpointer a,
446 EmpathyMessage *msg = (EmpathyMessage *) a;
447 TpMessage *message = (TpMessage *) b;
449 if (empathy_message_get_tp_message (msg) == message)
456 pending_message_removed_cb (TpTextChannel *channel,
462 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
463 find_pending_message_func);
468 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
470 g_object_unref (m->data);
471 g_queue_delete_link (self->priv->pending_messages_queue, m);
475 message_sent_cb (TpTextChannel *channel,
477 TpMessageSendingFlags flags,
483 message_body = tp_message_to_text (message, NULL);
485 DEBUG ("Message sent: %s", message_body);
487 tp_chat_build_message (self, message, FALSE);
489 g_free (message_body);
492 static TpChannelTextSendError
493 error_to_text_send_error (GError *error)
495 if (error->domain != TP_ERRORS)
496 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
500 case TP_ERROR_OFFLINE:
501 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
502 case TP_ERROR_INVALID_HANDLE:
503 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
504 case TP_ERROR_PERMISSION_DENIED:
505 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
506 case TP_ERROR_NOT_IMPLEMENTED:
507 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
510 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
514 message_send_cb (GObject *source,
515 GAsyncResult *result,
518 EmpathyTpChat *self = user_data;
519 TpTextChannel *channel = (TpTextChannel *) source;
521 GError *error = NULL;
523 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
525 DEBUG ("Error: %s", error->message);
527 /* FIXME: we should use the body of the message as first argument of the
528 * signal but can't easily get it as we just get a user_data pointer. Once
529 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
530 * to use the user_data pointer to pass the message and fix this. */
531 g_signal_emit (self, signals[SEND_ERROR], 0,
532 NULL, error_to_text_send_error (error), NULL);
534 g_error_free (error);
537 tp_chat_set_delivery_status (self, token,
538 EMPATHY_DELIVERY_STATUS_SENDING);
544 TpChannelChatState state;
548 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
549 EmpathyContact *contact,
554 TpChannelChatState state;
558 DEBUG ("Error: %s", error->message);
562 state = GPOINTER_TO_UINT (user_data);
563 DEBUG ("Chat state changed for %s (%d): %d",
564 empathy_contact_get_alias (contact),
565 empathy_contact_get_handle (contact), state);
567 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
571 tp_chat_state_changed_cb (TpChannel *channel,
573 TpChannelChatState state,
576 TpConnection *connection = tp_channel_borrow_connection (
579 empathy_tp_contact_factory_get_from_handle (connection, handle,
580 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
581 NULL, G_OBJECT (self));
585 list_pending_messages (EmpathyTpChat *self)
589 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
591 for (l = messages; l != NULL; l = g_list_next (l))
593 TpMessage *message = l->data;
595 handle_incoming_message (self, message, FALSE);
598 g_list_free (messages);
602 update_subject (EmpathyTpChat *self,
603 GHashTable *properties)
605 gboolean can_set, valid;
606 const gchar *subject;
608 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
610 self->priv->can_set_subject = can_set;
612 subject = tp_asv_get_string (properties, "Subject");
617 g_free (self->priv->subject);
618 self->priv->subject = g_strdup (subject);
620 /* If the actor is included with this update, use it;
621 * otherwise, clear it to avoid showing stale information.
622 * Why might it not be included? When you join an IRC channel,
623 * you get a pair of messages: first, the current topic; next,
624 * who set it, and when. Idle reports these in two separate
627 actor = tp_asv_get_string (properties, "Actor");
628 g_free (self->priv->subject_actor);
629 self->priv->subject_actor = g_strdup (actor);
631 g_object_notify (G_OBJECT (self), "subject");
634 /* TODO: track Timestamp. */
638 tp_chat_get_all_subject_cb (TpProxy *proxy,
639 GHashTable *properties,
641 gpointer user_data G_GNUC_UNUSED,
644 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
648 DEBUG ("Error fetching subject: %s", error->message);
652 self->priv->supports_subject = TRUE;
653 update_subject (self, properties);
657 update_title (EmpathyTpChat *self,
658 GHashTable *properties)
660 const gchar *title = tp_asv_get_string (properties, "Title");
664 if (tp_str_empty (title))
667 g_free (self->priv->title);
668 self->priv->title = g_strdup (title);
669 g_object_notify (G_OBJECT (self), "title");
674 tp_chat_get_all_room_config_cb (TpProxy *proxy,
675 GHashTable *properties,
677 gpointer user_data G_GNUC_UNUSED,
680 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
684 DEBUG ("Error fetching room config: %s", error->message);
688 update_title (self, properties);
692 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
693 const gchar *interface_name,
695 const gchar **invalidated,
699 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
701 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
702 update_subject (self, changed);
704 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
705 update_title (self, changed);
709 empathy_tp_chat_set_subject (EmpathyTpChat *self,
710 const gchar *subject)
712 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
713 subject, tp_chat_async_cb, "while setting subject", NULL,
718 empathy_tp_chat_get_title (EmpathyTpChat *self)
720 return self->priv->title;
724 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
726 return self->priv->supports_subject;
730 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
732 return self->priv->can_set_subject;
736 empathy_tp_chat_get_subject (EmpathyTpChat *self)
738 return self->priv->subject;
742 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
744 return self->priv->subject_actor;
748 tp_chat_dispose (GObject *object)
750 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
752 tp_clear_object (&self->priv->account);
753 tp_clear_object (&self->priv->remote_contact);
754 tp_clear_object (&self->priv->user);
756 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
757 g_queue_clear (self->priv->messages_queue);
759 g_queue_foreach (self->priv->pending_messages_queue,
760 (GFunc) g_object_unref, NULL);
761 g_queue_clear (self->priv->pending_messages_queue);
763 tp_clear_object (&self->priv->ready_result);
765 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
766 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
770 tp_chat_finalize (GObject *object)
772 EmpathyTpChat *self = (EmpathyTpChat *) object;
774 DEBUG ("Finalize: %p", object);
776 g_queue_free (self->priv->messages_queue);
777 g_queue_free (self->priv->pending_messages_queue);
778 g_hash_table_unref (self->priv->messages_being_sent);
780 g_free (self->priv->title);
781 g_free (self->priv->subject);
782 g_free (self->priv->subject_actor);
784 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
788 check_almost_ready (EmpathyTpChat *self)
790 TpChannel *channel = (TpChannel *) self;
792 if (self->priv->ready_result == NULL)
795 if (self->priv->user == NULL)
798 /* We need either the members (room) or the remote contact (private chat).
799 * If the chat is protected by a password we can't get these information so
800 * consider the chat as ready so it can be presented to the user. */
801 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
802 self->priv->remote_contact == NULL)
805 g_assert (tp_proxy_is_prepared (self,
806 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
808 tp_g_signal_connect_object (self, "message-received",
809 G_CALLBACK (message_received_cb), self, 0);
810 tp_g_signal_connect_object (self, "pending-message-removed",
811 G_CALLBACK (pending_message_removed_cb), self, 0);
813 list_pending_messages (self);
815 tp_g_signal_connect_object (self, "message-sent",
816 G_CALLBACK (message_sent_cb), self, 0);
818 tp_g_signal_connect_object (self, "chat-state-changed",
819 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
825 tp_chat_got_added_contacts_cb (TpConnection *connection,
827 EmpathyContact * const * contacts,
829 const TpHandle *failed,
834 EmpathyTpChat *self = (EmpathyTpChat *) chat;
836 const TpIntSet *members;
838 EmpathyContact *contact;
842 DEBUG ("Error: %s", error->message);
846 members = tp_channel_group_get_members ((TpChannel *) self);
847 for (i = 0; i < n_contacts; i++)
849 contact = contacts[i];
850 handle = empathy_contact_get_handle (contact);
852 /* Make sure the contact is still member */
853 if (tp_intset_is_member (members, handle))
855 self->priv->members = g_list_prepend (self->priv->members,
856 g_object_ref (contact));
857 g_signal_emit_by_name (chat, "members-changed",
858 contact, NULL, 0, NULL, TRUE);
862 check_almost_ready (EMPATHY_TP_CHAT (chat));
865 static EmpathyContact *
866 chat_lookup_contact (EmpathyTpChat *self,
872 for (l = self->priv->members; l; l = l->next)
874 EmpathyContact *c = l->data;
876 if (empathy_contact_get_handle (c) != handle)
881 /* Caller takes the reference. */
882 self->priv->members = g_list_delete_link (self->priv->members, l);
902 static ContactRenameData *
903 contact_rename_data_new (TpHandle handle,
905 const gchar* message)
907 ContactRenameData *data = g_new (ContactRenameData, 1);
908 data->old_handle = handle;
909 data->reason = reason;
910 data->message = g_strdup (message);
916 contact_rename_data_free (ContactRenameData* data)
918 g_free (data->message);
923 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
925 EmpathyContact * const * contacts,
927 const TpHandle *failed,
932 EmpathyTpChat *self = (EmpathyTpChat *) chat;
933 const TpIntSet *members;
935 EmpathyContact *old = NULL, *new = NULL;
936 ContactRenameData *rename_data = (ContactRenameData *) user_data;
940 DEBUG ("Error: %s", error->message);
944 /* renamed members can only be delivered one at a time */
945 g_warn_if_fail (n_contacts == 1);
949 members = tp_channel_group_get_members ((TpChannel *) self);
950 handle = empathy_contact_get_handle (new);
952 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
954 /* Make sure the contact is still member */
955 if (tp_intset_is_member (members, handle))
957 self->priv->members = g_list_prepend (self->priv->members,
962 g_signal_emit_by_name (self, "member-renamed",
963 old, new, rename_data->reason,
964 rename_data->message);
965 g_object_unref (old);
969 if (self->priv->user == old)
971 /* We change our nick */
972 tp_clear_object (&self->priv->user);
973 self->priv->user = g_object_ref (new);
974 g_object_notify (chat, "self-contact");
977 check_almost_ready (self);
982 tp_chat_group_members_changed_cb (TpChannel *channel,
986 GArray *local_pending,
987 GArray *remote_pending,
992 EmpathyContact *contact;
993 EmpathyContact *actor_contact = NULL;
995 ContactRenameData *rename_data;
997 TpConnection *connection = tp_channel_borrow_connection (
1000 /* Contact renamed */
1001 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
1003 /* there can only be a single 'added' and a single 'removed' handle */
1004 if (removed->len != 1 || added->len != 1)
1006 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1007 added->len, removed->len);
1011 old_handle = g_array_index (removed, guint, 0);
1013 rename_data = contact_rename_data_new (old_handle, reason, message);
1014 empathy_tp_contact_factory_get_from_handles (connection,
1015 added->len, (TpHandle *) added->data,
1016 tp_chat_got_renamed_contacts_cb,
1017 rename_data, (GDestroyNotify) contact_rename_data_free,
1024 actor_contact = chat_lookup_contact (self, actor, FALSE);
1025 if (actor_contact == NULL)
1027 /* FIXME: handle this a tad more gracefully: perhaps
1028 * the actor was a server op. We could use the
1029 * contact-ids detail of MembersChangedDetailed.
1031 DEBUG ("actor %u not a channel member", actor);
1035 /* Remove contacts that are not members anymore */
1036 for (i = 0; i < removed->len; i++)
1038 contact = chat_lookup_contact (self,
1039 g_array_index (removed, TpHandle, i), TRUE);
1041 if (contact != NULL)
1043 g_signal_emit_by_name (self, "members-changed", contact,
1044 actor_contact, reason, message,
1046 g_object_unref (contact);
1050 /* Request added contacts */
1053 empathy_tp_contact_factory_get_from_handles (connection,
1054 added->len, (TpHandle *) added->data,
1055 tp_chat_got_added_contacts_cb, NULL, NULL,
1059 if (actor_contact != NULL)
1060 g_object_unref (actor_contact);
1064 tp_chat_got_remote_contact_cb (TpConnection *connection,
1065 EmpathyContact *contact,
1066 const GError *error,
1070 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1074 DEBUG ("Error: %s", error->message);
1075 empathy_tp_chat_leave (self, "");
1079 self->priv->remote_contact = g_object_ref (contact);
1080 g_object_notify (chat, "remote-contact");
1082 check_almost_ready (self);
1086 tp_chat_got_self_contact_cb (TpConnection *connection,
1087 EmpathyContact *contact,
1088 const GError *error,
1092 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1096 DEBUG ("Error: %s", error->message);
1097 empathy_tp_chat_leave (self, "");
1101 self->priv->user = g_object_ref (contact);
1102 empathy_contact_set_is_user (self->priv->user, TRUE);
1103 g_object_notify (chat, "self-contact");
1104 check_almost_ready (self);
1108 tp_chat_get_property (GObject *object,
1113 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1118 g_value_set_object (value, self->priv->account);
1120 case PROP_SELF_CONTACT:
1121 g_value_set_object (value, self->priv->user);
1123 case PROP_REMOTE_CONTACT:
1124 g_value_set_object (value, self->priv->remote_contact);
1126 case PROP_N_MESSAGES_SENDING:
1127 g_value_set_uint (value,
1128 g_hash_table_size (self->priv->messages_being_sent));
1131 g_value_set_string (value,
1132 empathy_tp_chat_get_title (self));
1135 g_value_set_string (value,
1136 empathy_tp_chat_get_subject (self));
1139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1145 tp_chat_set_property (GObject *object,
1147 const GValue *value,
1150 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1155 self->priv->account = g_value_dup_object (value);
1158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1168 static const TpProxyFeature *
1169 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1171 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1172 static GQuark need[2] = {0, 0};
1174 if (G_LIKELY (features[0].name != 0))
1177 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1178 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1179 features[FEAT_READY].depends_on = need;
1180 features[FEAT_READY].prepare_async =
1181 tp_chat_prepare_ready_async;
1183 /* assert that the terminator at the end is there */
1184 g_assert (features[N_FEAT].name == 0);
1190 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1192 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1193 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1195 object_class->dispose = tp_chat_dispose;
1196 object_class->finalize = tp_chat_finalize;
1197 object_class->get_property = tp_chat_get_property;
1198 object_class->set_property = tp_chat_set_property;
1200 proxy_class->list_features = tp_chat_list_features;
1202 g_object_class_install_property (object_class, PROP_ACCOUNT,
1203 g_param_spec_object ("account", "TpAccount",
1204 "the account associated with the chat",
1207 G_PARAM_CONSTRUCT_ONLY |
1208 G_PARAM_STATIC_STRINGS));
1211 * EmpathyTpChat:self-contact:
1213 * Not to be confused with TpChannel:group-self-contact.
1215 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1216 g_param_spec_object ("self-contact", "The local contact",
1217 "The EmpathyContact for the local user on this channel",
1218 EMPATHY_TYPE_CONTACT,
1221 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1222 g_param_spec_object ("remote-contact", "The remote contact",
1223 "The remote contact if there is no group iface on the channel",
1224 EMPATHY_TYPE_CONTACT,
1227 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1228 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1229 "The number of messages being sent",
1233 g_object_class_install_property (object_class, PROP_TITLE,
1234 g_param_spec_string ("title", "Title",
1235 "A human-readable name for the room, if any",
1238 G_PARAM_STATIC_STRINGS));
1240 g_object_class_install_property (object_class, PROP_SUBJECT,
1241 g_param_spec_string ("subject", "Subject",
1242 "The room's current subject, if any",
1245 G_PARAM_STATIC_STRINGS));
1248 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1249 G_TYPE_FROM_CLASS (klass),
1253 g_cclosure_marshal_generic,
1255 1, EMPATHY_TYPE_MESSAGE);
1257 signals[SEND_ERROR] = g_signal_new ("send-error",
1258 G_TYPE_FROM_CLASS (klass),
1262 g_cclosure_marshal_generic,
1264 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1266 signals[CHAT_STATE_CHANGED] = g_signal_new ("chat-state-changed-empathy",
1267 G_TYPE_FROM_CLASS (klass),
1271 g_cclosure_marshal_generic,
1273 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1275 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1276 G_TYPE_FROM_CLASS (klass),
1280 g_cclosure_marshal_generic,
1282 1, EMPATHY_TYPE_MESSAGE);
1284 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1288 empathy_tp_chat_init (EmpathyTpChat *self)
1290 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1291 EmpathyTpChatPrivate);
1293 self->priv->messages_queue = g_queue_new ();
1294 self->priv->pending_messages_queue = g_queue_new ();
1295 self->priv->messages_being_sent = g_hash_table_new_full (
1296 g_str_hash, g_str_equal, g_free, NULL);
1300 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1303 const gchar *object_path,
1304 const GHashTable *immutable_properties)
1306 TpProxy *conn_proxy = (TpProxy *) conn;
1308 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1309 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1310 g_return_val_if_fail (immutable_properties != NULL, NULL);
1312 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1316 "dbus-daemon", conn_proxy->dbus_daemon,
1317 "bus-name", conn_proxy->bus_name,
1318 "object-path", object_path,
1319 "channel-properties", immutable_properties,
1324 empathy_tp_chat_get_id (EmpathyTpChat *self)
1328 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1330 id = tp_channel_get_identifier ((TpChannel *) self);
1331 if (!EMP_STR_EMPTY (id))
1333 else if (self->priv->remote_contact)
1334 return empathy_contact_get_id (self->priv->remote_contact);
1341 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1343 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1345 return self->priv->remote_contact;
1349 empathy_tp_chat_get_account (EmpathyTpChat *self)
1351 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1353 return self->priv->account;
1357 empathy_tp_chat_send (EmpathyTpChat *self,
1360 gchar *message_body;
1362 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1363 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1365 message_body = tp_message_to_text (message, NULL);
1367 DEBUG ("Sending message: %s", message_body);
1369 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1370 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1371 message_send_cb, self);
1373 g_free (message_body);
1377 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1379 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1381 return self->priv->pending_messages_queue->head;
1385 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1386 EmpathyMessage *message)
1390 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1392 if (!empathy_message_is_incoming (message))
1395 tp_msg = empathy_message_get_tp_message (message);
1396 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1397 tp_msg, NULL, NULL);
1401 * empathy_tp_chat_can_add_contact:
1403 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1404 * That is if this chat is a 1-to-1 channel that can be upgraded to
1405 * a MUC using the Conference interface or if the channel is a MUC.
1408 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1410 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1412 return self->priv->can_upgrade_to_muc ||
1413 tp_proxy_has_interface_by_id (self,
1414 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1418 tp_channel_leave_async_cb (GObject *source_object,
1422 GError *error = NULL;
1424 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1426 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1428 g_error_free (error);
1433 empathy_tp_chat_leave (EmpathyTpChat *self,
1434 const gchar *message)
1436 TpChannel *channel = (TpChannel *) self;
1438 DEBUG ("Leaving channel %s with message \"%s\"",
1439 tp_channel_get_identifier (channel), message);
1441 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1442 message, tp_channel_leave_async_cb, self);
1446 add_members_cb (TpChannel *proxy,
1447 const GError *error,
1449 GObject *weak_object)
1451 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1455 DEBUG ("Failed to join chat (%s): %s",
1456 tp_channel_get_identifier ((TpChannel *) self), error->message);
1461 empathy_tp_chat_join (EmpathyTpChat *self)
1463 TpHandle self_handle;
1466 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1468 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1469 g_array_append_val (members, self_handle);
1471 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1,
1472 members, "", add_members_cb, NULL, NULL, G_OBJECT (self));
1474 g_array_unref (members);
1478 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1481 TpHandle self_handle;
1483 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1486 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1487 if (self_handle == 0)
1490 return tp_channel_group_get_local_pending_info ((TpChannel *) self,
1491 self_handle, inviter, NULL, NULL);
1495 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1496 EmpathyContact *contact)
1498 return tp_channel_get_chat_state ((TpChannel *) self,
1499 empathy_contact_get_handle (contact));
1503 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1505 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1507 return self->priv->user;
1511 empathy_tp_chat_get_feature_ready (void)
1513 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1517 tp_chat_prepare_ready_async (TpProxy *proxy,
1518 const TpProxyFeature *feature,
1519 GAsyncReadyCallback callback,
1522 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1523 TpChannel *channel = (TpChannel *) proxy;
1524 TpConnection *connection;
1525 gboolean listen_for_dbus_properties_changed = FALSE;
1527 g_assert (self->priv->ready_result == NULL);
1528 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1529 callback, user_data, tp_chat_prepare_ready_async);
1531 connection = tp_channel_borrow_connection (channel);
1533 if (tp_proxy_has_interface_by_id (self,
1534 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1536 const TpIntSet *members;
1540 /* Get self contact from the group's self handle */
1541 handle = tp_channel_group_get_self_handle (channel);
1542 empathy_tp_contact_factory_get_from_handle (connection,
1543 handle, tp_chat_got_self_contact_cb,
1544 NULL, NULL, G_OBJECT (self));
1546 /* Get initial member contacts */
1547 members = tp_channel_group_get_members (channel);
1548 handles = tp_intset_to_array (members);
1549 empathy_tp_contact_factory_get_from_handles (connection,
1550 handles->len, (TpHandle *) handles->data,
1551 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1553 self->priv->can_upgrade_to_muc = FALSE;
1555 tp_g_signal_connect_object (self, "group-members-changed",
1556 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1560 TpCapabilities *caps;
1565 /* Get the self contact from the connection's self handle */
1566 handle = tp_connection_get_self_handle (connection);
1567 empathy_tp_contact_factory_get_from_handle (connection,
1568 handle, tp_chat_got_self_contact_cb,
1569 NULL, NULL, G_OBJECT (self));
1571 /* Get the remote contact */
1572 handle = tp_channel_get_handle (channel, NULL);
1573 empathy_tp_contact_factory_get_from_handle (connection,
1574 handle, tp_chat_got_remote_contact_cb,
1575 NULL, NULL, G_OBJECT (self));
1577 caps = tp_connection_get_capabilities (connection);
1578 g_assert (caps != NULL);
1580 classes = tp_capabilities_get_channel_classes (caps);
1582 for (i = 0; i < classes->len; i++)
1584 GValueArray *array = g_ptr_array_index (classes, i);
1585 const char **oprops = g_value_get_boxed (
1586 g_value_array_get_nth (array, 1));
1588 if (tp_strv_contains (oprops,
1589 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1591 self->priv->can_upgrade_to_muc = TRUE;
1597 if (tp_proxy_has_interface_by_id (self,
1598 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1600 tp_cli_dbus_properties_call_get_all (channel, -1,
1601 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1602 tp_chat_get_all_subject_cb,
1605 listen_for_dbus_properties_changed = TRUE;
1608 if (tp_proxy_has_interface_by_id (self,
1609 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1611 tp_cli_dbus_properties_call_get_all (channel, -1,
1612 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1613 tp_chat_get_all_room_config_cb,
1616 listen_for_dbus_properties_changed = TRUE;
1619 if (listen_for_dbus_properties_changed)
1621 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1622 tp_chat_dbus_properties_changed_cb,
1624 G_OBJECT (self), NULL);