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-marshal.h"
34 #include "empathy-request-util.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
41 struct _EmpathyTpChatPrivate {
44 EmpathyContact *remote_contact;
46 /* Queue of messages not signalled yet */
47 GQueue *messages_queue;
48 /* Queue of messages signalled but not acked yet */
49 GQueue *pending_messages_queue;
50 gboolean had_properties_list;
51 GPtrArray *properties;
52 TpChannelPasswordFlags password_flags;
53 /* TRUE if we fetched the password flag of the channel or if it's not needed
54 * (channel doesn't implement the Password interface) */
55 gboolean got_password_flags;
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;
64 static void tp_chat_iface_init (EmpathyContactListIface *iface);
71 PROP_N_MESSAGES_SENDING,
83 static guint signals[LAST_SIGNAL];
85 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
86 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
90 tp_chat_set_delivery_status (EmpathyTpChat *self,
92 EmpathyDeliveryStatus delivery_status)
94 TpDeliveryReportingSupportFlags flags =
95 tp_text_channel_get_delivery_reporting_support (
96 TP_TEXT_CHANNEL (self));
98 /* channel must support receiving failures and successes */
99 if (!tp_str_empty (token) &&
100 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
101 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
103 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
105 switch (delivery_status) {
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);
139 create_conference_cb (GObject *source,
140 GAsyncResult *result,
143 GError *error = NULL;
145 if (!tp_account_channel_request_create_channel_finish (
146 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
147 DEBUG ("Failed to create conference channel: %s", error->message);
148 g_error_free (error);
153 tp_chat_add (EmpathyContactList *list,
154 EmpathyContact *contact,
155 const gchar *message)
157 EmpathyTpChat *self = (EmpathyTpChat *) list;
158 TpChannel *channel = (TpChannel *) self;
160 if (tp_proxy_has_interface_by_id (self,
161 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
163 GArray handles = {(gchar *) &handle, 1};
165 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
166 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
168 handle = empathy_contact_get_handle (contact);
169 tp_cli_channel_interface_group_call_add_members (channel,
170 -1, &handles, NULL, NULL, NULL, NULL, NULL);
171 } else if (self->priv->can_upgrade_to_muc) {
172 TpAccountChannelRequest *req;
174 const char *object_path;
175 GPtrArray channels = { (gpointer *) &object_path, 1 };
176 const char *invitees[2] = { NULL, };
178 invitees[0] = empathy_contact_get_id (contact);
179 object_path = tp_proxy_get_object_path (self);
182 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
183 TP_IFACE_CHANNEL_TYPE_TEXT,
184 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
186 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
187 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
188 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
189 G_TYPE_STRV, invitees,
190 /* FIXME: InvitationMessage ? */
193 req = tp_account_channel_request_new (self->priv->account, props,
194 TP_USER_ACTION_TIME_NOT_USER_ACTION);
196 /* Although this is a MUC, it's anonymous, so CreateChannel is
198 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
199 NULL, create_conference_cb, NULL);
201 g_object_unref (req);
202 g_hash_table_unref (props);
204 g_warning ("Cannot add to this channel");
209 tp_chat_remove (EmpathyContactList *list,
210 EmpathyContact *contact,
211 const gchar *message)
213 EmpathyTpChat *self = (EmpathyTpChat *) list;
215 GArray handles = {(gchar *) &handle, 1};
217 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
218 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
220 handle = empathy_contact_get_handle (contact);
221 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
228 tp_chat_get_members (EmpathyContactList *list)
230 EmpathyTpChat *self = (EmpathyTpChat *) list;
231 GList *members = NULL;
233 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
235 if (self->priv->members) {
236 members = g_list_copy (self->priv->members);
237 g_list_foreach (members, (GFunc) g_object_ref, NULL);
239 members = g_list_prepend (members, g_object_ref (self->priv->user));
240 if (self->priv->remote_contact != NULL)
241 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
248 check_ready (EmpathyTpChat *self)
250 if (self->priv->ready_result == NULL)
253 if (g_queue_get_length (self->priv->messages_queue) > 0)
258 g_simple_async_result_complete (self->priv->ready_result);
259 tp_clear_object (&self->priv->ready_result);
263 tp_chat_emit_queued_messages (EmpathyTpChat *self)
265 EmpathyMessage *message;
267 /* Check if we can now emit some queued messages */
268 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
269 if (empathy_message_get_sender (message) == NULL) {
273 DEBUG ("Queued message ready");
274 g_queue_pop_head (self->priv->messages_queue);
275 g_queue_push_tail (self->priv->pending_messages_queue, message);
276 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
283 tp_chat_got_sender_cb (TpConnection *connection,
284 EmpathyContact *contact,
289 EmpathyTpChat *self = (EmpathyTpChat *) chat;
292 DEBUG ("Error: %s", error->message);
293 /* Do not block the message queue, just drop this message */
294 g_queue_remove (self->priv->messages_queue, message);
296 empathy_message_set_sender (message, contact);
299 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
303 tp_chat_build_message (EmpathyTpChat *self,
307 EmpathyMessage *message;
310 message = empathy_message_new_from_tp_message (msg, incoming);
311 /* FIXME: this is actually a lie for incoming messages. */
312 empathy_message_set_receiver (message, self->priv->user);
314 g_queue_push_tail (self->priv->messages_queue, message);
316 sender = tp_signalled_message_get_sender (msg);
317 g_assert (sender != NULL);
319 if (tp_contact_get_handle (sender) == 0) {
320 empathy_message_set_sender (message, self->priv->user);
321 tp_chat_emit_queued_messages (self);
323 TpConnection *connection = tp_channel_borrow_connection (
326 empathy_tp_contact_factory_get_from_handle (connection,
327 tp_contact_get_handle (sender),
328 tp_chat_got_sender_cb,
329 message, NULL, G_OBJECT (self));
334 handle_delivery_report (EmpathyTpChat *self,
337 TpDeliveryStatus delivery_status;
338 const GHashTable *header;
339 TpChannelTextSendError delivery_error;
342 const gchar *message_body = NULL;
343 const gchar *delivery_dbus_error;
344 const gchar *delivery_token = NULL;
346 header = tp_message_peek (message, 0);
350 delivery_token = tp_asv_get_string (header, "delivery-token");
351 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
355 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
356 DEBUG ("Accepted %s", delivery_token);
357 tp_chat_set_delivery_status (self, delivery_token,
358 EMPATHY_DELIVERY_STATUS_ACCEPTED);
360 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
361 DEBUG ("Delivered %s", delivery_token);
362 tp_chat_set_delivery_status (self, delivery_token,
363 EMPATHY_DELIVERY_STATUS_NONE);
365 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
369 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
371 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
373 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
375 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
376 * TpMessage. (fdo #35884) */
377 echo = tp_asv_get_boxed (header, "delivery-echo",
378 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
379 if (echo != NULL && echo->len >= 2) {
380 const GHashTable *echo_body;
382 echo_body = g_ptr_array_index (echo, 1);
383 if (echo_body != NULL)
384 message_body = tp_asv_get_string (echo_body, "content");
387 tp_chat_set_delivery_status (self, delivery_token,
388 EMPATHY_DELIVERY_STATUS_NONE);
389 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
390 delivery_error, delivery_dbus_error);
393 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
394 message, NULL, NULL);
398 handle_incoming_message (EmpathyTpChat *self,
404 if (tp_message_is_delivery_report (message)) {
405 handle_delivery_report (self, message);
409 message_body = tp_message_to_text (message, NULL);
411 DEBUG ("Message %s (channel %s): %s",
412 pending ? "pending" : "received",
413 tp_proxy_get_object_path (self), message_body);
415 if (message_body == NULL) {
416 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
418 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
419 message, NULL, NULL);
423 tp_chat_build_message (self, message, TRUE);
425 g_free (message_body);
429 message_received_cb (TpTextChannel *channel,
433 handle_incoming_message (self, message, FALSE);
437 find_pending_message_func (gconstpointer a,
440 EmpathyMessage *msg = (EmpathyMessage *) a;
441 TpMessage *message = (TpMessage *) b;
443 if (empathy_message_get_tp_message (msg) == message)
450 pending_message_removed_cb (TpTextChannel *channel,
456 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
457 find_pending_message_func);
462 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
464 g_object_unref (m->data);
465 g_queue_delete_link (self->priv->pending_messages_queue, m);
469 message_sent_cb (TpTextChannel *channel,
471 TpMessageSendingFlags flags,
477 message_body = tp_message_to_text (message, NULL);
479 DEBUG ("Message sent: %s", message_body);
481 tp_chat_build_message (self, message, FALSE);
483 g_free (message_body);
486 static TpChannelTextSendError
487 error_to_text_send_error (GError *error)
489 if (error->domain != TP_ERRORS)
490 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
492 switch (error->code) {
493 case TP_ERROR_OFFLINE:
494 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
495 case TP_ERROR_INVALID_HANDLE:
496 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
497 case TP_ERROR_PERMISSION_DENIED:
498 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
499 case TP_ERROR_NOT_IMPLEMENTED:
500 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
503 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
507 message_send_cb (GObject *source,
508 GAsyncResult *result,
511 EmpathyTpChat *self = user_data;
512 TpTextChannel *channel = (TpTextChannel *) source;
514 GError *error = NULL;
516 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
517 DEBUG ("Error: %s", error->message);
519 /* FIXME: we should use the body of the message as first argument of the
520 * signal but can't easily get it as we just get a user_data pointer. Once
521 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
522 * to use the user_data pointer to pass the message and fix this. */
523 g_signal_emit (self, signals[SEND_ERROR], 0,
524 NULL, error_to_text_send_error (error), NULL);
526 g_error_free (error);
529 tp_chat_set_delivery_status (self, token,
530 EMPATHY_DELIVERY_STATUS_SENDING);
536 TpChannelChatState state;
540 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
541 EmpathyContact *contact,
546 TpChannelChatState state;
549 DEBUG ("Error: %s", error->message);
553 state = GPOINTER_TO_UINT (user_data);
554 DEBUG ("Chat state changed for %s (%d): %d",
555 empathy_contact_get_alias (contact),
556 empathy_contact_get_handle (contact), state);
558 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
562 tp_chat_state_changed_cb (TpChannel *channel,
564 TpChannelChatState state,
567 TpConnection *connection = tp_channel_borrow_connection (
570 empathy_tp_contact_factory_get_from_handle (connection, handle,
571 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
572 NULL, G_OBJECT (self));
576 list_pending_messages (EmpathyTpChat *self)
580 messages = tp_text_channel_get_pending_messages (
581 TP_TEXT_CHANNEL (self));
583 for (l = messages; l != NULL; l = g_list_next (l)) {
584 TpMessage *message = l->data;
586 handle_incoming_message (self, message, FALSE);
589 g_list_free (messages);
593 tp_chat_property_flags_changed_cb (TpProxy *proxy,
594 const GPtrArray *properties,
598 EmpathyTpChat *self = (EmpathyTpChat *) chat;
601 if (!self->priv->had_properties_list || !properties) {
605 for (i = 0; i < properties->len; i++) {
606 GValueArray *prop_struct;
607 EmpathyTpChatProperty *property;
611 prop_struct = g_ptr_array_index (properties, i);
612 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
613 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
615 for (j = 0; j < self->priv->properties->len; j++) {
616 property = g_ptr_array_index (self->priv->properties, j);
617 if (property->id == id) {
618 property->flags = flags;
619 DEBUG ("property %s flags changed: %d",
620 property->name, property->flags);
628 tp_chat_properties_changed_cb (TpProxy *proxy,
629 const GPtrArray *properties,
633 EmpathyTpChat *self = (EmpathyTpChat *) chat;
636 if (!self->priv->had_properties_list || !properties) {
640 for (i = 0; i < properties->len; i++) {
641 GValueArray *prop_struct;
642 EmpathyTpChatProperty *property;
646 prop_struct = g_ptr_array_index (properties, i);
647 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
648 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
650 for (j = 0; j < self->priv->properties->len; j++) {
651 property = g_ptr_array_index (self->priv->properties, j);
652 if (property->id == id) {
653 if (property->value) {
654 g_value_copy (src_value, property->value);
656 property->value = tp_g_value_slice_dup (src_value);
659 DEBUG ("property %s changed", property->name);
660 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
661 property->name, property->value);
669 tp_chat_get_properties_cb (TpProxy *proxy,
670 const GPtrArray *properties,
676 DEBUG ("Error getting properties: %s", error->message);
680 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
684 tp_chat_list_properties_cb (TpProxy *proxy,
685 const GPtrArray *properties,
690 EmpathyTpChat *self = (EmpathyTpChat *) chat;
694 self->priv->had_properties_list = TRUE;
697 DEBUG ("Error listing properties: %s", error->message);
701 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
702 self->priv->properties = g_ptr_array_sized_new (properties->len);
703 for (i = 0; i < properties->len; i++) {
704 GValueArray *prop_struct;
705 EmpathyTpChatProperty *property;
707 prop_struct = g_ptr_array_index (properties, i);
708 property = g_slice_new0 (EmpathyTpChatProperty);
709 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
710 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
711 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
713 DEBUG ("Adding property name=%s id=%d flags=%d",
714 property->name, property->id, property->flags);
715 g_ptr_array_add (self->priv->properties, property);
716 if (property->flags & TP_PROPERTY_FLAG_READ) {
717 g_array_append_val (ids, property->id);
721 tp_cli_properties_interface_call_get_properties (proxy, -1,
723 tp_chat_get_properties_cb,
727 g_array_free (ids, TRUE);
731 empathy_tp_chat_set_property (EmpathyTpChat *self,
735 EmpathyTpChatProperty *property;
738 if (!self->priv->had_properties_list) {
742 for (i = 0; i < self->priv->properties->len; i++) {
743 property = g_ptr_array_index (self->priv->properties, i);
744 if (!tp_strdiff (property->name, name)) {
745 GPtrArray *properties;
748 GValue dest_value = {0, };
750 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
754 g_value_init (&id, G_TYPE_UINT);
755 g_value_init (&dest_value, G_TYPE_VALUE);
756 g_value_set_uint (&id, property->id);
757 g_value_set_boxed (&dest_value, value);
759 prop = g_value_array_new (2);
760 g_value_array_append (prop, &id);
761 g_value_array_append (prop, &dest_value);
763 properties = g_ptr_array_sized_new (1);
764 g_ptr_array_add (properties, prop);
766 DEBUG ("Set property %s", name);
767 tp_cli_properties_interface_call_set_properties (self, -1,
769 (tp_cli_properties_interface_callback_for_set_properties)
771 "Seting property", NULL,
774 g_ptr_array_free (properties, TRUE);
775 g_value_array_free (prop);
782 EmpathyTpChatProperty *
783 empathy_tp_chat_get_property (EmpathyTpChat *self,
786 EmpathyTpChatProperty *property;
789 if (!self->priv->had_properties_list) {
793 for (i = 0; i < self->priv->properties->len; i++) {
794 property = g_ptr_array_index (self->priv->properties, i);
795 if (!tp_strdiff (property->name, name)) {
804 empathy_tp_chat_get_properties (EmpathyTpChat *self)
806 return self->priv->properties;
810 tp_chat_dispose (GObject *object)
812 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
814 tp_clear_object (&self->priv->account);
815 tp_clear_object (&self->priv->remote_contact);
816 tp_clear_object (&self->priv->user);
818 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
819 g_queue_clear (self->priv->messages_queue);
821 g_queue_foreach (self->priv->pending_messages_queue,
822 (GFunc) g_object_unref, NULL);
823 g_queue_clear (self->priv->pending_messages_queue);
825 tp_clear_object (&self->priv->ready_result);
827 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
828 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
832 tp_chat_finalize (GObject *object)
834 EmpathyTpChat *self = (EmpathyTpChat *) object;
837 DEBUG ("Finalize: %p", object);
839 if (self->priv->properties) {
840 for (i = 0; i < self->priv->properties->len; i++) {
841 EmpathyTpChatProperty *property;
843 property = g_ptr_array_index (self->priv->properties, i);
844 g_free (property->name);
845 if (property->value) {
846 tp_g_value_slice_free (property->value);
848 g_slice_free (EmpathyTpChatProperty, property);
850 g_ptr_array_free (self->priv->properties, TRUE);
853 g_queue_free (self->priv->messages_queue);
854 g_queue_free (self->priv->pending_messages_queue);
855 g_hash_table_destroy (self->priv->messages_being_sent);
857 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
861 check_almost_ready (EmpathyTpChat *self)
863 if (self->priv->ready_result == NULL)
866 if (self->priv->user == NULL)
869 if (!self->priv->got_password_flags)
872 /* We need either the members (room) or the remote contact (private chat).
873 * If the chat is protected by a password we can't get these information so
874 * consider the chat as ready so it can be presented to the user. */
875 if (!empathy_tp_chat_password_needed (self) && self->priv->members == NULL &&
876 self->priv->remote_contact == NULL)
879 g_assert (tp_proxy_is_prepared (self,
880 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
882 tp_g_signal_connect_object (self, "message-received",
883 G_CALLBACK (message_received_cb), self, 0);
884 tp_g_signal_connect_object (self, "pending-message-removed",
885 G_CALLBACK (pending_message_removed_cb), self, 0);
887 list_pending_messages (self);
889 tp_g_signal_connect_object (self, "message-sent",
890 G_CALLBACK (message_sent_cb), self, 0);
892 tp_g_signal_connect_object (self, "chat-state-changed",
893 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
899 tp_chat_update_remote_contact (EmpathyTpChat *self)
901 TpChannel *channel = (TpChannel *) self;
902 EmpathyContact *contact = NULL;
903 TpHandle self_handle;
904 TpHandleType handle_type;
907 /* If this is a named chatroom, never pretend it is a private chat */
908 tp_channel_get_handle (channel, &handle_type);
909 if (handle_type == TP_HANDLE_TYPE_ROOM) {
913 /* This is an MSN chat, but it's the new style where 1-1 chats don't
914 * have the group interface. If it has the conference interface, then
915 * it is indeed a MUC. */
916 if (tp_proxy_has_interface_by_id (self,
917 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
921 /* This is an MSN-like chat where anyone can join the chat at anytime.
922 * If there is only one non-self contact member, we are in a private
923 * chat and we set the "remote-contact" property to that contact. If
924 * there are more, set the "remote-contact" property to NULL and the
925 * UI will display a contact list. */
926 self_handle = tp_channel_group_get_self_handle (channel);
927 for (l = self->priv->members; l; l = l->next) {
928 /* Skip self contact if member */
929 if (empathy_contact_get_handle (l->data) == self_handle) {
933 /* We have more than one remote contact, break */
934 if (contact != NULL) {
939 /* If we didn't find yet a remote contact, keep this one */
943 if (self->priv->remote_contact == contact) {
947 DEBUG ("Changing remote contact from %p to %p",
948 self->priv->remote_contact, contact);
950 if (self->priv->remote_contact) {
951 g_object_unref (self->priv->remote_contact);
954 self->priv->remote_contact = contact ? g_object_ref (contact) : NULL;
955 g_object_notify (G_OBJECT (self), "remote-contact");
959 tp_chat_got_added_contacts_cb (TpConnection *connection,
961 EmpathyContact * const * contacts,
963 const TpHandle *failed,
968 EmpathyTpChat *self = (EmpathyTpChat *) chat;
970 const TpIntSet *members;
972 EmpathyContact *contact;
975 DEBUG ("Error: %s", error->message);
979 members = tp_channel_group_get_members ((TpChannel *) self);
980 for (i = 0; i < n_contacts; i++) {
981 contact = contacts[i];
982 handle = empathy_contact_get_handle (contact);
984 /* Make sure the contact is still member */
985 if (tp_intset_is_member (members, handle)) {
986 self->priv->members = g_list_prepend (self->priv->members,
987 g_object_ref (contact));
988 g_signal_emit_by_name (chat, "members-changed",
989 contact, NULL, 0, NULL, TRUE);
993 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
994 check_almost_ready (EMPATHY_TP_CHAT (chat));
997 static EmpathyContact *
998 chat_lookup_contact (EmpathyTpChat *self,
1004 for (l = self->priv->members; l; l = l->next) {
1005 EmpathyContact *c = l->data;
1007 if (empathy_contact_get_handle (c) != handle) {
1012 /* Caller takes the reference. */
1013 self->priv->members = g_list_delete_link (self->priv->members, l);
1026 TpHandle old_handle;
1029 } ContactRenameData;
1031 static ContactRenameData *
1032 contact_rename_data_new (TpHandle handle,
1034 const gchar* message)
1036 ContactRenameData *data = g_new (ContactRenameData, 1);
1037 data->old_handle = handle;
1038 data->reason = reason;
1039 data->message = g_strdup (message);
1045 contact_rename_data_free (ContactRenameData* data)
1047 g_free (data->message);
1052 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1054 EmpathyContact * const * contacts,
1056 const TpHandle *failed,
1057 const GError *error,
1061 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1062 const TpIntSet *members;
1064 EmpathyContact *old = NULL, *new = NULL;
1065 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1068 DEBUG ("Error: %s", error->message);
1072 /* renamed members can only be delivered one at a time */
1073 g_warn_if_fail (n_contacts == 1);
1077 members = tp_channel_group_get_members ((TpChannel *) self);
1078 handle = empathy_contact_get_handle (new);
1080 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
1082 /* Make sure the contact is still member */
1083 if (tp_intset_is_member (members, handle)) {
1084 self->priv->members = g_list_prepend (self->priv->members,
1085 g_object_ref (new));
1088 g_signal_emit_by_name (self, "member-renamed",
1089 old, new, rename_data->reason,
1090 rename_data->message);
1091 g_object_unref (old);
1095 if (self->priv->user == old) {
1096 /* We change our nick */
1097 tp_clear_object (&self->priv->user);
1098 self->priv->user = g_object_ref (new);
1101 tp_chat_update_remote_contact (self);
1102 check_almost_ready (self);
1107 tp_chat_group_members_changed_cb (TpChannel *channel,
1111 GArray *local_pending,
1112 GArray *remote_pending,
1115 EmpathyTpChat *self)
1117 EmpathyContact *contact;
1118 EmpathyContact *actor_contact = NULL;
1120 ContactRenameData *rename_data;
1121 TpHandle old_handle;
1122 TpConnection *connection = tp_channel_borrow_connection (
1123 (TpChannel *) self);
1125 /* Contact renamed */
1126 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1127 /* there can only be a single 'added' and a single 'removed' handle */
1128 if (removed->len != 1 || added->len != 1) {
1129 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1130 added->len, removed->len);
1134 old_handle = g_array_index (removed, guint, 0);
1136 rename_data = contact_rename_data_new (old_handle, reason, message);
1137 empathy_tp_contact_factory_get_from_handles (connection,
1138 added->len, (TpHandle *) added->data,
1139 tp_chat_got_renamed_contacts_cb,
1140 rename_data, (GDestroyNotify) contact_rename_data_free,
1146 actor_contact = chat_lookup_contact (self, actor, FALSE);
1147 if (actor_contact == NULL) {
1148 /* FIXME: handle this a tad more gracefully: perhaps
1149 * the actor was a server op. We could use the
1150 * contact-ids detail of MembersChangedDetailed.
1152 DEBUG ("actor %u not a channel member", actor);
1156 /* Remove contacts that are not members anymore */
1157 for (i = 0; i < removed->len; i++) {
1158 contact = chat_lookup_contact (self,
1159 g_array_index (removed, TpHandle, i), TRUE);
1161 if (contact != NULL) {
1162 g_signal_emit_by_name (self, "members-changed", contact,
1163 actor_contact, reason, message,
1165 g_object_unref (contact);
1169 /* Request added contacts */
1170 if (added->len > 0) {
1171 empathy_tp_contact_factory_get_from_handles (connection,
1172 added->len, (TpHandle *) added->data,
1173 tp_chat_got_added_contacts_cb, NULL, NULL,
1177 tp_chat_update_remote_contact (self);
1179 if (actor_contact != NULL) {
1180 g_object_unref (actor_contact);
1185 tp_chat_got_remote_contact_cb (TpConnection *connection,
1186 EmpathyContact *contact,
1187 const GError *error,
1191 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1194 DEBUG ("Error: %s", error->message);
1195 empathy_tp_chat_leave (self, "");
1199 self->priv->remote_contact = g_object_ref (contact);
1200 g_object_notify (chat, "remote-contact");
1202 check_almost_ready (self);
1206 tp_chat_got_self_contact_cb (TpConnection *connection,
1207 EmpathyContact *contact,
1208 const GError *error,
1212 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1215 DEBUG ("Error: %s", error->message);
1216 empathy_tp_chat_leave (self, "");
1220 self->priv->user = g_object_ref (contact);
1221 empathy_contact_set_is_user (self->priv->user, TRUE);
1222 check_almost_ready (self);
1226 password_flags_changed_cb (TpChannel *channel,
1230 GObject *weak_object)
1232 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1233 gboolean was_needed, needed;
1235 was_needed = empathy_tp_chat_password_needed (self);
1237 self->priv->password_flags |= added;
1238 self->priv->password_flags ^= removed;
1240 needed = empathy_tp_chat_password_needed (self);
1242 if (was_needed != needed)
1243 g_object_notify (G_OBJECT (self), "password-needed");
1247 got_password_flags_cb (TpChannel *proxy,
1248 guint password_flags,
1249 const GError *error,
1251 GObject *weak_object)
1253 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1255 self->priv->got_password_flags = TRUE;
1256 self->priv->password_flags = password_flags;
1258 check_almost_ready (EMPATHY_TP_CHAT (self));
1262 tp_chat_get_property (GObject *object,
1267 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1271 g_value_set_object (value, self->priv->account);
1273 case PROP_REMOTE_CONTACT:
1274 g_value_set_object (value, self->priv->remote_contact);
1276 case PROP_PASSWORD_NEEDED:
1277 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1279 case PROP_N_MESSAGES_SENDING:
1280 g_value_set_uint (value,
1281 g_hash_table_size (self->priv->messages_being_sent));
1284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1290 tp_chat_set_property (GObject *object,
1292 const GValue *value,
1295 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1299 self->priv->account = g_value_dup_object (value);
1302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1312 static const TpProxyFeature *
1313 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1315 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1316 static GQuark need[2] = {0, 0};
1318 if (G_LIKELY (features[0].name != 0))
1321 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1322 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1323 features[FEAT_READY].depends_on = need;
1324 features[FEAT_READY].prepare_async =
1325 tp_chat_prepare_ready_async;
1327 /* assert that the terminator at the end is there */
1328 g_assert (features[N_FEAT].name == 0);
1334 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1336 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1337 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1339 object_class->dispose = tp_chat_dispose;
1340 object_class->finalize = tp_chat_finalize;
1341 object_class->get_property = tp_chat_get_property;
1342 object_class->set_property = tp_chat_set_property;
1344 proxy_class->list_features = tp_chat_list_features;
1346 g_object_class_install_property (object_class,
1348 g_param_spec_object ("account",
1350 "the account associated with the chat",
1353 G_PARAM_CONSTRUCT_ONLY |
1354 G_PARAM_STATIC_STRINGS));
1356 g_object_class_install_property (object_class,
1357 PROP_REMOTE_CONTACT,
1358 g_param_spec_object ("remote-contact",
1359 "The remote contact",
1360 "The remote contact if there is no group iface on the channel",
1361 EMPATHY_TYPE_CONTACT,
1364 g_object_class_install_property (object_class,
1365 PROP_PASSWORD_NEEDED,
1366 g_param_spec_boolean ("password-needed",
1368 "TRUE if a password is needed to join the channel",
1372 g_object_class_install_property (object_class,
1373 PROP_N_MESSAGES_SENDING,
1374 g_param_spec_uint ("n-messages-sending",
1375 "Num Messages Sending",
1376 "The number of messages being sent",
1381 signals[MESSAGE_RECEIVED] =
1382 g_signal_new ("message-received-empathy",
1383 G_TYPE_FROM_CLASS (klass),
1387 g_cclosure_marshal_VOID__OBJECT,
1389 1, EMPATHY_TYPE_MESSAGE);
1391 signals[SEND_ERROR] =
1392 g_signal_new ("send-error",
1393 G_TYPE_FROM_CLASS (klass),
1397 _empathy_marshal_VOID__STRING_UINT_STRING,
1399 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1401 signals[CHAT_STATE_CHANGED] =
1402 g_signal_new ("chat-state-changed-empathy",
1403 G_TYPE_FROM_CLASS (klass),
1407 _empathy_marshal_VOID__OBJECT_UINT,
1409 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1411 signals[PROPERTY_CHANGED] =
1412 g_signal_new ("property-changed",
1413 G_TYPE_FROM_CLASS (klass),
1417 _empathy_marshal_VOID__STRING_BOXED,
1419 2, G_TYPE_STRING, G_TYPE_VALUE);
1421 signals[MESSAGE_ACKNOWLEDGED] =
1422 g_signal_new ("message-acknowledged",
1423 G_TYPE_FROM_CLASS (klass),
1427 g_cclosure_marshal_VOID__OBJECT,
1429 1, EMPATHY_TYPE_MESSAGE);
1431 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1435 empathy_tp_chat_init (EmpathyTpChat *self)
1437 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1438 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1440 self->priv->messages_queue = g_queue_new ();
1441 self->priv->pending_messages_queue = g_queue_new ();
1442 self->priv->messages_being_sent = g_hash_table_new_full (
1443 g_str_hash, g_str_equal, g_free, NULL);
1447 tp_chat_iface_init (EmpathyContactListIface *iface)
1449 iface->add = tp_chat_add;
1450 iface->remove = tp_chat_remove;
1451 iface->get_members = tp_chat_get_members;
1455 empathy_tp_chat_new (TpAccount *account,
1457 const gchar *object_path,
1458 const GHashTable *immutable_properties)
1460 TpProxy *conn_proxy = (TpProxy *) conn;
1462 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1463 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1464 g_return_val_if_fail (immutable_properties != NULL, NULL);
1466 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1469 "dbus-daemon", conn_proxy->dbus_daemon,
1470 "bus-name", conn_proxy->bus_name,
1471 "object-path", object_path,
1472 "channel-properties", immutable_properties,
1477 empathy_tp_chat_get_id (EmpathyTpChat *self)
1481 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1483 id = tp_channel_get_identifier ((TpChannel *) self);
1484 if (!EMP_STR_EMPTY (id))
1486 else if (self->priv->remote_contact)
1487 return empathy_contact_get_id (self->priv->remote_contact);
1494 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1496 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1498 return self->priv->remote_contact;
1502 empathy_tp_chat_get_account (EmpathyTpChat *self)
1504 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1506 return self->priv->account;
1510 empathy_tp_chat_send (EmpathyTpChat *self,
1513 gchar *message_body;
1515 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1516 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1518 message_body = tp_message_to_text (message, NULL);
1520 DEBUG ("Sending message: %s", message_body);
1522 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1523 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1524 message_send_cb, self);
1526 g_free (message_body);
1530 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1532 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1534 return self->priv->pending_messages_queue->head;
1538 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1539 EmpathyMessage *message) {
1542 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1544 if (!empathy_message_is_incoming (message))
1547 tp_msg = empathy_message_get_tp_message (message);
1548 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1549 tp_msg, NULL, NULL);
1553 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *self,
1554 const GSList *messages) {
1556 GList *messages_to_ack = NULL;
1558 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1560 if (messages == NULL)
1563 for (l = messages; l != NULL; l = g_slist_next (l)) {
1564 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1566 if (empathy_message_is_incoming (message)) {
1567 TpMessage *tp_msg = empathy_message_get_tp_message (message);
1568 messages_to_ack = g_list_append (messages_to_ack, tp_msg);
1572 if (messages_to_ack != NULL) {
1573 tp_text_channel_ack_messages_async (TP_TEXT_CHANNEL (self),
1574 messages_to_ack, NULL, NULL);
1577 g_list_free (messages_to_ack);
1581 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *self)
1583 empathy_tp_chat_acknowledge_messages (self,
1584 (GSList *) empathy_tp_chat_get_pending_messages (self));
1588 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1590 return self->priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1594 provide_password_cb (TpChannel *channel,
1596 const GError *error,
1598 GObject *weak_object)
1600 GSimpleAsyncResult *result = user_data;
1602 if (error != NULL) {
1603 g_simple_async_result_set_from_error (result, error);
1605 else if (!correct) {
1606 /* The current D-Bus API is a bit weird so re-use the
1607 * AuthenticationFailed error */
1608 g_simple_async_result_set_error (result, TP_ERRORS,
1609 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1612 g_simple_async_result_complete (result);
1613 g_object_unref (result);
1617 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1618 const gchar *password,
1619 GAsyncReadyCallback callback,
1622 GSimpleAsyncResult *result;
1624 result = g_simple_async_result_new (G_OBJECT (self),
1625 callback, user_data,
1626 empathy_tp_chat_provide_password_finish);
1628 tp_cli_channel_interface_password_call_provide_password
1629 ((TpChannel *) self, -1, password, provide_password_cb, result,
1630 NULL, G_OBJECT (self));
1634 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1635 GAsyncResult *result,
1638 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1642 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1643 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1649 * empathy_tp_chat_can_add_contact:
1651 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1652 * That is if this chat is a 1-to-1 channel that can be upgraded to
1653 * a MUC using the Conference interface or if the channel is a MUC.
1656 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1658 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1660 return self->priv->can_upgrade_to_muc ||
1661 tp_proxy_has_interface_by_id (self,
1662 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1666 tp_channel_leave_async_cb (GObject *source_object,
1670 GError *error = NULL;
1672 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1673 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1675 g_error_free (error);
1680 empathy_tp_chat_leave (EmpathyTpChat *self,
1681 const gchar *message)
1683 TpChannel *channel = (TpChannel *) self;
1685 DEBUG ("Leaving channel %s with message \"%s\"",
1686 tp_channel_get_identifier (channel), message);
1688 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1689 message, tp_channel_leave_async_cb, self);
1693 add_members_cb (TpChannel *proxy,
1694 const GError *error,
1696 GObject *weak_object)
1698 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1700 if (error != NULL) {
1701 DEBUG ("Failed to join chat (%s): %s",
1702 tp_channel_get_identifier ((TpChannel *) self), error->message);
1707 empathy_tp_chat_join (EmpathyTpChat *self)
1709 TpHandle self_handle;
1712 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1714 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1715 g_array_append_val (members, self_handle);
1717 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1718 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1720 g_array_free (members, TRUE);
1724 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1727 TpHandle self_handle;
1729 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1732 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1733 if (self_handle == 0)
1736 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1737 inviter, NULL, NULL);
1741 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1742 EmpathyContact *contact)
1744 return tp_channel_get_chat_state ((TpChannel *) self,
1745 empathy_contact_get_handle (contact));
1749 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1751 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1753 return self->priv->user;
1757 empathy_tp_chat_get_feature_ready (void)
1759 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1763 conn_prepared_cb (GObject *source,
1764 GAsyncResult *result,
1767 EmpathyTpChat *self = user_data;
1768 TpChannel *channel = (TpChannel *) self;
1769 GError *error = NULL;
1771 TpConnection *connection = (TpConnection *) source;
1773 if (!tp_proxy_prepare_finish (source, result, &error)) {
1774 g_simple_async_result_set_from_error (self->priv->ready_result, error);
1775 g_error_free (error);
1776 g_simple_async_result_complete (self->priv->ready_result);
1777 tp_clear_object (&self->priv->ready_result);
1781 if (tp_proxy_has_interface_by_id (self,
1782 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1783 const TpIntSet *members;
1786 /* Get self contact from the group's self handle */
1787 handle = tp_channel_group_get_self_handle (channel);
1788 empathy_tp_contact_factory_get_from_handle (connection,
1789 handle, tp_chat_got_self_contact_cb,
1790 NULL, NULL, G_OBJECT (self));
1792 /* Get initial member contacts */
1793 members = tp_channel_group_get_members (channel);
1794 handles = tp_intset_to_array (members);
1795 empathy_tp_contact_factory_get_from_handles (connection,
1796 handles->len, (TpHandle *) handles->data,
1797 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1799 self->priv->can_upgrade_to_muc = FALSE;
1801 tp_g_signal_connect_object (self, "group-members-changed",
1802 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1804 TpCapabilities *caps;
1808 /* Get the self contact from the connection's self handle */
1809 handle = tp_connection_get_self_handle (connection);
1810 empathy_tp_contact_factory_get_from_handle (connection,
1811 handle, tp_chat_got_self_contact_cb,
1812 NULL, NULL, G_OBJECT (self));
1814 /* Get the remote contact */
1815 handle = tp_channel_get_handle (channel, NULL);
1816 empathy_tp_contact_factory_get_from_handle (connection,
1817 handle, tp_chat_got_remote_contact_cb,
1818 NULL, NULL, G_OBJECT (self));
1820 caps = tp_connection_get_capabilities (connection);
1821 g_assert (caps != NULL);
1823 classes = tp_capabilities_get_channel_classes (caps);
1825 for (i = 0; i < classes->len; i++) {
1826 GValueArray *array = g_ptr_array_index (classes, i);
1827 const char **oprops = g_value_get_boxed (
1828 g_value_array_get_nth (array, 1));
1830 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1831 self->priv->can_upgrade_to_muc = TRUE;
1837 if (tp_proxy_has_interface_by_id (self,
1838 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1839 tp_cli_properties_interface_call_list_properties (channel, -1,
1840 tp_chat_list_properties_cb,
1843 tp_cli_properties_interface_connect_to_properties_changed (channel,
1844 tp_chat_properties_changed_cb,
1846 G_OBJECT (self), NULL);
1847 tp_cli_properties_interface_connect_to_property_flags_changed (channel,
1848 tp_chat_property_flags_changed_cb,
1850 G_OBJECT (self), NULL);
1853 /* Check if the chat is password protected */
1854 if (tp_proxy_has_interface_by_id (self,
1855 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1856 self->priv->got_password_flags = FALSE;
1858 tp_cli_channel_interface_password_connect_to_password_flags_changed
1859 (channel, password_flags_changed_cb, self, NULL,
1860 G_OBJECT (self), NULL);
1862 tp_cli_channel_interface_password_call_get_password_flags
1863 (channel, -1, got_password_flags_cb, self, NULL, G_OBJECT (self));
1865 /* No Password interface, so no need to fetch the password flags */
1866 self->priv->got_password_flags = TRUE;
1871 tp_chat_prepare_ready_async (TpProxy *proxy,
1872 const TpProxyFeature *feature,
1873 GAsyncReadyCallback callback,
1876 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1877 TpChannel *channel = (TpChannel *) proxy;
1878 TpConnection *connection;
1879 GQuark conn_features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
1881 g_assert (self->priv->ready_result == NULL);
1882 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1883 callback, user_data, tp_chat_prepare_ready_async);
1885 connection = tp_channel_borrow_connection (channel);
1887 tp_proxy_prepare_async (connection, conn_features,
1888 conn_prepared_cb, self);