]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
Inform the chatroom mgr when we are handling a new room
[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 process_tp_chat (EmpathyTpChat *tp_chat,
99     TpAccount *account,
100     gint64 user_action_time)
101 {
102   EmpathyChat *chat = NULL;
103   const gchar *id;
104
105   id = empathy_tp_chat_get_id (tp_chat);
106   if (!tp_str_empty (id))
107     {
108       chat = empathy_chat_window_find_chat (account, id);
109     }
110
111   if (chat != NULL)
112     {
113       empathy_chat_set_tp_chat (chat, tp_chat);
114     }
115   else
116     {
117       chat = empathy_chat_new (tp_chat);
118       /* empathy_chat_new returns a floating reference as EmpathyChat is
119        * a GtkWidget. This reference will be taken by a container
120        * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
121     }
122
123   empathy_chat_window_present_chat (chat, user_action_time);
124
125   if (empathy_tp_chat_is_invited (tp_chat, NULL))
126     {
127       /* We have been invited to the room. Add ourself as member as this
128        * channel has been approved. */
129       empathy_tp_chat_join (tp_chat);
130     }
131
132   if (empathy_chat_is_room (chat))
133     {
134       EmpathyChatroomManager *chatroom_mgr;
135
136       chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
137       empathy_chatroom_manager_chat_handled (chatroom_mgr, tp_chat, account);
138       g_object_unref (chatroom_mgr);
139     }
140
141   g_object_unref (tp_chat);
142 }
143
144 typedef struct
145 {
146   EmpathyTpChat *tp_chat;
147   TpAccount *account;
148   gint64 user_action_time;
149   gulong sig_id;
150 } chat_ready_ctx;
151
152 static chat_ready_ctx *
153 chat_ready_ctx_new (EmpathyTpChat *tp_chat,
154     TpAccount *account,
155     gint64 user_action_time)
156 {
157   chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
158
159   ctx->tp_chat = g_object_ref (tp_chat);
160   ctx->account = g_object_ref (account);
161   ctx->user_action_time = user_action_time;
162   return ctx;
163 }
164
165 static void
166 chat_ready_ctx_free (chat_ready_ctx *ctx)
167 {
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 (tp_chat, ctx->account, ctx->user_action_time);
189
190   chat_ready_ctx_free (ctx);
191 }
192
193 static void
194 handle_channels (TpSimpleHandler *handler,
195     TpAccount *account,
196     TpConnection *connection,
197     GList *channels,
198     GList *requests_satisfied,
199     gint64 user_action_time,
200     TpHandleChannelsContext *context,
201     gpointer user_data)
202 {
203   GList *l;
204
205   for (l = channels; l != NULL; l = g_list_next (l))
206     {
207       TpChannel *channel = l->data;
208       EmpathyTpChat *tp_chat;
209
210       tp_chat = empathy_tp_chat_new (channel);
211
212       if (empathy_tp_chat_is_ready (tp_chat))
213         {
214           process_tp_chat (tp_chat, account, user_action_time);
215         }
216       else
217         {
218           chat_ready_ctx *ctx = chat_ready_ctx_new (tp_chat, account,
219               user_action_time);
220
221           ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
222               G_CALLBACK (tp_chat_ready_cb), ctx);
223         }
224     }
225
226   tp_handle_channels_context_accept (context);
227 }
228
229 static void
230 empathy_chat_manager_init (EmpathyChatManager *self)
231 {
232   EmpathyChatManagerPriv *priv = GET_PRIV (self);
233   TpDBusDaemon *dbus;
234   GError *error = NULL;
235
236   priv->queue = g_queue_new ();
237
238   dbus = tp_dbus_daemon_dup (&error);
239   if (dbus == NULL)
240     {
241       g_critical ("Failed to get D-Bus daemon: %s", error->message);
242       g_error_free (error);
243       return;
244     }
245
246   /* Text channels handler */
247   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE, "Empathy", FALSE,
248       handle_channels, self, NULL);
249
250   g_object_unref (dbus);
251
252   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
253         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
254         NULL));
255
256   if (!tp_base_client_register (priv->handler, &error))
257     {
258       g_critical ("Failed to register text handler: %s", error->message);
259       g_error_free (error);
260     }
261 }
262
263 static void
264 empathy_chat_manager_finalize (GObject *object)
265 {
266   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
267   EmpathyChatManagerPriv *priv = GET_PRIV (self);
268
269   if (priv->queue != NULL)
270     {
271       g_queue_foreach (priv->queue, (GFunc) chat_data_free, NULL);
272       g_queue_free (priv->queue);
273       priv->queue = NULL;
274     }
275
276   tp_clear_object (&priv->handler);
277
278   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
279 }
280
281 static GObject *
282 empathy_chat_manager_constructor (GType type,
283     guint n_construct_params,
284     GObjectConstructParam *construct_params)
285 {
286   GObject *retval;
287
288   if (!chat_manager_singleton)
289     {
290       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
291         (type, n_construct_params, construct_params);
292
293       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
294       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
295     }
296   else
297     {
298       retval = g_object_ref (chat_manager_singleton);
299     }
300
301   return retval;
302 }
303
304 static void
305 empathy_chat_manager_class_init (
306   EmpathyChatManagerClass *empathy_chat_manager_class)
307 {
308   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
309
310   object_class->finalize = empathy_chat_manager_finalize;
311   object_class->constructor = empathy_chat_manager_constructor;
312
313   signals[CHATS_CHANGED] =
314     g_signal_new ("chats-changed",
315         G_TYPE_FROM_CLASS (object_class),
316         G_SIGNAL_RUN_LAST,
317         0,
318         NULL, NULL,
319         g_cclosure_marshal_VOID__UINT,
320         G_TYPE_NONE,
321         1, G_TYPE_UINT, NULL);
322
323   g_type_class_add_private (empathy_chat_manager_class,
324     sizeof (EmpathyChatManagerPriv));
325 }
326
327 EmpathyChatManager *
328 empathy_chat_manager_dup_singleton (void)
329 {
330   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
331 }
332
333 void
334 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
335     EmpathyChat *chat)
336 {
337   EmpathyChatManagerPriv *priv = GET_PRIV (self);
338   ChatData *data;
339
340   data = chat_data_new (chat);
341
342   DEBUG ("Adding %s to queue: %s", data->room ? "room" : "contact", data->id);
343
344   g_queue_push_tail (priv->queue, data);
345
346   g_signal_emit (self, signals[CHATS_CHANGED], 0,
347       g_queue_get_length (priv->queue));
348 }
349
350 static void
351 connection_ready_cb (TpConnection *connection,
352     const GError *error,
353     gpointer user_data)
354 {
355   ChatData *data = user_data;
356   EmpathyChatManager *self = chat_manager_singleton;
357   EmpathyChatManagerPriv *priv;
358
359   /* Extremely unlikely to happen, but I don't really want to keep refs to the
360    * chat manager in the ChatData structs as it'll then prevent the manager
361    * from being finalized. */
362   if (G_UNLIKELY (self == NULL))
363     goto out;
364
365   priv = GET_PRIV (self);
366
367   if (error == NULL)
368     {
369       if (data->room)
370         empathy_dispatcher_join_muc (connection, data->id,
371           EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
372       else
373         empathy_dispatcher_chat_with_contact_id (connection, data->id,
374             EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
375
376       g_signal_emit (self, signals[CHATS_CHANGED], 0,
377           g_queue_get_length (priv->queue));
378     }
379   else
380     {
381       DEBUG ("Error readying connection, no chat: %s", error->message);
382     }
383
384 out:
385   chat_data_free (data);
386 }
387
388 void
389 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
390 {
391   EmpathyChatManagerPriv *priv = GET_PRIV (self);
392   ChatData *data;
393   TpConnection *connection;
394
395   data = g_queue_pop_tail (priv->queue);
396
397   if (data == NULL)
398     return;
399
400   DEBUG ("Removing %s from queue and starting a chat with: %s",
401       data->room ? "room" : "contact", data->id);
402
403   connection = tp_account_get_connection (data->account);
404
405   if (connection != NULL)
406     {
407       tp_connection_call_when_ready (connection, connection_ready_cb, data);
408     }
409   else
410     {
411       DEBUG ("No connection, no chat.");
412       chat_data_free (data);
413     }
414 }
415
416 guint
417 empathy_chat_manager_get_num_chats (EmpathyChatManager *self)
418 {
419   EmpathyChatManagerPriv *priv = GET_PRIV (self);
420
421   return g_queue_get_length (priv->queue);
422 }