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