]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
chat-manager: cache the individual mgr
[empathy.git] / src / empathy-chat-manager.c
1 /*
2  * empathy-chat-manager.c - Source for EmpathyChatManager
3  * Copyright (C) 2010 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21
22 #include <telepathy-glib/telepathy-glib.h>
23 #include <telepathy-glib/proxy-subclass.h>
24
25 #include <libempathy/empathy-chatroom-manager.h>
26 #include <libempathy/empathy-request-util.h>
27 #include <libempathy/empathy-utils.h>
28 #include <libempathy/empathy-individual-manager.h>
29
30 #include <libempathy-gtk/empathy-ui-utils.h>
31
32 #include "empathy-chat-window.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 #include "empathy-chat-manager.h"
38
39 #include <extensions/extensions.h>
40
41 enum {
42   CLOSED_CHATS_CHANGED,
43   DISPLAYED_CHATS_CHANGED,
44   LAST_SIGNAL
45 };
46
47 static guint signals[LAST_SIGNAL];
48
49 static void svc_iface_init (gpointer, gpointer);
50
51 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
52     G_TYPE_OBJECT,
53     G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
54         svc_iface_init)
55     )
56
57 /* private structure */
58 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
59
60 struct _EmpathyChatManagerPriv
61 {
62   EmpathyChatroomManager *chatroom_mgr;
63   /* Queue of (ChatData *) representing the closed chats */
64   GQueue *closed_queue;
65
66   guint num_displayed_chat;
67
68   /* account path -> (GHashTable<(owned gchar *) contact ID
69    *                  -> (owned gchar *) non-NULL message>)
70    */
71   GHashTable *messages;
72
73   TpBaseClient *handler;
74
75   /* Cached to keep Folks in memory while empathy-chat is running; we don't
76    * want to reload it each time the last chat window is closed
77    * and re-opened. */
78   EmpathyIndividualManager *individual_mgr;
79 };
80
81 #define GET_PRIV(o) \
82   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
83     EmpathyChatManagerPriv))
84
85 static EmpathyChatManager *chat_manager_singleton = NULL;
86
87 typedef struct
88 {
89   TpAccount *account;
90   gchar *id;
91   gboolean room;
92   gboolean sms;
93 } ChatData;
94
95 static ChatData *
96 chat_data_new (EmpathyChat *chat)
97 {
98   ChatData *data = NULL;
99
100   data = g_slice_new0 (ChatData);
101
102   data->account = g_object_ref (empathy_chat_get_account (chat));
103   data->id = g_strdup (empathy_chat_get_id (chat));
104   data->room = empathy_chat_is_room (chat);
105   data->sms = empathy_chat_is_sms_channel (chat);
106
107   return data;
108 }
109
110 static void
111 chat_data_free (ChatData *data)
112 {
113   if (data->account != NULL)
114     {
115       g_object_unref (data->account);
116       data->account = NULL;
117     }
118
119   if (data->id != NULL)
120     {
121       g_free (data->id);
122       data->id = NULL;
123     }
124
125   g_slice_free (ChatData, data);
126 }
127
128 static void
129 chat_destroyed_cb (gpointer data,
130     GObject *object)
131 {
132   EmpathyChatManager *self = data;
133   EmpathyChatManagerPriv *priv = GET_PRIV (self);
134
135   priv->num_displayed_chat--;
136
137   DEBUG ("Chat destroyed; we are now displaying %u chats",
138       priv->num_displayed_chat);
139
140   g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
141       priv->num_displayed_chat);
142 }
143
144 static void
145 join_cb (GObject *source,
146     GAsyncResult *result,
147     gpointer user_data)
148 {
149   TpChannel *channel = TP_CHANNEL (source);
150   GError *error = NULL;
151
152   if (!tp_channel_join_finish (channel, result, &error))
153     {
154       DEBUG ("Failed to join chat (%s): %s",
155           tp_channel_get_identifier (channel), error->message);
156       g_error_free (error);
157     }
158 }
159
160 static void
161 individual_mgr_cb (EmpathyChatWindow *window,
162     GParamSpec *spec,
163     EmpathyChatManager *self)
164 {
165   EmpathyChatManagerPriv *priv = GET_PRIV (self);
166
167   if (priv->individual_mgr != NULL)
168     return;
169
170   priv->individual_mgr = empathy_chat_window_get_individual_manager (window);
171   g_object_ref (priv->individual_mgr);
172 }
173
174 static void
175 process_tp_chat (EmpathyChatManager *self,
176     EmpathyTpChat *tp_chat,
177     TpAccount *account,
178     gint64 user_action_time)
179 {
180   EmpathyChatManagerPriv *priv = GET_PRIV (self);
181   EmpathyChat *chat = NULL;
182   const gchar *id;
183   EmpathyChatWindow *window;
184
185   id = empathy_tp_chat_get_id (tp_chat);
186   if (!tp_str_empty (id))
187     {
188       chat = empathy_chat_window_find_chat (account, id,
189           tp_text_channel_is_sms_channel ((TpTextChannel *) tp_chat));
190     }
191
192   if (chat != NULL)
193     {
194       empathy_chat_set_tp_chat (chat, tp_chat);
195     }
196   else
197     {
198       GHashTable *chats = NULL;
199
200       chat = empathy_chat_new (tp_chat);
201       /* empathy_chat_new returns a floating reference as EmpathyChat is
202        * a GtkWidget. This reference will be taken by a container
203        * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
204
205       priv->num_displayed_chat++;
206
207       DEBUG ("Chat displayed; we are now displaying %u chat",
208           priv->num_displayed_chat);
209
210       g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
211           priv->num_displayed_chat);
212
213       /* Set the saved message in the channel if we have one. */
214       chats = g_hash_table_lookup (priv->messages,
215           tp_proxy_get_object_path (account));
216
217       if (chats != NULL)
218         {
219           const gchar *msg = g_hash_table_lookup (chats, id);
220
221           if (msg != NULL)
222             empathy_chat_set_text (chat, msg);
223         }
224
225       g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
226     }
227
228   window = empathy_chat_window_present_chat (chat, user_action_time);
229
230   if (priv->individual_mgr == NULL)
231     {
232       /* We want to cache it as soon it's created */
233       tp_g_signal_connect_object (window, "notify::individual-manager",
234         G_CALLBACK (individual_mgr_cb), self, 0);
235     }
236
237   if (empathy_tp_chat_is_invited (tp_chat, NULL))
238     {
239       /* We have been invited to the room. Add ourself as member as this
240        * channel has been approved. */
241       tp_channel_join_async (TP_CHANNEL (tp_chat), "", join_cb, self);
242     }
243 }
244
245 static void
246 handle_channels (TpSimpleHandler *handler,
247     TpAccount *account,
248     TpConnection *connection,
249     GList *channels,
250     GList *requests_satisfied,
251     gint64 user_action_time,
252     TpHandleChannelsContext *context,
253     gpointer user_data)
254 {
255   EmpathyChatManager *self = (EmpathyChatManager *) user_data;
256   GList *l;
257
258   for (l = channels; l != NULL; l = g_list_next (l))
259     {
260       EmpathyTpChat *tp_chat = l->data;
261
262       if (tp_proxy_get_invalidated (tp_chat) != NULL)
263         continue;
264
265       if (!EMPATHY_IS_TP_CHAT (tp_chat))
266         {
267           DEBUG ("Channel %s doesn't implement Messages; can't handle it",
268               tp_proxy_get_object_path (tp_chat));
269           continue;
270         }
271
272       DEBUG ("Now handling channel %s", tp_proxy_get_object_path (tp_chat));
273
274       process_tp_chat (self, tp_chat, account, user_action_time);
275     }
276
277   tp_handle_channels_context_accept (context);
278 }
279
280 static void
281 empathy_chat_manager_init (EmpathyChatManager *self)
282 {
283   EmpathyChatManagerPriv *priv = GET_PRIV (self);
284   TpAccountManager *am;
285   GError *error = NULL;
286
287   priv->closed_queue = g_queue_new ();
288   priv->messages = g_hash_table_new_full (g_str_hash, g_str_equal,
289       g_free, (GDestroyNotify) g_hash_table_unref);
290
291   am = tp_account_manager_dup ();
292
293   priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
294
295   /* Text channels handler */
296   priv->handler = tp_simple_handler_new_with_am (am, FALSE, FALSE,
297       EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
298
299   g_object_unref (am);
300
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,
304         NULL));
305
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,
309         NULL));
310
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,
314         NULL));
315
316   if (!tp_base_client_register (priv->handler, &error))
317     {
318       g_critical ("Failed to register text handler: %s", error->message);
319       g_error_free (error);
320     }
321 }
322
323 static void
324 empathy_chat_manager_finalize (GObject *object)
325 {
326   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
327   EmpathyChatManagerPriv *priv = GET_PRIV (self);
328
329   if (priv->closed_queue != NULL)
330     {
331       g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
332       g_queue_free (priv->closed_queue);
333       priv->closed_queue = NULL;
334     }
335
336   tp_clear_pointer (&priv->messages, g_hash_table_unref);
337
338   tp_clear_object (&priv->handler);
339   tp_clear_object (&priv->chatroom_mgr);
340   tp_clear_object (&priv->individual_mgr);
341
342   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
343 }
344
345 static GObject *
346 empathy_chat_manager_constructor (GType type,
347     guint n_construct_params,
348     GObjectConstructParam *construct_params)
349 {
350   GObject *retval;
351
352   if (!chat_manager_singleton)
353     {
354       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
355         (type, n_construct_params, construct_params);
356
357       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
358       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
359     }
360   else
361     {
362       retval = g_object_ref (chat_manager_singleton);
363     }
364
365   return retval;
366 }
367
368 static void
369 empathy_chat_manager_constructed (GObject *obj)
370 {
371   TpDBusDaemon *dbus_daemon;
372
373   dbus_daemon = tp_dbus_daemon_dup (NULL);
374
375   if (dbus_daemon != NULL)
376     {
377       tp_dbus_daemon_register_object (dbus_daemon,
378           "/org/gnome/Empathy/ChatManager", obj);
379
380       g_object_unref (dbus_daemon);
381     }
382 }
383
384 static void
385 empathy_chat_manager_class_init (
386   EmpathyChatManagerClass *empathy_chat_manager_class)
387 {
388   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
389
390   object_class->finalize = empathy_chat_manager_finalize;
391   object_class->constructor = empathy_chat_manager_constructor;
392   object_class->constructed = empathy_chat_manager_constructed;
393
394   signals[CLOSED_CHATS_CHANGED] =
395     g_signal_new ("closed-chats-changed",
396         G_TYPE_FROM_CLASS (object_class),
397         G_SIGNAL_RUN_LAST,
398         0,
399         NULL, NULL,
400         g_cclosure_marshal_generic,
401         G_TYPE_NONE,
402         1, G_TYPE_UINT, NULL);
403
404   signals[DISPLAYED_CHATS_CHANGED] =
405     g_signal_new ("displayed-chats-changed",
406         G_TYPE_FROM_CLASS (object_class),
407         G_SIGNAL_RUN_LAST,
408         0,
409         NULL, NULL,
410         g_cclosure_marshal_generic,
411         G_TYPE_NONE,
412         1, G_TYPE_UINT, NULL);
413
414   g_type_class_add_private (empathy_chat_manager_class,
415     sizeof (EmpathyChatManagerPriv));
416 }
417
418 EmpathyChatManager *
419 empathy_chat_manager_dup_singleton (void)
420 {
421   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
422 }
423
424 void
425 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
426     EmpathyChat *chat)
427 {
428   EmpathyChatManagerPriv *priv = GET_PRIV (self);
429   ChatData *data;
430   GHashTable *chats;
431   gchar *message;
432
433   data = chat_data_new (chat);
434
435   DEBUG ("Adding %s to closed queue: %s",
436       data->room ? "room" : "contact", data->id);
437
438   g_queue_push_tail (priv->closed_queue, data);
439
440   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
441       g_queue_get_length (priv->closed_queue));
442
443   /* If there was a message saved from last time it was closed
444    * (perhaps by accident?) save it to our hash table so it can be
445    * used again when the same chat pops up. Hot. */
446   message = empathy_chat_dup_text (chat);
447
448   chats = g_hash_table_lookup (priv->messages,
449       tp_proxy_get_object_path (data->account));
450
451   /* Don't create a new hash table if we don't already have one and we
452    * don't actually have a message to save. */
453   if (chats == NULL && tp_str_empty (message))
454     {
455       g_free (message);
456       return;
457     }
458   else if (chats == NULL && !tp_str_empty (message))
459     {
460       chats = g_hash_table_new_full (g_str_hash, g_str_equal,
461           g_free, g_free);
462
463       g_hash_table_insert (priv->messages,
464           g_strdup (tp_proxy_get_object_path (data->account)),
465           chats);
466     }
467
468   if (tp_str_empty (message))
469     {
470       g_hash_table_remove (chats, data->id);
471       /* might be '\0' */
472       g_free (message);
473     }
474   else
475     {
476       /* takes ownership of message */
477       g_hash_table_insert (chats, g_strdup (data->id), message);
478     }
479 }
480
481 void
482 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self,
483     gint64 timestamp)
484 {
485   EmpathyChatManagerPriv *priv = GET_PRIV (self);
486   ChatData *data;
487
488   data = g_queue_pop_tail (priv->closed_queue);
489
490   if (data == NULL)
491     return;
492
493   DEBUG ("Removing %s from closed queue and starting a chat with: %s",
494       data->room ? "room" : "contact", data->id);
495
496   if (data->room)
497     empathy_join_muc (data->account, data->id, timestamp);
498   else if (data->sms)
499     empathy_sms_contact_id (data->account, data->id, timestamp,
500         NULL, NULL);
501   else
502     empathy_chat_with_contact_id (data->account, data->id, timestamp,
503         NULL, NULL);
504
505   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
506       g_queue_get_length (priv->closed_queue));
507
508   chat_data_free (data);
509 }
510
511 guint
512 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
513 {
514   EmpathyChatManagerPriv *priv = GET_PRIV (self);
515
516   return g_queue_get_length (priv->closed_queue);
517 }
518
519 static void
520 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
521     gint64 timestamp,
522     DBusGMethodInvocation *context)
523 {
524   empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager,
525       timestamp);
526
527   emp_svc_chat_manager_return_from_undo_closed_chat (context);
528 }
529
530 static void
531 svc_iface_init (gpointer g_iface,
532     gpointer iface_data)
533 {
534   EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
535
536 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
537     klass, empathy_chat_manager_dbus_##x)
538   IMPLEMENT(undo_closed_chat);
539 #undef IMPLEMENT
540 }
541
542 void
543 empathy_chat_manager_call_undo_closed_chat (void)
544 {
545   TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
546   TpProxy *proxy;
547
548   if (dbus_daemon == NULL)
549     return;
550
551   proxy = g_object_new (TP_TYPE_PROXY,
552       "dbus-daemon", dbus_daemon,
553       "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
554       "bus-name", EMPATHY_CHAT_BUS_NAME,
555       "object-path", "/org/gnome/Empathy/ChatManager",
556       NULL);
557
558   tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
559
560   emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, empathy_get_current_action_time (),
561       NULL, NULL, NULL, NULL);
562
563   g_object_unref (proxy);
564   g_object_unref (dbus_daemon);
565 }