]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
remove unused empathy_chat_manager_get_num_handled_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-request-util.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       if (!TP_IS_TEXT_CHANNEL (channel))
235         {
236           DEBUG ("Channel %s doesn't implement Messages; can't handle it",
237               tp_proxy_get_object_path (channel));
238           continue;
239         }
240
241       handling = TRUE;
242
243       tp_chat = empathy_tp_chat_new (account, channel);
244
245       if (empathy_tp_chat_is_ready (tp_chat))
246         {
247           process_tp_chat (self, tp_chat, account, user_action_time);
248         }
249       else
250         {
251           chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
252               user_action_time);
253
254           ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
255               G_CALLBACK (tp_chat_ready_cb), ctx);
256         }
257
258       priv->num_handled_channels++;
259
260       g_signal_connect (channel, "invalidated",
261           G_CALLBACK (channel_invalidated), self);
262     }
263
264   tp_handle_channels_context_accept (context);
265
266   if (handling)
267     {
268       DEBUG ("Channels handled; we are now handling %u text channels",
269           priv->num_handled_channels);
270
271       g_signal_emit (self, signals[HANDLED_CHATS_CHANGED], 0,
272           priv->num_handled_channels);
273     }
274 }
275
276 static void
277 empathy_chat_manager_init (EmpathyChatManager *self)
278 {
279   EmpathyChatManagerPriv *priv = GET_PRIV (self);
280   TpDBusDaemon *dbus;
281   GError *error = NULL;
282
283   priv->closed_queue = g_queue_new ();
284
285   dbus = tp_dbus_daemon_dup (&error);
286   if (dbus == NULL)
287     {
288       g_critical ("Failed to get D-Bus daemon: %s", error->message);
289       g_error_free (error);
290       return;
291     }
292
293   priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
294
295   /* Text channels handler */
296   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
297       EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
298
299   /* EmpathyTpChat relies on these features being prepared */
300   tp_base_client_add_connection_features_varargs (priv->handler,
301     TP_CONNECTION_FEATURE_CAPABILITIES, 0);
302   tp_base_client_add_channel_features_varargs (priv->handler,
303       TP_CHANNEL_FEATURE_CHAT_STATES, 0);
304
305   g_object_unref (dbus);
306
307   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
308         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
309         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
310         NULL));
311
312   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
313         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
314         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
315         NULL));
316
317   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
318         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
319         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
320         NULL));
321
322   if (!tp_base_client_register (priv->handler, &error))
323     {
324       g_critical ("Failed to register text handler: %s", error->message);
325       g_error_free (error);
326     }
327 }
328
329 static void
330 empathy_chat_manager_finalize (GObject *object)
331 {
332   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
333   EmpathyChatManagerPriv *priv = GET_PRIV (self);
334
335   if (priv->closed_queue != NULL)
336     {
337       g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
338       g_queue_free (priv->closed_queue);
339       priv->closed_queue = NULL;
340     }
341
342   tp_clear_object (&priv->handler);
343   tp_clear_object (&priv->chatroom_mgr);
344
345   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
346 }
347
348 static GObject *
349 empathy_chat_manager_constructor (GType type,
350     guint n_construct_params,
351     GObjectConstructParam *construct_params)
352 {
353   GObject *retval;
354
355   if (!chat_manager_singleton)
356     {
357       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
358         (type, n_construct_params, construct_params);
359
360       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
361       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
362     }
363   else
364     {
365       retval = g_object_ref (chat_manager_singleton);
366     }
367
368   return retval;
369 }
370
371 static void
372 empathy_chat_manager_class_init (
373   EmpathyChatManagerClass *empathy_chat_manager_class)
374 {
375   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
376
377   object_class->finalize = empathy_chat_manager_finalize;
378   object_class->constructor = empathy_chat_manager_constructor;
379
380   signals[CLOSED_CHATS_CHANGED] =
381     g_signal_new ("closed-chats-changed",
382         G_TYPE_FROM_CLASS (object_class),
383         G_SIGNAL_RUN_LAST,
384         0,
385         NULL, NULL,
386         g_cclosure_marshal_VOID__UINT,
387         G_TYPE_NONE,
388         1, G_TYPE_UINT, NULL);
389
390   signals[HANDLED_CHATS_CHANGED] =
391     g_signal_new ("handled-chats-changed",
392         G_TYPE_FROM_CLASS (object_class),
393         G_SIGNAL_RUN_LAST,
394         0,
395         NULL, NULL,
396         g_cclosure_marshal_VOID__UINT,
397         G_TYPE_NONE,
398         1, G_TYPE_UINT, NULL);
399
400   g_type_class_add_private (empathy_chat_manager_class,
401     sizeof (EmpathyChatManagerPriv));
402 }
403
404 EmpathyChatManager *
405 empathy_chat_manager_dup_singleton (void)
406 {
407   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
408 }
409
410 void
411 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
412     EmpathyChat *chat)
413 {
414   EmpathyChatManagerPriv *priv = GET_PRIV (self);
415   ChatData *data;
416
417   data = chat_data_new (chat);
418
419   DEBUG ("Adding %s to closed queue: %s",
420       data->room ? "room" : "contact", data->id);
421
422   g_queue_push_tail (priv->closed_queue, data);
423
424   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
425       g_queue_get_length (priv->closed_queue));
426 }
427
428 void
429 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
430 {
431   EmpathyChatManagerPriv *priv = GET_PRIV (self);
432   ChatData *data;
433
434   data = g_queue_pop_tail (priv->closed_queue);
435
436   if (data == NULL)
437     return;
438
439   DEBUG ("Removing %s from closed queue and starting a chat with: %s",
440       data->room ? "room" : "contact", data->id);
441
442   if (data->room)
443     empathy_join_muc (data->account, data->id,
444         TP_USER_ACTION_TIME_NOT_USER_ACTION);
445   else
446     empathy_chat_with_contact_id (data->account, data->id,
447         TP_USER_ACTION_TIME_NOT_USER_ACTION);
448
449   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
450       g_queue_get_length (priv->closed_queue));
451
452   chat_data_free (data);
453 }
454
455 guint
456 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
457 {
458   EmpathyChatManagerPriv *priv = GET_PRIV (self);
459
460   return g_queue_get_length (priv->closed_queue);
461 }