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>
24 #include <glib/gi18n.h>
26 #include "libempathy/empathy-presence-manager.h"
27 #include "libempathy/empathy-connection-aggregator.h"
28 #include "libempathy/empathy-tp-chat.h"
29 #include "libempathy/empathy-utils.h"
30 #include "libempathy/empathy-gsettings.h"
31 #include "libempathy/empathy-sasl-mechanisms.h"
33 #include "libempathy-gtk/empathy-images.h"
34 #include "libempathy-gtk/empathy-sound-manager.h"
35 #include "libempathy-gtk/empathy-ui-utils.h"
36 #include "libempathy-gtk/empathy-call-utils.h"
37 #include "libempathy-gtk/empathy-subscription-dialog.h"
39 #include "empathy-event-manager.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
42 #include "libempathy/empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
46 #define NOTIFICATION_TIMEOUT 2 /* seconds */
48 #define ACCEPT_WITHOUT_VIDEO 1
50 /* The time interval in milliseconds between 2 incoming rings */
51 #define MS_BETWEEN_RING 500
54 EmpathyEventManager *manager;
55 TpChannelDispatchOperation *operation;
56 gulong invalidated_handler;
57 /* Remove contact if applicable */
58 EmpathyContact *contact;
59 /* option signal handler and it's instance */
61 GObject *handler_instance;
62 /* optional accept widget */
64 /* Channel of the CDO that will be used during the approval */
65 TpChannel *main_channel;
66 gboolean auto_approved;
67 } EventManagerApproval;
70 TpBaseClient *approver;
71 TpBaseClient *auth_approver;
72 EmpathyConnectionAggregator *conn_aggregator;
74 /* Ongoing approvals */
79 GSettings *gsettings_notif;
80 GSettings *gsettings_ui;
82 EmpathySoundManager *sound_mgr;
84 /* TpContact -> EmpathyContact */
86 } EmpathyEventManagerPriv;
88 typedef struct _EventPriv EventPriv;
89 typedef void (*EventFunc) (EventPriv *event);
93 EmpathyEventManager *manager;
94 EventManagerApproval *approval;
98 guint autoremove_timeout_id;
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 TpChannelDispatchOperation *operation,
117 TpChannel *main_channel)
119 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
120 result->operation = g_object_ref (operation);
121 result->manager = manager;
122 result->main_channel = g_object_ref (main_channel);
128 event_manager_approval_free (EventManagerApproval *approval)
130 g_signal_handler_disconnect (approval->operation,
131 approval->invalidated_handler);
132 g_object_unref (approval->operation);
134 g_object_unref (approval->main_channel);
136 if (approval->handler != 0)
137 g_signal_handler_disconnect (approval->handler_instance,
140 if (approval->handler_instance != NULL)
141 g_object_unref (approval->handler_instance);
143 if (approval->contact != NULL)
144 g_object_unref (approval->contact);
146 if (approval->dialog != NULL)
148 gtk_widget_destroy (approval->dialog);
151 g_slice_free (EventManagerApproval, approval);
155 event_free (EventPriv *event)
157 g_free (event->public.icon_name);
158 g_free (event->public.header);
159 g_free (event->public.message);
161 if (event->autoremove_timeout_id != 0)
162 g_source_remove (event->autoremove_timeout_id);
164 tp_clear_object (&(event->public.contact));
165 tp_clear_object (&(event->public.account));
167 g_slice_free (EventPriv, event);
171 event_remove (EventPriv *event)
173 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
175 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 empathy_event_remove (EmpathyEvent *event_public)
185 EventPriv *event = (EventPriv *) event_public;
187 event_remove (event);
191 autoremove_event_timeout_cb (EventPriv *event)
193 event->autoremove_timeout_id = 0;
194 event_remove (event);
199 display_notify_area (EmpathyEventManager *self)
201 EmpathyEventManagerPriv *priv = GET_PRIV (self);
203 return g_settings_get_boolean (priv->gsettings_ui,
204 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
208 event_manager_add (EmpathyEventManager *manager,
210 EmpathyContact *contact,
211 EmpathyEventType type,
212 const gchar *icon_name,
214 const gchar *message,
215 EventManagerApproval *approval,
219 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
222 event = g_slice_new0 (EventPriv);
223 event->public.account = account != NULL ? g_object_ref (account) : NULL;
224 event->public.contact = contact ? g_object_ref (contact) : NULL;
225 event->public.type = type;
226 event->public.icon_name = g_strdup (icon_name);
227 event->public.header = g_strdup (header);
228 event->public.message = g_strdup (message);
229 event->public.must_ack = (func != NULL);
230 if (approval != NULL)
231 event->public.handler_instance = approval->handler_instance;
232 event->inhibit = FALSE;
234 event->user_data = user_data;
235 event->manager = manager;
236 event->approval = approval;
238 DEBUG ("Adding event %p", event);
239 priv->events = g_slist_prepend (priv->events, event);
241 if (!display_notify_area (manager))
243 /* Don't fire the 'event-added' signal as we activate the event now */
244 if (approval != NULL)
245 approval->auto_approved = TRUE;
247 empathy_event_activate (&event->public);
251 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
253 if (!event->public.must_ack)
255 event->autoremove_timeout_id = g_timeout_add_seconds (
256 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
262 handle_with_cb (GObject *source,
263 GAsyncResult *result,
266 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
267 GError *error = NULL;
269 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
271 DEBUG ("HandleWith failed: %s\n", error->message);
272 g_error_free (error);
277 handle_with_time_cb (GObject *source,
278 GAsyncResult *result,
281 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
282 GError *error = NULL;
284 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
287 if (g_error_matches (error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED))
289 EventManagerApproval *approval = user_data;
291 DEBUG ("HandleWithTime() is not implemented, falling back to "
292 "HandleWith(). Please upgrade to telepathy-mission-control "
295 tp_channel_dispatch_operation_handle_with_async (approval->operation,
296 NULL, handle_with_cb, approval);
300 DEBUG ("HandleWithTime failed: %s\n", error->message);
302 g_error_free (error);
307 event_manager_approval_approve (EventManagerApproval *approval)
311 if (approval->auto_approved)
313 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
317 timestamp = empathy_get_current_action_time ();
320 g_assert (approval->operation != NULL);
322 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
323 NULL, timestamp, handle_with_time_cb, approval);
327 event_channel_process_func (EventPriv *event)
329 event_manager_approval_approve (event->approval);
333 event_text_channel_process_func (EventPriv *event)
335 EmpathyTpChat *tp_chat;
337 if (event->approval->handler != 0)
339 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
341 g_signal_handler_disconnect (tp_chat, event->approval->handler);
342 event->approval->handler = 0;
345 event_manager_approval_approve (event->approval);
349 event_lookup_by_approval (EmpathyEventManager *manager,
350 EventManagerApproval *approval)
352 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
354 EventPriv *retval = NULL;
356 for (l = priv->events; l; l = l->next)
358 EventPriv *event = l->data;
360 if (event->approval == approval)
371 event_update (EmpathyEventManager *manager, EventPriv *event,
372 const char *icon_name, const char *header, const char *msg)
374 g_free (event->public.icon_name);
375 g_free (event->public.header);
376 g_free (event->public.message);
378 event->public.icon_name = g_strdup (icon_name);
379 event->public.header = g_strdup (header);
380 event->public.message = g_strdup (msg);
382 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
386 reject_channel_claim_cb (GObject *source,
387 GAsyncResult *result,
390 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
391 GError *error = NULL;
393 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
395 DEBUG ("Failed to claim channel: %s", error->message);
397 g_error_free (error);
401 if (TP_IS_CALL_CHANNEL (user_data))
403 tp_call_channel_hangup_async (user_data,
404 TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
406 tp_channel_close_async (user_data, NULL, NULL);
408 else if (EMPATHY_IS_TP_CHAT (user_data))
410 empathy_tp_chat_leave (user_data, "");
412 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
414 tp_channel_close_async (user_data, NULL, NULL);
418 g_object_unref (user_data);
422 reject_approval (EventManagerApproval *approval)
424 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
426 /* We have to claim the channel before closing it */
428 /* Unfortunately, we need to special case the auth channels for the
429 * time being as they don't have a wrapper object handler in
430 * approval->handler_instance as they're not actually handled by
431 * this process, so we can just use a noddy callback to call Close()
433 if (approval->handler_instance != NULL)
435 tp_channel_dispatch_operation_claim_with_async (approval->operation,
436 priv->approver, reject_channel_claim_cb,
437 g_object_ref (approval->handler_instance));
439 else if (tp_channel_get_channel_type_id (approval->main_channel)
440 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
442 tp_channel_dispatch_operation_close_channels_async (approval->operation,
448 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
449 gint response, gpointer user_data)
451 EventManagerApproval *approval = user_data;
453 gtk_widget_destroy (approval->dialog);
454 approval->dialog = NULL;
456 if (response == GTK_RESPONSE_ACCEPT)
458 event_manager_approval_approve (approval);
460 else if (response == ACCEPT_WITHOUT_VIDEO)
462 empathy_call_channel_send_video (TP_CALL_CHANNEL (approval->main_channel),
464 event_manager_approval_approve (approval);
468 reject_approval (approval);
473 event_channel_process_voip_func (EventPriv *event)
480 EmpathyEventType etype = event->public.type;
482 if (event->approval->dialog != NULL)
484 gtk_window_present (GTK_WINDOW (event->approval->dialog));
488 if (etype == EMPATHY_EVENT_TYPE_CALL)
491 call = TP_CALL_CHANNEL (event->approval->handler_instance);
492 video = tp_call_channel_has_initial_video (call, NULL);
496 g_warning ("Unknown event type: %d", event->public.type);
500 dialog = gtk_message_dialog_new (NULL, 0,
501 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
502 video ? _("Incoming video call"): _("Incoming call"));
504 gtk_message_dialog_format_secondary_text (
505 GTK_MESSAGE_DIALOG (dialog), video ?
506 _("%s is video calling you. Do you want to answer?"):
507 _("%s is calling you. Do you want to answer?"),
508 empathy_contact_get_alias (event->approval->contact));
510 title = g_strdup_printf (_("Incoming call from %s"),
511 empathy_contact_get_alias (event->approval->contact));
513 gtk_window_set_title (GTK_WINDOW (dialog), title);
516 /* Set image of the dialog */
519 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
520 GTK_ICON_SIZE_DIALOG);
524 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
525 GTK_ICON_SIZE_DIALOG);
528 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
529 gtk_widget_show (image);
531 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
534 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
535 _("_Reject"), GTK_RESPONSE_REJECT);
536 image = gtk_image_new_from_icon_name ("call-stop",
537 GTK_ICON_SIZE_BUTTON);
538 gtk_button_set_image (GTK_BUTTON (button), image);
540 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
542 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
543 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
545 image = gtk_image_new_from_icon_name ("call-start",
546 GTK_ICON_SIZE_BUTTON);
547 gtk_button_set_image (GTK_BUTTON (button), image);
550 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
551 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
553 image = gtk_image_new_from_icon_name ("call-start",
554 GTK_ICON_SIZE_BUTTON);
555 gtk_button_set_image (GTK_BUTTON (button), image);
557 g_signal_connect (dialog, "response",
558 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
561 gtk_widget_show (dialog);
563 event->approval->dialog = dialog;
567 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
568 EmpathyMessage *message,
569 EventManagerApproval *approval)
571 EmpathyContact *sender;
575 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
577 /* try to update the event if it's referring to a chat which is already in the
579 event = event_lookup_by_approval (approval->manager, approval);
581 sender = empathy_message_get_sender (message);
583 /* We only want to show incoming messages */
584 if (empathy_contact_is_user (sender))
587 header = empathy_contact_get_alias (sender);
588 msg = empathy_message_get_body (message);
591 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
594 event_manager_add (approval->manager, NULL, sender,
595 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
596 approval, event_text_channel_process_func, NULL);
598 empathy_sound_manager_play (priv->sound_mgr, NULL,
599 EMPATHY_SOUND_CONVERSATION_NEW);
603 event_manager_approval_done (EventManagerApproval *approval)
605 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
608 if (approval->operation != NULL)
612 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
614 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
615 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
618 if (priv->ringing == 0)
619 empathy_sound_manager_stop (priv->sound_mgr,
620 EMPATHY_SOUND_PHONE_INCOMING);
624 priv->approvals = g_slist_remove (priv->approvals, approval);
626 for (l = priv->events; l; l = l->next)
628 EventPriv *event = l->data;
630 if (event->approval == approval)
632 event_remove (event);
637 event_manager_approval_free (approval);
641 cdo_invalidated_cb (TpProxy *cdo,
645 EventManagerApproval *approval)
647 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
649 event_manager_approval_done (approval);
653 event_manager_call_state_changed_cb (TpCallChannel *call,
656 TpCallStateReason *reason,
658 EventManagerApproval *approval)
660 if (state == TP_CALL_STATE_ENDED)
662 DEBUG ("Call ended, seems we missed it :/");
663 reject_approval (approval);
668 invite_dialog_response_cb (GtkDialog *dialog,
670 EventManagerApproval *approval)
672 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
673 approval->dialog = NULL;
675 if (response != GTK_RESPONSE_OK)
678 DEBUG ("Muc invitation rejected");
680 reject_approval (approval);
685 DEBUG ("Muc invitation accepted");
687 /* We'll join the room when handling the channel */
688 event_manager_approval_approve (approval);
692 event_room_channel_process_func (EventPriv *event)
694 GtkWidget *dialog, *button, *image;
695 TpChannel *channel = event->approval->main_channel;
698 if (event->approval->dialog != NULL)
700 gtk_window_present (GTK_WINDOW (event->approval->dialog));
705 dialog = gtk_message_dialog_new (NULL, 0,
706 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
708 title = g_strdup_printf (_("Invitation to join %s"),
709 tp_channel_get_identifier (channel));
711 gtk_window_set_title (GTK_WINDOW (dialog), title);
714 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
715 _("%s is inviting you to join %s"),
716 empathy_contact_get_alias (event->approval->contact),
717 tp_channel_get_identifier (channel));
719 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
722 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
723 _("_Decline"), GTK_RESPONSE_CANCEL);
724 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
725 gtk_button_set_image (GTK_BUTTON (button), image);
727 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
728 _("_Join"), GTK_RESPONSE_OK);
729 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
730 gtk_button_set_image (GTK_BUTTON (button), image);
732 g_signal_connect (dialog, "response",
733 G_CALLBACK (invite_dialog_response_cb), event->approval);
735 gtk_widget_show (dialog);
737 event->approval->dialog = dialog;
741 display_invite_room_dialog (EventManagerApproval *approval)
743 const gchar *invite_msg;
745 TpContact *self_contact;
746 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
748 self_contact = tp_channel_group_get_self_contact (approval->main_channel);
749 tp_channel_group_get_local_pending_contact_info (approval->main_channel,
750 self_contact, NULL, NULL, &invite_msg);
752 if (approval->contact != NULL)
754 msg = g_strdup_printf (_("%s invited you to join %s"),
755 empathy_contact_get_alias (approval->contact),
756 tp_channel_get_identifier (approval->main_channel));
760 msg = g_strdup_printf (_("You have been invited to join %s"),
761 tp_channel_get_identifier (approval->main_channel));
764 event_manager_add (approval->manager, NULL,
765 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
766 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
767 event_room_channel_process_func, NULL);
769 empathy_sound_manager_play (priv->sound_mgr, NULL,
770 EMPATHY_SOUND_CONVERSATION_NEW);
776 event_manager_auth_process_func (EventPriv *event)
778 empathy_event_approve ((EmpathyEvent *) event);
781 /* If there is a file-transfer, media, or auth channel consider it as
784 find_main_channel (GList *channels)
787 TpChannel *text = NULL;
789 for (l = channels; l != NULL; l = g_list_next (l))
791 TpChannel *channel = l->data;
794 if (tp_proxy_get_invalidated (channel) != NULL)
797 channel_type = tp_channel_get_channel_type_id (channel);
799 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
800 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
801 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
802 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
805 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
813 approve_text_channel (EmpathyEventManager *self,
814 EventManagerApproval *approval,
815 TpAddDispatchOperationContext *context,
816 EmpathyTpChat *tp_chat)
820 approval->handler_instance = g_object_ref (tp_chat);
822 if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
824 /* Are we in local-pending ? */
827 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
829 /* We are invited to a room */
830 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
831 tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
835 approval->contact = empathy_contact_dup_from_tp_contact (
839 display_invite_room_dialog (approval);
840 tp_add_dispatch_operation_context_accept (context);
845 /* We are not invited, approve the channel right now */
846 tp_add_dispatch_operation_context_accept (context);
848 approval->auto_approved = TRUE;
849 event_manager_approval_approve (approval);
853 /* 1-1 text channel, wait for the first message */
854 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
855 G_CALLBACK (event_manager_chat_message_received_cb), approval);
857 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
858 for (l = messages; l != NULL; l = g_list_next (l))
860 EmpathyMessage *msg = l->data;
862 event_manager_chat_message_received_cb (tp_chat, msg, approval);
865 tp_add_dispatch_operation_context_accept (context);
869 approval_set_target_contact (EventManagerApproval *approval,
874 contact = tp_channel_get_target_contact (channel);
875 approval->contact = empathy_contact_dup_from_tp_contact (contact);
879 approve_call_channel (EmpathyEventManager *self,
880 EventManagerApproval *approval,
881 TpAddDispatchOperationContext *context,
884 TpChannel *channel = TP_CHANNEL (call);
885 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
889 approval->handler_instance = g_object_ref (call);
890 approval_set_target_contact (approval, channel);
892 tp_add_dispatch_operation_context_accept (context);
894 if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
896 DEBUG ("Call already ended, seems we missed it :/");
897 reject_approval (approval);
901 approval->handler = g_signal_connect (call, "state-changed",
902 G_CALLBACK (event_manager_call_state_changed_cb), approval);
904 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
906 header = g_strdup_printf (
907 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
908 empathy_contact_get_alias (approval->contact));
910 event_manager_add (approval->manager, NULL,
911 approval->contact, EMPATHY_EVENT_TYPE_CALL,
912 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
913 header, NULL, approval,
914 event_channel_process_voip_func, NULL);
919 if (priv->ringing == 1)
920 empathy_sound_manager_start_playing (priv->sound_mgr, NULL,
921 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
925 approve_ft_channel (EmpathyEventManager *self,
926 EventManagerApproval *approval,
927 TpAddDispatchOperationContext *context,
928 TpFileTransferChannel *ft)
930 TpChannel *channel = TP_CHANNEL (ft);
931 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
934 approval->handler_instance = g_object_ref (ft);
935 approval_set_target_contact (approval, channel);
937 tp_add_dispatch_operation_context_accept (context);
939 header = g_strdup_printf (_("Incoming file transfer from %s"),
940 empathy_contact_get_alias (approval->contact));
942 event_manager_add (approval->manager, NULL,
943 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
944 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
945 approval, event_channel_process_func, NULL);
947 /* FIXME better sound for incoming file transfers ?*/
948 empathy_sound_manager_play (priv->sound_mgr, NULL,
949 EMPATHY_SOUND_CONVERSATION_NEW);
955 approve_sasl_channel (EmpathyEventManager *self,
957 EventManagerApproval *approval,
958 TpAddDispatchOperationContext *context,
961 if (empathy_sasl_channel_supports_mechanism (channel, "X-TELEPATHY-PASSWORD"))
963 event_manager_add (approval->manager, account, NULL,
964 EMPATHY_EVENT_TYPE_AUTH,
965 GTK_STOCK_DIALOG_AUTHENTICATION,
966 tp_account_get_display_name (account),
967 _("Password required"), approval,
968 event_manager_auth_process_func, NULL);
972 GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
973 "Support only X-TELEPATHY-PASSWORD auth method" };
975 tp_add_dispatch_operation_context_fail (context, &error);
979 tp_add_dispatch_operation_context_accept (context);
983 approve_channels (TpSimpleApprover *approver,
985 TpConnection *connection,
987 TpChannelDispatchOperation *dispatch_operation,
988 TpAddDispatchOperationContext *context,
991 EmpathyEventManager *self = user_data;
992 EmpathyEventManagerPriv *priv = GET_PRIV (self);
994 EventManagerApproval *approval;
997 channel = find_main_channel (channels);
1000 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1001 "Unknown channel type" };
1003 DEBUG ("Failed to find the main channel; ignoring");
1005 tp_add_dispatch_operation_context_fail (context, &error);
1009 approval = event_manager_approval_new (self, dispatch_operation, channel);
1010 priv->approvals = g_slist_prepend (priv->approvals, approval);
1012 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1013 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1015 channel_type = tp_channel_get_channel_type_id (channel);
1017 if (EMPATHY_IS_TP_CHAT (channel))
1019 approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1021 else if (TP_IS_CALL_CHANNEL (channel))
1023 approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1025 else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1027 approve_ft_channel (self, approval, context,
1028 TP_FILE_TRANSFER_CHANNEL (channel));
1030 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1032 approve_sasl_channel (self, account, approval, context, channel);
1036 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1037 "Invalid channel type" };
1039 DEBUG ("Unknown channel type (%s), ignoring..",
1040 g_quark_to_string (channel_type));
1042 tp_add_dispatch_operation_context_fail (context, &error);
1048 event_pending_subscribe_func (EventPriv *event)
1051 FolksIndividual *individual;
1053 individual = empathy_ensure_individual_from_tp_contact (
1054 empathy_contact_get_tp_contact (event->public.contact));
1056 dialog = empathy_subscription_dialog_new (individual, event->public.message);
1057 gtk_window_present (GTK_WINDOW (dialog));
1059 event_remove (event);
1061 g_object_unref (individual);
1065 check_publish_state (EmpathyEventManager *self,
1066 TpContact *tp_contact)
1068 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1069 gchar *header, *event_msg;
1070 TpSubscriptionState state;
1071 EmpathyContact *contact;
1072 const gchar *message;
1074 state = tp_contact_get_publish_state (tp_contact);
1076 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1078 if (state != TP_SUBSCRIPTION_STATE_ASK)
1082 for (l = priv->events; l; l = l->next)
1084 EventPriv *event = l->data;
1086 if (event->public.contact == contact &&
1087 event->func == event_pending_subscribe_func)
1089 event_remove (event);
1097 header = g_strdup_printf (
1098 _("%s would like permission to see when you are online"),
1099 empathy_contact_get_alias (contact));
1101 message = tp_contact_get_publish_request (tp_contact);
1103 if (!EMP_STR_EMPTY (message))
1104 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1108 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1109 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1110 event_pending_subscribe_func, NULL);
1116 g_object_unref (contact);
1120 event_manager_publish_state_changed_cb (TpContact *contact,
1122 EmpathyEventManager *self)
1124 check_publish_state (self, contact);
1128 event_manager_presence_changed_cb (EmpathyContact *contact,
1129 TpConnectionPresenceType current,
1130 TpConnectionPresenceType previous,
1131 EmpathyEventManager *manager)
1133 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1135 EmpathyPresenceManager *presence_mgr;
1137 account = empathy_contact_get_account (contact);
1138 presence_mgr = empathy_presence_manager_dup_singleton ();
1140 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1143 if (tp_connection_presence_type_cmp_availability (previous,
1144 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1146 /* contact was online */
1147 if (tp_connection_presence_type_cmp_availability (current,
1148 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1150 /* someone is logging off */
1151 empathy_sound_manager_play (priv->sound_mgr, NULL,
1152 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1154 if (g_settings_get_boolean (priv->gsettings_notif,
1155 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1157 event_manager_add (manager, NULL, contact,
1158 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1159 EMPATHY_IMAGE_AVATAR_DEFAULT,
1160 empathy_contact_get_alias (contact), _("Disconnected"),
1167 /* contact was offline */
1168 if (tp_connection_presence_type_cmp_availability (current,
1169 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1171 /* someone is logging in */
1172 empathy_sound_manager_play (priv->sound_mgr, NULL,
1173 EMPATHY_SOUND_CONTACT_CONNECTED);
1175 if (g_settings_get_boolean (priv->gsettings_notif,
1176 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1178 event_manager_add (manager, NULL, contact,
1179 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1180 EMPATHY_IMAGE_AVATAR_DEFAULT,
1181 empathy_contact_get_alias (contact), _("Connected"),
1188 g_object_unref (presence_mgr);
1192 event_manager_constructor (GType type,
1194 GObjectConstructParam *props)
1198 if (manager_singleton) {
1199 retval = g_object_ref (manager_singleton);
1201 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1202 (type, n_props, props);
1204 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1205 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1212 event_manager_finalize (GObject *object)
1214 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1216 if (priv->ringing > 0)
1217 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1219 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1220 g_slist_free (priv->events);
1221 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1222 g_slist_free (priv->approvals);
1223 g_object_unref (priv->conn_aggregator);
1224 g_object_unref (priv->approver);
1225 g_object_unref (priv->auth_approver);
1226 g_object_unref (priv->gsettings_notif);
1227 g_object_unref (priv->gsettings_ui);
1228 g_object_unref (priv->sound_mgr);
1229 g_hash_table_unref (priv->contacts);
1233 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1235 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1237 object_class->finalize = event_manager_finalize;
1238 object_class->constructor = event_manager_constructor;
1240 signals[EVENT_ADDED] =
1241 g_signal_new ("event-added",
1242 G_TYPE_FROM_CLASS (klass),
1246 g_cclosure_marshal_generic,
1250 signals[EVENT_REMOVED] =
1251 g_signal_new ("event-removed",
1252 G_TYPE_FROM_CLASS (klass),
1256 g_cclosure_marshal_generic,
1257 G_TYPE_NONE, 1, G_TYPE_POINTER);
1259 signals[EVENT_UPDATED] =
1260 g_signal_new ("event-updated",
1261 G_TYPE_FROM_CLASS (klass),
1265 g_cclosure_marshal_generic,
1266 G_TYPE_NONE, 1, G_TYPE_POINTER);
1268 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1272 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1275 EmpathyEventManager *self)
1277 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1280 for (i = 0; i < added->len; i++)
1282 TpContact *tp_contact = g_ptr_array_index (added, i);
1283 EmpathyContact *contact;
1285 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1288 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1290 tp_g_signal_connect_object (contact, "presence-changed",
1291 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1293 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1294 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1296 check_publish_state (self, tp_contact);
1298 /* Pass ownership to the hash table */
1299 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1302 for (i = 0; i < removed->len; i++)
1304 TpContact *tp_contact = g_ptr_array_index (removed, i);
1305 EmpathyContact *contact;
1307 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1308 if (contact == NULL)
1311 g_signal_handlers_disconnect_by_func (contact,
1312 event_manager_presence_changed_cb, self);
1314 g_signal_handlers_disconnect_by_func (tp_contact,
1315 event_manager_publish_state_changed_cb, self);
1317 g_hash_table_remove (priv->contacts, tp_contact);
1322 empathy_event_manager_init (EmpathyEventManager *manager)
1324 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1325 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1326 GError *error = NULL;
1327 TpAccountManager *am;
1328 GPtrArray *contacts, *empty;
1330 manager->priv = priv;
1332 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1333 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1335 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1337 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1340 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1342 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1343 G_CALLBACK (contact_list_changed_cb), manager, 0);
1345 contacts = empathy_connection_aggregator_dup_all_contacts (
1346 priv->conn_aggregator);
1348 empty = g_ptr_array_new ();
1350 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1352 g_ptr_array_unref (contacts);
1353 g_ptr_array_unref (empty);
1355 am = tp_account_manager_dup ();
1357 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1358 FALSE, approve_channels, manager, NULL);
1360 /* Private text channels */
1361 tp_base_client_take_approver_filter (priv->approver,
1363 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1364 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1367 /* Muc text channels */
1368 tp_base_client_take_approver_filter (priv->approver,
1370 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1371 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1375 tp_base_client_take_approver_filter (priv->approver,
1377 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1378 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1379 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1383 tp_base_client_take_approver_filter (priv->approver,
1385 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1386 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1387 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1389 tp_base_client_take_approver_filter (priv->approver,
1391 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1392 TP_IFACE_CHANNEL_TYPE_CALL,
1393 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1396 /* I don't feel good about doing this, and I'm sorry, but the
1397 * capabilities connection feature is added earlier because it's
1398 * needed for EmpathyTpChat. If the capabilities feature is required
1399 * then preparing an auth channel (which of course appears in the
1400 * CONNECTING state) will never be prepared. So the options are
1401 * either to create another approver like I've done, or to port
1402 * EmpathyTpChat and its users to not depend on the connection being
1403 * prepared with capabilities. I chose the former, obviously. :-) */
1405 priv->auth_approver = tp_simple_approver_new_with_am (am,
1406 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1409 /* SASL auth channels */
1410 tp_base_client_take_approver_filter (priv->auth_approver,
1412 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1413 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1414 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1416 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1419 if (!tp_base_client_register (priv->approver, &error))
1421 DEBUG ("Failed to register Approver: %s", error->message);
1422 g_error_free (error);
1425 if (!tp_base_client_register (priv->auth_approver, &error))
1427 DEBUG ("Failed to register auth Approver: %s", error->message);
1428 g_error_free (error);
1431 g_object_unref (am);
1434 EmpathyEventManager *
1435 empathy_event_manager_dup_singleton (void)
1437 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1441 empathy_event_manager_get_events (EmpathyEventManager *manager)
1443 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1445 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1447 return priv->events;
1451 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1453 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1455 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1457 return priv->events ? priv->events->data : NULL;
1461 empathy_event_activate (EmpathyEvent *event_public)
1463 EventPriv *event = (EventPriv *) event_public;
1465 g_return_if_fail (event_public != NULL);
1468 event->func (event);
1470 event_remove (event);
1474 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1476 EventPriv *event = (EventPriv *) event_public;
1478 g_return_if_fail (event_public != NULL);
1480 event->inhibit = TRUE;
1484 empathy_event_approve (EmpathyEvent *event_public)
1486 EventPriv *event = (EventPriv *) event_public;
1488 g_return_if_fail (event_public != NULL);
1490 event_manager_approval_approve (event->approval);
1494 empathy_event_decline (EmpathyEvent *event_public)
1496 EventPriv *event = (EventPriv *) event_public;
1498 g_return_if_fail (event_public != NULL);
1500 reject_approval (event->approval);