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