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 gboolean had_properties_list;
53 GPtrArray *properties;
62 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
63 static void empathy_tp_chat_init (EmpathyTpChat *chat);
82 static guint signals[LAST_SIGNAL];
84 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
87 tp_chat_invalidated_cb (TpProxy *proxy,
93 EmpathyTpChatPriv *priv = GET_PRIV (chat);
95 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
97 g_object_unref (priv->channel);
98 g_object_unref (priv->tp_chan);
100 priv->tp_chan = NULL;
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);
117 static EmpathyMessage *
118 tp_chat_build_message (EmpathyTpChat *chat,
122 const gchar *message_body)
124 EmpathyTpChatPriv *priv;
125 EmpathyMessage *message;
126 EmpathyContact *sender;
128 priv = GET_PRIV (chat);
130 if (from_handle == 0) {
131 sender = g_object_ref (priv->user);
133 sender = empathy_contact_factory_get_from_handle (priv->factory,
138 message = empathy_message_new (message_body);
139 empathy_message_set_type (message, type);
140 empathy_message_set_sender (message, sender);
141 empathy_message_set_receiver (message, priv->user);
142 empathy_message_set_timestamp (message, timestamp);
144 g_object_unref (sender);
150 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
151 GParamSpec *param_spec,
154 EmpathyTpChatPriv *priv = GET_PRIV (chat);
155 EmpathyMessage *message;
156 EmpathyContactReady ready;
157 EmpathyContact *sender;
158 gboolean removed = FALSE;
160 /* Emit all messages queued until we find a message with not
161 * ready sender. When leaving this loop, sender is the first not ready
162 * contact queued and removed tells if at least one message got removed
164 while (priv->message_queue) {
165 message = priv->message_queue->data;
166 sender = empathy_message_get_sender (message);
167 ready = empathy_contact_get_ready (sender);
169 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
173 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
174 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
175 priv->message_queue = g_slist_remove (priv->message_queue,
177 g_object_unref (message);
182 g_signal_handlers_disconnect_by_func (contact,
183 tp_chat_sender_ready_notify_cb,
186 if (priv->message_queue) {
187 g_signal_connect (sender, "notify::ready",
188 G_CALLBACK (tp_chat_sender_ready_notify_cb),
195 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
196 EmpathyMessage *message)
198 EmpathyTpChatPriv *priv = GET_PRIV (chat);
199 EmpathyContact *sender;
200 EmpathyContactReady ready;
202 if (priv->message_queue != NULL) {
203 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
204 priv->message_queue = g_slist_append (priv->message_queue,
205 g_object_ref (message));
209 sender = empathy_message_get_sender (message);
210 ready = empathy_contact_get_ready (sender);
211 if (ready & EMPATHY_CONTACT_READY_NAME) {
212 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
213 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
217 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
218 priv->message_queue = g_slist_append (priv->message_queue,
219 g_object_ref (message));
220 g_signal_connect (sender, "notify::ready",
221 G_CALLBACK (tp_chat_sender_ready_notify_cb),
226 tp_chat_received_cb (TpChannel *channel,
232 const gchar *message_body,
236 EmpathyTpChatPriv *priv = GET_PRIV (chat);
237 EmpathyMessage *message;
239 if (!priv->had_pending_messages) {
243 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
245 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
251 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
252 g_object_unref (message);
254 if (priv->acknowledge) {
257 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
258 g_array_append_val (message_ids, message_id);
259 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
263 "acknowledging pending messages",
266 g_array_free (message_ids, TRUE);
271 tp_chat_sent_cb (TpChannel *channel,
274 const gchar *message_body,
278 EmpathyMessage *message;
280 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
282 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
288 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
289 g_object_unref (message);
293 tp_chat_send_error_cb (TpChannel *channel,
297 const gchar *message_body,
301 EmpathyMessage *message;
303 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
304 message_body, error_code);
306 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
312 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
313 g_object_unref (message);
317 tp_chat_state_changed_cb (TpChannel *channel,
323 EmpathyTpChatPriv *priv = GET_PRIV (chat);
324 EmpathyContact *contact;
326 contact = empathy_contact_factory_get_from_handle (priv->factory,
330 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
331 empathy_contact_get_name (contact),
334 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
335 g_object_unref (contact);
339 tp_chat_list_pending_messages_cb (TpChannel *channel,
340 const GPtrArray *messages_list,
345 EmpathyTpChatPriv *priv = GET_PRIV (chat);
348 priv->had_pending_messages = TRUE;
350 for (i = 0; i < messages_list->len; i++) {
351 EmpathyMessage *message;
352 GValueArray *message_struct;
353 const gchar *message_body;
360 message_struct = g_ptr_array_index (messages_list, i);
362 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
363 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
364 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
365 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
366 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
367 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
369 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
371 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
377 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
378 g_object_unref (message);
383 tp_chat_properties_changed_cb (TpProxy *proxy,
384 const GPtrArray *properties,
388 EmpathyTpChatPriv *priv = GET_PRIV (chat);
391 if (!priv->had_properties_list || !properties) {
395 for (i = 0; i < properties->len; i++) {
396 GValueArray *prop_struct;
397 TpChatProperty *property;
401 prop_struct = g_ptr_array_index (properties, i);
402 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
403 src_value = g_value_array_get_nth (prop_struct, 1);
405 for (j = 0; j < priv->properties->len; j++) {
406 property = g_ptr_array_index (priv->properties, j);
407 if (property->id == id) {
408 if (property->value) {
409 g_value_unset (property->value);
411 property->value = g_slice_new0 (GValue);
413 g_value_copy (src_value, property->value);
415 empathy_debug (DEBUG_DOMAIN, "property %s changed",
417 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
418 property->name, property->value);
426 tp_chat_get_properties_cb (TpProxy *proxy,
427 const GPtrArray *properties,
432 empathy_debug (DEBUG_DOMAIN, "Got value of properties");
433 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
437 tp_chat_list_properties_cb (TpProxy *proxy,
438 const GPtrArray *properties,
443 EmpathyTpChatPriv *priv = GET_PRIV (chat);
447 priv->had_properties_list = TRUE;
449 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
450 priv->properties = g_ptr_array_sized_new (properties->len);
451 for (i = 0; i < properties->len; i++) {
452 GValueArray *prop_struct;
453 TpChatProperty *property;
457 prop_struct = g_ptr_array_index (properties, i);
458 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
459 name = g_value_get_string (g_value_array_get_nth (prop_struct, 1));
461 empathy_debug (DEBUG_DOMAIN, "Adding property %s", name);
462 property = g_slice_new0 (TpChatProperty);
464 property->name = g_strdup (name);
465 g_ptr_array_add (priv->properties, property);
466 g_array_append_val (ids, id);
469 tp_cli_properties_interface_call_get_properties (proxy, -1,
471 tp_chat_get_properties_cb,
475 g_array_free (ids, TRUE);
479 empathy_tp_chat_set_property (EmpathyTpChat *chat,
482 /* FIXME: not implemented */
486 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
488 EmpathyTpChatPriv *priv = GET_PRIV (chat);
490 empathy_debug (DEBUG_DOMAIN, "Channel ready");
492 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
494 tp_chat_list_pending_messages_cb,
497 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
498 tp_chat_list_properties_cb,
503 tp_cli_channel_type_text_connect_to_received (priv->channel,
506 G_OBJECT (chat), NULL);
507 tp_cli_channel_type_text_connect_to_sent (priv->channel,
510 G_OBJECT (chat), NULL);
511 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
512 tp_chat_send_error_cb,
514 G_OBJECT (chat), NULL);
515 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
516 tp_chat_state_changed_cb,
518 G_OBJECT (chat), NULL);
519 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
520 tp_chat_state_changed_cb,
522 G_OBJECT (chat), NULL);
523 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
524 tp_chat_properties_changed_cb,
526 G_OBJECT (chat), NULL);
532 tp_chat_finalize (GObject *object)
534 EmpathyTpChatPriv *priv = GET_PRIV (object);
537 if (priv->acknowledge && priv->channel) {
538 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
539 tp_cli_channel_call_close (priv->channel, -1,
541 "closing channel", NULL,
546 g_signal_handlers_disconnect_by_func (priv->channel,
547 tp_chat_invalidated_cb,
549 g_object_unref (priv->channel);
552 g_object_unref (priv->tp_chan);
555 if (priv->properties) {
556 for (i = 0; i < priv->properties->len; i++) {
557 TpChatProperty *property;
559 property = g_ptr_array_index (priv->properties, i);
560 g_free (property->name);
561 if (property->value) {
562 g_value_unset (property->value);
563 g_slice_free (GValue, property->value);
565 g_slice_free (TpChatProperty, property);
567 g_ptr_array_free (priv->properties, TRUE);
570 g_object_unref (priv->factory);
571 g_object_unref (priv->user);
572 g_object_unref (priv->account);
573 g_object_unref (priv->mc);
576 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
580 tp_chat_constructor (GType type,
582 GObjectConstructParam *props)
585 EmpathyTpChatPriv *priv;
586 gboolean channel_ready;
588 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
590 priv = GET_PRIV (chat);
591 priv->factory = empathy_contact_factory_new ();
592 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
593 priv->mc = empathy_mission_control_new ();
595 g_signal_connect (priv->channel, "invalidated",
596 G_CALLBACK (tp_chat_invalidated_cb),
599 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
601 /* FIXME: We do that in a cb to let time to set the acknowledge
602 * property, this property should be required for construct. */
603 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
605 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
606 G_CALLBACK (tp_chat_channel_ready_cb),
614 tp_chat_get_property (GObject *object,
619 EmpathyTpChatPriv *priv = GET_PRIV (object);
623 g_value_set_object (value, priv->account);
626 g_value_set_object (value, priv->tp_chan);
629 g_value_set_object (value, priv->channel);
631 case PROP_ACKNOWLEDGE:
632 g_value_set_boolean (value, priv->acknowledge);
635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
641 tp_chat_set_property (GObject *object,
646 EmpathyTpChatPriv *priv = GET_PRIV (object);
650 priv->account = g_object_ref (g_value_get_object (value));
653 priv->tp_chan = g_object_ref (g_value_get_object (value));
656 priv->channel = g_object_ref (g_value_get_object (value));
658 case PROP_ACKNOWLEDGE:
659 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
660 g_value_get_boolean (value));
663 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
669 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
671 GObjectClass *object_class = G_OBJECT_CLASS (klass);
673 object_class->finalize = tp_chat_finalize;
674 object_class->constructor = tp_chat_constructor;
675 object_class->get_property = tp_chat_get_property;
676 object_class->set_property = tp_chat_set_property;
678 /* Construct properties */
679 g_object_class_install_property (object_class,
681 g_param_spec_object ("account",
683 "The account associated with the channel",
686 G_PARAM_CONSTRUCT_ONLY));
687 g_object_class_install_property (object_class,
689 g_param_spec_object ("tp-chan",
691 "The text channel for the chat",
694 G_PARAM_CONSTRUCT_ONLY));
696 g_object_class_install_property (object_class,
698 g_param_spec_object ("channel",
700 "The text channel for the chat",
703 G_PARAM_CONSTRUCT_ONLY));
705 g_object_class_install_property (object_class,
707 g_param_spec_boolean ("acknowledge",
708 "acknowledge messages",
709 "Wheter or not received messages should be acknowledged",
715 signals[MESSAGE_RECEIVED] =
716 g_signal_new ("message-received",
717 G_TYPE_FROM_CLASS (klass),
721 g_cclosure_marshal_VOID__OBJECT,
723 1, EMPATHY_TYPE_MESSAGE);
725 signals[SEND_ERROR] =
726 g_signal_new ("send-error",
727 G_TYPE_FROM_CLASS (klass),
731 _empathy_marshal_VOID__OBJECT_UINT,
733 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
735 signals[CHAT_STATE_CHANGED] =
736 g_signal_new ("chat-state-changed",
737 G_TYPE_FROM_CLASS (klass),
741 _empathy_marshal_VOID__OBJECT_UINT,
743 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
745 signals[PROPERTY_CHANGED] =
746 g_signal_new ("property-changed",
747 G_TYPE_FROM_CLASS (klass),
751 _empathy_marshal_VOID__STRING_BOXED,
753 2, G_TYPE_STRING, G_TYPE_VALUE);
756 g_signal_new ("destroy",
757 G_TYPE_FROM_CLASS (klass),
761 g_cclosure_marshal_VOID__VOID,
765 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
769 empathy_tp_chat_init (EmpathyTpChat *chat)
774 empathy_tp_chat_new (McAccount *account,
779 TpConnection *connection;
783 mc = empathy_mission_control_new ();
784 tp_conn = mission_control_get_connection (mc, account, NULL);
785 connection = tp_conn_dup_connection (tp_conn);
786 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
788 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
794 g_object_unref (channel);
795 g_object_unref (tp_conn);
796 g_object_unref (connection);
803 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
810 const gchar *bus_name;
813 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
815 mc = empathy_mission_control_new ();
816 account = empathy_contact_get_account (contact);
818 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
819 /* The account is not connected. */
823 tp_conn = mission_control_get_connection (mc, account, NULL);
824 g_return_val_if_fail (tp_conn != NULL, NULL);
825 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
826 handle = empathy_contact_get_handle (contact);
828 text_chan = tp_conn_new_channel (tp_get_bus (),
831 TP_IFACE_CHANNEL_TYPE_TEXT,
832 TP_HANDLE_TYPE_CONTACT,
836 chat = empathy_tp_chat_new (account, text_chan);
838 g_object_unref (tp_conn);
839 g_object_unref (text_chan);
846 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
848 EmpathyTpChatPriv *priv;
850 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
852 priv = GET_PRIV (chat);
854 return priv->acknowledge;
858 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
859 gboolean acknowledge)
861 EmpathyTpChatPriv *priv;
863 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
865 priv = GET_PRIV (chat);
867 priv->acknowledge = acknowledge;
868 g_object_notify (G_OBJECT (chat), "acknowledge");
872 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
874 EmpathyTpChatPriv *priv;
876 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
878 priv = GET_PRIV (chat);
880 return priv->tp_chan;
884 empathy_tp_chat_get_account (EmpathyTpChat *chat)
886 EmpathyTpChatPriv *priv;
888 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
890 priv = GET_PRIV (chat);
892 return priv->account;
896 empathy_tp_chat_send (EmpathyTpChat *chat,
897 EmpathyMessage *message)
899 EmpathyTpChatPriv *priv;
900 const gchar *message_body;
901 EmpathyMessageType message_type;
903 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
904 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
906 priv = GET_PRIV (chat);
908 message_body = empathy_message_get_body (message);
909 message_type = empathy_message_get_type (message);
911 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
912 tp_cli_channel_type_text_call_send (priv->channel, -1,
916 "sending message", NULL,
921 empathy_tp_chat_set_state (EmpathyTpChat *chat,
922 TpChannelChatState state)
924 EmpathyTpChatPriv *priv;
926 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
928 priv = GET_PRIV (chat);
930 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
931 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
934 "setting chat state",
940 empathy_tp_chat_get_id (EmpathyTpChat *chat)
942 EmpathyTpChatPriv *priv;
944 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
946 priv = GET_PRIV (chat);
949 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);