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