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