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/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-tp-group.h"
32 #include "empathy-contact-factory.h"
33 #include "empathy-contact-monitor.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-marshal.h"
36 #include "empathy-time.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
44 gboolean dispose_has_run;
45 EmpathyContactFactory *factory;
46 EmpathyContactMonitor *contact_monitor;
48 EmpathyContact *remote_contact;
49 EmpathyTpGroup *group;
53 gboolean listing_pending_messages;
54 /* Queue of messages not signalled yet */
55 GQueue *messages_queue;
56 /* Queue of messages signalled but not acked yet */
57 GQueue *pending_messages_queue;
58 gboolean had_properties_list;
59 GPtrArray *properties;
67 TpPropertyFlags flags;
71 static void tp_chat_iface_init (EmpathyContactListIface *iface);
89 static guint signals[LAST_SIGNAL];
91 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
92 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
95 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
98 tp_chat_invalidated_cb (TpProxy *proxy,
104 EmpathyTpChatPriv *priv = GET_PRIV (chat);
106 g_object_unref (priv->channel);
107 priv->channel = NULL;
109 DEBUG ("Channel invalidated: %s", message);
110 g_signal_emit (chat, signals[DESTROY], 0);
115 tp_chat_async_cb (TpChannel *proxy,
118 GObject *weak_object)
121 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
126 tp_chat_member_added_cb (EmpathyTpGroup *group,
127 EmpathyContact *contact,
128 EmpathyContact *actor,
130 const gchar *message,
133 EmpathyTpChatPriv *priv = GET_PRIV (chat);
134 guint handle_type = 0;
136 if (priv->channel == NULL)
139 priv->members_count++;
140 g_signal_emit_by_name (chat, "members-changed",
141 contact, actor, reason, message,
144 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
145 if (handle_type == TP_HANDLE_TYPE_ROOM) {
149 if (priv->members_count > 2 && priv->remote_contact) {
150 /* We now have more than 2 members, this is not a p2p chat
151 * anymore. Remove the remote-contact as it makes no sense, the
152 * EmpathyContactList interface must be used now. */
153 g_object_unref (priv->remote_contact);
154 priv->remote_contact = NULL;
155 g_object_notify (G_OBJECT (chat), "remote-contact");
157 if (priv->members_count <= 2 && !priv->remote_contact &&
158 !empathy_contact_is_user (contact)) {
159 /* This is a p2p chat, if it's not ourself that means this is
160 * the remote contact with who we are chatting. This is to
161 * avoid forcing the usage of the EmpathyContactList interface
163 priv->remote_contact = g_object_ref (contact);
164 g_object_notify (G_OBJECT (chat), "remote-contact");
169 tp_chat_member_removed_cb (EmpathyTpGroup *group,
170 EmpathyContact *contact,
171 EmpathyContact *actor,
173 const gchar *message,
176 EmpathyTpChatPriv *priv = GET_PRIV (chat);
177 guint handle_type = 0;
179 if (priv->channel == NULL)
182 priv->members_count--;
183 g_signal_emit_by_name (chat, "members-changed",
184 contact, actor, reason, message,
187 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
188 if (handle_type == TP_HANDLE_TYPE_ROOM) {
192 if (priv->members_count <= 2 && !priv->remote_contact) {
195 /* We are not a MUC anymore, get the remote contact back */
196 members = empathy_tp_group_get_members (group);
197 for (l = members; l; l = l->next) {
198 if (!empathy_contact_is_user (l->data)) {
199 priv->remote_contact = g_object_ref (l->data);
200 g_object_notify (G_OBJECT (chat), "remote-contact");
204 g_list_foreach (members, (GFunc) g_object_unref, NULL);
205 g_list_free (members);
210 tp_chat_local_pending_cb (EmpathyTpGroup *group,
211 EmpathyContact *contact,
212 EmpathyContact *actor,
214 const gchar *message,
217 EmpathyTpChatPriv *priv = GET_PRIV (chat);
219 if (priv->channel == NULL)
222 g_signal_emit_by_name (chat, "pendings-changed",
223 contact, actor, reason, message,
228 tp_chat_add (EmpathyContactList *list,
229 EmpathyContact *contact,
230 const gchar *message)
232 EmpathyTpChatPriv *priv = GET_PRIV (list);
234 GArray handles = {(gchar *) &handle, 1};
236 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
237 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
239 handle = empathy_contact_get_handle (contact);
240 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
247 tp_chat_remove (EmpathyContactList *list,
248 EmpathyContact *contact,
249 const gchar *message)
251 EmpathyTpChatPriv *priv = GET_PRIV (list);
253 GArray handles = {(gchar *) &handle, 1};
255 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
256 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
258 handle = empathy_contact_get_handle (contact);
259 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
266 tp_chat_get_members (EmpathyContactList *list)
268 EmpathyTpChatPriv *priv = GET_PRIV (list);
269 GList *members = NULL;
271 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
274 members = empathy_tp_group_get_members (priv->group);
276 members = g_list_prepend (members, g_object_ref (priv->user));
277 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
283 static EmpathyContactMonitor *
284 tp_chat_get_monitor (EmpathyContactList *list)
286 EmpathyTpChatPriv *priv;
288 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
290 priv = GET_PRIV (list);
292 if (priv->contact_monitor == NULL) {
293 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
296 return priv->contact_monitor;
299 static EmpathyMessage *
300 tp_chat_build_message (EmpathyTpChat *chat,
305 const gchar *message_body)
307 EmpathyTpChatPriv *priv;
308 EmpathyMessage *message;
309 EmpathyContact *sender;
311 priv = GET_PRIV (chat);
313 if (from_handle == 0) {
314 sender = g_object_ref (priv->user);
316 sender = empathy_contact_factory_get_from_handle (priv->factory,
321 message = empathy_message_new (message_body);
322 empathy_message_set_tptype (message, type);
323 empathy_message_set_sender (message, sender);
324 empathy_message_set_receiver (message, priv->user);
325 empathy_message_set_timestamp (message, timestamp);
326 empathy_message_set_id (message, id);
328 g_object_unref (sender);
334 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
335 GParamSpec *param_spec,
338 EmpathyTpChatPriv *priv = GET_PRIV (chat);
339 EmpathyMessage *message;
340 EmpathyContactReady ready;
341 EmpathyContact *sender = NULL;
342 gboolean removed = FALSE;
344 /* Emit all messages queued until we find a message with not
345 * ready sender (in case of a MUC we could have more than one sender).
346 * When leaving this loop, sender is the first not ready contact queued
347 * and removed tells if at least one message got removed
349 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
350 sender = empathy_message_get_sender (message);
351 ready = empathy_contact_get_ready (sender);
353 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
354 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
358 DEBUG ("Queued message ready");
359 message = g_queue_pop_head (priv->messages_queue);
360 g_queue_push_tail (priv->pending_messages_queue, message);
361 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
366 /* We removed at least one message from the queue, disconnect
367 * the ready signal from the previous contact */
368 g_signal_handlers_disconnect_by_func (contact,
369 tp_chat_sender_ready_notify_cb,
372 if (g_queue_get_length (priv->messages_queue) > 0) {
373 /* We still have queued message, connect the ready
374 * signal on the new first message sender. */
375 g_signal_connect (sender, "notify::ready",
376 G_CALLBACK (tp_chat_sender_ready_notify_cb),
383 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
384 EmpathyMessage *message)
386 EmpathyTpChatPriv *priv = GET_PRIV (chat);
387 EmpathyContact *sender;
388 EmpathyContactReady ready;
390 if (g_queue_get_length (priv->messages_queue) > 0) {
391 DEBUG ("Message queue not empty");
392 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
397 sender = empathy_message_get_sender (message);
398 ready = empathy_contact_get_ready (sender);
399 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
400 (ready & EMPATHY_CONTACT_READY_ID)) {
401 DEBUG ("Message queue empty and sender ready");
402 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
403 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
407 DEBUG ("Sender not ready");
408 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
409 g_signal_connect (sender, "notify::ready",
410 G_CALLBACK (tp_chat_sender_ready_notify_cb),
415 tp_chat_received_cb (TpChannel *channel,
421 const gchar *message_body,
425 EmpathyTpChatPriv *priv = GET_PRIV (chat);
426 EmpathyMessage *message;
428 if (priv->channel == NULL)
431 if (priv->listing_pending_messages) {
435 DEBUG ("Message received: %s", message_body);
437 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
438 !tp_strdiff (message_body, "")) {
441 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
443 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
444 g_array_append_val (ids, message_id);
445 acknowledge_messages (EMPATHY_TP_CHAT (chat), ids);
446 g_array_free (ids, TRUE);
451 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
458 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
459 g_object_unref (message);
463 tp_chat_sent_cb (TpChannel *channel,
466 const gchar *message_body,
470 EmpathyTpChatPriv *priv = GET_PRIV (chat);
471 EmpathyMessage *message;
473 if (priv->channel == NULL)
476 DEBUG ("Message sent: %s", message_body);
478 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
485 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
486 g_object_unref (message);
490 tp_chat_send_error_cb (TpChannel *channel,
494 const gchar *message_body,
498 EmpathyMessage *message;
499 EmpathyTpChatPriv *priv = GET_PRIV (chat);
501 if (priv->channel == NULL)
504 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
506 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
513 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
514 g_object_unref (message);
518 tp_chat_send_cb (TpChannel *proxy,
523 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
526 DEBUG ("Error: %s", error->message);
527 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
528 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
533 tp_chat_state_changed_cb (TpChannel *channel,
539 EmpathyTpChatPriv *priv = GET_PRIV (chat);
540 EmpathyContact *contact;
542 if (priv->channel == NULL)
545 contact = empathy_contact_factory_get_from_handle (priv->factory,
549 DEBUG ("Chat state changed for %s (%d): %d",
550 empathy_contact_get_name (contact), handle, state);
552 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
553 g_object_unref (contact);
557 tp_chat_list_pending_messages_cb (TpChannel *channel,
558 const GPtrArray *messages_list,
563 EmpathyTpChatPriv *priv = GET_PRIV (chat);
565 GArray *empty_non_text_content_ids = NULL;
567 priv->listing_pending_messages = FALSE;
569 if (priv->channel == NULL)
573 DEBUG ("Error listing pending messages: %s", error->message);
577 for (i = 0; i < messages_list->len; i++) {
578 EmpathyMessage *message;
579 GValueArray *message_struct;
580 const gchar *message_body;
587 message_struct = g_ptr_array_index (messages_list, i);
589 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
590 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
591 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
592 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
593 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
594 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
596 DEBUG ("Message pending: %s", message_body);
598 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
599 !tp_strdiff (message_body, "")) {
600 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
602 if (empty_non_text_content_ids == NULL) {
603 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
606 g_array_append_val (empty_non_text_content_ids, message_id);
610 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
617 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
618 g_object_unref (message);
621 if (empty_non_text_content_ids != NULL) {
622 acknowledge_messages (EMPATHY_TP_CHAT (chat), empty_non_text_content_ids);
623 g_array_free (empty_non_text_content_ids, TRUE);
628 tp_chat_property_flags_changed_cb (TpProxy *proxy,
629 const GPtrArray *properties,
633 EmpathyTpChatPriv *priv = GET_PRIV (chat);
636 if (priv->channel == NULL)
639 if (!priv->had_properties_list || !properties) {
643 for (i = 0; i < properties->len; i++) {
644 GValueArray *prop_struct;
645 TpChatProperty *property;
649 prop_struct = g_ptr_array_index (properties, i);
650 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
651 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
653 for (j = 0; j < priv->properties->len; j++) {
654 property = g_ptr_array_index (priv->properties, j);
655 if (property->id == id) {
656 property->flags = flags;
657 DEBUG ("property %s flags changed: %d",
658 property->name, property->flags);
666 tp_chat_properties_changed_cb (TpProxy *proxy,
667 const GPtrArray *properties,
671 EmpathyTpChatPriv *priv = GET_PRIV (chat);
674 if (priv->channel == NULL)
677 if (!priv->had_properties_list || !properties) {
681 for (i = 0; i < properties->len; i++) {
682 GValueArray *prop_struct;
683 TpChatProperty *property;
687 prop_struct = g_ptr_array_index (properties, i);
688 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
689 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
691 for (j = 0; j < priv->properties->len; j++) {
692 property = g_ptr_array_index (priv->properties, j);
693 if (property->id == id) {
694 if (property->value) {
695 g_value_copy (src_value, property->value);
697 property->value = tp_g_value_slice_dup (src_value);
700 DEBUG ("property %s changed", property->name);
701 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
702 property->name, property->value);
710 tp_chat_get_properties_cb (TpProxy *proxy,
711 const GPtrArray *properties,
717 DEBUG ("Error getting properties: %s", error->message);
721 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
725 tp_chat_list_properties_cb (TpProxy *proxy,
726 const GPtrArray *properties,
731 EmpathyTpChatPriv *priv = GET_PRIV (chat);
735 if (priv->channel == NULL)
738 priv->had_properties_list = TRUE;
741 DEBUG ("Error listing properties: %s", error->message);
745 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
746 priv->properties = g_ptr_array_sized_new (properties->len);
747 for (i = 0; i < properties->len; i++) {
748 GValueArray *prop_struct;
749 TpChatProperty *property;
751 prop_struct = g_ptr_array_index (properties, i);
752 property = g_slice_new0 (TpChatProperty);
753 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
754 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
755 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
757 DEBUG ("Adding property name=%s id=%d flags=%d",
758 property->name, property->id, property->flags);
759 g_ptr_array_add (priv->properties, property);
760 if (property->flags & TP_PROPERTY_FLAG_READ) {
761 g_array_append_val (ids, property->id);
765 tp_cli_properties_interface_call_get_properties (proxy, -1,
767 tp_chat_get_properties_cb,
771 g_array_free (ids, TRUE);
775 empathy_tp_chat_set_property (EmpathyTpChat *chat,
779 EmpathyTpChatPriv *priv = GET_PRIV (chat);
780 TpChatProperty *property;
783 g_return_if_fail (priv->ready);
785 for (i = 0; i < priv->properties->len; i++) {
786 property = g_ptr_array_index (priv->properties, i);
787 if (!tp_strdiff (property->name, name)) {
788 GPtrArray *properties;
791 GValue dest_value = {0, };
793 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
797 g_value_init (&id, G_TYPE_UINT);
798 g_value_init (&dest_value, G_TYPE_VALUE);
799 g_value_set_uint (&id, property->id);
800 g_value_set_boxed (&dest_value, value);
802 prop = g_value_array_new (2);
803 g_value_array_append (prop, &id);
804 g_value_array_append (prop, &dest_value);
806 properties = g_ptr_array_sized_new (1);
807 g_ptr_array_add (properties, prop);
809 DEBUG ("Set property %s", name);
810 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
812 (tp_cli_properties_interface_callback_for_set_properties)
814 "Seting property", NULL,
817 g_ptr_array_free (properties, TRUE);
818 g_value_array_free (prop);
826 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
828 EmpathyTpChatPriv *priv = GET_PRIV (chat);
829 TpConnection *connection;
830 guint handle, handle_type;
832 if (priv->channel == NULL)
835 DEBUG ("Channel ready");
837 g_object_get (priv->channel,
838 "connection", &connection,
840 "handle_type", &handle_type,
843 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
847 handles = g_array_new (FALSE, FALSE, sizeof (guint));
848 g_array_append_val (handles, handle);
849 tp_cli_connection_run_inspect_handles (connection, -1,
850 handle_type, handles,
853 g_array_free (handles, TRUE);
857 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
858 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
861 g_object_notify (G_OBJECT (chat), "remote-contact");
864 if (tp_proxy_has_interface_by_id (priv->channel,
865 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
866 priv->group = empathy_tp_group_new (priv->channel);
868 g_signal_connect (priv->group, "member-added",
869 G_CALLBACK (tp_chat_member_added_cb),
871 g_signal_connect (priv->group, "member-removed",
872 G_CALLBACK (tp_chat_member_removed_cb),
874 g_signal_connect (priv->group, "local-pending",
875 G_CALLBACK (tp_chat_local_pending_cb),
877 empathy_run_until_ready (priv->group);
879 priv->members_count = 2;
882 if (tp_proxy_has_interface_by_id (priv->channel,
883 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
884 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
885 tp_chat_list_properties_cb,
888 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
889 tp_chat_properties_changed_cb,
891 G_OBJECT (chat), NULL);
892 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
893 tp_chat_property_flags_changed_cb,
895 G_OBJECT (chat), NULL);
898 priv->listing_pending_messages = TRUE;
899 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
901 tp_chat_list_pending_messages_cb,
905 tp_cli_channel_type_text_connect_to_received (priv->channel,
908 G_OBJECT (chat), NULL);
909 tp_cli_channel_type_text_connect_to_sent (priv->channel,
912 G_OBJECT (chat), NULL);
913 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
914 tp_chat_send_error_cb,
916 G_OBJECT (chat), NULL);
917 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
918 tp_chat_state_changed_cb,
920 G_OBJECT (chat), NULL);
921 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
922 tp_chat_state_changed_cb,
924 G_OBJECT (chat), NULL);
927 g_object_notify (G_OBJECT (chat), "ready");
931 tp_chat_dispose (GObject *object)
933 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
934 EmpathyTpChatPriv *priv = GET_PRIV (self);
936 if (priv->dispose_has_run)
939 priv->dispose_has_run = TRUE;
941 if (priv->channel != NULL)
943 g_signal_handlers_disconnect_by_func (priv->channel,
944 tp_chat_invalidated_cb, self);
945 g_object_unref (priv->channel);
946 priv->channel = NULL;
949 if (priv->remote_contact != NULL)
950 g_object_unref (priv->remote_contact);
952 priv->remote_contact = NULL;
954 if (priv->group != NULL)
955 g_object_unref (priv->group);
958 if (priv->factory != NULL)
959 g_object_unref (priv->factory);
960 priv->factory = NULL;
962 if (priv->user != NULL);
963 g_object_unref (priv->user);
966 if (priv->account != NULL);
967 g_object_unref (priv->account);
968 priv->account = NULL;
970 if (priv->contact_monitor)
971 g_object_unref (priv->contact_monitor);
972 priv->contact_monitor = NULL;
974 if (!g_queue_is_empty (priv->messages_queue)) {
975 EmpathyMessage *message;
976 EmpathyContact *contact;
978 message = g_queue_peek_head (priv->messages_queue);
979 contact = empathy_message_get_sender (message);
980 g_signal_handlers_disconnect_by_func (contact,
981 tp_chat_sender_ready_notify_cb, object);
984 g_list_foreach (priv->messages_queue->head,
985 (GFunc) g_object_unref, NULL);
987 g_list_foreach (priv->pending_messages_queue->head,
988 (GFunc) g_object_unref, NULL);
990 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
991 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
995 tp_chat_finalize (GObject *object)
997 EmpathyTpChatPriv *priv = GET_PRIV (object);
1000 DEBUG ("Finalize: %p", object);
1002 if (priv->properties) {
1003 for (i = 0; i < priv->properties->len; i++) {
1004 TpChatProperty *property;
1006 property = g_ptr_array_index (priv->properties, i);
1007 g_free (property->name);
1008 if (property->value) {
1009 tp_g_value_slice_free (property->value);
1011 g_slice_free (TpChatProperty, property);
1013 g_ptr_array_free (priv->properties, TRUE);
1018 g_queue_free (priv->messages_queue);
1019 g_queue_free (priv->pending_messages_queue);
1021 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
1025 tp_chat_constructor (GType type,
1027 GObjectConstructParam *props)
1030 EmpathyTpChatPriv *priv;
1031 gboolean channel_ready;
1033 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1035 priv = GET_PRIV (chat);
1036 priv->account = empathy_channel_get_account (priv->channel);
1037 priv->factory = empathy_contact_factory_dup_singleton ();
1038 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1040 g_signal_connect (priv->channel, "invalidated",
1041 G_CALLBACK (tp_chat_invalidated_cb),
1044 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1045 if (channel_ready) {
1046 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1048 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1049 G_CALLBACK (tp_chat_channel_ready_cb),
1057 tp_chat_get_property (GObject *object,
1062 EmpathyTpChatPriv *priv = GET_PRIV (object);
1066 g_value_set_object (value, priv->channel);
1068 case PROP_REMOTE_CONTACT:
1069 g_value_set_object (value, priv->remote_contact);
1072 g_value_set_boolean (value, priv->ready);
1075 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1081 tp_chat_set_property (GObject *object,
1083 const GValue *value,
1086 EmpathyTpChatPriv *priv = GET_PRIV (object);
1090 priv->channel = g_value_dup_object (value);
1093 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1099 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1103 object_class->dispose = tp_chat_dispose;
1104 object_class->finalize = tp_chat_finalize;
1105 object_class->constructor = tp_chat_constructor;
1106 object_class->get_property = tp_chat_get_property;
1107 object_class->set_property = tp_chat_set_property;
1109 g_object_class_install_property (object_class,
1111 g_param_spec_object ("channel",
1112 "telepathy channel",
1113 "The text channel for the chat",
1116 G_PARAM_CONSTRUCT_ONLY));
1118 g_object_class_install_property (object_class,
1119 PROP_REMOTE_CONTACT,
1120 g_param_spec_object ("remote-contact",
1121 "The remote contact",
1122 "The remote contact if there is no group iface on the channel",
1123 EMPATHY_TYPE_CONTACT,
1125 g_object_class_install_property (object_class,
1127 g_param_spec_boolean ("ready",
1128 "Is the object ready",
1129 "This object can't be used until this becomes true",
1134 signals[MESSAGE_RECEIVED] =
1135 g_signal_new ("message-received",
1136 G_TYPE_FROM_CLASS (klass),
1140 g_cclosure_marshal_VOID__OBJECT,
1142 1, EMPATHY_TYPE_MESSAGE);
1144 signals[SEND_ERROR] =
1145 g_signal_new ("send-error",
1146 G_TYPE_FROM_CLASS (klass),
1150 _empathy_marshal_VOID__OBJECT_UINT,
1152 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1154 signals[CHAT_STATE_CHANGED] =
1155 g_signal_new ("chat-state-changed",
1156 G_TYPE_FROM_CLASS (klass),
1160 _empathy_marshal_VOID__OBJECT_UINT,
1162 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1164 signals[PROPERTY_CHANGED] =
1165 g_signal_new ("property-changed",
1166 G_TYPE_FROM_CLASS (klass),
1170 _empathy_marshal_VOID__STRING_BOXED,
1172 2, G_TYPE_STRING, G_TYPE_VALUE);
1175 g_signal_new ("destroy",
1176 G_TYPE_FROM_CLASS (klass),
1180 g_cclosure_marshal_VOID__VOID,
1184 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1188 empathy_tp_chat_init (EmpathyTpChat *chat)
1190 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1191 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1194 priv->contact_monitor = NULL;
1195 priv->messages_queue = g_queue_new ();
1196 priv->pending_messages_queue = g_queue_new ();
1200 tp_chat_iface_init (EmpathyContactListIface *iface)
1202 iface->add = tp_chat_add;
1203 iface->remove = tp_chat_remove;
1204 iface->get_members = tp_chat_get_members;
1205 iface->get_monitor = tp_chat_get_monitor;
1209 empathy_tp_chat_new (TpChannel *channel)
1211 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1217 empathy_tp_chat_close (EmpathyTpChat *chat) {
1218 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1220 /* If there are still messages left, it'll come back..
1221 We loose the ordering of sent messages though */
1222 g_signal_handlers_disconnect_by_func (priv->channel,
1223 tp_chat_invalidated_cb, chat);
1225 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1226 "closing channel", NULL, NULL);
1228 g_object_unref (priv->channel);
1229 priv->channel = NULL;
1231 g_signal_emit (chat, signals[DESTROY], 0);
1235 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1237 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1239 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1240 g_return_val_if_fail (priv->ready, NULL);
1246 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1248 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1250 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1252 return priv->remote_contact;
1256 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1258 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1260 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1262 return priv->account;
1266 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1268 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1270 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1272 return priv->channel;
1276 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1278 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1280 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1286 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1288 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1290 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1292 return priv->members_count;
1296 empathy_tp_chat_send (EmpathyTpChat *chat,
1297 EmpathyMessage *message)
1299 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1300 const gchar *message_body;
1301 TpChannelTextMessageType message_type;
1303 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1304 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1305 g_return_if_fail (priv->ready);
1307 message_body = empathy_message_get_body (message);
1308 message_type = empathy_message_get_tptype (message);
1310 DEBUG ("Sending message: %s", message_body);
1311 tp_cli_channel_type_text_call_send (priv->channel, -1,
1315 g_object_ref (message),
1316 (GDestroyNotify) g_object_unref,
1321 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1322 TpChannelChatState state)
1324 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1326 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1327 g_return_if_fail (priv->ready);
1329 DEBUG ("Set state: %d", state);
1330 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1333 "setting chat state",
1340 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1342 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1344 return priv->pending_messages_queue->head;
1348 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1349 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1351 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1352 priv->channel, -1, ids, tp_chat_async_cb,
1353 "acknowledging received message", NULL, G_OBJECT (chat));
1357 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1358 EmpathyMessage *message) {
1359 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1360 GArray *message_ids;
1364 if (empathy_message_get_sender (message) == priv->user)
1367 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1369 id = empathy_message_get_id (message);
1370 g_array_append_val (message_ids, id);
1371 acknowledge_messages (chat, message_ids);
1372 g_array_free (message_ids, TRUE);
1375 m = g_queue_find (priv->pending_messages_queue, message);
1376 g_assert (m != NULL);
1377 g_queue_delete_link (priv->pending_messages_queue, m);
1378 g_object_unref (message);
1382 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1383 const GList *messages) {
1384 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1385 /* Copy messages as the messges list (probably is) our own */
1386 GList *msgs = g_list_copy ((GList *) messages);
1389 GArray *message_ids;
1391 length = g_list_length ((GList *)messages);
1396 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1398 for (l = msgs; l != NULL; l = g_list_next (l)) {
1401 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1403 m = g_queue_find (priv->pending_messages_queue, message);
1404 g_assert (m != NULL);
1405 g_queue_delete_link (priv->pending_messages_queue, m);
1407 if (empathy_message_get_sender (message) != priv->user) {
1408 guint id = empathy_message_get_id (message);
1409 g_array_append_val (message_ids, id);
1413 if (message_ids->len > 0)
1414 acknowledge_messages (chat, message_ids);
1416 g_array_free (message_ids, TRUE);