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