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) EMPATHY_GET_PRIV (obj, EmpathyTpChat)
42 EmpathyContactFactory *factory;
44 EmpathyContact *remote_contact;
45 EmpathyTpGroup *group;
50 gboolean listing_pending_messages;
51 GSList *message_queue;
52 gboolean had_properties_list;
53 GPtrArray *properties;
61 TpPropertyFlags flags;
65 static void tp_chat_iface_init (EmpathyContactListIface *iface);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 tp_chat_invalidated_cb (TpProxy *proxy,
97 DEBUG ("Channel invalidated: %s", message);
98 g_signal_emit (chat, signals[DESTROY], 0);
102 tp_chat_async_cb (TpChannel *proxy,
105 GObject *weak_object)
108 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
113 tp_chat_member_added_cb (EmpathyTpGroup *group,
114 EmpathyContact *contact,
115 EmpathyContact *actor,
117 const gchar *message,
120 EmpathyTpChatPriv *priv = GET_PRIV (chat);
121 guint handle_type = 0;
123 priv->members_count++;
124 g_signal_emit_by_name (chat, "members-changed",
125 contact, actor, reason, message,
128 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
129 if (handle_type == TP_HANDLE_TYPE_ROOM) {
133 if (priv->members_count > 2 && priv->remote_contact) {
134 /* We now have more than 2 members, this is not a p2p chat
135 * anymore. Remove the remote-contact as it makes no sense, the
136 * EmpathyContactList interface must be used now. */
137 g_object_unref (priv->remote_contact);
138 priv->remote_contact = NULL;
139 g_object_notify (G_OBJECT (chat), "remote-contact");
141 if (priv->members_count <= 2 && !priv->remote_contact &&
142 !empathy_contact_is_user (contact)) {
143 /* This is a p2p chat, if it's not ourself that means this is
144 * the remote contact with who we are chatting. This is to
145 * avoid forcing the usage of the EmpathyContactList interface
147 priv->remote_contact = g_object_ref (contact);
148 g_object_notify (G_OBJECT (chat), "remote-contact");
153 tp_chat_member_removed_cb (EmpathyTpGroup *group,
154 EmpathyContact *contact,
155 EmpathyContact *actor,
157 const gchar *message,
160 EmpathyTpChatPriv *priv = GET_PRIV (chat);
161 guint handle_type = 0;
163 priv->members_count--;
164 g_signal_emit_by_name (chat, "members-changed",
165 contact, actor, reason, message,
168 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
169 if (handle_type == TP_HANDLE_TYPE_ROOM) {
173 if (priv->members_count <= 2 && !priv->remote_contact) {
176 /* We are not a MUC anymore, get the remote contact back */
177 members = empathy_tp_group_get_members (group);
178 for (l = members; l; l = l->next) {
179 if (!empathy_contact_is_user (l->data)) {
180 priv->remote_contact = g_object_ref (l->data);
181 g_object_notify (G_OBJECT (chat), "remote-contact");
185 g_list_foreach (members, (GFunc) g_object_unref, NULL);
186 g_list_free (members);
191 tp_chat_local_pending_cb (EmpathyTpGroup *group,
192 EmpathyContact *contact,
193 EmpathyContact *actor,
195 const gchar *message,
198 g_signal_emit_by_name (chat, "pendings-changed",
199 contact, actor, reason, message,
204 tp_chat_add (EmpathyContactList *list,
205 EmpathyContact *contact,
206 const gchar *message)
208 EmpathyTpChatPriv *priv = GET_PRIV (list);
210 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
211 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
214 empathy_tp_group_add_member (priv->group, contact, message);
219 tp_chat_remove (EmpathyContactList *list,
220 EmpathyContact *contact,
221 const gchar *message)
223 EmpathyTpChatPriv *priv = GET_PRIV (list);
225 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
226 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
229 empathy_tp_group_remove_member (priv->group, contact, message);
234 tp_chat_get_members (EmpathyContactList *list)
236 EmpathyTpChatPriv *priv = GET_PRIV (list);
237 GList *members = NULL;
239 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
242 members = empathy_tp_group_get_members (priv->group);
244 members = g_list_prepend (members, g_object_ref (priv->user));
245 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
251 static EmpathyMessage *
252 tp_chat_build_message (EmpathyTpChat *chat,
256 const gchar *message_body)
258 EmpathyTpChatPriv *priv;
259 EmpathyMessage *message;
260 EmpathyContact *sender;
262 priv = GET_PRIV (chat);
264 if (from_handle == 0) {
265 sender = g_object_ref (priv->user);
267 sender = empathy_contact_factory_get_from_handle (priv->factory,
272 message = empathy_message_new (message_body);
273 empathy_message_set_tptype (message, type);
274 empathy_message_set_sender (message, sender);
275 empathy_message_set_receiver (message, priv->user);
276 empathy_message_set_timestamp (message, timestamp);
278 g_object_unref (sender);
284 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
285 GParamSpec *param_spec,
288 EmpathyTpChatPriv *priv = GET_PRIV (chat);
289 EmpathyMessage *message;
290 EmpathyContactReady ready;
291 EmpathyContact *sender = NULL;
292 gboolean removed = FALSE;
294 /* Emit all messages queued until we find a message with not
295 * ready sender. When leaving this loop, sender is the first not ready
296 * contact queued and removed tells if at least one message got removed
298 while (priv->message_queue) {
299 message = priv->message_queue->data;
300 sender = empathy_message_get_sender (message);
301 ready = empathy_contact_get_ready (sender);
303 if ((ready & EMPATHY_CONTACT_READY_NAME) == 0 ||
304 (ready & EMPATHY_CONTACT_READY_ID) == 0) {
308 DEBUG ("Queued message ready");
309 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
310 priv->message_queue = g_slist_remove (priv->message_queue,
312 g_object_unref (message);
317 g_signal_handlers_disconnect_by_func (contact,
318 tp_chat_sender_ready_notify_cb,
321 if (priv->message_queue) {
322 g_signal_connect (sender, "notify::ready",
323 G_CALLBACK (tp_chat_sender_ready_notify_cb),
330 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
331 EmpathyMessage *message)
333 EmpathyTpChatPriv *priv = GET_PRIV (chat);
334 EmpathyContact *sender;
335 EmpathyContactReady ready;
337 if (priv->message_queue != NULL) {
338 DEBUG ("Message queue not empty");
339 priv->message_queue = g_slist_append (priv->message_queue,
340 g_object_ref (message));
344 sender = empathy_message_get_sender (message);
345 ready = empathy_contact_get_ready (sender);
346 if ((ready & EMPATHY_CONTACT_READY_NAME) &&
347 (ready & EMPATHY_CONTACT_READY_ID)) {
348 DEBUG ("Message queue empty and sender ready");
349 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
353 DEBUG ("Sender not ready");
354 priv->message_queue = g_slist_append (priv->message_queue,
355 g_object_ref (message));
356 g_signal_connect (sender, "notify::ready",
357 G_CALLBACK (tp_chat_sender_ready_notify_cb),
362 tp_chat_received_cb (TpChannel *channel,
368 const gchar *message_body,
372 EmpathyTpChatPriv *priv = GET_PRIV (chat);
373 EmpathyMessage *message;
375 if (priv->listing_pending_messages) {
379 DEBUG ("Message received: %s", message_body);
381 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
387 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
388 g_object_unref (message);
390 if (priv->acknowledge) {
393 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
394 g_array_append_val (message_ids, message_id);
395 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
399 "acknowledging received message",
402 g_array_free (message_ids, TRUE);
407 tp_chat_sent_cb (TpChannel *channel,
410 const gchar *message_body,
414 EmpathyMessage *message;
416 DEBUG ("Message sent: %s", message_body);
418 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
424 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
425 g_object_unref (message);
429 tp_chat_send_error_cb (TpChannel *channel,
433 const gchar *message_body,
437 EmpathyMessage *message;
439 DEBUG ("Message sent error: %s (%d)", message_body, error_code);
441 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
447 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
448 g_object_unref (message);
452 tp_chat_send_cb (TpChannel *proxy,
457 EmpathyMessage *message = EMPATHY_MESSAGE (user_data);
460 DEBUG ("Error: %s", error->message);
461 g_signal_emit (chat, signals[SEND_ERROR], 0, message,
462 TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN);
467 tp_chat_state_changed_cb (TpChannel *channel,
473 EmpathyTpChatPriv *priv = GET_PRIV (chat);
474 EmpathyContact *contact;
476 contact = empathy_contact_factory_get_from_handle (priv->factory,
480 DEBUG ("Chat state changed for %s (%d): %d",
481 empathy_contact_get_name (contact), handle, state);
483 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
484 g_object_unref (contact);
488 tp_chat_list_pending_messages_cb (TpChannel *channel,
489 const GPtrArray *messages_list,
494 EmpathyTpChatPriv *priv = GET_PRIV (chat);
496 GArray *message_ids = NULL;
498 priv->listing_pending_messages = FALSE;
501 DEBUG ("Error listing pending messages: %s", error->message);
505 if (priv->acknowledge) {
506 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
510 for (i = 0; i < messages_list->len; i++) {
511 EmpathyMessage *message;
512 GValueArray *message_struct;
513 const gchar *message_body;
520 message_struct = g_ptr_array_index (messages_list, i);
522 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
523 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
524 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
525 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
526 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
527 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
529 DEBUG ("Message pending: %s", message_body);
532 g_array_append_val (message_ids, message_id);
535 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
541 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
542 g_object_unref (message);
546 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
550 "acknowledging pending messages",
553 g_array_free (message_ids, TRUE);
558 tp_chat_property_flags_changed_cb (TpProxy *proxy,
559 const GPtrArray *properties,
563 EmpathyTpChatPriv *priv = GET_PRIV (chat);
566 if (!priv->had_properties_list || !properties) {
570 for (i = 0; i < properties->len; i++) {
571 GValueArray *prop_struct;
572 TpChatProperty *property;
576 prop_struct = g_ptr_array_index (properties, i);
577 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
578 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
580 for (j = 0; j < priv->properties->len; j++) {
581 property = g_ptr_array_index (priv->properties, j);
582 if (property->id == id) {
583 property->flags = flags;
584 DEBUG ("property %s flags changed: %d",
585 property->name, property->flags);
593 tp_chat_properties_changed_cb (TpProxy *proxy,
594 const GPtrArray *properties,
598 EmpathyTpChatPriv *priv = GET_PRIV (chat);
601 if (!priv->had_properties_list || !properties) {
605 for (i = 0; i < properties->len; i++) {
606 GValueArray *prop_struct;
607 TpChatProperty *property;
611 prop_struct = g_ptr_array_index (properties, i);
612 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
613 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
615 for (j = 0; j < priv->properties->len; j++) {
616 property = g_ptr_array_index (priv->properties, j);
617 if (property->id == id) {
618 if (property->value) {
619 g_value_copy (src_value, property->value);
621 property->value = tp_g_value_slice_dup (src_value);
624 DEBUG ("property %s changed", property->name);
625 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
626 property->name, property->value);
634 tp_chat_get_properties_cb (TpProxy *proxy,
635 const GPtrArray *properties,
641 DEBUG ("Error getting properties: %s", error->message);
645 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
649 tp_chat_list_properties_cb (TpProxy *proxy,
650 const GPtrArray *properties,
655 EmpathyTpChatPriv *priv = GET_PRIV (chat);
659 priv->had_properties_list = TRUE;
662 DEBUG ("Error listing properties: %s", error->message);
666 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
667 priv->properties = g_ptr_array_sized_new (properties->len);
668 for (i = 0; i < properties->len; i++) {
669 GValueArray *prop_struct;
670 TpChatProperty *property;
672 prop_struct = g_ptr_array_index (properties, i);
673 property = g_slice_new0 (TpChatProperty);
674 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
675 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
676 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
678 DEBUG ("Adding property name=%s id=%d flags=%d",
679 property->name, property->id, property->flags);
680 g_ptr_array_add (priv->properties, property);
681 if (property->flags & TP_PROPERTY_FLAG_READ) {
682 g_array_append_val (ids, property->id);
686 tp_cli_properties_interface_call_get_properties (proxy, -1,
688 tp_chat_get_properties_cb,
692 g_array_free (ids, TRUE);
696 empathy_tp_chat_set_property (EmpathyTpChat *chat,
700 EmpathyTpChatPriv *priv = GET_PRIV (chat);
701 TpChatProperty *property;
704 g_return_if_fail (priv->ready);
706 for (i = 0; i < priv->properties->len; i++) {
707 property = g_ptr_array_index (priv->properties, i);
708 if (!tp_strdiff (property->name, name)) {
709 GPtrArray *properties;
712 GValue dest_value = {0, };
714 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
718 g_value_init (&id, G_TYPE_UINT);
719 g_value_init (&dest_value, G_TYPE_VALUE);
720 g_value_set_uint (&id, property->id);
721 g_value_set_boxed (&dest_value, value);
723 prop = g_value_array_new (2);
724 g_value_array_append (prop, &id);
725 g_value_array_append (prop, &dest_value);
727 properties = g_ptr_array_sized_new (1);
728 g_ptr_array_add (properties, prop);
730 DEBUG ("Set property %s", name);
731 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
733 (tp_cli_properties_interface_callback_for_set_properties)
735 "Seting property", NULL,
738 g_ptr_array_free (properties, TRUE);
739 g_value_array_free (prop);
747 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
749 EmpathyTpChatPriv *priv = GET_PRIV (chat);
750 TpConnection *connection;
751 guint handle, handle_type;
753 DEBUG ("Channel ready");
755 g_object_get (priv->channel,
756 "connection", &connection,
758 "handle_type", &handle_type,
761 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
765 handles = g_array_new (FALSE, FALSE, sizeof (guint));
766 g_array_append_val (handles, handle);
767 tp_cli_connection_run_inspect_handles (connection, -1,
768 handle_type, handles,
771 g_array_free (handles, TRUE);
775 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
776 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
779 g_object_notify (G_OBJECT (chat), "remote-contact");
782 if (tp_proxy_has_interface_by_id (priv->channel,
783 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
784 priv->group = empathy_tp_group_new (priv->channel);
786 g_signal_connect (priv->group, "member-added",
787 G_CALLBACK (tp_chat_member_added_cb),
789 g_signal_connect (priv->group, "member-removed",
790 G_CALLBACK (tp_chat_member_removed_cb),
792 g_signal_connect (priv->group, "local-pending",
793 G_CALLBACK (tp_chat_local_pending_cb),
795 empathy_run_until_ready (priv->group);
797 priv->members_count = 2;
800 if (tp_proxy_has_interface_by_id (priv->channel,
801 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
802 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
803 tp_chat_list_properties_cb,
806 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
807 tp_chat_properties_changed_cb,
809 G_OBJECT (chat), NULL);
810 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
811 tp_chat_property_flags_changed_cb,
813 G_OBJECT (chat), NULL);
816 priv->listing_pending_messages = TRUE;
817 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
819 tp_chat_list_pending_messages_cb,
823 tp_cli_channel_type_text_connect_to_received (priv->channel,
826 G_OBJECT (chat), NULL);
827 tp_cli_channel_type_text_connect_to_sent (priv->channel,
830 G_OBJECT (chat), NULL);
831 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
832 tp_chat_send_error_cb,
834 G_OBJECT (chat), NULL);
835 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
836 tp_chat_state_changed_cb,
838 G_OBJECT (chat), NULL);
839 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
840 tp_chat_state_changed_cb,
842 G_OBJECT (chat), NULL);
845 g_object_notify (G_OBJECT (chat), "ready");
849 tp_chat_finalize (GObject *object)
851 EmpathyTpChatPriv *priv = GET_PRIV (object);
854 DEBUG ("Finalize: %p", object);
856 if (priv->acknowledge && priv->channel) {
857 DEBUG ("Closing channel...");
858 tp_cli_channel_call_close (priv->channel, -1,
860 "closing channel", NULL,
865 g_signal_handlers_disconnect_by_func (priv->channel,
866 tp_chat_invalidated_cb,
868 g_object_unref (priv->channel);
871 if (priv->properties) {
872 for (i = 0; i < priv->properties->len; i++) {
873 TpChatProperty *property;
875 property = g_ptr_array_index (priv->properties, i);
876 g_free (property->name);
877 if (property->value) {
878 tp_g_value_slice_free (property->value);
880 g_slice_free (TpChatProperty, property);
882 g_ptr_array_free (priv->properties, TRUE);
885 if (priv->remote_contact) {
886 g_object_unref (priv->remote_contact);
889 g_object_unref (priv->group);
892 g_object_unref (priv->factory);
893 g_object_unref (priv->user);
894 g_object_unref (priv->account);
897 if (priv->message_queue) {
898 EmpathyMessage *message;
899 EmpathyContact *contact;
901 message = priv->message_queue->data;
902 contact = empathy_message_get_sender (message);
903 g_signal_handlers_disconnect_by_func (contact,
904 tp_chat_sender_ready_notify_cb,
907 g_slist_foreach (priv->message_queue, (GFunc) g_object_unref, NULL);
908 g_slist_free (priv->message_queue);
910 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
914 tp_chat_constructor (GType type,
916 GObjectConstructParam *props)
919 EmpathyTpChatPriv *priv;
920 gboolean channel_ready;
922 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
924 priv = GET_PRIV (chat);
925 priv->account = empathy_channel_get_account (priv->channel);
926 priv->factory = empathy_contact_factory_new ();
927 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
929 g_signal_connect (priv->channel, "invalidated",
930 G_CALLBACK (tp_chat_invalidated_cb),
933 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
935 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
937 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
938 G_CALLBACK (tp_chat_channel_ready_cb),
946 tp_chat_get_property (GObject *object,
951 EmpathyTpChatPriv *priv = GET_PRIV (object);
955 g_value_set_object (value, priv->channel);
957 case PROP_ACKNOWLEDGE:
958 g_value_set_boolean (value, priv->acknowledge);
960 case PROP_REMOTE_CONTACT:
961 g_value_set_object (value, priv->remote_contact);
964 g_value_set_boolean (value, priv->ready);
967 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
973 tp_chat_set_property (GObject *object,
978 EmpathyTpChatPriv *priv = GET_PRIV (object);
982 priv->channel = g_object_ref (g_value_get_object (value));
984 case PROP_ACKNOWLEDGE:
985 priv->acknowledge = g_value_get_boolean (value);
988 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
994 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
996 GObjectClass *object_class = G_OBJECT_CLASS (klass);
998 object_class->finalize = tp_chat_finalize;
999 object_class->constructor = tp_chat_constructor;
1000 object_class->get_property = tp_chat_get_property;
1001 object_class->set_property = tp_chat_set_property;
1003 g_object_class_install_property (object_class,
1005 g_param_spec_object ("channel",
1006 "telepathy channel",
1007 "The text channel for the chat",
1010 G_PARAM_CONSTRUCT_ONLY));
1011 g_object_class_install_property (object_class,
1013 g_param_spec_boolean ("acknowledge",
1014 "acknowledge messages",
1015 "Wheter or not received messages should be acknowledged",
1017 G_PARAM_READWRITE));
1019 g_object_class_install_property (object_class,
1020 PROP_REMOTE_CONTACT,
1021 g_param_spec_object ("remote-contact",
1022 "The remote contact",
1023 "The remote contact if there is no group iface on the channel",
1024 EMPATHY_TYPE_CONTACT,
1026 g_object_class_install_property (object_class,
1028 g_param_spec_boolean ("ready",
1029 "Is the object ready",
1030 "This object can't be used until this becomes true",
1035 signals[MESSAGE_RECEIVED] =
1036 g_signal_new ("message-received",
1037 G_TYPE_FROM_CLASS (klass),
1041 g_cclosure_marshal_VOID__OBJECT,
1043 1, EMPATHY_TYPE_MESSAGE);
1045 signals[SEND_ERROR] =
1046 g_signal_new ("send-error",
1047 G_TYPE_FROM_CLASS (klass),
1051 _empathy_marshal_VOID__OBJECT_UINT,
1053 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1055 signals[CHAT_STATE_CHANGED] =
1056 g_signal_new ("chat-state-changed",
1057 G_TYPE_FROM_CLASS (klass),
1061 _empathy_marshal_VOID__OBJECT_UINT,
1063 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1065 signals[PROPERTY_CHANGED] =
1066 g_signal_new ("property-changed",
1067 G_TYPE_FROM_CLASS (klass),
1071 _empathy_marshal_VOID__STRING_BOXED,
1073 2, G_TYPE_STRING, G_TYPE_VALUE);
1076 g_signal_new ("destroy",
1077 G_TYPE_FROM_CLASS (klass),
1081 g_cclosure_marshal_VOID__VOID,
1085 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1089 empathy_tp_chat_init (EmpathyTpChat *chat)
1091 EmpathyTpChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1092 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv);
1098 tp_chat_iface_init (EmpathyContactListIface *iface)
1100 iface->add = tp_chat_add;
1101 iface->remove = tp_chat_remove;
1102 iface->get_members = tp_chat_get_members;
1106 empathy_tp_chat_new (TpChannel *channel)
1108 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1114 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1116 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1118 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1119 g_return_val_if_fail (priv->ready, NULL);
1125 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1127 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1129 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1131 return priv->remote_contact;
1135 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1137 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1139 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1141 return priv->account;
1145 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1147 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1149 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1151 return priv->channel;
1155 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1157 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1159 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1165 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1167 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1169 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1171 return priv->members_count;
1175 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
1176 gboolean acknowledge)
1178 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1180 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1182 priv->acknowledge = acknowledge;
1183 g_object_notify (G_OBJECT (chat), "acknowledge");
1187 empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
1189 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1191 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1192 g_return_if_fail (priv->ready);
1194 if (priv->listing_pending_messages) {
1198 priv->listing_pending_messages = TRUE;
1199 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
1201 tp_chat_list_pending_messages_cb,
1207 empathy_tp_chat_send (EmpathyTpChat *chat,
1208 EmpathyMessage *message)
1210 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1211 const gchar *message_body;
1212 TpChannelTextMessageType message_type;
1214 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1215 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1216 g_return_if_fail (priv->ready);
1218 message_body = empathy_message_get_body (message);
1219 message_type = empathy_message_get_tptype (message);
1221 DEBUG ("Sending message: %s", message_body);
1222 tp_cli_channel_type_text_call_send (priv->channel, -1,
1226 g_object_ref (message),
1227 (GDestroyNotify) g_object_unref,
1232 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1233 TpChannelChatState state)
1235 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1237 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1238 g_return_if_fail (priv->ready);
1240 DEBUG ("Set state: %d", state);
1241 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1244 "setting chat state",