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;
52 gboolean listing_pending_messages;
53 /* Queue of messages not signalled yet */
54 GQueue *messages_queue;
55 /* Queue of messages signalled but not acked yet */
56 GQueue *pending_messages_queue;
57 gboolean had_properties_list;
58 GPtrArray *properties;
66 TpPropertyFlags flags;
70 static void tp_chat_iface_init (EmpathyContactListIface *iface);
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
94 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
97 tp_chat_invalidated_cb (TpProxy *proxy,
103 EmpathyTpChatPriv *priv = GET_PRIV (chat);
105 g_object_unref (priv->channel);
106 priv->channel = NULL;
108 DEBUG ("Channel invalidated: %s", message);
109 g_signal_emit (chat, signals[DESTROY], 0);
114 tp_chat_async_cb (TpChannel *proxy,
117 GObject *weak_object)
120 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
125 tp_chat_member_added_cb (EmpathyTpGroup *group,
126 EmpathyContact *contact,
127 EmpathyContact *actor,
129 const gchar *message,
132 EmpathyTpChatPriv *priv = GET_PRIV (chat);
133 guint handle_type = 0;
135 if (priv->channel == NULL)
138 priv->members_count++;
139 g_signal_emit_by_name (chat, "members-changed",
140 contact, actor, reason, message,
143 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
144 if (handle_type == TP_HANDLE_TYPE_ROOM) {
148 if (priv->members_count > 2 && priv->remote_contact) {
149 /* We now have more than 2 members, this is not a p2p chat
150 * anymore. Remove the remote-contact as it makes no sense, the
151 * EmpathyContactList interface must be used now. */
152 g_object_unref (priv->remote_contact);
153 priv->remote_contact = NULL;
154 g_object_notify (G_OBJECT (chat), "remote-contact");
156 if (priv->members_count <= 2 && !priv->remote_contact &&
157 !empathy_contact_is_user (contact)) {
158 /* This is a p2p chat, if it's not ourself that means this is
159 * the remote contact with who we are chatting. This is to
160 * avoid forcing the usage of the EmpathyContactList interface
162 priv->remote_contact = g_object_ref (contact);
163 g_object_notify (G_OBJECT (chat), "remote-contact");
168 tp_chat_member_removed_cb (EmpathyTpGroup *group,
169 EmpathyContact *contact,
170 EmpathyContact *actor,
172 const gchar *message,
175 EmpathyTpChatPriv *priv = GET_PRIV (chat);
176 guint handle_type = 0;
178 if (priv->channel == NULL)
181 priv->members_count--;
182 g_signal_emit_by_name (chat, "members-changed",
183 contact, actor, reason, message,
186 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
187 if (handle_type == TP_HANDLE_TYPE_ROOM) {
191 if (priv->members_count <= 2 && !priv->remote_contact) {
194 /* We are not a MUC anymore, get the remote contact back */
195 members = empathy_tp_group_get_members (group);
196 for (l = members; l; l = l->next) {
197 if (!empathy_contact_is_user (l->data)) {
198 priv->remote_contact = g_object_ref (l->data);
199 g_object_notify (G_OBJECT (chat), "remote-contact");
203 g_list_foreach (members, (GFunc) g_object_unref, NULL);
204 g_list_free (members);
209 tp_chat_local_pending_cb (EmpathyTpGroup *group,
210 EmpathyContact *contact,
211 EmpathyContact *actor,
213 const gchar *message,
216 EmpathyTpChatPriv *priv = GET_PRIV (chat);
218 if (priv->channel == NULL)
221 g_signal_emit_by_name (chat, "pendings-changed",
222 contact, actor, reason, message,
227 tp_chat_add (EmpathyContactList *list,
228 EmpathyContact *contact,
229 const gchar *message)
231 EmpathyTpChatPriv *priv = GET_PRIV (list);
233 GArray handles = {(gchar *) &handle, 1};
235 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
236 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
238 handle = empathy_contact_get_handle (contact);
239 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
246 tp_chat_remove (EmpathyContactList *list,
247 EmpathyContact *contact,
248 const gchar *message)
250 EmpathyTpChatPriv *priv = GET_PRIV (list);
252 GArray handles = {(gchar *) &handle, 1};
254 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
255 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
257 handle = empathy_contact_get_handle (contact);
258 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
265 tp_chat_get_members (EmpathyContactList *list)
267 EmpathyTpChatPriv *priv = GET_PRIV (list);
268 GList *members = NULL;
270 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
273 members = empathy_tp_group_get_members (priv->group);
275 members = g_list_prepend (members, g_object_ref (priv->user));
276 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
282 static EmpathyContactMonitor *
283 tp_chat_get_monitor (EmpathyContactList *list)
285 EmpathyTpChatPriv *priv;
287 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
289 priv = GET_PRIV (list);
291 if (priv->contact_monitor == NULL) {
292 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
295 return priv->contact_monitor;
298 static EmpathyMessage *
299 tp_chat_build_message (EmpathyTpChat *chat,
304 const gchar *message_body)
306 EmpathyTpChatPriv *priv;
307 EmpathyMessage *message;
308 EmpathyContact *sender;
310 priv = GET_PRIV (chat);
312 if (from_handle == 0) {
313 sender = g_object_ref (priv->user);
315 sender = empathy_contact_factory_get_from_handle (priv->factory,
320 message = empathy_message_new (message_body);
321 empathy_message_set_tptype (message, type);
322 empathy_message_set_sender (message, sender);
323 empathy_message_set_receiver (message, priv->user);
324 empathy_message_set_timestamp (message, timestamp);
325 empathy_message_set_id (message, id);
327 g_object_unref (sender);
333 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
334 GParamSpec *param_spec,
337 EmpathyTpChatPriv *priv = GET_PRIV (chat);
338 EmpathyMessage *message;
339 EmpathyContactReady ready;
340 EmpathyContact *sender = NULL;
341 gboolean removed = FALSE;
343 /* Emit all messages queued until we find a message with not
344 * ready sender (in case of a MUC we could have more than one sender).
345 * When leaving this loop, sender is the first not ready contact queued
346 * and removed tells if at least one message got removed
348 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
349 sender = empathy_message_get_sender (message);
350 ready = empathy_contact_get_ready (sender);
352 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
353 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
357 DEBUG ("Queued message ready");
358 message = g_queue_pop_head (priv->messages_queue);
359 g_queue_push_tail (priv->pending_messages_queue, message);
360 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
365 /* We removed at least one message from the queue, disconnect
366 * the ready signal from the previous contact */
367 g_signal_handlers_disconnect_by_func (contact,
368 tp_chat_sender_ready_notify_cb,
371 if (g_queue_get_length (priv->messages_queue) > 0) {
372 /* We still have queued message, connect the ready
373 * signal on the new first message sender. */
374 g_signal_connect (sender, "notify::ready",
375 G_CALLBACK (tp_chat_sender_ready_notify_cb),
382 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
383 EmpathyMessage *message)
385 EmpathyTpChatPriv *priv = GET_PRIV (chat);
386 EmpathyContact *sender;
387 EmpathyContactReady ready;
389 if (g_queue_get_length (priv->messages_queue) > 0) {
390 DEBUG ("Message queue not empty");
391 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
396 sender = empathy_message_get_sender (message);
397 ready = empathy_contact_get_ready (sender);
398 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
399 (ready & EMPATHY_CONTACT_READY_ID)) {
400 DEBUG ("Message queue empty and sender ready");
401 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
402 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
406 DEBUG ("Sender not ready");
407 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
408 g_signal_connect (sender, "notify::ready",
409 G_CALLBACK (tp_chat_sender_ready_notify_cb),
414 tp_chat_received_cb (TpChannel *channel,
420 const gchar *message_body,
424 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
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 (chat, ids);
446 g_array_free (ids, TRUE);
451 message = tp_chat_build_message (chat,
458 tp_chat_emit_or_queue_message (chat, message);
459 g_object_unref (message);
463 tp_chat_sent_cb (TpChannel *channel,
466 const gchar *message_body,
470 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
471 EmpathyTpChatPriv *priv = GET_PRIV (chat);
472 EmpathyMessage *message;
474 if (priv->channel == NULL)
477 DEBUG ("Message sent: %s", message_body);
479 message = tp_chat_build_message (chat,
486 tp_chat_emit_or_queue_message (chat, message);
487 g_object_unref (message);
491 tp_chat_send_error_cb (TpChannel *channel,
495 const gchar *message_body,
499 EmpathyMessage *message;
500 EmpathyTpChatPriv *priv = GET_PRIV (chat);
502 if (priv->channel == NULL)
505 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
507 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
514 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
515 g_object_unref (message);
519 tp_chat_send_cb (TpChannel *proxy,
524 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
527 DEBUG ("Error: %s", error->message);
528 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
529 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
534 tp_chat_state_changed_cb (TpChannel *channel,
540 EmpathyTpChatPriv *priv = GET_PRIV (chat);
541 EmpathyContact *contact;
543 if (priv->channel == NULL)
546 contact = empathy_contact_factory_get_from_handle (priv->factory,
550 DEBUG ("Chat state changed for %s (%d): %d",
551 empathy_contact_get_name (contact), handle, state);
553 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
554 g_object_unref (contact);
558 tp_chat_list_pending_messages_cb (TpChannel *channel,
559 const GPtrArray *messages_list,
564 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
565 EmpathyTpChatPriv *priv = GET_PRIV (chat);
567 GArray *empty_non_text_content_ids = NULL;
569 priv->listing_pending_messages = FALSE;
571 if (priv->channel == NULL)
575 DEBUG ("Error listing pending messages: %s", error->message);
579 for (i = 0; i < messages_list->len; i++) {
580 EmpathyMessage *message;
581 GValueArray *message_struct;
582 const gchar *message_body;
589 message_struct = g_ptr_array_index (messages_list, i);
591 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
592 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
593 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
594 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
595 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
596 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
598 DEBUG ("Message pending: %s", message_body);
600 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
601 !tp_strdiff (message_body, "")) {
602 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
604 if (empty_non_text_content_ids == NULL) {
605 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
608 g_array_append_val (empty_non_text_content_ids, message_id);
612 message = tp_chat_build_message (chat,
619 tp_chat_emit_or_queue_message (chat, message);
620 g_object_unref (message);
623 if (empty_non_text_content_ids != NULL) {
624 acknowledge_messages (chat, empty_non_text_content_ids);
625 g_array_free (empty_non_text_content_ids, TRUE);
630 tp_chat_property_flags_changed_cb (TpProxy *proxy,
631 const GPtrArray *properties,
635 EmpathyTpChatPriv *priv = GET_PRIV (chat);
638 if (priv->channel == NULL)
641 if (!priv->had_properties_list || !properties) {
645 for (i = 0; i < properties->len; i++) {
646 GValueArray *prop_struct;
647 TpChatProperty *property;
651 prop_struct = g_ptr_array_index (properties, i);
652 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
653 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
655 for (j = 0; j < priv->properties->len; j++) {
656 property = g_ptr_array_index (priv->properties, j);
657 if (property->id == id) {
658 property->flags = flags;
659 DEBUG ("property %s flags changed: %d",
660 property->name, property->flags);
668 tp_chat_properties_changed_cb (TpProxy *proxy,
669 const GPtrArray *properties,
673 EmpathyTpChatPriv *priv = GET_PRIV (chat);
676 if (priv->channel == NULL)
679 if (!priv->had_properties_list || !properties) {
683 for (i = 0; i < properties->len; i++) {
684 GValueArray *prop_struct;
685 TpChatProperty *property;
689 prop_struct = g_ptr_array_index (properties, i);
690 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
691 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
693 for (j = 0; j < priv->properties->len; j++) {
694 property = g_ptr_array_index (priv->properties, j);
695 if (property->id == id) {
696 if (property->value) {
697 g_value_copy (src_value, property->value);
699 property->value = tp_g_value_slice_dup (src_value);
702 DEBUG ("property %s changed", property->name);
703 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
704 property->name, property->value);
712 tp_chat_get_properties_cb (TpProxy *proxy,
713 const GPtrArray *properties,
719 DEBUG ("Error getting properties: %s", error->message);
723 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
727 tp_chat_list_properties_cb (TpProxy *proxy,
728 const GPtrArray *properties,
733 EmpathyTpChatPriv *priv = GET_PRIV (chat);
737 if (priv->channel == NULL)
740 priv->had_properties_list = TRUE;
743 DEBUG ("Error listing properties: %s", error->message);
747 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
748 priv->properties = g_ptr_array_sized_new (properties->len);
749 for (i = 0; i < properties->len; i++) {
750 GValueArray *prop_struct;
751 TpChatProperty *property;
753 prop_struct = g_ptr_array_index (properties, i);
754 property = g_slice_new0 (TpChatProperty);
755 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
756 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
757 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
759 DEBUG ("Adding property name=%s id=%d flags=%d",
760 property->name, property->id, property->flags);
761 g_ptr_array_add (priv->properties, property);
762 if (property->flags & TP_PROPERTY_FLAG_READ) {
763 g_array_append_val (ids, property->id);
767 tp_cli_properties_interface_call_get_properties (proxy, -1,
769 tp_chat_get_properties_cb,
773 g_array_free (ids, TRUE);
777 empathy_tp_chat_set_property (EmpathyTpChat *chat,
781 EmpathyTpChatPriv *priv = GET_PRIV (chat);
782 TpChatProperty *property;
785 g_return_if_fail (priv->ready);
787 for (i = 0; i < priv->properties->len; i++) {
788 property = g_ptr_array_index (priv->properties, i);
789 if (!tp_strdiff (property->name, name)) {
790 GPtrArray *properties;
793 GValue dest_value = {0, };
795 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
799 g_value_init (&id, G_TYPE_UINT);
800 g_value_init (&dest_value, G_TYPE_VALUE);
801 g_value_set_uint (&id, property->id);
802 g_value_set_boxed (&dest_value, value);
804 prop = g_value_array_new (2);
805 g_value_array_append (prop, &id);
806 g_value_array_append (prop, &dest_value);
808 properties = g_ptr_array_sized_new (1);
809 g_ptr_array_add (properties, prop);
811 DEBUG ("Set property %s", name);
812 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
814 (tp_cli_properties_interface_callback_for_set_properties)
816 "Seting property", NULL,
819 g_ptr_array_free (properties, TRUE);
820 g_value_array_free (prop);
828 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
830 EmpathyTpChatPriv *priv = GET_PRIV (chat);
831 TpConnection *connection;
832 guint handle, handle_type;
834 if (priv->channel == NULL)
837 DEBUG ("Channel ready");
839 g_object_get (priv->channel,
840 "connection", &connection,
842 "handle_type", &handle_type,
845 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
846 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
849 g_object_notify (G_OBJECT (chat), "remote-contact");
852 if (tp_proxy_has_interface_by_id (priv->channel,
853 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
854 priv->group = empathy_tp_group_new (priv->channel);
856 g_signal_connect (priv->group, "member-added",
857 G_CALLBACK (tp_chat_member_added_cb),
859 g_signal_connect (priv->group, "member-removed",
860 G_CALLBACK (tp_chat_member_removed_cb),
862 g_signal_connect (priv->group, "local-pending",
863 G_CALLBACK (tp_chat_local_pending_cb),
865 empathy_run_until_ready (priv->group);
867 priv->members_count = 2;
870 if (tp_proxy_has_interface_by_id (priv->channel,
871 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
872 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
873 tp_chat_list_properties_cb,
876 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
877 tp_chat_properties_changed_cb,
879 G_OBJECT (chat), NULL);
880 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
881 tp_chat_property_flags_changed_cb,
883 G_OBJECT (chat), NULL);
886 priv->listing_pending_messages = TRUE;
887 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
889 tp_chat_list_pending_messages_cb,
893 tp_cli_channel_type_text_connect_to_received (priv->channel,
896 G_OBJECT (chat), NULL);
897 tp_cli_channel_type_text_connect_to_sent (priv->channel,
900 G_OBJECT (chat), NULL);
901 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
902 tp_chat_send_error_cb,
904 G_OBJECT (chat), NULL);
905 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
906 tp_chat_state_changed_cb,
908 G_OBJECT (chat), NULL);
909 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
910 tp_chat_state_changed_cb,
912 G_OBJECT (chat), NULL);
915 g_object_notify (G_OBJECT (chat), "ready");
919 tp_chat_dispose (GObject *object)
921 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
922 EmpathyTpChatPriv *priv = GET_PRIV (self);
924 if (priv->dispose_has_run)
927 priv->dispose_has_run = TRUE;
929 if (priv->channel != NULL)
931 g_signal_handlers_disconnect_by_func (priv->channel,
932 tp_chat_invalidated_cb, self);
933 g_object_unref (priv->channel);
934 priv->channel = NULL;
937 if (priv->remote_contact != NULL)
938 g_object_unref (priv->remote_contact);
940 priv->remote_contact = NULL;
942 if (priv->group != NULL)
943 g_object_unref (priv->group);
946 if (priv->factory != NULL)
947 g_object_unref (priv->factory);
948 priv->factory = NULL;
950 if (priv->user != NULL);
951 g_object_unref (priv->user);
954 if (priv->account != NULL);
955 g_object_unref (priv->account);
956 priv->account = NULL;
958 if (priv->contact_monitor)
959 g_object_unref (priv->contact_monitor);
960 priv->contact_monitor = NULL;
962 if (!g_queue_is_empty (priv->messages_queue)) {
963 EmpathyMessage *message;
964 EmpathyContact *contact;
966 message = g_queue_peek_head (priv->messages_queue);
967 contact = empathy_message_get_sender (message);
968 g_signal_handlers_disconnect_by_func (contact,
969 tp_chat_sender_ready_notify_cb, object);
972 g_list_foreach (priv->messages_queue->head,
973 (GFunc) g_object_unref, NULL);
975 g_list_foreach (priv->pending_messages_queue->head,
976 (GFunc) g_object_unref, NULL);
978 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
979 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
983 tp_chat_finalize (GObject *object)
985 EmpathyTpChatPriv *priv = GET_PRIV (object);
988 DEBUG ("Finalize: %p", object);
990 if (priv->properties) {
991 for (i = 0; i < priv->properties->len; i++) {
992 TpChatProperty *property;
994 property = g_ptr_array_index (priv->properties, i);
995 g_free (property->name);
996 if (property->value) {
997 tp_g_value_slice_free (property->value);
999 g_slice_free (TpChatProperty, property);
1001 g_ptr_array_free (priv->properties, TRUE);
1004 g_queue_free (priv->messages_queue);
1005 g_queue_free (priv->pending_messages_queue);
1007 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
1011 tp_chat_constructor (GType type,
1013 GObjectConstructParam *props)
1016 EmpathyTpChatPriv *priv;
1017 gboolean channel_ready;
1019 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1021 priv = GET_PRIV (chat);
1022 priv->account = empathy_channel_get_account (priv->channel);
1023 priv->factory = empathy_contact_factory_dup_singleton ();
1024 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1026 g_signal_connect (priv->channel, "invalidated",
1027 G_CALLBACK (tp_chat_invalidated_cb),
1030 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1031 if (channel_ready) {
1032 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1034 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1035 G_CALLBACK (tp_chat_channel_ready_cb),
1043 tp_chat_get_property (GObject *object,
1048 EmpathyTpChatPriv *priv = GET_PRIV (object);
1052 g_value_set_object (value, priv->channel);
1054 case PROP_REMOTE_CONTACT:
1055 g_value_set_object (value, priv->remote_contact);
1058 g_value_set_boolean (value, priv->ready);
1061 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1067 tp_chat_set_property (GObject *object,
1069 const GValue *value,
1072 EmpathyTpChatPriv *priv = GET_PRIV (object);
1076 priv->channel = g_value_dup_object (value);
1079 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1085 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1087 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1089 object_class->dispose = tp_chat_dispose;
1090 object_class->finalize = tp_chat_finalize;
1091 object_class->constructor = tp_chat_constructor;
1092 object_class->get_property = tp_chat_get_property;
1093 object_class->set_property = tp_chat_set_property;
1095 g_object_class_install_property (object_class,
1097 g_param_spec_object ("channel",
1098 "telepathy channel",
1099 "The text channel for the chat",
1102 G_PARAM_CONSTRUCT_ONLY));
1104 g_object_class_install_property (object_class,
1105 PROP_REMOTE_CONTACT,
1106 g_param_spec_object ("remote-contact",
1107 "The remote contact",
1108 "The remote contact if there is no group iface on the channel",
1109 EMPATHY_TYPE_CONTACT,
1111 g_object_class_install_property (object_class,
1113 g_param_spec_boolean ("ready",
1114 "Is the object ready",
1115 "This object can't be used until this becomes true",
1120 signals[MESSAGE_RECEIVED] =
1121 g_signal_new ("message-received",
1122 G_TYPE_FROM_CLASS (klass),
1126 g_cclosure_marshal_VOID__OBJECT,
1128 1, EMPATHY_TYPE_MESSAGE);
1130 signals[SEND_ERROR] =
1131 g_signal_new ("send-error",
1132 G_TYPE_FROM_CLASS (klass),
1136 _empathy_marshal_VOID__OBJECT_UINT,
1138 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1140 signals[CHAT_STATE_CHANGED] =
1141 g_signal_new ("chat-state-changed",
1142 G_TYPE_FROM_CLASS (klass),
1146 _empathy_marshal_VOID__OBJECT_UINT,
1148 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1150 signals[PROPERTY_CHANGED] =
1151 g_signal_new ("property-changed",
1152 G_TYPE_FROM_CLASS (klass),
1156 _empathy_marshal_VOID__STRING_BOXED,
1158 2, G_TYPE_STRING, G_TYPE_VALUE);
1161 g_signal_new ("destroy",
1162 G_TYPE_FROM_CLASS (klass),
1166 g_cclosure_marshal_VOID__VOID,
1170 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1174 empathy_tp_chat_init (EmpathyTpChat *chat)
1176 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1177 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1180 priv->contact_monitor = NULL;
1181 priv->messages_queue = g_queue_new ();
1182 priv->pending_messages_queue = g_queue_new ();
1186 tp_chat_iface_init (EmpathyContactListIface *iface)
1188 iface->add = tp_chat_add;
1189 iface->remove = tp_chat_remove;
1190 iface->get_members = tp_chat_get_members;
1191 iface->get_monitor = tp_chat_get_monitor;
1195 empathy_tp_chat_new (TpChannel *channel)
1197 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1203 empathy_tp_chat_close (EmpathyTpChat *chat) {
1204 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1206 /* If there are still messages left, it'll come back..
1207 We loose the ordering of sent messages though */
1208 g_signal_handlers_disconnect_by_func (priv->channel,
1209 tp_chat_invalidated_cb, chat);
1211 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1212 "closing channel", NULL, NULL);
1214 g_object_unref (priv->channel);
1215 priv->channel = NULL;
1217 g_signal_emit (chat, signals[DESTROY], 0);
1221 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1223 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1225 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1226 g_return_val_if_fail (priv->ready, NULL);
1228 return tp_channel_get_identifier (priv->channel);
1232 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1234 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1238 return priv->remote_contact;
1242 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1244 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1246 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1248 return priv->account;
1252 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1254 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1256 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1258 return priv->channel;
1262 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1264 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1266 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1272 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1274 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1276 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1278 return priv->members_count;
1282 empathy_tp_chat_send (EmpathyTpChat *chat,
1283 EmpathyMessage *message)
1285 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1286 const gchar *message_body;
1287 TpChannelTextMessageType message_type;
1289 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1290 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1291 g_return_if_fail (priv->ready);
1293 message_body = empathy_message_get_body (message);
1294 message_type = empathy_message_get_tptype (message);
1296 DEBUG ("Sending message: %s", message_body);
1297 tp_cli_channel_type_text_call_send (priv->channel, -1,
1301 g_object_ref (message),
1302 (GDestroyNotify) g_object_unref,
1307 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1308 TpChannelChatState state)
1310 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1312 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1313 g_return_if_fail (priv->ready);
1315 DEBUG ("Set state: %d", state);
1316 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1319 "setting chat state",
1326 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1328 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1330 return priv->pending_messages_queue->head;
1334 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1335 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1337 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1338 priv->channel, -1, ids, tp_chat_async_cb,
1339 "acknowledging received message", NULL, G_OBJECT (chat));
1343 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1344 EmpathyMessage *message) {
1345 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1346 GArray *message_ids;
1350 if (empathy_message_get_sender (message) == priv->user)
1353 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1355 id = empathy_message_get_id (message);
1356 g_array_append_val (message_ids, id);
1357 acknowledge_messages (chat, message_ids);
1358 g_array_free (message_ids, TRUE);
1361 m = g_queue_find (priv->pending_messages_queue, message);
1362 g_assert (m != NULL);
1363 g_queue_delete_link (priv->pending_messages_queue, m);
1364 g_object_unref (message);
1368 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1369 const GList *messages) {
1370 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1371 /* Copy messages as the messges list (probably is) our own */
1372 GList *msgs = g_list_copy ((GList *) messages);
1375 GArray *message_ids;
1377 length = g_list_length ((GList *)messages);
1382 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1384 for (l = msgs; l != NULL; l = g_list_next (l)) {
1387 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1389 m = g_queue_find (priv->pending_messages_queue, message);
1390 g_assert (m != NULL);
1391 g_queue_delete_link (priv->pending_messages_queue, m);
1393 if (empathy_message_get_sender (message) != priv->user) {
1394 guint id = empathy_message_get_id (message);
1395 g_array_append_val (message_ids, id);
1399 if (message_ids->len > 0)
1400 acknowledge_messages (chat, message_ids);
1402 g_array_free (message_ids, TRUE);