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;
56 /* Room config (for now, we only track the title and don't support
60 gboolean can_upgrade_to_muc;
62 GHashTable *messages_being_sent;
64 /* GSimpleAsyncResult used when preparing EMPATHY_TP_CHAT_FEATURE_CORE */
65 GSimpleAsyncResult *ready_result;
68 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_async_cb (TpChannel *proxy,
136 GObject *weak_object)
139 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
144 create_conference_cb (GObject *source,
145 GAsyncResult *result,
148 GError *error = NULL;
150 if (!tp_account_channel_request_create_channel_finish (
151 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error)) {
152 DEBUG ("Failed to create conference channel: %s", error->message);
153 g_error_free (error);
158 tp_chat_add (EmpathyContactList *list,
159 EmpathyContact *contact,
160 const gchar *message)
162 EmpathyTpChat *self = (EmpathyTpChat *) list;
163 TpChannel *channel = (TpChannel *) self;
165 if (tp_proxy_has_interface_by_id (self,
166 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
168 GArray handles = {(gchar *) &handle, 1};
170 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
171 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
173 handle = empathy_contact_get_handle (contact);
174 tp_cli_channel_interface_group_call_add_members (channel,
175 -1, &handles, NULL, NULL, NULL, NULL, NULL);
176 } else if (self->priv->can_upgrade_to_muc) {
177 TpAccountChannelRequest *req;
179 const char *object_path;
180 GPtrArray channels = { (gpointer *) &object_path, 1 };
181 const char *invitees[2] = { NULL, };
183 invitees[0] = empathy_contact_get_id (contact);
184 object_path = tp_proxy_get_object_path (self);
187 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
188 TP_IFACE_CHANNEL_TYPE_TEXT,
189 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
191 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS,
192 TP_ARRAY_TYPE_OBJECT_PATH_LIST, &channels,
193 TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_INVITEE_IDS,
194 G_TYPE_STRV, invitees,
195 /* FIXME: InvitationMessage ? */
198 req = tp_account_channel_request_new (self->priv->account, props,
199 TP_USER_ACTION_TIME_NOT_USER_ACTION);
201 /* Although this is a MUC, it's anonymous, so CreateChannel is
203 tp_account_channel_request_create_channel_async (req, EMPATHY_CHAT_BUS_NAME,
204 NULL, create_conference_cb, NULL);
206 g_object_unref (req);
207 g_hash_table_unref (props);
209 g_warning ("Cannot add to this channel");
214 tp_chat_remove (EmpathyContactList *list,
215 EmpathyContact *contact,
216 const gchar *message)
218 EmpathyTpChat *self = (EmpathyTpChat *) list;
220 GArray handles = {(gchar *) &handle, 1};
222 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
223 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
225 handle = empathy_contact_get_handle (contact);
226 tp_cli_channel_interface_group_call_remove_members ((TpChannel *) self, -1,
233 tp_chat_get_members (EmpathyContactList *list)
235 EmpathyTpChat *self = (EmpathyTpChat *) list;
236 GList *members = NULL;
238 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
240 if (self->priv->members) {
241 members = g_list_copy (self->priv->members);
242 g_list_foreach (members, (GFunc) g_object_ref, NULL);
244 members = g_list_prepend (members, g_object_ref (self->priv->user));
245 if (self->priv->remote_contact != NULL)
246 members = g_list_prepend (members, g_object_ref (self->priv->remote_contact));
253 check_ready (EmpathyTpChat *self)
255 if (self->priv->ready_result == NULL)
258 if (g_queue_get_length (self->priv->messages_queue) > 0)
263 g_simple_async_result_complete (self->priv->ready_result);
264 tp_clear_object (&self->priv->ready_result);
268 tp_chat_emit_queued_messages (EmpathyTpChat *self)
270 EmpathyMessage *message;
272 /* Check if we can now emit some queued messages */
273 while ((message = g_queue_peek_head (self->priv->messages_queue)) != NULL) {
274 if (empathy_message_get_sender (message) == NULL) {
278 DEBUG ("Queued message ready");
279 g_queue_pop_head (self->priv->messages_queue);
280 g_queue_push_tail (self->priv->pending_messages_queue, message);
281 g_signal_emit (self, signals[MESSAGE_RECEIVED], 0, message);
288 tp_chat_got_sender_cb (TpConnection *connection,
289 EmpathyContact *contact,
294 EmpathyTpChat *self = (EmpathyTpChat *) chat;
297 DEBUG ("Error: %s", error->message);
298 /* Do not block the message queue, just drop this message */
299 g_queue_remove (self->priv->messages_queue, message);
301 empathy_message_set_sender (message, contact);
304 tp_chat_emit_queued_messages (EMPATHY_TP_CHAT (self));
308 tp_chat_build_message (EmpathyTpChat *self,
312 EmpathyMessage *message;
315 message = empathy_message_new_from_tp_message (msg, incoming);
316 /* FIXME: this is actually a lie for incoming messages. */
317 empathy_message_set_receiver (message, self->priv->user);
319 g_queue_push_tail (self->priv->messages_queue, message);
321 sender = tp_signalled_message_get_sender (msg);
322 g_assert (sender != NULL);
324 if (tp_contact_get_handle (sender) == 0) {
325 empathy_message_set_sender (message, self->priv->user);
326 tp_chat_emit_queued_messages (self);
328 TpConnection *connection = tp_channel_borrow_connection (
331 empathy_tp_contact_factory_get_from_handle (connection,
332 tp_contact_get_handle (sender),
333 tp_chat_got_sender_cb,
334 message, NULL, G_OBJECT (self));
339 handle_delivery_report (EmpathyTpChat *self,
342 TpDeliveryStatus delivery_status;
343 const GHashTable *header;
344 TpChannelTextSendError delivery_error;
347 const gchar *message_body = NULL;
348 const gchar *delivery_dbus_error;
349 const gchar *delivery_token = NULL;
351 header = tp_message_peek (message, 0);
355 delivery_token = tp_asv_get_string (header, "delivery-token");
356 delivery_status = tp_asv_get_uint32 (header, "delivery-status", &valid);
360 } else if (delivery_status == TP_DELIVERY_STATUS_ACCEPTED) {
361 DEBUG ("Accepted %s", delivery_token);
362 tp_chat_set_delivery_status (self, delivery_token,
363 EMPATHY_DELIVERY_STATUS_ACCEPTED);
365 } else if (delivery_status == TP_DELIVERY_STATUS_DELIVERED) {
366 DEBUG ("Delivered %s", delivery_token);
367 tp_chat_set_delivery_status (self, delivery_token,
368 EMPATHY_DELIVERY_STATUS_NONE);
370 } else if (delivery_status != TP_DELIVERY_STATUS_PERMANENTLY_FAILED &&
371 delivery_status != TP_DELIVERY_STATUS_TEMPORARILY_FAILED) {
375 delivery_error = tp_asv_get_uint32 (header, "delivery-error", &valid);
377 delivery_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
379 delivery_dbus_error = tp_asv_get_string (header, "delivery-dbus-error");
381 /* TODO: ideally we should use tp-glib API giving us the echoed message as a
382 * TpMessage. (fdo #35884) */
383 echo = tp_asv_get_boxed (header, "delivery-echo",
384 TP_ARRAY_TYPE_MESSAGE_PART_LIST);
385 if (echo != NULL && echo->len >= 2) {
386 const GHashTable *echo_body;
388 echo_body = g_ptr_array_index (echo, 1);
389 if (echo_body != NULL)
390 message_body = tp_asv_get_string (echo_body, "content");
393 tp_chat_set_delivery_status (self, delivery_token,
394 EMPATHY_DELIVERY_STATUS_NONE);
395 g_signal_emit (self, signals[SEND_ERROR], 0, message_body,
396 delivery_error, delivery_dbus_error);
399 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
400 message, NULL, NULL);
404 handle_incoming_message (EmpathyTpChat *self,
410 if (tp_message_is_delivery_report (message)) {
411 handle_delivery_report (self, message);
415 message_body = tp_message_to_text (message, NULL);
417 DEBUG ("Message %s (channel %s): %s",
418 pending ? "pending" : "received",
419 tp_proxy_get_object_path (self), message_body);
421 if (message_body == NULL) {
422 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
424 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
425 message, NULL, NULL);
429 tp_chat_build_message (self, message, TRUE);
431 g_free (message_body);
435 message_received_cb (TpTextChannel *channel,
439 handle_incoming_message (self, message, FALSE);
443 find_pending_message_func (gconstpointer a,
446 EmpathyMessage *msg = (EmpathyMessage *) a;
447 TpMessage *message = (TpMessage *) b;
449 if (empathy_message_get_tp_message (msg) == message)
456 pending_message_removed_cb (TpTextChannel *channel,
462 m = g_queue_find_custom (self->priv->pending_messages_queue, message,
463 find_pending_message_func);
468 g_signal_emit (self, signals[MESSAGE_ACKNOWLEDGED], 0, m->data);
470 g_object_unref (m->data);
471 g_queue_delete_link (self->priv->pending_messages_queue, m);
475 message_sent_cb (TpTextChannel *channel,
477 TpMessageSendingFlags flags,
483 message_body = tp_message_to_text (message, NULL);
485 DEBUG ("Message sent: %s", message_body);
487 tp_chat_build_message (self, message, FALSE);
489 g_free (message_body);
492 static TpChannelTextSendError
493 error_to_text_send_error (GError *error)
495 if (error->domain != TP_ERRORS)
496 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
498 switch (error->code) {
499 case TP_ERROR_OFFLINE:
500 return TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
501 case TP_ERROR_INVALID_HANDLE:
502 return TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
503 case TP_ERROR_PERMISSION_DENIED:
504 return TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
505 case TP_ERROR_NOT_IMPLEMENTED:
506 return TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
509 return TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
513 message_send_cb (GObject *source,
514 GAsyncResult *result,
517 EmpathyTpChat *self = user_data;
518 TpTextChannel *channel = (TpTextChannel *) source;
520 GError *error = NULL;
522 if (!tp_text_channel_send_message_finish (channel, result, &token, &error)) {
523 DEBUG ("Error: %s", error->message);
525 /* FIXME: we should use the body of the message as first argument of the
526 * signal but can't easily get it as we just get a user_data pointer. Once
527 * we'll have rebased EmpathyTpChat on top of TpTextChannel we'll be able
528 * to use the user_data pointer to pass the message and fix this. */
529 g_signal_emit (self, signals[SEND_ERROR], 0,
530 NULL, error_to_text_send_error (error), NULL);
532 g_error_free (error);
535 tp_chat_set_delivery_status (self, token,
536 EMPATHY_DELIVERY_STATUS_SENDING);
542 TpChannelChatState state;
546 tp_chat_state_changed_got_contact_cb (TpConnection *connection,
547 EmpathyContact *contact,
552 TpChannelChatState state;
555 DEBUG ("Error: %s", error->message);
559 state = GPOINTER_TO_UINT (user_data);
560 DEBUG ("Chat state changed for %s (%d): %d",
561 empathy_contact_get_alias (contact),
562 empathy_contact_get_handle (contact), state);
564 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
568 tp_chat_state_changed_cb (TpChannel *channel,
570 TpChannelChatState state,
573 TpConnection *connection = tp_channel_borrow_connection (
576 empathy_tp_contact_factory_get_from_handle (connection, handle,
577 tp_chat_state_changed_got_contact_cb, GUINT_TO_POINTER (state),
578 NULL, G_OBJECT (self));
582 list_pending_messages (EmpathyTpChat *self)
586 messages = tp_text_channel_get_pending_messages (
587 TP_TEXT_CHANNEL (self));
589 for (l = messages; l != NULL; l = g_list_next (l)) {
590 TpMessage *message = l->data;
592 handle_incoming_message (self, message, FALSE);
595 g_list_free (messages);
599 update_subject (EmpathyTpChat *self,
600 GHashTable *properties)
602 EmpathyTpChatPrivate *priv = self->priv;
603 gboolean can_set, valid;
604 const gchar *subject;
606 can_set = tp_asv_get_boolean (properties, "CanSet", &valid);
608 priv->can_set_subject = can_set;
611 subject = tp_asv_get_string (properties, "Subject");
612 if (subject != NULL) {
615 g_free (priv->subject);
616 priv->subject = g_strdup (subject);
618 /* If the actor is included with this update, use it;
619 * otherwise, clear it to avoid showing stale information.
620 * Why might it not be included? When you join an IRC channel,
621 * you get a pair of messages: first, the current topic; next,
622 * who set it, and when. Idle reports these in two separate
625 actor = tp_asv_get_string (properties, "Actor");
626 g_free (priv->subject_actor);
627 priv->subject_actor = g_strdup (actor);
629 g_object_notify (G_OBJECT (self), "subject");
632 /* TODO: track Timestamp. */
636 tp_chat_get_all_subject_cb (TpProxy *proxy,
637 GHashTable *properties,
639 gpointer user_data G_GNUC_UNUSED,
642 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
643 EmpathyTpChatPrivate *priv = self->priv;
646 DEBUG ("Error fetching subject: %s", error->message);
650 priv->supports_subject = TRUE;
651 update_subject (self, properties);
655 update_title (EmpathyTpChat *self,
656 GHashTable *properties)
658 EmpathyTpChatPrivate *priv = self->priv;
659 const gchar *title = tp_asv_get_string (properties, "Title");
662 if (tp_str_empty (title)) {
666 g_free (priv->title);
667 priv->title = g_strdup (title);
668 g_object_notify (G_OBJECT (self), "title");
673 tp_chat_get_all_room_config_cb (TpProxy *proxy,
674 GHashTable *properties,
676 gpointer user_data G_GNUC_UNUSED,
679 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
682 DEBUG ("Error fetching room config: %s", error->message);
686 update_title (self, properties);
690 tp_chat_dbus_properties_changed_cb (TpProxy *proxy,
691 const gchar *interface_name,
693 const gchar **invalidated,
697 EmpathyTpChat *self = EMPATHY_TP_CHAT (chat);
699 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT)) {
700 update_subject (self, changed);
703 if (!tp_strdiff (interface_name, TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG)) {
704 update_title (self, changed);
709 empathy_tp_chat_set_subject (EmpathyTpChat *self,
710 const gchar *subject)
712 tp_cli_channel_interface_subject_call_set_subject (TP_CHANNEL (self), -1,
715 "while setting subject", NULL,
720 empathy_tp_chat_get_title (EmpathyTpChat *self)
722 EmpathyTpChatPrivate *priv = self->priv;
728 empathy_tp_chat_supports_subject (EmpathyTpChat *self)
730 EmpathyTpChatPrivate *priv = self->priv;
732 return priv->supports_subject;
736 empathy_tp_chat_can_set_subject (EmpathyTpChat *self)
738 EmpathyTpChatPrivate *priv = self->priv;
740 return priv->can_set_subject;
744 empathy_tp_chat_get_subject (EmpathyTpChat *self)
746 EmpathyTpChatPrivate *priv = self->priv;
748 return priv->subject;
752 empathy_tp_chat_get_subject_actor (EmpathyTpChat *self)
754 EmpathyTpChatPrivate *priv = self->priv;
756 return priv->subject_actor;
760 tp_chat_dispose (GObject *object)
762 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
764 tp_clear_object (&self->priv->account);
765 tp_clear_object (&self->priv->remote_contact);
766 tp_clear_object (&self->priv->user);
768 g_queue_foreach (self->priv->messages_queue, (GFunc) g_object_unref, NULL);
769 g_queue_clear (self->priv->messages_queue);
771 g_queue_foreach (self->priv->pending_messages_queue,
772 (GFunc) g_object_unref, NULL);
773 g_queue_clear (self->priv->pending_messages_queue);
775 tp_clear_object (&self->priv->ready_result);
777 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
778 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
782 tp_chat_finalize (GObject *object)
784 EmpathyTpChat *self = (EmpathyTpChat *) object;
786 DEBUG ("Finalize: %p", object);
788 g_queue_free (self->priv->messages_queue);
789 g_queue_free (self->priv->pending_messages_queue);
790 g_hash_table_unref (self->priv->messages_being_sent);
792 g_free (self->priv->title);
793 g_free (self->priv->subject);
794 g_free (self->priv->subject_actor);
796 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
800 check_almost_ready (EmpathyTpChat *self)
802 TpChannel *channel = (TpChannel *) self;
804 if (self->priv->ready_result == NULL)
807 if (self->priv->user == NULL)
810 /* We need either the members (room) or the remote contact (private chat).
811 * If the chat is protected by a password we can't get these information so
812 * consider the chat as ready so it can be presented to the user. */
813 if (!tp_channel_password_needed (channel) && self->priv->members == NULL &&
814 self->priv->remote_contact == NULL)
817 g_assert (tp_proxy_is_prepared (self,
818 TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES));
820 tp_g_signal_connect_object (self, "message-received",
821 G_CALLBACK (message_received_cb), self, 0);
822 tp_g_signal_connect_object (self, "pending-message-removed",
823 G_CALLBACK (pending_message_removed_cb), self, 0);
825 list_pending_messages (self);
827 tp_g_signal_connect_object (self, "message-sent",
828 G_CALLBACK (message_sent_cb), self, 0);
830 tp_g_signal_connect_object (self, "chat-state-changed",
831 G_CALLBACK (tp_chat_state_changed_cb), self, 0);
837 tp_chat_got_added_contacts_cb (TpConnection *connection,
839 EmpathyContact * const * contacts,
841 const TpHandle *failed,
846 EmpathyTpChat *self = (EmpathyTpChat *) chat;
848 const TpIntSet *members;
850 EmpathyContact *contact;
853 DEBUG ("Error: %s", error->message);
857 members = tp_channel_group_get_members ((TpChannel *) self);
858 for (i = 0; i < n_contacts; i++) {
859 contact = contacts[i];
860 handle = empathy_contact_get_handle (contact);
862 /* Make sure the contact is still member */
863 if (tp_intset_is_member (members, handle)) {
864 self->priv->members = g_list_prepend (self->priv->members,
865 g_object_ref (contact));
866 g_signal_emit_by_name (chat, "members-changed",
867 contact, NULL, 0, NULL, TRUE);
871 check_almost_ready (EMPATHY_TP_CHAT (chat));
874 static EmpathyContact *
875 chat_lookup_contact (EmpathyTpChat *self,
881 for (l = self->priv->members; l; l = l->next) {
882 EmpathyContact *c = l->data;
884 if (empathy_contact_get_handle (c) != handle) {
889 /* Caller takes the reference. */
890 self->priv->members = g_list_delete_link (self->priv->members, l);
908 static ContactRenameData *
909 contact_rename_data_new (TpHandle handle,
911 const gchar* message)
913 ContactRenameData *data = g_new (ContactRenameData, 1);
914 data->old_handle = handle;
915 data->reason = reason;
916 data->message = g_strdup (message);
922 contact_rename_data_free (ContactRenameData* data)
924 g_free (data->message);
929 tp_chat_got_renamed_contacts_cb (TpConnection *connection,
931 EmpathyContact * const * contacts,
933 const TpHandle *failed,
938 EmpathyTpChat *self = (EmpathyTpChat *) chat;
939 const TpIntSet *members;
941 EmpathyContact *old = NULL, *new = NULL;
942 ContactRenameData *rename_data = (ContactRenameData *) user_data;
945 DEBUG ("Error: %s", error->message);
949 /* renamed members can only be delivered one at a time */
950 g_warn_if_fail (n_contacts == 1);
954 members = tp_channel_group_get_members ((TpChannel *) self);
955 handle = empathy_contact_get_handle (new);
957 old = chat_lookup_contact (self, rename_data->old_handle, TRUE);
959 /* Make sure the contact is still member */
960 if (tp_intset_is_member (members, handle)) {
961 self->priv->members = g_list_prepend (self->priv->members,
965 g_signal_emit_by_name (self, "member-renamed",
966 old, new, rename_data->reason,
967 rename_data->message);
968 g_object_unref (old);
972 if (self->priv->user == old) {
973 /* We change our nick */
974 tp_clear_object (&self->priv->user);
975 self->priv->user = g_object_ref (new);
976 g_object_notify (chat, "self-contact");
979 check_almost_ready (self);
984 tp_chat_group_members_changed_cb (TpChannel *channel,
988 GArray *local_pending,
989 GArray *remote_pending,
994 EmpathyContact *contact;
995 EmpathyContact *actor_contact = NULL;
997 ContactRenameData *rename_data;
999 TpConnection *connection = tp_channel_borrow_connection (
1000 (TpChannel *) self);
1002 /* Contact renamed */
1003 if (reason == TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED) {
1004 /* there can only be a single 'added' and a single 'removed' handle */
1005 if (removed->len != 1 || added->len != 1) {
1006 g_warning ("RENAMED with %u added, %u removed (expected 1, 1)",
1007 added->len, removed->len);
1011 old_handle = g_array_index (removed, guint, 0);
1013 rename_data = contact_rename_data_new (old_handle, reason, message);
1014 empathy_tp_contact_factory_get_from_handles (connection,
1015 added->len, (TpHandle *) added->data,
1016 tp_chat_got_renamed_contacts_cb,
1017 rename_data, (GDestroyNotify) contact_rename_data_free,
1023 actor_contact = chat_lookup_contact (self, actor, FALSE);
1024 if (actor_contact == NULL) {
1025 /* FIXME: handle this a tad more gracefully: perhaps
1026 * the actor was a server op. We could use the
1027 * contact-ids detail of MembersChangedDetailed.
1029 DEBUG ("actor %u not a channel member", actor);
1033 /* Remove contacts that are not members anymore */
1034 for (i = 0; i < removed->len; i++) {
1035 contact = chat_lookup_contact (self,
1036 g_array_index (removed, TpHandle, i), TRUE);
1038 if (contact != NULL) {
1039 g_signal_emit_by_name (self, "members-changed", contact,
1040 actor_contact, reason, message,
1042 g_object_unref (contact);
1046 /* Request added contacts */
1047 if (added->len > 0) {
1048 empathy_tp_contact_factory_get_from_handles (connection,
1049 added->len, (TpHandle *) added->data,
1050 tp_chat_got_added_contacts_cb, NULL, NULL,
1054 if (actor_contact != NULL) {
1055 g_object_unref (actor_contact);
1060 tp_chat_got_remote_contact_cb (TpConnection *connection,
1061 EmpathyContact *contact,
1062 const GError *error,
1066 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1069 DEBUG ("Error: %s", error->message);
1070 empathy_tp_chat_leave (self, "");
1074 self->priv->remote_contact = g_object_ref (contact);
1075 g_object_notify (chat, "remote-contact");
1077 check_almost_ready (self);
1081 tp_chat_got_self_contact_cb (TpConnection *connection,
1082 EmpathyContact *contact,
1083 const GError *error,
1087 EmpathyTpChat *self = (EmpathyTpChat *) chat;
1090 DEBUG ("Error: %s", error->message);
1091 empathy_tp_chat_leave (self, "");
1095 self->priv->user = g_object_ref (contact);
1096 empathy_contact_set_is_user (self->priv->user, TRUE);
1097 g_object_notify (chat, "self-contact");
1098 check_almost_ready (self);
1102 tp_chat_get_property (GObject *object,
1107 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1111 g_value_set_object (value, self->priv->account);
1113 case PROP_SELF_CONTACT:
1114 g_value_set_object (value, self->priv->user);
1116 case PROP_REMOTE_CONTACT:
1117 g_value_set_object (value, self->priv->remote_contact);
1119 case PROP_N_MESSAGES_SENDING:
1120 g_value_set_uint (value,
1121 g_hash_table_size (self->priv->messages_being_sent));
1124 g_value_set_string (value,
1125 empathy_tp_chat_get_title (self));
1128 g_value_set_string (value,
1129 empathy_tp_chat_get_subject (self));
1132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1138 tp_chat_set_property (GObject *object,
1140 const GValue *value,
1143 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
1147 self->priv->account = g_value_dup_object (value);
1150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1160 static const TpProxyFeature *
1161 tp_chat_list_features (TpProxyClass *cls G_GNUC_UNUSED)
1163 static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
1164 static GQuark need[2] = {0, 0};
1166 if (G_LIKELY (features[0].name != 0))
1169 features[FEAT_READY].name = EMPATHY_TP_CHAT_FEATURE_READY;
1170 need[0] = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
1171 features[FEAT_READY].depends_on = need;
1172 features[FEAT_READY].prepare_async =
1173 tp_chat_prepare_ready_async;
1175 /* assert that the terminator at the end is there */
1176 g_assert (features[N_FEAT].name == 0);
1182 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1185 TpProxyClass *proxy_class = TP_PROXY_CLASS (klass);
1187 object_class->dispose = tp_chat_dispose;
1188 object_class->finalize = tp_chat_finalize;
1189 object_class->get_property = tp_chat_get_property;
1190 object_class->set_property = tp_chat_set_property;
1192 proxy_class->list_features = tp_chat_list_features;
1194 g_object_class_install_property (object_class,
1196 g_param_spec_object ("account",
1198 "the account associated with the chat",
1201 G_PARAM_CONSTRUCT_ONLY |
1202 G_PARAM_STATIC_STRINGS));
1205 * EmpathyTpChat:self-contact:
1207 * Not to be confused with TpChannel:group-self-contact.
1209 g_object_class_install_property (object_class,
1211 g_param_spec_object ("self-contact",
1212 "The local contact",
1213 "The EmpathyContact for the local user on this channel",
1214 EMPATHY_TYPE_CONTACT,
1217 g_object_class_install_property (object_class,
1218 PROP_REMOTE_CONTACT,
1219 g_param_spec_object ("remote-contact",
1220 "The remote contact",
1221 "The remote contact if there is no group iface on the channel",
1222 EMPATHY_TYPE_CONTACT,
1225 g_object_class_install_property (object_class,
1226 PROP_N_MESSAGES_SENDING,
1227 g_param_spec_uint ("n-messages-sending",
1228 "Num Messages Sending",
1229 "The number of messages being sent",
1233 g_object_class_install_property (object_class,
1235 g_param_spec_string ("title",
1237 "A human-readable name for the room, if any",
1240 G_PARAM_STATIC_STRINGS));
1242 g_object_class_install_property (object_class,
1244 g_param_spec_string ("subject",
1246 "The room's current subject, if any",
1249 G_PARAM_STATIC_STRINGS));
1252 signals[MESSAGE_RECEIVED] =
1253 g_signal_new ("message-received-empathy",
1254 G_TYPE_FROM_CLASS (klass),
1258 g_cclosure_marshal_generic,
1260 1, EMPATHY_TYPE_MESSAGE);
1262 signals[SEND_ERROR] =
1263 g_signal_new ("send-error",
1264 G_TYPE_FROM_CLASS (klass),
1268 g_cclosure_marshal_generic,
1270 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);
1272 signals[CHAT_STATE_CHANGED] =
1273 g_signal_new ("chat-state-changed-empathy",
1274 G_TYPE_FROM_CLASS (klass),
1278 g_cclosure_marshal_generic,
1280 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1282 signals[MESSAGE_ACKNOWLEDGED] =
1283 g_signal_new ("message-acknowledged",
1284 G_TYPE_FROM_CLASS (klass),
1288 g_cclosure_marshal_generic,
1290 1, EMPATHY_TYPE_MESSAGE);
1292 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPrivate));
1296 empathy_tp_chat_init (EmpathyTpChat *self)
1298 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1299 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPrivate);
1301 self->priv->messages_queue = g_queue_new ();
1302 self->priv->pending_messages_queue = g_queue_new ();
1303 self->priv->messages_being_sent = g_hash_table_new_full (
1304 g_str_hash, g_str_equal, g_free, NULL);
1308 tp_chat_iface_init (EmpathyContactListIface *iface)
1310 iface->add = tp_chat_add;
1311 iface->remove = tp_chat_remove;
1312 iface->get_members = tp_chat_get_members;
1316 empathy_tp_chat_new (
1317 TpSimpleClientFactory *factory,
1320 const gchar *object_path,
1321 const GHashTable *immutable_properties)
1323 TpProxy *conn_proxy = (TpProxy *) conn;
1325 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1326 g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
1327 g_return_val_if_fail (immutable_properties != NULL, NULL);
1329 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1333 "dbus-daemon", conn_proxy->dbus_daemon,
1334 "bus-name", conn_proxy->bus_name,
1335 "object-path", object_path,
1336 "channel-properties", immutable_properties,
1341 empathy_tp_chat_get_id (EmpathyTpChat *self)
1345 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1347 id = tp_channel_get_identifier ((TpChannel *) self);
1348 if (!EMP_STR_EMPTY (id))
1350 else if (self->priv->remote_contact)
1351 return empathy_contact_get_id (self->priv->remote_contact);
1358 empathy_tp_chat_get_remote_contact (EmpathyTpChat *self)
1360 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1362 return self->priv->remote_contact;
1366 empathy_tp_chat_get_account (EmpathyTpChat *self)
1368 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1370 return self->priv->account;
1374 empathy_tp_chat_send (EmpathyTpChat *self,
1377 gchar *message_body;
1379 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1380 g_return_if_fail (TP_IS_CLIENT_MESSAGE (message));
1382 message_body = tp_message_to_text (message, NULL);
1384 DEBUG ("Sending message: %s", message_body);
1386 tp_text_channel_send_message_async (TP_TEXT_CHANNEL (self),
1387 message, TP_MESSAGE_SENDING_FLAG_REPORT_DELIVERY,
1388 message_send_cb, self);
1390 g_free (message_body);
1394 empathy_tp_chat_get_pending_messages (EmpathyTpChat *self)
1396 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1398 return self->priv->pending_messages_queue->head;
1402 empathy_tp_chat_acknowledge_message (EmpathyTpChat *self,
1403 EmpathyMessage *message) {
1406 g_return_if_fail (EMPATHY_IS_TP_CHAT (self));
1408 if (!empathy_message_is_incoming (message))
1411 tp_msg = empathy_message_get_tp_message (message);
1412 tp_text_channel_ack_message_async (TP_TEXT_CHANNEL (self),
1413 tp_msg, NULL, NULL);
1417 * empathy_tp_chat_can_add_contact:
1419 * Returns: %TRUE if empathy_contact_list_add() will work for this channel.
1420 * That is if this chat is a 1-to-1 channel that can be upgraded to
1421 * a MUC using the Conference interface or if the channel is a MUC.
1424 empathy_tp_chat_can_add_contact (EmpathyTpChat *self)
1426 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), FALSE);
1428 return self->priv->can_upgrade_to_muc ||
1429 tp_proxy_has_interface_by_id (self,
1430 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);;
1434 tp_channel_leave_async_cb (GObject *source_object,
1438 GError *error = NULL;
1440 if (!tp_channel_leave_finish (TP_CHANNEL (source_object), res, &error)) {
1441 DEBUG ("Could not leave channel properly: (%s); closing the channel",
1443 g_error_free (error);
1448 empathy_tp_chat_leave (EmpathyTpChat *self,
1449 const gchar *message)
1451 TpChannel *channel = (TpChannel *) self;
1453 DEBUG ("Leaving channel %s with message \"%s\"",
1454 tp_channel_get_identifier (channel), message);
1456 tp_channel_leave_async (channel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1457 message, tp_channel_leave_async_cb, self);
1461 add_members_cb (TpChannel *proxy,
1462 const GError *error,
1464 GObject *weak_object)
1466 EmpathyTpChat *self = (EmpathyTpChat *) weak_object;
1468 if (error != NULL) {
1469 DEBUG ("Failed to join chat (%s): %s",
1470 tp_channel_get_identifier ((TpChannel *) self), error->message);
1475 empathy_tp_chat_join (EmpathyTpChat *self)
1477 TpHandle self_handle;
1480 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1482 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1483 g_array_append_val (members, self_handle);
1485 tp_cli_channel_interface_group_call_add_members ((TpChannel *) self, -1, members,
1486 "", add_members_cb, NULL, NULL, G_OBJECT (self));
1488 g_array_unref (members);
1492 empathy_tp_chat_is_invited (EmpathyTpChat *self,
1495 TpHandle self_handle;
1497 if (!tp_proxy_has_interface (self, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1500 self_handle = tp_channel_group_get_self_handle ((TpChannel *) self);
1501 if (self_handle == 0)
1504 return tp_channel_group_get_local_pending_info ((TpChannel *) self, self_handle,
1505 inviter, NULL, NULL);
1509 empathy_tp_chat_get_chat_state (EmpathyTpChat *self,
1510 EmpathyContact *contact)
1512 return tp_channel_get_chat_state ((TpChannel *) self,
1513 empathy_contact_get_handle (contact));
1517 empathy_tp_chat_get_self_contact (EmpathyTpChat *self)
1519 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (self), NULL);
1521 return self->priv->user;
1525 empathy_tp_chat_get_feature_ready (void)
1527 return g_quark_from_static_string ("empathy-tp-chat-feature-ready");
1531 tp_chat_prepare_ready_async (TpProxy *proxy,
1532 const TpProxyFeature *feature,
1533 GAsyncReadyCallback callback,
1536 EmpathyTpChat *self = (EmpathyTpChat *) proxy;
1537 TpChannel *channel = (TpChannel *) proxy;
1538 TpConnection *connection;
1539 gboolean listen_for_dbus_properties_changed = FALSE;
1541 g_assert (self->priv->ready_result == NULL);
1542 self->priv->ready_result = g_simple_async_result_new (G_OBJECT (self),
1543 callback, user_data, tp_chat_prepare_ready_async);
1545 connection = tp_channel_borrow_connection (channel);
1547 if (tp_proxy_has_interface_by_id (self,
1548 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
1549 const TpIntSet *members;
1553 /* Get self contact from the group's self handle */
1554 handle = tp_channel_group_get_self_handle (channel);
1555 empathy_tp_contact_factory_get_from_handle (connection,
1556 handle, tp_chat_got_self_contact_cb,
1557 NULL, NULL, G_OBJECT (self));
1559 /* Get initial member contacts */
1560 members = tp_channel_group_get_members (channel);
1561 handles = tp_intset_to_array (members);
1562 empathy_tp_contact_factory_get_from_handles (connection,
1563 handles->len, (TpHandle *) handles->data,
1564 tp_chat_got_added_contacts_cb, NULL, NULL, G_OBJECT (self));
1566 self->priv->can_upgrade_to_muc = FALSE;
1568 tp_g_signal_connect_object (self, "group-members-changed",
1569 G_CALLBACK (tp_chat_group_members_changed_cb), self, 0);
1571 TpCapabilities *caps;
1576 /* Get the self contact from the connection's self handle */
1577 handle = tp_connection_get_self_handle (connection);
1578 empathy_tp_contact_factory_get_from_handle (connection,
1579 handle, tp_chat_got_self_contact_cb,
1580 NULL, NULL, G_OBJECT (self));
1582 /* Get the remote contact */
1583 handle = tp_channel_get_handle (channel, NULL);
1584 empathy_tp_contact_factory_get_from_handle (connection,
1585 handle, tp_chat_got_remote_contact_cb,
1586 NULL, NULL, G_OBJECT (self));
1588 caps = tp_connection_get_capabilities (connection);
1589 g_assert (caps != NULL);
1591 classes = tp_capabilities_get_channel_classes (caps);
1593 for (i = 0; i < classes->len; i++) {
1594 GValueArray *array = g_ptr_array_index (classes, i);
1595 const char **oprops = g_value_get_boxed (
1596 g_value_array_get_nth (array, 1));
1598 if (tp_strv_contains (oprops, TP_PROP_CHANNEL_INTERFACE_CONFERENCE_INITIAL_CHANNELS)) {
1599 self->priv->can_upgrade_to_muc = TRUE;
1605 if (tp_proxy_has_interface_by_id (self,
1606 TP_IFACE_QUARK_CHANNEL_INTERFACE_SUBJECT)) {
1607 tp_cli_dbus_properties_call_get_all (channel, -1,
1608 TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
1609 tp_chat_get_all_subject_cb,
1612 listen_for_dbus_properties_changed = TRUE;
1615 if (tp_proxy_has_interface_by_id (self,
1616 TP_IFACE_QUARK_CHANNEL_INTERFACE_ROOM_CONFIG)) {
1617 tp_cli_dbus_properties_call_get_all (channel, -1,
1618 TP_IFACE_CHANNEL_INTERFACE_ROOM_CONFIG,
1619 tp_chat_get_all_room_config_cb,
1622 listen_for_dbus_properties_changed = TRUE;
1625 if (listen_for_dbus_properties_changed) {
1626 tp_cli_dbus_properties_connect_to_properties_changed (channel,
1627 tp_chat_dbus_properties_changed_cb,
1629 G_OBJECT (self), NULL);