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