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_channel_ready_cb (EmpathyTpChat *chat)
375 EmpathyTpChatPriv *priv = GET_PRIV (chat);
377 empathy_debug (DEBUG_DOMAIN, "Channel ready");
379 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
381 tp_chat_list_pending_messages_cb,
385 tp_cli_channel_type_text_connect_to_received (priv->channel,
388 G_OBJECT (chat), NULL);
389 tp_cli_channel_type_text_connect_to_sent (priv->channel,
392 G_OBJECT (chat), NULL);
393 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
394 tp_chat_send_error_cb,
396 G_OBJECT (chat), NULL);
397 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
398 tp_chat_state_changed_cb,
400 G_OBJECT (chat), NULL);
406 tp_chat_finalize (GObject *object)
408 EmpathyTpChatPriv *priv = GET_PRIV (object);
410 if (priv->acknowledge) {
411 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
412 tp_cli_channel_call_close (priv->channel, -1,
414 "closing channel", NULL,
419 g_signal_handlers_disconnect_by_func (priv->channel,
420 tp_chat_invalidated_cb,
422 g_object_unref (priv->channel);
425 g_object_unref (priv->tp_chan);
428 g_object_unref (priv->factory);
429 g_object_unref (priv->user);
430 g_object_unref (priv->account);
431 g_object_unref (priv->mc);
434 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
438 tp_chat_constructor (GType type,
440 GObjectConstructParam *props)
443 EmpathyTpChatPriv *priv;
444 gboolean channel_ready;
446 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
448 priv = GET_PRIV (chat);
449 priv->factory = empathy_contact_factory_new ();
450 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
451 priv->mc = empathy_mission_control_new ();
453 g_signal_connect (priv->channel, "invalidated",
454 G_CALLBACK (tp_chat_invalidated_cb),
457 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
459 /* FIXME: We do that in a cb to let time to set the acknowledge
460 * property, this property should be required for construct. */
461 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
463 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
464 G_CALLBACK (tp_chat_channel_ready_cb),
468 /* FIXME: We do that in a cb to let time to set the acknowledge
469 * property, this property should be required for construct. */
470 g_idle_add ((GSourceFunc) empathy_tp_chat_get_pendings, chat);
476 tp_chat_get_property (GObject *object,
481 EmpathyTpChatPriv *priv = GET_PRIV (object);
485 g_value_set_object (value, priv->account);
488 g_value_set_object (value, priv->channel);
490 case PROP_ACKNOWLEDGE:
491 g_value_set_boolean (value, priv->acknowledge);
494 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
500 tp_chat_set_property (GObject *object,
505 EmpathyTpChatPriv *priv = GET_PRIV (object);
509 priv->account = g_object_ref (g_value_get_object (value));
512 priv->channel = g_object_ref (g_value_get_object (value));
514 case PROP_ACKNOWLEDGE:
515 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
516 g_value_get_boolean (value));
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
525 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
527 GObjectClass *object_class = G_OBJECT_CLASS (klass);
529 object_class->finalize = tp_chat_finalize;
530 object_class->constructor = tp_chat_constructor;
531 object_class->get_property = tp_chat_get_property;
532 object_class->set_property = tp_chat_set_property;
534 /* Construct properties */
535 g_object_class_install_property (object_class,
537 g_param_spec_object ("account",
539 "The account associated with the channel",
542 G_PARAM_CONSTRUCT_ONLY));
543 g_object_class_install_property (object_class,
545 g_param_spec_object ("channel",
547 "The text channel for the chat",
550 G_PARAM_CONSTRUCT_ONLY));
552 g_object_class_install_property (object_class,
554 g_param_spec_boolean ("acknowledge",
555 "acknowledge messages",
556 "Wheter or not received messages should be acknowledged",
562 signals[MESSAGE_RECEIVED] =
563 g_signal_new ("message-received",
564 G_TYPE_FROM_CLASS (klass),
568 g_cclosure_marshal_VOID__OBJECT,
570 1, EMPATHY_TYPE_MESSAGE);
572 signals[SEND_ERROR] =
573 g_signal_new ("send-error",
574 G_TYPE_FROM_CLASS (klass),
578 _empathy_marshal_VOID__OBJECT_UINT,
580 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
582 signals[CHAT_STATE_CHANGED] =
583 g_signal_new ("chat-state-changed",
584 G_TYPE_FROM_CLASS (klass),
588 _empathy_marshal_VOID__OBJECT_UINT,
590 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
593 g_signal_new ("destroy",
594 G_TYPE_FROM_CLASS (klass),
598 g_cclosure_marshal_VOID__VOID,
602 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
606 empathy_tp_chat_init (EmpathyTpChat *chat)
611 empathy_tp_chat_new (McAccount *account,
615 EmpathyTpChatPriv *priv;
617 TpConnection *connection;
621 mc = empathy_mission_control_new ();
622 tp_conn = mission_control_get_connection (mc, account, NULL);
623 connection = tp_conn_dup_connection (tp_conn);
624 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
626 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
631 priv = GET_PRIV (chat);
632 priv->tp_chan = g_object_ref (tp_chan);
634 g_object_unref (channel);
635 g_object_unref (tp_conn);
636 g_object_unref (connection);
643 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
650 const gchar *bus_name;
653 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
655 mc = empathy_mission_control_new ();
656 account = empathy_contact_get_account (contact);
658 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
659 /* The account is not connected. */
663 tp_conn = mission_control_get_connection (mc, account, NULL);
664 g_return_val_if_fail (tp_conn != NULL, NULL);
665 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
666 handle = empathy_contact_get_handle (contact);
668 text_chan = tp_conn_new_channel (tp_get_bus (),
671 TP_IFACE_CHANNEL_TYPE_TEXT,
672 TP_HANDLE_TYPE_CONTACT,
676 chat = empathy_tp_chat_new (account, text_chan);
678 g_object_unref (tp_conn);
679 g_object_unref (text_chan);
686 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
688 EmpathyTpChatPriv *priv;
690 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
692 priv = GET_PRIV (chat);
694 return priv->acknowledge;
698 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
699 gboolean acknowledge)
701 EmpathyTpChatPriv *priv;
703 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
705 priv = GET_PRIV (chat);
707 priv->acknowledge = acknowledge;
708 g_object_notify (G_OBJECT (chat), "acknowledge");
712 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
714 EmpathyTpChatPriv *priv;
716 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
718 priv = GET_PRIV (chat);
720 return priv->tp_chan;
724 empathy_tp_chat_get_account (EmpathyTpChat *chat)
726 EmpathyTpChatPriv *priv;
728 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
730 priv = GET_PRIV (chat);
732 return priv->account;
736 empathy_tp_chat_send (EmpathyTpChat *chat,
737 EmpathyMessage *message)
739 EmpathyTpChatPriv *priv;
740 const gchar *message_body;
741 EmpathyMessageType message_type;
743 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
744 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
746 priv = GET_PRIV (chat);
748 message_body = empathy_message_get_body (message);
749 message_type = empathy_message_get_type (message);
751 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
752 tp_cli_channel_type_text_call_send (priv->channel, -1,
756 "sending message", NULL,
761 empathy_tp_chat_set_state (EmpathyTpChat *chat,
762 TpChannelChatState state)
764 EmpathyTpChatPriv *priv;
766 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
768 priv = GET_PRIV (chat);
770 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
771 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
774 "setting chat state",
780 empathy_tp_chat_get_id (EmpathyTpChat *chat)
782 EmpathyTpChatPriv *priv;
784 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
786 priv = GET_PRIV (chat);
789 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);