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-dispatcher.h"
34 #include "empathy-marshal.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 gboolean listing_pending_messages;
51 /* Queue of messages not signalled yet */
52 GQueue *messages_queue;
53 /* Queue of messages signalled but not acked yet */
54 GQueue *pending_messages_queue;
55 gboolean had_properties_list;
56 GPtrArray *properties;
57 TpChannelPasswordFlags password_flags;
58 /* TRUE if we fetched the password flag of the channel or if it's not needed
59 * (channel doesn't implement the Password interface) */
60 gboolean got_password_flags;
62 gboolean can_upgrade_to_muc;
65 static void tp_chat_iface_init (EmpathyContactListIface *iface);
85 static guint signals[LAST_SIGNAL];
87 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
94 tp_chat_invalidated_cb (TpProxy *proxy,
100 DEBUG ("Channel invalidated: %s", message);
101 g_signal_emit (chat, signals[DESTROY], 0);
105 tp_chat_async_cb (TpChannel *proxy,
108 GObject *weak_object)
111 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
116 create_conference_cb (GObject *source,
117 GAsyncResult *result,
120 GError *error = NULL;
122 if (!tp_account_channel_request_create_channel_finish (
123 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
124 DEBUG ("Failed to create conference channel: %s", error->message);
125 g_error_free (error);
130 tp_chat_add (EmpathyContactList *list,
131 EmpathyContact *contact,
132 const gchar *message)
134 EmpathyTpChatPriv *priv = GET_PRIV (list);
136 if (tp_proxy_has_interface_by_id (priv->channel,
137 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
139 GArray handles = {(gchar *) &handle, 1};
141 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
142 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
144 handle = empathy_contact_get_handle (contact);
145 tp_cli_channel_interface_group_call_add_members (priv->channel,
146 -1, &handles, NULL, NULL, NULL, NULL, NULL);
147 } else if (priv->can_upgrade_to_muc) {
148 TpAccountChannelRequest *req;
150 const char *object_path;
151 GPtrArray channels = { (gpointer *) &object_path, 1 };
152 const char *invitees[2] = { NULL, };
154 invitees[0] = empathy_contact_get_id (contact);
155 object_path = tp_proxy_get_object_path (priv->channel);
158 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
159 TP_IFACE_CHANNEL_TYPE_TEXT,
160 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT,
162 EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialChannels",
163 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
164 EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialInviteeIDs",
165 G_TYPE_STRV, invitees,
166 /* FIXME: InvitationMessage ? */
169 req = tp_account_channel_request_new (priv->account, props,
170 TP_USER_ACTION_TIME_NOT_USER_ACTION);
172 /* Although this is a MUC, it's anonymous, so CreateChannel is
174 tp_account_channel_request_create_channel_async (req, NULL, NULL,
175 create_conference_cb, NULL);
177 g_object_unref (req);
178 g_hash_table_unref (props);
180 g_warning ("Cannot add to this channel");
185 tp_chat_remove (EmpathyContactList *list,
186 EmpathyContact *contact,
187 const gchar *message)
189 EmpathyTpChatPriv *priv = GET_PRIV (list);
191 GArray handles = {(gchar *) &handle, 1};
193 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
194 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
196 handle = empathy_contact_get_handle (contact);
197 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
204 tp_chat_get_members (EmpathyContactList *list)
206 EmpathyTpChatPriv *priv = GET_PRIV (list);
207 GList *members = NULL;
209 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
212 members = g_list_copy (priv->members);
213 g_list_foreach (members, (GFunc) g_object_ref, NULL);
215 members = g_list_prepend (members, g_object_ref (priv->user));
216 if (priv->remote_contact != NULL)
217 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
224 check_ready (EmpathyTpChat *chat)
226 EmpathyTpChatPriv *priv = GET_PRIV (chat);
231 if (g_queue_get_length (priv->messages_queue) > 0)
237 g_object_notify (G_OBJECT (chat), "ready");
241 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
243 EmpathyTpChatPriv *priv = GET_PRIV (chat);
244 EmpathyMessage *message;
246 /* Check if we can now emit some queued messages */
247 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
248 if (empathy_message_get_sender (message) == NULL) {
252 DEBUG ("Queued message ready");
253 g_queue_pop_head (priv->messages_queue);
254 g_queue_push_tail (priv->pending_messages_queue, message);
255 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
262 tp_chat_got_sender_cb (TpConnection *connection,
263 EmpathyContact *contact,
268 EmpathyTpChatPriv *priv = GET_PRIV (chat);
271 DEBUG ("Error: %s", error->message);
272 /* Do not block the message queue, just drop this message */
273 g_queue_remove (priv->messages_queue, message);
275 empathy_message_set_sender (message, contact);
278 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
282 tp_chat_build_message (EmpathyTpChat *chat,
288 const gchar *message_body,
289 TpChannelTextMessageFlags flags)
291 EmpathyTpChatPriv *priv;
292 EmpathyMessage *message;
294 priv = GET_PRIV (chat);
296 message = empathy_message_new (message_body);
297 empathy_message_set_tptype (message, type);
298 empathy_message_set_receiver (message, priv->user);
299 empathy_message_set_timestamp (message, timestamp);
300 empathy_message_set_id (message, id);
301 empathy_message_set_incoming (message, incoming);
302 empathy_message_set_flags (message, flags);
304 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK)
305 empathy_message_set_is_backlog (message, TRUE);
307 g_queue_push_tail (priv->messages_queue, message);
309 if (from_handle == 0) {
310 empathy_message_set_sender (message, priv->user);
311 tp_chat_emit_queued_messages (chat);
313 empathy_tp_contact_factory_get_from_handle (priv->connection,
315 tp_chat_got_sender_cb,
316 message, NULL, G_OBJECT (chat));
321 tp_chat_received_cb (TpChannel *channel,
327 const gchar *message_body,
331 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
332 EmpathyTpChatPriv *priv = GET_PRIV (chat);
334 if (priv->channel == NULL)
337 if (priv->listing_pending_messages) {
341 DEBUG ("Message received: %s", message_body);
343 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
344 !tp_strdiff (message_body, "")) {
347 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
349 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
350 g_array_append_val (ids, message_id);
351 acknowledge_messages (chat, ids);
352 g_array_free (ids, TRUE);
357 tp_chat_build_message (chat,
368 tp_chat_sent_cb (TpChannel *channel,
371 const gchar *message_body,
375 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
376 EmpathyTpChatPriv *priv = GET_PRIV (chat);
378 if (priv->channel == NULL)
381 DEBUG ("Message sent: %s", message_body);
383 tp_chat_build_message (chat,
394 tp_chat_send_error_cb (TpChannel *channel,
398 const gchar *message_body,
402 EmpathyTpChatPriv *priv = GET_PRIV (chat);
404 if (priv->channel == NULL)
407 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
409 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
413 tp_chat_send_cb (TpChannel *proxy,
418 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
421 DEBUG ("Error: %s", error->message);
422 g_signal_emit (chat, signals[SEND_ERROR], 0,
423 empathy_message_get_body (message),
424 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
430 TpChannelChatState state;
434 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
435 EmpathyContact *contact,
440 TpChannelChatState state;
443 DEBUG ("Error: %s", error->message);
447 state = GPOINTER_TO_UINT (user_data);
448 DEBUG ("Chat state changed for %s (%d): %d",
449 empathy_contact_get_alias (contact),
450 empathy_contact_get_handle (contact), state);
452 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
456 tp_chat_state_changed_cb (TpChannel *channel,
458 TpChannelChatState state,
462 EmpathyTpChatPriv *priv = GET_PRIV (chat);
464 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
465 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
470 tp_chat_list_pending_messages_cb (TpChannel *channel,
471 const GPtrArray *messages_list,
476 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
477 EmpathyTpChatPriv *priv = GET_PRIV (chat);
479 GArray *empty_non_text_content_ids = NULL;
481 priv->listing_pending_messages = FALSE;
483 if (priv->channel == NULL)
487 DEBUG ("Error listing pending messages: %s", error->message);
491 for (i = 0; i < messages_list->len; i++) {
492 GValueArray *message_struct;
493 const gchar *message_body;
500 message_struct = g_ptr_array_index (messages_list, i);
502 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
503 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
504 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
505 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
506 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
507 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
509 DEBUG ("Message pending: %s", message_body);
511 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
512 !tp_strdiff (message_body, "")) {
513 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
515 if (empty_non_text_content_ids == NULL) {
516 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
519 g_array_append_val (empty_non_text_content_ids, message_id);
523 tp_chat_build_message (chat,
533 if (empty_non_text_content_ids != NULL) {
534 acknowledge_messages (chat, empty_non_text_content_ids);
535 g_array_free (empty_non_text_content_ids, TRUE);
542 tp_chat_property_flags_changed_cb (TpProxy *proxy,
543 const GPtrArray *properties,
547 EmpathyTpChatPriv *priv = GET_PRIV (chat);
550 if (priv->channel == NULL)
553 if (!priv->had_properties_list || !properties) {
557 for (i = 0; i < properties->len; i++) {
558 GValueArray *prop_struct;
559 EmpathyTpChatProperty *property;
563 prop_struct = g_ptr_array_index (properties, i);
564 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
565 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
567 for (j = 0; j < priv->properties->len; j++) {
568 property = g_ptr_array_index (priv->properties, j);
569 if (property->id == id) {
570 property->flags = flags;
571 DEBUG ("property %s flags changed: %d",
572 property->name, property->flags);
580 tp_chat_properties_changed_cb (TpProxy *proxy,
581 const GPtrArray *properties,
585 EmpathyTpChatPriv *priv = GET_PRIV (chat);
588 if (priv->channel == NULL)
591 if (!priv->had_properties_list || !properties) {
595 for (i = 0; i < properties->len; i++) {
596 GValueArray *prop_struct;
597 EmpathyTpChatProperty *property;
601 prop_struct = g_ptr_array_index (properties, i);
602 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
603 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
605 for (j = 0; j < priv->properties->len; j++) {
606 property = g_ptr_array_index (priv->properties, j);
607 if (property->id == id) {
608 if (property->value) {
609 g_value_copy (src_value, property->value);
611 property->value = tp_g_value_slice_dup (src_value);
614 DEBUG ("property %s changed", property->name);
615 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
616 property->name, property->value);
624 tp_chat_get_properties_cb (TpProxy *proxy,
625 const GPtrArray *properties,
631 DEBUG ("Error getting properties: %s", error->message);
635 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
639 tp_chat_list_properties_cb (TpProxy *proxy,
640 const GPtrArray *properties,
645 EmpathyTpChatPriv *priv = GET_PRIV (chat);
649 if (priv->channel == NULL)
652 priv->had_properties_list = TRUE;
655 DEBUG ("Error listing properties: %s", error->message);
659 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
660 priv->properties = g_ptr_array_sized_new (properties->len);
661 for (i = 0; i < properties->len; i++) {
662 GValueArray *prop_struct;
663 EmpathyTpChatProperty *property;
665 prop_struct = g_ptr_array_index (properties, i);
666 property = g_slice_new0 (EmpathyTpChatProperty);
667 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
668 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
669 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
671 DEBUG ("Adding property name=%s id=%d flags=%d",
672 property->name, property->id, property->flags);
673 g_ptr_array_add (priv->properties, property);
674 if (property->flags & TP_PROPERTY_FLAG_READ) {
675 g_array_append_val (ids, property->id);
679 tp_cli_properties_interface_call_get_properties (proxy, -1,
681 tp_chat_get_properties_cb,
685 g_array_free (ids, TRUE);
689 empathy_tp_chat_set_property (EmpathyTpChat *chat,
693 EmpathyTpChatPriv *priv = GET_PRIV (chat);
694 EmpathyTpChatProperty *property;
697 if (!priv->had_properties_list) {
701 for (i = 0; i < priv->properties->len; i++) {
702 property = g_ptr_array_index (priv->properties, i);
703 if (!tp_strdiff (property->name, name)) {
704 GPtrArray *properties;
707 GValue dest_value = {0, };
709 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
713 g_value_init (&id, G_TYPE_UINT);
714 g_value_init (&dest_value, G_TYPE_VALUE);
715 g_value_set_uint (&id, property->id);
716 g_value_set_boxed (&dest_value, value);
718 prop = g_value_array_new (2);
719 g_value_array_append (prop, &id);
720 g_value_array_append (prop, &dest_value);
722 properties = g_ptr_array_sized_new (1);
723 g_ptr_array_add (properties, prop);
725 DEBUG ("Set property %s", name);
726 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
728 (tp_cli_properties_interface_callback_for_set_properties)
730 "Seting property", NULL,
733 g_ptr_array_free (properties, TRUE);
734 g_value_array_free (prop);
741 EmpathyTpChatProperty *
742 empathy_tp_chat_get_property (EmpathyTpChat *chat,
745 EmpathyTpChatPriv *priv = GET_PRIV (chat);
746 EmpathyTpChatProperty *property;
749 if (!priv->had_properties_list) {
753 for (i = 0; i < priv->properties->len; i++) {
754 property = g_ptr_array_index (priv->properties, i);
755 if (!tp_strdiff (property->name, name)) {
764 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
766 EmpathyTpChatPriv *priv = GET_PRIV (chat);
768 return priv->properties;
772 tp_chat_dispose (GObject *object)
774 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
775 EmpathyTpChatPriv *priv = GET_PRIV (self);
777 if (priv->dispose_has_run)
780 priv->dispose_has_run = TRUE;
782 tp_clear_object (&priv->account);
784 if (priv->connection != NULL)
785 g_object_unref (priv->connection);
786 priv->connection = NULL;
788 if (priv->channel != NULL) {
789 g_signal_handlers_disconnect_by_func (priv->channel,
790 tp_chat_invalidated_cb, self);
791 g_object_unref (priv->channel);
793 priv->channel = NULL;
795 if (priv->remote_contact != NULL)
796 g_object_unref (priv->remote_contact);
797 priv->remote_contact = NULL;
799 if (priv->user != NULL)
800 g_object_unref (priv->user);
803 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
804 g_queue_clear (priv->messages_queue);
806 g_queue_foreach (priv->pending_messages_queue,
807 (GFunc) g_object_unref, NULL);
808 g_queue_clear (priv->pending_messages_queue);
810 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
811 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
815 tp_chat_finalize (GObject *object)
817 EmpathyTpChatPriv *priv = GET_PRIV (object);
820 DEBUG ("Finalize: %p", object);
822 if (priv->properties) {
823 for (i = 0; i < priv->properties->len; i++) {
824 EmpathyTpChatProperty *property;
826 property = g_ptr_array_index (priv->properties, i);
827 g_free (property->name);
828 if (property->value) {
829 tp_g_value_slice_free (property->value);
831 g_slice_free (EmpathyTpChatProperty, property);
833 g_ptr_array_free (priv->properties, TRUE);
836 g_queue_free (priv->messages_queue);
837 g_queue_free (priv->pending_messages_queue);
839 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
843 check_almost_ready (EmpathyTpChat *chat)
845 EmpathyTpChatPriv *priv = GET_PRIV (chat);
850 if (priv->user == NULL)
853 if (!priv->got_password_flags)
856 /* We need either the members (room) or the remote contact (private chat).
857 * If the chat is protected by a password we can't get these information so
858 * consider the chat as ready so it can be presented to the user. */
859 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
860 priv->remote_contact == NULL)
863 tp_cli_channel_type_text_connect_to_received (priv->channel,
866 G_OBJECT (chat), NULL);
867 priv->listing_pending_messages = TRUE;
869 /* TpChat will be ready once ListPendingMessages returned and all the messages
870 * have been added to the pending messages queue. */
871 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
873 tp_chat_list_pending_messages_cb,
877 tp_cli_channel_type_text_connect_to_sent (priv->channel,
880 G_OBJECT (chat), NULL);
881 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
882 tp_chat_send_error_cb,
884 G_OBJECT (chat), NULL);
885 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
886 tp_chat_state_changed_cb,
888 G_OBJECT (chat), NULL);
892 tp_chat_update_remote_contact (EmpathyTpChat *chat)
894 EmpathyTpChatPriv *priv = GET_PRIV (chat);
895 EmpathyContact *contact = NULL;
896 TpHandle self_handle;
897 TpHandleType handle_type;
900 /* If this is a named chatroom, never pretend it is a private chat */
901 tp_channel_get_handle (priv->channel, &handle_type);
902 if (handle_type == TP_HANDLE_TYPE_ROOM) {
906 /* This is an MSN chat, but it's the new style where 1-1 chats don't
907 * have the group interface. If it has the conference interface, then
908 * it is indeed a MUC. */
909 if (tp_proxy_has_interface_by_id (priv->channel,
910 EMP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
914 /* This is an MSN-like chat where anyone can join the chat at anytime.
915 * If there is only one non-self contact member, we are in a private
916 * chat and we set the "remote-contact" property to that contact. If
917 * there are more, set the "remote-contact" property to NULL and the
918 * UI will display a contact list. */
919 self_handle = tp_channel_group_get_self_handle (priv->channel);
920 for (l = priv->members; l; l = l->next) {
921 /* Skip self contact if member */
922 if (empathy_contact_get_handle (l->data) == self_handle) {
926 /* We have more than one remote contact, break */
927 if (contact != NULL) {
932 /* If we didn't find yet a remote contact, keep this one */
936 if (priv->remote_contact == contact) {
940 DEBUG ("Changing remote contact from %p to %p",
941 priv->remote_contact, contact);
943 if (priv->remote_contact) {
944 g_object_unref (priv->remote_contact);
947 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
948 g_object_notify (G_OBJECT (chat), "remote-contact");
952 tp_chat_got_added_contacts_cb (TpConnection *connection,
954 EmpathyContact * const * contacts,
956 const TpHandle *failed,
961 EmpathyTpChatPriv *priv = GET_PRIV (chat);
963 const TpIntSet *members;
965 EmpathyContact *contact;
968 DEBUG ("Error: %s", error->message);
972 members = tp_channel_group_get_members (priv->channel);
973 for (i = 0; i < n_contacts; i++) {
974 contact = contacts[i];
975 handle = empathy_contact_get_handle (contact);
977 /* Make sure the contact is still member */
978 if (tp_intset_is_member (members, handle)) {
979 priv->members = g_list_prepend (priv->members,
980 g_object_ref (contact));
981 g_signal_emit_by_name (chat, "members-changed",
982 contact, NULL, 0, NULL, TRUE);
986 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
987 check_almost_ready (EMPATHY_TP_CHAT (chat));
990 static EmpathyContact *
991 chat_lookup_contact (EmpathyTpChat *chat,
995 EmpathyTpChatPriv *priv = GET_PRIV (chat);
998 for (l = priv->members; l; l = l->next) {
999 EmpathyContact *c = l->data;
1001 if (empathy_contact_get_handle (c) != handle) {
1006 /* Caller takes the reference. */
1007 priv->members = g_list_delete_link (priv->members, l);
1020 TpHandle old_handle;
1023 } ContactRenameData;
1025 static ContactRenameData *
1026 contact_rename_data_new (TpHandle handle,
1028 const gchar* message)
1030 ContactRenameData *data = g_new (ContactRenameData, 1);
1031 data->old_handle = handle;
1032 data->reason = reason;
1033 data->message = g_strdup (message);
1039 contact_rename_data_free (ContactRenameData* data)
1041 g_free (data->message);
1046 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1048 EmpathyContact * const * contacts,
1050 const TpHandle *failed,
1051 const GError *error,
1055 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1056 const TpIntSet *members;
1058 EmpathyContact *old = NULL, *new = NULL;
1059 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1062 DEBUG ("Error: %s", error->message);
1066 /* renamed members can only be delivered one at a time */
1067 g_warn_if_fail (n_contacts == 1);
1071 members = tp_channel_group_get_members (priv->channel);
1072 handle = empathy_contact_get_handle (new);
1074 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1075 rename_data->old_handle, TRUE);
1077 /* Make sure the contact is still member */
1078 if (tp_intset_is_member (members, handle)) {
1079 priv->members = g_list_prepend (priv->members,
1080 g_object_ref (new));
1083 g_signal_emit_by_name (chat, "member-renamed",
1084 old, new, rename_data->reason,
1085 rename_data->message);
1086 g_object_unref (old);
1090 if (priv->user == old) {
1091 /* We change our nick */
1092 g_object_unref (priv->user);
1093 priv->user = g_object_ref (new);
1096 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1097 check_almost_ready (EMPATHY_TP_CHAT (chat));
1102 tp_chat_group_members_changed_cb (TpChannel *self,
1106 GArray *local_pending,
1107 GArray *remote_pending,
1110 EmpathyTpChat *chat)
1112 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1113 EmpathyContact *contact;
1114 EmpathyContact *actor_contact = NULL;
1116 ContactRenameData *rename_data;
1117 TpHandle old_handle;
1119 /* Contact renamed */
1120 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1121 /* there can only be a single 'added' and a single 'removed' handle */
1122 if (removed->len != 1 || added->len != 1) {
1123 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1124 added->len, removed->len);
1128 old_handle = g_array_index (removed, guint, 0);
1130 rename_data = contact_rename_data_new (old_handle, reason, message);
1131 empathy_tp_contact_factory_get_from_handles (priv->connection,
1132 added->len, (TpHandle *) added->data,
1133 tp_chat_got_renamed_contacts_cb,
1134 rename_data, (GDestroyNotify) contact_rename_data_free,
1140 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1141 if (actor_contact == NULL) {
1142 /* FIXME: handle this a tad more gracefully: perhaps
1143 * the actor was a server op. We could use the
1144 * contact-ids detail of MembersChangedDetailed.
1146 DEBUG ("actor %u not a channel member", actor);
1150 /* Remove contacts that are not members anymore */
1151 for (i = 0; i < removed->len; i++) {
1152 contact = chat_lookup_contact (chat,
1153 g_array_index (removed, TpHandle, i), TRUE);
1155 if (contact != NULL) {
1156 g_signal_emit_by_name (chat, "members-changed", contact,
1157 actor_contact, reason, message,
1159 g_object_unref (contact);
1163 /* Request added contacts */
1164 if (added->len > 0) {
1165 empathy_tp_contact_factory_get_from_handles (priv->connection,
1166 added->len, (TpHandle *) added->data,
1167 tp_chat_got_added_contacts_cb, NULL, NULL,
1171 tp_chat_update_remote_contact (chat);
1173 if (actor_contact != NULL) {
1174 g_object_unref (actor_contact);
1179 tp_chat_got_remote_contact_cb (TpConnection *connection,
1180 EmpathyContact *contact,
1181 const GError *error,
1185 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1188 DEBUG ("Error: %s", error->message);
1189 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
1193 priv->remote_contact = g_object_ref (contact);
1194 g_object_notify (chat, "remote-contact");
1196 check_almost_ready (EMPATHY_TP_CHAT (chat));
1200 tp_chat_got_self_contact_cb (TpConnection *connection,
1201 EmpathyContact *contact,
1202 const GError *error,
1206 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1209 DEBUG ("Error: %s", error->message);
1210 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
1214 priv->user = g_object_ref (contact);
1215 empathy_contact_set_is_user (priv->user, TRUE);
1216 check_almost_ready (EMPATHY_TP_CHAT (chat));
1220 password_flags_changed_cb (TpChannel *channel,
1224 GObject *weak_object)
1226 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1227 EmpathyTpChatPriv *priv = GET_PRIV (self);
1228 gboolean was_needed, needed;
1230 was_needed = empathy_tp_chat_password_needed (self);
1232 priv->password_flags |= added;
1233 priv->password_flags ^= removed;
1235 needed = empathy_tp_chat_password_needed (self);
1237 if (was_needed != needed)
1238 g_object_notify (G_OBJECT (self), "password-needed");
1242 got_password_flags_cb (TpChannel *proxy,
1243 guint password_flags,
1244 const GError *error,
1246 GObject *weak_object)
1248 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1249 EmpathyTpChatPriv *priv = GET_PRIV (self);
1251 priv->got_password_flags = TRUE;
1252 priv->password_flags = password_flags;
1254 check_almost_ready (EMPATHY_TP_CHAT (self));
1258 tp_chat_constructor (GType type,
1260 GObjectConstructParam *props)
1263 EmpathyTpChatPriv *priv;
1266 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1268 priv = GET_PRIV (chat);
1270 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1271 tp_g_signal_connect_object (priv->channel, "invalidated",
1272 G_CALLBACK (tp_chat_invalidated_cb),
1275 if (tp_proxy_has_interface_by_id (priv->channel,
1276 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1277 const TpIntSet *members;
1280 /* Get self contact from the group's self handle */
1281 handle = tp_channel_group_get_self_handle (priv->channel);
1282 empathy_tp_contact_factory_get_from_handle (priv->connection,
1283 handle, tp_chat_got_self_contact_cb,
1286 /* Get initial member contacts */
1287 members = tp_channel_group_get_members (priv->channel);
1288 handles = tp_intset_to_array (members);
1289 empathy_tp_contact_factory_get_from_handles (priv->connection,
1290 handles->len, (TpHandle *) handles->data,
1291 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1293 priv->can_upgrade_to_muc = FALSE;
1295 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1296 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1298 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1301 /* Get the self contact from the connection's self handle */
1302 handle = tp_connection_get_self_handle (priv->connection);
1303 empathy_tp_contact_factory_get_from_handle (priv->connection,
1304 handle, tp_chat_got_self_contact_cb,
1307 /* Get the remote contact */
1308 handle = tp_channel_get_handle (priv->channel, NULL);
1309 empathy_tp_contact_factory_get_from_handle (priv->connection,
1310 handle, tp_chat_got_remote_contact_cb,
1313 list = empathy_dispatcher_find_requestable_channel_classes (
1314 dispatcher, priv->connection,
1315 tp_channel_get_channel_type (priv->channel),
1316 TP_UNKNOWN_HANDLE_TYPE, NULL);
1318 for (ptr = list; ptr; ptr = ptr->next) {
1319 GValueArray *array = ptr->data;
1320 const char **oprops = g_value_get_boxed (
1321 g_value_array_get_nth (array, 1));
1323 if (tp_strv_contains (oprops, EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialChannels")) {
1324 priv->can_upgrade_to_muc = TRUE;
1330 g_object_unref (dispatcher);
1333 if (tp_proxy_has_interface_by_id (priv->channel,
1334 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1335 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1336 tp_chat_list_properties_cb,
1339 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1340 tp_chat_properties_changed_cb,
1342 G_OBJECT (chat), NULL);
1343 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1344 tp_chat_property_flags_changed_cb,
1346 G_OBJECT (chat), NULL);
1349 /* Check if the chat is password protected */
1350 if (tp_proxy_has_interface_by_id (priv->channel,
1351 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1352 priv->got_password_flags = FALSE;
1354 tp_cli_channel_interface_password_connect_to_password_flags_changed
1355 (priv->channel, password_flags_changed_cb, chat, NULL,
1356 G_OBJECT (chat), NULL);
1358 tp_cli_channel_interface_password_call_get_password_flags
1359 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1361 /* No Password interface, so no need to fetch the password flags */
1362 priv->got_password_flags = TRUE;
1369 tp_chat_get_property (GObject *object,
1374 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1375 EmpathyTpChatPriv *priv = GET_PRIV (object);
1379 g_value_set_object (value, priv->account);
1382 g_value_set_object (value, priv->channel);
1384 case PROP_REMOTE_CONTACT:
1385 g_value_set_object (value, priv->remote_contact);
1388 g_value_set_boolean (value, priv->ready);
1390 case PROP_PASSWORD_NEEDED:
1391 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1400 tp_chat_set_property (GObject *object,
1402 const GValue *value,
1405 EmpathyTpChatPriv *priv = GET_PRIV (object);
1409 priv->account = g_value_dup_object (value);
1412 priv->channel = g_value_dup_object (value);
1415 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1421 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1423 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1425 object_class->dispose = tp_chat_dispose;
1426 object_class->finalize = tp_chat_finalize;
1427 object_class->constructor = tp_chat_constructor;
1428 object_class->get_property = tp_chat_get_property;
1429 object_class->set_property = tp_chat_set_property;
1431 g_object_class_install_property (object_class,
1433 g_param_spec_object ("account",
1435 "the account associated with the chat",
1438 G_PARAM_CONSTRUCT_ONLY |
1439 G_PARAM_STATIC_STRINGS));
1441 g_object_class_install_property (object_class,
1443 g_param_spec_object ("channel",
1444 "telepathy channel",
1445 "The text channel for the chat",
1448 G_PARAM_CONSTRUCT_ONLY));
1450 g_object_class_install_property (object_class,
1451 PROP_REMOTE_CONTACT,
1452 g_param_spec_object ("remote-contact",
1453 "The remote contact",
1454 "The remote contact if there is no group iface on the channel",
1455 EMPATHY_TYPE_CONTACT,
1458 g_object_class_install_property (object_class,
1460 g_param_spec_boolean ("ready",
1461 "Is the object ready",
1462 "This object can't be used until this becomes true",
1466 g_object_class_install_property (object_class,
1467 PROP_PASSWORD_NEEDED,
1468 g_param_spec_boolean ("password-needed",
1470 "TRUE if a password is needed to join the channel",
1475 signals[MESSAGE_RECEIVED] =
1476 g_signal_new ("message-received",
1477 G_TYPE_FROM_CLASS (klass),
1481 g_cclosure_marshal_VOID__OBJECT,
1483 1, EMPATHY_TYPE_MESSAGE);
1485 signals[SEND_ERROR] =
1486 g_signal_new ("send-error",
1487 G_TYPE_FROM_CLASS (klass),
1491 _empathy_marshal_VOID__STRING_UINT,
1493 2, G_TYPE_STRING, G_TYPE_UINT);
1495 signals[CHAT_STATE_CHANGED] =
1496 g_signal_new ("chat-state-changed",
1497 G_TYPE_FROM_CLASS (klass),
1501 _empathy_marshal_VOID__OBJECT_UINT,
1503 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1505 signals[PROPERTY_CHANGED] =
1506 g_signal_new ("property-changed",
1507 G_TYPE_FROM_CLASS (klass),
1511 _empathy_marshal_VOID__STRING_BOXED,
1513 2, G_TYPE_STRING, G_TYPE_VALUE);
1516 g_signal_new ("destroy",
1517 G_TYPE_FROM_CLASS (klass),
1521 g_cclosure_marshal_VOID__VOID,
1525 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1529 empathy_tp_chat_init (EmpathyTpChat *chat)
1531 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1532 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1535 priv->messages_queue = g_queue_new ();
1536 priv->pending_messages_queue = g_queue_new ();
1540 tp_chat_iface_init (EmpathyContactListIface *iface)
1542 iface->add = tp_chat_add;
1543 iface->remove = tp_chat_remove;
1544 iface->get_members = tp_chat_get_members;
1548 empathy_tp_chat_new (TpAccount *account,
1551 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1552 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
1554 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1561 empathy_tp_chat_close (EmpathyTpChat *chat) {
1562 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1564 /* If there are still messages left, it'll come back..
1565 * We loose the ordering of sent messages though */
1566 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1567 "closing channel", NULL, NULL);
1571 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1573 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1577 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1579 id = tp_channel_get_identifier (priv->channel);
1580 if (!EMP_STR_EMPTY (id))
1582 else if (priv->remote_contact)
1583 return empathy_contact_get_id (priv->remote_contact);
1590 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1592 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1594 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1595 g_return_val_if_fail (priv->ready, NULL);
1597 return priv->remote_contact;
1601 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1603 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1605 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1607 return priv->channel;
1611 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1613 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1615 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1617 return priv->account;
1621 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1623 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1625 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1627 return tp_channel_borrow_connection (priv->channel);
1630 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1632 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1634 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1640 empathy_tp_chat_send (EmpathyTpChat *chat,
1641 EmpathyMessage *message)
1643 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1644 const gchar *message_body;
1645 TpChannelTextMessageType message_type;
1647 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1648 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1649 g_return_if_fail (priv->ready);
1651 message_body = empathy_message_get_body (message);
1652 message_type = empathy_message_get_tptype (message);
1654 DEBUG ("Sending message: %s", message_body);
1655 tp_cli_channel_type_text_call_send (priv->channel, -1,
1659 g_object_ref (message),
1660 (GDestroyNotify) g_object_unref,
1665 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1666 TpChannelChatState state)
1668 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1670 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1671 g_return_if_fail (priv->ready);
1673 if (tp_proxy_has_interface_by_id (priv->channel,
1674 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1675 DEBUG ("Set state: %d", state);
1676 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1679 "setting chat state",
1687 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1689 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1691 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1692 g_return_val_if_fail (priv->ready, NULL);
1694 return priv->pending_messages_queue->head;
1698 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1699 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1701 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1702 priv->channel, -1, ids, tp_chat_async_cb,
1703 "acknowledging received message", NULL, G_OBJECT (chat));
1707 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1708 EmpathyMessage *message) {
1709 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1710 GArray *message_ids;
1714 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1715 g_return_if_fail (priv->ready);
1717 if (!empathy_message_is_incoming (message))
1720 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1722 id = empathy_message_get_id (message);
1723 g_array_append_val (message_ids, id);
1724 acknowledge_messages (chat, message_ids);
1725 g_array_free (message_ids, TRUE);
1728 m = g_queue_find (priv->pending_messages_queue, message);
1729 g_assert (m != NULL);
1730 g_queue_delete_link (priv->pending_messages_queue, m);
1731 g_object_unref (message);
1735 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1736 const GSList *messages) {
1737 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1738 /* Copy messages as the messges list (probably is) our own */
1739 GSList *msgs = g_slist_copy ((GSList *) messages);
1742 GArray *message_ids;
1744 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1745 g_return_if_fail (priv->ready);
1747 length = g_slist_length ((GSList *) messages);
1752 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1754 for (l = msgs; l != NULL; l = g_slist_next (l)) {
1757 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1759 m = g_queue_find (priv->pending_messages_queue, message);
1760 g_assert (m != NULL);
1761 g_queue_delete_link (priv->pending_messages_queue, m);
1763 if (empathy_message_is_incoming (message)) {
1764 guint id = empathy_message_get_id (message);
1765 g_array_append_val (message_ids, id);
1767 g_object_unref (message);
1770 if (message_ids->len > 0)
1771 acknowledge_messages (chat, message_ids);
1773 g_array_free (message_ids, TRUE);
1774 g_slist_free (msgs);
1778 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
1780 empathy_tp_chat_acknowledge_messages (chat,
1781 (GSList *) empathy_tp_chat_get_pending_messages (chat));
1785 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1787 EmpathyTpChatPriv *priv = GET_PRIV (self);
1789 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1793 provide_password_cb (TpChannel *channel,
1795 const GError *error,
1797 GObject *weak_object)
1799 GSimpleAsyncResult *result = user_data;
1801 if (error != NULL) {
1802 g_simple_async_result_set_from_error (result, error);
1804 else if (!correct) {
1805 /* The current D-Bus API is a bit weird so re-use the
1806 * AuthenticationFailed error */
1807 g_simple_async_result_set_error (result, TP_ERRORS,
1808 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1811 g_simple_async_result_complete (result);
1812 g_object_unref (result);
1816 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1817 const gchar *password,
1818 GAsyncReadyCallback callback,
1821 EmpathyTpChatPriv *priv = GET_PRIV (self);
1822 GSimpleAsyncResult *result;
1824 result = g_simple_async_result_new (G_OBJECT (self),
1825 callback, user_data,
1826 empathy_tp_chat_provide_password_finish);
1828 tp_cli_channel_interface_password_call_provide_password
1829 (priv->channel, -1, password, provide_password_cb, result,
1830 NULL, G_OBJECT (self));
1834 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1835 GAsyncResult *result,
1838 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1842 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1843 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1849 * empathy_tp_chat_can_add_contact:
1851 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1852 * That is if this chat is a 1-to-1 channel that can be upgraded to
1853 * a MUC using the Conference interface or if the channel is a MUC.
1856 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1858 EmpathyTpChatPriv *priv;
1860 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1862 priv = GET_PRIV (self);
1864 return priv->can_upgrade_to_muc ||
1865 tp_proxy_has_interface_by_id (priv->channel,
1866 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1870 leave_remove_members_cb (TpChannel *proxy,
1871 const GError *error,
1873 GObject *weak_object)
1875 EmpathyTpChat *self = user_data;
1880 DEBUG ("RemoveMembers failed (%s); closing the channel", error->message);
1881 empathy_tp_chat_close (self);
1885 empathy_tp_chat_leave (EmpathyTpChat *self)
1887 EmpathyTpChatPriv *priv = GET_PRIV (self);
1888 TpHandle self_handle;
1891 if (!tp_proxy_has_interface_by_id (priv->channel,
1892 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1893 empathy_tp_chat_close (self);
1897 self_handle = tp_channel_group_get_self_handle (priv->channel);
1898 if (self_handle == 0) {
1899 /* we are not member of the channel */
1900 empathy_tp_chat_close (self);
1904 array = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1905 g_array_insert_val (array, 0, self_handle);
1907 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1, array,
1908 "", leave_remove_members_cb, self, NULL, G_OBJECT (self));
1910 g_array_free (array, TRUE);
1914 add_members_cb (TpChannel *proxy,
1915 const GError *error,
1917 GObject *weak_object)
1919 EmpathyTpChatPriv *priv = GET_PRIV (weak_object);
1921 if (error != NULL) {
1922 DEBUG ("Failed to join chat (%s): %s",
1923 tp_channel_get_identifier (priv->channel), error->message);
1928 empathy_tp_chat_join (EmpathyTpChat *self)
1930 EmpathyTpChatPriv *priv = GET_PRIV (self);
1931 TpHandle self_handle;
1934 self_handle = tp_channel_group_get_self_handle (priv->channel);
1936 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1937 g_array_append_val (members, self_handle);
1939 tp_cli_channel_interface_group_call_add_members (priv->channel, -1, members,
1940 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1942 g_array_free (members, TRUE);
1946 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1949 EmpathyTpChatPriv *priv = GET_PRIV (self);
1950 TpHandle self_handle;
1952 if (!tp_proxy_has_interface (priv->channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1955 self_handle = tp_channel_group_get_self_handle (priv->channel);
1956 if (self_handle == 0)
1959 return tp_channel_group_get_local_pending_info (priv->channel, self_handle,
1960 inviter, NULL, NULL);