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