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