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 TpBaseClient *handler;
67 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
68 EmpathyChatManagerPriv))
70 static EmpathyChatManager *chat_manager_singleton = NULL;
80 chat_data_new (EmpathyChat *chat)
82 ChatData *data = NULL;
84 data = g_slice_new0 (ChatData);
86 data->account = g_object_ref (empathy_chat_get_account (chat));
87 data->id = g_strdup (empathy_chat_get_id (chat));
88 data->room = empathy_chat_is_room (chat);
94 chat_data_free (ChatData *data)
96 if (data->account != NULL)
98 g_object_unref (data->account);
102 if (data->id != NULL)
108 g_slice_free (ChatData, data);
112 chat_destroyed_cb (gpointer data,
115 EmpathyChatManager *self = data;
116 EmpathyChatManagerPriv *priv = GET_PRIV (self);
118 priv->num_displayed_chat--;
120 DEBUG ("Chat destroyed; we are now displaying %u chats",
121 priv->num_displayed_chat);
123 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
124 priv->num_displayed_chat);
128 process_tp_chat (EmpathyChatManager *self,
129 EmpathyTpChat *tp_chat,
131 gint64 user_action_time)
133 EmpathyChatManagerPriv *priv = GET_PRIV (self);
134 EmpathyChat *chat = NULL;
137 id = empathy_tp_chat_get_id (tp_chat);
138 if (!tp_str_empty (id))
140 chat = empathy_chat_window_find_chat (account, id,
141 empathy_tp_chat_is_sms_channel (tp_chat));
146 empathy_chat_set_tp_chat (chat, tp_chat);
150 chat = empathy_chat_new (tp_chat);
151 /* empathy_chat_new returns a floating reference as EmpathyChat is
152 * a GtkWidget. This reference will be taken by a container
153 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
155 priv->num_displayed_chat++;
157 DEBUG ("Chat displayed; we are now displaying %u chat",
158 priv->num_displayed_chat);
160 g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
161 priv->num_displayed_chat);
163 g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
165 empathy_chat_window_present_chat (chat, user_action_time);
167 if (empathy_tp_chat_is_invited (tp_chat, NULL))
169 /* We have been invited to the room. Add ourself as member as this
170 * channel has been approved. */
171 empathy_tp_chat_join (tp_chat);
174 g_object_unref (tp_chat);
179 EmpathyChatManager *self;
180 EmpathyTpChat *tp_chat;
182 gint64 user_action_time;
186 static chat_ready_ctx *
187 chat_ready_ctx_new (EmpathyChatManager *self,
188 EmpathyTpChat *tp_chat,
190 gint64 user_action_time)
192 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
194 ctx->self = g_object_ref (self);
195 ctx->tp_chat = g_object_ref (tp_chat);
196 ctx->account = g_object_ref (account);
197 ctx->user_action_time = user_action_time;
202 chat_ready_ctx_free (chat_ready_ctx *ctx)
204 g_object_unref (ctx->self);
205 g_object_unref (ctx->tp_chat);
206 g_object_unref (ctx->account);
208 if (ctx->sig_id != 0)
209 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
211 g_slice_free (chat_ready_ctx, ctx);
215 tp_chat_ready_cb (GObject *object,
219 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
220 chat_ready_ctx *ctx = user_data;
222 if (!empathy_tp_chat_is_ready (tp_chat))
225 process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
227 chat_ready_ctx_free (ctx);
231 handle_channels (TpSimpleHandler *handler,
233 TpConnection *connection,
235 GList *requests_satisfied,
236 gint64 user_action_time,
237 TpHandleChannelsContext *context,
240 EmpathyChatManager *self = (EmpathyChatManager *) user_data;
243 for (l = channels; l != NULL; l = g_list_next (l))
245 TpChannel *channel = l->data;
246 EmpathyTpChat *tp_chat;
248 if (tp_proxy_get_invalidated (channel) != NULL)
251 if (!TP_IS_TEXT_CHANNEL (channel))
253 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
254 tp_proxy_get_object_path (channel));
258 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
260 tp_chat = empathy_tp_chat_new (account, channel);
262 if (empathy_tp_chat_is_ready (tp_chat))
264 process_tp_chat (self, tp_chat, account, user_action_time);
268 chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
271 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
272 G_CALLBACK (tp_chat_ready_cb), ctx);
276 tp_handle_channels_context_accept (context);
280 empathy_chat_manager_init (EmpathyChatManager *self)
282 EmpathyChatManagerPriv *priv = GET_PRIV (self);
284 GError *error = NULL;
286 priv->closed_queue = g_queue_new ();
288 dbus = tp_dbus_daemon_dup (&error);
291 g_critical ("Failed to get D-Bus daemon: %s", error->message);
292 g_error_free (error);
296 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
298 /* Text channels handler */
299 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
300 EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
302 /* EmpathyTpChat relies on these features being prepared */
303 tp_base_client_add_connection_features_varargs (priv->handler,
304 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
305 tp_base_client_add_channel_features_varargs (priv->handler,
306 TP_CHANNEL_FEATURE_CHAT_STATES, 0);
308 g_object_unref (dbus);
310 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
311 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
312 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
315 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
316 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
317 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
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_NONE,
325 if (!tp_base_client_register (priv->handler, &error))
327 g_critical ("Failed to register text handler: %s", error->message);
328 g_error_free (error);
333 empathy_chat_manager_finalize (GObject *object)
335 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
336 EmpathyChatManagerPriv *priv = GET_PRIV (self);
338 if (priv->closed_queue != NULL)
340 g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
341 g_queue_free (priv->closed_queue);
342 priv->closed_queue = NULL;
345 tp_clear_object (&priv->handler);
346 tp_clear_object (&priv->chatroom_mgr);
348 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
352 empathy_chat_manager_constructor (GType type,
353 guint n_construct_params,
354 GObjectConstructParam *construct_params)
358 if (!chat_manager_singleton)
360 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
361 (type, n_construct_params, construct_params);
363 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
364 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
368 retval = g_object_ref (chat_manager_singleton);
375 empathy_chat_manager_constructed (GObject *obj)
377 TpDBusDaemon *dbus_daemon;
379 dbus_daemon = tp_dbus_daemon_dup (NULL);
381 if (dbus_daemon != NULL)
383 tp_dbus_daemon_register_object (dbus_daemon,
384 "/org/gnome/Empathy/ChatManager", obj);
386 g_object_unref (dbus_daemon);
391 empathy_chat_manager_class_init (
392 EmpathyChatManagerClass *empathy_chat_manager_class)
394 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
396 object_class->finalize = empathy_chat_manager_finalize;
397 object_class->constructor = empathy_chat_manager_constructor;
398 object_class->constructed = empathy_chat_manager_constructed;
400 signals[CLOSED_CHATS_CHANGED] =
401 g_signal_new ("closed-chats-changed",
402 G_TYPE_FROM_CLASS (object_class),
406 g_cclosure_marshal_VOID__UINT,
408 1, G_TYPE_UINT, NULL);
410 signals[DISPLAYED_CHATS_CHANGED] =
411 g_signal_new ("displayed-chats-changed",
412 G_TYPE_FROM_CLASS (object_class),
416 g_cclosure_marshal_VOID__UINT,
418 1, G_TYPE_UINT, NULL);
420 g_type_class_add_private (empathy_chat_manager_class,
421 sizeof (EmpathyChatManagerPriv));
425 empathy_chat_manager_dup_singleton (void)
427 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
431 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
434 EmpathyChatManagerPriv *priv = GET_PRIV (self);
437 data = chat_data_new (chat);
439 DEBUG ("Adding %s to closed queue: %s",
440 data->room ? "room" : "contact", data->id);
442 g_queue_push_tail (priv->closed_queue, data);
444 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
445 g_queue_get_length (priv->closed_queue));
449 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
451 EmpathyChatManagerPriv *priv = GET_PRIV (self);
454 data = g_queue_pop_tail (priv->closed_queue);
459 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
460 data->room ? "room" : "contact", data->id);
463 empathy_join_muc (data->account, data->id,
464 TP_USER_ACTION_TIME_NOT_USER_ACTION);
466 empathy_chat_with_contact_id (data->account, data->id,
467 TP_USER_ACTION_TIME_NOT_USER_ACTION);
469 g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
470 g_queue_get_length (priv->closed_queue));
472 chat_data_free (data);
476 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
478 EmpathyChatManagerPriv *priv = GET_PRIV (self);
480 return g_queue_get_length (priv->closed_queue);
484 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
485 DBusGMethodInvocation *context)
487 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager);
489 emp_svc_chat_manager_return_from_undo_closed_chat (context);
493 svc_iface_init (gpointer g_iface,
496 EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
498 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
499 klass, empathy_chat_manager_dbus_##x)
500 IMPLEMENT(undo_closed_chat);
505 empathy_chat_manager_call_undo_closed_chat (void)
507 TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
510 if (dbus_daemon == NULL)
513 proxy = g_object_new (TP_TYPE_PROXY,
514 "dbus-daemon", dbus_daemon,
515 "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
516 "bus-name", "org.freedesktop.Telepathy.Client.Empathy.Chat",
517 "object-path", "/org/gnome/Empathy/ChatManager",
520 tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
522 emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, NULL, NULL, NULL, NULL);
524 g_object_unref (proxy);
525 g_object_unref (dbus_daemon);