]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Handle Dbus Tubes and ignore non p2p channels
[empathy.git] / src / empathy-event-manager.c
1 /*
2  * Copyright (C) 2007-2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-tp-chat.h>
33 #include <libempathy/empathy-tp-call.h>
34 #include <libempathy/empathy-tp-group.h>
35 #include <libempathy/empathy-utils.h>
36
37 #include <extensions/extensions.h>
38
39 #include <libempathy-gtk/empathy-images.h>
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
41
42 #include "empathy-event-manager.h"
43 #include "empathy-tube-dispatch.h"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
49
50 typedef struct {
51   EmpathyEventManager *manager;
52   EmpathyDispatchOperation *operation;
53   gulong approved_handler;
54   gulong claimed_handler;
55   gulong invalidated_handler;
56   /* Remove contact if applicable */
57   EmpathyContact *contact;
58   /* Tube dispatcher if applicable */
59   EmpathyTubeDispatch *tube_dispatch;
60   /* option signal handler */
61   gulong handler;
62 } EventManagerApproval;
63
64 typedef struct {
65   EmpathyDispatcher *dispatcher;
66   EmpathyContactManager *contact_manager;
67   GSList *events;
68   /* Ongoing approvals */
69   GSList *approvals;
70 } EmpathyEventManagerPriv;
71
72 typedef struct _EventPriv EventPriv;
73 typedef void (*EventFunc) (EventPriv *event);
74
75 struct _EventPriv {
76   EmpathyEvent public;
77   EmpathyEventManager *manager;
78   EventManagerApproval *approval;
79   EventFunc func;
80   gpointer user_data;
81 };
82
83 enum {
84   EVENT_ADDED,
85   EVENT_REMOVED,
86   LAST_SIGNAL
87 };
88
89 static guint signals[LAST_SIGNAL];
90
91 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
92
93 static EmpathyEventManager * manager_singleton = NULL;
94
95 static EventManagerApproval *
96 event_manager_approval_new (EmpathyEventManager *manager,
97   EmpathyDispatchOperation *operation)
98 {
99   EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
100   result->operation = g_object_ref (operation);
101   result->manager = manager;
102
103   return result;
104 }
105
106 static void
107 event_manager_approval_free (EventManagerApproval *approval)
108 {
109   g_signal_handler_disconnect (approval->operation,
110     approval->approved_handler);
111   g_signal_handler_disconnect (approval->operation,
112     approval->claimed_handler);
113   g_signal_handler_disconnect (approval->operation,
114     approval->invalidated_handler);
115   g_object_unref (approval->operation);
116
117   if (approval->contact != NULL)
118     g_object_unref (approval->contact);
119
120   if (approval->tube_dispatch != NULL)
121     g_object_unref (approval->tube_dispatch);
122
123   g_slice_free (EventManagerApproval, approval);
124 }
125
126 static void event_remove (EventPriv *event);
127
128 static void
129 event_free (EventPriv *event)
130 {
131   g_free (event->public.icon_name);
132   g_free (event->public.message);
133
134   if (event->public.contact)
135     {
136       g_object_unref (event->public.contact);
137     }
138
139   g_slice_free (EventPriv, event);
140 }
141
142 static void
143 event_remove (EventPriv *event)
144 {
145   EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
146
147   DEBUG ("Removing event %p", event);
148   priv->events = g_slist_remove (priv->events, event);
149   g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
150   event_free (event);
151 }
152
153 static void
154 event_manager_add (EmpathyEventManager *manager, EmpathyContact      *contact,
155   const gchar         *icon_name, const gchar         *message,
156   EventManagerApproval *approval, EventFunc            func,
157   gpointer             user_data)
158 {
159   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
160   EventPriv               *event;
161
162   event = g_slice_new0 (EventPriv);
163   event->public.contact = contact ? g_object_ref (contact) : NULL;
164   event->public.icon_name = g_strdup (icon_name);
165   event->public.message = g_strdup (message);
166   event->func = func;
167   event->user_data = user_data;
168   event->manager = manager;
169
170   if (approval)
171     event->approval = approval;
172
173   DEBUG ("Adding event %p", event);
174   priv->events = g_slist_prepend (priv->events, event);
175   g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
176 }
177
178 static void
179 event_channel_process_func (EventPriv *event)
180 {
181   empathy_dispatch_operation_approve (event->approval->operation);
182 }
183
184 static void
185 event_manager_chat_message_received_cb (EmpathyTpChat       *tp_chat,
186   EmpathyMessage      *message, EventManagerApproval *approval)
187 {
188   EmpathyContact  *sender;
189   gchar           *msg;
190   TpChannel       *channel;
191
192   g_signal_handlers_disconnect_by_func (tp_chat,
193     event_manager_chat_message_received_cb, approval);
194
195   sender = empathy_message_get_sender (message);
196   msg = g_strdup_printf (_("New message from %s:\n%s"),
197     empathy_contact_get_name (sender), empathy_message_get_body (message));
198
199   channel = empathy_tp_chat_get_channel (tp_chat);
200   event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
201     approval, event_channel_process_func, NULL);
202
203   g_free (msg);
204 }
205
206 static void
207 event_manager_approval_done (EventManagerApproval *approval)
208 {
209   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
210   GSList                  *l;
211
212   priv->approvals = g_slist_remove (priv->approvals, approval);
213
214   for (l = priv->events; l; l = l->next)
215     {
216       EventPriv *event = l->data;
217
218       if (event->approval == approval)
219         {
220           event_remove (event);
221           break;
222         }
223     }
224
225   event_manager_approval_free (approval);
226 }
227
228 static void
229 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
230   EventManagerApproval *approval)
231 {
232   event_manager_approval_done (approval);
233 }
234
235 static void
236 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
237   EventManagerApproval *approval)
238 {
239   event_manager_approval_done (approval);
240 }
241
242 static void
243 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
244   guint domain, gint code, gchar *message,
245   EventManagerApproval *approval)
246 {
247   event_manager_approval_done (approval);
248 }
249
250 static void
251 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
252   const GError *error, gpointer user_data, GObject *object)
253 {
254   EventManagerApproval *approval = user_data;
255   gchar *msg;
256
257   if (error != NULL)
258     {
259       /* FIXME just returning assuming the operation will be invalidated as
260        * well */
261       return;
262     }
263
264   msg = g_strdup_printf (_("Incoming call from %s"),
265     empathy_contact_get_name (contact));
266
267   event_manager_add (approval->manager,
268     approval->contact, EMPATHY_IMAGE_VOIP, msg,
269     approval, event_channel_process_func, NULL);
270
271   g_free (msg);
272 }
273
274 static void
275 event_manager_media_channel_got_contact (EventManagerApproval *approval)
276 {
277   empathy_contact_call_when_ready (approval->contact,
278      EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
279         approval, NULL, G_OBJECT (approval->manager));
280 }
281
282 static void
283 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
284   GParamSpec *param, EventManagerApproval *approval)
285 {
286   EmpathyContact *contact;
287
288   g_object_get (G_OBJECT (call), "contact", &contact, NULL);
289
290   if (contact == NULL)
291     return;
292
293   approval->contact = contact;
294   event_manager_media_channel_got_contact (approval);
295 }
296
297 static void
298 event_manager_tube_approved_cb (EventPriv *event)
299 {
300   empathy_tube_dispatch_handle (event->approval->tube_dispatch);
301 }
302
303 static void
304 event_manager_add_tube_approval (EventManagerApproval *approval,
305   EmpathyTubeDispatchAbility ability)
306 {
307   const gchar *icon_name;
308   gchar       *msg;
309
310   if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
311     {
312       icon_name = GTK_STOCK_EXECUTE;
313       msg = g_strdup_printf (_("%s is offering you an invitation."
314         " An external application will be started to handle it."),
315       empathy_contact_get_name (approval->contact));
316     }
317   else
318     {
319       icon_name = GTK_STOCK_DIALOG_ERROR;
320       msg = g_strdup_printf (_("%s is offering you an invitation, but "
321         "you don't have the needed external "
322         "application to handle it."),
323         empathy_contact_get_name (approval->contact));
324     }
325
326   event_manager_add (approval->manager, approval->contact, icon_name, msg,
327     approval, event_manager_tube_approved_cb, approval);
328
329   g_free (msg);
330 }
331
332 static void
333 event_manager_tube_dispatch_ability_cb (GObject *object,
334    GParamSpec *spec, gpointer user_data)
335 {
336   EventManagerApproval *approval = (EventManagerApproval *)user_data;
337   EmpathyTubeDispatchAbility dispatchability;
338
339   dispatchability =
340     empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
341
342   if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
343     {
344       event_manager_add_tube_approval (approval, dispatchability);
345       g_signal_handler_disconnect (object, approval->handler);
346       approval->handler = 0;
347     }
348 }
349
350 static void
351 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
352   const GError *error, gpointer user_data, GObject *object)
353 {
354   EventManagerApproval *approval = (EventManagerApproval *)user_data;
355   EmpathyTubeDispatchAbility dispatchability;
356
357   if (error != NULL)
358     {
359       /* FIXME?, we assume that the operation gets invalidated as well (if it
360        * didn't already */
361        return;
362     }
363
364   dispatchability = empathy_tube_dispatch_is_dispatchable
365     (approval->tube_dispatch);
366
367
368   switch (dispatchability)
369     {
370       case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
371         approval->handler = g_signal_connect (approval->tube_dispatch,
372           "notify::dispatchability",
373           G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
374         break;
375       case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
376         /* fallthrough */
377       case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
378         event_manager_add_tube_approval (approval, dispatchability);
379         break;
380     }
381 }
382
383 static void
384 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
385   EmpathyDispatchOperation  *operation, EmpathyEventManager *manager)
386 {
387   const gchar *channel_type;
388   EventManagerApproval *approval;
389   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
390
391   channel_type = empathy_dispatch_operation_get_channel_type (operation);
392
393   approval = event_manager_approval_new (manager, operation);
394   priv->approvals = g_slist_prepend (priv->approvals, approval);
395
396   approval->approved_handler = g_signal_connect (operation, "approved",
397     G_CALLBACK (event_manager_operation_approved_cb), approval);
398
399   approval->claimed_handler = g_signal_connect (operation, "claimed",
400      G_CALLBACK (event_manager_operation_claimed_cb), approval);
401
402   approval->invalidated_handler = g_signal_connect (operation, "invalidated",
403      G_CALLBACK (event_manager_operation_invalidated_cb), approval);
404
405   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
406     {
407       EmpathyTpChat *tp_chat =
408         EMPATHY_TP_CHAT (
409           empathy_dispatch_operation_get_channel_wrapper (operation));
410
411       g_signal_connect (tp_chat, "message-received",
412         G_CALLBACK (event_manager_chat_message_received_cb), approval);
413
414     }
415   else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
416     {
417       EmpathyContact *contact;
418       EmpathyTpCall *call = EMPATHY_TP_CALL (
419           empathy_dispatch_operation_get_channel_wrapper (operation));
420
421       g_object_get (G_OBJECT (call), "contact", &contact, NULL);
422
423       if (contact == NULL)
424         {
425           g_signal_connect (call, "notify::contact",
426             G_CALLBACK (event_manager_media_channel_contact_changed_cb),
427             approval);
428         }
429       else
430         {
431           approval->contact = contact;
432           event_manager_media_channel_got_contact (approval);
433         }
434
435     }
436   else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
437     {
438       EmpathyContact        *contact;
439       gchar                 *msg;
440       TpHandle               handle;
441       McAccount             *account;
442       EmpathyContactFactory *factory;
443       TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
444
445       factory = empathy_contact_factory_dup_singleton ();
446       handle = tp_channel_get_handle (channel, NULL);
447       account = empathy_channel_get_account (channel);
448
449       contact = empathy_contact_factory_get_from_handle (factory, account,
450         handle);
451
452       empathy_contact_run_until_ready (contact,
453         EMPATHY_CONTACT_READY_NAME, NULL);
454
455       msg = g_strdup_printf (_("Incoming file transfer from %s"),
456         empathy_contact_get_name (contact));
457
458       event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
459         msg, approval, event_channel_process_func, NULL);
460
461       g_object_unref (factory);
462       g_object_unref (account);
463     }
464   else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
465       !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
466     {
467       EmpathyContact        *contact;
468       TpHandle               handle;
469       TpHandleType           handle_type;
470       McAccount             *account;
471       EmpathyContactFactory *factory;
472       EmpathyTubeDispatch *tube_dispatch;
473       TpChannel *channel;
474
475       channel = empathy_dispatch_operation_get_channel (operation);
476
477       handle = tp_channel_get_handle (channel, &handle_type);
478
479       /* Only understand p2p tubes */
480       if (handle_type != TP_HANDLE_TYPE_CONTACT)
481         return;
482
483       factory = empathy_contact_factory_new ();
484       account = empathy_channel_get_account (channel);
485
486       contact = empathy_contact_factory_get_from_handle (factory, account,
487         handle);
488
489       tube_dispatch = empathy_tube_dispatch_new (operation);
490
491       approval->contact = contact;
492       approval->tube_dispatch = tube_dispatch;
493
494       empathy_contact_call_when_ready (contact,
495         EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
496         approval, NULL, G_OBJECT (manager));
497
498       g_object_unref (factory);
499       g_object_unref (account);
500     }
501   else
502     {
503       DEBUG ("Unknown channel type, ignoring..");
504     }
505 }
506
507 static void
508 event_pending_subscribe_func (EventPriv *event)
509 {
510   empathy_subscription_dialog_show (event->public.contact, NULL);
511   event_remove (event);
512 }
513
514 static void
515 event_manager_pendings_changed_cb (EmpathyContactList  *list,
516   EmpathyContact *contact, EmpathyContact *actor,
517   guint reason, gchar *message, gboolean is_pending,
518   EmpathyEventManager *manager)
519 {
520   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
521   GString                 *str;
522
523   if (!is_pending)
524     {
525       GSList *l;
526
527       for (l = priv->events; l; l = l->next)
528         {
529           EventPriv *event = l->data;
530
531       if (event->public.contact == contact &&
532           event->func == event_pending_subscribe_func)
533         {
534           event_remove (event);
535           break;
536         }
537       }
538
539       return;
540     }
541
542   empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
543
544   str = g_string_new (NULL);
545   g_string_printf (str, _("Subscription requested by %s"),
546     empathy_contact_get_name (contact));
547
548   if (!G_STR_EMPTY (message))
549     g_string_append_printf (str, _("\nMessage: %s"), message);
550
551   event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
552     NULL, event_pending_subscribe_func, NULL);
553
554   g_string_free (str, TRUE);
555 }
556
557 static GObject *
558 event_manager_constructor (GType type,
559                            guint n_props,
560                            GObjectConstructParam *props)
561 {
562         GObject *retval;
563
564         if (manager_singleton) {
565                 retval = g_object_ref (manager_singleton);
566         } else {
567                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
568                         (type, n_props, props);
569
570                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
571                 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
572         }
573
574         return retval;
575 }
576
577 static void
578 event_manager_finalize (GObject *object)
579 {
580   EmpathyEventManagerPriv *priv = GET_PRIV (object);
581
582   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
583   g_slist_free (priv->events);
584   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
585   g_slist_free (priv->approvals);
586   g_object_unref (priv->contact_manager);
587   g_object_unref (priv->dispatcher);
588 }
589
590 static void
591 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
592 {
593   GObjectClass *object_class = G_OBJECT_CLASS (klass);
594
595   object_class->finalize = event_manager_finalize;
596   object_class->constructor = event_manager_constructor;
597
598   signals[EVENT_ADDED] =
599     g_signal_new ("event-added",
600       G_TYPE_FROM_CLASS (klass),
601       G_SIGNAL_RUN_LAST,
602       0,
603       NULL, NULL,
604       g_cclosure_marshal_VOID__POINTER,
605       G_TYPE_NONE,
606       1, G_TYPE_POINTER);
607
608   signals[EVENT_REMOVED] =
609   g_signal_new ("event-removed",
610       G_TYPE_FROM_CLASS (klass),
611       G_SIGNAL_RUN_LAST,
612       0,
613       NULL, NULL,
614       g_cclosure_marshal_VOID__POINTER,
615       G_TYPE_NONE, 1, G_TYPE_POINTER);
616
617   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
618 }
619
620 static void
621 empathy_event_manager_init (EmpathyEventManager *manager)
622 {
623   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
624     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
625
626   manager->priv = priv;
627
628   priv->dispatcher = empathy_dispatcher_dup_singleton ();
629   priv->contact_manager = empathy_contact_manager_dup_singleton ();
630   g_signal_connect (priv->dispatcher, "approve",
631     G_CALLBACK (event_manager_approve_channel_cb), manager);
632   g_signal_connect (priv->contact_manager, "pendings-changed",
633     G_CALLBACK (event_manager_pendings_changed_cb), manager);
634 }
635
636 EmpathyEventManager *
637 empathy_event_manager_dup_singleton (void)
638 {
639   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
640 }
641
642 GSList *
643 empathy_event_manager_get_events (EmpathyEventManager *manager)
644 {
645   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
646
647   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
648
649   return priv->events;
650 }
651
652 EmpathyEvent *
653 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
654 {
655   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
656
657   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
658
659   return priv->events ? priv->events->data : NULL;
660 }
661
662 void
663 empathy_event_activate (EmpathyEvent *event_public)
664 {
665   EventPriv *event = (EventPriv*) event_public;
666
667   g_return_if_fail (event_public != NULL);
668
669   if (event->func)
670     event->func (event);
671   else
672     event_remove (event);
673 }