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/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
31 #include <libempathy/empathy-dispatcher.h>
32 #include <libempathy/empathy-idle.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-call.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-call-factory.h>
41 #include <extensions/extensions.h>
43 #include <libempathy-gtk/empathy-conf.h>
44 #include <libempathy-gtk/empathy-images.h>
45 #include <libempathy-gtk/empathy-contact-dialogs.h>
46 #include <libempathy-gtk/empathy-sound.h>
48 #include "empathy-event-manager.h"
49 #include "empathy-main-window.h"
50 #include "empathy-tube-dispatch.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
53 #include <libempathy/empathy-debug.h>
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
57 #define NOTIFICATION_TIMEOUT 2 /* seconds */
59 /* The time interval in milliseconds between 2 incoming rings */
60 #define MS_BETWEEN_RING 500
63 EmpathyEventManager *manager;
64 EmpathyDispatchOperation *operation;
65 gulong approved_handler;
66 gulong claimed_handler;
67 gulong invalidated_handler;
68 /* Remove contact if applicable */
69 EmpathyContact *contact;
70 /* Tube dispatcher if applicable */
71 EmpathyTubeDispatch *tube_dispatch;
72 /* option signal handler and it's instance */
74 GObject *handler_instance;
75 /* optional accept widget */
77 } EventManagerApproval;
80 EmpathyDispatcher *dispatcher;
81 EmpathyContactManager *contact_manager;
83 /* Ongoing approvals */
87 } EmpathyEventManagerPriv;
89 typedef struct _EventPriv EventPriv;
90 typedef void (*EventFunc) (EventPriv *event);
94 EmpathyEventManager *manager;
95 EventManagerApproval *approval;
108 static guint signals[LAST_SIGNAL];
110 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
112 static EmpathyEventManager * manager_singleton = NULL;
114 static EventManagerApproval *
115 event_manager_approval_new (EmpathyEventManager *manager,
116 EmpathyDispatchOperation *operation)
118 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
119 result->operation = g_object_ref (operation);
120 result->manager = manager;
126 event_manager_approval_free (EventManagerApproval *approval)
128 g_signal_handler_disconnect (approval->operation,
129 approval->approved_handler);
130 g_signal_handler_disconnect (approval->operation,
131 approval->claimed_handler);
132 g_signal_handler_disconnect (approval->operation,
133 approval->invalidated_handler);
134 g_object_unref (approval->operation);
136 if (approval->handler != 0)
137 g_signal_handler_disconnect (approval->handler_instance,
140 if (approval->contact != NULL)
141 g_object_unref (approval->contact);
143 if (approval->tube_dispatch != NULL)
144 g_object_unref (approval->tube_dispatch);
146 if (approval->dialog != NULL)
148 gtk_widget_destroy (approval->dialog);
151 g_slice_free (EventManagerApproval, approval);
154 static void event_remove (EventPriv *event);
157 event_free (EventPriv *event)
159 g_free (event->public.icon_name);
160 g_free (event->public.header);
161 g_free (event->public.message);
163 if (event->public.contact)
165 g_object_unref (event->public.contact);
168 g_slice_free (EventPriv, event);
172 event_remove (EventPriv *event)
174 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
176 DEBUG ("Removing event %p", event);
177 priv->events = g_slist_remove (priv->events, event);
178 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
183 autoremove_event_timeout_cb (EventPriv *event)
185 event_remove (event);
190 event_manager_add (EmpathyEventManager *manager,
191 EmpathyContact *contact,
192 EmpathyEventType type,
193 const gchar *icon_name,
195 const gchar *message,
196 EventManagerApproval *approval,
200 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
203 event = g_slice_new0 (EventPriv);
204 event->public.contact = contact ? g_object_ref (contact) : NULL;
205 event->public.type = type;
206 event->public.icon_name = g_strdup (icon_name);
207 event->public.header = g_strdup (header);
208 event->public.message = g_strdup (message);
209 event->public.must_ack = (func != NULL);
210 event->inhibit = FALSE;
212 event->user_data = user_data;
213 event->manager = manager;
214 event->approval = approval;
216 DEBUG ("Adding event %p", event);
217 priv->events = g_slist_prepend (priv->events, event);
218 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
220 if (!event->public.must_ack)
222 g_timeout_add_seconds (NOTIFICATION_TIMEOUT,
223 (GSourceFunc) autoremove_event_timeout_cb, event);
228 event_channel_process_func (EventPriv *event)
230 empathy_dispatch_operation_approve (event->approval->operation);
234 event_text_channel_process_func (EventPriv *event)
236 EmpathyTpChat *tp_chat;
238 if (event->approval->handler != 0)
240 tp_chat = EMPATHY_TP_CHAT
241 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
243 g_signal_handler_disconnect (tp_chat, event->approval->handler);
244 event->approval->handler = 0;
247 empathy_dispatch_operation_approve (event->approval->operation);
251 event_lookup_by_approval (EmpathyEventManager *manager,
252 EventManagerApproval *approval)
254 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
256 EventPriv *retval = NULL;
258 for (l = priv->events; l; l = l->next)
260 EventPriv *event = l->data;
262 if (event->approval == approval)
273 event_update (EmpathyEventManager *manager, EventPriv *event,
274 const char *icon_name, const char *header, const char *msg)
276 g_free (event->public.icon_name);
277 g_free (event->public.header);
278 g_free (event->public.message);
280 event->public.icon_name = g_strdup (icon_name);
281 event->public.header = g_strdup (header);
282 event->public.message = g_strdup (msg);
284 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
288 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
289 gint response, gpointer user_data)
291 EventManagerApproval *approval = user_data;
293 gtk_widget_destroy (approval->dialog);
294 approval->dialog = NULL;
296 if (response != GTK_RESPONSE_ACCEPT)
298 EmpathyTpCall *call =
300 empathy_dispatch_operation_get_channel_wrapper (
301 approval->operation));
304 if (empathy_dispatch_operation_claim (approval->operation))
305 empathy_tp_call_close (call);
306 g_object_unref (call);
311 EmpathyCallFactory *factory = empathy_call_factory_get ();
312 empathy_call_factory_claim_channel (factory, approval->operation);
317 event_channel_process_voip_func (EventPriv *event)
325 if (event->approval->dialog != NULL)
327 gtk_window_present (GTK_WINDOW (event->approval->dialog));
331 call = EMPATHY_TP_CALL (empathy_dispatch_operation_get_channel_wrapper (
332 event->approval->operation));
334 video = empathy_tp_call_has_initial_video (call);
336 dialog = gtk_message_dialog_new (NULL, 0,
337 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
338 video ? _("Incoming video call"): _("Incoming call"));
339 gtk_message_dialog_format_secondary_text (
340 GTK_MESSAGE_DIALOG (dialog), video ?
341 _("%s is video calling you, do you want to answer?"):
342 _("%s is calling you, do you want to answer?"),
343 empathy_contact_get_name (event->approval->contact));
345 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
348 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
349 _("_Reject"), GTK_RESPONSE_REJECT);
350 image = gtk_image_new_from_icon_name ("call-stop",
351 GTK_ICON_SIZE_BUTTON);
352 gtk_button_set_image (GTK_BUTTON (button), image);
354 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
355 _("_Answer"), GTK_RESPONSE_ACCEPT);
357 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
358 gtk_button_set_image (GTK_BUTTON (button), image);
360 g_signal_connect (dialog, "response",
361 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
364 gtk_widget_show (dialog);
366 event->approval->dialog = dialog;
370 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
371 EmpathyMessage *message, EventManagerApproval *approval)
373 EmpathyContact *sender;
379 /* try to update the event if it's referring to a chat which is already in the
381 event = event_lookup_by_approval (approval->manager, approval);
383 sender = empathy_message_get_sender (message);
384 header = empathy_contact_get_name (sender);
385 msg = empathy_message_get_body (message);
387 channel = empathy_tp_chat_get_channel (tp_chat);
390 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
392 event_manager_add (approval->manager, sender, EMPATHY_EVENT_TYPE_CHAT,
393 EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval,
394 event_text_channel_process_func, NULL);
396 empathy_sound_play (empathy_main_window_get (),
397 EMPATHY_SOUND_CONVERSATION_NEW);
401 event_manager_approval_done (EventManagerApproval *approval)
403 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
406 if (approval->operation != NULL)
410 channel_type = empathy_dispatch_operation_get_channel_type_id (
411 approval->operation);
412 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
415 if (priv->ringing == 0)
416 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
420 priv->approvals = g_slist_remove (priv->approvals, approval);
422 for (l = priv->events; l; l = l->next)
424 EventPriv *event = l->data;
426 if (event->approval == approval)
428 event_remove (event);
433 event_manager_approval_free (approval);
437 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
438 EventManagerApproval *approval)
440 event_manager_approval_done (approval);
444 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
445 EventManagerApproval *approval)
447 event_manager_approval_done (approval);
451 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
452 guint domain, gint code, gchar *message,
453 EventManagerApproval *approval)
455 event_manager_approval_done (approval);
459 event_manager_media_channel_got_contact (EventManagerApproval *approval)
461 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
466 call = EMPATHY_TP_CALL (empathy_dispatch_operation_get_channel_wrapper (
467 approval->operation));
469 video = empathy_tp_call_has_initial_video (call);
471 header = g_strdup_printf (
472 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
473 empathy_contact_get_name (approval->contact));
475 event_manager_add (approval->manager, approval->contact,
476 EMPATHY_EVENT_TYPE_VOIP,
477 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
478 header, NULL, approval,
479 event_channel_process_voip_func, NULL);
484 if (priv->ringing == 1)
485 empathy_sound_start_playing (empathy_main_window_get (),
486 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
490 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
491 GParamSpec *param, EventManagerApproval *approval)
493 EmpathyContact *contact;
495 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
500 approval->contact = contact;
501 event_manager_media_channel_got_contact (approval);
505 event_manager_tube_approved_cb (EventPriv *event)
507 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
511 event_manager_add_tube_approval (EventManagerApproval *approval,
512 EmpathyTubeDispatchAbility ability)
514 const gchar *icon_name;
518 header = g_strdup_printf (_("%s is offering you an invitation"),
519 empathy_contact_get_name (approval->contact));
521 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
523 icon_name = GTK_STOCK_EXECUTE;
524 msg = _("An external application will be started to handle it.");
528 icon_name = GTK_STOCK_DIALOG_ERROR;
529 msg = _("You don't have the needed external "
530 "application to handle it.");
533 event_manager_add (approval->manager, approval->contact,
534 EMPATHY_EVENT_TYPE_TUBE, icon_name, header, msg, approval,
535 event_manager_tube_approved_cb, approval);
538 /* FIXME better sound for incoming tubes ? */
539 empathy_sound_play (empathy_main_window_get (),
540 EMPATHY_SOUND_CONVERSATION_NEW);
544 event_manager_tube_dispatch_ability_cb (GObject *object,
545 GParamSpec *spec, gpointer user_data)
547 EventManagerApproval *approval = (EventManagerApproval *) user_data;
548 EmpathyTubeDispatchAbility dispatchability;
551 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
553 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
555 event_manager_add_tube_approval (approval, dispatchability);
556 g_signal_handler_disconnect (object, approval->handler);
557 approval->handler = 0;
562 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
563 EmpathyContact *contact,
568 EventManagerApproval *approval = (EventManagerApproval *) user_data;
569 EmpathyTubeDispatchAbility dispatchability;
573 /* FIXME: We should probably still display the event */
574 DEBUG ("Error: %s", error->message);
578 approval->contact = g_object_ref (contact);
580 dispatchability = empathy_tube_dispatch_is_dispatchable
581 (approval->tube_dispatch);
583 switch (dispatchability)
585 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
586 approval->handler = g_signal_connect (approval->tube_dispatch,
587 "notify::dispatchability",
588 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
589 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
591 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
593 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
594 event_manager_add_tube_approval (approval, dispatchability);
600 invite_dialog_response_cb (GtkDialog *dialog,
602 EventManagerApproval *approval)
604 EmpathyTpChat *tp_chat;
606 TpHandle self_handle;
609 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
610 approval->dialog = NULL;
612 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
613 approval->operation));
615 if (response != GTK_RESPONSE_OK)
618 DEBUG ("Muc invitation rejected");
620 if (empathy_dispatch_operation_claim (approval->operation))
621 empathy_tp_chat_close (tp_chat);
625 DEBUG ("Muc invitation accepted");
628 channel = empathy_tp_chat_get_channel (tp_chat);
630 self_handle = tp_channel_group_get_self_handle (channel);
631 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
632 g_array_append_val (members, self_handle);
634 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
635 "", NULL, NULL, NULL, NULL);
637 empathy_dispatch_operation_approve (approval->operation);
639 g_array_free (members, TRUE);
643 event_room_channel_process_func (EventPriv *event)
645 GtkWidget *dialog, *button, *image;
646 TpChannel *channel = empathy_dispatch_operation_get_channel (
647 event->approval->operation);
649 if (event->approval->dialog != NULL)
651 gtk_window_present (GTK_WINDOW (event->approval->dialog));
656 dialog = gtk_message_dialog_new (NULL, 0,
657 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
659 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
660 _("%s is inviting you to join %s"),
661 empathy_contact_get_name (event->approval->contact),
662 tp_channel_get_identifier (channel));
664 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
667 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
668 _("_Decline"), GTK_RESPONSE_CANCEL);
669 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
670 gtk_button_set_image (GTK_BUTTON (button), image);
672 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
673 _("_Join"), GTK_RESPONSE_OK);
674 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
675 gtk_button_set_image (GTK_BUTTON (button), image);
677 g_signal_connect (dialog, "response",
678 G_CALLBACK (invite_dialog_response_cb), event->approval);
680 gtk_widget_show (dialog);
682 event->approval->dialog = dialog;
686 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
687 EmpathyContact *contact,
692 EventManagerApproval *approval = (EventManagerApproval *) user_data;
694 const gchar *invite_msg;
696 TpHandle self_handle;
700 /* FIXME: We should probably still display the event */
701 DEBUG ("Error: %s", error->message);
705 approval->contact = g_object_ref (contact);
706 channel = empathy_dispatch_operation_get_channel (approval->operation);
708 self_handle = tp_channel_group_get_self_handle (channel);
709 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
712 msg = g_strdup_printf (_("%s invited you to join %s"),
713 empathy_contact_get_name (approval->contact),
714 tp_channel_get_identifier (channel));
716 event_manager_add (approval->manager, approval->contact,
717 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
718 approval, event_room_channel_process_func, NULL);
720 empathy_sound_play (empathy_main_window_get (),
721 EMPATHY_SOUND_CONVERSATION_NEW);
727 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
728 EmpathyContact *contact,
733 EventManagerApproval *approval = (EventManagerApproval *) user_data;
736 approval->contact = g_object_ref (contact);
738 header = g_strdup_printf (_("Incoming file transfer from %s"),
739 empathy_contact_get_name (approval->contact));
741 event_manager_add (approval->manager, approval->contact,
742 EMPATHY_EVENT_TYPE_TRANSFER, EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
743 approval, event_channel_process_func, NULL);
745 /* FIXME better sound for incoming file transfers ?*/
746 empathy_sound_play (empathy_main_window_get (),
747 EMPATHY_SOUND_CONVERSATION_NEW);
753 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
754 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
756 const gchar *channel_type;
757 EventManagerApproval *approval;
758 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
760 channel_type = empathy_dispatch_operation_get_channel_type (operation);
762 approval = event_manager_approval_new (manager, operation);
763 priv->approvals = g_slist_prepend (priv->approvals, approval);
765 approval->approved_handler = g_signal_connect (operation, "approved",
766 G_CALLBACK (event_manager_operation_approved_cb), approval);
768 approval->claimed_handler = g_signal_connect (operation, "claimed",
769 G_CALLBACK (event_manager_operation_claimed_cb), approval);
771 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
772 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
774 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
776 EmpathyTpChat *tp_chat =
778 empathy_dispatch_operation_get_channel_wrapper (operation));
779 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
781 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
783 /* Are we in local-pending ? */
784 TpHandle self_handle, inviter;
786 self_handle = tp_channel_group_get_self_handle (channel);
788 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
789 channel, self_handle, &inviter, NULL, NULL))
791 /* We are invited to a room */
792 EmpathyTpContactFactory *factory;
793 TpConnection *connection;
795 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
796 tp_channel_get_identifier (channel));
798 connection = empathy_tp_chat_get_connection (tp_chat);
799 factory = empathy_tp_contact_factory_dup_singleton (connection);
801 empathy_tp_contact_factory_get_from_handle (factory,
802 inviter, event_manager_muc_invite_got_contact_cb,
803 approval, NULL, G_OBJECT (manager));
805 g_object_unref (factory);
809 /* if we are not invited, let's wait for the first message */
812 /* 1-1 text channel, wait for the first message */
813 approval->handler = g_signal_connect (tp_chat, "message-received",
814 G_CALLBACK (event_manager_chat_message_received_cb), approval);
815 approval->handler_instance = G_OBJECT (tp_chat);
817 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
819 EmpathyContact *contact;
820 EmpathyTpCall *call = EMPATHY_TP_CALL (
821 empathy_dispatch_operation_get_channel_wrapper (operation));
823 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
827 g_signal_connect (call, "notify::contact",
828 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
833 approval->contact = contact;
834 event_manager_media_channel_got_contact (approval);
838 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
841 TpConnection *connection;
843 EmpathyTpContactFactory *factory;
845 channel = empathy_dispatch_operation_get_channel (operation);
846 handle = tp_channel_get_handle (channel, NULL);
848 connection = tp_channel_borrow_connection (channel);
849 factory = empathy_tp_contact_factory_dup_singleton (connection);
850 empathy_tp_contact_factory_get_from_handle (factory, handle,
851 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
853 g_object_unref (factory);
855 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
856 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
860 TpHandleType handle_type;
861 TpConnection *connection;
862 EmpathyTpContactFactory *factory;
864 channel = empathy_dispatch_operation_get_channel (operation);
865 handle = tp_channel_get_handle (channel, &handle_type);
867 /* Only understand p2p tubes */
868 if (handle_type != TP_HANDLE_TYPE_CONTACT)
871 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
872 connection = tp_channel_borrow_connection (channel);
873 factory = empathy_tp_contact_factory_dup_singleton (connection);
874 empathy_tp_contact_factory_get_from_handle (factory, handle,
875 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
879 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
884 event_pending_subscribe_func (EventPriv *event)
886 empathy_subscription_dialog_show (event->public.contact, NULL);
887 event_remove (event);
891 event_manager_pendings_changed_cb (EmpathyContactList *list,
892 EmpathyContact *contact, EmpathyContact *actor,
893 guint reason, gchar *message, gboolean is_pending,
894 EmpathyEventManager *manager)
896 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
897 gchar *header, *event_msg;
903 for (l = priv->events; l; l = l->next)
905 EventPriv *event = l->data;
907 if (event->public.contact == contact &&
908 event->func == event_pending_subscribe_func)
910 event_remove (event);
918 header = g_strdup_printf (_("Subscription requested by %s"),
919 empathy_contact_get_name (contact));
921 if (!EMP_STR_EMPTY (message))
922 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
926 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
927 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
928 event_pending_subscribe_func, NULL);
935 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
936 EmpathyContact *contact,
937 TpConnectionPresenceType current,
938 TpConnectionPresenceType previous,
939 EmpathyEventManager *manager)
942 gchar *header = NULL;
943 gboolean preference = FALSE;
946 account = empathy_contact_get_account (contact);
947 idle = empathy_idle_dup_singleton ();
949 if (empathy_idle_account_is_just_connected (idle, account))
952 if (tp_connection_presence_type_cmp_availability (previous,
953 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
955 /* contact was online */
956 empathy_conf_get_bool (empathy_conf_get (),
957 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
958 if (preference && tp_connection_presence_type_cmp_availability (current,
959 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
961 /* someone is logging off */
962 header = g_strdup_printf (_("%s is now offline."),
963 empathy_contact_get_name (contact));
965 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
966 "stock_person", header, NULL, NULL, NULL, NULL);
971 /* contact was offline */
972 empathy_conf_get_bool (empathy_conf_get (),
973 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
974 if (preference && tp_connection_presence_type_cmp_availability (current,
975 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
977 /* someone is logging in */
978 header = g_strdup_printf (_("%s is now online."),
979 empathy_contact_get_name (contact));
981 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
982 "stock_person", header, NULL, NULL, NULL, NULL);
988 g_object_unref (idle);
993 event_manager_constructor (GType type,
995 GObjectConstructParam *props)
999 if (manager_singleton) {
1000 retval = g_object_ref (manager_singleton);
1002 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1003 (type, n_props, props);
1005 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1006 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1013 event_manager_finalize (GObject *object)
1015 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1017 if (priv->ringing > 0)
1018 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
1020 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1021 g_slist_free (priv->events);
1022 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1023 g_slist_free (priv->approvals);
1024 g_object_unref (priv->contact_manager);
1025 g_object_unref (priv->dispatcher);
1029 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1031 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1033 object_class->finalize = event_manager_finalize;
1034 object_class->constructor = event_manager_constructor;
1036 signals[EVENT_ADDED] =
1037 g_signal_new ("event-added",
1038 G_TYPE_FROM_CLASS (klass),
1042 g_cclosure_marshal_VOID__POINTER,
1046 signals[EVENT_REMOVED] =
1047 g_signal_new ("event-removed",
1048 G_TYPE_FROM_CLASS (klass),
1052 g_cclosure_marshal_VOID__POINTER,
1053 G_TYPE_NONE, 1, G_TYPE_POINTER);
1055 signals[EVENT_UPDATED] =
1056 g_signal_new ("event-updated",
1057 G_TYPE_FROM_CLASS (klass),
1061 g_cclosure_marshal_VOID__POINTER,
1062 G_TYPE_NONE, 1, G_TYPE_POINTER);
1065 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1069 empathy_event_manager_init (EmpathyEventManager *manager)
1071 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1072 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1073 EmpathyContactMonitor *monitor;
1074 EmpathyContactList *list_iface;
1076 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1077 monitor = empathy_contact_list_get_monitor (list_iface);
1078 g_object_unref (list_iface);
1080 manager->priv = priv;
1082 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1083 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1084 g_signal_connect (priv->dispatcher, "approve",
1085 G_CALLBACK (event_manager_approve_channel_cb), manager);
1086 g_signal_connect (priv->contact_manager, "pendings-changed",
1087 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1088 g_signal_connect (monitor, "contact-presence-changed",
1089 G_CALLBACK (event_manager_presence_changed_cb), manager);
1092 EmpathyEventManager *
1093 empathy_event_manager_dup_singleton (void)
1095 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1099 empathy_event_manager_get_events (EmpathyEventManager *manager)
1101 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1103 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1105 return priv->events;
1109 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1111 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1113 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1115 return priv->events ? priv->events->data : NULL;
1119 empathy_event_activate (EmpathyEvent *event_public)
1121 EventPriv *event = (EventPriv *) event_public;
1123 g_return_if_fail (event_public != NULL);
1126 event->func (event);
1128 event_remove (event);
1132 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1134 EventPriv *event = (EventPriv *) event_public;
1136 g_return_if_fail (event_public != NULL);
1138 event->inhibit = TRUE;