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