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>
22 #include <libempathy/empathy-chatroom-manager.h>
23 #include <libempathy/empathy-request-util.h>
24 #include <libempathy/empathy-utils.h>
26 #include "empathy-chat-window.h"
28 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
29 #include <libempathy/empathy-debug.h>
31 #include "empathy-chat-manager.h"
35 DISPLAYED_CHATS_CHANGED,
39 static guint signals[LAST_SIGNAL];
41 G_DEFINE_TYPE(EmpathyChatManager, empathy_chat_manager, G_TYPE_OBJECT)
43 /* private structure */
44 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
46 struct _EmpathyChatManagerPriv
48 EmpathyChatroomManager *chatroom_mgr;
49 /* Queue of (ChatData *) representing the closed chats */
52 guint num_displayed_chat;
54 /* account path -> (GHashTable<(owned gchar *) contact ID
55 * -> (owned gchar *) non-NULL message>)
59 TpBaseClient *handler;
63 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
64 EmpathyChatManagerPriv))
66 static EmpathyChatManager *chat_manager_singleton = NULL;
76 chat_data_new (EmpathyChat *chat)
78 ChatData *data = NULL;
80 data = g_slice_new0 (ChatData);
82 data->account = g_object_ref (empathy_chat_get_account (chat));
83 data->id = g_strdup (empathy_chat_get_id (chat));
84 data->room = empathy_chat_is_room (chat);
90 chat_data_free (ChatData *data)
92 if (data->account != NULL)
94 g_object_unref (data->account);
104 g_slice_free (ChatData, data);
108 chat_destroyed_cb (gpointer data,
111 EmpathyChatManager *self = data;
112 EmpathyChatManagerPriv *priv = GET_PRIV (self);
114 priv->num_displayed_chat--;
116 DEBUG ("Chat destroyed; we are now displaying %u chats",
117 priv->num_displayed_chat);
119 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
120 priv->num_displayed_chat);
124 process_tp_chat (EmpathyChatManager *self,
125 EmpathyTpChat *tp_chat,
127 gint64 user_action_time)
129 EmpathyChatManagerPriv *priv = GET_PRIV (self);
130 EmpathyChat *chat = NULL;
133 id = empathy_tp_chat_get_id (tp_chat);
134 if (!tp_str_empty (id))
136 chat = empathy_chat_window_find_chat (account, id,
137 empathy_tp_chat_is_sms_channel (tp_chat));
142 empathy_chat_set_tp_chat (chat, tp_chat);
146 GHashTable *chats = NULL;
148 chat = empathy_chat_new (tp_chat);
149 /* empathy_chat_new returns a floating reference as EmpathyChat is
150 * a GtkWidget. This reference will be taken by a container
151 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
153 priv->num_displayed_chat++;
155 DEBUG ("Chat displayed; we are now displaying %u chat",
156 priv->num_displayed_chat);
158 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
159 priv->num_displayed_chat);
161 /* Set the saved message in the channel if we have one. */
162 chats = g_hash_table_lookup (priv->messages,
163 tp_proxy_get_object_path (account));
167 const gchar *msg = g_hash_table_lookup (chats, id);
170 empathy_chat_set_text (chat, msg);
173 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
175 empathy_chat_window_present_chat (chat, user_action_time);
177 if (empathy_tp_chat_is_invited (tp_chat, NULL))
179 /* We have been invited to the room. Add ourself as member as this
180 * channel has been approved. */
181 empathy_tp_chat_join (tp_chat);
184 g_object_unref (tp_chat);
189 EmpathyChatManager *self;
190 EmpathyTpChat *tp_chat;
192 gint64 user_action_time;
196 static chat_ready_ctx *
197 chat_ready_ctx_new (EmpathyChatManager *self,
198 EmpathyTpChat *tp_chat,
200 gint64 user_action_time)
202 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
204 ctx->self = g_object_ref (self);
205 ctx->tp_chat = g_object_ref (tp_chat);
206 ctx->account = g_object_ref (account);
207 ctx->user_action_time = user_action_time;
212 chat_ready_ctx_free (chat_ready_ctx *ctx)
214 g_object_unref (ctx->self);
215 g_object_unref (ctx->tp_chat);
216 g_object_unref (ctx->account);
218 if (ctx->sig_id != 0)
219 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
221 g_slice_free (chat_ready_ctx, ctx);
225 tp_chat_ready_cb (GObject *object,
229 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
230 chat_ready_ctx *ctx = user_data;
232 if (!empathy_tp_chat_is_ready (tp_chat))
235 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
237 chat_ready_ctx_free (ctx);
241 handle_channels (TpSimpleHandler *handler,
243 TpConnection *connection,
245 GList *requests_satisfied,
246 gint64 user_action_time,
247 TpHandleChannelsContext *context,
250 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
253 for (l = channels; l != NULL; l = g_list_next (l))
255 TpChannel *channel = l->data;
256 EmpathyTpChat *tp_chat;
258 if (tp_proxy_get_invalidated (channel) != NULL)
261 if (!TP_IS_TEXT_CHANNEL (channel))
263 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
264 tp_proxy_get_object_path (channel));
268 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
270 tp_chat = empathy_tp_chat_new (account, channel);
272 if (empathy_tp_chat_is_ready (tp_chat))
274 process_tp_chat (self, tp_chat, account, user_action_time);
278 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
281 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
282 G_CALLBACK (tp_chat_ready_cb), ctx);
286 tp_handle_channels_context_accept (context);
290 empathy_chat_manager_init (EmpathyChatManager *self)
292 EmpathyChatManagerPriv *priv = GET_PRIV (self);
294 GError *error = NULL;
296 priv->closed_queue = g_queue_new ();
297 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
298 g_free, (GDestroyNotify) g_hash_table_unref);
300 dbus = tp_dbus_daemon_dup (&error);
303 g_critical ("Failed to get D-Bus daemon: %s", error->message);
304 g_error_free (error);
308 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
310 /* Text channels handler */
311 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
312 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
314 /* EmpathyTpChat relies on these features being prepared */
315 tp_base_client_add_connection_features_varargs (priv->handler,
316 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
317 tp_base_client_add_channel_features_varargs (priv->handler,
318 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
320 g_object_unref (dbus);
322 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
323 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
324 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
327 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
328 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
329 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
332 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
333 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
334 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
337 if (!tp_base_client_register (priv->handler, &error))
339 g_critical ("Failed to register text handler: %s", error->message);
340 g_error_free (error);
345 empathy_chat_manager_finalize (GObject *object)
347 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
348 EmpathyChatManagerPriv *priv = GET_PRIV (self);
350 if (priv->closed_queue != NULL)
352 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
353 g_queue_free (priv->closed_queue);
354 priv->closed_queue = NULL;
357 tp_clear_pointer (&priv->messages, g_hash_table_unref);
359 tp_clear_object (&priv->handler);
360 tp_clear_object (&priv->chatroom_mgr);
362 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
366 empathy_chat_manager_constructor (GType type,
367 guint n_construct_params,
368 GObjectConstructParam *construct_params)
372 if (!chat_manager_singleton)
374 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
375 (type, n_construct_params, construct_params);
377 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
378 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
382 retval = g_object_ref (chat_manager_singleton);
389 empathy_chat_manager_class_init (
390 EmpathyChatManagerClass *empathy_chat_manager_class)
392 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
394 object_class->finalize = empathy_chat_manager_finalize;
395 object_class->constructor = empathy_chat_manager_constructor;
397 signals[CLOSED_CHATS_CHANGED] =
398 g_signal_new ("closed-chats-changed",
399 G_TYPE_FROM_CLASS (object_class),
403 g_cclosure_marshal_VOID__UINT,
405 1, G_TYPE_UINT, NULL);
407 signals[DISPLAYED_CHATS_CHANGED] =
408 g_signal_new ("displayed-chats-changed",
409 G_TYPE_FROM_CLASS (object_class),
413 g_cclosure_marshal_VOID__UINT,
415 1, G_TYPE_UINT, NULL);
417 g_type_class_add_private (empathy_chat_manager_class,
418 sizeof (EmpathyChatManagerPriv));
422 empathy_chat_manager_dup_singleton (void)
424 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
428 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
431 EmpathyChatManagerPriv *priv = GET_PRIV (self);
436 data = chat_data_new (chat);
438 DEBUG ("Adding %s to closed queue: %s",
439 data->room ? "room" : "contact", data->id);
441 g_queue_push_tail (priv->closed_queue, data);
443 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
444 g_queue_get_length (priv->closed_queue));
446 /* If there was a message saved from last time it was closed
447 * (perhaps by accident?) save it to our hash table so it can be
448 * used again when the same chat pops up. Hot. */
449 message = empathy_chat_dup_text (chat);
451 chats = g_hash_table_lookup (priv->messages,
452 tp_proxy_get_object_path (data->account));
454 /* Don't create a new hash table if we don't already have one and we
455 * don't actually have a message to save. */
456 if (chats == NULL && tp_str_empty (message))
461 else if (chats == NULL && !tp_str_empty (message))
463 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
466 g_hash_table_insert (priv->messages,
467 g_strdup (tp_proxy_get_object_path (data->account)),
471 if (tp_str_empty (message))
473 g_hash_table_remove (chats, data->id);
479 /* takes ownership of message */
480 g_hash_table_insert (chats, g_strdup (data->id), message);
485 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
487 EmpathyChatManagerPriv *priv = GET_PRIV (self);
490 data = g_queue_pop_tail (priv->closed_queue);
495 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
496 data->room ? "room" : "contact", data->id);
499 empathy_join_muc (data->account, data->id,
500 TP_USER_ACTION_TIME_NOT_USER_ACTION);
502 empathy_chat_with_contact_id (data->account, data->id,
503 TP_USER_ACTION_TIME_NOT_USER_ACTION);
505 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
506 g_queue_get_length (priv->closed_queue));
508 chat_data_free (data);
512 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
514 EmpathyChatManagerPriv *priv = GET_PRIV (self);
516 return g_queue_get_length (priv->closed_queue);