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-dispatcher.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"
38 static guint signals[LAST_SIGNAL];
40 G_DEFINE_TYPE(EmpathyChatManager, empathy_chat_manager, G_TYPE_OBJECT)
42 /* private structure */
43 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
45 struct _EmpathyChatManagerPriv
47 /* Queue of (ChatData *) representing the closed chats */
50 TpBaseClient *handler;
54 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
55 EmpathyChatManagerPriv))
57 static EmpathyChatManager *chat_manager_singleton = NULL;
67 chat_data_new (EmpathyChat *chat)
69 ChatData *data = NULL;
71 data = g_slice_new0 (ChatData);
73 data->account = g_object_ref (empathy_chat_get_account (chat));
74 data->id = g_strdup (empathy_chat_get_id (chat));
75 data->room = empathy_chat_is_room (chat);
81 chat_data_free (ChatData *data)
83 if (data->account != NULL)
85 g_object_unref (data->account);
95 g_slice_free (ChatData, data);
99 tell_chatroom_manager_if_needed (TpAccount *account,
104 tp_channel_get_handle (empathy_tp_chat_get_channel (chat), &type);
106 if (type == TP_HANDLE_TYPE_ROOM)
108 EmpathyChatroomManager *chatroom_mgr;
110 chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
111 empathy_chatroom_manager_chat_handled (chatroom_mgr, chat, account);
112 g_object_unref (chatroom_mgr);
117 process_tp_chat (EmpathyTpChat *tp_chat,
119 gint64 user_action_time)
121 EmpathyChat *chat = NULL;
124 tell_chatroom_manager_if_needed (account, tp_chat);
126 id = empathy_tp_chat_get_id (tp_chat);
127 if (!tp_str_empty (id))
129 chat = empathy_chat_window_find_chat (account, id);
134 empathy_chat_set_tp_chat (chat, tp_chat);
138 chat = empathy_chat_new (tp_chat);
139 /* empathy_chat_new returns a floating reference as EmpathyChat is
140 * a GtkWidget. This reference will be taken by a container
141 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
143 empathy_chat_window_present_chat (chat, user_action_time);
145 if (empathy_tp_chat_is_invited (tp_chat, NULL))
147 /* We have been invited to the room. Add ourself as member as this
148 * channel has been approved. */
149 empathy_tp_chat_join (tp_chat);
152 g_object_unref (tp_chat);
157 EmpathyTpChat *tp_chat;
159 gint64 user_action_time;
163 static chat_ready_ctx *
164 chat_ready_ctx_new (EmpathyTpChat *tp_chat,
166 gint64 user_action_time)
168 chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
170 ctx->tp_chat = g_object_ref (tp_chat);
171 ctx->account = g_object_ref (account);
172 ctx->user_action_time = user_action_time;
177 chat_ready_ctx_free (chat_ready_ctx *ctx)
179 g_object_unref (ctx->tp_chat);
180 g_object_unref (ctx->account);
182 if (ctx->sig_id != 0)
183 g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
185 g_slice_free (chat_ready_ctx, ctx);
189 tp_chat_ready_cb (GObject *object,
193 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
194 chat_ready_ctx *ctx = user_data;
196 if (!empathy_tp_chat_is_ready (tp_chat))
199 process_tp_chat (tp_chat, ctx->account, ctx->user_action_time);
201 chat_ready_ctx_free (ctx);
205 handle_channels (TpSimpleHandler *handler,
207 TpConnection *connection,
209 GList *requests_satisfied,
210 gint64 user_action_time,
211 TpHandleChannelsContext *context,
216 for (l = channels; l != NULL; l = g_list_next (l))
218 TpChannel *channel = l->data;
219 EmpathyTpChat *tp_chat;
221 tp_chat = empathy_tp_chat_new (account, channel);
223 if (empathy_tp_chat_is_ready (tp_chat))
225 process_tp_chat (tp_chat, account, user_action_time);
229 chat_ready_ctx *ctx = chat_ready_ctx_new (tp_chat, account,
232 ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
233 G_CALLBACK (tp_chat_ready_cb), ctx);
237 tp_handle_channels_context_accept (context);
241 empathy_chat_manager_init (EmpathyChatManager *self)
243 EmpathyChatManagerPriv *priv = GET_PRIV (self);
245 GError *error = NULL;
247 priv->queue = g_queue_new ();
249 dbus = tp_dbus_daemon_dup (&error);
252 g_critical ("Failed to get D-Bus daemon: %s", error->message);
253 g_error_free (error);
257 /* Text channels handler */
258 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE, "Empathy", FALSE,
259 handle_channels, self, NULL);
261 g_object_unref (dbus);
263 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
264 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
265 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
268 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
269 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
270 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
273 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
274 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
275 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
278 if (!tp_base_client_register (priv->handler, &error))
280 g_critical ("Failed to register text handler: %s", error->message);
281 g_error_free (error);
286 empathy_chat_manager_finalize (GObject *object)
288 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
289 EmpathyChatManagerPriv *priv = GET_PRIV (self);
291 if (priv->queue != NULL)
293 g_queue_foreach (priv->queue, (GFunc) chat_data_free, NULL);
294 g_queue_free (priv->queue);
298 tp_clear_object (&priv->handler);
300 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
304 empathy_chat_manager_constructor (GType type,
305 guint n_construct_params,
306 GObjectConstructParam *construct_params)
310 if (!chat_manager_singleton)
312 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
313 (type, n_construct_params, construct_params);
315 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
316 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
320 retval = g_object_ref (chat_manager_singleton);
327 empathy_chat_manager_class_init (
328 EmpathyChatManagerClass *empathy_chat_manager_class)
330 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
332 object_class->finalize = empathy_chat_manager_finalize;
333 object_class->constructor = empathy_chat_manager_constructor;
335 signals[CHATS_CHANGED] =
336 g_signal_new ("chats-changed",
337 G_TYPE_FROM_CLASS (object_class),
341 g_cclosure_marshal_VOID__UINT,
343 1, G_TYPE_UINT, NULL);
345 g_type_class_add_private (empathy_chat_manager_class,
346 sizeof (EmpathyChatManagerPriv));
350 empathy_chat_manager_dup_singleton (void)
352 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
356 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
359 EmpathyChatManagerPriv *priv = GET_PRIV (self);
362 data = chat_data_new (chat);
364 DEBUG ("Adding %s to queue: %s", data->room ? "room" : "contact", data->id);
366 g_queue_push_tail (priv->queue, data);
368 g_signal_emit (self, signals[CHATS_CHANGED], 0,
369 g_queue_get_length (priv->queue));
373 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
375 EmpathyChatManagerPriv *priv = GET_PRIV (self);
378 data = g_queue_pop_tail (priv->queue);
383 DEBUG ("Removing %s from queue and starting a chat with: %s",
384 data->room ? "room" : "contact", data->id);
387 empathy_dispatcher_join_muc (data->account, data->id,
388 TP_USER_ACTION_TIME_NOT_USER_ACTION);
390 empathy_dispatcher_chat_with_contact_id (data->account, data->id,
391 TP_USER_ACTION_TIME_NOT_USER_ACTION);
393 g_signal_emit (self, signals[CHATS_CHANGED], 0,
394 g_queue_get_length (priv->queue));
396 chat_data_free (data);
400 empathy_chat_manager_get_num_chats (EmpathyChatManager *self)
402 EmpathyChatManagerPriv *priv = GET_PRIV (self);
404 return g_queue_get_length (priv->queue);