]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
3b6ee3e182f929dc1f03deff20417cf4761e0a3e
[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_text_channel (EmpathyEventManager *self,
918     EventManagerApproval *approval,
919     TpAddDispatchOperationContext *context,
920     EmpathyTpChat *tp_chat)
921 {
922   GList *messages, *l;
923
924   approval->handler_instance = g_object_ref (tp_chat);
925
926   if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
927     {
928       /* Are we in local-pending ? */
929       TpContact *inviter;
930
931       if (empathy_tp_chat_is_invited (tp_chat, &inviter))
932         {
933           /* We are invited to a room */
934           DEBUG ("Have been invited to %s. Ask user if he wants to accept",
935               tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
936
937           if (inviter != NULL)
938             {
939               approval->contact = empathy_contact_dup_from_tp_contact (
940                   inviter);
941             }
942
943           display_invite_room_dialog (approval);
944           tp_add_dispatch_operation_context_accept (context);
945
946           return;
947         }
948
949       /* We are not invited, approve the channel right now */
950       tp_add_dispatch_operation_context_accept (context);
951
952       approval->auto_approved = TRUE;
953       event_manager_approval_approve (approval);
954       return;
955     }
956
957   /* 1-1 text channel, wait for the first message */
958   approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
959     G_CALLBACK (event_manager_chat_message_received_cb), approval);
960
961   messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
962   for (l = messages; l != NULL; l = g_list_next (l))
963     {
964       EmpathyMessage *msg = l->data;
965
966       event_manager_chat_message_received_cb (tp_chat, msg, approval);
967     }
968
969   tp_add_dispatch_operation_context_accept (context);
970 }
971
972 static void
973 approve_call_channel (EmpathyEventManager *self,
974     EventManagerApproval *approval,
975     TpAddDispatchOperationContext *context,
976     TpCallChannel *call)
977 {
978   const gchar *id;
979   TpChannel *channel = TP_CHANNEL (call);
980   TpConnection *connection = tp_channel_borrow_connection (channel);
981
982   approval->handler_instance = g_object_ref (call);
983
984   id = tp_channel_get_identifier (channel);
985
986   empathy_tp_contact_factory_get_from_id (connection, id,
987     event_manager_call_channel_got_contact_cb,
988     approval, NULL, G_OBJECT (self));
989
990   tp_add_dispatch_operation_context_accept (context);
991 }
992
993 static void
994 approve_ft_channel (EmpathyEventManager *self,
995     EventManagerApproval *approval,
996     TpAddDispatchOperationContext *context,
997     TpFileTransferChannel *ft)
998 {
999   TpHandle handle;
1000   TpChannel *channel = TP_CHANNEL (ft);
1001   TpConnection *connection = tp_channel_borrow_connection (channel);
1002
1003   approval->handler_instance = g_object_ref (ft);
1004
1005   handle = tp_channel_get_handle (channel, NULL);
1006
1007   empathy_tp_contact_factory_get_from_handle (connection, handle,
1008     event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1009
1010   tp_add_dispatch_operation_context_accept (context);
1011 }
1012
1013 static void
1014 approve_sasl_channel (EmpathyEventManager *self,
1015     TpAccount *account,
1016     EventManagerApproval *approval,
1017     TpAddDispatchOperationContext *context,
1018     TpChannel *channel)
1019 {
1020   GHashTable *props;
1021   const gchar * const *available_mechanisms;
1022
1023   props = tp_channel_borrow_immutable_properties (channel);
1024   available_mechanisms = tp_asv_get_boxed (props,
1025       TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1026       G_TYPE_STRV);
1027
1028   if (tp_strv_contains (available_mechanisms, "X-TELEPATHY-PASSWORD"))
1029     {
1030       event_manager_add (approval->manager, account, NULL,
1031           EMPATHY_EVENT_TYPE_AUTH,
1032           GTK_STOCK_DIALOG_AUTHENTICATION,
1033           tp_account_get_display_name (account),
1034           _("Password required"), approval,
1035           event_manager_auth_process_func, NULL);
1036     }
1037   else
1038     {
1039       GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
1040           "Support only X-TELEPATHY-PASSWORD auth method" };
1041
1042       tp_add_dispatch_operation_context_fail (context, &error);
1043       return;
1044     }
1045
1046   tp_add_dispatch_operation_context_accept (context);
1047 }
1048
1049 static void
1050 approve_channels (TpSimpleApprover *approver,
1051     TpAccount *account,
1052     TpConnection *connection,
1053     GList *channels,
1054     TpChannelDispatchOperation *dispatch_operation,
1055     TpAddDispatchOperationContext *context,
1056     gpointer user_data)
1057 {
1058   EmpathyEventManager *self = user_data;
1059   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1060   TpChannel *channel;
1061   EventManagerApproval *approval;
1062   GQuark channel_type;
1063
1064   channel = find_main_channel (channels);
1065   if (channel == NULL)
1066     {
1067       GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1068           "Unknown channel type" };
1069
1070       DEBUG ("Failed to find the main channel; ignoring");
1071
1072       tp_add_dispatch_operation_context_fail (context, &error);
1073       return;
1074     }
1075
1076   approval = event_manager_approval_new (self, dispatch_operation, channel);
1077   priv->approvals = g_slist_prepend (priv->approvals, approval);
1078
1079   approval->invalidated_handler = g_signal_connect (dispatch_operation,
1080       "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1081
1082   channel_type = tp_channel_get_channel_type_id (channel);
1083
1084   if (EMPATHY_IS_TP_CHAT (channel))
1085     {
1086       approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1087     }
1088   else if (TP_IS_CALL_CHANNEL (channel))
1089     {
1090       approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1091     }
1092   else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1093     {
1094       approve_ft_channel (self, approval, context,
1095           TP_FILE_TRANSFER_CHANNEL (channel));
1096     }
1097   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1098     {
1099       approve_sasl_channel (self, account, approval, context, channel);
1100     }
1101   else
1102     {
1103       GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1104           "Invalid channel type" };
1105
1106       DEBUG ("Unknown channel type (%s), ignoring..",
1107           g_quark_to_string (channel_type));
1108
1109       tp_add_dispatch_operation_context_fail (context, &error);
1110       return;
1111     }
1112 }
1113
1114 static void
1115 event_pending_subscribe_func (EventPriv *event)
1116 {
1117   empathy_subscription_dialog_show (event->public.contact, event->public.header,
1118       NULL);
1119   event_remove (event);
1120 }
1121
1122 static void
1123 check_publish_state (EmpathyEventManager *self,
1124     TpContact *tp_contact)
1125 {
1126   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1127   gchar *header, *event_msg;
1128   TpSubscriptionState state;
1129   EmpathyContact *contact;
1130   const gchar *message;
1131
1132   state = tp_contact_get_publish_state (tp_contact);
1133
1134   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1135
1136   if (state != TP_SUBSCRIPTION_STATE_ASK)
1137     {
1138       GSList *l;
1139
1140       for (l = priv->events; l; l = l->next)
1141         {
1142           EventPriv *event = l->data;
1143
1144           if (event->public.contact == contact &&
1145               event->func == event_pending_subscribe_func)
1146             {
1147               event_remove (event);
1148               break;
1149             }
1150         }
1151
1152       goto out;
1153     }
1154
1155   header = g_strdup_printf (
1156       _("%s would like permission to see when you are online"),
1157       empathy_contact_get_alias (contact));
1158
1159   message = tp_contact_get_publish_request (tp_contact);
1160
1161   if (!EMP_STR_EMPTY (message))
1162     event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1163   else
1164     event_msg = NULL;
1165
1166   event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1167       GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1168       event_pending_subscribe_func, NULL);
1169
1170   g_free (event_msg);
1171   g_free (header);
1172
1173 out:
1174   g_object_unref (contact);
1175 }
1176
1177 static void
1178 event_manager_publish_state_changed_cb (TpContact *contact,
1179     GParamSpec *spec,
1180     EmpathyEventManager *self)
1181 {
1182   check_publish_state (self, contact);
1183 }
1184
1185 static void
1186 event_manager_presence_changed_cb (EmpathyContact *contact,
1187     TpConnectionPresenceType current,
1188     TpConnectionPresenceType previous,
1189     EmpathyEventManager *manager)
1190 {
1191   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1192   TpAccount *account;
1193   EmpathyPresenceManager *presence_mgr;
1194   GtkWidget *window = empathy_roster_window_dup ();
1195
1196   account = empathy_contact_get_account (contact);
1197   presence_mgr = empathy_presence_manager_dup_singleton ();
1198
1199   if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1200     goto out;
1201
1202   if (tp_connection_presence_type_cmp_availability (previous,
1203         TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1204     {
1205       /* contact was online */
1206       if (tp_connection_presence_type_cmp_availability (current,
1207           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1208         {
1209           /* someone is logging off */
1210           empathy_sound_manager_play (priv->sound_mgr, window,
1211               EMPATHY_SOUND_CONTACT_DISCONNECTED);
1212
1213           if (g_settings_get_boolean (priv->gsettings_notif,
1214                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1215             {
1216               event_manager_add (manager, NULL, contact,
1217                   EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1218                   EMPATHY_IMAGE_AVATAR_DEFAULT,
1219                   empathy_contact_get_alias (contact), _("Disconnected"),
1220                   NULL, NULL, NULL);
1221             }
1222         }
1223     }
1224   else
1225     {
1226       /* contact was offline */
1227       if (tp_connection_presence_type_cmp_availability (current,
1228             TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1229         {
1230           /* someone is logging in */
1231           empathy_sound_manager_play (priv->sound_mgr, window,
1232               EMPATHY_SOUND_CONTACT_CONNECTED);
1233
1234           if (g_settings_get_boolean (priv->gsettings_notif,
1235                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1236             {
1237               event_manager_add (manager, NULL, contact,
1238                   EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1239                   EMPATHY_IMAGE_AVATAR_DEFAULT,
1240                   empathy_contact_get_alias (contact), _("Connected"),
1241                   NULL, NULL, NULL);
1242             }
1243         }
1244     }
1245
1246 out:
1247   g_object_unref (presence_mgr);
1248   g_object_unref (window);
1249 }
1250
1251 static GObject *
1252 event_manager_constructor (GType type,
1253                            guint n_props,
1254                            GObjectConstructParam *props)
1255 {
1256         GObject *retval;
1257
1258         if (manager_singleton) {
1259                 retval = g_object_ref (manager_singleton);
1260         } else {
1261                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1262                         (type, n_props, props);
1263
1264                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1265                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1266         }
1267
1268         return retval;
1269 }
1270
1271 static void
1272 event_manager_finalize (GObject *object)
1273 {
1274   EmpathyEventManagerPriv *priv = GET_PRIV (object);
1275
1276   if (priv->ringing > 0)
1277     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1278
1279   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1280   g_slist_free (priv->events);
1281   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1282   g_slist_free (priv->approvals);
1283   g_object_unref (priv->conn_aggregator);
1284   g_object_unref (priv->approver);
1285   g_object_unref (priv->auth_approver);
1286   g_object_unref (priv->gsettings_notif);
1287   g_object_unref (priv->gsettings_ui);
1288   g_object_unref (priv->sound_mgr);
1289   g_hash_table_unref (priv->contacts);
1290 }
1291
1292 static void
1293 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1294 {
1295   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1296
1297   object_class->finalize = event_manager_finalize;
1298   object_class->constructor = event_manager_constructor;
1299
1300   signals[EVENT_ADDED] =
1301     g_signal_new ("event-added",
1302       G_TYPE_FROM_CLASS (klass),
1303       G_SIGNAL_RUN_LAST,
1304       0,
1305       NULL, NULL,
1306       g_cclosure_marshal_generic,
1307       G_TYPE_NONE,
1308       1, G_TYPE_POINTER);
1309
1310   signals[EVENT_REMOVED] =
1311   g_signal_new ("event-removed",
1312       G_TYPE_FROM_CLASS (klass),
1313       G_SIGNAL_RUN_LAST,
1314       0,
1315       NULL, NULL,
1316       g_cclosure_marshal_generic,
1317       G_TYPE_NONE, 1, G_TYPE_POINTER);
1318
1319   signals[EVENT_UPDATED] =
1320   g_signal_new ("event-updated",
1321       G_TYPE_FROM_CLASS (klass),
1322       G_SIGNAL_RUN_LAST,
1323       0,
1324       NULL, NULL,
1325       g_cclosure_marshal_generic,
1326       G_TYPE_NONE, 1, G_TYPE_POINTER);
1327
1328   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1329 }
1330
1331 static void
1332 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1333     GPtrArray *added,
1334     GPtrArray *removed,
1335     EmpathyEventManager *self)
1336 {
1337   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1338   guint i;
1339
1340   for (i = 0; i < added->len; i++)
1341     {
1342       TpContact *tp_contact = g_ptr_array_index (added, i);
1343       EmpathyContact *contact;
1344
1345       if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1346         continue;
1347
1348       contact = empathy_contact_dup_from_tp_contact (tp_contact);
1349
1350       tp_g_signal_connect_object (contact, "presence-changed",
1351           G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1352
1353       tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1354           G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1355
1356       check_publish_state (self, tp_contact);
1357
1358       /* Pass ownership to the hash table */
1359       g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1360     }
1361
1362   for (i = 0; i < removed->len; i++)
1363     {
1364       TpContact *tp_contact = g_ptr_array_index (removed, i);
1365       EmpathyContact *contact;
1366
1367       contact = g_hash_table_lookup (priv->contacts, tp_contact);
1368       if (contact == NULL)
1369         continue;
1370
1371       g_signal_handlers_disconnect_by_func (contact,
1372           event_manager_presence_changed_cb, self);
1373
1374       g_signal_handlers_disconnect_by_func (tp_contact,
1375           event_manager_publish_state_changed_cb, self);
1376
1377       g_hash_table_remove (priv->contacts, tp_contact);
1378     }
1379 }
1380
1381 static void
1382 empathy_event_manager_init (EmpathyEventManager *manager)
1383 {
1384   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1385     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1386   GError *error = NULL;
1387   TpAccountManager *am;
1388   GPtrArray *contacts, *empty;
1389
1390   manager->priv = priv;
1391
1392   priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1393   priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1394
1395   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1396
1397   priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1398       g_object_unref);
1399
1400   priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1401
1402   tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1403       G_CALLBACK (contact_list_changed_cb), manager, 0);
1404
1405   contacts = empathy_connection_aggregator_dup_all_contacts (
1406       priv->conn_aggregator);
1407
1408   empty = g_ptr_array_new ();
1409
1410   contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1411
1412   g_ptr_array_unref (contacts);
1413   g_ptr_array_unref (empty);
1414
1415    am = tp_account_manager_dup ();
1416
1417   priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1418       FALSE, approve_channels, manager, NULL);
1419
1420   /* Private text channels */
1421   tp_base_client_take_approver_filter (priv->approver,
1422       tp_asv_new (
1423         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1424         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1425         NULL));
1426
1427   /* Muc text channels */
1428   tp_base_client_take_approver_filter (priv->approver,
1429       tp_asv_new (
1430         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1431         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1432         NULL));
1433
1434   /* File transfer */
1435   tp_base_client_take_approver_filter (priv->approver,
1436       tp_asv_new (
1437         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1438           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1439         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1440         NULL));
1441
1442   /* Calls */
1443   tp_base_client_take_approver_filter (priv->approver,
1444       tp_asv_new (
1445         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1446           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1447         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1448         NULL));
1449   tp_base_client_take_approver_filter (priv->approver,
1450       tp_asv_new (
1451         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1452           TP_IFACE_CHANNEL_TYPE_CALL,
1453         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1454         NULL));
1455
1456   /* I don't feel good about doing this, and I'm sorry, but the
1457    * capabilities connection feature is added earlier because it's
1458    * needed for EmpathyTpChat. If the capabilities feature is required
1459    * then preparing an auth channel (which of course appears in the
1460    * CONNECTING state) will never be prepared. So the options are
1461    * either to create another approver like I've done, or to port
1462    * EmpathyTpChat and its users to not depend on the connection being
1463    * prepared with capabilities. I chose the former, obviously. :-) */
1464
1465   priv->auth_approver = tp_simple_approver_new_with_am (am,
1466       "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1467       NULL);
1468
1469   /* SASL auth channels */
1470   tp_base_client_take_approver_filter (priv->auth_approver,
1471       tp_asv_new (
1472         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1473           TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1474         TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1475           G_TYPE_STRING,
1476           TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1477         NULL));
1478
1479   if (!tp_base_client_register (priv->approver, &error))
1480     {
1481       DEBUG ("Failed to register Approver: %s", error->message);
1482       g_error_free (error);
1483     }
1484
1485   if (!tp_base_client_register (priv->auth_approver, &error))
1486     {
1487       DEBUG ("Failed to register auth Approver: %s", error->message);
1488       g_error_free (error);
1489     }
1490
1491   g_object_unref (am);
1492 }
1493
1494 EmpathyEventManager *
1495 empathy_event_manager_dup_singleton (void)
1496 {
1497   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1498 }
1499
1500 GSList *
1501 empathy_event_manager_get_events (EmpathyEventManager *manager)
1502 {
1503   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1504
1505   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1506
1507   return priv->events;
1508 }
1509
1510 EmpathyEvent *
1511 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1512 {
1513   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1514
1515   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1516
1517   return priv->events ? priv->events->data : NULL;
1518 }
1519
1520 void
1521 empathy_event_activate (EmpathyEvent *event_public)
1522 {
1523   EventPriv *event = (EventPriv *) event_public;
1524
1525   g_return_if_fail (event_public != NULL);
1526
1527   if (event->func)
1528     event->func (event);
1529   else
1530     event_remove (event);
1531 }
1532
1533 void
1534 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1535 {
1536   EventPriv *event = (EventPriv *) event_public;
1537
1538   g_return_if_fail (event_public != NULL);
1539
1540   event->inhibit = TRUE;
1541 }
1542
1543 void
1544 empathy_event_approve (EmpathyEvent *event_public)
1545 {
1546   EventPriv *event = (EventPriv *) event_public;
1547
1548   g_return_if_fail (event_public != NULL);
1549
1550   event_manager_approval_approve (event->approval);
1551 }
1552
1553 void
1554 empathy_event_decline (EmpathyEvent *event_public)
1555 {
1556   EventPriv *event = (EventPriv *) event_public;
1557
1558   g_return_if_fail (event_public != NULL);
1559
1560   reject_approval (event->approval);
1561 }