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