]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
Merge remote branch 'glassrose/remove-typing-icon-in-muc-tabs-609420'
[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-dispatcher.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   HANDLED_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_handled_channels;
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 process_tp_chat (EmpathyChatManager *self,
104     EmpathyTpChat *tp_chat,
105     TpAccount *account,
106     gint64 user_action_time)
107 {
108   EmpathyChat *chat = NULL;
109   const gchar *id;
110
111   id = empathy_tp_chat_get_id (tp_chat);
112   if (!tp_str_empty (id))
113     {
114       chat = empathy_chat_window_find_chat (account, id);
115     }
116
117   if (chat != NULL)
118     {
119       empathy_chat_set_tp_chat (chat, tp_chat);
120     }
121   else
122     {
123       chat = empathy_chat_new (tp_chat);
124       /* empathy_chat_new returns a floating reference as EmpathyChat is
125        * a GtkWidget. This reference will be taken by a container
126        * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
127     }
128   empathy_chat_window_present_chat (chat, user_action_time);
129
130   if (empathy_tp_chat_is_invited (tp_chat, NULL))
131     {
132       /* We have been invited to the room. Add ourself as member as this
133        * channel has been approved. */
134       empathy_tp_chat_join (tp_chat);
135     }
136
137   g_object_unref (tp_chat);
138 }
139
140 typedef struct
141 {
142   EmpathyChatManager *self;
143   EmpathyTpChat *tp_chat;
144   TpAccount *account;
145   gint64 user_action_time;
146   gulong sig_id;
147 } chat_ready_ctx;
148
149 static chat_ready_ctx *
150 chat_ready_ctx_new (EmpathyChatManager *self,
151     EmpathyTpChat *tp_chat,
152     TpAccount *account,
153     gint64 user_action_time)
154 {
155   chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
156
157   ctx->self = g_object_ref (self);
158   ctx->tp_chat = g_object_ref (tp_chat);
159   ctx->account = g_object_ref (account);
160   ctx->user_action_time = user_action_time;
161   return ctx;
162 }
163
164 static void
165 chat_ready_ctx_free (chat_ready_ctx *ctx)
166 {
167   g_object_unref (ctx->self);
168   g_object_unref (ctx->tp_chat);
169   g_object_unref (ctx->account);
170
171   if (ctx->sig_id != 0)
172     g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
173
174   g_slice_free (chat_ready_ctx, ctx);
175 }
176
177 static void
178 tp_chat_ready_cb (GObject *object,
179   GParamSpec *spec,
180   gpointer user_data)
181 {
182   EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
183   chat_ready_ctx *ctx = user_data;
184
185   if (!empathy_tp_chat_is_ready (tp_chat))
186     return;
187
188   process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
189
190   chat_ready_ctx_free (ctx);
191 }
192
193 static void
194 channel_invalidated (TpChannel *channel,
195     guint domain,
196     gint code,
197     gchar *message,
198     EmpathyChatManager *self)
199 {
200   EmpathyChatManagerPriv *priv = GET_PRIV (self);
201
202   priv->num_handled_channels--;
203
204   DEBUG ("Channel closed; we are now handling %u text channels",
205       priv->num_handled_channels);
206
207   g_signal_emit (self, signals[HANDLED_CHATS_CHANGED], 0,
208       priv->num_handled_channels);
209 }
210
211 static void
212 handle_channels (TpSimpleHandler *handler,
213     TpAccount *account,
214     TpConnection *connection,
215     GList *channels,
216     GList *requests_satisfied,
217     gint64 user_action_time,
218     TpHandleChannelsContext *context,
219     gpointer user_data)
220 {
221   EmpathyChatManager *self = (EmpathyChatManager *) user_data;
222   EmpathyChatManagerPriv *priv = GET_PRIV (self);
223   GList *l;
224   gboolean handling = FALSE;
225
226   for (l = channels; l != NULL; l = g_list_next (l))
227     {
228       TpChannel *channel = l->data;
229       EmpathyTpChat *tp_chat;
230
231       if (tp_proxy_get_invalidated (channel) != NULL)
232         continue;
233
234       handling = TRUE;
235
236       tp_chat = empathy_tp_chat_new (account, channel);
237
238       if (empathy_tp_chat_is_ready (tp_chat))
239         {
240           process_tp_chat (self, tp_chat, account, user_action_time);
241         }
242       else
243         {
244           chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
245               user_action_time);
246
247           ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
248               G_CALLBACK (tp_chat_ready_cb), ctx);
249         }
250
251       priv->num_handled_channels++;
252
253       g_signal_connect (channel, "invalidated",
254           G_CALLBACK (channel_invalidated), self);
255     }
256
257   tp_handle_channels_context_accept (context);
258
259   if (handling)
260     {
261       DEBUG ("Channels handled; we are now handling %u text channels",
262           priv->num_handled_channels);
263
264       g_signal_emit (self, signals[HANDLED_CHATS_CHANGED], 0,
265           priv->num_handled_channels);
266     }
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, "Empathy.Chat",
290       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[HANDLED_CHATS_CHANGED] =
384     g_signal_new ("handled-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_dispatcher_join_muc (data->account, data->id,
437         TP_USER_ACTION_TIME_NOT_USER_ACTION);
438   else
439     empathy_dispatcher_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 }
455
456 guint
457 empathy_chat_manager_get_num_handled_chats (EmpathyChatManager *self)
458 {
459   EmpathyChatManagerPriv *priv = GET_PRIV (self);
460
461   return priv->num_handled_channels;
462 }