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, };
180 invitees[0] = empathy_contact_get_id (contact);
181 object_path = tp_proxy_get_object_path (self);
184 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
185 TP_IFACE_CHANNEL_TYPE_TEXT,
186 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
188 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
189 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
190 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
191 G_TYPE_STRV, invitees,
192 /* FIXME: InvitationMessage ? */
195 req = tp_account_channel_request_new (self->priv->account, props,
196 TP_USER_ACTION_TIME_NOT_USER_ACTION);
198 /* Although this is a MUC, it's anonymous, so CreateChannel is
200 tp_account_channel_request_create_channel_async (req,
201 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
203 g_object_unref (req);
204 g_hash_table_unref (props);
208 g_warning ("Cannot add to this channel");
213 empathy_tp_chat_get_members (EmpathyTpChat *self)
215 GList *members = NULL;
217 if (self->priv->members)
219 members = g_list_copy (self->priv->members);
220 g_list_foreach (members, (GFunc) g_object_ref, NULL);
224 members = g_list_prepend (members, g_object_ref (self->priv->user));
226 if (self->priv->remote_contact != NULL)
227 members = g_list_prepend (members,
228 g_object_ref (self->priv->remote_contact));
235 check_ready (EmpathyTpChat *self)
237 if (self->priv->ready_result == NULL)
242 g_simple_async_result_complete_in_idle (self->priv->ready_result);
243 tp_clear_object (&self->priv->ready_result);
247 tp_chat_build_message (EmpathyTpChat *self,
251 EmpathyMessage *message;
254 message = empathy_message_new_from_tp_message (msg, incoming);
255 /* FIXME: this is actually a lie for incoming messages. */
256 empathy_message_set_receiver (message, self->priv->user);
258 sender = tp_signalled_message_get_sender (msg);
259 g_assert (sender != NULL);
261 if (tp_contact_get_handle (sender) == 0)
263 empathy_message_set_sender (message, self->priv->user);
267 EmpathyContact *contact;
269 contact = empathy_contact_dup_from_tp_contact (sender);
271 empathy_message_set_sender (message, contact);
273 g_object_unref (contact);
276 g_queue_push_tail (self->priv->pending_messages_queue, message);
277 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
281 handle_delivery_report (EmpathyTpChat *self,
284 TpDeliveryStatus delivery_status;
285 const GHashTable *header;
286 TpChannelTextSendError delivery_error;
289 const gchar *message_body = NULL;
290 const gchar *delivery_dbus_error;
291 const gchar *delivery_token = NULL;
293 header = tp_message_peek (message, 0);
297 delivery_token = tp_asv_get_string (header, "delivery-token");
298 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
304 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
306 DEBUG ("Accepted %s", delivery_token);
307 tp_chat_set_delivery_status (self, delivery_token,
308 EMPATHY_DELIVERY_STATUS_ACCEPTED);
311 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
313 DEBUG ("Delivered %s", delivery_token);
314 tp_chat_set_delivery_status (self, delivery_token,
315 EMPATHY_DELIVERY_STATUS_NONE);
318 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
319 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
324 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
326 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
328 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
330 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
331 * TpMessage. (fdo #35884) */
332 echo = tp_asv_get_boxed (header, "delivery-echo",
333 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
334 if (echo != NULL && echo->len >= 2)
336 const GHashTable *echo_body;
338 echo_body = g_ptr_array_index (echo, 1);
339 if (echo_body != NULL)
340 message_body = tp_asv_get_string (echo_body, "content");
343 tp_chat_set_delivery_status (self, delivery_token,
344 EMPATHY_DELIVERY_STATUS_NONE);
345 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
346 delivery_error, delivery_dbus_error);
349 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
350 message, NULL, NULL);
354 handle_incoming_message (EmpathyTpChat *self,
360 if (tp_message_is_delivery_report (message))
362 handle_delivery_report (self, message);
366 message_body = tp_message_to_text (message, NULL);
368 DEBUG ("Message %s (channel %s): %s",
369 pending ? "pending" : "received",
370 tp_proxy_get_object_path (self), message_body);
372 if (message_body == NULL)
374 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
376 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
377 message, NULL, NULL);
381 tp_chat_build_message (self, message, TRUE);
383 g_free (message_body);
387 message_received_cb (TpTextChannel *channel,
391 handle_incoming_message (self, message, FALSE);
395 find_pending_message_func (gconstpointer a,
398 EmpathyMessage *msg = (EmpathyMessage *) a;
399 TpMessage *message = (TpMessage *) b;
401 if (empathy_message_get_tp_message (msg) == message)
408 pending_message_removed_cb (TpTextChannel *channel,
414 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
415 find_pending_message_func);
420 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
422 g_object_unref (m->data);
423 g_queue_delete_link (self->priv->pending_messages_queue, m);
427 message_sent_cb (TpTextChannel *channel,
429 TpMessageSendingFlags flags,
435 message_body = tp_message_to_text (message, NULL);
437 DEBUG ("Message sent: %s", message_body);
439 tp_chat_build_message (self, message, FALSE);
441 g_free (message_body);
444 static TpChannelTextSendError
445 error_to_text_send_error (GError *error)
447 if (error->domain != TP_ERROR)
448 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
452 case TP_ERROR_OFFLINE:
453 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
454 case TP_ERROR_INVALID_HANDLE:
455 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
456 case TP_ERROR_PERMISSION_DENIED:
457 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
458 case TP_ERROR_NOT_IMPLEMENTED:
459 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
462 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
466 message_send_cb (GObject *source,
467 GAsyncResult *result,
470 EmpathyTpChat *self = user_data;
471 TpTextChannel *channel = (TpTextChannel *) source;
473 GError *error = NULL;
475 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
477 DEBUG ("Error: %s", error->message);
479 /* FIXME: we should use the body of the message as first argument of the
480 * signal but can't easily get it as we just get a user_data pointer. Once
481 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
482 * to use the user_data pointer to pass the message and fix this. */
483 g_signal_emit (self, signals[SEND_ERROR], 0,
484 NULL, error_to_text_send_error (error), NULL);
486 g_error_free (error);
489 tp_chat_set_delivery_status (self, token,
490 EMPATHY_DELIVERY_STATUS_SENDING);
495 list_pending_messages (EmpathyTpChat *self)
499 messages = tp_text_channel_get_pending_messages (TP_TEXT_CHANNEL (self));
501 for (l = messages; l != NULL; l = g_list_next (l))
503 TpMessage *message = l->data;
505 handle_incoming_message (self, message, FALSE);
508 g_list_free (messages);
512 update_subject (EmpathyTpChat *self,
513 GHashTable *properties)
515 gboolean can_set, valid;
516 const gchar *subject;
518 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
520 self->priv->can_set_subject = can_set;
522 subject = tp_asv_get_string (properties, "Subject");
527 g_free (self->priv->subject);
528 self->priv->subject = g_strdup (subject);
530 /* If the actor is included with this update, use it;
531 * otherwise, clear it to avoid showing stale information.
532 * Why might it not be included? When you join an IRC channel,
533 * you get a pair of messages: first, the current topic; next,
534 * who set it, and when. Idle reports these in two separate
537 actor = tp_asv_get_string (properties, "Actor");
538 g_free (self->priv->subject_actor);
539 self->priv->subject_actor = g_strdup (actor);
541 g_object_notify (G_OBJECT (self), "subject");
544 /* TODO: track Timestamp. */
548 tp_chat_get_all_subject_cb (TpProxy *proxy,
549 GHashTable *properties,
551 gpointer user_data G_GNUC_UNUSED,
554 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
558 DEBUG ("Error fetching subject: %s", error->message);
562 self->priv->supports_subject = TRUE;
563 update_subject (self, properties);
567 update_title (EmpathyTpChat *self,
568 GHashTable *properties)
570 const gchar *title = tp_asv_get_string (properties, "Title");
574 if (tp_str_empty (title))
577 g_free (self->priv->title);
578 self->priv->title = g_strdup (title);
579 g_object_notify (G_OBJECT (self), "title");
584 tp_chat_get_all_room_config_cb (TpProxy *proxy,
585 GHashTable *properties,
587 gpointer user_data G_GNUC_UNUSED,
590 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
594 DEBUG ("Error fetching room config: %s", error->message);
598 update_title (self, properties);
602 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
603 const gchar *interface_name,
605 const gchar **invalidated,
609 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
611 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
612 update_subject (self, changed);
614 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
615 update_title (self, changed);
619 empathy_tp_chat_set_subject (EmpathyTpChat *self,
620 const gchar *subject)
622 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
623 subject, tp_chat_async_cb, "while setting subject", NULL,
628 empathy_tp_chat_get_title (EmpathyTpChat *self)
630 return self->priv->title;
634 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
636 return self->priv->supports_subject;
640 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
642 return self->priv->can_set_subject;
646 empathy_tp_chat_get_subject (EmpathyTpChat *self)
648 return self->priv->subject;
652 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
654 return self->priv->subject_actor;
658 tp_chat_dispose (GObject *object)
660 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
662 tp_clear_object (&self->priv->account);
663 tp_clear_object (&self->priv->remote_contact);
664 tp_clear_object (&self->priv->user);
666 g_queue_foreach (self->priv->pending_messages_queue,
667 (GFunc) g_object_unref, NULL);
668 g_queue_clear (self->priv->pending_messages_queue);
670 tp_clear_object (&self->priv->ready_result);
672 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
673 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
677 tp_chat_finalize (GObject *object)
679 EmpathyTpChat *self = (EmpathyTpChat *) object;
681 DEBUG ("Finalize: %p", object);
683 g_queue_free (self->priv->pending_messages_queue);
684 g_hash_table_unref (self->priv->messages_being_sent);
686 g_free (self->priv->title);
687 g_free (self->priv->subject);
688 g_free (self->priv->subject_actor);
690 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
694 check_almost_ready (EmpathyTpChat *self)
696 TpChannel *channel = (TpChannel *) self;
698 if (self->priv->ready_result == NULL)
701 if (self->priv->user == NULL)
704 /* We need either the members (room) or the remote contact (private chat).
705 * If the chat is protected by a password we can't get these information so
706 * consider the chat as ready so it can be presented to the user. */
707 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
708 self->priv->remote_contact == NULL)
711 g_assert (tp_proxy_is_prepared (self,
712 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
714 tp_g_signal_connect_object (self, "message-received",
715 G_CALLBACK (message_received_cb), self, 0);
716 tp_g_signal_connect_object (self, "pending-message-removed",
717 G_CALLBACK (pending_message_removed_cb), self, 0);
719 list_pending_messages (self);
721 tp_g_signal_connect_object (self, "message-sent",
722 G_CALLBACK (message_sent_cb), self, 0);
728 add_members_contact (EmpathyTpChat *self,
733 for (i = 0; i < contacts->len; i++)
735 EmpathyContact *contact;
737 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
740 self->priv->members = g_list_prepend (self->priv->members, contact);
742 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
743 contact, NULL, 0, NULL, TRUE);
746 check_almost_ready (self);
750 remove_member (EmpathyTpChat *self,
751 EmpathyContact *contact)
755 for (l = self->priv->members; l; l = l->next)
757 EmpathyContact *c = l->data;
761 self->priv->members = g_list_delete_link (self->priv->members, l);
769 contact_renamed (EmpathyTpChat *self,
770 TpContact *old_contact,
771 TpContact *new_contact,
772 TpChannelGroupChangeReason reason,
773 const gchar *message)
775 EmpathyContact *old = NULL, *new = NULL;
777 old = empathy_contact_dup_from_tp_contact (old_contact);
778 new = empathy_contact_dup_from_tp_contact (new_contact);
780 self->priv->members = g_list_prepend (self->priv->members, new);
784 remove_member (self, old);
786 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
788 g_object_unref (old);
791 if (self->priv->user == old)
793 /* We change our nick */
794 tp_clear_object (&self->priv->user);
795 self->priv->user = g_object_ref (new);
796 g_object_notify (G_OBJECT (self), "self-contact");
799 check_almost_ready (self);
803 tp_chat_group_contacts_changed_cb (TpChannel *channel,
806 GPtrArray *local_pending,
807 GPtrArray *remote_pending,
812 EmpathyContact *actor_contact = NULL;
814 TpChannelGroupChangeReason reason;
815 const gchar *message;
817 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
818 message = tp_asv_get_string (details, "message");
820 /* Contact renamed */
821 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
823 /* there can only be a single 'added' and a single 'removed' handle */
824 if (removed->len != 1 || added->len != 1)
826 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
827 added->len, removed->len);
831 contact_renamed (self, g_ptr_array_index (removed, 0),
832 g_ptr_array_index (added, 0), reason, message);
838 actor_contact = empathy_contact_dup_from_tp_contact (actor);
840 if (actor_contact == NULL)
842 /* FIXME: handle this a tad more gracefully: perhaps
843 * the actor was a server op. We could use the
844 * contact-ids detail of MembersChangedDetailed.
846 DEBUG ("actor %s not a channel member",
847 tp_contact_get_identifier (actor));
851 /* Remove contacts that are not members anymore */
852 for (i = 0; i < removed->len; i++)
854 TpContact *tp_contact = g_ptr_array_index (removed, i);
855 EmpathyContact *contact;
857 contact = empathy_contact_dup_from_tp_contact (tp_contact);
861 remove_member (self, contact);
863 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
864 contact, actor_contact, reason, message, FALSE);
865 g_object_unref (contact);
871 add_members_contact (self, added);
874 if (actor_contact != NULL)
875 g_object_unref (actor_contact);
879 create_remote_contact (EmpathyTpChat *self,
882 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
883 g_object_notify (G_OBJECT (self), "remote-contact");
885 check_almost_ready (self);
889 create_self_contact (EmpathyTpChat *self,
892 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
893 empathy_contact_set_is_user (self->priv->user, TRUE);
894 g_object_notify (G_OBJECT (self), "self-contact");
895 check_almost_ready (self);
899 tp_chat_get_property (GObject *object,
904 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
909 g_value_set_object (value, self->priv->account);
911 case PROP_SELF_CONTACT:
912 g_value_set_object (value, self->priv->user);
914 case PROP_REMOTE_CONTACT:
915 g_value_set_object (value, self->priv->remote_contact);
917 case PROP_N_MESSAGES_SENDING:
918 g_value_set_uint (value,
919 g_hash_table_size (self->priv->messages_being_sent));
922 g_value_set_string (value,
923 empathy_tp_chat_get_title (self));
926 g_value_set_string (value,
927 empathy_tp_chat_get_subject (self));
930 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
936 tp_chat_set_property (GObject *object,
941 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
946 self->priv->account = g_value_dup_object (value);
949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
959 static const TpProxyFeature *
960 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
962 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
963 static GQuark need[2] = {0, 0};
965 if (G_LIKELY (features[0].name != 0))
968 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
969 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
970 features[FEAT_READY].depends_on = need;
971 features[FEAT_READY].prepare_async =
972 tp_chat_prepare_ready_async;
974 /* assert that the terminator at the end is there */
975 g_assert (features[N_FEAT].name == 0);
981 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
983 GObjectClass *object_class = G_OBJECT_CLASS (klass);
984 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
986 object_class->dispose = tp_chat_dispose;
987 object_class->finalize = tp_chat_finalize;
988 object_class->get_property = tp_chat_get_property;
989 object_class->set_property = tp_chat_set_property;
991 proxy_class->list_features = tp_chat_list_features;
993 g_object_class_install_property (object_class, PROP_ACCOUNT,
994 g_param_spec_object ("account", "TpAccount",
995 "the account associated with the chat",
998 G_PARAM_CONSTRUCT_ONLY |
999 G_PARAM_STATIC_STRINGS));
1002 * EmpathyTpChat:self-contact:
1004 * Not to be confused with TpChannel:group-self-contact.
1006 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
1007 g_param_spec_object ("self-contact", "The local contact",
1008 "The EmpathyContact for the local user on this channel",
1009 EMPATHY_TYPE_CONTACT,
1012 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
1013 g_param_spec_object ("remote-contact", "The remote contact",
1014 "The remote contact if there is no group iface on the channel",
1015 EMPATHY_TYPE_CONTACT,
1018 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
1019 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
1020 "The number of messages being sent",
1024 g_object_class_install_property (object_class, PROP_TITLE,
1025 g_param_spec_string ("title", "Title",
1026 "A human-readable name for the room, if any",
1029 G_PARAM_STATIC_STRINGS));
1031 g_object_class_install_property (object_class, PROP_SUBJECT,
1032 g_param_spec_string ("subject", "Subject",
1033 "The room's current subject, if any",
1036 G_PARAM_STATIC_STRINGS));
1039 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1040 G_TYPE_FROM_CLASS (klass),
1044 g_cclosure_marshal_generic,
1046 1, EMPATHY_TYPE_MESSAGE);
1048 signals[SEND_ERROR] = g_signal_new ("send-error",
1049 G_TYPE_FROM_CLASS (klass),
1053 g_cclosure_marshal_generic,
1055 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1057 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1058 G_TYPE_FROM_CLASS (klass),
1062 g_cclosure_marshal_generic,
1064 1, EMPATHY_TYPE_MESSAGE);
1066 signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1067 G_OBJECT_CLASS_TYPE (klass),
1069 0, NULL, NULL, NULL,
1071 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1072 G_TYPE_UINT, G_TYPE_STRING);
1074 signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1075 G_OBJECT_CLASS_TYPE (klass),
1077 0, NULL, NULL, NULL,
1079 5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1080 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1082 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1086 empathy_tp_chat_init (EmpathyTpChat *self)
1088 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1089 EmpathyTpChatPrivate);
1091 self->priv->pending_messages_queue = g_queue_new ();
1092 self->priv->messages_being_sent = g_hash_table_new_full (
1093 g_str_hash, g_str_equal, g_free, NULL);
1097 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1100 const gchar *object_path,
1101 const GHashTable *immutable_properties)
1103 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1104 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1105 g_return_val_if_fail (immutable_properties != NULL, NULL);
1107 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1111 "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1112 "bus-name", tp_proxy_get_bus_name (conn),
1113 "object-path", object_path,
1114 "channel-properties", immutable_properties,
1119 empathy_tp_chat_get_id (EmpathyTpChat *self)
1123 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1125 id = tp_channel_get_identifier ((TpChannel *) self);
1126 if (!EMP_STR_EMPTY (id))
1128 else if (self->priv->remote_contact)
1129 return empathy_contact_get_id (self->priv->remote_contact);
1136 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1138 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1140 return self->priv->remote_contact;
1144 empathy_tp_chat_get_account (EmpathyTpChat *self)
1146 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1148 return self->priv->account;
1152 empathy_tp_chat_send (EmpathyTpChat *self,
1155 gchar *message_body;
1157 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1158 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1160 message_body = tp_message_to_text (message, NULL);
1162 DEBUG ("Sending message: %s", message_body);
1164 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1165 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1166 message_send_cb, self);
1168 g_free (message_body);
1172 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1174 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1176 return self->priv->pending_messages_queue->head;
1180 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1181 EmpathyMessage *message)
1185 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1187 if (!empathy_message_is_incoming (message))
1190 tp_msg = empathy_message_get_tp_message (message);
1191 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1192 tp_msg, NULL, NULL);
1196 * empathy_tp_chat_can_add_contact:
1198 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1199 * That is if this chat is a 1-to-1 channel that can be upgraded to
1200 * a MUC using the Conference interface or if the channel is a MUC.
1203 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1205 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1207 return self->priv->can_upgrade_to_muc ||
1208 tp_proxy_has_interface_by_id (self,
1209 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1213 tp_channel_leave_async_cb (GObject *source_object,
1217 GError *error = NULL;
1219 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1221 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1223 g_error_free (error);
1228 empathy_tp_chat_leave (EmpathyTpChat *self,
1229 const gchar *message)
1231 TpChannel *channel = (TpChannel *) self;
1233 DEBUG ("Leaving channel %s with message \"%s\"",
1234 tp_channel_get_identifier (channel), message);
1236 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1237 message, tp_channel_leave_async_cb, self);
1241 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1242 TpContact **inviter)
1244 TpContact *self_contact;
1245 TpChannel *channel = TP_CHANNEL (self);
1247 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1250 self_contact = tp_channel_group_get_self_contact (channel);
1251 if (self_contact == NULL)
1254 return tp_channel_group_get_local_pending_contact_info (channel,
1255 self_contact, inviter, NULL, NULL);
1259 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1260 EmpathyContact *contact)
1262 return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1263 empathy_contact_get_tp_contact (contact));
1267 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1269 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1271 return self->priv->user;
1275 empathy_tp_chat_get_feature_ready (void)
1277 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1281 tp_chat_prepare_ready_async (TpProxy *proxy,
1282 const TpProxyFeature *feature,
1283 GAsyncReadyCallback callback,
1286 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1287 TpChannel *channel = (TpChannel *) proxy;
1288 TpConnection *connection;
1289 gboolean listen_for_dbus_properties_changed = FALSE;
1291 g_assert (self->priv->ready_result == NULL);
1292 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1293 callback, user_data, tp_chat_prepare_ready_async);
1295 connection = tp_channel_borrow_connection (channel);
1297 if (tp_proxy_has_interface_by_id (self,
1298 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1300 GPtrArray *contacts;
1303 /* Get self contact from the group's self handle */
1304 contact = tp_channel_group_get_self_contact (channel);
1305 create_self_contact (self, contact);
1307 /* Get initial member contacts */
1308 contacts = tp_channel_group_dup_members_contacts (channel);
1309 add_members_contact (self, contacts);
1310 g_ptr_array_unref (contacts);
1312 self->priv->can_upgrade_to_muc = FALSE;
1314 tp_g_signal_connect_object (self, "group-contacts-changed",
1315 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1319 TpCapabilities *caps;
1320 GVariant *classes, *class;
1324 /* Get the self contact from the connection's self handle */
1325 contact = tp_connection_get_self_contact (connection);
1326 create_self_contact (self, contact);
1328 /* Get the remote contact */
1329 contact = tp_channel_get_target_contact (channel);
1330 create_remote_contact (self, contact);
1332 caps = tp_connection_get_capabilities (connection);
1333 g_assert (caps != NULL);
1335 classes = tp_capabilities_dup_channel_classes_variant (caps);
1337 g_variant_iter_init (&iter, classes);
1338 while ((class = g_variant_iter_next_value (&iter)))
1340 GVariant *fixed, *allowed;
1341 const gchar *chan_type = NULL;
1343 fixed = g_variant_get_child_value (class, 0);
1344 allowed = g_variant_get_child_value (class, 1);
1346 g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1348 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1350 const gchar **oprops;
1352 oprops = g_variant_get_strv (allowed, NULL);
1354 if (tp_strv_contains (oprops,
1355 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1357 self->priv->can_upgrade_to_muc = TRUE;
1363 g_variant_unref (class);
1364 g_variant_unref (fixed);
1365 g_variant_unref (allowed);
1367 if (self->priv->can_upgrade_to_muc)
1371 g_variant_unref (classes);
1374 if (tp_proxy_has_interface_by_id (self,
1375 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1377 tp_cli_dbus_properties_call_get_all (channel, -1,
1378 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1379 tp_chat_get_all_subject_cb,
1382 listen_for_dbus_properties_changed = TRUE;
1385 if (tp_proxy_has_interface_by_id (self,
1386 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1388 tp_cli_dbus_properties_call_get_all (channel, -1,
1389 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1390 tp_chat_get_all_room_config_cb,
1393 listen_for_dbus_properties_changed = TRUE;
1396 if (listen_for_dbus_properties_changed)
1398 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1399 tp_chat_dbus_properties_changed_cb,
1401 G_OBJECT (self), NULL);