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<contact -> non-NULL message> */
57 TpBaseClient *handler;
61 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
62 EmpathyChatManagerPriv))
64 static EmpathyChatManager *chat_manager_singleton = NULL;
74 chat_data_new (EmpathyChat *chat)
76 ChatData *data = NULL;
78 data = g_slice_new0 (ChatData);
80 data->account = g_object_ref (empathy_chat_get_account (chat));
81 data->id = g_strdup (empathy_chat_get_id (chat));
82 data->room = empathy_chat_is_room (chat);
88 chat_data_free (ChatData *data)
90 if (data->account != NULL)
92 g_object_unref (data->account);
102 g_slice_free (ChatData, data);
106 chat_destroyed_cb (gpointer data,
109 EmpathyChatManager *self = data;
110 EmpathyChatManagerPriv *priv = GET_PRIV (self);
112 priv->num_displayed_chat--;
114 DEBUG ("Chat destroyed; we are now displaying %u chats",
115 priv->num_displayed_chat);
117 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
118 priv->num_displayed_chat);
122 process_tp_chat (EmpathyChatManager *self,
123 EmpathyTpChat *tp_chat,
125 gint64 user_action_time)
127 EmpathyChatManagerPriv *priv = GET_PRIV (self);
128 EmpathyChat *chat = NULL;
131 id = empathy_tp_chat_get_id (tp_chat);
132 if (!tp_str_empty (id))
134 chat = empathy_chat_window_find_chat (account, id,
135 empathy_tp_chat_is_sms_channel (tp_chat));
140 empathy_chat_set_tp_chat (chat, tp_chat);
144 GHashTable *chats = NULL;
146 chat = empathy_chat_new (tp_chat);
147 /* empathy_chat_new returns a floating reference as EmpathyChat is
148 * a GtkWidget. This reference will be taken by a container
149 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
151 priv->num_displayed_chat++;
153 DEBUG ("Chat displayed; we are now displaying %u chat",
154 priv->num_displayed_chat);
156 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
157 priv->num_displayed_chat);
159 /* Set the saved message in the channel if we have one. */
160 chats = g_hash_table_lookup (priv->messages,
161 tp_proxy_get_object_path (account));
165 const gchar *msg = g_hash_table_lookup (chats, id);
168 empathy_chat_set_text (chat, msg);
171 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
173 empathy_chat_window_present_chat (chat, user_action_time);
175 if (empathy_tp_chat_is_invited (tp_chat, NULL))
177 /* We have been invited to the room. Add ourself as member as this
178 * channel has been approved. */
179 empathy_tp_chat_join (tp_chat);
182 g_object_unref (tp_chat);
187 EmpathyChatManager *self;
188 EmpathyTpChat *tp_chat;
190 gint64 user_action_time;
194 static chat_ready_ctx *
195 chat_ready_ctx_new (EmpathyChatManager *self,
196 EmpathyTpChat *tp_chat,
198 gint64 user_action_time)
200 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
202 ctx->self = g_object_ref (self);
203 ctx->tp_chat = g_object_ref (tp_chat);
204 ctx->account = g_object_ref (account);
205 ctx->user_action_time = user_action_time;
210 chat_ready_ctx_free (chat_ready_ctx *ctx)
212 g_object_unref (ctx->self);
213 g_object_unref (ctx->tp_chat);
214 g_object_unref (ctx->account);
216 if (ctx->sig_id != 0)
217 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
219 g_slice_free (chat_ready_ctx, ctx);
223 tp_chat_ready_cb (GObject *object,
227 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
228 chat_ready_ctx *ctx = user_data;
230 if (!empathy_tp_chat_is_ready (tp_chat))
233 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
235 chat_ready_ctx_free (ctx);
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 TpChannel *channel = l->data;
254 EmpathyTpChat *tp_chat;
256 if (tp_proxy_get_invalidated (channel) != NULL)
259 if (!TP_IS_TEXT_CHANNEL (channel))
261 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
262 tp_proxy_get_object_path (channel));
266 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
268 tp_chat = empathy_tp_chat_new (account, channel);
270 if (empathy_tp_chat_is_ready (tp_chat))
272 process_tp_chat (self, tp_chat, account, user_action_time);
276 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
279 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
280 G_CALLBACK (tp_chat_ready_cb), ctx);
284 tp_handle_channels_context_accept (context);
288 empathy_chat_manager_init (EmpathyChatManager *self)
290 EmpathyChatManagerPriv *priv = GET_PRIV (self);
292 GError *error = NULL;
294 priv->closed_queue = g_queue_new ();
295 priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
296 g_free, (GDestroyNotify) g_hash_table_unref);
298 dbus = tp_dbus_daemon_dup (&error);
301 g_critical ("Failed to get D-Bus daemon: %s", error->message);
302 g_error_free (error);
306 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
308 /* Text channels handler */
309 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
310 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
312 /* EmpathyTpChat relies on these features being prepared */
313 tp_base_client_add_connection_features_varargs (priv->handler,
314 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
315 tp_base_client_add_channel_features_varargs (priv->handler,
316 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
318 g_object_unref (dbus);
320 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
321 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
322 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
325 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
326 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
327 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
330 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
331 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
332 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
335 if (!tp_base_client_register (priv->handler, &error))
337 g_critical ("Failed to register text handler: %s", error->message);
338 g_error_free (error);
343 empathy_chat_manager_finalize (GObject *object)
345 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
346 EmpathyChatManagerPriv *priv = GET_PRIV (self);
348 if (priv->closed_queue != NULL)
350 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
351 g_queue_free (priv->closed_queue);
352 priv->closed_queue = NULL;
355 tp_clear_pointer (&priv->messages, g_hash_table_unref);
357 tp_clear_object (&priv->handler);
358 tp_clear_object (&priv->chatroom_mgr);
360 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
364 empathy_chat_manager_constructor (GType type,
365 guint n_construct_params,
366 GObjectConstructParam *construct_params)
370 if (!chat_manager_singleton)
372 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
373 (type, n_construct_params, construct_params);
375 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
376 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
380 retval = g_object_ref (chat_manager_singleton);
387 empathy_chat_manager_class_init (
388 EmpathyChatManagerClass *empathy_chat_manager_class)
390 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
392 object_class->finalize = empathy_chat_manager_finalize;
393 object_class->constructor = empathy_chat_manager_constructor;
395 signals[CLOSED_CHATS_CHANGED] =
396 g_signal_new ("closed-chats-changed",
397 G_TYPE_FROM_CLASS (object_class),
401 g_cclosure_marshal_VOID__UINT,
403 1, G_TYPE_UINT, NULL);
405 signals[DISPLAYED_CHATS_CHANGED] =
406 g_signal_new ("displayed-chats-changed",
407 G_TYPE_FROM_CLASS (object_class),
411 g_cclosure_marshal_VOID__UINT,
413 1, G_TYPE_UINT, NULL);
415 g_type_class_add_private (empathy_chat_manager_class,
416 sizeof (EmpathyChatManagerPriv));
420 empathy_chat_manager_dup_singleton (void)
422 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
426 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
429 EmpathyChatManagerPriv *priv = GET_PRIV (self);
434 data = chat_data_new (chat);
436 DEBUG ("Adding %s to closed queue: %s",
437 data->room ? "room" : "contact", data->id);
439 g_queue_push_tail (priv->closed_queue, data);
441 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
442 g_queue_get_length (priv->closed_queue));
444 /* If there was a message saved from last time it was closed
445 * (perhaps by accident?) save it to our hash table so it can be
446 * used again when the same chat pops up. Hot. */
447 message = empathy_chat_dup_text (chat);
449 chats = g_hash_table_lookup (priv->messages,
450 tp_proxy_get_object_path (data->account));
452 /* Don't create a new hash table if we don't already have one and we
453 * don't actually have a message to save. */
454 if (chats == NULL && tp_str_empty (message))
459 else if (chats == null && !tp_str_empty (message))
461 chats = g_hash_table_new_full (g_str_hash, g_str_equal,
464 g_hash_table_insert (priv->messages,
465 g_strdup (tp_proxy_get_object_path (data->account)),
469 if (tp_str_empty (message))
471 g_hash_table_remove (chats, data->id);
477 /* takes ownership of message */
478 g_hash_table_insert (chats, g_strdup (data->id), message);
483 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
485 EmpathyChatManagerPriv *priv = GET_PRIV (self);
488 data = g_queue_pop_tail (priv->closed_queue);
493 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
494 data->room ? "room" : "contact", data->id);
497 empathy_join_muc (data->account, data->id,
498 TP_USER_ACTION_TIME_NOT_USER_ACTION);
500 empathy_chat_with_contact_id (data->account, data->id,
501 TP_USER_ACTION_TIME_NOT_USER_ACTION);
503 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
504 g_queue_get_length (priv->closed_queue));
506 chat_data_free (data);
510 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
512 EmpathyChatManagerPriv *priv = GET_PRIV (self);
514 return g_queue_get_length (priv->closed_queue);