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