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_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->manager);
169 g_object_unref (priv->list);
172 g_object_unref (priv->account);
175 g_object_unref (priv->mc);
179 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
183 empathy_tp_chat_new (McAccount *account,
186 EmpathyTpChatPriv *priv;
189 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
190 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
192 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
193 priv = GET_PRIV (chat);
195 priv->manager = empathy_contact_manager_new ();
196 priv->list = empathy_contact_manager_get_list (priv->manager, account);
197 priv->tp_chan = g_object_ref (tp_chan);
198 priv->account = g_object_ref (account);
199 priv->mc = mission_control_new (tp_get_bus ());
200 g_object_ref (priv->list);
202 priv->text_iface = tp_chan_get_interface (tp_chan,
203 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
204 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
205 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
207 g_signal_connect (priv->tp_chan, "destroy",
208 G_CALLBACK (tp_chat_destroy_cb),
210 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
211 G_CALLBACK (tp_chat_received_cb),
213 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
214 G_CALLBACK (tp_chat_sent_cb),
217 if (priv->chat_state_iface != NULL) {
218 dbus_g_proxy_connect_signal (priv->chat_state_iface,
220 G_CALLBACK (tp_chat_state_changed_cb),
228 empathy_tp_chat_new_with_contact (GossipContact *contact)
235 const gchar *bus_name;
238 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
240 mc = mission_control_new (tp_get_bus ());
241 account = gossip_contact_get_account (contact);
243 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
244 /* The account is not connected, nothing to do. */
248 tp_conn = mission_control_get_connection (mc, account, NULL);
249 g_return_val_if_fail (tp_conn != NULL, NULL);
250 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
251 handle = gossip_contact_get_handle (contact);
253 text_chan = tp_conn_new_channel (tp_get_bus (),
256 TP_IFACE_CHANNEL_TYPE_TEXT,
257 TP_HANDLE_TYPE_CONTACT,
261 chat = empathy_tp_chat_new (account, text_chan);
263 g_object_unref (tp_conn);
264 g_object_unref (text_chan);
271 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
273 EmpathyTpChatPriv *priv;
274 GPtrArray *messages_list;
276 GError *error = NULL;
278 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
280 priv = GET_PRIV (chat);
282 /* If we do this call async, don't forget to ignore Received signal
283 * until we get the answer */
284 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
288 gossip_debug (DEBUG_DOMAIN,
289 "Error retrieving pending messages: %s",
290 error ? error->message : "No error given");
291 g_clear_error (&error);
295 for (i = 0; i < messages_list->len; i++) {
296 GValueArray *message_struct;
297 const gchar *message_body;
304 message_struct = g_ptr_array_index (messages_list, i);
306 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
307 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
308 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
309 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
310 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
311 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
313 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
315 tp_chat_emit_message (chat,
321 g_value_array_free (message_struct);
324 g_ptr_array_free (messages_list, TRUE);
328 empathy_tp_chat_send (EmpathyTpChat *chat,
329 GossipMessage *message)
331 EmpathyTpChatPriv *priv;
332 const gchar *message_body;
333 GossipMessageType message_type;
334 GError *error = NULL;
336 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
337 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
339 priv = GET_PRIV (chat);
341 message_body = gossip_message_get_body (message);
342 message_type = gossip_message_get_type (message);
344 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
345 if (!tp_chan_type_text_send (priv->text_iface,
349 gossip_debug (DEBUG_DOMAIN,
351 error ? error->message : "No error given");
352 g_clear_error (&error);
357 empathy_tp_chat_send_state (EmpathyTpChat *chat,
358 EmpathyTpChatState state)
360 EmpathyTpChatPriv *priv;
361 GError *error = NULL;
363 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
365 priv = GET_PRIV (chat);
367 if (priv->chat_state_iface) {
368 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
369 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
372 gossip_debug (DEBUG_DOMAIN,
373 "Set Chat State Error: %s",
374 error ? error->message : "No error given");
375 g_clear_error (&error);
381 empathy_tp_chat_get_id (EmpathyTpChat *chat)
383 EmpathyTpChatPriv *priv;
387 GError *error = NULL;
389 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
391 priv = GET_PRIV (chat);
397 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
398 handles = g_array_new (FALSE, FALSE, sizeof (guint));
399 g_array_append_val (handles, priv->tp_chan->handle);
401 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
402 priv->tp_chan->handle_type,
406 gossip_debug (DEBUG_DOMAIN,
407 "Couldn't get id: %s",
408 error ? error->message : "No error given");
409 g_clear_error (&error);
410 g_array_free (handles, TRUE);
411 g_object_unref (tp_conn);
416 /* A handle name is unique only for a specific account */
417 priv->id = g_strdup_printf ("%s/%s",
418 mc_account_get_unique_name (priv->account),
422 g_object_unref (tp_conn);
428 tp_chat_destroy_cb (TpChan *text_chan,
431 EmpathyTpChatPriv *priv;
433 priv = GET_PRIV (chat);
435 gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
437 g_object_unref (priv->tp_chan);
438 priv->tp_chan = NULL;
439 priv->text_iface = NULL;
440 priv->chat_state_iface = NULL;
442 g_signal_emit (chat, signals[DESTROY], 0);
446 tp_chat_received_cb (DBusGProxy *text_iface,
455 EmpathyTpChatPriv *priv;
458 priv = GET_PRIV (chat);
460 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
462 tp_chat_emit_message (chat,
468 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
469 g_array_append_val (message_ids, message_id);
470 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
472 g_array_free (message_ids, TRUE);
476 tp_chat_sent_cb (DBusGProxy *text_iface,
482 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
484 tp_chat_emit_message (chat,
492 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
494 EmpathyTpChatState state,
497 EmpathyTpChatPriv *priv;
498 GossipContact *contact;
500 priv = GET_PRIV (chat);
502 contact = empathy_contact_list_get_from_handle (priv->list, handle);
504 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
506 g_object_unref (contact);
510 tp_chat_emit_message (EmpathyTpChat *chat,
514 const gchar *message_body)
516 EmpathyTpChatPriv *priv;
517 GossipMessage *message;
518 GossipContact *sender;
520 priv = GET_PRIV (chat);
522 if (from_handle == 0) {
523 sender = empathy_contact_list_get_own (priv->list);
524 g_object_ref (sender);
526 sender = empathy_contact_list_get_from_handle (priv->list,
530 message = gossip_message_new (message_body);
531 gossip_message_set_type (message, type);
532 gossip_message_set_sender (message, sender);
533 gossip_message_set_timestamp (message, (GossipTime) timestamp);
535 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
537 g_object_unref (message);
538 g_object_unref (sender);