1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/telepathy-glib.h>
28 #include <extensions/extensions.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-request-util.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 struct _EmpathyTpChatPrivate {
43 EmpathyContact *remote_contact;
45 /* Queue of messages not signalled yet */
46 GQueue *messages_queue;
47 /* Queue of messages signalled but not acked yet */
48 GQueue *pending_messages_queue;
49 gboolean had_properties_list;
50 GPtrArray *properties;
51 gboolean can_upgrade_to_muc;
53 GHashTable *messages_being_sent;
55 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
56 GSimpleAsyncResult *ready_result;
59 static void tp_chat_iface_init (EmpathyContactListIface *iface);
65 PROP_N_MESSAGES_SENDING,
77 static guint signals[LAST_SIGNAL];
79 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
80 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
84 tp_chat_set_delivery_status (EmpathyTpChat *self,
86 EmpathyDeliveryStatus delivery_status)
88 TpDeliveryReportingSupportFlags flags =
89 tp_text_channel_get_delivery_reporting_support (
90 TP_TEXT_CHANNEL (self));
92 /* channel must support receiving failures and successes */
93 if (!tp_str_empty (token) &&
94 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
95 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
97 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
99 switch (delivery_status) {
100 case EMPATHY_DELIVERY_STATUS_NONE:
101 g_hash_table_remove (self->priv->messages_being_sent,
106 g_hash_table_insert (self->priv->messages_being_sent,
108 GUINT_TO_POINTER (delivery_status));
112 g_object_notify (G_OBJECT (self), "n-messages-sending");
116 static void tp_chat_prepare_ready_async (TpProxy *proxy,
117 const TpProxyFeature *feature,
118 GAsyncReadyCallback callback,
122 tp_chat_async_cb (TpChannel *proxy,
125 GObject *weak_object)
128 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
133 create_conference_cb (GObject *source,
134 GAsyncResult *result,
137 GError *error = NULL;
139 if (!tp_account_channel_request_create_channel_finish (
140 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
141 DEBUG ("Failed to create conference channel: %s", error->message);
142 g_error_free (error);
147 tp_chat_add (EmpathyContactList *list,
148 EmpathyContact *contact,
149 const gchar *message)
151 EmpathyTpChat *self = (EmpathyTpChat *) list;
152 TpChannel *channel = (TpChannel *) self;
154 if (tp_proxy_has_interface_by_id (self,
155 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
157 GArray handles = {(gchar *) &handle, 1};
159 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
160 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
162 handle = empathy_contact_get_handle (contact);
163 tp_cli_channel_interface_group_call_add_members (channel,
164 -1, &handles, NULL, NULL, NULL, NULL, NULL);
165 } else if (self->priv->can_upgrade_to_muc) {
166 TpAccountChannelRequest *req;
168 const char *object_path;
169 GPtrArray channels = { (gpointer *) &object_path, 1 };
170 const char *invitees[2] = { NULL, };
172 invitees[0] = empathy_contact_get_id (contact);
173 object_path = tp_proxy_get_object_path (self);
176 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
177 TP_IFACE_CHANNEL_TYPE_TEXT,
178 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
180 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
181 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
182 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
183 G_TYPE_STRV, invitees,
184 /* FIXME: InvitationMessage ? */
187 req = tp_account_channel_request_new (self->priv->account, props,
188 TP_USER_ACTION_TIME_NOT_USER_ACTION);
190 /* Although this is a MUC, it's anonymous, so CreateChannel is
192 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
193 NULL, create_conference_cb, NULL);
195 g_object_unref (req);
196 g_hash_table_unref (props);
198 g_warning ("Cannot add to this channel");
203 tp_chat_remove (EmpathyContactList *list,
204 EmpathyContact *contact,
205 const gchar *message)
207 EmpathyTpChat *self = (EmpathyTpChat *) list;
209 GArray handles = {(gchar *) &handle, 1};
211 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
212 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
214 handle = empathy_contact_get_handle (contact);
215 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
222 tp_chat_get_members (EmpathyContactList *list)
224 EmpathyTpChat *self = (EmpathyTpChat *) list;
225 GList *members = NULL;
227 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
229 if (self->priv->members) {
230 members = g_list_copy (self->priv->members);
231 g_list_foreach (members, (GFunc) g_object_ref, NULL);
233 members = g_list_prepend (members, g_object_ref (self->priv->user));
234 if (self->priv->remote_contact != NULL)
235 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
242 check_ready (EmpathyTpChat *self)
244 if (self->priv->ready_result == NULL)
247 if (g_queue_get_length (self->priv->messages_queue) > 0)
252 g_simple_async_result_complete (self->priv->ready_result);
253 tp_clear_object (&self->priv->ready_result);
257 tp_chat_emit_queued_messages (EmpathyTpChat *self)
259 EmpathyMessage *message;
261 /* Check if we can now emit some queued messages */
262 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
263 if (empathy_message_get_sender (message) == NULL) {
267 DEBUG ("Queued message ready");
268 g_queue_pop_head (self->priv->messages_queue);
269 g_queue_push_tail (self->priv->pending_messages_queue, message);
270 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
277 tp_chat_got_sender_cb (TpConnection *connection,
278 EmpathyContact *contact,
283 EmpathyTpChat *self = (EmpathyTpChat *) chat;
286 DEBUG ("Error: %s", error->message);
287 /* Do not block the message queue, just drop this message */
288 g_queue_remove (self->priv->messages_queue, message);
290 empathy_message_set_sender (message, contact);
293 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
297 tp_chat_build_message (EmpathyTpChat *self,
301 EmpathyMessage *message;
304 message = empathy_message_new_from_tp_message (msg, incoming);
305 /* FIXME: this is actually a lie for incoming messages. */
306 empathy_message_set_receiver (message, self->priv->user);
308 g_queue_push_tail (self->priv->messages_queue, message);
310 sender = tp_signalled_message_get_sender (msg);
311 g_assert (sender != NULL);
313 if (tp_contact_get_handle (sender) == 0) {
314 empathy_message_set_sender (message, self->priv->user);
315 tp_chat_emit_queued_messages (self);
317 TpConnection *connection = tp_channel_borrow_connection (
320 empathy_tp_contact_factory_get_from_handle (connection,
321 tp_contact_get_handle (sender),
322 tp_chat_got_sender_cb,
323 message, NULL, G_OBJECT (self));
328 handle_delivery_report (EmpathyTpChat *self,
331 TpDeliveryStatus delivery_status;
332 const GHashTable *header;
333 TpChannelTextSendError delivery_error;
336 const gchar *message_body = NULL;
337 const gchar *delivery_dbus_error;
338 const gchar *delivery_token = NULL;
340 header = tp_message_peek (message, 0);
344 delivery_token = tp_asv_get_string (header, "delivery-token");
345 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
349 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
350 DEBUG ("Accepted %s", delivery_token);
351 tp_chat_set_delivery_status (self, delivery_token,
352 EMPATHY_DELIVERY_STATUS_ACCEPTED);
354 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
355 DEBUG ("Delivered %s", delivery_token);
356 tp_chat_set_delivery_status (self, delivery_token,
357 EMPATHY_DELIVERY_STATUS_NONE);
359 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
363 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
365 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
367 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
369 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
370 * TpMessage. (fdo #35884) */
371 echo = tp_asv_get_boxed (header, "delivery-echo",
372 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
373 if (echo != NULL && echo->len >= 2) {
374 const GHashTable *echo_body;
376 echo_body = g_ptr_array_index (echo, 1);
377 if (echo_body != NULL)
378 message_body = tp_asv_get_string (echo_body, "content");
381 tp_chat_set_delivery_status (self, delivery_token,
382 EMPATHY_DELIVERY_STATUS_NONE);
383 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
384 delivery_error, delivery_dbus_error);
387 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
388 message, NULL, NULL);
392 handle_incoming_message (EmpathyTpChat *self,
398 if (tp_message_is_delivery_report (message)) {
399 handle_delivery_report (self, message);
403 message_body = tp_message_to_text (message, NULL);
405 DEBUG ("Message %s (channel %s): %s",
406 pending ? "pending" : "received",
407 tp_proxy_get_object_path (self), message_body);
409 if (message_body == NULL) {
410 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
412 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
413 message, NULL, NULL);
417 tp_chat_build_message (self, message, TRUE);
419 g_free (message_body);
423 message_received_cb (TpTextChannel *channel,
427 handle_incoming_message (self, message, FALSE);
431 find_pending_message_func (gconstpointer a,
434 EmpathyMessage *msg = (EmpathyMessage *) a;
435 TpMessage *message = (TpMessage *) b;
437 if (empathy_message_get_tp_message (msg) == message)
444 pending_message_removed_cb (TpTextChannel *channel,
450 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
451 find_pending_message_func);
456 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
458 g_object_unref (m->data);
459 g_queue_delete_link (self->priv->pending_messages_queue, m);
463 message_sent_cb (TpTextChannel *channel,
465 TpMessageSendingFlags flags,
471 message_body = tp_message_to_text (message, NULL);
473 DEBUG ("Message sent: %s", message_body);
475 tp_chat_build_message (self, message, FALSE);
477 g_free (message_body);
480 static TpChannelTextSendError
481 error_to_text_send_error (GError *error)
483 if (error->domain != TP_ERRORS)
484 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
486 switch (error->code) {
487 case TP_ERROR_OFFLINE:
488 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
489 case TP_ERROR_INVALID_HANDLE:
490 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
491 case TP_ERROR_PERMISSION_DENIED:
492 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
493 case TP_ERROR_NOT_IMPLEMENTED:
494 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
497 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
501 message_send_cb (GObject *source,
502 GAsyncResult *result,
505 EmpathyTpChat *self = user_data;
506 TpTextChannel *channel = (TpTextChannel *) source;
508 GError *error = NULL;
510 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
511 DEBUG ("Error: %s", error->message);
513 /* FIXME: we should use the body of the message as first argument of the
514 * signal but can't easily get it as we just get a user_data pointer. Once
515 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
516 * to use the user_data pointer to pass the message and fix this. */
517 g_signal_emit (self, signals[SEND_ERROR], 0,
518 NULL, error_to_text_send_error (error), NULL);
520 g_error_free (error);
523 tp_chat_set_delivery_status (self, token,
524 EMPATHY_DELIVERY_STATUS_SENDING);
530 TpChannelChatState state;
534 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
535 EmpathyContact *contact,
540 TpChannelChatState state;
543 DEBUG ("Error: %s", error->message);
547 state = GPOINTER_TO_UINT (user_data);
548 DEBUG ("Chat state changed for %s (%d): %d",
549 empathy_contact_get_alias (contact),
550 empathy_contact_get_handle (contact), state);
552 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
556 tp_chat_state_changed_cb (TpChannel *channel,
558 TpChannelChatState state,
561 TpConnection *connection = tp_channel_borrow_connection (
564 empathy_tp_contact_factory_get_from_handle (connection, handle,
565 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
566 NULL, G_OBJECT (self));
570 list_pending_messages (EmpathyTpChat *self)
574 messages = tp_text_channel_get_pending_messages (
575 TP_TEXT_CHANNEL (self));
577 for (l = messages; l != NULL; l = g_list_next (l)) {
578 TpMessage *message = l->data;
580 handle_incoming_message (self, message, FALSE);
583 g_list_free (messages);
587 tp_chat_property_flags_changed_cb (TpProxy *proxy,
588 const GPtrArray *properties,
592 EmpathyTpChat *self = (EmpathyTpChat *) chat;
595 if (!self->priv->had_properties_list || !properties) {
599 for (i = 0; i < properties->len; i++) {
600 GValueArray *prop_struct;
601 EmpathyTpChatProperty *property;
605 prop_struct = g_ptr_array_index (properties, i);
606 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
607 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
609 for (j = 0; j < self->priv->properties->len; j++) {
610 property = g_ptr_array_index (self->priv->properties, j);
611 if (property->id == id) {
612 property->flags = flags;
613 DEBUG ("property %s flags changed: %d",
614 property->name, property->flags);
622 tp_chat_properties_changed_cb (TpProxy *proxy,
623 const GPtrArray *properties,
627 EmpathyTpChat *self = (EmpathyTpChat *) chat;
630 if (!self->priv->had_properties_list || !properties) {
634 for (i = 0; i < properties->len; i++) {
635 GValueArray *prop_struct;
636 EmpathyTpChatProperty *property;
640 prop_struct = g_ptr_array_index (properties, i);
641 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
642 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
644 for (j = 0; j < self->priv->properties->len; j++) {
645 property = g_ptr_array_index (self->priv->properties, j);
646 if (property->id == id) {
647 if (property->value) {
648 g_value_copy (src_value, property->value);
650 property->value = tp_g_value_slice_dup (src_value);
653 DEBUG ("property %s changed", property->name);
654 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
655 property->name, property->value);
663 tp_chat_get_properties_cb (TpProxy *proxy,
664 const GPtrArray *properties,
670 DEBUG ("Error getting properties: %s", error->message);
674 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
678 tp_chat_list_properties_cb (TpProxy *proxy,
679 const GPtrArray *properties,
684 EmpathyTpChat *self = (EmpathyTpChat *) chat;
689 DEBUG ("Error listing properties: %s", error->message);
693 self->priv->had_properties_list = TRUE;
695 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
696 self->priv->properties = g_ptr_array_sized_new (properties->len);
697 for (i = 0; i < properties->len; i++) {
698 GValueArray *prop_struct;
699 EmpathyTpChatProperty *property;
701 prop_struct = g_ptr_array_index (properties, i);
702 property = g_slice_new0 (EmpathyTpChatProperty);
703 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
704 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
705 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
707 DEBUG ("Adding property name=%s id=%d flags=%d",
708 property->name, property->id, property->flags);
709 g_ptr_array_add (self->priv->properties, property);
710 if (property->flags & TP_PROPERTY_FLAG_READ) {
711 g_array_append_val (ids, property->id);
715 tp_cli_properties_interface_call_get_properties (proxy, -1,
717 tp_chat_get_properties_cb,
721 g_array_free (ids, TRUE);
725 empathy_tp_chat_set_property (EmpathyTpChat *self,
729 EmpathyTpChatProperty *property;
732 if (!self->priv->had_properties_list) {
736 for (i = 0; i < self->priv->properties->len; i++) {
737 property = g_ptr_array_index (self->priv->properties, i);
738 if (!tp_strdiff (property->name, name)) {
739 GPtrArray *properties;
742 GValue dest_value = {0, };
744 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
748 g_value_init (&id, G_TYPE_UINT);
749 g_value_init (&dest_value, G_TYPE_VALUE);
750 g_value_set_uint (&id, property->id);
751 g_value_set_boxed (&dest_value, value);
753 prop = g_value_array_new (2);
754 g_value_array_append (prop, &id);
755 g_value_array_append (prop, &dest_value);
757 properties = g_ptr_array_sized_new (1);
758 g_ptr_array_add (properties, prop);
760 DEBUG ("Set property %s", name);
761 tp_cli_properties_interface_call_set_properties (self, -1,
763 (tp_cli_properties_interface_callback_for_set_properties)
765 "Seting property", NULL,
768 g_ptr_array_free (properties, TRUE);
769 g_value_array_free (prop);
776 EmpathyTpChatProperty *
777 empathy_tp_chat_get_property (EmpathyTpChat *self,
780 EmpathyTpChatProperty *property;
783 if (!self->priv->had_properties_list) {
787 for (i = 0; i < self->priv->properties->len; i++) {
788 property = g_ptr_array_index (self->priv->properties, i);
789 if (!tp_strdiff (property->name, name)) {
798 empathy_tp_chat_get_properties (EmpathyTpChat *self)
800 return self->priv->properties;
804 tp_chat_dispose (GObject *object)
806 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
808 tp_clear_object (&self->priv->account);
809 tp_clear_object (&self->priv->remote_contact);
810 tp_clear_object (&self->priv->user);
812 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
813 g_queue_clear (self->priv->messages_queue);
815 g_queue_foreach (self->priv->pending_messages_queue,
816 (GFunc) g_object_unref, NULL);
817 g_queue_clear (self->priv->pending_messages_queue);
819 tp_clear_object (&self->priv->ready_result);
821 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
822 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
826 tp_chat_finalize (GObject *object)
828 EmpathyTpChat *self = (EmpathyTpChat *) object;
831 DEBUG ("Finalize: %p", object);
833 if (self->priv->properties) {
834 for (i = 0; i < self->priv->properties->len; i++) {
835 EmpathyTpChatProperty *property;
837 property = g_ptr_array_index (self->priv->properties, i);
838 g_free (property->name);
839 if (property->value) {
840 tp_g_value_slice_free (property->value);
842 g_slice_free (EmpathyTpChatProperty, property);
844 g_ptr_array_free (self->priv->properties, TRUE);
847 g_queue_free (self->priv->messages_queue);
848 g_queue_free (self->priv->pending_messages_queue);
849 g_hash_table_destroy (self->priv->messages_being_sent);
851 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
855 check_almost_ready (EmpathyTpChat *self)
857 TpChannel *channel = (TpChannel *) self;
859 if (self->priv->ready_result == NULL)
862 if (self->priv->user == NULL)
865 /* We need either the members (room) or the remote contact (private chat).
866 * If the chat is protected by a password we can't get these information so
867 * consider the chat as ready so it can be presented to the user. */
868 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
869 self->priv->remote_contact == NULL)
872 g_assert (tp_proxy_is_prepared (self,
873 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
875 tp_g_signal_connect_object (self, "message-received",
876 G_CALLBACK (message_received_cb), self, 0);
877 tp_g_signal_connect_object (self, "pending-message-removed",
878 G_CALLBACK (pending_message_removed_cb), self, 0);
880 list_pending_messages (self);
882 tp_g_signal_connect_object (self, "message-sent",
883 G_CALLBACK (message_sent_cb), self, 0);
885 tp_g_signal_connect_object (self, "chat-state-changed",
886 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
892 tp_chat_got_added_contacts_cb (TpConnection *connection,
894 EmpathyContact * const * contacts,
896 const TpHandle *failed,
901 EmpathyTpChat *self = (EmpathyTpChat *) chat;
903 const TpIntSet *members;
905 EmpathyContact *contact;
908 DEBUG ("Error: %s", error->message);
912 members = tp_channel_group_get_members ((TpChannel *) self);
913 for (i = 0; i < n_contacts; i++) {
914 contact = contacts[i];
915 handle = empathy_contact_get_handle (contact);
917 /* Make sure the contact is still member */
918 if (tp_intset_is_member (members, handle)) {
919 self->priv->members = g_list_prepend (self->priv->members,
920 g_object_ref (contact));
921 g_signal_emit_by_name (chat, "members-changed",
922 contact, NULL, 0, NULL, TRUE);
926 check_almost_ready (EMPATHY_TP_CHAT (chat));
929 static EmpathyContact *
930 chat_lookup_contact (EmpathyTpChat *self,
936 for (l = self->priv->members; l; l = l->next) {
937 EmpathyContact *c = l->data;
939 if (empathy_contact_get_handle (c) != handle) {
944 /* Caller takes the reference. */
945 self->priv->members = g_list_delete_link (self->priv->members, l);
963 static ContactRenameData *
964 contact_rename_data_new (TpHandle handle,
966 const gchar* message)
968 ContactRenameData *data = g_new (ContactRenameData, 1);
969 data->old_handle = handle;
970 data->reason = reason;
971 data->message = g_strdup (message);
977 contact_rename_data_free (ContactRenameData* data)
979 g_free (data->message);
984 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
986 EmpathyContact * const * contacts,
988 const TpHandle *failed,
993 EmpathyTpChat *self = (EmpathyTpChat *) chat;
994 const TpIntSet *members;
996 EmpathyContact *old = NULL, *new = NULL;
997 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1000 DEBUG ("Error: %s", error->message);
1004 /* renamed members can only be delivered one at a time */
1005 g_warn_if_fail (n_contacts == 1);
1009 members = tp_channel_group_get_members ((TpChannel *) self);
1010 handle = empathy_contact_get_handle (new);
1012 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
1014 /* Make sure the contact is still member */
1015 if (tp_intset_is_member (members, handle)) {
1016 self->priv->members = g_list_prepend (self->priv->members,
1017 g_object_ref (new));
1020 g_signal_emit_by_name (self, "member-renamed",
1021 old, new, rename_data->reason,
1022 rename_data->message);
1023 g_object_unref (old);
1027 if (self->priv->user == old) {
1028 /* We change our nick */
1029 tp_clear_object (&self->priv->user);
1030 self->priv->user = g_object_ref (new);
1033 check_almost_ready (self);
1038 tp_chat_group_members_changed_cb (TpChannel *channel,
1042 GArray *local_pending,
1043 GArray *remote_pending,
1046 EmpathyTpChat *self)
1048 EmpathyContact *contact;
1049 EmpathyContact *actor_contact = NULL;
1051 ContactRenameData *rename_data;
1052 TpHandle old_handle;
1053 TpConnection *connection = tp_channel_borrow_connection (
1054 (TpChannel *) self);
1056 /* Contact renamed */
1057 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1058 /* there can only be a single 'added' and a single 'removed' handle */
1059 if (removed->len != 1 || added->len != 1) {
1060 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1061 added->len, removed->len);
1065 old_handle = g_array_index (removed, guint, 0);
1067 rename_data = contact_rename_data_new (old_handle, reason, message);
1068 empathy_tp_contact_factory_get_from_handles (connection,
1069 added->len, (TpHandle *) added->data,
1070 tp_chat_got_renamed_contacts_cb,
1071 rename_data, (GDestroyNotify) contact_rename_data_free,
1077 actor_contact = chat_lookup_contact (self, actor, FALSE);
1078 if (actor_contact == NULL) {
1079 /* FIXME: handle this a tad more gracefully: perhaps
1080 * the actor was a server op. We could use the
1081 * contact-ids detail of MembersChangedDetailed.
1083 DEBUG ("actor %u not a channel member", actor);
1087 /* Remove contacts that are not members anymore */
1088 for (i = 0; i < removed->len; i++) {
1089 contact = chat_lookup_contact (self,
1090 g_array_index (removed, TpHandle, i), TRUE);
1092 if (contact != NULL) {
1093 g_signal_emit_by_name (self, "members-changed", contact,
1094 actor_contact, reason, message,
1096 g_object_unref (contact);
1100 /* Request added contacts */
1101 if (added->len > 0) {
1102 empathy_tp_contact_factory_get_from_handles (connection,
1103 added->len, (TpHandle *) added->data,
1104 tp_chat_got_added_contacts_cb, NULL, NULL,
1108 if (actor_contact != NULL) {
1109 g_object_unref (actor_contact);
1114 tp_chat_got_remote_contact_cb (TpConnection *connection,
1115 EmpathyContact *contact,
1116 const GError *error,
1120 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1123 DEBUG ("Error: %s", error->message);
1124 empathy_tp_chat_leave (self, "");
1128 self->priv->remote_contact = g_object_ref (contact);
1129 g_object_notify (chat, "remote-contact");
1131 check_almost_ready (self);
1135 tp_chat_got_self_contact_cb (TpConnection *connection,
1136 EmpathyContact *contact,
1137 const GError *error,
1141 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1144 DEBUG ("Error: %s", error->message);
1145 empathy_tp_chat_leave (self, "");
1149 self->priv->user = g_object_ref (contact);
1150 empathy_contact_set_is_user (self->priv->user, TRUE);
1151 check_almost_ready (self);
1155 tp_chat_get_property (GObject *object,
1160 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1164 g_value_set_object (value, self->priv->account);
1166 case PROP_REMOTE_CONTACT:
1167 g_value_set_object (value, self->priv->remote_contact);
1169 case PROP_N_MESSAGES_SENDING:
1170 g_value_set_uint (value,
1171 g_hash_table_size (self->priv->messages_being_sent));
1174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1180 tp_chat_set_property (GObject *object,
1182 const GValue *value,
1185 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1189 self->priv->account = g_value_dup_object (value);
1192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1202 static const TpProxyFeature *
1203 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1205 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1206 static GQuark need[2] = {0, 0};
1208 if (G_LIKELY (features[0].name != 0))
1211 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1212 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1213 features[FEAT_READY].depends_on = need;
1214 features[FEAT_READY].prepare_async =
1215 tp_chat_prepare_ready_async;
1217 /* assert that the terminator at the end is there */
1218 g_assert (features[N_FEAT].name == 0);
1224 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1226 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1227 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1229 object_class->dispose = tp_chat_dispose;
1230 object_class->finalize = tp_chat_finalize;
1231 object_class->get_property = tp_chat_get_property;
1232 object_class->set_property = tp_chat_set_property;
1234 proxy_class->list_features = tp_chat_list_features;
1236 g_object_class_install_property (object_class,
1238 g_param_spec_object ("account",
1240 "the account associated with the chat",
1243 G_PARAM_CONSTRUCT_ONLY |
1244 G_PARAM_STATIC_STRINGS));
1246 g_object_class_install_property (object_class,
1247 PROP_REMOTE_CONTACT,
1248 g_param_spec_object ("remote-contact",
1249 "The remote contact",
1250 "The remote contact if there is no group iface on the channel",
1251 EMPATHY_TYPE_CONTACT,
1254 g_object_class_install_property (object_class,
1255 PROP_N_MESSAGES_SENDING,
1256 g_param_spec_uint ("n-messages-sending",
1257 "Num Messages Sending",
1258 "The number of messages being sent",
1263 signals[MESSAGE_RECEIVED] =
1264 g_signal_new ("message-received-empathy",
1265 G_TYPE_FROM_CLASS (klass),
1269 g_cclosure_marshal_generic,
1271 1, EMPATHY_TYPE_MESSAGE);
1273 signals[SEND_ERROR] =
1274 g_signal_new ("send-error",
1275 G_TYPE_FROM_CLASS (klass),
1279 g_cclosure_marshal_generic,
1281 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1283 signals[CHAT_STATE_CHANGED] =
1284 g_signal_new ("chat-state-changed-empathy",
1285 G_TYPE_FROM_CLASS (klass),
1289 g_cclosure_marshal_generic,
1291 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1293 signals[PROPERTY_CHANGED] =
1294 g_signal_new ("property-changed",
1295 G_TYPE_FROM_CLASS (klass),
1299 g_cclosure_marshal_generic,
1301 2, G_TYPE_STRING, G_TYPE_VALUE);
1303 signals[MESSAGE_ACKNOWLEDGED] =
1304 g_signal_new ("message-acknowledged",
1305 G_TYPE_FROM_CLASS (klass),
1309 g_cclosure_marshal_generic,
1311 1, EMPATHY_TYPE_MESSAGE);
1313 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1317 empathy_tp_chat_init (EmpathyTpChat *self)
1319 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1320 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1322 self->priv->messages_queue = g_queue_new ();
1323 self->priv->pending_messages_queue = g_queue_new ();
1324 self->priv->messages_being_sent = g_hash_table_new_full (
1325 g_str_hash, g_str_equal, g_free, NULL);
1329 tp_chat_iface_init (EmpathyContactListIface *iface)
1331 iface->add = tp_chat_add;
1332 iface->remove = tp_chat_remove;
1333 iface->get_members = tp_chat_get_members;
1337 empathy_tp_chat_new (
1338 TpSimpleClientFactory *factory,
1341 const gchar *object_path,
1342 const GHashTable *immutable_properties)
1344 TpProxy *conn_proxy = (TpProxy *) conn;
1346 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1347 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1348 g_return_val_if_fail (immutable_properties != NULL, NULL);
1350 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1354 "dbus-daemon", conn_proxy->dbus_daemon,
1355 "bus-name", conn_proxy->bus_name,
1356 "object-path", object_path,
1357 "channel-properties", immutable_properties,
1362 empathy_tp_chat_get_id (EmpathyTpChat *self)
1366 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1368 id = tp_channel_get_identifier ((TpChannel *) self);
1369 if (!EMP_STR_EMPTY (id))
1371 else if (self->priv->remote_contact)
1372 return empathy_contact_get_id (self->priv->remote_contact);
1379 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1381 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1383 return self->priv->remote_contact;
1387 empathy_tp_chat_get_account (EmpathyTpChat *self)
1389 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1391 return self->priv->account;
1395 empathy_tp_chat_send (EmpathyTpChat *self,
1398 gchar *message_body;
1400 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1401 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1403 message_body = tp_message_to_text (message, NULL);
1405 DEBUG ("Sending message: %s", message_body);
1407 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1408 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1409 message_send_cb, self);
1411 g_free (message_body);
1415 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1417 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1419 return self->priv->pending_messages_queue->head;
1423 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1424 EmpathyMessage *message) {
1427 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1429 if (!empathy_message_is_incoming (message))
1432 tp_msg = empathy_message_get_tp_message (message);
1433 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1434 tp_msg, NULL, NULL);
1438 * empathy_tp_chat_can_add_contact:
1440 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1441 * That is if this chat is a 1-to-1 channel that can be upgraded to
1442 * a MUC using the Conference interface or if the channel is a MUC.
1445 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1447 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1449 return self->priv->can_upgrade_to_muc ||
1450 tp_proxy_has_interface_by_id (self,
1451 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1455 tp_channel_leave_async_cb (GObject *source_object,
1459 GError *error = NULL;
1461 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1462 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1464 g_error_free (error);
1469 empathy_tp_chat_leave (EmpathyTpChat *self,
1470 const gchar *message)
1472 TpChannel *channel = (TpChannel *) self;
1474 DEBUG ("Leaving channel %s with message \"%s\"",
1475 tp_channel_get_identifier (channel), message);
1477 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1478 message, tp_channel_leave_async_cb, self);
1482 add_members_cb (TpChannel *proxy,
1483 const GError *error,
1485 GObject *weak_object)
1487 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1489 if (error != NULL) {
1490 DEBUG ("Failed to join chat (%s): %s",
1491 tp_channel_get_identifier ((TpChannel *) self), error->message);
1496 empathy_tp_chat_join (EmpathyTpChat *self)
1498 TpHandle self_handle;
1501 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1503 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1504 g_array_append_val (members, self_handle);
1506 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1507 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1509 g_array_free (members, TRUE);
1513 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1516 TpHandle self_handle;
1518 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1521 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1522 if (self_handle == 0)
1525 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1526 inviter, NULL, NULL);
1530 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1531 EmpathyContact *contact)
1533 return tp_channel_get_chat_state ((TpChannel *) self,
1534 empathy_contact_get_handle (contact));
1538 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1540 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1542 return self->priv->user;
1546 empathy_tp_chat_get_feature_ready (void)
1548 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1552 tp_chat_prepare_ready_async (TpProxy *proxy,
1553 const TpProxyFeature *feature,
1554 GAsyncReadyCallback callback,
1557 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1558 TpChannel *channel = (TpChannel *) proxy;
1559 TpConnection *connection;
1561 g_assert (self->priv->ready_result == NULL);
1562 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1563 callback, user_data, tp_chat_prepare_ready_async);
1565 connection = tp_channel_borrow_connection (channel);
1567 if (tp_proxy_has_interface_by_id (self,
1568 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1569 const TpIntSet *members;
1573 /* Get self contact from the group's self handle */
1574 handle = tp_channel_group_get_self_handle (channel);
1575 empathy_tp_contact_factory_get_from_handle (connection,
1576 handle, tp_chat_got_self_contact_cb,
1577 NULL, NULL, G_OBJECT (self));
1579 /* Get initial member contacts */
1580 members = tp_channel_group_get_members (channel);
1581 handles = tp_intset_to_array (members);
1582 empathy_tp_contact_factory_get_from_handles (connection,
1583 handles->len, (TpHandle *) handles->data,
1584 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1586 self->priv->can_upgrade_to_muc = FALSE;
1588 tp_g_signal_connect_object (self, "group-members-changed",
1589 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1591 TpCapabilities *caps;
1596 /* Get the self contact from the connection's self handle */
1597 handle = tp_connection_get_self_handle (connection);
1598 empathy_tp_contact_factory_get_from_handle (connection,
1599 handle, tp_chat_got_self_contact_cb,
1600 NULL, NULL, G_OBJECT (self));
1602 /* Get the remote contact */
1603 handle = tp_channel_get_handle (channel, NULL);
1604 empathy_tp_contact_factory_get_from_handle (connection,
1605 handle, tp_chat_got_remote_contact_cb,
1606 NULL, NULL, G_OBJECT (self));
1608 caps = tp_connection_get_capabilities (connection);
1609 g_assert (caps != NULL);
1611 classes = tp_capabilities_get_channel_classes (caps);
1613 for (i = 0; i < classes->len; i++) {
1614 GValueArray *array = g_ptr_array_index (classes, i);
1615 const char **oprops = g_value_get_boxed (
1616 g_value_array_get_nth (array, 1));
1618 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1619 self->priv->can_upgrade_to_muc = TRUE;
1625 if (tp_proxy_has_interface_by_id (self,
1626 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1627 tp_cli_properties_interface_call_list_properties (channel, -1,
1628 tp_chat_list_properties_cb,
1631 tp_cli_properties_interface_connect_to_properties_changed (channel,
1632 tp_chat_properties_changed_cb,
1634 G_OBJECT (self), NULL);
1635 tp_cli_properties_interface_connect_to_property_flags_changed (channel,
1636 tp_chat_property_flags_changed_cb,
1638 G_OBJECT (self), NULL);