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-request-util.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 struct _EmpathyTpChatPrivate {
43 EmpathyContact *remote_contact;
45 /* Queue of messages not signalled yet */
46 GQueue *messages_queue;
47 /* Queue of messages signalled but not acked yet */
48 GQueue *pending_messages_queue;
51 gboolean supports_subject;
52 gboolean can_set_subject;
55 /* Room config (for now, we only track the title and don't support
59 gboolean can_upgrade_to_muc;
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);
73 PROP_N_MESSAGES_SENDING,
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, TP_TYPE_TEXT_CHANNEL,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
93 tp_chat_set_delivery_status (EmpathyTpChat *self,
95 EmpathyDeliveryStatus delivery_status)
97 TpDeliveryReportingSupportFlags flags =
98 tp_text_channel_get_delivery_reporting_support (
99 TP_TEXT_CHANNEL (self));
101 /* channel must support receiving failures and successes */
102 if (!tp_str_empty (token) &&
103 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_FAILURES &&
104 flags & TP_DELIVERY_REPORTING_SUPPORT_FLAG_RECEIVE_SUCCESSES) {
106 DEBUG ("Delivery status (%s) = %u", token, delivery_status);
108 switch (delivery_status) {
109 case EMPATHY_DELIVERY_STATUS_NONE:
110 g_hash_table_remove (self->priv->messages_being_sent,
115 g_hash_table_insert (self->priv->messages_being_sent,
117 GUINT_TO_POINTER (delivery_status));
121 g_object_notify (G_OBJECT (self), "n-messages-sending");
125 static void tp_chat_prepare_ready_async (TpProxy *proxy,
126 const TpProxyFeature *feature,
127 GAsyncReadyCallback callback,
131 tp_chat_async_cb (TpChannel *proxy,
134 GObject *weak_object)
137 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
142 create_conference_cb (GObject *source,
143 GAsyncResult *result,
146 GError *error = NULL;
148 if (!tp_account_channel_request_create_channel_finish (
149 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
150 DEBUG ("Failed to create conference channel: %s", error->message);
151 g_error_free (error);
156 tp_chat_add (EmpathyContactList *list,
157 EmpathyContact *contact,
158 const gchar *message)
160 EmpathyTpChat *self = (EmpathyTpChat *) list;
161 TpChannel *channel = (TpChannel *) self;
163 if (tp_proxy_has_interface_by_id (self,
164 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
166 GArray handles = {(gchar *) &handle, 1};
168 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
169 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
171 handle = empathy_contact_get_handle (contact);
172 tp_cli_channel_interface_group_call_add_members (channel,
173 -1, &handles, NULL, NULL, NULL, NULL, NULL);
174 } else if (self->priv->can_upgrade_to_muc) {
175 TpAccountChannelRequest *req;
177 const char *object_path;
178 GPtrArray channels = { (gpointer *) &object_path, 1 };
179 const char *invitees[2] = { NULL, };
181 invitees[0] = empathy_contact_get_id (contact);
182 object_path = tp_proxy_get_object_path (self);
185 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
186 TP_IFACE_CHANNEL_TYPE_TEXT,
187 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
189 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
190 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
191 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
192 G_TYPE_STRV, invitees,
193 /* FIXME: InvitationMessage ? */
196 req = tp_account_channel_request_new (self->priv->account, props,
197 TP_USER_ACTION_TIME_NOT_USER_ACTION);
199 /* Although this is a MUC, it's anonymous, so CreateChannel is
201 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
202 NULL, create_conference_cb, NULL);
204 g_object_unref (req);
205 g_hash_table_unref (props);
207 g_warning ("Cannot add to this channel");
212 tp_chat_remove (EmpathyContactList *list,
213 EmpathyContact *contact,
214 const gchar *message)
216 EmpathyTpChat *self = (EmpathyTpChat *) list;
218 GArray handles = {(gchar *) &handle, 1};
220 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
221 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
223 handle = empathy_contact_get_handle (contact);
224 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
231 tp_chat_get_members (EmpathyContactList *list)
233 EmpathyTpChat *self = (EmpathyTpChat *) list;
234 GList *members = NULL;
236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
238 if (self->priv->members) {
239 members = g_list_copy (self->priv->members);
240 g_list_foreach (members, (GFunc) g_object_ref, NULL);
242 members = g_list_prepend (members, g_object_ref (self->priv->user));
243 if (self->priv->remote_contact != NULL)
244 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
251 check_ready (EmpathyTpChat *self)
253 if (self->priv->ready_result == NULL)
256 if (g_queue_get_length (self->priv->messages_queue) > 0)
261 g_simple_async_result_complete (self->priv->ready_result);
262 tp_clear_object (&self->priv->ready_result);
266 tp_chat_emit_queued_messages (EmpathyTpChat *self)
268 EmpathyMessage *message;
270 /* Check if we can now emit some queued messages */
271 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
272 if (empathy_message_get_sender (message) == NULL) {
276 DEBUG ("Queued message ready");
277 g_queue_pop_head (self->priv->messages_queue);
278 g_queue_push_tail (self->priv->pending_messages_queue, message);
279 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
286 tp_chat_got_sender_cb (TpConnection *connection,
287 EmpathyContact *contact,
292 EmpathyTpChat *self = (EmpathyTpChat *) chat;
295 DEBUG ("Error: %s", error->message);
296 /* Do not block the message queue, just drop this message */
297 g_queue_remove (self->priv->messages_queue, message);
299 empathy_message_set_sender (message, contact);
302 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
306 tp_chat_build_message (EmpathyTpChat *self,
310 EmpathyMessage *message;
313 message = empathy_message_new_from_tp_message (msg, incoming);
314 /* FIXME: this is actually a lie for incoming messages. */
315 empathy_message_set_receiver (message, self->priv->user);
317 g_queue_push_tail (self->priv->messages_queue, message);
319 sender = tp_signalled_message_get_sender (msg);
320 g_assert (sender != NULL);
322 if (tp_contact_get_handle (sender) == 0) {
323 empathy_message_set_sender (message, self->priv->user);
324 tp_chat_emit_queued_messages (self);
326 TpConnection *connection = tp_channel_borrow_connection (
329 empathy_tp_contact_factory_get_from_handle (connection,
330 tp_contact_get_handle (sender),
331 tp_chat_got_sender_cb,
332 message, NULL, G_OBJECT (self));
337 handle_delivery_report (EmpathyTpChat *self,
340 TpDeliveryStatus delivery_status;
341 const GHashTable *header;
342 TpChannelTextSendError delivery_error;
345 const gchar *message_body = NULL;
346 const gchar *delivery_dbus_error;
347 const gchar *delivery_token = NULL;
349 header = tp_message_peek (message, 0);
353 delivery_token = tp_asv_get_string (header, "delivery-token");
354 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
358 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
359 DEBUG ("Accepted %s", delivery_token);
360 tp_chat_set_delivery_status (self, delivery_token,
361 EMPATHY_DELIVERY_STATUS_ACCEPTED);
363 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
364 DEBUG ("Delivered %s", delivery_token);
365 tp_chat_set_delivery_status (self, delivery_token,
366 EMPATHY_DELIVERY_STATUS_NONE);
368 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED) {
372 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
374 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
376 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
378 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
379 * TpMessage. (fdo #35884) */
380 echo = tp_asv_get_boxed (header, "delivery-echo",
381 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
382 if (echo != NULL && echo->len >= 2) {
383 const GHashTable *echo_body;
385 echo_body = g_ptr_array_index (echo, 1);
386 if (echo_body != NULL)
387 message_body = tp_asv_get_string (echo_body, "content");
390 tp_chat_set_delivery_status (self, delivery_token,
391 EMPATHY_DELIVERY_STATUS_NONE);
392 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
393 delivery_error, delivery_dbus_error);
396 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
397 message, NULL, NULL);
401 handle_incoming_message (EmpathyTpChat *self,
407 if (tp_message_is_delivery_report (message)) {
408 handle_delivery_report (self, message);
412 message_body = tp_message_to_text (message, NULL);
414 DEBUG ("Message %s (channel %s): %s",
415 pending ? "pending" : "received",
416 tp_proxy_get_object_path (self), message_body);
418 if (message_body == NULL) {
419 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
421 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
422 message, NULL, NULL);
426 tp_chat_build_message (self, message, TRUE);
428 g_free (message_body);
432 message_received_cb (TpTextChannel *channel,
436 handle_incoming_message (self, message, FALSE);
440 find_pending_message_func (gconstpointer a,
443 EmpathyMessage *msg = (EmpathyMessage *) a;
444 TpMessage *message = (TpMessage *) b;
446 if (empathy_message_get_tp_message (msg) == message)
453 pending_message_removed_cb (TpTextChannel *channel,
459 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
460 find_pending_message_func);
465 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
467 g_object_unref (m->data);
468 g_queue_delete_link (self->priv->pending_messages_queue, m);
472 message_sent_cb (TpTextChannel *channel,
474 TpMessageSendingFlags flags,
480 message_body = tp_message_to_text (message, NULL);
482 DEBUG ("Message sent: %s", message_body);
484 tp_chat_build_message (self, message, FALSE);
486 g_free (message_body);
489 static TpChannelTextSendError
490 error_to_text_send_error (GError *error)
492 if (error->domain != TP_ERRORS)
493 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
495 switch (error->code) {
496 case TP_ERROR_OFFLINE:
497 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
498 case TP_ERROR_INVALID_HANDLE:
499 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
500 case TP_ERROR_PERMISSION_DENIED:
501 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
502 case TP_ERROR_NOT_IMPLEMENTED:
503 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
506 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
510 message_send_cb (GObject *source,
511 GAsyncResult *result,
514 EmpathyTpChat *self = user_data;
515 TpTextChannel *channel = (TpTextChannel *) source;
517 GError *error = NULL;
519 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
520 DEBUG ("Error: %s", error->message);
522 /* FIXME: we should use the body of the message as first argument of the
523 * signal but can't easily get it as we just get a user_data pointer. Once
524 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
525 * to use the user_data pointer to pass the message and fix this. */
526 g_signal_emit (self, signals[SEND_ERROR], 0,
527 NULL, error_to_text_send_error (error), NULL);
529 g_error_free (error);
532 tp_chat_set_delivery_status (self, token,
533 EMPATHY_DELIVERY_STATUS_SENDING);
539 TpChannelChatState state;
543 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
544 EmpathyContact *contact,
549 TpChannelChatState state;
552 DEBUG ("Error: %s", error->message);
556 state = GPOINTER_TO_UINT (user_data);
557 DEBUG ("Chat state changed for %s (%d): %d",
558 empathy_contact_get_alias (contact),
559 empathy_contact_get_handle (contact), state);
561 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
565 tp_chat_state_changed_cb (TpChannel *channel,
567 TpChannelChatState state,
570 TpConnection *connection = tp_channel_borrow_connection (
573 empathy_tp_contact_factory_get_from_handle (connection, handle,
574 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
575 NULL, G_OBJECT (self));
579 list_pending_messages (EmpathyTpChat *self)
583 messages = tp_text_channel_get_pending_messages (
584 TP_TEXT_CHANNEL (self));
586 for (l = messages; l != NULL; l = g_list_next (l)) {
587 TpMessage *message = l->data;
589 handle_incoming_message (self, message, FALSE);
592 g_list_free (messages);
596 update_subject (EmpathyTpChat *self,
597 GHashTable *properties)
599 EmpathyTpChatPrivate *priv = self->priv;
600 gboolean can_set, valid;
601 const gchar *subject;
603 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
605 priv->can_set_subject = can_set;
608 subject = tp_asv_get_string (properties, "Subject");
609 if (subject != NULL) {
610 g_free (priv->subject);
611 priv->subject = g_strdup (subject);
612 g_object_notify (G_OBJECT (self), "subject");
615 /* TODO: track Actor and Timestamp. */
619 tp_chat_get_all_subject_cb (TpProxy *proxy,
620 GHashTable *properties,
622 gpointer user_data G_GNUC_UNUSED,
625 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
626 EmpathyTpChatPrivate *priv = self->priv;
629 DEBUG ("Error fetching subject: %s", error->message);
633 priv->supports_subject = TRUE;
634 update_subject (self, properties);
638 update_title (EmpathyTpChat *self,
639 GHashTable *properties)
641 EmpathyTpChatPrivate *priv = self->priv;
642 const gchar *title = tp_asv_get_string (properties, "Title");
645 if (tp_str_empty (title)) {
649 g_free (priv->title);
650 priv->title = g_strdup (title);
651 g_object_notify (G_OBJECT (self), "title");
656 tp_chat_get_all_room_config_cb (TpProxy *proxy,
657 GHashTable *properties,
659 gpointer user_data G_GNUC_UNUSED,
662 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
665 DEBUG ("Error fetching room config: %s", error->message);
669 update_title (self, properties);
673 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
674 const gchar *interface_name,
676 const gchar **invalidated,
680 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
682 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT)) {
683 update_subject (self, changed);
686 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG)) {
687 update_title (self, changed);
692 empathy_tp_chat_set_subject (EmpathyTpChat *self,
693 const gchar *subject)
695 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
698 "while setting subject", NULL,
703 empathy_tp_chat_get_title (EmpathyTpChat *self)
705 EmpathyTpChatPrivate *priv = self->priv;
711 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
713 EmpathyTpChatPrivate *priv = self->priv;
715 return priv->supports_subject;
719 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
721 EmpathyTpChatPrivate *priv = self->priv;
723 return priv->can_set_subject;
727 empathy_tp_chat_get_subject (EmpathyTpChat *self)
729 EmpathyTpChatPrivate *priv = self->priv;
731 return priv->subject;
735 tp_chat_dispose (GObject *object)
737 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
739 tp_clear_object (&self->priv->account);
740 tp_clear_object (&self->priv->remote_contact);
741 tp_clear_object (&self->priv->user);
743 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
744 g_queue_clear (self->priv->messages_queue);
746 g_queue_foreach (self->priv->pending_messages_queue,
747 (GFunc) g_object_unref, NULL);
748 g_queue_clear (self->priv->pending_messages_queue);
750 tp_clear_object (&self->priv->ready_result);
752 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
753 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
757 tp_chat_finalize (GObject *object)
759 EmpathyTpChat *self = (EmpathyTpChat *) object;
761 DEBUG ("Finalize: %p", object);
763 g_queue_free (self->priv->messages_queue);
764 g_queue_free (self->priv->pending_messages_queue);
765 g_hash_table_unref (self->priv->messages_being_sent);
767 g_free (self->priv->title);
768 g_free (self->priv->subject);
770 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
774 check_almost_ready (EmpathyTpChat *self)
776 TpChannel *channel = (TpChannel *) self;
778 if (self->priv->ready_result == NULL)
781 if (self->priv->user == NULL)
784 /* We need either the members (room) or the remote contact (private chat).
785 * If the chat is protected by a password we can't get these information so
786 * consider the chat as ready so it can be presented to the user. */
787 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
788 self->priv->remote_contact == NULL)
791 g_assert (tp_proxy_is_prepared (self,
792 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
794 tp_g_signal_connect_object (self, "message-received",
795 G_CALLBACK (message_received_cb), self, 0);
796 tp_g_signal_connect_object (self, "pending-message-removed",
797 G_CALLBACK (pending_message_removed_cb), self, 0);
799 list_pending_messages (self);
801 tp_g_signal_connect_object (self, "message-sent",
802 G_CALLBACK (message_sent_cb), self, 0);
804 tp_g_signal_connect_object (self, "chat-state-changed",
805 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
811 tp_chat_got_added_contacts_cb (TpConnection *connection,
813 EmpathyContact * const * contacts,
815 const TpHandle *failed,
820 EmpathyTpChat *self = (EmpathyTpChat *) chat;
822 const TpIntSet *members;
824 EmpathyContact *contact;
827 DEBUG ("Error: %s", error->message);
831 members = tp_channel_group_get_members ((TpChannel *) self);
832 for (i = 0; i < n_contacts; i++) {
833 contact = contacts[i];
834 handle = empathy_contact_get_handle (contact);
836 /* Make sure the contact is still member */
837 if (tp_intset_is_member (members, handle)) {
838 self->priv->members = g_list_prepend (self->priv->members,
839 g_object_ref (contact));
840 g_signal_emit_by_name (chat, "members-changed",
841 contact, NULL, 0, NULL, TRUE);
845 check_almost_ready (EMPATHY_TP_CHAT (chat));
848 static EmpathyContact *
849 chat_lookup_contact (EmpathyTpChat *self,
855 for (l = self->priv->members; l; l = l->next) {
856 EmpathyContact *c = l->data;
858 if (empathy_contact_get_handle (c) != handle) {
863 /* Caller takes the reference. */
864 self->priv->members = g_list_delete_link (self->priv->members, l);
882 static ContactRenameData *
883 contact_rename_data_new (TpHandle handle,
885 const gchar* message)
887 ContactRenameData *data = g_new (ContactRenameData, 1);
888 data->old_handle = handle;
889 data->reason = reason;
890 data->message = g_strdup (message);
896 contact_rename_data_free (ContactRenameData* data)
898 g_free (data->message);
903 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
905 EmpathyContact * const * contacts,
907 const TpHandle *failed,
912 EmpathyTpChat *self = (EmpathyTpChat *) chat;
913 const TpIntSet *members;
915 EmpathyContact *old = NULL, *new = NULL;
916 ContactRenameData *rename_data = (ContactRenameData *) user_data;
919 DEBUG ("Error: %s", error->message);
923 /* renamed members can only be delivered one at a time */
924 g_warn_if_fail (n_contacts == 1);
928 members = tp_channel_group_get_members ((TpChannel *) self);
929 handle = empathy_contact_get_handle (new);
931 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
933 /* Make sure the contact is still member */
934 if (tp_intset_is_member (members, handle)) {
935 self->priv->members = g_list_prepend (self->priv->members,
939 g_signal_emit_by_name (self, "member-renamed",
940 old, new, rename_data->reason,
941 rename_data->message);
942 g_object_unref (old);
946 if (self->priv->user == old) {
947 /* We change our nick */
948 tp_clear_object (&self->priv->user);
949 self->priv->user = g_object_ref (new);
952 check_almost_ready (self);
957 tp_chat_group_members_changed_cb (TpChannel *channel,
961 GArray *local_pending,
962 GArray *remote_pending,
967 EmpathyContact *contact;
968 EmpathyContact *actor_contact = NULL;
970 ContactRenameData *rename_data;
972 TpConnection *connection = tp_channel_borrow_connection (
975 /* Contact renamed */
976 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
977 /* there can only be a single 'added' and a single 'removed' handle */
978 if (removed->len != 1 || added->len != 1) {
979 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
980 added->len, removed->len);
984 old_handle = g_array_index (removed, guint, 0);
986 rename_data = contact_rename_data_new (old_handle, reason, message);
987 empathy_tp_contact_factory_get_from_handles (connection,
988 added->len, (TpHandle *) added->data,
989 tp_chat_got_renamed_contacts_cb,
990 rename_data, (GDestroyNotify) contact_rename_data_free,
996 actor_contact = chat_lookup_contact (self, actor, FALSE);
997 if (actor_contact == NULL) {
998 /* FIXME: handle this a tad more gracefully: perhaps
999 * the actor was a server op. We could use the
1000 * contact-ids detail of MembersChangedDetailed.
1002 DEBUG ("actor %u not a channel member", actor);
1006 /* Remove contacts that are not members anymore */
1007 for (i = 0; i < removed->len; i++) {
1008 contact = chat_lookup_contact (self,
1009 g_array_index (removed, TpHandle, i), TRUE);
1011 if (contact != NULL) {
1012 g_signal_emit_by_name (self, "members-changed", contact,
1013 actor_contact, reason, message,
1015 g_object_unref (contact);
1019 /* Request added contacts */
1020 if (added->len > 0) {
1021 empathy_tp_contact_factory_get_from_handles (connection,
1022 added->len, (TpHandle *) added->data,
1023 tp_chat_got_added_contacts_cb, NULL, NULL,
1027 if (actor_contact != NULL) {
1028 g_object_unref (actor_contact);
1033 tp_chat_got_remote_contact_cb (TpConnection *connection,
1034 EmpathyContact *contact,
1035 const GError *error,
1039 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1042 DEBUG ("Error: %s", error->message);
1043 empathy_tp_chat_leave (self, "");
1047 self->priv->remote_contact = g_object_ref (contact);
1048 g_object_notify (chat, "remote-contact");
1050 check_almost_ready (self);
1054 tp_chat_got_self_contact_cb (TpConnection *connection,
1055 EmpathyContact *contact,
1056 const GError *error,
1060 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1063 DEBUG ("Error: %s", error->message);
1064 empathy_tp_chat_leave (self, "");
1068 self->priv->user = g_object_ref (contact);
1069 empathy_contact_set_is_user (self->priv->user, TRUE);
1070 check_almost_ready (self);
1074 tp_chat_get_property (GObject *object,
1079 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1083 g_value_set_object (value, self->priv->account);
1085 case PROP_REMOTE_CONTACT:
1086 g_value_set_object (value, self->priv->remote_contact);
1088 case PROP_N_MESSAGES_SENDING:
1089 g_value_set_uint (value,
1090 g_hash_table_size (self->priv->messages_being_sent));
1093 g_value_set_string (value,
1094 empathy_tp_chat_get_title (self));
1097 g_value_set_string (value,
1098 empathy_tp_chat_get_subject (self));
1101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1107 tp_chat_set_property (GObject *object,
1109 const GValue *value,
1112 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1116 self->priv->account = g_value_dup_object (value);
1119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1129 static const TpProxyFeature *
1130 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1132 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1133 static GQuark need[2] = {0, 0};
1135 if (G_LIKELY (features[0].name != 0))
1138 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1139 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1140 features[FEAT_READY].depends_on = need;
1141 features[FEAT_READY].prepare_async =
1142 tp_chat_prepare_ready_async;
1144 /* assert that the terminator at the end is there */
1145 g_assert (features[N_FEAT].name == 0);
1151 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1154 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1156 object_class->dispose = tp_chat_dispose;
1157 object_class->finalize = tp_chat_finalize;
1158 object_class->get_property = tp_chat_get_property;
1159 object_class->set_property = tp_chat_set_property;
1161 proxy_class->list_features = tp_chat_list_features;
1163 g_object_class_install_property (object_class,
1165 g_param_spec_object ("account",
1167 "the account associated with the chat",
1170 G_PARAM_CONSTRUCT_ONLY |
1171 G_PARAM_STATIC_STRINGS));
1173 g_object_class_install_property (object_class,
1174 PROP_REMOTE_CONTACT,
1175 g_param_spec_object ("remote-contact",
1176 "The remote contact",
1177 "The remote contact if there is no group iface on the channel",
1178 EMPATHY_TYPE_CONTACT,
1181 g_object_class_install_property (object_class,
1182 PROP_N_MESSAGES_SENDING,
1183 g_param_spec_uint ("n-messages-sending",
1184 "Num Messages Sending",
1185 "The number of messages being sent",
1189 g_object_class_install_property (object_class,
1191 g_param_spec_string ("title",
1193 "A human-readable name for the room, if any",
1196 G_PARAM_STATIC_STRINGS));
1198 g_object_class_install_property (object_class,
1200 g_param_spec_string ("subject",
1202 "The room's current subject, if any",
1205 G_PARAM_STATIC_STRINGS));
1208 signals[MESSAGE_RECEIVED] =
1209 g_signal_new ("message-received-empathy",
1210 G_TYPE_FROM_CLASS (klass),
1214 g_cclosure_marshal_generic,
1216 1, EMPATHY_TYPE_MESSAGE);
1218 signals[SEND_ERROR] =
1219 g_signal_new ("send-error",
1220 G_TYPE_FROM_CLASS (klass),
1224 g_cclosure_marshal_generic,
1226 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1228 signals[CHAT_STATE_CHANGED] =
1229 g_signal_new ("chat-state-changed-empathy",
1230 G_TYPE_FROM_CLASS (klass),
1234 g_cclosure_marshal_generic,
1236 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1238 signals[MESSAGE_ACKNOWLEDGED] =
1239 g_signal_new ("message-acknowledged",
1240 G_TYPE_FROM_CLASS (klass),
1244 g_cclosure_marshal_generic,
1246 1, EMPATHY_TYPE_MESSAGE);
1248 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1252 empathy_tp_chat_init (EmpathyTpChat *self)
1254 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1255 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1257 self->priv->messages_queue = g_queue_new ();
1258 self->priv->pending_messages_queue = g_queue_new ();
1259 self->priv->messages_being_sent = g_hash_table_new_full (
1260 g_str_hash, g_str_equal, g_free, NULL);
1264 tp_chat_iface_init (EmpathyContactListIface *iface)
1266 iface->add = tp_chat_add;
1267 iface->remove = tp_chat_remove;
1268 iface->get_members = tp_chat_get_members;
1272 empathy_tp_chat_new (
1273 TpSimpleClientFactory *factory,
1276 const gchar *object_path,
1277 const GHashTable *immutable_properties)
1279 TpProxy *conn_proxy = (TpProxy *) conn;
1281 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1282 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1283 g_return_val_if_fail (immutable_properties != NULL, NULL);
1285 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1289 "dbus-daemon", conn_proxy->dbus_daemon,
1290 "bus-name", conn_proxy->bus_name,
1291 "object-path", object_path,
1292 "channel-properties", immutable_properties,
1297 empathy_tp_chat_get_id (EmpathyTpChat *self)
1301 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1303 id = tp_channel_get_identifier ((TpChannel *) self);
1304 if (!EMP_STR_EMPTY (id))
1306 else if (self->priv->remote_contact)
1307 return empathy_contact_get_id (self->priv->remote_contact);
1314 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1316 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1318 return self->priv->remote_contact;
1322 empathy_tp_chat_get_account (EmpathyTpChat *self)
1324 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1326 return self->priv->account;
1330 empathy_tp_chat_send (EmpathyTpChat *self,
1333 gchar *message_body;
1335 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1336 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1338 message_body = tp_message_to_text (message, NULL);
1340 DEBUG ("Sending message: %s", message_body);
1342 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1343 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1344 message_send_cb, self);
1346 g_free (message_body);
1350 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1352 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1354 return self->priv->pending_messages_queue->head;
1358 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1359 EmpathyMessage *message) {
1362 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1364 if (!empathy_message_is_incoming (message))
1367 tp_msg = empathy_message_get_tp_message (message);
1368 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1369 tp_msg, NULL, NULL);
1373 * empathy_tp_chat_can_add_contact:
1375 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1376 * That is if this chat is a 1-to-1 channel that can be upgraded to
1377 * a MUC using the Conference interface or if the channel is a MUC.
1380 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1382 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1384 return self->priv->can_upgrade_to_muc ||
1385 tp_proxy_has_interface_by_id (self,
1386 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1390 tp_channel_leave_async_cb (GObject *source_object,
1394 GError *error = NULL;
1396 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1397 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1399 g_error_free (error);
1404 empathy_tp_chat_leave (EmpathyTpChat *self,
1405 const gchar *message)
1407 TpChannel *channel = (TpChannel *) self;
1409 DEBUG ("Leaving channel %s with message \"%s\"",
1410 tp_channel_get_identifier (channel), message);
1412 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1413 message, tp_channel_leave_async_cb, self);
1417 add_members_cb (TpChannel *proxy,
1418 const GError *error,
1420 GObject *weak_object)
1422 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1424 if (error != NULL) {
1425 DEBUG ("Failed to join chat (%s): %s",
1426 tp_channel_get_identifier ((TpChannel *) self), error->message);
1431 empathy_tp_chat_join (EmpathyTpChat *self)
1433 TpHandle self_handle;
1436 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1438 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1439 g_array_append_val (members, self_handle);
1441 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1442 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1444 g_array_unref (members);
1448 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1451 TpHandle self_handle;
1453 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1456 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1457 if (self_handle == 0)
1460 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1461 inviter, NULL, NULL);
1465 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1466 EmpathyContact *contact)
1468 return tp_channel_get_chat_state ((TpChannel *) self,
1469 empathy_contact_get_handle (contact));
1473 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1475 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1477 return self->priv->user;
1481 empathy_tp_chat_get_feature_ready (void)
1483 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1487 tp_chat_prepare_ready_async (TpProxy *proxy,
1488 const TpProxyFeature *feature,
1489 GAsyncReadyCallback callback,
1492 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1493 TpChannel *channel = (TpChannel *) proxy;
1494 TpConnection *connection;
1495 gboolean listen_for_dbus_properties_changed = FALSE;
1497 g_assert (self->priv->ready_result == NULL);
1498 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1499 callback, user_data, tp_chat_prepare_ready_async);
1501 connection = tp_channel_borrow_connection (channel);
1503 if (tp_proxy_has_interface_by_id (self,
1504 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1505 const TpIntSet *members;
1509 /* Get self contact from the group's self handle */
1510 handle = tp_channel_group_get_self_handle (channel);
1511 empathy_tp_contact_factory_get_from_handle (connection,
1512 handle, tp_chat_got_self_contact_cb,
1513 NULL, NULL, G_OBJECT (self));
1515 /* Get initial member contacts */
1516 members = tp_channel_group_get_members (channel);
1517 handles = tp_intset_to_array (members);
1518 empathy_tp_contact_factory_get_from_handles (connection,
1519 handles->len, (TpHandle *) handles->data,
1520 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1522 self->priv->can_upgrade_to_muc = FALSE;
1524 tp_g_signal_connect_object (self, "group-members-changed",
1525 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1527 TpCapabilities *caps;
1532 /* Get the self contact from the connection's self handle */
1533 handle = tp_connection_get_self_handle (connection);
1534 empathy_tp_contact_factory_get_from_handle (connection,
1535 handle, tp_chat_got_self_contact_cb,
1536 NULL, NULL, G_OBJECT (self));
1538 /* Get the remote contact */
1539 handle = tp_channel_get_handle (channel, NULL);
1540 empathy_tp_contact_factory_get_from_handle (connection,
1541 handle, tp_chat_got_remote_contact_cb,
1542 NULL, NULL, G_OBJECT (self));
1544 caps = tp_connection_get_capabilities (connection);
1545 g_assert (caps != NULL);
1547 classes = tp_capabilities_get_channel_classes (caps);
1549 for (i = 0; i < classes->len; i++) {
1550 GValueArray *array = g_ptr_array_index (classes, i);
1551 const char **oprops = g_value_get_boxed (
1552 g_value_array_get_nth (array, 1));
1554 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1555 self->priv->can_upgrade_to_muc = TRUE;
1561 if (tp_proxy_has_interface_by_id (self,
1562 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT)) {
1563 tp_cli_dbus_properties_call_get_all (channel, -1,
1564 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1565 tp_chat_get_all_subject_cb,
1568 listen_for_dbus_properties_changed = TRUE;
1571 if (tp_proxy_has_interface_by_id (self,
1572 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG)) {
1573 tp_cli_dbus_properties_call_get_all (channel, -1,
1574 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1575 tp_chat_get_all_room_config_cb,
1578 listen_for_dbus_properties_changed = TRUE;
1581 if (listen_for_dbus_properties_changed) {
1582 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1583 tp_chat_dbus_properties_changed_cb,
1585 G_OBJECT (self), NULL);