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