]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
chat-manager: use EMPATHY_CHAT_BUS_NAME
[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 #include <telepathy-glib/proxy-subclass.h>
22
23 #include <libempathy/empathy-chatroom-manager.h>
24 #include <libempathy/empathy-request-util.h>
25 #include <libempathy/empathy-utils.h>
26
27 #include "empathy-chat-window.h"
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
31
32 #include "empathy-chat-manager.h"
33
34 #include <extensions/extensions.h>
35
36 enum {
37   CLOSED_CHATS_CHANGED,
38   DISPLAYED_CHATS_CHANGED,
39   LAST_SIGNAL
40 };
41
42 static guint signals[LAST_SIGNAL];
43
44 static void svc_iface_init (gpointer, gpointer);
45
46 G_DEFINE_TYPE_WITH_CODE (EmpathyChatManager, empathy_chat_manager,
47     G_TYPE_OBJECT,
48     G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_CHAT_MANAGER,
49         svc_iface_init)
50     )
51
52 /* private structure */
53 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
54
55 struct _EmpathyChatManagerPriv
56 {
57   EmpathyChatroomManager *chatroom_mgr;
58   /* Queue of (ChatData *) representing the closed chats */
59   GQueue *closed_queue;
60
61   guint num_displayed_chat;
62
63   TpBaseClient *handler;
64 };
65
66 #define GET_PRIV(o) \
67   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
68     EmpathyChatManagerPriv))
69
70 static EmpathyChatManager *chat_manager_singleton = NULL;
71
72 typedef struct
73 {
74   TpAccount *account;
75   gchar *id;
76   gboolean room;
77 } ChatData;
78
79 static ChatData *
80 chat_data_new (EmpathyChat *chat)
81 {
82   ChatData *data = NULL;
83
84   data = g_slice_new0 (ChatData);
85
86   data->account = g_object_ref (empathy_chat_get_account (chat));
87   data->id = g_strdup (empathy_chat_get_id (chat));
88   data->room = empathy_chat_is_room (chat);
89
90   return data;
91 }
92
93 static void
94 chat_data_free (ChatData *data)
95 {
96   if (data->account != NULL)
97     {
98       g_object_unref (data->account);
99       data->account = NULL;
100     }
101
102   if (data->id != NULL)
103     {
104       g_free (data->id);
105       data->id = NULL;
106     }
107
108   g_slice_free (ChatData, data);
109 }
110
111 static void
112 chat_destroyed_cb (gpointer data,
113     GObject *object)
114 {
115   EmpathyChatManager *self = data;
116   EmpathyChatManagerPriv *priv = GET_PRIV (self);
117
118   priv->num_displayed_chat--;
119
120   DEBUG ("Chat destroyed; we are now displaying %u chats",
121       priv->num_displayed_chat);
122
123   g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
124       priv->num_displayed_chat);
125 }
126
127 static void
128 process_tp_chat (EmpathyChatManager *self,
129     EmpathyTpChat *tp_chat,
130     TpAccount *account,
131     gint64 user_action_time)
132 {
133   EmpathyChatManagerPriv *priv = GET_PRIV (self);
134   EmpathyChat *chat = NULL;
135   const gchar *id;
136
137   id = empathy_tp_chat_get_id (tp_chat);
138   if (!tp_str_empty (id))
139     {
140       chat = empathy_chat_window_find_chat (account, id,
141           empathy_tp_chat_is_sms_channel (tp_chat));
142     }
143
144   if (chat != NULL)
145     {
146       empathy_chat_set_tp_chat (chat, tp_chat);
147     }
148   else
149     {
150       chat = empathy_chat_new (tp_chat);
151       /* empathy_chat_new returns a floating reference as EmpathyChat is
152        * a GtkWidget. This reference will be taken by a container
153        * (a GtkNotebook) when we'll call empathy_chat_window_present_chat */
154
155       priv->num_displayed_chat++;
156
157       DEBUG ("Chat displayed; we are now displaying %u chat",
158           priv->num_displayed_chat);
159
160       g_signal_emit (self, signals[DISPLAYED_CHATS_CHANGED], 0,
161           priv->num_displayed_chat);
162
163       g_object_weak_ref ((GObject *) chat, chat_destroyed_cb, self);
164     }
165   empathy_chat_window_present_chat (chat, user_action_time);
166
167   if (empathy_tp_chat_is_invited (tp_chat, NULL))
168     {
169       /* We have been invited to the room. Add ourself as member as this
170        * channel has been approved. */
171       empathy_tp_chat_join (tp_chat);
172     }
173
174   g_object_unref (tp_chat);
175 }
176
177 typedef struct
178 {
179   EmpathyChatManager *self;
180   EmpathyTpChat *tp_chat;
181   TpAccount *account;
182   gint64 user_action_time;
183   gulong sig_id;
184 } chat_ready_ctx;
185
186 static chat_ready_ctx *
187 chat_ready_ctx_new (EmpathyChatManager *self,
188     EmpathyTpChat *tp_chat,
189     TpAccount *account,
190     gint64 user_action_time)
191 {
192   chat_ready_ctx *ctx = g_slice_new0 (chat_ready_ctx);
193
194   ctx->self = g_object_ref (self);
195   ctx->tp_chat = g_object_ref (tp_chat);
196   ctx->account = g_object_ref (account);
197   ctx->user_action_time = user_action_time;
198   return ctx;
199 }
200
201 static void
202 chat_ready_ctx_free (chat_ready_ctx *ctx)
203 {
204   g_object_unref (ctx->self);
205   g_object_unref (ctx->tp_chat);
206   g_object_unref (ctx->account);
207
208   if (ctx->sig_id != 0)
209     g_signal_handler_disconnect (ctx->tp_chat, ctx->sig_id);
210
211   g_slice_free (chat_ready_ctx, ctx);
212 }
213
214 static void
215 tp_chat_ready_cb (GObject *object,
216   GParamSpec *spec,
217   gpointer user_data)
218 {
219   EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (object);
220   chat_ready_ctx *ctx = user_data;
221
222   if (!empathy_tp_chat_is_ready (tp_chat))
223     return;
224
225   process_tp_chat (ctx->self, tp_chat, ctx->account, ctx->user_action_time);
226
227   chat_ready_ctx_free (ctx);
228 }
229
230 static void
231 handle_channels (TpSimpleHandler *handler,
232     TpAccount *account,
233     TpConnection *connection,
234     GList *channels,
235     GList *requests_satisfied,
236     gint64 user_action_time,
237     TpHandleChannelsContext *context,
238     gpointer user_data)
239 {
240   EmpathyChatManager *self = (EmpathyChatManager *) user_data;
241   GList *l;
242
243   for (l = channels; l != NULL; l = g_list_next (l))
244     {
245       TpChannel *channel = l->data;
246       EmpathyTpChat *tp_chat;
247
248       if (tp_proxy_get_invalidated (channel) != NULL)
249         continue;
250
251       if (!TP_IS_TEXT_CHANNEL (channel))
252         {
253           DEBUG ("Channel %s doesn't implement Messages; can't handle it",
254               tp_proxy_get_object_path (channel));
255           continue;
256         }
257
258       DEBUG ("Now handling channel %s", tp_proxy_get_object_path (channel));
259
260       tp_chat = empathy_tp_chat_new (account, channel);
261
262       if (empathy_tp_chat_is_ready (tp_chat))
263         {
264           process_tp_chat (self, tp_chat, account, user_action_time);
265         }
266       else
267         {
268           chat_ready_ctx *ctx = chat_ready_ctx_new (self, tp_chat, account,
269               user_action_time);
270
271           ctx->sig_id = g_signal_connect (tp_chat, "notify::ready",
272               G_CALLBACK (tp_chat_ready_cb), ctx);
273         }
274     }
275
276   tp_handle_channels_context_accept (context);
277 }
278
279 static void
280 empathy_chat_manager_init (EmpathyChatManager *self)
281 {
282   EmpathyChatManagerPriv *priv = GET_PRIV (self);
283   TpDBusDaemon *dbus;
284   GError *error = NULL;
285
286   priv->closed_queue = g_queue_new ();
287
288   dbus = tp_dbus_daemon_dup (&error);
289   if (dbus == NULL)
290     {
291       g_critical ("Failed to get D-Bus daemon: %s", error->message);
292       g_error_free (error);
293       return;
294     }
295
296   priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
297
298   /* Text channels handler */
299   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
300       EMPATHY_CHAT_BUS_NAME_SUFFIX, FALSE, handle_channels, self, NULL);
301
302   /* EmpathyTpChat relies on these features being prepared */
303   tp_base_client_add_connection_features_varargs (priv->handler,
304     TP_CONNECTION_FEATURE_CAPABILITIES, 0);
305   tp_base_client_add_channel_features_varargs (priv->handler,
306       TP_CHANNEL_FEATURE_CHAT_STATES, 0);
307
308   g_object_unref (dbus);
309
310   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
311         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
312         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
313         NULL));
314
315   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
316         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
317         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
318         NULL));
319
320   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
321         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
322         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
323         NULL));
324
325   if (!tp_base_client_register (priv->handler, &error))
326     {
327       g_critical ("Failed to register text handler: %s", error->message);
328       g_error_free (error);
329     }
330 }
331
332 static void
333 empathy_chat_manager_finalize (GObject *object)
334 {
335   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
336   EmpathyChatManagerPriv *priv = GET_PRIV (self);
337
338   if (priv->closed_queue != NULL)
339     {
340       g_queue_foreach (priv->closed_queue, (GFunc) chat_data_free, NULL);
341       g_queue_free (priv->closed_queue);
342       priv->closed_queue = NULL;
343     }
344
345   tp_clear_object (&priv->handler);
346   tp_clear_object (&priv->chatroom_mgr);
347
348   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
349 }
350
351 static GObject *
352 empathy_chat_manager_constructor (GType type,
353     guint n_construct_params,
354     GObjectConstructParam *construct_params)
355 {
356   GObject *retval;
357
358   if (!chat_manager_singleton)
359     {
360       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
361         (type, n_construct_params, construct_params);
362
363       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
364       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
365     }
366   else
367     {
368       retval = g_object_ref (chat_manager_singleton);
369     }
370
371   return retval;
372 }
373
374 static void
375 empathy_chat_manager_constructed (GObject *obj)
376 {
377   TpDBusDaemon *dbus_daemon;
378
379   dbus_daemon = tp_dbus_daemon_dup (NULL);
380
381   if (dbus_daemon != NULL)
382     {
383       tp_dbus_daemon_register_object (dbus_daemon,
384           "/org/gnome/Empathy/ChatManager", obj);
385
386       g_object_unref (dbus_daemon);
387     }
388 }
389
390 static void
391 empathy_chat_manager_class_init (
392   EmpathyChatManagerClass *empathy_chat_manager_class)
393 {
394   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
395
396   object_class->finalize = empathy_chat_manager_finalize;
397   object_class->constructor = empathy_chat_manager_constructor;
398   object_class->constructed = empathy_chat_manager_constructed;
399
400   signals[CLOSED_CHATS_CHANGED] =
401     g_signal_new ("closed-chats-changed",
402         G_TYPE_FROM_CLASS (object_class),
403         G_SIGNAL_RUN_LAST,
404         0,
405         NULL, NULL,
406         g_cclosure_marshal_VOID__UINT,
407         G_TYPE_NONE,
408         1, G_TYPE_UINT, NULL);
409
410   signals[DISPLAYED_CHATS_CHANGED] =
411     g_signal_new ("displayed-chats-changed",
412         G_TYPE_FROM_CLASS (object_class),
413         G_SIGNAL_RUN_LAST,
414         0,
415         NULL, NULL,
416         g_cclosure_marshal_VOID__UINT,
417         G_TYPE_NONE,
418         1, G_TYPE_UINT, NULL);
419
420   g_type_class_add_private (empathy_chat_manager_class,
421     sizeof (EmpathyChatManagerPriv));
422 }
423
424 EmpathyChatManager *
425 empathy_chat_manager_dup_singleton (void)
426 {
427   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
428 }
429
430 void
431 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
432     EmpathyChat *chat)
433 {
434   EmpathyChatManagerPriv *priv = GET_PRIV (self);
435   ChatData *data;
436
437   data = chat_data_new (chat);
438
439   DEBUG ("Adding %s to closed queue: %s",
440       data->room ? "room" : "contact", data->id);
441
442   g_queue_push_tail (priv->closed_queue, data);
443
444   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
445       g_queue_get_length (priv->closed_queue));
446 }
447
448 void
449 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
450 {
451   EmpathyChatManagerPriv *priv = GET_PRIV (self);
452   ChatData *data;
453
454   data = g_queue_pop_tail (priv->closed_queue);
455
456   if (data == NULL)
457     return;
458
459   DEBUG ("Removing %s from closed queue and starting a chat with: %s",
460       data->room ? "room" : "contact", data->id);
461
462   if (data->room)
463     empathy_join_muc (data->account, data->id,
464         TP_USER_ACTION_TIME_NOT_USER_ACTION);
465   else
466     empathy_chat_with_contact_id (data->account, data->id,
467         TP_USER_ACTION_TIME_NOT_USER_ACTION);
468
469   g_signal_emit (self, signals[CLOSED_CHATS_CHANGED], 0,
470       g_queue_get_length (priv->closed_queue));
471
472   chat_data_free (data);
473 }
474
475 guint
476 empathy_chat_manager_get_num_closed_chats (EmpathyChatManager *self)
477 {
478   EmpathyChatManagerPriv *priv = GET_PRIV (self);
479
480   return g_queue_get_length (priv->closed_queue);
481 }
482
483 static void
484 empathy_chat_manager_dbus_undo_closed_chat (EmpSvcChatManager *manager,
485     DBusGMethodInvocation *context)
486 {
487   empathy_chat_manager_undo_closed_chat ((EmpathyChatManager *) manager);
488
489   emp_svc_chat_manager_return_from_undo_closed_chat (context);
490 }
491
492 static void
493 svc_iface_init (gpointer g_iface,
494     gpointer iface_data)
495 {
496   EmpSvcChatManagerClass *klass = (EmpSvcChatManagerClass *) g_iface;
497
498 #define IMPLEMENT(x) emp_svc_chat_manager_implement_##x (\
499     klass, empathy_chat_manager_dbus_##x)
500   IMPLEMENT(undo_closed_chat);
501 #undef IMPLEMENT
502 }
503
504 void
505 empathy_chat_manager_call_undo_closed_chat (void)
506 {
507   TpDBusDaemon *dbus_daemon = tp_dbus_daemon_dup (NULL);
508   TpProxy *proxy;
509
510   if (dbus_daemon == NULL)
511     return;
512
513   proxy = g_object_new (TP_TYPE_PROXY,
514       "dbus-daemon", dbus_daemon,
515       "dbus-connection", tp_proxy_get_dbus_connection (TP_PROXY (dbus_daemon)),
516       "bus-name", EMPATHY_CHAT_BUS_NAME,
517       "object-path", "/org/gnome/Empathy/ChatManager",
518       NULL);
519
520   tp_proxy_add_interface_by_id (proxy, EMP_IFACE_QUARK_CHAT_MANAGER);
521
522   emp_cli_chat_manager_call_undo_closed_chat (proxy, -1, NULL, NULL, NULL, NULL);
523
524   g_object_unref (proxy);
525   g_object_unref (dbus_daemon);
526 }