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,
285 EmpathyTpChatPriv *priv;
286 EmpathyMessage *message;
289 priv = GET_PRIV (chat);
291 message = empathy_message_new_from_tp_message (msg, incoming);
292 /* FIXME: this is actually a lie for incoming messages. */
293 empathy_message_set_receiver (message, priv->user);
295 g_queue_push_tail (priv->messages_queue, message);
297 sender = tp_signalled_message_get_sender (msg);
298 g_assert (sender != NULL);
300 if (tp_contact_get_handle (sender) == 0) {
301 empathy_message_set_sender (message, priv->user);
302 tp_chat_emit_queued_messages (chat);
304 empathy_tp_contact_factory_get_from_handle (priv->connection,
305 tp_contact_get_handle (sender),
306 tp_chat_got_sender_cb,
307 message, NULL, G_OBJECT (chat));
312 handle_delivery_report (EmpathyTpChat *self,
315 EmpathyTpChatPriv *priv = GET_PRIV (self);
316 TpDeliveryStatus delivery_status;
317 const GHashTable *header;
318 TpChannelTextSendError delivery_error;
321 const gchar *message_body = NULL;
323 header = tp_message_peek (message, 0);
327 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
328 if (!valid || delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED)
331 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
333 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
335 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
336 * TpMessage. (fdo #35884) */
337 echo = tp_asv_get_boxed (header, "delivery-echo",
338 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
339 if (echo != NULL && echo->len >= 1) {
340 const GHashTable *echo_body;
342 echo_body = g_ptr_array_index (echo, 1);
343 if (echo_body != NULL)
344 message_body = tp_asv_get_string (echo_body, "content");
347 g_signal_emit (self, signals[SEND_ERROR], 0, message_body, delivery_error);
350 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
351 message, NULL, NULL);
355 handle_incoming_message (EmpathyTpChat *self,
359 EmpathyTpChatPriv *priv = GET_PRIV (self);
362 if (tp_message_is_delivery_report (message)) {
363 handle_delivery_report (self, message);
367 message_body = tp_message_to_text (message, NULL);
369 DEBUG ("Message %s (channel %s): %s",
370 pending ? "pending" : "received",
371 tp_proxy_get_object_path (priv->channel), message_body);
373 if (message_body == NULL) {
374 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
376 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
377 message, NULL, NULL);
381 tp_chat_build_message (self, message, TRUE);
383 g_free (message_body);
387 message_received_cb (TpTextChannel *channel,
391 handle_incoming_message (chat, message, FALSE);
395 message_sent_cb (TpTextChannel *channel,
397 TpMessageSendingFlags flags,
403 message_body = tp_message_to_text (message, NULL);
405 DEBUG ("Message sent: %s", message_body);
407 tp_chat_build_message (chat, message, FALSE);
409 g_free (message_body);
412 static TpChannelTextSendError
413 error_to_text_send_error (GError *error)
415 if (error->domain != TP_ERRORS)
416 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
418 switch (error->code) {
419 case TP_ERROR_OFFLINE:
420 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
421 case TP_ERROR_INVALID_HANDLE:
422 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
423 case TP_ERROR_PERMISSION_DENIED:
424 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
425 case TP_ERROR_NOT_IMPLEMENTED:
426 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
429 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
433 message_send_cb (GObject *source,
434 GAsyncResult *result,
437 EmpathyTpChat *chat = user_data;
438 TpTextChannel *channel = (TpTextChannel *) source;
439 GError *error = NULL;
441 if (!tp_text_channel_send_message_finish (channel, result, NULL, &error)) {
442 DEBUG ("Error: %s", error->message);
444 /* FIXME: we should use the body of the message as first argument of the
445 * signal but can't easily get it as we just get a user_data pointer. Once
446 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
447 * to use the user_data pointer to pass the message and fix this. */
448 g_signal_emit (chat, signals[SEND_ERROR], 0,
449 NULL, error_to_text_send_error (error));
451 g_error_free (error);
457 TpChannelChatState state;
461 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
462 EmpathyContact *contact,
467 TpChannelChatState state;
470 DEBUG ("Error: %s", error->message);
474 state = GPOINTER_TO_UINT (user_data);
475 DEBUG ("Chat state changed for %s (%d): %d",
476 empathy_contact_get_alias (contact),
477 empathy_contact_get_handle (contact), state);
479 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
483 tp_chat_state_changed_cb (TpChannel *channel,
485 TpChannelChatState state,
488 EmpathyTpChatPriv *priv = GET_PRIV (chat);
490 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
491 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
492 NULL, G_OBJECT (chat));
496 list_pending_messages (EmpathyTpChat *self)
498 EmpathyTpChatPriv *priv = GET_PRIV (self);
501 g_assert (priv->channel != NULL);
503 messages = tp_text_channel_get_pending_messages (
504 TP_TEXT_CHANNEL (priv->channel));
506 for (l = messages; l != NULL; l = g_list_next (l)) {
507 TpMessage *message = l->data;
509 handle_incoming_message (self, message, FALSE);
512 g_list_free (messages);
516 tp_chat_property_flags_changed_cb (TpProxy *proxy,
517 const GPtrArray *properties,
521 EmpathyTpChatPriv *priv = GET_PRIV (chat);
524 if (priv->channel == NULL)
527 if (!priv->had_properties_list || !properties) {
531 for (i = 0; i < properties->len; i++) {
532 GValueArray *prop_struct;
533 EmpathyTpChatProperty *property;
537 prop_struct = g_ptr_array_index (properties, i);
538 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
539 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
541 for (j = 0; j < priv->properties->len; j++) {
542 property = g_ptr_array_index (priv->properties, j);
543 if (property->id == id) {
544 property->flags = flags;
545 DEBUG ("property %s flags changed: %d",
546 property->name, property->flags);
554 tp_chat_properties_changed_cb (TpProxy *proxy,
555 const GPtrArray *properties,
559 EmpathyTpChatPriv *priv = GET_PRIV (chat);
562 if (priv->channel == NULL)
565 if (!priv->had_properties_list || !properties) {
569 for (i = 0; i < properties->len; i++) {
570 GValueArray *prop_struct;
571 EmpathyTpChatProperty *property;
575 prop_struct = g_ptr_array_index (properties, i);
576 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
577 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
579 for (j = 0; j < priv->properties->len; j++) {
580 property = g_ptr_array_index (priv->properties, j);
581 if (property->id == id) {
582 if (property->value) {
583 g_value_copy (src_value, property->value);
585 property->value = tp_g_value_slice_dup (src_value);
588 DEBUG ("property %s changed", property->name);
589 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
590 property->name, property->value);
598 tp_chat_get_properties_cb (TpProxy *proxy,
599 const GPtrArray *properties,
605 DEBUG ("Error getting properties: %s", error->message);
609 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
613 tp_chat_list_properties_cb (TpProxy *proxy,
614 const GPtrArray *properties,
619 EmpathyTpChatPriv *priv = GET_PRIV (chat);
623 if (priv->channel == NULL)
626 priv->had_properties_list = TRUE;
629 DEBUG ("Error listing properties: %s", error->message);
633 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
634 priv->properties = g_ptr_array_sized_new (properties->len);
635 for (i = 0; i < properties->len; i++) {
636 GValueArray *prop_struct;
637 EmpathyTpChatProperty *property;
639 prop_struct = g_ptr_array_index (properties, i);
640 property = g_slice_new0 (EmpathyTpChatProperty);
641 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
642 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
643 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
645 DEBUG ("Adding property name=%s id=%d flags=%d",
646 property->name, property->id, property->flags);
647 g_ptr_array_add (priv->properties, property);
648 if (property->flags & TP_PROPERTY_FLAG_READ) {
649 g_array_append_val (ids, property->id);
653 tp_cli_properties_interface_call_get_properties (proxy, -1,
655 tp_chat_get_properties_cb,
659 g_array_free (ids, TRUE);
663 empathy_tp_chat_set_property (EmpathyTpChat *chat,
667 EmpathyTpChatPriv *priv = GET_PRIV (chat);
668 EmpathyTpChatProperty *property;
671 if (!priv->had_properties_list) {
675 for (i = 0; i < priv->properties->len; i++) {
676 property = g_ptr_array_index (priv->properties, i);
677 if (!tp_strdiff (property->name, name)) {
678 GPtrArray *properties;
681 GValue dest_value = {0, };
683 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
687 g_value_init (&id, G_TYPE_UINT);
688 g_value_init (&dest_value, G_TYPE_VALUE);
689 g_value_set_uint (&id, property->id);
690 g_value_set_boxed (&dest_value, value);
692 prop = g_value_array_new (2);
693 g_value_array_append (prop, &id);
694 g_value_array_append (prop, &dest_value);
696 properties = g_ptr_array_sized_new (1);
697 g_ptr_array_add (properties, prop);
699 DEBUG ("Set property %s", name);
700 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
702 (tp_cli_properties_interface_callback_for_set_properties)
704 "Seting property", NULL,
707 g_ptr_array_free (properties, TRUE);
708 g_value_array_free (prop);
715 EmpathyTpChatProperty *
716 empathy_tp_chat_get_property (EmpathyTpChat *chat,
719 EmpathyTpChatPriv *priv = GET_PRIV (chat);
720 EmpathyTpChatProperty *property;
723 if (!priv->had_properties_list) {
727 for (i = 0; i < priv->properties->len; i++) {
728 property = g_ptr_array_index (priv->properties, i);
729 if (!tp_strdiff (property->name, name)) {
738 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
740 EmpathyTpChatPriv *priv = GET_PRIV (chat);
742 return priv->properties;
746 tp_chat_dispose (GObject *object)
748 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
749 EmpathyTpChatPriv *priv = GET_PRIV (self);
751 if (priv->dispose_has_run)
754 priv->dispose_has_run = TRUE;
756 tp_clear_object (&priv->account);
758 if (priv->connection != NULL)
759 g_object_unref (priv->connection);
760 priv->connection = NULL;
762 if (priv->channel != NULL) {
763 g_signal_handlers_disconnect_by_func (priv->channel,
764 tp_chat_invalidated_cb, self);
765 g_object_unref (priv->channel);
767 priv->channel = NULL;
769 if (priv->remote_contact != NULL)
770 g_object_unref (priv->remote_contact);
771 priv->remote_contact = NULL;
773 if (priv->user != NULL)
774 g_object_unref (priv->user);
777 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
778 g_queue_clear (priv->messages_queue);
780 g_queue_foreach (priv->pending_messages_queue,
781 (GFunc) g_object_unref, NULL);
782 g_queue_clear (priv->pending_messages_queue);
784 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
785 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
789 tp_chat_finalize (GObject *object)
791 EmpathyTpChatPriv *priv = GET_PRIV (object);
794 DEBUG ("Finalize: %p", object);
796 if (priv->properties) {
797 for (i = 0; i < priv->properties->len; i++) {
798 EmpathyTpChatProperty *property;
800 property = g_ptr_array_index (priv->properties, i);
801 g_free (property->name);
802 if (property->value) {
803 tp_g_value_slice_free (property->value);
805 g_slice_free (EmpathyTpChatProperty, property);
807 g_ptr_array_free (priv->properties, TRUE);
810 g_queue_free (priv->messages_queue);
811 g_queue_free (priv->pending_messages_queue);
813 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
817 check_almost_ready (EmpathyTpChat *chat)
819 EmpathyTpChatPriv *priv = GET_PRIV (chat);
824 if (priv->user == NULL)
827 if (!priv->got_password_flags)
830 /* We need either the members (room) or the remote contact (private chat).
831 * If the chat is protected by a password we can't get these information so
832 * consider the chat as ready so it can be presented to the user. */
833 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
834 priv->remote_contact == NULL)
837 /* We use the default factory so this feature should have been prepared */
838 g_assert (tp_proxy_is_prepared (priv->channel,
839 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
841 tp_g_signal_connect_object (priv->channel, "message-received",
842 G_CALLBACK (message_received_cb), chat, 0);
844 list_pending_messages (chat);
846 tp_g_signal_connect_object (priv->channel, "message-sent",
847 G_CALLBACK (message_sent_cb), chat, 0);
849 tp_g_signal_connect_object (priv->channel, "chat-state-changed",
850 G_CALLBACK (tp_chat_state_changed_cb), chat, 0);
856 tp_chat_update_remote_contact (EmpathyTpChat *chat)
858 EmpathyTpChatPriv *priv = GET_PRIV (chat);
859 EmpathyContact *contact = NULL;
860 TpHandle self_handle;
861 TpHandleType handle_type;
864 /* If this is a named chatroom, never pretend it is a private chat */
865 tp_channel_get_handle (priv->channel, &handle_type);
866 if (handle_type == TP_HANDLE_TYPE_ROOM) {
870 /* This is an MSN chat, but it's the new style where 1-1 chats don't
871 * have the group interface. If it has the conference interface, then
872 * it is indeed a MUC. */
873 if (tp_proxy_has_interface_by_id (priv->channel,
874 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
878 /* This is an MSN-like chat where anyone can join the chat at anytime.
879 * If there is only one non-self contact member, we are in a private
880 * chat and we set the "remote-contact" property to that contact. If
881 * there are more, set the "remote-contact" property to NULL and the
882 * UI will display a contact list. */
883 self_handle = tp_channel_group_get_self_handle (priv->channel);
884 for (l = priv->members; l; l = l->next) {
885 /* Skip self contact if member */
886 if (empathy_contact_get_handle (l->data) == self_handle) {
890 /* We have more than one remote contact, break */
891 if (contact != NULL) {
896 /* If we didn't find yet a remote contact, keep this one */
900 if (priv->remote_contact == contact) {
904 DEBUG ("Changing remote contact from %p to %p",
905 priv->remote_contact, contact);
907 if (priv->remote_contact) {
908 g_object_unref (priv->remote_contact);
911 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
912 g_object_notify (G_OBJECT (chat), "remote-contact");
916 tp_chat_got_added_contacts_cb (TpConnection *connection,
918 EmpathyContact * const * contacts,
920 const TpHandle *failed,
925 EmpathyTpChatPriv *priv = GET_PRIV (chat);
927 const TpIntSet *members;
929 EmpathyContact *contact;
932 DEBUG ("Error: %s", error->message);
936 members = tp_channel_group_get_members (priv->channel);
937 for (i = 0; i < n_contacts; i++) {
938 contact = contacts[i];
939 handle = empathy_contact_get_handle (contact);
941 /* Make sure the contact is still member */
942 if (tp_intset_is_member (members, handle)) {
943 priv->members = g_list_prepend (priv->members,
944 g_object_ref (contact));
945 g_signal_emit_by_name (chat, "members-changed",
946 contact, NULL, 0, NULL, TRUE);
950 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
951 check_almost_ready (EMPATHY_TP_CHAT (chat));
954 static EmpathyContact *
955 chat_lookup_contact (EmpathyTpChat *chat,
959 EmpathyTpChatPriv *priv = GET_PRIV (chat);
962 for (l = priv->members; l; l = l->next) {
963 EmpathyContact *c = l->data;
965 if (empathy_contact_get_handle (c) != handle) {
970 /* Caller takes the reference. */
971 priv->members = g_list_delete_link (priv->members, l);
989 static ContactRenameData *
990 contact_rename_data_new (TpHandle handle,
992 const gchar* message)
994 ContactRenameData *data = g_new (ContactRenameData, 1);
995 data->old_handle = handle;
996 data->reason = reason;
997 data->message = g_strdup (message);
1003 contact_rename_data_free (ContactRenameData* data)
1005 g_free (data->message);
1010 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1012 EmpathyContact * const * contacts,
1014 const TpHandle *failed,
1015 const GError *error,
1019 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1020 const TpIntSet *members;
1022 EmpathyContact *old = NULL, *new = NULL;
1023 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1026 DEBUG ("Error: %s", error->message);
1030 /* renamed members can only be delivered one at a time */
1031 g_warn_if_fail (n_contacts == 1);
1035 members = tp_channel_group_get_members (priv->channel);
1036 handle = empathy_contact_get_handle (new);
1038 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1039 rename_data->old_handle, TRUE);
1041 /* Make sure the contact is still member */
1042 if (tp_intset_is_member (members, handle)) {
1043 priv->members = g_list_prepend (priv->members,
1044 g_object_ref (new));
1047 g_signal_emit_by_name (chat, "member-renamed",
1048 old, new, rename_data->reason,
1049 rename_data->message);
1050 g_object_unref (old);
1054 if (priv->user == old) {
1055 /* We change our nick */
1056 tp_clear_object (&priv->user);
1057 priv->user = g_object_ref (new);
1060 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1061 check_almost_ready (EMPATHY_TP_CHAT (chat));
1066 tp_chat_group_members_changed_cb (TpChannel *self,
1070 GArray *local_pending,
1071 GArray *remote_pending,
1074 EmpathyTpChat *chat)
1076 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1077 EmpathyContact *contact;
1078 EmpathyContact *actor_contact = NULL;
1080 ContactRenameData *rename_data;
1081 TpHandle old_handle;
1083 /* Contact renamed */
1084 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1085 /* there can only be a single 'added' and a single 'removed' handle */
1086 if (removed->len != 1 || added->len != 1) {
1087 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1088 added->len, removed->len);
1092 old_handle = g_array_index (removed, guint, 0);
1094 rename_data = contact_rename_data_new (old_handle, reason, message);
1095 empathy_tp_contact_factory_get_from_handles (priv->connection,
1096 added->len, (TpHandle *) added->data,
1097 tp_chat_got_renamed_contacts_cb,
1098 rename_data, (GDestroyNotify) contact_rename_data_free,
1104 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1105 if (actor_contact == NULL) {
1106 /* FIXME: handle this a tad more gracefully: perhaps
1107 * the actor was a server op. We could use the
1108 * contact-ids detail of MembersChangedDetailed.
1110 DEBUG ("actor %u not a channel member", actor);
1114 /* Remove contacts that are not members anymore */
1115 for (i = 0; i < removed->len; i++) {
1116 contact = chat_lookup_contact (chat,
1117 g_array_index (removed, TpHandle, i), TRUE);
1119 if (contact != NULL) {
1120 g_signal_emit_by_name (chat, "members-changed", contact,
1121 actor_contact, reason, message,
1123 g_object_unref (contact);
1127 /* Request added contacts */
1128 if (added->len > 0) {
1129 empathy_tp_contact_factory_get_from_handles (priv->connection,
1130 added->len, (TpHandle *) added->data,
1131 tp_chat_got_added_contacts_cb, NULL, NULL,
1135 tp_chat_update_remote_contact (chat);
1137 if (actor_contact != NULL) {
1138 g_object_unref (actor_contact);
1143 tp_chat_got_remote_contact_cb (TpConnection *connection,
1144 EmpathyContact *contact,
1145 const GError *error,
1149 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1152 DEBUG ("Error: %s", error->message);
1153 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1157 priv->remote_contact = g_object_ref (contact);
1158 g_object_notify (chat, "remote-contact");
1160 check_almost_ready (EMPATHY_TP_CHAT (chat));
1164 tp_chat_got_self_contact_cb (TpConnection *connection,
1165 EmpathyContact *contact,
1166 const GError *error,
1170 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1173 DEBUG ("Error: %s", error->message);
1174 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1178 priv->user = g_object_ref (contact);
1179 empathy_contact_set_is_user (priv->user, TRUE);
1180 check_almost_ready (EMPATHY_TP_CHAT (chat));
1184 password_flags_changed_cb (TpChannel *channel,
1188 GObject *weak_object)
1190 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1191 EmpathyTpChatPriv *priv = GET_PRIV (self);
1192 gboolean was_needed, needed;
1194 was_needed = empathy_tp_chat_password_needed (self);
1196 priv->password_flags |= added;
1197 priv->password_flags ^= removed;
1199 needed = empathy_tp_chat_password_needed (self);
1201 if (was_needed != needed)
1202 g_object_notify (G_OBJECT (self), "password-needed");
1206 got_password_flags_cb (TpChannel *proxy,
1207 guint password_flags,
1208 const GError *error,
1210 GObject *weak_object)
1212 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1213 EmpathyTpChatPriv *priv = GET_PRIV (self);
1215 priv->got_password_flags = TRUE;
1216 priv->password_flags = password_flags;
1218 check_almost_ready (EMPATHY_TP_CHAT (self));
1222 tp_chat_constructor (GType type,
1224 GObjectConstructParam *props)
1227 EmpathyTpChatPriv *priv;
1230 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1232 priv = GET_PRIV (chat);
1234 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1235 tp_g_signal_connect_object (priv->channel, "invalidated",
1236 G_CALLBACK (tp_chat_invalidated_cb),
1239 g_assert (tp_proxy_is_prepared (priv->connection,
1240 TP_CONNECTION_FEATURE_CAPABILITIES));
1242 if (tp_proxy_has_interface_by_id (priv->channel,
1243 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1244 const TpIntSet *members;
1247 /* Get self contact from the group's self handle */
1248 handle = tp_channel_group_get_self_handle (priv->channel);
1249 empathy_tp_contact_factory_get_from_handle (priv->connection,
1250 handle, tp_chat_got_self_contact_cb,
1253 /* Get initial member contacts */
1254 members = tp_channel_group_get_members (priv->channel);
1255 handles = tp_intset_to_array (members);
1256 empathy_tp_contact_factory_get_from_handles (priv->connection,
1257 handles->len, (TpHandle *) handles->data,
1258 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1260 priv->can_upgrade_to_muc = FALSE;
1262 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1263 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1265 TpCapabilities *caps;
1269 /* Get the self contact from the connection's self handle */
1270 handle = tp_connection_get_self_handle (priv->connection);
1271 empathy_tp_contact_factory_get_from_handle (priv->connection,
1272 handle, tp_chat_got_self_contact_cb,
1275 /* Get the remote contact */
1276 handle = tp_channel_get_handle (priv->channel, NULL);
1277 empathy_tp_contact_factory_get_from_handle (priv->connection,
1278 handle, tp_chat_got_remote_contact_cb,
1281 caps = tp_connection_get_capabilities (priv->connection);
1282 g_assert (caps != NULL);
1284 classes = tp_capabilities_get_channel_classes (caps);
1286 for (i = 0; i < classes->len; i++) {
1287 GValueArray *array = g_ptr_array_index (classes, i);
1288 const char **oprops = g_value_get_boxed (
1289 g_value_array_get_nth (array, 1));
1291 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1292 priv->can_upgrade_to_muc = TRUE;
1298 if (tp_proxy_has_interface_by_id (priv->channel,
1299 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1300 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1301 tp_chat_list_properties_cb,
1304 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1305 tp_chat_properties_changed_cb,
1307 G_OBJECT (chat), NULL);
1308 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1309 tp_chat_property_flags_changed_cb,
1311 G_OBJECT (chat), NULL);
1314 /* Check if the chat is password protected */
1315 if (tp_proxy_has_interface_by_id (priv->channel,
1316 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1317 priv->got_password_flags = FALSE;
1319 tp_cli_channel_interface_password_connect_to_password_flags_changed
1320 (priv->channel, password_flags_changed_cb, chat, NULL,
1321 G_OBJECT (chat), NULL);
1323 tp_cli_channel_interface_password_call_get_password_flags
1324 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1326 /* No Password interface, so no need to fetch the password flags */
1327 priv->got_password_flags = TRUE;
1334 tp_chat_get_property (GObject *object,
1339 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1340 EmpathyTpChatPriv *priv = GET_PRIV (object);
1344 g_value_set_object (value, priv->account);
1347 g_value_set_object (value, priv->channel);
1349 case PROP_REMOTE_CONTACT:
1350 g_value_set_object (value, priv->remote_contact);
1353 g_value_set_boolean (value, priv->ready);
1355 case PROP_PASSWORD_NEEDED:
1356 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1365 tp_chat_set_property (GObject *object,
1367 const GValue *value,
1370 EmpathyTpChatPriv *priv = GET_PRIV (object);
1374 priv->account = g_value_dup_object (value);
1377 priv->channel = g_value_dup_object (value);
1380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1386 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1388 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1390 object_class->dispose = tp_chat_dispose;
1391 object_class->finalize = tp_chat_finalize;
1392 object_class->constructor = tp_chat_constructor;
1393 object_class->get_property = tp_chat_get_property;
1394 object_class->set_property = tp_chat_set_property;
1396 g_object_class_install_property (object_class,
1398 g_param_spec_object ("account",
1400 "the account associated with the chat",
1403 G_PARAM_CONSTRUCT_ONLY |
1404 G_PARAM_STATIC_STRINGS));
1406 g_object_class_install_property (object_class,
1408 g_param_spec_object ("channel",
1409 "telepathy channel",
1410 "The text channel for the chat",
1413 G_PARAM_CONSTRUCT_ONLY));
1415 g_object_class_install_property (object_class,
1416 PROP_REMOTE_CONTACT,
1417 g_param_spec_object ("remote-contact",
1418 "The remote contact",
1419 "The remote contact if there is no group iface on the channel",
1420 EMPATHY_TYPE_CONTACT,
1423 g_object_class_install_property (object_class,
1425 g_param_spec_boolean ("ready",
1426 "Is the object ready",
1427 "This object can't be used until this becomes true",
1431 g_object_class_install_property (object_class,
1432 PROP_PASSWORD_NEEDED,
1433 g_param_spec_boolean ("password-needed",
1435 "TRUE if a password is needed to join the channel",
1440 signals[MESSAGE_RECEIVED] =
1441 g_signal_new ("message-received",
1442 G_TYPE_FROM_CLASS (klass),
1446 g_cclosure_marshal_VOID__OBJECT,
1448 1, EMPATHY_TYPE_MESSAGE);
1450 signals[SEND_ERROR] =
1451 g_signal_new ("send-error",
1452 G_TYPE_FROM_CLASS (klass),
1456 _empathy_marshal_VOID__STRING_UINT,
1458 2, G_TYPE_STRING, G_TYPE_UINT);
1460 signals[CHAT_STATE_CHANGED] =
1461 g_signal_new ("chat-state-changed",
1462 G_TYPE_FROM_CLASS (klass),
1466 _empathy_marshal_VOID__OBJECT_UINT,
1468 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1470 signals[PROPERTY_CHANGED] =
1471 g_signal_new ("property-changed",
1472 G_TYPE_FROM_CLASS (klass),
1476 _empathy_marshal_VOID__STRING_BOXED,
1478 2, G_TYPE_STRING, G_TYPE_VALUE);
1481 g_signal_new ("destroy",
1482 G_TYPE_FROM_CLASS (klass),
1486 g_cclosure_marshal_VOID__VOID,
1490 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1494 empathy_tp_chat_init (EmpathyTpChat *chat)
1496 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1497 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1500 priv->messages_queue = g_queue_new ();
1501 priv->pending_messages_queue = g_queue_new ();
1505 tp_chat_iface_init (EmpathyContactListIface *iface)
1507 iface->add = tp_chat_add;
1508 iface->remove = tp_chat_remove;
1509 iface->get_members = tp_chat_get_members;
1513 empathy_tp_chat_new (TpAccount *account,
1516 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1517 g_return_val_if_fail (TP_IS_TEXT_CHANNEL (channel), NULL);
1519 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1526 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1528 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1532 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1534 id = tp_channel_get_identifier (priv->channel);
1535 if (!EMP_STR_EMPTY (id))
1537 else if (priv->remote_contact)
1538 return empathy_contact_get_id (priv->remote_contact);
1545 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1547 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1549 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1550 g_return_val_if_fail (priv->ready, NULL);
1552 return priv->remote_contact;
1556 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1558 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1560 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1562 return priv->channel;
1566 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1568 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1570 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1572 return priv->account;
1576 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1578 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1580 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1582 return tp_channel_borrow_connection (priv->channel);
1585 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1587 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1589 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1595 empathy_tp_chat_send (EmpathyTpChat *chat,
1598 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1599 gchar *message_body;
1601 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1602 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1603 g_return_if_fail (priv->ready);
1605 message_body = tp_message_to_text (message, NULL);
1607 DEBUG ("Sending message: %s", message_body);
1609 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel),
1610 message, 0, message_send_cb, chat);
1612 g_free (message_body);
1616 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1617 TpChannelChatState state)
1619 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1621 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1622 g_return_if_fail (priv->ready);
1624 if (tp_proxy_has_interface_by_id (priv->channel,
1625 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1626 DEBUG ("Set state: %d", state);
1627 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1630 "setting chat state",
1638 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1640 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1642 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1643 g_return_val_if_fail (priv->ready, NULL);
1645 return priv->pending_messages_queue->head;
1649 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1650 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1652 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1653 priv->channel, -1, ids, tp_chat_async_cb,
1654 "acknowledging received message", NULL, G_OBJECT (chat));
1658 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1659 EmpathyMessage *message) {
1660 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1661 GArray *message_ids;
1665 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1666 g_return_if_fail (priv->ready);
1668 if (!empathy_message_is_incoming (message))
1671 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1673 id = empathy_message_get_id (message);
1674 g_array_append_val (message_ids, id);
1675 acknowledge_messages (chat, message_ids);
1676 g_array_free (message_ids, TRUE);
1679 m = g_queue_find (priv->pending_messages_queue, message);
1680 g_assert (m != NULL);
1681 g_queue_delete_link (priv->pending_messages_queue, m);
1682 g_object_unref (message);
1686 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1687 const GSList *messages) {
1688 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1689 /* Copy messages as the messges list (probably is) our own */
1690 GSList *msgs = g_slist_copy ((GSList *) messages);
1693 GArray *message_ids;
1695 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1696 g_return_if_fail (priv->ready);
1698 length = g_slist_length ((GSList *) messages);
1703 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1705 for (l = msgs; l != NULL; l = g_slist_next (l)) {
1708 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1710 m = g_queue_find (priv->pending_messages_queue, message);
1711 g_assert (m != NULL);
1712 g_queue_delete_link (priv->pending_messages_queue, m);
1714 if (empathy_message_is_incoming (message)) {
1715 guint id = empathy_message_get_id (message);
1716 g_array_append_val (message_ids, id);
1718 g_object_unref (message);
1721 if (message_ids->len > 0)
1722 acknowledge_messages (chat, message_ids);
1724 g_array_free (message_ids, TRUE);
1725 g_slist_free (msgs);
1729 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
1731 empathy_tp_chat_acknowledge_messages (chat,
1732 (GSList *) empathy_tp_chat_get_pending_messages (chat));
1736 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1738 EmpathyTpChatPriv *priv = GET_PRIV (self);
1740 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1744 provide_password_cb (TpChannel *channel,
1746 const GError *error,
1748 GObject *weak_object)
1750 GSimpleAsyncResult *result = user_data;
1752 if (error != NULL) {
1753 g_simple_async_result_set_from_error (result, error);
1755 else if (!correct) {
1756 /* The current D-Bus API is a bit weird so re-use the
1757 * AuthenticationFailed error */
1758 g_simple_async_result_set_error (result, TP_ERRORS,
1759 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1762 g_simple_async_result_complete (result);
1763 g_object_unref (result);
1767 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1768 const gchar *password,
1769 GAsyncReadyCallback callback,
1772 EmpathyTpChatPriv *priv = GET_PRIV (self);
1773 GSimpleAsyncResult *result;
1775 result = g_simple_async_result_new (G_OBJECT (self),
1776 callback, user_data,
1777 empathy_tp_chat_provide_password_finish);
1779 tp_cli_channel_interface_password_call_provide_password
1780 (priv->channel, -1, password, provide_password_cb, result,
1781 NULL, G_OBJECT (self));
1785 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1786 GAsyncResult *result,
1789 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1793 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1794 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1800 * empathy_tp_chat_can_add_contact:
1802 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1803 * That is if this chat is a 1-to-1 channel that can be upgraded to
1804 * a MUC using the Conference interface or if the channel is a MUC.
1807 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1809 EmpathyTpChatPriv *priv;
1811 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1813 priv = GET_PRIV (self);
1815 return priv->can_upgrade_to_muc ||
1816 tp_proxy_has_interface_by_id (priv->channel,
1817 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1821 tp_channel_leave_async_cb (GObject *source_object,
1825 GError *error = NULL;
1827 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1828 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1830 g_error_free (error);
1835 empathy_tp_chat_leave (EmpathyTpChat *self,
1836 const gchar *message)
1838 EmpathyTpChatPriv *priv = GET_PRIV (self);
1840 tp_channel_leave_async (priv->channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1841 message, tp_channel_leave_async_cb, self);
1845 add_members_cb (TpChannel *proxy,
1846 const GError *error,
1848 GObject *weak_object)
1850 EmpathyTpChatPriv *priv = GET_PRIV (weak_object);
1852 if (error != NULL) {
1853 DEBUG ("Failed to join chat (%s): %s",
1854 tp_channel_get_identifier (priv->channel), error->message);
1859 empathy_tp_chat_join (EmpathyTpChat *self)
1861 EmpathyTpChatPriv *priv = GET_PRIV (self);
1862 TpHandle self_handle;
1865 self_handle = tp_channel_group_get_self_handle (priv->channel);
1867 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1868 g_array_append_val (members, self_handle);
1870 tp_cli_channel_interface_group_call_add_members (priv->channel, -1, members,
1871 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1873 g_array_free (members, TRUE);
1877 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1880 EmpathyTpChatPriv *priv = GET_PRIV (self);
1881 TpHandle self_handle;
1883 if (!tp_proxy_has_interface (priv->channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1886 self_handle = tp_channel_group_get_self_handle (priv->channel);
1887 if (self_handle == 0)
1890 return tp_channel_group_get_local_pending_info (priv->channel, self_handle,
1891 inviter, NULL, NULL);
1895 empathy_tp_chat_get_chat_state (EmpathyTpChat *chat,
1896 EmpathyContact *contact)
1898 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1899 return tp_channel_get_chat_state (priv->channel,
1900 empathy_contact_get_handle (contact));
1904 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1906 EmpathyTpChatPriv *priv = GET_PRIV (self);
1908 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);