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