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 g_value_init (&id, G_TYPE_UINT);
516 g_value_init (&dest_value, G_TYPE_VALUE);
517 g_value_set_uint (&id, property->id);
518 g_value_set_boxed (&dest_value, value);
520 prop = g_value_array_new (2);
521 g_value_array_append (prop, &id);
522 g_value_array_append (prop, &dest_value);
524 properties = g_ptr_array_sized_new (1);
525 g_ptr_array_add (properties, prop);
527 empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
528 tp_cli_properties_interface_call_set_properties (priv->channel, -1,
530 (tp_cli_properties_interface_callback_for_set_properties)
532 "Seting property", NULL,
541 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
543 EmpathyTpChatPriv *priv = GET_PRIV (chat);
545 empathy_debug (DEBUG_DOMAIN, "Channel ready");
547 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
549 tp_chat_list_pending_messages_cb,
552 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
553 tp_chat_list_properties_cb,
558 tp_cli_channel_type_text_connect_to_received (priv->channel,
561 G_OBJECT (chat), NULL);
562 tp_cli_channel_type_text_connect_to_sent (priv->channel,
565 G_OBJECT (chat), NULL);
566 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
567 tp_chat_send_error_cb,
569 G_OBJECT (chat), NULL);
570 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
571 tp_chat_state_changed_cb,
573 G_OBJECT (chat), NULL);
574 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
575 tp_chat_state_changed_cb,
577 G_OBJECT (chat), NULL);
578 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
579 tp_chat_properties_changed_cb,
581 G_OBJECT (chat), NULL);
587 tp_chat_finalize (GObject *object)
589 EmpathyTpChatPriv *priv = GET_PRIV (object);
592 if (priv->acknowledge && priv->channel) {
593 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
594 tp_cli_channel_call_close (priv->channel, -1,
596 "closing channel", NULL,
601 g_signal_handlers_disconnect_by_func (priv->channel,
602 tp_chat_invalidated_cb,
604 g_object_unref (priv->channel);
607 g_object_unref (priv->tp_chan);
610 if (priv->properties) {
611 for (i = 0; i < priv->properties->len; i++) {
612 TpChatProperty *property;
614 property = g_ptr_array_index (priv->properties, i);
615 g_free (property->name);
616 if (property->value) {
617 g_value_unset (property->value);
618 g_slice_free (GValue, property->value);
620 g_slice_free (TpChatProperty, property);
622 g_ptr_array_free (priv->properties, TRUE);
625 g_object_unref (priv->factory);
626 g_object_unref (priv->user);
627 g_object_unref (priv->account);
628 g_object_unref (priv->mc);
631 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
635 tp_chat_constructor (GType type,
637 GObjectConstructParam *props)
640 EmpathyTpChatPriv *priv;
641 gboolean channel_ready;
643 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
645 priv = GET_PRIV (chat);
646 priv->factory = empathy_contact_factory_new ();
647 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
648 priv->mc = empathy_mission_control_new ();
650 g_signal_connect (priv->channel, "invalidated",
651 G_CALLBACK (tp_chat_invalidated_cb),
654 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
656 /* FIXME: We do that in a cb to let time to set the acknowledge
657 * property, this property should be required for construct. */
658 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
660 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
661 G_CALLBACK (tp_chat_channel_ready_cb),
669 tp_chat_get_property (GObject *object,
674 EmpathyTpChatPriv *priv = GET_PRIV (object);
678 g_value_set_object (value, priv->account);
681 g_value_set_object (value, priv->tp_chan);
684 g_value_set_object (value, priv->channel);
686 case PROP_ACKNOWLEDGE:
687 g_value_set_boolean (value, priv->acknowledge);
690 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
696 tp_chat_set_property (GObject *object,
701 EmpathyTpChatPriv *priv = GET_PRIV (object);
705 priv->account = g_object_ref (g_value_get_object (value));
708 priv->tp_chan = g_object_ref (g_value_get_object (value));
711 priv->channel = g_object_ref (g_value_get_object (value));
713 case PROP_ACKNOWLEDGE:
714 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
715 g_value_get_boolean (value));
718 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
724 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
726 GObjectClass *object_class = G_OBJECT_CLASS (klass);
728 object_class->finalize = tp_chat_finalize;
729 object_class->constructor = tp_chat_constructor;
730 object_class->get_property = tp_chat_get_property;
731 object_class->set_property = tp_chat_set_property;
733 /* Construct properties */
734 g_object_class_install_property (object_class,
736 g_param_spec_object ("account",
738 "The account associated with the channel",
741 G_PARAM_CONSTRUCT_ONLY));
742 g_object_class_install_property (object_class,
744 g_param_spec_object ("tp-chan",
746 "The text channel for the chat",
749 G_PARAM_CONSTRUCT_ONLY));
751 g_object_class_install_property (object_class,
753 g_param_spec_object ("channel",
755 "The text channel for the chat",
758 G_PARAM_CONSTRUCT_ONLY));
760 g_object_class_install_property (object_class,
762 g_param_spec_boolean ("acknowledge",
763 "acknowledge messages",
764 "Wheter or not received messages should be acknowledged",
770 signals[MESSAGE_RECEIVED] =
771 g_signal_new ("message-received",
772 G_TYPE_FROM_CLASS (klass),
776 g_cclosure_marshal_VOID__OBJECT,
778 1, EMPATHY_TYPE_MESSAGE);
780 signals[SEND_ERROR] =
781 g_signal_new ("send-error",
782 G_TYPE_FROM_CLASS (klass),
786 _empathy_marshal_VOID__OBJECT_UINT,
788 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
790 signals[CHAT_STATE_CHANGED] =
791 g_signal_new ("chat-state-changed",
792 G_TYPE_FROM_CLASS (klass),
796 _empathy_marshal_VOID__OBJECT_UINT,
798 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
800 signals[PROPERTY_CHANGED] =
801 g_signal_new ("property-changed",
802 G_TYPE_FROM_CLASS (klass),
806 _empathy_marshal_VOID__STRING_BOXED,
808 2, G_TYPE_STRING, G_TYPE_VALUE);
811 g_signal_new ("destroy",
812 G_TYPE_FROM_CLASS (klass),
816 g_cclosure_marshal_VOID__VOID,
820 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
824 empathy_tp_chat_init (EmpathyTpChat *chat)
829 empathy_tp_chat_new (McAccount *account,
834 TpConnection *connection;
838 mc = empathy_mission_control_new ();
839 tp_conn = mission_control_get_connection (mc, account, NULL);
840 connection = tp_conn_dup_connection (tp_conn);
841 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
843 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
849 g_object_unref (channel);
850 g_object_unref (tp_conn);
851 g_object_unref (connection);
858 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
865 const gchar *bus_name;
868 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
870 mc = empathy_mission_control_new ();
871 account = empathy_contact_get_account (contact);
873 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
874 /* The account is not connected. */
878 tp_conn = mission_control_get_connection (mc, account, NULL);
879 g_return_val_if_fail (tp_conn != NULL, NULL);
880 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
881 handle = empathy_contact_get_handle (contact);
883 text_chan = tp_conn_new_channel (tp_get_bus (),
886 TP_IFACE_CHANNEL_TYPE_TEXT,
887 TP_HANDLE_TYPE_CONTACT,
891 chat = empathy_tp_chat_new (account, text_chan);
893 g_object_unref (tp_conn);
894 g_object_unref (text_chan);
901 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
903 EmpathyTpChatPriv *priv;
905 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
907 priv = GET_PRIV (chat);
909 return priv->acknowledge;
913 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
914 gboolean acknowledge)
916 EmpathyTpChatPriv *priv;
918 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
920 priv = GET_PRIV (chat);
922 priv->acknowledge = acknowledge;
923 g_object_notify (G_OBJECT (chat), "acknowledge");
927 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
929 EmpathyTpChatPriv *priv;
931 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
933 priv = GET_PRIV (chat);
935 return priv->tp_chan;
939 empathy_tp_chat_get_account (EmpathyTpChat *chat)
941 EmpathyTpChatPriv *priv;
943 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
945 priv = GET_PRIV (chat);
947 return priv->account;
951 empathy_tp_chat_send (EmpathyTpChat *chat,
952 EmpathyMessage *message)
954 EmpathyTpChatPriv *priv;
955 const gchar *message_body;
956 EmpathyMessageType message_type;
958 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
959 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
961 priv = GET_PRIV (chat);
963 message_body = empathy_message_get_body (message);
964 message_type = empathy_message_get_type (message);
966 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
967 tp_cli_channel_type_text_call_send (priv->channel, -1,
971 "sending message", NULL,
976 empathy_tp_chat_set_state (EmpathyTpChat *chat,
977 TpChannelChatState state)
979 EmpathyTpChatPriv *priv;
981 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
983 priv = GET_PRIV (chat);
985 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
986 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
989 "setting chat state",
995 empathy_tp_chat_get_id (EmpathyTpChat *chat)
997 EmpathyTpChatPriv *priv;
999 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1001 priv = GET_PRIV (chat);
1004 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);