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>
41 #include "empathy-event-manager.h"
42 #include "empathy-tube-dispatch.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
45 #include <libempathy/empathy-debug.h>
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
50 EmpathyEventManager *manager;
51 EmpathyDispatchOperation *operation;
52 gulong approved_handler;
53 gulong claimed_handler;
54 gulong invalidated_handler;
55 /* Remove contact if applicable */
56 EmpathyContact *contact;
57 /* Tube dispatcher if applicable */
58 EmpathyTubeDispatch *tube_dispatch;
59 /* option signal handler */
61 } EventManagerApproval;
64 EmpathyDispatcher *dispatcher;
65 EmpathyContactManager *contact_manager;
67 /* Ongoing approvals */
69 } EmpathyEventManagerPriv;
71 typedef struct _EventPriv EventPriv;
72 typedef void (*EventFunc) (EventPriv *event);
76 EmpathyEventManager *manager;
77 EventManagerApproval *approval;
90 static guint signals[LAST_SIGNAL];
92 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
94 static EmpathyEventManager * manager_singleton = NULL;
96 static EventManagerApproval *
97 event_manager_approval_new (EmpathyEventManager *manager,
98 EmpathyDispatchOperation *operation)
100 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
101 result->operation = g_object_ref (operation);
102 result->manager = manager;
108 event_manager_approval_free (EventManagerApproval *approval)
110 g_signal_handler_disconnect (approval->operation,
111 approval->approved_handler);
112 g_signal_handler_disconnect (approval->operation,
113 approval->claimed_handler);
114 g_signal_handler_disconnect (approval->operation,
115 approval->invalidated_handler);
116 g_object_unref (approval->operation);
118 if (approval->contact != NULL)
119 g_object_unref (approval->contact);
121 if (approval->tube_dispatch != NULL)
122 g_object_unref (approval->tube_dispatch);
124 g_slice_free (EventManagerApproval, approval);
127 static void event_remove (EventPriv *event);
130 event_free (EventPriv *event)
132 g_free (event->public.icon_name);
133 g_free (event->public.header);
134 g_free (event->public.message);
136 if (event->public.contact)
138 g_object_unref (event->public.contact);
141 g_slice_free (EventPriv, event);
145 event_remove (EventPriv *event)
147 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
149 DEBUG ("Removing event %p", event);
150 priv->events = g_slist_remove (priv->events, event);
151 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
156 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
157 const gchar *icon_name, const gchar *header, const gchar *message,
158 EventManagerApproval *approval, EventFunc func, gpointer user_data)
160 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
163 event = g_slice_new0 (EventPriv);
164 event->public.contact = contact ? g_object_ref (contact) : NULL;
165 event->public.icon_name = g_strdup (icon_name);
166 event->public.header = g_strdup (header);
167 event->public.message = g_strdup (message);
168 event->inhibit = FALSE;
170 event->user_data = user_data;
171 event->manager = manager;
172 event->approval = approval;
174 DEBUG ("Adding event %p", event);
175 priv->events = g_slist_prepend (priv->events, event);
176 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
180 event_channel_process_func (EventPriv *event)
182 empathy_dispatch_operation_approve (event->approval->operation);
186 event_text_channel_process_func (EventPriv *event)
188 EmpathyTpChat *tp_chat;
190 if (event->approval->handler != 0)
192 tp_chat = EMPATHY_TP_CHAT
193 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
195 g_signal_handler_disconnect (tp_chat, event->approval->handler);
196 event->approval->handler = 0;
199 empathy_dispatch_operation_approve (event->approval->operation);
203 event_lookup_by_approval (EmpathyEventManager *manager,
204 EventManagerApproval *approval)
206 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
208 EventPriv *retval = NULL;
210 for (l = priv->events; l; l = l->next)
212 EventPriv *event = l->data;
214 if (event->approval == approval)
225 event_update (EmpathyEventManager *manager, EventPriv *event,
226 const char *icon_name, const char *header, const char *msg)
228 g_free (event->public.icon_name);
229 g_free (event->public.header);
230 g_free (event->public.message);
232 event->public.icon_name = g_strdup (icon_name);
233 event->public.header = g_strdup (header);
234 event->public.message = g_strdup (msg);
236 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
240 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
241 EmpathyMessage *message, EventManagerApproval *approval)
243 EmpathyContact *sender;
249 /* try to update the event if it's referring to a chat which is already in the
251 event = event_lookup_by_approval (approval->manager, approval);
253 if (event != NULL && event->inhibit && approval->handler != 0)
255 g_signal_handler_disconnect (tp_chat, approval->handler);
256 approval->handler = 0;
260 sender = empathy_message_get_sender (message);
261 header = g_strdup_printf (_("New message from %s"),
262 empathy_contact_get_name (sender));
263 msg = empathy_message_get_body (message);
265 channel = empathy_tp_chat_get_channel (tp_chat);
268 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
270 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
271 msg, approval, event_text_channel_process_func, NULL);
277 event_manager_approval_done (EventManagerApproval *approval)
279 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
282 priv->approvals = g_slist_remove (priv->approvals, approval);
284 for (l = priv->events; l; l = l->next)
286 EventPriv *event = l->data;
288 if (event->approval == approval)
290 event_remove (event);
295 event_manager_approval_free (approval);
299 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
300 EventManagerApproval *approval)
302 event_manager_approval_done (approval);
306 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
307 EventManagerApproval *approval)
309 event_manager_approval_done (approval);
313 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
314 guint domain, gint code, gchar *message,
315 EventManagerApproval *approval)
317 event_manager_approval_done (approval);
321 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
322 const GError *error, gpointer user_data, GObject *object)
324 EventManagerApproval *approval = user_data;
329 /* FIXME just returning assuming the operation will be invalidated as
334 header = g_strdup_printf (_("Incoming call from %s"),
335 empathy_contact_get_name (contact));
337 event_manager_add (approval->manager,
338 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
339 approval, event_channel_process_func, NULL);
345 event_manager_media_channel_got_contact (EventManagerApproval *approval)
347 empathy_contact_call_when_ready (approval->contact,
348 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
349 approval, NULL, G_OBJECT (approval->manager));
353 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
354 GParamSpec *param, EventManagerApproval *approval)
356 EmpathyContact *contact;
358 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
363 approval->contact = contact;
364 event_manager_media_channel_got_contact (approval);
368 event_manager_tube_approved_cb (EventPriv *event)
370 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
374 event_manager_add_tube_approval (EventManagerApproval *approval,
375 EmpathyTubeDispatchAbility ability)
377 const gchar *icon_name;
381 header = g_strdup_printf (_("%s is offering you an invitation"),
382 empathy_contact_get_name (approval->contact));
384 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
386 icon_name = GTK_STOCK_EXECUTE;
387 msg = _("An external application will be started to handle it.");
391 icon_name = GTK_STOCK_DIALOG_ERROR;
392 msg = _("You don't have the needed external "
393 "application to handle it.");
396 event_manager_add (approval->manager, approval->contact, icon_name, header,
397 msg, approval, event_manager_tube_approved_cb, approval);
403 event_manager_tube_dispatch_ability_cb (GObject *object,
404 GParamSpec *spec, gpointer user_data)
406 EventManagerApproval *approval = (EventManagerApproval *)user_data;
407 EmpathyTubeDispatchAbility dispatchability;
410 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
412 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
414 event_manager_add_tube_approval (approval, dispatchability);
415 g_signal_handler_disconnect (object, approval->handler);
416 approval->handler = 0;
421 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
422 const GError *error, gpointer user_data, GObject *object)
424 EventManagerApproval *approval = (EventManagerApproval *)user_data;
425 EmpathyTubeDispatchAbility dispatchability;
429 /* FIXME?, we assume that the operation gets invalidated as well (if it
434 dispatchability = empathy_tube_dispatch_is_dispatchable
435 (approval->tube_dispatch);
438 switch (dispatchability)
440 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
441 approval->handler = g_signal_connect (approval->tube_dispatch,
442 "notify::dispatchability",
443 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
445 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
447 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
448 event_manager_add_tube_approval (approval, dispatchability);
454 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
455 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
457 const gchar *channel_type;
458 EventManagerApproval *approval;
459 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
461 channel_type = empathy_dispatch_operation_get_channel_type (operation);
463 approval = event_manager_approval_new (manager, operation);
464 priv->approvals = g_slist_prepend (priv->approvals, approval);
466 approval->approved_handler = g_signal_connect (operation, "approved",
467 G_CALLBACK (event_manager_operation_approved_cb), approval);
469 approval->claimed_handler = g_signal_connect (operation, "claimed",
470 G_CALLBACK (event_manager_operation_claimed_cb), approval);
472 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
473 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
475 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
477 EmpathyTpChat *tp_chat =
479 empathy_dispatch_operation_get_channel_wrapper (operation));
481 approval->handler = g_signal_connect (tp_chat, "message-received",
482 G_CALLBACK (event_manager_chat_message_received_cb), approval);
485 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
487 EmpathyContact *contact;
488 EmpathyTpCall *call = EMPATHY_TP_CALL (
489 empathy_dispatch_operation_get_channel_wrapper (operation));
491 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
495 g_signal_connect (call, "notify::contact",
496 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
501 approval->contact = contact;
502 event_manager_media_channel_got_contact (approval);
506 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
508 EmpathyContact *contact;
512 EmpathyContactFactory *factory;
513 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
515 factory = empathy_contact_factory_dup_singleton ();
516 handle = tp_channel_get_handle (channel, NULL);
517 account = empathy_channel_get_account (channel);
519 contact = empathy_contact_factory_get_from_handle (factory, account,
522 empathy_contact_run_until_ready (contact,
523 EMPATHY_CONTACT_READY_NAME, NULL);
525 header = g_strdup_printf (_("Incoming file transfer from %s"),
526 empathy_contact_get_name (contact));
528 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
529 header, NULL, approval, event_channel_process_func, NULL);
531 g_object_unref (factory);
532 g_object_unref (account);
535 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
536 !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
538 EmpathyContact *contact;
540 TpHandleType handle_type;
542 EmpathyContactFactory *factory;
543 EmpathyTubeDispatch *tube_dispatch;
546 channel = empathy_dispatch_operation_get_channel (operation);
548 handle = tp_channel_get_handle (channel, &handle_type);
550 /* Only understand p2p tubes */
551 if (handle_type != TP_HANDLE_TYPE_CONTACT)
554 factory = empathy_contact_factory_dup_singleton ();
555 account = empathy_channel_get_account (channel);
557 contact = empathy_contact_factory_get_from_handle (factory, account,
560 tube_dispatch = empathy_tube_dispatch_new (operation);
562 approval->contact = contact;
563 approval->tube_dispatch = tube_dispatch;
565 empathy_contact_call_when_ready (contact,
566 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
567 approval, NULL, G_OBJECT (manager));
569 g_object_unref (factory);
570 g_object_unref (account);
574 DEBUG ("Unknown channel type, ignoring..");
579 event_pending_subscribe_func (EventPriv *event)
581 empathy_subscription_dialog_show (event->public.contact, NULL);
582 event_remove (event);
586 event_manager_pendings_changed_cb (EmpathyContactList *list,
587 EmpathyContact *contact, EmpathyContact *actor,
588 guint reason, gchar *message, gboolean is_pending,
589 EmpathyEventManager *manager)
591 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
592 gchar *header, *event_msg;
598 for (l = priv->events; l; l = l->next)
600 EventPriv *event = l->data;
602 if (event->public.contact == contact &&
603 event->func == event_pending_subscribe_func)
605 event_remove (event);
613 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
615 header = g_strdup_printf (_("Subscription requested by %s"),
616 empathy_contact_get_name (contact));
618 if (!EMP_STR_EMPTY (message))
619 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
623 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
624 event_msg, NULL, event_pending_subscribe_func, NULL);
631 event_manager_constructor (GType type,
633 GObjectConstructParam *props)
637 if (manager_singleton) {
638 retval = g_object_ref (manager_singleton);
640 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
641 (type, n_props, props);
643 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
644 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
651 event_manager_finalize (GObject *object)
653 EmpathyEventManagerPriv *priv = GET_PRIV (object);
655 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
656 g_slist_free (priv->events);
657 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
658 g_slist_free (priv->approvals);
659 g_object_unref (priv->contact_manager);
660 g_object_unref (priv->dispatcher);
664 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
666 GObjectClass *object_class = G_OBJECT_CLASS (klass);
668 object_class->finalize = event_manager_finalize;
669 object_class->constructor = event_manager_constructor;
671 signals[EVENT_ADDED] =
672 g_signal_new ("event-added",
673 G_TYPE_FROM_CLASS (klass),
677 g_cclosure_marshal_VOID__POINTER,
681 signals[EVENT_REMOVED] =
682 g_signal_new ("event-removed",
683 G_TYPE_FROM_CLASS (klass),
687 g_cclosure_marshal_VOID__POINTER,
688 G_TYPE_NONE, 1, G_TYPE_POINTER);
690 signals[EVENT_UPDATED] =
691 g_signal_new ("event-updated",
692 G_TYPE_FROM_CLASS (klass),
696 g_cclosure_marshal_VOID__POINTER,
697 G_TYPE_NONE, 1, G_TYPE_POINTER);
700 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
704 empathy_event_manager_init (EmpathyEventManager *manager)
706 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
707 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
709 manager->priv = priv;
711 priv->dispatcher = empathy_dispatcher_dup_singleton ();
712 priv->contact_manager = empathy_contact_manager_dup_singleton ();
713 g_signal_connect (priv->dispatcher, "approve",
714 G_CALLBACK (event_manager_approve_channel_cb), manager);
715 g_signal_connect (priv->contact_manager, "pendings-changed",
716 G_CALLBACK (event_manager_pendings_changed_cb), manager);
719 EmpathyEventManager *
720 empathy_event_manager_dup_singleton (void)
722 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
726 empathy_event_manager_get_events (EmpathyEventManager *manager)
728 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
730 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
736 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
738 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
740 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
742 return priv->events ? priv->events->data : NULL;
746 empathy_event_activate (EmpathyEvent *event_public)
748 EventPriv *event = (EventPriv*) event_public;
750 g_return_if_fail (event_public != NULL);
755 event_remove (event);
759 empathy_event_inhibit_updates (EmpathyEvent *event_public)
761 EventPriv *event = (EventPriv *) event_public;
763 g_return_if_fail (event_public != NULL);
765 event->inhibit = TRUE;