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,
96 tp_chat_invalidated_cb (TpProxy *proxy,
102 EmpathyTpChatPriv *priv = GET_PRIV (chat);
104 g_object_unref (priv->channel);
105 priv->channel = NULL;
107 DEBUG ("Channel invalidated: %s", message);
108 g_signal_emit (chat, signals[DESTROY], 0);
113 tp_chat_async_cb (TpChannel *proxy,
116 GObject *weak_object)
119 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
124 tp_chat_member_added_cb (EmpathyTpGroup *group,
125 EmpathyContact *contact,
126 EmpathyContact *actor,
128 const gchar *message,
131 EmpathyTpChatPriv *priv = GET_PRIV (chat);
132 guint handle_type = 0;
134 if (priv->channel == NULL)
137 priv->members_count++;
138 g_signal_emit_by_name (chat, "members-changed",
139 contact, actor, reason, message,
142 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
143 if (handle_type == TP_HANDLE_TYPE_ROOM) {
147 if (priv->members_count > 2 && priv->remote_contact) {
148 /* We now have more than 2 members, this is not a p2p chat
149 * anymore. Remove the remote-contact as it makes no sense, the
150 * EmpathyContactList interface must be used now. */
151 g_object_unref (priv->remote_contact);
152 priv->remote_contact = NULL;
153 g_object_notify (G_OBJECT (chat), "remote-contact");
155 if (priv->members_count <= 2 && !priv->remote_contact &&
156 !empathy_contact_is_user (contact)) {
157 /* This is a p2p chat, if it's not ourself that means this is
158 * the remote contact with who we are chatting. This is to
159 * avoid forcing the usage of the EmpathyContactList interface
161 priv->remote_contact = g_object_ref (contact);
162 g_object_notify (G_OBJECT (chat), "remote-contact");
167 tp_chat_member_removed_cb (EmpathyTpGroup *group,
168 EmpathyContact *contact,
169 EmpathyContact *actor,
171 const gchar *message,
174 EmpathyTpChatPriv *priv = GET_PRIV (chat);
175 guint handle_type = 0;
177 if (priv->channel == NULL)
180 priv->members_count--;
181 g_signal_emit_by_name (chat, "members-changed",
182 contact, actor, reason, message,
185 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
186 if (handle_type == TP_HANDLE_TYPE_ROOM) {
190 if (priv->members_count <= 2 && !priv->remote_contact) {
193 /* We are not a MUC anymore, get the remote contact back */
194 members = empathy_tp_group_get_members (group);
195 for (l = members; l; l = l->next) {
196 if (!empathy_contact_is_user (l->data)) {
197 priv->remote_contact = g_object_ref (l->data);
198 g_object_notify (G_OBJECT (chat), "remote-contact");
202 g_list_foreach (members, (GFunc) g_object_unref, NULL);
203 g_list_free (members);
208 tp_chat_local_pending_cb (EmpathyTpGroup *group,
209 EmpathyContact *contact,
210 EmpathyContact *actor,
212 const gchar *message,
215 EmpathyTpChatPriv *priv = GET_PRIV (chat);
217 if (priv->channel == NULL)
220 g_signal_emit_by_name (chat, "pendings-changed",
221 contact, actor, reason, message,
226 tp_chat_add (EmpathyContactList *list,
227 EmpathyContact *contact,
228 const gchar *message)
230 EmpathyTpChatPriv *priv = GET_PRIV (list);
232 GArray handles = {(gchar *) &handle, 1};
234 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
235 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
237 handle = empathy_contact_get_handle (contact);
238 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
245 tp_chat_remove (EmpathyContactList *list,
246 EmpathyContact *contact,
247 const gchar *message)
249 EmpathyTpChatPriv *priv = GET_PRIV (list);
251 GArray handles = {(gchar *) &handle, 1};
253 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
254 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
256 handle = empathy_contact_get_handle (contact);
257 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
264 tp_chat_get_members (EmpathyContactList *list)
266 EmpathyTpChatPriv *priv = GET_PRIV (list);
267 GList *members = NULL;
269 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
272 members = empathy_tp_group_get_members (priv->group);
274 members = g_list_prepend (members, g_object_ref (priv->user));
275 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
281 static EmpathyContactMonitor *
282 tp_chat_get_monitor (EmpathyContactList *list)
284 EmpathyTpChatPriv *priv;
286 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
288 priv = GET_PRIV (list);
290 if (priv->contact_monitor == NULL) {
291 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
294 return priv->contact_monitor;
297 static EmpathyMessage *
298 tp_chat_build_message (EmpathyTpChat *chat,
303 const gchar *message_body)
305 EmpathyTpChatPriv *priv;
306 EmpathyMessage *message;
307 EmpathyContact *sender;
309 priv = GET_PRIV (chat);
311 if (from_handle == 0) {
312 sender = g_object_ref (priv->user);
314 sender = empathy_contact_factory_get_from_handle (priv->factory,
319 message = empathy_message_new (message_body);
320 empathy_message_set_tptype (message, type);
321 empathy_message_set_sender (message, sender);
322 empathy_message_set_receiver (message, priv->user);
323 empathy_message_set_timestamp (message, timestamp);
324 empathy_message_set_id (message, id);
326 g_object_unref (sender);
332 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
333 GParamSpec *param_spec,
336 EmpathyTpChatPriv *priv = GET_PRIV (chat);
337 EmpathyMessage *message;
338 EmpathyContactReady ready;
339 EmpathyContact *sender = NULL;
340 gboolean removed = FALSE;
342 /* Emit all messages queued until we find a message with not
343 * ready sender (in case of a MUC we could have more than one sender).
344 * When leaving this loop, sender is the first not ready contact queued
345 * and removed tells if at least one message got removed
347 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
348 sender = empathy_message_get_sender (message);
349 ready = empathy_contact_get_ready (sender);
351 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
352 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
356 DEBUG ("Queued message ready");
357 message = g_queue_pop_head (priv->messages_queue);
358 g_queue_push_tail (priv->pending_messages_queue, message);
359 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
364 /* We removed at least one message from the queue, disconnect
365 * the ready signal from the previous contact */
366 g_signal_handlers_disconnect_by_func (contact,
367 tp_chat_sender_ready_notify_cb,
370 if (g_queue_get_length (priv->messages_queue) > 0) {
371 /* We still have queued message, connect the ready
372 * signal on the new first message sender. */
373 g_signal_connect (sender, "notify::ready",
374 G_CALLBACK (tp_chat_sender_ready_notify_cb),
381 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
382 EmpathyMessage *message)
384 EmpathyTpChatPriv *priv = GET_PRIV (chat);
385 EmpathyContact *sender;
386 EmpathyContactReady ready;
388 if (g_queue_get_length (priv->messages_queue) > 0) {
389 DEBUG ("Message queue not empty");
390 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
395 sender = empathy_message_get_sender (message);
396 ready = empathy_contact_get_ready (sender);
397 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
398 (ready & EMPATHY_CONTACT_READY_ID)) {
399 DEBUG ("Message queue empty and sender ready");
400 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
401 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
405 DEBUG ("Sender not ready");
406 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
407 g_signal_connect (sender, "notify::ready",
408 G_CALLBACK (tp_chat_sender_ready_notify_cb),
413 tp_chat_received_cb (TpChannel *channel,
419 const gchar *message_body,
423 EmpathyTpChatPriv *priv = GET_PRIV (chat);
424 EmpathyMessage *message;
426 if (priv->channel == NULL)
429 if (priv->listing_pending_messages) {
433 DEBUG ("Message received: %s", message_body);
435 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
442 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
443 g_object_unref (message);
447 tp_chat_sent_cb (TpChannel *channel,
450 const gchar *message_body,
454 EmpathyTpChatPriv *priv = GET_PRIV (chat);
455 EmpathyMessage *message;
457 if (priv->channel == NULL)
460 DEBUG ("Message sent: %s", message_body);
462 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
469 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
470 g_object_unref (message);
474 tp_chat_send_error_cb (TpChannel *channel,
478 const gchar *message_body,
482 EmpathyMessage *message;
483 EmpathyTpChatPriv *priv = GET_PRIV (chat);
485 if (priv->channel == NULL)
488 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
490 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
497 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
498 g_object_unref (message);
502 tp_chat_send_cb (TpChannel *proxy,
507 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
510 DEBUG ("Error: %s", error->message);
511 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
512 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
517 tp_chat_state_changed_cb (TpChannel *channel,
523 EmpathyTpChatPriv *priv = GET_PRIV (chat);
524 EmpathyContact *contact;
526 if (priv->channel == NULL)
529 contact = empathy_contact_factory_get_from_handle (priv->factory,
533 DEBUG ("Chat state changed for %s (%d): %d",
534 empathy_contact_get_name (contact), handle, state);
536 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
537 g_object_unref (contact);
541 tp_chat_list_pending_messages_cb (TpChannel *channel,
542 const GPtrArray *messages_list,
547 EmpathyTpChatPriv *priv = GET_PRIV (chat);
550 priv->listing_pending_messages = FALSE;
552 if (priv->channel == NULL)
556 DEBUG ("Error listing pending messages: %s", error->message);
560 for (i = 0; i < messages_list->len; i++) {
561 EmpathyMessage *message;
562 GValueArray *message_struct;
563 const gchar *message_body;
570 message_struct = g_ptr_array_index (messages_list, i);
572 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
573 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
574 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
575 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
576 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
577 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
579 DEBUG ("Message pending: %s", message_body);
581 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
588 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
589 g_object_unref (message);
594 tp_chat_property_flags_changed_cb (TpProxy *proxy,
595 const GPtrArray *properties,
599 EmpathyTpChatPriv *priv = GET_PRIV (chat);
602 if (priv->channel == NULL)
605 if (!priv->had_properties_list || !properties) {
609 for (i = 0; i < properties->len; i++) {
610 GValueArray *prop_struct;
611 TpChatProperty *property;
615 prop_struct = g_ptr_array_index (properties, i);
616 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
617 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
619 for (j = 0; j < priv->properties->len; j++) {
620 property = g_ptr_array_index (priv->properties, j);
621 if (property->id == id) {
622 property->flags = flags;
623 DEBUG ("property %s flags changed: %d",
624 property->name, property->flags);
632 tp_chat_properties_changed_cb (TpProxy *proxy,
633 const GPtrArray *properties,
637 EmpathyTpChatPriv *priv = GET_PRIV (chat);
640 if (priv->channel == NULL)
643 if (!priv->had_properties_list || !properties) {
647 for (i = 0; i < properties->len; i++) {
648 GValueArray *prop_struct;
649 TpChatProperty *property;
653 prop_struct = g_ptr_array_index (properties, i);
654 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
655 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
657 for (j = 0; j < priv->properties->len; j++) {
658 property = g_ptr_array_index (priv->properties, j);
659 if (property->id == id) {
660 if (property->value) {
661 g_value_copy (src_value, property->value);
663 property->value = tp_g_value_slice_dup (src_value);
666 DEBUG ("property %s changed", property->name);
667 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
668 property->name, property->value);
676 tp_chat_get_properties_cb (TpProxy *proxy,
677 const GPtrArray *properties,
683 DEBUG ("Error getting properties: %s", error->message);
687 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
691 tp_chat_list_properties_cb (TpProxy *proxy,
692 const GPtrArray *properties,
697 EmpathyTpChatPriv *priv = GET_PRIV (chat);
701 if (priv->channel == NULL)
704 priv->had_properties_list = TRUE;
707 DEBUG ("Error listing properties: %s", error->message);
711 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
712 priv->properties = g_ptr_array_sized_new (properties->len);
713 for (i = 0; i < properties->len; i++) {
714 GValueArray *prop_struct;
715 TpChatProperty *property;
717 prop_struct = g_ptr_array_index (properties, i);
718 property = g_slice_new0 (TpChatProperty);
719 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
720 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
721 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
723 DEBUG ("Adding property name=%s id=%d flags=%d",
724 property->name, property->id, property->flags);
725 g_ptr_array_add (priv->properties, property);
726 if (property->flags & TP_PROPERTY_FLAG_READ) {
727 g_array_append_val (ids, property->id);
731 tp_cli_properties_interface_call_get_properties (proxy, -1,
733 tp_chat_get_properties_cb,
737 g_array_free (ids, TRUE);
741 empathy_tp_chat_set_property (EmpathyTpChat *chat,
745 EmpathyTpChatPriv *priv = GET_PRIV (chat);
746 TpChatProperty *property;
749 g_return_if_fail (priv->ready);
751 for (i = 0; i < priv->properties->len; i++) {
752 property = g_ptr_array_index (priv->properties, i);
753 if (!tp_strdiff (property->name, name)) {
754 GPtrArray *properties;
757 GValue dest_value = {0, };
759 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
763 g_value_init (&id, G_TYPE_UINT);
764 g_value_init (&dest_value, G_TYPE_VALUE);
765 g_value_set_uint (&id, property->id);
766 g_value_set_boxed (&dest_value, value);
768 prop = g_value_array_new (2);
769 g_value_array_append (prop, &id);
770 g_value_array_append (prop, &dest_value);
772 properties = g_ptr_array_sized_new (1);
773 g_ptr_array_add (properties, prop);
775 DEBUG ("Set property %s", name);
776 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
778 (tp_cli_properties_interface_callback_for_set_properties)
780 "Seting property", NULL,
783 g_ptr_array_free (properties, TRUE);
784 g_value_array_free (prop);
792 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
794 EmpathyTpChatPriv *priv = GET_PRIV (chat);
795 TpConnection *connection;
796 guint handle, handle_type;
798 if (priv->channel == NULL)
801 DEBUG ("Channel ready");
803 g_object_get (priv->channel,
804 "connection", &connection,
806 "handle_type", &handle_type,
809 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
813 handles = g_array_new (FALSE, FALSE, sizeof (guint));
814 g_array_append_val (handles, handle);
815 tp_cli_connection_run_inspect_handles (connection, -1,
816 handle_type, handles,
819 g_array_free (handles, TRUE);
823 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
824 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
827 g_object_notify (G_OBJECT (chat), "remote-contact");
830 if (tp_proxy_has_interface_by_id (priv->channel,
831 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
832 priv->group = empathy_tp_group_new (priv->channel);
834 g_signal_connect (priv->group, "member-added",
835 G_CALLBACK (tp_chat_member_added_cb),
837 g_signal_connect (priv->group, "member-removed",
838 G_CALLBACK (tp_chat_member_removed_cb),
840 g_signal_connect (priv->group, "local-pending",
841 G_CALLBACK (tp_chat_local_pending_cb),
843 empathy_run_until_ready (priv->group);
845 priv->members_count = 2;
848 if (tp_proxy_has_interface_by_id (priv->channel,
849 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
850 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
851 tp_chat_list_properties_cb,
854 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
855 tp_chat_properties_changed_cb,
857 G_OBJECT (chat), NULL);
858 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
859 tp_chat_property_flags_changed_cb,
861 G_OBJECT (chat), NULL);
864 priv->listing_pending_messages = TRUE;
865 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
867 tp_chat_list_pending_messages_cb,
871 tp_cli_channel_type_text_connect_to_received (priv->channel,
874 G_OBJECT (chat), NULL);
875 tp_cli_channel_type_text_connect_to_sent (priv->channel,
878 G_OBJECT (chat), NULL);
879 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
880 tp_chat_send_error_cb,
882 G_OBJECT (chat), NULL);
883 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
884 tp_chat_state_changed_cb,
886 G_OBJECT (chat), NULL);
887 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
888 tp_chat_state_changed_cb,
890 G_OBJECT (chat), NULL);
893 g_object_notify (G_OBJECT (chat), "ready");
897 tp_chat_dispose (GObject *object)
899 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
900 EmpathyTpChatPriv *priv = GET_PRIV (self);
902 if (priv->dispose_has_run)
905 priv->dispose_has_run = TRUE;
907 if (priv->channel != NULL)
909 g_signal_handlers_disconnect_by_func (priv->channel,
910 tp_chat_invalidated_cb, self);
911 g_object_unref (priv->channel);
912 priv->channel = NULL;
915 if (priv->remote_contact != NULL)
916 g_object_unref (priv->remote_contact);
918 priv->remote_contact = NULL;
920 if (priv->group != NULL)
921 g_object_unref (priv->group);
924 if (priv->factory != NULL)
925 g_object_unref (priv->factory);
926 priv->factory = NULL;
928 if (priv->user != NULL);
929 g_object_unref (priv->user);
932 if (priv->account != NULL);
933 g_object_unref (priv->account);
934 priv->account = NULL;
936 if (priv->contact_monitor)
937 g_object_unref (priv->contact_monitor);
938 priv->contact_monitor = NULL;
940 if (!g_queue_is_empty (priv->messages_queue)) {
941 EmpathyMessage *message;
942 EmpathyContact *contact;
944 message = g_queue_peek_head (priv->messages_queue);
945 contact = empathy_message_get_sender (message);
946 g_signal_handlers_disconnect_by_func (contact,
947 tp_chat_sender_ready_notify_cb, object);
950 g_list_foreach (priv->messages_queue->head,
951 (GFunc) g_object_unref, NULL);
953 g_list_foreach (priv->pending_messages_queue->head,
954 (GFunc) g_object_unref, NULL);
956 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
957 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
961 tp_chat_finalize (GObject *object)
963 EmpathyTpChatPriv *priv = GET_PRIV (object);
966 DEBUG ("Finalize: %p", object);
968 if (priv->properties) {
969 for (i = 0; i < priv->properties->len; i++) {
970 TpChatProperty *property;
972 property = g_ptr_array_index (priv->properties, i);
973 g_free (property->name);
974 if (property->value) {
975 tp_g_value_slice_free (property->value);
977 g_slice_free (TpChatProperty, property);
979 g_ptr_array_free (priv->properties, TRUE);
984 g_queue_free (priv->messages_queue);
985 g_queue_free (priv->pending_messages_queue);
987 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
991 tp_chat_constructor (GType type,
993 GObjectConstructParam *props)
996 EmpathyTpChatPriv *priv;
997 gboolean channel_ready;
999 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1001 priv = GET_PRIV (chat);
1002 priv->account = empathy_channel_get_account (priv->channel);
1003 priv->factory = empathy_contact_factory_dup_singleton ();
1004 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1006 g_signal_connect (priv->channel, "invalidated",
1007 G_CALLBACK (tp_chat_invalidated_cb),
1010 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1011 if (channel_ready) {
1012 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1014 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1015 G_CALLBACK (tp_chat_channel_ready_cb),
1023 tp_chat_get_property (GObject *object,
1028 EmpathyTpChatPriv *priv = GET_PRIV (object);
1032 g_value_set_object (value, priv->channel);
1034 case PROP_REMOTE_CONTACT:
1035 g_value_set_object (value, priv->remote_contact);
1038 g_value_set_boolean (value, priv->ready);
1041 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1047 tp_chat_set_property (GObject *object,
1049 const GValue *value,
1052 EmpathyTpChatPriv *priv = GET_PRIV (object);
1056 priv->channel = g_value_dup_object (value);
1059 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1065 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1067 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1069 object_class->dispose = tp_chat_dispose;
1070 object_class->finalize = tp_chat_finalize;
1071 object_class->constructor = tp_chat_constructor;
1072 object_class->get_property = tp_chat_get_property;
1073 object_class->set_property = tp_chat_set_property;
1075 g_object_class_install_property (object_class,
1077 g_param_spec_object ("channel",
1078 "telepathy channel",
1079 "The text channel for the chat",
1082 G_PARAM_CONSTRUCT_ONLY));
1084 g_object_class_install_property (object_class,
1085 PROP_REMOTE_CONTACT,
1086 g_param_spec_object ("remote-contact",
1087 "The remote contact",
1088 "The remote contact if there is no group iface on the channel",
1089 EMPATHY_TYPE_CONTACT,
1091 g_object_class_install_property (object_class,
1093 g_param_spec_boolean ("ready",
1094 "Is the object ready",
1095 "This object can't be used until this becomes true",
1100 signals[MESSAGE_RECEIVED] =
1101 g_signal_new ("message-received",
1102 G_TYPE_FROM_CLASS (klass),
1106 g_cclosure_marshal_VOID__OBJECT,
1108 1, EMPATHY_TYPE_MESSAGE);
1110 signals[SEND_ERROR] =
1111 g_signal_new ("send-error",
1112 G_TYPE_FROM_CLASS (klass),
1116 _empathy_marshal_VOID__OBJECT_UINT,
1118 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1120 signals[CHAT_STATE_CHANGED] =
1121 g_signal_new ("chat-state-changed",
1122 G_TYPE_FROM_CLASS (klass),
1126 _empathy_marshal_VOID__OBJECT_UINT,
1128 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1130 signals[PROPERTY_CHANGED] =
1131 g_signal_new ("property-changed",
1132 G_TYPE_FROM_CLASS (klass),
1136 _empathy_marshal_VOID__STRING_BOXED,
1138 2, G_TYPE_STRING, G_TYPE_VALUE);
1141 g_signal_new ("destroy",
1142 G_TYPE_FROM_CLASS (klass),
1146 g_cclosure_marshal_VOID__VOID,
1150 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1154 empathy_tp_chat_init (EmpathyTpChat *chat)
1156 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1157 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1160 priv->contact_monitor = NULL;
1161 priv->messages_queue = g_queue_new ();
1162 priv->pending_messages_queue = g_queue_new ();
1166 tp_chat_iface_init (EmpathyContactListIface *iface)
1168 iface->add = tp_chat_add;
1169 iface->remove = tp_chat_remove;
1170 iface->get_members = tp_chat_get_members;
1171 iface->get_monitor = tp_chat_get_monitor;
1175 empathy_tp_chat_new (TpChannel *channel)
1177 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1183 empathy_tp_chat_close (EmpathyTpChat *chat) {
1184 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1186 /* If there are still messages left, it'll come back..
1187 We loose the ordering of sent messages though */
1188 g_signal_handlers_disconnect_by_func (priv->channel,
1189 tp_chat_invalidated_cb, chat);
1191 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1192 "closing channel", NULL, NULL);
1194 g_object_unref (priv->channel);
1195 priv->channel = NULL;
1197 g_signal_emit (chat, signals[DESTROY], 0);
1201 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1203 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1205 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1206 g_return_val_if_fail (priv->ready, NULL);
1212 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1214 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1216 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1218 return priv->remote_contact;
1222 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1224 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1226 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1228 return priv->account;
1232 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1234 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1238 return priv->channel;
1242 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1244 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1246 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1252 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1254 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1256 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1258 return priv->members_count;
1262 empathy_tp_chat_send (EmpathyTpChat *chat,
1263 EmpathyMessage *message)
1265 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1266 const gchar *message_body;
1267 TpChannelTextMessageType message_type;
1269 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1270 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1271 g_return_if_fail (priv->ready);
1273 message_body = empathy_message_get_body (message);
1274 message_type = empathy_message_get_tptype (message);
1276 DEBUG ("Sending message: %s", message_body);
1277 tp_cli_channel_type_text_call_send (priv->channel, -1,
1281 g_object_ref (message),
1282 (GDestroyNotify) g_object_unref,
1287 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1288 TpChannelChatState state)
1290 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1292 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1293 g_return_if_fail (priv->ready);
1295 DEBUG ("Set state: %d", state);
1296 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1299 "setting chat state",
1306 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1308 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1310 return priv->pending_messages_queue->head;
1314 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1315 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1317 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1318 priv->channel, -1, ids, tp_chat_async_cb,
1319 "acknowledging received message", NULL, G_OBJECT (chat));
1323 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1324 EmpathyMessage *message) {
1325 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1326 GArray *message_ids;
1330 if (empathy_message_get_sender (message) == priv->user)
1333 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1335 id = empathy_message_get_id (message);
1336 g_array_append_val (message_ids, id);
1337 acknowledge_messages (chat, message_ids);
1338 g_array_free (message_ids, TRUE);
1341 m = g_queue_find (priv->pending_messages_queue, message);
1342 g_assert (m != NULL);
1343 g_queue_delete_link (priv->pending_messages_queue, m);
1344 g_object_unref (message);
1348 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1349 const GList *messages) {
1350 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1351 /* Copy messages as the messges list (probably is) our own */
1352 GList *msgs = g_list_copy ((GList *) messages);
1355 GArray *message_ids;
1357 length = g_list_length ((GList *)messages);
1362 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1364 for (l = msgs; l != NULL; l = g_list_next (l)) {
1367 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1369 m = g_queue_find (priv->pending_messages_queue, message);
1370 g_assert (m != NULL);
1371 g_queue_delete_link (priv->pending_messages_queue, m);
1373 if (empathy_message_get_sender (message) != priv->user) {
1374 guint id = empathy_message_get_id (message);
1375 g_array_append_val (message_ids, id);
1379 if (message_ids->len > 0)
1380 acknowledge_messages (chat, message_ids);
1382 g_array_free (message_ids, TRUE);