]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
EmpathyCallFactory -> EmpathyStreamedMediaFactory
[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/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/simple-approver.h>
31
32 #include <libempathy/empathy-presence-manager.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-streamed-media.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-gsettings.h>
40
41 #include <extensions/extensions.h>
42
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-contact-dialogs.h>
45 #include <libempathy-gtk/empathy-sound-manager.h>
46
47 #include "empathy-event-manager.h"
48 #include "empathy-main-window.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
51 #include <libempathy/empathy-debug.h>
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
54
55 #define NOTIFICATION_TIMEOUT 2 /* seconds */
56
57 /* The time interval in milliseconds between 2 incoming rings */
58 #define MS_BETWEEN_RING 500
59
60 typedef struct {
61   EmpathyEventManager *manager;
62   TpChannelDispatchOperation *operation;
63   gulong invalidated_handler;
64   /* Remove contact if applicable */
65   EmpathyContact *contact;
66   /* option signal handler and it's instance */
67   gulong handler;
68   GObject *handler_instance;
69   /* optional accept widget */
70   GtkWidget *dialog;
71   /* Channel of the CDO that will be used during the approval */
72   TpChannel *main_channel;
73   gboolean auto_approved;
74 } EventManagerApproval;
75
76 typedef struct {
77   TpBaseClient *approver;
78   TpBaseClient *auth_approver;
79   EmpathyContactManager *contact_manager;
80   GSList *events;
81   /* Ongoing approvals */
82   GSList *approvals;
83
84   gint ringing;
85
86   GSettings *gsettings_notif;
87   GSettings *gsettings_ui;
88
89   EmpathySoundManager *sound_mgr;
90 } EmpathyEventManagerPriv;
91
92 typedef struct _EventPriv EventPriv;
93 typedef void (*EventFunc) (EventPriv *event);
94
95 struct _EventPriv {
96   EmpathyEvent public;
97   EmpathyEventManager *manager;
98   EventManagerApproval *approval;
99   EventFunc func;
100   gboolean inhibit;
101   gpointer user_data;
102   guint autoremove_timeout_id;
103 };
104
105 enum {
106   EVENT_ADDED,
107   EVENT_REMOVED,
108   EVENT_UPDATED,
109   LAST_SIGNAL
110 };
111
112 static guint signals[LAST_SIGNAL];
113
114 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
115
116 static EmpathyEventManager * manager_singleton = NULL;
117
118 static EventManagerApproval *
119 event_manager_approval_new (EmpathyEventManager *manager,
120   TpChannelDispatchOperation *operation,
121   TpChannel *main_channel)
122 {
123   EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
124   result->operation = g_object_ref (operation);
125   result->manager = manager;
126   result->main_channel = g_object_ref (main_channel);
127
128   return result;
129 }
130
131 static void
132 event_manager_approval_free (EventManagerApproval *approval)
133 {
134   g_signal_handler_disconnect (approval->operation,
135     approval->invalidated_handler);
136   g_object_unref (approval->operation);
137
138   g_object_unref (approval->main_channel);
139
140   if (approval->handler != 0)
141     g_signal_handler_disconnect (approval->handler_instance,
142       approval->handler);
143
144   if (approval->handler_instance != NULL)
145     g_object_unref (approval->handler_instance);
146
147   if (approval->contact != NULL)
148     g_object_unref (approval->contact);
149
150   if (approval->dialog != NULL)
151     {
152       gtk_widget_destroy (approval->dialog);
153     }
154
155   g_slice_free (EventManagerApproval, approval);
156 }
157
158 static void
159 event_free (EventPriv *event)
160 {
161   g_free (event->public.icon_name);
162   g_free (event->public.header);
163   g_free (event->public.message);
164
165   if (event->autoremove_timeout_id != 0)
166     g_source_remove (event->autoremove_timeout_id);
167
168   tp_clear_object (&(event->public.contact));
169   tp_clear_object (&(event->public.account));
170
171   g_slice_free (EventPriv, event);
172 }
173
174 static void
175 event_remove (EventPriv *event)
176 {
177   EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
178
179   DEBUG ("Removing event %p", event);
180
181   priv->events = g_slist_remove (priv->events, event);
182   g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
183   event_free (event);
184 }
185
186 void
187 empathy_event_remove (EmpathyEvent *event_public)
188 {
189   EventPriv *event = (EventPriv *) event_public;
190
191   event_remove (event);
192 }
193
194 static gboolean
195 autoremove_event_timeout_cb (EventPriv *event)
196 {
197   event->autoremove_timeout_id = 0;
198   event_remove (event);
199   return FALSE;
200 }
201
202 static gboolean
203 display_notify_area (EmpathyEventManager *self)
204 {
205   EmpathyEventManagerPriv *priv = GET_PRIV (self);
206
207   return g_settings_get_boolean (priv->gsettings_ui,
208       EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
209 }
210
211 static void
212 event_manager_add (EmpathyEventManager *manager,
213     TpAccount *account,
214     EmpathyContact *contact,
215     EmpathyEventType type,
216     const gchar *icon_name,
217     const gchar *header,
218     const gchar *message,
219     EventManagerApproval *approval,
220     EventFunc func,
221     gpointer user_data)
222 {
223   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
224   EventPriv               *event;
225
226   event = g_slice_new0 (EventPriv);
227   event->public.account = account != NULL ? g_object_ref (account) : NULL;
228   event->public.contact = contact ? g_object_ref (contact) : NULL;
229   event->public.type = type;
230   event->public.icon_name = g_strdup (icon_name);
231   event->public.header = g_strdup (header);
232   event->public.message = g_strdup (message);
233   event->public.must_ack = (func != NULL);
234   event->inhibit = FALSE;
235   event->func = func;
236   event->user_data = user_data;
237   event->manager = manager;
238   event->approval = approval;
239
240   DEBUG ("Adding event %p", event);
241   priv->events = g_slist_prepend (priv->events, event);
242
243   if (!display_notify_area (manager))
244     {
245       /* Don't fire the 'event-added' signal as we activate the event now */
246       if (approval != NULL)
247         approval->auto_approved = TRUE;
248
249       empathy_event_activate (&event->public);
250       return;
251     }
252
253   g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
254
255   if (!event->public.must_ack)
256     {
257       event->autoremove_timeout_id = g_timeout_add_seconds (
258           NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
259           event);
260     }
261 }
262
263 static void
264 handle_with_cb (GObject *source,
265     GAsyncResult *result,
266     gpointer user_data)
267 {
268   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
269   GError *error = NULL;
270
271   if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
272     {
273       DEBUG ("HandleWith failed: %s\n", error->message);
274       g_error_free (error);
275     }
276 }
277
278 static void
279 handle_with_time_cb (GObject *source,
280     GAsyncResult *result,
281     gpointer user_data)
282 {
283   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
284   GError *error = NULL;
285
286   if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
287         &error))
288     {
289       if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
290         {
291           EventManagerApproval *approval = user_data;
292
293           DEBUG ("HandleWithTime() is not implemented, falling back to "
294               "HandleWith(). Please upgrade to telepathy-mission-control "
295               "5.5.0 or later");
296
297           tp_channel_dispatch_operation_handle_with_async (approval->operation,
298               NULL, handle_with_cb, approval);
299         }
300       else
301         {
302           DEBUG ("HandleWithTime failed: %s\n", error->message);
303         }
304       g_error_free (error);
305     }
306 }
307
308 static void
309 event_manager_approval_approve (EventManagerApproval *approval)
310 {
311   gint64 timestamp;
312
313   if (approval->auto_approved)
314     {
315       timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
316     }
317   else
318     {
319       timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
320     }
321
322   g_assert (approval->operation != NULL);
323
324   tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
325       NULL, timestamp, handle_with_time_cb, approval);
326 }
327
328 static void
329 event_channel_process_func (EventPriv *event)
330 {
331   event_manager_approval_approve (event->approval);
332 }
333
334 static void
335 event_text_channel_process_func (EventPriv *event)
336 {
337   EmpathyTpChat *tp_chat;
338   gint64 timestamp;
339
340   timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
341
342   if (event->approval->handler != 0)
343     {
344       tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
345
346       g_signal_handler_disconnect (tp_chat, event->approval->handler);
347       event->approval->handler = 0;
348     }
349
350   event_manager_approval_approve (event->approval);
351 }
352
353 static EventPriv *
354 event_lookup_by_approval (EmpathyEventManager *manager,
355   EventManagerApproval *approval)
356 {
357   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
358   GSList *l;
359   EventPriv *retval = NULL;
360
361   for (l = priv->events; l; l = l->next)
362     {
363       EventPriv *event = l->data;
364
365       if (event->approval == approval)
366         {
367           retval = event;
368           break;
369         }
370     }
371
372   return retval;
373 }
374
375 static void
376 event_update (EmpathyEventManager *manager, EventPriv *event,
377   const char *icon_name, const char *header, const char *msg)
378 {
379   g_free (event->public.icon_name);
380   g_free (event->public.header);
381   g_free (event->public.message);
382
383   event->public.icon_name = g_strdup (icon_name);
384   event->public.header = g_strdup (header);
385   event->public.message = g_strdup (msg);
386
387   g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
388 }
389
390 static void
391 reject_channel_claim_cb (GObject *source,
392     GAsyncResult *result,
393     gpointer user_data)
394 {
395   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
396   GError *error = NULL;
397
398   if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
399     {
400       DEBUG ("Failed to claim channel: %s", error->message);
401
402       g_error_free (error);
403       goto out;
404     }
405
406   if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
407     {
408       empathy_tp_streamed_media_close (user_data);
409     }
410   else if (EMPATHY_IS_TP_CHAT (user_data))
411     {
412       empathy_tp_chat_leave (user_data);
413     }
414   else if (EMPATHY_IS_TP_FILE (user_data))
415     {
416       empathy_tp_file_close (user_data);
417     }
418
419 out:
420   g_object_unref (user_data);
421 }
422
423 static void
424 reject_auth_channel_claim_cb (GObject *source,
425     GAsyncResult *result,
426     gpointer user_data)
427 {
428   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
429   GError *error = NULL;
430
431   if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
432     {
433       DEBUG ("Failed to claim channel: %s", error->message);
434
435       g_error_free (error);
436       return;
437     }
438
439   tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
440       NULL, NULL, NULL, NULL);
441 }
442
443 static void
444 reject_approval (EventManagerApproval *approval)
445 {
446   /* We have to claim the channel before closing it */
447
448   /* Unfortunately, we need to special case the auth channels for the
449    * time being as they don't have a wrapper object handler in
450    * approval->handler_instance as they're not actually handled by
451    * this process, so we can just use a noddy callback to call Close()
452    * directly. */
453   if (approval->handler_instance != NULL)
454     {
455       tp_channel_dispatch_operation_claim_async (approval->operation,
456           reject_channel_claim_cb, g_object_ref (approval->handler_instance));
457     }
458   else if (tp_channel_get_channel_type_id (approval->main_channel)
459       == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
460     {
461       tp_channel_dispatch_operation_claim_async (approval->operation,
462           reject_auth_channel_claim_cb, approval->main_channel);
463     }
464 }
465
466 static void
467 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
468   gint response, gpointer user_data)
469 {
470   EventManagerApproval *approval = user_data;
471
472   gtk_widget_destroy (approval->dialog);
473   approval->dialog = NULL;
474
475   if (response != GTK_RESPONSE_ACCEPT)
476     {
477       reject_approval (approval);
478     }
479   else
480     {
481       event_manager_approval_approve (approval);
482     }
483 }
484
485 static void
486 event_channel_process_voip_func (EventPriv *event)
487 {
488   GtkWidget *dialog;
489   GtkWidget *button;
490   GtkWidget *image;
491   EmpathyTpStreamedMedia *call;
492   gboolean video;
493   gchar *title;
494
495   if (event->approval->dialog != NULL)
496     {
497       gtk_window_present (GTK_WINDOW (event->approval->dialog));
498       return;
499     }
500
501   call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
502
503   video = empathy_tp_streamed_media_has_initial_video (call);
504
505   dialog = gtk_message_dialog_new (NULL, 0,
506       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
507       video ? _("Incoming video call"): _("Incoming call"));
508
509   gtk_message_dialog_format_secondary_text (
510     GTK_MESSAGE_DIALOG (dialog), video ?
511       _("%s is video calling you. Do you want to answer?"):
512       _("%s is calling you. Do you want to answer?"),
513       empathy_contact_get_alias (event->approval->contact));
514
515   title = g_strdup_printf (_("Incoming call from %s"),
516       empathy_contact_get_alias (event->approval->contact));
517
518   gtk_window_set_title (GTK_WINDOW (dialog), title);
519   g_free (title);
520
521   /* Set image of the dialog */
522   if (video)
523     {
524       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
525           GTK_ICON_SIZE_DIALOG);
526     }
527   else
528     {
529       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
530           GTK_ICON_SIZE_DIALOG);
531     }
532
533   gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
534   gtk_widget_show (image);
535
536   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
537       GTK_RESPONSE_OK);
538
539   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
540       _("_Reject"), GTK_RESPONSE_REJECT);
541   image = gtk_image_new_from_icon_name ("call-stop",
542     GTK_ICON_SIZE_BUTTON);
543   gtk_button_set_image (GTK_BUTTON (button), image);
544
545   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
546       _("_Answer"), GTK_RESPONSE_ACCEPT);
547
548   image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
549   gtk_button_set_image (GTK_BUTTON (button), image);
550
551   g_signal_connect (dialog, "response",
552       G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
553       event->approval);
554
555   gtk_widget_show (dialog);
556
557   event->approval->dialog = dialog;
558 }
559
560 static void
561 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
562   EmpathyMessage *message,
563   EventManagerApproval *approval)
564 {
565   GtkWidget       *window = empathy_main_window_dup ();
566   EmpathyContact  *sender;
567   const gchar     *header;
568   const gchar     *msg;
569   TpChannel       *channel;
570   EventPriv       *event;
571   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
572
573   /* try to update the event if it's referring to a chat which is already in the
574    * queue. */
575   event = event_lookup_by_approval (approval->manager, approval);
576
577   sender = empathy_message_get_sender (message);
578   header = empathy_contact_get_alias (sender);
579   msg = empathy_message_get_body (message);
580
581   channel = empathy_tp_chat_get_channel (tp_chat);
582
583   if (event != NULL)
584     event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
585         msg);
586   else
587     event_manager_add (approval->manager, NULL, sender,
588         EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
589         approval, event_text_channel_process_func, NULL);
590
591   empathy_sound_manager_play (priv->sound_mgr, window,
592       EMPATHY_SOUND_CONVERSATION_NEW);
593
594   g_object_unref (window);
595 }
596
597 static void
598 event_manager_approval_done (EventManagerApproval *approval)
599 {
600   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
601   GSList                  *l;
602
603   if (approval->operation != NULL)
604     {
605       GQuark channel_type;
606
607       channel_type = tp_channel_get_channel_type_id (approval->main_channel);
608
609       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
610         {
611           priv->ringing--;
612           if (priv->ringing == 0)
613             empathy_sound_manager_stop (priv->sound_mgr,
614                 EMPATHY_SOUND_PHONE_INCOMING);
615         }
616     }
617
618   priv->approvals = g_slist_remove (priv->approvals, approval);
619
620   for (l = priv->events; l; l = l->next)
621     {
622       EventPriv *event = l->data;
623
624       if (event->approval == approval)
625         {
626           event_remove (event);
627           break;
628         }
629     }
630
631   event_manager_approval_free (approval);
632 }
633
634 static void
635 cdo_invalidated_cb (TpProxy *cdo,
636     guint domain,
637     gint code,
638     gchar *message,
639     EventManagerApproval *approval)
640 {
641   DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
642
643   event_manager_approval_done (approval);
644 }
645
646 static void
647 event_manager_media_channel_got_contact (EventManagerApproval *approval)
648 {
649   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
650   GtkWidget *window = empathy_main_window_dup ();
651   gchar *header;
652   EmpathyTpStreamedMedia *call;
653   gboolean video;
654
655   call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
656
657   video = empathy_tp_streamed_media_has_initial_video (call);
658
659   header = g_strdup_printf (
660     video ? _("Incoming video call from %s") :_("Incoming call from %s"),
661     empathy_contact_get_alias (approval->contact));
662
663   event_manager_add (approval->manager, NULL,
664       approval->contact, EMPATHY_EVENT_TYPE_VOIP,
665       video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
666       header, NULL, approval,
667       event_channel_process_voip_func, NULL);
668
669   g_free (header);
670
671   priv->ringing++;
672   if (priv->ringing == 1)
673     empathy_sound_manager_start_playing (priv->sound_mgr, window,
674         EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
675
676   g_object_unref (window);
677 }
678
679 static void
680 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
681   GParamSpec *param, EventManagerApproval *approval)
682 {
683   EmpathyContact *contact;
684
685   g_object_get (G_OBJECT (call), "contact", &contact, NULL);
686
687   if (contact == NULL)
688     return;
689
690   approval->contact = contact;
691   event_manager_media_channel_got_contact (approval);
692 }
693
694 static void
695 invite_dialog_response_cb (GtkDialog *dialog,
696                            gint response,
697                            EventManagerApproval *approval)
698 {
699   EmpathyTpChat *tp_chat;
700
701   gtk_widget_destroy (GTK_WIDGET (approval->dialog));
702   approval->dialog = NULL;
703
704   tp_chat = EMPATHY_TP_CHAT (approval->handler_instance);
705
706   if (response != GTK_RESPONSE_OK)
707     {
708       /* close channel */
709       DEBUG ("Muc invitation rejected");
710
711       reject_approval (approval);
712
713       return;
714     }
715
716   DEBUG ("Muc invitation accepted");
717
718   /* We'll join the room when handling the channel */
719   event_manager_approval_approve (approval);
720 }
721
722 static void
723 event_room_channel_process_func (EventPriv *event)
724 {
725   GtkWidget *dialog, *button, *image;
726   TpChannel *channel = event->approval->main_channel;
727   gchar *title;
728
729   if (event->approval->dialog != NULL)
730     {
731       gtk_window_present (GTK_WINDOW (event->approval->dialog));
732       return;
733     }
734
735   /* create dialog */
736   dialog = gtk_message_dialog_new (NULL, 0,
737       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
738
739   title = g_strdup_printf (_("Invitation to join %s"),
740       tp_channel_get_identifier (channel));
741
742   gtk_window_set_title (GTK_WINDOW (dialog), title);
743   g_free (title);
744
745   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
746       _("%s is inviting you to join %s"),
747       empathy_contact_get_alias (event->approval->contact),
748       tp_channel_get_identifier (channel));
749
750   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
751       GTK_RESPONSE_OK);
752
753   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
754       _("_Decline"), GTK_RESPONSE_CANCEL);
755   image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
756   gtk_button_set_image (GTK_BUTTON (button), image);
757
758   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
759       _("_Join"), GTK_RESPONSE_OK);
760   image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
761   gtk_button_set_image (GTK_BUTTON (button), image);
762
763   g_signal_connect (dialog, "response",
764       G_CALLBACK (invite_dialog_response_cb), event->approval);
765
766   gtk_widget_show (dialog);
767
768   event->approval->dialog = dialog;
769 }
770
771 static void
772 display_invite_room_dialog (EventManagerApproval *approval)
773 {
774   GtkWidget *window = empathy_main_window_dup ();
775   const gchar *invite_msg;
776   gchar *msg;
777   TpHandle self_handle;
778   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
779
780   self_handle = tp_channel_group_get_self_handle (approval->main_channel);
781   tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
782       NULL, NULL, &invite_msg);
783
784   if (approval->contact != NULL)
785     {
786       msg = g_strdup_printf (_("%s invited you to join %s"),
787           empathy_contact_get_alias (approval->contact),
788           tp_channel_get_identifier (approval->main_channel));
789     }
790   else
791     {
792       msg = g_strdup_printf (_("You have been invited to join %s"),
793           tp_channel_get_identifier (approval->main_channel));
794     }
795
796   event_manager_add (approval->manager, NULL,
797       approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
798       EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
799       event_room_channel_process_func, NULL);
800
801   empathy_sound_manager_play (priv->sound_mgr, window,
802       EMPATHY_SOUND_CONVERSATION_NEW);
803
804   g_free (msg);
805   g_object_unref (window);
806 }
807
808 static void
809 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
810                                          EmpathyContact *contact,
811                                          const GError *error,
812                                          gpointer user_data,
813                                          GObject *object)
814 {
815   EventManagerApproval *approval = (EventManagerApproval *) user_data;
816
817   if (error != NULL)
818     {
819       DEBUG ("Error: %s", error->message);
820     }
821   else
822     {
823       approval->contact = g_object_ref (contact);
824     }
825
826   display_invite_room_dialog (approval);
827 }
828
829 static void
830 event_manager_ft_got_contact_cb (TpConnection *connection,
831                                  EmpathyContact *contact,
832                                  const GError *error,
833                                  gpointer user_data,
834                                  GObject *object)
835 {
836   EventManagerApproval *approval = (EventManagerApproval *) user_data;
837   GtkWidget *window = empathy_main_window_dup ();
838   char *header;
839   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
840
841   approval->contact = g_object_ref (contact);
842
843   header = g_strdup_printf (_("Incoming file transfer from %s"),
844                             empathy_contact_get_alias (approval->contact));
845
846   event_manager_add (approval->manager, NULL,
847       approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
848       EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
849       approval, event_channel_process_func, NULL);
850
851   /* FIXME better sound for incoming file transfers ?*/
852   empathy_sound_manager_play (priv->sound_mgr, window,
853       EMPATHY_SOUND_CONVERSATION_NEW);
854
855   g_free (header);
856   g_object_unref (window);
857 }
858
859 static void
860 event_manager_auth_process_func (EventPriv *event)
861 {
862   empathy_event_approve ((EmpathyEvent *) event);
863 }
864
865 /* If there is a file-transfer, media, or auth channel consider it as
866  * the main one. */
867 static TpChannel *
868 find_main_channel (GList *channels)
869 {
870   GList *l;
871   TpChannel *text = NULL;
872
873   for (l = channels; l != NULL; l = g_list_next (l))
874     {
875       TpChannel *channel = l->data;
876       GQuark channel_type;
877
878       if (tp_proxy_get_invalidated (channel) != NULL)
879         continue;
880
881       channel_type = tp_channel_get_channel_type_id (channel);
882
883       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
884           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
885           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
886         return channel;
887
888       else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
889         text = channel;
890     }
891
892   return text;
893 }
894
895 static void
896 approve_channels (TpSimpleApprover *approver,
897     TpAccount *account,
898     TpConnection *connection,
899     GList *channels,
900     TpChannelDispatchOperation *dispatch_operation,
901     TpAddDispatchOperationContext *context,
902     gpointer user_data)
903 {
904   EmpathyEventManager *self = user_data;
905   EmpathyEventManagerPriv *priv = GET_PRIV (self);
906   TpChannel *channel;
907   EventManagerApproval *approval;
908   GQuark channel_type;
909
910   channel = find_main_channel (channels);
911   if (channel == NULL)
912     {
913       GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
914           "Unknown channel type" };
915
916       DEBUG ("Failed to find the main channel; ignoring");
917
918       tp_add_dispatch_operation_context_fail (context, &error);
919       return;
920     }
921
922   approval = event_manager_approval_new (self, dispatch_operation, channel);
923   priv->approvals = g_slist_prepend (priv->approvals, approval);
924
925   approval->invalidated_handler = g_signal_connect (dispatch_operation,
926       "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
927
928   channel_type = tp_channel_get_channel_type_id (channel);
929
930   if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
931     {
932       EmpathyTpChat *tp_chat;
933
934       tp_chat = empathy_tp_chat_new (account, channel);
935       approval->handler_instance = G_OBJECT (tp_chat);
936
937       if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
938         {
939           /* Are we in local-pending ? */
940           TpHandle inviter;
941
942           if (empathy_tp_chat_is_invited (tp_chat, &inviter))
943             {
944               /* We are invited to a room */
945               DEBUG ("Have been invited to %s. Ask user if he wants to accept",
946                   tp_channel_get_identifier (channel));
947
948               if (inviter != 0)
949                 {
950                   empathy_tp_contact_factory_get_from_handle (connection,
951                       inviter, event_manager_muc_invite_got_contact_cb,
952                       approval, NULL, G_OBJECT (self));
953                 }
954               else
955                 {
956                   display_invite_room_dialog (approval);
957                 }
958
959               goto out;
960             }
961
962           /* We are not invited, approve the channel right now */
963           tp_add_dispatch_operation_context_accept (context);
964
965           approval->auto_approved = TRUE;
966           event_manager_approval_approve (approval);
967           return;
968         }
969
970       /* 1-1 text channel, wait for the first message */
971       approval->handler = g_signal_connect (tp_chat, "message-received",
972         G_CALLBACK (event_manager_chat_message_received_cb), approval);
973     }
974   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
975     {
976       EmpathyContact *contact;
977       EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account, channel);
978
979       approval->handler_instance = G_OBJECT (call);
980
981       g_object_get (G_OBJECT (call), "contact", &contact, NULL);
982
983       if (contact == NULL)
984         {
985           g_signal_connect (call, "notify::contact",
986             G_CALLBACK (event_manager_media_channel_contact_changed_cb),
987             approval);
988         }
989       else
990         {
991           approval->contact = contact;
992           event_manager_media_channel_got_contact (approval);
993         }
994
995     }
996   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
997     {
998       TpHandle handle;
999       EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1000
1001       approval->handler_instance = G_OBJECT (tp_file);
1002
1003       handle = tp_channel_get_handle (channel, NULL);
1004
1005       connection = tp_channel_borrow_connection (channel);
1006       empathy_tp_contact_factory_get_from_handle (connection, handle,
1007         event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1008     }
1009   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1010     {
1011       event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1012           GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1013           _("Password required"), approval,
1014           event_manager_auth_process_func, NULL);
1015     }
1016   else
1017     {
1018       GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1019           "Invalid channel type" };
1020
1021       DEBUG ("Unknown channel type (%s), ignoring..",
1022           g_quark_to_string (channel_type));
1023
1024       tp_add_dispatch_operation_context_fail (context, &error);
1025       return;
1026     }
1027
1028 out:
1029   tp_add_dispatch_operation_context_accept (context);
1030 }
1031
1032 static void
1033 event_pending_subscribe_func (EventPriv *event)
1034 {
1035   empathy_subscription_dialog_show (event->public.contact, event->public.header,
1036       NULL);
1037   event_remove (event);
1038 }
1039
1040 static void
1041 event_manager_pendings_changed_cb (EmpathyContactList  *list,
1042   EmpathyContact *contact, EmpathyContact *actor,
1043   guint reason, gchar *message, gboolean is_pending,
1044   EmpathyEventManager *manager)
1045 {
1046   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1047   gchar                   *header, *event_msg;
1048
1049   if (!is_pending)
1050     {
1051       GSList *l;
1052
1053       for (l = priv->events; l; l = l->next)
1054         {
1055           EventPriv *event = l->data;
1056
1057           if (event->public.contact == contact &&
1058               event->func == event_pending_subscribe_func)
1059             {
1060               event_remove (event);
1061               break;
1062             }
1063         }
1064
1065       return;
1066     }
1067
1068   header = g_strdup_printf (
1069       _("%s would like permission to see when you are available"),
1070       empathy_contact_get_alias (contact));
1071
1072   if (!EMP_STR_EMPTY (message))
1073     event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1074   else
1075     event_msg = NULL;
1076
1077   event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1078       GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1079       event_pending_subscribe_func, NULL);
1080
1081   g_free (event_msg);
1082   g_free (header);
1083 }
1084
1085 static void
1086 event_manager_presence_changed_cb (EmpathyContact *contact,
1087     TpConnectionPresenceType current,
1088     TpConnectionPresenceType previous,
1089     EmpathyEventManager *manager)
1090 {
1091   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1092   TpAccount *account;
1093   gchar *header = NULL;
1094   EmpathyPresenceManager *presence_mgr;
1095   GtkWidget *window = empathy_main_window_dup ();
1096
1097   account = empathy_contact_get_account (contact);
1098   presence_mgr = empathy_presence_manager_dup_singleton ();
1099
1100   if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1101     goto out;
1102
1103   if (tp_connection_presence_type_cmp_availability (previous,
1104         TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1105     {
1106       /* contact was online */
1107       if (tp_connection_presence_type_cmp_availability (current,
1108           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1109         {
1110           /* someone is logging off */
1111           empathy_sound_manager_play (priv->sound_mgr, window,
1112               EMPATHY_SOUND_CONTACT_DISCONNECTED);
1113
1114           if (g_settings_get_boolean (priv->gsettings_notif,
1115                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1116             {
1117               header = g_strdup_printf ("<b>%s</b>",
1118                   empathy_contact_get_alias (contact));
1119
1120               event_manager_add (manager, NULL, contact,
1121                   EMPATHY_EVENT_TYPE_PRESENCE, EMPATHY_IMAGE_AVATAR_DEFAULT,
1122                   header, _("Disconnected"), NULL, NULL, NULL);
1123             }
1124         }
1125     }
1126   else
1127     {
1128       /* contact was offline */
1129       if (tp_connection_presence_type_cmp_availability (current,
1130             TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1131         {
1132           /* someone is logging in */
1133           empathy_sound_manager_play (priv->sound_mgr, window,
1134               EMPATHY_SOUND_CONTACT_CONNECTED);
1135
1136           if (g_settings_get_boolean (priv->gsettings_notif,
1137                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1138             {
1139               header = g_strdup_printf ("<b>%s</b>",
1140                   empathy_contact_get_alias (contact));
1141
1142               event_manager_add (manager, NULL, contact,
1143                   EMPATHY_EVENT_TYPE_PRESENCE, EMPATHY_IMAGE_AVATAR_DEFAULT,
1144                   header, _("Connected"), NULL, NULL, NULL);
1145             }
1146         }
1147     }
1148   g_free (header);
1149
1150 out:
1151   g_object_unref (presence_mgr);
1152   g_object_unref (window);
1153 }
1154
1155 static void
1156 event_manager_members_changed_cb (EmpathyContactList  *list,
1157     EmpathyContact *contact,
1158     EmpathyContact *actor,
1159     guint reason,
1160     gchar *message,
1161     gboolean is_member,
1162     EmpathyEventManager *manager)
1163 {
1164   if (is_member)
1165     g_signal_connect (contact, "presence-changed",
1166         G_CALLBACK (event_manager_presence_changed_cb), manager);
1167   else
1168     g_signal_handlers_disconnect_by_func (contact,
1169         event_manager_presence_changed_cb, manager);
1170 }
1171
1172 static GObject *
1173 event_manager_constructor (GType type,
1174                            guint n_props,
1175                            GObjectConstructParam *props)
1176 {
1177         GObject *retval;
1178
1179         if (manager_singleton) {
1180                 retval = g_object_ref (manager_singleton);
1181         } else {
1182                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1183                         (type, n_props, props);
1184
1185                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1186                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1187         }
1188
1189         return retval;
1190 }
1191
1192 static void
1193 event_manager_finalize (GObject *object)
1194 {
1195   EmpathyEventManagerPriv *priv = GET_PRIV (object);
1196
1197   if (priv->ringing > 0)
1198     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1199
1200   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1201   g_slist_free (priv->events);
1202   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1203   g_slist_free (priv->approvals);
1204   g_object_unref (priv->contact_manager);
1205   g_object_unref (priv->approver);
1206   g_object_unref (priv->auth_approver);
1207   g_object_unref (priv->gsettings_notif);
1208   g_object_unref (priv->gsettings_ui);
1209   g_object_unref (priv->sound_mgr);
1210 }
1211
1212 static void
1213 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1214 {
1215   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1216
1217   object_class->finalize = event_manager_finalize;
1218   object_class->constructor = event_manager_constructor;
1219
1220   signals[EVENT_ADDED] =
1221     g_signal_new ("event-added",
1222       G_TYPE_FROM_CLASS (klass),
1223       G_SIGNAL_RUN_LAST,
1224       0,
1225       NULL, NULL,
1226       g_cclosure_marshal_VOID__POINTER,
1227       G_TYPE_NONE,
1228       1, G_TYPE_POINTER);
1229
1230   signals[EVENT_REMOVED] =
1231   g_signal_new ("event-removed",
1232       G_TYPE_FROM_CLASS (klass),
1233       G_SIGNAL_RUN_LAST,
1234       0,
1235       NULL, NULL,
1236       g_cclosure_marshal_VOID__POINTER,
1237       G_TYPE_NONE, 1, G_TYPE_POINTER);
1238
1239   signals[EVENT_UPDATED] =
1240   g_signal_new ("event-updated",
1241       G_TYPE_FROM_CLASS (klass),
1242       G_SIGNAL_RUN_LAST,
1243       0,
1244       NULL, NULL,
1245       g_cclosure_marshal_VOID__POINTER,
1246       G_TYPE_NONE, 1, G_TYPE_POINTER);
1247
1248   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1249 }
1250
1251 static void
1252 empathy_event_manager_init (EmpathyEventManager *manager)
1253 {
1254   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1255     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1256   TpDBusDaemon *dbus;
1257   GError *error = NULL;
1258
1259   manager->priv = priv;
1260
1261   priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1262   priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1263
1264   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1265
1266   priv->contact_manager = empathy_contact_manager_dup_singleton ();
1267   g_signal_connect (priv->contact_manager, "pendings-changed",
1268     G_CALLBACK (event_manager_pendings_changed_cb), manager);
1269
1270   g_signal_connect (priv->contact_manager, "members-changed",
1271     G_CALLBACK (event_manager_members_changed_cb), manager);
1272
1273   dbus = tp_dbus_daemon_dup (&error);
1274   if (dbus == NULL)
1275     {
1276       DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1277       g_error_free (error);
1278       return;
1279     }
1280
1281   priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1282       approve_channels, manager, NULL);
1283
1284   /* EmpathyTpChat relies on this feature being prepared */
1285   tp_base_client_add_connection_features_varargs (priv->approver,
1286     TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1287
1288   /* Private text channels */
1289   tp_base_client_take_approver_filter (priv->approver,
1290       tp_asv_new (
1291         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1292         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1293         NULL));
1294
1295   /* Muc text channels */
1296   tp_base_client_take_approver_filter (priv->approver,
1297       tp_asv_new (
1298         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1299         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1300         NULL));
1301
1302   /* File transfer */
1303   tp_base_client_take_approver_filter (priv->approver,
1304       tp_asv_new (
1305         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1306           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1307         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1308         NULL));
1309
1310   /* Calls */
1311   tp_base_client_take_approver_filter (priv->approver,
1312       tp_asv_new (
1313         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1314           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1315         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1316         NULL));
1317
1318   /* I don't feel good about doing this, and I'm sorry, but the
1319    * capabilities connection feature is added earlier because it's
1320    * needed for EmpathyTpChat. If the capabilities feature is required
1321    * then preparing an auth channel (which of course appears in the
1322    * CONNECTING state) will never be prepared. So the options are
1323    * either to create another approver like I've done, or to port
1324    * EmpathyTpChat and its users to not depend on the connection being
1325    * prepared with capabilities. I chose the former, obviously. :-) */
1326
1327   priv->auth_approver = tp_simple_approver_new (dbus,
1328       "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1329       NULL);
1330
1331   /* SASL auth channels */
1332   tp_base_client_take_approver_filter (priv->auth_approver,
1333       tp_asv_new (
1334         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1335           TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1336         TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1337           G_TYPE_STRING,
1338           TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1339         NULL));
1340
1341   if (!tp_base_client_register (priv->approver, &error))
1342     {
1343       DEBUG ("Failed to register Approver: %s", error->message);
1344       g_error_free (error);
1345     }
1346
1347   if (!tp_base_client_register (priv->auth_approver, &error))
1348     {
1349       DEBUG ("Failed to register auth Approver: %s", error->message);
1350       g_error_free (error);
1351     }
1352
1353   g_object_unref (dbus);
1354 }
1355
1356 EmpathyEventManager *
1357 empathy_event_manager_dup_singleton (void)
1358 {
1359   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1360 }
1361
1362 GSList *
1363 empathy_event_manager_get_events (EmpathyEventManager *manager)
1364 {
1365   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1366
1367   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1368
1369   return priv->events;
1370 }
1371
1372 EmpathyEvent *
1373 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1374 {
1375   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1376
1377   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1378
1379   return priv->events ? priv->events->data : NULL;
1380 }
1381
1382 void
1383 empathy_event_activate (EmpathyEvent *event_public)
1384 {
1385   EventPriv *event = (EventPriv *) event_public;
1386
1387   g_return_if_fail (event_public != NULL);
1388
1389   if (event->func)
1390     event->func (event);
1391   else
1392     event_remove (event);
1393 }
1394
1395 void
1396 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1397 {
1398   EventPriv *event = (EventPriv *) event_public;
1399
1400   g_return_if_fail (event_public != NULL);
1401
1402   event->inhibit = TRUE;
1403 }
1404
1405 void
1406 empathy_event_approve (EmpathyEvent *event_public)
1407 {
1408   EventPriv *event = (EventPriv *) event_public;
1409
1410   g_return_if_fail (event_public != NULL);
1411
1412   event_manager_approval_approve (event->approval);
1413 }
1414
1415 void
1416 empathy_event_decline (EmpathyEvent *event_public)
1417 {
1418   EventPriv *event = (EventPriv *) event_public;
1419
1420   g_return_if_fail (event_public != NULL);
1421
1422   reject_approval (event->approval);
1423 }