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