]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
7e34f4ce6b0851f82d6d128576a3dde135039e15
[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-connection-aggregator.h>
34 #include <libempathy/empathy-tp-chat.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-gsettings.h>
37 #include <libempathy/empathy-sasl-mechanisms.h>
38
39 #include <extensions/extensions.h>
40
41 #include <libempathy-gtk/empathy-images.h>
42 #include <libempathy-gtk/empathy-sound-manager.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-call-utils.h>
45 #include <libempathy-gtk/empathy-subscription-dialog.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   EmpathyContact  *sender;
581   const gchar     *header;
582   const gchar     *msg;
583   EventPriv       *event;
584   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
585
586   /* try to update the event if it's referring to a chat which is already in the
587    * queue. */
588   event = event_lookup_by_approval (approval->manager, approval);
589
590   sender = empathy_message_get_sender (message);
591
592   /* We only want to show incoming messages */
593   if (empathy_contact_is_user (sender))
594     return;
595
596   header = empathy_contact_get_alias (sender);
597   msg = empathy_message_get_body (message);
598
599   if (event != NULL)
600     event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
601         msg);
602   else
603     event_manager_add (approval->manager, NULL, sender,
604         EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
605         approval, event_text_channel_process_func, NULL);
606
607   empathy_sound_manager_play (priv->sound_mgr, NULL,
608       EMPATHY_SOUND_CONVERSATION_NEW);
609 }
610
611 static void
612 event_manager_approval_done (EventManagerApproval *approval)
613 {
614   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
615   GSList                  *l;
616
617   if (approval->operation != NULL)
618     {
619       GQuark channel_type;
620
621       channel_type = tp_channel_get_channel_type_id (approval->main_channel);
622
623       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
624           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
625         {
626           priv->ringing--;
627           if (priv->ringing == 0)
628             empathy_sound_manager_stop (priv->sound_mgr,
629                 EMPATHY_SOUND_PHONE_INCOMING);
630         }
631     }
632
633   priv->approvals = g_slist_remove (priv->approvals, approval);
634
635   for (l = priv->events; l; l = l->next)
636     {
637       EventPriv *event = l->data;
638
639       if (event->approval == approval)
640         {
641           event_remove (event);
642           break;
643         }
644     }
645
646   event_manager_approval_free (approval);
647 }
648
649 static void
650 cdo_invalidated_cb (TpProxy *cdo,
651     guint domain,
652     gint code,
653     gchar *message,
654     EventManagerApproval *approval)
655 {
656   DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
657
658   event_manager_approval_done (approval);
659 }
660
661 static void
662 event_manager_call_state_changed_cb (TpCallChannel *call,
663   TpCallState state,
664   TpCallFlags flags,
665   TpCallStateReason *reason,
666   GHashTable *details,
667   EventManagerApproval *approval)
668 {
669   if (state == TP_CALL_STATE_ENDED)
670     {
671       DEBUG ("Call ended, seems we missed it :/");
672       reject_approval (approval);
673     }
674 }
675
676 static void
677 invite_dialog_response_cb (GtkDialog *dialog,
678                            gint response,
679                            EventManagerApproval *approval)
680 {
681   gtk_widget_destroy (GTK_WIDGET (approval->dialog));
682   approval->dialog = NULL;
683
684   if (response != GTK_RESPONSE_OK)
685     {
686       /* close channel */
687       DEBUG ("Muc invitation rejected");
688
689       reject_approval (approval);
690
691       return;
692     }
693
694   DEBUG ("Muc invitation accepted");
695
696   /* We'll join the room when handling the channel */
697   event_manager_approval_approve (approval);
698 }
699
700 static void
701 event_room_channel_process_func (EventPriv *event)
702 {
703   GtkWidget *dialog, *button, *image;
704   TpChannel *channel = event->approval->main_channel;
705   gchar *title;
706
707   if (event->approval->dialog != NULL)
708     {
709       gtk_window_present (GTK_WINDOW (event->approval->dialog));
710       return;
711     }
712
713   /* create dialog */
714   dialog = gtk_message_dialog_new (NULL, 0,
715       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
716
717   title = g_strdup_printf (_("Invitation to join %s"),
718       tp_channel_get_identifier (channel));
719
720   gtk_window_set_title (GTK_WINDOW (dialog), title);
721   g_free (title);
722
723   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
724       _("%s is inviting you to join %s"),
725       empathy_contact_get_alias (event->approval->contact),
726       tp_channel_get_identifier (channel));
727
728   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
729       GTK_RESPONSE_OK);
730
731   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
732       _("_Decline"), GTK_RESPONSE_CANCEL);
733   image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
734   gtk_button_set_image (GTK_BUTTON (button), image);
735
736   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
737       _("_Join"), GTK_RESPONSE_OK);
738   image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
739   gtk_button_set_image (GTK_BUTTON (button), image);
740
741   g_signal_connect (dialog, "response",
742       G_CALLBACK (invite_dialog_response_cb), event->approval);
743
744   gtk_widget_show (dialog);
745
746   event->approval->dialog = dialog;
747 }
748
749 static void
750 display_invite_room_dialog (EventManagerApproval *approval)
751 {
752   const gchar *invite_msg;
753   gchar *msg;
754   TpContact *self_contact;
755   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
756
757   self_contact = tp_channel_group_get_self_contact (approval->main_channel);
758   tp_channel_group_get_local_pending_contact_info (approval->main_channel,
759       self_contact, NULL, NULL, &invite_msg);
760
761   if (approval->contact != NULL)
762     {
763       msg = g_strdup_printf (_("%s invited you to join %s"),
764           empathy_contact_get_alias (approval->contact),
765           tp_channel_get_identifier (approval->main_channel));
766     }
767   else
768     {
769       msg = g_strdup_printf (_("You have been invited to join %s"),
770           tp_channel_get_identifier (approval->main_channel));
771     }
772
773   event_manager_add (approval->manager, NULL,
774       approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
775       EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
776       event_room_channel_process_func, NULL);
777
778   empathy_sound_manager_play (priv->sound_mgr, NULL,
779       EMPATHY_SOUND_CONVERSATION_NEW);
780
781   g_free (msg);
782 }
783
784 static void
785 event_manager_auth_process_func (EventPriv *event)
786 {
787   empathy_event_approve ((EmpathyEvent *) event);
788 }
789
790 /* If there is a file-transfer, media, or auth channel consider it as
791  * the main one. */
792 static TpChannel *
793 find_main_channel (GList *channels)
794 {
795   GList *l;
796   TpChannel *text = NULL;
797
798   for (l = channels; l != NULL; l = g_list_next (l))
799     {
800       TpChannel *channel = l->data;
801       GQuark channel_type;
802
803       if (tp_proxy_get_invalidated (channel) != NULL)
804         continue;
805
806       channel_type = tp_channel_get_channel_type_id (channel);
807
808       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
809           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
810           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
811           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
812         return channel;
813
814       else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
815         text = channel;
816     }
817
818   return text;
819 }
820
821 static void
822 approve_text_channel (EmpathyEventManager *self,
823     EventManagerApproval *approval,
824     TpAddDispatchOperationContext *context,
825     EmpathyTpChat *tp_chat)
826 {
827   GList *messages, *l;
828
829   approval->handler_instance = g_object_ref (tp_chat);
830
831   if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
832     {
833       /* Are we in local-pending ? */
834       TpContact *inviter;
835
836       if (empathy_tp_chat_is_invited (tp_chat, &inviter))
837         {
838           /* We are invited to a room */
839           DEBUG ("Have been invited to %s. Ask user if he wants to accept",
840               tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
841
842           if (inviter != NULL)
843             {
844               approval->contact = empathy_contact_dup_from_tp_contact (
845                   inviter);
846             }
847
848           display_invite_room_dialog (approval);
849           tp_add_dispatch_operation_context_accept (context);
850
851           return;
852         }
853
854       /* We are not invited, approve the channel right now */
855       tp_add_dispatch_operation_context_accept (context);
856
857       approval->auto_approved = TRUE;
858       event_manager_approval_approve (approval);
859       return;
860     }
861
862   /* 1-1 text channel, wait for the first message */
863   approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
864     G_CALLBACK (event_manager_chat_message_received_cb), approval);
865
866   messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
867   for (l = messages; l != NULL; l = g_list_next (l))
868     {
869       EmpathyMessage *msg = l->data;
870
871       event_manager_chat_message_received_cb (tp_chat, msg, approval);
872     }
873
874   tp_add_dispatch_operation_context_accept (context);
875 }
876
877 static void
878 approval_set_target_contact (EventManagerApproval *approval,
879     TpChannel *channel)
880 {
881   TpContact *contact;
882
883   contact = tp_channel_get_target_contact (channel);
884   approval->contact = empathy_contact_dup_from_tp_contact (contact);
885 }
886
887 static void
888 approve_call_channel (EmpathyEventManager *self,
889     EventManagerApproval *approval,
890     TpAddDispatchOperationContext *context,
891     TpCallChannel *call)
892 {
893   TpChannel *channel = TP_CHANNEL (call);
894   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
895   gchar *header;
896   gboolean video;
897
898   approval->handler_instance = g_object_ref (call);
899   approval_set_target_contact (approval, channel);
900
901   tp_add_dispatch_operation_context_accept (context);
902
903   if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
904     {
905       DEBUG ("Call already ended, seems we missed it :/");
906       reject_approval (approval);
907       return;
908     }
909
910   approval->handler = g_signal_connect (call, "state-changed",
911     G_CALLBACK (event_manager_call_state_changed_cb), approval);
912
913   g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
914
915   header = g_strdup_printf (
916     video ? _("Incoming video call from %s") :_("Incoming call from %s"),
917     empathy_contact_get_alias (approval->contact));
918
919   event_manager_add (approval->manager, NULL,
920       approval->contact, EMPATHY_EVENT_TYPE_CALL,
921       video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
922       header, NULL, approval,
923       event_channel_process_voip_func, NULL);
924
925   g_free (header);
926
927   priv->ringing++;
928   if (priv->ringing == 1)
929     empathy_sound_manager_start_playing (priv->sound_mgr, NULL,
930         EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
931 }
932
933 static void
934 approve_ft_channel (EmpathyEventManager *self,
935     EventManagerApproval *approval,
936     TpAddDispatchOperationContext *context,
937     TpFileTransferChannel *ft)
938 {
939   TpChannel *channel = TP_CHANNEL (ft);
940   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
941   gchar *header;
942
943   approval->handler_instance = g_object_ref (ft);
944   approval_set_target_contact (approval, channel);
945
946   tp_add_dispatch_operation_context_accept (context);
947
948   header = g_strdup_printf (_("Incoming file transfer from %s"),
949       empathy_contact_get_alias (approval->contact));
950
951   event_manager_add (approval->manager, NULL,
952       approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
953       EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
954       approval, event_channel_process_func, NULL);
955
956   /* FIXME better sound for incoming file transfers ?*/
957   empathy_sound_manager_play (priv->sound_mgr, NULL,
958       EMPATHY_SOUND_CONVERSATION_NEW);
959
960   g_free (header);
961 }
962
963 static void
964 approve_sasl_channel (EmpathyEventManager *self,
965     TpAccount *account,
966     EventManagerApproval *approval,
967     TpAddDispatchOperationContext *context,
968     TpChannel *channel)
969 {
970   if (empathy_sasl_channel_supports_mechanism (channel, "X-TELEPATHY-PASSWORD"))
971     {
972       event_manager_add (approval->manager, account, NULL,
973           EMPATHY_EVENT_TYPE_AUTH,
974           GTK_STOCK_DIALOG_AUTHENTICATION,
975           tp_account_get_display_name (account),
976           _("Password required"), approval,
977           event_manager_auth_process_func, NULL);
978     }
979   else
980     {
981       GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
982           "Support only X-TELEPATHY-PASSWORD auth method" };
983
984       tp_add_dispatch_operation_context_fail (context, &error);
985       return;
986     }
987
988   tp_add_dispatch_operation_context_accept (context);
989 }
990
991 static void
992 approve_channels (TpSimpleApprover *approver,
993     TpAccount *account,
994     TpConnection *connection,
995     GList *channels,
996     TpChannelDispatchOperation *dispatch_operation,
997     TpAddDispatchOperationContext *context,
998     gpointer user_data)
999 {
1000   EmpathyEventManager *self = user_data;
1001   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1002   TpChannel *channel;
1003   EventManagerApproval *approval;
1004   GQuark channel_type;
1005
1006   channel = find_main_channel (channels);
1007   if (channel == NULL)
1008     {
1009       GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1010           "Unknown channel type" };
1011
1012       DEBUG ("Failed to find the main channel; ignoring");
1013
1014       tp_add_dispatch_operation_context_fail (context, &error);
1015       return;
1016     }
1017
1018   approval = event_manager_approval_new (self, dispatch_operation, channel);
1019   priv->approvals = g_slist_prepend (priv->approvals, approval);
1020
1021   approval->invalidated_handler = g_signal_connect (dispatch_operation,
1022       "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1023
1024   channel_type = tp_channel_get_channel_type_id (channel);
1025
1026   if (EMPATHY_IS_TP_CHAT (channel))
1027     {
1028       approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1029     }
1030   else if (TP_IS_CALL_CHANNEL (channel))
1031     {
1032       approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1033     }
1034   else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1035     {
1036       approve_ft_channel (self, approval, context,
1037           TP_FILE_TRANSFER_CHANNEL (channel));
1038     }
1039   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1040     {
1041       approve_sasl_channel (self, account, approval, context, channel);
1042     }
1043   else
1044     {
1045       GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1046           "Invalid channel type" };
1047
1048       DEBUG ("Unknown channel type (%s), ignoring..",
1049           g_quark_to_string (channel_type));
1050
1051       tp_add_dispatch_operation_context_fail (context, &error);
1052       return;
1053     }
1054 }
1055
1056 static void
1057 event_pending_subscribe_func (EventPriv *event)
1058 {
1059   GtkWidget *dialog;
1060   FolksIndividual *individual;
1061
1062   individual = empathy_ensure_individual_from_tp_contact (
1063       empathy_contact_get_tp_contact (event->public.contact));
1064
1065   dialog = empathy_subscription_dialog_new (individual, event->public.message);
1066   gtk_window_present (GTK_WINDOW (dialog));
1067
1068   event_remove (event);
1069
1070   g_object_unref (individual);
1071 }
1072
1073 static void
1074 check_publish_state (EmpathyEventManager *self,
1075     TpContact *tp_contact)
1076 {
1077   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1078   gchar *header, *event_msg;
1079   TpSubscriptionState state;
1080   EmpathyContact *contact;
1081   const gchar *message;
1082
1083   state = tp_contact_get_publish_state (tp_contact);
1084
1085   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1086
1087   if (state != TP_SUBSCRIPTION_STATE_ASK)
1088     {
1089       GSList *l;
1090
1091       for (l = priv->events; l; l = l->next)
1092         {
1093           EventPriv *event = l->data;
1094
1095           if (event->public.contact == contact &&
1096               event->func == event_pending_subscribe_func)
1097             {
1098               event_remove (event);
1099               break;
1100             }
1101         }
1102
1103       goto out;
1104     }
1105
1106   header = g_strdup_printf (
1107       _("%s would like permission to see when you are online"),
1108       empathy_contact_get_alias (contact));
1109
1110   message = tp_contact_get_publish_request (tp_contact);
1111
1112   if (!EMP_STR_EMPTY (message))
1113     event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1114   else
1115     event_msg = NULL;
1116
1117   event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1118       GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1119       event_pending_subscribe_func, NULL);
1120
1121   g_free (event_msg);
1122   g_free (header);
1123
1124 out:
1125   g_object_unref (contact);
1126 }
1127
1128 static void
1129 event_manager_publish_state_changed_cb (TpContact *contact,
1130     GParamSpec *spec,
1131     EmpathyEventManager *self)
1132 {
1133   check_publish_state (self, contact);
1134 }
1135
1136 static void
1137 event_manager_presence_changed_cb (EmpathyContact *contact,
1138     TpConnectionPresenceType current,
1139     TpConnectionPresenceType previous,
1140     EmpathyEventManager *manager)
1141 {
1142   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1143   TpAccount *account;
1144   EmpathyPresenceManager *presence_mgr;
1145
1146   account = empathy_contact_get_account (contact);
1147   presence_mgr = empathy_presence_manager_dup_singleton ();
1148
1149   if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1150     goto out;
1151
1152   if (tp_connection_presence_type_cmp_availability (previous,
1153         TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1154     {
1155       /* contact was online */
1156       if (tp_connection_presence_type_cmp_availability (current,
1157           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1158         {
1159           /* someone is logging off */
1160           empathy_sound_manager_play (priv->sound_mgr, NULL,
1161               EMPATHY_SOUND_CONTACT_DISCONNECTED);
1162
1163           if (g_settings_get_boolean (priv->gsettings_notif,
1164                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1165             {
1166               event_manager_add (manager, NULL, contact,
1167                   EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1168                   EMPATHY_IMAGE_AVATAR_DEFAULT,
1169                   empathy_contact_get_alias (contact), _("Disconnected"),
1170                   NULL, NULL, NULL);
1171             }
1172         }
1173     }
1174   else
1175     {
1176       /* contact was offline */
1177       if (tp_connection_presence_type_cmp_availability (current,
1178             TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1179         {
1180           /* someone is logging in */
1181           empathy_sound_manager_play (priv->sound_mgr, NULL,
1182               EMPATHY_SOUND_CONTACT_CONNECTED);
1183
1184           if (g_settings_get_boolean (priv->gsettings_notif,
1185                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1186             {
1187               event_manager_add (manager, NULL, contact,
1188                   EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1189                   EMPATHY_IMAGE_AVATAR_DEFAULT,
1190                   empathy_contact_get_alias (contact), _("Connected"),
1191                   NULL, NULL, NULL);
1192             }
1193         }
1194     }
1195
1196 out:
1197   g_object_unref (presence_mgr);
1198 }
1199
1200 static GObject *
1201 event_manager_constructor (GType type,
1202                            guint n_props,
1203                            GObjectConstructParam *props)
1204 {
1205         GObject *retval;
1206
1207         if (manager_singleton) {
1208                 retval = g_object_ref (manager_singleton);
1209         } else {
1210                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1211                         (type, n_props, props);
1212
1213                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1214                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1215         }
1216
1217         return retval;
1218 }
1219
1220 static void
1221 event_manager_finalize (GObject *object)
1222 {
1223   EmpathyEventManagerPriv *priv = GET_PRIV (object);
1224
1225   if (priv->ringing > 0)
1226     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1227
1228   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1229   g_slist_free (priv->events);
1230   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1231   g_slist_free (priv->approvals);
1232   g_object_unref (priv->conn_aggregator);
1233   g_object_unref (priv->approver);
1234   g_object_unref (priv->auth_approver);
1235   g_object_unref (priv->gsettings_notif);
1236   g_object_unref (priv->gsettings_ui);
1237   g_object_unref (priv->sound_mgr);
1238   g_hash_table_unref (priv->contacts);
1239 }
1240
1241 static void
1242 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1243 {
1244   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1245
1246   object_class->finalize = event_manager_finalize;
1247   object_class->constructor = event_manager_constructor;
1248
1249   signals[EVENT_ADDED] =
1250     g_signal_new ("event-added",
1251       G_TYPE_FROM_CLASS (klass),
1252       G_SIGNAL_RUN_LAST,
1253       0,
1254       NULL, NULL,
1255       g_cclosure_marshal_generic,
1256       G_TYPE_NONE,
1257       1, G_TYPE_POINTER);
1258
1259   signals[EVENT_REMOVED] =
1260   g_signal_new ("event-removed",
1261       G_TYPE_FROM_CLASS (klass),
1262       G_SIGNAL_RUN_LAST,
1263       0,
1264       NULL, NULL,
1265       g_cclosure_marshal_generic,
1266       G_TYPE_NONE, 1, G_TYPE_POINTER);
1267
1268   signals[EVENT_UPDATED] =
1269   g_signal_new ("event-updated",
1270       G_TYPE_FROM_CLASS (klass),
1271       G_SIGNAL_RUN_LAST,
1272       0,
1273       NULL, NULL,
1274       g_cclosure_marshal_generic,
1275       G_TYPE_NONE, 1, G_TYPE_POINTER);
1276
1277   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1278 }
1279
1280 static void
1281 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1282     GPtrArray *added,
1283     GPtrArray *removed,
1284     EmpathyEventManager *self)
1285 {
1286   EmpathyEventManagerPriv *priv = GET_PRIV (self);
1287   guint i;
1288
1289   for (i = 0; i < added->len; i++)
1290     {
1291       TpContact *tp_contact = g_ptr_array_index (added, i);
1292       EmpathyContact *contact;
1293
1294       if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1295         continue;
1296
1297       contact = empathy_contact_dup_from_tp_contact (tp_contact);
1298
1299       tp_g_signal_connect_object (contact, "presence-changed",
1300           G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1301
1302       tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1303           G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1304
1305       check_publish_state (self, tp_contact);
1306
1307       /* Pass ownership to the hash table */
1308       g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1309     }
1310
1311   for (i = 0; i < removed->len; i++)
1312     {
1313       TpContact *tp_contact = g_ptr_array_index (removed, i);
1314       EmpathyContact *contact;
1315
1316       contact = g_hash_table_lookup (priv->contacts, tp_contact);
1317       if (contact == NULL)
1318         continue;
1319
1320       g_signal_handlers_disconnect_by_func (contact,
1321           event_manager_presence_changed_cb, self);
1322
1323       g_signal_handlers_disconnect_by_func (tp_contact,
1324           event_manager_publish_state_changed_cb, self);
1325
1326       g_hash_table_remove (priv->contacts, tp_contact);
1327     }
1328 }
1329
1330 static void
1331 empathy_event_manager_init (EmpathyEventManager *manager)
1332 {
1333   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1334     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1335   GError *error = NULL;
1336   TpAccountManager *am;
1337   GPtrArray *contacts, *empty;
1338
1339   manager->priv = priv;
1340
1341   priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1342   priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1343
1344   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1345
1346   priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1347       g_object_unref);
1348
1349   priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1350
1351   tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1352       G_CALLBACK (contact_list_changed_cb), manager, 0);
1353
1354   contacts = empathy_connection_aggregator_dup_all_contacts (
1355       priv->conn_aggregator);
1356
1357   empty = g_ptr_array_new ();
1358
1359   contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1360
1361   g_ptr_array_unref (contacts);
1362   g_ptr_array_unref (empty);
1363
1364    am = tp_account_manager_dup ();
1365
1366   priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1367       FALSE, approve_channels, manager, NULL);
1368
1369   /* Private text channels */
1370   tp_base_client_take_approver_filter (priv->approver,
1371       tp_asv_new (
1372         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1373         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1374         NULL));
1375
1376   /* Muc text channels */
1377   tp_base_client_take_approver_filter (priv->approver,
1378       tp_asv_new (
1379         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1380         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1381         NULL));
1382
1383   /* File transfer */
1384   tp_base_client_take_approver_filter (priv->approver,
1385       tp_asv_new (
1386         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1387           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1388         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1389         NULL));
1390
1391   /* Calls */
1392   tp_base_client_take_approver_filter (priv->approver,
1393       tp_asv_new (
1394         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1395           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1396         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1397         NULL));
1398   tp_base_client_take_approver_filter (priv->approver,
1399       tp_asv_new (
1400         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1401           TP_IFACE_CHANNEL_TYPE_CALL,
1402         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1403         NULL));
1404
1405   /* I don't feel good about doing this, and I'm sorry, but the
1406    * capabilities connection feature is added earlier because it's
1407    * needed for EmpathyTpChat. If the capabilities feature is required
1408    * then preparing an auth channel (which of course appears in the
1409    * CONNECTING state) will never be prepared. So the options are
1410    * either to create another approver like I've done, or to port
1411    * EmpathyTpChat and its users to not depend on the connection being
1412    * prepared with capabilities. I chose the former, obviously. :-) */
1413
1414   priv->auth_approver = tp_simple_approver_new_with_am (am,
1415       "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1416       NULL);
1417
1418   /* SASL auth channels */
1419   tp_base_client_take_approver_filter (priv->auth_approver,
1420       tp_asv_new (
1421         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1422           TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1423         TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1424           G_TYPE_STRING,
1425           TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1426         NULL));
1427
1428   if (!tp_base_client_register (priv->approver, &error))
1429     {
1430       DEBUG ("Failed to register Approver: %s", error->message);
1431       g_error_free (error);
1432     }
1433
1434   if (!tp_base_client_register (priv->auth_approver, &error))
1435     {
1436       DEBUG ("Failed to register auth Approver: %s", error->message);
1437       g_error_free (error);
1438     }
1439
1440   g_object_unref (am);
1441 }
1442
1443 EmpathyEventManager *
1444 empathy_event_manager_dup_singleton (void)
1445 {
1446   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1447 }
1448
1449 GSList *
1450 empathy_event_manager_get_events (EmpathyEventManager *manager)
1451 {
1452   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1453
1454   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1455
1456   return priv->events;
1457 }
1458
1459 EmpathyEvent *
1460 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1461 {
1462   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1463
1464   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1465
1466   return priv->events ? priv->events->data : NULL;
1467 }
1468
1469 void
1470 empathy_event_activate (EmpathyEvent *event_public)
1471 {
1472   EventPriv *event = (EventPriv *) event_public;
1473
1474   g_return_if_fail (event_public != NULL);
1475
1476   if (event->func)
1477     event->func (event);
1478   else
1479     event_remove (event);
1480 }
1481
1482 void
1483 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1484 {
1485   EventPriv *event = (EventPriv *) event_public;
1486
1487   g_return_if_fail (event_public != NULL);
1488
1489   event->inhibit = TRUE;
1490 }
1491
1492 void
1493 empathy_event_approve (EmpathyEvent *event_public)
1494 {
1495   EventPriv *event = (EventPriv *) event_public;
1496
1497   g_return_if_fail (event_public != NULL);
1498
1499   event_manager_approval_approve (event->approval);
1500 }
1501
1502 void
1503 empathy_event_decline (EmpathyEvent *event_public)
1504 {
1505   EventPriv *event = (EventPriv *) event_public;
1506
1507   g_return_if_fail (event_public != NULL);
1508
1509   reject_approval (event->approval);
1510 }