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;
66 static void tp_chat_iface_init (EmpathyContactListIface *iface);
87 static guint signals[LAST_SIGNAL];
89 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
90 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
93 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
96 tp_chat_invalidated_cb (TpProxy *proxy,
102 DEBUG ("Channel invalidated: %s", message);
103 g_signal_emit (chat, signals[DESTROY], 0);
107 tp_chat_async_cb (TpChannel *proxy,
110 GObject *weak_object)
113 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
118 create_conference_cb (GObject *source,
119 GAsyncResult *result,
122 GError *error = NULL;
124 if (!tp_account_channel_request_create_channel_finish (
125 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
126 DEBUG ("Failed to create conference channel: %s", error->message);
127 g_error_free (error);
132 tp_chat_add (EmpathyContactList *list,
133 EmpathyContact *contact,
134 const gchar *message)
136 EmpathyTpChatPriv *priv = GET_PRIV (list);
138 if (tp_proxy_has_interface_by_id (priv->channel,
139 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
141 GArray handles = {(gchar *) &handle, 1};
143 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
144 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
146 handle = empathy_contact_get_handle (contact);
147 tp_cli_channel_interface_group_call_add_members (priv->channel,
148 -1, &handles, NULL, NULL, NULL, NULL, NULL);
149 } else if (priv->can_upgrade_to_muc) {
150 TpAccountChannelRequest *req;
152 const char *object_path;
153 GPtrArray channels = { (gpointer *) &object_path, 1 };
154 const char *invitees[2] = { NULL, };
156 invitees[0] = empathy_contact_get_id (contact);
157 object_path = tp_proxy_get_object_path (priv->channel);
160 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
161 TP_IFACE_CHANNEL_TYPE_TEXT,
162 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
164 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
165 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
166 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
167 G_TYPE_STRV, invitees,
168 /* FIXME: InvitationMessage ? */
171 req = tp_account_channel_request_new (priv->account, props,
172 TP_USER_ACTION_TIME_NOT_USER_ACTION);
174 /* Although this is a MUC, it's anonymous, so CreateChannel is
176 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
177 NULL, create_conference_cb, NULL);
179 g_object_unref (req);
180 g_hash_table_unref (props);
182 g_warning ("Cannot add to this channel");
187 tp_chat_remove (EmpathyContactList *list,
188 EmpathyContact *contact,
189 const gchar *message)
191 EmpathyTpChatPriv *priv = GET_PRIV (list);
193 GArray handles = {(gchar *) &handle, 1};
195 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
196 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
198 handle = empathy_contact_get_handle (contact);
199 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
206 tp_chat_get_members (EmpathyContactList *list)
208 EmpathyTpChatPriv *priv = GET_PRIV (list);
209 GList *members = NULL;
211 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
214 members = g_list_copy (priv->members);
215 g_list_foreach (members, (GFunc) g_object_ref, NULL);
217 members = g_list_prepend (members, g_object_ref (priv->user));
218 if (priv->remote_contact != NULL)
219 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
226 check_ready (EmpathyTpChat *chat)
228 EmpathyTpChatPriv *priv = GET_PRIV (chat);
233 if (g_queue_get_length (priv->messages_queue) > 0)
239 g_object_notify (G_OBJECT (chat), "ready");
243 tp_chat_emit_queued_messages (EmpathyTpChat *chat)
245 EmpathyTpChatPriv *priv = GET_PRIV (chat);
246 EmpathyMessage *message;
248 /* Check if we can now emit some queued messages */
249 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
250 if (empathy_message_get_sender (message) == NULL) {
254 DEBUG ("Queued message ready");
255 g_queue_pop_head (priv->messages_queue);
256 g_queue_push_tail (priv->pending_messages_queue, message);
257 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
264 tp_chat_got_sender_cb (TpConnection *connection,
265 EmpathyContact *contact,
270 EmpathyTpChatPriv *priv = GET_PRIV (chat);
273 DEBUG ("Error: %s", error->message);
274 /* Do not block the message queue, just drop this message */
275 g_queue_remove (priv->messages_queue, message);
277 empathy_message_set_sender (message, contact);
280 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (chat));
284 tp_chat_build_message (EmpathyTpChat *chat,
288 EmpathyTpChatPriv *priv;
289 EmpathyMessage *message;
292 priv = GET_PRIV (chat);
294 message = empathy_message_new_from_tp_message (msg, incoming);
295 /* FIXME: this is actually a lie for incoming messages. */
296 empathy_message_set_receiver (message, priv->user);
298 g_queue_push_tail (priv->messages_queue, message);
300 sender = tp_signalled_message_get_sender (msg);
301 g_assert (sender != NULL);
303 if (tp_contact_get_handle (sender) == 0) {
304 empathy_message_set_sender (message, priv->user);
305 tp_chat_emit_queued_messages (chat);
307 empathy_tp_contact_factory_get_from_handle (priv->connection,
308 tp_contact_get_handle (sender),
309 tp_chat_got_sender_cb,
310 message, NULL, G_OBJECT (chat));
315 handle_delivery_report (EmpathyTpChat *self,
318 EmpathyTpChatPriv *priv = GET_PRIV (self);
319 TpDeliveryStatus delivery_status;
320 const GHashTable *header;
321 TpChannelTextSendError delivery_error;
324 const gchar *message_body = NULL;
325 const gchar *delivery_dbus_error;
327 header = tp_message_peek (message, 0);
331 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
332 if (!valid || delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED)
335 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
337 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
339 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
341 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
342 * TpMessage. (fdo #35884) */
343 echo = tp_asv_get_boxed (header, "delivery-echo",
344 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
345 if (echo != NULL && echo->len >= 1) {
346 const GHashTable *echo_body;
348 echo_body = g_ptr_array_index (echo, 1);
349 if (echo_body != NULL)
350 message_body = tp_asv_get_string (echo_body, "content");
353 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
354 delivery_error, delivery_dbus_error);
357 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
358 message, NULL, NULL);
362 handle_incoming_message (EmpathyTpChat *self,
366 EmpathyTpChatPriv *priv = GET_PRIV (self);
369 if (tp_message_is_delivery_report (message)) {
370 handle_delivery_report (self, message);
374 message_body = tp_message_to_text (message, NULL);
376 DEBUG ("Message %s (channel %s): %s",
377 pending ? "pending" : "received",
378 tp_proxy_get_object_path (priv->channel), message_body);
380 if (message_body == NULL) {
381 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
383 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (priv->channel),
384 message, NULL, NULL);
388 tp_chat_build_message (self, message, TRUE);
390 g_free (message_body);
394 message_received_cb (TpTextChannel *channel,
398 handle_incoming_message (chat, message, FALSE);
402 message_sent_cb (TpTextChannel *channel,
404 TpMessageSendingFlags flags,
410 message_body = tp_message_to_text (message, NULL);
412 DEBUG ("Message sent: %s", message_body);
414 tp_chat_build_message (chat, message, FALSE);
416 g_free (message_body);
419 static TpChannelTextSendError
420 error_to_text_send_error (GError *error)
422 if (error->domain != TP_ERRORS)
423 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
425 switch (error->code) {
426 case TP_ERROR_OFFLINE:
427 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
428 case TP_ERROR_INVALID_HANDLE:
429 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
430 case TP_ERROR_PERMISSION_DENIED:
431 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
432 case TP_ERROR_NOT_IMPLEMENTED:
433 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
436 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
440 message_send_cb (GObject *source,
441 GAsyncResult *result,
444 EmpathyTpChat *chat = user_data;
445 TpTextChannel *channel = (TpTextChannel *) source;
446 GError *error = NULL;
448 if (!tp_text_channel_send_message_finish (channel, result, NULL, &error)) {
449 DEBUG ("Error: %s", error->message);
451 /* FIXME: we should use the body of the message as first argument of the
452 * signal but can't easily get it as we just get a user_data pointer. Once
453 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
454 * to use the user_data pointer to pass the message and fix this. */
455 g_signal_emit (chat, signals[SEND_ERROR], 0,
456 NULL, error_to_text_send_error (error), NULL);
458 g_error_free (error);
464 TpChannelChatState state;
468 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
469 EmpathyContact *contact,
474 TpChannelChatState state;
477 DEBUG ("Error: %s", error->message);
481 state = GPOINTER_TO_UINT (user_data);
482 DEBUG ("Chat state changed for %s (%d): %d",
483 empathy_contact_get_alias (contact),
484 empathy_contact_get_handle (contact), state);
486 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
490 tp_chat_state_changed_cb (TpChannel *channel,
492 TpChannelChatState state,
495 EmpathyTpChatPriv *priv = GET_PRIV (chat);
497 empathy_tp_contact_factory_get_from_handle (priv->connection, handle,
498 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
499 NULL, G_OBJECT (chat));
503 list_pending_messages (EmpathyTpChat *self)
505 EmpathyTpChatPriv *priv = GET_PRIV (self);
508 g_assert (priv->channel != NULL);
510 messages = tp_text_channel_get_pending_messages (
511 TP_TEXT_CHANNEL (priv->channel));
513 for (l = messages; l != NULL; l = g_list_next (l)) {
514 TpMessage *message = l->data;
516 handle_incoming_message (self, message, FALSE);
519 g_list_free (messages);
523 tp_chat_property_flags_changed_cb (TpProxy *proxy,
524 const GPtrArray *properties,
528 EmpathyTpChatPriv *priv = GET_PRIV (chat);
531 if (priv->channel == NULL)
534 if (!priv->had_properties_list || !properties) {
538 for (i = 0; i < properties->len; i++) {
539 GValueArray *prop_struct;
540 EmpathyTpChatProperty *property;
544 prop_struct = g_ptr_array_index (properties, i);
545 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
546 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
548 for (j = 0; j < priv->properties->len; j++) {
549 property = g_ptr_array_index (priv->properties, j);
550 if (property->id == id) {
551 property->flags = flags;
552 DEBUG ("property %s flags changed: %d",
553 property->name, property->flags);
561 tp_chat_properties_changed_cb (TpProxy *proxy,
562 const GPtrArray *properties,
566 EmpathyTpChatPriv *priv = GET_PRIV (chat);
569 if (priv->channel == NULL)
572 if (!priv->had_properties_list || !properties) {
576 for (i = 0; i < properties->len; i++) {
577 GValueArray *prop_struct;
578 EmpathyTpChatProperty *property;
582 prop_struct = g_ptr_array_index (properties, i);
583 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
584 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
586 for (j = 0; j < priv->properties->len; j++) {
587 property = g_ptr_array_index (priv->properties, j);
588 if (property->id == id) {
589 if (property->value) {
590 g_value_copy (src_value, property->value);
592 property->value = tp_g_value_slice_dup (src_value);
595 DEBUG ("property %s changed", property->name);
596 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
597 property->name, property->value);
605 tp_chat_get_properties_cb (TpProxy *proxy,
606 const GPtrArray *properties,
612 DEBUG ("Error getting properties: %s", error->message);
616 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
620 tp_chat_list_properties_cb (TpProxy *proxy,
621 const GPtrArray *properties,
626 EmpathyTpChatPriv *priv = GET_PRIV (chat);
630 if (priv->channel == NULL)
633 priv->had_properties_list = TRUE;
636 DEBUG ("Error listing properties: %s", error->message);
640 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
641 priv->properties = g_ptr_array_sized_new (properties->len);
642 for (i = 0; i < properties->len; i++) {
643 GValueArray *prop_struct;
644 EmpathyTpChatProperty *property;
646 prop_struct = g_ptr_array_index (properties, i);
647 property = g_slice_new0 (EmpathyTpChatProperty);
648 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
649 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
650 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
652 DEBUG ("Adding property name=%s id=%d flags=%d",
653 property->name, property->id, property->flags);
654 g_ptr_array_add (priv->properties, property);
655 if (property->flags & TP_PROPERTY_FLAG_READ) {
656 g_array_append_val (ids, property->id);
660 tp_cli_properties_interface_call_get_properties (proxy, -1,
662 tp_chat_get_properties_cb,
666 g_array_free (ids, TRUE);
670 empathy_tp_chat_set_property (EmpathyTpChat *chat,
674 EmpathyTpChatPriv *priv = GET_PRIV (chat);
675 EmpathyTpChatProperty *property;
678 if (!priv->had_properties_list) {
682 for (i = 0; i < priv->properties->len; i++) {
683 property = g_ptr_array_index (priv->properties, i);
684 if (!tp_strdiff (property->name, name)) {
685 GPtrArray *properties;
688 GValue dest_value = {0, };
690 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
694 g_value_init (&id, G_TYPE_UINT);
695 g_value_init (&dest_value, G_TYPE_VALUE);
696 g_value_set_uint (&id, property->id);
697 g_value_set_boxed (&dest_value, value);
699 prop = g_value_array_new (2);
700 g_value_array_append (prop, &id);
701 g_value_array_append (prop, &dest_value);
703 properties = g_ptr_array_sized_new (1);
704 g_ptr_array_add (properties, prop);
706 DEBUG ("Set property %s", name);
707 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
709 (tp_cli_properties_interface_callback_for_set_properties)
711 "Seting property", NULL,
714 g_ptr_array_free (properties, TRUE);
715 g_value_array_free (prop);
722 EmpathyTpChatProperty *
723 empathy_tp_chat_get_property (EmpathyTpChat *chat,
726 EmpathyTpChatPriv *priv = GET_PRIV (chat);
727 EmpathyTpChatProperty *property;
730 if (!priv->had_properties_list) {
734 for (i = 0; i < priv->properties->len; i++) {
735 property = g_ptr_array_index (priv->properties, i);
736 if (!tp_strdiff (property->name, name)) {
745 empathy_tp_chat_get_properties (EmpathyTpChat *chat)
747 EmpathyTpChatPriv *priv = GET_PRIV (chat);
749 return priv->properties;
753 tp_chat_dispose (GObject *object)
755 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
756 EmpathyTpChatPriv *priv = GET_PRIV (self);
758 if (priv->dispose_has_run)
761 priv->dispose_has_run = TRUE;
763 tp_clear_object (&priv->account);
765 if (priv->connection != NULL)
766 g_object_unref (priv->connection);
767 priv->connection = NULL;
769 if (priv->channel != NULL) {
770 g_signal_handlers_disconnect_by_func (priv->channel,
771 tp_chat_invalidated_cb, self);
772 g_object_unref (priv->channel);
774 priv->channel = NULL;
776 if (priv->remote_contact != NULL)
777 g_object_unref (priv->remote_contact);
778 priv->remote_contact = NULL;
780 if (priv->user != NULL)
781 g_object_unref (priv->user);
784 g_queue_foreach (priv->messages_queue, (GFunc) g_object_unref, NULL);
785 g_queue_clear (priv->messages_queue);
787 g_queue_foreach (priv->pending_messages_queue,
788 (GFunc) g_object_unref, NULL);
789 g_queue_clear (priv->pending_messages_queue);
791 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
792 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
796 tp_chat_finalize (GObject *object)
798 EmpathyTpChatPriv *priv = GET_PRIV (object);
801 DEBUG ("Finalize: %p", object);
803 if (priv->properties) {
804 for (i = 0; i < priv->properties->len; i++) {
805 EmpathyTpChatProperty *property;
807 property = g_ptr_array_index (priv->properties, i);
808 g_free (property->name);
809 if (property->value) {
810 tp_g_value_slice_free (property->value);
812 g_slice_free (EmpathyTpChatProperty, property);
814 g_ptr_array_free (priv->properties, TRUE);
817 g_queue_free (priv->messages_queue);
818 g_queue_free (priv->pending_messages_queue);
820 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
824 check_almost_ready (EmpathyTpChat *chat)
826 EmpathyTpChatPriv *priv = GET_PRIV (chat);
831 if (priv->user == NULL)
834 if (!priv->got_password_flags)
837 if (!priv->got_sms_channel)
840 /* We need either the members (room) or the remote contact (private chat).
841 * If the chat is protected by a password we can't get these information so
842 * consider the chat as ready so it can be presented to the user. */
843 if (!empathy_tp_chat_password_needed (chat) && priv->members == NULL &&
844 priv->remote_contact == NULL)
847 /* We use the default factory so this feature should have been prepared */
848 g_assert (tp_proxy_is_prepared (priv->channel,
849 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
851 tp_g_signal_connect_object (priv->channel, "message-received",
852 G_CALLBACK (message_received_cb), chat, 0);
854 list_pending_messages (chat);
856 tp_g_signal_connect_object (priv->channel, "message-sent",
857 G_CALLBACK (message_sent_cb), chat, 0);
859 tp_g_signal_connect_object (priv->channel, "chat-state-changed",
860 G_CALLBACK (tp_chat_state_changed_cb), chat, 0);
866 tp_chat_update_remote_contact (EmpathyTpChat *chat)
868 EmpathyTpChatPriv *priv = GET_PRIV (chat);
869 EmpathyContact *contact = NULL;
870 TpHandle self_handle;
871 TpHandleType handle_type;
874 /* If this is a named chatroom, never pretend it is a private chat */
875 tp_channel_get_handle (priv->channel, &handle_type);
876 if (handle_type == TP_HANDLE_TYPE_ROOM) {
880 /* This is an MSN chat, but it's the new style where 1-1 chats don't
881 * have the group interface. If it has the conference interface, then
882 * it is indeed a MUC. */
883 if (tp_proxy_has_interface_by_id (priv->channel,
884 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
888 /* This is an MSN-like chat where anyone can join the chat at anytime.
889 * If there is only one non-self contact member, we are in a private
890 * chat and we set the "remote-contact" property to that contact. If
891 * there are more, set the "remote-contact" property to NULL and the
892 * UI will display a contact list. */
893 self_handle = tp_channel_group_get_self_handle (priv->channel);
894 for (l = priv->members; l; l = l->next) {
895 /* Skip self contact if member */
896 if (empathy_contact_get_handle (l->data) == self_handle) {
900 /* We have more than one remote contact, break */
901 if (contact != NULL) {
906 /* If we didn't find yet a remote contact, keep this one */
910 if (priv->remote_contact == contact) {
914 DEBUG ("Changing remote contact from %p to %p",
915 priv->remote_contact, contact);
917 if (priv->remote_contact) {
918 g_object_unref (priv->remote_contact);
921 priv->remote_contact = contact ? g_object_ref (contact) : NULL;
922 g_object_notify (G_OBJECT (chat), "remote-contact");
926 tp_chat_got_added_contacts_cb (TpConnection *connection,
928 EmpathyContact * const * contacts,
930 const TpHandle *failed,
935 EmpathyTpChatPriv *priv = GET_PRIV (chat);
937 const TpIntSet *members;
939 EmpathyContact *contact;
942 DEBUG ("Error: %s", error->message);
946 members = tp_channel_group_get_members (priv->channel);
947 for (i = 0; i < n_contacts; i++) {
948 contact = contacts[i];
949 handle = empathy_contact_get_handle (contact);
951 /* Make sure the contact is still member */
952 if (tp_intset_is_member (members, handle)) {
953 priv->members = g_list_prepend (priv->members,
954 g_object_ref (contact));
955 g_signal_emit_by_name (chat, "members-changed",
956 contact, NULL, 0, NULL, TRUE);
960 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
961 check_almost_ready (EMPATHY_TP_CHAT (chat));
964 static EmpathyContact *
965 chat_lookup_contact (EmpathyTpChat *chat,
969 EmpathyTpChatPriv *priv = GET_PRIV (chat);
972 for (l = priv->members; l; l = l->next) {
973 EmpathyContact *c = l->data;
975 if (empathy_contact_get_handle (c) != handle) {
980 /* Caller takes the reference. */
981 priv->members = g_list_delete_link (priv->members, l);
999 static ContactRenameData *
1000 contact_rename_data_new (TpHandle handle,
1002 const gchar* message)
1004 ContactRenameData *data = g_new (ContactRenameData, 1);
1005 data->old_handle = handle;
1006 data->reason = reason;
1007 data->message = g_strdup (message);
1013 contact_rename_data_free (ContactRenameData* data)
1015 g_free (data->message);
1020 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1022 EmpathyContact * const * contacts,
1024 const TpHandle *failed,
1025 const GError *error,
1029 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1030 const TpIntSet *members;
1032 EmpathyContact *old = NULL, *new = NULL;
1033 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1036 DEBUG ("Error: %s", error->message);
1040 /* renamed members can only be delivered one at a time */
1041 g_warn_if_fail (n_contacts == 1);
1045 members = tp_channel_group_get_members (priv->channel);
1046 handle = empathy_contact_get_handle (new);
1048 old = chat_lookup_contact (EMPATHY_TP_CHAT (chat),
1049 rename_data->old_handle, TRUE);
1051 /* Make sure the contact is still member */
1052 if (tp_intset_is_member (members, handle)) {
1053 priv->members = g_list_prepend (priv->members,
1054 g_object_ref (new));
1057 g_signal_emit_by_name (chat, "member-renamed",
1058 old, new, rename_data->reason,
1059 rename_data->message);
1060 g_object_unref (old);
1064 if (priv->user == old) {
1065 /* We change our nick */
1066 tp_clear_object (&priv->user);
1067 priv->user = g_object_ref (new);
1070 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1071 check_almost_ready (EMPATHY_TP_CHAT (chat));
1076 tp_chat_group_members_changed_cb (TpChannel *self,
1080 GArray *local_pending,
1081 GArray *remote_pending,
1084 EmpathyTpChat *chat)
1086 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1087 EmpathyContact *contact;
1088 EmpathyContact *actor_contact = NULL;
1090 ContactRenameData *rename_data;
1091 TpHandle old_handle;
1093 /* Contact renamed */
1094 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1095 /* there can only be a single 'added' and a single 'removed' handle */
1096 if (removed->len != 1 || added->len != 1) {
1097 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1098 added->len, removed->len);
1102 old_handle = g_array_index (removed, guint, 0);
1104 rename_data = contact_rename_data_new (old_handle, reason, message);
1105 empathy_tp_contact_factory_get_from_handles (priv->connection,
1106 added->len, (TpHandle *) added->data,
1107 tp_chat_got_renamed_contacts_cb,
1108 rename_data, (GDestroyNotify) contact_rename_data_free,
1114 actor_contact = chat_lookup_contact (chat, actor, FALSE);
1115 if (actor_contact == NULL) {
1116 /* FIXME: handle this a tad more gracefully: perhaps
1117 * the actor was a server op. We could use the
1118 * contact-ids detail of MembersChangedDetailed.
1120 DEBUG ("actor %u not a channel member", actor);
1124 /* Remove contacts that are not members anymore */
1125 for (i = 0; i < removed->len; i++) {
1126 contact = chat_lookup_contact (chat,
1127 g_array_index (removed, TpHandle, i), TRUE);
1129 if (contact != NULL) {
1130 g_signal_emit_by_name (chat, "members-changed", contact,
1131 actor_contact, reason, message,
1133 g_object_unref (contact);
1137 /* Request added contacts */
1138 if (added->len > 0) {
1139 empathy_tp_contact_factory_get_from_handles (priv->connection,
1140 added->len, (TpHandle *) added->data,
1141 tp_chat_got_added_contacts_cb, NULL, NULL,
1145 tp_chat_update_remote_contact (chat);
1147 if (actor_contact != NULL) {
1148 g_object_unref (actor_contact);
1153 tp_chat_got_remote_contact_cb (TpConnection *connection,
1154 EmpathyContact *contact,
1155 const GError *error,
1159 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1162 DEBUG ("Error: %s", error->message);
1163 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1167 priv->remote_contact = g_object_ref (contact);
1168 g_object_notify (chat, "remote-contact");
1170 check_almost_ready (EMPATHY_TP_CHAT (chat));
1174 tp_chat_got_self_contact_cb (TpConnection *connection,
1175 EmpathyContact *contact,
1176 const GError *error,
1180 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1183 DEBUG ("Error: %s", error->message);
1184 empathy_tp_chat_leave (EMPATHY_TP_CHAT (chat), "");
1188 priv->user = g_object_ref (contact);
1189 empathy_contact_set_is_user (priv->user, TRUE);
1190 check_almost_ready (EMPATHY_TP_CHAT (chat));
1194 password_flags_changed_cb (TpChannel *channel,
1198 GObject *weak_object)
1200 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1201 EmpathyTpChatPriv *priv = GET_PRIV (self);
1202 gboolean was_needed, needed;
1204 was_needed = empathy_tp_chat_password_needed (self);
1206 priv->password_flags |= added;
1207 priv->password_flags ^= removed;
1209 needed = empathy_tp_chat_password_needed (self);
1211 if (was_needed != needed)
1212 g_object_notify (G_OBJECT (self), "password-needed");
1216 got_password_flags_cb (TpChannel *proxy,
1217 guint password_flags,
1218 const GError *error,
1220 GObject *weak_object)
1222 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1223 EmpathyTpChatPriv *priv = GET_PRIV (self);
1225 priv->got_password_flags = TRUE;
1226 priv->password_flags = password_flags;
1228 check_almost_ready (EMPATHY_TP_CHAT (self));
1232 sms_channel_changed_cb (TpChannel *channel,
1233 gboolean sms_channel,
1237 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1239 priv->sms_channel = sms_channel;
1241 g_object_notify (G_OBJECT (chat), "sms-channel");
1245 get_sms_channel_cb (TpProxy *channel,
1246 const GValue *value,
1247 const GError *in_error,
1251 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1253 if (in_error != NULL) {
1254 DEBUG ("Failed to get SMSChannel: %s", in_error->message);
1258 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
1260 priv->sms_channel = g_value_get_boolean (value);
1261 priv->got_sms_channel = TRUE;
1263 check_almost_ready (EMPATHY_TP_CHAT (chat));
1267 tp_chat_constructor (GType type,
1269 GObjectConstructParam *props)
1272 EmpathyTpChatPriv *priv;
1275 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1277 priv = GET_PRIV (chat);
1279 priv->connection = g_object_ref (tp_account_get_connection (priv->account));
1280 tp_g_signal_connect_object (priv->channel, "invalidated",
1281 G_CALLBACK (tp_chat_invalidated_cb),
1284 g_assert (tp_proxy_is_prepared (priv->connection,
1285 TP_CONNECTION_FEATURE_CAPABILITIES));
1287 if (tp_proxy_has_interface_by_id (priv->channel,
1288 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1289 const TpIntSet *members;
1292 /* Get self contact from the group's self handle */
1293 handle = tp_channel_group_get_self_handle (priv->channel);
1294 empathy_tp_contact_factory_get_from_handle (priv->connection,
1295 handle, tp_chat_got_self_contact_cb,
1298 /* Get initial member contacts */
1299 members = tp_channel_group_get_members (priv->channel);
1300 handles = tp_intset_to_array (members);
1301 empathy_tp_contact_factory_get_from_handles (priv->connection,
1302 handles->len, (TpHandle *) handles->data,
1303 tp_chat_got_added_contacts_cb, NULL, NULL, chat);
1305 priv->can_upgrade_to_muc = FALSE;
1307 tp_g_signal_connect_object (priv->channel, "group-members-changed",
1308 G_CALLBACK (tp_chat_group_members_changed_cb), chat, 0);
1310 TpCapabilities *caps;
1314 /* Get the self contact from the connection's self handle */
1315 handle = tp_connection_get_self_handle (priv->connection);
1316 empathy_tp_contact_factory_get_from_handle (priv->connection,
1317 handle, tp_chat_got_self_contact_cb,
1320 /* Get the remote contact */
1321 handle = tp_channel_get_handle (priv->channel, NULL);
1322 empathy_tp_contact_factory_get_from_handle (priv->connection,
1323 handle, tp_chat_got_remote_contact_cb,
1326 caps = tp_connection_get_capabilities (priv->connection);
1327 g_assert (caps != NULL);
1329 classes = tp_capabilities_get_channel_classes (caps);
1331 for (i = 0; i < classes->len; i++) {
1332 GValueArray *array = g_ptr_array_index (classes, i);
1333 const char **oprops = g_value_get_boxed (
1334 g_value_array_get_nth (array, 1));
1336 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1337 priv->can_upgrade_to_muc = TRUE;
1343 if (tp_proxy_has_interface_by_id (priv->channel,
1344 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1345 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
1346 tp_chat_list_properties_cb,
1349 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
1350 tp_chat_properties_changed_cb,
1352 G_OBJECT (chat), NULL);
1353 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
1354 tp_chat_property_flags_changed_cb,
1356 G_OBJECT (chat), NULL);
1359 /* Check if the chat is password protected */
1360 if (tp_proxy_has_interface_by_id (priv->channel,
1361 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1362 priv->got_password_flags = FALSE;
1364 tp_cli_channel_interface_password_connect_to_password_flags_changed
1365 (priv->channel, password_flags_changed_cb, chat, NULL,
1366 G_OBJECT (chat), NULL);
1368 tp_cli_channel_interface_password_call_get_password_flags
1369 (priv->channel, -1, got_password_flags_cb, chat, NULL, chat);
1371 /* No Password interface, so no need to fetch the password flags */
1372 priv->got_password_flags = TRUE;
1375 /* Check if the chat is for SMS */
1376 if (tp_proxy_has_interface_by_id (priv->channel,
1377 TP_IFACE_QUARK_CHANNEL_INTERFACE_SMS)) {
1378 tp_cli_channel_interface_sms_connect_to_sms_channel_changed (
1380 sms_channel_changed_cb, chat, NULL, G_OBJECT (chat),
1383 tp_cli_dbus_properties_call_get (priv->channel, -1,
1384 TP_IFACE_CHANNEL_INTERFACE_SMS, "SMSChannel",
1385 get_sms_channel_cb, chat, NULL, G_OBJECT (chat));
1387 /* if there's no SMS support, then we're not waiting for it */
1388 priv->got_sms_channel = TRUE;
1395 tp_chat_get_property (GObject *object,
1400 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1401 EmpathyTpChatPriv *priv = GET_PRIV (object);
1405 g_value_set_object (value, priv->account);
1408 g_value_set_object (value, priv->channel);
1410 case PROP_REMOTE_CONTACT:
1411 g_value_set_object (value, priv->remote_contact);
1414 g_value_set_boolean (value, priv->ready);
1416 case PROP_PASSWORD_NEEDED:
1417 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1419 case PROP_SMS_CHANNEL:
1420 g_value_set_boolean (value, priv->sms_channel);
1423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1429 tp_chat_set_property (GObject *object,
1431 const GValue *value,
1434 EmpathyTpChatPriv *priv = GET_PRIV (object);
1438 priv->account = g_value_dup_object (value);
1441 priv->channel = g_value_dup_object (value);
1444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1450 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1452 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1454 object_class->dispose = tp_chat_dispose;
1455 object_class->finalize = tp_chat_finalize;
1456 object_class->constructor = tp_chat_constructor;
1457 object_class->get_property = tp_chat_get_property;
1458 object_class->set_property = tp_chat_set_property;
1460 g_object_class_install_property (object_class,
1462 g_param_spec_object ("account",
1464 "the account associated with the chat",
1467 G_PARAM_CONSTRUCT_ONLY |
1468 G_PARAM_STATIC_STRINGS));
1470 g_object_class_install_property (object_class,
1472 g_param_spec_object ("channel",
1473 "telepathy channel",
1474 "The text channel for the chat",
1477 G_PARAM_CONSTRUCT_ONLY));
1479 g_object_class_install_property (object_class,
1480 PROP_REMOTE_CONTACT,
1481 g_param_spec_object ("remote-contact",
1482 "The remote contact",
1483 "The remote contact if there is no group iface on the channel",
1484 EMPATHY_TYPE_CONTACT,
1487 g_object_class_install_property (object_class,
1489 g_param_spec_boolean ("ready",
1490 "Is the object ready",
1491 "This object can't be used until this becomes true",
1495 g_object_class_install_property (object_class,
1496 PROP_PASSWORD_NEEDED,
1497 g_param_spec_boolean ("password-needed",
1499 "TRUE if a password is needed to join the channel",
1503 g_object_class_install_property (object_class,
1505 g_param_spec_boolean ("sms-channel",
1507 "TRUE if channel is for sending SMSes",
1512 signals[MESSAGE_RECEIVED] =
1513 g_signal_new ("message-received",
1514 G_TYPE_FROM_CLASS (klass),
1518 g_cclosure_marshal_VOID__OBJECT,
1520 1, EMPATHY_TYPE_MESSAGE);
1522 signals[SEND_ERROR] =
1523 g_signal_new ("send-error",
1524 G_TYPE_FROM_CLASS (klass),
1528 _empathy_marshal_VOID__STRING_UINT_STRING,
1530 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1532 signals[CHAT_STATE_CHANGED] =
1533 g_signal_new ("chat-state-changed",
1534 G_TYPE_FROM_CLASS (klass),
1538 _empathy_marshal_VOID__OBJECT_UINT,
1540 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1542 signals[PROPERTY_CHANGED] =
1543 g_signal_new ("property-changed",
1544 G_TYPE_FROM_CLASS (klass),
1548 _empathy_marshal_VOID__STRING_BOXED,
1550 2, G_TYPE_STRING, G_TYPE_VALUE);
1553 g_signal_new ("destroy",
1554 G_TYPE_FROM_CLASS (klass),
1558 g_cclosure_marshal_VOID__VOID,
1562 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1566 empathy_tp_chat_init (EmpathyTpChat *chat)
1568 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1569 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1572 priv->messages_queue = g_queue_new ();
1573 priv->pending_messages_queue = g_queue_new ();
1577 tp_chat_iface_init (EmpathyContactListIface *iface)
1579 iface->add = tp_chat_add;
1580 iface->remove = tp_chat_remove;
1581 iface->get_members = tp_chat_get_members;
1585 empathy_tp_chat_new (TpAccount *account,
1588 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1589 g_return_val_if_fail (TP_IS_TEXT_CHANNEL (channel), NULL);
1591 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1598 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1600 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1604 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1606 id = tp_channel_get_identifier (priv->channel);
1607 if (!EMP_STR_EMPTY (id))
1609 else if (priv->remote_contact)
1610 return empathy_contact_get_id (priv->remote_contact);
1617 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1619 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1621 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1622 g_return_val_if_fail (priv->ready, NULL);
1624 return priv->remote_contact;
1628 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1630 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1632 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1634 return priv->channel;
1638 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1640 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1642 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1644 return priv->account;
1648 empathy_tp_chat_get_connection (EmpathyTpChat *chat)
1650 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1652 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1654 return tp_channel_borrow_connection (priv->channel);
1657 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1659 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1661 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1667 empathy_tp_chat_send (EmpathyTpChat *chat,
1670 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1671 gchar *message_body;
1673 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1674 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1675 g_return_if_fail (priv->ready);
1677 message_body = tp_message_to_text (message, NULL);
1679 DEBUG ("Sending message: %s", message_body);
1681 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel),
1682 message, 0, message_send_cb, chat);
1684 g_free (message_body);
1688 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1689 TpChannelChatState state)
1691 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1693 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1694 g_return_if_fail (priv->ready);
1696 if (tp_proxy_has_interface_by_id (priv->channel,
1697 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1698 DEBUG ("Set state: %d", state);
1699 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1702 "setting chat state",
1710 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1712 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1714 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1715 g_return_val_if_fail (priv->ready, NULL);
1717 return priv->pending_messages_queue->head;
1721 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1722 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1724 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1725 priv->channel, -1, ids, tp_chat_async_cb,
1726 "acknowledging received message", NULL, G_OBJECT (chat));
1730 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1731 EmpathyMessage *message) {
1732 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1733 GArray *message_ids;
1737 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1738 g_return_if_fail (priv->ready);
1740 if (!empathy_message_is_incoming (message))
1743 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1745 id = empathy_message_get_id (message);
1746 g_array_append_val (message_ids, id);
1747 acknowledge_messages (chat, message_ids);
1748 g_array_free (message_ids, TRUE);
1751 m = g_queue_find (priv->pending_messages_queue, message);
1752 g_assert (m != NULL);
1753 g_queue_delete_link (priv->pending_messages_queue, m);
1754 g_object_unref (message);
1758 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1759 const GSList *messages) {
1760 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1761 /* Copy messages as the messges list (probably is) our own */
1762 GSList *msgs = g_slist_copy ((GSList *) messages);
1765 GArray *message_ids;
1767 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1768 g_return_if_fail (priv->ready);
1770 length = g_slist_length ((GSList *) messages);
1775 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1777 for (l = msgs; l != NULL; l = g_slist_next (l)) {
1780 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1782 m = g_queue_find (priv->pending_messages_queue, message);
1783 g_assert (m != NULL);
1784 g_queue_delete_link (priv->pending_messages_queue, m);
1786 if (empathy_message_is_incoming (message)) {
1787 guint id = empathy_message_get_id (message);
1788 g_array_append_val (message_ids, id);
1790 g_object_unref (message);
1793 if (message_ids->len > 0)
1794 acknowledge_messages (chat, message_ids);
1796 g_array_free (message_ids, TRUE);
1797 g_slist_free (msgs);
1801 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *chat)
1803 empathy_tp_chat_acknowledge_messages (chat,
1804 (GSList *) empathy_tp_chat_get_pending_messages (chat));
1808 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1810 EmpathyTpChatPriv *priv = GET_PRIV (self);
1812 return priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1816 provide_password_cb (TpChannel *channel,
1818 const GError *error,
1820 GObject *weak_object)
1822 GSimpleAsyncResult *result = user_data;
1824 if (error != NULL) {
1825 g_simple_async_result_set_from_error (result, error);
1827 else if (!correct) {
1828 /* The current D-Bus API is a bit weird so re-use the
1829 * AuthenticationFailed error */
1830 g_simple_async_result_set_error (result, TP_ERRORS,
1831 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1834 g_simple_async_result_complete (result);
1835 g_object_unref (result);
1839 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1840 const gchar *password,
1841 GAsyncReadyCallback callback,
1844 EmpathyTpChatPriv *priv = GET_PRIV (self);
1845 GSimpleAsyncResult *result;
1847 result = g_simple_async_result_new (G_OBJECT (self),
1848 callback, user_data,
1849 empathy_tp_chat_provide_password_finish);
1851 tp_cli_channel_interface_password_call_provide_password
1852 (priv->channel, -1, password, provide_password_cb, result,
1853 NULL, G_OBJECT (self));
1857 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1858 GAsyncResult *result,
1861 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1865 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1866 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1872 * empathy_tp_chat_can_add_contact:
1874 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1875 * That is if this chat is a 1-to-1 channel that can be upgraded to
1876 * a MUC using the Conference interface or if the channel is a MUC.
1879 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1881 EmpathyTpChatPriv *priv;
1883 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1885 priv = GET_PRIV (self);
1887 return priv->can_upgrade_to_muc ||
1888 tp_proxy_has_interface_by_id (priv->channel,
1889 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1893 tp_channel_leave_async_cb (GObject *source_object,
1897 GError *error = NULL;
1899 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1900 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1902 g_error_free (error);
1907 empathy_tp_chat_leave (EmpathyTpChat *self,
1908 const gchar *message)
1910 EmpathyTpChatPriv *priv = GET_PRIV (self);
1912 tp_channel_leave_async (priv->channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1913 message, tp_channel_leave_async_cb, self);
1917 add_members_cb (TpChannel *proxy,
1918 const GError *error,
1920 GObject *weak_object)
1922 EmpathyTpChatPriv *priv = GET_PRIV (weak_object);
1924 if (error != NULL) {
1925 DEBUG ("Failed to join chat (%s): %s",
1926 tp_channel_get_identifier (priv->channel), error->message);
1931 empathy_tp_chat_join (EmpathyTpChat *self)
1933 EmpathyTpChatPriv *priv = GET_PRIV (self);
1934 TpHandle self_handle;
1937 self_handle = tp_channel_group_get_self_handle (priv->channel);
1939 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1940 g_array_append_val (members, self_handle);
1942 tp_cli_channel_interface_group_call_add_members (priv->channel, -1, members,
1943 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1945 g_array_free (members, TRUE);
1949 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1952 EmpathyTpChatPriv *priv = GET_PRIV (self);
1953 TpHandle self_handle;
1955 if (!tp_proxy_has_interface (priv->channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1958 self_handle = tp_channel_group_get_self_handle (priv->channel);
1959 if (self_handle == 0)
1962 return tp_channel_group_get_local_pending_info (priv->channel, self_handle,
1963 inviter, NULL, NULL);
1967 empathy_tp_chat_get_chat_state (EmpathyTpChat *chat,
1968 EmpathyContact *contact)
1970 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1971 return tp_channel_get_chat_state (priv->channel,
1972 empathy_contact_get_handle (contact));
1976 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1978 EmpathyTpChatPriv *priv = GET_PRIV (self);
1980 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1986 empathy_tp_chat_is_sms_channel (EmpathyTpChat *self)
1988 EmpathyTpChatPriv *priv;
1990 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1992 priv = GET_PRIV (self);
1994 return priv->sms_channel;