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;
89 static guint signals[LAST_SIGNAL];
91 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
93 static EmpathyEventManager * manager_singleton = NULL;
95 static EventManagerApproval *
96 event_manager_approval_new (EmpathyEventManager *manager,
97 EmpathyDispatchOperation *operation)
99 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
100 result->operation = g_object_ref (operation);
101 result->manager = manager;
107 event_manager_approval_free (EventManagerApproval *approval)
109 g_signal_handler_disconnect (approval->operation,
110 approval->approved_handler);
111 g_signal_handler_disconnect (approval->operation,
112 approval->claimed_handler);
113 g_signal_handler_disconnect (approval->operation,
114 approval->invalidated_handler);
115 g_object_unref (approval->operation);
117 if (approval->contact != NULL)
118 g_object_unref (approval->contact);
120 if (approval->tube_dispatch != NULL)
121 g_object_unref (approval->tube_dispatch);
123 g_slice_free (EventManagerApproval, approval);
126 static void event_remove (EventPriv *event);
129 event_free (EventPriv *event)
131 g_free (event->public.icon_name);
132 g_free (event->public.header);
133 g_free (event->public.message);
135 if (event->public.contact)
137 g_object_unref (event->public.contact);
140 g_slice_free (EventPriv, event);
144 event_remove (EventPriv *event)
146 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
148 DEBUG ("Removing event %p", event);
149 priv->events = g_slist_remove (priv->events, event);
150 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
155 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
156 const gchar *icon_name, const gchar *header, const gchar *message,
157 EventManagerApproval *approval, EventFunc func, gpointer user_data)
159 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
162 event = g_slice_new0 (EventPriv);
163 event->public.contact = contact ? g_object_ref (contact) : NULL;
164 event->public.icon_name = g_strdup (icon_name);
165 event->public.header = g_strdup (header);
166 event->public.message = g_strdup (message);
168 event->user_data = user_data;
169 event->manager = manager;
170 event->approval = approval;
172 DEBUG ("Adding event %p", event);
173 priv->events = g_slist_prepend (priv->events, event);
174 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
178 event_channel_process_func (EventPriv *event)
180 empathy_dispatch_operation_approve (event->approval->operation);
184 event_lookup_by_approval (EmpathyEventManager *manager,
185 EventManagerApproval *approval)
187 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
189 EventPriv *retval = NULL;
191 for (l = priv->events; l; l = l->next)
193 EventPriv *event = l->data;
195 if (event->approval == approval)
205 event_update (EmpathyEventManager *manager, EventPriv *event,
206 const char *icon_name, const char *header, const char *msg)
208 g_free (event->public.icon_name);
209 g_free (event->public.header);
210 g_free (event->public.message);
212 event->public.icon_name = g_strdup (icon_name);
213 event->public.header = g_strdup (header);
214 event->public.message = g_strdup (msg);
216 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
220 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
221 EmpathyMessage *message, EventManagerApproval *approval)
223 EmpathyContact *sender;
229 /* update the event if it's referring to a chat which is already in the
232 event = event_lookup_by_approval (approval->manager, approval);
234 sender = empathy_message_get_sender (message);
235 header = g_strdup_printf (_("New message from %s"),
236 empathy_contact_get_name (sender));
237 msg = empathy_message_get_body (message);
239 channel = empathy_tp_chat_get_channel (tp_chat);
242 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
244 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
245 msg, approval, event_channel_process_func, NULL);
251 event_manager_approval_done (EventManagerApproval *approval)
253 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
256 priv->approvals = g_slist_remove (priv->approvals, approval);
258 for (l = priv->events; l; l = l->next)
260 EventPriv *event = l->data;
262 if (event->approval == approval)
264 event_remove (event);
269 event_manager_approval_free (approval);
273 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
274 EventManagerApproval *approval)
276 event_manager_approval_done (approval);
280 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
281 EventManagerApproval *approval)
283 event_manager_approval_done (approval);
287 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
288 guint domain, gint code, gchar *message,
289 EventManagerApproval *approval)
291 event_manager_approval_done (approval);
295 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
296 const GError *error, gpointer user_data, GObject *object)
298 EventManagerApproval *approval = user_data;
303 /* FIXME just returning assuming the operation will be invalidated as
308 header = g_strdup_printf (_("Incoming call from %s"),
309 empathy_contact_get_name (contact));
311 event_manager_add (approval->manager,
312 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
313 approval, event_channel_process_func, NULL);
319 event_manager_media_channel_got_contact (EventManagerApproval *approval)
321 empathy_contact_call_when_ready (approval->contact,
322 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
323 approval, NULL, G_OBJECT (approval->manager));
327 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
328 GParamSpec *param, EventManagerApproval *approval)
330 EmpathyContact *contact;
332 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
337 approval->contact = contact;
338 event_manager_media_channel_got_contact (approval);
342 event_manager_tube_approved_cb (EventPriv *event)
344 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
348 event_manager_add_tube_approval (EventManagerApproval *approval,
349 EmpathyTubeDispatchAbility ability)
351 const gchar *icon_name;
355 header = g_strdup_printf (_("%s is offering you an invitation"),
356 empathy_contact_get_name (approval->contact));
358 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
360 icon_name = GTK_STOCK_EXECUTE;
361 msg = _("An external application will be started to handle it.");
365 icon_name = GTK_STOCK_DIALOG_ERROR;
366 msg = _("You don't have the needed external "
367 "application to handle it.");
370 event_manager_add (approval->manager, approval->contact, icon_name, header,
371 msg, approval, event_manager_tube_approved_cb, approval);
377 event_manager_tube_dispatch_ability_cb (GObject *object,
378 GParamSpec *spec, gpointer user_data)
380 EventManagerApproval *approval = (EventManagerApproval *)user_data;
381 EmpathyTubeDispatchAbility dispatchability;
384 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
386 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
388 event_manager_add_tube_approval (approval, dispatchability);
389 g_signal_handler_disconnect (object, approval->handler);
390 approval->handler = 0;
395 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
396 const GError *error, gpointer user_data, GObject *object)
398 EventManagerApproval *approval = (EventManagerApproval *)user_data;
399 EmpathyTubeDispatchAbility dispatchability;
403 /* FIXME?, we assume that the operation gets invalidated as well (if it
408 dispatchability = empathy_tube_dispatch_is_dispatchable
409 (approval->tube_dispatch);
412 switch (dispatchability)
414 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
415 approval->handler = g_signal_connect (approval->tube_dispatch,
416 "notify::dispatchability",
417 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
419 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
421 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
422 event_manager_add_tube_approval (approval, dispatchability);
428 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
429 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
431 const gchar *channel_type;
432 EventManagerApproval *approval;
433 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
435 channel_type = empathy_dispatch_operation_get_channel_type (operation);
437 approval = event_manager_approval_new (manager, operation);
438 priv->approvals = g_slist_prepend (priv->approvals, approval);
440 approval->approved_handler = g_signal_connect (operation, "approved",
441 G_CALLBACK (event_manager_operation_approved_cb), approval);
443 approval->claimed_handler = g_signal_connect (operation, "claimed",
444 G_CALLBACK (event_manager_operation_claimed_cb), approval);
446 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
447 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
449 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
451 EmpathyTpChat *tp_chat =
453 empathy_dispatch_operation_get_channel_wrapper (operation));
455 g_signal_connect (tp_chat, "message-received",
456 G_CALLBACK (event_manager_chat_message_received_cb), approval);
459 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
461 EmpathyContact *contact;
462 EmpathyTpCall *call = EMPATHY_TP_CALL (
463 empathy_dispatch_operation_get_channel_wrapper (operation));
465 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
469 g_signal_connect (call, "notify::contact",
470 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
475 approval->contact = contact;
476 event_manager_media_channel_got_contact (approval);
480 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
482 EmpathyContact *contact;
486 EmpathyContactFactory *factory;
487 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
489 factory = empathy_contact_factory_dup_singleton ();
490 handle = tp_channel_get_handle (channel, NULL);
491 account = empathy_channel_get_account (channel);
493 contact = empathy_contact_factory_get_from_handle (factory, account,
496 empathy_contact_run_until_ready (contact,
497 EMPATHY_CONTACT_READY_NAME, NULL);
499 header = g_strdup_printf (_("Incoming file transfer from %s"),
500 empathy_contact_get_name (contact));
502 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
503 header, NULL, approval, event_channel_process_func, NULL);
505 g_object_unref (factory);
506 g_object_unref (account);
509 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
510 !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
512 EmpathyContact *contact;
514 TpHandleType handle_type;
516 EmpathyContactFactory *factory;
517 EmpathyTubeDispatch *tube_dispatch;
520 channel = empathy_dispatch_operation_get_channel (operation);
522 handle = tp_channel_get_handle (channel, &handle_type);
524 /* Only understand p2p tubes */
525 if (handle_type != TP_HANDLE_TYPE_CONTACT)
528 factory = empathy_contact_factory_dup_singleton ();
529 account = empathy_channel_get_account (channel);
531 contact = empathy_contact_factory_get_from_handle (factory, account,
534 tube_dispatch = empathy_tube_dispatch_new (operation);
536 approval->contact = contact;
537 approval->tube_dispatch = tube_dispatch;
539 empathy_contact_call_when_ready (contact,
540 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
541 approval, NULL, G_OBJECT (manager));
543 g_object_unref (factory);
544 g_object_unref (account);
548 DEBUG ("Unknown channel type, ignoring..");
553 event_pending_subscribe_func (EventPriv *event)
555 empathy_subscription_dialog_show (event->public.contact, NULL);
556 event_remove (event);
560 event_manager_pendings_changed_cb (EmpathyContactList *list,
561 EmpathyContact *contact, EmpathyContact *actor,
562 guint reason, gchar *message, gboolean is_pending,
563 EmpathyEventManager *manager)
565 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
566 gchar *header, *event_msg;
572 for (l = priv->events; l; l = l->next)
574 EventPriv *event = l->data;
576 if (event->public.contact == contact &&
577 event->func == event_pending_subscribe_func)
579 event_remove (event);
587 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
589 header = g_strdup_printf (_("Subscription requested by %s"),
590 empathy_contact_get_name (contact));
592 if (!EMP_STR_EMPTY (message))
593 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
597 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
598 event_msg, NULL, event_pending_subscribe_func, NULL);
605 event_manager_constructor (GType type,
607 GObjectConstructParam *props)
611 if (manager_singleton) {
612 retval = g_object_ref (manager_singleton);
614 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
615 (type, n_props, props);
617 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
618 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
625 event_manager_finalize (GObject *object)
627 EmpathyEventManagerPriv *priv = GET_PRIV (object);
629 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
630 g_slist_free (priv->events);
631 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
632 g_slist_free (priv->approvals);
633 g_object_unref (priv->contact_manager);
634 g_object_unref (priv->dispatcher);
638 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
640 GObjectClass *object_class = G_OBJECT_CLASS (klass);
642 object_class->finalize = event_manager_finalize;
643 object_class->constructor = event_manager_constructor;
645 signals[EVENT_ADDED] =
646 g_signal_new ("event-added",
647 G_TYPE_FROM_CLASS (klass),
651 g_cclosure_marshal_VOID__POINTER,
655 signals[EVENT_REMOVED] =
656 g_signal_new ("event-removed",
657 G_TYPE_FROM_CLASS (klass),
661 g_cclosure_marshal_VOID__POINTER,
662 G_TYPE_NONE, 1, G_TYPE_POINTER);
664 signals[EVENT_UPDATED] =
665 g_signal_new ("event-updated",
666 G_TYPE_FROM_CLASS (klass),
670 g_cclosure_marshal_VOID__POINTER,
671 G_TYPE_NONE, 1, G_TYPE_POINTER);
674 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
678 empathy_event_manager_init (EmpathyEventManager *manager)
680 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
681 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
683 manager->priv = priv;
685 priv->dispatcher = empathy_dispatcher_dup_singleton ();
686 priv->contact_manager = empathy_contact_manager_dup_singleton ();
687 g_signal_connect (priv->dispatcher, "approve",
688 G_CALLBACK (event_manager_approve_channel_cb), manager);
689 g_signal_connect (priv->contact_manager, "pendings-changed",
690 G_CALLBACK (event_manager_pendings_changed_cb), manager);
693 EmpathyEventManager *
694 empathy_event_manager_dup_singleton (void)
696 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
700 empathy_event_manager_get_events (EmpathyEventManager *manager)
702 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
704 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
710 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
712 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
714 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
716 return priv->events ? priv->events->data : NULL;
720 empathy_event_activate (EmpathyEvent *event_public)
722 EventPriv *event = (EventPriv*) event_public;
724 g_return_if_fail (event_public != NULL);
729 event_remove (event);