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;
59 TpPropertyFlags flags;
63 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
64 static void empathy_tp_chat_init (EmpathyTpChat *chat);
83 static guint signals[LAST_SIGNAL];
85 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
88 tp_chat_invalidated_cb (TpProxy *proxy,
94 EmpathyTpChatPriv *priv = GET_PRIV (chat);
96 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
98 g_object_unref (priv->channel);
99 g_object_unref (priv->tp_chan);
100 priv->channel = NULL;
101 priv->tp_chan = NULL;
103 g_signal_emit (chat, signals[DESTROY], 0);
107 tp_chat_async_cb (TpChannel *proxy,
110 GObject *weak_object)
113 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
114 user_data, error->message);
118 static EmpathyMessage *
119 tp_chat_build_message (EmpathyTpChat *chat,
123 const gchar *message_body)
125 EmpathyTpChatPriv *priv;
126 EmpathyMessage *message;
127 EmpathyContact *sender;
129 priv = GET_PRIV (chat);
131 if (from_handle == 0) {
132 sender = g_object_ref (priv->user);
134 sender = empathy_contact_factory_get_from_handle (priv->factory,
139 message = empathy_message_new (message_body);
140 empathy_message_set_type (message, type);
141 empathy_message_set_sender (message, sender);
142 empathy_message_set_receiver (message, priv->user);
143 empathy_message_set_timestamp (message, timestamp);
145 g_object_unref (sender);
151 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
152 GParamSpec *param_spec,
155 EmpathyTpChatPriv *priv = GET_PRIV (chat);
156 EmpathyMessage *message;
157 EmpathyContactReady ready;
158 EmpathyContact *sender;
159 gboolean removed = FALSE;
161 /* Emit all messages queued until we find a message with not
162 * ready sender. When leaving this loop, sender is the first not ready
163 * contact queued and removed tells if at least one message got removed
165 while (priv->message_queue) {
166 message = priv->message_queue->data;
167 sender = empathy_message_get_sender (message);
168 ready = empathy_contact_get_ready (sender);
170 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
174 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
175 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
176 priv->message_queue = g_slist_remove (priv->message_queue,
178 g_object_unref (message);
183 g_signal_handlers_disconnect_by_func (contact,
184 tp_chat_sender_ready_notify_cb,
187 if (priv->message_queue) {
188 g_signal_connect (sender, "notify::ready",
189 G_CALLBACK (tp_chat_sender_ready_notify_cb),
196 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
197 EmpathyMessage *message)
199 EmpathyTpChatPriv *priv = GET_PRIV (chat);
200 EmpathyContact *sender;
201 EmpathyContactReady ready;
203 if (priv->message_queue != NULL) {
204 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
205 priv->message_queue = g_slist_append (priv->message_queue,
206 g_object_ref (message));
210 sender = empathy_message_get_sender (message);
211 ready = empathy_contact_get_ready (sender);
212 if (ready & EMPATHY_CONTACT_READY_NAME) {
213 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
214 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
218 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
219 priv->message_queue = g_slist_append (priv->message_queue,
220 g_object_ref (message));
221 g_signal_connect (sender, "notify::ready",
222 G_CALLBACK (tp_chat_sender_ready_notify_cb),
227 tp_chat_received_cb (TpChannel *channel,
233 const gchar *message_body,
237 EmpathyTpChatPriv *priv = GET_PRIV (chat);
238 EmpathyMessage *message;
240 if (!priv->had_pending_messages) {
244 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
246 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
252 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
253 g_object_unref (message);
255 if (priv->acknowledge) {
258 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
259 g_array_append_val (message_ids, message_id);
260 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
264 "acknowledging pending messages",
267 g_array_free (message_ids, TRUE);
272 tp_chat_sent_cb (TpChannel *channel,
275 const gchar *message_body,
279 EmpathyMessage *message;
281 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
283 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
289 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
290 g_object_unref (message);
294 tp_chat_send_error_cb (TpChannel *channel,
298 const gchar *message_body,
302 EmpathyMessage *message;
304 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
305 message_body, error_code);
307 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
313 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
314 g_object_unref (message);
318 tp_chat_state_changed_cb (TpChannel *channel,
324 EmpathyTpChatPriv *priv = GET_PRIV (chat);
325 EmpathyContact *contact;
327 contact = empathy_contact_factory_get_from_handle (priv->factory,
331 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
332 empathy_contact_get_name (contact),
335 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
336 g_object_unref (contact);
340 tp_chat_list_pending_messages_cb (TpChannel *channel,
341 const GPtrArray *messages_list,
346 EmpathyTpChatPriv *priv = GET_PRIV (chat);
349 priv->had_pending_messages = TRUE;
352 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
357 for (i = 0; i < messages_list->len; i++) {
358 EmpathyMessage *message;
359 GValueArray *message_struct;
360 const gchar *message_body;
367 message_struct = g_ptr_array_index (messages_list, i);
369 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
370 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
371 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
372 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
373 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
374 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
376 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
378 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
384 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
385 g_object_unref (message);
390 tp_chat_properties_changed_cb (TpProxy *proxy,
391 const GPtrArray *properties,
395 EmpathyTpChatPriv *priv = GET_PRIV (chat);
398 for (i = 0; i < properties->len; i++) {
399 GValueArray *prop_struct;
400 TpChatProperty *property;
404 prop_struct = g_ptr_array_index (properties, i);
405 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
406 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
408 for (j = 0; j < priv->properties->len; j++) {
409 property = g_ptr_array_index (priv->properties, j);
410 if (property->id == id) {
411 if (property->value) {
412 g_value_unset (property->value);
414 property->value = g_slice_new0 (GValue);
416 g_value_init (property->value, G_VALUE_TYPE (src_value));
417 g_value_copy (src_value, property->value);
419 empathy_debug (DEBUG_DOMAIN, "property %s changed",
421 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
422 property->name, property->value);
430 tp_chat_get_properties_cb (TpProxy *proxy,
431 const GPtrArray *properties,
437 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
442 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
446 tp_chat_list_properties_cb (TpProxy *proxy,
447 const GPtrArray *properties,
452 EmpathyTpChatPriv *priv = GET_PRIV (chat);
456 priv->had_properties_list = TRUE;
459 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
464 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
465 priv->properties = g_ptr_array_sized_new (properties->len);
466 for (i = 0; i < properties->len; i++) {
467 GValueArray *prop_struct;
468 TpChatProperty *property;
470 prop_struct = g_ptr_array_index (properties, i);
471 property = g_slice_new0 (TpChatProperty);
472 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
473 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
474 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
476 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
477 property->name, property->id, property->flags);
478 g_ptr_array_add (priv->properties, property);
479 if (property->flags & TP_PROPERTY_FLAG_READ) {
480 g_array_append_val (ids, property->id);
484 tp_cli_properties_interface_call_get_properties (proxy, -1,
486 tp_chat_get_properties_cb,
490 g_array_free (ids, TRUE);
494 empathy_tp_chat_set_property (EmpathyTpChat *chat,
497 /* FIXME: not implemented */
501 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
503 EmpathyTpChatPriv *priv = GET_PRIV (chat);
505 empathy_debug (DEBUG_DOMAIN, "Channel ready");
507 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
509 tp_chat_list_pending_messages_cb,
512 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
513 tp_chat_list_properties_cb,
518 tp_cli_channel_type_text_connect_to_received (priv->channel,
521 G_OBJECT (chat), NULL);
522 tp_cli_channel_type_text_connect_to_sent (priv->channel,
525 G_OBJECT (chat), NULL);
526 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
527 tp_chat_send_error_cb,
529 G_OBJECT (chat), NULL);
530 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
531 tp_chat_state_changed_cb,
533 G_OBJECT (chat), NULL);
534 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
535 tp_chat_state_changed_cb,
537 G_OBJECT (chat), NULL);
538 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
539 tp_chat_properties_changed_cb,
541 G_OBJECT (chat), NULL);
547 tp_chat_finalize (GObject *object)
549 EmpathyTpChatPriv *priv = GET_PRIV (object);
552 if (priv->acknowledge && priv->channel) {
553 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
554 tp_cli_channel_call_close (priv->channel, -1,
556 "closing channel", NULL,
561 g_signal_handlers_disconnect_by_func (priv->channel,
562 tp_chat_invalidated_cb,
564 g_object_unref (priv->channel);
567 g_object_unref (priv->tp_chan);
570 if (priv->properties) {
571 for (i = 0; i < priv->properties->len; i++) {
572 TpChatProperty *property;
574 property = g_ptr_array_index (priv->properties, i);
575 g_free (property->name);
576 if (property->value) {
577 g_value_unset (property->value);
578 g_slice_free (GValue, property->value);
580 g_slice_free (TpChatProperty, property);
582 g_ptr_array_free (priv->properties, TRUE);
585 g_object_unref (priv->factory);
586 g_object_unref (priv->user);
587 g_object_unref (priv->account);
588 g_object_unref (priv->mc);
591 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
595 tp_chat_constructor (GType type,
597 GObjectConstructParam *props)
600 EmpathyTpChatPriv *priv;
601 gboolean channel_ready;
603 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
605 priv = GET_PRIV (chat);
606 priv->factory = empathy_contact_factory_new ();
607 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
608 priv->mc = empathy_mission_control_new ();
610 g_signal_connect (priv->channel, "invalidated",
611 G_CALLBACK (tp_chat_invalidated_cb),
614 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
616 /* FIXME: We do that in a cb to let time to set the acknowledge
617 * property, this property should be required for construct. */
618 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
620 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
621 G_CALLBACK (tp_chat_channel_ready_cb),
629 tp_chat_get_property (GObject *object,
634 EmpathyTpChatPriv *priv = GET_PRIV (object);
638 g_value_set_object (value, priv->account);
641 g_value_set_object (value, priv->tp_chan);
644 g_value_set_object (value, priv->channel);
646 case PROP_ACKNOWLEDGE:
647 g_value_set_boolean (value, priv->acknowledge);
650 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
656 tp_chat_set_property (GObject *object,
661 EmpathyTpChatPriv *priv = GET_PRIV (object);
665 priv->account = g_object_ref (g_value_get_object (value));
668 priv->tp_chan = g_object_ref (g_value_get_object (value));
671 priv->channel = g_object_ref (g_value_get_object (value));
673 case PROP_ACKNOWLEDGE:
674 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
675 g_value_get_boolean (value));
678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
684 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
686 GObjectClass *object_class = G_OBJECT_CLASS (klass);
688 object_class->finalize = tp_chat_finalize;
689 object_class->constructor = tp_chat_constructor;
690 object_class->get_property = tp_chat_get_property;
691 object_class->set_property = tp_chat_set_property;
693 /* Construct properties */
694 g_object_class_install_property (object_class,
696 g_param_spec_object ("account",
698 "The account associated with the channel",
701 G_PARAM_CONSTRUCT_ONLY));
702 g_object_class_install_property (object_class,
704 g_param_spec_object ("tp-chan",
706 "The text channel for the chat",
709 G_PARAM_CONSTRUCT_ONLY));
711 g_object_class_install_property (object_class,
713 g_param_spec_object ("channel",
715 "The text channel for the chat",
718 G_PARAM_CONSTRUCT_ONLY));
720 g_object_class_install_property (object_class,
722 g_param_spec_boolean ("acknowledge",
723 "acknowledge messages",
724 "Wheter or not received messages should be acknowledged",
730 signals[MESSAGE_RECEIVED] =
731 g_signal_new ("message-received",
732 G_TYPE_FROM_CLASS (klass),
736 g_cclosure_marshal_VOID__OBJECT,
738 1, EMPATHY_TYPE_MESSAGE);
740 signals[SEND_ERROR] =
741 g_signal_new ("send-error",
742 G_TYPE_FROM_CLASS (klass),
746 _empathy_marshal_VOID__OBJECT_UINT,
748 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
750 signals[CHAT_STATE_CHANGED] =
751 g_signal_new ("chat-state-changed",
752 G_TYPE_FROM_CLASS (klass),
756 _empathy_marshal_VOID__OBJECT_UINT,
758 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
760 signals[PROPERTY_CHANGED] =
761 g_signal_new ("property-changed",
762 G_TYPE_FROM_CLASS (klass),
766 _empathy_marshal_VOID__STRING_BOXED,
768 2, G_TYPE_STRING, G_TYPE_VALUE);
771 g_signal_new ("destroy",
772 G_TYPE_FROM_CLASS (klass),
776 g_cclosure_marshal_VOID__VOID,
780 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
784 empathy_tp_chat_init (EmpathyTpChat *chat)
789 empathy_tp_chat_new (McAccount *account,
794 TpConnection *connection;
798 mc = empathy_mission_control_new ();
799 tp_conn = mission_control_get_connection (mc, account, NULL);
800 connection = tp_conn_dup_connection (tp_conn);
801 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
803 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
809 g_object_unref (channel);
810 g_object_unref (tp_conn);
811 g_object_unref (connection);
818 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
825 const gchar *bus_name;
828 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
830 mc = empathy_mission_control_new ();
831 account = empathy_contact_get_account (contact);
833 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
834 /* The account is not connected. */
838 tp_conn = mission_control_get_connection (mc, account, NULL);
839 g_return_val_if_fail (tp_conn != NULL, NULL);
840 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
841 handle = empathy_contact_get_handle (contact);
843 text_chan = tp_conn_new_channel (tp_get_bus (),
846 TP_IFACE_CHANNEL_TYPE_TEXT,
847 TP_HANDLE_TYPE_CONTACT,
851 chat = empathy_tp_chat_new (account, text_chan);
853 g_object_unref (tp_conn);
854 g_object_unref (text_chan);
861 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
863 EmpathyTpChatPriv *priv;
865 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
867 priv = GET_PRIV (chat);
869 return priv->acknowledge;
873 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
874 gboolean acknowledge)
876 EmpathyTpChatPriv *priv;
878 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
880 priv = GET_PRIV (chat);
882 priv->acknowledge = acknowledge;
883 g_object_notify (G_OBJECT (chat), "acknowledge");
887 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
889 EmpathyTpChatPriv *priv;
891 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
893 priv = GET_PRIV (chat);
895 return priv->tp_chan;
899 empathy_tp_chat_get_account (EmpathyTpChat *chat)
901 EmpathyTpChatPriv *priv;
903 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
905 priv = GET_PRIV (chat);
907 return priv->account;
911 empathy_tp_chat_send (EmpathyTpChat *chat,
912 EmpathyMessage *message)
914 EmpathyTpChatPriv *priv;
915 const gchar *message_body;
916 EmpathyMessageType message_type;
918 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
919 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
921 priv = GET_PRIV (chat);
923 message_body = empathy_message_get_body (message);
924 message_type = empathy_message_get_type (message);
926 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
927 tp_cli_channel_type_text_call_send (priv->channel, -1,
931 "sending message", NULL,
936 empathy_tp_chat_set_state (EmpathyTpChat *chat,
937 TpChannelChatState state)
939 EmpathyTpChatPriv *priv;
941 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
943 priv = GET_PRIV (chat);
945 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
946 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
949 "setting chat state",
955 empathy_tp_chat_get_id (EmpathyTpChat *chat)
957 EmpathyTpChatPriv *priv;
959 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
961 priv = GET_PRIV (chat);
964 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);