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>
22 #include "empathy-tp-chat.h"
24 #include <tp-account-widgets/tpaw-utils.h>
25 #include <telepathy-glib/telepathy-glib-dbus.h>
27 #include "empathy-request-util.h"
28 #include "empathy-utils.h"
30 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
31 #include "empathy-debug.h"
33 struct _EmpathyTpChatPrivate
37 EmpathyContact *remote_contact;
39 /* Queue of messages signalled but not acked yet */
40 GQueue *pending_messages_queue;
43 gboolean supports_subject;
44 gboolean can_set_subject;
48 /* Room config (for now, we only track the title and don't support
52 gboolean can_upgrade_to_muc;
54 GHashTable *messages_being_sent;
56 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
57 GSimpleAsyncResult *ready_result;
58 gboolean preparing_password;
67 PROP_N_MESSAGES_SENDING,
82 static guint signals[LAST_SIGNAL];
84 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
87 tp_chat_set_delivery_status (EmpathyTpChat *self,
89 EmpathyDeliveryStatus delivery_status)
91 TpDeliveryReportingSupportFlags flags =
92 tp_text_channel_get_delivery_reporting_support (
93 TP_TEXT_CHANNEL (self));
95 /* channel must support receiving failures and successes */
96 if (!tp_str_empty (token) &&
97 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
98 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
100 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
102 switch (delivery_status)
104 case EMPATHY_DELIVERY_STATUS_NONE:
105 g_hash_table_remove (self->priv->messages_being_sent,
110 g_hash_table_insert (self->priv->messages_being_sent,
112 GUINT_TO_POINTER (delivery_status));
116 g_object_notify (G_OBJECT (self), "n-messages-sending");
120 static void tp_chat_prepare_ready_async (TpProxy *proxy,
121 const TpProxyFeature *feature,
122 GAsyncReadyCallback callback,
126 tp_chat_async_cb (TpChannel *proxy,
129 GObject *weak_object)
132 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
136 update_config_cb (TpChannel *proxy,
139 GObject *weak_object)
142 DEBUG ("Failed to change config of the room: %s", error->message);
146 create_conference_cb (GObject *source,
147 GAsyncResult *result,
151 GError *error = NULL;
154 channel = tp_account_channel_request_create_and_observe_channel_finish (
155 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
158 DEBUG ("Failed to create conference channel: %s", error->message);
159 g_error_free (error);
163 /* Make the channel more confidential as only people invited are supposed to
166 "Private", G_TYPE_BOOLEAN, TRUE,
167 "InviteOnly", G_TYPE_BOOLEAN, TRUE,
170 tp_cli_channel_interface_room_config_call_update_configuration (channel, -1,
171 props, update_config_cb, NULL, NULL, NULL);
173 g_object_unref (channel);
174 g_hash_table_unref (props);
178 empathy_tp_chat_add (EmpathyTpChat *self,
179 EmpathyContact *contact,
180 const gchar *message)
182 TpChannel *channel = (TpChannel *) self;
184 if (tp_proxy_has_interface_by_id (self,
185 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
188 GArray handles = {(gchar *) &handle, 1};
190 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
192 handle = empathy_contact_get_handle (contact);
193 tp_cli_channel_interface_group_call_add_members (channel,
194 -1, &handles, NULL, NULL, NULL, NULL, NULL);
196 else if (self->priv->can_upgrade_to_muc)
198 TpAccountChannelRequest *req;
199 const gchar *channels[2] = { NULL, };
200 const char *invitees[2] = { NULL, };
203 invitees[0] = empathy_contact_get_id (contact);
204 channels[0] = tp_proxy_get_object_path (self);
206 account = empathy_tp_chat_get_account (self);
208 req = tp_account_channel_request_new_text (account,
209 TP_USER_ACTION_TIME_NOT_USER_ACTION);
211 tp_account_channel_request_set_request_property (req,
212 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
213 g_variant_new_uint32 (TP_HANDLE_TYPE_NONE));
215 tp_account_channel_request_set_request_property (req,
216 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
217 g_variant_new_objv (channels, -1));
219 tp_account_channel_request_set_request_property (req,
220 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
221 g_variant_new_strv (invitees, -1));
223 /* FIXME: InvitationMessage ? */
225 /* Although this is a MUC, it's anonymous, so CreateChannel is
227 tp_account_channel_request_create_and_observe_channel_async (req,
228 EMPATHY_CHAT_TP_BUS_NAME, NULL, create_conference_cb, NULL);
230 g_object_unref (req);
234 g_warning ("Cannot add to this channel");
239 empathy_tp_chat_get_members (EmpathyTpChat *self)
241 GList *members = NULL;
243 if (self->priv->members)
245 members = g_list_copy (self->priv->members);
246 g_list_foreach (members, (GFunc) g_object_ref, NULL);
250 members = g_list_prepend (members, g_object_ref (self->priv->user));
252 if (self->priv->remote_contact != NULL)
253 members = g_list_prepend (members,
254 g_object_ref (self->priv->remote_contact));
261 check_ready (EmpathyTpChat *self)
263 if (self->priv->ready_result == NULL)
268 g_simple_async_result_complete_in_idle (self->priv->ready_result);
269 tp_clear_object (&self->priv->ready_result);
273 tp_chat_build_message (EmpathyTpChat *self,
277 EmpathyMessage *message;
280 message = empathy_message_new_from_tp_message (msg, incoming);
281 /* FIXME: this is actually a lie for incoming messages. */
282 empathy_message_set_receiver (message, self->priv->user);
284 sender = tp_signalled_message_get_sender (msg);
285 g_assert (sender != NULL);
287 if (tp_contact_get_handle (sender) == 0)
289 empathy_message_set_sender (message, self->priv->user);
293 EmpathyContact *contact;
295 contact = empathy_contact_dup_from_tp_contact (sender);
297 empathy_message_set_sender (message, contact);
299 g_object_unref (contact);
302 g_queue_push_tail (self->priv->pending_messages_queue, message);
303 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
307 handle_delivery_report (EmpathyTpChat *self,
310 TpDeliveryStatus delivery_status;
311 const GHashTable *header;
312 TpChannelTextSendError delivery_error;
315 const gchar *message_body = NULL;
316 const gchar *delivery_dbus_error;
317 const gchar *delivery_token = NULL;
319 header = tp_message_peek (message, 0);
323 delivery_token = tp_asv_get_string (header, "delivery-token");
324 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
330 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
332 DEBUG ("Accepted %s", delivery_token);
333 tp_chat_set_delivery_status (self, delivery_token,
334 EMPATHY_DELIVERY_STATUS_ACCEPTED);
337 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
339 DEBUG ("Delivered %s", delivery_token);
340 tp_chat_set_delivery_status (self, delivery_token,
341 EMPATHY_DELIVERY_STATUS_NONE);
344 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
345 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
350 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
352 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
354 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
356 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
357 * TpMessage. (fdo #35884) */
358 echo = tp_asv_get_boxed (header, "delivery-echo",
359 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
360 if (echo != NULL && echo->len >= 2)
362 const GHashTable *echo_body;
364 echo_body = g_ptr_array_index (echo, 1);
365 if (echo_body != NULL)
366 message_body = tp_asv_get_string (echo_body, "content");
369 tp_chat_set_delivery_status (self, delivery_token,
370 EMPATHY_DELIVERY_STATUS_NONE);
371 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
372 delivery_error, delivery_dbus_error);
375 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
376 message, NULL, NULL);
380 handle_incoming_message (EmpathyTpChat *self,
386 if (tp_message_is_delivery_report (message))
388 handle_delivery_report (self, message);
392 message_body = tp_message_to_text (message, NULL);
394 DEBUG ("Message %s (channel %s): %s",
395 pending ? "pending" : "received",
396 tp_proxy_get_object_path (self), message_body);
398 if (message_body == NULL)
400 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
402 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
403 message, NULL, NULL);
407 tp_chat_build_message (self, message, TRUE);
409 g_free (message_body);
413 message_received_cb (TpTextChannel *channel,
417 handle_incoming_message (self, message, FALSE);
421 find_pending_message_func (gconstpointer a,
424 EmpathyMessage *msg = (EmpathyMessage *) a;
425 TpMessage *message = (TpMessage *) b;
427 if (empathy_message_get_tp_message (msg) == message)
434 pending_message_removed_cb (TpTextChannel *channel,
440 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
441 find_pending_message_func);
446 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
448 g_object_unref (m->data);
449 g_queue_delete_link (self->priv->pending_messages_queue, m);
453 message_sent_cb (TpTextChannel *channel,
455 TpMessageSendingFlags flags,
461 message_body = tp_message_to_text (message, NULL);
463 DEBUG ("Message sent: %s", message_body);
465 tp_chat_build_message (self, message, FALSE);
467 g_free (message_body);
470 static TpChannelTextSendError
471 error_to_text_send_error (GError *error)
473 if (error->domain != TP_ERROR)
474 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
478 case TP_ERROR_OFFLINE:
479 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
480 case TP_ERROR_INVALID_HANDLE:
481 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
482 case TP_ERROR_PERMISSION_DENIED:
483 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
484 case TP_ERROR_NOT_IMPLEMENTED:
485 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
488 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
492 message_send_cb (GObject *source,
493 GAsyncResult *result,
496 EmpathyTpChat *self = user_data;
497 TpTextChannel *channel = (TpTextChannel *) source;
499 GError *error = NULL;
501 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
503 DEBUG ("Error: %s", error->message);
505 /* FIXME: we should use the body of the message as first argument of the
506 * signal but can't easily get it as we just get a user_data pointer. Once
507 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
508 * to use the user_data pointer to pass the message and fix this. */
509 g_signal_emit (self, signals[SEND_ERROR], 0,
510 NULL, error_to_text_send_error (error), NULL);
512 g_error_free (error);
515 tp_chat_set_delivery_status (self, token,
516 EMPATHY_DELIVERY_STATUS_SENDING);
521 list_pending_messages (EmpathyTpChat *self)
525 messages = tp_text_channel_dup_pending_messages (TP_TEXT_CHANNEL (self));
527 for (l = messages; l != NULL; l = g_list_next (l))
529 TpMessage *message = l->data;
531 handle_incoming_message (self, message, FALSE);
534 g_list_free_full (messages, g_object_unref);
538 update_subject (EmpathyTpChat *self,
539 GHashTable *properties)
541 gboolean can_set, valid;
542 const gchar *subject;
544 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
546 self->priv->can_set_subject = can_set;
548 subject = tp_asv_get_string (properties, "Subject");
553 g_free (self->priv->subject);
554 self->priv->subject = g_strdup (subject);
556 /* If the actor is included with this update, use it;
557 * otherwise, clear it to avoid showing stale information.
558 * Why might it not be included? When you join an IRC channel,
559 * you get a pair of messages: first, the current topic; next,
560 * who set it, and when. Idle reports these in two separate
563 actor = tp_asv_get_string (properties, "Actor");
564 g_free (self->priv->subject_actor);
565 self->priv->subject_actor = g_strdup (actor);
567 g_object_notify (G_OBJECT (self), "subject");
570 /* TODO: track Timestamp. */
574 tp_chat_get_all_subject_cb (TpProxy *proxy,
575 GHashTable *properties,
577 gpointer user_data G_GNUC_UNUSED,
580 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
584 DEBUG ("Error fetching subject: %s", error->message);
588 self->priv->supports_subject = TRUE;
589 update_subject (self, properties);
593 update_title (EmpathyTpChat *self,
594 GHashTable *properties)
596 const gchar *title = tp_asv_get_string (properties, "Title");
600 if (tp_str_empty (title))
603 g_free (self->priv->title);
604 self->priv->title = g_strdup (title);
605 g_object_notify (G_OBJECT (self), "title");
610 tp_chat_get_all_room_config_cb (TpProxy *proxy,
611 GHashTable *properties,
613 gpointer user_data G_GNUC_UNUSED,
616 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
620 DEBUG ("Error fetching room config: %s", error->message);
624 update_title (self, properties);
628 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
629 const gchar *interface_name,
631 const gchar **invalidated,
635 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
637 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
638 update_subject (self, changed);
640 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
641 update_title (self, changed);
645 empathy_tp_chat_set_subject (EmpathyTpChat *self,
646 const gchar *subject)
648 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
649 subject, tp_chat_async_cb, "while setting subject", NULL,
654 empathy_tp_chat_get_title (EmpathyTpChat *self)
656 return self->priv->title;
660 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
662 return self->priv->supports_subject;
666 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
668 return self->priv->can_set_subject;
672 empathy_tp_chat_get_subject (EmpathyTpChat *self)
674 return self->priv->subject;
678 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
680 return self->priv->subject_actor;
684 tp_chat_dispose (GObject *object)
686 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
688 tp_clear_object (&self->priv->remote_contact);
689 tp_clear_object (&self->priv->user);
691 g_queue_foreach (self->priv->pending_messages_queue,
692 (GFunc) g_object_unref, NULL);
693 g_queue_clear (self->priv->pending_messages_queue);
695 tp_clear_object (&self->priv->ready_result);
697 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
698 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
702 tp_chat_finalize (GObject *object)
704 EmpathyTpChat *self = (EmpathyTpChat *) object;
706 DEBUG ("Finalize: %p", object);
708 g_queue_free (self->priv->pending_messages_queue);
709 g_hash_table_unref (self->priv->messages_being_sent);
711 g_free (self->priv->title);
712 g_free (self->priv->subject);
713 g_free (self->priv->subject_actor);
715 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
719 check_almost_ready (EmpathyTpChat *self)
721 TpChannel *channel = (TpChannel *) self;
723 if (self->priv->ready_result == NULL)
726 if (self->priv->user == NULL)
729 if (self->priv->preparing_password)
732 /* We need either the members (room) or the remote contact (private chat).
733 * If the chat is protected by a password we can't get these information so
734 * consider the chat as ready so it can be presented to the user. */
735 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
736 self->priv->remote_contact == NULL)
739 g_assert (tp_proxy_is_prepared (self,
740 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
742 tp_g_signal_connect_object (self, "message-received",
743 G_CALLBACK (message_received_cb), self, 0);
744 tp_g_signal_connect_object (self, "pending-message-removed",
745 G_CALLBACK (pending_message_removed_cb), self, 0);
747 list_pending_messages (self);
749 tp_g_signal_connect_object (self, "message-sent",
750 G_CALLBACK (message_sent_cb), self, 0);
756 add_members_contact (EmpathyTpChat *self,
761 for (i = 0; i < contacts->len; i++)
763 EmpathyContact *contact;
765 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
768 self->priv->members = g_list_prepend (self->priv->members, contact);
770 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
771 contact, NULL, 0, NULL, TRUE);
774 check_almost_ready (self);
778 remove_member (EmpathyTpChat *self,
779 EmpathyContact *contact)
783 for (l = self->priv->members; l; l = l->next)
785 EmpathyContact *c = l->data;
789 self->priv->members = g_list_delete_link (self->priv->members, l);
797 contact_renamed (EmpathyTpChat *self,
798 TpContact *old_contact,
799 TpContact *new_contact,
800 TpChannelGroupChangeReason reason,
801 const gchar *message)
803 EmpathyContact *old = NULL, *new = NULL;
805 old = empathy_contact_dup_from_tp_contact (old_contact);
806 new = empathy_contact_dup_from_tp_contact (new_contact);
808 self->priv->members = g_list_prepend (self->priv->members, new);
812 remove_member (self, old);
814 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
816 g_object_unref (old);
819 if (self->priv->user == old)
821 /* We change our nick */
822 tp_clear_object (&self->priv->user);
823 self->priv->user = g_object_ref (new);
824 g_object_notify (G_OBJECT (self), "self-contact");
827 check_almost_ready (self);
831 tp_chat_group_contacts_changed_cb (TpChannel *channel,
834 GPtrArray *local_pending,
835 GPtrArray *remote_pending,
840 EmpathyContact *actor_contact = NULL;
842 TpChannelGroupChangeReason reason;
843 const gchar *message;
845 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
846 message = tp_asv_get_string (details, "message");
848 /* Contact renamed */
849 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
851 /* there can only be a single 'added' and a single 'removed' handle */
852 if (removed->len != 1 || added->len != 1)
854 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
855 added->len, removed->len);
859 contact_renamed (self, g_ptr_array_index (removed, 0),
860 g_ptr_array_index (added, 0), reason, message);
866 actor_contact = empathy_contact_dup_from_tp_contact (actor);
868 if (actor_contact == NULL)
870 /* FIXME: handle this a tad more gracefully: perhaps
871 * the actor was a server op. We could use the
872 * contact-ids detail of MembersChangedDetailed.
874 DEBUG ("actor %s not a channel member",
875 tp_contact_get_identifier (actor));
879 /* Remove contacts that are not members anymore */
880 for (i = 0; i < removed->len; i++)
882 TpContact *tp_contact = g_ptr_array_index (removed, i);
883 EmpathyContact *contact;
885 contact = empathy_contact_dup_from_tp_contact (tp_contact);
889 remove_member (self, contact);
891 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
892 contact, actor_contact, reason, message, FALSE);
893 g_object_unref (contact);
899 add_members_contact (self, added);
902 if (actor_contact != NULL)
903 g_object_unref (actor_contact);
907 create_remote_contact (EmpathyTpChat *self,
910 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
911 g_object_notify (G_OBJECT (self), "remote-contact");
913 check_almost_ready (self);
917 create_self_contact (EmpathyTpChat *self,
920 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
921 empathy_contact_set_is_user (self->priv->user, TRUE);
922 g_object_notify (G_OBJECT (self), "self-contact");
923 check_almost_ready (self);
927 tp_chat_get_property (GObject *object,
932 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
936 case PROP_SELF_CONTACT:
937 g_value_set_object (value, self->priv->user);
939 case PROP_REMOTE_CONTACT:
940 g_value_set_object (value, self->priv->remote_contact);
942 case PROP_N_MESSAGES_SENDING:
943 g_value_set_uint (value,
944 g_hash_table_size (self->priv->messages_being_sent));
947 g_value_set_string (value,
948 empathy_tp_chat_get_title (self));
951 g_value_set_string (value,
952 empathy_tp_chat_get_subject (self));
955 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
965 static const TpProxyFeature *
966 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
968 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
969 static GQuark need[3] = {0, 0, 0};
971 if (G_LIKELY (features[0].name != 0))
974 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
975 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
976 need[1] = TP_CHANNEL_FEATURE_CONTACTS;
977 features[FEAT_READY].depends_on = need;
978 features[FEAT_READY].prepare_async =
979 tp_chat_prepare_ready_async;
981 /* assert that the terminator at the end is there */
982 g_assert (features[N_FEAT].name == 0);
988 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
990 GObjectClass *object_class = G_OBJECT_CLASS (klass);
991 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
993 object_class->dispose = tp_chat_dispose;
994 object_class->finalize = tp_chat_finalize;
995 object_class->get_property = tp_chat_get_property;
997 proxy_class->list_features = tp_chat_list_features;
1000 * EmpathyTpChat:self-contact:
1002 * Not to be confused with TpChannel:group-self-contact.
1004 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1005 g_param_spec_object ("self-contact", "The local contact",
1006 "The EmpathyContact for the local user on this channel",
1007 EMPATHY_TYPE_CONTACT,
1010 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1011 g_param_spec_object ("remote-contact", "The remote contact",
1012 "The remote contact if there is no group iface on the channel",
1013 EMPATHY_TYPE_CONTACT,
1016 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1017 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1018 "The number of messages being sent",
1022 g_object_class_install_property (object_class, PROP_TITLE,
1023 g_param_spec_string ("title", "Title",
1024 "A human-readable name for the room, if any",
1027 G_PARAM_STATIC_STRINGS));
1029 g_object_class_install_property (object_class, PROP_SUBJECT,
1030 g_param_spec_string ("subject", "Subject",
1031 "The room's current subject, if any",
1034 G_PARAM_STATIC_STRINGS));
1037 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1038 G_TYPE_FROM_CLASS (klass),
1042 g_cclosure_marshal_generic,
1044 1, EMPATHY_TYPE_MESSAGE);
1046 signals[SEND_ERROR] = g_signal_new ("send-error",
1047 G_TYPE_FROM_CLASS (klass),
1051 g_cclosure_marshal_generic,
1053 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1055 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1056 G_TYPE_FROM_CLASS (klass),
1060 g_cclosure_marshal_generic,
1062 1, EMPATHY_TYPE_MESSAGE);
1064 signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1065 G_OBJECT_CLASS_TYPE (klass),
1067 0, NULL, NULL, NULL,
1069 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1070 G_TYPE_UINT, G_TYPE_STRING);
1072 signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1073 G_OBJECT_CLASS_TYPE (klass),
1075 0, NULL, NULL, NULL,
1077 5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1078 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1080 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1084 empathy_tp_chat_init (EmpathyTpChat *self)
1086 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1087 EmpathyTpChatPrivate);
1089 self->priv->pending_messages_queue = g_queue_new ();
1090 self->priv->messages_being_sent = g_hash_table_new_full (
1091 g_str_hash, g_str_equal, g_free, NULL);
1095 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1097 const gchar *object_path,
1098 const GHashTable *immutable_properties)
1100 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1101 g_return_val_if_fail (immutable_properties != NULL, NULL);
1103 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1106 "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1107 "bus-name", tp_proxy_get_bus_name (conn),
1108 "object-path", object_path,
1109 "channel-properties", immutable_properties,
1114 empathy_tp_chat_get_id (EmpathyTpChat *self)
1118 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1120 id = tp_channel_get_identifier ((TpChannel *) self);
1121 if (!TPAW_STR_EMPTY (id))
1123 else if (self->priv->remote_contact)
1124 return empathy_contact_get_id (self->priv->remote_contact);
1131 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1133 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1135 return self->priv->remote_contact;
1139 empathy_tp_chat_get_account (EmpathyTpChat *self)
1141 TpConnection *connection;
1143 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1145 connection = tp_channel_get_connection (TP_CHANNEL (self));
1147 return tp_connection_get_account (connection);
1151 empathy_tp_chat_send (EmpathyTpChat *self,
1154 gchar *message_body;
1156 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1157 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1159 message_body = tp_message_to_text (message, NULL);
1161 DEBUG ("Sending message: %s", message_body);
1163 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1164 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1165 message_send_cb, self);
1167 g_free (message_body);
1171 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1173 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1175 return self->priv->pending_messages_queue->head;
1179 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1180 EmpathyMessage *message)
1184 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1186 if (!empathy_message_is_incoming (message))
1189 tp_msg = empathy_message_get_tp_message (message);
1190 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1191 tp_msg, NULL, NULL);
1195 * empathy_tp_chat_can_add_contact:
1197 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1198 * That is if this chat is a 1-to-1 channel that can be upgraded to
1199 * a MUC using the Conference interface or if the channel is a MUC.
1202 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1204 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1206 return self->priv->can_upgrade_to_muc ||
1207 tp_proxy_has_interface_by_id (self,
1208 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1212 tp_channel_leave_async_cb (GObject *source_object,
1216 GError *error = NULL;
1218 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1220 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1222 g_error_free (error);
1227 empathy_tp_chat_leave (EmpathyTpChat *self,
1228 const gchar *message)
1230 TpChannel *channel = (TpChannel *) self;
1232 DEBUG ("Leaving channel %s with message \"%s\"",
1233 tp_channel_get_identifier (channel), message);
1235 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1236 message, tp_channel_leave_async_cb, self);
1240 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1241 TpContact **inviter)
1243 TpContact *self_contact;
1244 TpChannel *channel = TP_CHANNEL (self);
1246 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1249 self_contact = tp_channel_group_get_self_contact (channel);
1250 if (self_contact == NULL)
1253 return tp_channel_group_get_local_pending_contact_info (channel,
1254 self_contact, inviter, NULL, NULL);
1258 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1259 EmpathyContact *contact)
1261 return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1262 empathy_contact_get_tp_contact (contact));
1266 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1268 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1270 return self->priv->user;
1274 empathy_tp_chat_get_feature_ready (void)
1276 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1280 password_feature_prepare_cb (GObject *source,
1281 GAsyncResult *result,
1284 EmpathyTpChat *self = user_data;
1285 GError *error = NULL;
1287 if (!tp_proxy_prepare_finish (source, result, &error))
1289 DEBUG ("Failed to prepare Password: %s", error->message);
1290 g_error_free (error);
1293 self->priv->preparing_password = FALSE;
1295 check_almost_ready (self);
1299 continue_preparing (EmpathyTpChat *self)
1301 TpChannel *channel = (TpChannel *) self;
1302 TpConnection *connection;
1303 gboolean listen_for_dbus_properties_changed = FALSE;
1305 connection = tp_channel_get_connection (channel);
1307 if (tp_proxy_has_interface_by_id (self,
1308 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD))
1310 /* The password feature can't be a hard dep on our own feature has we
1311 * depend on it only if the channel implements the
1312 * Password interface.
1314 GQuark features[] = { TP_CHANNEL_FEATURE_PASSWORD , 0 };
1316 self->priv->preparing_password = TRUE;
1318 tp_proxy_prepare_async (self, features, password_feature_prepare_cb,
1322 if (tp_proxy_has_interface_by_id (self,
1323 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1325 GPtrArray *contacts;
1328 /* Get self contact from the group's self handle */
1329 contact = tp_channel_group_get_self_contact (channel);
1330 create_self_contact (self, contact);
1332 /* Get initial member contacts */
1333 contacts = tp_channel_group_dup_members_contacts (channel);
1334 add_members_contact (self, contacts);
1335 g_ptr_array_unref (contacts);
1337 self->priv->can_upgrade_to_muc = FALSE;
1339 tp_g_signal_connect_object (self, "group-contacts-changed",
1340 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1344 TpCapabilities *caps;
1345 GVariant *classes, *class;
1349 /* Get the self contact from the connection's self handle */
1350 contact = tp_connection_get_self_contact (connection);
1351 create_self_contact (self, contact);
1353 /* Get the remote contact */
1354 contact = tp_channel_get_target_contact (channel);
1355 create_remote_contact (self, contact);
1357 caps = tp_connection_get_capabilities (connection);
1358 g_assert (caps != NULL);
1360 classes = tp_capabilities_dup_channel_classes_variant (caps);
1362 g_variant_iter_init (&iter, classes);
1363 while ((class = g_variant_iter_next_value (&iter)))
1365 GVariant *fixed, *allowed;
1366 const gchar *chan_type = NULL;
1368 fixed = g_variant_get_child_value (class, 0);
1369 allowed = g_variant_get_child_value (class, 1);
1371 g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1373 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1375 const gchar **oprops;
1377 oprops = g_variant_get_strv (allowed, NULL);
1379 if (tp_strv_contains (oprops,
1380 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1382 self->priv->can_upgrade_to_muc = TRUE;
1388 g_variant_unref (class);
1389 g_variant_unref (fixed);
1390 g_variant_unref (allowed);
1392 if (self->priv->can_upgrade_to_muc)
1396 g_variant_unref (classes);
1399 if (tp_proxy_has_interface_by_id (self,
1400 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1402 tp_cli_dbus_properties_call_get_all (channel, -1,
1403 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1404 tp_chat_get_all_subject_cb,
1407 listen_for_dbus_properties_changed = TRUE;
1410 if (tp_proxy_has_interface_by_id (self,
1411 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1413 tp_cli_dbus_properties_call_get_all (channel, -1,
1414 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1415 tp_chat_get_all_room_config_cb,
1418 listen_for_dbus_properties_changed = TRUE;
1421 if (listen_for_dbus_properties_changed)
1423 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1424 tp_chat_dbus_properties_changed_cb,
1426 G_OBJECT (self), NULL);
1431 conn_connected_cb (GObject *source,
1432 GAsyncResult *result,
1435 EmpathyTpChat *self = user_data;
1436 GError *error = NULL;
1438 if (!tp_proxy_prepare_finish (source, result, &error))
1440 DEBUG ("Failed to prepare Connected: %s", error->message);
1441 g_simple_async_result_take_error (self->priv->ready_result, error);
1442 g_simple_async_result_complete (self->priv->ready_result);
1443 tp_clear_object (&self->priv->ready_result);
1447 continue_preparing (self);
1451 tp_chat_prepare_ready_async (TpProxy *proxy,
1452 const TpProxyFeature *feature,
1453 GAsyncReadyCallback callback,
1456 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1457 TpChannel *channel = (TpChannel *) proxy;
1458 TpConnection *connection;
1459 GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
1461 g_assert (self->priv->ready_result == NULL);
1463 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1464 callback, user_data, tp_chat_prepare_ready_async);
1466 connection = tp_channel_get_connection (channel);
1468 /* First we have to make sure that TP_CONNECTION_FEATURE_CONNECTED is
1469 * prepared as we rely on TpConnection::self-contact
1470 * in continue_preparing().
1472 * It would be nice if tp-glib could do this for us: fdo#59126 */
1473 tp_proxy_prepare_async (connection, features, conn_connected_cb, proxy);