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-debug.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
41 #define DEBUG_DOMAIN "TpChat"
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 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
102 g_signal_emit (chat, signals[DESTROY], 0);
106 tp_chat_async_cb (TpChannel *proxy,
109 GObject *weak_object)
112 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
113 user_data, error->message);
118 tp_chat_member_added_cb (EmpathyTpGroup *group,
119 EmpathyContact *contact,
120 EmpathyContact *actor,
122 const gchar *message,
125 EmpathyTpChatPriv *priv = GET_PRIV (chat);
126 guint handle_type = 0;
128 priv->members_count++;
129 g_signal_emit_by_name (chat, "members-changed",
130 contact, actor, reason, message,
133 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
134 if (handle_type == TP_HANDLE_TYPE_ROOM) {
138 if (priv->members_count > 2 && priv->remote_contact) {
139 /* We now have more than 2 members, this is not a p2p chat
140 * anymore. Remove the remote-contact as it makes no sense, the
141 * EmpathyContactList interface must be used now. */
142 g_object_unref (priv->remote_contact);
143 priv->remote_contact = NULL;
144 g_object_notify (G_OBJECT (chat), "remote-contact");
146 if (priv->members_count <= 2 && !priv->remote_contact &&
147 !empathy_contact_is_user (contact)) {
148 /* This is a p2p chat, if it's not ourself that means this is
149 * the remote contact with who we are chatting. This is to
150 * avoid forcing the usage of the EmpathyContactList interface
152 priv->remote_contact = g_object_ref (contact);
153 g_object_notify (G_OBJECT (chat), "remote-contact");
158 tp_chat_member_removed_cb (EmpathyTpGroup *group,
159 EmpathyContact *contact,
160 EmpathyContact *actor,
162 const gchar *message,
165 EmpathyTpChatPriv *priv = GET_PRIV (chat);
166 guint handle_type = 0;
168 priv->members_count--;
169 g_signal_emit_by_name (chat, "members-changed",
170 contact, actor, reason, message,
173 g_object_get (priv->channel, "handle-type", &handle_type, NULL);
174 if (handle_type == TP_HANDLE_TYPE_ROOM) {
178 if (priv->members_count <= 2 && !priv->remote_contact) {
181 /* We are not a MUC anymore, get the remote contact back */
182 members = empathy_tp_group_get_members (group);
183 for (l = members; l; l = l->next) {
184 if (!empathy_contact_is_user (l->data)) {
185 priv->remote_contact = g_object_ref (l->data);
186 g_object_notify (G_OBJECT (chat), "remote-contact");
190 g_list_foreach (members, (GFunc) g_object_unref, NULL);
191 g_list_free (members);
196 tp_chat_local_pending_cb (EmpathyTpGroup *group,
197 EmpathyContact *contact,
198 EmpathyContact *actor,
200 const gchar *message,
203 g_signal_emit_by_name (chat, "pendings-changed",
204 contact, actor, reason, message,
209 tp_chat_add (EmpathyContactList *list,
210 EmpathyContact *contact,
211 const gchar *message)
213 EmpathyTpChatPriv *priv = GET_PRIV (list);
215 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
216 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
219 empathy_tp_group_add_member (priv->group, contact, message);
224 tp_chat_remove (EmpathyContactList *list,
225 EmpathyContact *contact,
226 const gchar *message)
228 EmpathyTpChatPriv *priv = GET_PRIV (list);
230 g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
231 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
234 empathy_tp_group_remove_member (priv->group, contact, message);
239 tp_chat_get_members (EmpathyContactList *list)
241 EmpathyTpChatPriv *priv = GET_PRIV (list);
242 GList *members = NULL;
244 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
247 members = empathy_tp_group_get_members (priv->group);
249 members = g_list_prepend (members, g_object_ref (priv->user));
250 members = g_list_prepend (members, g_object_ref (priv->remote_contact));
256 static EmpathyMessage *
257 tp_chat_build_message (EmpathyTpChat *chat,
261 const gchar *message_body)
263 EmpathyTpChatPriv *priv;
264 EmpathyMessage *message;
265 EmpathyContact *sender;
267 priv = GET_PRIV (chat);
269 if (from_handle == 0) {
270 sender = g_object_ref (priv->user);
272 sender = empathy_contact_factory_get_from_handle (priv->factory,
277 message = empathy_message_new (message_body);
278 empathy_message_set_type (message, type);
279 empathy_message_set_sender (message, sender);
280 empathy_message_set_receiver (message, priv->user);
281 empathy_message_set_timestamp (message, timestamp);
283 g_object_unref (sender);
289 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
290 GParamSpec *param_spec,
293 EmpathyTpChatPriv *priv = GET_PRIV (chat);
294 EmpathyMessage *message;
295 EmpathyContactReady ready;
296 EmpathyContact *sender;
297 gboolean removed = FALSE;
299 /* Emit all messages queued until we find a message with not
300 * ready sender. When leaving this loop, sender is the first not ready
301 * contact queued and removed tells if at least one message got removed
303 while (priv->message_queue) {
304 message = priv->message_queue->data;
305 sender = empathy_message_get_sender (message);
306 ready = empathy_contact_get_ready (sender);
308 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
312 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
313 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
314 priv->message_queue = g_slist_remove (priv->message_queue,
316 g_object_unref (message);
321 g_signal_handlers_disconnect_by_func (contact,
322 tp_chat_sender_ready_notify_cb,
325 if (priv->message_queue) {
326 g_signal_connect (sender, "notify::ready",
327 G_CALLBACK (tp_chat_sender_ready_notify_cb),
334 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
335 EmpathyMessage *message)
337 EmpathyTpChatPriv *priv = GET_PRIV (chat);
338 EmpathyContact *sender;
339 EmpathyContactReady ready;
341 if (priv->message_queue != NULL) {
342 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
343 priv->message_queue = g_slist_append (priv->message_queue,
344 g_object_ref (message));
348 sender = empathy_message_get_sender (message);
349 ready = empathy_contact_get_ready (sender);
350 if (ready & EMPATHY_CONTACT_READY_NAME) {
351 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
352 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
356 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
357 priv->message_queue = g_slist_append (priv->message_queue,
358 g_object_ref (message));
359 g_signal_connect (sender, "notify::ready",
360 G_CALLBACK (tp_chat_sender_ready_notify_cb),
365 tp_chat_received_cb (TpChannel *channel,
371 const gchar *message_body,
375 EmpathyTpChatPriv *priv = GET_PRIV (chat);
376 EmpathyMessage *message;
378 if (!priv->had_pending_messages) {
382 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
384 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
390 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
391 g_object_unref (message);
393 if (priv->acknowledge) {
396 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
397 g_array_append_val (message_ids, message_id);
398 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
402 "acknowledging received message",
405 g_array_free (message_ids, TRUE);
410 tp_chat_sent_cb (TpChannel *channel,
413 const gchar *message_body,
417 EmpathyMessage *message;
419 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
421 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
427 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
428 g_object_unref (message);
432 tp_chat_send_error_cb (TpChannel *channel,
436 const gchar *message_body,
440 EmpathyMessage *message;
442 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
443 message_body, error_code);
445 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
451 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
452 g_object_unref (message);
456 tp_chat_state_changed_cb (TpChannel *channel,
462 EmpathyTpChatPriv *priv = GET_PRIV (chat);
463 EmpathyContact *contact;
465 contact = empathy_contact_factory_get_from_handle (priv->factory,
469 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
470 empathy_contact_get_name (contact),
473 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
474 g_object_unref (contact);
478 tp_chat_list_pending_messages_cb (TpChannel *channel,
479 const GPtrArray *messages_list,
484 EmpathyTpChatPriv *priv = GET_PRIV (chat);
486 GArray *message_ids = NULL;
488 priv->had_pending_messages = TRUE;
491 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
496 if (priv->acknowledge) {
497 message_ids = g_array_sized_new (FALSE, FALSE, sizeof (guint),
501 for (i = 0; i < messages_list->len; i++) {
502 EmpathyMessage *message;
503 GValueArray *message_struct;
504 const gchar *message_body;
511 message_struct = g_ptr_array_index (messages_list, i);
513 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
514 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
515 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
516 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
517 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
518 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
520 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
523 g_array_append_val (message_ids, message_id);
526 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
532 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
533 g_object_unref (message);
537 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
541 "acknowledging pending messages",
544 g_array_free (message_ids, TRUE);
549 tp_chat_property_flags_changed_cb (TpProxy *proxy,
550 const GPtrArray *properties,
554 EmpathyTpChatPriv *priv = GET_PRIV (chat);
557 if (!priv->had_properties_list || !properties) {
561 for (i = 0; i < properties->len; i++) {
562 GValueArray *prop_struct;
563 TpChatProperty *property;
567 prop_struct = g_ptr_array_index (properties, i);
568 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
569 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
571 for (j = 0; j < priv->properties->len; j++) {
572 property = g_ptr_array_index (priv->properties, j);
573 if (property->id == id) {
574 property->flags = flags;
575 empathy_debug (DEBUG_DOMAIN,
576 "property %s flags changed: %d",
577 property->name, property->flags);
585 tp_chat_properties_changed_cb (TpProxy *proxy,
586 const GPtrArray *properties,
590 EmpathyTpChatPriv *priv = GET_PRIV (chat);
593 if (!priv->had_properties_list || !properties) {
597 for (i = 0; i < properties->len; i++) {
598 GValueArray *prop_struct;
599 TpChatProperty *property;
603 prop_struct = g_ptr_array_index (properties, i);
604 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
605 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
607 for (j = 0; j < priv->properties->len; j++) {
608 property = g_ptr_array_index (priv->properties, j);
609 if (property->id == id) {
610 if (property->value) {
611 g_value_copy (src_value, property->value);
613 property->value = tp_g_value_slice_dup (src_value);
616 empathy_debug (DEBUG_DOMAIN, "property %s changed",
618 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
619 property->name, property->value);
627 tp_chat_get_properties_cb (TpProxy *proxy,
628 const GPtrArray *properties,
634 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
639 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
643 tp_chat_list_properties_cb (TpProxy *proxy,
644 const GPtrArray *properties,
649 EmpathyTpChatPriv *priv = GET_PRIV (chat);
653 priv->had_properties_list = TRUE;
656 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
661 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
662 priv->properties = g_ptr_array_sized_new (properties->len);
663 for (i = 0; i < properties->len; i++) {
664 GValueArray *prop_struct;
665 TpChatProperty *property;
667 prop_struct = g_ptr_array_index (properties, i);
668 property = g_slice_new0 (TpChatProperty);
669 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
670 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
671 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
673 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
674 property->name, property->id, property->flags);
675 g_ptr_array_add (priv->properties, property);
676 if (property->flags & TP_PROPERTY_FLAG_READ) {
677 g_array_append_val (ids, property->id);
681 tp_cli_properties_interface_call_get_properties (proxy, -1,
683 tp_chat_get_properties_cb,
687 g_array_free (ids, TRUE);
691 empathy_tp_chat_set_property (EmpathyTpChat *chat,
695 EmpathyTpChatPriv *priv = GET_PRIV (chat);
696 TpChatProperty *property;
699 g_return_if_fail (priv->ready);
701 for (i = 0; i < priv->properties->len; i++) {
702 property = g_ptr_array_index (priv->properties, i);
703 if (!tp_strdiff (property->name, name)) {
704 GPtrArray *properties;
707 GValue dest_value = {0, };
709 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
713 g_value_init (&id, G_TYPE_UINT);
714 g_value_init (&dest_value, G_TYPE_VALUE);
715 g_value_set_uint (&id, property->id);
716 g_value_set_boxed (&dest_value, value);
718 prop = g_value_array_new (2);
719 g_value_array_append (prop, &id);
720 g_value_array_append (prop, &dest_value);
722 properties = g_ptr_array_sized_new (1);
723 g_ptr_array_add (properties, prop);
725 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
726 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
728 (tp_cli_properties_interface_callback_for_set_properties)
730 "Seting property", NULL,
733 g_ptr_array_free (properties, TRUE);
734 g_value_array_free (prop);
742 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
744 EmpathyTpChatPriv *priv = GET_PRIV (chat);
745 TpConnection *connection;
746 guint handle, handle_type;
748 empathy_debug (DEBUG_DOMAIN, "Channel ready");
750 g_object_get (priv->channel,
751 "connection", &connection,
753 "handle_type", &handle_type,
756 if (handle_type != TP_HANDLE_TYPE_NONE && handle != 0) {
760 handles = g_array_new (FALSE, FALSE, sizeof (guint));
761 g_array_append_val (handles, handle);
762 tp_cli_connection_run_inspect_handles (connection, -1,
763 handle_type, handles,
766 g_array_free (handles, TRUE);
770 if (handle_type == TP_HANDLE_TYPE_CONTACT && handle != 0) {
771 priv->remote_contact = empathy_contact_factory_get_from_handle (priv->factory,
774 g_object_notify (G_OBJECT (chat), "remote-contact");
777 if (tp_proxy_has_interface_by_id (priv->channel,
778 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
779 priv->group = empathy_tp_group_new (priv->channel);
781 g_signal_connect (priv->group, "member-added",
782 G_CALLBACK (tp_chat_member_added_cb),
784 g_signal_connect (priv->group, "member-removed",
785 G_CALLBACK (tp_chat_member_removed_cb),
787 g_signal_connect (priv->group, "local-pending",
788 G_CALLBACK (tp_chat_local_pending_cb),
790 empathy_run_until_ready (priv->group);
792 priv->members_count = 2;
795 if (tp_proxy_has_interface_by_id (priv->channel,
796 TP_IFACE_QUARK_PROPERTIES_INTERFACE)) {
797 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
798 tp_chat_list_properties_cb,
801 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
802 tp_chat_properties_changed_cb,
804 G_OBJECT (chat), NULL);
805 tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
806 tp_chat_property_flags_changed_cb,
808 G_OBJECT (chat), NULL);
811 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
813 tp_chat_list_pending_messages_cb,
817 tp_cli_channel_type_text_connect_to_received (priv->channel,
820 G_OBJECT (chat), NULL);
821 tp_cli_channel_type_text_connect_to_sent (priv->channel,
824 G_OBJECT (chat), NULL);
825 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
826 tp_chat_send_error_cb,
828 G_OBJECT (chat), NULL);
829 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
830 tp_chat_state_changed_cb,
832 G_OBJECT (chat), NULL);
833 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
834 tp_chat_state_changed_cb,
836 G_OBJECT (chat), NULL);
839 g_object_notify (G_OBJECT (chat), "ready");
843 tp_chat_finalize (GObject *object)
845 EmpathyTpChatPriv *priv = GET_PRIV (object);
848 if (priv->acknowledge && priv->channel) {
849 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
850 tp_cli_channel_call_close (priv->channel, -1,
852 "closing channel", NULL,
857 g_signal_handlers_disconnect_by_func (priv->channel,
858 tp_chat_invalidated_cb,
860 g_object_unref (priv->channel);
863 if (priv->properties) {
864 for (i = 0; i < priv->properties->len; i++) {
865 TpChatProperty *property;
867 property = g_ptr_array_index (priv->properties, i);
868 g_free (property->name);
869 if (property->value) {
870 tp_g_value_slice_free (property->value);
872 g_slice_free (TpChatProperty, property);
874 g_ptr_array_free (priv->properties, TRUE);
877 if (priv->remote_contact) {
878 g_object_unref (priv->remote_contact);
881 g_object_unref (priv->group);
884 g_object_unref (priv->factory);
885 g_object_unref (priv->user);
886 g_object_unref (priv->account);
889 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
893 tp_chat_constructor (GType type,
895 GObjectConstructParam *props)
898 EmpathyTpChatPriv *priv;
899 gboolean channel_ready;
901 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
903 priv = GET_PRIV (chat);
904 priv->account = empathy_channel_get_account (priv->channel);
905 priv->factory = empathy_contact_factory_new ();
906 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
908 g_signal_connect (priv->channel, "invalidated",
909 G_CALLBACK (tp_chat_invalidated_cb),
912 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
914 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
916 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
917 G_CALLBACK (tp_chat_channel_ready_cb),
925 tp_chat_get_property (GObject *object,
930 EmpathyTpChatPriv *priv = GET_PRIV (object);
934 g_value_set_object (value, priv->channel);
936 case PROP_ACKNOWLEDGE:
937 g_value_set_boolean (value, priv->acknowledge);
939 case PROP_REMOTE_CONTACT:
940 g_value_set_object (value, priv->remote_contact);
943 g_value_set_boolean (value, priv->ready);
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
952 tp_chat_set_property (GObject *object,
957 EmpathyTpChatPriv *priv = GET_PRIV (object);
961 priv->channel = g_object_ref (g_value_get_object (value));
963 case PROP_ACKNOWLEDGE:
964 priv->acknowledge = g_value_get_boolean (value);
967 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
973 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
975 GObjectClass *object_class = G_OBJECT_CLASS (klass);
977 object_class->finalize = tp_chat_finalize;
978 object_class->constructor = tp_chat_constructor;
979 object_class->get_property = tp_chat_get_property;
980 object_class->set_property = tp_chat_set_property;
982 g_object_class_install_property (object_class,
984 g_param_spec_object ("channel",
986 "The text channel for the chat",
989 G_PARAM_CONSTRUCT_ONLY));
990 g_object_class_install_property (object_class,
992 g_param_spec_boolean ("acknowledge",
993 "acknowledge messages",
994 "Wheter or not received messages should be acknowledged",
999 g_object_class_install_property (object_class,
1000 PROP_REMOTE_CONTACT,
1001 g_param_spec_object ("remote-contact",
1002 "The remote contact",
1003 "The remote contact if there is no group iface on the channel",
1004 EMPATHY_TYPE_CONTACT,
1006 g_object_class_install_property (object_class,
1008 g_param_spec_boolean ("ready",
1009 "Is the object ready",
1010 "This object can't be used until this becomes true",
1015 signals[MESSAGE_RECEIVED] =
1016 g_signal_new ("message-received",
1017 G_TYPE_FROM_CLASS (klass),
1021 g_cclosure_marshal_VOID__OBJECT,
1023 1, EMPATHY_TYPE_MESSAGE);
1025 signals[SEND_ERROR] =
1026 g_signal_new ("send-error",
1027 G_TYPE_FROM_CLASS (klass),
1031 _empathy_marshal_VOID__OBJECT_UINT,
1033 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
1035 signals[CHAT_STATE_CHANGED] =
1036 g_signal_new ("chat-state-changed",
1037 G_TYPE_FROM_CLASS (klass),
1041 _empathy_marshal_VOID__OBJECT_UINT,
1043 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
1045 signals[PROPERTY_CHANGED] =
1046 g_signal_new ("property-changed",
1047 G_TYPE_FROM_CLASS (klass),
1051 _empathy_marshal_VOID__STRING_BOXED,
1053 2, G_TYPE_STRING, G_TYPE_VALUE);
1056 g_signal_new ("destroy",
1057 G_TYPE_FROM_CLASS (klass),
1061 g_cclosure_marshal_VOID__VOID,
1065 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
1069 empathy_tp_chat_init (EmpathyTpChat *chat)
1074 tp_chat_iface_init (EmpathyContactListIface *iface)
1076 iface->add = tp_chat_add;
1077 iface->remove = tp_chat_remove;
1078 iface->get_members = tp_chat_get_members;
1082 empathy_tp_chat_new (TpChannel *channel,
1083 gboolean acknowledge)
1085 return g_object_new (EMPATHY_TYPE_TP_CHAT,
1087 "acknowledge", acknowledge,
1092 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1094 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1096 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1097 g_return_val_if_fail (priv->ready, NULL);
1103 empathy_tp_chat_get_remote_contact (EmpathyTpChat *chat)
1105 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1107 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1109 return priv->remote_contact;
1113 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1115 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1117 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1119 return priv->account;
1123 empathy_tp_chat_is_ready (EmpathyTpChat *chat)
1125 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1127 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
1133 empathy_tp_chat_get_members_count (EmpathyTpChat *chat)
1135 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1137 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), 0);
1139 return priv->members_count;
1143 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
1144 gboolean acknowledge)
1146 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1148 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1150 priv->acknowledge = acknowledge;
1151 g_object_notify (G_OBJECT (chat), "acknowledge");
1155 empathy_tp_chat_emit_pendings (EmpathyTpChat *chat)
1157 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1159 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1160 g_return_if_fail (priv->ready);
1162 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
1164 tp_chat_list_pending_messages_cb,
1170 empathy_tp_chat_send (EmpathyTpChat *chat,
1171 EmpathyMessage *message)
1173 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1174 const gchar *message_body;
1175 EmpathyMessageType message_type;
1177 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1178 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1179 g_return_if_fail (priv->ready);
1181 message_body = empathy_message_get_body (message);
1182 message_type = empathy_message_get_type (message);
1184 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1185 tp_cli_channel_type_text_call_send (priv->channel, -1,
1189 "sending message", NULL,
1194 empathy_tp_chat_set_state (EmpathyTpChat *chat,
1195 TpChannelChatState state)
1197 EmpathyTpChatPriv *priv = GET_PRIV (chat);
1199 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1200 g_return_if_fail (priv->ready);
1202 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1203 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1206 "setting chat state",