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>
24 #include <glib/gi18n.h>
26 #include <telepathy-glib/util.h>
28 #include <libempathy/empathy-dispatcher.h>
29 #include <libempathy/empathy-contact-factory.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-chat.h>
32 #include <libempathy/empathy-tp-call.h>
33 #include <libempathy/empathy-tp-group.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 guint approved_handler;
53 guint claimed_handler;
54 /* Remove contact if applicable */
55 EmpathyContact *contact;
56 /* Tube dispatcher if applicable */
57 EmpathyTubeDispatch *tube_dispatch;
58 /* option signal handler */
60 } EventManagerApproval;
63 EmpathyDispatcher *dispatcher;
64 EmpathyContactManager *contact_manager;
66 /* Ongoing approvals */
68 } EmpathyEventManagerPriv;
70 typedef struct _EventPriv EventPriv;
71 typedef void (*EventFunc) (EventPriv *event);
75 EmpathyEventManager *manager;
76 EventManagerApproval *approval;
87 static guint signals[LAST_SIGNAL];
89 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
91 static EmpathyEventManager * manager_singleton = NULL;
93 static EventManagerApproval *
94 event_manager_approval_new (EmpathyEventManager *manager,
95 EmpathyDispatchOperation *operation)
97 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
98 result->operation = g_object_ref (operation);
99 result->manager = manager;
105 event_manager_approval_free (EventManagerApproval *approval)
107 g_signal_handler_disconnect (approval->operation,
108 approval->approved_handler);
109 g_signal_handler_disconnect (approval->operation,
110 approval->claimed_handler);
111 g_object_unref (approval->operation);
113 if (approval->contact != NULL)
114 g_object_unref (approval->contact);
116 if (approval->tube_dispatch != NULL)
117 g_object_unref (approval->tube_dispatch);
119 g_slice_free (EventManagerApproval, approval);
122 static void event_remove (EventPriv *event);
125 event_free (EventPriv *event)
127 g_free (event->public.icon_name);
128 g_free (event->public.message);
130 if (event->public.contact) {
131 g_object_unref (event->public.contact);
134 g_slice_free (EventPriv, event);
138 event_remove (EventPriv *event)
140 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
142 DEBUG ("Removing event %p", event);
143 priv->events = g_slist_remove (priv->events, event);
144 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
149 event_manager_add (EmpathyEventManager *manager,
150 EmpathyContact *contact,
151 const gchar *icon_name,
152 const gchar *message,
153 EventManagerApproval *approval,
157 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
160 event = g_slice_new0 (EventPriv);
161 event->public.contact = contact ? g_object_ref (contact) : NULL;
162 event->public.icon_name = g_strdup (icon_name);
163 event->public.message = g_strdup (message);
165 event->user_data = user_data;
166 event->manager = manager;
169 event->approval = approval;
171 g_signal_connect_swapped (channel, "invalidated",
172 G_CALLBACK (event_remove),
177 DEBUG ("Adding event %p", event);
178 priv->events = g_slist_prepend (priv->events, event);
179 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
183 event_channel_process_func (EventPriv *event)
185 empathy_dispatch_operation_approve (event->approval->operation);
189 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
190 EmpathyMessage *message,
191 EventManagerApproval *approval)
193 EmpathyContact *sender;
197 g_signal_handlers_disconnect_by_func (tp_chat,
198 event_manager_chat_message_received_cb,
201 sender = empathy_message_get_sender (message);
202 msg = g_strdup_printf (_("New message from %s:\n%s"),
203 empathy_contact_get_name (sender),
204 empathy_message_get_body (message));
206 channel = empathy_tp_chat_get_channel (tp_chat);
207 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
208 approval, event_channel_process_func, NULL);
214 event_manager_approval_done (EventManagerApproval *approval)
216 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
219 priv->approvals = g_slist_remove (priv->approvals, approval);
221 for (l = priv->events; l; l = l->next) {
222 EventPriv *event = l->data;
224 if (event->approval == approval) {
225 event_remove (event);
230 event_manager_approval_free (approval);
234 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
235 EventManagerApproval *approval)
237 event_manager_approval_done (approval);
241 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
242 EventManagerApproval *approval)
244 event_manager_approval_done (approval);
248 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
251 EventManagerApproval *approval = user_data;
254 msg = g_strdup_printf (_("Incoming call from %s"),
255 empathy_contact_get_name (contact));
257 event_manager_add (approval->manager,
258 approval->contact, EMPATHY_IMAGE_VOIP, msg,
259 approval, event_channel_process_func, NULL);
265 event_manager_media_channel_got_contact (EventManagerApproval *approval)
267 empathy_contact_call_when_ready (approval->contact,
268 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
273 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
274 GParamSpec *param, EventManagerApproval *approval)
276 EmpathyContact *contact;
278 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
283 approval->contact = contact;
284 event_manager_media_channel_got_contact (approval);
288 event_manager_tube_approved_cb (EventPriv *event)
290 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
294 event_manager_add_tube_approval (EventManagerApproval *approval,
295 EmpathyTubeDispatchAbility ability)
297 const gchar *icon_name;
300 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
302 icon_name = GTK_STOCK_EXECUTE;
303 msg = g_strdup_printf (_("%s is offering you an invitation."
304 " An external application will be started to handle it."),
305 empathy_contact_get_name (approval->contact));
309 icon_name = GTK_STOCK_DIALOG_ERROR;
310 msg = g_strdup_printf (_("%s is offering you an invitation, but "
311 "you don't have the needed external "
312 "application to handle it."),
313 empathy_contact_get_name (approval->contact));
316 event_manager_add (approval->manager, approval->contact, icon_name, msg,
317 approval, event_manager_tube_approved_cb, approval);
323 event_manager_tube_dispatch_ability_cb (GObject *object,
324 GParamSpec *spec, gpointer user_data)
326 EventManagerApproval *approval = (EventManagerApproval *)user_data;
327 EmpathyTubeDispatchAbility dispatchability;
330 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
332 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
334 event_manager_add_tube_approval (approval, dispatchability);
335 g_signal_handler_disconnect (object, approval->handler);
336 approval->handler = 0;
341 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
344 EventManagerApproval *approval = (EventManagerApproval *)user_data;
345 EmpathyTubeDispatchAbility dispatchability;
347 dispatchability = empathy_tube_dispatch_is_dispatchable
348 (approval->tube_dispatch);
350 switch (dispatchability)
352 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
353 approval->handler = g_signal_connect (approval->tube_dispatch,
354 "notify::dispatchability",
355 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
357 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
359 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
360 event_manager_add_tube_approval (approval, dispatchability);
366 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
367 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
369 const gchar *channel_type;
370 EventManagerApproval *approval;
371 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
373 channel_type = empathy_dispatch_operation_get_channel_type (operation);
375 approval = event_manager_approval_new (manager, operation);
376 priv->approvals = g_slist_prepend (priv->approvals, approval);
378 approval->approved_handler = g_signal_connect (operation, "approved",
379 G_CALLBACK (event_manager_operation_approved_cb), approval);
381 approval->claimed_handler = g_signal_connect (operation, "claimed",
382 G_CALLBACK (event_manager_operation_claimed_cb), approval);
384 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
386 EmpathyTpChat *tp_chat =
388 empathy_dispatch_operation_get_channel_wrapper (operation));
390 g_signal_connect (tp_chat, "message-received",
391 G_CALLBACK (event_manager_chat_message_received_cb), approval);
392 g_object_unref (G_OBJECT (tp_chat));
395 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
397 EmpathyContact *contact;
398 EmpathyTpCall *call = EMPATHY_TP_CALL (
399 empathy_dispatch_operation_get_channel_wrapper (operation));
401 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
405 g_signal_connect (call, "notify::contact",
406 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
411 approval->contact = contact;
412 event_manager_media_channel_got_contact (approval);
416 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
418 EmpathyContact *contact;
422 EmpathyContactFactory *factory;
423 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
425 factory = empathy_contact_factory_dup_singleton ();
426 handle = tp_channel_get_handle (channel, NULL);
427 account = empathy_channel_get_account (channel);
429 contact = empathy_contact_factory_get_from_handle (factory, account,
432 empathy_contact_run_until_ready (contact,
433 EMPATHY_CONTACT_READY_NAME, NULL);
435 msg = g_strdup_printf (_("Incoming file transfer from %s"),
436 empathy_contact_get_name (contact));
438 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
439 msg, approval, event_channel_process_func, NULL);
441 g_object_unref (channel);
442 g_object_unref (factory);
443 g_object_unref (account);
445 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
447 EmpathyContact *contact;
450 EmpathyContactFactory *factory;
451 EmpathyTubeDispatch *tube_dispatch;
454 channel = empathy_dispatch_operation_get_channel (operation);
456 factory = empathy_contact_factory_new ();
457 handle = tp_channel_get_handle (channel, NULL);
458 account = empathy_channel_get_account (channel);
460 contact = empathy_contact_factory_get_from_handle (factory, account,
463 tube_dispatch = empathy_tube_dispatch_new (operation);
465 approval->contact = contact;
466 approval->tube_dispatch = tube_dispatch;
468 empathy_contact_call_when_ready (contact,
469 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
472 g_object_unref (channel);
473 g_object_unref (factory);
474 g_object_unref (account);
478 DEBUG ("Unknown channel type, ignoring..");
482 #if 0 /* FIXME dispatcher */
484 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
485 "you don't have the needed external " \
486 "application to handle it.")
490 event_tube_process_func (EventPriv *event)
492 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
493 EmpathyDispatcherTube *tube = (EmpathyDispatcherTube*) event->user_data;
495 if (tube->activatable) {
496 empathy_dispatcher_tube_process (priv->dispatcher, tube);
501 /* Tell the user that the tube can't be handled */
502 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
503 empathy_contact_get_name (tube->initiator));
505 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
509 gtk_window_set_title (GTK_WINDOW (dialog),
510 _("Invitation Error"));
513 gtk_widget_show (dialog);
514 g_signal_connect (dialog, "response",
515 G_CALLBACK (gtk_widget_destroy),
519 empathy_dispatcher_tube_unref (tube);
520 event_remove (event);
524 event_manager_filter_tube_cb (EmpathyDispatcher *dispatcher,
525 EmpathyDispatcherTube *tube,
526 EmpathyEventManager *manager)
528 const gchar *icon_name;
531 empathy_contact_run_until_ready (tube->initiator,
532 EMPATHY_CONTACT_READY_NAME, NULL);
534 if (tube->activatable) {
535 icon_name = GTK_STOCK_EXECUTE;
536 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
537 "application will be started to handle it."),
538 empathy_contact_get_name (tube->initiator));
540 icon_name = GTK_STOCK_DIALOG_ERROR;
541 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
542 empathy_contact_get_name (tube->initiator));
545 event_manager_add (manager, tube->initiator, icon_name, msg,
546 tube->channel, event_tube_process_func,
547 empathy_dispatcher_tube_ref (tube));
554 event_pending_subscribe_func (EventPriv *event)
556 empathy_subscription_dialog_show (event->public.contact, NULL);
557 event_remove (event);
561 event_manager_pendings_changed_cb (EmpathyContactList *list,
562 EmpathyContact *contact,
563 EmpathyContact *actor,
567 EmpathyEventManager *manager)
569 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
575 for (l = priv->events; l; l = l->next) {
576 EventPriv *event = l->data;
578 if (event->public.contact == contact &&
579 event->func == event_pending_subscribe_func) {
580 event_remove (event);
588 empathy_contact_run_until_ready (contact,
589 EMPATHY_CONTACT_READY_NAME,
592 str = g_string_new (NULL);
593 g_string_printf (str, _("Subscription requested by %s"),
594 empathy_contact_get_name (contact));
595 if (!G_STR_EMPTY (message)) {
596 g_string_append_printf (str, _("\nMessage: %s"), message);
599 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
600 NULL, event_pending_subscribe_func, NULL);
602 g_string_free (str, TRUE);
606 event_manager_constructor (GType type,
608 GObjectConstructParam *props)
612 if (manager_singleton) {
613 retval = g_object_ref (manager_singleton);
615 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
616 (type, n_props, props);
618 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
619 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
626 event_manager_finalize (GObject *object)
628 EmpathyEventManagerPriv *priv = GET_PRIV (object);
630 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
631 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
632 g_slist_free (priv->events);
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,
665 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
669 empathy_event_manager_init (EmpathyEventManager *manager)
671 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
672 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
674 manager->priv = priv;
676 priv->dispatcher = empathy_get_dispatcher ();
677 priv->contact_manager = empathy_contact_manager_dup_singleton ();
678 g_signal_connect (priv->dispatcher, "approve",
679 G_CALLBACK (event_manager_approve_channel_cb),
681 /*g_signal_connect (priv->dispatcher, "dispatch-channel",
682 G_CALLBACK (event_manager_dispatch_channel_cb),
685 #if 0 /* FIXME dispatcher */
686 g_signal_connect (priv->dispatcher, "filter-tube",
687 G_CALLBACK (event_manager_filter_tube_cb),
690 g_signal_connect (priv->contact_manager, "pendings-changed",
691 G_CALLBACK (event_manager_pendings_changed_cb),
695 EmpathyEventManager *
696 empathy_event_manager_dup_singleton (void)
698 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
702 empathy_event_manager_get_events (EmpathyEventManager *manager)
704 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
706 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
712 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
714 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
716 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
718 return priv->events ? priv->events->data : NULL;
722 empathy_event_activate (EmpathyEvent *event_public)
724 EventPriv *event = (EventPriv*) event_public;
726 g_return_if_fail (event_public != NULL);
731 event_remove (event);