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-request-util.h"
31 #include "empathy-time.h"
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
35 #include "empathy-debug.h"
37 struct _EmpathyTpChatPrivate
41 EmpathyContact *remote_contact;
43 /* Queue of messages signalled but not acked yet */
44 GQueue *pending_messages_queue;
47 gboolean supports_subject;
48 gboolean can_set_subject;
52 /* Room config (for now, we only track the title and don't support
56 gboolean can_upgrade_to_muc;
58 GHashTable *messages_being_sent;
60 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
61 GSimpleAsyncResult *ready_result;
70 PROP_N_MESSAGES_SENDING,
85 static guint signals[LAST_SIGNAL];
87 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
90 tp_chat_set_delivery_status (EmpathyTpChat *self,
92 EmpathyDeliveryStatus delivery_status)
94 TpDeliveryReportingSupportFlags flags =
95 tp_text_channel_get_delivery_reporting_support (
96 TP_TEXT_CHANNEL (self));
98 /* channel must support receiving failures and successes */
99 if (!tp_str_empty (token) &&
100 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
101 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
103 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
105 switch (delivery_status)
107 case EMPATHY_DELIVERY_STATUS_NONE:
108 g_hash_table_remove (self->priv->messages_being_sent,
113 g_hash_table_insert (self->priv->messages_being_sent,
115 GUINT_TO_POINTER (delivery_status));
119 g_object_notify (G_OBJECT (self), "n-messages-sending");
123 static void tp_chat_prepare_ready_async (TpProxy *proxy,
124 const TpProxyFeature *feature,
125 GAsyncReadyCallback callback,
129 tp_chat_async_cb (TpChannel *proxy,
132 GObject *weak_object)
135 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
139 create_conference_cb (GObject *source,
140 GAsyncResult *result,
143 GError *error = NULL;
145 if (!tp_account_channel_request_create_channel_finish (
146 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
148 DEBUG ("Failed to create conference channel: %s", error->message);
149 g_error_free (error);
154 empathy_tp_chat_add (EmpathyTpChat *self,
155 EmpathyContact *contact,
156 const gchar *message)
158 TpChannel *channel = (TpChannel *) self;
160 if (tp_proxy_has_interface_by_id (self,
161 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
164 GArray handles = {(gchar *) &handle, 1};
166 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
168 handle = empathy_contact_get_handle (contact);
169 tp_cli_channel_interface_group_call_add_members (channel,
170 -1, &handles, NULL, NULL, NULL, NULL, NULL);
172 else if (self->priv->can_upgrade_to_muc)
174 TpAccountChannelRequest *req;
176 const char *object_path;
177 GPtrArray channels = { (gpointer *) &object_path, 1 };
178 const char *invitees[2] = { NULL, };
181 invitees[0] = empathy_contact_get_id (contact);
182 object_path = tp_proxy_get_object_path (self);
185 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
186 TP_IFACE_CHANNEL_TYPE_TEXT,
187 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
189 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
190 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
191 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
192 G_TYPE_STRV, invitees,
193 /* FIXME: InvitationMessage ? */
196 account = empathy_tp_chat_get_account (self);
198 req = tp_account_channel_request_new (account, props,
199 TP_USER_ACTION_TIME_NOT_USER_ACTION);
201 /* Although this is a MUC, it's anonymous, so CreateChannel is
203 tp_account_channel_request_create_channel_async (req,
204 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
206 g_object_unref (req);
207 g_hash_table_unref (props);
211 g_warning ("Cannot add to this channel");
216 empathy_tp_chat_get_members (EmpathyTpChat *self)
218 GList *members = NULL;
220 if (self->priv->members)
222 members = g_list_copy (self->priv->members);
223 g_list_foreach (members, (GFunc) g_object_ref, NULL);
227 members = g_list_prepend (members, g_object_ref (self->priv->user));
229 if (self->priv->remote_contact != NULL)
230 members = g_list_prepend (members,
231 g_object_ref (self->priv->remote_contact));
238 check_ready (EmpathyTpChat *self)
240 if (self->priv->ready_result == NULL)
245 g_simple_async_result_complete_in_idle (self->priv->ready_result);
246 tp_clear_object (&self->priv->ready_result);
250 tp_chat_build_message (EmpathyTpChat *self,
254 EmpathyMessage *message;
257 message = empathy_message_new_from_tp_message (msg, incoming);
258 /* FIXME: this is actually a lie for incoming messages. */
259 empathy_message_set_receiver (message, self->priv->user);
261 sender = tp_signalled_message_get_sender (msg);
262 g_assert (sender != NULL);
264 if (tp_contact_get_handle (sender) == 0)
266 empathy_message_set_sender (message, self->priv->user);
270 EmpathyContact *contact;
272 contact = empathy_contact_dup_from_tp_contact (sender);
274 empathy_message_set_sender (message, contact);
276 g_object_unref (contact);
279 g_queue_push_tail (self->priv->pending_messages_queue, message);
280 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
284 handle_delivery_report (EmpathyTpChat *self,
287 TpDeliveryStatus delivery_status;
288 const GHashTable *header;
289 TpChannelTextSendError delivery_error;
292 const gchar *message_body = NULL;
293 const gchar *delivery_dbus_error;
294 const gchar *delivery_token = NULL;
296 header = tp_message_peek (message, 0);
300 delivery_token = tp_asv_get_string (header, "delivery-token");
301 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
307 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
309 DEBUG ("Accepted %s", delivery_token);
310 tp_chat_set_delivery_status (self, delivery_token,
311 EMPATHY_DELIVERY_STATUS_ACCEPTED);
314 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
316 DEBUG ("Delivered %s", delivery_token);
317 tp_chat_set_delivery_status (self, delivery_token,
318 EMPATHY_DELIVERY_STATUS_NONE);
321 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
322 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
327 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
329 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
331 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
333 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
334 * TpMessage. (fdo #35884) */
335 echo = tp_asv_get_boxed (header, "delivery-echo",
336 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
337 if (echo != NULL && echo->len >= 2)
339 const GHashTable *echo_body;
341 echo_body = g_ptr_array_index (echo, 1);
342 if (echo_body != NULL)
343 message_body = tp_asv_get_string (echo_body, "content");
346 tp_chat_set_delivery_status (self, delivery_token,
347 EMPATHY_DELIVERY_STATUS_NONE);
348 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
349 delivery_error, delivery_dbus_error);
352 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
353 message, NULL, NULL);
357 handle_incoming_message (EmpathyTpChat *self,
363 if (tp_message_is_delivery_report (message))
365 handle_delivery_report (self, message);
369 message_body = tp_message_to_text (message, NULL);
371 DEBUG ("Message %s (channel %s): %s",
372 pending ? "pending" : "received",
373 tp_proxy_get_object_path (self), message_body);
375 if (message_body == NULL)
377 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
379 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
380 message, NULL, NULL);
384 tp_chat_build_message (self, message, TRUE);
386 g_free (message_body);
390 message_received_cb (TpTextChannel *channel,
394 handle_incoming_message (self, message, FALSE);
398 find_pending_message_func (gconstpointer a,
401 EmpathyMessage *msg = (EmpathyMessage *) a;
402 TpMessage *message = (TpMessage *) b;
404 if (empathy_message_get_tp_message (msg) == message)
411 pending_message_removed_cb (TpTextChannel *channel,
417 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
418 find_pending_message_func);
423 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
425 g_object_unref (m->data);
426 g_queue_delete_link (self->priv->pending_messages_queue, m);
430 message_sent_cb (TpTextChannel *channel,
432 TpMessageSendingFlags flags,
438 message_body = tp_message_to_text (message, NULL);
440 DEBUG ("Message sent: %s", message_body);
442 tp_chat_build_message (self, message, FALSE);
444 g_free (message_body);
447 static TpChannelTextSendError
448 error_to_text_send_error (GError *error)
450 if (error->domain != TP_ERROR)
451 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
455 case TP_ERROR_OFFLINE:
456 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
457 case TP_ERROR_INVALID_HANDLE:
458 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
459 case TP_ERROR_PERMISSION_DENIED:
460 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
461 case TP_ERROR_NOT_IMPLEMENTED:
462 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
465 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
469 message_send_cb (GObject *source,
470 GAsyncResult *result,
473 EmpathyTpChat *self = user_data;
474 TpTextChannel *channel = (TpTextChannel *) source;
476 GError *error = NULL;
478 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
480 DEBUG ("Error: %s", error->message);
482 /* FIXME: we should use the body of the message as first argument of the
483 * signal but can't easily get it as we just get a user_data pointer. Once
484 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
485 * to use the user_data pointer to pass the message and fix this. */
486 g_signal_emit (self, signals[SEND_ERROR], 0,
487 NULL, error_to_text_send_error (error), NULL);
489 g_error_free (error);
492 tp_chat_set_delivery_status (self, token,
493 EMPATHY_DELIVERY_STATUS_SENDING);
498 list_pending_messages (EmpathyTpChat *self)
502 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
504 for (l = messages; l != NULL; l = g_list_next (l))
506 TpMessage *message = l->data;
508 handle_incoming_message (self, message, FALSE);
511 g_list_free (messages);
515 update_subject (EmpathyTpChat *self,
516 GHashTable *properties)
518 gboolean can_set, valid;
519 const gchar *subject;
521 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
523 self->priv->can_set_subject = can_set;
525 subject = tp_asv_get_string (properties, "Subject");
530 g_free (self->priv->subject);
531 self->priv->subject = g_strdup (subject);
533 /* If the actor is included with this update, use it;
534 * otherwise, clear it to avoid showing stale information.
535 * Why might it not be included? When you join an IRC channel,
536 * you get a pair of messages: first, the current topic; next,
537 * who set it, and when. Idle reports these in two separate
540 actor = tp_asv_get_string (properties, "Actor");
541 g_free (self->priv->subject_actor);
542 self->priv->subject_actor = g_strdup (actor);
544 g_object_notify (G_OBJECT (self), "subject");
547 /* TODO: track Timestamp. */
551 tp_chat_get_all_subject_cb (TpProxy *proxy,
552 GHashTable *properties,
554 gpointer user_data G_GNUC_UNUSED,
557 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
561 DEBUG ("Error fetching subject: %s", error->message);
565 self->priv->supports_subject = TRUE;
566 update_subject (self, properties);
570 update_title (EmpathyTpChat *self,
571 GHashTable *properties)
573 const gchar *title = tp_asv_get_string (properties, "Title");
577 if (tp_str_empty (title))
580 g_free (self->priv->title);
581 self->priv->title = g_strdup (title);
582 g_object_notify (G_OBJECT (self), "title");
587 tp_chat_get_all_room_config_cb (TpProxy *proxy,
588 GHashTable *properties,
590 gpointer user_data G_GNUC_UNUSED,
593 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
597 DEBUG ("Error fetching room config: %s", error->message);
601 update_title (self, properties);
605 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
606 const gchar *interface_name,
608 const gchar **invalidated,
612 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
614 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
615 update_subject (self, changed);
617 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
618 update_title (self, changed);
622 empathy_tp_chat_set_subject (EmpathyTpChat *self,
623 const gchar *subject)
625 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
626 subject, tp_chat_async_cb, "while setting subject", NULL,
631 empathy_tp_chat_get_title (EmpathyTpChat *self)
633 return self->priv->title;
637 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
639 return self->priv->supports_subject;
643 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
645 return self->priv->can_set_subject;
649 empathy_tp_chat_get_subject (EmpathyTpChat *self)
651 return self->priv->subject;
655 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
657 return self->priv->subject_actor;
661 tp_chat_dispose (GObject *object)
663 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
665 tp_clear_object (&self->priv->remote_contact);
666 tp_clear_object (&self->priv->user);
668 g_queue_foreach (self->priv->pending_messages_queue,
669 (GFunc) g_object_unref, NULL);
670 g_queue_clear (self->priv->pending_messages_queue);
672 tp_clear_object (&self->priv->ready_result);
674 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
675 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
679 tp_chat_finalize (GObject *object)
681 EmpathyTpChat *self = (EmpathyTpChat *) object;
683 DEBUG ("Finalize: %p", object);
685 g_queue_free (self->priv->pending_messages_queue);
686 g_hash_table_unref (self->priv->messages_being_sent);
688 g_free (self->priv->title);
689 g_free (self->priv->subject);
690 g_free (self->priv->subject_actor);
692 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
696 check_almost_ready (EmpathyTpChat *self)
698 TpChannel *channel = (TpChannel *) self;
700 if (self->priv->ready_result == NULL)
703 if (self->priv->user == NULL)
706 /* We need either the members (room) or the remote contact (private chat).
707 * If the chat is protected by a password we can't get these information so
708 * consider the chat as ready so it can be presented to the user. */
709 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
710 self->priv->remote_contact == NULL)
713 g_assert (tp_proxy_is_prepared (self,
714 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
716 tp_g_signal_connect_object (self, "message-received",
717 G_CALLBACK (message_received_cb), self, 0);
718 tp_g_signal_connect_object (self, "pending-message-removed",
719 G_CALLBACK (pending_message_removed_cb), self, 0);
721 list_pending_messages (self);
723 tp_g_signal_connect_object (self, "message-sent",
724 G_CALLBACK (message_sent_cb), self, 0);
730 add_members_contact (EmpathyTpChat *self,
735 for (i = 0; i < contacts->len; i++)
737 EmpathyContact *contact;
739 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
742 self->priv->members = g_list_prepend (self->priv->members, contact);
744 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
745 contact, NULL, 0, NULL, TRUE);
748 check_almost_ready (self);
752 remove_member (EmpathyTpChat *self,
753 EmpathyContact *contact)
757 for (l = self->priv->members; l; l = l->next)
759 EmpathyContact *c = l->data;
763 self->priv->members = g_list_delete_link (self->priv->members, l);
771 contact_renamed (EmpathyTpChat *self,
772 TpContact *old_contact,
773 TpContact *new_contact,
774 TpChannelGroupChangeReason reason,
775 const gchar *message)
777 EmpathyContact *old = NULL, *new = NULL;
779 old = empathy_contact_dup_from_tp_contact (old_contact);
780 new = empathy_contact_dup_from_tp_contact (new_contact);
782 self->priv->members = g_list_prepend (self->priv->members, new);
786 remove_member (self, old);
788 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
790 g_object_unref (old);
793 if (self->priv->user == old)
795 /* We change our nick */
796 tp_clear_object (&self->priv->user);
797 self->priv->user = g_object_ref (new);
798 g_object_notify (G_OBJECT (self), "self-contact");
801 check_almost_ready (self);
805 tp_chat_group_contacts_changed_cb (TpChannel *channel,
808 GPtrArray *local_pending,
809 GPtrArray *remote_pending,
814 EmpathyContact *actor_contact = NULL;
816 TpChannelGroupChangeReason reason;
817 const gchar *message;
819 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
820 message = tp_asv_get_string (details, "message");
822 /* Contact renamed */
823 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
825 /* there can only be a single 'added' and a single 'removed' handle */
826 if (removed->len != 1 || added->len != 1)
828 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
829 added->len, removed->len);
833 contact_renamed (self, g_ptr_array_index (removed, 0),
834 g_ptr_array_index (added, 0), reason, message);
840 actor_contact = empathy_contact_dup_from_tp_contact (actor);
842 if (actor_contact == NULL)
844 /* FIXME: handle this a tad more gracefully: perhaps
845 * the actor was a server op. We could use the
846 * contact-ids detail of MembersChangedDetailed.
848 DEBUG ("actor %s not a channel member",
849 tp_contact_get_identifier (actor));
853 /* Remove contacts that are not members anymore */
854 for (i = 0; i < removed->len; i++)
856 TpContact *tp_contact = g_ptr_array_index (removed, i);
857 EmpathyContact *contact;
859 contact = empathy_contact_dup_from_tp_contact (tp_contact);
863 remove_member (self, contact);
865 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
866 contact, actor_contact, reason, message, FALSE);
867 g_object_unref (contact);
873 add_members_contact (self, added);
876 if (actor_contact != NULL)
877 g_object_unref (actor_contact);
881 create_remote_contact (EmpathyTpChat *self,
884 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
885 g_object_notify (G_OBJECT (self), "remote-contact");
887 check_almost_ready (self);
891 create_self_contact (EmpathyTpChat *self,
894 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
895 empathy_contact_set_is_user (self->priv->user, TRUE);
896 g_object_notify (G_OBJECT (self), "self-contact");
897 check_almost_ready (self);
901 tp_chat_get_property (GObject *object,
906 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
910 case PROP_SELF_CONTACT:
911 g_value_set_object (value, self->priv->user);
913 case PROP_REMOTE_CONTACT:
914 g_value_set_object (value, self->priv->remote_contact);
916 case PROP_N_MESSAGES_SENDING:
917 g_value_set_uint (value,
918 g_hash_table_size (self->priv->messages_being_sent));
921 g_value_set_string (value,
922 empathy_tp_chat_get_title (self));
925 g_value_set_string (value,
926 empathy_tp_chat_get_subject (self));
929 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
939 static const TpProxyFeature *
940 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
942 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
943 static GQuark need[2] = {0, 0};
945 if (G_LIKELY (features[0].name != 0))
948 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
949 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
950 features[FEAT_READY].depends_on = need;
951 features[FEAT_READY].prepare_async =
952 tp_chat_prepare_ready_async;
954 /* assert that the terminator at the end is there */
955 g_assert (features[N_FEAT].name == 0);
961 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
963 GObjectClass *object_class = G_OBJECT_CLASS (klass);
964 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
966 object_class->dispose = tp_chat_dispose;
967 object_class->finalize = tp_chat_finalize;
968 object_class->get_property = tp_chat_get_property;
970 proxy_class->list_features = tp_chat_list_features;
973 * EmpathyTpChat:self-contact:
975 * Not to be confused with TpChannel:group-self-contact.
977 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
978 g_param_spec_object ("self-contact", "The local contact",
979 "The EmpathyContact for the local user on this channel",
980 EMPATHY_TYPE_CONTACT,
983 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
984 g_param_spec_object ("remote-contact", "The remote contact",
985 "The remote contact if there is no group iface on the channel",
986 EMPATHY_TYPE_CONTACT,
989 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
990 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
991 "The number of messages being sent",
995 g_object_class_install_property (object_class, PROP_TITLE,
996 g_param_spec_string ("title", "Title",
997 "A human-readable name for the room, if any",
1000 G_PARAM_STATIC_STRINGS));
1002 g_object_class_install_property (object_class, PROP_SUBJECT,
1003 g_param_spec_string ("subject", "Subject",
1004 "The room's current subject, if any",
1007 G_PARAM_STATIC_STRINGS));
1010 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1011 G_TYPE_FROM_CLASS (klass),
1015 g_cclosure_marshal_generic,
1017 1, EMPATHY_TYPE_MESSAGE);
1019 signals[SEND_ERROR] = g_signal_new ("send-error",
1020 G_TYPE_FROM_CLASS (klass),
1024 g_cclosure_marshal_generic,
1026 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1028 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1029 G_TYPE_FROM_CLASS (klass),
1033 g_cclosure_marshal_generic,
1035 1, EMPATHY_TYPE_MESSAGE);
1037 signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1038 G_OBJECT_CLASS_TYPE (klass),
1040 0, NULL, NULL, NULL,
1042 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1043 G_TYPE_UINT, G_TYPE_STRING);
1045 signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1046 G_OBJECT_CLASS_TYPE (klass),
1048 0, NULL, NULL, NULL,
1050 5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1051 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1053 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1057 empathy_tp_chat_init (EmpathyTpChat *self)
1059 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1060 EmpathyTpChatPrivate);
1062 self->priv->pending_messages_queue = g_queue_new ();
1063 self->priv->messages_being_sent = g_hash_table_new_full (
1064 g_str_hash, g_str_equal, g_free, NULL);
1068 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1070 const gchar *object_path,
1071 const GHashTable *immutable_properties)
1073 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1074 g_return_val_if_fail (immutable_properties != NULL, NULL);
1076 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1079 "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1080 "bus-name", tp_proxy_get_bus_name (conn),
1081 "object-path", object_path,
1082 "channel-properties", immutable_properties,
1087 empathy_tp_chat_get_id (EmpathyTpChat *self)
1091 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1093 id = tp_channel_get_identifier ((TpChannel *) self);
1094 if (!EMP_STR_EMPTY (id))
1096 else if (self->priv->remote_contact)
1097 return empathy_contact_get_id (self->priv->remote_contact);
1104 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1106 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1108 return self->priv->remote_contact;
1112 empathy_tp_chat_get_account (EmpathyTpChat *self)
1114 TpConnection *connection;
1116 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1118 connection = tp_channel_borrow_connection (TP_CHANNEL (self));
1120 return tp_connection_get_account (connection);
1124 empathy_tp_chat_send (EmpathyTpChat *self,
1127 gchar *message_body;
1129 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1130 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1132 message_body = tp_message_to_text (message, NULL);
1134 DEBUG ("Sending message: %s", message_body);
1136 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1137 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1138 message_send_cb, self);
1140 g_free (message_body);
1144 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1146 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1148 return self->priv->pending_messages_queue->head;
1152 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1153 EmpathyMessage *message)
1157 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1159 if (!empathy_message_is_incoming (message))
1162 tp_msg = empathy_message_get_tp_message (message);
1163 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1164 tp_msg, NULL, NULL);
1168 * empathy_tp_chat_can_add_contact:
1170 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1171 * That is if this chat is a 1-to-1 channel that can be upgraded to
1172 * a MUC using the Conference interface or if the channel is a MUC.
1175 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1177 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1179 return self->priv->can_upgrade_to_muc ||
1180 tp_proxy_has_interface_by_id (self,
1181 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1185 tp_channel_leave_async_cb (GObject *source_object,
1189 GError *error = NULL;
1191 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1193 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1195 g_error_free (error);
1200 empathy_tp_chat_leave (EmpathyTpChat *self,
1201 const gchar *message)
1203 TpChannel *channel = (TpChannel *) self;
1205 DEBUG ("Leaving channel %s with message \"%s\"",
1206 tp_channel_get_identifier (channel), message);
1208 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1209 message, tp_channel_leave_async_cb, self);
1213 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1214 TpContact **inviter)
1216 TpContact *self_contact;
1217 TpChannel *channel = TP_CHANNEL (self);
1219 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1222 self_contact = tp_channel_group_get_self_contact (channel);
1223 if (self_contact == NULL)
1226 return tp_channel_group_get_local_pending_contact_info (channel,
1227 self_contact, inviter, NULL, NULL);
1231 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1232 EmpathyContact *contact)
1234 return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1235 empathy_contact_get_tp_contact (contact));
1239 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1241 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1243 return self->priv->user;
1247 empathy_tp_chat_get_feature_ready (void)
1249 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1253 tp_chat_prepare_ready_async (TpProxy *proxy,
1254 const TpProxyFeature *feature,
1255 GAsyncReadyCallback callback,
1258 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1259 TpChannel *channel = (TpChannel *) proxy;
1260 TpConnection *connection;
1261 gboolean listen_for_dbus_properties_changed = FALSE;
1263 g_assert (self->priv->ready_result == NULL);
1264 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1265 callback, user_data, tp_chat_prepare_ready_async);
1267 connection = tp_channel_borrow_connection (channel);
1269 if (tp_proxy_has_interface_by_id (self,
1270 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1272 GPtrArray *contacts;
1275 /* Get self contact from the group's self handle */
1276 contact = tp_channel_group_get_self_contact (channel);
1277 create_self_contact (self, contact);
1279 /* Get initial member contacts */
1280 contacts = tp_channel_group_dup_members_contacts (channel);
1281 add_members_contact (self, contacts);
1282 g_ptr_array_unref (contacts);
1284 self->priv->can_upgrade_to_muc = FALSE;
1286 tp_g_signal_connect_object (self, "group-contacts-changed",
1287 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1291 TpCapabilities *caps;
1292 GVariant *classes, *class;
1296 /* Get the self contact from the connection's self handle */
1297 contact = tp_connection_get_self_contact (connection);
1298 create_self_contact (self, contact);
1300 /* Get the remote contact */
1301 contact = tp_channel_get_target_contact (channel);
1302 create_remote_contact (self, contact);
1304 caps = tp_connection_get_capabilities (connection);
1305 g_assert (caps != NULL);
1307 classes = tp_capabilities_dup_channel_classes_variant (caps);
1309 g_variant_iter_init (&iter, classes);
1310 while ((class = g_variant_iter_next_value (&iter)))
1312 GVariant *fixed, *allowed;
1313 const gchar *chan_type = NULL;
1315 fixed = g_variant_get_child_value (class, 0);
1316 allowed = g_variant_get_child_value (class, 1);
1318 g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1320 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1322 const gchar **oprops;
1324 oprops = g_variant_get_strv (allowed, NULL);
1326 if (tp_strv_contains (oprops,
1327 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1329 self->priv->can_upgrade_to_muc = TRUE;
1335 g_variant_unref (class);
1336 g_variant_unref (fixed);
1337 g_variant_unref (allowed);
1339 if (self->priv->can_upgrade_to_muc)
1343 g_variant_unref (classes);
1346 if (tp_proxy_has_interface_by_id (self,
1347 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1349 tp_cli_dbus_properties_call_get_all (channel, -1,
1350 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1351 tp_chat_get_all_subject_cb,
1354 listen_for_dbus_properties_changed = TRUE;
1357 if (tp_proxy_has_interface_by_id (self,
1358 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1360 tp_cli_dbus_properties_call_get_all (channel, -1,
1361 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1362 tp_chat_get_all_room_config_cb,
1365 listen_for_dbus_properties_changed = TRUE;
1368 if (listen_for_dbus_properties_changed)
1370 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1371 tp_chat_dbus_properties_changed_cb,
1373 G_OBJECT (self), NULL);