]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
Merge branch 'sasl'
[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 this feature being prepared */
293   tp_base_client_add_connection_features_varargs (priv->handler,
294     TP_CONNECTION_FEATURE_CAPABILITIES, 0);
295
296   g_object_unref (dbus);
297
298   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
299         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
300         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
301         NULL));
302
303   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
304         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
305         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
306         NULL));
307
308   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
309         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
310         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
311         NULL));
312
313   if (!tp_base_client_register (priv->handler, &error))
314     {
315       g_critical ("Failed to register text handler: %s", error->message);
316       g_error_free (error);
317     }
318 }
319
320 static void
321 empathy_chat_manager_finalize (GObject *object)
322 {
323   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
324   EmpathyChatManagerPriv *priv = GET_PRIV (self);
325
326   if (priv->closed_queue != NULL)
327     {
328       g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
329       g_queue_free (priv->closed_queue);
330       priv->closed_queue = NULL;
331     }
332
333   tp_clear_object (&priv->handler);
334   tp_clear_object (&priv->chatroom_mgr);
335
336   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
337 }
338
339 static GObject *
340 empathy_chat_manager_constructor (GType type,
341     guint n_construct_params,
342     GObjectConstructParam *construct_params)
343 {
344   GObject *retval;
345
346   if (!chat_manager_singleton)
347     {
348       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
349         (type, n_construct_params, construct_params);
350
351       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
352       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
353     }
354   else
355     {
356       retval = g_object_ref (chat_manager_singleton);
357     }
358
359   return retval;
360 }
361
362 static void
363 empathy_chat_manager_class_init (
364   EmpathyChatManagerClass *empathy_chat_manager_class)
365 {
366   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
367
368   object_class->finalize = empathy_chat_manager_finalize;
369   object_class->constructor = empathy_chat_manager_constructor;
370
371   signals[CLOSED_CHATS_CHANGED] =
372     g_signal_new ("closed-chats-changed",
373         G_TYPE_FROM_CLASS (object_class),
374         G_SIGNAL_RUN_LAST,
375         0,
376         NULL, NULL,
377         g_cclosure_marshal_VOID__UINT,
378         G_TYPE_NONE,
379         1, G_TYPE_UINT, NULL);
380
381   signals[HANDLED_CHATS_CHANGED] =
382     g_signal_new ("handled-chats-changed",
383         G_TYPE_FROM_CLASS (object_class),
384         G_SIGNAL_RUN_LAST,
385         0,
386         NULL, NULL,
387         g_cclosure_marshal_VOID__UINT,
388         G_TYPE_NONE,
389         1, G_TYPE_UINT, NULL);
390
391   g_type_class_add_private (empathy_chat_manager_class,
392     sizeof (EmpathyChatManagerPriv));
393 }
394
395 EmpathyChatManager *
396 empathy_chat_manager_dup_singleton (void)
397 {
398   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
399 }
400
401 void
402 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
403     EmpathyChat *chat)
404 {
405   EmpathyChatManagerPriv *priv = GET_PRIV (self);
406   ChatData *data;
407
408   data = chat_data_new (chat);
409
410   DEBUG ("Adding %s to closed queue: %s",
411       data->room ? "room" : "contact", data->id);
412
413   g_queue_push_tail (priv->closed_queue, data);
414
415   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
416       g_queue_get_length (priv->closed_queue));
417 }
418
419 void
420 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
421 {
422   EmpathyChatManagerPriv *priv = GET_PRIV (self);
423   ChatData *data;
424
425   data = g_queue_pop_tail (priv->closed_queue);
426
427   if (data == NULL)
428     return;
429
430   DEBUG ("Removing %s from closed queue and starting a chat with: %s",
431       data->room ? "room" : "contact", data->id);
432
433   if (data->room)
434     empathy_dispatcher_join_muc (data->account, data->id,
435         TP_USER_ACTION_TIME_NOT_USER_ACTION);
436   else
437     empathy_dispatcher_chat_with_contact_id (data->account, data->id,
438         TP_USER_ACTION_TIME_NOT_USER_ACTION);
439
440   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
441       g_queue_get_length (priv->closed_queue));
442
443   chat_data_free (data);
444 }
445
446 guint
447 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
448 {
449   EmpathyChatManagerPriv *priv = GET_PRIV (self);
450
451   return g_queue_get_length (priv->closed_queue);
452 }
453
454 guint
455 empathy_chat_manager_get_num_handled_chats (EmpathyChatManager *self)
456 {
457   EmpathyChatManagerPriv *priv = GET_PRIV (self);
458
459   return priv->num_handled_channels;
460 }