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-marshal.h"
33 #include "empathy-debug.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
38 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
40 #define DEBUG_DOMAIN "TpChat"
42 struct _EmpathyTpChatPriv {
43 EmpathyContactFactory *factory;
51 gboolean had_pending_messages;
52 GSList *message_queue;
53 gboolean had_properties_list;
54 GPtrArray *properties;
60 TpPropertyFlags flags;
64 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
65 static void empathy_tp_chat_init (EmpathyTpChat *chat);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
89 tp_chat_invalidated_cb (TpProxy *proxy,
95 EmpathyTpChatPriv *priv = GET_PRIV (chat);
97 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
99 g_object_unref (priv->channel);
100 g_object_unref (priv->tp_chan);
101 priv->channel = NULL;
102 priv->tp_chan = NULL;
104 g_signal_emit (chat, signals[DESTROY], 0);
108 tp_chat_async_cb (TpChannel *proxy,
111 GObject *weak_object)
114 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
115 user_data, error->message);
119 static EmpathyMessage *
120 tp_chat_build_message (EmpathyTpChat *chat,
124 const gchar *message_body)
126 EmpathyTpChatPriv *priv;
127 EmpathyMessage *message;
128 EmpathyContact *sender;
130 priv = GET_PRIV (chat);
132 if (from_handle == 0) {
133 sender = g_object_ref (priv->user);
135 sender = empathy_contact_factory_get_from_handle (priv->factory,
140 message = empathy_message_new (message_body);
141 empathy_message_set_type (message, type);
142 empathy_message_set_sender (message, sender);
143 empathy_message_set_receiver (message, priv->user);
144 empathy_message_set_timestamp (message, timestamp);
146 g_object_unref (sender);
152 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
153 GParamSpec *param_spec,
156 EmpathyTpChatPriv *priv = GET_PRIV (chat);
157 EmpathyMessage *message;
158 EmpathyContactReady ready;
159 EmpathyContact *sender;
160 gboolean removed = FALSE;
162 /* Emit all messages queued until we find a message with not
163 * ready sender. When leaving this loop, sender is the first not ready
164 * contact queued and removed tells if at least one message got removed
166 while (priv->message_queue) {
167 message = priv->message_queue->data;
168 sender = empathy_message_get_sender (message);
169 ready = empathy_contact_get_ready (sender);
171 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
175 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
176 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
177 priv->message_queue = g_slist_remove (priv->message_queue,
179 g_object_unref (message);
184 g_signal_handlers_disconnect_by_func (contact,
185 tp_chat_sender_ready_notify_cb,
188 if (priv->message_queue) {
189 g_signal_connect (sender, "notify::ready",
190 G_CALLBACK (tp_chat_sender_ready_notify_cb),
197 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
198 EmpathyMessage *message)
200 EmpathyTpChatPriv *priv = GET_PRIV (chat);
201 EmpathyContact *sender;
202 EmpathyContactReady ready;
204 if (priv->message_queue != NULL) {
205 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
206 priv->message_queue = g_slist_append (priv->message_queue,
207 g_object_ref (message));
211 sender = empathy_message_get_sender (message);
212 ready = empathy_contact_get_ready (sender);
213 if (ready & EMPATHY_CONTACT_READY_NAME) {
214 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
215 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
219 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
220 priv->message_queue = g_slist_append (priv->message_queue,
221 g_object_ref (message));
222 g_signal_connect (sender, "notify::ready",
223 G_CALLBACK (tp_chat_sender_ready_notify_cb),
228 tp_chat_received_cb (TpChannel *channel,
234 const gchar *message_body,
238 EmpathyTpChatPriv *priv = GET_PRIV (chat);
239 EmpathyMessage *message;
241 if (!priv->had_pending_messages) {
245 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
247 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
253 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
254 g_object_unref (message);
256 if (priv->acknowledge) {
259 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
260 g_array_append_val (message_ids, message_id);
261 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
265 "acknowledging pending messages",
268 g_array_free (message_ids, TRUE);
273 tp_chat_sent_cb (TpChannel *channel,
276 const gchar *message_body,
280 EmpathyMessage *message;
282 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
284 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
290 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
291 g_object_unref (message);
295 tp_chat_send_error_cb (TpChannel *channel,
299 const gchar *message_body,
303 EmpathyMessage *message;
305 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
306 message_body, error_code);
308 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
314 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
315 g_object_unref (message);
319 tp_chat_state_changed_cb (TpChannel *channel,
325 EmpathyTpChatPriv *priv = GET_PRIV (chat);
326 EmpathyContact *contact;
328 contact = empathy_contact_factory_get_from_handle (priv->factory,
332 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
333 empathy_contact_get_name (contact),
336 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
337 g_object_unref (contact);
341 tp_chat_list_pending_messages_cb (TpChannel *channel,
342 const GPtrArray *messages_list,
347 EmpathyTpChatPriv *priv = GET_PRIV (chat);
350 priv->had_pending_messages = TRUE;
353 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
358 for (i = 0; i < messages_list->len; i++) {
359 EmpathyMessage *message;
360 GValueArray *message_struct;
361 const gchar *message_body;
368 message_struct = g_ptr_array_index (messages_list, i);
370 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
371 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
372 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
373 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
374 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
375 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
377 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
379 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
385 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
386 g_object_unref (message);
391 tp_chat_properties_changed_cb (TpProxy *proxy,
392 const GPtrArray *properties,
396 EmpathyTpChatPriv *priv = GET_PRIV (chat);
399 if (!priv->had_properties_list || !properties) {
403 for (i = 0; i < properties->len; i++) {
404 GValueArray *prop_struct;
405 TpChatProperty *property;
409 prop_struct = g_ptr_array_index (properties, i);
410 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
411 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
413 for (j = 0; j < priv->properties->len; j++) {
414 property = g_ptr_array_index (priv->properties, j);
415 if (property->id == id) {
416 if (property->value) {
417 g_value_unset (property->value);
419 property->value = g_slice_new0 (GValue);
421 g_value_init (property->value, G_VALUE_TYPE (src_value));
422 g_value_copy (src_value, property->value);
424 empathy_debug (DEBUG_DOMAIN, "property %s changed",
426 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
427 property->name, property->value);
435 tp_chat_get_properties_cb (TpProxy *proxy,
436 const GPtrArray *properties,
442 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
447 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
451 tp_chat_list_properties_cb (TpProxy *proxy,
452 const GPtrArray *properties,
457 EmpathyTpChatPriv *priv = GET_PRIV (chat);
461 priv->had_properties_list = TRUE;
464 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
469 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
470 priv->properties = g_ptr_array_sized_new (properties->len);
471 for (i = 0; i < properties->len; i++) {
472 GValueArray *prop_struct;
473 TpChatProperty *property;
475 prop_struct = g_ptr_array_index (properties, i);
476 property = g_slice_new0 (TpChatProperty);
477 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
478 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
479 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
481 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
482 property->name, property->id, property->flags);
483 g_ptr_array_add (priv->properties, property);
484 if (property->flags & TP_PROPERTY_FLAG_READ) {
485 g_array_append_val (ids, property->id);
489 tp_cli_properties_interface_call_get_properties (proxy, -1,
491 tp_chat_get_properties_cb,
495 g_array_free (ids, TRUE);
499 empathy_tp_chat_set_property (EmpathyTpChat *chat,
503 EmpathyTpChatPriv *priv = GET_PRIV (chat);
504 TpChatProperty *property;
507 for (i = 0; i < priv->properties->len; i++) {
508 property = g_ptr_array_index (priv->properties, i);
509 if (!tp_strdiff (property->name, name)) {
510 GPtrArray *properties;
513 GValue dest_value = {0, };
515 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
519 g_value_init (&id, G_TYPE_UINT);
520 g_value_init (&dest_value, G_TYPE_VALUE);
521 g_value_set_uint (&id, property->id);
522 g_value_set_boxed (&dest_value, value);
524 prop = g_value_array_new (2);
525 g_value_array_append (prop, &id);
526 g_value_array_append (prop, &dest_value);
528 properties = g_ptr_array_sized_new (1);
529 g_ptr_array_add (properties, prop);
531 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
532 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
534 (tp_cli_properties_interface_callback_for_set_properties)
536 "Seting property", NULL,
539 g_ptr_array_free (properties, TRUE);
540 g_value_array_free (prop);
548 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
550 EmpathyTpChatPriv *priv = GET_PRIV (chat);
552 empathy_debug (DEBUG_DOMAIN, "Channel ready");
554 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
556 tp_chat_list_pending_messages_cb,
559 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
560 tp_chat_list_properties_cb,
565 tp_cli_channel_type_text_connect_to_received (priv->channel,
568 G_OBJECT (chat), NULL);
569 tp_cli_channel_type_text_connect_to_sent (priv->channel,
572 G_OBJECT (chat), NULL);
573 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
574 tp_chat_send_error_cb,
576 G_OBJECT (chat), NULL);
577 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
578 tp_chat_state_changed_cb,
580 G_OBJECT (chat), NULL);
581 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
582 tp_chat_state_changed_cb,
584 G_OBJECT (chat), NULL);
585 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
586 tp_chat_properties_changed_cb,
588 G_OBJECT (chat), NULL);
594 tp_chat_finalize (GObject *object)
596 EmpathyTpChatPriv *priv = GET_PRIV (object);
599 if (priv->acknowledge && priv->channel) {
600 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
601 tp_cli_channel_call_close (priv->channel, -1,
603 "closing channel", NULL,
608 g_signal_handlers_disconnect_by_func (priv->channel,
609 tp_chat_invalidated_cb,
611 g_object_unref (priv->channel);
614 g_object_unref (priv->tp_chan);
617 if (priv->properties) {
618 for (i = 0; i < priv->properties->len; i++) {
619 TpChatProperty *property;
621 property = g_ptr_array_index (priv->properties, i);
622 g_free (property->name);
623 if (property->value) {
624 g_value_unset (property->value);
625 g_slice_free (GValue, property->value);
627 g_slice_free (TpChatProperty, property);
629 g_ptr_array_free (priv->properties, TRUE);
632 g_object_unref (priv->factory);
633 g_object_unref (priv->user);
634 g_object_unref (priv->account);
635 g_object_unref (priv->mc);
638 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
642 tp_chat_constructor (GType type,
644 GObjectConstructParam *props)
647 EmpathyTpChatPriv *priv;
648 gboolean channel_ready;
650 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
652 priv = GET_PRIV (chat);
653 priv->factory = empathy_contact_factory_new ();
654 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
655 priv->mc = empathy_mission_control_new ();
657 g_signal_connect (priv->channel, "invalidated",
658 G_CALLBACK (tp_chat_invalidated_cb),
661 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
663 /* FIXME: We do that in a cb to let time to set the acknowledge
664 * property, this property should be required for construct. */
665 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
667 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
668 G_CALLBACK (tp_chat_channel_ready_cb),
676 tp_chat_get_property (GObject *object,
681 EmpathyTpChatPriv *priv = GET_PRIV (object);
685 g_value_set_object (value, priv->account);
688 g_value_set_object (value, priv->tp_chan);
691 g_value_set_object (value, priv->channel);
693 case PROP_ACKNOWLEDGE:
694 g_value_set_boolean (value, priv->acknowledge);
697 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
703 tp_chat_set_property (GObject *object,
708 EmpathyTpChatPriv *priv = GET_PRIV (object);
712 priv->account = g_object_ref (g_value_get_object (value));
715 priv->tp_chan = g_object_ref (g_value_get_object (value));
718 priv->channel = g_object_ref (g_value_get_object (value));
720 case PROP_ACKNOWLEDGE:
721 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
722 g_value_get_boolean (value));
725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
731 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
733 GObjectClass *object_class = G_OBJECT_CLASS (klass);
735 object_class->finalize = tp_chat_finalize;
736 object_class->constructor = tp_chat_constructor;
737 object_class->get_property = tp_chat_get_property;
738 object_class->set_property = tp_chat_set_property;
740 /* Construct properties */
741 g_object_class_install_property (object_class,
743 g_param_spec_object ("account",
745 "The account associated with the channel",
748 G_PARAM_CONSTRUCT_ONLY));
749 g_object_class_install_property (object_class,
751 g_param_spec_object ("tp-chan",
753 "The text channel for the chat",
756 G_PARAM_CONSTRUCT_ONLY));
758 g_object_class_install_property (object_class,
760 g_param_spec_object ("channel",
762 "The text channel for the chat",
765 G_PARAM_CONSTRUCT_ONLY));
767 g_object_class_install_property (object_class,
769 g_param_spec_boolean ("acknowledge",
770 "acknowledge messages",
771 "Wheter or not received messages should be acknowledged",
777 signals[MESSAGE_RECEIVED] =
778 g_signal_new ("message-received",
779 G_TYPE_FROM_CLASS (klass),
783 g_cclosure_marshal_VOID__OBJECT,
785 1, EMPATHY_TYPE_MESSAGE);
787 signals[SEND_ERROR] =
788 g_signal_new ("send-error",
789 G_TYPE_FROM_CLASS (klass),
793 _empathy_marshal_VOID__OBJECT_UINT,
795 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
797 signals[CHAT_STATE_CHANGED] =
798 g_signal_new ("chat-state-changed",
799 G_TYPE_FROM_CLASS (klass),
803 _empathy_marshal_VOID__OBJECT_UINT,
805 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
807 signals[PROPERTY_CHANGED] =
808 g_signal_new ("property-changed",
809 G_TYPE_FROM_CLASS (klass),
813 _empathy_marshal_VOID__STRING_BOXED,
815 2, G_TYPE_STRING, G_TYPE_VALUE);
818 g_signal_new ("destroy",
819 G_TYPE_FROM_CLASS (klass),
823 g_cclosure_marshal_VOID__VOID,
827 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
831 empathy_tp_chat_init (EmpathyTpChat *chat)
836 empathy_tp_chat_new (McAccount *account,
841 TpConnection *connection;
845 mc = empathy_mission_control_new ();
846 tp_conn = mission_control_get_connection (mc, account, NULL);
847 connection = tp_conn_dup_connection (tp_conn);
848 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
850 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
856 g_object_unref (channel);
857 g_object_unref (tp_conn);
858 g_object_unref (connection);
865 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
872 const gchar *bus_name;
875 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
877 mc = empathy_mission_control_new ();
878 account = empathy_contact_get_account (contact);
880 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
881 /* The account is not connected. */
885 tp_conn = mission_control_get_connection (mc, account, NULL);
886 g_return_val_if_fail (tp_conn != NULL, NULL);
887 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
888 handle = empathy_contact_get_handle (contact);
890 text_chan = tp_conn_new_channel (tp_get_bus (),
893 TP_IFACE_CHANNEL_TYPE_TEXT,
894 TP_HANDLE_TYPE_CONTACT,
898 chat = empathy_tp_chat_new (account, text_chan);
900 g_object_unref (tp_conn);
901 g_object_unref (text_chan);
908 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
910 EmpathyTpChatPriv *priv;
912 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
914 priv = GET_PRIV (chat);
916 return priv->acknowledge;
920 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
921 gboolean acknowledge)
923 EmpathyTpChatPriv *priv;
925 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
927 priv = GET_PRIV (chat);
929 priv->acknowledge = acknowledge;
930 g_object_notify (G_OBJECT (chat), "acknowledge");
934 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
936 EmpathyTpChatPriv *priv;
938 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
940 priv = GET_PRIV (chat);
942 return priv->tp_chan;
946 empathy_tp_chat_get_account (EmpathyTpChat *chat)
948 EmpathyTpChatPriv *priv;
950 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
952 priv = GET_PRIV (chat);
954 return priv->account;
958 empathy_tp_chat_send (EmpathyTpChat *chat,
959 EmpathyMessage *message)
961 EmpathyTpChatPriv *priv;
962 const gchar *message_body;
963 EmpathyMessageType message_type;
965 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
966 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
968 priv = GET_PRIV (chat);
970 message_body = empathy_message_get_body (message);
971 message_type = empathy_message_get_type (message);
973 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
974 tp_cli_channel_type_text_call_send (priv->channel, -1,
978 "sending message", NULL,
983 empathy_tp_chat_set_state (EmpathyTpChat *chat,
984 TpChannelChatState state)
986 EmpathyTpChatPriv *priv;
988 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
990 priv = GET_PRIV (chat);
992 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
993 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
996 "setting chat state",
1002 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1004 EmpathyTpChatPriv *priv;
1006 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1008 priv = GET_PRIV (chat);
1011 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);