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