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