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 (EmpathyTpChat *chat)
79 EmpathyTpChatPriv *priv = GET_PRIV (chat);
81 empathy_debug (DEBUG_DOMAIN, "Channel invalidated");
83 g_object_unref (priv->channel);
86 g_signal_emit (chat, signals[DESTROY], 0);
90 tp_chat_async_cb (TpChannel *proxy,
96 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
97 user_data, error->message);
101 static EmpathyMessage *
102 tp_chat_build_message (EmpathyTpChat *chat,
106 const gchar *message_body)
108 EmpathyTpChatPriv *priv;
109 EmpathyMessage *message;
110 EmpathyContact *sender;
112 priv = GET_PRIV (chat);
114 if (from_handle == 0) {
115 sender = g_object_ref (priv->user);
117 sender = empathy_contact_factory_get_from_handle (priv->factory,
122 message = empathy_message_new (message_body);
123 empathy_message_set_type (message, type);
124 empathy_message_set_sender (message, sender);
125 empathy_message_set_receiver (message, priv->user);
126 empathy_message_set_timestamp (message, timestamp);
128 g_object_unref (sender);
134 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
135 GParamSpec *param_spec,
138 EmpathyTpChatPriv *priv = GET_PRIV (chat);
139 EmpathyMessage *message;
140 EmpathyContact *sender;
141 gboolean removed = FALSE;
142 const gchar *name, *id;
144 /* Emit all messages queued until we find a message with not
145 * ready sender. When leaving this loop, sender is the first not ready
146 * contact queued and removed tells if at least one message got removed
148 while (priv->message_queue) {
149 message = priv->message_queue->data;
150 sender = empathy_message_get_sender (message);
151 name = empathy_contact_get_name (sender);
152 id = empathy_contact_get_id (sender);
154 if (!tp_strdiff (name, id)) {
158 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
159 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
160 priv->message_queue = g_slist_remove (priv->message_queue,
162 g_object_unref (message);
167 g_signal_handlers_disconnect_by_func (contact,
168 tp_chat_sender_ready_notify_cb,
171 if (priv->message_queue) {
172 g_signal_connect (sender, "notify::name",
173 G_CALLBACK (tp_chat_sender_ready_notify_cb),
180 tp_chat_emit_or_queue_message (EmpathyTpChat *chat,
181 EmpathyMessage *message)
183 EmpathyTpChatPriv *priv = GET_PRIV (chat);
184 EmpathyContact *sender;
185 const gchar *name, *id;
187 if (priv->message_queue != NULL) {
188 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
189 priv->message_queue = g_slist_append (priv->message_queue,
190 g_object_ref (message));
194 sender = empathy_message_get_sender (message);
195 name = empathy_contact_get_name (sender);
196 id = empathy_contact_get_id (sender);
197 if (tp_strdiff (name, id)) {
198 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
199 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
203 empathy_debug (DEBUG_DOMAIN, "Sender not ready");
204 priv->message_queue = g_slist_append (priv->message_queue,
205 g_object_ref (message));
206 g_signal_connect (sender, "notify::name",
207 G_CALLBACK (tp_chat_sender_ready_notify_cb),
211 tp_chat_received_cb (TpChannel *channel,
217 const gchar *message_body,
221 EmpathyTpChatPriv *priv = GET_PRIV (chat);
222 EmpathyMessage *message;
224 if (!priv->had_pending_messages) {
228 empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
230 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
236 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
237 g_object_unref (message);
239 if (priv->acknowledge) {
242 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
243 g_array_append_val (message_ids, message_id);
244 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
248 "acknowledging pending messages",
251 g_array_free (message_ids, TRUE);
256 tp_chat_sent_cb (TpChannel *channel,
259 const gchar *message_body,
263 EmpathyMessage *message;
265 empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
267 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
273 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
274 g_object_unref (message);
278 tp_chat_send_error_cb (TpChannel *channel,
282 const gchar *message_body,
286 EmpathyMessage *message;
288 empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
289 message_body, error_code);
291 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
297 g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
298 g_object_unref (message);
302 tp_chat_state_changed_cb (TpChannel *channel,
308 EmpathyTpChatPriv *priv = GET_PRIV (chat);
309 EmpathyContact *contact;
311 contact = empathy_contact_factory_get_from_handle (priv->factory,
315 empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
316 empathy_contact_get_name (contact),
319 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
320 g_object_unref (contact);
324 tp_chat_list_pending_messages_cb (TpChannel *channel,
325 const GPtrArray *messages_list,
330 EmpathyTpChatPriv *priv = GET_PRIV (chat);
333 priv->had_pending_messages = TRUE;
335 for (i = 0; i < messages_list->len; i++) {
336 EmpathyMessage *message;
337 GValueArray *message_struct;
338 const gchar *message_body;
345 message_struct = g_ptr_array_index (messages_list, i);
347 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
348 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
349 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
350 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
351 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
352 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
354 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
356 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
362 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
363 g_object_unref (message);
368 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
370 EmpathyTpChatPriv *priv = GET_PRIV (chat);
372 empathy_debug (DEBUG_DOMAIN, "Channel ready");
374 tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
376 tp_chat_list_pending_messages_cb,
380 tp_cli_channel_type_text_connect_to_received (priv->channel,
383 G_OBJECT (chat), NULL);
384 tp_cli_channel_type_text_connect_to_sent (priv->channel,
387 G_OBJECT (chat), NULL);
388 tp_cli_channel_type_text_connect_to_send_error (priv->channel,
389 tp_chat_send_error_cb,
391 G_OBJECT (chat), NULL);
392 tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
393 tp_chat_state_changed_cb,
395 G_OBJECT (chat), NULL);
401 tp_chat_finalize (GObject *object)
403 EmpathyTpChatPriv *priv = GET_PRIV (object);
405 if (priv->acknowledge) {
406 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
407 tp_cli_channel_call_close (priv->channel, -1,
409 "closing channel", NULL,
413 g_object_unref (priv->channel);
414 g_object_unref (priv->factory);
415 g_object_unref (priv->user);
416 g_object_unref (priv->account);
417 g_object_unref (priv->mc);
420 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
424 tp_chat_constructor (GType type,
426 GObjectConstructParam *props)
429 EmpathyTpChatPriv *priv;
430 gboolean channel_ready;
432 chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
434 priv = GET_PRIV (chat);
435 priv->factory = empathy_contact_factory_new ();
436 priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
437 priv->mc = empathy_mission_control_new ();
439 g_signal_connect (priv->channel, "invalidated",
440 G_CALLBACK (tp_chat_invalidated_cb),
443 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
445 /* FIXME: We do that in a cb to let time to set the acknowledge
446 * property, this property should be required for construct. */
447 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
449 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
450 G_CALLBACK (tp_chat_channel_ready_cb),
454 /* FIXME: We do that in a cb to let time to set the acknowledge
455 * property, this property should be required for construct. */
456 g_idle_add ((GSourceFunc) empathy_tp_chat_get_pendings, chat);
462 tp_chat_get_property (GObject *object,
467 EmpathyTpChatPriv *priv = GET_PRIV (object);
471 g_value_set_object (value, priv->account);
474 g_value_set_object (value, priv->channel);
476 case PROP_ACKNOWLEDGE:
477 g_value_set_boolean (value, priv->acknowledge);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
486 tp_chat_set_property (GObject *object,
491 EmpathyTpChatPriv *priv = GET_PRIV (object);
495 priv->account = g_object_ref (g_value_get_object (value));
498 priv->channel = g_object_ref (g_value_get_object (value));
500 case PROP_ACKNOWLEDGE:
501 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
502 g_value_get_boolean (value));
505 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
511 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
513 GObjectClass *object_class = G_OBJECT_CLASS (klass);
515 object_class->finalize = tp_chat_finalize;
516 object_class->constructor = tp_chat_constructor;
517 object_class->get_property = tp_chat_get_property;
518 object_class->set_property = tp_chat_set_property;
520 /* Construct properties */
521 g_object_class_install_property (object_class,
523 g_param_spec_object ("account",
525 "The account associated with the channel",
528 G_PARAM_CONSTRUCT_ONLY));
529 g_object_class_install_property (object_class,
531 g_param_spec_object ("channel",
533 "The text channel for the chat",
536 G_PARAM_CONSTRUCT_ONLY));
538 g_object_class_install_property (object_class,
540 g_param_spec_boolean ("acknowledge",
541 "acknowledge messages",
542 "Wheter or not received messages should be acknowledged",
548 signals[MESSAGE_RECEIVED] =
549 g_signal_new ("message-received",
550 G_TYPE_FROM_CLASS (klass),
554 g_cclosure_marshal_VOID__OBJECT,
556 1, EMPATHY_TYPE_MESSAGE);
558 signals[SEND_ERROR] =
559 g_signal_new ("send-error",
560 G_TYPE_FROM_CLASS (klass),
564 _empathy_marshal_VOID__OBJECT_UINT,
566 2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
568 signals[CHAT_STATE_CHANGED] =
569 g_signal_new ("chat-state-changed",
570 G_TYPE_FROM_CLASS (klass),
574 _empathy_marshal_VOID__OBJECT_UINT,
576 2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
579 g_signal_new ("destroy",
580 G_TYPE_FROM_CLASS (klass),
584 g_cclosure_marshal_VOID__VOID,
588 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
592 empathy_tp_chat_init (EmpathyTpChat *chat)
597 empathy_tp_chat_new (McAccount *account,
602 TpConnection *connection;
606 mc = empathy_mission_control_new ();
607 tp_conn = mission_control_get_connection (mc, account, NULL);
608 connection = tp_conn_dup_connection (tp_conn);
609 channel = tp_chan_dup_channel (tp_chan, connection, NULL);
611 chat = g_object_new (EMPATHY_TYPE_TP_CHAT,
616 g_object_unref (channel);
617 g_object_unref (tp_conn);
618 g_object_unref (connection);
625 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
632 const gchar *bus_name;
635 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
637 mc = empathy_mission_control_new ();
638 account = empathy_contact_get_account (contact);
640 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
641 /* The account is not connected. */
645 tp_conn = mission_control_get_connection (mc, account, NULL);
646 g_return_val_if_fail (tp_conn != NULL, NULL);
647 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
648 handle = empathy_contact_get_handle (contact);
650 text_chan = tp_conn_new_channel (tp_get_bus (),
653 TP_IFACE_CHANNEL_TYPE_TEXT,
654 TP_HANDLE_TYPE_CONTACT,
658 chat = empathy_tp_chat_new (account, text_chan);
660 g_object_unref (tp_conn);
661 g_object_unref (text_chan);
668 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
670 EmpathyTpChatPriv *priv;
672 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
674 priv = GET_PRIV (chat);
676 return priv->acknowledge;
680 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
681 gboolean acknowledge)
683 EmpathyTpChatPriv *priv;
685 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
687 priv = GET_PRIV (chat);
689 priv->acknowledge = acknowledge;
690 g_object_notify (G_OBJECT (chat), "acknowledge");
694 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
696 EmpathyTpChatPriv *priv;
698 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
700 priv = GET_PRIV (chat);
702 return priv->channel;
706 empathy_tp_chat_get_account (EmpathyTpChat *chat)
708 EmpathyTpChatPriv *priv;
710 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
712 priv = GET_PRIV (chat);
714 return priv->account;
718 empathy_tp_chat_send (EmpathyTpChat *chat,
719 EmpathyMessage *message)
721 EmpathyTpChatPriv *priv;
722 const gchar *message_body;
723 EmpathyMessageType message_type;
725 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
726 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
728 priv = GET_PRIV (chat);
730 message_body = empathy_message_get_body (message);
731 message_type = empathy_message_get_type (message);
733 empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
734 tp_cli_channel_type_text_call_send (priv->channel, -1,
738 "sending message", NULL,
743 empathy_tp_chat_set_state (EmpathyTpChat *chat,
744 TpChannelChatState state)
746 EmpathyTpChatPriv *priv;
748 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
750 priv = GET_PRIV (chat);
752 empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
753 tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
756 "setting chat state",
762 empathy_tp_chat_get_id (EmpathyTpChat *chat)
764 EmpathyTpChatPriv *priv;
766 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
768 priv = GET_PRIV (chat);
773 tp_chan = tp_chan_new_from_channel (priv->channel);
774 priv->id = empathy_inspect_channel (priv->account, tp_chan);
775 g_object_unref (tp_chan);