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