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>
27 #include "empathy-call-utils.h"
28 #include "empathy-connection-aggregator.h"
29 #include "empathy-gsettings.h"
30 #include "empathy-images.h"
31 #include "empathy-presence-manager.h"
32 #include "empathy-sasl-mechanisms.h"
33 #include "empathy-sound-manager.h"
34 #include "empathy-subscription-dialog.h"
35 #include "empathy-tp-chat.h"
36 #include "empathy-ui-utils.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
44 #define NOTIFICATION_TIMEOUT 2 /* seconds */
46 #define ACCEPT_WITHOUT_VIDEO 1
48 /* The time interval in milliseconds between 2 incoming rings */
49 #define MS_BETWEEN_RING 500
52 EmpathyEventManager *manager;
53 TpChannelDispatchOperation *operation;
54 gulong invalidated_handler;
55 /* Remove contact if applicable */
56 EmpathyContact *contact;
57 /* option signal handler and it's instance */
59 GObject *handler_instance;
60 /* optional accept widget */
62 /* Channel of the CDO that will be used during the approval */
63 TpChannel *main_channel;
64 gboolean auto_approved;
65 } EventManagerApproval;
68 TpBaseClient *approver;
69 TpBaseClient *auth_approver;
70 EmpathyConnectionAggregator *conn_aggregator;
72 /* Ongoing approvals */
77 GSettings *gsettings_notif;
78 GSettings *gsettings_ui;
80 EmpathySoundManager *sound_mgr;
82 /* TpContact -> EmpathyContact */
84 } EmpathyEventManagerPriv;
86 typedef struct _EventPriv EventPriv;
87 typedef void (*EventFunc) (EventPriv *event);
91 EmpathyEventManager *manager;
92 EventManagerApproval *approval;
96 guint autoremove_timeout_id;
106 static guint signals[LAST_SIGNAL];
108 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
110 static EmpathyEventManager * manager_singleton = NULL;
112 static EventManagerApproval *
113 event_manager_approval_new (EmpathyEventManager *manager,
114 TpChannelDispatchOperation *operation,
115 TpChannel *main_channel)
117 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
118 result->operation = g_object_ref (operation);
119 result->manager = manager;
120 result->main_channel = g_object_ref (main_channel);
126 event_manager_approval_free (EventManagerApproval *approval)
128 g_signal_handler_disconnect (approval->operation,
129 approval->invalidated_handler);
130 g_object_unref (approval->operation);
132 g_object_unref (approval->main_channel);
134 if (approval->handler != 0)
135 g_signal_handler_disconnect (approval->handler_instance,
138 if (approval->handler_instance != NULL)
139 g_object_unref (approval->handler_instance);
141 if (approval->contact != NULL)
142 g_object_unref (approval->contact);
144 if (approval->dialog != NULL)
146 gtk_widget_destroy (approval->dialog);
149 g_slice_free (EventManagerApproval, approval);
153 event_free (EventPriv *event)
155 g_free (event->public.icon_name);
156 g_free (event->public.header);
157 g_free (event->public.message);
159 if (event->autoremove_timeout_id != 0)
160 g_source_remove (event->autoremove_timeout_id);
162 tp_clear_object (&(event->public.contact));
163 tp_clear_object (&(event->public.account));
165 g_slice_free (EventPriv, event);
169 event_remove (EventPriv *event)
171 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
173 DEBUG ("Removing event %p", event);
175 priv->events = g_slist_remove (priv->events, event);
176 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
181 empathy_event_remove (EmpathyEvent *event_public)
183 EventPriv *event = (EventPriv *) event_public;
185 event_remove (event);
189 autoremove_event_timeout_cb (EventPriv *event)
191 event->autoremove_timeout_id = 0;
192 event_remove (event);
197 display_notify_area (EmpathyEventManager *self)
199 EmpathyEventManagerPriv *priv = GET_PRIV (self);
201 return g_settings_get_boolean (priv->gsettings_ui,
202 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
206 event_manager_add (EmpathyEventManager *manager,
208 EmpathyContact *contact,
209 EmpathyEventType type,
210 const gchar *icon_name,
212 const gchar *message,
213 EventManagerApproval *approval,
217 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
220 event = g_slice_new0 (EventPriv);
221 event->public.account = account != NULL ? g_object_ref (account) : NULL;
222 event->public.contact = contact ? g_object_ref (contact) : NULL;
223 event->public.type = type;
224 event->public.icon_name = g_strdup (icon_name);
225 event->public.header = g_strdup (header);
226 event->public.message = g_strdup (message);
227 event->public.must_ack = (func != NULL);
228 if (approval != NULL)
229 event->public.handler_instance = approval->handler_instance;
230 event->inhibit = FALSE;
232 event->user_data = user_data;
233 event->manager = manager;
234 event->approval = approval;
236 DEBUG ("Adding event %p", event);
237 priv->events = g_slist_prepend (priv->events, event);
239 if (!display_notify_area (manager))
241 /* Don't fire the 'event-added' signal as we activate the event now */
242 if (approval != NULL)
243 approval->auto_approved = TRUE;
245 empathy_event_activate (&event->public);
249 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
251 if (!event->public.must_ack)
253 event->autoremove_timeout_id = g_timeout_add_seconds (
254 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
260 handle_with_cb (GObject *source,
261 GAsyncResult *result,
264 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
265 GError *error = NULL;
267 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
269 DEBUG ("HandleWith failed: %s\n", error->message);
270 g_error_free (error);
275 handle_with_time_cb (GObject *source,
276 GAsyncResult *result,
279 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
280 GError *error = NULL;
282 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
285 if (g_error_matches (error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED))
287 EventManagerApproval *approval = user_data;
289 DEBUG ("HandleWithTime() is not implemented, falling back to "
290 "HandleWith(). Please upgrade to telepathy-mission-control "
293 tp_channel_dispatch_operation_handle_with_async (approval->operation,
294 NULL, handle_with_cb, approval);
298 DEBUG ("HandleWithTime failed: %s\n", error->message);
300 g_error_free (error);
305 event_manager_approval_approve (EventManagerApproval *approval)
309 if (approval->auto_approved)
311 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
315 timestamp = empathy_get_current_action_time ();
318 g_assert (approval->operation != NULL);
320 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
321 NULL, timestamp, handle_with_time_cb, approval);
325 event_channel_process_func (EventPriv *event)
327 event_manager_approval_approve (event->approval);
331 event_text_channel_process_func (EventPriv *event)
333 EmpathyTpChat *tp_chat;
335 if (event->approval->handler != 0)
337 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
339 g_signal_handler_disconnect (tp_chat, event->approval->handler);
340 event->approval->handler = 0;
343 event_manager_approval_approve (event->approval);
347 event_lookup_by_approval (EmpathyEventManager *manager,
348 EventManagerApproval *approval)
350 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
352 EventPriv *retval = NULL;
354 for (l = priv->events; l; l = l->next)
356 EventPriv *event = l->data;
358 if (event->approval == approval)
369 event_update (EmpathyEventManager *manager, EventPriv *event,
370 const char *icon_name, const char *header, const char *msg)
372 g_free (event->public.icon_name);
373 g_free (event->public.header);
374 g_free (event->public.message);
376 event->public.icon_name = g_strdup (icon_name);
377 event->public.header = g_strdup (header);
378 event->public.message = g_strdup (msg);
380 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
384 reject_channel_claim_cb (GObject *source,
385 GAsyncResult *result,
388 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
389 GError *error = NULL;
391 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
393 DEBUG ("Failed to claim channel: %s", error->message);
395 g_error_free (error);
399 if (TP_IS_CALL_CHANNEL (user_data))
401 tp_call_channel_hangup_async (user_data,
402 TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
404 tp_channel_close_async (user_data, NULL, NULL);
406 else if (EMPATHY_IS_TP_CHAT (user_data))
408 empathy_tp_chat_leave (user_data, "");
410 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
412 tp_channel_close_async (user_data, NULL, NULL);
416 g_object_unref (user_data);
420 reject_approval (EventManagerApproval *approval)
422 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
424 /* We have to claim the channel before closing it */
426 /* Unfortunately, we need to special case the auth channels for the
427 * time being as they don't have a wrapper object handler in
428 * approval->handler_instance as they're not actually handled by
429 * this process, so we can just use a noddy callback to call Close()
431 if (approval->handler_instance != NULL)
433 tp_channel_dispatch_operation_claim_with_async (approval->operation,
434 priv->approver, reject_channel_claim_cb,
435 g_object_ref (approval->handler_instance));
437 else if (tp_channel_get_channel_type_id (approval->main_channel)
438 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
440 tp_channel_dispatch_operation_close_channels_async (approval->operation,
446 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
447 gint response, gpointer user_data)
449 EventManagerApproval *approval = user_data;
451 gtk_widget_destroy (approval->dialog);
452 approval->dialog = NULL;
454 if (response == GTK_RESPONSE_ACCEPT)
456 event_manager_approval_approve (approval);
458 else if (response == ACCEPT_WITHOUT_VIDEO)
460 empathy_call_channel_send_video (TP_CALL_CHANNEL (approval->main_channel),
462 event_manager_approval_approve (approval);
466 reject_approval (approval);
471 event_channel_process_voip_func (EventPriv *event)
478 EmpathyEventType etype = event->public.type;
480 if (event->approval->dialog != NULL)
482 gtk_window_present (GTK_WINDOW (event->approval->dialog));
486 if (etype == EMPATHY_EVENT_TYPE_CALL)
489 call = TP_CALL_CHANNEL (event->approval->handler_instance);
490 video = tp_call_channel_has_initial_video (call, NULL);
494 g_warning ("Unknown event type: %d", event->public.type);
498 dialog = gtk_message_dialog_new (NULL, 0,
499 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
500 video ? _("Incoming video call"): _("Incoming call"));
502 gtk_message_dialog_format_secondary_text (
503 GTK_MESSAGE_DIALOG (dialog), video ?
504 _("%s is video calling you. Do you want to answer?"):
505 _("%s is calling you. Do you want to answer?"),
506 empathy_contact_get_alias (event->approval->contact));
508 title = g_strdup_printf (_("Incoming call from %s"),
509 empathy_contact_get_alias (event->approval->contact));
511 gtk_window_set_title (GTK_WINDOW (dialog), title);
514 /* Set image of the dialog */
517 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
518 GTK_ICON_SIZE_DIALOG);
522 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
523 GTK_ICON_SIZE_DIALOG);
526 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
527 gtk_widget_show (image);
529 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
532 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
533 _("_Reject"), GTK_RESPONSE_REJECT);
534 image = gtk_image_new_from_icon_name ("call-stop",
535 GTK_ICON_SIZE_BUTTON);
536 gtk_button_set_image (GTK_BUTTON (button), image);
538 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
540 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
541 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
543 image = gtk_image_new_from_icon_name ("call-start",
544 GTK_ICON_SIZE_BUTTON);
545 gtk_button_set_image (GTK_BUTTON (button), image);
548 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
549 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
551 image = gtk_image_new_from_icon_name ("call-start",
552 GTK_ICON_SIZE_BUTTON);
553 gtk_button_set_image (GTK_BUTTON (button), image);
555 g_signal_connect (dialog, "response",
556 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
559 gtk_widget_show (dialog);
561 event->approval->dialog = dialog;
565 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
566 EmpathyMessage *message,
567 EventManagerApproval *approval)
569 EmpathyContact *sender;
573 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
575 /* try to update the event if it's referring to a chat which is already in the
577 event = event_lookup_by_approval (approval->manager, approval);
579 sender = empathy_message_get_sender (message);
581 /* We only want to show incoming messages */
582 if (empathy_contact_is_user (sender))
585 header = empathy_contact_get_alias (sender);
586 msg = empathy_message_get_body (message);
589 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
592 event_manager_add (approval->manager, NULL, sender,
593 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
594 approval, event_text_channel_process_func, NULL);
596 empathy_sound_manager_play (priv->sound_mgr, NULL,
597 EMPATHY_SOUND_CONVERSATION_NEW);
601 event_manager_approval_done (EventManagerApproval *approval)
603 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
606 if (approval->operation != NULL)
610 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
612 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
613 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
616 if (priv->ringing == 0)
617 empathy_sound_manager_stop (priv->sound_mgr,
618 EMPATHY_SOUND_PHONE_INCOMING);
622 priv->approvals = g_slist_remove (priv->approvals, approval);
624 for (l = priv->events; l; l = l->next)
626 EventPriv *event = l->data;
628 if (event->approval == approval)
630 event_remove (event);
635 event_manager_approval_free (approval);
639 cdo_invalidated_cb (TpProxy *cdo,
643 EventManagerApproval *approval)
645 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
647 event_manager_approval_done (approval);
651 event_manager_call_state_changed_cb (TpCallChannel *call,
654 TpCallStateReason *reason,
656 EventManagerApproval *approval)
658 if (state == TP_CALL_STATE_ENDED)
660 DEBUG ("Call ended, seems we missed it :/");
661 reject_approval (approval);
666 invite_dialog_response_cb (GtkDialog *dialog,
668 EventManagerApproval *approval)
670 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
671 approval->dialog = NULL;
673 if (response != GTK_RESPONSE_OK)
676 DEBUG ("Muc invitation rejected");
678 reject_approval (approval);
683 DEBUG ("Muc invitation accepted");
685 /* We'll join the room when handling the channel */
686 event_manager_approval_approve (approval);
690 event_room_channel_process_func (EventPriv *event)
692 GtkWidget *dialog, *button, *image;
693 TpChannel *channel = event->approval->main_channel;
696 if (event->approval->dialog != NULL)
698 gtk_window_present (GTK_WINDOW (event->approval->dialog));
703 dialog = gtk_message_dialog_new (NULL, 0,
704 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
706 title = g_strdup_printf (_("Invitation to join %s"),
707 tp_channel_get_identifier (channel));
709 gtk_window_set_title (GTK_WINDOW (dialog), title);
712 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
713 _("%s is inviting you to join %s"),
714 empathy_contact_get_alias (event->approval->contact),
715 tp_channel_get_identifier (channel));
717 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
720 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
721 _("_Decline"), GTK_RESPONSE_CANCEL);
722 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
723 gtk_button_set_image (GTK_BUTTON (button), image);
725 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
726 _("_Join"), GTK_RESPONSE_OK);
727 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
728 gtk_button_set_image (GTK_BUTTON (button), image);
730 g_signal_connect (dialog, "response",
731 G_CALLBACK (invite_dialog_response_cb), event->approval);
733 gtk_widget_show (dialog);
735 event->approval->dialog = dialog;
739 display_invite_room_dialog (EventManagerApproval *approval)
741 const gchar *invite_msg;
743 TpContact *self_contact;
744 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
746 self_contact = tp_channel_group_get_self_contact (approval->main_channel);
747 tp_channel_group_get_local_pending_contact_info (approval->main_channel,
748 self_contact, NULL, NULL, &invite_msg);
750 if (approval->contact != NULL)
752 msg = g_strdup_printf (_("%s invited you to join %s"),
753 empathy_contact_get_alias (approval->contact),
754 tp_channel_get_identifier (approval->main_channel));
758 msg = g_strdup_printf (_("You have been invited to join %s"),
759 tp_channel_get_identifier (approval->main_channel));
762 event_manager_add (approval->manager, NULL,
763 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
764 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
765 event_room_channel_process_func, NULL);
767 empathy_sound_manager_play (priv->sound_mgr, NULL,
768 EMPATHY_SOUND_CONVERSATION_NEW);
774 event_manager_auth_process_func (EventPriv *event)
776 empathy_event_approve ((EmpathyEvent *) event);
779 /* If there is a file-transfer, media, or auth channel consider it as
782 find_main_channel (GList *channels)
785 TpChannel *text = NULL;
787 for (l = channels; l != NULL; l = g_list_next (l))
789 TpChannel *channel = l->data;
792 if (tp_proxy_get_invalidated (channel) != NULL)
795 channel_type = tp_channel_get_channel_type_id (channel);
797 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
798 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
799 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
800 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
803 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
811 approve_text_channel (EmpathyEventManager *self,
812 EventManagerApproval *approval,
813 TpAddDispatchOperationContext *context,
814 EmpathyTpChat *tp_chat)
818 approval->handler_instance = g_object_ref (tp_chat);
820 if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
822 /* Are we in local-pending ? */
825 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
827 /* We are invited to a room */
828 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
829 tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
833 approval->contact = empathy_contact_dup_from_tp_contact (
837 display_invite_room_dialog (approval);
838 tp_add_dispatch_operation_context_accept (context);
843 /* We are not invited, approve the channel right now */
844 tp_add_dispatch_operation_context_accept (context);
846 approval->auto_approved = TRUE;
847 event_manager_approval_approve (approval);
851 /* 1-1 text channel, wait for the first message */
852 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
853 G_CALLBACK (event_manager_chat_message_received_cb), approval);
855 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
856 for (l = messages; l != NULL; l = g_list_next (l))
858 EmpathyMessage *msg = l->data;
860 event_manager_chat_message_received_cb (tp_chat, msg, approval);
863 tp_add_dispatch_operation_context_accept (context);
867 approval_set_target_contact (EventManagerApproval *approval,
872 contact = tp_channel_get_target_contact (channel);
873 approval->contact = empathy_contact_dup_from_tp_contact (contact);
877 approve_call_channel (EmpathyEventManager *self,
878 EventManagerApproval *approval,
879 TpAddDispatchOperationContext *context,
882 TpChannel *channel = TP_CHANNEL (call);
883 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
887 approval->handler_instance = g_object_ref (call);
888 approval_set_target_contact (approval, channel);
890 tp_add_dispatch_operation_context_accept (context);
892 if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
894 DEBUG ("Call already ended, seems we missed it :/");
895 reject_approval (approval);
899 approval->handler = g_signal_connect (call, "state-changed",
900 G_CALLBACK (event_manager_call_state_changed_cb), approval);
902 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
904 header = g_strdup_printf (
905 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
906 empathy_contact_get_alias (approval->contact));
908 event_manager_add (approval->manager, NULL,
909 approval->contact, EMPATHY_EVENT_TYPE_CALL,
910 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
911 header, NULL, approval,
912 event_channel_process_voip_func, NULL);
917 if (priv->ringing == 1)
919 TpAccountManager *am = tp_account_manager_dup ();
920 TpConnectionPresenceType presence;
922 presence = tp_account_manager_get_most_available_presence (am,
925 if (presence != TP_CONNECTION_PRESENCE_TYPE_BUSY)
926 empathy_sound_manager_start_playing (priv->sound_mgr, NULL,
927 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
934 approve_ft_channel (EmpathyEventManager *self,
935 EventManagerApproval *approval,
936 TpAddDispatchOperationContext *context,
937 TpFileTransferChannel *ft)
939 TpChannel *channel = TP_CHANNEL (ft);
940 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
943 approval->handler_instance = g_object_ref (ft);
944 approval_set_target_contact (approval, channel);
946 tp_add_dispatch_operation_context_accept (context);
948 header = g_strdup_printf (_("Incoming file transfer from %s"),
949 empathy_contact_get_alias (approval->contact));
951 event_manager_add (approval->manager, NULL,
952 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
953 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
954 approval, event_channel_process_func, NULL);
956 /* FIXME better sound for incoming file transfers ?*/
957 empathy_sound_manager_play (priv->sound_mgr, NULL,
958 EMPATHY_SOUND_CONVERSATION_NEW);
964 approve_sasl_channel (EmpathyEventManager *self,
966 EventManagerApproval *approval,
967 TpAddDispatchOperationContext *context,
970 if (empathy_sasl_channel_supports_mechanism (channel, "X-TELEPATHY-PASSWORD"))
972 event_manager_add (approval->manager, account, NULL,
973 EMPATHY_EVENT_TYPE_AUTH,
974 GTK_STOCK_DIALOG_AUTHENTICATION,
975 tp_account_get_display_name (account),
976 _("Password required"), approval,
977 event_manager_auth_process_func, NULL);
981 GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
982 "Support only X-TELEPATHY-PASSWORD auth method" };
984 tp_add_dispatch_operation_context_fail (context, &error);
988 tp_add_dispatch_operation_context_accept (context);
992 approve_channels (TpSimpleApprover *approver,
994 TpConnection *connection,
996 TpChannelDispatchOperation *dispatch_operation,
997 TpAddDispatchOperationContext *context,
1000 EmpathyEventManager *self = user_data;
1001 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1003 EventManagerApproval *approval;
1004 GQuark channel_type;
1006 channel = find_main_channel (channels);
1007 if (channel == NULL)
1009 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1010 "Unknown channel type" };
1012 DEBUG ("Failed to find the main channel; ignoring");
1014 tp_add_dispatch_operation_context_fail (context, &error);
1018 approval = event_manager_approval_new (self, dispatch_operation, channel);
1019 priv->approvals = g_slist_prepend (priv->approvals, approval);
1021 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1022 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1024 channel_type = tp_channel_get_channel_type_id (channel);
1026 if (EMPATHY_IS_TP_CHAT (channel))
1028 approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1030 else if (TP_IS_CALL_CHANNEL (channel))
1032 approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1034 else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1036 approve_ft_channel (self, approval, context,
1037 TP_FILE_TRANSFER_CHANNEL (channel));
1039 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1041 approve_sasl_channel (self, account, approval, context, channel);
1045 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1046 "Invalid channel type" };
1048 DEBUG ("Unknown channel type (%s), ignoring..",
1049 g_quark_to_string (channel_type));
1051 tp_add_dispatch_operation_context_fail (context, &error);
1057 event_pending_subscribe_func (EventPriv *event)
1060 FolksIndividual *individual;
1062 individual = empathy_ensure_individual_from_tp_contact (
1063 empathy_contact_get_tp_contact (event->public.contact));
1065 dialog = empathy_subscription_dialog_new (individual, event->public.message);
1066 gtk_window_present (GTK_WINDOW (dialog));
1068 event_remove (event);
1070 g_object_unref (individual);
1074 check_publish_state (EmpathyEventManager *self,
1075 TpContact *tp_contact)
1077 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1078 gchar *header, *event_msg;
1079 TpSubscriptionState state;
1080 EmpathyContact *contact;
1081 const gchar *message;
1083 state = tp_contact_get_publish_state (tp_contact);
1085 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1087 if (state != TP_SUBSCRIPTION_STATE_ASK)
1091 for (l = priv->events; l; l = l->next)
1093 EventPriv *event = l->data;
1095 if (event->public.contact == contact &&
1096 event->func == event_pending_subscribe_func)
1098 event_remove (event);
1106 header = g_strdup_printf (
1107 _("%s would like permission to see when you are online"),
1108 empathy_contact_get_alias (contact));
1110 message = tp_contact_get_publish_request (tp_contact);
1112 if (!EMP_STR_EMPTY (message))
1113 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1117 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1118 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1119 event_pending_subscribe_func, NULL);
1125 g_object_unref (contact);
1129 event_manager_publish_state_changed_cb (TpContact *contact,
1131 EmpathyEventManager *self)
1133 check_publish_state (self, contact);
1137 check_presence (EmpathyEventManager *manager,
1138 EmpathyContact *contact,
1139 TpConnectionPresenceType current,
1140 TpConnectionPresenceType previous)
1142 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1144 EmpathyPresenceManager *presence_mgr;
1146 account = empathy_contact_get_account (contact);
1147 presence_mgr = empathy_presence_manager_dup_singleton ();
1149 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1152 if (tp_connection_presence_type_cmp_availability (previous,
1153 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1155 /* contact was online */
1156 if (tp_connection_presence_type_cmp_availability (current,
1157 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1159 /* someone is logging off */
1160 empathy_sound_manager_play (priv->sound_mgr, NULL,
1161 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1163 if (g_settings_get_boolean (priv->gsettings_notif,
1164 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1166 event_manager_add (manager, NULL, contact,
1167 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1168 EMPATHY_IMAGE_AVATAR_DEFAULT,
1169 empathy_contact_get_alias (contact), _("Disconnected"),
1176 /* contact was offline */
1177 if (tp_connection_presence_type_cmp_availability (current,
1178 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1180 /* someone is logging in */
1181 empathy_sound_manager_play (priv->sound_mgr, NULL,
1182 EMPATHY_SOUND_CONTACT_CONNECTED);
1184 if (g_settings_get_boolean (priv->gsettings_notif,
1185 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1187 event_manager_add (manager, NULL, contact,
1188 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1189 EMPATHY_IMAGE_AVATAR_DEFAULT,
1190 empathy_contact_get_alias (contact), _("Connected"),
1197 g_object_unref (presence_mgr);
1201 event_manager_presence_changed_cb (EmpathyContact *contact,
1202 TpConnectionPresenceType current,
1203 TpConnectionPresenceType previous,
1204 EmpathyEventManager *manager)
1206 check_presence (manager, contact, current, previous);
1210 event_manager_constructor (GType type,
1212 GObjectConstructParam *props)
1216 if (manager_singleton) {
1217 retval = g_object_ref (manager_singleton);
1219 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1220 (type, n_props, props);
1222 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1223 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1230 event_manager_finalize (GObject *object)
1232 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1234 if (priv->ringing > 0)
1235 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1237 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1238 g_slist_free (priv->events);
1239 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1240 g_slist_free (priv->approvals);
1241 g_object_unref (priv->conn_aggregator);
1242 g_object_unref (priv->approver);
1243 g_object_unref (priv->auth_approver);
1244 g_object_unref (priv->gsettings_notif);
1245 g_object_unref (priv->gsettings_ui);
1246 g_object_unref (priv->sound_mgr);
1247 g_hash_table_unref (priv->contacts);
1251 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1253 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1255 object_class->finalize = event_manager_finalize;
1256 object_class->constructor = event_manager_constructor;
1258 signals[EVENT_ADDED] =
1259 g_signal_new ("event-added",
1260 G_TYPE_FROM_CLASS (klass),
1264 g_cclosure_marshal_generic,
1268 signals[EVENT_REMOVED] =
1269 g_signal_new ("event-removed",
1270 G_TYPE_FROM_CLASS (klass),
1274 g_cclosure_marshal_generic,
1275 G_TYPE_NONE, 1, G_TYPE_POINTER);
1277 signals[EVENT_UPDATED] =
1278 g_signal_new ("event-updated",
1279 G_TYPE_FROM_CLASS (klass),
1283 g_cclosure_marshal_generic,
1284 G_TYPE_NONE, 1, G_TYPE_POINTER);
1286 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1290 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1293 EmpathyEventManager *self)
1295 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1298 for (i = 0; i < added->len; i++)
1300 TpContact *tp_contact = g_ptr_array_index (added, i);
1301 EmpathyContact *contact;
1303 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1306 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1308 tp_g_signal_connect_object (contact, "presence-changed",
1309 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1311 check_presence (self, contact,
1312 empathy_contact_get_presence (contact),
1313 TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1315 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1316 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1318 check_publish_state (self, tp_contact);
1320 /* Pass ownership to the hash table */
1321 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1324 for (i = 0; i < removed->len; i++)
1326 TpContact *tp_contact = g_ptr_array_index (removed, i);
1327 EmpathyContact *contact;
1329 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1330 if (contact == NULL)
1333 g_signal_handlers_disconnect_by_func (contact,
1334 event_manager_presence_changed_cb, self);
1336 g_signal_handlers_disconnect_by_func (tp_contact,
1337 event_manager_publish_state_changed_cb, self);
1339 g_hash_table_remove (priv->contacts, tp_contact);
1344 empathy_event_manager_init (EmpathyEventManager *manager)
1346 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1347 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1348 GError *error = NULL;
1349 TpAccountManager *am;
1350 GPtrArray *contacts, *empty;
1352 manager->priv = priv;
1354 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1355 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1357 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1359 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1362 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1364 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1365 G_CALLBACK (contact_list_changed_cb), manager, 0);
1367 contacts = empathy_connection_aggregator_dup_all_contacts (
1368 priv->conn_aggregator);
1370 empty = g_ptr_array_new ();
1372 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1374 g_ptr_array_unref (contacts);
1375 g_ptr_array_unref (empty);
1377 am = tp_account_manager_dup ();
1379 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1380 FALSE, approve_channels, manager, NULL);
1382 /* Private text channels */
1383 tp_base_client_take_approver_filter (priv->approver,
1385 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1386 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1389 /* Muc text channels */
1390 tp_base_client_take_approver_filter (priv->approver,
1392 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1393 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1397 tp_base_client_take_approver_filter (priv->approver,
1399 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1400 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1401 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1405 tp_base_client_take_approver_filter (priv->approver,
1407 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1408 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1409 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1411 tp_base_client_take_approver_filter (priv->approver,
1413 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1414 TP_IFACE_CHANNEL_TYPE_CALL,
1415 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1418 /* I don't feel good about doing this, and I'm sorry, but the
1419 * capabilities connection feature is added earlier because it's
1420 * needed for EmpathyTpChat. If the capabilities feature is required
1421 * then preparing an auth channel (which of course appears in the
1422 * CONNECTING state) will never be prepared. So the options are
1423 * either to create another approver like I've done, or to port
1424 * EmpathyTpChat and its users to not depend on the connection being
1425 * prepared with capabilities. I chose the former, obviously. :-) */
1427 priv->auth_approver = tp_simple_approver_new_with_am (am,
1428 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1431 /* SASL auth channels */
1432 tp_base_client_take_approver_filter (priv->auth_approver,
1434 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1435 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1436 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1438 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1441 if (!tp_base_client_register (priv->approver, &error))
1443 DEBUG ("Failed to register Approver: %s", error->message);
1444 g_error_free (error);
1447 if (!tp_base_client_register (priv->auth_approver, &error))
1449 DEBUG ("Failed to register auth Approver: %s", error->message);
1450 g_error_free (error);
1453 g_object_unref (am);
1456 EmpathyEventManager *
1457 empathy_event_manager_dup_singleton (void)
1459 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1463 empathy_event_manager_get_events (EmpathyEventManager *manager)
1465 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1467 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1469 return priv->events;
1473 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1475 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1477 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1479 return priv->events ? priv->events->data : NULL;
1483 empathy_event_activate (EmpathyEvent *event_public)
1485 EventPriv *event = (EventPriv *) event_public;
1487 g_return_if_fail (event_public != NULL);
1490 event->func (event);
1492 event_remove (event);
1496 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1498 EventPriv *event = (EventPriv *) event_public;
1500 g_return_if_fail (event_public != NULL);
1502 event->inhibit = TRUE;
1506 empathy_event_approve (EmpathyEvent *event_public)
1508 EventPriv *event = (EventPriv *) event_public;
1510 g_return_if_fail (event_public != NULL);
1512 event_manager_approval_approve (event->approval);
1516 empathy_event_decline (EmpathyEvent *event_public)
1518 EventPriv *event = (EventPriv *) event_public;
1520 g_return_if_fail (event_public != NULL);
1522 reject_approval (event->approval);