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