2 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
25 #include <glib/gi18n.h>
27 #include <telepathy-glib/util.h>
29 #include <libempathy/empathy-dispatcher.h>
30 #include <libempathy/empathy-contact-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-tp-chat.h>
33 #include <libempathy/empathy-tp-call.h>
34 #include <libempathy/empathy-utils.h>
36 #include <extensions/extensions.h>
38 #include <libempathy-gtk/empathy-images.h>
39 #include <libempathy-gtk/empathy-contact-dialogs.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include "empathy-event-manager.h"
43 #include "empathy-main-window.h"
44 #include "empathy-tube-dispatch.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
52 EmpathyEventManager *manager;
53 EmpathyDispatchOperation *operation;
54 gulong approved_handler;
55 gulong claimed_handler;
56 gulong invalidated_handler;
57 /* Remove contact if applicable */
58 EmpathyContact *contact;
59 /* Tube dispatcher if applicable */
60 EmpathyTubeDispatch *tube_dispatch;
61 /* option signal handler */
63 } EventManagerApproval;
66 EmpathyDispatcher *dispatcher;
67 EmpathyContactManager *contact_manager;
69 /* Ongoing approvals */
72 /* voip ringing sound */
75 } EmpathyEventManagerPriv;
77 typedef struct _EventPriv EventPriv;
78 typedef void (*EventFunc) (EventPriv *event);
82 EmpathyEventManager *manager;
83 EventManagerApproval *approval;
96 static guint signals[LAST_SIGNAL];
98 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
100 static EmpathyEventManager * manager_singleton = NULL;
102 static EventManagerApproval *
103 event_manager_approval_new (EmpathyEventManager *manager,
104 EmpathyDispatchOperation *operation)
106 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
107 result->operation = g_object_ref (operation);
108 result->manager = manager;
114 event_manager_approval_free (EventManagerApproval *approval)
116 g_signal_handler_disconnect (approval->operation,
117 approval->approved_handler);
118 g_signal_handler_disconnect (approval->operation,
119 approval->claimed_handler);
120 g_signal_handler_disconnect (approval->operation,
121 approval->invalidated_handler);
122 g_object_unref (approval->operation);
124 if (approval->contact != NULL)
125 g_object_unref (approval->contact);
127 if (approval->tube_dispatch != NULL)
128 g_object_unref (approval->tube_dispatch);
130 g_slice_free (EventManagerApproval, approval);
133 static void event_remove (EventPriv *event);
136 event_free (EventPriv *event)
138 g_free (event->public.icon_name);
139 g_free (event->public.header);
140 g_free (event->public.message);
142 if (event->public.contact)
144 g_object_unref (event->public.contact);
147 g_slice_free (EventPriv, event);
150 static void event_manager_ringing_finished_cb (ca_context *c, guint id,
151 int error_code, gpointer user_data);
154 event_manager_ringing_timeout_cb (gpointer data)
156 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
157 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
159 priv->voip_timeout = 0;
161 empathy_sound_play_full (empathy_main_window_get (),
162 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
169 event_manager_ringing_idle_cb (gpointer data)
171 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
172 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
174 if (priv->ringing > 0)
175 priv->voip_timeout = g_timeout_add (500, event_manager_ringing_timeout_cb,
182 event_manager_ringing_finished_cb (ca_context *c, guint id, int error_code,
185 if (error_code == CA_ERROR_CANCELED)
188 g_idle_add (event_manager_ringing_idle_cb, user_data);
192 event_manager_start_ringing (EmpathyEventManager *manager)
194 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
198 if (priv->ringing == 1)
200 empathy_sound_play_full (empathy_main_window_get (),
201 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
207 event_manager_stop_ringing (EmpathyEventManager *manager)
209 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
213 if (priv->ringing > 0)
216 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
218 if (priv->voip_timeout != 0)
220 g_source_remove (priv->voip_timeout);
221 priv->voip_timeout = 0;
226 event_remove (EventPriv *event)
228 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
230 DEBUG ("Removing event %p", event);
231 priv->events = g_slist_remove (priv->events, event);
232 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
237 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
238 const gchar *icon_name, const gchar *header, const gchar *message,
239 EventManagerApproval *approval, EventFunc func, gpointer user_data)
241 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
244 event = g_slice_new0 (EventPriv);
245 event->public.contact = contact ? g_object_ref (contact) : NULL;
246 event->public.icon_name = g_strdup (icon_name);
247 event->public.header = g_strdup (header);
248 event->public.message = g_strdup (message);
249 event->inhibit = FALSE;
251 event->user_data = user_data;
252 event->manager = manager;
253 event->approval = approval;
255 DEBUG ("Adding event %p", event);
256 priv->events = g_slist_prepend (priv->events, event);
257 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
261 event_channel_process_func (EventPriv *event)
263 empathy_dispatch_operation_approve (event->approval->operation);
267 event_text_channel_process_func (EventPriv *event)
269 EmpathyTpChat *tp_chat;
271 if (event->approval->handler != 0)
273 tp_chat = EMPATHY_TP_CHAT
274 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
276 g_signal_handler_disconnect (tp_chat, event->approval->handler);
277 event->approval->handler = 0;
280 empathy_dispatch_operation_approve (event->approval->operation);
284 event_lookup_by_approval (EmpathyEventManager *manager,
285 EventManagerApproval *approval)
287 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
289 EventPriv *retval = NULL;
291 for (l = priv->events; l; l = l->next)
293 EventPriv *event = l->data;
295 if (event->approval == approval)
306 event_update (EmpathyEventManager *manager, EventPriv *event,
307 const char *icon_name, const char *header, const char *msg)
309 g_free (event->public.icon_name);
310 g_free (event->public.header);
311 g_free (event->public.message);
313 event->public.icon_name = g_strdup (icon_name);
314 event->public.header = g_strdup (header);
315 event->public.message = g_strdup (msg);
317 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
321 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
322 EmpathyMessage *message, EventManagerApproval *approval)
324 EmpathyContact *sender;
330 /* try to update the event if it's referring to a chat which is already in the
332 event = event_lookup_by_approval (approval->manager, approval);
334 if (event != NULL && event->inhibit && approval->handler != 0)
336 g_signal_handler_disconnect (tp_chat, approval->handler);
337 approval->handler = 0;
341 sender = empathy_message_get_sender (message);
342 header = g_strdup_printf (_("New message from %s"),
343 empathy_contact_get_name (sender));
344 msg = empathy_message_get_body (message);
346 channel = empathy_tp_chat_get_channel (tp_chat);
349 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
351 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
352 msg, approval, event_text_channel_process_func, NULL);
355 empathy_sound_play (empathy_main_window_get (),
356 EMPATHY_SOUND_CONVERSATION_NEW);
362 event_manager_approval_done (EventManagerApproval *approval)
364 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
367 if (approval->operation != NULL)
371 channel_type = empathy_dispatch_operation_get_channel_type_id (
372 approval->operation);
373 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
375 event_manager_stop_ringing (approval->manager);
379 priv->approvals = g_slist_remove (priv->approvals, approval);
381 for (l = priv->events; l; l = l->next)
383 EventPriv *event = l->data;
385 if (event->approval == approval)
387 event_remove (event);
392 event_manager_approval_free (approval);
396 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
397 EventManagerApproval *approval)
399 event_manager_approval_done (approval);
403 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
404 EventManagerApproval *approval)
406 event_manager_approval_done (approval);
410 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
411 guint domain, gint code, gchar *message,
412 EventManagerApproval *approval)
414 event_manager_approval_done (approval);
418 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
419 const GError *error, gpointer user_data, GObject *object)
421 EventManagerApproval *approval = user_data;
426 /* FIXME just returning assuming the operation will be invalidated as
431 header = g_strdup_printf (_("Incoming call from %s"),
432 empathy_contact_get_name (contact));
434 event_manager_add (approval->manager,
435 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
436 approval, event_channel_process_func, NULL);
439 event_manager_start_ringing (approval->manager);
445 event_manager_media_channel_got_contact (EventManagerApproval *approval)
447 empathy_contact_call_when_ready (approval->contact,
448 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
449 approval, NULL, G_OBJECT (approval->manager));
453 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
454 GParamSpec *param, EventManagerApproval *approval)
456 EmpathyContact *contact;
458 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
463 approval->contact = contact;
464 event_manager_media_channel_got_contact (approval);
468 event_manager_tube_approved_cb (EventPriv *event)
470 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
474 event_manager_add_tube_approval (EventManagerApproval *approval,
475 EmpathyTubeDispatchAbility ability)
477 const gchar *icon_name;
481 header = g_strdup_printf (_("%s is offering you an invitation"),
482 empathy_contact_get_name (approval->contact));
484 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
486 icon_name = GTK_STOCK_EXECUTE;
487 msg = _("An external application will be started to handle it.");
491 icon_name = GTK_STOCK_DIALOG_ERROR;
492 msg = _("You don't have the needed external "
493 "application to handle it.");
496 event_manager_add (approval->manager, approval->contact, icon_name, header,
497 msg, approval, event_manager_tube_approved_cb, approval);
500 /* FIXME better sound for incoming tubes ? */
501 empathy_sound_play (empathy_main_window_get (),
502 EMPATHY_SOUND_CONVERSATION_NEW);
508 event_manager_tube_dispatch_ability_cb (GObject *object,
509 GParamSpec *spec, gpointer user_data)
511 EventManagerApproval *approval = (EventManagerApproval *)user_data;
512 EmpathyTubeDispatchAbility dispatchability;
515 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
517 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
519 event_manager_add_tube_approval (approval, dispatchability);
520 g_signal_handler_disconnect (object, approval->handler);
521 approval->handler = 0;
526 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
527 const GError *error, gpointer user_data, GObject *object)
529 EventManagerApproval *approval = (EventManagerApproval *)user_data;
530 EmpathyTubeDispatchAbility dispatchability;
534 /* FIXME?, we assume that the operation gets invalidated as well (if it
539 dispatchability = empathy_tube_dispatch_is_dispatchable
540 (approval->tube_dispatch);
543 switch (dispatchability)
545 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
546 approval->handler = g_signal_connect (approval->tube_dispatch,
547 "notify::dispatchability",
548 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
550 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
552 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
553 event_manager_add_tube_approval (approval, dispatchability);
559 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
560 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
562 const gchar *channel_type;
563 EventManagerApproval *approval;
564 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
566 channel_type = empathy_dispatch_operation_get_channel_type (operation);
568 approval = event_manager_approval_new (manager, operation);
569 priv->approvals = g_slist_prepend (priv->approvals, approval);
571 approval->approved_handler = g_signal_connect (operation, "approved",
572 G_CALLBACK (event_manager_operation_approved_cb), approval);
574 approval->claimed_handler = g_signal_connect (operation, "claimed",
575 G_CALLBACK (event_manager_operation_claimed_cb), approval);
577 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
578 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
580 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
582 EmpathyTpChat *tp_chat =
584 empathy_dispatch_operation_get_channel_wrapper (operation));
586 approval->handler = g_signal_connect (tp_chat, "message-received",
587 G_CALLBACK (event_manager_chat_message_received_cb), approval);
590 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
592 EmpathyContact *contact;
593 EmpathyTpCall *call = EMPATHY_TP_CALL (
594 empathy_dispatch_operation_get_channel_wrapper (operation));
596 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
600 g_signal_connect (call, "notify::contact",
601 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
606 approval->contact = contact;
607 event_manager_media_channel_got_contact (approval);
611 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
613 EmpathyContact *contact;
617 EmpathyContactFactory *factory;
618 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
620 factory = empathy_contact_factory_dup_singleton ();
621 handle = tp_channel_get_handle (channel, NULL);
622 account = empathy_channel_get_account (channel);
624 contact = empathy_contact_factory_get_from_handle (factory, account,
627 empathy_contact_run_until_ready (contact,
628 EMPATHY_CONTACT_READY_NAME, NULL);
630 header = g_strdup_printf (_("Incoming file transfer from %s"),
631 empathy_contact_get_name (contact));
633 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
634 header, NULL, approval, event_channel_process_func, NULL);
636 /* FIXME better sound for incoming file transfers ?*/
637 empathy_sound_play (empathy_main_window_get (),
638 EMPATHY_SOUND_CONVERSATION_NEW);
640 g_object_unref (factory);
641 g_object_unref (account);
644 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
645 !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
647 EmpathyContact *contact;
649 TpHandleType handle_type;
651 EmpathyContactFactory *factory;
652 EmpathyTubeDispatch *tube_dispatch;
655 channel = empathy_dispatch_operation_get_channel (operation);
657 handle = tp_channel_get_handle (channel, &handle_type);
659 /* Only understand p2p tubes */
660 if (handle_type != TP_HANDLE_TYPE_CONTACT)
663 factory = empathy_contact_factory_dup_singleton ();
664 account = empathy_channel_get_account (channel);
666 contact = empathy_contact_factory_get_from_handle (factory, account,
669 tube_dispatch = empathy_tube_dispatch_new (operation);
671 approval->contact = contact;
672 approval->tube_dispatch = tube_dispatch;
674 empathy_contact_call_when_ready (contact,
675 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
676 approval, NULL, G_OBJECT (manager));
678 g_object_unref (factory);
679 g_object_unref (account);
683 DEBUG ("Unknown channel type, ignoring..");
688 event_pending_subscribe_func (EventPriv *event)
690 empathy_subscription_dialog_show (event->public.contact, NULL);
691 event_remove (event);
695 event_manager_pendings_changed_cb (EmpathyContactList *list,
696 EmpathyContact *contact, EmpathyContact *actor,
697 guint reason, gchar *message, gboolean is_pending,
698 EmpathyEventManager *manager)
700 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
701 gchar *header, *event_msg;
707 for (l = priv->events; l; l = l->next)
709 EventPriv *event = l->data;
711 if (event->public.contact == contact &&
712 event->func == event_pending_subscribe_func)
714 event_remove (event);
722 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
724 header = g_strdup_printf (_("Subscription requested by %s"),
725 empathy_contact_get_name (contact));
727 if (!EMP_STR_EMPTY (message))
728 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
732 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
733 event_msg, NULL, event_pending_subscribe_func, NULL);
740 event_manager_constructor (GType type,
742 GObjectConstructParam *props)
746 if (manager_singleton) {
747 retval = g_object_ref (manager_singleton);
749 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
750 (type, n_props, props);
752 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
753 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
760 event_manager_finalize (GObject *object)
762 EmpathyEventManagerPriv *priv = GET_PRIV (object);
764 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
765 g_slist_free (priv->events);
766 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
767 g_slist_free (priv->approvals);
768 g_object_unref (priv->contact_manager);
769 g_object_unref (priv->dispatcher);
773 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
775 GObjectClass *object_class = G_OBJECT_CLASS (klass);
777 object_class->finalize = event_manager_finalize;
778 object_class->constructor = event_manager_constructor;
780 signals[EVENT_ADDED] =
781 g_signal_new ("event-added",
782 G_TYPE_FROM_CLASS (klass),
786 g_cclosure_marshal_VOID__POINTER,
790 signals[EVENT_REMOVED] =
791 g_signal_new ("event-removed",
792 G_TYPE_FROM_CLASS (klass),
796 g_cclosure_marshal_VOID__POINTER,
797 G_TYPE_NONE, 1, G_TYPE_POINTER);
799 signals[EVENT_UPDATED] =
800 g_signal_new ("event-updated",
801 G_TYPE_FROM_CLASS (klass),
805 g_cclosure_marshal_VOID__POINTER,
806 G_TYPE_NONE, 1, G_TYPE_POINTER);
809 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
813 empathy_event_manager_init (EmpathyEventManager *manager)
815 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
816 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
818 manager->priv = priv;
820 priv->dispatcher = empathy_dispatcher_dup_singleton ();
821 priv->contact_manager = empathy_contact_manager_dup_singleton ();
822 g_signal_connect (priv->dispatcher, "approve",
823 G_CALLBACK (event_manager_approve_channel_cb), manager);
824 g_signal_connect (priv->contact_manager, "pendings-changed",
825 G_CALLBACK (event_manager_pendings_changed_cb), manager);
828 EmpathyEventManager *
829 empathy_event_manager_dup_singleton (void)
831 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
835 empathy_event_manager_get_events (EmpathyEventManager *manager)
837 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
839 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
845 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
847 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
849 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
851 return priv->events ? priv->events->data : NULL;
855 empathy_event_activate (EmpathyEvent *event_public)
857 EventPriv *event = (EventPriv*) event_public;
859 g_return_if_fail (event_public != NULL);
864 event_remove (event);
868 empathy_event_inhibit_updates (EmpathyEvent *event_public)
870 EventPriv *event = (EventPriv *) event_public;
872 g_return_if_fail (event_public != NULL);
874 event->inhibit = TRUE;