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
21 #include "empathy-chat-manager.h"
23 #include <telepathy-glib/proxy-subclass.h>
24 #include <telepathy-glib/telepathy-glib-dbus.h>
26 #include "empathy-bus-names.h"
27 #include "empathy-chatroom-manager.h"
28 #include "empathy-chat-window.h"
29 #include "empathy-request-util.h"
30 #include "empathy-ui-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include "empathy-debug.h"
35 #define CHAT_MANAGER_PATH "/org/gnome/Empathy/ChatManager"
39 DISPLAYED_CHATS_CHANGED
,
43 static guint signals
[LAST_SIGNAL
];
45 static void chat_manager_iface_init (gpointer
, gpointer
);
47 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager
, empathy_chat_manager
,
48 EMPATHY_GEN_TYPE_CHAT_MANAGER_SKELETON
,
49 G_IMPLEMENT_INTERFACE (EMPATHY_GEN_TYPE_CHAT_MANAGER
,
50 chat_manager_iface_init
)
53 /* private structure */
54 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv
;
56 struct _EmpathyChatManagerPriv
58 EmpathyChatroomManager
*chatroom_mgr
;
59 /* Queue of (ChatData *) representing the closed chats */
62 guint num_displayed_chat
;
64 /* account path -> (GHashTable<(owned gchar *) contact ID
65 * -> (owned gchar *) non-NULL message>)
69 TpBaseClient
*handler
;
71 /* Cached to keep Folks in memory while empathy-chat is running; we don't
72 * want to reload it each time the last chat window is closed
74 EmpathyIndividualManager
*individual_mgr
;
78 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
79 EmpathyChatManagerPriv))
81 static EmpathyChatManager
*chat_manager_singleton
= NULL
;
92 chat_data_new (EmpathyChat
*chat
)
94 ChatData
*data
= NULL
;
96 data
= g_slice_new0 (ChatData
);
98 data
->account
= g_object_ref (empathy_chat_get_account (chat
));
99 data
->id
= g_strdup (empathy_chat_get_id (chat
));
100 data
->room
= empathy_chat_is_room (chat
);
101 data
->sms
= empathy_chat_is_sms_channel (chat
);
107 chat_data_free (ChatData
*data
)
109 if (data
->account
!= NULL
)
111 g_object_unref (data
->account
);
112 data
->account
= NULL
;
115 if (data
->id
!= NULL
)
121 g_slice_free (ChatData
, data
);
125 chat_destroyed_cb (gpointer data
,
128 EmpathyChatManager
*self
= data
;
129 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
131 priv
->num_displayed_chat
--;
133 DEBUG ("Chat destroyed; we are now displaying %u chats",
134 priv
->num_displayed_chat
);
136 g_signal_emit (self
, signals
[DISPLAYED_CHATS_CHANGED
], 0,
137 priv
->num_displayed_chat
);
141 join_cb (GObject
*source
,
142 GAsyncResult
*result
,
145 TpChannel
*channel
= TP_CHANNEL (source
);
146 GError
*error
= NULL
;
148 if (!tp_channel_join_finish (channel
, result
, &error
))
150 DEBUG ("Failed to join chat (%s): %s",
151 tp_channel_get_identifier (channel
), error
->message
);
152 g_error_free (error
);
157 individual_mgr_cb (EmpathyChatWindow
*window
,
159 EmpathyChatManager
*self
)
161 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
163 if (priv
->individual_mgr
!= NULL
)
166 priv
->individual_mgr
= empathy_chat_window_get_individual_manager (window
);
167 g_object_ref (priv
->individual_mgr
);
171 process_tp_chat (EmpathyChatManager
*self
,
172 EmpathyTpChat
*tp_chat
,
174 gint64 user_action_time
)
176 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
177 EmpathyChat
*chat
= NULL
;
179 EmpathyChatWindow
*window
;
181 id
= empathy_tp_chat_get_id (tp_chat
);
182 if (!tp_str_empty (id
))
184 chat
= empathy_chat_window_find_chat (account
, id
,
185 tp_text_channel_is_sms_channel ((TpTextChannel
*) tp_chat
));
190 empathy_chat_set_tp_chat (chat
, tp_chat
);
194 GHashTable
*chats
= NULL
;
196 chat
= empathy_chat_new (tp_chat
);
197 /* empathy_chat_new returns a floating reference as EmpathyChat is
198 * a GtkWidget. This reference will be taken by a container
199 * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
201 priv
->num_displayed_chat
++;
203 DEBUG ("Chat displayed; we are now displaying %u chat",
204 priv
->num_displayed_chat
);
206 g_signal_emit (self
, signals
[DISPLAYED_CHATS_CHANGED
], 0,
207 priv
->num_displayed_chat
);
209 /* Set the saved message in the channel if we have one. */
210 chats
= g_hash_table_lookup (priv
->messages
,
211 tp_proxy_get_object_path (account
));
215 const gchar
*msg
= g_hash_table_lookup (chats
, id
);
218 empathy_chat_set_text (chat
, msg
);
221 g_object_weak_ref ((GObject
*) chat
, chat_destroyed_cb
, self
);
224 window
= empathy_chat_window_present_chat (chat
, user_action_time
);
226 if (priv
->individual_mgr
== NULL
)
228 /* We want to cache it as soon it's created */
229 tp_g_signal_connect_object (window
, "notify::individual-manager",
230 G_CALLBACK (individual_mgr_cb
), self
, 0);
233 if (empathy_tp_chat_is_invited (tp_chat
, NULL
))
235 /* We have been invited to the room. Add ourself as member as this
236 * channel has been approved. */
237 tp_channel_join_async (TP_CHANNEL (tp_chat
), "", join_cb
, self
);
242 handle_channels (TpSimpleHandler
*handler
,
244 TpConnection
*connection
,
246 GList
*requests_satisfied
,
247 gint64 user_action_time
,
248 TpHandleChannelsContext
*context
,
251 EmpathyChatManager
*self
= (EmpathyChatManager
*) user_data
;
254 for (l
= channels
; l
!= NULL
; l
= g_list_next (l
))
256 EmpathyTpChat
*tp_chat
= l
->data
;
258 if (tp_proxy_get_invalidated (tp_chat
) != NULL
)
261 if (!EMPATHY_IS_TP_CHAT (tp_chat
))
263 DEBUG ("Channel %s doesn't implement Messages; can't handle it",
264 tp_proxy_get_object_path (tp_chat
));
268 DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat
));
270 process_tp_chat (self
, tp_chat
, account
, user_action_time
);
273 tp_handle_channels_context_accept (context
);
277 empathy_chat_manager_init (EmpathyChatManager
*self
)
279 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
280 TpAccountManager
*am
;
281 GError
*error
= NULL
;
283 priv
->closed_queue
= g_queue_new ();
284 priv
->messages
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
285 g_free
, (GDestroyNotify
) g_hash_table_unref
);
287 am
= tp_account_manager_dup ();
289 priv
->chatroom_mgr
= empathy_chatroom_manager_dup_singleton (NULL
);
291 /* Text channels handler */
292 priv
->handler
= tp_simple_handler_new_with_am (am
, FALSE
, FALSE
,
293 EMPATHY_CHAT_TP_BUS_NAME_SUFFIX
, FALSE
, handle_channels
, self
, NULL
);
297 tp_base_client_take_handler_filter (priv
->handler
, tp_asv_new (
298 TP_PROP_CHANNEL_CHANNEL_TYPE
, G_TYPE_STRING
, TP_IFACE_CHANNEL_TYPE_TEXT
,
299 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE
, G_TYPE_UINT
, TP_HANDLE_TYPE_CONTACT
,
302 tp_base_client_take_handler_filter (priv
->handler
, tp_asv_new (
303 TP_PROP_CHANNEL_CHANNEL_TYPE
, G_TYPE_STRING
, TP_IFACE_CHANNEL_TYPE_TEXT
,
304 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE
, G_TYPE_UINT
, TP_HANDLE_TYPE_ROOM
,
307 tp_base_client_take_handler_filter (priv
->handler
, tp_asv_new (
308 TP_PROP_CHANNEL_CHANNEL_TYPE
, G_TYPE_STRING
, TP_IFACE_CHANNEL_TYPE_TEXT
,
309 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE
, G_TYPE_UINT
, TP_HANDLE_TYPE_NONE
,
312 if (!tp_base_client_register (priv
->handler
, &error
))
314 g_critical ("Failed to register text handler: %s", error
->message
);
315 g_error_free (error
);
320 empathy_chat_manager_finalize (GObject
*object
)
322 EmpathyChatManager
*self
= EMPATHY_CHAT_MANAGER (object
);
323 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
325 if (priv
->closed_queue
!= NULL
)
327 g_queue_foreach (priv
->closed_queue
, (GFunc
) chat_data_free
, NULL
);
328 g_queue_free (priv
->closed_queue
);
329 priv
->closed_queue
= NULL
;
332 tp_clear_pointer (&priv
->messages
, g_hash_table_unref
);
334 tp_clear_object (&priv
->handler
);
335 tp_clear_object (&priv
->chatroom_mgr
);
336 tp_clear_object (&priv
->individual_mgr
);
338 G_OBJECT_CLASS (empathy_chat_manager_parent_class
)->finalize (object
);
342 empathy_chat_manager_constructor (GType type
,
343 guint n_construct_params
,
344 GObjectConstructParam
*construct_params
)
348 if (!chat_manager_singleton
)
350 retval
= G_OBJECT_CLASS (empathy_chat_manager_parent_class
)->constructor
351 (type
, n_construct_params
, construct_params
);
353 chat_manager_singleton
= EMPATHY_CHAT_MANAGER (retval
);
354 g_object_add_weak_pointer (retval
, (gpointer
) &chat_manager_singleton
);
358 retval
= g_object_ref (chat_manager_singleton
);
365 empathy_chat_manager_constructed (GObject
*obj
)
367 GDBusConnection
*conn
;
368 GError
*error
= NULL
;
370 conn
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, &error
);
373 DEBUG ("Failed to get bus: %s", error
->message
);
374 g_error_free (error
);
378 if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (obj
), conn
,
379 CHAT_MANAGER_PATH
, &error
))
381 DEBUG ("Failed to export chat manager: %s\n", error
->message
);
382 g_error_free (error
);
385 g_object_unref (conn
);
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
;
396 object_class
->constructed
= empathy_chat_manager_constructed
;
398 signals
[CLOSED_CHATS_CHANGED
] =
399 g_signal_new ("closed-chats-changed",
400 G_TYPE_FROM_CLASS (object_class
),
404 g_cclosure_marshal_generic
,
406 1, G_TYPE_UINT
, NULL
);
408 signals
[DISPLAYED_CHATS_CHANGED
] =
409 g_signal_new ("displayed-chats-changed",
410 G_TYPE_FROM_CLASS (object_class
),
414 g_cclosure_marshal_generic
,
416 1, G_TYPE_UINT
, NULL
);
418 g_type_class_add_private (empathy_chat_manager_class
,
419 sizeof (EmpathyChatManagerPriv
));
423 empathy_chat_manager_dup_singleton (void)
425 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER
, NULL
);
429 empathy_chat_manager_closed_chat (EmpathyChatManager
*self
,
432 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
));
447 /* If there was a message saved from last time it was closed
448 * (perhaps by accident?) save it to our hash table so it can be
449 * used again when the same chat pops up. Hot. */
450 message
= empathy_chat_dup_text (chat
);
452 chats
= g_hash_table_lookup (priv
->messages
,
453 tp_proxy_get_object_path (data
->account
));
455 /* Don't create a new hash table if we don't already have one and we
456 * don't actually have a message to save. */
457 if (chats
== NULL
&& tp_str_empty (message
))
462 else if (chats
== NULL
&& !tp_str_empty (message
))
464 chats
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
467 g_hash_table_insert (priv
->messages
,
468 g_strdup (tp_proxy_get_object_path (data
->account
)),
472 if (tp_str_empty (message
))
474 g_hash_table_remove (chats
, data
->id
);
480 /* takes ownership of message */
481 g_hash_table_insert (chats
, g_strdup (data
->id
), message
);
486 empathy_chat_manager_undo_closed_chat (EmpathyChatManager
*self
,
489 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
492 data
= g_queue_pop_tail (priv
->closed_queue
);
497 DEBUG ("Removing %s from closed queue and starting a chat with: %s",
498 data
->room
? "room" : "contact", data
->id
);
501 empathy_join_muc (data
->account
, data
->id
, timestamp
);
503 empathy_sms_contact_id (data
->account
, data
->id
, timestamp
,
506 empathy_chat_with_contact_id (data
->account
, data
->id
, timestamp
,
509 g_signal_emit (self
, signals
[CLOSED_CHATS_CHANGED
], 0,
510 g_queue_get_length (priv
->closed_queue
));
512 chat_data_free (data
);
516 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager
*self
)
518 EmpathyChatManagerPriv
*priv
= GET_PRIV (self
);
520 return g_queue_get_length (priv
->closed_queue
);
524 empathy_chat_manager_dbus_undo_closed_chat (EmpathyGenChatManager
*manager
,
525 GDBusMethodInvocation
*invocation
,
528 empathy_chat_manager_undo_closed_chat ((EmpathyChatManager
*) manager
,
531 empathy_gen_chat_manager_complete_undo_closed_chat (manager
, invocation
);
536 chat_manager_iface_init (gpointer g_iface
,
539 EmpathyGenChatManagerIface
*iface
= (EmpathyGenChatManagerIface
*) g_iface
;
541 iface
->handle_undo_closed_chat
= empathy_chat_manager_dbus_undo_closed_chat
;
545 undo_closed_cb (GObject
*source
,
546 GAsyncResult
*result
,
549 GError
*error
= NULL
;
551 if (!empathy_gen_chat_manager_call_undo_closed_chat_finish (
552 EMPATHY_GEN_CHAT_MANAGER (source
), result
,
555 DEBUG ("UndoClosedChat failed: %s", error
->message
);
556 g_error_free (error
);
561 chat_mgr_proxy_cb (GObject
*source
,
562 GAsyncResult
*result
,
565 EmpathyGenChatManager
*proxy
;
566 GError
*error
= NULL
;
567 GVariant
*action_time
= user_data
;
569 proxy
= empathy_gen_chat_manager_proxy_new_for_bus_finish (result
, &error
);
572 DEBUG ("Failed to create ChatManager proxy: %s", error
->message
);
573 g_error_free (error
);
577 empathy_gen_chat_manager_call_undo_closed_chat (proxy
,
578 g_variant_get_int64 (action_time
), NULL
, undo_closed_cb
, NULL
);
580 g_object_unref (proxy
);
583 g_variant_unref (action_time
);
587 empathy_chat_manager_call_undo_closed_chat (void)
591 action_time
= empathy_get_current_action_time ();
593 empathy_gen_chat_manager_proxy_new_for_bus (G_BUS_TYPE_SESSION
,
594 G_DBUS_PROXY_FLAGS_NONE
, EMPATHY_CHAT_BUS_NAME
, CHAT_MANAGER_PATH
,
595 NULL
, chat_mgr_proxy_cb
,
596 /* We can't use GINT_TO_POINTER as we won't be able to store a 64 bits
597 * integer on a 32 bits plateform. Use a GVariant for its convenient
599 g_variant_new_int64 (action_time
));