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-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
42 gboolean dispose_has_run;
44 TpConnection *connection;
46 EmpathyContact *remote_contact;
49 gboolean listing_pending_messages;
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, NULL, NULL,
174 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 tp_chat_received_cb (TpChannel *channel,
326 const gchar *message_body,
330 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
331 EmpathyTpChatPriv *priv = GET_PRIV (chat);
333 if (priv->channel == NULL)
336 if (priv->listing_pending_messages) {
340 DEBUG ("Message received: %s", message_body);
342 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
343 !tp_strdiff (message_body, "")) {
346 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
348 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
349 g_array_append_val (ids, message_id);
350 acknowledge_messages (chat, ids);
351 g_array_free (ids, TRUE);
356 tp_chat_build_message (chat,
367 tp_chat_sent_cb (TpChannel *channel,
370 const gchar *message_body,
374 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
375 EmpathyTpChatPriv *priv = GET_PRIV (chat);
377 if (priv->channel == NULL)
380 DEBUG ("Message sent: %s", message_body);
382 tp_chat_build_message (chat,
393 tp_chat_send_error_cb (TpChannel *channel,
397 const gchar *message_body,
401 EmpathyTpChatPriv *priv = GET_PRIV (chat);
403 if (priv->channel == NULL)
406 DEBUG ("Error sending '%s' (%d)", message_body, error_code);
408 g_signal_emit (chat, signals[SEND_ERROR], 0, message_body, error_code);
412 tp_chat_send_cb (TpChannel *proxy,
417 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
420 DEBUG ("Error: %s", error->message);
421 g_signal_emit (chat, signals[SEND_ERROR], 0,
422 empathy_message_get_body (message),
423 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
429 TpChannelChatState state;
433 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
434 EmpathyContact *contact,
439 TpChannelChatState state;
442 DEBUG ("Error: %s", error->message);
446 state = GPOINTER_TO_UINT (user_data);
447 DEBUG ("Chat state changed for %s (%d): %d",
448 empathy_contact_get_alias (contact),
449 empathy_contact_get_handle (contact), state);
451 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
455 tp_chat_state_changed_cb (TpChannel *channel,
457 TpChannelChatState state,
461 EmpathyTpChatPriv *priv = GET_PRIV (chat);
463 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
464 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
469 tp_chat_list_pending_messages_cb (TpChannel *channel,
470 const GPtrArray *messages_list,
475 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
476 EmpathyTpChatPriv *priv = GET_PRIV (chat);
478 GArray *empty_non_text_content_ids = NULL;
480 priv->listing_pending_messages = FALSE;
482 if (priv->channel == NULL)
486 DEBUG ("Error listing pending messages: %s", error->message);
490 for (i = 0; i < messages_list->len; i++) {
491 GValueArray *message_struct;
492 const gchar *message_body;
499 message_struct = g_ptr_array_index (messages_list, i);
501 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
502 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
503 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
504 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
505 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
506 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
508 DEBUG ("Message pending: %s", message_body);
510 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
511 !tp_strdiff (message_body, "")) {
512 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
514 if (empty_non_text_content_ids == NULL) {
515 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
518 g_array_append_val (empty_non_text_content_ids, message_id);
522 tp_chat_build_message (chat,
532 if (empty_non_text_content_ids != NULL) {
533 acknowledge_messages (chat, empty_non_text_content_ids);
534 g_array_free (empty_non_text_content_ids, TRUE);
541 tp_chat_property_flags_changed_cb (TpProxy *proxy,
542 const GPtrArray *properties,
546 EmpathyTpChatPriv *priv = GET_PRIV (chat);
549 if (priv->channel == NULL)
552 if (!priv->had_properties_list || !properties) {
556 for (i = 0; i < properties->len; i++) {
557 GValueArray *prop_struct;
558 EmpathyTpChatProperty *property;
562 prop_struct = g_ptr_array_index (properties, i);
563 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
564 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
566 for (j = 0; j < priv->properties->len; j++) {
567 property = g_ptr_array_index (priv->properties, j);
568 if (property->id == id) {
569 property->flags = flags;
570 DEBUG ("property %s flags changed: %d",
571 property->name, property->flags);
579 tp_chat_properties_changed_cb (TpProxy *proxy,
580 const GPtrArray *properties,
584 EmpathyTpChatPriv *priv = GET_PRIV (chat);
587 if (priv->channel == NULL)
590 if (!priv->had_properties_list || !properties) {
594 for (i = 0; i < properties->len; i++) {
595 GValueArray *prop_struct;
596 EmpathyTpChatProperty *property;
600 prop_struct = g_ptr_array_index (properties, i);
601 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
602 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
604 for (j = 0; j < priv->properties->len; j++) {
605 property = g_ptr_array_index (priv->properties, j);
606 if (property->id == id) {
607 if (property->value) {
608 g_value_copy (src_value, property->value);
610 property->value = tp_g_value_slice_dup (src_value);
613 DEBUG ("property %s changed", property->name);
614 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
615 property->name, property->value);
623 tp_chat_get_properties_cb (TpProxy *proxy,
624 const GPtrArray *properties,
630 DEBUG ("Error getting properties: %s", error->message);
634 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
638 tp_chat_list_properties_cb (TpProxy *proxy,
639 const GPtrArray *properties,
644 EmpathyTpChatPriv *priv = GET_PRIV (chat);
648 if (priv->channel == NULL)
651 priv->had_properties_list = TRUE;
654 DEBUG ("Error listing properties: %s", error->message);
658 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
659 priv->properties = g_ptr_array_sized_new (properties->len);
660 for (i = 0; i < properties->len; i++) {
661 GValueArray *prop_struct;
662 EmpathyTpChatProperty *property;
664 prop_struct = g_ptr_array_index (properties, i);
665 property = g_slice_new0 (EmpathyTpChatProperty);
666 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
667 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
668 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
670 DEBUG ("Adding property name=%s id=%d flags=%d",
671 property->name, property->id, property->flags);
672 g_ptr_array_add (priv->properties, property);
673 if (property->flags & TP_PROPERTY_FLAG_READ) {
674 g_array_append_val (ids, property->id);
678 tp_cli_properties_interface_call_get_properties (proxy, -1,
680 tp_chat_get_properties_cb,
684 g_array_free (ids, TRUE);
688 empathy_tp_chat_set_property (EmpathyTpChat *chat,
692 EmpathyTpChatPriv *priv = GET_PRIV (chat);
693 EmpathyTpChatProperty *property;
696 if (!priv->had_properties_list) {
700 for (i = 0; i < priv->properties->len; i++) {
701 property = g_ptr_array_index (priv->properties, i);
702 if (!tp_strdiff (property->name, name)) {
703 GPtrArray *properties;
706 GValue dest_value = {0, };
708 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
712 g_value_init (&id, G_TYPE_UINT);
713 g_value_init (&dest_value, G_TYPE_VALUE);
714 g_value_set_uint (&id, property->id);
715 g_value_set_boxed (&dest_value, value);
717 prop = g_value_array_new (2);
718 g_value_array_append (prop, &id);
719 g_value_array_append (prop, &dest_value);
721 properties = g_ptr_array_sized_new (1);
722 g_ptr_array_add (properties, prop);
724 DEBUG ("Set property %s", name);
725 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
727 (tp_cli_properties_interface_callback_for_set_properties)
729 "Seting property", NULL,
732 g_ptr_array_free (properties, TRUE);
733 g_value_array_free (prop);
740 EmpathyTpChatProperty *
741 empathy_tp_chat_get_property (EmpathyTpChat *chat,
744 EmpathyTpChatPriv *priv = GET_PRIV (chat);
745 EmpathyTpChatProperty *property;
748 if (!priv->had_properties_list) {
752 for (i = 0; i < priv->properties->len; i++) {
753 property = g_ptr_array_index (priv->properties, i);
754 if (!tp_strdiff (property->name, name)) {
763 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
765 EmpathyTpChatPriv *priv = GET_PRIV (chat);
767 return priv->properties;
771 tp_chat_dispose (GObject *object)
773 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
774 EmpathyTpChatPriv *priv = GET_PRIV (self);
776 if (priv->dispose_has_run)
779 priv->dispose_has_run = TRUE;
781 tp_clear_object (&priv->account);
783 if (priv->connection != NULL)
784 g_object_unref (priv->connection);
785 priv->connection = NULL;
787 if (priv->channel != NULL) {
788 g_signal_handlers_disconnect_by_func (priv->channel,
789 tp_chat_invalidated_cb, self);
790 g_object_unref (priv->channel);
792 priv->channel = NULL;
794 if (priv->remote_contact != NULL)
795 g_object_unref (priv->remote_contact);
796 priv->remote_contact = NULL;
798 if (priv->user != NULL)
799 g_object_unref (priv->user);
802 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
803 g_queue_clear (priv->messages_queue);
805 g_queue_foreach (priv->pending_messages_queue,
806 (GFunc) g_object_unref, NULL);
807 g_queue_clear (priv->pending_messages_queue);
809 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
810 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
814 tp_chat_finalize (GObject *object)
816 EmpathyTpChatPriv *priv = GET_PRIV (object);
819 DEBUG ("Finalize: %p", object);
821 if (priv->properties) {
822 for (i = 0; i < priv->properties->len; i++) {
823 EmpathyTpChatProperty *property;
825 property = g_ptr_array_index (priv->properties, i);
826 g_free (property->name);
827 if (property->value) {
828 tp_g_value_slice_free (property->value);
830 g_slice_free (EmpathyTpChatProperty, property);
832 g_ptr_array_free (priv->properties, TRUE);
835 g_queue_free (priv->messages_queue);
836 g_queue_free (priv->pending_messages_queue);
838 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
842 check_almost_ready (EmpathyTpChat *chat)
844 EmpathyTpChatPriv *priv = GET_PRIV (chat);
849 if (priv->user == NULL)
852 if (!priv->got_password_flags)
855 /* We need either the members (room) or the remote contact (private chat).
856 * If the chat is protected by a password we can't get these information so
857 * consider the chat as ready so it can be presented to the user. */
858 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
859 priv->remote_contact == NULL)
862 tp_cli_channel_type_text_connect_to_received (priv->channel,
865 G_OBJECT (chat), NULL);
866 priv->listing_pending_messages = TRUE;
868 /* TpChat will be ready once ListPendingMessages returned and all the messages
869 * have been added to the pending messages queue. */
870 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
872 tp_chat_list_pending_messages_cb,
876 tp_cli_channel_type_text_connect_to_sent (priv->channel,
879 G_OBJECT (chat), NULL);
880 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
881 tp_chat_send_error_cb,
883 G_OBJECT (chat), NULL);
884 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
885 tp_chat_state_changed_cb,
887 G_OBJECT (chat), NULL);
891 tp_chat_update_remote_contact (EmpathyTpChat *chat)
893 EmpathyTpChatPriv *priv = GET_PRIV (chat);
894 EmpathyContact *contact = NULL;
895 TpHandle self_handle;
896 TpHandleType handle_type;
899 /* If this is a named chatroom, never pretend it is a private chat */
900 tp_channel_get_handle (priv->channel, &handle_type);
901 if (handle_type == TP_HANDLE_TYPE_ROOM) {
905 /* This is an MSN chat, but it's the new style where 1-1 chats don't
906 * have the group interface. If it has the conference interface, then
907 * it is indeed a MUC. */
908 if (tp_proxy_has_interface_by_id (priv->channel,
909 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
913 /* This is an MSN-like chat where anyone can join the chat at anytime.
914 * If there is only one non-self contact member, we are in a private
915 * chat and we set the "remote-contact" property to that contact. If
916 * there are more, set the "remote-contact" property to NULL and the
917 * UI will display a contact list. */
918 self_handle = tp_channel_group_get_self_handle (priv->channel);
919 for (l = priv->members; l; l = l->next) {
920 /* Skip self contact if member */
921 if (empathy_contact_get_handle (l->data) == self_handle) {
925 /* We have more than one remote contact, break */
926 if (contact != NULL) {
931 /* If we didn't find yet a remote contact, keep this one */
935 if (priv->remote_contact == contact) {
939 DEBUG ("Changing remote contact from %p to %p",
940 priv->remote_contact, contact);
942 if (priv->remote_contact) {
943 g_object_unref (priv->remote_contact);
946 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
947 g_object_notify (G_OBJECT (chat), "remote-contact");
951 tp_chat_got_added_contacts_cb (TpConnection *connection,
953 EmpathyContact * const * contacts,
955 const TpHandle *failed,
960 EmpathyTpChatPriv *priv = GET_PRIV (chat);
962 const TpIntSet *members;
964 EmpathyContact *contact;
967 DEBUG ("Error: %s", error->message);
971 members = tp_channel_group_get_members (priv->channel);
972 for (i = 0; i < n_contacts; i++) {
973 contact = contacts[i];
974 handle = empathy_contact_get_handle (contact);
976 /* Make sure the contact is still member */
977 if (tp_intset_is_member (members, handle)) {
978 priv->members = g_list_prepend (priv->members,
979 g_object_ref (contact));
980 g_signal_emit_by_name (chat, "members-changed",
981 contact, NULL, 0, NULL, TRUE);
985 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
986 check_almost_ready (EMPATHY_TP_CHAT (chat));
989 static EmpathyContact *
990 chat_lookup_contact (EmpathyTpChat *chat,
994 EmpathyTpChatPriv *priv = GET_PRIV (chat);
997 for (l = priv->members; l; l = l->next) {
998 EmpathyContact *c = l->data;
1000 if (empathy_contact_get_handle (c) != handle) {
1005 /* Caller takes the reference. */
1006 priv->members = g_list_delete_link (priv->members, l);
1019 TpHandle old_handle;
1022 } ContactRenameData;
1024 static ContactRenameData *
1025 contact_rename_data_new (TpHandle handle,
1027 const gchar* message)
1029 ContactRenameData *data = g_new (ContactRenameData, 1);
1030 data->old_handle = handle;
1031 data->reason = reason;
1032 data->message = g_strdup (message);
1038 contact_rename_data_free (ContactRenameData* data)
1040 g_free (data->message);
1045 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1047 EmpathyContact * const * contacts,
1049 const TpHandle *failed,
1050 const GError *error,
1054 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1055 const TpIntSet *members;
1057 EmpathyContact *old = NULL, *new = NULL;
1058 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1061 DEBUG ("Error: %s", error->message);
1065 /* renamed members can only be delivered one at a time */
1066 g_warn_if_fail (n_contacts == 1);
1070 members = tp_channel_group_get_members (priv->channel);
1071 handle = empathy_contact_get_handle (new);
1073 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1074 rename_data->old_handle, TRUE);
1076 /* Make sure the contact is still member */
1077 if (tp_intset_is_member (members, handle)) {
1078 priv->members = g_list_prepend (priv->members,
1079 g_object_ref (new));
1082 g_signal_emit_by_name (chat, "member-renamed",
1083 old, new, rename_data->reason,
1084 rename_data->message);
1085 g_object_unref (old);
1089 if (priv->user == old) {
1090 /* We change our nick */
1091 g_object_unref (priv->user);
1092 priv->user = g_object_ref (new);
1095 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1096 check_almost_ready (EMPATHY_TP_CHAT (chat));
1101 tp_chat_group_members_changed_cb (TpChannel *self,
1105 GArray *local_pending,
1106 GArray *remote_pending,
1109 EmpathyTpChat *chat)
1111 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1112 EmpathyContact *contact;
1113 EmpathyContact *actor_contact = NULL;
1115 ContactRenameData *rename_data;
1116 TpHandle old_handle;
1118 /* Contact renamed */
1119 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1120 /* there can only be a single 'added' and a single 'removed' handle */
1121 if (removed->len != 1 || added->len != 1) {
1122 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1123 added->len, removed->len);
1127 old_handle = g_array_index (removed, guint, 0);
1129 rename_data = contact_rename_data_new (old_handle, reason, message);
1130 empathy_tp_contact_factory_get_from_handles (priv->connection,
1131 added->len, (TpHandle *) added->data,
1132 tp_chat_got_renamed_contacts_cb,
1133 rename_data, (GDestroyNotify) contact_rename_data_free,
1139 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1140 if (actor_contact == NULL) {
1141 /* FIXME: handle this a tad more gracefully: perhaps
1142 * the actor was a server op. We could use the
1143 * contact-ids detail of MembersChangedDetailed.
1145 DEBUG ("actor %u not a channel member", actor);
1149 /* Remove contacts that are not members anymore */
1150 for (i = 0; i < removed->len; i++) {
1151 contact = chat_lookup_contact (chat,
1152 g_array_index (removed, TpHandle, i), TRUE);
1154 if (contact != NULL) {
1155 g_signal_emit_by_name (chat, "members-changed", contact,
1156 actor_contact, reason, message,
1158 g_object_unref (contact);
1162 /* Request added contacts */
1163 if (added->len > 0) {
1164 empathy_tp_contact_factory_get_from_handles (priv->connection,
1165 added->len, (TpHandle *) added->data,
1166 tp_chat_got_added_contacts_cb, NULL, NULL,
1170 tp_chat_update_remote_contact (chat);
1172 if (actor_contact != NULL) {
1173 g_object_unref (actor_contact);
1178 tp_chat_got_remote_contact_cb (TpConnection *connection,
1179 EmpathyContact *contact,
1180 const GError *error,
1184 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1187 DEBUG ("Error: %s", error->message);
1188 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
1192 priv->remote_contact = g_object_ref (contact);
1193 g_object_notify (chat, "remote-contact");
1195 check_almost_ready (EMPATHY_TP_CHAT (chat));
1199 tp_chat_got_self_contact_cb (TpConnection *connection,
1200 EmpathyContact *contact,
1201 const GError *error,
1205 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1208 DEBUG ("Error: %s", error->message);
1209 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat));
1213 priv->user = g_object_ref (contact);
1214 empathy_contact_set_is_user (priv->user, TRUE);
1215 check_almost_ready (EMPATHY_TP_CHAT (chat));
1219 password_flags_changed_cb (TpChannel *channel,
1223 GObject *weak_object)
1225 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1226 EmpathyTpChatPriv *priv = GET_PRIV (self);
1227 gboolean was_needed, needed;
1229 was_needed = empathy_tp_chat_password_needed (self);
1231 priv->password_flags |= added;
1232 priv->password_flags ^= removed;
1234 needed = empathy_tp_chat_password_needed (self);
1236 if (was_needed != needed)
1237 g_object_notify (G_OBJECT (self), "password-needed");
1241 got_password_flags_cb (TpChannel *proxy,
1242 guint password_flags,
1243 const GError *error,
1245 GObject *weak_object)
1247 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1248 EmpathyTpChatPriv *priv = GET_PRIV (self);
1250 priv->got_password_flags = TRUE;
1251 priv->password_flags = password_flags;
1253 check_almost_ready (EMPATHY_TP_CHAT (self));
1257 tp_chat_constructor (GType type,
1259 GObjectConstructParam *props)
1262 EmpathyTpChatPriv *priv;
1265 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1267 priv = GET_PRIV (chat);
1269 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1270 tp_g_signal_connect_object (priv->channel, "invalidated",
1271 G_CALLBACK (tp_chat_invalidated_cb),
1274 g_assert (tp_proxy_is_prepared (priv->connection,
1275 TP_CONNECTION_FEATURE_CAPABILITIES));
1277 if (tp_proxy_has_interface_by_id (priv->channel,
1278 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1279 const TpIntSet *members;
1282 /* Get self contact from the group's self handle */
1283 handle = tp_channel_group_get_self_handle (priv->channel);
1284 empathy_tp_contact_factory_get_from_handle (priv->connection,
1285 handle, tp_chat_got_self_contact_cb,
1288 /* Get initial member contacts */
1289 members = tp_channel_group_get_members (priv->channel);
1290 handles = tp_intset_to_array (members);
1291 empathy_tp_contact_factory_get_from_handles (priv->connection,
1292 handles->len, (TpHandle *) handles->data,
1293 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1295 priv->can_upgrade_to_muc = FALSE;
1297 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1298 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1300 TpCapabilities *caps;
1304 /* Get the self contact from the connection's self handle */
1305 handle = tp_connection_get_self_handle (priv->connection);
1306 empathy_tp_contact_factory_get_from_handle (priv->connection,
1307 handle, tp_chat_got_self_contact_cb,
1310 /* Get the remote contact */
1311 handle = tp_channel_get_handle (priv->channel, NULL);
1312 empathy_tp_contact_factory_get_from_handle (priv->connection,
1313 handle, tp_chat_got_remote_contact_cb,
1316 caps = tp_connection_get_capabilities (priv->connection);
1317 g_assert (caps != NULL);
1319 classes = tp_capabilities_get_channel_classes (caps);
1321 for (i = 0; i < classes->len; i++) {
1322 GValueArray *array = g_ptr_array_index (classes, i);
1323 const char **oprops = g_value_get_boxed (
1324 g_value_array_get_nth (array, 1));
1326 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1327 priv->can_upgrade_to_muc = TRUE;
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);