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;
62 gboolean preparing_password;
71 PROP_N_MESSAGES_SENDING,
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL)
91 tp_chat_set_delivery_status (EmpathyTpChat *self,
93 EmpathyDeliveryStatus delivery_status)
95 TpDeliveryReportingSupportFlags flags =
96 tp_text_channel_get_delivery_reporting_support (
97 TP_TEXT_CHANNEL (self));
99 /* channel must support receiving failures and successes */
100 if (!tp_str_empty (token) &&
101 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
102 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES)
104 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
106 switch (delivery_status)
108 case EMPATHY_DELIVERY_STATUS_NONE:
109 g_hash_table_remove (self->priv->messages_being_sent,
114 g_hash_table_insert (self->priv->messages_being_sent,
116 GUINT_TO_POINTER (delivery_status));
120 g_object_notify (G_OBJECT (self), "n-messages-sending");
124 static void tp_chat_prepare_ready_async (TpProxy *proxy,
125 const TpProxyFeature *feature,
126 GAsyncReadyCallback callback,
130 tp_chat_async_cb (TpChannel *proxy,
133 GObject *weak_object)
136 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
140 create_conference_cb (GObject *source,
141 GAsyncResult *result,
144 GError *error = NULL;
146 if (!tp_account_channel_request_create_channel_finish (
147 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
149 DEBUG ("Failed to create conference channel: %s", error->message);
150 g_error_free (error);
155 empathy_tp_chat_add (EmpathyTpChat *self,
156 EmpathyContact *contact,
157 const gchar *message)
159 TpChannel *channel = (TpChannel *) self;
161 if (tp_proxy_has_interface_by_id (self,
162 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
165 GArray handles = {(gchar *) &handle, 1};
167 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
169 handle = empathy_contact_get_handle (contact);
170 tp_cli_channel_interface_group_call_add_members (channel,
171 -1, &handles, NULL, NULL, NULL, NULL, NULL);
173 else if (self->priv->can_upgrade_to_muc)
175 TpAccountChannelRequest *req;
177 const char *object_path;
178 GPtrArray channels = { (gpointer *) &object_path, 1 };
179 const char *invitees[2] = { NULL, };
182 invitees[0] = empathy_contact_get_id (contact);
183 object_path = tp_proxy_get_object_path (self);
186 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
187 TP_IFACE_CHANNEL_TYPE_TEXT,
188 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
190 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
191 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
192 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
193 G_TYPE_STRV, invitees,
194 /* FIXME: InvitationMessage ? */
197 account = empathy_tp_chat_get_account (self);
199 req = tp_account_channel_request_new (account, props,
200 TP_USER_ACTION_TIME_NOT_USER_ACTION);
202 /* Although this is a MUC, it's anonymous, so CreateChannel is
204 tp_account_channel_request_create_channel_async (req,
205 EMPATHY_CHAT_BUS_NAME, NULL, create_conference_cb, NULL);
207 g_object_unref (req);
208 g_hash_table_unref (props);
212 g_warning ("Cannot add to this channel");
217 empathy_tp_chat_get_members (EmpathyTpChat *self)
219 GList *members = NULL;
221 if (self->priv->members)
223 members = g_list_copy (self->priv->members);
224 g_list_foreach (members, (GFunc) g_object_ref, NULL);
228 members = g_list_prepend (members, g_object_ref (self->priv->user));
230 if (self->priv->remote_contact != NULL)
231 members = g_list_prepend (members,
232 g_object_ref (self->priv->remote_contact));
239 check_ready (EmpathyTpChat *self)
241 if (self->priv->ready_result == NULL)
246 g_simple_async_result_complete_in_idle (self->priv->ready_result);
247 tp_clear_object (&self->priv->ready_result);
251 tp_chat_build_message (EmpathyTpChat *self,
255 EmpathyMessage *message;
258 message = empathy_message_new_from_tp_message (msg, incoming);
259 /* FIXME: this is actually a lie for incoming messages. */
260 empathy_message_set_receiver (message, self->priv->user);
262 sender = tp_signalled_message_get_sender (msg);
263 g_assert (sender != NULL);
265 if (tp_contact_get_handle (sender) == 0)
267 empathy_message_set_sender (message, self->priv->user);
271 EmpathyContact *contact;
273 contact = empathy_contact_dup_from_tp_contact (sender);
275 empathy_message_set_sender (message, contact);
277 g_object_unref (contact);
280 g_queue_push_tail (self->priv->pending_messages_queue, message);
281 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
285 handle_delivery_report (EmpathyTpChat *self,
288 TpDeliveryStatus delivery_status;
289 const GHashTable *header;
290 TpChannelTextSendError delivery_error;
293 const gchar *message_body = NULL;
294 const gchar *delivery_dbus_error;
295 const gchar *delivery_token = NULL;
297 header = tp_message_peek (message, 0);
301 delivery_token = tp_asv_get_string (header, "delivery-token");
302 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
308 else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED)
310 DEBUG ("Accepted %s", delivery_token);
311 tp_chat_set_delivery_status (self, delivery_token,
312 EMPATHY_DELIVERY_STATUS_ACCEPTED);
315 else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED)
317 DEBUG ("Delivered %s", delivery_token);
318 tp_chat_set_delivery_status (self, delivery_token,
319 EMPATHY_DELIVERY_STATUS_NONE);
322 else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
323 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED)
328 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
330 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
332 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
334 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
335 * TpMessage. (fdo #35884) */
336 echo = tp_asv_get_boxed (header, "delivery-echo",
337 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
338 if (echo != NULL && echo->len >= 2)
340 const GHashTable *echo_body;
342 echo_body = g_ptr_array_index (echo, 1);
343 if (echo_body != NULL)
344 message_body = tp_asv_get_string (echo_body, "content");
347 tp_chat_set_delivery_status (self, delivery_token,
348 EMPATHY_DELIVERY_STATUS_NONE);
349 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
350 delivery_error, delivery_dbus_error);
353 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
354 message, NULL, NULL);
358 handle_incoming_message (EmpathyTpChat *self,
364 if (tp_message_is_delivery_report (message))
366 handle_delivery_report (self, message);
370 message_body = tp_message_to_text (message, NULL);
372 DEBUG ("Message %s (channel %s): %s",
373 pending ? "pending" : "received",
374 tp_proxy_get_object_path (self), message_body);
376 if (message_body == NULL)
378 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
380 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
381 message, NULL, NULL);
385 tp_chat_build_message (self, message, TRUE);
387 g_free (message_body);
391 message_received_cb (TpTextChannel *channel,
395 handle_incoming_message (self, message, FALSE);
399 find_pending_message_func (gconstpointer a,
402 EmpathyMessage *msg = (EmpathyMessage *) a;
403 TpMessage *message = (TpMessage *) b;
405 if (empathy_message_get_tp_message (msg) == message)
412 pending_message_removed_cb (TpTextChannel *channel,
418 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
419 find_pending_message_func);
424 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
426 g_object_unref (m->data);
427 g_queue_delete_link (self->priv->pending_messages_queue, m);
431 message_sent_cb (TpTextChannel *channel,
433 TpMessageSendingFlags flags,
439 message_body = tp_message_to_text (message, NULL);
441 DEBUG ("Message sent: %s", message_body);
443 tp_chat_build_message (self, message, FALSE);
445 g_free (message_body);
448 static TpChannelTextSendError
449 error_to_text_send_error (GError *error)
451 if (error->domain != TP_ERROR)
452 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
456 case TP_ERROR_OFFLINE:
457 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
458 case TP_ERROR_INVALID_HANDLE:
459 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
460 case TP_ERROR_PERMISSION_DENIED:
461 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
462 case TP_ERROR_NOT_IMPLEMENTED:
463 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
466 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
470 message_send_cb (GObject *source,
471 GAsyncResult *result,
474 EmpathyTpChat *self = user_data;
475 TpTextChannel *channel = (TpTextChannel *) source;
477 GError *error = NULL;
479 if (!tp_text_channel_send_message_finish (channel, result, &token, &error))
481 DEBUG ("Error: %s", error->message);
483 /* FIXME: we should use the body of the message as first argument of the
484 * signal but can't easily get it as we just get a user_data pointer. Once
485 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
486 * to use the user_data pointer to pass the message and fix this. */
487 g_signal_emit (self, signals[SEND_ERROR], 0,
488 NULL, error_to_text_send_error (error), NULL);
490 g_error_free (error);
493 tp_chat_set_delivery_status (self, token,
494 EMPATHY_DELIVERY_STATUS_SENDING);
499 list_pending_messages (EmpathyTpChat *self)
503 messages = tp_text_channel_dup_pending_messages (TP_TEXT_CHANNEL (self));
505 for (l = messages; l != NULL; l = g_list_next (l))
507 TpMessage *message = l->data;
509 handle_incoming_message (self, message, FALSE);
512 g_list_free_full (messages, g_object_unref);
516 update_subject (EmpathyTpChat *self,
517 GHashTable *properties)
519 gboolean can_set, valid;
520 const gchar *subject;
522 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
524 self->priv->can_set_subject = can_set;
526 subject = tp_asv_get_string (properties, "Subject");
531 g_free (self->priv->subject);
532 self->priv->subject = g_strdup (subject);
534 /* If the actor is included with this update, use it;
535 * otherwise, clear it to avoid showing stale information.
536 * Why might it not be included? When you join an IRC channel,
537 * you get a pair of messages: first, the current topic; next,
538 * who set it, and when. Idle reports these in two separate
541 actor = tp_asv_get_string (properties, "Actor");
542 g_free (self->priv->subject_actor);
543 self->priv->subject_actor = g_strdup (actor);
545 g_object_notify (G_OBJECT (self), "subject");
548 /* TODO: track Timestamp. */
552 tp_chat_get_all_subject_cb (TpProxy *proxy,
553 GHashTable *properties,
555 gpointer user_data G_GNUC_UNUSED,
558 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
562 DEBUG ("Error fetching subject: %s", error->message);
566 self->priv->supports_subject = TRUE;
567 update_subject (self, properties);
571 update_title (EmpathyTpChat *self,
572 GHashTable *properties)
574 const gchar *title = tp_asv_get_string (properties, "Title");
578 if (tp_str_empty (title))
581 g_free (self->priv->title);
582 self->priv->title = g_strdup (title);
583 g_object_notify (G_OBJECT (self), "title");
588 tp_chat_get_all_room_config_cb (TpProxy *proxy,
589 GHashTable *properties,
591 gpointer user_data G_GNUC_UNUSED,
594 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
598 DEBUG ("Error fetching room config: %s", error->message);
602 update_title (self, properties);
606 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
607 const gchar *interface_name,
609 const gchar **invalidated,
613 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
615 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT))
616 update_subject (self, changed);
618 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG))
619 update_title (self, changed);
623 empathy_tp_chat_set_subject (EmpathyTpChat *self,
624 const gchar *subject)
626 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
627 subject, tp_chat_async_cb, "while setting subject", NULL,
632 empathy_tp_chat_get_title (EmpathyTpChat *self)
634 return self->priv->title;
638 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
640 return self->priv->supports_subject;
644 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
646 return self->priv->can_set_subject;
650 empathy_tp_chat_get_subject (EmpathyTpChat *self)
652 return self->priv->subject;
656 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
658 return self->priv->subject_actor;
662 tp_chat_dispose (GObject *object)
664 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
666 tp_clear_object (&self->priv->remote_contact);
667 tp_clear_object (&self->priv->user);
669 g_queue_foreach (self->priv->pending_messages_queue,
670 (GFunc) g_object_unref, NULL);
671 g_queue_clear (self->priv->pending_messages_queue);
673 tp_clear_object (&self->priv->ready_result);
675 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
676 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
680 tp_chat_finalize (GObject *object)
682 EmpathyTpChat *self = (EmpathyTpChat *) object;
684 DEBUG ("Finalize: %p", object);
686 g_queue_free (self->priv->pending_messages_queue);
687 g_hash_table_unref (self->priv->messages_being_sent);
689 g_free (self->priv->title);
690 g_free (self->priv->subject);
691 g_free (self->priv->subject_actor);
693 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
697 check_almost_ready (EmpathyTpChat *self)
699 TpChannel *channel = (TpChannel *) self;
701 if (self->priv->ready_result == NULL)
704 if (self->priv->user == NULL)
707 if (self->priv->preparing_password)
710 /* We need either the members (room) or the remote contact (private chat).
711 * If the chat is protected by a password we can't get these information so
712 * consider the chat as ready so it can be presented to the user. */
713 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
714 self->priv->remote_contact == NULL)
717 g_assert (tp_proxy_is_prepared (self,
718 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
720 tp_g_signal_connect_object (self, "message-received",
721 G_CALLBACK (message_received_cb), self, 0);
722 tp_g_signal_connect_object (self, "pending-message-removed",
723 G_CALLBACK (pending_message_removed_cb), self, 0);
725 list_pending_messages (self);
727 tp_g_signal_connect_object (self, "message-sent",
728 G_CALLBACK (message_sent_cb), self, 0);
734 add_members_contact (EmpathyTpChat *self,
739 for (i = 0; i < contacts->len; i++)
741 EmpathyContact *contact;
743 contact = empathy_contact_dup_from_tp_contact (g_ptr_array_index (
746 self->priv->members = g_list_prepend (self->priv->members, contact);
748 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
749 contact, NULL, 0, NULL, TRUE);
752 check_almost_ready (self);
756 remove_member (EmpathyTpChat *self,
757 EmpathyContact *contact)
761 for (l = self->priv->members; l; l = l->next)
763 EmpathyContact *c = l->data;
767 self->priv->members = g_list_delete_link (self->priv->members, l);
775 contact_renamed (EmpathyTpChat *self,
776 TpContact *old_contact,
777 TpContact *new_contact,
778 TpChannelGroupChangeReason reason,
779 const gchar *message)
781 EmpathyContact *old = NULL, *new = NULL;
783 old = empathy_contact_dup_from_tp_contact (old_contact);
784 new = empathy_contact_dup_from_tp_contact (new_contact);
786 self->priv->members = g_list_prepend (self->priv->members, new);
790 remove_member (self, old);
792 g_signal_emit (self, signals[SIG_MEMBER_RENAMED], 0, old, new,
794 g_object_unref (old);
797 if (self->priv->user == old)
799 /* We change our nick */
800 tp_clear_object (&self->priv->user);
801 self->priv->user = g_object_ref (new);
802 g_object_notify (G_OBJECT (self), "self-contact");
805 check_almost_ready (self);
809 tp_chat_group_contacts_changed_cb (TpChannel *channel,
812 GPtrArray *local_pending,
813 GPtrArray *remote_pending,
818 EmpathyContact *actor_contact = NULL;
820 TpChannelGroupChangeReason reason;
821 const gchar *message;
823 reason = tp_asv_get_uint32 (details, "change-reason", NULL);
824 message = tp_asv_get_string (details, "message");
826 /* Contact renamed */
827 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED)
829 /* there can only be a single 'added' and a single 'removed' handle */
830 if (removed->len != 1 || added->len != 1)
832 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
833 added->len, removed->len);
837 contact_renamed (self, g_ptr_array_index (removed, 0),
838 g_ptr_array_index (added, 0), reason, message);
844 actor_contact = empathy_contact_dup_from_tp_contact (actor);
846 if (actor_contact == NULL)
848 /* FIXME: handle this a tad more gracefully: perhaps
849 * the actor was a server op. We could use the
850 * contact-ids detail of MembersChangedDetailed.
852 DEBUG ("actor %s not a channel member",
853 tp_contact_get_identifier (actor));
857 /* Remove contacts that are not members anymore */
858 for (i = 0; i < removed->len; i++)
860 TpContact *tp_contact = g_ptr_array_index (removed, i);
861 EmpathyContact *contact;
863 contact = empathy_contact_dup_from_tp_contact (tp_contact);
867 remove_member (self, contact);
869 g_signal_emit (self, signals[SIG_MEMBERS_CHANGED], 0,
870 contact, actor_contact, reason, message, FALSE);
871 g_object_unref (contact);
877 add_members_contact (self, added);
880 if (actor_contact != NULL)
881 g_object_unref (actor_contact);
885 create_remote_contact (EmpathyTpChat *self,
888 self->priv->remote_contact = empathy_contact_dup_from_tp_contact (contact);
889 g_object_notify (G_OBJECT (self), "remote-contact");
891 check_almost_ready (self);
895 create_self_contact (EmpathyTpChat *self,
898 self->priv->user = empathy_contact_dup_from_tp_contact (contact);
899 empathy_contact_set_is_user (self->priv->user, TRUE);
900 g_object_notify (G_OBJECT (self), "self-contact");
901 check_almost_ready (self);
905 tp_chat_get_property (GObject *object,
910 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
914 case PROP_SELF_CONTACT:
915 g_value_set_object (value, self->priv->user);
917 case PROP_REMOTE_CONTACT:
918 g_value_set_object (value, self->priv->remote_contact);
920 case PROP_N_MESSAGES_SENDING:
921 g_value_set_uint (value,
922 g_hash_table_size (self->priv->messages_being_sent));
925 g_value_set_string (value,
926 empathy_tp_chat_get_title (self));
929 g_value_set_string (value,
930 empathy_tp_chat_get_subject (self));
933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
943 static const TpProxyFeature *
944 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
946 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
947 static GQuark need[2] = {0, 0};
949 if (G_LIKELY (features[0].name != 0))
952 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
953 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
954 features[FEAT_READY].depends_on = need;
955 features[FEAT_READY].prepare_async =
956 tp_chat_prepare_ready_async;
958 /* assert that the terminator at the end is there */
959 g_assert (features[N_FEAT].name == 0);
965 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
967 GObjectClass *object_class = G_OBJECT_CLASS (klass);
968 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
970 object_class->dispose = tp_chat_dispose;
971 object_class->finalize = tp_chat_finalize;
972 object_class->get_property = tp_chat_get_property;
974 proxy_class->list_features = tp_chat_list_features;
977 * EmpathyTpChat:self-contact:
979 * Not to be confused with TpChannel:group-self-contact.
981 g_object_class_install_property (object_class, PROP_SELF_CONTACT,
982 g_param_spec_object ("self-contact", "The local contact",
983 "The EmpathyContact for the local user on this channel",
984 EMPATHY_TYPE_CONTACT,
987 g_object_class_install_property (object_class, PROP_REMOTE_CONTACT,
988 g_param_spec_object ("remote-contact", "The remote contact",
989 "The remote contact if there is no group iface on the channel",
990 EMPATHY_TYPE_CONTACT,
993 g_object_class_install_property (object_class, PROP_N_MESSAGES_SENDING,
994 g_param_spec_uint ("n-messages-sending", "Num Messages Sending",
995 "The number of messages being sent",
999 g_object_class_install_property (object_class, PROP_TITLE,
1000 g_param_spec_string ("title", "Title",
1001 "A human-readable name for the room, if any",
1004 G_PARAM_STATIC_STRINGS));
1006 g_object_class_install_property (object_class, PROP_SUBJECT,
1007 g_param_spec_string ("subject", "Subject",
1008 "The room's current subject, if any",
1011 G_PARAM_STATIC_STRINGS));
1014 signals[MESSAGE_RECEIVED] = g_signal_new ("message-received-empathy",
1015 G_TYPE_FROM_CLASS (klass),
1019 g_cclosure_marshal_generic,
1021 1, EMPATHY_TYPE_MESSAGE);
1023 signals[SEND_ERROR] = g_signal_new ("send-error",
1024 G_TYPE_FROM_CLASS (klass),
1028 g_cclosure_marshal_generic,
1030 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1032 signals[MESSAGE_ACKNOWLEDGED] = g_signal_new ("message-acknowledged",
1033 G_TYPE_FROM_CLASS (klass),
1037 g_cclosure_marshal_generic,
1039 1, EMPATHY_TYPE_MESSAGE);
1041 signals[SIG_MEMBER_RENAMED] = g_signal_new ("member-renamed",
1042 G_OBJECT_CLASS_TYPE (klass),
1044 0, NULL, NULL, NULL,
1046 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1047 G_TYPE_UINT, G_TYPE_STRING);
1049 signals[SIG_MEMBERS_CHANGED] = g_signal_new ("members-changed",
1050 G_OBJECT_CLASS_TYPE (klass),
1052 0, NULL, NULL, NULL,
1054 5, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT,
1055 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_BOOLEAN);
1057 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1061 empathy_tp_chat_init (EmpathyTpChat *self)
1063 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_TP_CHAT,
1064 EmpathyTpChatPrivate);
1066 self->priv->pending_messages_queue = g_queue_new ();
1067 self->priv->messages_being_sent = g_hash_table_new_full (
1068 g_str_hash, g_str_equal, g_free, NULL);
1072 empathy_tp_chat_new (TpSimpleClientFactory *factory,
1074 const gchar *object_path,
1075 const GHashTable *immutable_properties)
1077 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1078 g_return_val_if_fail (immutable_properties != NULL, NULL);
1080 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1083 "dbus-daemon", tp_proxy_get_dbus_daemon (conn),
1084 "bus-name", tp_proxy_get_bus_name (conn),
1085 "object-path", object_path,
1086 "channel-properties", immutable_properties,
1091 empathy_tp_chat_get_id (EmpathyTpChat *self)
1095 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1097 id = tp_channel_get_identifier ((TpChannel *) self);
1098 if (!EMP_STR_EMPTY (id))
1100 else if (self->priv->remote_contact)
1101 return empathy_contact_get_id (self->priv->remote_contact);
1108 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1110 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1112 return self->priv->remote_contact;
1116 empathy_tp_chat_get_account (EmpathyTpChat *self)
1118 TpConnection *connection;
1120 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1122 connection = tp_channel_get_connection (TP_CHANNEL (self));
1124 return tp_connection_get_account (connection);
1128 empathy_tp_chat_send (EmpathyTpChat *self,
1131 gchar *message_body;
1133 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1134 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1136 message_body = tp_message_to_text (message, NULL);
1138 DEBUG ("Sending message: %s", message_body);
1140 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1141 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1142 message_send_cb, self);
1144 g_free (message_body);
1148 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1150 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1152 return self->priv->pending_messages_queue->head;
1156 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1157 EmpathyMessage *message)
1161 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1163 if (!empathy_message_is_incoming (message))
1166 tp_msg = empathy_message_get_tp_message (message);
1167 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1168 tp_msg, NULL, NULL);
1172 * empathy_tp_chat_can_add_contact:
1174 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1175 * That is if this chat is a 1-to-1 channel that can be upgraded to
1176 * a MUC using the Conference interface or if the channel is a MUC.
1179 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1181 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1183 return self->priv->can_upgrade_to_muc ||
1184 tp_proxy_has_interface_by_id (self,
1185 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1189 tp_channel_leave_async_cb (GObject *source_object,
1193 GError *error = NULL;
1195 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error))
1197 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1199 g_error_free (error);
1204 empathy_tp_chat_leave (EmpathyTpChat *self,
1205 const gchar *message)
1207 TpChannel *channel = (TpChannel *) self;
1209 DEBUG ("Leaving channel %s with message \"%s\"",
1210 tp_channel_get_identifier (channel), message);
1212 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1213 message, tp_channel_leave_async_cb, self);
1217 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1218 TpContact **inviter)
1220 TpContact *self_contact;
1221 TpChannel *channel = TP_CHANNEL (self);
1223 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1226 self_contact = tp_channel_group_get_self_contact (channel);
1227 if (self_contact == NULL)
1230 return tp_channel_group_get_local_pending_contact_info (channel,
1231 self_contact, inviter, NULL, NULL);
1235 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1236 EmpathyContact *contact)
1238 return tp_text_channel_get_chat_state ((TpTextChannel *) self,
1239 empathy_contact_get_tp_contact (contact));
1243 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1245 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1247 return self->priv->user;
1251 empathy_tp_chat_get_feature_ready (void)
1253 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1257 password_feature_prepare_cb (GObject *source,
1258 GAsyncResult *result,
1261 EmpathyTpChat *self = user_data;
1262 GError *error = NULL;
1264 if (!tp_proxy_prepare_finish (source, result, &error))
1266 DEBUG ("Failed to prepare Password: %s", error->message);
1267 g_error_free (error);
1270 self->priv->preparing_password = FALSE;
1272 check_almost_ready (self);
1276 tp_chat_prepare_ready_async (TpProxy *proxy,
1277 const TpProxyFeature *feature,
1278 GAsyncReadyCallback callback,
1281 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1282 TpChannel *channel = (TpChannel *) proxy;
1283 TpConnection *connection;
1284 gboolean listen_for_dbus_properties_changed = FALSE;
1286 g_assert (self->priv->ready_result == NULL);
1287 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1288 callback, user_data, tp_chat_prepare_ready_async);
1290 connection = tp_channel_get_connection (channel);
1292 if (tp_proxy_has_interface_by_id (self,
1293 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD))
1295 /* The password feature can't be a hard dep on our own feature has we
1296 * depend on it only if the channel implements the
1297 * Password interface.
1299 GQuark features[] = { TP_CHANNEL_FEATURE_PASSWORD , 0 };
1301 self->priv->preparing_password = TRUE;
1303 tp_proxy_prepare_async (self, features, password_feature_prepare_cb,
1307 if (tp_proxy_has_interface_by_id (self,
1308 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
1310 GPtrArray *contacts;
1313 /* Get self contact from the group's self handle */
1314 contact = tp_channel_group_get_self_contact (channel);
1315 create_self_contact (self, contact);
1317 /* Get initial member contacts */
1318 contacts = tp_channel_group_dup_members_contacts (channel);
1319 add_members_contact (self, contacts);
1320 g_ptr_array_unref (contacts);
1322 self->priv->can_upgrade_to_muc = FALSE;
1324 tp_g_signal_connect_object (self, "group-contacts-changed",
1325 G_CALLBACK (tp_chat_group_contacts_changed_cb), self, 0);
1329 TpCapabilities *caps;
1330 GVariant *classes, *class;
1334 /* Get the self contact from the connection's self handle */
1335 contact = tp_connection_get_self_contact (connection);
1336 create_self_contact (self, contact);
1338 /* Get the remote contact */
1339 contact = tp_channel_get_target_contact (channel);
1340 create_remote_contact (self, contact);
1342 caps = tp_connection_get_capabilities (connection);
1343 g_assert (caps != NULL);
1345 classes = tp_capabilities_dup_channel_classes_variant (caps);
1347 g_variant_iter_init (&iter, classes);
1348 while ((class = g_variant_iter_next_value (&iter)))
1350 GVariant *fixed, *allowed;
1351 const gchar *chan_type = NULL;
1353 fixed = g_variant_get_child_value (class, 0);
1354 allowed = g_variant_get_child_value (class, 1);
1356 g_variant_lookup (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE, "&s",
1358 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1360 const gchar **oprops;
1362 oprops = g_variant_get_strv (allowed, NULL);
1364 if (tp_strv_contains (oprops,
1365 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS))
1367 self->priv->can_upgrade_to_muc = TRUE;
1373 g_variant_unref (class);
1374 g_variant_unref (fixed);
1375 g_variant_unref (allowed);
1377 if (self->priv->can_upgrade_to_muc)
1381 g_variant_unref (classes);
1384 if (tp_proxy_has_interface_by_id (self,
1385 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT))
1387 tp_cli_dbus_properties_call_get_all (channel, -1,
1388 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1389 tp_chat_get_all_subject_cb,
1392 listen_for_dbus_properties_changed = TRUE;
1395 if (tp_proxy_has_interface_by_id (self,
1396 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG))
1398 tp_cli_dbus_properties_call_get_all (channel, -1,
1399 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1400 tp_chat_get_all_room_config_cb,
1403 listen_for_dbus_properties_changed = TRUE;
1406 if (listen_for_dbus_properties_changed)
1408 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1409 tp_chat_dbus_properties_changed_cb,
1411 G_OBJECT (self), NULL);