]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Show a notification when a contact goes offline or online
[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/util.h>
28
29 #include <libempathy/empathy-account-manager.h>
30 #include <libempathy/empathy-dispatcher.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-tp-chat.h>
34 #include <libempathy/empathy-tp-call.h>
35 #include <libempathy/empathy-tp-file.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-call-factory.h>
38
39 #include <extensions/extensions.h>
40
41 #include <libempathy-gtk/empathy-conf.h>
42 #include <libempathy-gtk/empathy-images.h>
43 #include <libempathy-gtk/empathy-contact-dialogs.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45
46 #include "empathy-event-manager.h"
47 #include "empathy-main-window.h"
48 #include "empathy-tube-dispatch.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
51 #include <libempathy/empathy-debug.h>
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
54
55 typedef struct {
56   EmpathyEventManager *manager;
57   EmpathyDispatchOperation *operation;
58   gulong approved_handler;
59   gulong claimed_handler;
60   gulong invalidated_handler;
61   /* Remove contact if applicable */
62   EmpathyContact *contact;
63   /* Tube dispatcher if applicable */
64   EmpathyTubeDispatch *tube_dispatch;
65   /* option signal handler and it's instance */
66   gulong handler;
67   GObject *handler_instance;
68   /* optional accept widget */
69   GtkWidget *dialog;
70 } EventManagerApproval;
71
72 typedef struct {
73   EmpathyDispatcher *dispatcher;
74   EmpathyContactManager *contact_manager;
75   GSList *events;
76   /* Ongoing approvals */
77   GSList *approvals;
78
79   /* voip ringing sound */
80   guint voip_timeout;
81   gint ringing;
82 } EmpathyEventManagerPriv;
83
84 typedef struct _EventPriv EventPriv;
85 typedef void (*EventFunc) (EventPriv *event);
86
87 struct _EventPriv {
88   EmpathyEvent public;
89   EmpathyEventManager *manager;
90   EventManagerApproval *approval;
91   EventFunc func;
92   gboolean inhibit;
93   gpointer user_data;
94 };
95
96 enum {
97   EVENT_ADDED,
98   EVENT_REMOVED,
99   EVENT_UPDATED,
100   LAST_SIGNAL
101 };
102
103 static guint signals[LAST_SIGNAL];
104
105 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
106
107 static EmpathyEventManager * manager_singleton = NULL;
108
109 static EventManagerApproval *
110 event_manager_approval_new (EmpathyEventManager *manager,
111   EmpathyDispatchOperation *operation)
112 {
113   EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
114   result->operation = g_object_ref (operation);
115   result->manager = manager;
116
117   return result;
118 }
119
120 static void
121 event_manager_approval_free (EventManagerApproval *approval)
122 {
123   g_signal_handler_disconnect (approval->operation,
124     approval->approved_handler);
125   g_signal_handler_disconnect (approval->operation,
126     approval->claimed_handler);
127   g_signal_handler_disconnect (approval->operation,
128     approval->invalidated_handler);
129   g_object_unref (approval->operation);
130
131   if (approval->handler != 0)
132     g_signal_handler_disconnect (approval->handler_instance,
133       approval->handler);
134
135   if (approval->contact != NULL)
136     g_object_unref (approval->contact);
137
138   if (approval->tube_dispatch != NULL)
139     g_object_unref (approval->tube_dispatch);
140
141   if (approval->dialog != NULL)
142     {
143       gtk_widget_destroy (approval->dialog);
144     }
145
146   g_slice_free (EventManagerApproval, approval);
147 }
148
149 static void event_remove (EventPriv *event);
150
151 static void
152 event_free (EventPriv *event)
153 {
154   g_free (event->public.icon_name);
155   g_free (event->public.header);
156   g_free (event->public.message);
157
158   if (event->public.contact)
159     {
160       g_object_unref (event->public.contact);
161     }
162
163   g_slice_free (EventPriv, event);
164 }
165
166 static void event_manager_ringing_finished_cb (ca_context *c, guint id,
167   int error_code, gpointer user_data);
168
169 static gboolean
170 event_manager_ringing_timeout_cb (gpointer data)
171 {
172   EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
173   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
174
175   priv->voip_timeout = 0;
176
177   empathy_sound_play_full (empathy_main_window_get (),
178       EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
179       manager);
180
181   return FALSE;
182 }
183
184 static gboolean
185 event_manager_ringing_idle_cb (gpointer data)
186 {
187   EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
188   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
189
190   if (priv->ringing > 0)
191     priv->voip_timeout = g_timeout_add (500, event_manager_ringing_timeout_cb,
192       data);
193
194   return FALSE;
195 }
196
197 static void
198 event_manager_ringing_finished_cb (ca_context *c, guint id, int error_code,
199   gpointer user_data)
200 {
201   if (error_code == CA_ERROR_CANCELED)
202     return;
203
204   g_idle_add (event_manager_ringing_idle_cb, user_data);
205 }
206
207 static void
208 event_manager_start_ringing (EmpathyEventManager *manager)
209 {
210   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
211
212   priv->ringing++;
213
214   if (priv->ringing == 1)
215     {
216       empathy_sound_play_full (empathy_main_window_get (),
217         EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
218         manager);
219     }
220 }
221
222 static void
223 event_manager_stop_ringing (EmpathyEventManager *manager)
224 {
225   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
226
227   priv->ringing--;
228
229   if (priv->ringing > 0)
230     return;
231
232   empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
233
234   if (priv->voip_timeout != 0)
235     {
236       g_source_remove (priv->voip_timeout);
237       priv->voip_timeout = 0;
238     }
239 }
240
241 static void
242 event_remove (EventPriv *event)
243 {
244   EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
245
246   DEBUG ("Removing event %p", event);
247   priv->events = g_slist_remove (priv->events, event);
248   g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
249   event_free (event);
250 }
251
252 static void
253 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
254   const gchar *icon_name, const gchar *header, const gchar *message,
255   EventManagerApproval *approval, EventFunc func, gpointer user_data)
256 {
257   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
258   EventPriv               *event;
259
260   event = g_slice_new0 (EventPriv);
261   event->public.contact = contact ? g_object_ref (contact) : NULL;
262   event->public.icon_name = g_strdup (icon_name);
263   event->public.header = g_strdup (header);
264   event->public.message = g_strdup (message);
265   event->inhibit = FALSE;
266   event->func = func;
267   event->user_data = user_data;
268   event->manager = manager;
269   event->approval = approval;
270
271   DEBUG ("Adding event %p", event);
272   priv->events = g_slist_prepend (priv->events, event);
273   g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
274 }
275
276 static void
277 event_channel_process_func (EventPriv *event)
278 {
279   empathy_dispatch_operation_approve (event->approval->operation);
280 }
281
282 static void
283 event_text_channel_process_func (EventPriv *event)
284 {
285   EmpathyTpChat *tp_chat;
286
287   if (event->approval->handler != 0)
288     {
289       tp_chat = EMPATHY_TP_CHAT
290         (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
291
292       g_signal_handler_disconnect (tp_chat, event->approval->handler);
293       event->approval->handler = 0;
294     }
295
296   empathy_dispatch_operation_approve (event->approval->operation);
297 }
298
299 static EventPriv *
300 event_lookup_by_approval (EmpathyEventManager *manager,
301   EventManagerApproval *approval)
302 {
303   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
304   GSList *l;
305   EventPriv *retval = NULL;
306
307   for (l = priv->events; l; l = l->next)
308     {
309       EventPriv *event = l->data;
310
311       if (event->approval == approval)
312         {
313           retval = event;
314           break;
315         }
316     }
317
318   return retval;
319 }
320
321 static void
322 event_update (EmpathyEventManager *manager, EventPriv *event,
323   const char *icon_name, const char *header, const char *msg)
324 {
325   g_free (event->public.icon_name);
326   g_free (event->public.header);
327   g_free (event->public.message);
328
329   event->public.icon_name = g_strdup (icon_name);
330   event->public.header = g_strdup (header);
331   event->public.message = g_strdup (msg);
332
333   g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
334 }
335
336 static void
337 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
338   gint response, gpointer user_data)
339 {
340   EventManagerApproval *approval = user_data;
341
342   gtk_widget_destroy (approval->dialog);
343   approval->dialog = NULL;
344
345   if (response != GTK_RESPONSE_ACCEPT)
346     {
347       EmpathyTpCall *call =
348         EMPATHY_TP_CALL (
349           empathy_dispatch_operation_get_channel_wrapper (
350             approval->operation));
351
352       g_object_ref (call);
353       if (empathy_dispatch_operation_claim (approval->operation))
354         empathy_tp_call_close (call);
355       g_object_unref (call);
356
357     }
358   else
359     {
360       EmpathyCallFactory *factory = empathy_call_factory_get ();
361       empathy_call_factory_claim_channel (factory, approval->operation);
362     }
363 }
364
365 static void
366 event_channel_process_voip_func (EventPriv *event)
367 {
368   GtkWidget *dialog;
369   GtkWidget *button;
370   GtkWidget *image;
371
372   if (event->approval->dialog != NULL)
373     {
374       gtk_window_present (GTK_WINDOW (event->approval->dialog));
375       return;
376     }
377
378   dialog = gtk_message_dialog_new (NULL, 0,
379       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
380   gtk_message_dialog_format_secondary_text (
381     GTK_MESSAGE_DIALOG (dialog),
382       _("%s is calling you, do you want to answer?"),
383       empathy_contact_get_name (event->approval->contact));
384
385   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
386       GTK_RESPONSE_OK);
387
388   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
389       _("_Reject"), GTK_RESPONSE_REJECT);
390   image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
391     GTK_ICON_SIZE_BUTTON);
392   gtk_button_set_image (GTK_BUTTON (button), image);
393
394   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
395       _("_Answer"), GTK_RESPONSE_ACCEPT);
396
397   image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
398   gtk_button_set_image (GTK_BUTTON (button), image);
399
400   g_signal_connect (dialog, "response",
401       G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
402       event->approval);
403
404   gtk_widget_show (dialog);
405
406   event->approval->dialog = dialog;
407 }
408
409 static void
410 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
411   EmpathyMessage *message, EventManagerApproval *approval)
412 {
413   EmpathyContact  *sender;
414   const gchar     *header;
415   const gchar     *msg;
416   TpChannel       *channel;
417   EventPriv       *event;
418
419   /* try to update the event if it's referring to a chat which is already in the
420    * queue. */
421   event = event_lookup_by_approval (approval->manager, approval);
422
423   if (event != NULL && event->inhibit && approval->handler != 0)
424     {
425       g_signal_handler_disconnect (tp_chat, approval->handler);
426       approval->handler = 0;
427       return;
428     }
429
430   sender = empathy_message_get_sender (message);
431   header = empathy_contact_get_name (sender);
432   msg = empathy_message_get_body (message);
433
434   channel = empathy_tp_chat_get_channel (tp_chat);
435
436   if (event != NULL)
437     event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
438   else
439     event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
440       msg, approval, event_text_channel_process_func, NULL);
441
442   empathy_sound_play (empathy_main_window_get (),
443     EMPATHY_SOUND_CONVERSATION_NEW);
444 }
445
446 static void
447 event_manager_approval_done (EventManagerApproval *approval)
448 {
449   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
450   GSList                  *l;
451
452   if (approval->operation != NULL)
453     {
454       GQuark channel_type;
455
456       channel_type = empathy_dispatch_operation_get_channel_type_id (
457           approval->operation);
458       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
459         {
460           event_manager_stop_ringing (approval->manager);
461         }
462     }
463
464   priv->approvals = g_slist_remove (priv->approvals, approval);
465
466   for (l = priv->events; l; l = l->next)
467     {
468       EventPriv *event = l->data;
469
470       if (event->approval == approval)
471         {
472           event_remove (event);
473           break;
474         }
475     }
476
477   event_manager_approval_free (approval);
478 }
479
480 static void
481 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
482   EventManagerApproval *approval)
483 {
484   event_manager_approval_done (approval);
485 }
486
487 static void
488 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
489   EventManagerApproval *approval)
490 {
491   event_manager_approval_done (approval);
492 }
493
494 static void
495 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
496   guint domain, gint code, gchar *message,
497   EventManagerApproval *approval)
498 {
499   event_manager_approval_done (approval);
500 }
501
502 static void
503 event_manager_media_channel_got_contact (EventManagerApproval *approval)
504 {
505   gchar *header;
506
507   header = g_strdup_printf (_("Incoming call from %s"),
508     empathy_contact_get_name (approval->contact));
509
510   event_manager_add (approval->manager,
511     approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
512     approval, event_channel_process_voip_func, NULL);
513
514   g_free (header);
515   event_manager_start_ringing (approval->manager);
516 }
517
518 static void
519 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
520   GParamSpec *param, EventManagerApproval *approval)
521 {
522   EmpathyContact *contact;
523
524   g_object_get (G_OBJECT (call), "contact", &contact, NULL);
525
526   if (contact == NULL)
527     return;
528
529   approval->contact = contact;
530   event_manager_media_channel_got_contact (approval);
531 }
532
533 static void
534 event_manager_tube_approved_cb (EventPriv *event)
535 {
536   empathy_tube_dispatch_handle (event->approval->tube_dispatch);
537 }
538
539 static void
540 event_manager_add_tube_approval (EventManagerApproval *approval,
541   EmpathyTubeDispatchAbility ability)
542 {
543   const gchar *icon_name;
544   gchar       *header;
545   const gchar *msg;
546
547   header = g_strdup_printf (_("%s is offering you an invitation"),
548     empathy_contact_get_name (approval->contact));
549
550   if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
551     {
552       icon_name = GTK_STOCK_EXECUTE;
553       msg = _("An external application will be started to handle it.");
554     }
555   else
556     {
557       icon_name = GTK_STOCK_DIALOG_ERROR;
558       msg = _("You don't have the needed external "
559               "application to handle it.");
560     }
561
562   event_manager_add (approval->manager, approval->contact, icon_name, header,
563     msg, approval, event_manager_tube_approved_cb, approval);
564
565   g_free (header);
566   /* FIXME better sound for incoming tubes ? */
567   empathy_sound_play (empathy_main_window_get (),
568     EMPATHY_SOUND_CONVERSATION_NEW);
569 }
570
571 static void
572 event_manager_tube_dispatch_ability_cb (GObject *object,
573    GParamSpec *spec, gpointer user_data)
574 {
575   EventManagerApproval *approval = (EventManagerApproval *) user_data;
576   EmpathyTubeDispatchAbility dispatchability;
577
578   dispatchability =
579     empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
580
581   if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
582     {
583       event_manager_add_tube_approval (approval, dispatchability);
584       g_signal_handler_disconnect (object, approval->handler);
585       approval->handler = 0;
586     }
587 }
588
589 static void
590 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
591                                    EmpathyContact *contact,
592                                    const GError *error,
593                                    gpointer user_data,
594                                    GObject *object)
595 {
596   EventManagerApproval *approval = (EventManagerApproval *) user_data;
597   EmpathyTubeDispatchAbility dispatchability;
598
599   if (error != NULL)
600     {
601       /* FIXME: We should probably still display the event */
602       DEBUG ("Error: %s", error->message);
603       return;
604     }
605
606   approval->contact = g_object_ref (contact);
607
608   dispatchability = empathy_tube_dispatch_is_dispatchable
609     (approval->tube_dispatch);
610
611   switch (dispatchability)
612     {
613       case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
614         approval->handler = g_signal_connect (approval->tube_dispatch,
615           "notify::dispatchability",
616           G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
617         approval->handler_instance = G_OBJECT (approval->tube_dispatch);
618         break;
619       case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
620         /* fallthrough */
621       case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
622         event_manager_add_tube_approval (approval, dispatchability);
623         break;
624     }
625 }
626
627 static void
628 invite_dialog_response_cb (GtkDialog *dialog,
629                            gint response,
630                            EventManagerApproval *approval)
631 {
632   EmpathyTpChat *tp_chat;
633   TpChannel *channel;
634   TpHandle self_handle;
635   GArray *members;
636
637   gtk_widget_destroy (GTK_WIDGET (approval->dialog));
638   approval->dialog = NULL;
639
640   tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
641         approval->operation));
642
643   if (response != GTK_RESPONSE_OK)
644     {
645       /* close channel */
646       DEBUG ("Muc invitation rejected");
647
648       if (empathy_dispatch_operation_claim (approval->operation))
649         empathy_tp_chat_close (tp_chat);
650       return;
651     }
652
653   DEBUG ("Muc invitation accepted");
654
655   /* join the room */
656   channel = empathy_tp_chat_get_channel (tp_chat);
657
658   self_handle = tp_channel_group_get_self_handle (channel);
659   members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
660   g_array_append_val (members, self_handle);
661
662   tp_cli_channel_interface_group_call_add_members (channel, -1, members,
663       "", NULL, NULL, NULL, NULL);
664
665   empathy_dispatch_operation_approve (approval->operation);
666
667   g_array_free (members, TRUE);
668 }
669
670 static void
671 event_room_channel_process_func (EventPriv *event)
672 {
673   GtkWidget *dialog, *button, *image;
674   TpChannel *channel = empathy_dispatch_operation_get_channel (
675       event->approval->operation);
676
677   if (event->approval->dialog != NULL)
678     {
679       gtk_window_present (GTK_WINDOW (event->approval->dialog));
680       return;
681     }
682
683   /* create dialog */
684   dialog = gtk_message_dialog_new (NULL, 0,
685       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
686
687   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
688       _("%s is inviting you to join %s"),
689       empathy_contact_get_name (event->approval->contact),
690       tp_channel_get_identifier (channel));
691
692   gtk_dialog_set_default_response (GTK_DIALOG (dialog),
693       GTK_RESPONSE_OK);
694
695   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
696       _("_Decline"), GTK_RESPONSE_CANCEL);
697   image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
698   gtk_button_set_image (GTK_BUTTON (button), image);
699
700   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
701       _("_Join"), GTK_RESPONSE_OK);
702   image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
703   gtk_button_set_image (GTK_BUTTON (button), image);
704
705   g_signal_connect (dialog, "response",
706       G_CALLBACK (invite_dialog_response_cb), event->approval);
707
708   gtk_widget_show (dialog);
709
710   event->approval->dialog = dialog;
711 }
712
713 static void
714 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
715                                          EmpathyContact *contact,
716                                          const GError *error,
717                                          gpointer user_data,
718                                          GObject *object)
719 {
720   EventManagerApproval *approval = (EventManagerApproval *) user_data;
721   TpChannel *channel;
722   const gchar *invite_msg;
723   gchar *msg;
724   TpHandle self_handle;
725
726   if (error != NULL)
727     {
728       /* FIXME: We should probably still display the event */
729       DEBUG ("Error: %s", error->message);
730       return;
731     }
732
733   approval->contact = g_object_ref (contact);
734   channel = empathy_dispatch_operation_get_channel (approval->operation);
735
736   self_handle = tp_channel_group_get_self_handle (channel);
737   tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
738       &invite_msg);
739
740   msg = g_strdup_printf (_("%s invited you to join %s"),
741       empathy_contact_get_name (approval->contact),
742       tp_channel_get_identifier (channel));
743
744   event_manager_add (approval->manager,
745     approval->contact, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
746     approval, event_room_channel_process_func, NULL);
747
748   empathy_sound_play (empathy_main_window_get (),
749     EMPATHY_SOUND_CONVERSATION_NEW);
750
751   g_free (msg);
752 }
753
754 static void
755 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
756                                  EmpathyContact *contact,
757                                  const GError *error,
758                                  gpointer user_data,
759                                  GObject *object)
760 {
761   EventManagerApproval *approval = (EventManagerApproval *) user_data;
762   char *header;
763
764   approval->contact = contact;
765
766   header = g_strdup_printf (_("Incoming file transfer from %s"),
767                             empathy_contact_get_name (approval->contact));
768
769   event_manager_add (approval->manager, approval->contact,
770       EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL, approval,
771       event_channel_process_func, NULL);
772
773   /* FIXME better sound for incoming file transfers ?*/
774   empathy_sound_play (empathy_main_window_get (),
775                       EMPATHY_SOUND_CONVERSATION_NEW);
776
777   g_free (header);
778 }
779
780 static void
781 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
782   EmpathyDispatchOperation  *operation, EmpathyEventManager *manager)
783 {
784   const gchar *channel_type;
785   EventManagerApproval *approval;
786   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
787
788   channel_type = empathy_dispatch_operation_get_channel_type (operation);
789
790   approval = event_manager_approval_new (manager, operation);
791   priv->approvals = g_slist_prepend (priv->approvals, approval);
792
793   approval->approved_handler = g_signal_connect (operation, "approved",
794     G_CALLBACK (event_manager_operation_approved_cb), approval);
795
796   approval->claimed_handler = g_signal_connect (operation, "claimed",
797      G_CALLBACK (event_manager_operation_claimed_cb), approval);
798
799   approval->invalidated_handler = g_signal_connect (operation, "invalidated",
800      G_CALLBACK (event_manager_operation_invalidated_cb), approval);
801
802   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
803     {
804       EmpathyTpChat *tp_chat =
805         EMPATHY_TP_CHAT (
806           empathy_dispatch_operation_get_channel_wrapper (operation));
807       TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
808
809       if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
810         {
811           /* Are we in local-pending ? */
812           TpHandle self_handle, inviter;
813
814           self_handle = tp_channel_group_get_self_handle (channel);
815
816           if (self_handle != 0 && tp_channel_group_get_local_pending_info (
817                 channel, self_handle, &inviter, NULL, NULL))
818             {
819               /* We are invited to a room */
820               EmpathyTpContactFactory *factory;
821               TpConnection *connection;
822
823               DEBUG ("Have been invited to %s. Ask user if he wants to accept",
824                   tp_channel_get_identifier (channel));
825
826               connection = empathy_tp_chat_get_connection (tp_chat);
827               factory = empathy_tp_contact_factory_dup_singleton (connection);
828
829               empathy_tp_contact_factory_get_from_handle (factory,
830                   inviter, event_manager_muc_invite_got_contact_cb,
831                   approval, NULL, G_OBJECT (manager));
832
833               g_object_unref (factory);
834               return;
835             }
836
837           /* if we are not invited, let's wait for the first message */
838         }
839
840       /* 1-1 text channel, wait for the first message */
841       approval->handler = g_signal_connect (tp_chat, "message-received",
842         G_CALLBACK (event_manager_chat_message_received_cb), approval);
843       approval->handler_instance = G_OBJECT (tp_chat);
844     }
845   else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
846     {
847       EmpathyContact *contact;
848       EmpathyTpCall *call = EMPATHY_TP_CALL (
849           empathy_dispatch_operation_get_channel_wrapper (operation));
850
851       g_object_get (G_OBJECT (call), "contact", &contact, NULL);
852
853       if (contact == NULL)
854         {
855           g_signal_connect (call, "notify::contact",
856             G_CALLBACK (event_manager_media_channel_contact_changed_cb),
857             approval);
858         }
859       else
860         {
861           approval->contact = contact;
862           event_manager_media_channel_got_contact (approval);
863         }
864
865     }
866   else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
867     {
868       TpChannel *channel;
869       TpConnection *connection;
870       TpHandle handle;
871       EmpathyTpContactFactory *factory;
872
873       channel = empathy_dispatch_operation_get_channel (operation);
874       handle = tp_channel_get_handle (channel, NULL);
875
876       connection = tp_channel_borrow_connection (channel);
877       factory = empathy_tp_contact_factory_dup_singleton (connection);
878       empathy_tp_contact_factory_get_from_handle (factory, handle,
879         event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
880
881       g_object_unref (factory);
882     }
883   else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
884       !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
885     {
886       TpChannel *channel;
887       TpHandle handle;
888       TpHandleType handle_type;
889       TpConnection *connection;
890       EmpathyTpContactFactory *factory;
891
892       channel = empathy_dispatch_operation_get_channel (operation);
893       handle = tp_channel_get_handle (channel, &handle_type);
894
895       /* Only understand p2p tubes */
896       if (handle_type != TP_HANDLE_TYPE_CONTACT)
897         return;
898
899       approval->tube_dispatch = empathy_tube_dispatch_new (operation);
900       connection = tp_channel_borrow_connection (channel);
901       factory = empathy_tp_contact_factory_dup_singleton (connection);
902       empathy_tp_contact_factory_get_from_handle (factory, handle,
903         event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
904     }
905   else
906     {
907       DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
908     }
909 }
910
911 static void
912 event_pending_subscribe_func (EventPriv *event)
913 {
914   empathy_subscription_dialog_show (event->public.contact, NULL);
915   event_remove (event);
916 }
917
918 static void
919 event_manager_pendings_changed_cb (EmpathyContactList  *list,
920   EmpathyContact *contact, EmpathyContact *actor,
921   guint reason, gchar *message, gboolean is_pending,
922   EmpathyEventManager *manager)
923 {
924   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
925   gchar                   *header, *event_msg;
926
927   if (!is_pending)
928     {
929       GSList *l;
930
931       for (l = priv->events; l; l = l->next)
932         {
933           EventPriv *event = l->data;
934
935           if (event->public.contact == contact &&
936               event->func == event_pending_subscribe_func)
937             {
938               event_remove (event);
939               break;
940             }
941         }
942
943       return;
944     }
945
946   header = g_strdup_printf (_("Subscription requested by %s"),
947     empathy_contact_get_name (contact));
948
949   if (!EMP_STR_EMPTY (message))
950     event_msg = g_strdup_printf (_("\nMessage: %s"), message);
951   else
952     event_msg = NULL;
953
954   event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
955     event_msg, NULL, event_pending_subscribe_func, NULL);
956
957   g_free (event_msg);
958   g_free (header);
959 }
960
961 static void
962 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
963                                    EmpathyContact *contact,
964                                    TpConnectionPresenceType current,
965                                    TpConnectionPresenceType previous,
966                                    EmpathyEventManager *manager)
967 {
968   McAccount *account;
969   gboolean just_connected;
970   EmpathyAccountManager *account_manager;
971   gchar *header = NULL;
972   gboolean preference = FALSE;
973
974   account = empathy_contact_get_account (contact);
975   account_manager = empathy_account_manager_dup_singleton ();
976   just_connected = empathy_account_manager_is_account_just_connected (
977                   account_manager, account);
978
979   g_object_unref (account_manager);
980   if (just_connected) {
981     return;
982   }
983
984   if (tp_connection_presence_type_cmp_availability (previous,
985      TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
986     {
987       /* contact was online */
988       empathy_conf_get_bool (empathy_conf_get (),
989                       EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
990       if (preference && tp_connection_presence_type_cmp_availability (current,
991           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
992         {
993           /* someone is logging off */
994           header = g_strdup_printf (_("%s signed out."),
995             empathy_contact_get_name (contact));
996
997           event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
998                              NULL, NULL, NULL, NULL);
999         }
1000     }
1001   else
1002     {
1003       /* contact was offline */
1004       empathy_conf_get_bool (empathy_conf_get (),
1005                       EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
1006       if (preference && tp_connection_presence_type_cmp_availability (current,
1007           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1008         {
1009           /* someone is logging in */
1010           header = g_strdup_printf (_("%s signed in."),
1011             empathy_contact_get_name (contact));
1012
1013           event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
1014                              NULL, NULL, NULL, NULL);
1015         }
1016     }
1017   g_free (header);
1018 }
1019
1020
1021 static GObject *
1022 event_manager_constructor (GType type,
1023                            guint n_props,
1024                            GObjectConstructParam *props)
1025 {
1026         GObject *retval;
1027
1028         if (manager_singleton) {
1029                 retval = g_object_ref (manager_singleton);
1030         } else {
1031                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1032                         (type, n_props, props);
1033
1034                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1035                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1036         }
1037
1038         return retval;
1039 }
1040
1041 static void
1042 event_manager_finalize (GObject *object)
1043 {
1044   EmpathyEventManagerPriv *priv = GET_PRIV (object);
1045
1046   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1047   g_slist_free (priv->events);
1048   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1049   g_slist_free (priv->approvals);
1050   g_object_unref (priv->contact_manager);
1051   g_object_unref (priv->dispatcher);
1052 }
1053
1054 static void
1055 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1056 {
1057   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1058
1059   object_class->finalize = event_manager_finalize;
1060   object_class->constructor = event_manager_constructor;
1061
1062   signals[EVENT_ADDED] =
1063     g_signal_new ("event-added",
1064       G_TYPE_FROM_CLASS (klass),
1065       G_SIGNAL_RUN_LAST,
1066       0,
1067       NULL, NULL,
1068       g_cclosure_marshal_VOID__POINTER,
1069       G_TYPE_NONE,
1070       1, G_TYPE_POINTER);
1071
1072   signals[EVENT_REMOVED] =
1073   g_signal_new ("event-removed",
1074       G_TYPE_FROM_CLASS (klass),
1075       G_SIGNAL_RUN_LAST,
1076       0,
1077       NULL, NULL,
1078       g_cclosure_marshal_VOID__POINTER,
1079       G_TYPE_NONE, 1, G_TYPE_POINTER);
1080
1081   signals[EVENT_UPDATED] =
1082   g_signal_new ("event-updated",
1083       G_TYPE_FROM_CLASS (klass),
1084       G_SIGNAL_RUN_LAST,
1085       0,
1086       NULL, NULL,
1087       g_cclosure_marshal_VOID__POINTER,
1088       G_TYPE_NONE, 1, G_TYPE_POINTER);
1089
1090
1091   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1092 }
1093
1094 static void
1095 empathy_event_manager_init (EmpathyEventManager *manager)
1096 {
1097   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1098     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1099   EmpathyContactMonitor   *monitor;
1100   EmpathyContactList      *list_iface;
1101
1102   list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1103   monitor = empathy_contact_list_get_monitor (list_iface);
1104   g_object_unref (list_iface);
1105
1106   manager->priv = priv;
1107
1108   priv->dispatcher = empathy_dispatcher_dup_singleton ();
1109   priv->contact_manager = empathy_contact_manager_dup_singleton ();
1110   g_signal_connect (priv->dispatcher, "approve",
1111     G_CALLBACK (event_manager_approve_channel_cb), manager);
1112   g_signal_connect (priv->contact_manager, "pendings-changed",
1113     G_CALLBACK (event_manager_pendings_changed_cb), manager);
1114   g_signal_connect (monitor, "contact-presence-changed",
1115     G_CALLBACK (event_manager_presence_changed_cb), manager);
1116 }
1117
1118 EmpathyEventManager *
1119 empathy_event_manager_dup_singleton (void)
1120 {
1121   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1122 }
1123
1124 GSList *
1125 empathy_event_manager_get_events (EmpathyEventManager *manager)
1126 {
1127   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1128
1129   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1130
1131   return priv->events;
1132 }
1133
1134 EmpathyEvent *
1135 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1136 {
1137   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1138
1139   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1140
1141   return priv->events ? priv->events->data : NULL;
1142 }
1143
1144 void
1145 empathy_event_activate (EmpathyEvent *event_public)
1146 {
1147   EventPriv *event = (EventPriv *) event_public;
1148
1149   g_return_if_fail (event_public != NULL);
1150
1151   if (event->func)
1152     event->func (event);
1153   else
1154     event_remove (event);
1155 }
1156
1157 void
1158 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1159 {
1160   EventPriv *event = (EventPriv *) event_public;
1161
1162   g_return_if_fail (event_public != NULL);
1163
1164   event->inhibit = TRUE;
1165 }
1166