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);
81 static guint signals[LAST_SIGNAL];
83 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
86 tp_chat_invalidated_cb (TpProxy *proxy,
92 EmpathyTpChatPriv *priv = GET_PRIV (chat);
94 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
96 g_object_unref (priv->channel);
97 g_object_unref (priv->tp_chan);
101 g_signal_emit (chat, signals[DESTROY], 0);
105 tp_chat_async_cb (TpChannel *proxy,
108 GObject *weak_object)
111 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
112 user_data, error->message);
116 static EmpathyMessage *
117 tp_chat_build_message (EmpathyTpChat *chat,
121 const gchar *message_body)
123 EmpathyTpChatPriv *priv;
124 EmpathyMessage *message;
125 EmpathyContact *sender;
127 priv = GET_PRIV (chat);
129 if (from_handle == 0) {
130 sender = g_object_ref (priv->user);
132 sender = empathy_contact_factory_get_from_handle (priv->factory,
137 message = empathy_message_new (message_body);
138 empathy_message_set_type (message, type);
139 empathy_message_set_sender (message, sender);
140 empathy_message_set_receiver (message, priv->user);
141 empathy_message_set_timestamp (message, timestamp);
143 g_object_unref (sender);
149 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
150 GParamSpec *param_spec,
153 EmpathyTpChatPriv *priv = GET_PRIV (chat);
154 EmpathyMessage *message;
155 EmpathyContactReady ready;
156 EmpathyContact *sender;
157 gboolean removed = FALSE;
159 /* Emit all messages queued until we find a message with not
160 * ready sender. When leaving this loop, sender is the first not ready
161 * contact queued and removed tells if at least one message got removed
163 while (priv->message_queue) {
164 message = priv->message_queue->data;
165 sender = empathy_message_get_sender (message);
166 ready = empathy_contact_get_ready (sender);
168 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
172 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
173 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
174 priv->message_queue = g_slist_remove (priv->message_queue,
176 g_object_unref (message);
181 g_signal_handlers_disconnect_by_func (contact,
182 tp_chat_sender_ready_notify_cb,
185 if (priv->message_queue) {
186 g_signal_connect (sender, "notify::ready",
187 G_CALLBACK (tp_chat_sender_ready_notify_cb),
194 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
195 EmpathyMessage *message)
197 EmpathyTpChatPriv *priv = GET_PRIV (chat);
198 EmpathyContact *sender;
199 EmpathyContactReady ready;
201 if (priv->message_queue != NULL) {
202 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
203 priv->message_queue = g_slist_append (priv->message_queue,
204 g_object_ref (message));
208 sender = empathy_message_get_sender (message);
209 ready = empathy_contact_get_ready (sender);
210 if (ready & EMPATHY_CONTACT_READY_NAME) {
211 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
212 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
216 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
217 priv->message_queue = g_slist_append (priv->message_queue,
218 g_object_ref (message));
219 g_signal_connect (sender, "notify::ready",
220 G_CALLBACK (tp_chat_sender_ready_notify_cb),
225 tp_chat_received_cb (TpChannel *channel,
231 const gchar *message_body,
235 EmpathyTpChatPriv *priv = GET_PRIV (chat);
236 EmpathyMessage *message;
238 if (!priv->had_pending_messages) {
242 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
244 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
250 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
251 g_object_unref (message);
253 if (priv->acknowledge) {
256 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
257 g_array_append_val (message_ids, message_id);
258 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
262 "acknowledging pending messages",
265 g_array_free (message_ids, TRUE);
270 tp_chat_sent_cb (TpChannel *channel,
273 const gchar *message_body,
277 EmpathyMessage *message;
279 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
281 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
287 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
288 g_object_unref (message);
292 tp_chat_send_error_cb (TpChannel *channel,
296 const gchar *message_body,
300 EmpathyMessage *message;
302 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
303 message_body, error_code);
305 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
311 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
312 g_object_unref (message);
316 tp_chat_state_changed_cb (TpChannel *channel,
322 EmpathyTpChatPriv *priv = GET_PRIV (chat);
323 EmpathyContact *contact;
325 contact = empathy_contact_factory_get_from_handle (priv->factory,
329 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
330 empathy_contact_get_name (contact),
333 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
334 g_object_unref (contact);
338 tp_chat_list_pending_messages_cb (TpChannel *channel,
339 const GPtrArray *messages_list,
344 EmpathyTpChatPriv *priv = GET_PRIV (chat);
347 priv->had_pending_messages = TRUE;
349 for (i = 0; i < messages_list->len; i++) {
350 EmpathyMessage *message;
351 GValueArray *message_struct;
352 const gchar *message_body;
359 message_struct = g_ptr_array_index (messages_list, i);
361 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
362 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
363 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
364 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
365 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
366 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
368 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
370 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
376 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
377 g_object_unref (message);
382 tp_chat_properties_changed_cb (TpProxy *proxy,
383 const GPtrArray *properties,
387 EmpathyTpChatPriv *priv = GET_PRIV (chat);
390 for (i = 0; i < properties->len; i++) {
391 GValueArray *prop_struct;
392 TpChatProperty *property;
396 prop_struct = g_ptr_array_index (properties, i);
397 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
398 src_value = g_value_array_get_nth (prop_struct, 1);
400 for (j = 0; j < priv->properties->len; j++) {
401 property = g_ptr_array_index (priv->properties, j);
402 if (property->id == id) {
403 if (property->value) {
404 g_value_unset (property->value);
406 property->value = g_slice_new0 (GValue);
408 g_value_copy (src_value, property->value);
410 empathy_debug (DEBUG_DOMAIN, "property %s changed",
412 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
413 property->name, property->value);
421 tp_chat_get_properties_cb (TpProxy *proxy,
422 const GPtrArray *properties,
427 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
431 tp_chat_list_properties_cb (TpProxy *proxy,
432 const GPtrArray *properties,
437 EmpathyTpChatPriv *priv = GET_PRIV (chat);
441 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
442 priv->properties = g_ptr_array_sized_new (properties->len);
443 for (i = 0; i < properties->len; i++) {
444 GValueArray *prop_struct;
445 TpChatProperty *property;
449 prop_struct = g_ptr_array_index (properties, i);
450 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
451 name = g_value_get_string (g_value_array_get_nth (prop_struct, 1));
453 property = g_slice_new0 (TpChatProperty);
455 property->name = g_strdup (name);
456 g_ptr_array_add (priv->properties, property);
457 g_array_append_val (ids, id);
460 tp_cli_properties_interface_call_get_properties (proxy, -1,
462 tp_chat_get_properties_cb,
466 g_array_free (ids, TRUE);
470 empathy_tp_chat_set_property (EmpathyTpChat *chat,
473 /* FIXME: not implemented */
477 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
479 EmpathyTpChatPriv *priv = GET_PRIV (chat);
481 empathy_debug (DEBUG_DOMAIN, "Channel ready");
483 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
485 tp_chat_list_pending_messages_cb,
488 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
489 tp_chat_list_properties_cb,
494 tp_cli_channel_type_text_connect_to_received (priv->channel,
497 G_OBJECT (chat), NULL);
498 tp_cli_channel_type_text_connect_to_sent (priv->channel,
501 G_OBJECT (chat), NULL);
502 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
503 tp_chat_send_error_cb,
505 G_OBJECT (chat), NULL);
506 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
507 tp_chat_state_changed_cb,
509 G_OBJECT (chat), NULL);
510 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
511 tp_chat_state_changed_cb,
513 G_OBJECT (chat), NULL);
514 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
515 tp_chat_properties_changed_cb,
517 G_OBJECT (chat), NULL);
523 tp_chat_finalize (GObject *object)
525 EmpathyTpChatPriv *priv = GET_PRIV (object);
528 if (priv->acknowledge && priv->channel) {
529 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
530 tp_cli_channel_call_close (priv->channel, -1,
532 "closing channel", NULL,
537 g_signal_handlers_disconnect_by_func (priv->channel,
538 tp_chat_invalidated_cb,
540 g_object_unref (priv->channel);
543 g_object_unref (priv->tp_chan);
546 for (i = 0; i < priv->properties->len; i++) {
547 TpChatProperty *property;
549 property = g_ptr_array_index (priv->properties, i);
550 g_free (property->name);
551 g_value_unset (property->value);
552 g_slice_free (GValue, property->value);
553 g_slice_free (TpChatProperty, property);
555 g_ptr_array_free (priv->properties, TRUE);
557 g_object_unref (priv->factory);
558 g_object_unref (priv->user);
559 g_object_unref (priv->account);
560 g_object_unref (priv->mc);
563 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
567 tp_chat_constructor (GType type,
569 GObjectConstructParam *props)
572 EmpathyTpChatPriv *priv;
573 gboolean channel_ready;
575 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
577 priv = GET_PRIV (chat);
578 priv->factory = empathy_contact_factory_new ();
579 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
580 priv->mc = empathy_mission_control_new ();
582 g_signal_connect (priv->channel, "invalidated",
583 G_CALLBACK (tp_chat_invalidated_cb),
586 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
588 /* FIXME: We do that in a cb to let time to set the acknowledge
589 * property, this property should be required for construct. */
590 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
592 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
593 G_CALLBACK (tp_chat_channel_ready_cb),
601 tp_chat_get_property (GObject *object,
606 EmpathyTpChatPriv *priv = GET_PRIV (object);
610 g_value_set_object (value, priv->account);
613 g_value_set_object (value, priv->tp_chan);
616 g_value_set_object (value, priv->channel);
618 case PROP_ACKNOWLEDGE:
619 g_value_set_boolean (value, priv->acknowledge);
622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
628 tp_chat_set_property (GObject *object,
633 EmpathyTpChatPriv *priv = GET_PRIV (object);
637 priv->account = g_object_ref (g_value_get_object (value));
640 priv->tp_chan = g_object_ref (g_value_get_object (value));
643 priv->channel = g_object_ref (g_value_get_object (value));
645 case PROP_ACKNOWLEDGE:
646 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
647 g_value_get_boolean (value));
650 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
656 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
658 GObjectClass *object_class = G_OBJECT_CLASS (klass);
660 object_class->finalize = tp_chat_finalize;
661 object_class->constructor = tp_chat_constructor;
662 object_class->get_property = tp_chat_get_property;
663 object_class->set_property = tp_chat_set_property;
665 /* Construct properties */
666 g_object_class_install_property (object_class,
668 g_param_spec_object ("account",
670 "The account associated with the channel",
673 G_PARAM_CONSTRUCT_ONLY));
674 g_object_class_install_property (object_class,
676 g_param_spec_object ("tp-chan",
678 "The text channel for the chat",
681 G_PARAM_CONSTRUCT_ONLY));
683 g_object_class_install_property (object_class,
685 g_param_spec_object ("channel",
687 "The text channel for the chat",
690 G_PARAM_CONSTRUCT_ONLY));
692 g_object_class_install_property (object_class,
694 g_param_spec_boolean ("acknowledge",
695 "acknowledge messages",
696 "Wheter or not received messages should be acknowledged",
702 signals[MESSAGE_RECEIVED] =
703 g_signal_new ("message-received",
704 G_TYPE_FROM_CLASS (klass),
708 g_cclosure_marshal_VOID__OBJECT,
710 1, EMPATHY_TYPE_MESSAGE);
712 signals[SEND_ERROR] =
713 g_signal_new ("send-error",
714 G_TYPE_FROM_CLASS (klass),
718 _empathy_marshal_VOID__OBJECT_UINT,
720 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
722 signals[CHAT_STATE_CHANGED] =
723 g_signal_new ("chat-state-changed",
724 G_TYPE_FROM_CLASS (klass),
728 _empathy_marshal_VOID__OBJECT_UINT,
730 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
732 signals[PROPERTY_CHANGED] =
733 g_signal_new ("property-changed",
734 G_TYPE_FROM_CLASS (klass),
738 _empathy_marshal_VOID__STRING_BOXED,
740 2, G_TYPE_STRING, G_TYPE_VALUE);
743 g_signal_new ("destroy",
744 G_TYPE_FROM_CLASS (klass),
748 g_cclosure_marshal_VOID__VOID,
752 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
756 empathy_tp_chat_init (EmpathyTpChat *chat)
761 empathy_tp_chat_new (McAccount *account,
766 TpConnection *connection;
770 mc = empathy_mission_control_new ();
771 tp_conn = mission_control_get_connection (mc, account, NULL);
772 connection = tp_conn_dup_connection (tp_conn);
773 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
775 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
781 g_object_unref (channel);
782 g_object_unref (tp_conn);
783 g_object_unref (connection);
790 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
797 const gchar *bus_name;
800 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
802 mc = empathy_mission_control_new ();
803 account = empathy_contact_get_account (contact);
805 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
806 /* The account is not connected. */
810 tp_conn = mission_control_get_connection (mc, account, NULL);
811 g_return_val_if_fail (tp_conn != NULL, NULL);
812 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
813 handle = empathy_contact_get_handle (contact);
815 text_chan = tp_conn_new_channel (tp_get_bus (),
818 TP_IFACE_CHANNEL_TYPE_TEXT,
819 TP_HANDLE_TYPE_CONTACT,
823 chat = empathy_tp_chat_new (account, text_chan);
825 g_object_unref (tp_conn);
826 g_object_unref (text_chan);
833 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
835 EmpathyTpChatPriv *priv;
837 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
839 priv = GET_PRIV (chat);
841 return priv->acknowledge;
845 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
846 gboolean acknowledge)
848 EmpathyTpChatPriv *priv;
850 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
852 priv = GET_PRIV (chat);
854 priv->acknowledge = acknowledge;
855 g_object_notify (G_OBJECT (chat), "acknowledge");
859 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
861 EmpathyTpChatPriv *priv;
863 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
865 priv = GET_PRIV (chat);
867 return priv->tp_chan;
871 empathy_tp_chat_get_account (EmpathyTpChat *chat)
873 EmpathyTpChatPriv *priv;
875 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
877 priv = GET_PRIV (chat);
879 return priv->account;
883 empathy_tp_chat_send (EmpathyTpChat *chat,
884 EmpathyMessage *message)
886 EmpathyTpChatPriv *priv;
887 const gchar *message_body;
888 EmpathyMessageType message_type;
890 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
891 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
893 priv = GET_PRIV (chat);
895 message_body = empathy_message_get_body (message);
896 message_type = empathy_message_get_type (message);
898 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
899 tp_cli_channel_type_text_call_send (priv->channel, -1,
903 "sending message", NULL,
908 empathy_tp_chat_set_state (EmpathyTpChat *chat,
909 TpChannelChatState state)
911 EmpathyTpChatPriv *priv;
913 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
915 priv = GET_PRIV (chat);
917 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
918 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
921 "setting chat state",
927 empathy_tp_chat_get_id (EmpathyTpChat *chat)
929 EmpathyTpChatPriv *priv;
931 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
933 priv = GET_PRIV (chat);
936 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);