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