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,
89 static guint signals[LAST_SIGNAL];
91 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
94 tp_chat_set_delivery_status (EmpathyTpChat *self,
96 EmpathyDeliveryStatus delivery_status)
98 TpDeliveryReportingSupportFlags flags =
99 tp_text_channel_get_delivery_reporting_support (
100 TP_TEXT_CHANNEL (self));
102 /* channel must support receiving failures and successes */
103 if (!tp_str_empty (token) &&
104 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
105 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
107 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
109 switch (delivery_status)
111 case EMPATHY_DELIVERY_STATUS_NONE:
112 g_hash_table_remove (self->priv->messages_being_sent,
117 g_hash_table_insert (self->priv->messages_being_sent,
119 GUINT_TO_POINTER (delivery_status));
123 g_object_notify (G_OBJECT (self), "n-messages-sending");
127 static void tp_chat_prepare_ready_async (TpProxy *proxy,
128 const TpProxyFeature *feature,
129 GAsyncReadyCallback callback,
133 tp_chat_async_cb (TpChannel *proxy,
136 GObject *weak_object)
139 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
143 create_conference_cb (GObject *source,
144 GAsyncResult *result,
147 GError *error = NULL;
149 if (!tp_account_channel_request_create_channel_finish (
150 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
152 DEBUG ("Failed to create conference channel: %s", error->message);
153 g_error_free (error);
158 empathy_tp_chat_add (EmpathyTpChat *self,
159 EmpathyContact *contact,
160 const gchar *message)
162 TpChannel *channel = (TpChannel *) self;
164 if (tp_proxy_has_interface_by_id (self,
165 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
168 GArray handles = {(gchar *) &handle, 1};
170 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
172 handle = empathy_contact_get_handle (contact);
173 tp_cli_channel_interface_group_call_add_members (channel,
174 -1, &handles, NULL, NULL, NULL, NULL, NULL);
176 else if (self->priv->can_upgrade_to_muc)
178 TpAccountChannelRequest *req;
180 const char *object_path;
181 GPtrArray channels = { (gpointer *) &object_path, 1 };
182 const char *invitees[2] = { NULL, };
184 invitees[0] = empathy_contact_get_id (contact);
185 object_path = tp_proxy_get_object_path (self);
188 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
189 TP_IFACE_CHANNEL_TYPE_TEXT,
190 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
192 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
193 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
194 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
195 G_TYPE_STRV, invitees,
196 /* FIXME: InvitationMessage ? */
199 req = tp_account_channel_request_new (self->priv->account, props,
200 TP_USER_ACTION_TIME_NOT_USER_ACTION);
202 /* Although this is a MUC, it's anonymous, so CreateChannel is
204 tp_account_channel_request_create_channel_async (req,
205 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
207 g_object_unref (req);
208 g_hash_table_unref (props);
212 g_warning ("Cannot add to this channel");
217 empathy_tp_chat_get_members (EmpathyTpChat *self)
219 GList *members = NULL;
221 if (self->priv->members)
223 members = g_list_copy (self->priv->members);
224 g_list_foreach (members, (GFunc) g_object_ref, NULL);
228 members = g_list_prepend (members, g_object_ref (self->priv->user));
230 if (self->priv->remote_contact != NULL)
231 members = g_list_prepend (members,
232 g_object_ref (self->priv->remote_contact));
239 check_ready (EmpathyTpChat *self)
241 if (self->priv->ready_result == NULL)
244 if (g_queue_get_length (self->priv->messages_queue) > 0)
249 g_simple_async_result_complete (self->priv->ready_result);
250 tp_clear_object (&self->priv->ready_result);
254 tp_chat_emit_queued_messages (EmpathyTpChat *self)
256 EmpathyMessage *message;
258 /* Check if we can now emit some queued messages */
259 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL)
261 if (empathy_message_get_sender (message) == NULL)
264 DEBUG ("Queued message ready");
265 g_queue_pop_head (self->priv->messages_queue);
266 g_queue_push_tail (self->priv->pending_messages_queue, message);
267 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
274 tp_chat_got_sender_cb (TpConnection *connection,
275 EmpathyContact *contact,
280 EmpathyTpChat *self = (EmpathyTpChat *) chat;
284 DEBUG ("Error: %s", error->message);
285 /* Do not block the message queue, just drop this message */
286 g_queue_remove (self->priv->messages_queue, message);
290 empathy_message_set_sender (message, contact);
293 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
297 tp_chat_build_message (EmpathyTpChat *self,
301 EmpathyMessage *message;
304 message = empathy_message_new_from_tp_message (msg, incoming);
305 /* FIXME: this is actually a lie for incoming messages. */
306 empathy_message_set_receiver (message, self->priv->user);
308 g_queue_push_tail (self->priv->messages_queue, message);
310 sender = tp_signalled_message_get_sender (msg);
311 g_assert (sender != NULL);
313 if (tp_contact_get_handle (sender) == 0)
315 empathy_message_set_sender (message, self->priv->user);
316 tp_chat_emit_queued_messages (self);
320 TpConnection *connection = tp_channel_borrow_connection (
323 empathy_tp_contact_factory_get_from_handle (connection,
324 tp_contact_get_handle (sender),
325 tp_chat_got_sender_cb,
326 message, NULL, G_OBJECT (self));
331 handle_delivery_report (EmpathyTpChat *self,
334 TpDeliveryStatus delivery_status;
335 const GHashTable *header;
336 TpChannelTextSendError delivery_error;
339 const gchar *message_body = NULL;
340 const gchar *delivery_dbus_error;
341 const gchar *delivery_token = NULL;
343 header = tp_message_peek (message, 0);
347 delivery_token = tp_asv_get_string (header, "delivery-token");
348 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
354 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
356 DEBUG ("Accepted %s", delivery_token);
357 tp_chat_set_delivery_status (self, delivery_token,
358 EMPATHY_DELIVERY_STATUS_ACCEPTED);
361 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
363 DEBUG ("Delivered %s", delivery_token);
364 tp_chat_set_delivery_status (self, delivery_token,
365 EMPATHY_DELIVERY_STATUS_NONE);
368 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
369 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
374 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
376 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
378 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
380 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
381 * TpMessage. (fdo #35884) */
382 echo = tp_asv_get_boxed (header, "delivery-echo",
383 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
384 if (echo != NULL && echo->len >= 2)
386 const GHashTable *echo_body;
388 echo_body = g_ptr_array_index (echo, 1);
389 if (echo_body != NULL)
390 message_body = tp_asv_get_string (echo_body, "content");
393 tp_chat_set_delivery_status (self, delivery_token,
394 EMPATHY_DELIVERY_STATUS_NONE);
395 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
396 delivery_error, delivery_dbus_error);
399 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
400 message, NULL, NULL);
404 handle_incoming_message (EmpathyTpChat *self,
410 if (tp_message_is_delivery_report (message))
412 handle_delivery_report (self, message);
416 message_body = tp_message_to_text (message, NULL);
418 DEBUG ("Message %s (channel %s): %s",
419 pending ? "pending" : "received",
420 tp_proxy_get_object_path (self), message_body);
422 if (message_body == NULL)
424 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
426 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
427 message, NULL, NULL);
431 tp_chat_build_message (self, message, TRUE);
433 g_free (message_body);
437 message_received_cb (TpTextChannel *channel,
441 handle_incoming_message (self, message, FALSE);
445 find_pending_message_func (gconstpointer a,
448 EmpathyMessage *msg = (EmpathyMessage *) a;
449 TpMessage *message = (TpMessage *) b;
451 if (empathy_message_get_tp_message (msg) == message)
458 pending_message_removed_cb (TpTextChannel *channel,
464 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
465 find_pending_message_func);
470 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
472 g_object_unref (m->data);
473 g_queue_delete_link (self->priv->pending_messages_queue, m);
477 message_sent_cb (TpTextChannel *channel,
479 TpMessageSendingFlags flags,
485 message_body = tp_message_to_text (message, NULL);
487 DEBUG ("Message sent: %s", message_body);
489 tp_chat_build_message (self, message, FALSE);
491 g_free (message_body);
494 static TpChannelTextSendError
495 error_to_text_send_error (GError *error)
497 if (error->domain != TP_ERROR)
498 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
502 case TP_ERROR_OFFLINE:
503 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
504 case TP_ERROR_INVALID_HANDLE:
505 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
506 case TP_ERROR_PERMISSION_DENIED:
507 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
508 case TP_ERROR_NOT_IMPLEMENTED:
509 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
512 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
516 message_send_cb (GObject *source,
517 GAsyncResult *result,
520 EmpathyTpChat *self = user_data;
521 TpTextChannel *channel = (TpTextChannel *) source;
523 GError *error = NULL;
525 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
527 DEBUG ("Error: %s", error->message);
529 /* FIXME: we should use the body of the message as first argument of the
530 * signal but can't easily get it as we just get a user_data pointer. Once
531 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
532 * to use the user_data pointer to pass the message and fix this. */
533 g_signal_emit (self, signals[SEND_ERROR], 0,
534 NULL, error_to_text_send_error (error), NULL);
536 g_error_free (error);
539 tp_chat_set_delivery_status (self, token,
540 EMPATHY_DELIVERY_STATUS_SENDING);
546 TpChannelChatState state;
550 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
551 EmpathyContact *contact,
556 TpChannelChatState state;
560 DEBUG ("Error: %s", error->message);
564 state = GPOINTER_TO_UINT (user_data);
565 DEBUG ("Chat state changed for %s (%d): %d",
566 empathy_contact_get_alias (contact),
567 empathy_contact_get_handle (contact), state);
569 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
573 tp_chat_state_changed_cb (TpChannel *channel,
575 TpChannelChatState state,
578 TpConnection *connection = tp_channel_borrow_connection (
581 empathy_tp_contact_factory_get_from_handle (connection, handle,
582 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
583 NULL, G_OBJECT (self));
587 list_pending_messages (EmpathyTpChat *self)
591 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
593 for (l = messages; l != NULL; l = g_list_next (l))
595 TpMessage *message = l->data;
597 handle_incoming_message (self, message, FALSE);
600 g_list_free (messages);
604 update_subject (EmpathyTpChat *self,
605 GHashTable *properties)
607 gboolean can_set, valid;
608 const gchar *subject;
610 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
612 self->priv->can_set_subject = can_set;
614 subject = tp_asv_get_string (properties, "Subject");
619 g_free (self->priv->subject);
620 self->priv->subject = g_strdup (subject);
622 /* If the actor is included with this update, use it;
623 * otherwise, clear it to avoid showing stale information.
624 * Why might it not be included? When you join an IRC channel,
625 * you get a pair of messages: first, the current topic; next,
626 * who set it, and when. Idle reports these in two separate
629 actor = tp_asv_get_string (properties, "Actor");
630 g_free (self->priv->subject_actor);
631 self->priv->subject_actor = g_strdup (actor);
633 g_object_notify (G_OBJECT (self), "subject");
636 /* TODO: track Timestamp. */
640 tp_chat_get_all_subject_cb (TpProxy *proxy,
641 GHashTable *properties,
643 gpointer user_data G_GNUC_UNUSED,
646 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
650 DEBUG ("Error fetching subject: %s", error->message);
654 self->priv->supports_subject = TRUE;
655 update_subject (self, properties);
659 update_title (EmpathyTpChat *self,
660 GHashTable *properties)
662 const gchar *title = tp_asv_get_string (properties, "Title");
666 if (tp_str_empty (title))
669 g_free (self->priv->title);
670 self->priv->title = g_strdup (title);
671 g_object_notify (G_OBJECT (self), "title");
676 tp_chat_get_all_room_config_cb (TpProxy *proxy,
677 GHashTable *properties,
679 gpointer user_data G_GNUC_UNUSED,
682 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
686 DEBUG ("Error fetching room config: %s", error->message);
690 update_title (self, properties);
694 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
695 const gchar *interface_name,
697 const gchar **invalidated,
701 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
703 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
704 update_subject (self, changed);
706 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
707 update_title (self, changed);
711 empathy_tp_chat_set_subject (EmpathyTpChat *self,
712 const gchar *subject)
714 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
715 subject, tp_chat_async_cb, "while setting subject", NULL,
720 empathy_tp_chat_get_title (EmpathyTpChat *self)
722 return self->priv->title;
726 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
728 return self->priv->supports_subject;
732 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
734 return self->priv->can_set_subject;
738 empathy_tp_chat_get_subject (EmpathyTpChat *self)
740 return self->priv->subject;
744 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
746 return self->priv->subject_actor;
750 tp_chat_dispose (GObject *object)
752 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
754 tp_clear_object (&self->priv->account);
755 tp_clear_object (&self->priv->remote_contact);
756 tp_clear_object (&self->priv->user);
758 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
759 g_queue_clear (self->priv->messages_queue);
761 g_queue_foreach (self->priv->pending_messages_queue,
762 (GFunc) g_object_unref, NULL);
763 g_queue_clear (self->priv->pending_messages_queue);
765 tp_clear_object (&self->priv->ready_result);
767 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
768 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
772 tp_chat_finalize (GObject *object)
774 EmpathyTpChat *self = (EmpathyTpChat *) object;
776 DEBUG ("Finalize: %p", object);
778 g_queue_free (self->priv->messages_queue);
779 g_queue_free (self->priv->pending_messages_queue);
780 g_hash_table_unref (self->priv->messages_being_sent);
782 g_free (self->priv->title);
783 g_free (self->priv->subject);
784 g_free (self->priv->subject_actor);
786 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
790 check_almost_ready (EmpathyTpChat *self)
792 TpChannel *channel = (TpChannel *) self;
794 if (self->priv->ready_result == NULL)
797 if (self->priv->user == NULL)
800 /* We need either the members (room) or the remote contact (private chat).
801 * If the chat is protected by a password we can't get these information so
802 * consider the chat as ready so it can be presented to the user. */
803 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
804 self->priv->remote_contact == NULL)
807 g_assert (tp_proxy_is_prepared (self,
808 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
810 tp_g_signal_connect_object (self, "message-received",
811 G_CALLBACK (message_received_cb), self, 0);
812 tp_g_signal_connect_object (self, "pending-message-removed",
813 G_CALLBACK (pending_message_removed_cb), self, 0);
815 list_pending_messages (self);
817 tp_g_signal_connect_object (self, "message-sent",
818 G_CALLBACK (message_sent_cb), self, 0);
820 tp_g_signal_connect_object (self, "chat-state-changed",
821 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
827 add_members_contact (EmpathyTpChat *self,
832 for (i = 0; i < contacts->len; i++)
834 EmpathyContact *contact;
836 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
839 self->priv->members = g_list_prepend (self->priv->members, contact);
841 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
842 contact, NULL, 0, NULL, TRUE);
845 check_almost_ready (self);
849 remove_member (EmpathyTpChat *self,
850 EmpathyContact *contact)
854 for (l = self->priv->members; l; l = l->next)
856 EmpathyContact *c = l->data;
860 self->priv->members = g_list_delete_link (self->priv->members, l);
868 contact_renamed (EmpathyTpChat *self,
869 TpContact *old_contact,
870 TpContact *new_contact,
871 TpChannelGroupChangeReason reason,
872 const gchar *message)
874 EmpathyContact *old = NULL, *new = NULL;
876 old = empathy_contact_dup_from_tp_contact (old_contact);
877 new = empathy_contact_dup_from_tp_contact (new_contact);
879 self->priv->members = g_list_prepend (self->priv->members, new);
883 remove_member (self, old);
885 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
887 g_object_unref (old);
890 if (self->priv->user == old)
892 /* We change our nick */
893 tp_clear_object (&self->priv->user);
894 self->priv->user = g_object_ref (new);
895 g_object_notify (G_OBJECT (self), "self-contact");
898 check_almost_ready (self);
902 tp_chat_group_contacts_changed_cb (TpChannel *channel,
905 GPtrArray *local_pending,
906 GPtrArray *remote_pending,
911 EmpathyContact *actor_contact = NULL;
913 TpChannelGroupChangeReason reason;
914 const gchar *message;
916 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
917 message = tp_asv_get_string (details, "message");
919 /* Contact renamed */
920 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
922 /* there can only be a single 'added' and a single 'removed' handle */
923 if (removed->len != 1 || added->len != 1)
925 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
926 added->len, removed->len);
930 contact_renamed (self, g_ptr_array_index (removed, 0),
931 g_ptr_array_index (added, 0), reason, message);
937 actor_contact = empathy_contact_dup_from_tp_contact (actor);
939 if (actor_contact == NULL)
941 /* FIXME: handle this a tad more gracefully: perhaps
942 * the actor was a server op. We could use the
943 * contact-ids detail of MembersChangedDetailed.
945 DEBUG ("actor %s not a channel member",
946 tp_contact_get_identifier (actor));
950 /* Remove contacts that are not members anymore */
951 for (i = 0; i < removed->len; i++)
953 TpContact *tp_contact = g_ptr_array_index (removed, i);
954 EmpathyContact *contact;
956 contact = empathy_contact_dup_from_tp_contact (tp_contact);
960 remove_member (self, contact);
962 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
963 contact, actor_contact, reason, message, FALSE);
964 g_object_unref (contact);
970 add_members_contact (self, added);
973 if (actor_contact != NULL)
974 g_object_unref (actor_contact);
978 create_remote_contact (EmpathyTpChat *self,
981 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
982 g_object_notify (G_OBJECT (self), "remote-contact");
984 check_almost_ready (self);
988 create_self_contact (EmpathyTpChat *self,
991 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
992 empathy_contact_set_is_user (self->priv->user, TRUE);
993 g_object_notify (G_OBJECT (self), "self-contact");
994 check_almost_ready (self);
998 tp_chat_get_property (GObject *object,
1003 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1008 g_value_set_object (value, self->priv->account);
1010 case PROP_SELF_CONTACT:
1011 g_value_set_object (value, self->priv->user);
1013 case PROP_REMOTE_CONTACT:
1014 g_value_set_object (value, self->priv->remote_contact);
1016 case PROP_N_MESSAGES_SENDING:
1017 g_value_set_uint (value,
1018 g_hash_table_size (self->priv->messages_being_sent));
1021 g_value_set_string (value,
1022 empathy_tp_chat_get_title (self));
1025 g_value_set_string (value,
1026 empathy_tp_chat_get_subject (self));
1029 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1035 tp_chat_set_property (GObject *object,
1037 const GValue *value,
1040 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1045 self->priv->account = g_value_dup_object (value);
1048 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1058 static const TpProxyFeature *
1059 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1061 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1062 static GQuark need[2] = {0, 0};
1064 if (G_LIKELY (features[0].name != 0))
1067 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1068 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1069 features[FEAT_READY].depends_on = need;
1070 features[FEAT_READY].prepare_async =
1071 tp_chat_prepare_ready_async;
1073 /* assert that the terminator at the end is there */
1074 g_assert (features[N_FEAT].name == 0);
1080 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1082 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1083 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1085 object_class->dispose = tp_chat_dispose;
1086 object_class->finalize = tp_chat_finalize;
1087 object_class->get_property = tp_chat_get_property;
1088 object_class->set_property = tp_chat_set_property;
1090 proxy_class->list_features = tp_chat_list_features;
1092 g_object_class_install_property (object_class, PROP_ACCOUNT,
1093 g_param_spec_object ("account", "TpAccount",
1094 "the account associated with the chat",
1097 G_PARAM_CONSTRUCT_ONLY |
1098 G_PARAM_STATIC_STRINGS));
1101 * EmpathyTpChat:self-contact:
1103 * Not to be confused with TpChannel:group-self-contact.
1105 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1106 g_param_spec_object ("self-contact", "The local contact",
1107 "The EmpathyContact for the local user on this channel",
1108 EMPATHY_TYPE_CONTACT,
1111 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1112 g_param_spec_object ("remote-contact", "The remote contact",
1113 "The remote contact if there is no group iface on the channel",
1114 EMPATHY_TYPE_CONTACT,
1117 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1118 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1119 "The number of messages being sent",
1123 g_object_class_install_property (object_class, PROP_TITLE,
1124 g_param_spec_string ("title", "Title",
1125 "A human-readable name for the room, if any",
1128 G_PARAM_STATIC_STRINGS));
1130 g_object_class_install_property (object_class, PROP_SUBJECT,
1131 g_param_spec_string ("subject", "Subject",
1132 "The room's current subject, if any",
1135 G_PARAM_STATIC_STRINGS));
1138 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1139 G_TYPE_FROM_CLASS (klass),
1143 g_cclosure_marshal_generic,
1145 1, EMPATHY_TYPE_MESSAGE);
1147 signals[SEND_ERROR] = g_signal_new ("send-error",
1148 G_TYPE_FROM_CLASS (klass),
1152 g_cclosure_marshal_generic,
1154 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1156 signals[CHAT_STATE_CHANGED] = g_signal_new ("chat-state-changed-empathy",
1157 G_TYPE_FROM_CLASS (klass),
1161 g_cclosure_marshal_generic,
1163 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1165 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1166 G_TYPE_FROM_CLASS (klass),
1170 g_cclosure_marshal_generic,
1172 1, EMPATHY_TYPE_MESSAGE);
1174 signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1175 G_OBJECT_CLASS_TYPE (klass),
1177 0, NULL, NULL, NULL,
1179 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1180 G_TYPE_UINT, G_TYPE_STRING);
1182 signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1183 G_OBJECT_CLASS_TYPE (klass),
1185 0, NULL, NULL, NULL,
1187 5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1188 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1190 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1194 empathy_tp_chat_init (EmpathyTpChat *self)
1196 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1197 EmpathyTpChatPrivate);
1199 self->priv->messages_queue = g_queue_new ();
1200 self->priv->pending_messages_queue = g_queue_new ();
1201 self->priv->messages_being_sent = g_hash_table_new_full (
1202 g_str_hash, g_str_equal, g_free, NULL);
1206 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1209 const gchar *object_path,
1210 const GHashTable *immutable_properties)
1212 TpProxy *conn_proxy = (TpProxy *) conn;
1214 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1215 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1216 g_return_val_if_fail (immutable_properties != NULL, NULL);
1218 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1222 "dbus-daemon", conn_proxy->dbus_daemon,
1223 "bus-name", conn_proxy->bus_name,
1224 "object-path", object_path,
1225 "channel-properties", immutable_properties,
1230 empathy_tp_chat_get_id (EmpathyTpChat *self)
1234 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1236 id = tp_channel_get_identifier ((TpChannel *) self);
1237 if (!EMP_STR_EMPTY (id))
1239 else if (self->priv->remote_contact)
1240 return empathy_contact_get_id (self->priv->remote_contact);
1247 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1249 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1251 return self->priv->remote_contact;
1255 empathy_tp_chat_get_account (EmpathyTpChat *self)
1257 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1259 return self->priv->account;
1263 empathy_tp_chat_send (EmpathyTpChat *self,
1266 gchar *message_body;
1268 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1269 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1271 message_body = tp_message_to_text (message, NULL);
1273 DEBUG ("Sending message: %s", message_body);
1275 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1276 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1277 message_send_cb, self);
1279 g_free (message_body);
1283 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1285 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1287 return self->priv->pending_messages_queue->head;
1291 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1292 EmpathyMessage *message)
1296 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1298 if (!empathy_message_is_incoming (message))
1301 tp_msg = empathy_message_get_tp_message (message);
1302 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1303 tp_msg, NULL, NULL);
1307 * empathy_tp_chat_can_add_contact:
1309 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1310 * That is if this chat is a 1-to-1 channel that can be upgraded to
1311 * a MUC using the Conference interface or if the channel is a MUC.
1314 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1316 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1318 return self->priv->can_upgrade_to_muc ||
1319 tp_proxy_has_interface_by_id (self,
1320 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1324 tp_channel_leave_async_cb (GObject *source_object,
1328 GError *error = NULL;
1330 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1332 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1334 g_error_free (error);
1339 empathy_tp_chat_leave (EmpathyTpChat *self,
1340 const gchar *message)
1342 TpChannel *channel = (TpChannel *) self;
1344 DEBUG ("Leaving channel %s with message \"%s\"",
1345 tp_channel_get_identifier (channel), message);
1347 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1348 message, tp_channel_leave_async_cb, self);
1352 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1353 TpContact **inviter)
1355 TpContact *self_contact;
1356 TpChannel *channel = TP_CHANNEL (self);
1358 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1361 self_contact = tp_channel_group_get_self_contact (channel);
1362 if (self_contact == NULL)
1365 return tp_channel_group_get_local_pending_contact_info (channel,
1366 self_contact, inviter, NULL, NULL);
1370 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1371 EmpathyContact *contact)
1373 return tp_channel_get_chat_state ((TpChannel *) self,
1374 empathy_contact_get_handle (contact));
1378 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1380 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1382 return self->priv->user;
1386 empathy_tp_chat_get_feature_ready (void)
1388 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1392 tp_chat_prepare_ready_async (TpProxy *proxy,
1393 const TpProxyFeature *feature,
1394 GAsyncReadyCallback callback,
1397 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1398 TpChannel *channel = (TpChannel *) proxy;
1399 TpConnection *connection;
1400 gboolean listen_for_dbus_properties_changed = FALSE;
1402 g_assert (self->priv->ready_result == NULL);
1403 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1404 callback, user_data, tp_chat_prepare_ready_async);
1406 connection = tp_channel_borrow_connection (channel);
1408 if (tp_proxy_has_interface_by_id (self,
1409 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1411 GPtrArray *contacts;
1414 /* Get self contact from the group's self handle */
1415 contact = tp_channel_group_get_self_contact (channel);
1416 create_self_contact (self, contact);
1418 /* Get initial member contacts */
1419 contacts = tp_channel_group_dup_members_contacts (channel);
1420 add_members_contact (self, contacts);
1421 g_ptr_array_unref (contacts);
1423 self->priv->can_upgrade_to_muc = FALSE;
1425 tp_g_signal_connect_object (self, "group-contacts-changed",
1426 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1430 TpCapabilities *caps;
1435 /* Get the self contact from the connection's self handle */
1436 contact = tp_connection_get_self_contact (connection);
1437 create_self_contact (self, contact);
1439 /* Get the remote contact */
1440 contact = tp_channel_get_target_contact (channel);
1441 create_remote_contact (self, contact);
1443 caps = tp_connection_get_capabilities (connection);
1444 g_assert (caps != NULL);
1446 classes = tp_capabilities_get_channel_classes (caps);
1448 for (i = 0; i < classes->len; i++)
1450 GValueArray *array = g_ptr_array_index (classes, i);
1451 const char **oprops = g_value_get_boxed (
1452 g_value_array_get_nth (array, 1));
1454 if (tp_strv_contains (oprops,
1455 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1457 self->priv->can_upgrade_to_muc = TRUE;
1463 if (tp_proxy_has_interface_by_id (self,
1464 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1466 tp_cli_dbus_properties_call_get_all (channel, -1,
1467 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1468 tp_chat_get_all_subject_cb,
1471 listen_for_dbus_properties_changed = TRUE;
1474 if (tp_proxy_has_interface_by_id (self,
1475 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1477 tp_cli_dbus_properties_call_get_all (channel, -1,
1478 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1479 tp_chat_get_all_room_config_cb,
1482 listen_for_dbus_properties_changed = TRUE;
1485 if (listen_for_dbus_properties_changed)
1487 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1488 tp_chat_dbus_properties_changed_cb,
1490 G_OBJECT (self), NULL);