2 * empathy-chat-manager.c - Source for EmpathyChatManager
3 * Copyright (C) 2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <telepathy-glib/telepathy-glib.h>
21 #include <telepathy-glib/proxy-subclass.h>
23 #include <libempathy/empathy-channel-factory.h>
24 #include <libempathy/empathy-chatroom-manager.h>
25 #include <libempathy/empathy-request-util.h>
26 #include <libempathy/empathy-utils.h>
28 #include <libempathy-gtk/empathy-ui-utils.h>
30 #include "empathy-chat-window.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-chat-manager.h"
37 #include <extensions/extensions.h>
41 DISPLAYED_CHATS_CHANGED,
45 static guint signals[LAST_SIGNAL];
47 static void svc_iface_init (gpointer, gpointer);
49 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
51 G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
55 /* private structure */
56 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
58 struct _EmpathyChatManagerPriv
60 EmpathyChatroomManager *chatroom_mgr;
61 /* Queue of (ChatData *) representing the closed chats */
64 guint num_displayed_chat;
66 /* account path -> (GHashTable<(owned gchar *) contact ID
67 * -> (owned gchar *) non-NULL message>)
71 TpBaseClient *handler;
75 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
76 EmpathyChatManagerPriv))
78 static EmpathyChatManager *chat_manager_singleton = NULL;
89 chat_data_new (EmpathyChat *chat)
91 ChatData *data = NULL;
93 data = g_slice_new0 (ChatData);
95 data->account = g_object_ref (empathy_chat_get_account (chat));
96 data->id = g_strdup (empathy_chat_get_id (chat));
97 data->room = empathy_chat_is_room (chat);
98 data->sms = empathy_chat_is_sms_channel (chat);
104 chat_data_free (ChatData *data)
106 if (data->account != NULL)
108 g_object_unref (data->account);
109 data->account = NULL;
112 if (data->id != NULL)
118 g_slice_free (ChatData, data);
122 chat_destroyed_cb (gpointer data,
125 EmpathyChatManager *self = data;
126 EmpathyChatManagerPriv *priv = GET_PRIV (self);
128 priv->num_displayed_chat--;
130 DEBUG ("Chat destroyed; we are now displaying %u chats",
131 priv->num_displayed_chat);
133 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
134 priv->num_displayed_chat);
138 process_tp_chat (EmpathyChatManager *self,
139 EmpathyTpChat *tp_chat,
141 gint64 user_action_time)
143 EmpathyChatManagerPriv *priv = GET_PRIV (self);
144 EmpathyChat *chat = NULL;
147 id = empathy_tp_chat_get_id (tp_chat);
148 if (!tp_str_empty (id))
150 chat = empathy_chat_window_find_chat (account, id,
151 empathy_tp_chat_is_sms_channel (tp_chat));
156 empathy_chat_set_tp_chat (chat, tp_chat);
160 GHashTable *chats = NULL;
162 chat = empathy_chat_new (tp_chat);
163 /* empathy_chat_new returns a floating reference as EmpathyChat is
164 * a GtkWidget. This reference will be taken by a container
165 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
167 priv->num_displayed_chat++;
169 DEBUG ("Chat displayed; we are now displaying %u chat",
170 priv->num_displayed_chat);
172 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
173 priv->num_displayed_chat);
175 /* Set the saved message in the channel if we have one. */
176 chats = g_hash_table_lookup (priv->messages,
177 tp_proxy_get_object_path (account));
181 const gchar *msg = g_hash_table_lookup (chats, id);
184 empathy_chat_set_text (chat, msg);
187 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
189 empathy_chat_window_present_chat (chat, user_action_time);
191 if (empathy_tp_chat_is_invited (tp_chat, NULL))
193 /* We have been invited to the room. Add ourself as member as this
194 * channel has been approved. */
195 empathy_tp_chat_join (tp_chat);
200 handle_channels (TpSimpleHandler *handler,
202 TpConnection *connection,
204 GList *requests_satisfied,
205 gint64 user_action_time,
206 TpHandleChannelsContext *context,
209 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
212 for (l = channels; l != NULL; l = g_list_next (l))
214 EmpathyTpChat *tp_chat = l->data;
216 if (tp_proxy_get_invalidated (tp_chat) != NULL)
219 if (!EMPATHY_IS_TP_CHAT (tp_chat))
221 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
222 tp_proxy_get_object_path (tp_chat));
226 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat));
228 process_tp_chat (self, tp_chat, account, user_action_time);
231 tp_handle_channels_context_accept (context);
235 empathy_chat_manager_init (EmpathyChatManager *self)
237 EmpathyChatManagerPriv *priv = GET_PRIV (self);
239 GError *error = NULL;
240 EmpathyChannelFactory *factory;
242 priv->closed_queue = g_queue_new ();
243 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
244 g_free, (GDestroyNotify) g_hash_table_unref);
246 dbus = tp_dbus_daemon_dup (&error);
249 g_critical ("Failed to get D-Bus daemon: %s", error->message);
250 g_error_free (error);
254 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
256 /* Text channels handler */
257 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
258 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
260 /* EmpathyTpChat relies on these features being prepared */
261 tp_base_client_add_connection_features_varargs (priv->handler,
262 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
263 tp_base_client_add_channel_features_varargs (priv->handler,
264 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
266 g_object_unref (dbus);
268 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
269 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
270 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
273 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
274 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
275 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
278 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
279 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
280 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
283 factory = empathy_channel_factory_dup ();
285 tp_base_client_set_channel_factory (priv->handler,
286 TP_CLIENT_CHANNEL_FACTORY (factory));
288 if (!tp_base_client_register (priv->handler, &error))
290 g_critical ("Failed to register text handler: %s", error->message);
291 g_error_free (error);
294 g_object_unref (factory);
298 empathy_chat_manager_finalize (GObject *object)
300 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
301 EmpathyChatManagerPriv *priv = GET_PRIV (self);
303 if (priv->closed_queue != NULL)
305 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
306 g_queue_free (priv->closed_queue);
307 priv->closed_queue = NULL;
310 tp_clear_pointer (&priv->messages, g_hash_table_unref);
312 tp_clear_object (&priv->handler);
313 tp_clear_object (&priv->chatroom_mgr);
315 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
319 empathy_chat_manager_constructor (GType type,
320 guint n_construct_params,
321 GObjectConstructParam *construct_params)
325 if (!chat_manager_singleton)
327 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
328 (type, n_construct_params, construct_params);
330 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
331 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
335 retval = g_object_ref (chat_manager_singleton);
342 empathy_chat_manager_constructed (GObject *obj)
344 TpDBusDaemon *dbus_daemon;
346 dbus_daemon = tp_dbus_daemon_dup (NULL);
348 if (dbus_daemon != NULL)
350 tp_dbus_daemon_register_object (dbus_daemon,
351 "/org/gnome/Empathy/ChatManager", obj);
353 g_object_unref (dbus_daemon);
358 empathy_chat_manager_class_init (
359 EmpathyChatManagerClass *empathy_chat_manager_class)
361 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
363 object_class->finalize = empathy_chat_manager_finalize;
364 object_class->constructor = empathy_chat_manager_constructor;
365 object_class->constructed = empathy_chat_manager_constructed;
367 signals[CLOSED_CHATS_CHANGED] =
368 g_signal_new ("closed-chats-changed",
369 G_TYPE_FROM_CLASS (object_class),
373 g_cclosure_marshal_VOID__UINT,
375 1, G_TYPE_UINT, NULL);
377 signals[DISPLAYED_CHATS_CHANGED] =
378 g_signal_new ("displayed-chats-changed",
379 G_TYPE_FROM_CLASS (object_class),
383 g_cclosure_marshal_VOID__UINT,
385 1, G_TYPE_UINT, NULL);
387 g_type_class_add_private (empathy_chat_manager_class,
388 sizeof (EmpathyChatManagerPriv));
392 empathy_chat_manager_dup_singleton (void)
394 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
398 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
401 EmpathyChatManagerPriv *priv = GET_PRIV (self);
406 data = chat_data_new (chat);
408 DEBUG ("Adding %s to closed queue: %s",
409 data->room ? "room" : "contact", data->id);
411 g_queue_push_tail (priv->closed_queue, data);
413 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
414 g_queue_get_length (priv->closed_queue));
416 /* If there was a message saved from last time it was closed
417 * (perhaps by accident?) save it to our hash table so it can be
418 * used again when the same chat pops up. Hot. */
419 message = empathy_chat_dup_text (chat);
421 chats = g_hash_table_lookup (priv->messages,
422 tp_proxy_get_object_path (data->account));
424 /* Don't create a new hash table if we don't already have one and we
425 * don't actually have a message to save. */
426 if (chats == NULL && tp_str_empty (message))
431 else if (chats == NULL && !tp_str_empty (message))
433 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
436 g_hash_table_insert (priv->messages,
437 g_strdup (tp_proxy_get_object_path (data->account)),
441 if (tp_str_empty (message))
443 g_hash_table_remove (chats, data->id);
449 /* takes ownership of message */
450 g_hash_table_insert (chats, g_strdup (data->id), message);
455 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
458 EmpathyChatManagerPriv *priv = GET_PRIV (self);
461 data = g_queue_pop_tail (priv->closed_queue);
466 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
467 data->room ? "room" : "contact", data->id);
470 empathy_join_muc (data->account, data->id, timestamp);
472 empathy_sms_contact_id (data->account, data->id, timestamp);
474 empathy_chat_with_contact_id (data->account, data->id, timestamp);
476 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
477 g_queue_get_length (priv->closed_queue));
479 chat_data_free (data);
483 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
485 EmpathyChatManagerPriv *priv = GET_PRIV (self);
487 return g_queue_get_length (priv->closed_queue);
491 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
493 DBusGMethodInvocation *context)
495 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
498 emp_svc_chat_manager_return_from_undo_closed_chat (context);
502 svc_iface_init (gpointer g_iface,
505 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
507 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
508 klass, empathy_chat_manager_dbus_##x)
509 IMPLEMENT(undo_closed_chat);
514 empathy_chat_manager_call_undo_closed_chat (void)
516 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
519 if (dbus_daemon == NULL)
522 proxy = g_object_new (TP_TYPE_PROXY,
523 "dbus-daemon", dbus_daemon,
524 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
525 "bus-name", EMPATHY_CHAT_BUS_NAME,
526 "object-path", "/org/gnome/Empathy/ChatManager",
529 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
531 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
532 NULL, NULL, NULL, NULL);
534 g_object_unref (proxy);
535 g_object_unref (dbus_daemon);