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;
62 gboolean got_sms_channel;
65 GHashTable *messages_being_sent;
68 static void tp_chat_iface_init (EmpathyContactListIface *iface);
78 PROP_N_MESSAGES_SENDING,
90 static guint signals[LAST_SIGNAL];
92 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
93 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
96 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
99 tp_chat_set_delivery_status (EmpathyTpChat *self,
101 EmpathyDeliveryStatus delivery_status)
103 EmpathyTpChatPriv *priv = GET_PRIV (self);
104 TpDeliveryReportingSupportFlags flags =
105 tp_text_channel_get_delivery_reporting_support (
106 TP_TEXT_CHANNEL (priv->channel));
108 /* channel must support receiving failures and successes */
109 if (!tp_str_empty (token) &&
110 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
111 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
113 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
115 switch (delivery_status) {
116 case EMPATHY_DELIVERY_STATUS_NONE:
117 g_hash_table_remove (priv->messages_being_sent,
122 g_hash_table_insert (priv->messages_being_sent,
124 GUINT_TO_POINTER (delivery_status));
128 g_object_notify (G_OBJECT (self), "n-messages-sending");
133 tp_chat_invalidated_cb (TpProxy *proxy,
139 DEBUG ("Channel invalidated: %s", message);
140 g_signal_emit (chat, signals[DESTROY], 0);
144 tp_chat_async_cb (TpChannel *proxy,
147 GObject *weak_object)
150 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
155 create_conference_cb (GObject *source,
156 GAsyncResult *result,
159 GError *error = NULL;
161 if (!tp_account_channel_request_create_channel_finish (
162 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
163 DEBUG ("Failed to create conference channel: %s", error->message);
164 g_error_free (error);
169 tp_chat_add (EmpathyContactList *list,
170 EmpathyContact *contact,
171 const gchar *message)
173 EmpathyTpChatPriv *priv = GET_PRIV (list);
175 if (tp_proxy_has_interface_by_id (priv->channel,
176 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
178 GArray handles = {(gchar *) &handle, 1};
180 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
181 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
183 handle = empathy_contact_get_handle (contact);
184 tp_cli_channel_interface_group_call_add_members (priv->channel,
185 -1, &handles, NULL, NULL, NULL, NULL, NULL);
186 } else if (priv->can_upgrade_to_muc) {
187 TpAccountChannelRequest *req;
189 const char *object_path;
190 GPtrArray channels = { (gpointer *) &object_path, 1 };
191 const char *invitees[2] = { NULL, };
193 invitees[0] = empathy_contact_get_id (contact);
194 object_path = tp_proxy_get_object_path (priv->channel);
197 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
198 TP_IFACE_CHANNEL_TYPE_TEXT,
199 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
201 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
202 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
203 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
204 G_TYPE_STRV, invitees,
205 /* FIXME: InvitationMessage ? */
208 req = tp_account_channel_request_new (priv->account, props,
209 TP_USER_ACTION_TIME_NOT_USER_ACTION);
211 /* Although this is a MUC, it's anonymous, so CreateChannel is
213 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
214 NULL, create_conference_cb, NULL);
216 g_object_unref (req);
217 g_hash_table_unref (props);
219 g_warning ("Cannot add to this channel");
224 tp_chat_remove (EmpathyContactList *list,
225 EmpathyContact *contact,
226 const gchar *message)
228 EmpathyTpChatPriv *priv = GET_PRIV (list);
230 GArray handles = {(gchar *) &handle, 1};
232 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
233 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
235 handle = empathy_contact_get_handle (contact);
236 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
243 tp_chat_get_members (EmpathyContactList *list)
245 EmpathyTpChatPriv *priv = GET_PRIV (list);
246 GList *members = NULL;
248 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
251 members = g_list_copy (priv->members);
252 g_list_foreach (members, (GFunc) g_object_ref, NULL);
254 members = g_list_prepend (members, g_object_ref (priv->user));
255 if (priv->remote_contact != NULL)
256 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
263 check_ready (EmpathyTpChat *chat)
265 EmpathyTpChatPriv *priv = GET_PRIV (chat);
270 if (g_queue_get_length (priv->messages_queue) > 0)
276 g_object_notify (G_OBJECT (chat), "ready");
280 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
282 EmpathyTpChatPriv *priv = GET_PRIV (chat);
283 EmpathyMessage *message;
285 /* Check if we can now emit some queued messages */
286 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
287 if (empathy_message_get_sender (message) == NULL) {
291 DEBUG ("Queued message ready");
292 g_queue_pop_head (priv->messages_queue);
293 g_queue_push_tail (priv->pending_messages_queue, message);
294 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
301 tp_chat_got_sender_cb (TpConnection *connection,
302 EmpathyContact *contact,
307 EmpathyTpChatPriv *priv = GET_PRIV (chat);
310 DEBUG ("Error: %s", error->message);
311 /* Do not block the message queue, just drop this message */
312 g_queue_remove (priv->messages_queue, message);
314 empathy_message_set_sender (message, contact);
317 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
321 tp_chat_build_message (EmpathyTpChat *chat,
325 EmpathyTpChatPriv *priv;
326 EmpathyMessage *message;
329 priv = GET_PRIV (chat);
331 message = empathy_message_new_from_tp_message (msg, incoming);
332 /* FIXME: this is actually a lie for incoming messages. */
333 empathy_message_set_receiver (message, priv->user);
335 g_queue_push_tail (priv->messages_queue, message);
337 sender = tp_signalled_message_get_sender (msg);
338 g_assert (sender != NULL);
340 if (tp_contact_get_handle (sender) == 0) {
341 empathy_message_set_sender (message, priv->user);
342 tp_chat_emit_queued_messages (chat);
344 empathy_tp_contact_factory_get_from_handle (priv->connection,
345 tp_contact_get_handle (sender),
346 tp_chat_got_sender_cb,
347 message, NULL, G_OBJECT (chat));
352 handle_delivery_report (EmpathyTpChat *self,
355 EmpathyTpChatPriv *priv = GET_PRIV (self);
356 TpDeliveryStatus delivery_status;
357 const GHashTable *header;
358 TpChannelTextSendError delivery_error;
361 const gchar *message_body = NULL;
362 const gchar *delivery_dbus_error;
363 const gchar *delivery_token = NULL;
365 header = tp_message_peek (message, 0);
369 delivery_token = tp_asv_get_string (header, "delivery-token");
370 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
374 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
375 DEBUG ("Accepted %s", delivery_token);
376 tp_chat_set_delivery_status (self, delivery_token,
377 EMPATHY_DELIVERY_STATUS_ACCEPTED);
379 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
380 DEBUG ("Delivered %s", delivery_token);
381 tp_chat_set_delivery_status (self, delivery_token,
382 EMPATHY_DELIVERY_STATUS_NONE);
384 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
388 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
390 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
392 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
394 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
395 * TpMessage. (fdo #35884) */
396 echo = tp_asv_get_boxed (header, "delivery-echo",
397 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
398 if (echo != NULL && echo->len >= 1) {
399 const GHashTable *echo_body;
401 echo_body = g_ptr_array_index (echo, 1);
402 if (echo_body != NULL)
403 message_body = tp_asv_get_string (echo_body, "content");
406 tp_chat_set_delivery_status (self, delivery_token,
407 EMPATHY_DELIVERY_STATUS_NONE);
408 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
409 delivery_error, delivery_dbus_error);
412 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
413 message, NULL, NULL);
417 handle_incoming_message (EmpathyTpChat *self,
421 EmpathyTpChatPriv *priv = GET_PRIV (self);
424 if (tp_message_is_delivery_report (message)) {
425 handle_delivery_report (self, message);
429 message_body = tp_message_to_text (message, NULL);
431 DEBUG ("Message %s (channel %s): %s",
432 pending ? "pending" : "received",
433 tp_proxy_get_object_path (priv->channel), message_body);
435 if (message_body == NULL) {
436 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
438 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
439 message, NULL, NULL);
443 tp_chat_build_message (self, message, TRUE);
445 g_free (message_body);
449 message_received_cb (TpTextChannel *channel,
453 handle_incoming_message (chat, message, FALSE);
457 message_sent_cb (TpTextChannel *channel,
459 TpMessageSendingFlags flags,
465 message_body = tp_message_to_text (message, NULL);
467 DEBUG ("Message sent: %s", message_body);
469 tp_chat_build_message (chat, message, FALSE);
471 g_free (message_body);
474 static TpChannelTextSendError
475 error_to_text_send_error (GError *error)
477 if (error->domain != TP_ERRORS)
478 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
480 switch (error->code) {
481 case TP_ERROR_OFFLINE:
482 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
483 case TP_ERROR_INVALID_HANDLE:
484 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
485 case TP_ERROR_PERMISSION_DENIED:
486 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
487 case TP_ERROR_NOT_IMPLEMENTED:
488 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
491 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
495 message_send_cb (GObject *source,
496 GAsyncResult *result,
499 EmpathyTpChat *chat = user_data;
500 TpTextChannel *channel = (TpTextChannel *) source;
502 GError *error = NULL;
504 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
505 DEBUG ("Error: %s", error->message);
507 /* FIXME: we should use the body of the message as first argument of the
508 * signal but can't easily get it as we just get a user_data pointer. Once
509 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
510 * to use the user_data pointer to pass the message and fix this. */
511 g_signal_emit (chat, signals[SEND_ERROR], 0,
512 NULL, error_to_text_send_error (error), NULL);
514 g_error_free (error);
517 tp_chat_set_delivery_status (chat, token,
518 EMPATHY_DELIVERY_STATUS_SENDING);
524 TpChannelChatState state;
528 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
529 EmpathyContact *contact,
534 TpChannelChatState state;
537 DEBUG ("Error: %s", error->message);
541 state = GPOINTER_TO_UINT (user_data);
542 DEBUG ("Chat state changed for %s (%d): %d",
543 empathy_contact_get_alias (contact),
544 empathy_contact_get_handle (contact), state);
546 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
550 tp_chat_state_changed_cb (TpChannel *channel,
552 TpChannelChatState state,
555 EmpathyTpChatPriv *priv = GET_PRIV (chat);
557 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
558 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
559 NULL, G_OBJECT (chat));
563 list_pending_messages (EmpathyTpChat *self)
565 EmpathyTpChatPriv *priv = GET_PRIV (self);
568 g_assert (priv->channel != NULL);
570 messages = tp_text_channel_get_pending_messages (
571 TP_TEXT_CHANNEL (priv->channel));
573 for (l = messages; l != NULL; l = g_list_next (l)) {
574 TpMessage *message = l->data;
576 handle_incoming_message (self, message, FALSE);
579 g_list_free (messages);
583 tp_chat_property_flags_changed_cb (TpProxy *proxy,
584 const GPtrArray *properties,
588 EmpathyTpChatPriv *priv = GET_PRIV (chat);
591 if (priv->channel == NULL)
594 if (!priv->had_properties_list || !properties) {
598 for (i = 0; i < properties->len; i++) {
599 GValueArray *prop_struct;
600 EmpathyTpChatProperty *property;
604 prop_struct = g_ptr_array_index (properties, i);
605 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
606 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
608 for (j = 0; j < priv->properties->len; j++) {
609 property = g_ptr_array_index (priv->properties, j);
610 if (property->id == id) {
611 property->flags = flags;
612 DEBUG ("property %s flags changed: %d",
613 property->name, property->flags);
621 tp_chat_properties_changed_cb (TpProxy *proxy,
622 const GPtrArray *properties,
626 EmpathyTpChatPriv *priv = GET_PRIV (chat);
629 if (priv->channel == NULL)
632 if (!priv->had_properties_list || !properties) {
636 for (i = 0; i < properties->len; i++) {
637 GValueArray *prop_struct;
638 EmpathyTpChatProperty *property;
642 prop_struct = g_ptr_array_index (properties, i);
643 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
644 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
646 for (j = 0; j < priv->properties->len; j++) {
647 property = g_ptr_array_index (priv->properties, j);
648 if (property->id == id) {
649 if (property->value) {
650 g_value_copy (src_value, property->value);
652 property->value = tp_g_value_slice_dup (src_value);
655 DEBUG ("property %s changed", property->name);
656 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
657 property->name, property->value);
665 tp_chat_get_properties_cb (TpProxy *proxy,
666 const GPtrArray *properties,
672 DEBUG ("Error getting properties: %s", error->message);
676 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
680 tp_chat_list_properties_cb (TpProxy *proxy,
681 const GPtrArray *properties,
686 EmpathyTpChatPriv *priv = GET_PRIV (chat);
690 if (priv->channel == NULL)
693 priv->had_properties_list = TRUE;
696 DEBUG ("Error listing properties: %s", error->message);
700 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
701 priv->properties = g_ptr_array_sized_new (properties->len);
702 for (i = 0; i < properties->len; i++) {
703 GValueArray *prop_struct;
704 EmpathyTpChatProperty *property;
706 prop_struct = g_ptr_array_index (properties, i);
707 property = g_slice_new0 (EmpathyTpChatProperty);
708 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
709 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
710 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
712 DEBUG ("Adding property name=%s id=%d flags=%d",
713 property->name, property->id, property->flags);
714 g_ptr_array_add (priv->properties, property);
715 if (property->flags & TP_PROPERTY_FLAG_READ) {
716 g_array_append_val (ids, property->id);
720 tp_cli_properties_interface_call_get_properties (proxy, -1,
722 tp_chat_get_properties_cb,
726 g_array_free (ids, TRUE);
730 empathy_tp_chat_set_property (EmpathyTpChat *chat,
734 EmpathyTpChatPriv *priv = GET_PRIV (chat);
735 EmpathyTpChatProperty *property;
738 if (!priv->had_properties_list) {
742 for (i = 0; i < priv->properties->len; i++) {
743 property = g_ptr_array_index (priv->properties, i);
744 if (!tp_strdiff (property->name, name)) {
745 GPtrArray *properties;
748 GValue dest_value = {0, };
750 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
754 g_value_init (&id, G_TYPE_UINT);
755 g_value_init (&dest_value, G_TYPE_VALUE);
756 g_value_set_uint (&id, property->id);
757 g_value_set_boxed (&dest_value, value);
759 prop = g_value_array_new (2);
760 g_value_array_append (prop, &id);
761 g_value_array_append (prop, &dest_value);
763 properties = g_ptr_array_sized_new (1);
764 g_ptr_array_add (properties, prop);
766 DEBUG ("Set property %s", name);
767 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
769 (tp_cli_properties_interface_callback_for_set_properties)
771 "Seting property", NULL,
774 g_ptr_array_free (properties, TRUE);
775 g_value_array_free (prop);
782 EmpathyTpChatProperty *
783 empathy_tp_chat_get_property (EmpathyTpChat *chat,
786 EmpathyTpChatPriv *priv = GET_PRIV (chat);
787 EmpathyTpChatProperty *property;
790 if (!priv->had_properties_list) {
794 for (i = 0; i < priv->properties->len; i++) {
795 property = g_ptr_array_index (priv->properties, i);
796 if (!tp_strdiff (property->name, name)) {
805 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
807 EmpathyTpChatPriv *priv = GET_PRIV (chat);
809 return priv->properties;
813 tp_chat_dispose (GObject *object)
815 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
816 EmpathyTpChatPriv *priv = GET_PRIV (self);
818 if (priv->dispose_has_run)
821 priv->dispose_has_run = TRUE;
823 tp_clear_object (&priv->account);
825 if (priv->connection != NULL)
826 g_object_unref (priv->connection);
827 priv->connection = NULL;
829 if (priv->channel != NULL) {
830 g_signal_handlers_disconnect_by_func (priv->channel,
831 tp_chat_invalidated_cb, self);
832 g_object_unref (priv->channel);
834 priv->channel = NULL;
836 if (priv->remote_contact != NULL)
837 g_object_unref (priv->remote_contact);
838 priv->remote_contact = NULL;
840 if (priv->user != NULL)
841 g_object_unref (priv->user);
844 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
845 g_queue_clear (priv->messages_queue);
847 g_queue_foreach (priv->pending_messages_queue,
848 (GFunc) g_object_unref, NULL);
849 g_queue_clear (priv->pending_messages_queue);
851 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
852 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
856 tp_chat_finalize (GObject *object)
858 EmpathyTpChatPriv *priv = GET_PRIV (object);
861 DEBUG ("Finalize: %p", object);
863 if (priv->properties) {
864 for (i = 0; i < priv->properties->len; i++) {
865 EmpathyTpChatProperty *property;
867 property = g_ptr_array_index (priv->properties, i);
868 g_free (property->name);
869 if (property->value) {
870 tp_g_value_slice_free (property->value);
872 g_slice_free (EmpathyTpChatProperty, property);
874 g_ptr_array_free (priv->properties, TRUE);
877 g_queue_free (priv->messages_queue);
878 g_queue_free (priv->pending_messages_queue);
879 g_hash_table_destroy (priv->messages_being_sent);
881 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
885 check_almost_ready (EmpathyTpChat *chat)
887 EmpathyTpChatPriv *priv = GET_PRIV (chat);
892 if (priv->user == NULL)
895 if (!priv->got_password_flags)
898 if (!priv->got_sms_channel)
901 /* We need either the members (room) or the remote contact (private chat).
902 * If the chat is protected by a password we can't get these information so
903 * consider the chat as ready so it can be presented to the user. */
904 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
905 priv->remote_contact == NULL)
908 /* We use the default factory so this feature should have been prepared */
909 g_assert (tp_proxy_is_prepared (priv->channel,
910 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
912 tp_g_signal_connect_object (priv->channel, "message-received",
913 G_CALLBACK (message_received_cb), chat, 0);
915 list_pending_messages (chat);
917 tp_g_signal_connect_object (priv->channel, "message-sent",
918 G_CALLBACK (message_sent_cb), chat, 0);
920 tp_g_signal_connect_object (priv->channel, "chat-state-changed",
921 G_CALLBACK (tp_chat_state_changed_cb), chat, 0);
927 tp_chat_update_remote_contact (EmpathyTpChat *chat)
929 EmpathyTpChatPriv *priv = GET_PRIV (chat);
930 EmpathyContact *contact = NULL;
931 TpHandle self_handle;
932 TpHandleType handle_type;
935 /* If this is a named chatroom, never pretend it is a private chat */
936 tp_channel_get_handle (priv->channel, &handle_type);
937 if (handle_type == TP_HANDLE_TYPE_ROOM) {
941 /* This is an MSN chat, but it's the new style where 1-1 chats don't
942 * have the group interface. If it has the conference interface, then
943 * it is indeed a MUC. */
944 if (tp_proxy_has_interface_by_id (priv->channel,
945 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
949 /* This is an MSN-like chat where anyone can join the chat at anytime.
950 * If there is only one non-self contact member, we are in a private
951 * chat and we set the "remote-contact" property to that contact. If
952 * there are more, set the "remote-contact" property to NULL and the
953 * UI will display a contact list. */
954 self_handle = tp_channel_group_get_self_handle (priv->channel);
955 for (l = priv->members; l; l = l->next) {
956 /* Skip self contact if member */
957 if (empathy_contact_get_handle (l->data) == self_handle) {
961 /* We have more than one remote contact, break */
962 if (contact != NULL) {
967 /* If we didn't find yet a remote contact, keep this one */
971 if (priv->remote_contact == contact) {
975 DEBUG ("Changing remote contact from %p to %p",
976 priv->remote_contact, contact);
978 if (priv->remote_contact) {
979 g_object_unref (priv->remote_contact);
982 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
983 g_object_notify (G_OBJECT (chat), "remote-contact");
987 tp_chat_got_added_contacts_cb (TpConnection *connection,
989 EmpathyContact * const * contacts,
991 const TpHandle *failed,
996 EmpathyTpChatPriv *priv = GET_PRIV (chat);
998 const TpIntSet *members;
1000 EmpathyContact *contact;
1003 DEBUG ("Error: %s", error->message);
1007 members = tp_channel_group_get_members (priv->channel);
1008 for (i = 0; i < n_contacts; i++) {
1009 contact = contacts[i];
1010 handle = empathy_contact_get_handle (contact);
1012 /* Make sure the contact is still member */
1013 if (tp_intset_is_member (members, handle)) {
1014 priv->members = g_list_prepend (priv->members,
1015 g_object_ref (contact));
1016 g_signal_emit_by_name (chat, "members-changed",
1017 contact, NULL, 0, NULL, TRUE);
1021 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1022 check_almost_ready (EMPATHY_TP_CHAT (chat));
1025 static EmpathyContact *
1026 chat_lookup_contact (EmpathyTpChat *chat,
1030 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1033 for (l = priv->members; l; l = l->next) {
1034 EmpathyContact *c = l->data;
1036 if (empathy_contact_get_handle (c) != handle) {
1041 /* Caller takes the reference. */
1042 priv->members = g_list_delete_link (priv->members, l);
1055 TpHandle old_handle;
1058 } ContactRenameData;
1060 static ContactRenameData *
1061 contact_rename_data_new (TpHandle handle,
1063 const gchar* message)
1065 ContactRenameData *data = g_new (ContactRenameData, 1);
1066 data->old_handle = handle;
1067 data->reason = reason;
1068 data->message = g_strdup (message);
1074 contact_rename_data_free (ContactRenameData* data)
1076 g_free (data->message);
1081 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1083 EmpathyContact * const * contacts,
1085 const TpHandle *failed,
1086 const GError *error,
1090 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1091 const TpIntSet *members;
1093 EmpathyContact *old = NULL, *new = NULL;
1094 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1097 DEBUG ("Error: %s", error->message);
1101 /* renamed members can only be delivered one at a time */
1102 g_warn_if_fail (n_contacts == 1);
1106 members = tp_channel_group_get_members (priv->channel);
1107 handle = empathy_contact_get_handle (new);
1109 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1110 rename_data->old_handle, TRUE);
1112 /* Make sure the contact is still member */
1113 if (tp_intset_is_member (members, handle)) {
1114 priv->members = g_list_prepend (priv->members,
1115 g_object_ref (new));
1118 g_signal_emit_by_name (chat, "member-renamed",
1119 old, new, rename_data->reason,
1120 rename_data->message);
1121 g_object_unref (old);
1125 if (priv->user == old) {
1126 /* We change our nick */
1127 tp_clear_object (&priv->user);
1128 priv->user = g_object_ref (new);
1131 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1132 check_almost_ready (EMPATHY_TP_CHAT (chat));
1137 tp_chat_group_members_changed_cb (TpChannel *self,
1141 GArray *local_pending,
1142 GArray *remote_pending,
1145 EmpathyTpChat *chat)
1147 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1148 EmpathyContact *contact;
1149 EmpathyContact *actor_contact = NULL;
1151 ContactRenameData *rename_data;
1152 TpHandle old_handle;
1154 /* Contact renamed */
1155 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1156 /* there can only be a single 'added' and a single 'removed' handle */
1157 if (removed->len != 1 || added->len != 1) {
1158 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1159 added->len, removed->len);
1163 old_handle = g_array_index (removed, guint, 0);
1165 rename_data = contact_rename_data_new (old_handle, reason, message);
1166 empathy_tp_contact_factory_get_from_handles (priv->connection,
1167 added->len, (TpHandle *) added->data,
1168 tp_chat_got_renamed_contacts_cb,
1169 rename_data, (GDestroyNotify) contact_rename_data_free,
1175 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1176 if (actor_contact == NULL) {
1177 /* FIXME: handle this a tad more gracefully: perhaps
1178 * the actor was a server op. We could use the
1179 * contact-ids detail of MembersChangedDetailed.
1181 DEBUG ("actor %u not a channel member", actor);
1185 /* Remove contacts that are not members anymore */
1186 for (i = 0; i < removed->len; i++) {
1187 contact = chat_lookup_contact (chat,
1188 g_array_index (removed, TpHandle, i), TRUE);
1190 if (contact != NULL) {
1191 g_signal_emit_by_name (chat, "members-changed", contact,
1192 actor_contact, reason, message,
1194 g_object_unref (contact);
1198 /* Request added contacts */
1199 if (added->len > 0) {
1200 empathy_tp_contact_factory_get_from_handles (priv->connection,
1201 added->len, (TpHandle *) added->data,
1202 tp_chat_got_added_contacts_cb, NULL, NULL,
1206 tp_chat_update_remote_contact (chat);
1208 if (actor_contact != NULL) {
1209 g_object_unref (actor_contact);
1214 tp_chat_got_remote_contact_cb (TpConnection *connection,
1215 EmpathyContact *contact,
1216 const GError *error,
1220 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1223 DEBUG ("Error: %s", error->message);
1224 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1228 priv->remote_contact = g_object_ref (contact);
1229 g_object_notify (chat, "remote-contact");
1231 check_almost_ready (EMPATHY_TP_CHAT (chat));
1235 tp_chat_got_self_contact_cb (TpConnection *connection,
1236 EmpathyContact *contact,
1237 const GError *error,
1241 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1244 DEBUG ("Error: %s", error->message);
1245 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1249 priv->user = g_object_ref (contact);
1250 empathy_contact_set_is_user (priv->user, TRUE);
1251 check_almost_ready (EMPATHY_TP_CHAT (chat));
1255 password_flags_changed_cb (TpChannel *channel,
1259 GObject *weak_object)
1261 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1262 EmpathyTpChatPriv *priv = GET_PRIV (self);
1263 gboolean was_needed, needed;
1265 was_needed = empathy_tp_chat_password_needed (self);
1267 priv->password_flags |= added;
1268 priv->password_flags ^= removed;
1270 needed = empathy_tp_chat_password_needed (self);
1272 if (was_needed != needed)
1273 g_object_notify (G_OBJECT (self), "password-needed");
1277 got_password_flags_cb (TpChannel *proxy,
1278 guint password_flags,
1279 const GError *error,
1281 GObject *weak_object)
1283 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1284 EmpathyTpChatPriv *priv = GET_PRIV (self);
1286 priv->got_password_flags = TRUE;
1287 priv->password_flags = password_flags;
1289 check_almost_ready (EMPATHY_TP_CHAT (self));
1293 sms_channel_changed_cb (TpChannel *channel,
1294 gboolean sms_channel,
1298 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1300 priv->sms_channel = sms_channel;
1302 g_object_notify (G_OBJECT (chat), "sms-channel");
1306 get_sms_channel_cb (TpProxy *channel,
1307 const GValue *value,
1308 const GError *in_error,
1312 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1314 if (in_error != NULL) {
1315 DEBUG ("Failed to get SMSChannel: %s", in_error->message);
1319 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
1321 priv->sms_channel = g_value_get_boolean (value);
1322 priv->got_sms_channel = TRUE;
1324 check_almost_ready (EMPATHY_TP_CHAT (chat));
1328 tp_chat_constructor (GType type,
1330 GObjectConstructParam *props)
1333 EmpathyTpChatPriv *priv;
1336 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1338 priv = GET_PRIV (chat);
1340 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1341 tp_g_signal_connect_object (priv->channel, "invalidated",
1342 G_CALLBACK (tp_chat_invalidated_cb),
1345 g_assert (tp_proxy_is_prepared (priv->connection,
1346 TP_CONNECTION_FEATURE_CAPABILITIES));
1348 if (tp_proxy_has_interface_by_id (priv->channel,
1349 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1350 const TpIntSet *members;
1353 /* Get self contact from the group's self handle */
1354 handle = tp_channel_group_get_self_handle (priv->channel);
1355 empathy_tp_contact_factory_get_from_handle (priv->connection,
1356 handle, tp_chat_got_self_contact_cb,
1359 /* Get initial member contacts */
1360 members = tp_channel_group_get_members (priv->channel);
1361 handles = tp_intset_to_array (members);
1362 empathy_tp_contact_factory_get_from_handles (priv->connection,
1363 handles->len, (TpHandle *) handles->data,
1364 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1366 priv->can_upgrade_to_muc = FALSE;
1368 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1369 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1371 TpCapabilities *caps;
1375 /* Get the self contact from the connection's self handle */
1376 handle = tp_connection_get_self_handle (priv->connection);
1377 empathy_tp_contact_factory_get_from_handle (priv->connection,
1378 handle, tp_chat_got_self_contact_cb,
1381 /* Get the remote contact */
1382 handle = tp_channel_get_handle (priv->channel, NULL);
1383 empathy_tp_contact_factory_get_from_handle (priv->connection,
1384 handle, tp_chat_got_remote_contact_cb,
1387 caps = tp_connection_get_capabilities (priv->connection);
1388 g_assert (caps != NULL);
1390 classes = tp_capabilities_get_channel_classes (caps);
1392 for (i = 0; i < classes->len; i++) {
1393 GValueArray *array = g_ptr_array_index (classes, i);
1394 const char **oprops = g_value_get_boxed (
1395 g_value_array_get_nth (array, 1));
1397 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1398 priv->can_upgrade_to_muc = TRUE;
1404 if (tp_proxy_has_interface_by_id (priv->channel,
1405 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1406 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1407 tp_chat_list_properties_cb,
1410 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1411 tp_chat_properties_changed_cb,
1413 G_OBJECT (chat), NULL);
1414 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1415 tp_chat_property_flags_changed_cb,
1417 G_OBJECT (chat), NULL);
1420 /* Check if the chat is password protected */
1421 if (tp_proxy_has_interface_by_id (priv->channel,
1422 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1423 priv->got_password_flags = FALSE;
1425 tp_cli_channel_interface_password_connect_to_password_flags_changed
1426 (priv->channel, password_flags_changed_cb, chat, NULL,
1427 G_OBJECT (chat), NULL);
1429 tp_cli_channel_interface_password_call_get_password_flags
1430 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1432 /* No Password interface, so no need to fetch the password flags */
1433 priv->got_password_flags = TRUE;
1436 /* Check if the chat is for SMS */
1437 if (tp_proxy_has_interface_by_id (priv->channel,
1438 TP_IFACE_QUARK_CHANNEL_INTERFACE_SMS)) {
1439 tp_cli_channel_interface_sms_connect_to_sms_channel_changed (
1441 sms_channel_changed_cb, chat, NULL, G_OBJECT (chat),
1444 tp_cli_dbus_properties_call_get (priv->channel, -1,
1445 TP_IFACE_CHANNEL_INTERFACE_SMS, "SMSChannel",
1446 get_sms_channel_cb, chat, NULL, G_OBJECT (chat));
1448 /* if there's no SMS support, then we're not waiting for it */
1449 priv->got_sms_channel = TRUE;
1456 tp_chat_get_property (GObject *object,
1461 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1462 EmpathyTpChatPriv *priv = GET_PRIV (object);
1466 g_value_set_object (value, priv->account);
1469 g_value_set_object (value, priv->channel);
1471 case PROP_REMOTE_CONTACT:
1472 g_value_set_object (value, priv->remote_contact);
1475 g_value_set_boolean (value, priv->ready);
1477 case PROP_PASSWORD_NEEDED:
1478 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1480 case PROP_SMS_CHANNEL:
1481 g_value_set_boolean (value, priv->sms_channel);
1483 case PROP_N_MESSAGES_SENDING:
1484 g_value_set_uint (value,
1485 g_hash_table_size (priv->messages_being_sent));
1488 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1494 tp_chat_set_property (GObject *object,
1496 const GValue *value,
1499 EmpathyTpChatPriv *priv = GET_PRIV (object);
1503 priv->account = g_value_dup_object (value);
1506 priv->channel = g_value_dup_object (value);
1509 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1515 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1517 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1519 object_class->dispose = tp_chat_dispose;
1520 object_class->finalize = tp_chat_finalize;
1521 object_class->constructor = tp_chat_constructor;
1522 object_class->get_property = tp_chat_get_property;
1523 object_class->set_property = tp_chat_set_property;
1525 g_object_class_install_property (object_class,
1527 g_param_spec_object ("account",
1529 "the account associated with the chat",
1532 G_PARAM_CONSTRUCT_ONLY |
1533 G_PARAM_STATIC_STRINGS));
1535 g_object_class_install_property (object_class,
1537 g_param_spec_object ("channel",
1538 "telepathy channel",
1539 "The text channel for the chat",
1542 G_PARAM_CONSTRUCT_ONLY));
1544 g_object_class_install_property (object_class,
1545 PROP_REMOTE_CONTACT,
1546 g_param_spec_object ("remote-contact",
1547 "The remote contact",
1548 "The remote contact if there is no group iface on the channel",
1549 EMPATHY_TYPE_CONTACT,
1552 g_object_class_install_property (object_class,
1554 g_param_spec_boolean ("ready",
1555 "Is the object ready",
1556 "This object can't be used until this becomes true",
1560 g_object_class_install_property (object_class,
1561 PROP_PASSWORD_NEEDED,
1562 g_param_spec_boolean ("password-needed",
1564 "TRUE if a password is needed to join the channel",
1568 g_object_class_install_property (object_class,
1570 g_param_spec_boolean ("sms-channel",
1572 "TRUE if channel is for sending SMSes",
1576 g_object_class_install_property (object_class,
1577 PROP_N_MESSAGES_SENDING,
1578 g_param_spec_uint ("n-messages-sending",
1579 "Num Messages Sending",
1580 "The number of messages being sent",
1585 signals[MESSAGE_RECEIVED] =
1586 g_signal_new ("message-received",
1587 G_TYPE_FROM_CLASS (klass),
1591 g_cclosure_marshal_VOID__OBJECT,
1593 1, EMPATHY_TYPE_MESSAGE);
1595 signals[SEND_ERROR] =
1596 g_signal_new ("send-error",
1597 G_TYPE_FROM_CLASS (klass),
1601 _empathy_marshal_VOID__STRING_UINT_STRING,
1603 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1605 signals[CHAT_STATE_CHANGED] =
1606 g_signal_new ("chat-state-changed",
1607 G_TYPE_FROM_CLASS (klass),
1611 _empathy_marshal_VOID__OBJECT_UINT,
1613 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1615 signals[PROPERTY_CHANGED] =
1616 g_signal_new ("property-changed",
1617 G_TYPE_FROM_CLASS (klass),
1621 _empathy_marshal_VOID__STRING_BOXED,
1623 2, G_TYPE_STRING, G_TYPE_VALUE);
1626 g_signal_new ("destroy",
1627 G_TYPE_FROM_CLASS (klass),
1631 g_cclosure_marshal_VOID__VOID,
1635 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1639 empathy_tp_chat_init (EmpathyTpChat *chat)
1641 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1642 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1645 priv->messages_queue = g_queue_new ();
1646 priv->pending_messages_queue = g_queue_new ();
1647 priv->messages_being_sent = g_hash_table_new_full (
1648 g_str_hash, g_str_equal, g_free, NULL);
1652 tp_chat_iface_init (EmpathyContactListIface *iface)
1654 iface->add = tp_chat_add;
1655 iface->remove = tp_chat_remove;
1656 iface->get_members = tp_chat_get_members;
1660 empathy_tp_chat_new (TpAccount *account,
1663 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1664 g_return_val_if_fail (TP_IS_TEXT_CHANNEL (channel), NULL);
1666 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1673 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1675 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1679 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1681 id = tp_channel_get_identifier (priv->channel);
1682 if (!EMP_STR_EMPTY (id))
1684 else if (priv->remote_contact)
1685 return empathy_contact_get_id (priv->remote_contact);
1692 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1694 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1696 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1697 g_return_val_if_fail (priv->ready, NULL);
1699 return priv->remote_contact;
1703 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1705 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1707 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1709 return priv->channel;
1713 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1715 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1717 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1719 return priv->account;
1723 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1725 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1727 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1729 return tp_channel_borrow_connection (priv->channel);
1732 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1734 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1736 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1742 empathy_tp_chat_send (EmpathyTpChat *chat,
1745 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1746 gchar *message_body;
1748 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1749 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1750 g_return_if_fail (priv->ready);
1752 message_body = tp_message_to_text (message, NULL);
1754 DEBUG ("Sending message: %s", message_body);
1756 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel),
1757 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1758 message_send_cb, chat);
1760 g_free (message_body);
1764 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1765 TpChannelChatState state)
1767 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1769 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1770 g_return_if_fail (priv->ready);
1772 if (tp_proxy_has_interface_by_id (priv->channel,
1773 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1774 DEBUG ("Set state: %d", state);
1775 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1778 "setting chat state",
1786 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1788 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1790 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1791 g_return_val_if_fail (priv->ready, NULL);
1793 return priv->pending_messages_queue->head;
1797 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1798 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1800 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1801 priv->channel, -1, ids, tp_chat_async_cb,
1802 "acknowledging received message", NULL, G_OBJECT (chat));
1806 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1807 EmpathyMessage *message) {
1808 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1809 GArray *message_ids;
1813 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1814 g_return_if_fail (priv->ready);
1816 if (!empathy_message_is_incoming (message))
1819 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1821 id = empathy_message_get_id (message);
1822 g_array_append_val (message_ids, id);
1823 acknowledge_messages (chat, message_ids);
1824 g_array_free (message_ids, TRUE);
1827 m = g_queue_find (priv->pending_messages_queue, message);
1828 g_assert (m != NULL);
1829 g_queue_delete_link (priv->pending_messages_queue, m);
1830 g_object_unref (message);
1834 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1835 const GSList *messages) {
1836 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1837 /* Copy messages as the messges list (probably is) our own */
1838 GSList *msgs = g_slist_copy ((GSList *) messages);
1841 GArray *message_ids;
1843 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1844 g_return_if_fail (priv->ready);
1846 length = g_slist_length ((GSList *) messages);
1851 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1853 for (l = msgs; l != NULL; l = g_slist_next (l)) {
1856 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1858 m = g_queue_find (priv->pending_messages_queue, message);
1859 g_assert (m != NULL);
1860 g_queue_delete_link (priv->pending_messages_queue, m);
1862 if (empathy_message_is_incoming (message)) {
1863 guint id = empathy_message_get_id (message);
1864 g_array_append_val (message_ids, id);
1866 g_object_unref (message);
1869 if (message_ids->len > 0)
1870 acknowledge_messages (chat, message_ids);
1872 g_array_free (message_ids, TRUE);
1873 g_slist_free (msgs);
1877 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
1879 empathy_tp_chat_acknowledge_messages (chat,
1880 (GSList *) empathy_tp_chat_get_pending_messages (chat));
1884 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1886 EmpathyTpChatPriv *priv = GET_PRIV (self);
1888 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1892 provide_password_cb (TpChannel *channel,
1894 const GError *error,
1896 GObject *weak_object)
1898 GSimpleAsyncResult *result = user_data;
1900 if (error != NULL) {
1901 g_simple_async_result_set_from_error (result, error);
1903 else if (!correct) {
1904 /* The current D-Bus API is a bit weird so re-use the
1905 * AuthenticationFailed error */
1906 g_simple_async_result_set_error (result, TP_ERRORS,
1907 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1910 g_simple_async_result_complete (result);
1911 g_object_unref (result);
1915 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1916 const gchar *password,
1917 GAsyncReadyCallback callback,
1920 EmpathyTpChatPriv *priv = GET_PRIV (self);
1921 GSimpleAsyncResult *result;
1923 result = g_simple_async_result_new (G_OBJECT (self),
1924 callback, user_data,
1925 empathy_tp_chat_provide_password_finish);
1927 tp_cli_channel_interface_password_call_provide_password
1928 (priv->channel, -1, password, provide_password_cb, result,
1929 NULL, G_OBJECT (self));
1933 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1934 GAsyncResult *result,
1937 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1941 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1942 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1948 * empathy_tp_chat_can_add_contact:
1950 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1951 * That is if this chat is a 1-to-1 channel that can be upgraded to
1952 * a MUC using the Conference interface or if the channel is a MUC.
1955 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1957 EmpathyTpChatPriv *priv;
1959 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1961 priv = GET_PRIV (self);
1963 return priv->can_upgrade_to_muc ||
1964 tp_proxy_has_interface_by_id (priv->channel,
1965 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1969 tp_channel_leave_async_cb (GObject *source_object,
1973 GError *error = NULL;
1975 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1976 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1978 g_error_free (error);
1983 empathy_tp_chat_leave (EmpathyTpChat *self,
1984 const gchar *message)
1986 EmpathyTpChatPriv *priv = GET_PRIV (self);
1988 DEBUG ("Leaving channel %s with message \"%s\"",
1989 tp_channel_get_identifier (priv->channel), message);
1991 tp_channel_leave_async (priv->channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1992 message, tp_channel_leave_async_cb, self);
1996 add_members_cb (TpChannel *proxy,
1997 const GError *error,
1999 GObject *weak_object)
2001 EmpathyTpChatPriv *priv = GET_PRIV (weak_object);
2003 if (error != NULL) {
2004 DEBUG ("Failed to join chat (%s): %s",
2005 tp_channel_get_identifier (priv->channel), error->message);
2010 empathy_tp_chat_join (EmpathyTpChat *self)
2012 EmpathyTpChatPriv *priv = GET_PRIV (self);
2013 TpHandle self_handle;
2016 self_handle = tp_channel_group_get_self_handle (priv->channel);
2018 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
2019 g_array_append_val (members, self_handle);
2021 tp_cli_channel_interface_group_call_add_members (priv->channel, -1, members,
2022 "", add_members_cb, NULL, NULL, G_OBJECT (self));
2024 g_array_free (members, TRUE);
2028 empathy_tp_chat_is_invited (EmpathyTpChat *self,
2031 EmpathyTpChatPriv *priv = GET_PRIV (self);
2032 TpHandle self_handle;
2034 if (!tp_proxy_has_interface (priv->channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
2037 self_handle = tp_channel_group_get_self_handle (priv->channel);
2038 if (self_handle == 0)
2041 return tp_channel_group_get_local_pending_info (priv->channel, self_handle,
2042 inviter, NULL, NULL);
2046 empathy_tp_chat_get_chat_state (EmpathyTpChat *chat,
2047 EmpathyContact *contact)
2049 EmpathyTpChatPriv *priv = GET_PRIV (chat);
2050 return tp_channel_get_chat_state (priv->channel,
2051 empathy_contact_get_handle (contact));
2055 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
2057 EmpathyTpChatPriv *priv = GET_PRIV (self);
2059 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
2065 empathy_tp_chat_is_sms_channel (EmpathyTpChat *self)
2067 EmpathyTpChatPriv *priv;
2069 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
2071 priv = GET_PRIV (self);
2073 return priv->sms_channel;