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>
29 #include "empathy-tp-chat.h"
30 #include "empathy-contact-manager.h"
31 #include "empathy-contact-list.h"
32 #include "empathy-session.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;
45 DBusGProxy *text_iface;
46 DBusGProxy *chat_state_iface;
49 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
50 static void empathy_tp_chat_init (EmpathyTpChat *chat);
51 static void tp_chat_finalize (GObject *object);
52 static void tp_chat_destroy_cb (TpChan *text_chan,
54 static void tp_chat_received_cb (DBusGProxy *text_iface,
62 static void tp_chat_sent_cb (DBusGProxy *text_iface,
67 static void tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
69 EmpathyTpChatState state,
71 static void tp_chat_emit_message (EmpathyTpChat *chat,
75 const gchar *message_body);
84 static guint signals[LAST_SIGNAL];
86 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
89 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
91 GObjectClass *object_class = G_OBJECT_CLASS (klass);
93 object_class->finalize = tp_chat_finalize;
95 signals[MESSAGE_RECEIVED] =
96 g_signal_new ("message-received",
97 G_TYPE_FROM_CLASS (klass),
101 g_cclosure_marshal_VOID__OBJECT,
103 1, GOSSIP_TYPE_MESSAGE);
105 signals[CHAT_STATE_CHANGED] =
106 g_signal_new ("chat-state-changed",
107 G_TYPE_FROM_CLASS (klass),
111 empathy_marshal_VOID__OBJECT_UINT,
113 2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
116 g_signal_new ("destroy",
117 G_TYPE_FROM_CLASS (klass),
121 g_cclosure_marshal_VOID__VOID,
125 g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
129 empathy_tp_chat_init (EmpathyTpChat *chat)
135 tp_chat_finalize (GObject *object)
137 EmpathyTpChatPriv *priv;
139 GError *error = NULL;
141 chat = EMPATHY_TP_CHAT (object);
142 priv = GET_PRIV (chat);
145 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
147 g_signal_handlers_disconnect_by_func (priv->tp_chan,
151 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
152 gossip_debug (DEBUG_DOMAIN,
153 "Error closing text channel: %s",
154 error ? error->message : "No error given");
155 g_clear_error (&error);
157 g_object_unref (priv->tp_chan);
161 g_object_unref (priv->list);
164 G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
168 empathy_tp_chat_new (McAccount *account,
171 EmpathyTpChatPriv *priv;
173 EmpathyContactManager *manager;
175 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
176 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
178 chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
179 priv = GET_PRIV (chat);
181 manager = empathy_session_get_contact_manager ();
182 priv->list = empathy_contact_manager_get_list (manager, account);
183 priv->tp_chan = g_object_ref (tp_chan);
184 g_object_ref (priv->list);
186 priv->text_iface = tp_chan_get_interface (tp_chan,
187 TELEPATHY_CHAN_IFACE_TEXT_QUARK);
188 priv->chat_state_iface = tp_chan_get_interface (tp_chan,
189 TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
191 g_signal_connect (priv->tp_chan, "destroy",
192 G_CALLBACK (tp_chat_destroy_cb),
194 dbus_g_proxy_connect_signal (priv->text_iface, "Received",
195 G_CALLBACK (tp_chat_received_cb),
197 dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
198 G_CALLBACK (tp_chat_sent_cb),
201 if (priv->chat_state_iface != NULL) {
202 dbus_g_proxy_connect_signal (priv->chat_state_iface,
204 G_CALLBACK (tp_chat_state_changed_cb),
212 empathy_tp_chat_new_with_contact (GossipContact *contact)
219 const gchar *bus_name;
222 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
224 mc = empathy_session_get_mission_control ();
225 account = gossip_contact_get_account (contact);
227 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
228 /* The account is not connected, nothing to do. */
232 tp_conn = mission_control_get_connection (mc, account, NULL);
233 g_return_val_if_fail (tp_conn != NULL, NULL);
234 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
235 handle = gossip_contact_get_handle (contact);
237 text_chan = tp_conn_new_channel (tp_get_bus (),
240 TP_IFACE_CHANNEL_TYPE_TEXT,
241 TP_HANDLE_TYPE_CONTACT,
245 chat = empathy_tp_chat_new (account, text_chan);
247 g_object_unref (tp_conn);
248 g_object_unref (text_chan);
254 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
256 EmpathyTpChatPriv *priv;
257 GPtrArray *messages_list;
259 GError *error = NULL;
261 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
263 priv = GET_PRIV (chat);
265 /* If we do this call async, don't forget to ignore Received signal
266 * until we get the answer */
267 if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
271 gossip_debug (DEBUG_DOMAIN,
272 "Error retrieving pending messages: %s",
273 error ? error->message : "No error given");
274 g_clear_error (&error);
278 for (i = 0; i < messages_list->len; i++) {
279 GValueArray *message_struct;
280 const gchar *message_body;
287 message_struct = g_ptr_array_index (messages_list, i);
289 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
290 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
291 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
292 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
293 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
294 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
296 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
298 tp_chat_emit_message (chat,
304 g_value_array_free (message_struct);
307 g_ptr_array_free (messages_list, TRUE);
311 empathy_tp_chat_send (EmpathyTpChat *chat,
312 GossipMessage *message)
314 EmpathyTpChatPriv *priv;
315 const gchar *message_body;
316 GossipMessageType message_type;
317 GError *error = NULL;
319 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
320 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
322 priv = GET_PRIV (chat);
324 message_body = gossip_message_get_body (message);
325 message_type = gossip_message_get_type (message);
327 gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
328 if (!tp_chan_type_text_send (priv->text_iface,
332 gossip_debug (DEBUG_DOMAIN,
334 error ? error->message : "No error given");
335 g_clear_error (&error);
340 empathy_tp_chat_send_state (EmpathyTpChat *chat,
341 EmpathyTpChatState state)
343 EmpathyTpChatPriv *priv;
344 GError *error = NULL;
346 g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
348 priv = GET_PRIV (chat);
350 if (priv->chat_state_iface) {
351 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
352 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
355 gossip_debug (DEBUG_DOMAIN,
356 "Set Chat State Error: %s",
357 error ? error->message : "No error given");
358 g_clear_error (&error);
364 tp_chat_destroy_cb (TpChan *text_chan,
367 EmpathyTpChatPriv *priv;
369 priv = GET_PRIV (chat);
371 gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
373 g_object_unref (priv->tp_chan);
374 priv->tp_chan = NULL;
375 priv->text_iface = NULL;
376 priv->chat_state_iface = NULL;
378 g_signal_emit (chat, signals[DESTROY], 0);
382 tp_chat_received_cb (DBusGProxy *text_iface,
391 EmpathyTpChatPriv *priv;
394 priv = GET_PRIV (chat);
396 gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
398 tp_chat_emit_message (chat,
404 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
405 g_array_append_val (message_ids, message_id);
406 tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
408 g_array_free (message_ids, TRUE);
412 tp_chat_sent_cb (DBusGProxy *text_iface,
418 gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
420 tp_chat_emit_message (chat,
428 tp_chat_state_changed_cb (DBusGProxy *chat_state_iface,
430 EmpathyTpChatState state,
433 EmpathyTpChatPriv *priv;
434 GossipContact *contact;
436 priv = GET_PRIV (chat);
438 contact = empathy_contact_list_get_from_handle (priv->list, handle);
440 g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
442 g_object_unref (contact);
446 tp_chat_emit_message (EmpathyTpChat *chat,
450 const gchar *message_body)
452 EmpathyTpChatPriv *priv;
453 GossipMessage *message;
454 GossipContact *sender;
456 priv = GET_PRIV (chat);
458 if (from_handle == 0) {
459 sender = empathy_contact_list_get_own (priv->list);
460 g_object_ref (sender);
462 sender = empathy_contact_list_get_from_handle (priv->list,
466 message = gossip_message_new (message_body);
467 gossip_message_set_type (message, type);
468 gossip_message_set_sender (message, sender);
469 gossip_message_set_timestamp (message, (GossipTime) timestamp);
471 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
473 g_object_unref (message);
474 g_object_unref (sender);