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