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