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