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 TpBaseClient *handler;
58 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
59 EmpathyChatManagerPriv))
61 static EmpathyChatManager *chat_manager_singleton = NULL;
71 chat_data_new (EmpathyChat *chat)
73 ChatData *data = NULL;
75 data = g_slice_new0 (ChatData);
77 data->account = g_object_ref (empathy_chat_get_account (chat));
78 data->id = g_strdup (empathy_chat_get_id (chat));
79 data->room = empathy_chat_is_room (chat);
85 chat_data_free (ChatData *data)
87 if (data->account != NULL)
89 g_object_unref (data->account);
99 g_slice_free (ChatData, data);
103 chat_destroyed_cb (gpointer data,
106 EmpathyChatManager *self = data;
107 EmpathyChatManagerPriv *priv = GET_PRIV (self);
109 priv->num_displayed_chat--;
111 DEBUG ("Chat destroyed; we are now displaying %u chats",
112 priv->num_displayed_chat);
114 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
115 priv->num_displayed_chat);
119 process_tp_chat (EmpathyChatManager *self,
120 EmpathyTpChat *tp_chat,
122 gint64 user_action_time)
124 EmpathyChatManagerPriv *priv = GET_PRIV (self);
125 EmpathyChat *chat = NULL;
128 id = empathy_tp_chat_get_id (tp_chat);
129 if (!tp_str_empty (id))
131 chat = empathy_chat_window_find_chat (account, id,
132 empathy_tp_chat_is_sms_channel (tp_chat));
137 empathy_chat_set_tp_chat (chat, tp_chat);
141 chat = empathy_chat_new (tp_chat);
142 /* empathy_chat_new returns a floating reference as EmpathyChat is
143 * a GtkWidget. This reference will be taken by a container
144 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
146 priv->num_displayed_chat++;
148 DEBUG ("Chat displayed; we are now displaying %u chat",
149 priv->num_displayed_chat);
151 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
152 priv->num_displayed_chat);
154 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
156 empathy_chat_window_present_chat (chat, user_action_time);
158 if (empathy_tp_chat_is_invited (tp_chat, NULL))
160 /* We have been invited to the room. Add ourself as member as this
161 * channel has been approved. */
162 empathy_tp_chat_join (tp_chat);
165 g_object_unref (tp_chat);
170 EmpathyChatManager *self;
171 EmpathyTpChat *tp_chat;
173 gint64 user_action_time;
177 static chat_ready_ctx *
178 chat_ready_ctx_new (EmpathyChatManager *self,
179 EmpathyTpChat *tp_chat,
181 gint64 user_action_time)
183 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
185 ctx->self = g_object_ref (self);
186 ctx->tp_chat = g_object_ref (tp_chat);
187 ctx->account = g_object_ref (account);
188 ctx->user_action_time = user_action_time;
193 chat_ready_ctx_free (chat_ready_ctx *ctx)
195 g_object_unref (ctx->self);
196 g_object_unref (ctx->tp_chat);
197 g_object_unref (ctx->account);
199 if (ctx->sig_id != 0)
200 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
202 g_slice_free (chat_ready_ctx, ctx);
206 tp_chat_ready_cb (GObject *object,
210 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
211 chat_ready_ctx *ctx = user_data;
213 if (!empathy_tp_chat_is_ready (tp_chat))
216 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
218 chat_ready_ctx_free (ctx);
222 handle_channels (TpSimpleHandler *handler,
224 TpConnection *connection,
226 GList *requests_satisfied,
227 gint64 user_action_time,
228 TpHandleChannelsContext *context,
231 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
234 for (l = channels; l != NULL; l = g_list_next (l))
236 TpChannel *channel = l->data;
237 EmpathyTpChat *tp_chat;
239 if (tp_proxy_get_invalidated (channel) != NULL)
242 if (!TP_IS_TEXT_CHANNEL (channel))
244 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
245 tp_proxy_get_object_path (channel));
249 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
251 tp_chat = empathy_tp_chat_new (account, channel);
253 if (empathy_tp_chat_is_ready (tp_chat))
255 process_tp_chat (self, tp_chat, account, user_action_time);
259 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
262 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
263 G_CALLBACK (tp_chat_ready_cb), ctx);
267 tp_handle_channels_context_accept (context);
271 empathy_chat_manager_init (EmpathyChatManager *self)
273 EmpathyChatManagerPriv *priv = GET_PRIV (self);
275 GError *error = NULL;
277 priv->closed_queue = g_queue_new ();
279 dbus = tp_dbus_daemon_dup (&error);
282 g_critical ("Failed to get D-Bus daemon: %s", error->message);
283 g_error_free (error);
287 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
289 /* Text channels handler */
290 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
291 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
293 /* EmpathyTpChat relies on these features being prepared */
294 tp_base_client_add_connection_features_varargs (priv->handler,
295 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
296 tp_base_client_add_channel_features_varargs (priv->handler,
297 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
299 g_object_unref (dbus);
301 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
302 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
303 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
306 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
307 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
308 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
311 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
312 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
313 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
316 if (!tp_base_client_register (priv->handler, &error))
318 g_critical ("Failed to register text handler: %s", error->message);
319 g_error_free (error);
324 empathy_chat_manager_finalize (GObject *object)
326 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
327 EmpathyChatManagerPriv *priv = GET_PRIV (self);
329 if (priv->closed_queue != NULL)
331 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
332 g_queue_free (priv->closed_queue);
333 priv->closed_queue = NULL;
336 tp_clear_object (&priv->handler);
337 tp_clear_object (&priv->chatroom_mgr);
339 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
343 empathy_chat_manager_constructor (GType type,
344 guint n_construct_params,
345 GObjectConstructParam *construct_params)
349 if (!chat_manager_singleton)
351 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
352 (type, n_construct_params, construct_params);
354 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
355 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
359 retval = g_object_ref (chat_manager_singleton);
366 empathy_chat_manager_class_init (
367 EmpathyChatManagerClass *empathy_chat_manager_class)
369 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
371 object_class->finalize = empathy_chat_manager_finalize;
372 object_class->constructor = empathy_chat_manager_constructor;
374 signals[CLOSED_CHATS_CHANGED] =
375 g_signal_new ("closed-chats-changed",
376 G_TYPE_FROM_CLASS (object_class),
380 g_cclosure_marshal_VOID__UINT,
382 1, G_TYPE_UINT, NULL);
384 signals[DISPLAYED_CHATS_CHANGED] =
385 g_signal_new ("displayed-chats-changed",
386 G_TYPE_FROM_CLASS (object_class),
390 g_cclosure_marshal_VOID__UINT,
392 1, G_TYPE_UINT, NULL);
394 g_type_class_add_private (empathy_chat_manager_class,
395 sizeof (EmpathyChatManagerPriv));
399 empathy_chat_manager_dup_singleton (void)
401 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
405 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
408 EmpathyChatManagerPriv *priv = GET_PRIV (self);
411 data = chat_data_new (chat);
413 DEBUG ("Adding %s to closed queue: %s",
414 data->room ? "room" : "contact", data->id);
416 g_queue_push_tail (priv->closed_queue, data);
418 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
419 g_queue_get_length (priv->closed_queue));
423 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
425 EmpathyChatManagerPriv *priv = GET_PRIV (self);
428 data = g_queue_pop_tail (priv->closed_queue);
433 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
434 data->room ? "room" : "contact", data->id);
437 empathy_join_muc (data->account, data->id,
438 TP_USER_ACTION_TIME_NOT_USER_ACTION);
440 empathy_chat_with_contact_id (data->account, data->id,
441 TP_USER_ACTION_TIME_NOT_USER_ACTION);
443 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
444 g_queue_get_length (priv->closed_queue));
446 chat_data_free (data);
450 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
452 EmpathyChatManagerPriv *priv = GET_PRIV (self);
454 return g_queue_get_length (priv->closed_queue);