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