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
21 #include "empathy-chat-manager.h"
23 #include <telepathy-glib/proxy-subclass.h>
25 #include "empathy-chatroom-manager.h"
26 #include "empathy-chat-window.h"
27 #include "empathy-request-util.h"
28 #include "empathy-ui-utils.h"
29 #include "extensions.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
36 DISPLAYED_CHATS_CHANGED,
40 static guint signals[LAST_SIGNAL];
42 static void svc_iface_init (gpointer, gpointer);
44 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
46 G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
50 /* private structure */
51 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
53 struct _EmpathyChatManagerPriv
55 EmpathyChatroomManager *chatroom_mgr;
56 /* Queue of (ChatData *) representing the closed chats */
59 guint num_displayed_chat;
61 /* account path -> (GHashTable<(owned gchar *) contact ID
62 * -> (owned gchar *) non-NULL message>)
66 TpBaseClient *handler;
68 /* Cached to keep Folks in memory while empathy-chat is running; we don't
69 * want to reload it each time the last chat window is closed
71 EmpathyIndividualManager *individual_mgr;
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 join_cb (GObject *source,
139 GAsyncResult *result,
142 TpChannel *channel = TP_CHANNEL (source);
143 GError *error = NULL;
145 if (!tp_channel_join_finish (channel, result, &error))
147 DEBUG ("Failed to join chat (%s): %s",
148 tp_channel_get_identifier (channel), error->message);
149 g_error_free (error);
154 individual_mgr_cb (EmpathyChatWindow *window,
156 EmpathyChatManager *self)
158 EmpathyChatManagerPriv *priv = GET_PRIV (self);
160 if (priv->individual_mgr != NULL)
163 priv->individual_mgr = empathy_chat_window_get_individual_manager (window);
164 g_object_ref (priv->individual_mgr);
168 process_tp_chat (EmpathyChatManager *self,
169 EmpathyTpChat *tp_chat,
171 gint64 user_action_time)
173 EmpathyChatManagerPriv *priv = GET_PRIV (self);
174 EmpathyChat *chat = NULL;
176 EmpathyChatWindow *window;
178 id = empathy_tp_chat_get_id (tp_chat);
179 if (!tp_str_empty (id))
181 chat = empathy_chat_window_find_chat (account, id,
182 tp_text_channel_is_sms_channel ((TpTextChannel *) tp_chat));
187 empathy_chat_set_tp_chat (chat, tp_chat);
191 GHashTable *chats = NULL;
193 chat = empathy_chat_new (tp_chat);
194 /* empathy_chat_new returns a floating reference as EmpathyChat is
195 * a GtkWidget. This reference will be taken by a container
196 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
198 priv->num_displayed_chat++;
200 DEBUG ("Chat displayed; we are now displaying %u chat",
201 priv->num_displayed_chat);
203 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
204 priv->num_displayed_chat);
206 /* Set the saved message in the channel if we have one. */
207 chats = g_hash_table_lookup (priv->messages,
208 tp_proxy_get_object_path (account));
212 const gchar *msg = g_hash_table_lookup (chats, id);
215 empathy_chat_set_text (chat, msg);
218 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
221 window = empathy_chat_window_present_chat (chat, user_action_time);
223 if (priv->individual_mgr == NULL)
225 /* We want to cache it as soon it's created */
226 tp_g_signal_connect_object (window, "notify::individual-manager",
227 G_CALLBACK (individual_mgr_cb), self, 0);
230 if (empathy_tp_chat_is_invited (tp_chat, NULL))
232 /* We have been invited to the room. Add ourself as member as this
233 * channel has been approved. */
234 tp_channel_join_async (TP_CHANNEL (tp_chat), "", join_cb, self);
239 handle_channels (TpSimpleHandler *handler,
241 TpConnection *connection,
243 GList *requests_satisfied,
244 gint64 user_action_time,
245 TpHandleChannelsContext *context,
248 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
251 for (l = channels; l != NULL; l = g_list_next (l))
253 EmpathyTpChat *tp_chat = l->data;
255 if (tp_proxy_get_invalidated (tp_chat) != NULL)
258 if (!EMPATHY_IS_TP_CHAT (tp_chat))
260 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
261 tp_proxy_get_object_path (tp_chat));
265 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat));
267 process_tp_chat (self, tp_chat, account, user_action_time);
270 tp_handle_channels_context_accept (context);
274 empathy_chat_manager_init (EmpathyChatManager *self)
276 EmpathyChatManagerPriv *priv = GET_PRIV (self);
277 TpAccountManager *am;
278 GError *error = NULL;
280 priv->closed_queue = g_queue_new ();
281 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
282 g_free, (GDestroyNotify) g_hash_table_unref);
284 am = tp_account_manager_dup ();
286 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
288 /* Text channels handler */
289 priv->handler = tp_simple_handler_new_with_am (am, FALSE, FALSE,
290 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
294 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
295 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
296 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
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_ROOM,
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_NONE,
309 if (!tp_base_client_register (priv->handler, &error))
311 g_critical ("Failed to register text handler: %s", error->message);
312 g_error_free (error);
317 empathy_chat_manager_finalize (GObject *object)
319 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
320 EmpathyChatManagerPriv *priv = GET_PRIV (self);
322 if (priv->closed_queue != NULL)
324 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
325 g_queue_free (priv->closed_queue);
326 priv->closed_queue = NULL;
329 tp_clear_pointer (&priv->messages, g_hash_table_unref);
331 tp_clear_object (&priv->handler);
332 tp_clear_object (&priv->chatroom_mgr);
333 tp_clear_object (&priv->individual_mgr);
335 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
339 empathy_chat_manager_constructor (GType type,
340 guint n_construct_params,
341 GObjectConstructParam *construct_params)
345 if (!chat_manager_singleton)
347 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
348 (type, n_construct_params, construct_params);
350 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
351 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
355 retval = g_object_ref (chat_manager_singleton);
362 empathy_chat_manager_constructed (GObject *obj)
364 TpDBusDaemon *dbus_daemon;
366 dbus_daemon = tp_dbus_daemon_dup (NULL);
368 if (dbus_daemon != NULL)
370 tp_dbus_daemon_register_object (dbus_daemon,
371 "/org/gnome/Empathy/ChatManager", obj);
373 g_object_unref (dbus_daemon);
378 empathy_chat_manager_class_init (
379 EmpathyChatManagerClass *empathy_chat_manager_class)
381 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
383 object_class->finalize = empathy_chat_manager_finalize;
384 object_class->constructor = empathy_chat_manager_constructor;
385 object_class->constructed = empathy_chat_manager_constructed;
387 signals[CLOSED_CHATS_CHANGED] =
388 g_signal_new ("closed-chats-changed",
389 G_TYPE_FROM_CLASS (object_class),
393 g_cclosure_marshal_generic,
395 1, G_TYPE_UINT, NULL);
397 signals[DISPLAYED_CHATS_CHANGED] =
398 g_signal_new ("displayed-chats-changed",
399 G_TYPE_FROM_CLASS (object_class),
403 g_cclosure_marshal_generic,
405 1, G_TYPE_UINT, NULL);
407 g_type_class_add_private (empathy_chat_manager_class,
408 sizeof (EmpathyChatManagerPriv));
412 empathy_chat_manager_dup_singleton (void)
414 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
418 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
421 EmpathyChatManagerPriv *priv = GET_PRIV (self);
426 data = chat_data_new (chat);
428 DEBUG ("Adding %s to closed queue: %s",
429 data->room ? "room" : "contact", data->id);
431 g_queue_push_tail (priv->closed_queue, data);
433 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
434 g_queue_get_length (priv->closed_queue));
436 /* If there was a message saved from last time it was closed
437 * (perhaps by accident?) save it to our hash table so it can be
438 * used again when the same chat pops up. Hot. */
439 message = empathy_chat_dup_text (chat);
441 chats = g_hash_table_lookup (priv->messages,
442 tp_proxy_get_object_path (data->account));
444 /* Don't create a new hash table if we don't already have one and we
445 * don't actually have a message to save. */
446 if (chats == NULL && tp_str_empty (message))
451 else if (chats == NULL && !tp_str_empty (message))
453 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
456 g_hash_table_insert (priv->messages,
457 g_strdup (tp_proxy_get_object_path (data->account)),
461 if (tp_str_empty (message))
463 g_hash_table_remove (chats, data->id);
469 /* takes ownership of message */
470 g_hash_table_insert (chats, g_strdup (data->id), message);
475 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
478 EmpathyChatManagerPriv *priv = GET_PRIV (self);
481 data = g_queue_pop_tail (priv->closed_queue);
486 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
487 data->room ? "room" : "contact", data->id);
490 empathy_join_muc (data->account, data->id, timestamp);
492 empathy_sms_contact_id (data->account, data->id, timestamp,
495 empathy_chat_with_contact_id (data->account, data->id, timestamp,
498 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
499 g_queue_get_length (priv->closed_queue));
501 chat_data_free (data);
505 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
507 EmpathyChatManagerPriv *priv = GET_PRIV (self);
509 return g_queue_get_length (priv->closed_queue);
513 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
515 DBusGMethodInvocation *context)
517 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
520 emp_svc_chat_manager_return_from_undo_closed_chat (context);
524 svc_iface_init (gpointer g_iface,
527 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
529 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
530 klass, empathy_chat_manager_dbus_##x)
531 IMPLEMENT(undo_closed_chat);
536 empathy_chat_manager_call_undo_closed_chat (void)
538 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
541 if (dbus_daemon == NULL)
544 proxy = g_object_new (TP_TYPE_PROXY,
545 "dbus-daemon", dbus_daemon,
546 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
547 "bus-name", EMPATHY_CHAT_BUS_NAME,
548 "object-path", "/org/gnome/Empathy/ChatManager",
551 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
553 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
554 NULL, NULL, NULL, NULL);
556 g_object_unref (proxy);
557 g_object_unref (dbus_daemon);