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;
88 chat_data_new (EmpathyChat *chat)
90 ChatData *data = NULL;
92 data = g_slice_new0 (ChatData);
94 data->account = g_object_ref (empathy_chat_get_account (chat));
95 data->id = g_strdup (empathy_chat_get_id (chat));
96 data->room = empathy_chat_is_room (chat);
102 chat_data_free (ChatData *data)
104 if (data->account != NULL)
106 g_object_unref (data->account);
107 data->account = NULL;
110 if (data->id != NULL)
116 g_slice_free (ChatData, data);
120 chat_destroyed_cb (gpointer data,
123 EmpathyChatManager *self = data;
124 EmpathyChatManagerPriv *priv = GET_PRIV (self);
126 priv->num_displayed_chat--;
128 DEBUG ("Chat destroyed; we are now displaying %u chats",
129 priv->num_displayed_chat);
131 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
132 priv->num_displayed_chat);
136 process_tp_chat (EmpathyChatManager *self,
137 EmpathyTpChat *tp_chat,
139 gint64 user_action_time)
141 EmpathyChatManagerPriv *priv = GET_PRIV (self);
142 EmpathyChat *chat = NULL;
145 id = empathy_tp_chat_get_id (tp_chat);
146 if (!tp_str_empty (id))
148 chat = empathy_chat_window_find_chat (account, id,
149 empathy_tp_chat_is_sms_channel (tp_chat));
154 empathy_chat_set_tp_chat (chat, tp_chat);
158 GHashTable *chats = NULL;
160 chat = empathy_chat_new (tp_chat);
161 /* empathy_chat_new returns a floating reference as EmpathyChat is
162 * a GtkWidget. This reference will be taken by a container
163 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
165 priv->num_displayed_chat++;
167 DEBUG ("Chat displayed; we are now displaying %u chat",
168 priv->num_displayed_chat);
170 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
171 priv->num_displayed_chat);
173 /* Set the saved message in the channel if we have one. */
174 chats = g_hash_table_lookup (priv->messages,
175 tp_proxy_get_object_path (account));
179 const gchar *msg = g_hash_table_lookup (chats, id);
182 empathy_chat_set_text (chat, msg);
185 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
187 empathy_chat_window_present_chat (chat, user_action_time);
189 if (empathy_tp_chat_is_invited (tp_chat, NULL))
191 /* We have been invited to the room. Add ourself as member as this
192 * channel has been approved. */
193 empathy_tp_chat_join (tp_chat);
196 g_object_unref (tp_chat);
201 EmpathyChatManager *self;
202 EmpathyTpChat *tp_chat;
204 gint64 user_action_time;
208 static chat_ready_ctx *
209 chat_ready_ctx_new (EmpathyChatManager *self,
210 EmpathyTpChat *tp_chat,
212 gint64 user_action_time)
214 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
216 ctx->self = g_object_ref (self);
217 ctx->tp_chat = g_object_ref (tp_chat);
218 ctx->account = g_object_ref (account);
219 ctx->user_action_time = user_action_time;
224 chat_ready_ctx_free (chat_ready_ctx *ctx)
226 g_object_unref (ctx->self);
227 g_object_unref (ctx->tp_chat);
228 g_object_unref (ctx->account);
230 if (ctx->sig_id != 0)
231 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
233 g_slice_free (chat_ready_ctx, ctx);
237 tp_chat_ready_cb (GObject *object,
241 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
242 chat_ready_ctx *ctx = user_data;
244 if (!empathy_tp_chat_is_ready (tp_chat))
247 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
249 chat_ready_ctx_free (ctx);
253 handle_channels (TpSimpleHandler *handler,
255 TpConnection *connection,
257 GList *requests_satisfied,
258 gint64 user_action_time,
259 TpHandleChannelsContext *context,
262 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
265 for (l = channels; l != NULL; l = g_list_next (l))
267 TpChannel *channel = l->data;
268 EmpathyTpChat *tp_chat;
270 if (tp_proxy_get_invalidated (channel) != NULL)
273 if (!TP_IS_TEXT_CHANNEL (channel))
275 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
276 tp_proxy_get_object_path (channel));
280 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
282 tp_chat = empathy_tp_chat_new (account, channel);
284 if (empathy_tp_chat_is_ready (tp_chat))
286 process_tp_chat (self, tp_chat, account, user_action_time);
290 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
293 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
294 G_CALLBACK (tp_chat_ready_cb), ctx);
298 tp_handle_channels_context_accept (context);
302 empathy_chat_manager_init (EmpathyChatManager *self)
304 EmpathyChatManagerPriv *priv = GET_PRIV (self);
306 GError *error = NULL;
307 EmpathyChannelFactory *factory;
309 priv->closed_queue = g_queue_new ();
310 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
311 g_free, (GDestroyNotify) g_hash_table_unref);
313 dbus = tp_dbus_daemon_dup (&error);
316 g_critical ("Failed to get D-Bus daemon: %s", error->message);
317 g_error_free (error);
321 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
323 /* Text channels handler */
324 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
325 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
327 /* EmpathyTpChat relies on these features being prepared */
328 tp_base_client_add_connection_features_varargs (priv->handler,
329 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
330 tp_base_client_add_channel_features_varargs (priv->handler,
331 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
333 g_object_unref (dbus);
335 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
336 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
337 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
340 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
341 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
342 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
345 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
346 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
347 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
350 factory = empathy_channel_factory_dup ();
352 tp_base_client_set_channel_factory (priv->handler,
353 TP_CLIENT_CHANNEL_FACTORY (factory));
355 if (!tp_base_client_register (priv->handler, &error))
357 g_critical ("Failed to register text handler: %s", error->message);
358 g_error_free (error);
361 g_object_unref (factory);
365 empathy_chat_manager_finalize (GObject *object)
367 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
368 EmpathyChatManagerPriv *priv = GET_PRIV (self);
370 if (priv->closed_queue != NULL)
372 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
373 g_queue_free (priv->closed_queue);
374 priv->closed_queue = NULL;
377 tp_clear_pointer (&priv->messages, g_hash_table_unref);
379 tp_clear_object (&priv->handler);
380 tp_clear_object (&priv->chatroom_mgr);
382 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
386 empathy_chat_manager_constructor (GType type,
387 guint n_construct_params,
388 GObjectConstructParam *construct_params)
392 if (!chat_manager_singleton)
394 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
395 (type, n_construct_params, construct_params);
397 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
398 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
402 retval = g_object_ref (chat_manager_singleton);
409 empathy_chat_manager_constructed (GObject *obj)
411 TpDBusDaemon *dbus_daemon;
413 dbus_daemon = tp_dbus_daemon_dup (NULL);
415 if (dbus_daemon != NULL)
417 tp_dbus_daemon_register_object (dbus_daemon,
418 "/org/gnome/Empathy/ChatManager", obj);
420 g_object_unref (dbus_daemon);
425 empathy_chat_manager_class_init (
426 EmpathyChatManagerClass *empathy_chat_manager_class)
428 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
430 object_class->finalize = empathy_chat_manager_finalize;
431 object_class->constructor = empathy_chat_manager_constructor;
432 object_class->constructed = empathy_chat_manager_constructed;
434 signals[CLOSED_CHATS_CHANGED] =
435 g_signal_new ("closed-chats-changed",
436 G_TYPE_FROM_CLASS (object_class),
440 g_cclosure_marshal_VOID__UINT,
442 1, G_TYPE_UINT, NULL);
444 signals[DISPLAYED_CHATS_CHANGED] =
445 g_signal_new ("displayed-chats-changed",
446 G_TYPE_FROM_CLASS (object_class),
450 g_cclosure_marshal_VOID__UINT,
452 1, G_TYPE_UINT, NULL);
454 g_type_class_add_private (empathy_chat_manager_class,
455 sizeof (EmpathyChatManagerPriv));
459 empathy_chat_manager_dup_singleton (void)
461 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
465 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
468 EmpathyChatManagerPriv *priv = GET_PRIV (self);
473 data = chat_data_new (chat);
475 DEBUG ("Adding %s to closed queue: %s",
476 data->room ? "room" : "contact", data->id);
478 g_queue_push_tail (priv->closed_queue, data);
480 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
481 g_queue_get_length (priv->closed_queue));
483 /* If there was a message saved from last time it was closed
484 * (perhaps by accident?) save it to our hash table so it can be
485 * used again when the same chat pops up. Hot. */
486 message = empathy_chat_dup_text (chat);
488 chats = g_hash_table_lookup (priv->messages,
489 tp_proxy_get_object_path (data->account));
491 /* Don't create a new hash table if we don't already have one and we
492 * don't actually have a message to save. */
493 if (chats == NULL && tp_str_empty (message))
498 else if (chats == NULL && !tp_str_empty (message))
500 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
503 g_hash_table_insert (priv->messages,
504 g_strdup (tp_proxy_get_object_path (data->account)),
508 if (tp_str_empty (message))
510 g_hash_table_remove (chats, data->id);
516 /* takes ownership of message */
517 g_hash_table_insert (chats, g_strdup (data->id), message);
522 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
525 EmpathyChatManagerPriv *priv = GET_PRIV (self);
528 data = g_queue_pop_tail (priv->closed_queue);
533 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
534 data->room ? "room" : "contact", data->id);
537 empathy_join_muc (data->account, data->id, timestamp);
539 empathy_chat_with_contact_id (data->account, data->id, timestamp);
541 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
542 g_queue_get_length (priv->closed_queue));
544 chat_data_free (data);
548 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
550 EmpathyChatManagerPriv *priv = GET_PRIV (self);
552 return g_queue_get_length (priv->closed_queue);
556 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
558 DBusGMethodInvocation *context)
560 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
563 emp_svc_chat_manager_return_from_undo_closed_chat (context);
567 svc_iface_init (gpointer g_iface,
570 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
572 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
573 klass, empathy_chat_manager_dbus_##x)
574 IMPLEMENT(undo_closed_chat);
579 empathy_chat_manager_call_undo_closed_chat (void)
581 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
584 if (dbus_daemon == NULL)
587 proxy = g_object_new (TP_TYPE_PROXY,
588 "dbus-daemon", dbus_daemon,
589 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
590 "bus-name", EMPATHY_CHAT_BUS_NAME,
591 "object-path", "/org/gnome/Empathy/ChatManager",
594 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
596 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
597 NULL, NULL, NULL, NULL);
599 g_object_unref (proxy);
600 g_object_unref (dbus_daemon);