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 struct _EmpathyTpChatPrivate {
42 gboolean dispose_has_run;
45 EmpathyContact *remote_contact;
47 /* Queue of messages not signalled yet */
48 GQueue *messages_queue;
49 /* Queue of messages signalled but not acked yet */
50 GQueue *pending_messages_queue;
51 gboolean had_properties_list;
52 GPtrArray *properties;
53 TpChannelPasswordFlags password_flags;
54 /* TRUE if we fetched the password flag of the channel or if it's not needed
55 * (channel doesn't implement the Password interface) */
56 gboolean got_password_flags;
57 gboolean can_upgrade_to_muc;
58 gboolean got_sms_channel;
61 GHashTable *messages_being_sent;
63 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
64 GSimpleAsyncResult *ready_result;
67 static void tp_chat_iface_init (EmpathyContactListIface *iface);
75 PROP_N_MESSAGES_SENDING,
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
95 tp_chat_set_delivery_status (EmpathyTpChat *self,
97 EmpathyDeliveryStatus delivery_status)
99 TpDeliveryReportingSupportFlags flags =
100 tp_text_channel_get_delivery_reporting_support (
101 TP_TEXT_CHANNEL (self));
103 /* channel must support receiving failures and successes */
104 if (!tp_str_empty (token) &&
105 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
106 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
108 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
110 switch (delivery_status) {
111 case EMPATHY_DELIVERY_STATUS_NONE:
112 g_hash_table_remove (self->priv->messages_being_sent,
117 g_hash_table_insert (self->priv->messages_being_sent,
119 GUINT_TO_POINTER (delivery_status));
123 g_object_notify (G_OBJECT (self), "n-messages-sending");
127 static void tp_chat_prepare_ready_async (TpProxy *proxy,
128 const TpProxyFeature *feature,
129 GAsyncReadyCallback callback,
133 tp_chat_invalidated_cb (TpProxy *proxy,
139 DEBUG ("Channel invalidated: %s", message);
140 g_signal_emit (self, 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 EmpathyTpChat *self = (EmpathyTpChat *) list;
174 TpChannel *channel = (TpChannel *) self;
176 if (tp_proxy_has_interface_by_id (self,
177 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
179 GArray handles = {(gchar *) &handle, 1};
181 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
182 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
184 handle = empathy_contact_get_handle (contact);
185 tp_cli_channel_interface_group_call_add_members (channel,
186 -1, &handles, NULL, NULL, NULL, NULL, NULL);
187 } else if (self->priv->can_upgrade_to_muc) {
188 TpAccountChannelRequest *req;
190 const char *object_path;
191 GPtrArray channels = { (gpointer *) &object_path, 1 };
192 const char *invitees[2] = { NULL, };
194 invitees[0] = empathy_contact_get_id (contact);
195 object_path = tp_proxy_get_object_path (self);
198 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
199 TP_IFACE_CHANNEL_TYPE_TEXT,
200 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
202 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
203 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
204 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
205 G_TYPE_STRV, invitees,
206 /* FIXME: InvitationMessage ? */
209 req = tp_account_channel_request_new (self->priv->account, props,
210 TP_USER_ACTION_TIME_NOT_USER_ACTION);
212 /* Although this is a MUC, it's anonymous, so CreateChannel is
214 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
215 NULL, create_conference_cb, NULL);
217 g_object_unref (req);
218 g_hash_table_unref (props);
220 g_warning ("Cannot add to this channel");
225 tp_chat_remove (EmpathyContactList *list,
226 EmpathyContact *contact,
227 const gchar *message)
229 EmpathyTpChat *self = (EmpathyTpChat *) list;
231 GArray handles = {(gchar *) &handle, 1};
233 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
234 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
236 handle = empathy_contact_get_handle (contact);
237 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
244 tp_chat_get_members (EmpathyContactList *list)
246 EmpathyTpChat *self = (EmpathyTpChat *) list;
247 GList *members = NULL;
249 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
251 if (self->priv->members) {
252 members = g_list_copy (self->priv->members);
253 g_list_foreach (members, (GFunc) g_object_ref, NULL);
255 members = g_list_prepend (members, g_object_ref (self->priv->user));
256 if (self->priv->remote_contact != NULL)
257 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
264 check_ready (EmpathyTpChat *self)
266 if (self->priv->ready_result == NULL)
269 if (g_queue_get_length (self->priv->messages_queue) > 0)
274 g_simple_async_result_complete (self->priv->ready_result);
275 tp_clear_object (&self->priv->ready_result);
279 tp_chat_emit_queued_messages (EmpathyTpChat *self)
281 EmpathyMessage *message;
283 /* Check if we can now emit some queued messages */
284 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
285 if (empathy_message_get_sender (message) == NULL) {
289 DEBUG ("Queued message ready");
290 g_queue_pop_head (self->priv->messages_queue);
291 g_queue_push_tail (self->priv->pending_messages_queue, message);
292 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
299 tp_chat_got_sender_cb (TpConnection *connection,
300 EmpathyContact *contact,
305 EmpathyTpChat *self = (EmpathyTpChat *) chat;
308 DEBUG ("Error: %s", error->message);
309 /* Do not block the message queue, just drop this message */
310 g_queue_remove (self->priv->messages_queue, message);
312 empathy_message_set_sender (message, contact);
315 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
319 tp_chat_build_message (EmpathyTpChat *self,
323 EmpathyMessage *message;
326 message = empathy_message_new_from_tp_message (msg, incoming);
327 /* FIXME: this is actually a lie for incoming messages. */
328 empathy_message_set_receiver (message, self->priv->user);
330 g_queue_push_tail (self->priv->messages_queue, message);
332 sender = tp_signalled_message_get_sender (msg);
333 g_assert (sender != NULL);
335 if (tp_contact_get_handle (sender) == 0) {
336 empathy_message_set_sender (message, self->priv->user);
337 tp_chat_emit_queued_messages (self);
339 TpConnection *connection = tp_channel_borrow_connection (
342 empathy_tp_contact_factory_get_from_handle (connection,
343 tp_contact_get_handle (sender),
344 tp_chat_got_sender_cb,
345 message, NULL, G_OBJECT (self));
350 handle_delivery_report (EmpathyTpChat *self,
353 TpDeliveryStatus delivery_status;
354 const GHashTable *header;
355 TpChannelTextSendError delivery_error;
358 const gchar *message_body = NULL;
359 const gchar *delivery_dbus_error;
360 const gchar *delivery_token = NULL;
362 header = tp_message_peek (message, 0);
366 delivery_token = tp_asv_get_string (header, "delivery-token");
367 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
371 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
372 DEBUG ("Accepted %s", delivery_token);
373 tp_chat_set_delivery_status (self, delivery_token,
374 EMPATHY_DELIVERY_STATUS_ACCEPTED);
376 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
377 DEBUG ("Delivered %s", delivery_token);
378 tp_chat_set_delivery_status (self, delivery_token,
379 EMPATHY_DELIVERY_STATUS_NONE);
381 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
385 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
387 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
389 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
391 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
392 * TpMessage. (fdo #35884) */
393 echo = tp_asv_get_boxed (header, "delivery-echo",
394 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
395 if (echo != NULL && echo->len >= 1) {
396 const GHashTable *echo_body;
398 echo_body = g_ptr_array_index (echo, 1);
399 if (echo_body != NULL)
400 message_body = tp_asv_get_string (echo_body, "content");
403 tp_chat_set_delivery_status (self, delivery_token,
404 EMPATHY_DELIVERY_STATUS_NONE);
405 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
406 delivery_error, delivery_dbus_error);
409 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
410 message, NULL, NULL);
414 handle_incoming_message (EmpathyTpChat *self,
420 if (tp_message_is_delivery_report (message)) {
421 handle_delivery_report (self, message);
425 message_body = tp_message_to_text (message, NULL);
427 DEBUG ("Message %s (channel %s): %s",
428 pending ? "pending" : "received",
429 tp_proxy_get_object_path (self), message_body);
431 if (message_body == NULL) {
432 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
434 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
435 message, NULL, NULL);
439 tp_chat_build_message (self, message, TRUE);
441 g_free (message_body);
445 message_received_cb (TpTextChannel *channel,
449 handle_incoming_message (self, message, FALSE);
453 find_pending_message_func (gconstpointer a,
456 EmpathyMessage *msg = (EmpathyMessage *) a;
457 TpMessage *message = (TpMessage *) b;
459 if (empathy_message_get_tp_message (msg) == message)
466 pending_message_removed_cb (TpTextChannel *channel,
472 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
473 find_pending_message_func);
478 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
480 g_object_unref (m->data);
481 g_queue_delete_link (self->priv->pending_messages_queue, m);
485 message_sent_cb (TpTextChannel *channel,
487 TpMessageSendingFlags flags,
493 message_body = tp_message_to_text (message, NULL);
495 DEBUG ("Message sent: %s", message_body);
497 tp_chat_build_message (self, message, FALSE);
499 g_free (message_body);
502 static TpChannelTextSendError
503 error_to_text_send_error (GError *error)
505 if (error->domain != TP_ERRORS)
506 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
508 switch (error->code) {
509 case TP_ERROR_OFFLINE:
510 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
511 case TP_ERROR_INVALID_HANDLE:
512 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
513 case TP_ERROR_PERMISSION_DENIED:
514 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
515 case TP_ERROR_NOT_IMPLEMENTED:
516 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
519 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
523 message_send_cb (GObject *source,
524 GAsyncResult *result,
527 EmpathyTpChat *self = user_data;
528 TpTextChannel *channel = (TpTextChannel *) source;
530 GError *error = NULL;
532 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
533 DEBUG ("Error: %s", error->message);
535 /* FIXME: we should use the body of the message as first argument of the
536 * signal but can't easily get it as we just get a user_data pointer. Once
537 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
538 * to use the user_data pointer to pass the message and fix this. */
539 g_signal_emit (self, signals[SEND_ERROR], 0,
540 NULL, error_to_text_send_error (error), NULL);
542 g_error_free (error);
545 tp_chat_set_delivery_status (self, token,
546 EMPATHY_DELIVERY_STATUS_SENDING);
552 TpChannelChatState state;
556 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
557 EmpathyContact *contact,
562 TpChannelChatState state;
565 DEBUG ("Error: %s", error->message);
569 state = GPOINTER_TO_UINT (user_data);
570 DEBUG ("Chat state changed for %s (%d): %d",
571 empathy_contact_get_alias (contact),
572 empathy_contact_get_handle (contact), state);
574 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
578 tp_chat_state_changed_cb (TpChannel *channel,
580 TpChannelChatState state,
583 TpConnection *connection = tp_channel_borrow_connection (
586 empathy_tp_contact_factory_get_from_handle (connection, handle,
587 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
588 NULL, G_OBJECT (self));
592 list_pending_messages (EmpathyTpChat *self)
596 messages = tp_text_channel_get_pending_messages (
597 TP_TEXT_CHANNEL (self));
599 for (l = messages; l != NULL; l = g_list_next (l)) {
600 TpMessage *message = l->data;
602 handle_incoming_message (self, message, FALSE);
605 g_list_free (messages);
609 tp_chat_property_flags_changed_cb (TpProxy *proxy,
610 const GPtrArray *properties,
614 EmpathyTpChat *self = (EmpathyTpChat *) chat;
617 if (!self->priv->had_properties_list || !properties) {
621 for (i = 0; i < properties->len; i++) {
622 GValueArray *prop_struct;
623 EmpathyTpChatProperty *property;
627 prop_struct = g_ptr_array_index (properties, i);
628 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
629 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
631 for (j = 0; j < self->priv->properties->len; j++) {
632 property = g_ptr_array_index (self->priv->properties, j);
633 if (property->id == id) {
634 property->flags = flags;
635 DEBUG ("property %s flags changed: %d",
636 property->name, property->flags);
644 tp_chat_properties_changed_cb (TpProxy *proxy,
645 const GPtrArray *properties,
649 EmpathyTpChat *self = (EmpathyTpChat *) chat;
652 if (!self->priv->had_properties_list || !properties) {
656 for (i = 0; i < properties->len; i++) {
657 GValueArray *prop_struct;
658 EmpathyTpChatProperty *property;
662 prop_struct = g_ptr_array_index (properties, i);
663 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
664 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
666 for (j = 0; j < self->priv->properties->len; j++) {
667 property = g_ptr_array_index (self->priv->properties, j);
668 if (property->id == id) {
669 if (property->value) {
670 g_value_copy (src_value, property->value);
672 property->value = tp_g_value_slice_dup (src_value);
675 DEBUG ("property %s changed", property->name);
676 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
677 property->name, property->value);
685 tp_chat_get_properties_cb (TpProxy *proxy,
686 const GPtrArray *properties,
692 DEBUG ("Error getting properties: %s", error->message);
696 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
700 tp_chat_list_properties_cb (TpProxy *proxy,
701 const GPtrArray *properties,
706 EmpathyTpChat *self = (EmpathyTpChat *) chat;
710 self->priv->had_properties_list = TRUE;
713 DEBUG ("Error listing properties: %s", error->message);
717 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
718 self->priv->properties = g_ptr_array_sized_new (properties->len);
719 for (i = 0; i < properties->len; i++) {
720 GValueArray *prop_struct;
721 EmpathyTpChatProperty *property;
723 prop_struct = g_ptr_array_index (properties, i);
724 property = g_slice_new0 (EmpathyTpChatProperty);
725 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
726 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
727 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
729 DEBUG ("Adding property name=%s id=%d flags=%d",
730 property->name, property->id, property->flags);
731 g_ptr_array_add (self->priv->properties, property);
732 if (property->flags & TP_PROPERTY_FLAG_READ) {
733 g_array_append_val (ids, property->id);
737 tp_cli_properties_interface_call_get_properties (proxy, -1,
739 tp_chat_get_properties_cb,
743 g_array_free (ids, TRUE);
747 empathy_tp_chat_set_property (EmpathyTpChat *self,
751 EmpathyTpChatProperty *property;
754 if (!self->priv->had_properties_list) {
758 for (i = 0; i < self->priv->properties->len; i++) {
759 property = g_ptr_array_index (self->priv->properties, i);
760 if (!tp_strdiff (property->name, name)) {
761 GPtrArray *properties;
764 GValue dest_value = {0, };
766 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
770 g_value_init (&id, G_TYPE_UINT);
771 g_value_init (&dest_value, G_TYPE_VALUE);
772 g_value_set_uint (&id, property->id);
773 g_value_set_boxed (&dest_value, value);
775 prop = g_value_array_new (2);
776 g_value_array_append (prop, &id);
777 g_value_array_append (prop, &dest_value);
779 properties = g_ptr_array_sized_new (1);
780 g_ptr_array_add (properties, prop);
782 DEBUG ("Set property %s", name);
783 tp_cli_properties_interface_call_set_properties (self, -1,
785 (tp_cli_properties_interface_callback_for_set_properties)
787 "Seting property", NULL,
790 g_ptr_array_free (properties, TRUE);
791 g_value_array_free (prop);
798 EmpathyTpChatProperty *
799 empathy_tp_chat_get_property (EmpathyTpChat *self,
802 EmpathyTpChatProperty *property;
805 if (!self->priv->had_properties_list) {
809 for (i = 0; i < self->priv->properties->len; i++) {
810 property = g_ptr_array_index (self->priv->properties, i);
811 if (!tp_strdiff (property->name, name)) {
820 empathy_tp_chat_get_properties (EmpathyTpChat *self)
822 return self->priv->properties;
826 tp_chat_dispose (GObject *object)
828 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
830 if (self->priv->dispose_has_run)
833 self->priv->dispose_has_run = TRUE;
835 tp_clear_object (&self->priv->account);
837 if (self->priv->remote_contact != NULL)
838 g_object_unref (self->priv->remote_contact);
839 self->priv->remote_contact = NULL;
841 if (self->priv->user != NULL)
842 g_object_unref (self->priv->user);
843 self->priv->user = NULL;
845 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
846 g_queue_clear (self->priv->messages_queue);
848 g_queue_foreach (self->priv->pending_messages_queue,
849 (GFunc) g_object_unref, NULL);
850 g_queue_clear (self->priv->pending_messages_queue);
852 tp_clear_object (&self->priv->ready_result);
854 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
855 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
859 tp_chat_finalize (GObject *object)
861 EmpathyTpChat *self = (EmpathyTpChat *) object;
864 DEBUG ("Finalize: %p", object);
866 if (self->priv->properties) {
867 for (i = 0; i < self->priv->properties->len; i++) {
868 EmpathyTpChatProperty *property;
870 property = g_ptr_array_index (self->priv->properties, i);
871 g_free (property->name);
872 if (property->value) {
873 tp_g_value_slice_free (property->value);
875 g_slice_free (EmpathyTpChatProperty, property);
877 g_ptr_array_free (self->priv->properties, TRUE);
880 g_queue_free (self->priv->messages_queue);
881 g_queue_free (self->priv->pending_messages_queue);
882 g_hash_table_destroy (self->priv->messages_being_sent);
884 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
888 check_almost_ready (EmpathyTpChat *self)
890 if (self->priv->ready_result == NULL)
893 if (self->priv->user == NULL)
896 if (!self->priv->got_password_flags)
899 if (!self->priv->got_sms_channel)
902 /* We need either the members (room) or the remote contact (private chat).
903 * If the chat is protected by a password we can't get these information so
904 * consider the chat as ready so it can be presented to the user. */
905 if (!empathy_tp_chat_password_needed (self) && self->priv->members == NULL &&
906 self->priv->remote_contact == NULL)
909 g_assert (tp_proxy_is_prepared (self,
910 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
912 tp_g_signal_connect_object (self, "message-received",
913 G_CALLBACK (message_received_cb), self, 0);
914 tp_g_signal_connect_object (self, "pending-message-removed",
915 G_CALLBACK (pending_message_removed_cb), self, 0);
917 list_pending_messages (self);
919 tp_g_signal_connect_object (self, "message-sent",
920 G_CALLBACK (message_sent_cb), self, 0);
922 tp_g_signal_connect_object (self, "chat-state-changed",
923 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
929 tp_chat_update_remote_contact (EmpathyTpChat *self)
931 TpChannel *channel = (TpChannel *) self;
932 EmpathyContact *contact = NULL;
933 TpHandle self_handle;
934 TpHandleType handle_type;
937 /* If this is a named chatroom, never pretend it is a private chat */
938 tp_channel_get_handle (channel, &handle_type);
939 if (handle_type == TP_HANDLE_TYPE_ROOM) {
943 /* This is an MSN chat, but it's the new style where 1-1 chats don't
944 * have the group interface. If it has the conference interface, then
945 * it is indeed a MUC. */
946 if (tp_proxy_has_interface_by_id (self,
947 TP_IFACE_QUARK_CHANNEL_INTERFACE_CONFERENCE)) {
951 /* This is an MSN-like chat where anyone can join the chat at anytime.
952 * If there is only one non-self contact member, we are in a private
953 * chat and we set the "remote-contact" property to that contact. If
954 * there are more, set the "remote-contact" property to NULL and the
955 * UI will display a contact list. */
956 self_handle = tp_channel_group_get_self_handle (channel);
957 for (l = self->priv->members; l; l = l->next) {
958 /* Skip self contact if member */
959 if (empathy_contact_get_handle (l->data) == self_handle) {
963 /* We have more than one remote contact, break */
964 if (contact != NULL) {
969 /* If we didn't find yet a remote contact, keep this one */
973 if (self->priv->remote_contact == contact) {
977 DEBUG ("Changing remote contact from %p to %p",
978 self->priv->remote_contact, contact);
980 if (self->priv->remote_contact) {
981 g_object_unref (self->priv->remote_contact);
984 self->priv->remote_contact = contact ? g_object_ref (contact) : NULL;
985 g_object_notify (G_OBJECT (self), "remote-contact");
989 tp_chat_got_added_contacts_cb (TpConnection *connection,
991 EmpathyContact * const * contacts,
993 const TpHandle *failed,
998 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1000 const TpIntSet *members;
1002 EmpathyContact *contact;
1005 DEBUG ("Error: %s", error->message);
1009 members = tp_channel_group_get_members ((TpChannel *) self);
1010 for (i = 0; i < n_contacts; i++) {
1011 contact = contacts[i];
1012 handle = empathy_contact_get_handle (contact);
1014 /* Make sure the contact is still member */
1015 if (tp_intset_is_member (members, handle)) {
1016 self->priv->members = g_list_prepend (self->priv->members,
1017 g_object_ref (contact));
1018 g_signal_emit_by_name (chat, "members-changed",
1019 contact, NULL, 0, NULL, TRUE);
1023 tp_chat_update_remote_contact (EMPATHY_TP_CHAT (chat));
1024 check_almost_ready (EMPATHY_TP_CHAT (chat));
1027 static EmpathyContact *
1028 chat_lookup_contact (EmpathyTpChat *self,
1034 for (l = self->priv->members; l; l = l->next) {
1035 EmpathyContact *c = l->data;
1037 if (empathy_contact_get_handle (c) != handle) {
1042 /* Caller takes the reference. */
1043 self->priv->members = g_list_delete_link (self->priv->members, l);
1056 TpHandle old_handle;
1059 } ContactRenameData;
1061 static ContactRenameData *
1062 contact_rename_data_new (TpHandle handle,
1064 const gchar* message)
1066 ContactRenameData *data = g_new (ContactRenameData, 1);
1067 data->old_handle = handle;
1068 data->reason = reason;
1069 data->message = g_strdup (message);
1075 contact_rename_data_free (ContactRenameData* data)
1077 g_free (data->message);
1082 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
1084 EmpathyContact * const * contacts,
1086 const TpHandle *failed,
1087 const GError *error,
1091 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1092 const TpIntSet *members;
1094 EmpathyContact *old = NULL, *new = NULL;
1095 ContactRenameData *rename_data = (ContactRenameData *) user_data;
1098 DEBUG ("Error: %s", error->message);
1102 /* renamed members can only be delivered one at a time */
1103 g_warn_if_fail (n_contacts == 1);
1107 members = tp_channel_group_get_members ((TpChannel *) self);
1108 handle = empathy_contact_get_handle (new);
1110 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
1112 /* Make sure the contact is still member */
1113 if (tp_intset_is_member (members, handle)) {
1114 self->priv->members = g_list_prepend (self->priv->members,
1115 g_object_ref (new));
1118 g_signal_emit_by_name (self, "member-renamed",
1119 old, new, rename_data->reason,
1120 rename_data->message);
1121 g_object_unref (old);
1125 if (self->priv->user == old) {
1126 /* We change our nick */
1127 tp_clear_object (&self->priv->user);
1128 self->priv->user = g_object_ref (new);
1131 tp_chat_update_remote_contact (self);
1132 check_almost_ready (self);
1137 tp_chat_group_members_changed_cb (TpChannel *channel,
1141 GArray *local_pending,
1142 GArray *remote_pending,
1145 EmpathyTpChat *self)
1147 EmpathyContact *contact;
1148 EmpathyContact *actor_contact = NULL;
1150 ContactRenameData *rename_data;
1151 TpHandle old_handle;
1152 TpConnection *connection = tp_channel_borrow_connection (
1153 (TpChannel *) self);
1155 /* Contact renamed */
1156 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1157 /* there can only be a single 'added' and a single 'removed' handle */
1158 if (removed->len != 1 || added->len != 1) {
1159 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1160 added->len, removed->len);
1164 old_handle = g_array_index (removed, guint, 0);
1166 rename_data = contact_rename_data_new (old_handle, reason, message);
1167 empathy_tp_contact_factory_get_from_handles (connection,
1168 added->len, (TpHandle *) added->data,
1169 tp_chat_got_renamed_contacts_cb,
1170 rename_data, (GDestroyNotify) contact_rename_data_free,
1176 actor_contact = chat_lookup_contact (self, actor, FALSE);
1177 if (actor_contact == NULL) {
1178 /* FIXME: handle this a tad more gracefully: perhaps
1179 * the actor was a server op. We could use the
1180 * contact-ids detail of MembersChangedDetailed.
1182 DEBUG ("actor %u not a channel member", actor);
1186 /* Remove contacts that are not members anymore */
1187 for (i = 0; i < removed->len; i++) {
1188 contact = chat_lookup_contact (self,
1189 g_array_index (removed, TpHandle, i), TRUE);
1191 if (contact != NULL) {
1192 g_signal_emit_by_name (self, "members-changed", contact,
1193 actor_contact, reason, message,
1195 g_object_unref (contact);
1199 /* Request added contacts */
1200 if (added->len > 0) {
1201 empathy_tp_contact_factory_get_from_handles (connection,
1202 added->len, (TpHandle *) added->data,
1203 tp_chat_got_added_contacts_cb, NULL, NULL,
1207 tp_chat_update_remote_contact (self);
1209 if (actor_contact != NULL) {
1210 g_object_unref (actor_contact);
1215 tp_chat_got_remote_contact_cb (TpConnection *connection,
1216 EmpathyContact *contact,
1217 const GError *error,
1221 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1224 DEBUG ("Error: %s", error->message);
1225 empathy_tp_chat_leave (self, "");
1229 self->priv->remote_contact = g_object_ref (contact);
1230 g_object_notify (chat, "remote-contact");
1232 check_almost_ready (self);
1236 tp_chat_got_self_contact_cb (TpConnection *connection,
1237 EmpathyContact *contact,
1238 const GError *error,
1242 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1245 DEBUG ("Error: %s", error->message);
1246 empathy_tp_chat_leave (self, "");
1250 self->priv->user = g_object_ref (contact);
1251 empathy_contact_set_is_user (self->priv->user, TRUE);
1252 check_almost_ready (self);
1256 password_flags_changed_cb (TpChannel *channel,
1260 GObject *weak_object)
1262 EmpathyTpChat *self = EMPATHY_TP_CHAT (weak_object);
1263 gboolean was_needed, needed;
1265 was_needed = empathy_tp_chat_password_needed (self);
1267 self->priv->password_flags |= added;
1268 self->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);
1285 self->priv->got_password_flags = TRUE;
1286 self->priv->password_flags = password_flags;
1288 check_almost_ready (EMPATHY_TP_CHAT (self));
1292 sms_channel_changed_cb (TpChannel *channel,
1293 gboolean sms_channel,
1297 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1299 self->priv->sms_channel = sms_channel;
1301 g_object_notify (G_OBJECT (chat), "sms-channel");
1305 get_sms_channel_cb (TpProxy *channel,
1306 const GValue *value,
1307 const GError *in_error,
1311 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1313 if (in_error != NULL) {
1314 DEBUG ("Failed to get SMSChannel: %s", in_error->message);
1318 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
1320 self->priv->sms_channel = g_value_get_boolean (value);
1321 self->priv->got_sms_channel = TRUE;
1323 check_almost_ready (EMPATHY_TP_CHAT (chat));
1327 tp_chat_constructor (GType type,
1329 GObjectConstructParam *props)
1333 object = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1335 tp_g_signal_connect_object (object, "invalidated",
1336 G_CALLBACK (tp_chat_invalidated_cb), object, 0);
1342 tp_chat_get_property (GObject *object,
1347 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1351 g_value_set_object (value, self->priv->account);
1353 case PROP_REMOTE_CONTACT:
1354 g_value_set_object (value, self->priv->remote_contact);
1356 case PROP_PASSWORD_NEEDED:
1357 g_value_set_boolean (value, empathy_tp_chat_password_needed (self));
1359 case PROP_SMS_CHANNEL:
1360 g_value_set_boolean (value, self->priv->sms_channel);
1362 case PROP_N_MESSAGES_SENDING:
1363 g_value_set_uint (value,
1364 g_hash_table_size (self->priv->messages_being_sent));
1367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1373 tp_chat_set_property (GObject *object,
1375 const GValue *value,
1378 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1382 self->priv->account = g_value_dup_object (value);
1385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1395 static const TpProxyFeature *
1396 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1398 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1399 static GQuark need[2] = {0, 0};
1401 if (G_LIKELY (features[0].name != 0))
1404 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1405 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1406 features[FEAT_READY].depends_on = need;
1407 features[FEAT_READY].prepare_async =
1408 tp_chat_prepare_ready_async;
1410 /* assert that the terminator at the end is there */
1411 g_assert (features[N_FEAT].name == 0);
1417 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1419 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1420 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1422 object_class->dispose = tp_chat_dispose;
1423 object_class->finalize = tp_chat_finalize;
1424 object_class->constructor = tp_chat_constructor;
1425 object_class->get_property = tp_chat_get_property;
1426 object_class->set_property = tp_chat_set_property;
1428 proxy_class->list_features = tp_chat_list_features;
1430 g_object_class_install_property (object_class,
1432 g_param_spec_object ("account",
1434 "the account associated with the chat",
1437 G_PARAM_CONSTRUCT_ONLY |
1438 G_PARAM_STATIC_STRINGS));
1440 g_object_class_install_property (object_class,
1441 PROP_REMOTE_CONTACT,
1442 g_param_spec_object ("remote-contact",
1443 "The remote contact",
1444 "The remote contact if there is no group iface on the channel",
1445 EMPATHY_TYPE_CONTACT,
1448 g_object_class_install_property (object_class,
1449 PROP_PASSWORD_NEEDED,
1450 g_param_spec_boolean ("password-needed",
1452 "TRUE if a password is needed to join the channel",
1456 g_object_class_install_property (object_class,
1458 g_param_spec_boolean ("sms-channel",
1460 "TRUE if channel is for sending SMSes",
1464 g_object_class_install_property (object_class,
1465 PROP_N_MESSAGES_SENDING,
1466 g_param_spec_uint ("n-messages-sending",
1467 "Num Messages Sending",
1468 "The number of messages being sent",
1473 signals[MESSAGE_RECEIVED] =
1474 g_signal_new ("message-received-empathy",
1475 G_TYPE_FROM_CLASS (klass),
1479 g_cclosure_marshal_VOID__OBJECT,
1481 1, EMPATHY_TYPE_MESSAGE);
1483 signals[SEND_ERROR] =
1484 g_signal_new ("send-error",
1485 G_TYPE_FROM_CLASS (klass),
1489 _empathy_marshal_VOID__STRING_UINT_STRING,
1491 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1493 signals[CHAT_STATE_CHANGED] =
1494 g_signal_new ("chat-state-changed-empathy",
1495 G_TYPE_FROM_CLASS (klass),
1499 _empathy_marshal_VOID__OBJECT_UINT,
1501 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1503 signals[PROPERTY_CHANGED] =
1504 g_signal_new ("property-changed",
1505 G_TYPE_FROM_CLASS (klass),
1509 _empathy_marshal_VOID__STRING_BOXED,
1511 2, G_TYPE_STRING, G_TYPE_VALUE);
1513 /* TODO: remove, should just use invalidated */
1515 g_signal_new ("destroy",
1516 G_TYPE_FROM_CLASS (klass),
1520 g_cclosure_marshal_VOID__VOID,
1524 signals[MESSAGE_ACKNOWLEDGED] =
1525 g_signal_new ("message-acknowledged",
1526 G_TYPE_FROM_CLASS (klass),
1530 g_cclosure_marshal_VOID__OBJECT,
1532 1, EMPATHY_TYPE_MESSAGE);
1534 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1538 empathy_tp_chat_init (EmpathyTpChat *self)
1540 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1541 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1543 self->priv->messages_queue = g_queue_new ();
1544 self->priv->pending_messages_queue = g_queue_new ();
1545 self->priv->messages_being_sent = g_hash_table_new_full (
1546 g_str_hash, g_str_equal, g_free, NULL);
1550 tp_chat_iface_init (EmpathyContactListIface *iface)
1552 iface->add = tp_chat_add;
1553 iface->remove = tp_chat_remove;
1554 iface->get_members = tp_chat_get_members;
1558 empathy_tp_chat_new (TpAccount *account,
1560 const gchar *object_path,
1561 const GHashTable *immutable_properties)
1563 TpProxy *conn_proxy = (TpProxy *) conn;
1565 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1566 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1567 g_return_val_if_fail (immutable_properties != NULL, NULL);
1569 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1572 "dbus-daemon", conn_proxy->dbus_daemon,
1573 "bus-name", conn_proxy->bus_name,
1574 "object-path", object_path,
1575 "channel-properties", immutable_properties,
1580 empathy_tp_chat_get_id (EmpathyTpChat *self)
1584 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1586 id = tp_channel_get_identifier ((TpChannel *) self);
1587 if (!EMP_STR_EMPTY (id))
1589 else if (self->priv->remote_contact)
1590 return empathy_contact_get_id (self->priv->remote_contact);
1597 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1599 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1601 return self->priv->remote_contact;
1605 empathy_tp_chat_get_account (EmpathyTpChat *self)
1607 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1609 return self->priv->account;
1613 empathy_tp_chat_send (EmpathyTpChat *self,
1616 gchar *message_body;
1618 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1619 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1621 message_body = tp_message_to_text (message, NULL);
1623 DEBUG ("Sending message: %s", message_body);
1625 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1626 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1627 message_send_cb, self);
1629 g_free (message_body);
1633 empathy_tp_chat_set_state (EmpathyTpChat *self,
1634 TpChannelChatState state)
1636 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1638 if (tp_proxy_has_interface_by_id (self,
1639 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE)) {
1640 DEBUG ("Set state: %d", state);
1641 tp_cli_channel_interface_chat_state_call_set_chat_state ((TpChannel *) self, -1,
1644 "setting chat state",
1652 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1654 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1656 return self->priv->pending_messages_queue->head;
1660 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1661 EmpathyMessage *message) {
1664 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1666 if (!empathy_message_is_incoming (message))
1669 tp_msg = empathy_message_get_tp_message (message);
1670 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1671 tp_msg, NULL, NULL);
1675 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *self,
1676 const GSList *messages) {
1678 GList *messages_to_ack = NULL;
1680 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1682 if (messages == NULL)
1685 for (l = messages; l != NULL; l = g_slist_next (l)) {
1686 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1688 if (empathy_message_is_incoming (message)) {
1689 TpMessage *tp_msg = empathy_message_get_tp_message (message);
1690 messages_to_ack = g_list_append (messages_to_ack, tp_msg);
1694 if (messages_to_ack != NULL) {
1695 tp_text_channel_ack_messages_async (TP_TEXT_CHANNEL (self),
1696 messages_to_ack, NULL, NULL);
1699 g_list_free (messages_to_ack);
1703 empathy_tp_chat_acknowledge_all_messages (EmpathyTpChat *self)
1705 empathy_tp_chat_acknowledge_messages (self,
1706 (GSList *) empathy_tp_chat_get_pending_messages (self));
1710 empathy_tp_chat_password_needed (EmpathyTpChat *self)
1712 return self->priv->password_flags & TP_CHANNEL_PASSWORD_FLAG_PROVIDE;
1716 provide_password_cb (TpChannel *channel,
1718 const GError *error,
1720 GObject *weak_object)
1722 GSimpleAsyncResult *result = user_data;
1724 if (error != NULL) {
1725 g_simple_async_result_set_from_error (result, error);
1727 else if (!correct) {
1728 /* The current D-Bus API is a bit weird so re-use the
1729 * AuthenticationFailed error */
1730 g_simple_async_result_set_error (result, TP_ERRORS,
1731 TP_ERROR_AUTHENTICATION_FAILED, "Wrong password");
1734 g_simple_async_result_complete (result);
1735 g_object_unref (result);
1739 empathy_tp_chat_provide_password_async (EmpathyTpChat *self,
1740 const gchar *password,
1741 GAsyncReadyCallback callback,
1744 GSimpleAsyncResult *result;
1746 result = g_simple_async_result_new (G_OBJECT (self),
1747 callback, user_data,
1748 empathy_tp_chat_provide_password_finish);
1750 tp_cli_channel_interface_password_call_provide_password
1751 ((TpChannel *) self, -1, password, provide_password_cb, result,
1752 NULL, G_OBJECT (self));
1756 empathy_tp_chat_provide_password_finish (EmpathyTpChat *self,
1757 GAsyncResult *result,
1760 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1764 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1765 G_OBJECT (self), empathy_tp_chat_provide_password_finish), FALSE);
1771 * empathy_tp_chat_can_add_contact:
1773 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1774 * That is if this chat is a 1-to-1 channel that can be upgraded to
1775 * a MUC using the Conference interface or if the channel is a MUC.
1778 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1780 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1782 return self->priv->can_upgrade_to_muc ||
1783 tp_proxy_has_interface_by_id (self,
1784 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1788 tp_channel_leave_async_cb (GObject *source_object,
1792 GError *error = NULL;
1794 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1795 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1797 g_error_free (error);
1802 empathy_tp_chat_leave (EmpathyTpChat *self,
1803 const gchar *message)
1805 TpChannel *channel = (TpChannel *) self;
1807 DEBUG ("Leaving channel %s with message \"%s\"",
1808 tp_channel_get_identifier (channel), message);
1810 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1811 message, tp_channel_leave_async_cb, self);
1815 add_members_cb (TpChannel *proxy,
1816 const GError *error,
1818 GObject *weak_object)
1820 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1822 if (error != NULL) {
1823 DEBUG ("Failed to join chat (%s): %s",
1824 tp_channel_get_identifier ((TpChannel *) self), error->message);
1829 empathy_tp_chat_join (EmpathyTpChat *self)
1831 TpHandle self_handle;
1834 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1836 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1837 g_array_append_val (members, self_handle);
1839 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1840 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1842 g_array_free (members, TRUE);
1846 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1849 TpHandle self_handle;
1851 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1854 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1855 if (self_handle == 0)
1858 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1859 inviter, NULL, NULL);
1863 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1864 EmpathyContact *contact)
1866 return tp_channel_get_chat_state ((TpChannel *) self,
1867 empathy_contact_get_handle (contact));
1871 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1873 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1875 return self->priv->user;
1880 empathy_tp_chat_is_sms_channel (EmpathyTpChat *self)
1882 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1884 return self->priv->sms_channel;
1888 empathy_tp_chat_get_feature_ready (void)
1890 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1894 conn_prepared_cb (GObject *source,
1895 GAsyncResult *result,
1898 EmpathyTpChat *self = user_data;
1899 TpChannel *channel = (TpChannel *) self;
1900 GError *error = NULL;
1902 TpConnection *connection = (TpConnection *) source;
1904 if (!tp_proxy_prepare_finish (source, result, &error)) {
1905 g_simple_async_result_set_from_error (self->priv->ready_result, error);
1906 g_error_free (error);
1907 g_simple_async_result_complete (self->priv->ready_result);
1908 tp_clear_object (&self->priv->ready_result);
1912 if (tp_proxy_has_interface_by_id (self,
1913 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1914 const TpIntSet *members;
1917 /* Get self contact from the group's self handle */
1918 handle = tp_channel_group_get_self_handle (channel);
1919 empathy_tp_contact_factory_get_from_handle (connection,
1920 handle, tp_chat_got_self_contact_cb,
1921 NULL, NULL, G_OBJECT (self));
1923 /* Get initial member contacts */
1924 members = tp_channel_group_get_members (channel);
1925 handles = tp_intset_to_array (members);
1926 empathy_tp_contact_factory_get_from_handles (connection,
1927 handles->len, (TpHandle *) handles->data,
1928 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1930 self->priv->can_upgrade_to_muc = FALSE;
1932 tp_g_signal_connect_object (self, "group-members-changed",
1933 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1935 TpCapabilities *caps;
1939 /* Get the self contact from the connection's self handle */
1940 handle = tp_connection_get_self_handle (connection);
1941 empathy_tp_contact_factory_get_from_handle (connection,
1942 handle, tp_chat_got_self_contact_cb,
1943 NULL, NULL, G_OBJECT (self));
1945 /* Get the remote contact */
1946 handle = tp_channel_get_handle (channel, NULL);
1947 empathy_tp_contact_factory_get_from_handle (connection,
1948 handle, tp_chat_got_remote_contact_cb,
1949 NULL, NULL, G_OBJECT (self));
1951 caps = tp_connection_get_capabilities (connection);
1952 g_assert (caps != NULL);
1954 classes = tp_capabilities_get_channel_classes (caps);
1956 for (i = 0; i < classes->len; i++) {
1957 GValueArray *array = g_ptr_array_index (classes, i);
1958 const char **oprops = g_value_get_boxed (
1959 g_value_array_get_nth (array, 1));
1961 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1962 self->priv->can_upgrade_to_muc = TRUE;
1968 if (tp_proxy_has_interface_by_id (self,
1969 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
1970 tp_cli_properties_interface_call_list_properties (channel, -1,
1971 tp_chat_list_properties_cb,
1974 tp_cli_properties_interface_connect_to_properties_changed (channel,
1975 tp_chat_properties_changed_cb,
1977 G_OBJECT (self), NULL);
1978 tp_cli_properties_interface_connect_to_property_flags_changed (channel,
1979 tp_chat_property_flags_changed_cb,
1981 G_OBJECT (self), NULL);
1984 /* Check if the chat is password protected */
1985 if (tp_proxy_has_interface_by_id (self,
1986 TP_IFACE_QUARK_CHANNEL_INTERFACE_PASSWORD)) {
1987 self->priv->got_password_flags = FALSE;
1989 tp_cli_channel_interface_password_connect_to_password_flags_changed
1990 (channel, password_flags_changed_cb, self, NULL,
1991 G_OBJECT (self), NULL);
1993 tp_cli_channel_interface_password_call_get_password_flags
1994 (channel, -1, got_password_flags_cb, self, NULL, G_OBJECT (self));
1996 /* No Password interface, so no need to fetch the password flags */
1997 self->priv->got_password_flags = TRUE;
2000 /* Check if the chat is for SMS */
2001 if (tp_proxy_has_interface_by_id (channel,
2002 TP_IFACE_QUARK_CHANNEL_INTERFACE_SMS)) {
2003 tp_cli_channel_interface_sms_connect_to_sms_channel_changed (
2005 sms_channel_changed_cb, self, NULL, G_OBJECT (self), NULL);
2007 tp_cli_dbus_properties_call_get (channel, -1,
2008 TP_IFACE_CHANNEL_INTERFACE_SMS, "SMSChannel",
2009 get_sms_channel_cb, self, NULL, G_OBJECT (self));
2011 /* if there's no SMS support, then we're not waiting for it */
2012 self->priv->got_sms_channel = TRUE;
2017 tp_chat_prepare_ready_async (TpProxy *proxy,
2018 const TpProxyFeature *feature,
2019 GAsyncReadyCallback callback,
2022 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
2023 TpChannel *channel = (TpChannel *) proxy;
2024 TpConnection *connection;
2025 GQuark conn_features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
2027 g_assert (self->priv->ready_result == NULL);
2028 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
2029 callback, user_data, tp_chat_prepare_ready_async);
2031 connection = tp_channel_borrow_connection (channel);
2033 tp_proxy_prepare_async (connection, conn_features,
2034 conn_prepared_cb, self);