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