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;
54 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
55 static void empathy_tp_chat_init (EmpathyTpChat *chat);
72 static guint signals[LAST_SIGNAL];
74 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
77 tp_chat_invalidated_cb (TpProxy *proxy,
83 EmpathyTpChatPriv *priv = GET_PRIV (chat);
85 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
87 g_object_unref (priv->channel);
88 g_object_unref (priv->tp_chan);
92 g_signal_emit (chat, signals[DESTROY], 0);
96 tp_chat_async_cb (TpChannel *proxy,
102 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
103 user_data, error->message);
107 static EmpathyMessage *
108 tp_chat_build_message (EmpathyTpChat *chat,
112 const gchar *message_body)
114 EmpathyTpChatPriv *priv;
115 EmpathyMessage *message;
116 EmpathyContact *sender;
118 priv = GET_PRIV (chat);
120 if (from_handle == 0) {
121 sender = g_object_ref (priv->user);
123 sender = empathy_contact_factory_get_from_handle (priv->factory,
128 message = empathy_message_new (message_body);
129 empathy_message_set_type (message, type);
130 empathy_message_set_sender (message, sender);
131 empathy_message_set_receiver (message, priv->user);
132 empathy_message_set_timestamp (message, timestamp);
134 g_object_unref (sender);
140 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
141 GParamSpec *param_spec,
144 EmpathyTpChatPriv *priv = GET_PRIV (chat);
145 EmpathyMessage *message;
146 EmpathyContactReady ready;
147 EmpathyContact *sender;
148 gboolean removed = FALSE;
150 /* Emit all messages queued until we find a message with not
151 * ready sender. When leaving this loop, sender is the first not ready
152 * contact queued and removed tells if at least one message got removed
154 while (priv->message_queue) {
155 message = priv->message_queue->data;
156 sender = empathy_message_get_sender (message);
157 ready = empathy_contact_get_ready (sender);
159 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
163 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
164 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
165 priv->message_queue = g_slist_remove (priv->message_queue,
167 g_object_unref (message);
172 g_signal_handlers_disconnect_by_func (contact,
173 tp_chat_sender_ready_notify_cb,
176 if (priv->message_queue) {
177 g_signal_connect (sender, "notify::ready",
178 G_CALLBACK (tp_chat_sender_ready_notify_cb),
185 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
186 EmpathyMessage *message)
188 EmpathyTpChatPriv *priv = GET_PRIV (chat);
189 EmpathyContact *sender;
190 EmpathyContactReady ready;
192 if (priv->message_queue != NULL) {
193 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
194 priv->message_queue = g_slist_append (priv->message_queue,
195 g_object_ref (message));
199 sender = empathy_message_get_sender (message);
200 ready = empathy_contact_get_ready (sender);
201 if (ready & EMPATHY_CONTACT_READY_NAME) {
202 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
203 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
207 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
208 priv->message_queue = g_slist_append (priv->message_queue,
209 g_object_ref (message));
210 g_signal_connect (sender, "notify::ready",
211 G_CALLBACK (tp_chat_sender_ready_notify_cb),
216 tp_chat_received_cb (TpChannel *channel,
222 const gchar *message_body,
226 EmpathyTpChatPriv *priv = GET_PRIV (chat);
227 EmpathyMessage *message;
229 if (!priv->had_pending_messages) {
233 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
235 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
241 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
242 g_object_unref (message);
244 if (priv->acknowledge) {
247 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
248 g_array_append_val (message_ids, message_id);
249 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
253 "acknowledging pending messages",
256 g_array_free (message_ids, TRUE);
261 tp_chat_sent_cb (TpChannel *channel,
264 const gchar *message_body,
268 EmpathyMessage *message;
270 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
272 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
278 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
279 g_object_unref (message);
283 tp_chat_send_error_cb (TpChannel *channel,
287 const gchar *message_body,
291 EmpathyMessage *message;
293 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
294 message_body, error_code);
296 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
302 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
303 g_object_unref (message);
307 tp_chat_state_changed_cb (TpChannel *channel,
313 EmpathyTpChatPriv *priv = GET_PRIV (chat);
314 EmpathyContact *contact;
316 contact = empathy_contact_factory_get_from_handle (priv->factory,
320 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
321 empathy_contact_get_name (contact),
324 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
325 g_object_unref (contact);
329 tp_chat_list_pending_messages_cb (TpChannel *channel,
330 const GPtrArray *messages_list,
335 EmpathyTpChatPriv *priv = GET_PRIV (chat);
338 priv->had_pending_messages = TRUE;
340 for (i = 0; i < messages_list->len; i++) {
341 EmpathyMessage *message;
342 GValueArray *message_struct;
343 const gchar *message_body;
350 message_struct = g_ptr_array_index (messages_list, i);
352 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
353 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
354 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
355 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
356 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
357 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
359 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
361 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
367 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
368 g_object_unref (message);
373 tp_chat_properties_changed_cb (TpProxy *proxy,
374 const GPtrArray *properties,
381 tp_chat_get_properties_cb (TpProxy *proxy,
382 const GPtrArray *properties,
387 tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
391 tp_chat_list_properties_cb (TpProxy *proxy,
392 const GPtrArray *properties,
400 ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
401 for (i = 0; i < properties->len; i++) {
402 GValueArray *prop_struct;
405 prop_struct = g_ptr_array_index (properties, i);
406 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
407 g_array_append_val (ids, id);
410 tp_cli_properties_interface_call_get_properties (proxy, -1,
412 tp_chat_get_properties_cb,
416 g_array_free (ids, TRUE);
420 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
422 EmpathyTpChatPriv *priv = GET_PRIV (chat);
424 empathy_debug (DEBUG_DOMAIN, "Channel ready");
426 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
428 tp_chat_list_pending_messages_cb,
431 tp_cli_properties_interface_call_list_properties (priv->channel, -1,
432 tp_chat_list_properties_cb,
437 tp_cli_channel_type_text_connect_to_received (priv->channel,
440 G_OBJECT (chat), NULL);
441 tp_cli_channel_type_text_connect_to_sent (priv->channel,
444 G_OBJECT (chat), NULL);
445 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
446 tp_chat_send_error_cb,
448 G_OBJECT (chat), NULL);
449 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
450 tp_chat_state_changed_cb,
452 G_OBJECT (chat), NULL);
453 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
454 tp_chat_state_changed_cb,
456 G_OBJECT (chat), NULL);
457 tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
458 tp_chat_properties_changed_cb,
460 G_OBJECT (chat), NULL);
466 tp_chat_finalize (GObject *object)
468 EmpathyTpChatPriv *priv = GET_PRIV (object);
470 if (priv->acknowledge) {
471 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
472 tp_cli_channel_call_close (priv->channel, -1,
474 "closing channel", NULL,
479 g_signal_handlers_disconnect_by_func (priv->channel,
480 tp_chat_invalidated_cb,
482 g_object_unref (priv->channel);
485 g_object_unref (priv->tp_chan);
488 g_object_unref (priv->factory);
489 g_object_unref (priv->user);
490 g_object_unref (priv->account);
491 g_object_unref (priv->mc);
494 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
498 tp_chat_constructor (GType type,
500 GObjectConstructParam *props)
503 EmpathyTpChatPriv *priv;
504 gboolean channel_ready;
506 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
508 priv = GET_PRIV (chat);
509 priv->factory = empathy_contact_factory_new ();
510 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
511 priv->mc = empathy_mission_control_new ();
513 g_signal_connect (priv->channel, "invalidated",
514 G_CALLBACK (tp_chat_invalidated_cb),
517 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
519 /* FIXME: We do that in a cb to let time to set the acknowledge
520 * property, this property should be required for construct. */
521 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
523 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
524 G_CALLBACK (tp_chat_channel_ready_cb),
532 tp_chat_get_property (GObject *object,
537 EmpathyTpChatPriv *priv = GET_PRIV (object);
541 g_value_set_object (value, priv->account);
544 g_value_set_object (value, priv->channel);
546 case PROP_ACKNOWLEDGE:
547 g_value_set_boolean (value, priv->acknowledge);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
556 tp_chat_set_property (GObject *object,
561 EmpathyTpChatPriv *priv = GET_PRIV (object);
565 priv->account = g_object_ref (g_value_get_object (value));
568 priv->channel = g_object_ref (g_value_get_object (value));
570 case PROP_ACKNOWLEDGE:
571 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
572 g_value_get_boolean (value));
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
581 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
583 GObjectClass *object_class = G_OBJECT_CLASS (klass);
585 object_class->finalize = tp_chat_finalize;
586 object_class->constructor = tp_chat_constructor;
587 object_class->get_property = tp_chat_get_property;
588 object_class->set_property = tp_chat_set_property;
590 /* Construct properties */
591 g_object_class_install_property (object_class,
593 g_param_spec_object ("account",
595 "The account associated with the channel",
598 G_PARAM_CONSTRUCT_ONLY));
599 g_object_class_install_property (object_class,
601 g_param_spec_object ("channel",
603 "The text channel for the chat",
606 G_PARAM_CONSTRUCT_ONLY));
608 g_object_class_install_property (object_class,
610 g_param_spec_boolean ("acknowledge",
611 "acknowledge messages",
612 "Wheter or not received messages should be acknowledged",
618 signals[MESSAGE_RECEIVED] =
619 g_signal_new ("message-received",
620 G_TYPE_FROM_CLASS (klass),
624 g_cclosure_marshal_VOID__OBJECT,
626 1, EMPATHY_TYPE_MESSAGE);
628 signals[SEND_ERROR] =
629 g_signal_new ("send-error",
630 G_TYPE_FROM_CLASS (klass),
634 _empathy_marshal_VOID__OBJECT_UINT,
636 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
638 signals[CHAT_STATE_CHANGED] =
639 g_signal_new ("chat-state-changed",
640 G_TYPE_FROM_CLASS (klass),
644 _empathy_marshal_VOID__OBJECT_UINT,
646 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
649 g_signal_new ("destroy",
650 G_TYPE_FROM_CLASS (klass),
654 g_cclosure_marshal_VOID__VOID,
658 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
662 empathy_tp_chat_init (EmpathyTpChat *chat)
667 empathy_tp_chat_new (McAccount *account,
671 EmpathyTpChatPriv *priv;
673 TpConnection *connection;
677 mc = empathy_mission_control_new ();
678 tp_conn = mission_control_get_connection (mc, account, NULL);
679 connection = tp_conn_dup_connection (tp_conn);
680 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
682 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
687 priv = GET_PRIV (chat);
688 priv->tp_chan = g_object_ref (tp_chan);
690 g_object_unref (channel);
691 g_object_unref (tp_conn);
692 g_object_unref (connection);
699 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
706 const gchar *bus_name;
709 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
711 mc = empathy_mission_control_new ();
712 account = empathy_contact_get_account (contact);
714 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
715 /* The account is not connected. */
719 tp_conn = mission_control_get_connection (mc, account, NULL);
720 g_return_val_if_fail (tp_conn != NULL, NULL);
721 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
722 handle = empathy_contact_get_handle (contact);
724 text_chan = tp_conn_new_channel (tp_get_bus (),
727 TP_IFACE_CHANNEL_TYPE_TEXT,
728 TP_HANDLE_TYPE_CONTACT,
732 chat = empathy_tp_chat_new (account, text_chan);
734 g_object_unref (tp_conn);
735 g_object_unref (text_chan);
742 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
744 EmpathyTpChatPriv *priv;
746 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
748 priv = GET_PRIV (chat);
750 return priv->acknowledge;
754 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
755 gboolean acknowledge)
757 EmpathyTpChatPriv *priv;
759 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
761 priv = GET_PRIV (chat);
763 priv->acknowledge = acknowledge;
764 g_object_notify (G_OBJECT (chat), "acknowledge");
768 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
770 EmpathyTpChatPriv *priv;
772 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
774 priv = GET_PRIV (chat);
776 return priv->tp_chan;
780 empathy_tp_chat_get_account (EmpathyTpChat *chat)
782 EmpathyTpChatPriv *priv;
784 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
786 priv = GET_PRIV (chat);
788 return priv->account;
792 empathy_tp_chat_send (EmpathyTpChat *chat,
793 EmpathyMessage *message)
795 EmpathyTpChatPriv *priv;
796 const gchar *message_body;
797 EmpathyMessageType message_type;
799 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
800 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
802 priv = GET_PRIV (chat);
804 message_body = empathy_message_get_body (message);
805 message_type = empathy_message_get_type (message);
807 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
808 tp_cli_channel_type_text_call_send (priv->channel, -1,
812 "sending message", NULL,
817 empathy_tp_chat_set_state (EmpathyTpChat *chat,
818 TpChannelChatState state)
820 EmpathyTpChatPriv *priv;
822 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
824 priv = GET_PRIV (chat);
826 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
827 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
830 "setting chat state",
836 empathy_tp_chat_get_id (EmpathyTpChat *chat)
838 EmpathyTpChatPriv *priv;
840 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
842 priv = GET_PRIV (chat);
845 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);