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>
28 #include <telepathy-glib/interfaces.h>
30 #include <libempathy/empathy-account-manager.h>
31 #include <libempathy/empathy-dispatcher.h>
32 #include <libempathy/empathy-tp-contact-factory.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-tp-chat.h>
35 #include <libempathy/empathy-tp-call.h>
36 #include <libempathy/empathy-tp-file.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-call-factory.h>
40 #include <extensions/extensions.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-contact-dialogs.h>
45 #include <libempathy-gtk/empathy-sound.h>
47 #include "empathy-event-manager.h"
48 #include "empathy-main-window.h"
49 #include "empathy-tube-dispatch.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
52 #include <libempathy/empathy-debug.h>
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
56 #define NOTIFICATION_TIMEOUT 2 /* seconds */
58 /* The time interval in milliseconds between 2 incoming rings */
59 #define MS_BETWEEN_RING 500
62 EmpathyEventManager *manager;
63 EmpathyDispatchOperation *operation;
64 gulong approved_handler;
65 gulong claimed_handler;
66 gulong invalidated_handler;
67 /* Remove contact if applicable */
68 EmpathyContact *contact;
69 /* Tube dispatcher if applicable */
70 EmpathyTubeDispatch *tube_dispatch;
71 /* option signal handler and it's instance */
73 GObject *handler_instance;
74 /* optional accept widget */
76 } EventManagerApproval;
79 EmpathyDispatcher *dispatcher;
80 EmpathyContactManager *contact_manager;
82 /* Ongoing approvals */
86 } EmpathyEventManagerPriv;
88 typedef struct _EventPriv EventPriv;
89 typedef void (*EventFunc) (EventPriv *event);
93 EmpathyEventManager *manager;
94 EventManagerApproval *approval;
107 static guint signals[LAST_SIGNAL];
109 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
111 static EmpathyEventManager * manager_singleton = NULL;
113 static EventManagerApproval *
114 event_manager_approval_new (EmpathyEventManager *manager,
115 EmpathyDispatchOperation *operation)
117 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
118 result->operation = g_object_ref (operation);
119 result->manager = manager;
125 event_manager_approval_free (EventManagerApproval *approval)
127 g_signal_handler_disconnect (approval->operation,
128 approval->approved_handler);
129 g_signal_handler_disconnect (approval->operation,
130 approval->claimed_handler);
131 g_signal_handler_disconnect (approval->operation,
132 approval->invalidated_handler);
133 g_object_unref (approval->operation);
135 if (approval->handler != 0)
136 g_signal_handler_disconnect (approval->handler_instance,
139 if (approval->contact != NULL)
140 g_object_unref (approval->contact);
142 if (approval->tube_dispatch != NULL)
143 g_object_unref (approval->tube_dispatch);
145 if (approval->dialog != NULL)
147 gtk_widget_destroy (approval->dialog);
150 g_slice_free (EventManagerApproval, approval);
153 static void event_remove (EventPriv *event);
156 event_free (EventPriv *event)
158 g_free (event->public.icon_name);
159 g_free (event->public.header);
160 g_free (event->public.message);
162 if (event->public.contact)
164 g_object_unref (event->public.contact);
167 g_slice_free (EventPriv, event);
171 event_remove (EventPriv *event)
173 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
175 DEBUG ("Removing event %p", event);
176 priv->events = g_slist_remove (priv->events, event);
177 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
182 autoremove_event_timeout_cb (EventPriv *event)
184 event_remove (event);
189 event_manager_add (EmpathyEventManager *manager,
190 EmpathyContact *contact,
191 EmpathyEventType type,
192 const gchar *icon_name,
194 const gchar *message,
195 EventManagerApproval *approval,
199 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
202 event = g_slice_new0 (EventPriv);
203 event->public.contact = contact ? g_object_ref (contact) : NULL;
204 event->public.type = type;
205 event->public.icon_name = g_strdup (icon_name);
206 event->public.header = g_strdup (header);
207 event->public.message = g_strdup (message);
208 event->public.must_ack = (func != NULL);
209 event->inhibit = FALSE;
211 event->user_data = user_data;
212 event->manager = manager;
213 event->approval = approval;
215 DEBUG ("Adding event %p", event);
216 priv->events = g_slist_prepend (priv->events, event);
217 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
219 if (!event->public.must_ack)
221 g_timeout_add_seconds (NOTIFICATION_TIMEOUT,
222 (GSourceFunc) autoremove_event_timeout_cb, event);
227 event_channel_process_func (EventPriv *event)
229 empathy_dispatch_operation_approve (event->approval->operation);
233 event_text_channel_process_func (EventPriv *event)
235 EmpathyTpChat *tp_chat;
237 if (event->approval->handler != 0)
239 tp_chat = EMPATHY_TP_CHAT
240 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
242 g_signal_handler_disconnect (tp_chat, event->approval->handler);
243 event->approval->handler = 0;
246 empathy_dispatch_operation_approve (event->approval->operation);
250 event_lookup_by_approval (EmpathyEventManager *manager,
251 EventManagerApproval *approval)
253 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
255 EventPriv *retval = NULL;
257 for (l = priv->events; l; l = l->next)
259 EventPriv *event = l->data;
261 if (event->approval == approval)
272 event_update (EmpathyEventManager *manager, EventPriv *event,
273 const char *icon_name, const char *header, const char *msg)
275 g_free (event->public.icon_name);
276 g_free (event->public.header);
277 g_free (event->public.message);
279 event->public.icon_name = g_strdup (icon_name);
280 event->public.header = g_strdup (header);
281 event->public.message = g_strdup (msg);
283 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
287 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
288 gint response, gpointer user_data)
290 EventManagerApproval *approval = user_data;
292 gtk_widget_destroy (approval->dialog);
293 approval->dialog = NULL;
295 if (response != GTK_RESPONSE_ACCEPT)
297 EmpathyTpCall *call =
299 empathy_dispatch_operation_get_channel_wrapper (
300 approval->operation));
303 if (empathy_dispatch_operation_claim (approval->operation))
304 empathy_tp_call_close (call);
305 g_object_unref (call);
310 EmpathyCallFactory *factory = empathy_call_factory_get ();
311 empathy_call_factory_claim_channel (factory, approval->operation);
316 event_channel_process_voip_func (EventPriv *event)
322 if (event->approval->dialog != NULL)
324 gtk_window_present (GTK_WINDOW (event->approval->dialog));
328 dialog = gtk_message_dialog_new (NULL, 0,
329 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
330 gtk_message_dialog_format_secondary_text (
331 GTK_MESSAGE_DIALOG (dialog),
332 _("%s is calling you, do you want to answer?"),
333 empathy_contact_get_name (event->approval->contact));
335 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
338 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
339 _("_Reject"), GTK_RESPONSE_REJECT);
340 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
341 GTK_ICON_SIZE_BUTTON);
342 gtk_button_set_image (GTK_BUTTON (button), image);
344 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
345 _("_Answer"), GTK_RESPONSE_ACCEPT);
347 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
348 gtk_button_set_image (GTK_BUTTON (button), image);
350 g_signal_connect (dialog, "response",
351 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
354 gtk_widget_show (dialog);
356 event->approval->dialog = dialog;
360 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
361 EmpathyMessage *message, EventManagerApproval *approval)
363 EmpathyContact *sender;
369 /* try to update the event if it's referring to a chat which is already in the
371 event = event_lookup_by_approval (approval->manager, approval);
373 sender = empathy_message_get_sender (message);
374 header = empathy_contact_get_name (sender);
375 msg = empathy_message_get_body (message);
377 channel = empathy_tp_chat_get_channel (tp_chat);
380 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
382 event_manager_add (approval->manager, sender, EMPATHY_EVENT_TYPE_CHAT,
383 EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval,
384 event_text_channel_process_func, NULL);
386 empathy_sound_play (empathy_main_window_get (),
387 EMPATHY_SOUND_CONVERSATION_NEW);
391 event_manager_approval_done (EventManagerApproval *approval)
393 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
396 if (approval->operation != NULL)
400 channel_type = empathy_dispatch_operation_get_channel_type_id (
401 approval->operation);
402 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
405 if (priv->ringing == 0)
406 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
410 priv->approvals = g_slist_remove (priv->approvals, approval);
412 for (l = priv->events; l; l = l->next)
414 EventPriv *event = l->data;
416 if (event->approval == approval)
418 event_remove (event);
423 event_manager_approval_free (approval);
427 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
428 EventManagerApproval *approval)
430 event_manager_approval_done (approval);
434 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
435 EventManagerApproval *approval)
437 event_manager_approval_done (approval);
441 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
442 guint domain, gint code, gchar *message,
443 EventManagerApproval *approval)
445 event_manager_approval_done (approval);
449 event_manager_media_channel_got_contact (EventManagerApproval *approval)
451 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
454 header = g_strdup_printf (_("Incoming call from %s"),
455 empathy_contact_get_name (approval->contact));
457 event_manager_add (approval->manager, approval->contact,
458 EMPATHY_EVENT_TYPE_VOIP, EMPATHY_IMAGE_VOIP, header, NULL, approval,
459 event_channel_process_voip_func, NULL);
464 if (priv->ringing == 1)
465 empathy_sound_start_playing (empathy_main_window_get (),
466 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
470 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
471 GParamSpec *param, EventManagerApproval *approval)
473 EmpathyContact *contact;
475 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
480 approval->contact = contact;
481 event_manager_media_channel_got_contact (approval);
485 event_manager_tube_approved_cb (EventPriv *event)
487 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
491 event_manager_add_tube_approval (EventManagerApproval *approval,
492 EmpathyTubeDispatchAbility ability)
494 const gchar *icon_name;
498 header = g_strdup_printf (_("%s is offering you an invitation"),
499 empathy_contact_get_name (approval->contact));
501 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
503 icon_name = GTK_STOCK_EXECUTE;
504 msg = _("An external application will be started to handle it.");
508 icon_name = GTK_STOCK_DIALOG_ERROR;
509 msg = _("You don't have the needed external "
510 "application to handle it.");
513 event_manager_add (approval->manager, approval->contact,
514 EMPATHY_EVENT_TYPE_TUBE, icon_name, header, msg, approval,
515 event_manager_tube_approved_cb, approval);
518 /* FIXME better sound for incoming tubes ? */
519 empathy_sound_play (empathy_main_window_get (),
520 EMPATHY_SOUND_CONVERSATION_NEW);
524 event_manager_tube_dispatch_ability_cb (GObject *object,
525 GParamSpec *spec, gpointer user_data)
527 EventManagerApproval *approval = (EventManagerApproval *) user_data;
528 EmpathyTubeDispatchAbility dispatchability;
531 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
533 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
535 event_manager_add_tube_approval (approval, dispatchability);
536 g_signal_handler_disconnect (object, approval->handler);
537 approval->handler = 0;
542 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
543 EmpathyContact *contact,
548 EventManagerApproval *approval = (EventManagerApproval *) user_data;
549 EmpathyTubeDispatchAbility dispatchability;
553 /* FIXME: We should probably still display the event */
554 DEBUG ("Error: %s", error->message);
558 approval->contact = g_object_ref (contact);
560 dispatchability = empathy_tube_dispatch_is_dispatchable
561 (approval->tube_dispatch);
563 switch (dispatchability)
565 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
566 approval->handler = g_signal_connect (approval->tube_dispatch,
567 "notify::dispatchability",
568 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
569 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
571 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
573 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
574 event_manager_add_tube_approval (approval, dispatchability);
580 invite_dialog_response_cb (GtkDialog *dialog,
582 EventManagerApproval *approval)
584 EmpathyTpChat *tp_chat;
586 TpHandle self_handle;
589 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
590 approval->dialog = NULL;
592 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
593 approval->operation));
595 if (response != GTK_RESPONSE_OK)
598 DEBUG ("Muc invitation rejected");
600 if (empathy_dispatch_operation_claim (approval->operation))
601 empathy_tp_chat_close (tp_chat);
605 DEBUG ("Muc invitation accepted");
608 channel = empathy_tp_chat_get_channel (tp_chat);
610 self_handle = tp_channel_group_get_self_handle (channel);
611 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
612 g_array_append_val (members, self_handle);
614 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
615 "", NULL, NULL, NULL, NULL);
617 empathy_dispatch_operation_approve (approval->operation);
619 g_array_free (members, TRUE);
623 event_room_channel_process_func (EventPriv *event)
625 GtkWidget *dialog, *button, *image;
626 TpChannel *channel = empathy_dispatch_operation_get_channel (
627 event->approval->operation);
629 if (event->approval->dialog != NULL)
631 gtk_window_present (GTK_WINDOW (event->approval->dialog));
636 dialog = gtk_message_dialog_new (NULL, 0,
637 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
639 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
640 _("%s is inviting you to join %s"),
641 empathy_contact_get_name (event->approval->contact),
642 tp_channel_get_identifier (channel));
644 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
647 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
648 _("_Decline"), GTK_RESPONSE_CANCEL);
649 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
650 gtk_button_set_image (GTK_BUTTON (button), image);
652 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
653 _("_Join"), GTK_RESPONSE_OK);
654 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
655 gtk_button_set_image (GTK_BUTTON (button), image);
657 g_signal_connect (dialog, "response",
658 G_CALLBACK (invite_dialog_response_cb), event->approval);
660 gtk_widget_show (dialog);
662 event->approval->dialog = dialog;
666 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
667 EmpathyContact *contact,
672 EventManagerApproval *approval = (EventManagerApproval *) user_data;
674 const gchar *invite_msg;
676 TpHandle self_handle;
680 /* FIXME: We should probably still display the event */
681 DEBUG ("Error: %s", error->message);
685 approval->contact = g_object_ref (contact);
686 channel = empathy_dispatch_operation_get_channel (approval->operation);
688 self_handle = tp_channel_group_get_self_handle (channel);
689 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
692 msg = g_strdup_printf (_("%s invited you to join %s"),
693 empathy_contact_get_name (approval->contact),
694 tp_channel_get_identifier (channel));
696 event_manager_add (approval->manager, approval->contact,
697 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
698 approval, event_room_channel_process_func, NULL);
700 empathy_sound_play (empathy_main_window_get (),
701 EMPATHY_SOUND_CONVERSATION_NEW);
707 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
708 EmpathyContact *contact,
713 EventManagerApproval *approval = (EventManagerApproval *) user_data;
716 approval->contact = contact;
718 header = g_strdup_printf (_("Incoming file transfer from %s"),
719 empathy_contact_get_name (approval->contact));
721 event_manager_add (approval->manager, approval->contact,
722 EMPATHY_EVENT_TYPE_TRANSFER, EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
723 approval, event_channel_process_func, NULL);
725 /* FIXME better sound for incoming file transfers ?*/
726 empathy_sound_play (empathy_main_window_get (),
727 EMPATHY_SOUND_CONVERSATION_NEW);
733 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
734 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
736 const gchar *channel_type;
737 EventManagerApproval *approval;
738 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
740 channel_type = empathy_dispatch_operation_get_channel_type (operation);
742 approval = event_manager_approval_new (manager, operation);
743 priv->approvals = g_slist_prepend (priv->approvals, approval);
745 approval->approved_handler = g_signal_connect (operation, "approved",
746 G_CALLBACK (event_manager_operation_approved_cb), approval);
748 approval->claimed_handler = g_signal_connect (operation, "claimed",
749 G_CALLBACK (event_manager_operation_claimed_cb), approval);
751 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
752 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
754 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
756 EmpathyTpChat *tp_chat =
758 empathy_dispatch_operation_get_channel_wrapper (operation));
759 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
761 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
763 /* Are we in local-pending ? */
764 TpHandle self_handle, inviter;
766 self_handle = tp_channel_group_get_self_handle (channel);
768 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
769 channel, self_handle, &inviter, NULL, NULL))
771 /* We are invited to a room */
772 EmpathyTpContactFactory *factory;
773 TpConnection *connection;
775 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
776 tp_channel_get_identifier (channel));
778 connection = empathy_tp_chat_get_connection (tp_chat);
779 factory = empathy_tp_contact_factory_dup_singleton (connection);
781 empathy_tp_contact_factory_get_from_handle (factory,
782 inviter, event_manager_muc_invite_got_contact_cb,
783 approval, NULL, G_OBJECT (manager));
785 g_object_unref (factory);
789 /* if we are not invited, let's wait for the first message */
792 /* 1-1 text channel, wait for the first message */
793 approval->handler = g_signal_connect (tp_chat, "message-received",
794 G_CALLBACK (event_manager_chat_message_received_cb), approval);
795 approval->handler_instance = G_OBJECT (tp_chat);
797 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
799 EmpathyContact *contact;
800 EmpathyTpCall *call = EMPATHY_TP_CALL (
801 empathy_dispatch_operation_get_channel_wrapper (operation));
803 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
807 g_signal_connect (call, "notify::contact",
808 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
813 approval->contact = contact;
814 event_manager_media_channel_got_contact (approval);
818 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
821 TpConnection *connection;
823 EmpathyTpContactFactory *factory;
825 channel = empathy_dispatch_operation_get_channel (operation);
826 handle = tp_channel_get_handle (channel, NULL);
828 connection = tp_channel_borrow_connection (channel);
829 factory = empathy_tp_contact_factory_dup_singleton (connection);
830 empathy_tp_contact_factory_get_from_handle (factory, handle,
831 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
833 g_object_unref (factory);
835 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
836 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
840 TpHandleType handle_type;
841 TpConnection *connection;
842 EmpathyTpContactFactory *factory;
844 channel = empathy_dispatch_operation_get_channel (operation);
845 handle = tp_channel_get_handle (channel, &handle_type);
847 /* Only understand p2p tubes */
848 if (handle_type != TP_HANDLE_TYPE_CONTACT)
851 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
852 connection = tp_channel_borrow_connection (channel);
853 factory = empathy_tp_contact_factory_dup_singleton (connection);
854 empathy_tp_contact_factory_get_from_handle (factory, handle,
855 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
859 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
864 event_pending_subscribe_func (EventPriv *event)
866 empathy_subscription_dialog_show (event->public.contact, NULL);
867 event_remove (event);
871 event_manager_pendings_changed_cb (EmpathyContactList *list,
872 EmpathyContact *contact, EmpathyContact *actor,
873 guint reason, gchar *message, gboolean is_pending,
874 EmpathyEventManager *manager)
876 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
877 gchar *header, *event_msg;
883 for (l = priv->events; l; l = l->next)
885 EventPriv *event = l->data;
887 if (event->public.contact == contact &&
888 event->func == event_pending_subscribe_func)
890 event_remove (event);
898 header = g_strdup_printf (_("Subscription requested by %s"),
899 empathy_contact_get_name (contact));
901 if (!EMP_STR_EMPTY (message))
902 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
906 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
907 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
908 event_pending_subscribe_func, NULL);
915 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
916 EmpathyContact *contact,
917 TpConnectionPresenceType current,
918 TpConnectionPresenceType previous,
919 EmpathyEventManager *manager)
921 EmpathyAccount *account;
922 gchar *header = NULL;
923 gboolean preference = FALSE;
925 account = empathy_contact_get_account (contact);
926 if (empathy_account_is_just_connected (account))
929 if (tp_connection_presence_type_cmp_availability (previous,
930 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
932 /* contact was online */
933 empathy_conf_get_bool (empathy_conf_get (),
934 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
935 if (preference && tp_connection_presence_type_cmp_availability (current,
936 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
938 /* someone is logging off */
939 header = g_strdup_printf (_("%s is now offline."),
940 empathy_contact_get_name (contact));
942 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
943 GTK_STOCK_DIALOG_INFO, header, NULL, NULL, NULL, NULL);
948 /* contact was offline */
949 empathy_conf_get_bool (empathy_conf_get (),
950 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
951 if (preference && tp_connection_presence_type_cmp_availability (current,
952 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
954 /* someone is logging in */
955 header = g_strdup_printf (_("%s is now online."),
956 empathy_contact_get_name (contact));
958 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
959 GTK_STOCK_DIALOG_INFO, header, NULL, NULL, NULL, NULL);
967 event_manager_constructor (GType type,
969 GObjectConstructParam *props)
973 if (manager_singleton) {
974 retval = g_object_ref (manager_singleton);
976 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
977 (type, n_props, props);
979 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
980 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
987 event_manager_finalize (GObject *object)
989 EmpathyEventManagerPriv *priv = GET_PRIV (object);
991 if (priv->ringing > 0)
992 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
994 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
995 g_slist_free (priv->events);
996 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
997 g_slist_free (priv->approvals);
998 g_object_unref (priv->contact_manager);
999 g_object_unref (priv->dispatcher);
1003 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1005 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1007 object_class->finalize = event_manager_finalize;
1008 object_class->constructor = event_manager_constructor;
1010 signals[EVENT_ADDED] =
1011 g_signal_new ("event-added",
1012 G_TYPE_FROM_CLASS (klass),
1016 g_cclosure_marshal_VOID__POINTER,
1020 signals[EVENT_REMOVED] =
1021 g_signal_new ("event-removed",
1022 G_TYPE_FROM_CLASS (klass),
1026 g_cclosure_marshal_VOID__POINTER,
1027 G_TYPE_NONE, 1, G_TYPE_POINTER);
1029 signals[EVENT_UPDATED] =
1030 g_signal_new ("event-updated",
1031 G_TYPE_FROM_CLASS (klass),
1035 g_cclosure_marshal_VOID__POINTER,
1036 G_TYPE_NONE, 1, G_TYPE_POINTER);
1039 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1043 empathy_event_manager_init (EmpathyEventManager *manager)
1045 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1046 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1047 EmpathyContactMonitor *monitor;
1048 EmpathyContactList *list_iface;
1050 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1051 monitor = empathy_contact_list_get_monitor (list_iface);
1052 g_object_unref (list_iface);
1054 manager->priv = priv;
1056 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1057 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1058 g_signal_connect (priv->dispatcher, "approve",
1059 G_CALLBACK (event_manager_approve_channel_cb), manager);
1060 g_signal_connect (priv->contact_manager, "pendings-changed",
1061 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1062 g_signal_connect (monitor, "contact-presence-changed",
1063 G_CALLBACK (event_manager_presence_changed_cb), manager);
1066 EmpathyEventManager *
1067 empathy_event_manager_dup_singleton (void)
1069 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1073 empathy_event_manager_get_events (EmpathyEventManager *manager)
1075 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1077 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1079 return priv->events;
1083 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1085 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1087 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1089 return priv->events ? priv->events->data : NULL;
1093 empathy_event_activate (EmpathyEvent *event_public)
1095 EventPriv *event = (EventPriv *) event_public;
1097 g_return_if_fail (event_public != NULL);
1100 event->func (event);
1102 event_remove (event);
1106 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1108 EventPriv *event = (EventPriv *) event_public;
1110 g_return_if_fail (event_public != NULL);
1112 event->inhibit = TRUE;