]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
pass message to empathy_subscription_dialog_show
[empathy.git] / src / empathy-event-manager.c
1 /*
2  * Copyright (C) 2007-2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/simple-approver.h>
31
32 #include <libempathy/empathy-idle.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-call.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-call-factory.h>
40 #include <libempathy/empathy-gsettings.h>
41
42 #include <extensions/extensions.h>
43
44 #include <libempathy-gtk/empathy-images.h>
45 #include <libempathy-gtk/empathy-contact-dialogs.h>
46 #include <libempathy-gtk/empathy-sound.h>
47
48 #include "empathy-event-manager.h"
49 #include "empathy-main-window.h"
50
51 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
52 #include <libempathy/empathy-debug.h>
53
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
55
56 #define NOTIFICATION_TIMEOUT 2 /* seconds */
57
58 /* The time interval in milliseconds between 2 incoming rings */
59 #define MS_BETWEEN_RING 500
60
61 typedef struct {
62   EmpathyEventManager *manager;
63   TpChannelDispatchOperation *operation;
64   gulong invalidated_handler;
65   /* Remove contact if applicable */
66   EmpathyContact *contact;
67   /* option signal handler and it's instance */
68   gulong handler;
69   GObject *handler_instance;
70   /* optional accept widget */
71   GtkWidget *dialog;
72   /* Channel of the CDO that will be used during the approval */
73   TpChannel *main_channel;
74   gboolean auto_approved;
75 } EventManagerApproval;
76
77 typedef struct {
78   TpBaseClient *approver;
79   EmpathyContactManager *contact_manager;
80   GSList *events;
81   /* Ongoing approvals */
82   GSList *approvals;
83
84   gint ringing;
85 } EmpathyEventManagerPriv;
86
87 typedef struct _EventPriv EventPriv;
88 typedef void (*EventFunc) (EventPriv *event);
89
90 struct _EventPriv {
91   EmpathyEvent public;
92   EmpathyEventManager *manager;
93   EventManagerApproval *approval;
94   EventFunc func;
95   gboolean inhibit;
96   gpointer user_data;
97   guint autoremove_timeout_id;
98 };
99
100 enum {
101   EVENT_ADDED,
102   EVENT_REMOVED,
103   EVENT_UPDATED,
104   LAST_SIGNAL
105 };
106
107 static guint signals[LAST_SIGNAL];
108
109 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
110
111 static EmpathyEventManager * manager_singleton = NULL;
112
113 static EventManagerApproval *
114 event_manager_approval_new (EmpathyEventManager *manager,
115   TpChannelDispatchOperation *operation,
116   TpChannel *main_channel)
117 {
118   EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
119   result->operation = g_object_ref (operation);
120   result->manager = manager;
121   result->main_channel = g_object_ref (main_channel);
122
123   return result;
124 }
125
126 static void
127 event_manager_approval_free (EventManagerApproval *approval)
128 {
129   g_signal_handler_disconnect (approval->operation,
130     approval->invalidated_handler);
131   g_object_unref (approval->operation);
132
133   g_object_unref (approval->main_channel);
134
135   if (approval->handler != 0)
136     g_signal_handler_disconnect (approval->handler_instance,
137       approval->handler);
138
139   if (approval->handler_instance != NULL)
140     g_object_unref (approval->handler_instance);
141
142   if (approval->contact != NULL)
143     g_object_unref (approval->contact);
144
145   if (approval->dialog != NULL)
146     {
147       gtk_widget_destroy (approval->dialog);
148     }
149
150   g_slice_free (EventManagerApproval, approval);
151 }
152
153 static void
154 event_free (EventPriv *event)
155 {
156   g_free (event->public.icon_name);
157   g_free (event->public.header);
158   g_free (event->public.message);
159
160   if (event->autoremove_timeout_id != 0)
161     g_source_remove (event->autoremove_timeout_id);
162
163   if (event->public.contact)
164     {
165       g_object_unref (event->public.contact);
166     }
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 (void)
201
202 {
203   GSettings *gsettings;
204   gboolean result;
205
206   gsettings = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
207
208   result = g_settings_get_boolean (gsettings,
209       EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
210   g_object_unref (gsettings);
211
212   return result;
213 }
214
215 static void
216 event_manager_add (EmpathyEventManager *manager,
217     EmpathyContact *contact,
218     EmpathyEventType type,
219     const gchar *icon_name,
220     const gchar *header,
221     const gchar *message,
222     EventManagerApproval *approval,
223     EventFunc func,
224     gpointer user_data)
225 {
226   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
227   EventPriv               *event;
228
229   event = g_slice_new0 (EventPriv);
230   event->public.contact = contact ? g_object_ref (contact) : NULL;
231   event->public.type = type;
232   event->public.icon_name = g_strdup (icon_name);
233   event->public.header = g_strdup (header);
234   event->public.message = g_strdup (message);
235   event->public.must_ack = (func != NULL);
236   event->inhibit = FALSE;
237   event->func = func;
238   event->user_data = user_data;
239   event->manager = manager;
240   event->approval = approval;
241
242   DEBUG ("Adding event %p", event);
243   priv->events = g_slist_prepend (priv->events, event);
244
245   if (!display_notify_area ())
246     {
247       /* Don't fire the 'event-added' signal as we activate the event now */
248       if (approval != NULL)
249         approval->auto_approved = TRUE;
250
251       empathy_event_activate (&event->public);
252       return;
253     }
254
255   g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
256
257   if (!event->public.must_ack)
258     {
259       event->autoremove_timeout_id = g_timeout_add_seconds (
260           NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
261           event);
262     }
263 }
264
265 static void
266 handle_with_cb (GObject *source,
267     GAsyncResult *result,
268     gpointer user_data)
269 {
270   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
271   GError *error = NULL;
272
273   if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
274     {
275       DEBUG ("HandleWith failed: %s\n", error->message);
276       g_error_free (error);
277     }
278 }
279
280 static void
281 handle_with_time_cb (GObject *source,
282     GAsyncResult *result,
283     gpointer user_data)
284 {
285   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
286   GError *error = NULL;
287
288   if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
289         &error))
290     {
291       if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
292         {
293           EventManagerApproval *approval = user_data;
294
295           DEBUG ("HandleWithTime() is not implemented, falling back to "
296               "HandleWith(). Please upgrade to telepathy-mission-control "
297               "5.5.0 or later");
298
299           tp_channel_dispatch_operation_handle_with_async (approval->operation,
300               NULL, handle_with_cb, approval);
301         }
302       else
303         {
304           DEBUG ("HandleWithTime failed: %s\n", error->message);
305         }
306       g_error_free (error);
307     }
308 }
309
310 static void
311 event_manager_approval_approve (EventManagerApproval *approval)
312 {
313   gint64 timestamp;
314
315   if (approval->auto_approved)
316     {
317       timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
318     }
319   else
320     {
321       timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
322     }
323
324   g_assert (approval->operation != NULL);
325
326   tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
327       NULL, timestamp, handle_with_time_cb, approval);
328 }
329
330 static void
331 event_channel_process_func (EventPriv *event)
332 {
333   event_manager_approval_approve (event->approval);
334 }
335
336 static void
337 event_text_channel_process_func (EventPriv *event)
338 {
339   EmpathyTpChat *tp_chat;
340   gint64 timestamp;
341
342   timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
343
344   if (event->approval->handler != 0)
345     {
346       tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
347
348       g_signal_handler_disconnect (tp_chat, event->approval->handler);
349       event->approval->handler = 0;
350     }
351
352   event_manager_approval_approve (event->approval);
353 }
354
355 static EventPriv *
356 event_lookup_by_approval (EmpathyEventManager *manager,
357   EventManagerApproval *approval)
358 {
359   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
360   GSList *l;
361   EventPriv *retval = NULL;
362
363   for (l = priv->events; l; l = l->next)
364     {
365       EventPriv *event = l->data;
366
367       if (event->approval == approval)
368         {
369           retval = event;
370           break;
371         }
372     }
373
374   return retval;
375 }
376
377 static void
378 event_update (EmpathyEventManager *manager, EventPriv *event,
379   const char *icon_name, const char *header, const char *msg)
380 {
381   g_free (event->public.icon_name);
382   g_free (event->public.header);
383   g_free (event->public.message);
384
385   event->public.icon_name = g_strdup (icon_name);
386   event->public.header = g_strdup (header);
387   event->public.message = g_strdup (msg);
388
389   g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
390 }
391
392 static void
393 reject_channel_claim_cb (GObject *source,
394     GAsyncResult *result,
395     gpointer user_data)
396 {
397   TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
398   GError *error = NULL;
399
400   if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
401     {
402       DEBUG ("Failed to claim channel: %s", error->message);
403
404       g_error_free (error);
405       goto out;
406     }
407
408   if (EMPATHY_IS_TP_CALL (user_data))
409     {
410       empathy_tp_call_close (user_data);
411     }
412   else if (EMPATHY_IS_TP_CHAT (user_data))
413     {
414       empathy_tp_chat_leave (user_data);
415     }
416   else if (EMPATHY_IS_TP_FILE (user_data))
417     {
418       empathy_tp_file_close (user_data);
419     }
420
421 out:
422   g_object_unref (user_data);
423 }
424
425 static void
426 reject_approval (EventManagerApproval *approval)
427 {
428   /* We have to claim the channel before closing it */
429   tp_channel_dispatch_operation_claim_async (approval->operation,
430       reject_channel_claim_cb, g_object_ref (approval->handler_instance));
431 }
432
433 static void
434 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
435   gint response, gpointer user_data)
436 {
437   EventManagerApproval *approval = user_data;
438
439   gtk_widget_destroy (approval->dialog);
440   approval->dialog = NULL;
441
442   if (response != GTK_RESPONSE_ACCEPT)
443     {
444       reject_approval (approval);
445     }
446   else
447     {
448       event_manager_approval_approve (approval);
449     }
450 }
451
452 static void
453 event_channel_process_voip_func (EventPriv *event)
454 {
455   GtkWidget *dialog;
456   GtkWidget *button;
457   GtkWidget *image;
458   EmpathyTpCall *call;
459   gboolean video;
460   gchar *title;
461
462   if (event->approval->dialog != NULL)
463     {
464       gtk_window_present (GTK_WINDOW (event->approval->dialog));
465       return;
466     }
467
468   call = EMPATHY_TP_CALL (event->approval->handler_instance);
469
470   video = empathy_tp_call_has_initial_video (call);
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   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
513       _("_Answer"), GTK_RESPONSE_ACCEPT);
514
515   image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
516   gtk_button_set_image (GTK_BUTTON (button), image);
517
518   g_signal_connect (dialog, "response",
519       G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
520       event->approval);
521
522   gtk_widget_show (dialog);
523
524   event->approval->dialog = dialog;
525 }
526
527 static void
528 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
529   EmpathyMessage *message,
530   EventManagerApproval *approval)
531 {
532   GtkWidget       *window = empathy_main_window_dup ();
533   EmpathyContact  *sender;
534   const gchar     *header;
535   const gchar     *msg;
536   TpChannel       *channel;
537   EventPriv       *event;
538
539   /* try to update the event if it's referring to a chat which is already in the
540    * queue. */
541   event = event_lookup_by_approval (approval->manager, approval);
542
543   sender = empathy_message_get_sender (message);
544   header = empathy_contact_get_alias (sender);
545   msg = empathy_message_get_body (message);
546
547   channel = empathy_tp_chat_get_channel (tp_chat);
548
549   if (event != NULL)
550     event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
551         msg);
552   else
553     event_manager_add (approval->manager, sender, EMPATHY_EVENT_TYPE_CHAT,
554         EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval,
555         event_text_channel_process_func, NULL);
556
557   empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
558
559   g_object_unref (window);
560 }
561
562 static void
563 event_manager_approval_done (EventManagerApproval *approval)
564 {
565   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
566   GSList                  *l;
567
568   if (approval->operation != NULL)
569     {
570       GQuark channel_type;
571
572       channel_type = tp_channel_get_channel_type_id (approval->main_channel);
573
574       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
575         {
576           priv->ringing--;
577           if (priv->ringing == 0)
578             empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
579         }
580     }
581
582   priv->approvals = g_slist_remove (priv->approvals, approval);
583
584   for (l = priv->events; l; l = l->next)
585     {
586       EventPriv *event = l->data;
587
588       if (event->approval == approval)
589         {
590           event_remove (event);
591           break;
592         }
593     }
594
595   event_manager_approval_free (approval);
596 }
597
598 static void
599 cdo_invalidated_cb (TpProxy *cdo,
600     guint domain,
601     gint code,
602     gchar *message,
603     EventManagerApproval *approval)
604 {
605   DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
606
607   event_manager_approval_done (approval);
608 }
609
610 static void
611 event_manager_media_channel_got_contact (EventManagerApproval *approval)
612 {
613   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
614   GtkWidget *window = empathy_main_window_dup ();
615   gchar *header;
616   EmpathyTpCall *call;
617   gboolean video;
618
619   call = EMPATHY_TP_CALL (approval->handler_instance);
620
621   video = empathy_tp_call_has_initial_video (call);
622
623   header = g_strdup_printf (
624     video ? _("Incoming video call from %s") :_("Incoming call from %s"),
625     empathy_contact_get_alias (approval->contact));
626
627   event_manager_add (approval->manager, approval->contact,
628       EMPATHY_EVENT_TYPE_VOIP,
629       video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
630       header, NULL, approval,
631       event_channel_process_voip_func, NULL);
632
633   g_free (header);
634
635   priv->ringing++;
636   if (priv->ringing == 1)
637     empathy_sound_start_playing (window,
638         EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
639
640   g_object_unref (window);
641 }
642
643 static void
644 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
645   GParamSpec *param, EventManagerApproval *approval)
646 {
647   EmpathyContact *contact;
648
649   g_object_get (G_OBJECT (call), "contact", &contact, NULL);
650
651   if (contact == NULL)
652     return;
653
654   approval->contact = contact;
655   event_manager_media_channel_got_contact (approval);
656 }
657
658 static void
659 invite_dialog_response_cb (GtkDialog *dialog,
660                            gint response,
661                            EventManagerApproval *approval)
662 {
663   EmpathyTpChat *tp_chat;
664
665   gtk_widget_destroy (GTK_WIDGET (approval->dialog));
666   approval->dialog = NULL;
667
668   tp_chat = EMPATHY_TP_CHAT (approval->handler_instance);
669
670   if (response != GTK_RESPONSE_OK)
671     {
672       /* close channel */
673       DEBUG ("Muc invitation rejected");
674
675       reject_approval (approval);
676
677       return;
678     }
679
680   DEBUG ("Muc invitation accepted");
681
682   /* We'll join the room when handling the channel */
683   event_manager_approval_approve (approval);
684 }
685
686 static void
687 event_room_channel_process_func (EventPriv *event)
688 {
689   GtkWidget *dialog, *button, *image;
690   TpChannel *channel = event->approval->main_channel;
691   gchar *title;
692
693   if (event->approval->dialog != NULL)
694     {
695       gtk_window_present (GTK_WINDOW (event->approval->dialog));
696       return;
697     }
698
699   /* create dialog */
700   dialog = gtk_message_dialog_new (NULL, 0,
701       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
702
703   title = g_strdup_printf (_("Invitation to join %s"),
704       tp_channel_get_identifier (channel));
705
706   gtk_window_set_title (GTK_WINDOW (dialog), title);
707   g_free (title);
708
709   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
710       _("%s is inviting you to join %s"),
711       empathy_contact_get_alias (event->approval->contact),
712       tp_channel_get_identifier (channel));
713
714   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
715       GTK_RESPONSE_OK);
716
717   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
718       _("_Decline"), GTK_RESPONSE_CANCEL);
719   image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
720   gtk_button_set_image (GTK_BUTTON (button), image);
721
722   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
723       _("_Join"), GTK_RESPONSE_OK);
724   image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
725   gtk_button_set_image (GTK_BUTTON (button), image);
726
727   g_signal_connect (dialog, "response",
728       G_CALLBACK (invite_dialog_response_cb), event->approval);
729
730   gtk_widget_show (dialog);
731
732   event->approval->dialog = dialog;
733 }
734
735 static void
736 display_invite_room_dialog (EventManagerApproval *approval)
737 {
738   GtkWidget *window = empathy_main_window_dup ();
739   const gchar *invite_msg;
740   gchar *msg;
741   TpHandle self_handle;
742
743   self_handle = tp_channel_group_get_self_handle (approval->main_channel);
744   tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
745       NULL, NULL, &invite_msg);
746
747   if (approval->contact != NULL)
748     {
749       msg = g_strdup_printf (_("%s invited you to join %s"),
750           empathy_contact_get_alias (approval->contact),
751           tp_channel_get_identifier (approval->main_channel));
752     }
753   else
754     {
755       msg = g_strdup_printf (_("You have been invited to join %s"),
756           tp_channel_get_identifier (approval->main_channel));
757     }
758
759   event_manager_add (approval->manager, approval->contact,
760       EMPATHY_EVENT_TYPE_INVITATION, EMPATHY_IMAGE_GROUP_MESSAGE, msg,
761       invite_msg, approval, event_room_channel_process_func, NULL);
762
763   empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
764
765   g_free (msg);
766   g_object_unref (window);
767 }
768
769 static void
770 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
771                                          EmpathyContact *contact,
772                                          const GError *error,
773                                          gpointer user_data,
774                                          GObject *object)
775 {
776   EventManagerApproval *approval = (EventManagerApproval *) user_data;
777
778   if (error != NULL)
779     {
780       DEBUG ("Error: %s", error->message);
781     }
782   else
783     {
784       approval->contact = g_object_ref (contact);
785     }
786
787   display_invite_room_dialog (approval);
788 }
789
790 static void
791 event_manager_ft_got_contact_cb (TpConnection *connection,
792                                  EmpathyContact *contact,
793                                  const GError *error,
794                                  gpointer user_data,
795                                  GObject *object)
796 {
797   EventManagerApproval *approval = (EventManagerApproval *) user_data;
798   GtkWidget *window = empathy_main_window_dup ();
799   char *header;
800
801   approval->contact = g_object_ref (contact);
802
803   header = g_strdup_printf (_("Incoming file transfer from %s"),
804                             empathy_contact_get_alias (approval->contact));
805
806   event_manager_add (approval->manager, approval->contact,
807       EMPATHY_EVENT_TYPE_TRANSFER, EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
808       approval, event_channel_process_func, NULL);
809
810   /* FIXME better sound for incoming file transfers ?*/
811   empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
812
813   g_free (header);
814   g_object_unref (window);
815 }
816
817 /* If there is a file-transfer or media channel consider it as the
818  * main one. */
819 static TpChannel *
820 find_main_channel (GList *channels)
821 {
822   GList *l;
823   TpChannel *text = NULL;
824
825   for (l = channels; l != NULL; l = g_list_next (l))
826     {
827       TpChannel *channel = l->data;
828       GQuark channel_type;
829
830       if (tp_proxy_get_invalidated (channel) != NULL)
831         continue;
832
833       channel_type = tp_channel_get_channel_type_id (channel);
834
835       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
836           channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
837         return channel;
838
839       else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
840         text = channel;
841     }
842
843   return text;
844 }
845
846 static void
847 approve_channels (TpSimpleApprover *approver,
848     TpAccount *account,
849     TpConnection *connection,
850     GList *channels,
851     TpChannelDispatchOperation *dispatch_operation,
852     TpAddDispatchOperationContext *context,
853     gpointer user_data)
854 {
855   EmpathyEventManager *self = user_data;
856   EmpathyEventManagerPriv *priv = GET_PRIV (self);
857   TpChannel *channel;
858   EventManagerApproval *approval;
859   GQuark channel_type;
860
861   channel = find_main_channel (channels);
862   if (channel == NULL)
863     {
864       GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
865           "Unknown channel type" };
866
867       DEBUG ("Failed to find the main channel; ignoring");
868
869       tp_add_dispatch_operation_context_fail (context, &error);
870       return;
871     }
872
873   approval = event_manager_approval_new (self, dispatch_operation, channel);
874   priv->approvals = g_slist_prepend (priv->approvals, approval);
875
876   approval->invalidated_handler = g_signal_connect (dispatch_operation,
877       "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
878
879   channel_type = tp_channel_get_channel_type_id (channel);
880
881   if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
882     {
883       EmpathyTpChat *tp_chat;
884
885       tp_chat = empathy_tp_chat_new (account, channel);
886       approval->handler_instance = G_OBJECT (tp_chat);
887
888       if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
889         {
890           /* Are we in local-pending ? */
891           TpHandle inviter;
892
893           if (empathy_tp_chat_is_invited (tp_chat, &inviter))
894             {
895               /* We are invited to a room */
896               DEBUG ("Have been invited to %s. Ask user if he wants to accept",
897                   tp_channel_get_identifier (channel));
898
899               if (inviter != 0)
900                 {
901                   empathy_tp_contact_factory_get_from_handle (connection,
902                       inviter, event_manager_muc_invite_got_contact_cb,
903                       approval, NULL, G_OBJECT (self));
904                 }
905               else
906                 {
907                   display_invite_room_dialog (approval);
908                 }
909
910               goto out;
911             }
912
913           /* We are not invited, approve the channel right now */
914           tp_add_dispatch_operation_context_accept (context);
915
916           approval->auto_approved = TRUE;
917           event_manager_approval_approve (approval);
918           return;
919         }
920
921       /* 1-1 text channel, wait for the first message */
922       approval->handler = g_signal_connect (tp_chat, "message-received",
923         G_CALLBACK (event_manager_chat_message_received_cb), approval);
924     }
925   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
926     {
927       EmpathyContact *contact;
928       EmpathyTpCall *call = empathy_tp_call_new (account, channel);
929
930       approval->handler_instance = G_OBJECT (call);
931
932       g_object_get (G_OBJECT (call), "contact", &contact, NULL);
933
934       if (contact == NULL)
935         {
936           g_signal_connect (call, "notify::contact",
937             G_CALLBACK (event_manager_media_channel_contact_changed_cb),
938             approval);
939         }
940       else
941         {
942           approval->contact = contact;
943           event_manager_media_channel_got_contact (approval);
944         }
945
946     }
947   else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
948     {
949       TpHandle handle;
950       EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
951
952       approval->handler_instance = G_OBJECT (tp_file);
953
954       handle = tp_channel_get_handle (channel, NULL);
955
956       connection = tp_channel_borrow_connection (channel);
957       empathy_tp_contact_factory_get_from_handle (connection, handle,
958         event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
959     }
960   else
961     {
962       GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
963           "Invalid channel type" };
964
965       DEBUG ("Unknown channel type (%s), ignoring..",
966           g_quark_to_string (channel_type));
967
968       tp_add_dispatch_operation_context_fail (context, &error);
969       return;
970     }
971
972 out:
973   tp_add_dispatch_operation_context_accept (context);
974 }
975
976 static void
977 event_pending_subscribe_func (EventPriv *event)
978 {
979   empathy_subscription_dialog_show (event->public.contact, event->public.header,
980       NULL);
981   event_remove (event);
982 }
983
984 static void
985 event_manager_pendings_changed_cb (EmpathyContactList  *list,
986   EmpathyContact *contact, EmpathyContact *actor,
987   guint reason, gchar *message, gboolean is_pending,
988   EmpathyEventManager *manager)
989 {
990   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
991   gchar                   *header, *event_msg;
992
993   if (!is_pending)
994     {
995       GSList *l;
996
997       for (l = priv->events; l; l = l->next)
998         {
999           EventPriv *event = l->data;
1000
1001           if (event->public.contact == contact &&
1002               event->func == event_pending_subscribe_func)
1003             {
1004               event_remove (event);
1005               break;
1006             }
1007         }
1008
1009       return;
1010     }
1011
1012   header = g_strdup_printf (
1013       _("%s would like permission to see when you are available"),
1014       empathy_contact_get_alias (contact));
1015
1016   if (!EMP_STR_EMPTY (message))
1017     event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1018   else
1019     event_msg = NULL;
1020
1021   event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1022       GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1023       event_pending_subscribe_func, NULL);
1024
1025   g_free (event_msg);
1026   g_free (header);
1027 }
1028
1029 static void
1030 event_manager_presence_changed_cb (EmpathyContact *contact,
1031     TpConnectionPresenceType current,
1032     TpConnectionPresenceType previous,
1033     EmpathyEventManager *manager)
1034 {
1035   TpAccount *account;
1036   gchar *header = NULL;
1037   EmpathyIdle *idle;
1038   GSettings *gsettings = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1039   GtkWidget *window = empathy_main_window_dup ();
1040
1041   account = empathy_contact_get_account (contact);
1042   idle = empathy_idle_dup_singleton ();
1043
1044   if (empathy_idle_account_is_just_connected (idle, account))
1045     goto out;
1046
1047   if (tp_connection_presence_type_cmp_availability (previous,
1048         TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1049     {
1050       /* contact was online */
1051       if (tp_connection_presence_type_cmp_availability (current,
1052           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1053         {
1054           /* someone is logging off */
1055           empathy_sound_play (window, EMPATHY_SOUND_CONTACT_DISCONNECTED);
1056
1057           if (g_settings_get_boolean (gsettings,
1058                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1059             {
1060               header = g_strdup_printf (_("%s is now offline."),
1061                   empathy_contact_get_alias (contact));
1062
1063               event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1064                   EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1065             }
1066         }
1067     }
1068   else
1069     {
1070       /* contact was offline */
1071       if (tp_connection_presence_type_cmp_availability (current,
1072             TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1073         {
1074           /* someone is logging in */
1075           empathy_sound_play (window, EMPATHY_SOUND_CONTACT_CONNECTED);
1076
1077           if (g_settings_get_boolean (gsettings,
1078                 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1079             {
1080               header = g_strdup_printf (_("%s is now online."),
1081                   empathy_contact_get_alias (contact));
1082
1083               event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1084                   EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1085             }
1086         }
1087     }
1088   g_free (header);
1089
1090 out:
1091   g_object_unref (idle);
1092   g_object_unref (gsettings);
1093   g_object_unref (window);
1094 }
1095
1096 static void
1097 event_manager_members_changed_cb (EmpathyContactList  *list,
1098     EmpathyContact *contact,
1099     EmpathyContact *actor,
1100     guint reason,
1101     gchar *message,
1102     gboolean is_member,
1103     EmpathyEventManager *manager)
1104 {
1105   if (is_member)
1106     g_signal_connect (contact, "presence-changed",
1107         G_CALLBACK (event_manager_presence_changed_cb), manager);
1108   else
1109     g_signal_handlers_disconnect_by_func (contact,
1110         event_manager_presence_changed_cb, manager);
1111 }
1112
1113 static GObject *
1114 event_manager_constructor (GType type,
1115                            guint n_props,
1116                            GObjectConstructParam *props)
1117 {
1118         GObject *retval;
1119
1120         if (manager_singleton) {
1121                 retval = g_object_ref (manager_singleton);
1122         } else {
1123                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1124                         (type, n_props, props);
1125
1126                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1127                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1128         }
1129
1130         return retval;
1131 }
1132
1133 static void
1134 event_manager_finalize (GObject *object)
1135 {
1136   EmpathyEventManagerPriv *priv = GET_PRIV (object);
1137
1138   if (priv->ringing > 0)
1139     empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
1140
1141   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1142   g_slist_free (priv->events);
1143   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1144   g_slist_free (priv->approvals);
1145   g_object_unref (priv->contact_manager);
1146   g_object_unref (priv->approver);
1147 }
1148
1149 static void
1150 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1151 {
1152   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1153
1154   object_class->finalize = event_manager_finalize;
1155   object_class->constructor = event_manager_constructor;
1156
1157   signals[EVENT_ADDED] =
1158     g_signal_new ("event-added",
1159       G_TYPE_FROM_CLASS (klass),
1160       G_SIGNAL_RUN_LAST,
1161       0,
1162       NULL, NULL,
1163       g_cclosure_marshal_VOID__POINTER,
1164       G_TYPE_NONE,
1165       1, G_TYPE_POINTER);
1166
1167   signals[EVENT_REMOVED] =
1168   g_signal_new ("event-removed",
1169       G_TYPE_FROM_CLASS (klass),
1170       G_SIGNAL_RUN_LAST,
1171       0,
1172       NULL, NULL,
1173       g_cclosure_marshal_VOID__POINTER,
1174       G_TYPE_NONE, 1, G_TYPE_POINTER);
1175
1176   signals[EVENT_UPDATED] =
1177   g_signal_new ("event-updated",
1178       G_TYPE_FROM_CLASS (klass),
1179       G_SIGNAL_RUN_LAST,
1180       0,
1181       NULL, NULL,
1182       g_cclosure_marshal_VOID__POINTER,
1183       G_TYPE_NONE, 1, G_TYPE_POINTER);
1184
1185
1186   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1187 }
1188
1189 static void
1190 empathy_event_manager_init (EmpathyEventManager *manager)
1191 {
1192   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1193     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1194   TpDBusDaemon *dbus;
1195   GError *error = NULL;
1196
1197   manager->priv = priv;
1198
1199   priv->contact_manager = empathy_contact_manager_dup_singleton ();
1200   g_signal_connect (priv->contact_manager, "pendings-changed",
1201     G_CALLBACK (event_manager_pendings_changed_cb), manager);
1202
1203   g_signal_connect (priv->contact_manager, "members-changed",
1204     G_CALLBACK (event_manager_members_changed_cb), manager);
1205
1206   dbus = tp_dbus_daemon_dup (&error);
1207   if (dbus == NULL)
1208     {
1209       DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1210       g_error_free (error);
1211       return;
1212     }
1213
1214   priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1215       approve_channels, manager, NULL);
1216
1217   /* EmpathyTpChat relies on this feature being prepared */
1218   tp_base_client_add_connection_features_varargs (priv->approver,
1219     TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1220
1221   /* Private text channels */
1222   tp_base_client_take_approver_filter (priv->approver,
1223       tp_asv_new (
1224         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1225         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1226         NULL));
1227
1228   /* Muc text channels */
1229   tp_base_client_take_approver_filter (priv->approver,
1230       tp_asv_new (
1231         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1232         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1233         NULL));
1234
1235   /* File transfer */
1236   tp_base_client_take_approver_filter (priv->approver,
1237       tp_asv_new (
1238         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1239           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1240         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1241         NULL));
1242
1243   /* Calls */
1244   tp_base_client_take_approver_filter (priv->approver,
1245       tp_asv_new (
1246         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1247           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1248         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1249         NULL));
1250
1251   if (!tp_base_client_register (priv->approver, &error))
1252     {
1253       DEBUG ("Failed to register Approver: %s", error->message);
1254       g_error_free (error);
1255     }
1256
1257   g_object_unref (dbus);
1258 }
1259
1260 EmpathyEventManager *
1261 empathy_event_manager_dup_singleton (void)
1262 {
1263   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1264 }
1265
1266 GSList *
1267 empathy_event_manager_get_events (EmpathyEventManager *manager)
1268 {
1269   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1270
1271   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1272
1273   return priv->events;
1274 }
1275
1276 EmpathyEvent *
1277 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1278 {
1279   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1280
1281   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1282
1283   return priv->events ? priv->events->data : NULL;
1284 }
1285
1286 void
1287 empathy_event_activate (EmpathyEvent *event_public)
1288 {
1289   EventPriv *event = (EventPriv *) event_public;
1290
1291   g_return_if_fail (event_public != NULL);
1292
1293   if (event->func)
1294     event->func (event);
1295   else
1296     event_remove (event);
1297 }
1298
1299 void
1300 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1301 {
1302   EventPriv *event = (EventPriv *) event_public;
1303
1304   g_return_if_fail (event_public != NULL);
1305
1306   event->inhibit = TRUE;
1307 }
1308
1309 void
1310 empathy_event_approve (EmpathyEvent *event_public)
1311 {
1312   EventPriv *event = (EventPriv *) event_public;
1313
1314   g_return_if_fail (event_public != NULL);
1315
1316   event_manager_approval_approve (event->approval);
1317 }
1318
1319 void
1320 empathy_event_decline (EmpathyEvent *event_public)
1321 {
1322   EventPriv *event = (EventPriv *) event_public;
1323
1324   g_return_if_fail (event_public != NULL);
1325
1326   reject_approval (event->approval);
1327 }