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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
43 gboolean dispose_has_run;
45 TpConnection *connection;
47 EmpathyContact *remote_contact;
50 /* Queue of messages not signalled yet */
51 GQueue *messages_queue;
52 /* Queue of messages signalled but not acked yet */
53 GQueue *pending_messages_queue;
54 gboolean had_properties_list;
55 GPtrArray *properties;
56 TpChannelPasswordFlags password_flags;
57 /* TRUE if we fetched the password flag of the channel or if it's not needed
58 * (channel doesn't implement the Password interface) */
59 gboolean got_password_flags;
61 gboolean can_upgrade_to_muc;
64 static void tp_chat_iface_init (EmpathyContactListIface *iface);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
90 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
93 tp_chat_invalidated_cb (TpProxy *proxy,
99 DEBUG ("Channel invalidated: %s", message);
100 g_signal_emit (chat, signals[DESTROY], 0);
104 tp_chat_async_cb (TpChannel *proxy,
107 GObject *weak_object)
110 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
115 create_conference_cb (GObject *source,
116 GAsyncResult *result,
119 GError *error = NULL;
121 if (!tp_account_channel_request_create_channel_finish (
122 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
123 DEBUG ("Failed to create conference channel: %s", error->message);
124 g_error_free (error);
129 tp_chat_add (EmpathyContactList *list,
130 EmpathyContact *contact,
131 const gchar *message)
133 EmpathyTpChatPriv *priv = GET_PRIV (list);
135 if (tp_proxy_has_interface_by_id (priv->channel,
136 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
138 GArray handles = {(gchar *) &handle, 1};
140 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
141 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
143 handle = empathy_contact_get_handle (contact);
144 tp_cli_channel_interface_group_call_add_members (priv->channel,
145 -1, &handles, NULL, NULL, NULL, NULL, NULL);
146 } else if (priv->can_upgrade_to_muc) {
147 TpAccountChannelRequest *req;
149 const char *object_path;
150 GPtrArray channels = { (gpointer *) &object_path, 1 };
151 const char *invitees[2] = { NULL, };
153 invitees[0] = empathy_contact_get_id (contact);
154 object_path = tp_proxy_get_object_path (priv->channel);
157 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
158 TP_IFACE_CHANNEL_TYPE_TEXT,
159 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
161 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
162 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
163 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
164 G_TYPE_STRV, invitees,
165 /* FIXME: InvitationMessage ? */
168 req = tp_account_channel_request_new (priv->account, props,
169 TP_USER_ACTION_TIME_NOT_USER_ACTION);
171 /* Although this is a MUC, it's anonymous, so CreateChannel is
173 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
174 NULL, create_conference_cb, NULL);
176 g_object_unref (req);
177 g_hash_table_unref (props);
179 g_warning ("Cannot add to this channel");
184 tp_chat_remove (EmpathyContactList *list,
185 EmpathyContact *contact,
186 const gchar *message)
188 EmpathyTpChatPriv *priv = GET_PRIV (list);
190 GArray handles = {(gchar *) &handle, 1};
192 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
193 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
195 handle = empathy_contact_get_handle (contact);
196 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
203 tp_chat_get_members (EmpathyContactList *list)
205 EmpathyTpChatPriv *priv = GET_PRIV (list);
206 GList *members = NULL;
208 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
211 members = g_list_copy (priv->members);
212 g_list_foreach (members, (GFunc) g_object_ref, NULL);
214 members = g_list_prepend (members, g_object_ref (priv->user));
215 if (priv->remote_contact != NULL)
216 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
223 check_ready (EmpathyTpChat *chat)
225 EmpathyTpChatPriv *priv = GET_PRIV (chat);
230 if (g_queue_get_length (priv->messages_queue) > 0)
236 g_object_notify (G_OBJECT (chat), "ready");
240 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
242 EmpathyTpChatPriv *priv = GET_PRIV (chat);
243 EmpathyMessage *message;
245 /* Check if we can now emit some queued messages */
246 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
247 if (empathy_message_get_sender (message) == NULL) {
251 DEBUG ("Queued message ready");
252 g_queue_pop_head (priv->messages_queue);
253 g_queue_push_tail (priv->pending_messages_queue, message);
254 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
261 tp_chat_got_sender_cb (TpConnection *connection,
262 EmpathyContact *contact,
267 EmpathyTpChatPriv *priv = GET_PRIV (chat);
270 DEBUG ("Error: %s", error->message);
271 /* Do not block the message queue, just drop this message */
272 g_queue_remove (priv->messages_queue, message);
274 empathy_message_set_sender (message, contact);
277 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
281 tp_chat_build_message (EmpathyTpChat *chat,
287 const gchar *message_body,
288 TpChannelTextMessageFlags flags)
290 EmpathyTpChatPriv *priv;
291 EmpathyMessage *message;
293 priv = GET_PRIV (chat);
295 message = empathy_message_new (message_body);
296 empathy_message_set_tptype (message, type);
297 empathy_message_set_receiver (message, priv->user);
298 empathy_message_set_timestamp (message, timestamp);
299 empathy_message_set_id (message, id);
300 empathy_message_set_incoming (message, incoming);
301 empathy_message_set_flags (message, flags);
303 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK)
304 empathy_message_set_is_backlog (message, TRUE);
306 g_queue_push_tail (priv->messages_queue, message);
308 if (from_handle == 0) {
309 empathy_message_set_sender (message, priv->user);
310 tp_chat_emit_queued_messages (chat);
312 empathy_tp_contact_factory_get_from_handle (priv->connection,
314 tp_chat_got_sender_cb,
315 message, NULL, G_OBJECT (chat));
320 message_received_cb (TpTextChannel *channel,
324 EmpathyTpChatPriv *priv = GET_PRIV (chat);
328 TpHandle from_handle;
329 TpChannelTextMessageFlags message_flags;
331 message_id = tp_asv_get_uint32 (tp_message_peek (message, 0),
332 "pending-message-id", NULL);
334 sender = tp_signalled_message_get_sender (message);
335 g_assert (sender != NULL);
336 from_handle = tp_contact_get_handle (sender);
338 message_body = tp_message_to_text (message, &message_flags);
340 DEBUG ("Message received from channel %s: %s",
341 tp_proxy_get_object_path (channel), message_body);
343 if (message_body == NULL) {
344 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
346 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
347 message, NULL, NULL);
351 tp_chat_build_message (chat,
354 tp_message_get_message_type (message),
355 tp_message_get_received_timestamp (message),
360 g_free (message_body);
364 message_sent_cb (TpTextChannel *channel,
366 TpMessageSendingFlags flags,
372 message_body = tp_message_to_text (message, NULL);
374 DEBUG ("Message sent: %s", message_body);
376 tp_chat_build_message (chat,
379 tp_message_get_message_type (message),
380 tp_message_get_received_timestamp (message),
385 g_free (message_body);
389 tp_chat_send_error_cb (TpChannel *channel,
393 const gchar *message_body,
397 EmpathyTpChatPriv *priv = GET_PRIV (chat);
399 if (priv->channel == NULL)
402 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
404 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
408 tp_chat_send_cb (TpChannel *proxy,
413 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
416 DEBUG ("Error: %s", error->message);
417 g_signal_emit (chat, signals[SEND_ERROR], 0,
418 empathy_message_get_body (message),
419 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
425 TpChannelChatState state;
429 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
430 EmpathyContact *contact,
435 TpChannelChatState state;
438 DEBUG ("Error: %s", error->message);
442 state = GPOINTER_TO_UINT (user_data);
443 DEBUG ("Chat state changed for %s (%d): %d",
444 empathy_contact_get_alias (contact),
445 empathy_contact_get_handle (contact), state);
447 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
451 tp_chat_state_changed_cb (TpChannel *channel,
453 TpChannelChatState state,
457 EmpathyTpChatPriv *priv = GET_PRIV (chat);
459 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
460 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
465 list_pending_messages (EmpathyTpChat *self)
467 EmpathyTpChatPriv *priv = GET_PRIV (self);
470 g_assert (priv->channel != NULL);
472 messages = tp_text_channel_get_pending_messages (
473 TP_TEXT_CHANNEL (priv->channel));
475 for (l = messages; l != NULL; l = g_list_next (l)) {
476 TpMessage *message = l->data;
483 /* FIXME: this is pretty low level, ideally we shouldn't have to use the
484 * ID directly but we don't use TpTextChannel's ack API everywhere yet. */
485 message_id = tp_asv_get_uint32 (tp_message_peek (message, 0),
486 "pending-message-id", NULL);
488 sender = tp_signalled_message_get_sender (message);
489 g_assert (sender != NULL);
490 from_handle = tp_contact_get_handle (sender);
492 message_body = tp_message_to_text (message, &message_flags);
494 DEBUG ("Message pending: %s", message_body);
496 if (message_body == NULL) {
497 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
499 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
500 message, NULL, NULL);
504 tp_chat_build_message (self,
507 tp_message_get_message_type (message),
508 tp_message_get_received_timestamp (message),
513 g_free (message_body);
516 g_list_free (messages);
520 tp_chat_property_flags_changed_cb (TpProxy *proxy,
521 const GPtrArray *properties,
525 EmpathyTpChatPriv *priv = GET_PRIV (chat);
528 if (priv->channel == NULL)
531 if (!priv->had_properties_list || !properties) {
535 for (i = 0; i < properties->len; i++) {
536 GValueArray *prop_struct;
537 EmpathyTpChatProperty *property;
541 prop_struct = g_ptr_array_index (properties, i);
542 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
543 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
545 for (j = 0; j < priv->properties->len; j++) {
546 property = g_ptr_array_index (priv->properties, j);
547 if (property->id == id) {
548 property->flags = flags;
549 DEBUG ("property %s flags changed: %d",
550 property->name, property->flags);
558 tp_chat_properties_changed_cb (TpProxy *proxy,
559 const GPtrArray *properties,
563 EmpathyTpChatPriv *priv = GET_PRIV (chat);
566 if (priv->channel == NULL)
569 if (!priv->had_properties_list || !properties) {
573 for (i = 0; i < properties->len; i++) {
574 GValueArray *prop_struct;
575 EmpathyTpChatProperty *property;
579 prop_struct = g_ptr_array_index (properties, i);
580 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
581 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
583 for (j = 0; j < priv->properties->len; j++) {
584 property = g_ptr_array_index (priv->properties, j);
585 if (property->id == id) {
586 if (property->value) {
587 g_value_copy (src_value, property->value);
589 property->value = tp_g_value_slice_dup (src_value);
592 DEBUG ("property %s changed", property->name);
593 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
594 property->name, property->value);
602 tp_chat_get_properties_cb (TpProxy *proxy,
603 const GPtrArray *properties,
609 DEBUG ("Error getting properties: %s", error->message);
613 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
617 tp_chat_list_properties_cb (TpProxy *proxy,
618 const GPtrArray *properties,
623 EmpathyTpChatPriv *priv = GET_PRIV (chat);
627 if (priv->channel == NULL)
630 priv->had_properties_list = TRUE;
633 DEBUG ("Error listing properties: %s", error->message);
637 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
638 priv->properties = g_ptr_array_sized_new (properties->len);
639 for (i = 0; i < properties->len; i++) {
640 GValueArray *prop_struct;
641 EmpathyTpChatProperty *property;
643 prop_struct = g_ptr_array_index (properties, i);
644 property = g_slice_new0 (EmpathyTpChatProperty);
645 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
646 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
647 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
649 DEBUG ("Adding property name=%s id=%d flags=%d",
650 property->name, property->id, property->flags);
651 g_ptr_array_add (priv->properties, property);
652 if (property->flags & TP_PROPERTY_FLAG_READ) {
653 g_array_append_val (ids, property->id);
657 tp_cli_properties_interface_call_get_properties (proxy, -1,
659 tp_chat_get_properties_cb,
663 g_array_free (ids, TRUE);
667 empathy_tp_chat_set_property (EmpathyTpChat *chat,
671 EmpathyTpChatPriv *priv = GET_PRIV (chat);
672 EmpathyTpChatProperty *property;
675 if (!priv->had_properties_list) {
679 for (i = 0; i < priv->properties->len; i++) {
680 property = g_ptr_array_index (priv->properties, i);
681 if (!tp_strdiff (property->name, name)) {
682 GPtrArray *properties;
685 GValue dest_value = {0, };
687 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
691 g_value_init (&id, G_TYPE_UINT);
692 g_value_init (&dest_value, G_TYPE_VALUE);
693 g_value_set_uint (&id, property->id);
694 g_value_set_boxed (&dest_value, value);
696 prop = g_value_array_new (2);
697 g_value_array_append (prop, &id);
698 g_value_array_append (prop, &dest_value);
700 properties = g_ptr_array_sized_new (1);
701 g_ptr_array_add (properties, prop);
703 DEBUG ("Set property %s", name);
704 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
706 (tp_cli_properties_interface_callback_for_set_properties)
708 "Seting property", NULL,
711 g_ptr_array_free (properties, TRUE);
712 g_value_array_free (prop);
719 EmpathyTpChatProperty *
720 empathy_tp_chat_get_property (EmpathyTpChat *chat,
723 EmpathyTpChatPriv *priv = GET_PRIV (chat);
724 EmpathyTpChatProperty *property;
727 if (!priv->had_properties_list) {
731 for (i = 0; i < priv->properties->len; i++) {
732 property = g_ptr_array_index (priv->properties, i);
733 if (!tp_strdiff (property->name, name)) {
742 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
744 EmpathyTpChatPriv *priv = GET_PRIV (chat);
746 return priv->properties;
750 tp_chat_dispose (GObject *object)
752 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
753 EmpathyTpChatPriv *priv = GET_PRIV (self);
755 if (priv->dispose_has_run)
758 priv->dispose_has_run = TRUE;
760 tp_clear_object (&priv->account);
762 if (priv->connection != NULL)
763 g_object_unref (priv->connection);
764 priv->connection = NULL;
766 if (priv->channel != NULL) {
767 g_signal_handlers_disconnect_by_func (priv->channel,
768 tp_chat_invalidated_cb, self);
769 g_object_unref (priv->channel);
771 priv->channel = NULL;
773 if (priv->remote_contact != NULL)
774 g_object_unref (priv->remote_contact);
775 priv->remote_contact = NULL;
777 if (priv->user != NULL)
778 g_object_unref (priv->user);
781 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
782 g_queue_clear (priv->messages_queue);
784 g_queue_foreach (priv->pending_messages_queue,
785 (GFunc) g_object_unref, NULL);
786 g_queue_clear (priv->pending_messages_queue);
788 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
789 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
793 tp_chat_finalize (GObject *object)
795 EmpathyTpChatPriv *priv = GET_PRIV (object);
798 DEBUG ("Finalize: %p", object);
800 if (priv->properties) {
801 for (i = 0; i < priv->properties->len; i++) {
802 EmpathyTpChatProperty *property;
804 property = g_ptr_array_index (priv->properties, i);
805 g_free (property->name);
806 if (property->value) {
807 tp_g_value_slice_free (property->value);
809 g_slice_free (EmpathyTpChatProperty, property);
811 g_ptr_array_free (priv->properties, TRUE);
814 g_queue_free (priv->messages_queue);
815 g_queue_free (priv->pending_messages_queue);
817 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
821 check_almost_ready (EmpathyTpChat *chat)
823 EmpathyTpChatPriv *priv = GET_PRIV (chat);
828 if (priv->user == NULL)
831 if (!priv->got_password_flags)
834 /* We need either the members (room) or the remote contact (private chat).
835 * If the chat is protected by a password we can't get these information so
836 * consider the chat as ready so it can be presented to the user. */
837 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
838 priv->remote_contact == NULL)
841 /* We use the default factory so this feature should have been prepared */
842 g_assert (tp_proxy_is_prepared (priv->channel,
843 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
845 tp_g_signal_connect_object (priv->channel, "message-received",
846 G_CALLBACK (message_received_cb), chat, 0);
848 list_pending_messages (chat);
850 tp_g_signal_connect_object (priv->channel, "message-sent",
851 G_CALLBACK (message_sent_cb), chat, 0);
853 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
854 tp_chat_send_error_cb,
856 G_OBJECT (chat), NULL);
857 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
858 tp_chat_state_changed_cb,
860 G_OBJECT (chat), NULL);
866 tp_chat_update_remote_contact (EmpathyTpChat *chat)
868 EmpathyTpChatPriv *priv = GET_PRIV (chat);
869 EmpathyContact *contact = NULL;
870 TpHandle self_handle;
871 TpHandleType handle_type;
874 /* If this is a named chatroom, never pretend it is a private chat */
875 tp_channel_get_handle (priv->channel, &handle_type);
876 if (handle_type == TP_HANDLE_TYPE_ROOM) {
880 /* This is an MSN chat, but it's the new style where 1-1 chats don't
881 * have the group interface. If it has the conference interface, then
882 * it is indeed a MUC. */
883 if (tp_proxy_has_interface_by_id (priv->channel,
884 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
888 /* This is an MSN-like chat where anyone can join the chat at anytime.
889 * If there is only one non-self contact member, we are in a private
890 * chat and we set the "remote-contact" property to that contact. If
891 * there are more, set the "remote-contact" property to NULL and the
892 * UI will display a contact list. */
893 self_handle = tp_channel_group_get_self_handle (priv->channel);
894 for (l = priv->members; l; l = l->next) {
895 /* Skip self contact if member */
896 if (empathy_contact_get_handle (l->data) == self_handle) {
900 /* We have more than one remote contact, break */
901 if (contact != NULL) {
906 /* If we didn't find yet a remote contact, keep this one */
910 if (priv->remote_contact == contact) {
914 DEBUG ("Changing remote contact from %p to %p",
915 priv->remote_contact, contact);
917 if (priv->remote_contact) {
918 g_object_unref (priv->remote_contact);
921 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
922 g_object_notify (G_OBJECT (chat), "remote-contact");
926 tp_chat_got_added_contacts_cb (TpConnection *connection,
928 EmpathyContact * const * contacts,
930 const TpHandle *failed,
935 EmpathyTpChatPriv *priv = GET_PRIV (chat);
937 const TpIntSet *members;
939 EmpathyContact *contact;
942 DEBUG ("Error: %s", error->message);
946 members = tp_channel_group_get_members (priv->channel);
947 for (i = 0; i < n_contacts; i++) {
948 contact = contacts[i];
949 handle = empathy_contact_get_handle (contact);
951 /* Make sure the contact is still member */
952 if (tp_intset_is_member (members, handle)) {
953 priv->members = g_list_prepend (priv->members,
954 g_object_ref (contact));
955 g_signal_emit_by_name (chat, "members-changed",
956 contact, NULL, 0, NULL, TRUE);
960 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
961 check_almost_ready (EMPATHY_TP_CHAT (chat));
964 static EmpathyContact *
965 chat_lookup_contact (EmpathyTpChat *chat,
969 EmpathyTpChatPriv *priv = GET_PRIV (chat);
972 for (l = priv->members; l; l = l->next) {
973 EmpathyContact *c = l->data;
975 if (empathy_contact_get_handle (c) != handle) {
980 /* Caller takes the reference. */
981 priv->members = g_list_delete_link (priv->members, l);
999 static ContactRenameData *
1000 contact_rename_data_new (TpHandle handle,
1002 const gchar* message)
1004 ContactRenameData *data = g_new (ContactRenameData, 1);
1005 data->old_handle = handle;
1006 data->reason = reason;
1007 data->message = g_strdup (message);
1013 contact_rename_data_free (ContactRenameData* data)
1015 g_free (data->message);
1020 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1022 EmpathyContact * const * contacts,
1024 const TpHandle *failed,
1025 const GError *error,
1029 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1030 const TpIntSet *members;
1032 EmpathyContact *old = NULL, *new = NULL;
1033 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1036 DEBUG ("Error: %s", error->message);
1040 /* renamed members can only be delivered one at a time */
1041 g_warn_if_fail (n_contacts == 1);
1045 members = tp_channel_group_get_members (priv->channel);
1046 handle = empathy_contact_get_handle (new);
1048 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1049 rename_data->old_handle, TRUE);
1051 /* Make sure the contact is still member */
1052 if (tp_intset_is_member (members, handle)) {
1053 priv->members = g_list_prepend (priv->members,
1054 g_object_ref (new));
1057 g_signal_emit_by_name (chat, "member-renamed",
1058 old, new, rename_data->reason,
1059 rename_data->message);
1060 g_object_unref (old);
1064 if (priv->user == old) {
1065 /* We change our nick */
1066 tp_clear_object (&priv->user);
1067 priv->user = g_object_ref (new);
1070 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1071 check_almost_ready (EMPATHY_TP_CHAT (chat));
1076 tp_chat_group_members_changed_cb (TpChannel *self,
1080 GArray *local_pending,
1081 GArray *remote_pending,
1084 EmpathyTpChat *chat)
1086 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1087 EmpathyContact *contact;
1088 EmpathyContact *actor_contact = NULL;
1090 ContactRenameData *rename_data;
1091 TpHandle old_handle;
1093 /* Contact renamed */
1094 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1095 /* there can only be a single 'added' and a single 'removed' handle */
1096 if (removed->len != 1 || added->len != 1) {
1097 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1098 added->len, removed->len);
1102 old_handle = g_array_index (removed, guint, 0);
1104 rename_data = contact_rename_data_new (old_handle, reason, message);
1105 empathy_tp_contact_factory_get_from_handles (priv->connection,
1106 added->len, (TpHandle *) added->data,
1107 tp_chat_got_renamed_contacts_cb,
1108 rename_data, (GDestroyNotify) contact_rename_data_free,
1114 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1115 if (actor_contact == NULL) {
1116 /* FIXME: handle this a tad more gracefully: perhaps
1117 * the actor was a server op. We could use the
1118 * contact-ids detail of MembersChangedDetailed.
1120 DEBUG ("actor %u not a channel member", actor);
1124 /* Remove contacts that are not members anymore */
1125 for (i = 0; i < removed->len; i++) {
1126 contact = chat_lookup_contact (chat,
1127 g_array_index (removed, TpHandle, i), TRUE);
1129 if (contact != NULL) {
1130 g_signal_emit_by_name (chat, "members-changed", contact,
1131 actor_contact, reason, message,
1133 g_object_unref (contact);
1137 /* Request added contacts */
1138 if (added->len > 0) {
1139 empathy_tp_contact_factory_get_from_handles (priv->connection,
1140 added->len, (TpHandle *) added->data,
1141 tp_chat_got_added_contacts_cb, NULL, NULL,
1145 tp_chat_update_remote_contact (chat);
1147 if (actor_contact != NULL) {
1148 g_object_unref (actor_contact);
1153 tp_chat_got_remote_contact_cb (TpConnection *connection,
1154 EmpathyContact *contact,
1155 const GError *error,
1159 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1162 DEBUG ("Error: %s", error->message);
1163 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1167 priv->remote_contact = g_object_ref (contact);
1168 g_object_notify (chat, "remote-contact");
1170 check_almost_ready (EMPATHY_TP_CHAT (chat));
1174 tp_chat_got_self_contact_cb (TpConnection *connection,
1175 EmpathyContact *contact,
1176 const GError *error,
1180 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1183 DEBUG ("Error: %s", error->message);
1184 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1188 priv->user = g_object_ref (contact);
1189 empathy_contact_set_is_user (priv->user, TRUE);
1190 check_almost_ready (EMPATHY_TP_CHAT (chat));
1194 password_flags_changed_cb (TpChannel *channel,
1198 GObject *weak_object)
1200 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1201 EmpathyTpChatPriv *priv = GET_PRIV (self);
1202 gboolean was_needed, needed;
1204 was_needed = empathy_tp_chat_password_needed (self);
1206 priv->password_flags |= added;
1207 priv->password_flags ^= removed;
1209 needed = empathy_tp_chat_password_needed (self);
1211 if (was_needed != needed)
1212 g_object_notify (G_OBJECT (self), "password-needed");
1216 got_password_flags_cb (TpChannel *proxy,
1217 guint password_flags,
1218 const GError *error,
1220 GObject *weak_object)
1222 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1223 EmpathyTpChatPriv *priv = GET_PRIV (self);
1225 priv->got_password_flags = TRUE;
1226 priv->password_flags = password_flags;
1228 check_almost_ready (EMPATHY_TP_CHAT (self));
1232 tp_chat_constructor (GType type,
1234 GObjectConstructParam *props)
1237 EmpathyTpChatPriv *priv;
1240 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1242 priv = GET_PRIV (chat);
1244 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1245 tp_g_signal_connect_object (priv->channel, "invalidated",
1246 G_CALLBACK (tp_chat_invalidated_cb),
1249 g_assert (tp_proxy_is_prepared (priv->connection,
1250 TP_CONNECTION_FEATURE_CAPABILITIES));
1252 if (tp_proxy_has_interface_by_id (priv->channel,
1253 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1254 const TpIntSet *members;
1257 /* Get self contact from the group's self handle */
1258 handle = tp_channel_group_get_self_handle (priv->channel);
1259 empathy_tp_contact_factory_get_from_handle (priv->connection,
1260 handle, tp_chat_got_self_contact_cb,
1263 /* Get initial member contacts */
1264 members = tp_channel_group_get_members (priv->channel);
1265 handles = tp_intset_to_array (members);
1266 empathy_tp_contact_factory_get_from_handles (priv->connection,
1267 handles->len, (TpHandle *) handles->data,
1268 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1270 priv->can_upgrade_to_muc = FALSE;
1272 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1273 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1275 TpCapabilities *caps;
1279 /* Get the self contact from the connection's self handle */
1280 handle = tp_connection_get_self_handle (priv->connection);
1281 empathy_tp_contact_factory_get_from_handle (priv->connection,
1282 handle, tp_chat_got_self_contact_cb,
1285 /* Get the remote contact */
1286 handle = tp_channel_get_handle (priv->channel, NULL);
1287 empathy_tp_contact_factory_get_from_handle (priv->connection,
1288 handle, tp_chat_got_remote_contact_cb,
1291 caps = tp_connection_get_capabilities (priv->connection);
1292 g_assert (caps != NULL);
1294 classes = tp_capabilities_get_channel_classes (caps);
1296 for (i = 0; i < classes->len; i++) {
1297 GValueArray *array = g_ptr_array_index (classes, i);
1298 const char **oprops = g_value_get_boxed (
1299 g_value_array_get_nth (array, 1));
1301 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1302 priv->can_upgrade_to_muc = TRUE;
1308 if (tp_proxy_has_interface_by_id (priv->channel,
1309 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1310 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1311 tp_chat_list_properties_cb,
1314 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1315 tp_chat_properties_changed_cb,
1317 G_OBJECT (chat), NULL);
1318 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1319 tp_chat_property_flags_changed_cb,
1321 G_OBJECT (chat), NULL);
1324 /* Check if the chat is password protected */
1325 if (tp_proxy_has_interface_by_id (priv->channel,
1326 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1327 priv->got_password_flags = FALSE;
1329 tp_cli_channel_interface_password_connect_to_password_flags_changed
1330 (priv->channel, password_flags_changed_cb, chat, NULL,
1331 G_OBJECT (chat), NULL);
1333 tp_cli_channel_interface_password_call_get_password_flags
1334 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1336 /* No Password interface, so no need to fetch the password flags */
1337 priv->got_password_flags = TRUE;
1344 tp_chat_get_property (GObject *object,
1349 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1350 EmpathyTpChatPriv *priv = GET_PRIV (object);
1354 g_value_set_object (value, priv->account);
1357 g_value_set_object (value, priv->channel);
1359 case PROP_REMOTE_CONTACT:
1360 g_value_set_object (value, priv->remote_contact);
1363 g_value_set_boolean (value, priv->ready);
1365 case PROP_PASSWORD_NEEDED:
1366 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1375 tp_chat_set_property (GObject *object,
1377 const GValue *value,
1380 EmpathyTpChatPriv *priv = GET_PRIV (object);
1384 priv->account = g_value_dup_object (value);
1387 priv->channel = g_value_dup_object (value);
1390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1396 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1398 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1400 object_class->dispose = tp_chat_dispose;
1401 object_class->finalize = tp_chat_finalize;
1402 object_class->constructor = tp_chat_constructor;
1403 object_class->get_property = tp_chat_get_property;
1404 object_class->set_property = tp_chat_set_property;
1406 g_object_class_install_property (object_class,
1408 g_param_spec_object ("account",
1410 "the account associated with the chat",
1413 G_PARAM_CONSTRUCT_ONLY |
1414 G_PARAM_STATIC_STRINGS));
1416 g_object_class_install_property (object_class,
1418 g_param_spec_object ("channel",
1419 "telepathy channel",
1420 "The text channel for the chat",
1423 G_PARAM_CONSTRUCT_ONLY));
1425 g_object_class_install_property (object_class,
1426 PROP_REMOTE_CONTACT,
1427 g_param_spec_object ("remote-contact",
1428 "The remote contact",
1429 "The remote contact if there is no group iface on the channel",
1430 EMPATHY_TYPE_CONTACT,
1433 g_object_class_install_property (object_class,
1435 g_param_spec_boolean ("ready",
1436 "Is the object ready",
1437 "This object can't be used until this becomes true",
1441 g_object_class_install_property (object_class,
1442 PROP_PASSWORD_NEEDED,
1443 g_param_spec_boolean ("password-needed",
1445 "TRUE if a password is needed to join the channel",
1450 signals[MESSAGE_RECEIVED] =
1451 g_signal_new ("message-received",
1452 G_TYPE_FROM_CLASS (klass),
1456 g_cclosure_marshal_VOID__OBJECT,
1458 1, EMPATHY_TYPE_MESSAGE);
1460 signals[SEND_ERROR] =
1461 g_signal_new ("send-error",
1462 G_TYPE_FROM_CLASS (klass),
1466 _empathy_marshal_VOID__STRING_UINT,
1468 2, G_TYPE_STRING, G_TYPE_UINT);
1470 signals[CHAT_STATE_CHANGED] =
1471 g_signal_new ("chat-state-changed",
1472 G_TYPE_FROM_CLASS (klass),
1476 _empathy_marshal_VOID__OBJECT_UINT,
1478 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1480 signals[PROPERTY_CHANGED] =
1481 g_signal_new ("property-changed",
1482 G_TYPE_FROM_CLASS (klass),
1486 _empathy_marshal_VOID__STRING_BOXED,
1488 2, G_TYPE_STRING, G_TYPE_VALUE);
1491 g_signal_new ("destroy",
1492 G_TYPE_FROM_CLASS (klass),
1496 g_cclosure_marshal_VOID__VOID,
1500 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1504 empathy_tp_chat_init (EmpathyTpChat *chat)
1506 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1507 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1510 priv->messages_queue = g_queue_new ();
1511 priv->pending_messages_queue = g_queue_new ();
1515 tp_chat_iface_init (EmpathyContactListIface *iface)
1517 iface->add = tp_chat_add;
1518 iface->remove = tp_chat_remove;
1519 iface->get_members = tp_chat_get_members;
1523 empathy_tp_chat_new (TpAccount *account,
1526 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1527 g_return_val_if_fail (TP_IS_TEXT_CHANNEL (channel), NULL);
1529 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1536 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1538 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1542 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1544 id = tp_channel_get_identifier (priv->channel);
1545 if (!EMP_STR_EMPTY (id))
1547 else if (priv->remote_contact)
1548 return empathy_contact_get_id (priv->remote_contact);
1555 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1557 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1559 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1560 g_return_val_if_fail (priv->ready, NULL);
1562 return priv->remote_contact;
1566 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1568 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1570 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1572 return priv->channel;
1576 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1578 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1580 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1582 return priv->account;
1586 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1588 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1590 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1592 return tp_channel_borrow_connection (priv->channel);
1595 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1597 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1599 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1605 empathy_tp_chat_send (EmpathyTpChat *chat,
1606 EmpathyMessage *message)
1608 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1609 const gchar *message_body;
1610 TpChannelTextMessageType message_type;
1612 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1613 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1614 g_return_if_fail (priv->ready);
1616 message_body = empathy_message_get_body (message);
1617 message_type = empathy_message_get_tptype (message);
1619 DEBUG ("Sending message: %s", message_body);
1620 tp_cli_channel_type_text_call_send (priv->channel, -1,
1624 g_object_ref (message),
1625 (GDestroyNotify) g_object_unref,
1630 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1631 TpChannelChatState state)
1633 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1635 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1636 g_return_if_fail (priv->ready);
1638 if (tp_proxy_has_interface_by_id (priv->channel,
1639 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1640 DEBUG ("Set state: %d", state);
1641 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1644 "setting chat state",
1652 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1654 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1656 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1657 g_return_val_if_fail (priv->ready, NULL);
1659 return priv->pending_messages_queue->head;
1663 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1664 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1666 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1667 priv->channel, -1, ids, tp_chat_async_cb,
1668 "acknowledging received message", NULL, G_OBJECT (chat));
1672 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1673 EmpathyMessage *message) {
1674 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1675 GArray *message_ids;
1679 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1680 g_return_if_fail (priv->ready);
1682 if (!empathy_message_is_incoming (message))
1685 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1687 id = empathy_message_get_id (message);
1688 g_array_append_val (message_ids, id);
1689 acknowledge_messages (chat, message_ids);
1690 g_array_free (message_ids, TRUE);
1693 m = g_queue_find (priv->pending_messages_queue, message);
1694 g_assert (m != NULL);
1695 g_queue_delete_link (priv->pending_messages_queue, m);
1696 g_object_unref (message);
1700 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1701 const GSList *messages) {
1702 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1703 /* Copy messages as the messges list (probably is) our own */
1704 GSList *msgs = g_slist_copy ((GSList *) messages);
1707 GArray *message_ids;
1709 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1710 g_return_if_fail (priv->ready);
1712 length = g_slist_length ((GSList *) messages);
1717 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1719 for (l = msgs; l != NULL; l = g_slist_next (l)) {
1722 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1724 m = g_queue_find (priv->pending_messages_queue, message);
1725 g_assert (m != NULL);
1726 g_queue_delete_link (priv->pending_messages_queue, m);
1728 if (empathy_message_is_incoming (message)) {
1729 guint id = empathy_message_get_id (message);
1730 g_array_append_val (message_ids, id);
1732 g_object_unref (message);
1735 if (message_ids->len > 0)
1736 acknowledge_messages (chat, message_ids);
1738 g_array_free (message_ids, TRUE);
1739 g_slist_free (msgs);
1743 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
1745 empathy_tp_chat_acknowledge_messages (chat,
1746 (GSList *) empathy_tp_chat_get_pending_messages (chat));
1750 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1752 EmpathyTpChatPriv *priv = GET_PRIV (self);
1754 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1758 provide_password_cb (TpChannel *channel,
1760 const GError *error,
1762 GObject *weak_object)
1764 GSimpleAsyncResult *result = user_data;
1766 if (error != NULL) {
1767 g_simple_async_result_set_from_error (result, error);
1769 else if (!correct) {
1770 /* The current D-Bus API is a bit weird so re-use the
1771 * AuthenticationFailed error */
1772 g_simple_async_result_set_error (result, TP_ERRORS,
1773 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1776 g_simple_async_result_complete (result);
1777 g_object_unref (result);
1781 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1782 const gchar *password,
1783 GAsyncReadyCallback callback,
1786 EmpathyTpChatPriv *priv = GET_PRIV (self);
1787 GSimpleAsyncResult *result;
1789 result = g_simple_async_result_new (G_OBJECT (self),
1790 callback, user_data,
1791 empathy_tp_chat_provide_password_finish);
1793 tp_cli_channel_interface_password_call_provide_password
1794 (priv->channel, -1, password, provide_password_cb, result,
1795 NULL, G_OBJECT (self));
1799 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1800 GAsyncResult *result,
1803 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1807 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1808 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1814 * empathy_tp_chat_can_add_contact:
1816 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1817 * That is if this chat is a 1-to-1 channel that can be upgraded to
1818 * a MUC using the Conference interface or if the channel is a MUC.
1821 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1823 EmpathyTpChatPriv *priv;
1825 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1827 priv = GET_PRIV (self);
1829 return priv->can_upgrade_to_muc ||
1830 tp_proxy_has_interface_by_id (priv->channel,
1831 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1835 tp_channel_leave_async_cb (GObject *source_object,
1839 GError *error = NULL;
1841 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1842 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1844 g_error_free (error);
1849 empathy_tp_chat_leave (EmpathyTpChat *self,
1850 const gchar *message)
1852 EmpathyTpChatPriv *priv = GET_PRIV (self);
1854 tp_channel_leave_async (priv->channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1855 message, tp_channel_leave_async_cb, self);
1859 add_members_cb (TpChannel *proxy,
1860 const GError *error,
1862 GObject *weak_object)
1864 EmpathyTpChatPriv *priv = GET_PRIV (weak_object);
1866 if (error != NULL) {
1867 DEBUG ("Failed to join chat (%s): %s",
1868 tp_channel_get_identifier (priv->channel), error->message);
1873 empathy_tp_chat_join (EmpathyTpChat *self)
1875 EmpathyTpChatPriv *priv = GET_PRIV (self);
1876 TpHandle self_handle;
1879 self_handle = tp_channel_group_get_self_handle (priv->channel);
1881 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1882 g_array_append_val (members, self_handle);
1884 tp_cli_channel_interface_group_call_add_members (priv->channel, -1, members,
1885 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1887 g_array_free (members, TRUE);
1891 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1894 EmpathyTpChatPriv *priv = GET_PRIV (self);
1895 TpHandle self_handle;
1897 if (!tp_proxy_has_interface (priv->channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1900 self_handle = tp_channel_group_get_self_handle (priv->channel);
1901 if (self_handle == 0)
1904 return tp_channel_group_get_local_pending_info (priv->channel, self_handle,
1905 inviter, NULL, NULL);
1909 empathy_tp_chat_get_chat_state (EmpathyTpChat *chat,
1910 EmpathyContact *contact)
1912 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1913 return tp_channel_get_chat_state (priv->channel,
1914 empathy_contact_get_handle (contact));
1918 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1920 EmpathyTpChatPriv *priv = GET_PRIV (self);
1922 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);