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)
900 g_signal_handlers_disconnect_by_func (priv->channel,
901 tp_chat_invalidated_cb, self);
902 g_object_unref (priv->channel);
903 priv->channel = NULL;
906 if (priv->remote_contact != NULL)
907 g_object_unref (priv->remote_contact);
909 priv->remote_contact = NULL;
911 if (priv->group != NULL)
912 g_object_unref (priv->group);
915 if (priv->factory != NULL)
916 g_object_unref (priv->factory);
917 priv->factory = NULL;
919 if (priv->user != NULL);
920 g_object_unref (priv->user);
923 if (priv->account != NULL);
924 g_object_unref (priv->account);
925 priv->account = NULL;
927 if (!g_queue_is_empty (priv->messages_queue)) {
928 EmpathyMessage *message;
929 EmpathyContact *contact;
931 message = g_queue_peek_head (priv->messages_queue);
932 contact = empathy_message_get_sender (message);
933 g_signal_handlers_disconnect_by_func (contact,
934 tp_chat_sender_ready_notify_cb, object);
937 g_list_foreach (priv->messages_queue->head,
938 (GFunc) g_object_unref, NULL);
940 g_list_foreach (priv->pending_messages_queue->head,
941 (GFunc) g_object_unref, NULL);
943 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
944 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
948 tp_chat_finalize (GObject *object)
950 EmpathyTpChatPriv *priv = GET_PRIV (object);
953 DEBUG ("Finalize: %p", object);
955 if (priv->properties) {
956 for (i = 0; i < priv->properties->len; i++) {
957 TpChatProperty *property;
959 property = g_ptr_array_index (priv->properties, i);
960 g_free (property->name);
961 if (property->value) {
962 tp_g_value_slice_free (property->value);
964 g_slice_free (TpChatProperty, property);
966 g_ptr_array_free (priv->properties, TRUE);
969 if (priv->remote_contact) {
970 g_object_unref (priv->remote_contact);
973 g_object_unref (priv->group);
976 if (priv->contact_monitor) {
977 g_object_unref (priv->contact_monitor);
980 g_object_unref (priv->factory);
981 g_object_unref (priv->user);
982 g_object_unref (priv->account);
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);