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;
99 guint autoremove_timeout_id;
109 static guint signals[LAST_SIGNAL];
111 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
113 static EmpathyEventManager * manager_singleton = NULL;
115 static EventManagerApproval *
116 event_manager_approval_new (EmpathyEventManager *manager,
117 EmpathyDispatchOperation *operation)
119 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
120 result->operation = g_object_ref (operation);
121 result->manager = manager;
127 event_manager_approval_free (EventManagerApproval *approval)
129 g_signal_handler_disconnect (approval->operation,
130 approval->approved_handler);
131 g_signal_handler_disconnect (approval->operation,
132 approval->claimed_handler);
133 g_signal_handler_disconnect (approval->operation,
134 approval->invalidated_handler);
135 g_object_unref (approval->operation);
137 if (approval->handler != 0)
138 g_signal_handler_disconnect (approval->handler_instance,
141 if (approval->contact != NULL)
142 g_object_unref (approval->contact);
144 if (approval->tube_dispatch != NULL)
145 g_object_unref (approval->tube_dispatch);
147 if (approval->dialog != NULL)
149 gtk_widget_destroy (approval->dialog);
152 g_slice_free (EventManagerApproval, approval);
155 static void event_remove (EventPriv *event);
158 event_free (EventPriv *event)
160 g_free (event->public.icon_name);
161 g_free (event->public.header);
162 g_free (event->public.message);
164 if (event->autoremove_timeout_id != 0)
165 g_source_remove (event->autoremove_timeout_id);
167 if (event->public.contact)
169 g_object_unref (event->public.contact);
172 g_slice_free (EventPriv, event);
176 event_remove (EventPriv *event)
178 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
180 DEBUG ("Removing event %p", event);
182 priv->events = g_slist_remove (priv->events, event);
183 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
188 autoremove_event_timeout_cb (EventPriv *event)
190 event->autoremove_timeout_id = 0;
191 event_remove (event);
196 event_manager_add (EmpathyEventManager *manager,
197 EmpathyContact *contact,
198 EmpathyEventType type,
199 const gchar *icon_name,
201 const gchar *message,
202 EventManagerApproval *approval,
206 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
209 event = g_slice_new0 (EventPriv);
210 event->public.contact = contact ? g_object_ref (contact) : NULL;
211 event->public.type = type;
212 event->public.icon_name = g_strdup (icon_name);
213 event->public.header = g_strdup (header);
214 event->public.message = g_strdup (message);
215 event->public.must_ack = (func != NULL);
216 event->inhibit = FALSE;
218 event->user_data = user_data;
219 event->manager = manager;
220 event->approval = approval;
222 DEBUG ("Adding event %p", event);
223 priv->events = g_slist_prepend (priv->events, event);
224 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
226 if (!event->public.must_ack)
228 event->autoremove_timeout_id = g_timeout_add_seconds (
229 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
235 event_channel_process_func (EventPriv *event)
237 empathy_dispatch_operation_approve (event->approval->operation);
241 event_text_channel_process_func (EventPriv *event)
243 EmpathyTpChat *tp_chat;
245 if (event->approval->handler != 0)
247 tp_chat = EMPATHY_TP_CHAT
248 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
250 g_signal_handler_disconnect (tp_chat, event->approval->handler);
251 event->approval->handler = 0;
254 empathy_dispatch_operation_approve (event->approval->operation);
258 event_lookup_by_approval (EmpathyEventManager *manager,
259 EventManagerApproval *approval)
261 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
263 EventPriv *retval = NULL;
265 for (l = priv->events; l; l = l->next)
267 EventPriv *event = l->data;
269 if (event->approval == approval)
280 event_update (EmpathyEventManager *manager, EventPriv *event,
281 const char *icon_name, const char *header, const char *msg)
283 g_free (event->public.icon_name);
284 g_free (event->public.header);
285 g_free (event->public.message);
287 event->public.icon_name = g_strdup (icon_name);
288 event->public.header = g_strdup (header);
289 event->public.message = g_strdup (msg);
291 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
295 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
296 gint response, gpointer user_data)
298 EventManagerApproval *approval = user_data;
300 gtk_widget_destroy (approval->dialog);
301 approval->dialog = NULL;
303 if (response != GTK_RESPONSE_ACCEPT)
305 EmpathyTpCall *call =
307 empathy_dispatch_operation_get_channel_wrapper (
308 approval->operation));
311 if (empathy_dispatch_operation_claim (approval->operation))
312 empathy_tp_call_close (call);
313 g_object_unref (call);
318 EmpathyCallFactory *factory = empathy_call_factory_get ();
319 empathy_call_factory_claim_channel (factory, approval->operation);
324 event_channel_process_voip_func (EventPriv *event)
332 if (event->approval->dialog != NULL)
334 gtk_window_present (GTK_WINDOW (event->approval->dialog));
338 call = EMPATHY_TP_CALL (empathy_dispatch_operation_get_channel_wrapper (
339 event->approval->operation));
341 video = empathy_tp_call_has_initial_video (call);
343 dialog = gtk_message_dialog_new (NULL, 0,
344 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
345 video ? _("Incoming video call"): _("Incoming call"));
347 gtk_message_dialog_format_secondary_text (
348 GTK_MESSAGE_DIALOG (dialog), video ?
349 _("%s is video calling you, do you want to answer?"):
350 _("%s is calling you, do you want to answer?"),
351 empathy_contact_get_name (event->approval->contact));
353 /* Set image of the dialog */
356 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
357 GTK_ICON_SIZE_DIALOG);
361 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
362 GTK_ICON_SIZE_DIALOG);
365 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
366 gtk_widget_show (image);
368 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
371 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
372 _("_Reject"), GTK_RESPONSE_REJECT);
373 image = gtk_image_new_from_icon_name ("call-stop",
374 GTK_ICON_SIZE_BUTTON);
375 gtk_button_set_image (GTK_BUTTON (button), image);
377 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
378 _("_Answer"), GTK_RESPONSE_ACCEPT);
380 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
381 gtk_button_set_image (GTK_BUTTON (button), image);
383 g_signal_connect (dialog, "response",
384 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
387 gtk_widget_show (dialog);
389 event->approval->dialog = dialog;
393 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
394 EmpathyMessage *message, EventManagerApproval *approval)
396 EmpathyContact *sender;
402 /* try to update the event if it's referring to a chat which is already in the
404 event = event_lookup_by_approval (approval->manager, approval);
406 sender = empathy_message_get_sender (message);
407 header = empathy_contact_get_name (sender);
408 msg = empathy_message_get_body (message);
410 channel = empathy_tp_chat_get_channel (tp_chat);
413 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
415 event_manager_add (approval->manager, sender, EMPATHY_EVENT_TYPE_CHAT,
416 EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval,
417 event_text_channel_process_func, NULL);
419 empathy_sound_play (empathy_main_window_get (),
420 EMPATHY_SOUND_CONVERSATION_NEW);
424 event_manager_approval_done (EventManagerApproval *approval)
426 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
429 if (approval->operation != NULL)
433 channel_type = empathy_dispatch_operation_get_channel_type_id (
434 approval->operation);
435 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
438 if (priv->ringing == 0)
439 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
443 priv->approvals = g_slist_remove (priv->approvals, approval);
445 for (l = priv->events; l; l = l->next)
447 EventPriv *event = l->data;
449 if (event->approval == approval)
451 event_remove (event);
456 event_manager_approval_free (approval);
460 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
461 EventManagerApproval *approval)
463 event_manager_approval_done (approval);
467 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
468 EventManagerApproval *approval)
470 event_manager_approval_done (approval);
474 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
475 guint domain, gint code, gchar *message,
476 EventManagerApproval *approval)
478 event_manager_approval_done (approval);
482 event_manager_media_channel_got_contact (EventManagerApproval *approval)
484 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
489 call = EMPATHY_TP_CALL (empathy_dispatch_operation_get_channel_wrapper (
490 approval->operation));
492 video = empathy_tp_call_has_initial_video (call);
494 header = g_strdup_printf (
495 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
496 empathy_contact_get_name (approval->contact));
498 event_manager_add (approval->manager, approval->contact,
499 EMPATHY_EVENT_TYPE_VOIP,
500 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
501 header, NULL, approval,
502 event_channel_process_voip_func, NULL);
507 if (priv->ringing == 1)
508 empathy_sound_start_playing (empathy_main_window_get (),
509 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
513 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
514 GParamSpec *param, EventManagerApproval *approval)
516 EmpathyContact *contact;
518 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
523 approval->contact = contact;
524 event_manager_media_channel_got_contact (approval);
528 event_manager_tube_approved_cb (EventPriv *event)
530 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
534 event_manager_add_tube_approval (EventManagerApproval *approval,
535 EmpathyTubeDispatchAbility ability)
537 const gchar *icon_name;
541 header = g_strdup_printf (_("%s is offering you an invitation"),
542 empathy_contact_get_name (approval->contact));
544 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
546 icon_name = GTK_STOCK_EXECUTE;
547 msg = _("An external application will be started to handle it.");
551 icon_name = GTK_STOCK_DIALOG_ERROR;
552 msg = _("You don't have the needed external "
553 "application to handle it.");
556 event_manager_add (approval->manager, approval->contact,
557 EMPATHY_EVENT_TYPE_TUBE, icon_name, header, msg, approval,
558 event_manager_tube_approved_cb, approval);
561 /* FIXME better sound for incoming tubes ? */
562 empathy_sound_play (empathy_main_window_get (),
563 EMPATHY_SOUND_CONVERSATION_NEW);
567 event_manager_tube_dispatch_ability_cb (GObject *object,
568 GParamSpec *spec, gpointer user_data)
570 EventManagerApproval *approval = (EventManagerApproval *) user_data;
571 EmpathyTubeDispatchAbility dispatchability;
574 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
576 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
578 event_manager_add_tube_approval (approval, dispatchability);
579 g_signal_handler_disconnect (object, approval->handler);
580 approval->handler = 0;
585 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
586 EmpathyContact *contact,
591 EventManagerApproval *approval = (EventManagerApproval *) user_data;
592 EmpathyTubeDispatchAbility dispatchability;
596 /* FIXME: We should probably still display the event */
597 DEBUG ("Error: %s", error->message);
601 approval->contact = g_object_ref (contact);
603 dispatchability = empathy_tube_dispatch_is_dispatchable
604 (approval->tube_dispatch);
606 switch (dispatchability)
608 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
609 approval->handler = g_signal_connect (approval->tube_dispatch,
610 "notify::dispatchability",
611 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
612 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
614 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
616 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
617 event_manager_add_tube_approval (approval, dispatchability);
623 invite_dialog_response_cb (GtkDialog *dialog,
625 EventManagerApproval *approval)
627 EmpathyTpChat *tp_chat;
629 TpHandle self_handle;
632 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
633 approval->dialog = NULL;
635 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
636 approval->operation));
638 if (response != GTK_RESPONSE_OK)
641 DEBUG ("Muc invitation rejected");
643 if (empathy_dispatch_operation_claim (approval->operation))
644 empathy_tp_chat_close (tp_chat);
648 DEBUG ("Muc invitation accepted");
651 channel = empathy_tp_chat_get_channel (tp_chat);
653 self_handle = tp_channel_group_get_self_handle (channel);
654 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
655 g_array_append_val (members, self_handle);
657 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
658 "", NULL, NULL, NULL, NULL);
660 empathy_dispatch_operation_approve (approval->operation);
662 g_array_free (members, TRUE);
666 event_room_channel_process_func (EventPriv *event)
668 GtkWidget *dialog, *button, *image;
669 TpChannel *channel = empathy_dispatch_operation_get_channel (
670 event->approval->operation);
672 if (event->approval->dialog != NULL)
674 gtk_window_present (GTK_WINDOW (event->approval->dialog));
679 dialog = gtk_message_dialog_new (NULL, 0,
680 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
682 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
683 _("%s is inviting you to join %s"),
684 empathy_contact_get_name (event->approval->contact),
685 tp_channel_get_identifier (channel));
687 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
690 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
691 _("_Decline"), GTK_RESPONSE_CANCEL);
692 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
693 gtk_button_set_image (GTK_BUTTON (button), image);
695 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
696 _("_Join"), GTK_RESPONSE_OK);
697 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
698 gtk_button_set_image (GTK_BUTTON (button), image);
700 g_signal_connect (dialog, "response",
701 G_CALLBACK (invite_dialog_response_cb), event->approval);
703 gtk_widget_show (dialog);
705 event->approval->dialog = dialog;
709 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
710 EmpathyContact *contact,
715 EventManagerApproval *approval = (EventManagerApproval *) user_data;
717 const gchar *invite_msg;
719 TpHandle self_handle;
723 /* FIXME: We should probably still display the event */
724 DEBUG ("Error: %s", error->message);
728 approval->contact = g_object_ref (contact);
729 channel = empathy_dispatch_operation_get_channel (approval->operation);
731 self_handle = tp_channel_group_get_self_handle (channel);
732 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
735 msg = g_strdup_printf (_("%s invited you to join %s"),
736 empathy_contact_get_name (approval->contact),
737 tp_channel_get_identifier (channel));
739 event_manager_add (approval->manager, approval->contact,
740 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
741 approval, event_room_channel_process_func, NULL);
743 empathy_sound_play (empathy_main_window_get (),
744 EMPATHY_SOUND_CONVERSATION_NEW);
750 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
751 EmpathyContact *contact,
756 EventManagerApproval *approval = (EventManagerApproval *) user_data;
759 approval->contact = g_object_ref (contact);
761 header = g_strdup_printf (_("Incoming file transfer from %s"),
762 empathy_contact_get_name (approval->contact));
764 event_manager_add (approval->manager, approval->contact,
765 EMPATHY_EVENT_TYPE_TRANSFER, EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
766 approval, event_channel_process_func, NULL);
768 /* FIXME better sound for incoming file transfers ?*/
769 empathy_sound_play (empathy_main_window_get (),
770 EMPATHY_SOUND_CONVERSATION_NEW);
776 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
777 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
779 const gchar *channel_type;
780 EventManagerApproval *approval;
781 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
783 channel_type = empathy_dispatch_operation_get_channel_type (operation);
785 approval = event_manager_approval_new (manager, operation);
786 priv->approvals = g_slist_prepend (priv->approvals, approval);
788 approval->approved_handler = g_signal_connect (operation, "approved",
789 G_CALLBACK (event_manager_operation_approved_cb), approval);
791 approval->claimed_handler = g_signal_connect (operation, "claimed",
792 G_CALLBACK (event_manager_operation_claimed_cb), approval);
794 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
795 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
797 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
799 EmpathyTpChat *tp_chat =
801 empathy_dispatch_operation_get_channel_wrapper (operation));
802 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
804 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
806 /* Are we in local-pending ? */
807 TpHandle self_handle, inviter;
809 self_handle = tp_channel_group_get_self_handle (channel);
811 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
812 channel, self_handle, &inviter, NULL, NULL))
814 /* We are invited to a room */
815 EmpathyTpContactFactory *factory;
816 TpConnection *connection;
818 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
819 tp_channel_get_identifier (channel));
821 connection = empathy_tp_chat_get_connection (tp_chat);
822 factory = empathy_tp_contact_factory_dup_singleton (connection);
824 empathy_tp_contact_factory_get_from_handle (factory,
825 inviter, event_manager_muc_invite_got_contact_cb,
826 approval, NULL, G_OBJECT (manager));
828 g_object_unref (factory);
832 /* if we are not invited, let's wait for the first message */
835 /* 1-1 text channel, wait for the first message */
836 approval->handler = g_signal_connect (tp_chat, "message-received",
837 G_CALLBACK (event_manager_chat_message_received_cb), approval);
838 approval->handler_instance = G_OBJECT (tp_chat);
840 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
842 EmpathyContact *contact;
843 EmpathyTpCall *call = EMPATHY_TP_CALL (
844 empathy_dispatch_operation_get_channel_wrapper (operation));
846 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
850 g_signal_connect (call, "notify::contact",
851 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
856 approval->contact = contact;
857 event_manager_media_channel_got_contact (approval);
861 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
864 TpConnection *connection;
866 EmpathyTpContactFactory *factory;
868 channel = empathy_dispatch_operation_get_channel (operation);
869 handle = tp_channel_get_handle (channel, NULL);
871 connection = tp_channel_borrow_connection (channel);
872 factory = empathy_tp_contact_factory_dup_singleton (connection);
873 empathy_tp_contact_factory_get_from_handle (factory, handle,
874 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
876 g_object_unref (factory);
878 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
879 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
883 TpHandleType handle_type;
884 TpConnection *connection;
885 EmpathyTpContactFactory *factory;
887 channel = empathy_dispatch_operation_get_channel (operation);
888 handle = tp_channel_get_handle (channel, &handle_type);
890 /* Only understand p2p tubes */
891 if (handle_type != TP_HANDLE_TYPE_CONTACT)
894 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
895 connection = tp_channel_borrow_connection (channel);
896 factory = empathy_tp_contact_factory_dup_singleton (connection);
897 empathy_tp_contact_factory_get_from_handle (factory, handle,
898 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
902 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
907 event_pending_subscribe_func (EventPriv *event)
909 empathy_subscription_dialog_show (event->public.contact, NULL);
910 event_remove (event);
914 event_manager_pendings_changed_cb (EmpathyContactList *list,
915 EmpathyContact *contact, EmpathyContact *actor,
916 guint reason, gchar *message, gboolean is_pending,
917 EmpathyEventManager *manager)
919 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
920 gchar *header, *event_msg;
926 for (l = priv->events; l; l = l->next)
928 EventPriv *event = l->data;
930 if (event->public.contact == contact &&
931 event->func == event_pending_subscribe_func)
933 event_remove (event);
941 header = g_strdup_printf (_("Subscription requested by %s"),
942 empathy_contact_get_name (contact));
944 if (!EMP_STR_EMPTY (message))
945 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
949 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
950 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
951 event_pending_subscribe_func, NULL);
958 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
959 EmpathyContact *contact,
960 TpConnectionPresenceType current,
961 TpConnectionPresenceType previous,
962 EmpathyEventManager *manager)
965 gchar *header = NULL;
966 gboolean preference = FALSE;
969 account = empathy_contact_get_account (contact);
970 idle = empathy_idle_dup_singleton ();
972 if (empathy_idle_account_is_just_connected (idle, account))
975 if (tp_connection_presence_type_cmp_availability (previous,
976 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
978 /* contact was online */
979 empathy_conf_get_bool (empathy_conf_get (),
980 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
981 if (preference && tp_connection_presence_type_cmp_availability (current,
982 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
984 /* someone is logging off */
985 header = g_strdup_printf (_("%s is now offline."),
986 empathy_contact_get_name (contact));
988 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
989 "stock_person", header, NULL, NULL, NULL, NULL);
994 /* contact was offline */
995 empathy_conf_get_bool (empathy_conf_get (),
996 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
997 if (preference && tp_connection_presence_type_cmp_availability (current,
998 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1000 /* someone is logging in */
1001 header = g_strdup_printf (_("%s is now online."),
1002 empathy_contact_get_name (contact));
1004 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1005 "stock_person", header, NULL, NULL, NULL, NULL);
1011 g_object_unref (idle);
1016 event_manager_constructor (GType type,
1018 GObjectConstructParam *props)
1022 if (manager_singleton) {
1023 retval = g_object_ref (manager_singleton);
1025 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1026 (type, n_props, props);
1028 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1029 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1036 event_manager_finalize (GObject *object)
1038 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1040 if (priv->ringing > 0)
1041 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
1043 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1044 g_slist_free (priv->events);
1045 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1046 g_slist_free (priv->approvals);
1047 g_object_unref (priv->contact_manager);
1048 g_object_unref (priv->dispatcher);
1052 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1054 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1056 object_class->finalize = event_manager_finalize;
1057 object_class->constructor = event_manager_constructor;
1059 signals[EVENT_ADDED] =
1060 g_signal_new ("event-added",
1061 G_TYPE_FROM_CLASS (klass),
1065 g_cclosure_marshal_VOID__POINTER,
1069 signals[EVENT_REMOVED] =
1070 g_signal_new ("event-removed",
1071 G_TYPE_FROM_CLASS (klass),
1075 g_cclosure_marshal_VOID__POINTER,
1076 G_TYPE_NONE, 1, G_TYPE_POINTER);
1078 signals[EVENT_UPDATED] =
1079 g_signal_new ("event-updated",
1080 G_TYPE_FROM_CLASS (klass),
1084 g_cclosure_marshal_VOID__POINTER,
1085 G_TYPE_NONE, 1, G_TYPE_POINTER);
1088 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1092 empathy_event_manager_init (EmpathyEventManager *manager)
1094 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1095 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1096 EmpathyContactMonitor *monitor;
1097 EmpathyContactList *list_iface;
1099 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1100 monitor = empathy_contact_list_get_monitor (list_iface);
1101 g_object_unref (list_iface);
1103 manager->priv = priv;
1105 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1106 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1107 g_signal_connect (priv->dispatcher, "approve",
1108 G_CALLBACK (event_manager_approve_channel_cb), manager);
1109 g_signal_connect (priv->contact_manager, "pendings-changed",
1110 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1111 g_signal_connect (monitor, "contact-presence-changed",
1112 G_CALLBACK (event_manager_presence_changed_cb), manager);
1115 EmpathyEventManager *
1116 empathy_event_manager_dup_singleton (void)
1118 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1122 empathy_event_manager_get_events (EmpathyEventManager *manager)
1124 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1126 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1128 return priv->events;
1132 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1134 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1136 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1138 return priv->events ? priv->events->data : NULL;
1142 empathy_event_activate (EmpathyEvent *event_public)
1144 EventPriv *event = (EventPriv *) event_public;
1146 g_return_if_fail (event_public != NULL);
1149 event->func (event);
1151 event_remove (event);
1155 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1157 EventPriv *event = (EventPriv *) event_public;
1159 g_return_if_fail (event_public != NULL);
1161 event->inhibit = TRUE;