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