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 EmpathyContactFactory *factory;
44 EmpathyContactMonitor *contact_monitor;
46 EmpathyContact *remote_contact;
47 EmpathyTpGroup *group;
52 gboolean listing_pending_messages;
53 GSList *message_queue;
54 gboolean had_properties_list;
55 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void tp_chat_iface_init (EmpathyContactListIface *iface);
86 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
93 tp_chat_invalidated_cb (TpProxy *proxy,
99 DEBUG ("Channel invalidated: %s", message);
100 g_signal_emit (chat, signals[DESTROY], 0);
104 tp_chat_async_cb (TpChannel *proxy,
107 GObject *weak_object)
110 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
115 tp_chat_member_added_cb (EmpathyTpGroup *group,
116 EmpathyContact *contact,
117 EmpathyContact *actor,
119 const gchar *message,
122 EmpathyTpChatPriv *priv = GET_PRIV (chat);
123 guint handle_type = 0;
125 priv->members_count++;
126 g_signal_emit_by_name (chat, "members-changed",
127 contact, actor, reason, message,
130 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
131 if (handle_type == TP_HANDLE_TYPE_ROOM) {
135 if (priv->members_count > 2 && priv->remote_contact) {
136 /* We now have more than 2 members, this is not a p2p chat
137 * anymore. Remove the remote-contact as it makes no sense, the
138 * EmpathyContactList interface must be used now. */
139 g_object_unref (priv->remote_contact);
140 priv->remote_contact = NULL;
141 g_object_notify (G_OBJECT (chat), "remote-contact");
143 if (priv->members_count <= 2 && !priv->remote_contact &&
144 !empathy_contact_is_user (contact)) {
145 /* This is a p2p chat, if it's not ourself that means this is
146 * the remote contact with who we are chatting. This is to
147 * avoid forcing the usage of the EmpathyContactList interface
149 priv->remote_contact = g_object_ref (contact);
150 g_object_notify (G_OBJECT (chat), "remote-contact");
155 tp_chat_member_removed_cb (EmpathyTpGroup *group,
156 EmpathyContact *contact,
157 EmpathyContact *actor,
159 const gchar *message,
162 EmpathyTpChatPriv *priv = GET_PRIV (chat);
163 guint handle_type = 0;
165 priv->members_count--;
166 g_signal_emit_by_name (chat, "members-changed",
167 contact, actor, reason, message,
170 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
171 if (handle_type == TP_HANDLE_TYPE_ROOM) {
175 if (priv->members_count <= 2 && !priv->remote_contact) {
178 /* We are not a MUC anymore, get the remote contact back */
179 members = empathy_tp_group_get_members (group);
180 for (l = members; l; l = l->next) {
181 if (!empathy_contact_is_user (l->data)) {
182 priv->remote_contact = g_object_ref (l->data);
183 g_object_notify (G_OBJECT (chat), "remote-contact");
187 g_list_foreach (members, (GFunc) g_object_unref, NULL);
188 g_list_free (members);
193 tp_chat_local_pending_cb (EmpathyTpGroup *group,
194 EmpathyContact *contact,
195 EmpathyContact *actor,
197 const gchar *message,
200 g_signal_emit_by_name (chat, "pendings-changed",
201 contact, actor, reason, message,
206 tp_chat_add (EmpathyContactList *list,
207 EmpathyContact *contact,
208 const gchar *message)
210 EmpathyTpChatPriv *priv = GET_PRIV (list);
212 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
213 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
216 empathy_tp_group_add_member (priv->group, contact, message);
221 tp_chat_remove (EmpathyContactList *list,
222 EmpathyContact *contact,
223 const gchar *message)
225 EmpathyTpChatPriv *priv = GET_PRIV (list);
227 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
228 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
231 empathy_tp_group_remove_member (priv->group, contact, message);
236 tp_chat_get_members (EmpathyContactList *list)
238 EmpathyTpChatPriv *priv = GET_PRIV (list);
239 GList *members = NULL;
241 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
244 members = empathy_tp_group_get_members (priv->group);
246 members = g_list_prepend (members, g_object_ref (priv->user));
247 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
253 static EmpathyContactMonitor *
254 tp_chat_get_monitor (EmpathyContactList *list)
256 EmpathyTpChatPriv *priv;
258 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
260 priv = GET_PRIV (list);
262 if (priv->contact_monitor == NULL) {
263 priv->contact_monitor = empathy_contact_monitor_new_for_iface (list);
266 return priv->contact_monitor;
269 static EmpathyMessage *
270 tp_chat_build_message (EmpathyTpChat *chat,
274 const gchar *message_body)
276 EmpathyTpChatPriv *priv;
277 EmpathyMessage *message;
278 EmpathyContact *sender;
280 priv = GET_PRIV (chat);
282 if (from_handle == 0) {
283 sender = g_object_ref (priv->user);
285 sender = empathy_contact_factory_get_from_handle (priv->factory,
290 message = empathy_message_new (message_body);
291 empathy_message_set_tptype (message, type);
292 empathy_message_set_sender (message, sender);
293 empathy_message_set_receiver (message, priv->user);
294 empathy_message_set_timestamp (message, timestamp);
296 g_object_unref (sender);
302 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
303 GParamSpec *param_spec,
306 EmpathyTpChatPriv *priv = GET_PRIV (chat);
307 EmpathyMessage *message;
308 EmpathyContactReady ready;
309 EmpathyContact *sender = NULL;
310 gboolean removed = FALSE;
312 /* Emit all messages queued until we find a message with not
313 * ready sender (in case of a MUC we could have more than one sender).
314 * When leaving this loop, sender is the first not ready contact queued
315 * and removed tells if at least one message got removed
317 while (priv->message_queue) {
318 message = priv->message_queue->data;
319 sender = empathy_message_get_sender (message);
320 ready = empathy_contact_get_ready (sender);
322 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
323 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
327 DEBUG ("Queued message ready");
328 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
329 priv->message_queue = g_slist_remove (priv->message_queue,
331 g_object_unref (message);
336 /* We removed at least one message from the queue, disconnect
337 * the ready signal from the previous contact */
338 g_signal_handlers_disconnect_by_func (contact,
339 tp_chat_sender_ready_notify_cb,
342 if (priv->message_queue) {
343 /* We still have queued message, connect the ready
344 * signal on the new first message sender. */
345 g_signal_connect (sender, "notify::ready",
346 G_CALLBACK (tp_chat_sender_ready_notify_cb),
353 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
354 EmpathyMessage *message)
356 EmpathyTpChatPriv *priv = GET_PRIV (chat);
357 EmpathyContact *sender;
358 EmpathyContactReady ready;
360 if (priv->message_queue != NULL) {
361 DEBUG ("Message queue not empty");
362 priv->message_queue = g_slist_append (priv->message_queue,
363 g_object_ref (message));
367 sender = empathy_message_get_sender (message);
368 ready = empathy_contact_get_ready (sender);
369 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
370 (ready & EMPATHY_CONTACT_READY_ID)) {
371 DEBUG ("Message queue empty and sender ready");
372 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
376 DEBUG ("Sender not ready");
377 priv->message_queue = g_slist_append (priv->message_queue,
378 g_object_ref (message));
379 g_signal_connect (sender, "notify::ready",
380 G_CALLBACK (tp_chat_sender_ready_notify_cb),
385 tp_chat_received_cb (TpChannel *channel,
391 const gchar *message_body,
395 EmpathyTpChatPriv *priv = GET_PRIV (chat);
396 EmpathyMessage *message;
398 if (priv->listing_pending_messages) {
402 DEBUG ("Message received: %s", message_body);
404 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
410 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
411 g_object_unref (message);
413 if (priv->acknowledge) {
416 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
417 g_array_append_val (message_ids, message_id);
418 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
422 "acknowledging received message",
425 g_array_free (message_ids, TRUE);
430 tp_chat_sent_cb (TpChannel *channel,
433 const gchar *message_body,
437 EmpathyMessage *message;
439 DEBUG ("Message sent: %s", message_body);
441 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
447 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
448 g_object_unref (message);
452 tp_chat_send_error_cb (TpChannel *channel,
456 const gchar *message_body,
460 EmpathyMessage *message;
462 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
464 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
470 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
471 g_object_unref (message);
475 tp_chat_send_cb (TpChannel *proxy,
480 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
483 DEBUG ("Error: %s", error->message);
484 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
485 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
490 tp_chat_state_changed_cb (TpChannel *channel,
496 EmpathyTpChatPriv *priv = GET_PRIV (chat);
497 EmpathyContact *contact;
499 contact = empathy_contact_factory_get_from_handle (priv->factory,
503 DEBUG ("Chat state changed for %s (%d): %d",
504 empathy_contact_get_name (contact), handle, state);
506 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
507 g_object_unref (contact);
511 tp_chat_list_pending_messages_cb (TpChannel *channel,
512 const GPtrArray *messages_list,
517 EmpathyTpChatPriv *priv = GET_PRIV (chat);
519 GArray *message_ids = NULL;
521 priv->listing_pending_messages = FALSE;
524 DEBUG ("Error listing pending messages: %s", error->message);
528 if (priv->acknowledge) {
529 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
533 for (i = 0; i < messages_list->len; i++) {
534 EmpathyMessage *message;
535 GValueArray *message_struct;
536 const gchar *message_body;
543 message_struct = g_ptr_array_index (messages_list, i);
545 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
546 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
547 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
548 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
549 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
550 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
552 DEBUG ("Message pending: %s", message_body);
555 g_array_append_val (message_ids, message_id);
558 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
564 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
565 g_object_unref (message);
569 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
573 "acknowledging pending messages",
576 g_array_free (message_ids, TRUE);
581 tp_chat_property_flags_changed_cb (TpProxy *proxy,
582 const GPtrArray *properties,
586 EmpathyTpChatPriv *priv = GET_PRIV (chat);
589 if (!priv->had_properties_list || !properties) {
593 for (i = 0; i < properties->len; i++) {
594 GValueArray *prop_struct;
595 TpChatProperty *property;
599 prop_struct = g_ptr_array_index (properties, i);
600 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
601 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
603 for (j = 0; j < priv->properties->len; j++) {
604 property = g_ptr_array_index (priv->properties, j);
605 if (property->id == id) {
606 property->flags = flags;
607 DEBUG ("property %s flags changed: %d",
608 property->name, property->flags);
616 tp_chat_properties_changed_cb (TpProxy *proxy,
617 const GPtrArray *properties,
621 EmpathyTpChatPriv *priv = GET_PRIV (chat);
624 if (!priv->had_properties_list || !properties) {
628 for (i = 0; i < properties->len; i++) {
629 GValueArray *prop_struct;
630 TpChatProperty *property;
634 prop_struct = g_ptr_array_index (properties, i);
635 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
636 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
638 for (j = 0; j < priv->properties->len; j++) {
639 property = g_ptr_array_index (priv->properties, j);
640 if (property->id == id) {
641 if (property->value) {
642 g_value_copy (src_value, property->value);
644 property->value = tp_g_value_slice_dup (src_value);
647 DEBUG ("property %s changed", property->name);
648 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
649 property->name, property->value);
657 tp_chat_get_properties_cb (TpProxy *proxy,
658 const GPtrArray *properties,
664 DEBUG ("Error getting properties: %s", error->message);
668 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
672 tp_chat_list_properties_cb (TpProxy *proxy,
673 const GPtrArray *properties,
678 EmpathyTpChatPriv *priv = GET_PRIV (chat);
682 priv->had_properties_list = TRUE;
685 DEBUG ("Error listing properties: %s", error->message);
689 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
690 priv->properties = g_ptr_array_sized_new (properties->len);
691 for (i = 0; i < properties->len; i++) {
692 GValueArray *prop_struct;
693 TpChatProperty *property;
695 prop_struct = g_ptr_array_index (properties, i);
696 property = g_slice_new0 (TpChatProperty);
697 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
698 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
699 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
701 DEBUG ("Adding property name=%s id=%d flags=%d",
702 property->name, property->id, property->flags);
703 g_ptr_array_add (priv->properties, property);
704 if (property->flags & TP_PROPERTY_FLAG_READ) {
705 g_array_append_val (ids, property->id);
709 tp_cli_properties_interface_call_get_properties (proxy, -1,
711 tp_chat_get_properties_cb,
715 g_array_free (ids, TRUE);
719 empathy_tp_chat_set_property (EmpathyTpChat *chat,
723 EmpathyTpChatPriv *priv = GET_PRIV (chat);
724 TpChatProperty *property;
727 g_return_if_fail (priv->ready);
729 for (i = 0; i < priv->properties->len; i++) {
730 property = g_ptr_array_index (priv->properties, i);
731 if (!tp_strdiff (property->name, name)) {
732 GPtrArray *properties;
735 GValue dest_value = {0, };
737 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
741 g_value_init (&id, G_TYPE_UINT);
742 g_value_init (&dest_value, G_TYPE_VALUE);
743 g_value_set_uint (&id, property->id);
744 g_value_set_boxed (&dest_value, value);
746 prop = g_value_array_new (2);
747 g_value_array_append (prop, &id);
748 g_value_array_append (prop, &dest_value);
750 properties = g_ptr_array_sized_new (1);
751 g_ptr_array_add (properties, prop);
753 DEBUG ("Set property %s", name);
754 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
756 (tp_cli_properties_interface_callback_for_set_properties)
758 "Seting property", NULL,
761 g_ptr_array_free (properties, TRUE);
762 g_value_array_free (prop);
770 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
772 EmpathyTpChatPriv *priv = GET_PRIV (chat);
773 TpConnection *connection;
774 guint handle, handle_type;
776 DEBUG ("Channel ready");
778 g_object_get (priv->channel,
779 "connection", &connection,
781 "handle_type", &handle_type,
784 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
788 handles = g_array_new (FALSE, FALSE, sizeof (guint));
789 g_array_append_val (handles, handle);
790 tp_cli_connection_run_inspect_handles (connection, -1,
791 handle_type, handles,
794 g_array_free (handles, TRUE);
798 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
799 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
802 g_object_notify (G_OBJECT (chat), "remote-contact");
805 if (tp_proxy_has_interface_by_id (priv->channel,
806 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
807 priv->group = empathy_tp_group_new (priv->channel);
809 g_signal_connect (priv->group, "member-added",
810 G_CALLBACK (tp_chat_member_added_cb),
812 g_signal_connect (priv->group, "member-removed",
813 G_CALLBACK (tp_chat_member_removed_cb),
815 g_signal_connect (priv->group, "local-pending",
816 G_CALLBACK (tp_chat_local_pending_cb),
818 empathy_run_until_ready (priv->group);
820 priv->members_count = 2;
823 if (tp_proxy_has_interface_by_id (priv->channel,
824 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
825 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
826 tp_chat_list_properties_cb,
829 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
830 tp_chat_properties_changed_cb,
832 G_OBJECT (chat), NULL);
833 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
834 tp_chat_property_flags_changed_cb,
836 G_OBJECT (chat), NULL);
839 priv->listing_pending_messages = TRUE;
840 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
842 tp_chat_list_pending_messages_cb,
846 tp_cli_channel_type_text_connect_to_received (priv->channel,
849 G_OBJECT (chat), NULL);
850 tp_cli_channel_type_text_connect_to_sent (priv->channel,
853 G_OBJECT (chat), NULL);
854 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
855 tp_chat_send_error_cb,
857 G_OBJECT (chat), NULL);
858 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
859 tp_chat_state_changed_cb,
861 G_OBJECT (chat), NULL);
862 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
863 tp_chat_state_changed_cb,
865 G_OBJECT (chat), NULL);
868 g_object_notify (G_OBJECT (chat), "ready");
872 tp_chat_finalize (GObject *object)
874 EmpathyTpChatPriv *priv = GET_PRIV (object);
877 DEBUG ("Finalize: %p", object);
879 if (priv->acknowledge && priv->channel) {
880 DEBUG ("Closing channel...");
881 tp_cli_channel_call_close (priv->channel, -1,
883 "closing channel", NULL,
888 g_signal_handlers_disconnect_by_func (priv->channel,
889 tp_chat_invalidated_cb,
891 g_object_unref (priv->channel);
894 if (priv->properties) {
895 for (i = 0; i < priv->properties->len; i++) {
896 TpChatProperty *property;
898 property = g_ptr_array_index (priv->properties, i);
899 g_free (property->name);
900 if (property->value) {
901 tp_g_value_slice_free (property->value);
903 g_slice_free (TpChatProperty, property);
905 g_ptr_array_free (priv->properties, TRUE);
908 if (priv->remote_contact) {
909 g_object_unref (priv->remote_contact);
912 g_object_unref (priv->group);
915 if (priv->contact_monitor) {
916 g_object_unref (priv->contact_monitor);
919 g_object_unref (priv->factory);
920 g_object_unref (priv->user);
921 g_object_unref (priv->account);
924 if (priv->message_queue) {
925 EmpathyMessage *message;
926 EmpathyContact *contact;
928 message = priv->message_queue->data;
929 contact = empathy_message_get_sender (message);
930 g_signal_handlers_disconnect_by_func (contact,
931 tp_chat_sender_ready_notify_cb,
934 g_slist_foreach (priv->message_queue, (GFunc) g_object_unref, NULL);
935 g_slist_free (priv->message_queue);
937 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
941 tp_chat_constructor (GType type,
943 GObjectConstructParam *props)
946 EmpathyTpChatPriv *priv;
947 gboolean channel_ready;
949 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
951 priv = GET_PRIV (chat);
952 priv->account = empathy_channel_get_account (priv->channel);
953 priv->factory = empathy_contact_factory_new ();
954 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
956 g_signal_connect (priv->channel, "invalidated",
957 G_CALLBACK (tp_chat_invalidated_cb),
960 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
962 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
964 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
965 G_CALLBACK (tp_chat_channel_ready_cb),
973 tp_chat_get_property (GObject *object,
978 EmpathyTpChatPriv *priv = GET_PRIV (object);
982 g_value_set_object (value, priv->channel);
984 case PROP_ACKNOWLEDGE:
985 g_value_set_boolean (value, priv->acknowledge);
987 case PROP_REMOTE_CONTACT:
988 g_value_set_object (value, priv->remote_contact);
991 g_value_set_boolean (value, priv->ready);
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1000 tp_chat_set_property (GObject *object,
1002 const GValue *value,
1005 EmpathyTpChatPriv *priv = GET_PRIV (object);
1009 priv->channel = g_object_ref (g_value_get_object (value));
1011 case PROP_ACKNOWLEDGE:
1012 priv->acknowledge = g_value_get_boolean (value);
1015 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1021 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
1023 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1025 object_class->finalize = tp_chat_finalize;
1026 object_class->constructor = tp_chat_constructor;
1027 object_class->get_property = tp_chat_get_property;
1028 object_class->set_property = tp_chat_set_property;
1030 g_object_class_install_property (object_class,
1032 g_param_spec_object ("channel",
1033 "telepathy channel",
1034 "The text channel for the chat",
1037 G_PARAM_CONSTRUCT_ONLY));
1038 g_object_class_install_property (object_class,
1040 g_param_spec_boolean ("acknowledge",
1041 "acknowledge messages",
1042 "Wheter or not received messages should be acknowledged",
1044 G_PARAM_READWRITE));
1046 g_object_class_install_property (object_class,
1047 PROP_REMOTE_CONTACT,
1048 g_param_spec_object ("remote-contact",
1049 "The remote contact",
1050 "The remote contact if there is no group iface on the channel",
1051 EMPATHY_TYPE_CONTACT,
1053 g_object_class_install_property (object_class,
1055 g_param_spec_boolean ("ready",
1056 "Is the object ready",
1057 "This object can't be used until this becomes true",
1062 signals[MESSAGE_RECEIVED] =
1063 g_signal_new ("message-received",
1064 G_TYPE_FROM_CLASS (klass),
1068 g_cclosure_marshal_VOID__OBJECT,
1070 1, EMPATHY_TYPE_MESSAGE);
1072 signals[SEND_ERROR] =
1073 g_signal_new ("send-error",
1074 G_TYPE_FROM_CLASS (klass),
1078 _empathy_marshal_VOID__OBJECT_UINT,
1080 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1082 signals[CHAT_STATE_CHANGED] =
1083 g_signal_new ("chat-state-changed",
1084 G_TYPE_FROM_CLASS (klass),
1088 _empathy_marshal_VOID__OBJECT_UINT,
1090 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1092 signals[PROPERTY_CHANGED] =
1093 g_signal_new ("property-changed",
1094 G_TYPE_FROM_CLASS (klass),
1098 _empathy_marshal_VOID__STRING_BOXED,
1100 2, G_TYPE_STRING, G_TYPE_VALUE);
1103 g_signal_new ("destroy",
1104 G_TYPE_FROM_CLASS (klass),
1108 g_cclosure_marshal_VOID__VOID,
1112 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1116 empathy_tp_chat_init (EmpathyTpChat *chat)
1118 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1119 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1122 priv->contact_monitor = NULL;
1126 tp_chat_iface_init (EmpathyContactListIface *iface)
1128 iface->add = tp_chat_add;
1129 iface->remove = tp_chat_remove;
1130 iface->get_members = tp_chat_get_members;
1131 iface->get_monitor = tp_chat_get_monitor;
1135 empathy_tp_chat_new (TpChannel *channel)
1137 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1143 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1145 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1147 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1148 g_return_val_if_fail (priv->ready, NULL);
1154 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1156 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1158 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1160 return priv->remote_contact;
1164 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1166 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1168 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1170 return priv->account;
1174 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1176 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1178 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1180 return priv->channel;
1184 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1186 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1188 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1194 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1196 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1198 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1200 return priv->members_count;
1204 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
1205 gboolean acknowledge)
1207 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1209 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1211 priv->acknowledge = acknowledge;
1212 g_object_notify (G_OBJECT (chat), "acknowledge");
1216 empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
1218 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1220 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1221 g_return_if_fail (priv->ready);
1223 if (priv->listing_pending_messages) {
1227 priv->listing_pending_messages = TRUE;
1228 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
1230 tp_chat_list_pending_messages_cb,
1236 empathy_tp_chat_send (EmpathyTpChat *chat,
1237 EmpathyMessage *message)
1239 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1240 const gchar *message_body;
1241 TpChannelTextMessageType message_type;
1243 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1244 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1245 g_return_if_fail (priv->ready);
1247 message_body = empathy_message_get_body (message);
1248 message_type = empathy_message_get_tptype (message);
1250 DEBUG ("Sending message: %s", message_body);
1251 tp_cli_channel_type_text_call_send (priv->channel, -1,
1255 g_object_ref (message),
1256 (GDestroyNotify) g_object_unref,
1261 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1262 TpChannelChatState state)
1264 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1266 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1267 g_return_if_fail (priv->ready);
1269 DEBUG ("Set state: %d", state);
1270 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1273 "setting chat state",