]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Handle incoming file transfers in the event manager. (Jonny Lamb)
[empathy.git] / src / empathy-event-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 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  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #include <telepathy-glib/util.h>
28
29 #include <libempathy/empathy-dispatcher.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-chat.h>
32 #include <libempathy/empathy-tp-group.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-file.h>
35
36 #include <libempathy-gtk/empathy-ft-manager.h>
37 #include <libempathy-gtk/empathy-images.h>
38 #include <libempathy-gtk/empathy-contact-dialogs.h>
39
40 #include "empathy-event-manager.h"
41
42 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
43 #include <libempathy/empathy-debug.h>
44
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
46 typedef struct {
47         EmpathyDispatcher     *dispatcher;
48         EmpathyContactManager *contact_manager;
49         GSList                *events;
50 } EmpathyEventManagerPriv;
51
52 typedef struct _EventPriv EventPriv;
53 typedef void (*EventFunc) (EventPriv *event);
54
55 struct _EventPriv {
56         EmpathyEvent         public;
57         TpChannel           *channel;
58         EmpathyEventManager *manager;
59         EventFunc            func;
60         gpointer             user_data;
61 };
62
63 enum {
64         EVENT_ADDED,
65         EVENT_REMOVED,
66         LAST_SIGNAL
67 };
68
69 static guint signals[LAST_SIGNAL];
70
71 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
72
73 static void event_remove (EventPriv *event);
74
75 static void
76 event_free (EventPriv *event)
77 {
78         g_free (event->public.icon_name);
79         g_free (event->public.message);
80
81         if (event->public.contact) {
82                 g_object_unref (event->public.contact);
83         }
84
85         if (event->channel) {
86                 g_signal_handlers_disconnect_by_func (event->channel,
87                                                       event_remove,
88                                                       event);
89                 g_object_unref (event->channel);
90         }
91         g_slice_free (EventPriv, event);
92 }
93
94 static void
95 event_remove (EventPriv *event)
96 {
97         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
98
99         DEBUG ("Removing event %p", event);
100         priv->events = g_slist_remove (priv->events, event);
101         g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
102         event_free (event);
103 }
104
105 static void
106 event_manager_add (EmpathyEventManager *manager,
107                    EmpathyContact      *contact,
108                    const gchar         *icon_name,
109                    const gchar         *message,
110                    TpChannel           *channel,
111                    EventFunc            func,
112                    gpointer             user_data)
113 {
114         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
115         EventPriv               *event;
116
117         event = g_slice_new0 (EventPriv);
118         event->public.contact = contact ? g_object_ref (contact) : NULL;
119         event->public.icon_name = g_strdup (icon_name);
120         event->public.message = g_strdup (message);
121         event->manager = manager;
122         event->func = func;
123         event->user_data = user_data;
124
125         if (channel) {
126                 event->channel = g_object_ref (channel);
127                 g_signal_connect_swapped (channel, "invalidated",
128                                           G_CALLBACK (event_remove),
129                                           event);
130         }
131
132         DEBUG ("Adding event %p", event);
133         priv->events = g_slist_prepend (priv->events, event);
134         g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
135 }
136
137 static void
138 event_channel_process_func (EventPriv *event)
139 {
140         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
141
142         /* This will emit "dispatch-channel" and the event will be removed
143          * in the callback of that signal, no need to remove the event here. */ 
144         empathy_dispatcher_channel_process (priv->dispatcher, event->channel);
145 }
146
147 static gboolean
148 event_manager_chat_unref_idle (gpointer user_data)
149 {
150         g_object_unref (user_data);
151         return FALSE;
152 }
153
154 static void
155 event_manager_chat_message_received_cb (EmpathyTpChat       *tp_chat,
156                                         EmpathyMessage      *message,
157                                         EmpathyEventManager *manager)
158 {
159         EmpathyContact  *sender;
160         gchar           *msg;
161         TpChannel       *channel;
162
163         g_idle_add (event_manager_chat_unref_idle, tp_chat);
164         g_signal_handlers_disconnect_by_func (tp_chat,
165                                               event_manager_chat_message_received_cb,
166                                               manager);
167
168         sender = empathy_message_get_sender (message);
169         msg = g_strdup_printf (_("New message from %s:\n%s"),
170                                empathy_contact_get_name (sender),
171                                empathy_message_get_body (message));
172
173         channel = empathy_tp_chat_get_channel (tp_chat);
174         event_manager_add (manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
175                            channel, event_channel_process_func, NULL);
176
177         g_free (msg);
178 }
179
180 static void
181 event_manager_filter_channel_cb (EmpathyDispatcher   *dispatcher,
182                                  TpChannel           *channel,
183                                  EmpathyEventManager *manager)
184 {
185         gchar *channel_type;
186
187         g_object_get (channel, "channel-type", &channel_type, NULL);
188         if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
189                 EmpathyTpChat *tp_chat;
190
191                 tp_chat = empathy_tp_chat_new (channel);
192                 g_signal_connect (tp_chat, "message-received",
193                                   G_CALLBACK (event_manager_chat_message_received_cb),
194                                   manager);
195         }
196         else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
197                 EmpathyTpGroup *tp_group;
198                 EmpathyContact *contact;
199                 gchar          *msg;
200
201                 tp_group = empathy_tp_group_new (channel);
202                 empathy_run_until_ready (tp_group);
203                 empathy_tp_group_get_invitation (tp_group, &contact);
204                 empathy_contact_run_until_ready (contact,
205                                                  EMPATHY_CONTACT_READY_NAME,
206                                                  NULL);
207
208                 msg = g_strdup_printf (_("Incoming call from %s"),
209                                        empathy_contact_get_name (contact));
210
211                 event_manager_add (manager, contact, EMPATHY_IMAGE_VOIP, msg,
212                                    channel, event_channel_process_func, NULL);
213
214                 g_free (msg);
215                 g_object_unref (contact);
216                 g_object_unref (tp_group);
217         }
218         else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE)) {
219                 GValue *direction;
220
221                 tp_cli_dbus_properties_run_get (channel,
222                                                 -1,
223                                                 EMP_IFACE_CHANNEL_TYPE_FILE,
224                                                 "Direction",
225                                                 &direction,
226                                                 NULL,
227                                                 NULL);
228
229                 /* Only deal with incoming channels */
230                 if (g_value_get_uint (direction) == EMP_FILE_TRANSFER_DIRECTION_INCOMING) {
231                         EmpathyFTManager *manager;
232                         McAccount        *account;
233                         EmpathyFile      *file;
234
235                         manager = empathy_ft_manager_get_default ();
236                         account = empathy_channel_get_account (channel);
237
238                         file = empathy_file_new (account, channel);
239
240                         empathy_ft_manager_add_file (manager, file);
241                 }
242
243                 g_value_unset (direction);
244         }
245
246         g_free (channel_type);
247 }
248
249 static void
250 event_manager_dispatch_channel_cb (EmpathyDispatcher   *dispatcher,
251                                    TpChannel           *channel,
252                                    EmpathyEventManager *manager)
253 {
254         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
255         GSList                  *l;
256
257         for (l = priv->events; l; l = l->next) {
258                 EventPriv *event = l->data;
259
260                 if (event->channel &&
261                     empathy_proxy_equal (channel, event->channel)) {
262                         event_remove (event);
263                         break;
264                 }
265         }
266 }
267
268 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
269                               "you don't have the needed external " \
270                               "application to handle it.")
271
272 static void
273 event_tube_process_func (EventPriv *event)
274 {
275         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
276         EmpathyDispatcherTube   *tube = (EmpathyDispatcherTube*) event->user_data;
277
278         if (tube->activatable) {
279                 empathy_dispatcher_tube_process (priv->dispatcher, tube);
280         } else {
281                 GtkWidget *dialog;
282                 gchar     *str;
283
284                 /* Tell the user that the tube can't be handled */
285                 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
286                                        empathy_contact_get_name (tube->initiator));
287
288                 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
289                                                  GTK_MESSAGE_ERROR,
290                                                  GTK_BUTTONS_OK,
291                                                  "%s", str);
292                 gtk_window_set_title (GTK_WINDOW (dialog),
293                                       _("Invitation Error"));
294                 g_free (str);
295
296                 gtk_widget_show (dialog);
297                 g_signal_connect (dialog, "response",
298                                   G_CALLBACK (gtk_widget_destroy),
299                                   NULL);
300         }
301
302         empathy_dispatcher_tube_unref (tube);
303         event_remove (event);
304 }
305
306 static void
307 event_manager_filter_tube_cb (EmpathyDispatcher     *dispatcher,
308                               EmpathyDispatcherTube *tube,
309                               EmpathyEventManager   *manager)
310 {
311         const gchar *icon_name;
312         gchar       *msg;
313
314         empathy_contact_run_until_ready (tube->initiator,
315                                          EMPATHY_CONTACT_READY_NAME, NULL);
316
317         if (tube->activatable) {
318                 icon_name = GTK_STOCK_EXECUTE;
319                 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
320                                          "application will be started to handle it."),
321                                        empathy_contact_get_name (tube->initiator));
322         } else {
323                 icon_name = GTK_STOCK_DIALOG_ERROR;
324                 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
325                                        empathy_contact_get_name (tube->initiator));
326         }
327
328         event_manager_add (manager, tube->initiator, icon_name, msg,
329                            tube->channel, event_tube_process_func,
330                            empathy_dispatcher_tube_ref (tube));
331
332         g_free (msg);
333 }
334
335 static void
336 event_pending_subscribe_func (EventPriv *event)
337 {
338         empathy_subscription_dialog_show (event->public.contact, NULL);
339         event_remove (event);
340 }
341
342 static void
343 event_manager_pendings_changed_cb (EmpathyContactList  *list,
344                                    EmpathyContact      *contact,
345                                    EmpathyContact      *actor,
346                                    guint                reason,
347                                    gchar               *message,
348                                    gboolean             is_pending,
349                                    EmpathyEventManager *manager)
350 {
351         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
352         GString                 *str;
353
354         if (!is_pending) {
355                 GSList *l;
356
357                 for (l = priv->events; l; l = l->next) {
358                         EventPriv *event = l->data;
359
360                         if (event->public.contact == contact &&
361                             event->func == event_pending_subscribe_func) {
362                                 event_remove (event);
363                                 break;
364                         }
365                 }
366
367                 return;
368         }
369
370         empathy_contact_run_until_ready (contact,
371                                          EMPATHY_CONTACT_READY_NAME,
372                                          NULL);
373
374         str = g_string_new (NULL);
375         g_string_printf (str, _("Subscription requested by %s"),
376                          empathy_contact_get_name (contact));   
377         if (!G_STR_EMPTY (message)) {
378                 g_string_append_printf (str, _("\nMessage: %s"), message);
379         }
380
381         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
382                            NULL, event_pending_subscribe_func, NULL);
383
384         g_string_free (str, TRUE);
385 }
386
387 static void
388 event_manager_finalize (GObject *object)
389 {
390         EmpathyEventManagerPriv *priv = GET_PRIV (object);
391
392         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
393         g_slist_free (priv->events);
394         g_object_unref (priv->contact_manager);
395         g_object_unref (priv->dispatcher);
396 }
397
398 static void
399 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
400 {
401         GObjectClass *object_class = G_OBJECT_CLASS (klass);
402
403         object_class->finalize = event_manager_finalize;
404
405         signals[EVENT_ADDED] =
406                 g_signal_new ("event-added",
407                               G_TYPE_FROM_CLASS (klass),
408                               G_SIGNAL_RUN_LAST,
409                               0,
410                               NULL, NULL,
411                               g_cclosure_marshal_VOID__POINTER,
412                               G_TYPE_NONE,
413                               1, G_TYPE_POINTER);
414
415         signals[EVENT_REMOVED] =
416                 g_signal_new ("event-removed",
417                               G_TYPE_FROM_CLASS (klass),
418                               G_SIGNAL_RUN_LAST,
419                               0,
420                               NULL, NULL,
421                               g_cclosure_marshal_VOID__POINTER,
422                               G_TYPE_NONE,
423                               1, G_TYPE_POINTER);
424
425         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
426 }
427
428 static void
429 empathy_event_manager_init (EmpathyEventManager *manager)
430 {
431         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
432                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
433
434         manager->priv = priv;
435
436         priv->dispatcher = empathy_dispatcher_new ();
437         priv->contact_manager = empathy_contact_manager_new ();
438         g_signal_connect (priv->dispatcher, "filter-channel",
439                           G_CALLBACK (event_manager_filter_channel_cb),
440                           manager);
441         g_signal_connect (priv->dispatcher, "dispatch-channel",
442                           G_CALLBACK (event_manager_dispatch_channel_cb),
443                           manager);
444         g_signal_connect (priv->dispatcher, "filter-tube",
445                           G_CALLBACK (event_manager_filter_tube_cb),
446                           manager);
447         g_signal_connect (priv->contact_manager, "pendings-changed",
448                           G_CALLBACK (event_manager_pendings_changed_cb),
449                           manager);
450 }
451
452 EmpathyEventManager *
453 empathy_event_manager_new (void)
454 {
455         static EmpathyEventManager *manager = NULL;
456
457         if (!manager) {
458                 manager = g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
459                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
460         } else {
461                 g_object_ref (manager);
462         }
463
464         return manager;
465 }
466
467 GSList *
468 empathy_event_manager_get_events (EmpathyEventManager *manager)
469 {
470         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
471
472         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
473
474         return priv->events;
475 }
476
477 EmpathyEvent *
478 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
479 {
480         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
481
482         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
483
484         return priv->events ? priv->events->data : NULL;
485 }
486
487 void
488 empathy_event_activate (EmpathyEvent *event_public)
489 {
490         EventPriv *event = (EventPriv*) event_public;
491
492         g_return_if_fail (event_public != NULL);
493
494         if (event->func) {
495                 event->func (event);
496         } else {
497                 event_remove (event);
498         }
499 }
500