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