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-tp-group.h>
35 #include <libempathy/empathy-utils.h>
37 #include <extensions/extensions.h>
39 #include <libempathy-gtk/empathy-images.h>
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include "empathy-event-manager.h"
43 #include "empathy-tube-dispatch.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
51 EmpathyEventManager *manager;
52 EmpathyDispatchOperation *operation;
53 gulong approved_handler;
54 gulong claimed_handler;
55 gulong invalidated_handler;
56 /* Remove contact if applicable */
57 EmpathyContact *contact;
58 /* Tube dispatcher if applicable */
59 EmpathyTubeDispatch *tube_dispatch;
60 /* option signal handler */
62 } EventManagerApproval;
65 EmpathyDispatcher *dispatcher;
66 EmpathyContactManager *contact_manager;
68 /* Ongoing approvals */
70 } EmpathyEventManagerPriv;
72 typedef struct _EventPriv EventPriv;
73 typedef void (*EventFunc) (EventPriv *event);
77 EmpathyEventManager *manager;
78 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.message);
134 if (event->public.contact)
136 g_object_unref (event->public.contact);
139 g_slice_free (EventPriv, event);
143 event_remove (EventPriv *event)
145 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
147 DEBUG ("Removing event %p", event);
148 priv->events = g_slist_remove (priv->events, event);
149 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
154 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
155 const gchar *icon_name, const gchar *message,
156 EventManagerApproval *approval, EventFunc func,
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.message = g_strdup (message);
167 event->user_data = user_data;
168 event->manager = manager;
171 event->approval = approval;
173 DEBUG ("Adding event %p", event);
174 priv->events = g_slist_prepend (priv->events, event);
175 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
179 event_channel_process_func (EventPriv *event)
181 empathy_dispatch_operation_approve (event->approval->operation);
185 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
186 EmpathyMessage *message, EventManagerApproval *approval)
188 EmpathyContact *sender;
192 g_signal_handlers_disconnect_by_func (tp_chat,
193 event_manager_chat_message_received_cb, approval);
195 sender = empathy_message_get_sender (message);
196 msg = g_strdup_printf (_("New message from %s:\n%s"),
197 empathy_contact_get_name (sender), empathy_message_get_body (message));
199 channel = empathy_tp_chat_get_channel (tp_chat);
200 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
201 approval, event_channel_process_func, NULL);
207 event_manager_approval_done (EventManagerApproval *approval)
209 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
212 priv->approvals = g_slist_remove (priv->approvals, approval);
214 for (l = priv->events; l; l = l->next)
216 EventPriv *event = l->data;
218 if (event->approval == approval)
220 event_remove (event);
225 event_manager_approval_free (approval);
229 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
230 EventManagerApproval *approval)
232 event_manager_approval_done (approval);
236 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
237 EventManagerApproval *approval)
239 event_manager_approval_done (approval);
243 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
244 guint domain, gint code, gchar *message,
245 EventManagerApproval *approval)
247 event_manager_approval_done (approval);
251 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
252 const GError *error, gpointer user_data, GObject *object)
254 EventManagerApproval *approval = user_data;
259 /* FIXME just returning assuming the operation will be invalidated as
264 msg = g_strdup_printf (_("Incoming call from %s"),
265 empathy_contact_get_name (contact));
267 event_manager_add (approval->manager,
268 approval->contact, EMPATHY_IMAGE_VOIP, msg,
269 approval, event_channel_process_func, NULL);
275 event_manager_media_channel_got_contact (EventManagerApproval *approval)
277 empathy_contact_call_when_ready (approval->contact,
278 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
279 approval, NULL, G_OBJECT (approval->manager));
283 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
284 GParamSpec *param, EventManagerApproval *approval)
286 EmpathyContact *contact;
288 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
293 approval->contact = contact;
294 event_manager_media_channel_got_contact (approval);
298 event_manager_tube_approved_cb (EventPriv *event)
300 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
304 event_manager_add_tube_approval (EventManagerApproval *approval,
305 EmpathyTubeDispatchAbility ability)
307 const gchar *icon_name;
310 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
312 icon_name = GTK_STOCK_EXECUTE;
313 msg = g_strdup_printf (_("%s is offering you an invitation."
314 " An external application will be started to handle it."),
315 empathy_contact_get_name (approval->contact));
319 icon_name = GTK_STOCK_DIALOG_ERROR;
320 msg = g_strdup_printf (_("%s is offering you an invitation, but "
321 "you don't have the needed external "
322 "application to handle it."),
323 empathy_contact_get_name (approval->contact));
326 event_manager_add (approval->manager, approval->contact, icon_name, msg,
327 approval, event_manager_tube_approved_cb, approval);
333 event_manager_tube_dispatch_ability_cb (GObject *object,
334 GParamSpec *spec, gpointer user_data)
336 EventManagerApproval *approval = (EventManagerApproval *)user_data;
337 EmpathyTubeDispatchAbility dispatchability;
340 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
342 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
344 event_manager_add_tube_approval (approval, dispatchability);
345 g_signal_handler_disconnect (object, approval->handler);
346 approval->handler = 0;
351 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
352 const GError *error, gpointer user_data, GObject *object)
354 EventManagerApproval *approval = (EventManagerApproval *)user_data;
355 EmpathyTubeDispatchAbility dispatchability;
359 /* FIXME?, we assume that the operation gets invalidated as well (if it
364 dispatchability = empathy_tube_dispatch_is_dispatchable
365 (approval->tube_dispatch);
368 switch (dispatchability)
370 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
371 approval->handler = g_signal_connect (approval->tube_dispatch,
372 "notify::dispatchability",
373 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
375 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
377 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
378 event_manager_add_tube_approval (approval, dispatchability);
384 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
385 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
387 const gchar *channel_type;
388 EventManagerApproval *approval;
389 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
391 channel_type = empathy_dispatch_operation_get_channel_type (operation);
393 approval = event_manager_approval_new (manager, operation);
394 priv->approvals = g_slist_prepend (priv->approvals, approval);
396 approval->approved_handler = g_signal_connect (operation, "approved",
397 G_CALLBACK (event_manager_operation_approved_cb), approval);
399 approval->claimed_handler = g_signal_connect (operation, "claimed",
400 G_CALLBACK (event_manager_operation_claimed_cb), approval);
402 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
403 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
405 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
407 EmpathyTpChat *tp_chat =
409 empathy_dispatch_operation_get_channel_wrapper (operation));
411 g_signal_connect (tp_chat, "message-received",
412 G_CALLBACK (event_manager_chat_message_received_cb), approval);
415 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
417 EmpathyContact *contact;
418 EmpathyTpCall *call = EMPATHY_TP_CALL (
419 empathy_dispatch_operation_get_channel_wrapper (operation));
421 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
425 g_signal_connect (call, "notify::contact",
426 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
431 approval->contact = contact;
432 event_manager_media_channel_got_contact (approval);
436 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
438 EmpathyContact *contact;
442 EmpathyContactFactory *factory;
443 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
445 factory = empathy_contact_factory_dup_singleton ();
446 handle = tp_channel_get_handle (channel, NULL);
447 account = empathy_channel_get_account (channel);
449 contact = empathy_contact_factory_get_from_handle (factory, account,
452 empathy_contact_run_until_ready (contact,
453 EMPATHY_CONTACT_READY_NAME, NULL);
455 msg = g_strdup_printf (_("Incoming file transfer from %s"),
456 empathy_contact_get_name (contact));
458 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
459 msg, approval, event_channel_process_func, NULL);
461 g_object_unref (factory);
462 g_object_unref (account);
464 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
466 EmpathyContact *contact;
469 EmpathyContactFactory *factory;
470 EmpathyTubeDispatch *tube_dispatch;
473 channel = empathy_dispatch_operation_get_channel (operation);
475 factory = empathy_contact_factory_new ();
476 handle = tp_channel_get_handle (channel, NULL);
477 account = empathy_channel_get_account (channel);
479 contact = empathy_contact_factory_get_from_handle (factory, account,
482 tube_dispatch = empathy_tube_dispatch_new (operation);
484 approval->contact = contact;
485 approval->tube_dispatch = tube_dispatch;
487 empathy_contact_call_when_ready (contact,
488 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
489 approval, NULL, G_OBJECT (manager));
491 g_object_unref (factory);
492 g_object_unref (account);
496 DEBUG ("Unknown channel type, ignoring..");
501 event_pending_subscribe_func (EventPriv *event)
503 empathy_subscription_dialog_show (event->public.contact, NULL);
504 event_remove (event);
508 event_manager_pendings_changed_cb (EmpathyContactList *list,
509 EmpathyContact *contact, EmpathyContact *actor,
510 guint reason, gchar *message, gboolean is_pending,
511 EmpathyEventManager *manager)
513 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
520 for (l = priv->events; l; l = l->next)
522 EventPriv *event = l->data;
524 if (event->public.contact == contact &&
525 event->func == event_pending_subscribe_func)
527 event_remove (event);
535 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
537 str = g_string_new (NULL);
538 g_string_printf (str, _("Subscription requested by %s"),
539 empathy_contact_get_name (contact));
541 if (!G_STR_EMPTY (message))
542 g_string_append_printf (str, _("\nMessage: %s"), message);
544 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
545 NULL, event_pending_subscribe_func, NULL);
547 g_string_free (str, TRUE);
551 event_manager_constructor (GType type,
553 GObjectConstructParam *props)
557 if (manager_singleton) {
558 retval = g_object_ref (manager_singleton);
560 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
561 (type, n_props, props);
563 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
564 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
571 event_manager_finalize (GObject *object)
573 EmpathyEventManagerPriv *priv = GET_PRIV (object);
575 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
576 g_slist_free (priv->events);
577 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
578 g_slist_free (priv->approvals);
579 g_object_unref (priv->contact_manager);
580 g_object_unref (priv->dispatcher);
584 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
586 GObjectClass *object_class = G_OBJECT_CLASS (klass);
588 object_class->finalize = event_manager_finalize;
589 object_class->constructor = event_manager_constructor;
591 signals[EVENT_ADDED] =
592 g_signal_new ("event-added",
593 G_TYPE_FROM_CLASS (klass),
597 g_cclosure_marshal_VOID__POINTER,
601 signals[EVENT_REMOVED] =
602 g_signal_new ("event-removed",
603 G_TYPE_FROM_CLASS (klass),
607 g_cclosure_marshal_VOID__POINTER,
608 G_TYPE_NONE, 1, G_TYPE_POINTER);
610 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
614 empathy_event_manager_init (EmpathyEventManager *manager)
616 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
617 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
619 manager->priv = priv;
621 priv->dispatcher = empathy_dispatcher_dup_singleton ();
622 priv->contact_manager = empathy_contact_manager_dup_singleton ();
623 g_signal_connect (priv->dispatcher, "approve",
624 G_CALLBACK (event_manager_approve_channel_cb), manager);
625 g_signal_connect (priv->contact_manager, "pendings-changed",
626 G_CALLBACK (event_manager_pendings_changed_cb), manager);
629 EmpathyEventManager *
630 empathy_event_manager_dup_singleton (void)
632 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
636 empathy_event_manager_get_events (EmpathyEventManager *manager)
638 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
640 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
646 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
648 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
650 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
652 return priv->events ? priv->events->data : NULL;
656 empathy_event_activate (EmpathyEvent *event_public)
658 EventPriv *event = (EventPriv*) event_public;
660 g_return_if_fail (event_public != NULL);
665 event_remove (event);