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;
351 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
356 for (i = 0; i < messages_list->len; i++) {
357 EmpathyMessage *message;
358 GValueArray *message_struct;
359 const gchar *message_body;
366 message_struct = g_ptr_array_index (messages_list, i);
368 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
369 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
370 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
371 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
372 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
373 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
375 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
377 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
383 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
384 g_object_unref (message);
389 tp_chat_properties_changed_cb (TpProxy *proxy,
390 const GPtrArray *properties,
394 EmpathyTpChatPriv *priv = GET_PRIV (chat);
397 if (!priv->had_properties_list || !properties) {
401 for (i = 0; i < properties->len; i++) {
402 GValueArray *prop_struct;
403 TpChatProperty *property;
407 prop_struct = g_ptr_array_index (properties, i);
408 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
409 src_value = g_value_array_get_nth (prop_struct, 1);
411 for (j = 0; j < priv->properties->len; j++) {
412 property = g_ptr_array_index (priv->properties, j);
413 if (property->id == id) {
414 if (property->value) {
415 g_value_unset (property->value);
417 property->value = g_slice_new0 (GValue);
419 g_value_copy (src_value, property->value);
421 empathy_debug (DEBUG_DOMAIN, "property %s changed",
423 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
424 property->name, property->value);
432 tp_chat_get_properties_cb (TpProxy *proxy,
433 const GPtrArray *properties,
439 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
444 empathy_debug (DEBUG_DOMAIN, "Got value of properties");
445 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
449 tp_chat_list_properties_cb (TpProxy *proxy,
450 const GPtrArray *properties,
455 EmpathyTpChatPriv *priv = GET_PRIV (chat);
459 priv->had_properties_list = TRUE;
462 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
467 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
468 priv->properties = g_ptr_array_sized_new (properties->len);
469 for (i = 0; i < properties->len; i++) {
470 GValueArray *prop_struct;
471 TpChatProperty *property;
475 prop_struct = g_ptr_array_index (properties, i);
476 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
477 name = g_value_get_string (g_value_array_get_nth (prop_struct, 1));
479 empathy_debug (DEBUG_DOMAIN, "Adding property %s", name);
480 property = g_slice_new0 (TpChatProperty);
482 property->name = g_strdup (name);
483 g_ptr_array_add (priv->properties, property);
484 g_array_append_val (ids, id);
487 tp_cli_properties_interface_call_get_properties (proxy, -1,
489 tp_chat_get_properties_cb,
493 g_array_free (ids, TRUE);
497 empathy_tp_chat_set_property (EmpathyTpChat *chat,
500 /* FIXME: not implemented */
504 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
506 EmpathyTpChatPriv *priv = GET_PRIV (chat);
508 empathy_debug (DEBUG_DOMAIN, "Channel ready");
510 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
512 tp_chat_list_pending_messages_cb,
515 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
516 tp_chat_list_properties_cb,
521 tp_cli_channel_type_text_connect_to_received (priv->channel,
524 G_OBJECT (chat), NULL);
525 tp_cli_channel_type_text_connect_to_sent (priv->channel,
528 G_OBJECT (chat), NULL);
529 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
530 tp_chat_send_error_cb,
532 G_OBJECT (chat), NULL);
533 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
534 tp_chat_state_changed_cb,
536 G_OBJECT (chat), NULL);
537 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
538 tp_chat_state_changed_cb,
540 G_OBJECT (chat), NULL);
541 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
542 tp_chat_properties_changed_cb,
544 G_OBJECT (chat), NULL);
550 tp_chat_finalize (GObject *object)
552 EmpathyTpChatPriv *priv = GET_PRIV (object);
555 if (priv->acknowledge && priv->channel) {
556 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
557 tp_cli_channel_call_close (priv->channel, -1,
559 "closing channel", NULL,
564 g_signal_handlers_disconnect_by_func (priv->channel,
565 tp_chat_invalidated_cb,
567 g_object_unref (priv->channel);
570 g_object_unref (priv->tp_chan);
573 if (priv->properties) {
574 for (i = 0; i < priv->properties->len; i++) {
575 TpChatProperty *property;
577 property = g_ptr_array_index (priv->properties, i);
578 g_free (property->name);
579 if (property->value) {
580 g_value_unset (property->value);
581 g_slice_free (GValue, property->value);
583 g_slice_free (TpChatProperty, property);
585 g_ptr_array_free (priv->properties, TRUE);
588 g_object_unref (priv->factory);
589 g_object_unref (priv->user);
590 g_object_unref (priv->account);
591 g_object_unref (priv->mc);
594 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
598 tp_chat_constructor (GType type,
600 GObjectConstructParam *props)
603 EmpathyTpChatPriv *priv;
604 gboolean channel_ready;
606 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
608 priv = GET_PRIV (chat);
609 priv->factory = empathy_contact_factory_new ();
610 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
611 priv->mc = empathy_mission_control_new ();
613 g_signal_connect (priv->channel, "invalidated",
614 G_CALLBACK (tp_chat_invalidated_cb),
617 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
619 /* FIXME: We do that in a cb to let time to set the acknowledge
620 * property, this property should be required for construct. */
621 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
623 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
624 G_CALLBACK (tp_chat_channel_ready_cb),
632 tp_chat_get_property (GObject *object,
637 EmpathyTpChatPriv *priv = GET_PRIV (object);
641 g_value_set_object (value, priv->account);
644 g_value_set_object (value, priv->tp_chan);
647 g_value_set_object (value, priv->channel);
649 case PROP_ACKNOWLEDGE:
650 g_value_set_boolean (value, priv->acknowledge);
653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
659 tp_chat_set_property (GObject *object,
664 EmpathyTpChatPriv *priv = GET_PRIV (object);
668 priv->account = g_object_ref (g_value_get_object (value));
671 priv->tp_chan = g_object_ref (g_value_get_object (value));
674 priv->channel = g_object_ref (g_value_get_object (value));
676 case PROP_ACKNOWLEDGE:
677 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
678 g_value_get_boolean (value));
681 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
687 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
689 GObjectClass *object_class = G_OBJECT_CLASS (klass);
691 object_class->finalize = tp_chat_finalize;
692 object_class->constructor = tp_chat_constructor;
693 object_class->get_property = tp_chat_get_property;
694 object_class->set_property = tp_chat_set_property;
696 /* Construct properties */
697 g_object_class_install_property (object_class,
699 g_param_spec_object ("account",
701 "The account associated with the channel",
704 G_PARAM_CONSTRUCT_ONLY));
705 g_object_class_install_property (object_class,
707 g_param_spec_object ("tp-chan",
709 "The text channel for the chat",
712 G_PARAM_CONSTRUCT_ONLY));
714 g_object_class_install_property (object_class,
716 g_param_spec_object ("channel",
718 "The text channel for the chat",
721 G_PARAM_CONSTRUCT_ONLY));
723 g_object_class_install_property (object_class,
725 g_param_spec_boolean ("acknowledge",
726 "acknowledge messages",
727 "Wheter or not received messages should be acknowledged",
733 signals[MESSAGE_RECEIVED] =
734 g_signal_new ("message-received",
735 G_TYPE_FROM_CLASS (klass),
739 g_cclosure_marshal_VOID__OBJECT,
741 1, EMPATHY_TYPE_MESSAGE);
743 signals[SEND_ERROR] =
744 g_signal_new ("send-error",
745 G_TYPE_FROM_CLASS (klass),
749 _empathy_marshal_VOID__OBJECT_UINT,
751 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
753 signals[CHAT_STATE_CHANGED] =
754 g_signal_new ("chat-state-changed",
755 G_TYPE_FROM_CLASS (klass),
759 _empathy_marshal_VOID__OBJECT_UINT,
761 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
763 signals[PROPERTY_CHANGED] =
764 g_signal_new ("property-changed",
765 G_TYPE_FROM_CLASS (klass),
769 _empathy_marshal_VOID__STRING_BOXED,
771 2, G_TYPE_STRING, G_TYPE_VALUE);
774 g_signal_new ("destroy",
775 G_TYPE_FROM_CLASS (klass),
779 g_cclosure_marshal_VOID__VOID,
783 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
787 empathy_tp_chat_init (EmpathyTpChat *chat)
792 empathy_tp_chat_new (McAccount *account,
797 TpConnection *connection;
801 mc = empathy_mission_control_new ();
802 tp_conn = mission_control_get_connection (mc, account, NULL);
803 connection = tp_conn_dup_connection (tp_conn);
804 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
806 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
812 g_object_unref (channel);
813 g_object_unref (tp_conn);
814 g_object_unref (connection);
821 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
828 const gchar *bus_name;
831 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
833 mc = empathy_mission_control_new ();
834 account = empathy_contact_get_account (contact);
836 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
837 /* The account is not connected. */
841 tp_conn = mission_control_get_connection (mc, account, NULL);
842 g_return_val_if_fail (tp_conn != NULL, NULL);
843 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
844 handle = empathy_contact_get_handle (contact);
846 text_chan = tp_conn_new_channel (tp_get_bus (),
849 TP_IFACE_CHANNEL_TYPE_TEXT,
850 TP_HANDLE_TYPE_CONTACT,
854 chat = empathy_tp_chat_new (account, text_chan);
856 g_object_unref (tp_conn);
857 g_object_unref (text_chan);
864 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
866 EmpathyTpChatPriv *priv;
868 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
870 priv = GET_PRIV (chat);
872 return priv->acknowledge;
876 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
877 gboolean acknowledge)
879 EmpathyTpChatPriv *priv;
881 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
883 priv = GET_PRIV (chat);
885 priv->acknowledge = acknowledge;
886 g_object_notify (G_OBJECT (chat), "acknowledge");
890 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
892 EmpathyTpChatPriv *priv;
894 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
896 priv = GET_PRIV (chat);
898 return priv->tp_chan;
902 empathy_tp_chat_get_account (EmpathyTpChat *chat)
904 EmpathyTpChatPriv *priv;
906 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
908 priv = GET_PRIV (chat);
910 return priv->account;
914 empathy_tp_chat_send (EmpathyTpChat *chat,
915 EmpathyMessage *message)
917 EmpathyTpChatPriv *priv;
918 const gchar *message_body;
919 EmpathyMessageType message_type;
921 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
922 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
924 priv = GET_PRIV (chat);
926 message_body = empathy_message_get_body (message);
927 message_type = empathy_message_get_type (message);
929 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
930 tp_cli_channel_type_text_call_send (priv->channel, -1,
934 "sending message", NULL,
939 empathy_tp_chat_set_state (EmpathyTpChat *chat,
940 TpChannelChatState state)
942 EmpathyTpChatPriv *priv;
944 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
946 priv = GET_PRIV (chat);
948 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
949 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
952 "setting chat state",
958 empathy_tp_chat_get_id (EmpathyTpChat *chat)
960 EmpathyTpChatPriv *priv;
962 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
964 priv = GET_PRIV (chat);
967 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);