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>
29 #include "empathy-tp-chat.h"
30 #include "empathy-contact-factory.h"
31 #include "empathy-marshal.h"
32 #include "empathy-debug.h"
33 #include "empathy-time.h"
34 #include "empathy-utils.h"
36 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
37 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
39 #define DEBUG_DOMAIN "TpChat"
41 struct _EmpathyTpChatPriv {
42 EmpathyContactFactory *factory;
50 gboolean had_pending_messages;
51 GSList *message_queue;
52 GPtrArray *properties;
61 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
62 static void empathy_tp_chat_init (EmpathyTpChat *chat);
80 static guint signals[LAST_SIGNAL];
82 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
85 tp_chat_invalidated_cb (TpProxy *proxy,
91 EmpathyTpChatPriv *priv = GET_PRIV (chat);
93 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
95 g_object_unref (priv->channel);
96 g_object_unref (priv->tp_chan);
100 g_signal_emit (chat, signals[DESTROY], 0);
104 tp_chat_async_cb (TpChannel *proxy,
107 GObject *weak_object)
110 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
111 user_data, error->message);
115 static EmpathyMessage *
116 tp_chat_build_message (EmpathyTpChat *chat,
120 const gchar *message_body)
122 EmpathyTpChatPriv *priv;
123 EmpathyMessage *message;
124 EmpathyContact *sender;
126 priv = GET_PRIV (chat);
128 if (from_handle == 0) {
129 sender = g_object_ref (priv->user);
131 sender = empathy_contact_factory_get_from_handle (priv->factory,
136 message = empathy_message_new (message_body);
137 empathy_message_set_type (message, type);
138 empathy_message_set_sender (message, sender);
139 empathy_message_set_receiver (message, priv->user);
140 empathy_message_set_timestamp (message, timestamp);
142 g_object_unref (sender);
148 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
149 GParamSpec *param_spec,
152 EmpathyTpChatPriv *priv = GET_PRIV (chat);
153 EmpathyMessage *message;
154 EmpathyContactReady ready;
155 EmpathyContact *sender;
156 gboolean removed = FALSE;
158 /* Emit all messages queued until we find a message with not
159 * ready sender. When leaving this loop, sender is the first not ready
160 * contact queued and removed tells if at least one message got removed
162 while (priv->message_queue) {
163 message = priv->message_queue->data;
164 sender = empathy_message_get_sender (message);
165 ready = empathy_contact_get_ready (sender);
167 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
171 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
172 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
173 priv->message_queue = g_slist_remove (priv->message_queue,
175 g_object_unref (message);
180 g_signal_handlers_disconnect_by_func (contact,
181 tp_chat_sender_ready_notify_cb,
184 if (priv->message_queue) {
185 g_signal_connect (sender, "notify::ready",
186 G_CALLBACK (tp_chat_sender_ready_notify_cb),
193 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
194 EmpathyMessage *message)
196 EmpathyTpChatPriv *priv = GET_PRIV (chat);
197 EmpathyContact *sender;
198 EmpathyContactReady ready;
200 if (priv->message_queue != NULL) {
201 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
202 priv->message_queue = g_slist_append (priv->message_queue,
203 g_object_ref (message));
207 sender = empathy_message_get_sender (message);
208 ready = empathy_contact_get_ready (sender);
209 if (ready & EMPATHY_CONTACT_READY_NAME) {
210 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
211 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
215 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
216 priv->message_queue = g_slist_append (priv->message_queue,
217 g_object_ref (message));
218 g_signal_connect (sender, "notify::ready",
219 G_CALLBACK (tp_chat_sender_ready_notify_cb),
224 tp_chat_received_cb (TpChannel *channel,
230 const gchar *message_body,
234 EmpathyTpChatPriv *priv = GET_PRIV (chat);
235 EmpathyMessage *message;
237 if (!priv->had_pending_messages) {
241 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
243 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
249 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
250 g_object_unref (message);
252 if (priv->acknowledge) {
255 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
256 g_array_append_val (message_ids, message_id);
257 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
261 "acknowledging pending messages",
264 g_array_free (message_ids, TRUE);
269 tp_chat_sent_cb (TpChannel *channel,
272 const gchar *message_body,
276 EmpathyMessage *message;
278 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
280 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
286 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
287 g_object_unref (message);
291 tp_chat_send_error_cb (TpChannel *channel,
295 const gchar *message_body,
299 EmpathyMessage *message;
301 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
302 message_body, error_code);
304 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
310 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
311 g_object_unref (message);
315 tp_chat_state_changed_cb (TpChannel *channel,
321 EmpathyTpChatPriv *priv = GET_PRIV (chat);
322 EmpathyContact *contact;
324 contact = empathy_contact_factory_get_from_handle (priv->factory,
328 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
329 empathy_contact_get_name (contact),
332 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
333 g_object_unref (contact);
337 tp_chat_list_pending_messages_cb (TpChannel *channel,
338 const GPtrArray *messages_list,
343 EmpathyTpChatPriv *priv = GET_PRIV (chat);
346 priv->had_pending_messages = TRUE;
348 for (i = 0; i < messages_list->len; i++) {
349 EmpathyMessage *message;
350 GValueArray *message_struct;
351 const gchar *message_body;
358 message_struct = g_ptr_array_index (messages_list, i);
360 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
361 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
362 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
363 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
364 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
365 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
367 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
369 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
375 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
376 g_object_unref (message);
381 tp_chat_properties_changed_cb (TpProxy *proxy,
382 const GPtrArray *properties,
386 EmpathyTpChatPriv *priv = GET_PRIV (chat);
389 for (i = 0; i < properties->len; i++) {
390 GValueArray *prop_struct;
391 TpChatProperty *property;
395 prop_struct = g_ptr_array_index (properties, i);
396 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
397 src_value = g_value_array_get_nth (prop_struct, 1);
399 for (j = 0; j < priv->properties->len; j++) {
400 property = g_ptr_array_index (priv->properties, j);
401 if (property->id == id) {
402 if (property->value) {
403 g_value_unset (property->value);
405 property->value = g_slice_new0 (GValue);
407 g_value_copy (src_value, property->value);
409 empathy_debug (DEBUG_DOMAIN, "property %s changed",
411 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
412 property->name, property->value);
420 tp_chat_get_properties_cb (TpProxy *proxy,
421 const GPtrArray *properties,
426 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
430 tp_chat_list_properties_cb (TpProxy *proxy,
431 const GPtrArray *properties,
436 EmpathyTpChatPriv *priv = GET_PRIV (chat);
440 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
441 priv->properties = g_ptr_array_sized_new (properties->len);
442 for (i = 0; i < properties->len; i++) {
443 GValueArray *prop_struct;
444 TpChatProperty *property;
448 prop_struct = g_ptr_array_index (properties, i);
449 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
450 name = g_value_get_string (g_value_array_get_nth (prop_struct, 1));
452 property = g_slice_new0 (TpChatProperty);
454 property->name = g_strdup (name);
455 g_ptr_array_add (priv->properties, property);
456 g_array_append_val (ids, id);
459 tp_cli_properties_interface_call_get_properties (proxy, -1,
461 tp_chat_get_properties_cb,
465 g_array_free (ids, TRUE);
469 empathy_tp_chat_set_property (EmpathyTpChat *chat,
472 /* FIXME: not implemented */
476 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
478 EmpathyTpChatPriv *priv = GET_PRIV (chat);
480 empathy_debug (DEBUG_DOMAIN, "Channel ready");
482 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
484 tp_chat_list_pending_messages_cb,
487 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
488 tp_chat_list_properties_cb,
493 tp_cli_channel_type_text_connect_to_received (priv->channel,
496 G_OBJECT (chat), NULL);
497 tp_cli_channel_type_text_connect_to_sent (priv->channel,
500 G_OBJECT (chat), NULL);
501 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
502 tp_chat_send_error_cb,
504 G_OBJECT (chat), NULL);
505 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
506 tp_chat_state_changed_cb,
508 G_OBJECT (chat), NULL);
509 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
510 tp_chat_state_changed_cb,
512 G_OBJECT (chat), NULL);
513 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
514 tp_chat_properties_changed_cb,
516 G_OBJECT (chat), NULL);
522 tp_chat_finalize (GObject *object)
524 EmpathyTpChatPriv *priv = GET_PRIV (object);
527 if (priv->acknowledge && priv->channel) {
528 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
529 tp_cli_channel_call_close (priv->channel, -1,
531 "closing channel", NULL,
536 g_signal_handlers_disconnect_by_func (priv->channel,
537 tp_chat_invalidated_cb,
539 g_object_unref (priv->channel);
542 g_object_unref (priv->tp_chan);
545 for (i = 0; i < priv->properties->len; i++) {
546 TpChatProperty *property;
548 property = g_ptr_array_index (priv->properties, i);
549 g_free (property->name);
550 g_value_unset (property->value);
551 g_slice_free (GValue, property->value);
552 g_slice_free (TpChatProperty, property);
554 g_ptr_array_free (priv->properties, TRUE);
556 g_object_unref (priv->factory);
557 g_object_unref (priv->user);
558 g_object_unref (priv->account);
559 g_object_unref (priv->mc);
562 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
566 tp_chat_constructor (GType type,
568 GObjectConstructParam *props)
571 EmpathyTpChatPriv *priv;
572 gboolean channel_ready;
574 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
576 priv = GET_PRIV (chat);
577 priv->factory = empathy_contact_factory_new ();
578 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
579 priv->mc = empathy_mission_control_new ();
581 g_signal_connect (priv->channel, "invalidated",
582 G_CALLBACK (tp_chat_invalidated_cb),
585 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
587 /* FIXME: We do that in a cb to let time to set the acknowledge
588 * property, this property should be required for construct. */
589 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
591 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
592 G_CALLBACK (tp_chat_channel_ready_cb),
600 tp_chat_get_property (GObject *object,
605 EmpathyTpChatPriv *priv = GET_PRIV (object);
609 g_value_set_object (value, priv->account);
612 g_value_set_object (value, priv->channel);
614 case PROP_ACKNOWLEDGE:
615 g_value_set_boolean (value, priv->acknowledge);
618 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
624 tp_chat_set_property (GObject *object,
629 EmpathyTpChatPriv *priv = GET_PRIV (object);
633 priv->account = g_object_ref (g_value_get_object (value));
636 priv->channel = g_object_ref (g_value_get_object (value));
638 case PROP_ACKNOWLEDGE:
639 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
640 g_value_get_boolean (value));
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
649 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
651 GObjectClass *object_class = G_OBJECT_CLASS (klass);
653 object_class->finalize = tp_chat_finalize;
654 object_class->constructor = tp_chat_constructor;
655 object_class->get_property = tp_chat_get_property;
656 object_class->set_property = tp_chat_set_property;
658 /* Construct properties */
659 g_object_class_install_property (object_class,
661 g_param_spec_object ("account",
663 "The account associated with the channel",
666 G_PARAM_CONSTRUCT_ONLY));
667 g_object_class_install_property (object_class,
669 g_param_spec_object ("channel",
671 "The text channel for the chat",
674 G_PARAM_CONSTRUCT_ONLY));
676 g_object_class_install_property (object_class,
678 g_param_spec_boolean ("acknowledge",
679 "acknowledge messages",
680 "Wheter or not received messages should be acknowledged",
686 signals[MESSAGE_RECEIVED] =
687 g_signal_new ("message-received",
688 G_TYPE_FROM_CLASS (klass),
692 g_cclosure_marshal_VOID__OBJECT,
694 1, EMPATHY_TYPE_MESSAGE);
696 signals[SEND_ERROR] =
697 g_signal_new ("send-error",
698 G_TYPE_FROM_CLASS (klass),
702 _empathy_marshal_VOID__OBJECT_UINT,
704 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
706 signals[CHAT_STATE_CHANGED] =
707 g_signal_new ("chat-state-changed",
708 G_TYPE_FROM_CLASS (klass),
712 _empathy_marshal_VOID__OBJECT_UINT,
714 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
716 signals[PROPERTY_CHANGED] =
717 g_signal_new ("property-changed",
718 G_TYPE_FROM_CLASS (klass),
722 _empathy_marshal_VOID__STRING_BOXED,
724 2, G_TYPE_STRING, G_TYPE_VALUE);
727 g_signal_new ("destroy",
728 G_TYPE_FROM_CLASS (klass),
732 g_cclosure_marshal_VOID__VOID,
736 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
740 empathy_tp_chat_init (EmpathyTpChat *chat)
745 empathy_tp_chat_new (McAccount *account,
749 EmpathyTpChatPriv *priv;
751 TpConnection *connection;
755 mc = empathy_mission_control_new ();
756 tp_conn = mission_control_get_connection (mc, account, NULL);
757 connection = tp_conn_dup_connection (tp_conn);
758 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
760 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
765 priv = GET_PRIV (chat);
766 priv->tp_chan = g_object_ref (tp_chan);
768 g_object_unref (channel);
769 g_object_unref (tp_conn);
770 g_object_unref (connection);
777 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
784 const gchar *bus_name;
787 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
789 mc = empathy_mission_control_new ();
790 account = empathy_contact_get_account (contact);
792 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
793 /* The account is not connected. */
797 tp_conn = mission_control_get_connection (mc, account, NULL);
798 g_return_val_if_fail (tp_conn != NULL, NULL);
799 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
800 handle = empathy_contact_get_handle (contact);
802 text_chan = tp_conn_new_channel (tp_get_bus (),
805 TP_IFACE_CHANNEL_TYPE_TEXT,
806 TP_HANDLE_TYPE_CONTACT,
810 chat = empathy_tp_chat_new (account, text_chan);
812 g_object_unref (tp_conn);
813 g_object_unref (text_chan);
820 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
822 EmpathyTpChatPriv *priv;
824 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
826 priv = GET_PRIV (chat);
828 return priv->acknowledge;
832 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
833 gboolean acknowledge)
835 EmpathyTpChatPriv *priv;
837 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
839 priv = GET_PRIV (chat);
841 priv->acknowledge = acknowledge;
842 g_object_notify (G_OBJECT (chat), "acknowledge");
846 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
848 EmpathyTpChatPriv *priv;
850 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
852 priv = GET_PRIV (chat);
854 return priv->tp_chan;
858 empathy_tp_chat_get_account (EmpathyTpChat *chat)
860 EmpathyTpChatPriv *priv;
862 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
864 priv = GET_PRIV (chat);
866 return priv->account;
870 empathy_tp_chat_send (EmpathyTpChat *chat,
871 EmpathyMessage *message)
873 EmpathyTpChatPriv *priv;
874 const gchar *message_body;
875 EmpathyMessageType message_type;
877 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
878 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
880 priv = GET_PRIV (chat);
882 message_body = empathy_message_get_body (message);
883 message_type = empathy_message_get_type (message);
885 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
886 tp_cli_channel_type_text_call_send (priv->channel, -1,
890 "sending message", NULL,
895 empathy_tp_chat_set_state (EmpathyTpChat *chat,
896 TpChannelChatState state)
898 EmpathyTpChatPriv *priv;
900 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
902 priv = GET_PRIV (chat);
904 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
905 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
908 "setting chat state",
914 empathy_tp_chat_get_id (EmpathyTpChat *chat)
916 EmpathyTpChatPriv *priv;
918 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
920 priv = GET_PRIV (chat);
923 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);