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;
61 guint invalidated_handler;
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);
103 DEBUG ("Channel invalidated: %s", message);
104 g_signal_emit (chat, signals[DESTROY], 0);
106 g_object_unref (priv->channel);
107 priv->channel = NULL;
111 tp_chat_async_cb (TpChannel *proxy,
114 GObject *weak_object)
117 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
122 tp_chat_member_added_cb (EmpathyTpGroup *group,
123 EmpathyContact *contact,
124 EmpathyContact *actor,
126 const gchar *message,
129 EmpathyTpChatPriv *priv = GET_PRIV (chat);
130 guint handle_type = 0;
132 if (priv->channel == NULL)
135 priv->members_count++;
136 g_signal_emit_by_name (chat, "members-changed",
137 contact, actor, reason, message,
140 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
141 if (handle_type == TP_HANDLE_TYPE_ROOM) {
145 if (priv->members_count > 2 && priv->remote_contact) {
146 /* We now have more than 2 members, this is not a p2p chat
147 * anymore. Remove the remote-contact as it makes no sense, the
148 * EmpathyContactList interface must be used now. */
149 g_object_unref (priv->remote_contact);
150 priv->remote_contact = NULL;
151 g_object_notify (G_OBJECT (chat), "remote-contact");
153 if (priv->members_count <= 2 && !priv->remote_contact &&
154 !empathy_contact_is_user (contact)) {
155 /* This is a p2p chat, if it's not ourself that means this is
156 * the remote contact with who we are chatting. This is to
157 * avoid forcing the usage of the EmpathyContactList interface
159 priv->remote_contact = g_object_ref (contact);
160 g_object_notify (G_OBJECT (chat), "remote-contact");
165 tp_chat_member_removed_cb (EmpathyTpGroup *group,
166 EmpathyContact *contact,
167 EmpathyContact *actor,
169 const gchar *message,
172 EmpathyTpChatPriv *priv = GET_PRIV (chat);
173 guint handle_type = 0;
175 if (priv->channel == NULL)
178 priv->members_count--;
179 g_signal_emit_by_name (chat, "members-changed",
180 contact, actor, reason, message,
183 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
184 if (handle_type == TP_HANDLE_TYPE_ROOM) {
188 if (priv->members_count <= 2 && !priv->remote_contact) {
191 /* We are not a MUC anymore, get the remote contact back */
192 members = empathy_tp_group_get_members (group);
193 for (l = members; l; l = l->next) {
194 if (!empathy_contact_is_user (l->data)) {
195 priv->remote_contact = g_object_ref (l->data);
196 g_object_notify (G_OBJECT (chat), "remote-contact");
200 g_list_foreach (members, (GFunc) g_object_unref, NULL);
201 g_list_free (members);
206 tp_chat_local_pending_cb (EmpathyTpGroup *group,
207 EmpathyContact *contact,
208 EmpathyContact *actor,
210 const gchar *message,
213 EmpathyTpChatPriv *priv = GET_PRIV (chat);
215 if (priv->channel == NULL)
218 g_signal_emit_by_name (chat, "pendings-changed",
219 contact, actor, reason, message,
224 tp_chat_add (EmpathyContactList *list,
225 EmpathyContact *contact,
226 const gchar *message)
228 EmpathyTpChatPriv *priv = GET_PRIV (list);
230 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
231 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
234 empathy_tp_group_add_member (priv->group, contact, message);
239 tp_chat_remove (EmpathyContactList *list,
240 EmpathyContact *contact,
241 const gchar *message)
243 EmpathyTpChatPriv *priv = GET_PRIV (list);
245 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
246 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
249 empathy_tp_group_remove_member (priv->group, contact, message);
254 tp_chat_get_members (EmpathyContactList *list)
256 EmpathyTpChatPriv *priv = GET_PRIV (list);
257 GList *members = NULL;
259 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
262 members = empathy_tp_group_get_members (priv->group);
264 members = g_list_prepend (members, g_object_ref (priv->user));
265 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
271 static EmpathyContactMonitor *
272 tp_chat_get_monitor (EmpathyContactList *list)
274 EmpathyTpChatPriv *priv;
276 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
278 priv = GET_PRIV (list);
280 if (priv->contact_monitor == NULL) {
281 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
284 return priv->contact_monitor;
287 static EmpathyMessage *
288 tp_chat_build_message (EmpathyTpChat *chat,
293 const gchar *message_body)
295 EmpathyTpChatPriv *priv;
296 EmpathyMessage *message;
297 EmpathyContact *sender;
299 priv = GET_PRIV (chat);
301 if (from_handle == 0) {
302 sender = g_object_ref (priv->user);
304 sender = empathy_contact_factory_get_from_handle (priv->factory,
309 message = empathy_message_new (message_body);
310 empathy_message_set_tptype (message, type);
311 empathy_message_set_sender (message, sender);
312 empathy_message_set_receiver (message, priv->user);
313 empathy_message_set_timestamp (message, timestamp);
314 empathy_message_set_id (message, id);
316 g_object_unref (sender);
322 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
323 GParamSpec *param_spec,
326 EmpathyTpChatPriv *priv = GET_PRIV (chat);
327 EmpathyMessage *message;
328 EmpathyContactReady ready;
329 EmpathyContact *sender = NULL;
330 gboolean removed = FALSE;
332 /* Emit all messages queued until we find a message with not
333 * ready sender (in case of a MUC we could have more than one sender).
334 * When leaving this loop, sender is the first not ready contact queued
335 * and removed tells if at least one message got removed
337 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
338 sender = empathy_message_get_sender (message);
339 ready = empathy_contact_get_ready (sender);
341 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
342 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
346 DEBUG ("Queued message ready");
347 message = g_queue_pop_head (priv->messages_queue);
348 g_queue_push_tail (priv->pending_messages_queue, message);
349 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
354 /* We removed at least one message from the queue, disconnect
355 * the ready signal from the previous contact */
356 g_signal_handlers_disconnect_by_func (contact,
357 tp_chat_sender_ready_notify_cb,
360 if (g_queue_get_length (priv->messages_queue) > 0) {
361 /* We still have queued message, connect the ready
362 * signal on the new first message sender. */
363 g_signal_connect (sender, "notify::ready",
364 G_CALLBACK (tp_chat_sender_ready_notify_cb),
371 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
372 EmpathyMessage *message)
374 EmpathyTpChatPriv *priv = GET_PRIV (chat);
375 EmpathyContact *sender;
376 EmpathyContactReady ready;
378 if (g_queue_get_length (priv->messages_queue) > 0) {
379 DEBUG ("Message queue not empty");
380 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
385 sender = empathy_message_get_sender (message);
386 ready = empathy_contact_get_ready (sender);
387 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
388 (ready & EMPATHY_CONTACT_READY_ID)) {
389 DEBUG ("Message queue empty and sender ready");
390 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
391 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
395 DEBUG ("Sender not ready");
396 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
397 g_signal_connect (sender, "notify::ready",
398 G_CALLBACK (tp_chat_sender_ready_notify_cb),
403 tp_chat_received_cb (TpChannel *channel,
409 const gchar *message_body,
413 EmpathyTpChatPriv *priv = GET_PRIV (chat);
414 EmpathyMessage *message;
416 if (priv->channel == NULL)
419 if (priv->listing_pending_messages) {
423 DEBUG ("Message received: %s", message_body);
425 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
432 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
433 g_object_unref (message);
437 tp_chat_sent_cb (TpChannel *channel,
440 const gchar *message_body,
444 EmpathyTpChatPriv *priv = GET_PRIV (chat);
445 EmpathyMessage *message;
447 if (priv->channel == NULL)
450 DEBUG ("Message sent: %s", message_body);
452 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
459 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
460 g_object_unref (message);
464 tp_chat_send_error_cb (TpChannel *channel,
468 const gchar *message_body,
472 EmpathyMessage *message;
473 EmpathyTpChatPriv *priv = GET_PRIV (chat);
475 if (priv->channel == NULL)
478 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
480 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
487 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
488 g_object_unref (message);
492 tp_chat_send_cb (TpChannel *proxy,
497 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
500 DEBUG ("Error: %s", error->message);
501 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
502 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
507 tp_chat_state_changed_cb (TpChannel *channel,
513 EmpathyTpChatPriv *priv = GET_PRIV (chat);
514 EmpathyContact *contact;
516 if (priv->channel == NULL)
519 contact = empathy_contact_factory_get_from_handle (priv->factory,
523 DEBUG ("Chat state changed for %s (%d): %d",
524 empathy_contact_get_name (contact), handle, state);
526 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
527 g_object_unref (contact);
531 tp_chat_list_pending_messages_cb (TpChannel *channel,
532 const GPtrArray *messages_list,
537 EmpathyTpChatPriv *priv = GET_PRIV (chat);
540 priv->listing_pending_messages = FALSE;
542 if (priv->channel == NULL)
546 DEBUG ("Error listing pending messages: %s", error->message);
550 for (i = 0; i < messages_list->len; i++) {
551 EmpathyMessage *message;
552 GValueArray *message_struct;
553 const gchar *message_body;
560 message_struct = g_ptr_array_index (messages_list, i);
562 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
563 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
564 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
565 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
566 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
567 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
569 DEBUG ("Message pending: %s", message_body);
571 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
578 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
579 g_object_unref (message);
584 tp_chat_property_flags_changed_cb (TpProxy *proxy,
585 const GPtrArray *properties,
589 EmpathyTpChatPriv *priv = GET_PRIV (chat);
592 if (priv->channel == NULL)
595 if (!priv->had_properties_list || !properties) {
599 for (i = 0; i < properties->len; i++) {
600 GValueArray *prop_struct;
601 TpChatProperty *property;
605 prop_struct = g_ptr_array_index (properties, i);
606 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
607 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
609 for (j = 0; j < priv->properties->len; j++) {
610 property = g_ptr_array_index (priv->properties, j);
611 if (property->id == id) {
612 property->flags = flags;
613 DEBUG ("property %s flags changed: %d",
614 property->name, property->flags);
622 tp_chat_properties_changed_cb (TpProxy *proxy,
623 const GPtrArray *properties,
627 EmpathyTpChatPriv *priv = GET_PRIV (chat);
630 if (priv->channel == NULL)
633 if (!priv->had_properties_list || !properties) {
637 for (i = 0; i < properties->len; i++) {
638 GValueArray *prop_struct;
639 TpChatProperty *property;
643 prop_struct = g_ptr_array_index (properties, i);
644 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
645 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
647 for (j = 0; j < priv->properties->len; j++) {
648 property = g_ptr_array_index (priv->properties, j);
649 if (property->id == id) {
650 if (property->value) {
651 g_value_copy (src_value, property->value);
653 property->value = tp_g_value_slice_dup (src_value);
656 DEBUG ("property %s changed", property->name);
657 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
658 property->name, property->value);
666 tp_chat_get_properties_cb (TpProxy *proxy,
667 const GPtrArray *properties,
673 DEBUG ("Error getting properties: %s", error->message);
677 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
681 tp_chat_list_properties_cb (TpProxy *proxy,
682 const GPtrArray *properties,
687 EmpathyTpChatPriv *priv = GET_PRIV (chat);
691 if (priv->channel == NULL)
694 priv->had_properties_list = TRUE;
697 DEBUG ("Error listing properties: %s", error->message);
701 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
702 priv->properties = g_ptr_array_sized_new (properties->len);
703 for (i = 0; i < properties->len; i++) {
704 GValueArray *prop_struct;
705 TpChatProperty *property;
707 prop_struct = g_ptr_array_index (properties, i);
708 property = g_slice_new0 (TpChatProperty);
709 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
710 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
711 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
713 DEBUG ("Adding property name=%s id=%d flags=%d",
714 property->name, property->id, property->flags);
715 g_ptr_array_add (priv->properties, property);
716 if (property->flags & TP_PROPERTY_FLAG_READ) {
717 g_array_append_val (ids, property->id);
721 tp_cli_properties_interface_call_get_properties (proxy, -1,
723 tp_chat_get_properties_cb,
727 g_array_free (ids, TRUE);
731 empathy_tp_chat_set_property (EmpathyTpChat *chat,
735 EmpathyTpChatPriv *priv = GET_PRIV (chat);
736 TpChatProperty *property;
739 g_return_if_fail (priv->ready);
741 for (i = 0; i < priv->properties->len; i++) {
742 property = g_ptr_array_index (priv->properties, i);
743 if (!tp_strdiff (property->name, name)) {
744 GPtrArray *properties;
747 GValue dest_value = {0, };
749 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
753 g_value_init (&id, G_TYPE_UINT);
754 g_value_init (&dest_value, G_TYPE_VALUE);
755 g_value_set_uint (&id, property->id);
756 g_value_set_boxed (&dest_value, value);
758 prop = g_value_array_new (2);
759 g_value_array_append (prop, &id);
760 g_value_array_append (prop, &dest_value);
762 properties = g_ptr_array_sized_new (1);
763 g_ptr_array_add (properties, prop);
765 DEBUG ("Set property %s", name);
766 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
768 (tp_cli_properties_interface_callback_for_set_properties)
770 "Seting property", NULL,
773 g_ptr_array_free (properties, TRUE);
774 g_value_array_free (prop);
782 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
784 EmpathyTpChatPriv *priv = GET_PRIV (chat);
785 TpConnection *connection;
786 guint handle, handle_type;
788 if (priv->channel == NULL)
791 DEBUG ("Channel ready");
793 g_object_get (priv->channel,
794 "connection", &connection,
796 "handle_type", &handle_type,
799 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
803 handles = g_array_new (FALSE, FALSE, sizeof (guint));
804 g_array_append_val (handles, handle);
805 tp_cli_connection_run_inspect_handles (connection, -1,
806 handle_type, handles,
809 g_array_free (handles, TRUE);
813 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
814 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
817 g_object_notify (G_OBJECT (chat), "remote-contact");
820 if (tp_proxy_has_interface_by_id (priv->channel,
821 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
822 priv->group = empathy_tp_group_new (priv->channel);
824 g_signal_connect (priv->group, "member-added",
825 G_CALLBACK (tp_chat_member_added_cb),
827 g_signal_connect (priv->group, "member-removed",
828 G_CALLBACK (tp_chat_member_removed_cb),
830 g_signal_connect (priv->group, "local-pending",
831 G_CALLBACK (tp_chat_local_pending_cb),
833 empathy_run_until_ready (priv->group);
835 priv->members_count = 2;
838 if (tp_proxy_has_interface_by_id (priv->channel,
839 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
840 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
841 tp_chat_list_properties_cb,
844 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
845 tp_chat_properties_changed_cb,
847 G_OBJECT (chat), NULL);
848 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
849 tp_chat_property_flags_changed_cb,
851 G_OBJECT (chat), NULL);
854 priv->listing_pending_messages = TRUE;
855 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
857 tp_chat_list_pending_messages_cb,
861 tp_cli_channel_type_text_connect_to_received (priv->channel,
864 G_OBJECT (chat), NULL);
865 tp_cli_channel_type_text_connect_to_sent (priv->channel,
868 G_OBJECT (chat), NULL);
869 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
870 tp_chat_send_error_cb,
872 G_OBJECT (chat), NULL);
873 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
874 tp_chat_state_changed_cb,
876 G_OBJECT (chat), NULL);
877 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
878 tp_chat_state_changed_cb,
880 G_OBJECT (chat), NULL);
883 g_object_notify (G_OBJECT (chat), "ready");
887 tp_chat_dispose (GObject *object)
889 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
890 EmpathyTpChatPriv *priv = GET_PRIV (self);
892 if (priv->dispose_has_run)
895 priv->dispose_has_run = TRUE;
897 if (priv->channel != NULL)
898 empathy_tp_chat_close (self);
900 if (priv->remote_contact != NULL)
901 g_object_unref (priv->remote_contact);
902 priv->remote_contact = NULL;
904 if (priv->group != NULL)
905 g_object_unref (priv->group);
908 g_object_unref (priv->factory);
909 priv->factory = NULL;
911 g_object_unref (priv->user);
914 g_object_unref (priv->account);
915 priv->account = NULL;
917 if (g_queue_get_length (priv->messages_queue) > 0) {
918 EmpathyMessage *message;
919 EmpathyContact *contact;
921 message = g_queue_peek_head (priv->messages_queue);
922 contact = empathy_message_get_sender (message);
923 g_signal_handlers_disconnect_by_func (contact,
924 tp_chat_sender_ready_notify_cb, object);
927 g_list_foreach (priv->messages_queue->head, (GFunc) g_object_unref, NULL);
928 g_queue_free (priv->messages_queue);
930 g_list_foreach (priv->pending_messages_queue->head,
931 (GFunc) g_object_unref, NULL);
932 g_queue_free (priv->pending_messages_queue);
934 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
935 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
939 tp_chat_finalize (GObject *object)
941 EmpathyTpChatPriv *priv = GET_PRIV (object);
944 DEBUG ("Finalize: %p", object);
947 if (priv->properties) {
948 for (i = 0; i < priv->properties->len; i++) {
949 TpChatProperty *property;
951 property = g_ptr_array_index (priv->properties, i);
952 g_free (property->name);
953 if (property->value) {
954 tp_g_value_slice_free (property->value);
956 g_slice_free (TpChatProperty, property);
958 g_ptr_array_free (priv->properties, TRUE);
961 if (priv->remote_contact) {
962 g_object_unref (priv->remote_contact);
965 g_object_unref (priv->group);
968 if (priv->contact_monitor) {
969 g_object_unref (priv->contact_monitor);
972 g_object_unref (priv->factory);
973 g_object_unref (priv->user);
974 g_object_unref (priv->account);
978 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
982 tp_chat_constructor (GType type,
984 GObjectConstructParam *props)
987 EmpathyTpChatPriv *priv;
988 gboolean channel_ready;
990 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
992 priv = GET_PRIV (chat);
993 priv->account = empathy_channel_get_account (priv->channel);
994 priv->factory = empathy_contact_factory_dup_singleton ();
995 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
997 g_signal_connect (priv->channel, "invalidated",
998 G_CALLBACK (tp_chat_invalidated_cb),
1001 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1002 if (channel_ready) {
1003 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1005 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1006 G_CALLBACK (tp_chat_channel_ready_cb),
1014 tp_chat_get_property (GObject *object,
1019 EmpathyTpChatPriv *priv = GET_PRIV (object);
1023 g_value_set_object (value, priv->channel);
1025 case PROP_REMOTE_CONTACT:
1026 g_value_set_object (value, priv->remote_contact);
1029 g_value_set_boolean (value, priv->ready);
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1038 tp_chat_set_property (GObject *object,
1040 const GValue *value,
1043 EmpathyTpChatPriv *priv = GET_PRIV (object);
1047 priv->channel = g_object_ref (g_value_get_object (value));
1050 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1056 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1058 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1060 object_class->dispose = tp_chat_dispose;
1061 object_class->finalize = tp_chat_finalize;
1062 object_class->constructor = tp_chat_constructor;
1063 object_class->get_property = tp_chat_get_property;
1064 object_class->set_property = tp_chat_set_property;
1066 g_object_class_install_property (object_class,
1068 g_param_spec_object ("channel",
1069 "telepathy channel",
1070 "The text channel for the chat",
1073 G_PARAM_CONSTRUCT_ONLY));
1075 g_object_class_install_property (object_class,
1076 PROP_REMOTE_CONTACT,
1077 g_param_spec_object ("remote-contact",
1078 "The remote contact",
1079 "The remote contact if there is no group iface on the channel",
1080 EMPATHY_TYPE_CONTACT,
1082 g_object_class_install_property (object_class,
1084 g_param_spec_boolean ("ready",
1085 "Is the object ready",
1086 "This object can't be used until this becomes true",
1091 signals[MESSAGE_RECEIVED] =
1092 g_signal_new ("message-received",
1093 G_TYPE_FROM_CLASS (klass),
1097 g_cclosure_marshal_VOID__OBJECT,
1099 1, EMPATHY_TYPE_MESSAGE);
1101 signals[SEND_ERROR] =
1102 g_signal_new ("send-error",
1103 G_TYPE_FROM_CLASS (klass),
1107 _empathy_marshal_VOID__OBJECT_UINT,
1109 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1111 signals[CHAT_STATE_CHANGED] =
1112 g_signal_new ("chat-state-changed",
1113 G_TYPE_FROM_CLASS (klass),
1117 _empathy_marshal_VOID__OBJECT_UINT,
1119 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1121 signals[PROPERTY_CHANGED] =
1122 g_signal_new ("property-changed",
1123 G_TYPE_FROM_CLASS (klass),
1127 _empathy_marshal_VOID__STRING_BOXED,
1129 2, G_TYPE_STRING, G_TYPE_VALUE);
1132 g_signal_new ("destroy",
1133 G_TYPE_FROM_CLASS (klass),
1137 g_cclosure_marshal_VOID__VOID,
1141 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1145 empathy_tp_chat_init (EmpathyTpChat *chat)
1147 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1148 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1151 priv->contact_monitor = NULL;
1152 priv->messages_queue = g_queue_new ();
1153 priv->pending_messages_queue = g_queue_new ();
1157 tp_chat_iface_init (EmpathyContactListIface *iface)
1159 iface->add = tp_chat_add;
1160 iface->remove = tp_chat_remove;
1161 iface->get_members = tp_chat_get_members;
1162 iface->get_monitor = tp_chat_get_monitor;
1166 empathy_tp_chat_new (TpChannel *channel)
1168 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1174 empathy_tp_chat_close (EmpathyTpChat *chat)
1176 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1178 /* If there are still messages left, it'll come back..
1179 We loose the ordering of sent messages though */
1180 g_signal_handlers_disconnect_by_func (priv->channel,
1181 tp_chat_invalidated_cb, chat);
1182 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1183 "closing channel", NULL, NULL);
1184 g_object_unref (priv->channel);
1186 g_signal_emit (chat, signals[DESTROY], 0);
1187 priv->channel = NULL;
1191 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1193 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1195 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1196 g_return_val_if_fail (priv->ready, NULL);
1202 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1204 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1206 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1208 return priv->remote_contact;
1212 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1214 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1216 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1218 return priv->account;
1222 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1224 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1226 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1228 return priv->channel;
1232 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1234 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1236 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1242 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1244 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1246 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1248 return priv->members_count;
1252 empathy_tp_chat_send (EmpathyTpChat *chat,
1253 EmpathyMessage *message)
1255 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1256 const gchar *message_body;
1257 TpChannelTextMessageType message_type;
1259 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1260 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1261 g_return_if_fail (priv->ready);
1263 message_body = empathy_message_get_body (message);
1264 message_type = empathy_message_get_tptype (message);
1266 DEBUG ("Sending message: %s", message_body);
1267 tp_cli_channel_type_text_call_send (priv->channel, -1,
1271 g_object_ref (message),
1272 (GDestroyNotify) g_object_unref,
1277 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1278 TpChannelChatState state)
1280 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1282 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1283 g_return_if_fail (priv->ready);
1285 DEBUG ("Set state: %d", state);
1286 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1289 "setting chat state",
1296 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1298 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1300 return priv->pending_messages_queue->head;
1304 acknowledge_messages (EmpathyTpChat *chat, GArray *ids)
1306 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1308 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
1309 -1, ids, tp_chat_async_cb, "acknowledging received message",
1310 NULL, G_OBJECT (chat));
1314 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1315 EmpathyMessage *message)
1317 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1318 GArray *message_ids;
1322 if (empathy_message_get_sender (message) == priv->user)
1325 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1327 id = empathy_message_get_id (message);
1328 g_array_append_val (message_ids, id);
1329 acknowledge_messages (chat, message_ids);
1330 g_array_free (message_ids, TRUE);
1333 m = g_queue_find (priv->pending_messages_queue, message);
1334 g_assert (m != NULL);
1335 g_queue_delete_link (priv->pending_messages_queue, m);
1336 g_object_unref (message);
1340 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1341 const GList *messages)
1343 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1344 /* Copy messages as the messges list (probably is) our own */
1345 GList *msgs = g_list_copy ((GList *) messages);
1348 GArray *message_ids;
1349 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
1351 for (l = msgs; l != NULL; l = g_list_next (l))
1354 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1356 m = g_queue_find (priv->pending_messages_queue, message);
1357 g_assert (m != NULL);
1358 g_queue_delete_link (priv->pending_messages_queue, m);
1360 if (empathy_message_get_sender (message) != priv->user)
1362 guint id = empathy_message_get_id (message);
1363 g_array_append_val (message_ids, id);
1367 if (message_ids->len > 0)
1368 acknowledge_messages (chat, message_ids);
1370 g_array_free (message_ids, TRUE);