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>
23 #include "empathy-event-manager.h"
25 #include <glib/gi18n.h>
26 #include <tp-account-widgets/tpaw-images.h>
27 #include <tp-account-widgets/tpaw-utils.h>
28 #include <telepathy-glib/telepathy-glib-dbus.h>
30 #include "empathy-call-utils.h"
31 #include "empathy-connection-aggregator.h"
32 #include "empathy-gsettings.h"
33 #include "empathy-images.h"
34 #include "empathy-presence-manager.h"
35 #include "empathy-sasl-mechanisms.h"
36 #include "empathy-sound-manager.h"
37 #include "empathy-subscription-dialog.h"
38 #include "empathy-tp-chat.h"
39 #include "empathy-ui-utils.h"
40 #include "empathy-utils.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
43 #include "empathy-debug.h"
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
47 #define NOTIFICATION_TIMEOUT 2 /* seconds */
49 #define ACCEPT_WITHOUT_VIDEO 1
51 /* The time interval in milliseconds between 2 incoming rings */
52 #define MS_BETWEEN_RING 500
55 EmpathyEventManager *manager;
56 TpChannelDispatchOperation *operation;
57 gulong invalidated_handler;
58 /* Remove contact if applicable */
59 EmpathyContact *contact;
60 /* option signal handler and it's instance */
62 GObject *handler_instance;
63 /* optional accept widget */
65 /* Channel of the CDO that will be used during the approval */
66 TpChannel *main_channel;
67 gboolean auto_approved;
68 } EventManagerApproval;
71 TpBaseClient *approver;
72 TpBaseClient *auth_approver;
73 EmpathyConnectionAggregator *conn_aggregator;
75 /* Ongoing approvals */
80 GSettings *gsettings_notif;
81 GSettings *gsettings_ui;
83 EmpathySoundManager *sound_mgr;
85 /* TpContact -> EmpathyContact */
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 TpChannelDispatchOperation *operation,
118 TpChannel *main_channel)
120 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
121 result->operation = g_object_ref (operation);
122 result->manager = manager;
123 result->main_channel = g_object_ref (main_channel);
129 event_manager_approval_free (EventManagerApproval *approval)
131 g_signal_handler_disconnect (approval->operation,
132 approval->invalidated_handler);
133 g_object_unref (approval->operation);
135 g_object_unref (approval->main_channel);
137 if (approval->handler != 0)
138 g_signal_handler_disconnect (approval->handler_instance,
141 if (approval->handler_instance != NULL)
142 g_object_unref (approval->handler_instance);
144 if (approval->contact != NULL)
145 g_object_unref (approval->contact);
147 if (approval->dialog != NULL)
149 gtk_widget_destroy (approval->dialog);
152 g_slice_free (EventManagerApproval, approval);
156 event_free (EventPriv *event)
158 g_free (event->public.icon_name);
159 g_free (event->public.header);
160 g_free (event->public.message);
162 if (event->autoremove_timeout_id != 0)
163 g_source_remove (event->autoremove_timeout_id);
165 tp_clear_object (&(event->public.contact));
166 tp_clear_object (&(event->public.account));
168 g_slice_free (EventPriv, event);
172 event_remove (EventPriv *event)
174 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
176 DEBUG ("Removing event %p", event);
178 priv->events = g_slist_remove (priv->events, event);
179 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
184 empathy_event_remove (EmpathyEvent *event_public)
186 EventPriv *event = (EventPriv *) event_public;
188 event_remove (event);
192 autoremove_event_timeout_cb (EventPriv *event)
194 event->autoremove_timeout_id = 0;
195 event_remove (event);
200 display_notify_area (EmpathyEventManager *self)
202 EmpathyEventManagerPriv *priv = GET_PRIV (self);
204 return g_settings_get_boolean (priv->gsettings_ui,
205 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
209 event_manager_add (EmpathyEventManager *manager,
211 EmpathyContact *contact,
212 EmpathyEventType type,
213 const gchar *icon_name,
215 const gchar *message,
216 EventManagerApproval *approval,
220 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
223 event = g_slice_new0 (EventPriv);
224 event->public.account = account != NULL ? g_object_ref (account) : NULL;
225 event->public.contact = contact ? g_object_ref (contact) : NULL;
226 event->public.type = type;
227 event->public.icon_name = g_strdup (icon_name);
228 event->public.header = g_strdup (header);
229 event->public.message = g_strdup (message);
230 event->public.must_ack = (func != NULL);
231 if (approval != NULL)
232 event->public.handler_instance = approval->handler_instance;
233 event->inhibit = FALSE;
235 event->user_data = user_data;
236 event->manager = manager;
237 event->approval = approval;
239 DEBUG ("Adding event %p", event);
240 priv->events = g_slist_prepend (priv->events, event);
242 if (!display_notify_area (manager))
244 /* Don't fire the 'event-added' signal as we activate the event now */
245 if (approval != NULL)
246 approval->auto_approved = TRUE;
248 empathy_event_activate (&event->public);
252 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
254 if (!event->public.must_ack)
256 event->autoremove_timeout_id = g_timeout_add_seconds (
257 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
263 handle_with_cb (GObject *source,
264 GAsyncResult *result,
267 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
268 GError *error = NULL;
270 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
272 DEBUG ("HandleWith failed: %s\n", error->message);
273 g_error_free (error);
278 handle_with_time_cb (GObject *source,
279 GAsyncResult *result,
282 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
283 GError *error = NULL;
285 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
288 if (g_error_matches (error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED))
290 EventManagerApproval *approval = user_data;
292 DEBUG ("HandleWithTime() is not implemented, falling back to "
293 "HandleWith(). Please upgrade to telepathy-mission-control "
296 tp_channel_dispatch_operation_handle_with_async (approval->operation,
297 NULL, handle_with_cb, approval);
301 DEBUG ("HandleWithTime failed: %s\n", error->message);
303 g_error_free (error);
308 event_manager_approval_approve (EventManagerApproval *approval)
312 if (approval->auto_approved)
314 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
318 timestamp = empathy_get_current_action_time ();
321 g_assert (approval->operation != NULL);
323 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
324 NULL, timestamp, handle_with_time_cb, approval);
328 event_channel_process_func (EventPriv *event)
330 event_manager_approval_approve (event->approval);
334 event_text_channel_process_func (EventPriv *event)
336 EmpathyTpChat *tp_chat;
338 if (event->approval->handler != 0)
340 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
342 g_signal_handler_disconnect (tp_chat, event->approval->handler);
343 event->approval->handler = 0;
346 event_manager_approval_approve (event->approval);
350 event_lookup_by_approval (EmpathyEventManager *manager,
351 EventManagerApproval *approval)
353 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
355 EventPriv *retval = NULL;
357 for (l = priv->events; l; l = l->next)
359 EventPriv *event = l->data;
361 if (event->approval == approval)
372 event_update (EmpathyEventManager *manager, EventPriv *event,
373 const char *icon_name, const char *header, const char *msg)
375 g_free (event->public.icon_name);
376 g_free (event->public.header);
377 g_free (event->public.message);
379 event->public.icon_name = g_strdup (icon_name);
380 event->public.header = g_strdup (header);
381 event->public.message = g_strdup (msg);
383 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
387 reject_channel_claim_cb (GObject *source,
388 GAsyncResult *result,
391 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
392 GError *error = NULL;
394 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
396 DEBUG ("Failed to claim channel: %s", error->message);
398 g_error_free (error);
402 if (TP_IS_CALL_CHANNEL (user_data))
404 tp_call_channel_hangup_async (user_data,
405 TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
407 tp_channel_close_async (user_data, NULL, NULL);
409 else if (EMPATHY_IS_TP_CHAT (user_data))
411 empathy_tp_chat_leave (user_data, "");
413 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
415 tp_channel_close_async (user_data, NULL, NULL);
419 g_object_unref (user_data);
423 reject_approval (EventManagerApproval *approval)
425 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
427 /* We have to claim the channel before closing it */
429 /* Unfortunately, we need to special case the auth channels for the
430 * time being as they don't have a wrapper object handler in
431 * approval->handler_instance as they're not actually handled by
432 * this process, so we can just use a noddy callback to call Close()
434 if (approval->handler_instance != NULL)
436 tp_channel_dispatch_operation_claim_with_async (approval->operation,
437 priv->approver, reject_channel_claim_cb,
438 g_object_ref (approval->handler_instance));
440 else if (tp_channel_get_channel_type_id (approval->main_channel)
441 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
443 tp_channel_dispatch_operation_close_channels_async (approval->operation,
449 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
450 gint response, gpointer user_data)
452 EventManagerApproval *approval = user_data;
454 gtk_widget_destroy (approval->dialog);
455 approval->dialog = NULL;
457 if (response == GTK_RESPONSE_ACCEPT)
459 event_manager_approval_approve (approval);
461 else if (response == ACCEPT_WITHOUT_VIDEO)
463 empathy_call_channel_send_video (TP_CALL_CHANNEL (approval->main_channel),
465 event_manager_approval_approve (approval);
469 reject_approval (approval);
474 event_channel_process_voip_func (EventPriv *event)
481 EmpathyEventType etype = event->public.type;
483 if (event->approval->dialog != NULL)
485 gtk_window_present (GTK_WINDOW (event->approval->dialog));
489 if (etype == EMPATHY_EVENT_TYPE_CALL)
492 call = TP_CALL_CHANNEL (event->approval->handler_instance);
493 video = tp_call_channel_has_initial_video (call, NULL);
497 g_warning ("Unknown event type: %d", event->public.type);
501 dialog = gtk_message_dialog_new (NULL, 0,
502 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
503 video ? _("Incoming video call"): _("Incoming call"));
505 gtk_message_dialog_format_secondary_text (
506 GTK_MESSAGE_DIALOG (dialog), video ?
507 _("%s is video calling you. Do you want to answer?"):
508 _("%s is calling you. Do you want to answer?"),
509 empathy_contact_get_alias (event->approval->contact));
511 title = g_strdup_printf (_("Incoming call from %s"),
512 empathy_contact_get_alias (event->approval->contact));
514 gtk_window_set_title (GTK_WINDOW (dialog), title);
517 /* Set image of the dialog */
520 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
521 GTK_ICON_SIZE_DIALOG);
525 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
526 GTK_ICON_SIZE_DIALOG);
529 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
530 gtk_widget_show (image);
532 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
535 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
536 _("_Reject"), GTK_RESPONSE_REJECT);
537 image = gtk_image_new_from_icon_name ("call-stop",
538 GTK_ICON_SIZE_BUTTON);
539 gtk_button_set_image (GTK_BUTTON (button), image);
541 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
543 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
544 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
546 image = gtk_image_new_from_icon_name ("call-start",
547 GTK_ICON_SIZE_BUTTON);
548 gtk_button_set_image (GTK_BUTTON (button), image);
551 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
552 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
554 image = gtk_image_new_from_icon_name ("call-start",
555 GTK_ICON_SIZE_BUTTON);
556 gtk_button_set_image (GTK_BUTTON (button), image);
558 g_signal_connect (dialog, "response",
559 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
562 gtk_widget_show (dialog);
564 event->approval->dialog = dialog;
568 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
569 EmpathyMessage *message,
570 EventManagerApproval *approval)
572 EmpathyContact *sender;
576 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
578 /* try to update the event if it's referring to a chat which is already in the
580 event = event_lookup_by_approval (approval->manager, approval);
582 sender = empathy_message_get_sender (message);
584 /* We only want to show incoming messages */
585 if (empathy_contact_is_user (sender))
588 header = empathy_contact_get_alias (sender);
589 msg = empathy_message_get_body (message);
592 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
595 event_manager_add (approval->manager, NULL, sender,
596 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
597 approval, event_text_channel_process_func, NULL);
599 empathy_sound_manager_play (priv->sound_mgr, NULL,
600 EMPATHY_SOUND_CONVERSATION_NEW);
604 event_manager_approval_done (EventManagerApproval *approval)
606 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
609 if (approval->operation != NULL)
613 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
615 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
616 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
619 if (priv->ringing == 0)
620 empathy_sound_manager_stop (priv->sound_mgr,
621 EMPATHY_SOUND_PHONE_INCOMING);
625 priv->approvals = g_slist_remove (priv->approvals, approval);
627 for (l = priv->events; l; l = l->next)
629 EventPriv *event = l->data;
631 if (event->approval == approval)
633 event_remove (event);
638 event_manager_approval_free (approval);
642 cdo_invalidated_cb (TpProxy *cdo,
646 EventManagerApproval *approval)
648 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
650 event_manager_approval_done (approval);
654 event_manager_call_state_changed_cb (TpCallChannel *call,
657 TpCallStateReason *reason,
659 EventManagerApproval *approval)
661 if (state == TP_CALL_STATE_ENDED)
663 DEBUG ("Call ended, seems we missed it :/");
664 reject_approval (approval);
669 invite_dialog_response_cb (GtkDialog *dialog,
671 EventManagerApproval *approval)
673 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
674 approval->dialog = NULL;
676 if (response != GTK_RESPONSE_OK)
679 DEBUG ("Muc invitation rejected");
681 reject_approval (approval);
686 DEBUG ("Muc invitation accepted");
688 /* We'll join the room when handling the channel */
689 event_manager_approval_approve (approval);
693 event_room_channel_process_func (EventPriv *event)
695 GtkWidget *dialog, *button, *image;
696 TpChannel *channel = event->approval->main_channel;
699 if (event->approval->dialog != NULL)
701 gtk_window_present (GTK_WINDOW (event->approval->dialog));
706 dialog = gtk_message_dialog_new (NULL, 0,
707 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
709 title = g_strdup_printf (_("Invitation to join %s"),
710 tp_channel_get_identifier (channel));
712 gtk_window_set_title (GTK_WINDOW (dialog), title);
715 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
716 _("%s is inviting you to join %s"),
717 empathy_contact_get_alias (event->approval->contact),
718 tp_channel_get_identifier (channel));
720 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
723 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
724 _("_Decline"), GTK_RESPONSE_CANCEL);
725 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
726 gtk_button_set_image (GTK_BUTTON (button), image);
728 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
729 _("_Join"), GTK_RESPONSE_OK);
730 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
731 gtk_button_set_image (GTK_BUTTON (button), image);
733 g_signal_connect (dialog, "response",
734 G_CALLBACK (invite_dialog_response_cb), event->approval);
736 gtk_widget_show (dialog);
738 event->approval->dialog = dialog;
742 display_invite_room_dialog (EventManagerApproval *approval)
744 const gchar *invite_msg;
746 TpContact *self_contact;
747 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
749 self_contact = tp_channel_group_get_self_contact (approval->main_channel);
750 tp_channel_group_get_local_pending_contact_info (approval->main_channel,
751 self_contact, NULL, NULL, &invite_msg);
753 if (approval->contact != NULL)
755 msg = g_strdup_printf (_("%s invited you to join %s"),
756 empathy_contact_get_alias (approval->contact),
757 tp_channel_get_identifier (approval->main_channel));
761 msg = g_strdup_printf (_("You have been invited to join %s"),
762 tp_channel_get_identifier (approval->main_channel));
765 event_manager_add (approval->manager, NULL,
766 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
767 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
768 event_room_channel_process_func, NULL);
770 empathy_sound_manager_play (priv->sound_mgr, NULL,
771 EMPATHY_SOUND_CONVERSATION_NEW);
777 event_manager_auth_process_func (EventPriv *event)
779 empathy_event_approve ((EmpathyEvent *) event);
782 /* If there is a file-transfer, media, or auth channel consider it as
785 find_main_channel (GList *channels)
788 TpChannel *text = NULL;
790 for (l = channels; l != NULL; l = g_list_next (l))
792 TpChannel *channel = l->data;
795 if (tp_proxy_get_invalidated (channel) != NULL)
798 channel_type = tp_channel_get_channel_type_id (channel);
800 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
801 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
802 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
803 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
806 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
814 approve_text_channel (EmpathyEventManager *self,
815 EventManagerApproval *approval,
816 TpAddDispatchOperationContext *context,
817 EmpathyTpChat *tp_chat)
821 approval->handler_instance = g_object_ref (tp_chat);
823 if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
825 /* Are we in local-pending ? */
828 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
830 /* We are invited to a room */
831 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
832 tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
836 approval->contact = empathy_contact_dup_from_tp_contact (
840 display_invite_room_dialog (approval);
841 tp_add_dispatch_operation_context_accept (context);
846 /* We are not invited, approve the channel right now */
847 tp_add_dispatch_operation_context_accept (context);
849 approval->auto_approved = TRUE;
850 event_manager_approval_approve (approval);
854 /* 1-1 text channel, wait for the first message */
855 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
856 G_CALLBACK (event_manager_chat_message_received_cb), approval);
858 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
859 for (l = messages; l != NULL; l = g_list_next (l))
861 EmpathyMessage *msg = l->data;
863 event_manager_chat_message_received_cb (tp_chat, msg, approval);
866 tp_add_dispatch_operation_context_accept (context);
870 approval_set_target_contact (EventManagerApproval *approval,
875 contact = tp_channel_get_target_contact (channel);
876 approval->contact = empathy_contact_dup_from_tp_contact (contact);
880 approve_call_channel (EmpathyEventManager *self,
881 EventManagerApproval *approval,
882 TpAddDispatchOperationContext *context,
885 TpChannel *channel = TP_CHANNEL (call);
886 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
890 approval->handler_instance = g_object_ref (call);
891 approval_set_target_contact (approval, channel);
893 tp_add_dispatch_operation_context_accept (context);
895 if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
897 DEBUG ("Call already ended, seems we missed it :/");
898 reject_approval (approval);
902 approval->handler = g_signal_connect (call, "state-changed",
903 G_CALLBACK (event_manager_call_state_changed_cb), approval);
905 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
907 header = g_strdup_printf (
908 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
909 empathy_contact_get_alias (approval->contact));
911 event_manager_add (approval->manager, NULL,
912 approval->contact, EMPATHY_EVENT_TYPE_CALL,
913 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
914 header, NULL, approval,
915 event_channel_process_voip_func, NULL);
920 if (priv->ringing == 1)
922 TpAccountManager *am = tp_account_manager_dup ();
923 TpConnectionPresenceType presence;
925 presence = tp_account_manager_get_most_available_presence (am,
928 if (presence != TP_CONNECTION_PRESENCE_TYPE_BUSY)
929 empathy_sound_manager_start_playing (priv->sound_mgr, NULL,
930 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
937 approve_ft_channel (EmpathyEventManager *self,
938 EventManagerApproval *approval,
939 TpAddDispatchOperationContext *context,
940 TpFileTransferChannel *ft)
942 TpChannel *channel = TP_CHANNEL (ft);
943 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
946 approval->handler_instance = g_object_ref (ft);
947 approval_set_target_contact (approval, channel);
949 tp_add_dispatch_operation_context_accept (context);
951 header = g_strdup_printf (_("Incoming file transfer from %s"),
952 empathy_contact_get_alias (approval->contact));
954 event_manager_add (approval->manager, NULL,
955 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
956 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
957 approval, event_channel_process_func, NULL);
959 /* FIXME better sound for incoming file transfers ?*/
960 empathy_sound_manager_play (priv->sound_mgr, NULL,
961 EMPATHY_SOUND_CONVERSATION_NEW);
967 approve_sasl_channel (EmpathyEventManager *self,
969 EventManagerApproval *approval,
970 TpAddDispatchOperationContext *context,
973 if (empathy_sasl_channel_supports_mechanism (channel, "X-TELEPATHY-PASSWORD"))
975 event_manager_add (approval->manager, account, NULL,
976 EMPATHY_EVENT_TYPE_AUTH,
977 GTK_STOCK_DIALOG_AUTHENTICATION,
978 tp_account_get_display_name (account),
979 _("Password required"), approval,
980 event_manager_auth_process_func, NULL);
984 GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
985 "Support only X-TELEPATHY-PASSWORD auth method" };
987 tp_add_dispatch_operation_context_fail (context, &error);
991 tp_add_dispatch_operation_context_accept (context);
995 approve_channels (TpSimpleApprover *approver,
997 TpConnection *connection,
999 TpChannelDispatchOperation *dispatch_operation,
1000 TpAddDispatchOperationContext *context,
1003 EmpathyEventManager *self = user_data;
1004 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1006 EventManagerApproval *approval;
1007 GQuark channel_type;
1009 channel = find_main_channel (channels);
1010 if (channel == NULL)
1012 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1013 "Unknown channel type" };
1015 DEBUG ("Failed to find the main channel; ignoring");
1017 tp_add_dispatch_operation_context_fail (context, &error);
1021 approval = event_manager_approval_new (self, dispatch_operation, channel);
1022 priv->approvals = g_slist_prepend (priv->approvals, approval);
1024 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1025 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1027 channel_type = tp_channel_get_channel_type_id (channel);
1029 if (EMPATHY_IS_TP_CHAT (channel))
1031 approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1033 else if (TP_IS_CALL_CHANNEL (channel))
1035 approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1037 else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1039 approve_ft_channel (self, approval, context,
1040 TP_FILE_TRANSFER_CHANNEL (channel));
1042 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1044 approve_sasl_channel (self, account, approval, context, channel);
1048 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1049 "Invalid channel type" };
1051 DEBUG ("Unknown channel type (%s), ignoring..",
1052 g_quark_to_string (channel_type));
1054 tp_add_dispatch_operation_context_fail (context, &error);
1060 event_pending_subscribe_func (EventPriv *event)
1063 FolksIndividual *individual;
1065 individual = empathy_ensure_individual_from_tp_contact (
1066 empathy_contact_get_tp_contact (event->public.contact));
1068 dialog = empathy_subscription_dialog_new (individual, event->public.message);
1069 gtk_window_present (GTK_WINDOW (dialog));
1071 event_remove (event);
1073 g_object_unref (individual);
1077 check_publish_state (EmpathyEventManager *self,
1078 TpContact *tp_contact)
1080 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1081 gchar *header, *event_msg;
1082 TpSubscriptionState state;
1083 EmpathyContact *contact;
1084 const gchar *message;
1086 state = tp_contact_get_publish_state (tp_contact);
1088 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1090 if (state != TP_SUBSCRIPTION_STATE_ASK)
1094 for (l = priv->events; l; l = l->next)
1096 EventPriv *event = l->data;
1098 if (event->public.contact == contact &&
1099 event->func == event_pending_subscribe_func)
1101 event_remove (event);
1109 header = g_strdup_printf (
1110 _("%s would like permission to see when you are online"),
1111 empathy_contact_get_alias (contact));
1113 message = tp_contact_get_publish_request (tp_contact);
1115 if (!TPAW_STR_EMPTY (message))
1116 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1120 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1121 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1122 event_pending_subscribe_func, NULL);
1128 g_object_unref (contact);
1132 event_manager_publish_state_changed_cb (TpContact *contact,
1134 EmpathyEventManager *self)
1136 check_publish_state (self, contact);
1140 check_presence (EmpathyEventManager *manager,
1141 EmpathyContact *contact,
1142 TpConnectionPresenceType current,
1143 TpConnectionPresenceType previous)
1145 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1147 EmpathyPresenceManager *presence_mgr;
1149 account = empathy_contact_get_account (contact);
1150 presence_mgr = empathy_presence_manager_dup_singleton ();
1152 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1155 if (tp_connection_presence_type_cmp_availability (previous,
1156 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1158 /* contact was online */
1159 if (tp_connection_presence_type_cmp_availability (current,
1160 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1162 /* someone is logging off */
1163 empathy_sound_manager_play (priv->sound_mgr, NULL,
1164 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1166 if (g_settings_get_boolean (priv->gsettings_notif,
1167 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1169 event_manager_add (manager, NULL, contact,
1170 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1171 TPAW_IMAGE_AVATAR_DEFAULT,
1172 empathy_contact_get_alias (contact), _("Disconnected"),
1179 /* contact was offline */
1180 if (tp_connection_presence_type_cmp_availability (current,
1181 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1183 /* someone is logging in */
1184 empathy_sound_manager_play (priv->sound_mgr, NULL,
1185 EMPATHY_SOUND_CONTACT_CONNECTED);
1187 if (g_settings_get_boolean (priv->gsettings_notif,
1188 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1190 event_manager_add (manager, NULL, contact,
1191 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1192 TPAW_IMAGE_AVATAR_DEFAULT,
1193 empathy_contact_get_alias (contact), _("Connected"),
1200 g_object_unref (presence_mgr);
1204 event_manager_presence_changed_cb (EmpathyContact *contact,
1205 TpConnectionPresenceType current,
1206 TpConnectionPresenceType previous,
1207 EmpathyEventManager *manager)
1209 check_presence (manager, contact, current, previous);
1213 event_manager_constructor (GType type,
1215 GObjectConstructParam *props)
1219 if (manager_singleton) {
1220 retval = g_object_ref (manager_singleton);
1222 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1223 (type, n_props, props);
1225 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1226 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1233 event_manager_finalize (GObject *object)
1235 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1237 if (priv->ringing > 0)
1238 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1240 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1241 g_slist_free (priv->events);
1242 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1243 g_slist_free (priv->approvals);
1244 g_object_unref (priv->conn_aggregator);
1245 g_object_unref (priv->approver);
1246 g_object_unref (priv->auth_approver);
1247 g_object_unref (priv->gsettings_notif);
1248 g_object_unref (priv->gsettings_ui);
1249 g_object_unref (priv->sound_mgr);
1250 g_hash_table_unref (priv->contacts);
1254 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1256 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1258 object_class->finalize = event_manager_finalize;
1259 object_class->constructor = event_manager_constructor;
1261 signals[EVENT_ADDED] =
1262 g_signal_new ("event-added",
1263 G_TYPE_FROM_CLASS (klass),
1267 g_cclosure_marshal_generic,
1271 signals[EVENT_REMOVED] =
1272 g_signal_new ("event-removed",
1273 G_TYPE_FROM_CLASS (klass),
1277 g_cclosure_marshal_generic,
1278 G_TYPE_NONE, 1, G_TYPE_POINTER);
1280 signals[EVENT_UPDATED] =
1281 g_signal_new ("event-updated",
1282 G_TYPE_FROM_CLASS (klass),
1286 g_cclosure_marshal_generic,
1287 G_TYPE_NONE, 1, G_TYPE_POINTER);
1289 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1293 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1296 EmpathyEventManager *self)
1298 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1301 for (i = 0; i < added->len; i++)
1303 TpContact *tp_contact = g_ptr_array_index (added, i);
1304 EmpathyContact *contact;
1306 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1309 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1311 tp_g_signal_connect_object (contact, "presence-changed",
1312 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1314 check_presence (self, contact,
1315 empathy_contact_get_presence (contact),
1316 TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1318 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1319 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1321 check_publish_state (self, tp_contact);
1323 /* Pass ownership to the hash table */
1324 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1327 for (i = 0; i < removed->len; i++)
1329 TpContact *tp_contact = g_ptr_array_index (removed, i);
1330 EmpathyContact *contact;
1332 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1333 if (contact == NULL)
1336 g_signal_handlers_disconnect_by_func (contact,
1337 event_manager_presence_changed_cb, self);
1339 g_signal_handlers_disconnect_by_func (tp_contact,
1340 event_manager_publish_state_changed_cb, self);
1342 g_hash_table_remove (priv->contacts, tp_contact);
1347 empathy_event_manager_init (EmpathyEventManager *manager)
1349 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1350 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1351 GError *error = NULL;
1352 TpAccountManager *am;
1353 GPtrArray *contacts, *empty;
1355 manager->priv = priv;
1357 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1358 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1360 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1362 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1365 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1367 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1368 G_CALLBACK (contact_list_changed_cb), manager, 0);
1370 contacts = empathy_connection_aggregator_dup_all_contacts (
1371 priv->conn_aggregator);
1373 empty = g_ptr_array_new ();
1375 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1377 g_ptr_array_unref (contacts);
1378 g_ptr_array_unref (empty);
1380 am = tp_account_manager_dup ();
1382 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1383 FALSE, approve_channels, manager, NULL);
1385 /* Private text channels */
1386 tp_base_client_take_approver_filter (priv->approver,
1388 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1389 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1392 /* Muc text channels */
1393 tp_base_client_take_approver_filter (priv->approver,
1395 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1396 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1400 tp_base_client_take_approver_filter (priv->approver,
1402 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1403 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1404 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1408 tp_base_client_take_approver_filter (priv->approver,
1410 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1411 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1412 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1414 tp_base_client_take_approver_filter (priv->approver,
1416 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1417 TP_IFACE_CHANNEL_TYPE_CALL,
1418 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1421 /* I don't feel good about doing this, and I'm sorry, but the
1422 * capabilities connection feature is added earlier because it's
1423 * needed for EmpathyTpChat. If the capabilities feature is required
1424 * then preparing an auth channel (which of course appears in the
1425 * CONNECTING state) will never be prepared. So the options are
1426 * either to create another approver like I've done, or to port
1427 * EmpathyTpChat and its users to not depend on the connection being
1428 * prepared with capabilities. I chose the former, obviously. :-) */
1430 priv->auth_approver = tp_simple_approver_new_with_am (am,
1431 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1434 /* SASL auth channels */
1435 tp_base_client_take_approver_filter (priv->auth_approver,
1437 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1438 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1439 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1441 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1444 if (!tp_base_client_register (priv->approver, &error))
1446 DEBUG ("Failed to register Approver: %s", error->message);
1447 g_error_free (error);
1450 if (!tp_base_client_register (priv->auth_approver, &error))
1452 DEBUG ("Failed to register auth Approver: %s", error->message);
1453 g_error_free (error);
1456 g_object_unref (am);
1459 EmpathyEventManager *
1460 empathy_event_manager_dup_singleton (void)
1462 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1466 empathy_event_manager_get_events (EmpathyEventManager *manager)
1468 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1470 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1472 return priv->events;
1476 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1478 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1480 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1482 return priv->events ? priv->events->data : NULL;
1486 empathy_event_activate (EmpathyEvent *event_public)
1488 EventPriv *event = (EventPriv *) event_public;
1490 g_return_if_fail (event_public != NULL);
1493 event->func (event);
1495 event_remove (event);
1499 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1501 EventPriv *event = (EventPriv *) event_public;
1503 g_return_if_fail (event_public != NULL);
1505 event->inhibit = TRUE;
1509 empathy_event_approve (EmpathyEvent *event_public)
1511 EventPriv *event = (EventPriv *) event_public;
1513 g_return_if_fail (event_public != NULL);
1515 event_manager_approval_approve (event->approval);
1519 empathy_event_decline (EmpathyEvent *event_public)
1521 EventPriv *event = (EventPriv *) event_public;
1523 g_return_if_fail (event_public != NULL);
1525 reject_approval (event->approval);