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