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