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