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>
24 #include <telepathy-glib/telepathy-glib-dbus.h>
26 #include "empathy-chatroom-manager.h"
27 #include "empathy-chat-window.h"
28 #include "empathy-request-util.h"
29 #include "empathy-ui-utils.h"
30 #include "extensions.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include "empathy-debug.h"
37 DISPLAYED_CHATS_CHANGED,
41 static guint signals[LAST_SIGNAL];
43 static void svc_iface_init (gpointer, gpointer);
45 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
47 G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
51 /* private structure */
52 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
54 struct _EmpathyChatManagerPriv
56 EmpathyChatroomManager *chatroom_mgr;
57 /* Queue of (ChatData *) representing the closed chats */
60 guint num_displayed_chat;
62 /* account path -> (GHashTable<(owned gchar *) contact ID
63 * -> (owned gchar *) non-NULL message>)
67 TpBaseClient *handler;
69 /* Cached to keep Folks in memory while empathy-chat is running; we don't
70 * want to reload it each time the last chat window is closed
72 EmpathyIndividualManager *individual_mgr;
76 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
77 EmpathyChatManagerPriv))
79 static EmpathyChatManager *chat_manager_singleton = NULL;
90 chat_data_new (EmpathyChat *chat)
92 ChatData *data = NULL;
94 data = g_slice_new0 (ChatData);
96 data->account = g_object_ref (empathy_chat_get_account (chat));
97 data->id = g_strdup (empathy_chat_get_id (chat));
98 data->room = empathy_chat_is_room (chat);
99 data->sms = empathy_chat_is_sms_channel (chat);
105 chat_data_free (ChatData *data)
107 if (data->account != NULL)
109 g_object_unref (data->account);
110 data->account = NULL;
113 if (data->id != NULL)
119 g_slice_free (ChatData, data);
123 chat_destroyed_cb (gpointer data,
126 EmpathyChatManager *self = data;
127 EmpathyChatManagerPriv *priv = GET_PRIV (self);
129 priv->num_displayed_chat--;
131 DEBUG ("Chat destroyed; we are now displaying %u chats",
132 priv->num_displayed_chat);
134 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
135 priv->num_displayed_chat);
139 join_cb (GObject *source,
140 GAsyncResult *result,
143 TpChannel *channel = TP_CHANNEL (source);
144 GError *error = NULL;
146 if (!tp_channel_join_finish (channel, result, &error))
148 DEBUG ("Failed to join chat (%s): %s",
149 tp_channel_get_identifier (channel), error->message);
150 g_error_free (error);
155 individual_mgr_cb (EmpathyChatWindow *window,
157 EmpathyChatManager *self)
159 EmpathyChatManagerPriv *priv = GET_PRIV (self);
161 if (priv->individual_mgr != NULL)
164 priv->individual_mgr = empathy_chat_window_get_individual_manager (window);
165 g_object_ref (priv->individual_mgr);
169 process_tp_chat (EmpathyChatManager *self,
170 EmpathyTpChat *tp_chat,
172 gint64 user_action_time)
174 EmpathyChatManagerPriv *priv = GET_PRIV (self);
175 EmpathyChat *chat = NULL;
177 EmpathyChatWindow *window;
179 id = empathy_tp_chat_get_id (tp_chat);
180 if (!tp_str_empty (id))
182 chat = empathy_chat_window_find_chat (account, id,
183 tp_text_channel_is_sms_channel ((TpTextChannel *) tp_chat));
188 empathy_chat_set_tp_chat (chat, tp_chat);
192 GHashTable *chats = NULL;
194 chat = empathy_chat_new (tp_chat);
195 /* empathy_chat_new returns a floating reference as EmpathyChat is
196 * a GtkWidget. This reference will be taken by a container
197 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
199 priv->num_displayed_chat++;
201 DEBUG ("Chat displayed; we are now displaying %u chat",
202 priv->num_displayed_chat);
204 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
205 priv->num_displayed_chat);
207 /* Set the saved message in the channel if we have one. */
208 chats = g_hash_table_lookup (priv->messages,
209 tp_proxy_get_object_path (account));
213 const gchar *msg = g_hash_table_lookup (chats, id);
216 empathy_chat_set_text (chat, msg);
219 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
222 window = empathy_chat_window_present_chat (chat, user_action_time);
224 if (priv->individual_mgr == NULL)
226 /* We want to cache it as soon it's created */
227 tp_g_signal_connect_object (window, "notify::individual-manager",
228 G_CALLBACK (individual_mgr_cb), self, 0);
231 if (empathy_tp_chat_is_invited (tp_chat, NULL))
233 /* We have been invited to the room. Add ourself as member as this
234 * channel has been approved. */
235 tp_channel_join_async (TP_CHANNEL (tp_chat), "", join_cb, self);
240 handle_channels (TpSimpleHandler *handler,
242 TpConnection *connection,
244 GList *requests_satisfied,
245 gint64 user_action_time,
246 TpHandleChannelsContext *context,
249 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
252 for (l = channels; l != NULL; l = g_list_next (l))
254 EmpathyTpChat *tp_chat = l->data;
256 if (tp_proxy_get_invalidated (tp_chat) != NULL)
259 if (!EMPATHY_IS_TP_CHAT (tp_chat))
261 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
262 tp_proxy_get_object_path (tp_chat));
266 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat));
268 process_tp_chat (self, tp_chat, account, user_action_time);
271 tp_handle_channels_context_accept (context);
275 empathy_chat_manager_init (EmpathyChatManager *self)
277 EmpathyChatManagerPriv *priv = GET_PRIV (self);
278 TpAccountManager *am;
279 GError *error = NULL;
281 priv->closed_queue = g_queue_new ();
282 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
283 g_free, (GDestroyNotify) g_hash_table_unref);
285 am = tp_account_manager_dup ();
287 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
289 /* Text channels handler */
290 priv->handler = tp_simple_handler_new_with_am (am, FALSE, FALSE,
291 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
295 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
296 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
297 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
300 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
301 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
302 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
305 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
306 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
307 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
310 if (!tp_base_client_register (priv->handler, &error))
312 g_critical ("Failed to register text handler: %s", error->message);
313 g_error_free (error);
318 empathy_chat_manager_finalize (GObject *object)
320 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
321 EmpathyChatManagerPriv *priv = GET_PRIV (self);
323 if (priv->closed_queue != NULL)
325 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
326 g_queue_free (priv->closed_queue);
327 priv->closed_queue = NULL;
330 tp_clear_pointer (&priv->messages, g_hash_table_unref);
332 tp_clear_object (&priv->handler);
333 tp_clear_object (&priv->chatroom_mgr);
334 tp_clear_object (&priv->individual_mgr);
336 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
340 empathy_chat_manager_constructor (GType type,
341 guint n_construct_params,
342 GObjectConstructParam *construct_params)
346 if (!chat_manager_singleton)
348 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
349 (type, n_construct_params, construct_params);
351 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
352 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
356 retval = g_object_ref (chat_manager_singleton);
363 empathy_chat_manager_constructed (GObject *obj)
365 TpDBusDaemon *dbus_daemon;
367 dbus_daemon = tp_dbus_daemon_dup (NULL);
369 if (dbus_daemon != NULL)
371 tp_dbus_daemon_register_object (dbus_daemon,
372 "/org/gnome/Empathy/ChatManager", obj);
374 g_object_unref (dbus_daemon);
379 empathy_chat_manager_class_init (
380 EmpathyChatManagerClass *empathy_chat_manager_class)
382 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
384 object_class->finalize = empathy_chat_manager_finalize;
385 object_class->constructor = empathy_chat_manager_constructor;
386 object_class->constructed = empathy_chat_manager_constructed;
388 signals[CLOSED_CHATS_CHANGED] =
389 g_signal_new ("closed-chats-changed",
390 G_TYPE_FROM_CLASS (object_class),
394 g_cclosure_marshal_generic,
396 1, G_TYPE_UINT, NULL);
398 signals[DISPLAYED_CHATS_CHANGED] =
399 g_signal_new ("displayed-chats-changed",
400 G_TYPE_FROM_CLASS (object_class),
404 g_cclosure_marshal_generic,
406 1, G_TYPE_UINT, NULL);
408 g_type_class_add_private (empathy_chat_manager_class,
409 sizeof (EmpathyChatManagerPriv));
413 empathy_chat_manager_dup_singleton (void)
415 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
419 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
422 EmpathyChatManagerPriv *priv = GET_PRIV (self);
427 data = chat_data_new (chat);
429 DEBUG ("Adding %s to closed queue: %s",
430 data->room ? "room" : "contact", data->id);
432 g_queue_push_tail (priv->closed_queue, data);
434 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
435 g_queue_get_length (priv->closed_queue));
437 /* If there was a message saved from last time it was closed
438 * (perhaps by accident?) save it to our hash table so it can be
439 * used again when the same chat pops up. Hot. */
440 message = empathy_chat_dup_text (chat);
442 chats = g_hash_table_lookup (priv->messages,
443 tp_proxy_get_object_path (data->account));
445 /* Don't create a new hash table if we don't already have one and we
446 * don't actually have a message to save. */
447 if (chats == NULL && tp_str_empty (message))
452 else if (chats == NULL && !tp_str_empty (message))
454 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
457 g_hash_table_insert (priv->messages,
458 g_strdup (tp_proxy_get_object_path (data->account)),
462 if (tp_str_empty (message))
464 g_hash_table_remove (chats, data->id);
470 /* takes ownership of message */
471 g_hash_table_insert (chats, g_strdup (data->id), message);
476 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
479 EmpathyChatManagerPriv *priv = GET_PRIV (self);
482 data = g_queue_pop_tail (priv->closed_queue);
487 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
488 data->room ? "room" : "contact", data->id);
491 empathy_join_muc (data->account, data->id, timestamp);
493 empathy_sms_contact_id (data->account, data->id, timestamp,
496 empathy_chat_with_contact_id (data->account, data->id, timestamp,
499 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
500 g_queue_get_length (priv->closed_queue));
502 chat_data_free (data);
506 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
508 EmpathyChatManagerPriv *priv = GET_PRIV (self);
510 return g_queue_get_length (priv->closed_queue);
514 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
516 DBusGMethodInvocation *context)
518 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
521 emp_svc_chat_manager_return_from_undo_closed_chat (context);
525 svc_iface_init (gpointer g_iface,
528 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
530 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
531 klass, empathy_chat_manager_dbus_##x)
532 IMPLEMENT(undo_closed_chat);
537 empathy_chat_manager_call_undo_closed_chat (void)
539 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
542 if (dbus_daemon == NULL)
545 proxy = g_object_new (TP_TYPE_PROXY,
546 "dbus-daemon", dbus_daemon,
547 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
548 "bus-name", EMPATHY_CHAT_BUS_NAME,
549 "object-path", "/org/gnome/Empathy/ChatManager",
552 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
554 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
555 NULL, NULL, NULL, NULL);
557 g_object_unref (proxy);
558 g_object_unref (dbus_daemon);