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