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-chan-type-text-gen.h>
26 #include <libtelepathy/tp-chan-iface-chat-state-gen.h>
27 #include <libtelepathy/tp-conn.h>
28 #include <libtelepathy/tp-helpers.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"
36 #include "gossip-utils.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
41 #define DEBUG_DOMAIN "TpChat"
43 struct _EmpathyTpChatPriv {
44 EmpathyContactList *list;
45 EmpathyContactManager *manager;
51 DBusGProxy *text_iface;
52 DBusGProxy *chat_state_iface;
55 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
56 static void empathy_tp_chat_init (EmpathyTpChat *chat);
57 static void tp_chat_finalize (GObject *object);
58 static void tp_chat_destroy_cb (TpChan *text_chan,
60 static void tp_chat_closed_cb (TpChan *text_chan,
62 static void tp_chat_received_cb (DBusGProxy *text_iface,
70 static void tp_chat_sent_cb (DBusGProxy *text_iface,
75 static void tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
77 TelepathyChannelChatState state,
79 static void tp_chat_emit_message (EmpathyTpChat *chat,
83 const gchar *message_body);
92 static guint signals[LAST_SIGNAL];
94 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
97 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
99 GObjectClass *object_class = G_OBJECT_CLASS (klass);
101 object_class->finalize = tp_chat_finalize;
103 signals[MESSAGE_RECEIVED] =
104 g_signal_new ("message-received",
105 G_TYPE_FROM_CLASS (klass),
109 g_cclosure_marshal_VOID__OBJECT,
111 1, GOSSIP_TYPE_MESSAGE);
113 signals[CHAT_STATE_CHANGED] =
114 g_signal_new ("chat-state-changed",
115 G_TYPE_FROM_CLASS (klass),
119 empathy_marshal_VOID__OBJECT_UINT,
121 2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
124 g_signal_new ("destroy",
125 G_TYPE_FROM_CLASS (klass),
129 g_cclosure_marshal_VOID__VOID,
133 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
137 empathy_tp_chat_init (EmpathyTpChat *chat)
143 tp_chat_finalize (GObject *object)
145 EmpathyTpChatPriv *priv;
147 GError *error = NULL;
149 chat = EMPATHY_TP_CHAT (object);
150 priv = GET_PRIV (chat);
153 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
155 g_signal_handlers_disconnect_by_func (priv->tp_chan,
159 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
160 gossip_debug (DEBUG_DOMAIN,
161 "Error closing text channel: %s",
162 error ? error->message : "No error given");
163 g_clear_error (&error);
165 g_object_unref (priv->tp_chan);
169 g_object_unref (priv->manager);
172 g_object_unref (priv->list);
175 g_object_unref (priv->account);
178 g_object_unref (priv->mc);
182 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
186 empathy_tp_chat_new (McAccount *account,
189 EmpathyTpChatPriv *priv;
192 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
193 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
195 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
196 priv = GET_PRIV (chat);
198 priv->manager = empathy_contact_manager_new ();
199 priv->list = empathy_contact_manager_get_list (priv->manager, account);
200 priv->tp_chan = g_object_ref (tp_chan);
201 priv->account = g_object_ref (account);
202 priv->mc = gossip_mission_control_new ();
203 g_object_ref (priv->list);
205 priv->text_iface = tp_chan_get_interface (tp_chan,
206 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
207 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
208 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
210 g_signal_connect (priv->tp_chan, "destroy",
211 G_CALLBACK (tp_chat_destroy_cb),
213 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
214 G_CALLBACK (tp_chat_closed_cb),
216 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
217 G_CALLBACK (tp_chat_received_cb),
219 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
220 G_CALLBACK (tp_chat_sent_cb),
223 if (priv->chat_state_iface != NULL) {
224 dbus_g_proxy_connect_signal (priv->chat_state_iface,
226 G_CALLBACK (tp_chat_state_changed_cb),
234 empathy_tp_chat_new_with_contact (GossipContact *contact)
241 const gchar *bus_name;
244 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
246 mc = gossip_mission_control_new ();
247 account = gossip_contact_get_account (contact);
249 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
250 /* The account is not connected, nothing to do. */
254 tp_conn = mission_control_get_connection (mc, account, NULL);
255 g_return_val_if_fail (tp_conn != NULL, NULL);
256 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
257 handle = gossip_contact_get_handle (contact);
259 text_chan = tp_conn_new_channel (tp_get_bus (),
262 TP_IFACE_CHANNEL_TYPE_TEXT,
263 TP_HANDLE_TYPE_CONTACT,
267 chat = empathy_tp_chat_new (account, text_chan);
269 g_object_unref (tp_conn);
270 g_object_unref (text_chan);
277 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
279 EmpathyTpChatPriv *priv;
280 GPtrArray *messages_list;
282 GError *error = NULL;
284 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
286 priv = GET_PRIV (chat);
288 /* If we do this call async, don't forget to ignore Received signal
289 * until we get the answer */
290 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
294 gossip_debug (DEBUG_DOMAIN,
295 "Error retrieving pending messages: %s",
296 error ? error->message : "No error given");
297 g_clear_error (&error);
301 for (i = 0; i < messages_list->len; i++) {
302 GValueArray *message_struct;
303 const gchar *message_body;
310 message_struct = g_ptr_array_index (messages_list, i);
312 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
313 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
314 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
315 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
316 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
317 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
319 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
321 tp_chat_emit_message (chat,
327 g_value_array_free (message_struct);
330 g_ptr_array_free (messages_list, TRUE);
334 empathy_tp_chat_send (EmpathyTpChat *chat,
335 GossipMessage *message)
337 EmpathyTpChatPriv *priv;
338 const gchar *message_body;
339 GossipMessageType message_type;
340 GError *error = NULL;
342 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
343 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
345 priv = GET_PRIV (chat);
347 message_body = gossip_message_get_body (message);
348 message_type = gossip_message_get_type (message);
350 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
351 if (!tp_chan_type_text_send (priv->text_iface,
355 gossip_debug (DEBUG_DOMAIN,
357 error ? error->message : "No error given");
358 g_clear_error (&error);
363 empathy_tp_chat_set_state (EmpathyTpChat *chat,
364 TelepathyChannelChatState state)
366 EmpathyTpChatPriv *priv;
367 GError *error = NULL;
369 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
371 priv = GET_PRIV (chat);
373 if (priv->chat_state_iface) {
374 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
375 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
378 gossip_debug (DEBUG_DOMAIN,
379 "Set Chat State Error: %s",
380 error ? error->message : "No error given");
381 g_clear_error (&error);
387 empathy_tp_chat_get_id (EmpathyTpChat *chat)
389 EmpathyTpChatPriv *priv;
391 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
393 priv = GET_PRIV (chat);
399 priv->id = empathy_tp_chat_build_id (priv->account, priv->tp_chan);
405 empathy_tp_chat_build_id (McAccount *account,
413 GError *error = NULL;
415 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
416 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
418 mc = gossip_mission_control_new ();
419 tp_conn = mission_control_get_connection (mc, account, NULL);
422 handles = g_array_new (FALSE, FALSE, sizeof (guint));
423 g_array_append_val (handles, tp_chan->handle);
424 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
425 tp_chan->handle_type,
429 gossip_debug (DEBUG_DOMAIN,
430 "Couldn't get id: %s",
431 error ? error->message : "No error given");
432 g_clear_error (&error);
433 g_array_free (handles, TRUE);
434 g_object_unref (tp_conn);
439 /* A handle name is unique only for a specific account */
440 id = g_strdup_printf ("%s/%s",
441 mc_account_get_unique_name (account),
445 g_object_unref (tp_conn);
451 tp_chat_destroy_cb (TpChan *text_chan,
454 EmpathyTpChatPriv *priv;
456 priv = GET_PRIV (chat);
458 gossip_debug (DEBUG_DOMAIN, "Channel Closed or CM crashed");
460 g_object_unref (priv->tp_chan);
461 priv->tp_chan = NULL;
462 priv->text_iface = NULL;
463 priv->chat_state_iface = NULL;
465 g_signal_emit (chat, signals[DESTROY], 0);
469 tp_chat_closed_cb (TpChan *text_chan,
472 EmpathyTpChatPriv *priv;
474 priv = GET_PRIV (chat);
476 /* The channel is closed, do just like if the proxy was destroyed */
477 g_signal_handlers_disconnect_by_func (priv->tp_chan,
480 tp_chat_destroy_cb (text_chan, chat);
485 tp_chat_received_cb (DBusGProxy *text_iface,
494 EmpathyTpChatPriv *priv;
497 priv = GET_PRIV (chat);
499 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
501 tp_chat_emit_message (chat,
507 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
508 g_array_append_val (message_ids, message_id);
509 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
511 g_array_free (message_ids, TRUE);
515 tp_chat_sent_cb (DBusGProxy *text_iface,
521 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
523 tp_chat_emit_message (chat,
531 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
533 TelepathyChannelChatState state,
536 EmpathyTpChatPriv *priv;
537 GossipContact *contact;
539 priv = GET_PRIV (chat);
541 contact = empathy_contact_list_get_from_handle (priv->list, handle);
543 gossip_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
544 gossip_contact_get_name (contact),
548 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
550 g_object_unref (contact);
554 tp_chat_emit_message (EmpathyTpChat *chat,
558 const gchar *message_body)
560 EmpathyTpChatPriv *priv;
561 GossipMessage *message;
562 GossipContact *sender;
564 priv = GET_PRIV (chat);
566 if (from_handle == 0) {
567 sender = empathy_contact_list_get_own (priv->list);
568 g_object_ref (sender);
570 sender = empathy_contact_list_get_from_handle (priv->list,
574 message = gossip_message_new (message_body);
575 gossip_message_set_type (message, type);
576 gossip_message_set_sender (message, sender);
577 gossip_message_set_timestamp (message, (GossipTime) timestamp);
579 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
581 g_object_unref (message);
582 g_object_unref (sender);