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-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CHAT
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
43 struct _EmpathyTpChatPriv {
44 EmpathyContactFactory *factory;
46 EmpathyContact *remote_contact;
47 EmpathyTpGroup *group;
52 gboolean had_pending_messages;
53 GSList *message_queue;
54 gboolean had_properties_list;
55 GPtrArray *properties;
63 TpPropertyFlags flags;
67 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
68 static void empathy_tp_chat_init (EmpathyTpChat *chat);
69 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 DEBUG ("Channel invalidated: %s", message);
102 g_signal_emit (chat, signals[DESTROY], 0);
106 tp_chat_async_cb (TpChannel *proxy,
109 GObject *weak_object)
112 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
117 tp_chat_member_added_cb (EmpathyTpGroup *group,
118 EmpathyContact *contact,
119 EmpathyContact *actor,
121 const gchar *message,
124 EmpathyTpChatPriv *priv = GET_PRIV (chat);
125 guint handle_type = 0;
127 priv->members_count++;
128 g_signal_emit_by_name (chat, "members-changed",
129 contact, actor, reason, message,
132 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
133 if (handle_type == TP_HANDLE_TYPE_ROOM) {
137 if (priv->members_count > 2 && priv->remote_contact) {
138 /* We now have more than 2 members, this is not a p2p chat
139 * anymore. Remove the remote-contact as it makes no sense, the
140 * EmpathyContactList interface must be used now. */
141 g_object_unref (priv->remote_contact);
142 priv->remote_contact = NULL;
143 g_object_notify (G_OBJECT (chat), "remote-contact");
145 if (priv->members_count <= 2 && !priv->remote_contact &&
146 !empathy_contact_is_user (contact)) {
147 /* This is a p2p chat, if it's not ourself that means this is
148 * the remote contact with who we are chatting. This is to
149 * avoid forcing the usage of the EmpathyContactList interface
151 priv->remote_contact = g_object_ref (contact);
152 g_object_notify (G_OBJECT (chat), "remote-contact");
157 tp_chat_member_removed_cb (EmpathyTpGroup *group,
158 EmpathyContact *contact,
159 EmpathyContact *actor,
161 const gchar *message,
164 EmpathyTpChatPriv *priv = GET_PRIV (chat);
165 guint handle_type = 0;
167 priv->members_count--;
168 g_signal_emit_by_name (chat, "members-changed",
169 contact, actor, reason, message,
172 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
173 if (handle_type == TP_HANDLE_TYPE_ROOM) {
177 if (priv->members_count <= 2 && !priv->remote_contact) {
180 /* We are not a MUC anymore, get the remote contact back */
181 members = empathy_tp_group_get_members (group);
182 for (l = members; l; l = l->next) {
183 if (!empathy_contact_is_user (l->data)) {
184 priv->remote_contact = g_object_ref (l->data);
185 g_object_notify (G_OBJECT (chat), "remote-contact");
189 g_list_foreach (members, (GFunc) g_object_unref, NULL);
190 g_list_free (members);
195 tp_chat_local_pending_cb (EmpathyTpGroup *group,
196 EmpathyContact *contact,
197 EmpathyContact *actor,
199 const gchar *message,
202 g_signal_emit_by_name (chat, "pendings-changed",
203 contact, actor, reason, message,
208 tp_chat_add (EmpathyContactList *list,
209 EmpathyContact *contact,
210 const gchar *message)
212 EmpathyTpChatPriv *priv = GET_PRIV (list);
214 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
215 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
218 empathy_tp_group_add_member (priv->group, contact, message);
223 tp_chat_remove (EmpathyContactList *list,
224 EmpathyContact *contact,
225 const gchar *message)
227 EmpathyTpChatPriv *priv = GET_PRIV (list);
229 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
230 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
233 empathy_tp_group_remove_member (priv->group, contact, message);
238 tp_chat_get_members (EmpathyContactList *list)
240 EmpathyTpChatPriv *priv = GET_PRIV (list);
241 GList *members = NULL;
243 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
246 members = empathy_tp_group_get_members (priv->group);
248 members = g_list_prepend (members, g_object_ref (priv->user));
249 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
255 static EmpathyMessage *
256 tp_chat_build_message (EmpathyTpChat *chat,
260 const gchar *message_body)
262 EmpathyTpChatPriv *priv;
263 EmpathyMessage *message;
264 EmpathyContact *sender;
266 priv = GET_PRIV (chat);
268 if (from_handle == 0) {
269 sender = g_object_ref (priv->user);
271 sender = empathy_contact_factory_get_from_handle (priv->factory,
276 message = empathy_message_new (message_body);
277 empathy_message_set_type (message, type);
278 empathy_message_set_sender (message, sender);
279 empathy_message_set_receiver (message, priv->user);
280 empathy_message_set_timestamp (message, timestamp);
282 g_object_unref (sender);
288 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
289 GParamSpec *param_spec,
292 EmpathyTpChatPriv *priv = GET_PRIV (chat);
293 EmpathyMessage *message;
294 EmpathyContactReady ready;
295 EmpathyContact *sender;
296 gboolean removed = FALSE;
298 /* Emit all messages queued until we find a message with not
299 * ready sender. When leaving this loop, sender is the first not ready
300 * contact queued and removed tells if at least one message got removed
302 while (priv->message_queue) {
303 message = priv->message_queue->data;
304 sender = empathy_message_get_sender (message);
305 ready = empathy_contact_get_ready (sender);
307 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
311 DEBUG ("Queued message ready");
312 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
313 priv->message_queue = g_slist_remove (priv->message_queue,
315 g_object_unref (message);
320 g_signal_handlers_disconnect_by_func (contact,
321 tp_chat_sender_ready_notify_cb,
324 if (priv->message_queue) {
325 g_signal_connect (sender, "notify::ready",
326 G_CALLBACK (tp_chat_sender_ready_notify_cb),
333 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
334 EmpathyMessage *message)
336 EmpathyTpChatPriv *priv = GET_PRIV (chat);
337 EmpathyContact *sender;
338 EmpathyContactReady ready;
340 if (priv->message_queue != NULL) {
341 DEBUG ("Message queue not empty");
342 priv->message_queue = g_slist_append (priv->message_queue,
343 g_object_ref (message));
347 sender = empathy_message_get_sender (message);
348 ready = empathy_contact_get_ready (sender);
349 if (ready & EMPATHY_CONTACT_READY_NAME) {
350 DEBUG ("Message queue empty and sender ready");
351 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
355 DEBUG ("Sender not ready");
356 priv->message_queue = g_slist_append (priv->message_queue,
357 g_object_ref (message));
358 g_signal_connect (sender, "notify::ready",
359 G_CALLBACK (tp_chat_sender_ready_notify_cb),
364 tp_chat_received_cb (TpChannel *channel,
370 const gchar *message_body,
374 EmpathyTpChatPriv *priv = GET_PRIV (chat);
375 EmpathyMessage *message;
377 if (!priv->had_pending_messages) {
381 DEBUG ("Message received: %s", message_body);
383 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
389 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
390 g_object_unref (message);
392 if (priv->acknowledge) {
395 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
396 g_array_append_val (message_ids, message_id);
397 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
401 "acknowledging received message",
404 g_array_free (message_ids, TRUE);
409 tp_chat_sent_cb (TpChannel *channel,
412 const gchar *message_body,
416 EmpathyMessage *message;
418 DEBUG ("Message sent: %s", message_body);
420 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
426 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
427 g_object_unref (message);
431 tp_chat_send_error_cb (TpChannel *channel,
435 const gchar *message_body,
439 EmpathyMessage *message;
441 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
443 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
449 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
450 g_object_unref (message);
454 tp_chat_state_changed_cb (TpChannel *channel,
460 EmpathyTpChatPriv *priv = GET_PRIV (chat);
461 EmpathyContact *contact;
463 contact = empathy_contact_factory_get_from_handle (priv->factory,
467 DEBUG ("Chat state changed for %s (%d): %d",
468 empathy_contact_get_name (contact), handle, state);
470 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
471 g_object_unref (contact);
475 tp_chat_list_pending_messages_cb (TpChannel *channel,
476 const GPtrArray *messages_list,
481 EmpathyTpChatPriv *priv = GET_PRIV (chat);
483 GArray *message_ids = NULL;
485 priv->had_pending_messages = TRUE;
488 DEBUG ("Error listing pending messages: %s", error->message);
492 if (priv->acknowledge) {
493 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
497 for (i = 0; i < messages_list->len; i++) {
498 EmpathyMessage *message;
499 GValueArray *message_struct;
500 const gchar *message_body;
507 message_struct = g_ptr_array_index (messages_list, i);
509 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
510 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
511 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
512 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
513 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
514 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
516 DEBUG ("Message pending: %s", message_body);
519 g_array_append_val (message_ids, message_id);
522 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
528 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
529 g_object_unref (message);
533 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
537 "acknowledging pending messages",
540 g_array_free (message_ids, TRUE);
545 tp_chat_property_flags_changed_cb (TpProxy *proxy,
546 const GPtrArray *properties,
550 EmpathyTpChatPriv *priv = GET_PRIV (chat);
553 if (!priv->had_properties_list || !properties) {
557 for (i = 0; i < properties->len; i++) {
558 GValueArray *prop_struct;
559 TpChatProperty *property;
563 prop_struct = g_ptr_array_index (properties, i);
564 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
565 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
567 for (j = 0; j < priv->properties->len; j++) {
568 property = g_ptr_array_index (priv->properties, j);
569 if (property->id == id) {
570 property->flags = flags;
571 DEBUG ("property %s flags changed: %d",
572 property->name, property->flags);
580 tp_chat_properties_changed_cb (TpProxy *proxy,
581 const GPtrArray *properties,
585 EmpathyTpChatPriv *priv = GET_PRIV (chat);
588 if (!priv->had_properties_list || !properties) {
592 for (i = 0; i < properties->len; i++) {
593 GValueArray *prop_struct;
594 TpChatProperty *property;
598 prop_struct = g_ptr_array_index (properties, i);
599 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
600 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
602 for (j = 0; j < priv->properties->len; j++) {
603 property = g_ptr_array_index (priv->properties, j);
604 if (property->id == id) {
605 if (property->value) {
606 g_value_copy (src_value, property->value);
608 property->value = tp_g_value_slice_dup (src_value);
611 DEBUG ("property %s changed", property->name);
612 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
613 property->name, property->value);
621 tp_chat_get_properties_cb (TpProxy *proxy,
622 const GPtrArray *properties,
628 DEBUG ("Error getting properties: %s", error->message);
632 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
636 tp_chat_list_properties_cb (TpProxy *proxy,
637 const GPtrArray *properties,
642 EmpathyTpChatPriv *priv = GET_PRIV (chat);
646 priv->had_properties_list = TRUE;
649 DEBUG ("Error listing properties: %s", error->message);
653 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
654 priv->properties = g_ptr_array_sized_new (properties->len);
655 for (i = 0; i < properties->len; i++) {
656 GValueArray *prop_struct;
657 TpChatProperty *property;
659 prop_struct = g_ptr_array_index (properties, i);
660 property = g_slice_new0 (TpChatProperty);
661 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
662 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
663 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
665 DEBUG ("Adding property name=%s id=%d flags=%d",
666 property->name, property->id, property->flags);
667 g_ptr_array_add (priv->properties, property);
668 if (property->flags & TP_PROPERTY_FLAG_READ) {
669 g_array_append_val (ids, property->id);
673 tp_cli_properties_interface_call_get_properties (proxy, -1,
675 tp_chat_get_properties_cb,
679 g_array_free (ids, TRUE);
683 empathy_tp_chat_set_property (EmpathyTpChat *chat,
687 EmpathyTpChatPriv *priv = GET_PRIV (chat);
688 TpChatProperty *property;
691 g_return_if_fail (priv->ready);
693 for (i = 0; i < priv->properties->len; i++) {
694 property = g_ptr_array_index (priv->properties, i);
695 if (!tp_strdiff (property->name, name)) {
696 GPtrArray *properties;
699 GValue dest_value = {0, };
701 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
705 g_value_init (&id, G_TYPE_UINT);
706 g_value_init (&dest_value, G_TYPE_VALUE);
707 g_value_set_uint (&id, property->id);
708 g_value_set_boxed (&dest_value, value);
710 prop = g_value_array_new (2);
711 g_value_array_append (prop, &id);
712 g_value_array_append (prop, &dest_value);
714 properties = g_ptr_array_sized_new (1);
715 g_ptr_array_add (properties, prop);
717 DEBUG ("Set property %s", name);
718 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
720 (tp_cli_properties_interface_callback_for_set_properties)
722 "Seting property", NULL,
725 g_ptr_array_free (properties, TRUE);
726 g_value_array_free (prop);
734 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
736 EmpathyTpChatPriv *priv = GET_PRIV (chat);
737 TpConnection *connection;
738 guint handle, handle_type;
740 DEBUG ("Channel ready");
742 g_object_get (priv->channel,
743 "connection", &connection,
745 "handle_type", &handle_type,
748 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
752 handles = g_array_new (FALSE, FALSE, sizeof (guint));
753 g_array_append_val (handles, handle);
754 tp_cli_connection_run_inspect_handles (connection, -1,
755 handle_type, handles,
758 g_array_free (handles, TRUE);
762 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
763 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
766 g_object_notify (G_OBJECT (chat), "remote-contact");
769 if (tp_proxy_has_interface_by_id (priv->channel,
770 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
771 priv->group = empathy_tp_group_new (priv->channel);
773 g_signal_connect (priv->group, "member-added",
774 G_CALLBACK (tp_chat_member_added_cb),
776 g_signal_connect (priv->group, "member-removed",
777 G_CALLBACK (tp_chat_member_removed_cb),
779 g_signal_connect (priv->group, "local-pending",
780 G_CALLBACK (tp_chat_local_pending_cb),
782 empathy_run_until_ready (priv->group);
784 priv->members_count = 2;
787 if (tp_proxy_has_interface_by_id (priv->channel,
788 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
789 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
790 tp_chat_list_properties_cb,
793 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
794 tp_chat_properties_changed_cb,
796 G_OBJECT (chat), NULL);
797 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
798 tp_chat_property_flags_changed_cb,
800 G_OBJECT (chat), NULL);
803 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
805 tp_chat_list_pending_messages_cb,
809 tp_cli_channel_type_text_connect_to_received (priv->channel,
812 G_OBJECT (chat), NULL);
813 tp_cli_channel_type_text_connect_to_sent (priv->channel,
816 G_OBJECT (chat), NULL);
817 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
818 tp_chat_send_error_cb,
820 G_OBJECT (chat), NULL);
821 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
822 tp_chat_state_changed_cb,
824 G_OBJECT (chat), NULL);
825 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
826 tp_chat_state_changed_cb,
828 G_OBJECT (chat), NULL);
831 g_object_notify (G_OBJECT (chat), "ready");
835 tp_chat_finalize (GObject *object)
837 EmpathyTpChatPriv *priv = GET_PRIV (object);
840 if (priv->acknowledge && priv->channel) {
841 DEBUG ("Closing channel...");
842 tp_cli_channel_call_close (priv->channel, -1,
844 "closing channel", NULL,
849 g_signal_handlers_disconnect_by_func (priv->channel,
850 tp_chat_invalidated_cb,
852 g_object_unref (priv->channel);
855 if (priv->properties) {
856 for (i = 0; i < priv->properties->len; i++) {
857 TpChatProperty *property;
859 property = g_ptr_array_index (priv->properties, i);
860 g_free (property->name);
861 if (property->value) {
862 tp_g_value_slice_free (property->value);
864 g_slice_free (TpChatProperty, property);
866 g_ptr_array_free (priv->properties, TRUE);
869 if (priv->remote_contact) {
870 g_object_unref (priv->remote_contact);
873 g_object_unref (priv->group);
876 g_object_unref (priv->factory);
877 g_object_unref (priv->user);
878 g_object_unref (priv->account);
881 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
885 tp_chat_constructor (GType type,
887 GObjectConstructParam *props)
890 EmpathyTpChatPriv *priv;
891 gboolean channel_ready;
893 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
895 priv = GET_PRIV (chat);
896 priv->account = empathy_channel_get_account (priv->channel);
897 priv->factory = empathy_contact_factory_new ();
898 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
900 g_signal_connect (priv->channel, "invalidated",
901 G_CALLBACK (tp_chat_invalidated_cb),
904 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
906 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
908 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
909 G_CALLBACK (tp_chat_channel_ready_cb),
917 tp_chat_get_property (GObject *object,
922 EmpathyTpChatPriv *priv = GET_PRIV (object);
926 g_value_set_object (value, priv->channel);
928 case PROP_ACKNOWLEDGE:
929 g_value_set_boolean (value, priv->acknowledge);
931 case PROP_REMOTE_CONTACT:
932 g_value_set_object (value, priv->remote_contact);
935 g_value_set_boolean (value, priv->ready);
938 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
944 tp_chat_set_property (GObject *object,
949 EmpathyTpChatPriv *priv = GET_PRIV (object);
953 priv->channel = g_object_ref (g_value_get_object (value));
955 case PROP_ACKNOWLEDGE:
956 priv->acknowledge = g_value_get_boolean (value);
959 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
965 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
967 GObjectClass *object_class = G_OBJECT_CLASS (klass);
969 object_class->finalize = tp_chat_finalize;
970 object_class->constructor = tp_chat_constructor;
971 object_class->get_property = tp_chat_get_property;
972 object_class->set_property = tp_chat_set_property;
974 g_object_class_install_property (object_class,
976 g_param_spec_object ("channel",
978 "The text channel for the chat",
981 G_PARAM_CONSTRUCT_ONLY));
982 g_object_class_install_property (object_class,
984 g_param_spec_boolean ("acknowledge",
985 "acknowledge messages",
986 "Wheter or not received messages should be acknowledged",
991 g_object_class_install_property (object_class,
993 g_param_spec_object ("remote-contact",
994 "The remote contact",
995 "The remote contact if there is no group iface on the channel",
996 EMPATHY_TYPE_CONTACT,
998 g_object_class_install_property (object_class,
1000 g_param_spec_boolean ("ready",
1001 "Is the object ready",
1002 "This object can't be used until this becomes true",
1007 signals[MESSAGE_RECEIVED] =
1008 g_signal_new ("message-received",
1009 G_TYPE_FROM_CLASS (klass),
1013 g_cclosure_marshal_VOID__OBJECT,
1015 1, EMPATHY_TYPE_MESSAGE);
1017 signals[SEND_ERROR] =
1018 g_signal_new ("send-error",
1019 G_TYPE_FROM_CLASS (klass),
1023 _empathy_marshal_VOID__OBJECT_UINT,
1025 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1027 signals[CHAT_STATE_CHANGED] =
1028 g_signal_new ("chat-state-changed",
1029 G_TYPE_FROM_CLASS (klass),
1033 _empathy_marshal_VOID__OBJECT_UINT,
1035 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1037 signals[PROPERTY_CHANGED] =
1038 g_signal_new ("property-changed",
1039 G_TYPE_FROM_CLASS (klass),
1043 _empathy_marshal_VOID__STRING_BOXED,
1045 2, G_TYPE_STRING, G_TYPE_VALUE);
1048 g_signal_new ("destroy",
1049 G_TYPE_FROM_CLASS (klass),
1053 g_cclosure_marshal_VOID__VOID,
1057 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1061 empathy_tp_chat_init (EmpathyTpChat *chat)
1066 tp_chat_iface_init (EmpathyContactListIface *iface)
1068 iface->add = tp_chat_add;
1069 iface->remove = tp_chat_remove;
1070 iface->get_members = tp_chat_get_members;
1074 empathy_tp_chat_new (TpChannel *channel,
1075 gboolean acknowledge)
1077 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1079 "acknowledge", acknowledge,
1084 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1086 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1088 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1089 g_return_val_if_fail (priv->ready, NULL);
1095 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1097 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1099 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1101 return priv->remote_contact;
1105 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1107 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1109 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1111 return priv->account;
1115 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1117 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1119 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1121 return priv->channel;
1125 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1127 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1129 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1135 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1137 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1139 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1141 return priv->members_count;
1145 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
1146 gboolean acknowledge)
1148 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1150 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1152 priv->acknowledge = acknowledge;
1153 g_object_notify (G_OBJECT (chat), "acknowledge");
1157 empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
1159 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1161 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1162 g_return_if_fail (priv->ready);
1164 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
1166 tp_chat_list_pending_messages_cb,
1172 empathy_tp_chat_send (EmpathyTpChat *chat,
1173 EmpathyMessage *message)
1175 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1176 const gchar *message_body;
1177 EmpathyMessageType message_type;
1179 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1180 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1181 g_return_if_fail (priv->ready);
1183 message_body = empathy_message_get_body (message);
1184 message_type = empathy_message_get_type (message);
1186 DEBUG ("Sending message: %s", message_body);
1187 tp_cli_channel_type_text_call_send (priv->channel, -1,
1191 "sending message", NULL,
1196 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1197 TpChannelChatState state)
1199 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1201 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1202 g_return_if_fail (priv->ready);
1204 DEBUG ("Set state: %d", state);
1205 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1208 "setting chat state",