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