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,
95 static void acknowledge_messages (EmpathyTpChat *chat, GArray *ids);
98 tp_chat_invalidated_cb (TpProxy *proxy,
104 EmpathyTpChatPriv *priv = GET_PRIV (chat);
106 g_object_unref (priv->channel);
107 priv->channel = NULL;
109 DEBUG ("Channel invalidated: %s", message);
110 g_signal_emit (chat, signals[DESTROY], 0);
115 tp_chat_async_cb (TpChannel *proxy,
118 GObject *weak_object)
121 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
126 tp_chat_member_added_cb (EmpathyTpGroup *group,
127 EmpathyContact *contact,
128 EmpathyContact *actor,
130 const gchar *message,
133 EmpathyTpChatPriv *priv = GET_PRIV (chat);
134 guint handle_type = 0;
136 if (priv->channel == NULL)
139 priv->members_count++;
140 g_signal_emit_by_name (chat, "members-changed",
141 contact, actor, reason, message,
144 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
145 if (handle_type == TP_HANDLE_TYPE_ROOM) {
149 if (priv->members_count > 2 && priv->remote_contact) {
150 /* We now have more than 2 members, this is not a p2p chat
151 * anymore. Remove the remote-contact as it makes no sense, the
152 * EmpathyContactList interface must be used now. */
153 g_object_unref (priv->remote_contact);
154 priv->remote_contact = NULL;
155 g_object_notify (G_OBJECT (chat), "remote-contact");
157 if (priv->members_count <= 2 && !priv->remote_contact &&
158 !empathy_contact_is_user (contact)) {
159 /* This is a p2p chat, if it's not ourself that means this is
160 * the remote contact with who we are chatting. This is to
161 * avoid forcing the usage of the EmpathyContactList interface
163 priv->remote_contact = g_object_ref (contact);
164 g_object_notify (G_OBJECT (chat), "remote-contact");
169 tp_chat_member_removed_cb (EmpathyTpGroup *group,
170 EmpathyContact *contact,
171 EmpathyContact *actor,
173 const gchar *message,
176 EmpathyTpChatPriv *priv = GET_PRIV (chat);
177 guint handle_type = 0;
179 if (priv->channel == NULL)
182 priv->members_count--;
183 g_signal_emit_by_name (chat, "members-changed",
184 contact, actor, reason, message,
187 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
188 if (handle_type == TP_HANDLE_TYPE_ROOM) {
192 if (priv->members_count <= 2 && !priv->remote_contact) {
195 /* We are not a MUC anymore, get the remote contact back */
196 members = empathy_tp_group_get_members (group);
197 for (l = members; l; l = l->next) {
198 if (!empathy_contact_is_user (l->data)) {
199 priv->remote_contact = g_object_ref (l->data);
200 g_object_notify (G_OBJECT (chat), "remote-contact");
204 g_list_foreach (members, (GFunc) g_object_unref, NULL);
205 g_list_free (members);
210 tp_chat_local_pending_cb (EmpathyTpGroup *group,
211 EmpathyContact *contact,
212 EmpathyContact *actor,
214 const gchar *message,
217 EmpathyTpChatPriv *priv = GET_PRIV (chat);
219 if (priv->channel == NULL)
222 g_signal_emit_by_name (chat, "pendings-changed",
223 contact, actor, reason, message,
228 tp_chat_add (EmpathyContactList *list,
229 EmpathyContact *contact,
230 const gchar *message)
232 EmpathyTpChatPriv *priv = GET_PRIV (list);
234 GArray handles = {(gchar *) &handle, 1};
236 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
237 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
239 handle = empathy_contact_get_handle (contact);
240 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
247 tp_chat_remove (EmpathyContactList *list,
248 EmpathyContact *contact,
249 const gchar *message)
251 EmpathyTpChatPriv *priv = GET_PRIV (list);
253 GArray handles = {(gchar *) &handle, 1};
255 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
256 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
258 handle = empathy_contact_get_handle (contact);
259 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
266 tp_chat_get_members (EmpathyContactList *list)
268 EmpathyTpChatPriv *priv = GET_PRIV (list);
269 GList *members = NULL;
271 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
274 members = empathy_tp_group_get_members (priv->group);
276 members = g_list_prepend (members, g_object_ref (priv->user));
277 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
283 static EmpathyContactMonitor *
284 tp_chat_get_monitor (EmpathyContactList *list)
286 EmpathyTpChatPriv *priv;
288 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
290 priv = GET_PRIV (list);
292 if (priv->contact_monitor == NULL) {
293 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
296 return priv->contact_monitor;
299 static EmpathyMessage *
300 tp_chat_build_message (EmpathyTpChat *chat,
305 const gchar *message_body)
307 EmpathyTpChatPriv *priv;
308 EmpathyMessage *message;
309 EmpathyContact *sender;
311 priv = GET_PRIV (chat);
313 if (from_handle == 0) {
314 sender = g_object_ref (priv->user);
316 sender = empathy_contact_factory_get_from_handle (priv->factory,
321 message = empathy_message_new (message_body);
322 empathy_message_set_tptype (message, type);
323 empathy_message_set_sender (message, sender);
324 empathy_message_set_receiver (message, priv->user);
325 empathy_message_set_timestamp (message, timestamp);
326 empathy_message_set_id (message, id);
328 g_object_unref (sender);
334 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
335 GParamSpec *param_spec,
338 EmpathyTpChatPriv *priv = GET_PRIV (chat);
339 EmpathyMessage *message;
340 EmpathyContactReady ready;
341 EmpathyContact *sender = NULL;
342 gboolean removed = FALSE;
344 /* Emit all messages queued until we find a message with not
345 * ready sender (in case of a MUC we could have more than one sender).
346 * When leaving this loop, sender is the first not ready contact queued
347 * and removed tells if at least one message got removed
349 while ((message = g_queue_peek_head (priv->messages_queue)) != NULL) {
350 sender = empathy_message_get_sender (message);
351 ready = empathy_contact_get_ready (sender);
353 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
354 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
358 DEBUG ("Queued message ready");
359 message = g_queue_pop_head (priv->messages_queue);
360 g_queue_push_tail (priv->pending_messages_queue, message);
361 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
366 /* We removed at least one message from the queue, disconnect
367 * the ready signal from the previous contact */
368 g_signal_handlers_disconnect_by_func (contact,
369 tp_chat_sender_ready_notify_cb,
372 if (g_queue_get_length (priv->messages_queue) > 0) {
373 /* We still have queued message, connect the ready
374 * signal on the new first message sender. */
375 g_signal_connect (sender, "notify::ready",
376 G_CALLBACK (tp_chat_sender_ready_notify_cb),
383 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
384 EmpathyMessage *message)
386 EmpathyTpChatPriv *priv = GET_PRIV (chat);
387 EmpathyContact *sender;
388 EmpathyContactReady ready;
390 if (g_queue_get_length (priv->messages_queue) > 0) {
391 DEBUG ("Message queue not empty");
392 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
397 sender = empathy_message_get_sender (message);
398 ready = empathy_contact_get_ready (sender);
399 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
400 (ready & EMPATHY_CONTACT_READY_ID)) {
401 DEBUG ("Message queue empty and sender ready");
402 g_queue_push_tail (priv->pending_messages_queue, g_object_ref (message));
403 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
407 DEBUG ("Sender not ready");
408 g_queue_push_tail (priv->messages_queue, g_object_ref (message));
409 g_signal_connect (sender, "notify::ready",
410 G_CALLBACK (tp_chat_sender_ready_notify_cb),
415 tp_chat_received_cb (TpChannel *channel,
421 const gchar *message_body,
425 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
426 EmpathyTpChatPriv *priv = GET_PRIV (chat);
427 EmpathyMessage *message;
429 if (priv->channel == NULL)
432 if (priv->listing_pending_messages) {
436 DEBUG ("Message received: %s", message_body);
438 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
439 !tp_strdiff (message_body, "")) {
442 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
444 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
445 g_array_append_val (ids, message_id);
446 acknowledge_messages (chat, ids);
447 g_array_free (ids, TRUE);
452 message = tp_chat_build_message (chat,
459 tp_chat_emit_or_queue_message (chat, message);
460 g_object_unref (message);
464 tp_chat_sent_cb (TpChannel *channel,
467 const gchar *message_body,
471 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
472 EmpathyTpChatPriv *priv = GET_PRIV (chat);
473 EmpathyMessage *message;
475 if (priv->channel == NULL)
478 DEBUG ("Message sent: %s", message_body);
480 message = tp_chat_build_message (chat,
487 tp_chat_emit_or_queue_message (chat, message);
488 g_object_unref (message);
492 tp_chat_send_error_cb (TpChannel *channel,
496 const gchar *message_body,
500 EmpathyMessage *message;
501 EmpathyTpChatPriv *priv = GET_PRIV (chat);
503 if (priv->channel == NULL)
506 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
508 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
515 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
516 g_object_unref (message);
520 tp_chat_send_cb (TpChannel *proxy,
525 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
528 DEBUG ("Error: %s", error->message);
529 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
530 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
535 tp_chat_state_changed_cb (TpChannel *channel,
541 EmpathyTpChatPriv *priv = GET_PRIV (chat);
542 EmpathyContact *contact;
544 if (priv->channel == NULL)
547 contact = empathy_contact_factory_get_from_handle (priv->factory,
551 DEBUG ("Chat state changed for %s (%d): %d",
552 empathy_contact_get_name (contact), handle, state);
554 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
555 g_object_unref (contact);
559 tp_chat_list_pending_messages_cb (TpChannel *channel,
560 const GPtrArray *messages_list,
565 EmpathyTpChat *chat = EMPATHY_TP_CHAT (chat_);
566 EmpathyTpChatPriv *priv = GET_PRIV (chat);
568 GArray *empty_non_text_content_ids = NULL;
570 priv->listing_pending_messages = FALSE;
572 if (priv->channel == NULL)
576 DEBUG ("Error listing pending messages: %s", error->message);
580 for (i = 0; i < messages_list->len; i++) {
581 EmpathyMessage *message;
582 GValueArray *message_struct;
583 const gchar *message_body;
590 message_struct = g_ptr_array_index (messages_list, i);
592 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
593 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
594 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
595 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
596 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
597 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
599 DEBUG ("Message pending: %s", message_body);
601 if (message_flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_NON_TEXT_CONTENT &&
602 !tp_strdiff (message_body, "")) {
603 DEBUG ("Empty message with NonTextContent, ignoring and acking.");
605 if (empty_non_text_content_ids == NULL) {
606 empty_non_text_content_ids = g_array_new (FALSE, FALSE, sizeof (guint));
609 g_array_append_val (empty_non_text_content_ids, message_id);
613 message = tp_chat_build_message (chat,
620 tp_chat_emit_or_queue_message (chat, message);
621 g_object_unref (message);
624 if (empty_non_text_content_ids != NULL) {
625 acknowledge_messages (chat, empty_non_text_content_ids);
626 g_array_free (empty_non_text_content_ids, TRUE);
631 tp_chat_property_flags_changed_cb (TpProxy *proxy,
632 const GPtrArray *properties,
636 EmpathyTpChatPriv *priv = GET_PRIV (chat);
639 if (priv->channel == NULL)
642 if (!priv->had_properties_list || !properties) {
646 for (i = 0; i < properties->len; i++) {
647 GValueArray *prop_struct;
648 TpChatProperty *property;
652 prop_struct = g_ptr_array_index (properties, i);
653 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
654 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
656 for (j = 0; j < priv->properties->len; j++) {
657 property = g_ptr_array_index (priv->properties, j);
658 if (property->id == id) {
659 property->flags = flags;
660 DEBUG ("property %s flags changed: %d",
661 property->name, property->flags);
669 tp_chat_properties_changed_cb (TpProxy *proxy,
670 const GPtrArray *properties,
674 EmpathyTpChatPriv *priv = GET_PRIV (chat);
677 if (priv->channel == NULL)
680 if (!priv->had_properties_list || !properties) {
684 for (i = 0; i < properties->len; i++) {
685 GValueArray *prop_struct;
686 TpChatProperty *property;
690 prop_struct = g_ptr_array_index (properties, i);
691 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
692 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
694 for (j = 0; j < priv->properties->len; j++) {
695 property = g_ptr_array_index (priv->properties, j);
696 if (property->id == id) {
697 if (property->value) {
698 g_value_copy (src_value, property->value);
700 property->value = tp_g_value_slice_dup (src_value);
703 DEBUG ("property %s changed", property->name);
704 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
705 property->name, property->value);
713 tp_chat_get_properties_cb (TpProxy *proxy,
714 const GPtrArray *properties,
720 DEBUG ("Error getting properties: %s", error->message);
724 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
728 tp_chat_list_properties_cb (TpProxy *proxy,
729 const GPtrArray *properties,
734 EmpathyTpChatPriv *priv = GET_PRIV (chat);
738 if (priv->channel == NULL)
741 priv->had_properties_list = TRUE;
744 DEBUG ("Error listing properties: %s", error->message);
748 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
749 priv->properties = g_ptr_array_sized_new (properties->len);
750 for (i = 0; i < properties->len; i++) {
751 GValueArray *prop_struct;
752 TpChatProperty *property;
754 prop_struct = g_ptr_array_index (properties, i);
755 property = g_slice_new0 (TpChatProperty);
756 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
757 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
758 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
760 DEBUG ("Adding property name=%s id=%d flags=%d",
761 property->name, property->id, property->flags);
762 g_ptr_array_add (priv->properties, property);
763 if (property->flags & TP_PROPERTY_FLAG_READ) {
764 g_array_append_val (ids, property->id);
768 tp_cli_properties_interface_call_get_properties (proxy, -1,
770 tp_chat_get_properties_cb,
774 g_array_free (ids, TRUE);
778 empathy_tp_chat_set_property (EmpathyTpChat *chat,
782 EmpathyTpChatPriv *priv = GET_PRIV (chat);
783 TpChatProperty *property;
786 g_return_if_fail (priv->ready);
788 for (i = 0; i < priv->properties->len; i++) {
789 property = g_ptr_array_index (priv->properties, i);
790 if (!tp_strdiff (property->name, name)) {
791 GPtrArray *properties;
794 GValue dest_value = {0, };
796 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
800 g_value_init (&id, G_TYPE_UINT);
801 g_value_init (&dest_value, G_TYPE_VALUE);
802 g_value_set_uint (&id, property->id);
803 g_value_set_boxed (&dest_value, value);
805 prop = g_value_array_new (2);
806 g_value_array_append (prop, &id);
807 g_value_array_append (prop, &dest_value);
809 properties = g_ptr_array_sized_new (1);
810 g_ptr_array_add (properties, prop);
812 DEBUG ("Set property %s", name);
813 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
815 (tp_cli_properties_interface_callback_for_set_properties)
817 "Seting property", NULL,
820 g_ptr_array_free (properties, TRUE);
821 g_value_array_free (prop);
829 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
831 EmpathyTpChatPriv *priv = GET_PRIV (chat);
832 TpConnection *connection;
833 guint handle, handle_type;
835 if (priv->channel == NULL)
838 DEBUG ("Channel ready");
840 g_object_get (priv->channel,
841 "connection", &connection,
843 "handle_type", &handle_type,
846 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
850 handles = g_array_new (FALSE, FALSE, sizeof (guint));
851 g_array_append_val (handles, handle);
852 tp_cli_connection_run_inspect_handles (connection, -1,
853 handle_type, handles,
856 g_array_free (handles, TRUE);
860 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
861 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
864 g_object_notify (G_OBJECT (chat), "remote-contact");
867 if (tp_proxy_has_interface_by_id (priv->channel,
868 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
869 priv->group = empathy_tp_group_new (priv->channel);
871 g_signal_connect (priv->group, "member-added",
872 G_CALLBACK (tp_chat_member_added_cb),
874 g_signal_connect (priv->group, "member-removed",
875 G_CALLBACK (tp_chat_member_removed_cb),
877 g_signal_connect (priv->group, "local-pending",
878 G_CALLBACK (tp_chat_local_pending_cb),
880 empathy_run_until_ready (priv->group);
882 priv->members_count = 2;
885 if (tp_proxy_has_interface_by_id (priv->channel,
886 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
887 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
888 tp_chat_list_properties_cb,
891 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
892 tp_chat_properties_changed_cb,
894 G_OBJECT (chat), NULL);
895 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
896 tp_chat_property_flags_changed_cb,
898 G_OBJECT (chat), NULL);
901 priv->listing_pending_messages = TRUE;
902 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
904 tp_chat_list_pending_messages_cb,
908 tp_cli_channel_type_text_connect_to_received (priv->channel,
911 G_OBJECT (chat), NULL);
912 tp_cli_channel_type_text_connect_to_sent (priv->channel,
915 G_OBJECT (chat), NULL);
916 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
917 tp_chat_send_error_cb,
919 G_OBJECT (chat), NULL);
920 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
921 tp_chat_state_changed_cb,
923 G_OBJECT (chat), NULL);
924 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
925 tp_chat_state_changed_cb,
927 G_OBJECT (chat), NULL);
930 g_object_notify (G_OBJECT (chat), "ready");
934 tp_chat_dispose (GObject *object)
936 EmpathyTpChat *self = EMPATHY_TP_CHAT (object);
937 EmpathyTpChatPriv *priv = GET_PRIV (self);
939 if (priv->dispose_has_run)
942 priv->dispose_has_run = TRUE;
944 if (priv->channel != NULL)
946 g_signal_handlers_disconnect_by_func (priv->channel,
947 tp_chat_invalidated_cb, self);
948 g_object_unref (priv->channel);
949 priv->channel = NULL;
952 if (priv->remote_contact != NULL)
953 g_object_unref (priv->remote_contact);
955 priv->remote_contact = NULL;
957 if (priv->group != NULL)
958 g_object_unref (priv->group);
961 if (priv->factory != NULL)
962 g_object_unref (priv->factory);
963 priv->factory = NULL;
965 if (priv->user != NULL);
966 g_object_unref (priv->user);
969 if (priv->account != NULL);
970 g_object_unref (priv->account);
971 priv->account = NULL;
973 if (priv->contact_monitor)
974 g_object_unref (priv->contact_monitor);
975 priv->contact_monitor = NULL;
977 if (!g_queue_is_empty (priv->messages_queue)) {
978 EmpathyMessage *message;
979 EmpathyContact *contact;
981 message = g_queue_peek_head (priv->messages_queue);
982 contact = empathy_message_get_sender (message);
983 g_signal_handlers_disconnect_by_func (contact,
984 tp_chat_sender_ready_notify_cb, object);
987 g_list_foreach (priv->messages_queue->head,
988 (GFunc) g_object_unref, NULL);
990 g_list_foreach (priv->pending_messages_queue->head,
991 (GFunc) g_object_unref, NULL);
993 if (G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose)
994 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->dispose (object);
998 tp_chat_finalize (GObject *object)
1000 EmpathyTpChatPriv *priv = GET_PRIV (object);
1003 DEBUG ("Finalize: %p", object);
1005 if (priv->properties) {
1006 for (i = 0; i < priv->properties->len; i++) {
1007 TpChatProperty *property;
1009 property = g_ptr_array_index (priv->properties, i);
1010 g_free (property->name);
1011 if (property->value) {
1012 tp_g_value_slice_free (property->value);
1014 g_slice_free (TpChatProperty, property);
1016 g_ptr_array_free (priv->properties, TRUE);
1021 g_queue_free (priv->messages_queue);
1022 g_queue_free (priv->pending_messages_queue);
1024 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
1028 tp_chat_constructor (GType type,
1030 GObjectConstructParam *props)
1033 EmpathyTpChatPriv *priv;
1034 gboolean channel_ready;
1036 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
1038 priv = GET_PRIV (chat);
1039 priv->account = empathy_channel_get_account (priv->channel);
1040 priv->factory = empathy_contact_factory_dup_singleton ();
1041 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
1043 g_signal_connect (priv->channel, "invalidated",
1044 G_CALLBACK (tp_chat_invalidated_cb),
1047 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
1048 if (channel_ready) {
1049 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
1051 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
1052 G_CALLBACK (tp_chat_channel_ready_cb),
1060 tp_chat_get_property (GObject *object,
1065 EmpathyTpChatPriv *priv = GET_PRIV (object);
1069 g_value_set_object (value, priv->channel);
1071 case PROP_REMOTE_CONTACT:
1072 g_value_set_object (value, priv->remote_contact);
1075 g_value_set_boolean (value, priv->ready);
1078 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1084 tp_chat_set_property (GObject *object,
1086 const GValue *value,
1089 EmpathyTpChatPriv *priv = GET_PRIV (object);
1093 priv->channel = g_value_dup_object (value);
1096 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1102 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1104 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1106 object_class->dispose = tp_chat_dispose;
1107 object_class->finalize = tp_chat_finalize;
1108 object_class->constructor = tp_chat_constructor;
1109 object_class->get_property = tp_chat_get_property;
1110 object_class->set_property = tp_chat_set_property;
1112 g_object_class_install_property (object_class,
1114 g_param_spec_object ("channel",
1115 "telepathy channel",
1116 "The text channel for the chat",
1119 G_PARAM_CONSTRUCT_ONLY));
1121 g_object_class_install_property (object_class,
1122 PROP_REMOTE_CONTACT,
1123 g_param_spec_object ("remote-contact",
1124 "The remote contact",
1125 "The remote contact if there is no group iface on the channel",
1126 EMPATHY_TYPE_CONTACT,
1128 g_object_class_install_property (object_class,
1130 g_param_spec_boolean ("ready",
1131 "Is the object ready",
1132 "This object can't be used until this becomes true",
1137 signals[MESSAGE_RECEIVED] =
1138 g_signal_new ("message-received",
1139 G_TYPE_FROM_CLASS (klass),
1143 g_cclosure_marshal_VOID__OBJECT,
1145 1, EMPATHY_TYPE_MESSAGE);
1147 signals[SEND_ERROR] =
1148 g_signal_new ("send-error",
1149 G_TYPE_FROM_CLASS (klass),
1153 _empathy_marshal_VOID__OBJECT_UINT,
1155 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1157 signals[CHAT_STATE_CHANGED] =
1158 g_signal_new ("chat-state-changed",
1159 G_TYPE_FROM_CLASS (klass),
1163 _empathy_marshal_VOID__OBJECT_UINT,
1165 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1167 signals[PROPERTY_CHANGED] =
1168 g_signal_new ("property-changed",
1169 G_TYPE_FROM_CLASS (klass),
1173 _empathy_marshal_VOID__STRING_BOXED,
1175 2, G_TYPE_STRING, G_TYPE_VALUE);
1178 g_signal_new ("destroy",
1179 G_TYPE_FROM_CLASS (klass),
1183 g_cclosure_marshal_VOID__VOID,
1187 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1191 empathy_tp_chat_init (EmpathyTpChat *chat)
1193 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1194 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1197 priv->contact_monitor = NULL;
1198 priv->messages_queue = g_queue_new ();
1199 priv->pending_messages_queue = g_queue_new ();
1203 tp_chat_iface_init (EmpathyContactListIface *iface)
1205 iface->add = tp_chat_add;
1206 iface->remove = tp_chat_remove;
1207 iface->get_members = tp_chat_get_members;
1208 iface->get_monitor = tp_chat_get_monitor;
1212 empathy_tp_chat_new (TpChannel *channel)
1214 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1220 empathy_tp_chat_close (EmpathyTpChat *chat) {
1221 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1223 /* If there are still messages left, it'll come back..
1224 We loose the ordering of sent messages though */
1225 g_signal_handlers_disconnect_by_func (priv->channel,
1226 tp_chat_invalidated_cb, chat);
1228 tp_cli_channel_call_close (priv->channel, -1, tp_chat_async_cb,
1229 "closing channel", NULL, NULL);
1231 g_object_unref (priv->channel);
1232 priv->channel = NULL;
1234 g_signal_emit (chat, signals[DESTROY], 0);
1238 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1240 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1242 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1243 g_return_val_if_fail (priv->ready, NULL);
1249 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1251 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1253 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1255 return priv->remote_contact;
1259 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1261 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1263 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1265 return priv->account;
1269 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1271 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1273 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1275 return priv->channel;
1279 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1281 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1283 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1289 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1291 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1293 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1295 return priv->members_count;
1299 empathy_tp_chat_send (EmpathyTpChat *chat,
1300 EmpathyMessage *message)
1302 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1303 const gchar *message_body;
1304 TpChannelTextMessageType message_type;
1306 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1307 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1308 g_return_if_fail (priv->ready);
1310 message_body = empathy_message_get_body (message);
1311 message_type = empathy_message_get_tptype (message);
1313 DEBUG ("Sending message: %s", message_body);
1314 tp_cli_channel_type_text_call_send (priv->channel, -1,
1318 g_object_ref (message),
1319 (GDestroyNotify) g_object_unref,
1324 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1325 TpChannelChatState state)
1327 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1329 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1330 g_return_if_fail (priv->ready);
1332 DEBUG ("Set state: %d", state);
1333 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1336 "setting chat state",
1343 empathy_tp_chat_get_pending_messages (EmpathyTpChat *chat)
1345 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1347 return priv->pending_messages_queue->head;
1351 acknowledge_messages (EmpathyTpChat *chat, GArray *ids) {
1352 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1354 tp_cli_channel_type_text_call_acknowledge_pending_messages (
1355 priv->channel, -1, ids, tp_chat_async_cb,
1356 "acknowledging received message", NULL, G_OBJECT (chat));
1360 empathy_tp_chat_acknowledge_message (EmpathyTpChat *chat,
1361 EmpathyMessage *message) {
1362 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1363 GArray *message_ids;
1367 if (empathy_message_get_sender (message) == priv->user)
1370 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1372 id = empathy_message_get_id (message);
1373 g_array_append_val (message_ids, id);
1374 acknowledge_messages (chat, message_ids);
1375 g_array_free (message_ids, TRUE);
1378 m = g_queue_find (priv->pending_messages_queue, message);
1379 g_assert (m != NULL);
1380 g_queue_delete_link (priv->pending_messages_queue, m);
1381 g_object_unref (message);
1385 empathy_tp_chat_acknowledge_messages (EmpathyTpChat *chat,
1386 const GList *messages) {
1387 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1388 /* Copy messages as the messges list (probably is) our own */
1389 GList *msgs = g_list_copy ((GList *) messages);
1392 GArray *message_ids;
1394 length = g_list_length ((GList *)messages);
1399 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
1401 for (l = msgs; l != NULL; l = g_list_next (l)) {
1404 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1406 m = g_queue_find (priv->pending_messages_queue, message);
1407 g_assert (m != NULL);
1408 g_queue_delete_link (priv->pending_messages_queue, m);
1410 if (empathy_message_get_sender (message) != priv->user) {
1411 guint id = empathy_message_get_id (message);
1412 g_array_append_val (message_ids, id);
1416 if (message_ids->len > 0)
1417 acknowledge_messages (chat, message_ids);
1419 g_array_free (message_ids, TRUE);