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