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