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-contact-factory.h"
32 #include "empathy-contact-monitor.h"
33 #include "empathy-contact-list.h"
34 #include "empathy-marshal.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
43 gboolean dispose_has_run;
44 EmpathyContactFactory *factory;
45 EmpathyContactMonitor *contact_monitor;
47 EmpathyContact *remote_contact;
48 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,
95 tp_chat_invalidated_cb (TpProxy *proxy,
101 EmpathyTpChatPriv *priv = GET_PRIV (chat);
103 g_object_unref (priv->channel);
104 priv->channel = NULL;
106 DEBUG ("Channel invalidated: %s", message);
107 g_signal_emit (chat, signals[DESTROY], 0);
112 tp_chat_async_cb (TpChannel *proxy,
115 GObject *weak_object)
118 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
123 tp_chat_member_added_cb (EmpathyTpGroup *group,
124 EmpathyContact *contact,
125 EmpathyContact *actor,
127 const gchar *message,
130 EmpathyTpChatPriv *priv = GET_PRIV (chat);
131 guint handle_type = 0;
133 if (priv->channel == NULL)
136 priv->members_count++;
137 g_signal_emit_by_name (chat, "members-changed",
138 contact, actor, reason, message,
141 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
142 if (handle_type == TP_HANDLE_TYPE_ROOM) {
146 if (priv->members_count > 2 && priv->remote_contact) {
147 /* We now have more than 2 members, this is not a p2p chat
148 * anymore. Remove the remote-contact as it makes no sense, the
149 * EmpathyContactList interface must be used now. */
150 g_object_unref (priv->remote_contact);
151 priv->remote_contact = NULL;
152 g_object_notify (G_OBJECT (chat), "remote-contact");
154 if (priv->members_count <= 2 && !priv->remote_contact &&
155 !empathy_contact_is_user (contact)) {
156 /* This is a p2p chat, if it's not ourself that means this is
157 * the remote contact with who we are chatting. This is to
158 * avoid forcing the usage of the EmpathyContactList interface
160 priv->remote_contact = g_object_ref (contact);
161 g_object_notify (G_OBJECT (chat), "remote-contact");
166 tp_chat_member_removed_cb (EmpathyTpGroup *group,
167 EmpathyContact *contact,
168 EmpathyContact *actor,
170 const gchar *message,
173 EmpathyTpChatPriv *priv = GET_PRIV (chat);
174 guint handle_type = 0;
176 if (priv->channel == NULL)
179 priv->members_count--;
180 g_signal_emit_by_name (chat, "members-changed",
181 contact, actor, reason, message,
184 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
185 if (handle_type == TP_HANDLE_TYPE_ROOM) {
189 if (priv->members_count <= 2 && !priv->remote_contact) {
192 /* We are not a MUC anymore, get the remote contact back */
193 members = empathy_tp_group_get_members (group);
194 for (l = members; l; l = l->next) {
195 if (!empathy_contact_is_user (l->data)) {
196 priv->remote_contact = g_object_ref (l->data);
197 g_object_notify (G_OBJECT (chat), "remote-contact");
201 g_list_foreach (members, (GFunc) g_object_unref, NULL);
202 g_list_free (members);
207 tp_chat_local_pending_cb (EmpathyTpGroup *group,
208 EmpathyContact *contact,
209 EmpathyContact *actor,
211 const gchar *message,
214 EmpathyTpChatPriv *priv = GET_PRIV (chat);
216 if (priv->channel == NULL)
219 g_signal_emit_by_name (chat, "pendings-changed",
220 contact, actor, reason, message,
225 tp_chat_add (EmpathyContactList *list,
226 EmpathyContact *contact,
227 const gchar *message)
229 EmpathyTpChatPriv *priv = GET_PRIV (list);
231 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
232 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
235 empathy_tp_group_add_member (priv->group, contact, message);
240 tp_chat_remove (EmpathyContactList *list,
241 EmpathyContact *contact,
242 const gchar *message)
244 EmpathyTpChatPriv *priv = GET_PRIV (list);
246 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
247 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
250 empathy_tp_group_remove_member (priv->group, contact, message);
255 tp_chat_get_members (EmpathyContactList *list)
257 EmpathyTpChatPriv *priv = GET_PRIV (list);
258 GList *members = NULL;
260 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
263 members = empathy_tp_group_get_members (priv->group);
265 members = g_list_prepend (members, g_object_ref (priv->user));
266 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
272 static EmpathyContactMonitor *
273 tp_chat_get_monitor (EmpathyContactList *list)
275 EmpathyTpChatPriv *priv;
277 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
279 priv = GET_PRIV (list);
281 if (priv->contact_monitor == NULL) {
282 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
285 return priv->contact_monitor;
288 static EmpathyMessage *
289 tp_chat_build_message (EmpathyTpChat *chat,
294 const gchar *message_body)
296 EmpathyTpChatPriv *priv;
297 EmpathyMessage *message;
298 EmpathyContact *sender;
300 priv = GET_PRIV (chat);
302 if (from_handle == 0) {
303 sender = g_object_ref (priv->user);
305 sender = empathy_contact_factory_get_from_handle (priv->factory,
310 message = empathy_message_new (message_body);
311 empathy_message_set_tptype (message, type);
312 empathy_message_set_sender (message, sender);
313 empathy_message_set_receiver (message, priv->user);
314 empathy_message_set_timestamp (message, timestamp);
315 empathy_message_set_id (message, id);
317 g_object_unref (sender);
323 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
324 GParamSpec *param_spec,
327 EmpathyTpChatPriv *priv = GET_PRIV (chat);
328 EmpathyMessage *message;
329 EmpathyContactReady ready;
330 EmpathyContact *sender = NULL;
331 gboolean removed = FALSE;
333 /* Emit all messages queued until we find a message with not
334 * ready sender (in case of a MUC we could have more than one sender).
335 * When leaving this loop, sender is the first not ready contact queued
336 * and removed tells if at least one message got removed
338 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
339 sender = empathy_message_get_sender (message);
340 ready = empathy_contact_get_ready (sender);
342 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
343 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
347 DEBUG ("Queued message ready");
348 message = g_queue_pop_head (priv->messages_queue);
349 g_queue_push_tail (priv->pending_messages_queue, message);
350 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
355 /* We removed at least one message from the queue, disconnect
356 * the ready signal from the previous contact */
357 g_signal_handlers_disconnect_by_func (contact,
358 tp_chat_sender_ready_notify_cb,
361 if (g_queue_get_length (priv->messages_queue) > 0) {
362 /* We still have queued message, connect the ready
363 * signal on the new first message sender. */
364 g_signal_connect (sender, "notify::ready",
365 G_CALLBACK (tp_chat_sender_ready_notify_cb),
372 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
373 EmpathyMessage *message)
375 EmpathyTpChatPriv *priv = GET_PRIV (chat);
376 EmpathyContact *sender;
377 EmpathyContactReady ready;
379 if (g_queue_get_length (priv->messages_queue) > 0) {
380 DEBUG ("Message queue not empty");
381 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
386 sender = empathy_message_get_sender (message);
387 ready = empathy_contact_get_ready (sender);
388 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
389 (ready & EMPATHY_CONTACT_READY_ID)) {
390 DEBUG ("Message queue empty and sender ready");
391 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
392 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
396 DEBUG ("Sender not ready");
397 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
398 g_signal_connect (sender, "notify::ready",
399 G_CALLBACK (tp_chat_sender_ready_notify_cb),
404 tp_chat_received_cb (TpChannel *channel,
410 const gchar *message_body,
414 EmpathyTpChatPriv *priv = GET_PRIV (chat);
415 EmpathyMessage *message;
417 if (priv->channel == NULL)
420 if (priv->listing_pending_messages) {
424 DEBUG ("Message received: %s", message_body);
426 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
433 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
434 g_object_unref (message);
438 tp_chat_sent_cb (TpChannel *channel,
441 const gchar *message_body,
445 EmpathyTpChatPriv *priv = GET_PRIV (chat);
446 EmpathyMessage *message;
448 if (priv->channel == NULL)
451 DEBUG ("Message sent: %s", message_body);
453 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
460 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
461 g_object_unref (message);
465 tp_chat_send_error_cb (TpChannel *channel,
469 const gchar *message_body,
473 EmpathyMessage *message;
474 EmpathyTpChatPriv *priv = GET_PRIV (chat);
476 if (priv->channel == NULL)
479 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
481 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
488 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
489 g_object_unref (message);
493 tp_chat_send_cb (TpChannel *proxy,
498 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
501 DEBUG ("Error: %s", error->message);
502 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
503 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
508 tp_chat_state_changed_cb (TpChannel *channel,
514 EmpathyTpChatPriv *priv = GET_PRIV (chat);
515 EmpathyContact *contact;
517 if (priv->channel == NULL)
520 contact = empathy_contact_factory_get_from_handle (priv->factory,
524 DEBUG ("Chat state changed for %s (%d): %d",
525 empathy_contact_get_name (contact), handle, state);
527 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
528 g_object_unref (contact);
532 tp_chat_list_pending_messages_cb (TpChannel *channel,
533 const GPtrArray *messages_list,
538 EmpathyTpChatPriv *priv = GET_PRIV (chat);
541 priv->listing_pending_messages = FALSE;
543 if (priv->channel == NULL)
547 DEBUG ("Error listing pending messages: %s", error->message);
551 for (i = 0; i < messages_list->len; i++) {
552 EmpathyMessage *message;
553 GValueArray *message_struct;
554 const gchar *message_body;
561 message_struct = g_ptr_array_index (messages_list, i);
563 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
564 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
565 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
566 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
567 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
568 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
570 DEBUG ("Message pending: %s", message_body);
572 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
579 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
580 g_object_unref (message);
585 tp_chat_property_flags_changed_cb (TpProxy *proxy,
586 const GPtrArray *properties,
590 EmpathyTpChatPriv *priv = GET_PRIV (chat);
593 if (priv->channel == NULL)
596 if (!priv->had_properties_list || !properties) {
600 for (i = 0; i < properties->len; i++) {
601 GValueArray *prop_struct;
602 TpChatProperty *property;
606 prop_struct = g_ptr_array_index (properties, i);
607 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
608 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
610 for (j = 0; j < priv->properties->len; j++) {
611 property = g_ptr_array_index (priv->properties, j);
612 if (property->id == id) {
613 property->flags = flags;
614 DEBUG ("property %s flags changed: %d",
615 property->name, property->flags);
623 tp_chat_properties_changed_cb (TpProxy *proxy,
624 const GPtrArray *properties,
628 EmpathyTpChatPriv *priv = GET_PRIV (chat);
631 if (priv->channel == NULL)
634 if (!priv->had_properties_list || !properties) {
638 for (i = 0; i < properties->len; i++) {
639 GValueArray *prop_struct;
640 TpChatProperty *property;
644 prop_struct = g_ptr_array_index (properties, i);
645 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
646 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
648 for (j = 0; j < priv->properties->len; j++) {
649 property = g_ptr_array_index (priv->properties, j);
650 if (property->id == id) {
651 if (property->value) {
652 g_value_copy (src_value, property->value);
654 property->value = tp_g_value_slice_dup (src_value);
657 DEBUG ("property %s changed", property->name);
658 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
659 property->name, property->value);
667 tp_chat_get_properties_cb (TpProxy *proxy,
668 const GPtrArray *properties,
674 DEBUG ("Error getting properties: %s", error->message);
678 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
682 tp_chat_list_properties_cb (TpProxy *proxy,
683 const GPtrArray *properties,
688 EmpathyTpChatPriv *priv = GET_PRIV (chat);
692 if (priv->channel == NULL)
695 priv->had_properties_list = TRUE;
698 DEBUG ("Error listing properties: %s", error->message);
702 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
703 priv->properties = g_ptr_array_sized_new (properties->len);
704 for (i = 0; i < properties->len; i++) {
705 GValueArray *prop_struct;
706 TpChatProperty *property;
708 prop_struct = g_ptr_array_index (properties, i);
709 property = g_slice_new0 (TpChatProperty);
710 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
711 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
712 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
714 DEBUG ("Adding property name=%s id=%d flags=%d",
715 property->name, property->id, property->flags);
716 g_ptr_array_add (priv->properties, property);
717 if (property->flags & TP_PROPERTY_FLAG_READ) {
718 g_array_append_val (ids, property->id);
722 tp_cli_properties_interface_call_get_properties (proxy, -1,
724 tp_chat_get_properties_cb,
728 g_array_free (ids, TRUE);
732 empathy_tp_chat_set_property (EmpathyTpChat *chat,
736 EmpathyTpChatPriv *priv = GET_PRIV (chat);
737 TpChatProperty *property;
740 g_return_if_fail (priv->ready);
742 for (i = 0; i < priv->properties->len; i++) {
743 property = g_ptr_array_index (priv->properties, i);
744 if (!tp_strdiff (property->name, name)) {
745 GPtrArray *properties;
748 GValue dest_value = {0, };
750 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
754 g_value_init (&id, G_TYPE_UINT);
755 g_value_init (&dest_value, G_TYPE_VALUE);
756 g_value_set_uint (&id, property->id);
757 g_value_set_boxed (&dest_value, value);
759 prop = g_value_array_new (2);
760 g_value_array_append (prop, &id);
761 g_value_array_append (prop, &dest_value);
763 properties = g_ptr_array_sized_new (1);
764 g_ptr_array_add (properties, prop);
766 DEBUG ("Set property %s", name);
767 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
769 (tp_cli_properties_interface_callback_for_set_properties)
771 "Seting property", NULL,
774 g_ptr_array_free (properties, TRUE);
775 g_value_array_free (prop);
783 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
785 EmpathyTpChatPriv *priv = GET_PRIV (chat);
786 TpConnection *connection;
787 guint handle, handle_type;
789 if (priv->channel == NULL)
792 DEBUG ("Channel ready");
794 g_object_get (priv->channel,
795 "connection", &connection,
797 "handle_type", &handle_type,
800 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
804 handles = g_array_new (FALSE, FALSE, sizeof (guint));
805 g_array_append_val (handles, handle);
806 tp_cli_connection_run_inspect_handles (connection, -1,
807 handle_type, handles,
810 g_array_free (handles, TRUE);
814 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
815 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
818 g_object_notify (G_OBJECT (chat), "remote-contact");
821 if (tp_proxy_has_interface_by_id (priv->channel,
822 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
823 priv->group = empathy_tp_group_new (priv->channel);
825 g_signal_connect (priv->group, "member-added",
826 G_CALLBACK (tp_chat_member_added_cb),
828 g_signal_connect (priv->group, "member-removed",
829 G_CALLBACK (tp_chat_member_removed_cb),
831 g_signal_connect (priv->group, "local-pending",
832 G_CALLBACK (tp_chat_local_pending_cb),
834 empathy_run_until_ready (priv->group);
836 priv->members_count = 2;
839 if (tp_proxy_has_interface_by_id (priv->channel,
840 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
841 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
842 tp_chat_list_properties_cb,
845 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
846 tp_chat_properties_changed_cb,
848 G_OBJECT (chat), NULL);
849 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
850 tp_chat_property_flags_changed_cb,
852 G_OBJECT (chat), NULL);
855 priv->listing_pending_messages = TRUE;
856 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
858 tp_chat_list_pending_messages_cb,
862 tp_cli_channel_type_text_connect_to_received (priv->channel,
865 G_OBJECT (chat), NULL);
866 tp_cli_channel_type_text_connect_to_sent (priv->channel,
869 G_OBJECT (chat), NULL);
870 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
871 tp_chat_send_error_cb,
873 G_OBJECT (chat), NULL);
874 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
875 tp_chat_state_changed_cb,
877 G_OBJECT (chat), NULL);
878 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
879 tp_chat_state_changed_cb,
881 G_OBJECT (chat), NULL);
884 g_object_notify (G_OBJECT (chat), "ready");
888 tp_chat_dispose (GObject *object)
890 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
891 EmpathyTpChatPriv *priv = GET_PRIV (self);
893 if (priv->dispose_has_run)
896 priv->dispose_has_run = TRUE;
898 if (priv->channel != NULL)
899 empathy_tp_chat_close (self);
901 if (priv->remote_contact != NULL)
902 g_object_unref (priv->remote_contact);
904 priv->remote_contact = NULL;
906 if (priv->group != NULL)
907 g_object_unref (priv->group);
910 if (priv->factory != NULL)
911 g_object_unref (priv->factory);
912 priv->factory = NULL;
914 if (priv->user != NULL);
915 g_object_unref (priv->user);
918 if (priv->account != NULL);
919 g_object_unref (priv->account);
920 priv->account = NULL;
922 if (!g_queue_is_empty (priv->messages_queue)) {
923 EmpathyMessage *message;
924 EmpathyContact *contact;
926 message = g_queue_peek_head (priv->messages_queue);
927 contact = empathy_message_get_sender (message);
928 g_signal_handlers_disconnect_by_func (contact,
929 tp_chat_sender_ready_notify_cb, object);
932 g_list_foreach (priv->messages_queue->head,
933 (GFunc) g_object_unref, NULL);
935 g_list_foreach (priv->pending_messages_queue->head,
936 (GFunc) g_object_unref, NULL);
938 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
939 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
943 tp_chat_finalize (GObject *object)
945 EmpathyTpChatPriv *priv = GET_PRIV (object);
948 DEBUG ("Finalize: %p", object);
950 if (priv->properties) {
951 for (i = 0; i < priv->properties->len; i++) {
952 TpChatProperty *property;
954 property = g_ptr_array_index (priv->properties, i);
955 g_free (property->name);
956 if (property->value) {
957 tp_g_value_slice_free (property->value);
959 g_slice_free (TpChatProperty, property);
961 g_ptr_array_free (priv->properties, TRUE);
964 if (priv->remote_contact) {
965 g_object_unref (priv->remote_contact);
968 g_object_unref (priv->group);
971 if (priv->contact_monitor) {
972 g_object_unref (priv->contact_monitor);
975 g_object_unref (priv->factory);
976 g_object_unref (priv->user);
977 g_object_unref (priv->account);
979 g_queue_free (priv->messages_queue);
980 g_queue_free (priv->pending_messages_queue);
982 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
986 tp_chat_constructor (GType type,
988 GObjectConstructParam *props)
991 EmpathyTpChatPriv *priv;
992 gboolean channel_ready;
994 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
996 priv = GET_PRIV (chat);
997 priv->account = empathy_channel_get_account (priv->channel);
998 priv->factory = empathy_contact_factory_dup_singleton ();
999 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1001 g_signal_connect (priv->channel, "invalidated",
1002 G_CALLBACK (tp_chat_invalidated_cb),
1005 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1006 if (channel_ready) {
1007 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1009 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1010 G_CALLBACK (tp_chat_channel_ready_cb),
1018 tp_chat_get_property (GObject *object,
1023 EmpathyTpChatPriv *priv = GET_PRIV (object);
1027 g_value_set_object (value, priv->channel);
1029 case PROP_REMOTE_CONTACT:
1030 g_value_set_object (value, priv->remote_contact);
1033 g_value_set_boolean (value, priv->ready);
1036 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1042 tp_chat_set_property (GObject *object,
1044 const GValue *value,
1047 EmpathyTpChatPriv *priv = GET_PRIV (object);
1051 priv->channel = g_object_ref (g_value_get_object (value));
1054 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1060 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1062 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1064 object_class->dispose = tp_chat_dispose;
1065 object_class->finalize = tp_chat_finalize;
1066 object_class->constructor = tp_chat_constructor;
1067 object_class->get_property = tp_chat_get_property;
1068 object_class->set_property = tp_chat_set_property;
1070 g_object_class_install_property (object_class,
1072 g_param_spec_object ("channel",
1073 "telepathy channel",
1074 "The text channel for the chat",
1077 G_PARAM_CONSTRUCT_ONLY));
1079 g_object_class_install_property (object_class,
1080 PROP_REMOTE_CONTACT,
1081 g_param_spec_object ("remote-contact",
1082 "The remote contact",
1083 "The remote contact if there is no group iface on the channel",
1084 EMPATHY_TYPE_CONTACT,
1086 g_object_class_install_property (object_class,
1088 g_param_spec_boolean ("ready",
1089 "Is the object ready",
1090 "This object can't be used until this becomes true",
1095 signals[MESSAGE_RECEIVED] =
1096 g_signal_new ("message-received",
1097 G_TYPE_FROM_CLASS (klass),
1101 g_cclosure_marshal_VOID__OBJECT,
1103 1, EMPATHY_TYPE_MESSAGE);
1105 signals[SEND_ERROR] =
1106 g_signal_new ("send-error",
1107 G_TYPE_FROM_CLASS (klass),
1111 _empathy_marshal_VOID__OBJECT_UINT,
1113 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1115 signals[CHAT_STATE_CHANGED] =
1116 g_signal_new ("chat-state-changed",
1117 G_TYPE_FROM_CLASS (klass),
1121 _empathy_marshal_VOID__OBJECT_UINT,
1123 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1125 signals[PROPERTY_CHANGED] =
1126 g_signal_new ("property-changed",
1127 G_TYPE_FROM_CLASS (klass),
1131 _empathy_marshal_VOID__STRING_BOXED,
1133 2, G_TYPE_STRING, G_TYPE_VALUE);
1136 g_signal_new ("destroy",
1137 G_TYPE_FROM_CLASS (klass),
1141 g_cclosure_marshal_VOID__VOID,
1145 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1149 empathy_tp_chat_init (EmpathyTpChat *chat)
1151 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1152 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1155 priv->contact_monitor = NULL;
1156 priv->messages_queue = g_queue_new ();
1157 priv->pending_messages_queue = g_queue_new ();
1161 tp_chat_iface_init (EmpathyContactListIface *iface)
1163 iface->add = tp_chat_add;
1164 iface->remove = tp_chat_remove;
1165 iface->get_members = tp_chat_get_members;
1166 iface->get_monitor = tp_chat_get_monitor;
1170 empathy_tp_chat_new (TpChannel *channel)
1172 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1178 empathy_tp_chat_close (EmpathyTpChat *chat) {
1179 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1181 /* If there are still messages left, it'll come back..
1182 We loose the ordering of sent messages though */
1183 g_signal_handlers_disconnect_by_func (priv->channel,
1184 tp_chat_invalidated_cb, chat);
1185 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1186 "closing channel", NULL, NULL);
1188 g_object_unref (priv->channel);
1189 priv->channel = NULL;
1191 g_signal_emit (chat, signals[DESTROY], 0);
1195 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1197 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1199 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1200 g_return_val_if_fail (priv->ready, NULL);
1206 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1208 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1210 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1212 return priv->remote_contact;
1216 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1218 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1220 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1222 return priv->account;
1226 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1228 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1230 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1232 return priv->channel;
1236 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1238 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1240 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1246 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1248 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1250 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1252 return priv->members_count;
1256 empathy_tp_chat_send (EmpathyTpChat *chat,
1257 EmpathyMessage *message)
1259 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1260 const gchar *message_body;
1261 TpChannelTextMessageType message_type;
1263 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1264 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1265 g_return_if_fail (priv->ready);
1267 message_body = empathy_message_get_body (message);
1268 message_type = empathy_message_get_tptype (message);
1270 DEBUG ("Sending message: %s", message_body);
1271 tp_cli_channel_type_text_call_send (priv->channel, -1,
1275 g_object_ref (message),
1276 (GDestroyNotify) g_object_unref,
1281 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1282 TpChannelChatState state)
1284 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1286 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1287 g_return_if_fail (priv->ready);
1289 DEBUG ("Set state: %d", state);
1290 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1293 "setting chat state",
1300 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1302 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1304 return priv->pending_messages_queue->head;
1308 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1309 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1311 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1312 priv->channel, -1, ids, tp_chat_async_cb,
1313 "acknowledging received message", NULL, G_OBJECT (chat));
1317 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1318 EmpathyMessage *message) {
1319 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1320 GArray *message_ids;
1324 if (empathy_message_get_sender (message) == priv->user)
1327 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1329 id = empathy_message_get_id (message);
1330 g_array_append_val (message_ids, id);
1331 acknowledge_messages (chat, message_ids);
1332 g_array_free (message_ids, TRUE);
1335 m = g_queue_find (priv->pending_messages_queue, message);
1336 g_assert (m != NULL);
1337 g_queue_delete_link (priv->pending_messages_queue, m);
1338 g_object_unref (message);
1342 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1343 const GList *messages) {
1344 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1345 /* Copy messages as the messges list (probably is) our own */
1346 GList *msgs = g_list_copy ((GList *) messages);
1349 GArray *message_ids;
1351 length = g_list_length ((GList *)messages);
1356 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1358 for (l = msgs; l != NULL; l = g_list_next (l)) {
1361 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1363 m = g_queue_find (priv->pending_messages_queue, message);
1364 g_assert (m != NULL);
1365 g_queue_delete_link (priv->pending_messages_queue, m);
1367 if (empathy_message_get_sender (message) != priv->user) {
1368 guint id = empathy_message_get_id (message);
1369 g_array_append_val (message_ids, id);
1373 if (message_ids->len > 0)
1374 acknowledge_messages (chat, message_ids);
1376 g_array_free (message_ids, TRUE);