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-session.h"
34 #include "empathy-marshal.h"
35 #include "gossip-debug.h"
36 #include "gossip-time.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;
49 DBusGProxy *text_iface;
50 DBusGProxy *chat_state_iface;
53 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
54 static void empathy_tp_chat_init (EmpathyTpChat *chat);
55 static void tp_chat_finalize (GObject *object);
56 static void tp_chat_destroy_cb (TpChan *text_chan,
58 static void tp_chat_received_cb (DBusGProxy *text_iface,
66 static void tp_chat_sent_cb (DBusGProxy *text_iface,
71 static void tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
73 EmpathyTpChatState state,
75 static void tp_chat_emit_message (EmpathyTpChat *chat,
79 const gchar *message_body);
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
93 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 object_class->finalize = tp_chat_finalize;
99 signals[MESSAGE_RECEIVED] =
100 g_signal_new ("message-received",
101 G_TYPE_FROM_CLASS (klass),
105 g_cclosure_marshal_VOID__OBJECT,
107 1, GOSSIP_TYPE_MESSAGE);
109 signals[CHAT_STATE_CHANGED] =
110 g_signal_new ("chat-state-changed",
111 G_TYPE_FROM_CLASS (klass),
115 empathy_marshal_VOID__OBJECT_UINT,
117 2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
120 g_signal_new ("destroy",
121 G_TYPE_FROM_CLASS (klass),
125 g_cclosure_marshal_VOID__VOID,
129 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
133 empathy_tp_chat_init (EmpathyTpChat *chat)
139 tp_chat_finalize (GObject *object)
141 EmpathyTpChatPriv *priv;
143 GError *error = NULL;
145 chat = EMPATHY_TP_CHAT (object);
146 priv = GET_PRIV (chat);
149 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
151 g_signal_handlers_disconnect_by_func (priv->tp_chan,
155 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
156 gossip_debug (DEBUG_DOMAIN,
157 "Error closing text channel: %s",
158 error ? error->message : "No error given");
159 g_clear_error (&error);
161 g_object_unref (priv->tp_chan);
165 g_object_unref (priv->list);
168 g_object_unref (priv->account);
172 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
176 empathy_tp_chat_new (McAccount *account,
179 EmpathyTpChatPriv *priv;
181 EmpathyContactManager *manager;
183 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
184 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
186 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
187 priv = GET_PRIV (chat);
189 manager = empathy_session_get_contact_manager ();
190 priv->list = empathy_contact_manager_get_list (manager, account);
191 priv->tp_chan = g_object_ref (tp_chan);
192 priv->account = g_object_ref (account);
193 g_object_ref (priv->list);
195 priv->text_iface = tp_chan_get_interface (tp_chan,
196 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
197 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
198 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
200 g_signal_connect (priv->tp_chan, "destroy",
201 G_CALLBACK (tp_chat_destroy_cb),
203 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
204 G_CALLBACK (tp_chat_received_cb),
206 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
207 G_CALLBACK (tp_chat_sent_cb),
210 if (priv->chat_state_iface != NULL) {
211 dbus_g_proxy_connect_signal (priv->chat_state_iface,
213 G_CALLBACK (tp_chat_state_changed_cb),
221 empathy_tp_chat_new_with_contact (GossipContact *contact)
228 const gchar *bus_name;
231 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
233 mc = empathy_session_get_mission_control ();
234 account = gossip_contact_get_account (contact);
236 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
237 /* The account is not connected, nothing to do. */
241 tp_conn = mission_control_get_connection (mc, account, NULL);
242 g_return_val_if_fail (tp_conn != NULL, NULL);
243 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
244 handle = gossip_contact_get_handle (contact);
246 text_chan = tp_conn_new_channel (tp_get_bus (),
249 TP_IFACE_CHANNEL_TYPE_TEXT,
250 TP_HANDLE_TYPE_CONTACT,
254 chat = empathy_tp_chat_new (account, text_chan);
256 g_object_unref (tp_conn);
257 g_object_unref (text_chan);
263 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
265 EmpathyTpChatPriv *priv;
266 GPtrArray *messages_list;
268 GError *error = NULL;
270 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
272 priv = GET_PRIV (chat);
274 /* If we do this call async, don't forget to ignore Received signal
275 * until we get the answer */
276 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
280 gossip_debug (DEBUG_DOMAIN,
281 "Error retrieving pending messages: %s",
282 error ? error->message : "No error given");
283 g_clear_error (&error);
287 for (i = 0; i < messages_list->len; i++) {
288 GValueArray *message_struct;
289 const gchar *message_body;
296 message_struct = g_ptr_array_index (messages_list, i);
298 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
299 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
300 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
301 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
302 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
303 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
305 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
307 tp_chat_emit_message (chat,
313 g_value_array_free (message_struct);
316 g_ptr_array_free (messages_list, TRUE);
320 empathy_tp_chat_send (EmpathyTpChat *chat,
321 GossipMessage *message)
323 EmpathyTpChatPriv *priv;
324 const gchar *message_body;
325 GossipMessageType message_type;
326 GError *error = NULL;
328 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
329 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
331 priv = GET_PRIV (chat);
333 message_body = gossip_message_get_body (message);
334 message_type = gossip_message_get_type (message);
336 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
337 if (!tp_chan_type_text_send (priv->text_iface,
341 gossip_debug (DEBUG_DOMAIN,
343 error ? error->message : "No error given");
344 g_clear_error (&error);
349 empathy_tp_chat_send_state (EmpathyTpChat *chat,
350 EmpathyTpChatState state)
352 EmpathyTpChatPriv *priv;
353 GError *error = NULL;
355 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
357 priv = GET_PRIV (chat);
359 if (priv->chat_state_iface) {
360 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
361 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
364 gossip_debug (DEBUG_DOMAIN,
365 "Set Chat State Error: %s",
366 error ? error->message : "No error given");
367 g_clear_error (&error);
373 empathy_tp_chat_get_id (EmpathyTpChat *chat)
375 EmpathyTpChatPriv *priv;
380 GError *error = NULL;
382 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
384 priv = GET_PRIV (chat);
390 mc = empathy_session_get_mission_control ();
391 tp_conn = mission_control_get_connection (mc, priv->account, NULL);
392 handles = g_array_new (FALSE, FALSE, sizeof (guint));
393 g_array_append_val (handles, priv->tp_chan->handle);
395 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
396 priv->tp_chan->handle_type,
400 gossip_debug (DEBUG_DOMAIN,
401 "Couldn't get id: %s",
402 error ? error->message : "No error given");
403 g_clear_error (&error);
404 g_array_free (handles, TRUE);
405 g_object_unref (tp_conn);
410 /* A handle name is unique only for a specific account */
411 priv->id = g_strdup_printf ("%s/%s",
412 mc_account_get_unique_name (priv->account),
416 g_object_unref (tp_conn);
422 tp_chat_destroy_cb (TpChan *text_chan,
425 EmpathyTpChatPriv *priv;
427 priv = GET_PRIV (chat);
429 gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
431 g_object_unref (priv->tp_chan);
432 priv->tp_chan = NULL;
433 priv->text_iface = NULL;
434 priv->chat_state_iface = NULL;
436 g_signal_emit (chat, signals[DESTROY], 0);
440 tp_chat_received_cb (DBusGProxy *text_iface,
449 EmpathyTpChatPriv *priv;
452 priv = GET_PRIV (chat);
454 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
456 tp_chat_emit_message (chat,
462 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
463 g_array_append_val (message_ids, message_id);
464 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
466 g_array_free (message_ids, TRUE);
470 tp_chat_sent_cb (DBusGProxy *text_iface,
476 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
478 tp_chat_emit_message (chat,
486 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
488 EmpathyTpChatState state,
491 EmpathyTpChatPriv *priv;
492 GossipContact *contact;
494 priv = GET_PRIV (chat);
496 contact = empathy_contact_list_get_from_handle (priv->list, handle);
498 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
500 g_object_unref (contact);
504 tp_chat_emit_message (EmpathyTpChat *chat,
508 const gchar *message_body)
510 EmpathyTpChatPriv *priv;
511 GossipMessage *message;
512 GossipContact *sender;
514 priv = GET_PRIV (chat);
516 if (from_handle == 0) {
517 sender = empathy_contact_list_get_own (priv->list);
518 g_object_ref (sender);
520 sender = empathy_contact_list_get_from_handle (priv->list,
524 message = gossip_message_new (message_body);
525 gossip_message_set_type (message, type);
526 gossip_message_set_sender (message, sender);
527 gossip_message_set_timestamp (message, (GossipTime) timestamp);
529 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
531 g_object_unref (message);
532 g_object_unref (sender);