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