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