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 <extensions/extensions.h>
27 #include "empathy-tp-chat.h"
28 #include "empathy-request-util.h"
29 #include "empathy-time.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
33 #include "empathy-debug.h"
35 struct _EmpathyTpChatPrivate
39 EmpathyContact *remote_contact;
41 /* Queue of messages signalled but not acked yet */
42 GQueue *pending_messages_queue;
45 gboolean supports_subject;
46 gboolean can_set_subject;
50 /* Room config (for now, we only track the title and don't support
54 gboolean can_upgrade_to_muc;
56 GHashTable *messages_being_sent;
58 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
59 GSimpleAsyncResult *ready_result;
60 gboolean preparing_password;
69 PROP_N_MESSAGES_SENDING,
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
89 tp_chat_set_delivery_status (EmpathyTpChat *self,
91 EmpathyDeliveryStatus delivery_status)
93 TpDeliveryReportingSupportFlags flags =
94 tp_text_channel_get_delivery_reporting_support (
95 TP_TEXT_CHANNEL (self));
97 /* channel must support receiving failures and successes */
98 if (!tp_str_empty (token) &&
99 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
100 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
102 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
104 switch (delivery_status)
106 case EMPATHY_DELIVERY_STATUS_NONE:
107 g_hash_table_remove (self->priv->messages_being_sent,
112 g_hash_table_insert (self->priv->messages_being_sent,
114 GUINT_TO_POINTER (delivery_status));
118 g_object_notify (G_OBJECT (self), "n-messages-sending");
122 static void tp_chat_prepare_ready_async (TpProxy *proxy,
123 const TpProxyFeature *feature,
124 GAsyncReadyCallback callback,
128 tp_chat_async_cb (TpChannel *proxy,
131 GObject *weak_object)
134 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
138 update_config_cb (TpChannel *proxy,
141 GObject *weak_object)
144 DEBUG ("Failed to change config of the room: %s", error->message);
148 create_conference_cb (GObject *source,
149 GAsyncResult *result,
153 GError *error = NULL;
156 channel = tp_account_channel_request_create_and_observe_channel_finish (
157 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
160 DEBUG ("Failed to create conference channel: %s", error->message);
161 g_error_free (error);
165 /* Make the channel more confidential as only people invited are supposed to
168 "Private", G_TYPE_BOOLEAN, TRUE,
169 "InviteOnly", G_TYPE_BOOLEAN, TRUE,
172 tp_cli_channel_interface_room_config_call_update_configuration (channel, -1,
173 props, update_config_cb, NULL, NULL, NULL);
175 g_object_unref (channel);
176 g_hash_table_unref (props);
180 empathy_tp_chat_add (EmpathyTpChat *self,
181 EmpathyContact *contact,
182 const gchar *message)
184 TpChannel *channel = (TpChannel *) self;
186 if (tp_proxy_has_interface_by_id (self,
187 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
190 GArray handles = {(gchar *) &handle, 1};
192 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
194 handle = empathy_contact_get_handle (contact);
195 tp_cli_channel_interface_group_call_add_members (channel,
196 -1, &handles, NULL, NULL, NULL, NULL, NULL);
198 else if (self->priv->can_upgrade_to_muc)
200 TpAccountChannelRequest *req;
202 const char *object_path;
203 GPtrArray channels = { (gpointer *) &object_path, 1 };
204 const char *invitees[2] = { NULL, };
207 invitees[0] = empathy_contact_get_id (contact);
208 object_path = tp_proxy_get_object_path (self);
211 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
212 TP_IFACE_CHANNEL_TYPE_TEXT,
213 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
215 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
216 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
217 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
218 G_TYPE_STRV, invitees,
219 /* FIXME: InvitationMessage ? */
222 account = empathy_tp_chat_get_account (self);
224 req = tp_account_channel_request_new (account, props,
225 TP_USER_ACTION_TIME_NOT_USER_ACTION);
227 /* Although this is a MUC, it's anonymous, so CreateChannel is
229 tp_account_channel_request_create_and_observe_channel_async (req,
230 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
232 g_object_unref (req);
233 g_hash_table_unref (props);
237 g_warning ("Cannot add to this channel");
242 empathy_tp_chat_get_members (EmpathyTpChat *self)
244 GList *members = NULL;
246 if (self->priv->members)
248 members = g_list_copy (self->priv->members);
249 g_list_foreach (members, (GFunc) g_object_ref, NULL);
253 members = g_list_prepend (members, g_object_ref (self->priv->user));
255 if (self->priv->remote_contact != NULL)
256 members = g_list_prepend (members,
257 g_object_ref (self->priv->remote_contact));
264 check_ready (EmpathyTpChat *self)
266 if (self->priv->ready_result == NULL)
271 g_simple_async_result_complete_in_idle (self->priv->ready_result);
272 tp_clear_object (&self->priv->ready_result);
276 tp_chat_build_message (EmpathyTpChat *self,
280 EmpathyMessage *message;
283 message = empathy_message_new_from_tp_message (msg, incoming);
284 /* FIXME: this is actually a lie for incoming messages. */
285 empathy_message_set_receiver (message, self->priv->user);
287 sender = tp_signalled_message_get_sender (msg);
288 g_assert (sender != NULL);
290 if (tp_contact_get_handle (sender) == 0)
292 empathy_message_set_sender (message, self->priv->user);
296 EmpathyContact *contact;
298 contact = empathy_contact_dup_from_tp_contact (sender);
300 empathy_message_set_sender (message, contact);
302 g_object_unref (contact);
305 g_queue_push_tail (self->priv->pending_messages_queue, message);
306 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
310 handle_delivery_report (EmpathyTpChat *self,
313 TpDeliveryStatus delivery_status;
314 const GHashTable *header;
315 TpChannelTextSendError delivery_error;
318 const gchar *message_body = NULL;
319 const gchar *delivery_dbus_error;
320 const gchar *delivery_token = NULL;
322 header = tp_message_peek (message, 0);
326 delivery_token = tp_asv_get_string (header, "delivery-token");
327 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
333 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
335 DEBUG ("Accepted %s", delivery_token);
336 tp_chat_set_delivery_status (self, delivery_token,
337 EMPATHY_DELIVERY_STATUS_ACCEPTED);
340 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
342 DEBUG ("Delivered %s", delivery_token);
343 tp_chat_set_delivery_status (self, delivery_token,
344 EMPATHY_DELIVERY_STATUS_NONE);
347 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
348 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
353 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
355 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
357 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
359 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
360 * TpMessage. (fdo #35884) */
361 echo = tp_asv_get_boxed (header, "delivery-echo",
362 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
363 if (echo != NULL && echo->len >= 2)
365 const GHashTable *echo_body;
367 echo_body = g_ptr_array_index (echo, 1);
368 if (echo_body != NULL)
369 message_body = tp_asv_get_string (echo_body, "content");
372 tp_chat_set_delivery_status (self, delivery_token,
373 EMPATHY_DELIVERY_STATUS_NONE);
374 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
375 delivery_error, delivery_dbus_error);
378 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
379 message, NULL, NULL);
383 handle_incoming_message (EmpathyTpChat *self,
389 if (tp_message_is_delivery_report (message))
391 handle_delivery_report (self, message);
395 message_body = tp_message_to_text (message, NULL);
397 DEBUG ("Message %s (channel %s): %s",
398 pending ? "pending" : "received",
399 tp_proxy_get_object_path (self), message_body);
401 if (message_body == NULL)
403 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
405 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
406 message, NULL, NULL);
410 tp_chat_build_message (self, message, TRUE);
412 g_free (message_body);
416 message_received_cb (TpTextChannel *channel,
420 handle_incoming_message (self, message, FALSE);
424 find_pending_message_func (gconstpointer a,
427 EmpathyMessage *msg = (EmpathyMessage *) a;
428 TpMessage *message = (TpMessage *) b;
430 if (empathy_message_get_tp_message (msg) == message)
437 pending_message_removed_cb (TpTextChannel *channel,
443 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
444 find_pending_message_func);
449 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
451 g_object_unref (m->data);
452 g_queue_delete_link (self->priv->pending_messages_queue, m);
456 message_sent_cb (TpTextChannel *channel,
458 TpMessageSendingFlags flags,
464 message_body = tp_message_to_text (message, NULL);
466 DEBUG ("Message sent: %s", message_body);
468 tp_chat_build_message (self, message, FALSE);
470 g_free (message_body);
473 static TpChannelTextSendError
474 error_to_text_send_error (GError *error)
476 if (error->domain != TP_ERROR)
477 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
481 case TP_ERROR_OFFLINE:
482 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
483 case TP_ERROR_INVALID_HANDLE:
484 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
485 case TP_ERROR_PERMISSION_DENIED:
486 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
487 case TP_ERROR_NOT_IMPLEMENTED:
488 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
491 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
495 message_send_cb (GObject *source,
496 GAsyncResult *result,
499 EmpathyTpChat *self = user_data;
500 TpTextChannel *channel = (TpTextChannel *) source;
502 GError *error = NULL;
504 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
506 DEBUG ("Error: %s", error->message);
508 /* FIXME: we should use the body of the message as first argument of the
509 * signal but can't easily get it as we just get a user_data pointer. Once
510 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
511 * to use the user_data pointer to pass the message and fix this. */
512 g_signal_emit (self, signals[SEND_ERROR], 0,
513 NULL, error_to_text_send_error (error), NULL);
515 g_error_free (error);
518 tp_chat_set_delivery_status (self, token,
519 EMPATHY_DELIVERY_STATUS_SENDING);
524 list_pending_messages (EmpathyTpChat *self)
528 messages = tp_text_channel_dup_pending_messages (TP_TEXT_CHANNEL (self));
530 for (l = messages; l != NULL; l = g_list_next (l))
532 TpMessage *message = l->data;
534 handle_incoming_message (self, message, FALSE);
537 g_list_free_full (messages, g_object_unref);
541 update_subject (EmpathyTpChat *self,
542 GHashTable *properties)
544 gboolean can_set, valid;
545 const gchar *subject;
547 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
549 self->priv->can_set_subject = can_set;
551 subject = tp_asv_get_string (properties, "Subject");
556 g_free (self->priv->subject);
557 self->priv->subject = g_strdup (subject);
559 /* If the actor is included with this update, use it;
560 * otherwise, clear it to avoid showing stale information.
561 * Why might it not be included? When you join an IRC channel,
562 * you get a pair of messages: first, the current topic; next,
563 * who set it, and when. Idle reports these in two separate
566 actor = tp_asv_get_string (properties, "Actor");
567 g_free (self->priv->subject_actor);
568 self->priv->subject_actor = g_strdup (actor);
570 g_object_notify (G_OBJECT (self), "subject");
573 /* TODO: track Timestamp. */
577 tp_chat_get_all_subject_cb (TpProxy *proxy,
578 GHashTable *properties,
580 gpointer user_data G_GNUC_UNUSED,
583 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
587 DEBUG ("Error fetching subject: %s", error->message);
591 self->priv->supports_subject = TRUE;
592 update_subject (self, properties);
596 update_title (EmpathyTpChat *self,
597 GHashTable *properties)
599 const gchar *title = tp_asv_get_string (properties, "Title");
603 if (tp_str_empty (title))
606 g_free (self->priv->title);
607 self->priv->title = g_strdup (title);
608 g_object_notify (G_OBJECT (self), "title");
613 tp_chat_get_all_room_config_cb (TpProxy *proxy,
614 GHashTable *properties,
616 gpointer user_data G_GNUC_UNUSED,
619 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
623 DEBUG ("Error fetching room config: %s", error->message);
627 update_title (self, properties);
631 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
632 const gchar *interface_name,
634 const gchar **invalidated,
638 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
640 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
641 update_subject (self, changed);
643 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
644 update_title (self, changed);
648 empathy_tp_chat_set_subject (EmpathyTpChat *self,
649 const gchar *subject)
651 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
652 subject, tp_chat_async_cb, "while setting subject", NULL,
657 empathy_tp_chat_get_title (EmpathyTpChat *self)
659 return self->priv->title;
663 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
665 return self->priv->supports_subject;
669 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
671 return self->priv->can_set_subject;
675 empathy_tp_chat_get_subject (EmpathyTpChat *self)
677 return self->priv->subject;
681 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
683 return self->priv->subject_actor;
687 tp_chat_dispose (GObject *object)
689 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
691 tp_clear_object (&self->priv->remote_contact);
692 tp_clear_object (&self->priv->user);
694 g_queue_foreach (self->priv->pending_messages_queue,
695 (GFunc) g_object_unref, NULL);
696 g_queue_clear (self->priv->pending_messages_queue);
698 tp_clear_object (&self->priv->ready_result);
700 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
701 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
705 tp_chat_finalize (GObject *object)
707 EmpathyTpChat *self = (EmpathyTpChat *) object;
709 DEBUG ("Finalize: %p", object);
711 g_queue_free (self->priv->pending_messages_queue);
712 g_hash_table_unref (self->priv->messages_being_sent);
714 g_free (self->priv->title);
715 g_free (self->priv->subject);
716 g_free (self->priv->subject_actor);
718 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
722 check_almost_ready (EmpathyTpChat *self)
724 TpChannel *channel = (TpChannel *) self;
726 if (self->priv->ready_result == NULL)
729 if (self->priv->user == NULL)
732 if (self->priv->preparing_password)
735 /* We need either the members (room) or the remote contact (private chat).
736 * If the chat is protected by a password we can't get these information so
737 * consider the chat as ready so it can be presented to the user. */
738 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
739 self->priv->remote_contact == NULL)
742 g_assert (tp_proxy_is_prepared (self,
743 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
745 tp_g_signal_connect_object (self, "message-received",
746 G_CALLBACK (message_received_cb), self, 0);
747 tp_g_signal_connect_object (self, "pending-message-removed",
748 G_CALLBACK (pending_message_removed_cb), self, 0);
750 list_pending_messages (self);
752 tp_g_signal_connect_object (self, "message-sent",
753 G_CALLBACK (message_sent_cb), self, 0);
759 add_members_contact (EmpathyTpChat *self,
764 for (i = 0; i < contacts->len; i++)
766 EmpathyContact *contact;
768 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
771 self->priv->members = g_list_prepend (self->priv->members, contact);
773 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
774 contact, NULL, 0, NULL, TRUE);
777 check_almost_ready (self);
781 remove_member (EmpathyTpChat *self,
782 EmpathyContact *contact)
786 for (l = self->priv->members; l; l = l->next)
788 EmpathyContact *c = l->data;
792 self->priv->members = g_list_delete_link (self->priv->members, l);
800 contact_renamed (EmpathyTpChat *self,
801 TpContact *old_contact,
802 TpContact *new_contact,
803 TpChannelGroupChangeReason reason,
804 const gchar *message)
806 EmpathyContact *old = NULL, *new = NULL;
808 old = empathy_contact_dup_from_tp_contact (old_contact);
809 new = empathy_contact_dup_from_tp_contact (new_contact);
811 self->priv->members = g_list_prepend (self->priv->members, new);
815 remove_member (self, old);
817 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
819 g_object_unref (old);
822 if (self->priv->user == old)
824 /* We change our nick */
825 tp_clear_object (&self->priv->user);
826 self->priv->user = g_object_ref (new);
827 g_object_notify (G_OBJECT (self), "self-contact");
830 check_almost_ready (self);
834 tp_chat_group_contacts_changed_cb (TpChannel *channel,
837 GPtrArray *local_pending,
838 GPtrArray *remote_pending,
843 EmpathyContact *actor_contact = NULL;
845 TpChannelGroupChangeReason reason;
846 const gchar *message;
848 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
849 message = tp_asv_get_string (details, "message");
851 /* Contact renamed */
852 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
854 /* there can only be a single 'added' and a single 'removed' handle */
855 if (removed->len != 1 || added->len != 1)
857 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
858 added->len, removed->len);
862 contact_renamed (self, g_ptr_array_index (removed, 0),
863 g_ptr_array_index (added, 0), reason, message);
869 actor_contact = empathy_contact_dup_from_tp_contact (actor);
871 if (actor_contact == NULL)
873 /* FIXME: handle this a tad more gracefully: perhaps
874 * the actor was a server op. We could use the
875 * contact-ids detail of MembersChangedDetailed.
877 DEBUG ("actor %s not a channel member",
878 tp_contact_get_identifier (actor));
882 /* Remove contacts that are not members anymore */
883 for (i = 0; i < removed->len; i++)
885 TpContact *tp_contact = g_ptr_array_index (removed, i);
886 EmpathyContact *contact;
888 contact = empathy_contact_dup_from_tp_contact (tp_contact);
892 remove_member (self, contact);
894 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
895 contact, actor_contact, reason, message, FALSE);
896 g_object_unref (contact);
902 add_members_contact (self, added);
905 if (actor_contact != NULL)
906 g_object_unref (actor_contact);
910 create_remote_contact (EmpathyTpChat *self,
913 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
914 g_object_notify (G_OBJECT (self), "remote-contact");
916 check_almost_ready (self);
920 create_self_contact (EmpathyTpChat *self,
923 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
924 empathy_contact_set_is_user (self->priv->user, TRUE);
925 g_object_notify (G_OBJECT (self), "self-contact");
926 check_almost_ready (self);
930 tp_chat_get_property (GObject *object,
935 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
939 case PROP_SELF_CONTACT:
940 g_value_set_object (value, self->priv->user);
942 case PROP_REMOTE_CONTACT:
943 g_value_set_object (value, self->priv->remote_contact);
945 case PROP_N_MESSAGES_SENDING:
946 g_value_set_uint (value,
947 g_hash_table_size (self->priv->messages_being_sent));
950 g_value_set_string (value,
951 empathy_tp_chat_get_title (self));
954 g_value_set_string (value,
955 empathy_tp_chat_get_subject (self));
958 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
968 static const TpProxyFeature *
969 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
971 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
972 static GQuark need[2] = {0, 0};
974 if (G_LIKELY (features[0].name != 0))
977 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
978 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
979 features[FEAT_READY].depends_on = need;
980 features[FEAT_READY].prepare_async =
981 tp_chat_prepare_ready_async;
983 /* assert that the terminator at the end is there */
984 g_assert (features[N_FEAT].name == 0);
990 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
992 GObjectClass *object_class = G_OBJECT_CLASS (klass);
993 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
995 object_class->dispose = tp_chat_dispose;
996 object_class->finalize = tp_chat_finalize;
997 object_class->get_property = tp_chat_get_property;
999 proxy_class->list_features = tp_chat_list_features;
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,
1099 const gchar *object_path,
1100 const GHashTable *immutable_properties)
1102 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1103 g_return_val_if_fail (immutable_properties != NULL, NULL);
1105 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1108 "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1109 "bus-name", tp_proxy_get_bus_name (conn),
1110 "object-path", object_path,
1111 "channel-properties", immutable_properties,
1116 empathy_tp_chat_get_id (EmpathyTpChat *self)
1120 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1122 id = tp_channel_get_identifier ((TpChannel *) self);
1123 if (!EMP_STR_EMPTY (id))
1125 else if (self->priv->remote_contact)
1126 return empathy_contact_get_id (self->priv->remote_contact);
1133 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1135 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1137 return self->priv->remote_contact;
1141 empathy_tp_chat_get_account (EmpathyTpChat *self)
1143 TpConnection *connection;
1145 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1147 connection = tp_channel_get_connection (TP_CHANNEL (self));
1149 return tp_connection_get_account (connection);
1153 empathy_tp_chat_send (EmpathyTpChat *self,
1156 gchar *message_body;
1158 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1159 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1161 message_body = tp_message_to_text (message, NULL);
1163 DEBUG ("Sending message: %s", message_body);
1165 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1166 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1167 message_send_cb, self);
1169 g_free (message_body);
1173 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1175 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1177 return self->priv->pending_messages_queue->head;
1181 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1182 EmpathyMessage *message)
1186 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1188 if (!empathy_message_is_incoming (message))
1191 tp_msg = empathy_message_get_tp_message (message);
1192 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1193 tp_msg, NULL, NULL);
1197 * empathy_tp_chat_can_add_contact:
1199 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1200 * That is if this chat is a 1-to-1 channel that can be upgraded to
1201 * a MUC using the Conference interface or if the channel is a MUC.
1204 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1206 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1208 return self->priv->can_upgrade_to_muc ||
1209 tp_proxy_has_interface_by_id (self,
1210 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1214 tp_channel_leave_async_cb (GObject *source_object,
1218 GError *error = NULL;
1220 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1222 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1224 g_error_free (error);
1229 empathy_tp_chat_leave (EmpathyTpChat *self,
1230 const gchar *message)
1232 TpChannel *channel = (TpChannel *) self;
1234 DEBUG ("Leaving channel %s with message \"%s\"",
1235 tp_channel_get_identifier (channel), message);
1237 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1238 message, tp_channel_leave_async_cb, self);
1242 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1243 TpContact **inviter)
1245 TpContact *self_contact;
1246 TpChannel *channel = TP_CHANNEL (self);
1248 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1251 self_contact = tp_channel_group_get_self_contact (channel);
1252 if (self_contact == NULL)
1255 return tp_channel_group_get_local_pending_contact_info (channel,
1256 self_contact, inviter, NULL, NULL);
1260 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1261 EmpathyContact *contact)
1263 return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1264 empathy_contact_get_tp_contact (contact));
1268 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1270 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1272 return self->priv->user;
1276 empathy_tp_chat_get_feature_ready (void)
1278 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1282 password_feature_prepare_cb (GObject *source,
1283 GAsyncResult *result,
1286 EmpathyTpChat *self = user_data;
1287 GError *error = NULL;
1289 if (!tp_proxy_prepare_finish (source, result, &error))
1291 DEBUG ("Failed to prepare Password: %s", error->message);
1292 g_error_free (error);
1295 self->priv->preparing_password = FALSE;
1297 check_almost_ready (self);
1301 tp_chat_prepare_ready_async (TpProxy *proxy,
1302 const TpProxyFeature *feature,
1303 GAsyncReadyCallback callback,
1306 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1307 TpChannel *channel = (TpChannel *) proxy;
1308 TpConnection *connection;
1309 gboolean listen_for_dbus_properties_changed = FALSE;
1311 g_assert (self->priv->ready_result == NULL);
1312 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1313 callback, user_data, tp_chat_prepare_ready_async);
1315 connection = tp_channel_get_connection (channel);
1317 if (tp_proxy_has_interface_by_id (self,
1318 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD))
1320 /* The password feature can't be a hard dep on our own feature has we
1321 * depend on it only if the channel implements the
1322 * Password interface.
1324 GQuark features[] = { TP_CHANNEL_FEATURE_PASSWORD , 0 };
1326 self->priv->preparing_password = TRUE;
1328 tp_proxy_prepare_async (self, features, password_feature_prepare_cb,
1332 if (tp_proxy_has_interface_by_id (self,
1333 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1335 GPtrArray *contacts;
1338 /* Get self contact from the group's self handle */
1339 contact = tp_channel_group_get_self_contact (channel);
1340 create_self_contact (self, contact);
1342 /* Get initial member contacts */
1343 contacts = tp_channel_group_dup_members_contacts (channel);
1344 add_members_contact (self, contacts);
1345 g_ptr_array_unref (contacts);
1347 self->priv->can_upgrade_to_muc = FALSE;
1349 tp_g_signal_connect_object (self, "group-contacts-changed",
1350 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1354 TpCapabilities *caps;
1355 GVariant *classes, *class;
1359 /* Get the self contact from the connection's self handle */
1360 contact = tp_connection_get_self_contact (connection);
1361 create_self_contact (self, contact);
1363 /* Get the remote contact */
1364 contact = tp_channel_get_target_contact (channel);
1365 create_remote_contact (self, contact);
1367 caps = tp_connection_get_capabilities (connection);
1368 g_assert (caps != NULL);
1370 classes = tp_capabilities_dup_channel_classes_variant (caps);
1372 g_variant_iter_init (&iter, classes);
1373 while ((class = g_variant_iter_next_value (&iter)))
1375 GVariant *fixed, *allowed;
1376 const gchar *chan_type = NULL;
1378 fixed = g_variant_get_child_value (class, 0);
1379 allowed = g_variant_get_child_value (class, 1);
1381 g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1383 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1385 const gchar **oprops;
1387 oprops = g_variant_get_strv (allowed, NULL);
1389 if (tp_strv_contains (oprops,
1390 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1392 self->priv->can_upgrade_to_muc = TRUE;
1398 g_variant_unref (class);
1399 g_variant_unref (fixed);
1400 g_variant_unref (allowed);
1402 if (self->priv->can_upgrade_to_muc)
1406 g_variant_unref (classes);
1409 if (tp_proxy_has_interface_by_id (self,
1410 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1412 tp_cli_dbus_properties_call_get_all (channel, -1,
1413 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1414 tp_chat_get_all_subject_cb,
1417 listen_for_dbus_properties_changed = TRUE;
1420 if (tp_proxy_has_interface_by_id (self,
1421 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1423 tp_cli_dbus_properties_call_get_all (channel, -1,
1424 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1425 tp_chat_get_all_room_config_cb,
1428 listen_for_dbus_properties_changed = TRUE;
1431 if (listen_for_dbus_properties_changed)
1433 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1434 tp_chat_dbus_properties_changed_cb,
1436 G_OBJECT (self), NULL);