1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <libtelepathy/tp-helpers.h>
26 #include <libtelepathy/tp-chan-type-text-gen.h>
27 #include <libtelepathy/tp-chan-iface-chat-state-gen.h>
28 #include <libtelepathy/tp-conn.h>
30 #include "empathy-tp-chat.h"
31 #include "empathy-contact-manager.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "gossip-debug.h"
35 #include "gossip-time.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 EmpathyContactList *list;
44 EmpathyContactManager *manager;
50 DBusGProxy *text_iface;
51 DBusGProxy *chat_state_iface;
54 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
55 static void empathy_tp_chat_init (EmpathyTpChat *chat);
56 static void tp_chat_finalize (GObject *object);
57 static void tp_chat_destroy_cb (TpChan *text_chan,
59 static void tp_chat_closed_cb (TpChan *text_chan,
61 static void tp_chat_received_cb (DBusGProxy *text_iface,
69 static void tp_chat_sent_cb (DBusGProxy *text_iface,
74 static void tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
76 TelepathyChannelChatState state,
78 static void tp_chat_emit_message (EmpathyTpChat *chat,
82 const gchar *message_body);
91 static guint signals[LAST_SIGNAL];
93 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
96 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
98 GObjectClass *object_class = G_OBJECT_CLASS (klass);
100 object_class->finalize = tp_chat_finalize;
102 signals[MESSAGE_RECEIVED] =
103 g_signal_new ("message-received",
104 G_TYPE_FROM_CLASS (klass),
108 g_cclosure_marshal_VOID__OBJECT,
110 1, GOSSIP_TYPE_MESSAGE);
112 signals[CHAT_STATE_CHANGED] =
113 g_signal_new ("chat-state-changed",
114 G_TYPE_FROM_CLASS (klass),
118 empathy_marshal_VOID__OBJECT_UINT,
120 2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
123 g_signal_new ("destroy",
124 G_TYPE_FROM_CLASS (klass),
128 g_cclosure_marshal_VOID__VOID,
132 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
136 empathy_tp_chat_init (EmpathyTpChat *chat)
142 tp_chat_finalize (GObject *object)
144 EmpathyTpChatPriv *priv;
146 GError *error = NULL;
148 chat = EMPATHY_TP_CHAT (object);
149 priv = GET_PRIV (chat);
152 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
154 g_signal_handlers_disconnect_by_func (priv->tp_chan,
158 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
159 gossip_debug (DEBUG_DOMAIN,
160 "Error closing text channel: %s",
161 error ? error->message : "No error given");
162 g_clear_error (&error);
164 g_object_unref (priv->tp_chan);
168 g_object_unref (priv->manager);
171 g_object_unref (priv->list);
174 g_object_unref (priv->account);
177 g_object_unref (priv->mc);
181 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
185 empathy_tp_chat_new (McAccount *account,
188 EmpathyTpChatPriv *priv;
191 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
192 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
194 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
195 priv = GET_PRIV (chat);
197 priv->manager = empathy_contact_manager_new ();
198 priv->list = empathy_contact_manager_get_list (priv->manager, account);
199 priv->tp_chan = g_object_ref (tp_chan);
200 priv->account = g_object_ref (account);
201 priv->mc = mission_control_new (tp_get_bus ());
202 g_object_ref (priv->list);
204 priv->text_iface = tp_chan_get_interface (tp_chan,
205 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
206 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
207 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
209 g_signal_connect (priv->tp_chan, "destroy",
210 G_CALLBACK (tp_chat_destroy_cb),
212 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
213 G_CALLBACK (tp_chat_closed_cb),
215 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
216 G_CALLBACK (tp_chat_received_cb),
218 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
219 G_CALLBACK (tp_chat_sent_cb),
222 if (priv->chat_state_iface != NULL) {
223 dbus_g_proxy_connect_signal (priv->chat_state_iface,
225 G_CALLBACK (tp_chat_state_changed_cb),
233 empathy_tp_chat_new_with_contact (GossipContact *contact)
240 const gchar *bus_name;
243 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
245 mc = mission_control_new (tp_get_bus ());
246 account = gossip_contact_get_account (contact);
248 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
249 /* The account is not connected, nothing to do. */
253 tp_conn = mission_control_get_connection (mc, account, NULL);
254 g_return_val_if_fail (tp_conn != NULL, NULL);
255 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
256 handle = gossip_contact_get_handle (contact);
258 text_chan = tp_conn_new_channel (tp_get_bus (),
261 TP_IFACE_CHANNEL_TYPE_TEXT,
262 TP_HANDLE_TYPE_CONTACT,
266 chat = empathy_tp_chat_new (account, text_chan);
268 g_object_unref (tp_conn);
269 g_object_unref (text_chan);
276 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
278 EmpathyTpChatPriv *priv;
279 GPtrArray *messages_list;
281 GError *error = NULL;
283 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
285 priv = GET_PRIV (chat);
287 /* If we do this call async, don't forget to ignore Received signal
288 * until we get the answer */
289 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
293 gossip_debug (DEBUG_DOMAIN,
294 "Error retrieving pending messages: %s",
295 error ? error->message : "No error given");
296 g_clear_error (&error);
300 for (i = 0; i < messages_list->len; i++) {
301 GValueArray *message_struct;
302 const gchar *message_body;
309 message_struct = g_ptr_array_index (messages_list, i);
311 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
312 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
313 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
314 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
315 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
316 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
318 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
320 tp_chat_emit_message (chat,
326 g_value_array_free (message_struct);
329 g_ptr_array_free (messages_list, TRUE);
333 empathy_tp_chat_send (EmpathyTpChat *chat,
334 GossipMessage *message)
336 EmpathyTpChatPriv *priv;
337 const gchar *message_body;
338 GossipMessageType message_type;
339 GError *error = NULL;
341 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
342 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
344 priv = GET_PRIV (chat);
346 message_body = gossip_message_get_body (message);
347 message_type = gossip_message_get_type (message);
349 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
350 if (!tp_chan_type_text_send (priv->text_iface,
354 gossip_debug (DEBUG_DOMAIN,
356 error ? error->message : "No error given");
357 g_clear_error (&error);
362 empathy_tp_chat_set_state (EmpathyTpChat *chat,
363 TelepathyChannelChatState state)
365 EmpathyTpChatPriv *priv;
366 GError *error = NULL;
368 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
370 priv = GET_PRIV (chat);
372 if (priv->chat_state_iface) {
373 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
374 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
377 gossip_debug (DEBUG_DOMAIN,
378 "Set Chat State Error: %s",
379 error ? error->message : "No error given");
380 g_clear_error (&error);
386 empathy_tp_chat_get_id (EmpathyTpChat *chat)
388 EmpathyTpChatPriv *priv;
392 GError *error = NULL;
394 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
396 priv = GET_PRIV (chat);
402 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
403 handles = g_array_new (FALSE, FALSE, sizeof (guint));
404 g_array_append_val (handles, priv->tp_chan->handle);
406 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
407 priv->tp_chan->handle_type,
411 gossip_debug (DEBUG_DOMAIN,
412 "Couldn't get id: %s",
413 error ? error->message : "No error given");
414 g_clear_error (&error);
415 g_array_free (handles, TRUE);
416 g_object_unref (tp_conn);
421 /* A handle name is unique only for a specific account */
422 priv->id = g_strdup_printf ("%s/%s",
423 mc_account_get_unique_name (priv->account),
427 g_object_unref (tp_conn);
433 tp_chat_destroy_cb (TpChan *text_chan,
436 EmpathyTpChatPriv *priv;
438 priv = GET_PRIV (chat);
440 gossip_debug (DEBUG_DOMAIN, "Channel Closed or CM crashed");
442 g_object_unref (priv->tp_chan);
443 priv->tp_chan = NULL;
444 priv->text_iface = NULL;
445 priv->chat_state_iface = NULL;
447 g_signal_emit (chat, signals[DESTROY], 0);
451 tp_chat_closed_cb (TpChan *text_chan,
454 EmpathyTpChatPriv *priv;
456 priv = GET_PRIV (chat);
458 /* The channel is closed, do just like if the proxy was destroyed */
459 g_signal_handlers_disconnect_by_func (priv->tp_chan,
462 tp_chat_destroy_cb (text_chan, chat);
467 tp_chat_received_cb (DBusGProxy *text_iface,
476 EmpathyTpChatPriv *priv;
479 priv = GET_PRIV (chat);
481 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
483 tp_chat_emit_message (chat,
489 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
490 g_array_append_val (message_ids, message_id);
491 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
493 g_array_free (message_ids, TRUE);
497 tp_chat_sent_cb (DBusGProxy *text_iface,
503 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
505 tp_chat_emit_message (chat,
513 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
515 TelepathyChannelChatState state,
518 EmpathyTpChatPriv *priv;
519 GossipContact *contact;
521 priv = GET_PRIV (chat);
523 contact = empathy_contact_list_get_from_handle (priv->list, handle);
525 gossip_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
526 gossip_contact_get_name (contact),
530 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
532 g_object_unref (contact);
536 tp_chat_emit_message (EmpathyTpChat *chat,
540 const gchar *message_body)
542 EmpathyTpChatPriv *priv;
543 GossipMessage *message;
544 GossipContact *sender;
546 priv = GET_PRIV (chat);
548 if (from_handle == 0) {
549 sender = empathy_contact_list_get_own (priv->list);
550 g_object_ref (sender);
552 sender = empathy_contact_list_get_from_handle (priv->list,
556 message = gossip_message_new (message_body);
557 gossip_message_set_type (message, type);
558 gossip_message_set_sender (message, sender);
559 gossip_message_set_timestamp (message, (GossipTime) timestamp);
561 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
563 g_object_unref (message);
564 g_object_unref (sender);