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