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