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-chatroom-manager.h>
24 #include <libempathy/empathy-request-util.h>
25 #include <libempathy/empathy-utils.h>
27 #include "empathy-chat-window.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
32 #include "empathy-chat-manager.h"
34 #include <extensions/extensions.h>
38 DISPLAYED_CHATS_CHANGED,
42 static guint signals[LAST_SIGNAL];
44 static void svc_iface_init (gpointer, gpointer);
46 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
48 G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
52 /* private structure */
53 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
55 struct _EmpathyChatManagerPriv
57 EmpathyChatroomManager *chatroom_mgr;
58 /* Queue of (ChatData *) representing the closed chats */
61 guint num_displayed_chat;
63 /* account path -> (GHashTable<(owned gchar *) contact ID
64 * -> (owned gchar *) non-NULL message>)
68 TpBaseClient *handler;
72 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
73 EmpathyChatManagerPriv))
75 static EmpathyChatManager *chat_manager_singleton = NULL;
85 chat_data_new (EmpathyChat *chat)
87 ChatData *data = NULL;
89 data = g_slice_new0 (ChatData);
91 data->account = g_object_ref (empathy_chat_get_account (chat));
92 data->id = g_strdup (empathy_chat_get_id (chat));
93 data->room = empathy_chat_is_room (chat);
99 chat_data_free (ChatData *data)
101 if (data->account != NULL)
103 g_object_unref (data->account);
104 data->account = NULL;
107 if (data->id != NULL)
113 g_slice_free (ChatData, data);
117 chat_destroyed_cb (gpointer data,
120 EmpathyChatManager *self = data;
121 EmpathyChatManagerPriv *priv = GET_PRIV (self);
123 priv->num_displayed_chat--;
125 DEBUG ("Chat destroyed; we are now displaying %u chats",
126 priv->num_displayed_chat);
128 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
129 priv->num_displayed_chat);
133 process_tp_chat (EmpathyChatManager *self,
134 EmpathyTpChat *tp_chat,
136 gint64 user_action_time)
138 EmpathyChatManagerPriv *priv = GET_PRIV (self);
139 EmpathyChat *chat = NULL;
142 id = empathy_tp_chat_get_id (tp_chat);
143 if (!tp_str_empty (id))
145 chat = empathy_chat_window_find_chat (account, id,
146 empathy_tp_chat_is_sms_channel (tp_chat));
151 empathy_chat_set_tp_chat (chat, tp_chat);
155 GHashTable *chats = NULL;
157 chat = empathy_chat_new (tp_chat);
158 /* empathy_chat_new returns a floating reference as EmpathyChat is
159 * a GtkWidget. This reference will be taken by a container
160 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
162 priv->num_displayed_chat++;
164 DEBUG ("Chat displayed; we are now displaying %u chat",
165 priv->num_displayed_chat);
167 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
168 priv->num_displayed_chat);
170 /* Set the saved message in the channel if we have one. */
171 chats = g_hash_table_lookup (priv->messages,
172 tp_proxy_get_object_path (account));
176 const gchar *msg = g_hash_table_lookup (chats, id);
179 empathy_chat_set_text (chat, msg);
182 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
184 empathy_chat_window_present_chat (chat, user_action_time);
186 if (empathy_tp_chat_is_invited (tp_chat, NULL))
188 /* We have been invited to the room. Add ourself as member as this
189 * channel has been approved. */
190 empathy_tp_chat_join (tp_chat);
193 g_object_unref (tp_chat);
198 EmpathyChatManager *self;
199 EmpathyTpChat *tp_chat;
201 gint64 user_action_time;
205 static chat_ready_ctx *
206 chat_ready_ctx_new (EmpathyChatManager *self,
207 EmpathyTpChat *tp_chat,
209 gint64 user_action_time)
211 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
213 ctx->self = g_object_ref (self);
214 ctx->tp_chat = g_object_ref (tp_chat);
215 ctx->account = g_object_ref (account);
216 ctx->user_action_time = user_action_time;
221 chat_ready_ctx_free (chat_ready_ctx *ctx)
223 g_object_unref (ctx->self);
224 g_object_unref (ctx->tp_chat);
225 g_object_unref (ctx->account);
227 if (ctx->sig_id != 0)
228 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
230 g_slice_free (chat_ready_ctx, ctx);
234 tp_chat_ready_cb (GObject *object,
238 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
239 chat_ready_ctx *ctx = user_data;
241 if (!empathy_tp_chat_is_ready (tp_chat))
244 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
246 chat_ready_ctx_free (ctx);
250 handle_channels (TpSimpleHandler *handler,
252 TpConnection *connection,
254 GList *requests_satisfied,
255 gint64 user_action_time,
256 TpHandleChannelsContext *context,
259 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
262 for (l = channels; l != NULL; l = g_list_next (l))
264 TpChannel *channel = l->data;
265 EmpathyTpChat *tp_chat;
267 if (tp_proxy_get_invalidated (channel) != NULL)
270 if (!TP_IS_TEXT_CHANNEL (channel))
272 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
273 tp_proxy_get_object_path (channel));
277 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
279 tp_chat = empathy_tp_chat_new (account, channel);
281 if (empathy_tp_chat_is_ready (tp_chat))
283 process_tp_chat (self, tp_chat, account, user_action_time);
287 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
290 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
291 G_CALLBACK (tp_chat_ready_cb), ctx);
295 tp_handle_channels_context_accept (context);
299 empathy_chat_manager_init (EmpathyChatManager *self)
301 EmpathyChatManagerPriv *priv = GET_PRIV (self);
303 GError *error = NULL;
305 priv->closed_queue = g_queue_new ();
306 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
307 g_free, (GDestroyNotify) g_hash_table_unref);
309 dbus = tp_dbus_daemon_dup (&error);
312 g_critical ("Failed to get D-Bus daemon: %s", error->message);
313 g_error_free (error);
317 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
319 /* Text channels handler */
320 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
321 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
323 /* EmpathyTpChat relies on these features being prepared */
324 tp_base_client_add_connection_features_varargs (priv->handler,
325 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
326 tp_base_client_add_channel_features_varargs (priv->handler,
327 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
329 g_object_unref (dbus);
331 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
332 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
333 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
336 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
337 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
338 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
341 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
342 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
343 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
346 if (!tp_base_client_register (priv->handler, &error))
348 g_critical ("Failed to register text handler: %s", error->message);
349 g_error_free (error);
354 empathy_chat_manager_finalize (GObject *object)
356 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
357 EmpathyChatManagerPriv *priv = GET_PRIV (self);
359 if (priv->closed_queue != NULL)
361 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
362 g_queue_free (priv->closed_queue);
363 priv->closed_queue = NULL;
366 tp_clear_pointer (&priv->messages, g_hash_table_unref);
368 tp_clear_object (&priv->handler);
369 tp_clear_object (&priv->chatroom_mgr);
371 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
375 empathy_chat_manager_constructor (GType type,
376 guint n_construct_params,
377 GObjectConstructParam *construct_params)
381 if (!chat_manager_singleton)
383 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
384 (type, n_construct_params, construct_params);
386 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
387 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
391 retval = g_object_ref (chat_manager_singleton);
398 empathy_chat_manager_constructed (GObject *obj)
400 TpDBusDaemon *dbus_daemon;
402 dbus_daemon = tp_dbus_daemon_dup (NULL);
404 if (dbus_daemon != NULL)
406 tp_dbus_daemon_register_object (dbus_daemon,
407 "/org/gnome/Empathy/ChatManager", obj);
409 g_object_unref (dbus_daemon);
414 empathy_chat_manager_class_init (
415 EmpathyChatManagerClass *empathy_chat_manager_class)
417 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
419 object_class->finalize = empathy_chat_manager_finalize;
420 object_class->constructor = empathy_chat_manager_constructor;
421 object_class->constructed = empathy_chat_manager_constructed;
423 signals[CLOSED_CHATS_CHANGED] =
424 g_signal_new ("closed-chats-changed",
425 G_TYPE_FROM_CLASS (object_class),
429 g_cclosure_marshal_VOID__UINT,
431 1, G_TYPE_UINT, NULL);
433 signals[DISPLAYED_CHATS_CHANGED] =
434 g_signal_new ("displayed-chats-changed",
435 G_TYPE_FROM_CLASS (object_class),
439 g_cclosure_marshal_VOID__UINT,
441 1, G_TYPE_UINT, NULL);
443 g_type_class_add_private (empathy_chat_manager_class,
444 sizeof (EmpathyChatManagerPriv));
448 empathy_chat_manager_dup_singleton (void)
450 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
454 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
457 EmpathyChatManagerPriv *priv = GET_PRIV (self);
462 data = chat_data_new (chat);
464 DEBUG ("Adding %s to closed queue: %s",
465 data->room ? "room" : "contact", data->id);
467 g_queue_push_tail (priv->closed_queue, data);
469 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
470 g_queue_get_length (priv->closed_queue));
472 /* If there was a message saved from last time it was closed
473 * (perhaps by accident?) save it to our hash table so it can be
474 * used again when the same chat pops up. Hot. */
475 message = empathy_chat_dup_text (chat);
477 chats = g_hash_table_lookup (priv->messages,
478 tp_proxy_get_object_path (data->account));
480 /* Don't create a new hash table if we don't already have one and we
481 * don't actually have a message to save. */
482 if (chats == NULL && tp_str_empty (message))
487 else if (chats == NULL && !tp_str_empty (message))
489 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
492 g_hash_table_insert (priv->messages,
493 g_strdup (tp_proxy_get_object_path (data->account)),
497 if (tp_str_empty (message))
499 g_hash_table_remove (chats, data->id);
505 /* takes ownership of message */
506 g_hash_table_insert (chats, g_strdup (data->id), message);
511 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
513 EmpathyChatManagerPriv *priv = GET_PRIV (self);
516 data = g_queue_pop_tail (priv->closed_queue);
521 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
522 data->room ? "room" : "contact", data->id);
525 empathy_join_muc (data->account, data->id,
526 TP_USER_ACTION_TIME_NOT_USER_ACTION);
528 empathy_chat_with_contact_id (data->account, data->id,
529 TP_USER_ACTION_TIME_NOT_USER_ACTION);
531 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
532 g_queue_get_length (priv->closed_queue));
534 chat_data_free (data);
538 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
540 EmpathyChatManagerPriv *priv = GET_PRIV (self);
542 return g_queue_get_length (priv->closed_queue);
546 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
547 DBusGMethodInvocation *context)
549 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager);
551 emp_svc_chat_manager_return_from_undo_closed_chat (context);
555 svc_iface_init (gpointer g_iface,
558 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
560 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
561 klass, empathy_chat_manager_dbus_##x)
562 IMPLEMENT(undo_closed_chat);
567 empathy_chat_manager_call_undo_closed_chat (void)
569 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
572 if (dbus_daemon == NULL)
575 proxy = g_object_new (TP_TYPE_PROXY,
576 "dbus-daemon", dbus_daemon,
577 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
578 "bus-name", EMPATHY_CHAT_BUS_NAME,
579 "object-path", "/org/gnome/Empathy/ChatManager",
582 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
584 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, NULL, NULL, NULL, NULL);
586 g_object_unref (proxy);
587 g_object_unref (dbus_daemon);