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;
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_received_cb (DBusGProxy *text_iface,
67 static void tp_chat_sent_cb (DBusGProxy *text_iface,
72 static void tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
74 EmpathyTpChatState state,
76 static void tp_chat_emit_message (EmpathyTpChat *chat,
80 const gchar *message_body);
89 static guint signals[LAST_SIGNAL];
91 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
94 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->finalize = tp_chat_finalize;
100 signals[MESSAGE_RECEIVED] =
101 g_signal_new ("message-received",
102 G_TYPE_FROM_CLASS (klass),
106 g_cclosure_marshal_VOID__OBJECT,
108 1, GOSSIP_TYPE_MESSAGE);
110 signals[CHAT_STATE_CHANGED] =
111 g_signal_new ("chat-state-changed",
112 G_TYPE_FROM_CLASS (klass),
116 empathy_marshal_VOID__OBJECT_UINT,
118 2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
121 g_signal_new ("destroy",
122 G_TYPE_FROM_CLASS (klass),
126 g_cclosure_marshal_VOID__VOID,
130 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
134 empathy_tp_chat_init (EmpathyTpChat *chat)
140 tp_chat_finalize (GObject *object)
142 EmpathyTpChatPriv *priv;
144 GError *error = NULL;
146 chat = EMPATHY_TP_CHAT (object);
147 priv = GET_PRIV (chat);
150 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
152 g_signal_handlers_disconnect_by_func (priv->tp_chan,
156 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
157 gossip_debug (DEBUG_DOMAIN,
158 "Error closing text channel: %s",
159 error ? error->message : "No error given");
160 g_clear_error (&error);
162 g_object_unref (priv->tp_chan);
166 g_object_unref (priv->list);
169 g_object_unref (priv->account);
172 g_object_unref (priv->mc);
176 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
180 empathy_tp_chat_new (McAccount *account,
183 EmpathyTpChatPriv *priv;
185 EmpathyContactManager *manager;
187 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
188 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
190 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
191 priv = GET_PRIV (chat);
193 manager = empathy_session_get_contact_manager ();
194 priv->list = empathy_contact_manager_get_list (manager, account);
195 priv->tp_chan = g_object_ref (tp_chan);
196 priv->account = g_object_ref (account);
197 priv->mc = mission_control_new (tp_get_bus ());
198 g_object_ref (priv->list);
200 priv->text_iface = tp_chan_get_interface (tp_chan,
201 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
202 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
203 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
205 g_signal_connect (priv->tp_chan, "destroy",
206 G_CALLBACK (tp_chat_destroy_cb),
208 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
209 G_CALLBACK (tp_chat_received_cb),
211 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
212 G_CALLBACK (tp_chat_sent_cb),
215 if (priv->chat_state_iface != NULL) {
216 dbus_g_proxy_connect_signal (priv->chat_state_iface,
218 G_CALLBACK (tp_chat_state_changed_cb),
226 empathy_tp_chat_new_with_contact (GossipContact *contact)
233 const gchar *bus_name;
236 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
238 mc = mission_control_new (tp_get_bus ());
239 account = gossip_contact_get_account (contact);
241 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
242 /* The account is not connected, nothing to do. */
246 tp_conn = mission_control_get_connection (mc, account, NULL);
247 g_return_val_if_fail (tp_conn != NULL, NULL);
248 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
249 handle = gossip_contact_get_handle (contact);
251 text_chan = tp_conn_new_channel (tp_get_bus (),
254 TP_IFACE_CHANNEL_TYPE_TEXT,
255 TP_HANDLE_TYPE_CONTACT,
259 chat = empathy_tp_chat_new (account, text_chan);
261 g_object_unref (tp_conn);
262 g_object_unref (text_chan);
269 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
271 EmpathyTpChatPriv *priv;
272 GPtrArray *messages_list;
274 GError *error = NULL;
276 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
278 priv = GET_PRIV (chat);
280 /* If we do this call async, don't forget to ignore Received signal
281 * until we get the answer */
282 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
286 gossip_debug (DEBUG_DOMAIN,
287 "Error retrieving pending messages: %s",
288 error ? error->message : "No error given");
289 g_clear_error (&error);
293 for (i = 0; i < messages_list->len; i++) {
294 GValueArray *message_struct;
295 const gchar *message_body;
302 message_struct = g_ptr_array_index (messages_list, i);
304 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
305 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
306 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
307 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
308 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
309 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
311 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
313 tp_chat_emit_message (chat,
319 g_value_array_free (message_struct);
322 g_ptr_array_free (messages_list, TRUE);
326 empathy_tp_chat_send (EmpathyTpChat *chat,
327 GossipMessage *message)
329 EmpathyTpChatPriv *priv;
330 const gchar *message_body;
331 GossipMessageType message_type;
332 GError *error = NULL;
334 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
335 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
337 priv = GET_PRIV (chat);
339 message_body = gossip_message_get_body (message);
340 message_type = gossip_message_get_type (message);
342 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
343 if (!tp_chan_type_text_send (priv->text_iface,
347 gossip_debug (DEBUG_DOMAIN,
349 error ? error->message : "No error given");
350 g_clear_error (&error);
355 empathy_tp_chat_send_state (EmpathyTpChat *chat,
356 EmpathyTpChatState state)
358 EmpathyTpChatPriv *priv;
359 GError *error = NULL;
361 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
363 priv = GET_PRIV (chat);
365 if (priv->chat_state_iface) {
366 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
367 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
370 gossip_debug (DEBUG_DOMAIN,
371 "Set Chat State Error: %s",
372 error ? error->message : "No error given");
373 g_clear_error (&error);
379 empathy_tp_chat_get_id (EmpathyTpChat *chat)
381 EmpathyTpChatPriv *priv;
385 GError *error = NULL;
387 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
389 priv = GET_PRIV (chat);
395 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
396 handles = g_array_new (FALSE, FALSE, sizeof (guint));
397 g_array_append_val (handles, priv->tp_chan->handle);
399 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
400 priv->tp_chan->handle_type,
404 gossip_debug (DEBUG_DOMAIN,
405 "Couldn't get id: %s",
406 error ? error->message : "No error given");
407 g_clear_error (&error);
408 g_array_free (handles, TRUE);
409 g_object_unref (tp_conn);
414 /* A handle name is unique only for a specific account */
415 priv->id = g_strdup_printf ("%s/%s",
416 mc_account_get_unique_name (priv->account),
420 g_object_unref (tp_conn);
426 tp_chat_destroy_cb (TpChan *text_chan,
429 EmpathyTpChatPriv *priv;
431 priv = GET_PRIV (chat);
433 gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
435 g_object_unref (priv->tp_chan);
436 priv->tp_chan = NULL;
437 priv->text_iface = NULL;
438 priv->chat_state_iface = NULL;
440 g_signal_emit (chat, signals[DESTROY], 0);
444 tp_chat_received_cb (DBusGProxy *text_iface,
453 EmpathyTpChatPriv *priv;
456 priv = GET_PRIV (chat);
458 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
460 tp_chat_emit_message (chat,
466 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
467 g_array_append_val (message_ids, message_id);
468 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
470 g_array_free (message_ids, TRUE);
474 tp_chat_sent_cb (DBusGProxy *text_iface,
480 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
482 tp_chat_emit_message (chat,
490 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
492 EmpathyTpChatState state,
495 EmpathyTpChatPriv *priv;
496 GossipContact *contact;
498 priv = GET_PRIV (chat);
500 contact = empathy_contact_list_get_from_handle (priv->list, handle);
502 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
504 g_object_unref (contact);
508 tp_chat_emit_message (EmpathyTpChat *chat,
512 const gchar *message_body)
514 EmpathyTpChatPriv *priv;
515 GossipMessage *message;
516 GossipContact *sender;
518 priv = GET_PRIV (chat);
520 if (from_handle == 0) {
521 sender = empathy_contact_list_get_own (priv->list);
522 g_object_ref (sender);
524 sender = empathy_contact_list_get_from_handle (priv->list,
528 message = gossip_message_new (message_body);
529 gossip_message_set_type (message, type);
530 gossip_message_set_sender (message, sender);
531 gossip_message_set_timestamp (message, (GossipTime) timestamp);
533 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
535 g_object_unref (message);
536 g_object_unref (sender);