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