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 /* 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;
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
92 static EmpathyEventManager * manager_singleton = NULL;
94 static EventManagerApproval *
95 event_manager_approval_new (EmpathyEventManager *manager,
96 EmpathyDispatchOperation *operation)
98 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
99 result->operation = g_object_ref (operation);
100 result->manager = manager;
106 event_manager_approval_free (EventManagerApproval *approval)
108 g_signal_handler_disconnect (approval->operation,
109 approval->approved_handler);
110 g_signal_handler_disconnect (approval->operation,
111 approval->claimed_handler);
112 g_object_unref (approval->operation);
114 if (approval->contact != NULL)
115 g_object_unref (approval->contact);
117 if (approval->tube_dispatch != NULL)
118 g_object_unref (approval->tube_dispatch);
120 g_slice_free (EventManagerApproval, approval);
123 static void event_remove (EventPriv *event);
126 event_free (EventPriv *event)
128 g_free (event->public.icon_name);
129 g_free (event->public.message);
131 if (event->public.contact)
133 g_object_unref (event->public.contact);
136 g_slice_free (EventPriv, event);
140 event_remove (EventPriv *event)
142 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
144 DEBUG ("Removing event %p", event);
145 priv->events = g_slist_remove (priv->events, event);
146 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
151 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
152 const gchar *icon_name, const gchar *message,
153 EventManagerApproval *approval, EventFunc func,
156 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
159 event = g_slice_new0 (EventPriv);
160 event->public.contact = contact ? g_object_ref (contact) : NULL;
161 event->public.icon_name = g_strdup (icon_name);
162 event->public.message = g_strdup (message);
164 event->user_data = user_data;
165 event->manager = manager;
168 event->approval = approval;
170 DEBUG ("Adding event %p", event);
171 priv->events = g_slist_prepend (priv->events, event);
172 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
176 event_channel_process_func (EventPriv *event)
178 empathy_dispatch_operation_approve (event->approval->operation);
182 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
183 EmpathyMessage *message, EventManagerApproval *approval)
185 EmpathyContact *sender;
189 g_signal_handlers_disconnect_by_func (tp_chat,
190 event_manager_chat_message_received_cb, approval);
192 sender = empathy_message_get_sender (message);
193 msg = g_strdup_printf (_("New message from %s:\n%s"),
194 empathy_contact_get_name (sender), empathy_message_get_body (message));
196 channel = empathy_tp_chat_get_channel (tp_chat);
197 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
198 approval, event_channel_process_func, NULL);
204 event_manager_approval_done (EventManagerApproval *approval)
206 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
209 priv->approvals = g_slist_remove (priv->approvals, approval);
211 for (l = priv->events; l; l = l->next)
213 EventPriv *event = l->data;
215 if (event->approval == approval)
217 event_remove (event);
222 event_manager_approval_free (approval);
226 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
227 EventManagerApproval *approval)
229 event_manager_approval_done (approval);
233 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
234 EventManagerApproval *approval)
236 event_manager_approval_done (approval);
240 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
243 EventManagerApproval *approval = user_data;
246 msg = g_strdup_printf (_("Incoming call from %s"),
247 empathy_contact_get_name (contact));
249 event_manager_add (approval->manager,
250 approval->contact, EMPATHY_IMAGE_VOIP, msg,
251 approval, event_channel_process_func, NULL);
257 event_manager_media_channel_got_contact (EventManagerApproval *approval)
259 empathy_contact_call_when_ready (approval->contact,
260 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
265 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
266 GParamSpec *param, EventManagerApproval *approval)
268 EmpathyContact *contact;
270 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
275 approval->contact = contact;
276 event_manager_media_channel_got_contact (approval);
280 event_manager_tube_approved_cb (EventPriv *event)
282 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
286 event_manager_add_tube_approval (EventManagerApproval *approval,
287 EmpathyTubeDispatchAbility ability)
289 const gchar *icon_name;
292 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
294 icon_name = GTK_STOCK_EXECUTE;
295 msg = g_strdup_printf (_("%s is offering you an invitation."
296 " An external application will be started to handle it."),
297 empathy_contact_get_name (approval->contact));
301 icon_name = GTK_STOCK_DIALOG_ERROR;
302 msg = g_strdup_printf (_("%s is offering you an invitation, but "
303 "you don't have the needed external "
304 "application to handle it."),
305 empathy_contact_get_name (approval->contact));
308 event_manager_add (approval->manager, approval->contact, icon_name, msg,
309 approval, event_manager_tube_approved_cb, approval);
315 event_manager_tube_dispatch_ability_cb (GObject *object,
316 GParamSpec *spec, gpointer user_data)
318 EventManagerApproval *approval = (EventManagerApproval *)user_data;
319 EmpathyTubeDispatchAbility dispatchability;
322 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
324 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
326 event_manager_add_tube_approval (approval, dispatchability);
327 g_signal_handler_disconnect (object, approval->handler);
328 approval->handler = 0;
333 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
336 EventManagerApproval *approval = (EventManagerApproval *)user_data;
337 EmpathyTubeDispatchAbility dispatchability;
339 dispatchability = empathy_tube_dispatch_is_dispatchable
340 (approval->tube_dispatch);
342 switch (dispatchability)
344 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
345 approval->handler = g_signal_connect (approval->tube_dispatch,
346 "notify::dispatchability",
347 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
349 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
351 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
352 event_manager_add_tube_approval (approval, dispatchability);
358 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
359 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
361 const gchar *channel_type;
362 EventManagerApproval *approval;
363 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
365 channel_type = empathy_dispatch_operation_get_channel_type (operation);
367 approval = event_manager_approval_new (manager, operation);
368 priv->approvals = g_slist_prepend (priv->approvals, approval);
370 approval->approved_handler = g_signal_connect (operation, "approved",
371 G_CALLBACK (event_manager_operation_approved_cb), approval);
373 approval->claimed_handler = g_signal_connect (operation, "claimed",
374 G_CALLBACK (event_manager_operation_claimed_cb), approval);
376 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
378 EmpathyTpChat *tp_chat =
380 empathy_dispatch_operation_get_channel_wrapper (operation));
382 g_signal_connect (tp_chat, "message-received",
383 G_CALLBACK (event_manager_chat_message_received_cb), approval);
384 g_object_unref (G_OBJECT (tp_chat));
387 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
389 EmpathyContact *contact;
390 EmpathyTpCall *call = EMPATHY_TP_CALL (
391 empathy_dispatch_operation_get_channel_wrapper (operation));
393 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
397 g_signal_connect (call, "notify::contact",
398 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
403 approval->contact = contact;
404 event_manager_media_channel_got_contact (approval);
408 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
410 EmpathyContact *contact;
414 EmpathyContactFactory *factory;
415 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
417 factory = empathy_contact_factory_dup_singleton ();
418 handle = tp_channel_get_handle (channel, NULL);
419 account = empathy_channel_get_account (channel);
421 contact = empathy_contact_factory_get_from_handle (factory, account,
424 empathy_contact_run_until_ready (contact,
425 EMPATHY_CONTACT_READY_NAME, NULL);
427 msg = g_strdup_printf (_("Incoming file transfer from %s"),
428 empathy_contact_get_name (contact));
430 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
431 msg, approval, event_channel_process_func, NULL);
433 g_object_unref (channel);
434 g_object_unref (factory);
435 g_object_unref (account);
437 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
439 EmpathyContact *contact;
442 EmpathyContactFactory *factory;
443 EmpathyTubeDispatch *tube_dispatch;
446 channel = empathy_dispatch_operation_get_channel (operation);
448 factory = empathy_contact_factory_new ();
449 handle = tp_channel_get_handle (channel, NULL);
450 account = empathy_channel_get_account (channel);
452 contact = empathy_contact_factory_get_from_handle (factory, account,
455 tube_dispatch = empathy_tube_dispatch_new (operation);
457 approval->contact = contact;
458 approval->tube_dispatch = tube_dispatch;
460 empathy_contact_call_when_ready (contact,
461 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
464 g_object_unref (channel);
465 g_object_unref (factory);
466 g_object_unref (account);
470 DEBUG ("Unknown channel type, ignoring..");
475 event_pending_subscribe_func (EventPriv *event)
477 empathy_subscription_dialog_show (event->public.contact, NULL);
478 event_remove (event);
482 event_manager_pendings_changed_cb (EmpathyContactList *list,
483 EmpathyContact *contact, EmpathyContact *actor,
484 guint reason, gchar *message, gboolean is_pending,
485 EmpathyEventManager *manager)
487 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
494 for (l = priv->events; l; l = l->next)
496 EventPriv *event = l->data;
498 if (event->public.contact == contact &&
499 event->func == event_pending_subscribe_func)
501 event_remove (event);
509 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
511 str = g_string_new (NULL);
512 g_string_printf (str, _("Subscription requested by %s"),
513 empathy_contact_get_name (contact));
515 if (!G_STR_EMPTY (message))
516 g_string_append_printf (str, _("\nMessage: %s"), message);
518 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
519 NULL, event_pending_subscribe_func, NULL);
521 g_string_free (str, TRUE);
525 event_manager_constructor (GType type,
527 GObjectConstructParam *props)
531 if (manager_singleton) {
532 retval = g_object_ref (manager_singleton);
534 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
535 (type, n_props, props);
537 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
538 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
545 event_manager_finalize (GObject *object)
547 EmpathyEventManagerPriv *priv = GET_PRIV (object);
549 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
550 g_slist_free (priv->events);
551 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
552 g_slist_free (priv->approvals);
553 g_object_unref (priv->contact_manager);
554 g_object_unref (priv->dispatcher);
558 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
560 GObjectClass *object_class = G_OBJECT_CLASS (klass);
562 object_class->finalize = event_manager_finalize;
563 object_class->constructor = event_manager_constructor;
565 signals[EVENT_ADDED] =
566 g_signal_new ("event-added",
567 G_TYPE_FROM_CLASS (klass),
571 g_cclosure_marshal_VOID__POINTER,
575 signals[EVENT_REMOVED] =
576 g_signal_new ("event-removed",
577 G_TYPE_FROM_CLASS (klass),
581 g_cclosure_marshal_VOID__POINTER,
582 G_TYPE_NONE, 1, G_TYPE_POINTER);
584 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
588 empathy_event_manager_init (EmpathyEventManager *manager)
590 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
591 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
593 manager->priv = priv;
595 priv->dispatcher = empathy_dispatcher_dup_singleton ();
596 priv->contact_manager = empathy_contact_manager_dup_singleton ();
597 g_signal_connect (priv->dispatcher, "approve",
598 G_CALLBACK (event_manager_approve_channel_cb), manager);
599 g_signal_connect (priv->contact_manager, "pendings-changed",
600 G_CALLBACK (event_manager_pendings_changed_cb), manager);
603 EmpathyEventManager *
604 empathy_event_manager_dup_singleton (void)
606 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
610 empathy_event_manager_get_events (EmpathyEventManager *manager)
612 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
614 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
620 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
622 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
624 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
626 return priv->events ? priv->events->data : NULL;
630 empathy_event_activate (EmpathyEvent *event_public)
632 EventPriv *event = (EventPriv*) event_public;
634 g_return_if_fail (event_public != NULL);
639 event_remove (event);