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