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