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
22 #include <telepathy-glib/proxy-subclass.h>
24 #include <libempathy/empathy-chatroom-manager.h>
25 #include <libempathy/empathy-request-util.h>
26 #include <libempathy/empathy-individual-manager.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;
73 /* Cached to keep Folks in memory while empathy-chat is running; we don't
74 * want to reload it each time the last chat window is closed
76 EmpathyIndividualManager *individual_mgr;
80 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
81 EmpathyChatManagerPriv))
83 static EmpathyChatManager *chat_manager_singleton = NULL;
94 chat_data_new (EmpathyChat *chat)
96 ChatData *data = NULL;
98 data = g_slice_new0 (ChatData);
100 data->account = g_object_ref (empathy_chat_get_account (chat));
101 data->id = g_strdup (empathy_chat_get_id (chat));
102 data->room = empathy_chat_is_room (chat);
103 data->sms = empathy_chat_is_sms_channel (chat);
109 chat_data_free (ChatData *data)
111 if (data->account != NULL)
113 g_object_unref (data->account);
114 data->account = NULL;
117 if (data->id != NULL)
123 g_slice_free (ChatData, data);
127 chat_destroyed_cb (gpointer data,
130 EmpathyChatManager *self = data;
131 EmpathyChatManagerPriv *priv = GET_PRIV (self);
133 priv->num_displayed_chat--;
135 DEBUG ("Chat destroyed; we are now displaying %u chats",
136 priv->num_displayed_chat);
138 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
139 priv->num_displayed_chat);
143 join_cb (GObject *source,
144 GAsyncResult *result,
147 TpChannel *channel = TP_CHANNEL (source);
148 GError *error = NULL;
150 if (!tp_channel_join_finish (channel, result, &error))
152 DEBUG ("Failed to join chat (%s): %s",
153 tp_channel_get_identifier (channel), error->message);
154 g_error_free (error);
159 individual_mgr_cb (EmpathyChatWindow *window,
161 EmpathyChatManager *self)
163 EmpathyChatManagerPriv *priv = GET_PRIV (self);
165 if (priv->individual_mgr != NULL)
168 priv->individual_mgr = empathy_chat_window_get_individual_manager (window);
169 g_object_ref (priv->individual_mgr);
173 process_tp_chat (EmpathyChatManager *self,
174 EmpathyTpChat *tp_chat,
176 gint64 user_action_time)
178 EmpathyChatManagerPriv *priv = GET_PRIV (self);
179 EmpathyChat *chat = NULL;
181 EmpathyChatWindow *window;
183 id = empathy_tp_chat_get_id (tp_chat);
184 if (!tp_str_empty (id))
186 chat = empathy_chat_window_find_chat (account, id,
187 tp_text_channel_is_sms_channel ((TpTextChannel *) tp_chat));
192 empathy_chat_set_tp_chat (chat, tp_chat);
196 GHashTable *chats = NULL;
198 chat = empathy_chat_new (tp_chat);
199 /* empathy_chat_new returns a floating reference as EmpathyChat is
200 * a GtkWidget. This reference will be taken by a container
201 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
203 priv->num_displayed_chat++;
205 DEBUG ("Chat displayed; we are now displaying %u chat",
206 priv->num_displayed_chat);
208 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
209 priv->num_displayed_chat);
211 /* Set the saved message in the channel if we have one. */
212 chats = g_hash_table_lookup (priv->messages,
213 tp_proxy_get_object_path (account));
217 const gchar *msg = g_hash_table_lookup (chats, id);
220 empathy_chat_set_text (chat, msg);
223 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
226 window = empathy_chat_window_present_chat (chat, user_action_time);
228 if (priv->individual_mgr == NULL)
230 /* We want to cache it as soon it's created */
231 tp_g_signal_connect_object (window, "notify::individual-manager",
232 G_CALLBACK (individual_mgr_cb), self, 0);
235 if (empathy_tp_chat_is_invited (tp_chat, NULL))
237 /* We have been invited to the room. Add ourself as member as this
238 * channel has been approved. */
239 tp_channel_join_async (TP_CHANNEL (tp_chat), "", join_cb, self);
244 handle_channels (TpSimpleHandler *handler,
246 TpConnection *connection,
248 GList *requests_satisfied,
249 gint64 user_action_time,
250 TpHandleChannelsContext *context,
253 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
256 for (l = channels; l != NULL; l = g_list_next (l))
258 EmpathyTpChat *tp_chat = l->data;
260 if (tp_proxy_get_invalidated (tp_chat) != NULL)
263 if (!EMPATHY_IS_TP_CHAT (tp_chat))
265 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
266 tp_proxy_get_object_path (tp_chat));
270 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat));
272 process_tp_chat (self, tp_chat, account, user_action_time);
275 tp_handle_channels_context_accept (context);
279 empathy_chat_manager_init (EmpathyChatManager *self)
281 EmpathyChatManagerPriv *priv = GET_PRIV (self);
282 TpAccountManager *am;
283 GError *error = NULL;
285 priv->closed_queue = g_queue_new ();
286 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
287 g_free, (GDestroyNotify) g_hash_table_unref);
289 am = tp_account_manager_dup ();
291 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
293 /* Text channels handler */
294 priv->handler = tp_simple_handler_new_with_am (am, FALSE, FALSE,
295 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
299 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
300 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
301 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
304 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
305 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
306 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
309 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
310 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
311 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
314 if (!tp_base_client_register (priv->handler, &error))
316 g_critical ("Failed to register text handler: %s", error->message);
317 g_error_free (error);
322 empathy_chat_manager_finalize (GObject *object)
324 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
325 EmpathyChatManagerPriv *priv = GET_PRIV (self);
327 if (priv->closed_queue != NULL)
329 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
330 g_queue_free (priv->closed_queue);
331 priv->closed_queue = NULL;
334 tp_clear_pointer (&priv->messages, g_hash_table_unref);
336 tp_clear_object (&priv->handler);
337 tp_clear_object (&priv->chatroom_mgr);
338 tp_clear_object (&priv->individual_mgr);
340 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
344 empathy_chat_manager_constructor (GType type,
345 guint n_construct_params,
346 GObjectConstructParam *construct_params)
350 if (!chat_manager_singleton)
352 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
353 (type, n_construct_params, construct_params);
355 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
356 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
360 retval = g_object_ref (chat_manager_singleton);
367 empathy_chat_manager_constructed (GObject *obj)
369 TpDBusDaemon *dbus_daemon;
371 dbus_daemon = tp_dbus_daemon_dup (NULL);
373 if (dbus_daemon != NULL)
375 tp_dbus_daemon_register_object (dbus_daemon,
376 "/org/gnome/Empathy/ChatManager", obj);
378 g_object_unref (dbus_daemon);
383 empathy_chat_manager_class_init (
384 EmpathyChatManagerClass *empathy_chat_manager_class)
386 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
388 object_class->finalize = empathy_chat_manager_finalize;
389 object_class->constructor = empathy_chat_manager_constructor;
390 object_class->constructed = empathy_chat_manager_constructed;
392 signals[CLOSED_CHATS_CHANGED] =
393 g_signal_new ("closed-chats-changed",
394 G_TYPE_FROM_CLASS (object_class),
398 g_cclosure_marshal_generic,
400 1, G_TYPE_UINT, NULL);
402 signals[DISPLAYED_CHATS_CHANGED] =
403 g_signal_new ("displayed-chats-changed",
404 G_TYPE_FROM_CLASS (object_class),
408 g_cclosure_marshal_generic,
410 1, G_TYPE_UINT, NULL);
412 g_type_class_add_private (empathy_chat_manager_class,
413 sizeof (EmpathyChatManagerPriv));
417 empathy_chat_manager_dup_singleton (void)
419 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
423 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
426 EmpathyChatManagerPriv *priv = GET_PRIV (self);
431 data = chat_data_new (chat);
433 DEBUG ("Adding %s to closed queue: %s",
434 data->room ? "room" : "contact", data->id);
436 g_queue_push_tail (priv->closed_queue, data);
438 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
439 g_queue_get_length (priv->closed_queue));
441 /* If there was a message saved from last time it was closed
442 * (perhaps by accident?) save it to our hash table so it can be
443 * used again when the same chat pops up. Hot. */
444 message = empathy_chat_dup_text (chat);
446 chats = g_hash_table_lookup (priv->messages,
447 tp_proxy_get_object_path (data->account));
449 /* Don't create a new hash table if we don't already have one and we
450 * don't actually have a message to save. */
451 if (chats == NULL && tp_str_empty (message))
456 else if (chats == NULL && !tp_str_empty (message))
458 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
461 g_hash_table_insert (priv->messages,
462 g_strdup (tp_proxy_get_object_path (data->account)),
466 if (tp_str_empty (message))
468 g_hash_table_remove (chats, data->id);
474 /* takes ownership of message */
475 g_hash_table_insert (chats, g_strdup (data->id), message);
480 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
483 EmpathyChatManagerPriv *priv = GET_PRIV (self);
486 data = g_queue_pop_tail (priv->closed_queue);
491 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
492 data->room ? "room" : "contact", data->id);
495 empathy_join_muc (data->account, data->id, timestamp);
497 empathy_sms_contact_id (data->account, data->id, timestamp,
500 empathy_chat_with_contact_id (data->account, data->id, timestamp,
503 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
504 g_queue_get_length (priv->closed_queue));
506 chat_data_free (data);
510 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
512 EmpathyChatManagerPriv *priv = GET_PRIV (self);
514 return g_queue_get_length (priv->closed_queue);
518 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
520 DBusGMethodInvocation *context)
522 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
525 emp_svc_chat_manager_return_from_undo_closed_chat (context);
529 svc_iface_init (gpointer g_iface,
532 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
534 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
535 klass, empathy_chat_manager_dbus_##x)
536 IMPLEMENT(undo_closed_chat);
541 empathy_chat_manager_call_undo_closed_chat (void)
543 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
546 if (dbus_daemon == NULL)
549 proxy = g_object_new (TP_TYPE_PROXY,
550 "dbus-daemon", dbus_daemon,
551 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
552 "bus-name", EMPATHY_CHAT_BUS_NAME,
553 "object-path", "/org/gnome/Empathy/ChatManager",
556 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
558 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
559 NULL, NULL, NULL, NULL);
561 g_object_unref (proxy);
562 g_object_unref (dbus_daemon);