2 * Copyright (C) 2007-2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
25 #include <glib/gi18n.h>
27 #include <telepathy-glib/telepathy-glib.h>
29 #include <libempathy/empathy-presence-manager.h>
30 #include <libempathy/empathy-connection-aggregator.h>
31 #include <libempathy/empathy-tp-chat.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-gsettings.h>
34 #include <libempathy/empathy-sasl-mechanisms.h>
36 #include <extensions/extensions.h>
38 #include <libempathy-gtk/empathy-images.h>
39 #include <libempathy-gtk/empathy-sound-manager.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-call-utils.h>
42 #include <libempathy-gtk/empathy-subscription-dialog.h>
44 #include "empathy-event-manager.h"
45 #include "empathy-roster-window.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
48 #include <libempathy/empathy-debug.h>
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
52 #define NOTIFICATION_TIMEOUT 2 /* seconds */
54 #define ACCEPT_WITHOUT_VIDEO 1
56 /* The time interval in milliseconds between 2 incoming rings */
57 #define MS_BETWEEN_RING 500
60 EmpathyEventManager *manager;
61 TpChannelDispatchOperation *operation;
62 gulong invalidated_handler;
63 /* Remove contact if applicable */
64 EmpathyContact *contact;
65 /* option signal handler and it's instance */
67 GObject *handler_instance;
68 /* optional accept widget */
70 /* Channel of the CDO that will be used during the approval */
71 TpChannel *main_channel;
72 gboolean auto_approved;
73 } EventManagerApproval;
76 TpBaseClient *approver;
77 TpBaseClient *auth_approver;
78 EmpathyConnectionAggregator *conn_aggregator;
80 /* Ongoing approvals */
85 GSettings *gsettings_notif;
86 GSettings *gsettings_ui;
88 EmpathySoundManager *sound_mgr;
90 /* TpContact -> EmpathyContact */
92 } EmpathyEventManagerPriv;
94 typedef struct _EventPriv EventPriv;
95 typedef void (*EventFunc) (EventPriv *event);
99 EmpathyEventManager *manager;
100 EventManagerApproval *approval;
104 guint autoremove_timeout_id;
114 static guint signals[LAST_SIGNAL];
116 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
118 static EmpathyEventManager * manager_singleton = NULL;
120 static EventManagerApproval *
121 event_manager_approval_new (EmpathyEventManager *manager,
122 TpChannelDispatchOperation *operation,
123 TpChannel *main_channel)
125 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
126 result->operation = g_object_ref (operation);
127 result->manager = manager;
128 result->main_channel = g_object_ref (main_channel);
134 event_manager_approval_free (EventManagerApproval *approval)
136 g_signal_handler_disconnect (approval->operation,
137 approval->invalidated_handler);
138 g_object_unref (approval->operation);
140 g_object_unref (approval->main_channel);
142 if (approval->handler != 0)
143 g_signal_handler_disconnect (approval->handler_instance,
146 if (approval->handler_instance != NULL)
147 g_object_unref (approval->handler_instance);
149 if (approval->contact != NULL)
150 g_object_unref (approval->contact);
152 if (approval->dialog != NULL)
154 gtk_widget_destroy (approval->dialog);
157 g_slice_free (EventManagerApproval, approval);
161 event_free (EventPriv *event)
163 g_free (event->public.icon_name);
164 g_free (event->public.header);
165 g_free (event->public.message);
167 if (event->autoremove_timeout_id != 0)
168 g_source_remove (event->autoremove_timeout_id);
170 tp_clear_object (&(event->public.contact));
171 tp_clear_object (&(event->public.account));
173 g_slice_free (EventPriv, event);
177 event_remove (EventPriv *event)
179 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
181 DEBUG ("Removing event %p", event);
183 priv->events = g_slist_remove (priv->events, event);
184 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
189 empathy_event_remove (EmpathyEvent *event_public)
191 EventPriv *event = (EventPriv *) event_public;
193 event_remove (event);
197 autoremove_event_timeout_cb (EventPriv *event)
199 event->autoremove_timeout_id = 0;
200 event_remove (event);
205 display_notify_area (EmpathyEventManager *self)
207 EmpathyEventManagerPriv *priv = GET_PRIV (self);
209 return g_settings_get_boolean (priv->gsettings_ui,
210 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
214 event_manager_add (EmpathyEventManager *manager,
216 EmpathyContact *contact,
217 EmpathyEventType type,
218 const gchar *icon_name,
220 const gchar *message,
221 EventManagerApproval *approval,
225 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
228 event = g_slice_new0 (EventPriv);
229 event->public.account = account != NULL ? g_object_ref (account) : NULL;
230 event->public.contact = contact ? g_object_ref (contact) : NULL;
231 event->public.type = type;
232 event->public.icon_name = g_strdup (icon_name);
233 event->public.header = g_strdup (header);
234 event->public.message = g_strdup (message);
235 event->public.must_ack = (func != NULL);
236 if (approval != NULL)
237 event->public.handler_instance = approval->handler_instance;
238 event->inhibit = FALSE;
240 event->user_data = user_data;
241 event->manager = manager;
242 event->approval = approval;
244 DEBUG ("Adding event %p", event);
245 priv->events = g_slist_prepend (priv->events, event);
247 if (!display_notify_area (manager))
249 /* Don't fire the 'event-added' signal as we activate the event now */
250 if (approval != NULL)
251 approval->auto_approved = TRUE;
253 empathy_event_activate (&event->public);
257 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
259 if (!event->public.must_ack)
261 event->autoremove_timeout_id = g_timeout_add_seconds (
262 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
268 handle_with_cb (GObject *source,
269 GAsyncResult *result,
272 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
273 GError *error = NULL;
275 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
277 DEBUG ("HandleWith failed: %s\n", error->message);
278 g_error_free (error);
283 handle_with_time_cb (GObject *source,
284 GAsyncResult *result,
287 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
288 GError *error = NULL;
290 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
293 if (g_error_matches (error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED))
295 EventManagerApproval *approval = user_data;
297 DEBUG ("HandleWithTime() is not implemented, falling back to "
298 "HandleWith(). Please upgrade to telepathy-mission-control "
301 tp_channel_dispatch_operation_handle_with_async (approval->operation,
302 NULL, handle_with_cb, approval);
306 DEBUG ("HandleWithTime failed: %s\n", error->message);
308 g_error_free (error);
313 event_manager_approval_approve (EventManagerApproval *approval)
317 if (approval->auto_approved)
319 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
323 timestamp = empathy_get_current_action_time ();
326 g_assert (approval->operation != NULL);
328 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
329 NULL, timestamp, handle_with_time_cb, approval);
333 event_channel_process_func (EventPriv *event)
335 event_manager_approval_approve (event->approval);
339 event_text_channel_process_func (EventPriv *event)
341 EmpathyTpChat *tp_chat;
343 if (event->approval->handler != 0)
345 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
347 g_signal_handler_disconnect (tp_chat, event->approval->handler);
348 event->approval->handler = 0;
351 event_manager_approval_approve (event->approval);
355 event_lookup_by_approval (EmpathyEventManager *manager,
356 EventManagerApproval *approval)
358 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
360 EventPriv *retval = NULL;
362 for (l = priv->events; l; l = l->next)
364 EventPriv *event = l->data;
366 if (event->approval == approval)
377 event_update (EmpathyEventManager *manager, EventPriv *event,
378 const char *icon_name, const char *header, const char *msg)
380 g_free (event->public.icon_name);
381 g_free (event->public.header);
382 g_free (event->public.message);
384 event->public.icon_name = g_strdup (icon_name);
385 event->public.header = g_strdup (header);
386 event->public.message = g_strdup (msg);
388 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
392 reject_channel_claim_cb (GObject *source,
393 GAsyncResult *result,
396 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
397 GError *error = NULL;
399 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
401 DEBUG ("Failed to claim channel: %s", error->message);
403 g_error_free (error);
407 if (TP_IS_CALL_CHANNEL (user_data))
409 tp_call_channel_hangup_async (user_data,
410 TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
412 tp_channel_close_async (user_data, NULL, NULL);
414 else if (EMPATHY_IS_TP_CHAT (user_data))
416 empathy_tp_chat_leave (user_data, "");
418 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
420 tp_channel_close_async (user_data, NULL, NULL);
424 g_object_unref (user_data);
428 reject_approval (EventManagerApproval *approval)
430 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
432 /* We have to claim the channel before closing it */
434 /* Unfortunately, we need to special case the auth channels for the
435 * time being as they don't have a wrapper object handler in
436 * approval->handler_instance as they're not actually handled by
437 * this process, so we can just use a noddy callback to call Close()
439 if (approval->handler_instance != NULL)
441 tp_channel_dispatch_operation_claim_with_async (approval->operation,
442 priv->approver, reject_channel_claim_cb,
443 g_object_ref (approval->handler_instance));
445 else if (tp_channel_get_channel_type_id (approval->main_channel)
446 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
448 tp_channel_dispatch_operation_close_channels_async (approval->operation,
454 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
455 gint response, gpointer user_data)
457 EventManagerApproval *approval = user_data;
459 gtk_widget_destroy (approval->dialog);
460 approval->dialog = NULL;
462 if (response == GTK_RESPONSE_ACCEPT)
464 event_manager_approval_approve (approval);
466 else if (response == ACCEPT_WITHOUT_VIDEO)
468 empathy_call_channel_send_video (TP_CALL_CHANNEL (approval->main_channel),
470 event_manager_approval_approve (approval);
474 reject_approval (approval);
479 event_channel_process_voip_func (EventPriv *event)
486 EmpathyEventType etype = event->public.type;
488 if (event->approval->dialog != NULL)
490 gtk_window_present (GTK_WINDOW (event->approval->dialog));
494 if (etype == EMPATHY_EVENT_TYPE_CALL)
497 call = TP_CALL_CHANNEL (event->approval->handler_instance);
498 video = tp_call_channel_has_initial_video (call, NULL);
502 g_warning ("Unknown event type: %d", event->public.type);
506 dialog = gtk_message_dialog_new (NULL, 0,
507 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
508 video ? _("Incoming video call"): _("Incoming call"));
510 gtk_message_dialog_format_secondary_text (
511 GTK_MESSAGE_DIALOG (dialog), video ?
512 _("%s is video calling you. Do you want to answer?"):
513 _("%s is calling you. Do you want to answer?"),
514 empathy_contact_get_alias (event->approval->contact));
516 title = g_strdup_printf (_("Incoming call from %s"),
517 empathy_contact_get_alias (event->approval->contact));
519 gtk_window_set_title (GTK_WINDOW (dialog), title);
522 /* Set image of the dialog */
525 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
526 GTK_ICON_SIZE_DIALOG);
530 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
531 GTK_ICON_SIZE_DIALOG);
534 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
535 gtk_widget_show (image);
537 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
540 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
541 _("_Reject"), GTK_RESPONSE_REJECT);
542 image = gtk_image_new_from_icon_name ("call-stop",
543 GTK_ICON_SIZE_BUTTON);
544 gtk_button_set_image (GTK_BUTTON (button), image);
546 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
548 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
549 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
551 image = gtk_image_new_from_icon_name ("call-start",
552 GTK_ICON_SIZE_BUTTON);
553 gtk_button_set_image (GTK_BUTTON (button), image);
556 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
557 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
559 image = gtk_image_new_from_icon_name ("call-start",
560 GTK_ICON_SIZE_BUTTON);
561 gtk_button_set_image (GTK_BUTTON (button), image);
563 g_signal_connect (dialog, "response",
564 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
567 gtk_widget_show (dialog);
569 event->approval->dialog = dialog;
573 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
574 EmpathyMessage *message,
575 EventManagerApproval *approval)
577 EmpathyContact *sender;
581 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
583 /* try to update the event if it's referring to a chat which is already in the
585 event = event_lookup_by_approval (approval->manager, approval);
587 sender = empathy_message_get_sender (message);
589 /* We only want to show incoming messages */
590 if (empathy_contact_is_user (sender))
593 header = empathy_contact_get_alias (sender);
594 msg = empathy_message_get_body (message);
597 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
600 event_manager_add (approval->manager, NULL, sender,
601 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
602 approval, event_text_channel_process_func, NULL);
604 empathy_sound_manager_play (priv->sound_mgr, NULL,
605 EMPATHY_SOUND_CONVERSATION_NEW);
609 event_manager_approval_done (EventManagerApproval *approval)
611 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
614 if (approval->operation != NULL)
618 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
620 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
621 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
624 if (priv->ringing == 0)
625 empathy_sound_manager_stop (priv->sound_mgr,
626 EMPATHY_SOUND_PHONE_INCOMING);
630 priv->approvals = g_slist_remove (priv->approvals, approval);
632 for (l = priv->events; l; l = l->next)
634 EventPriv *event = l->data;
636 if (event->approval == approval)
638 event_remove (event);
643 event_manager_approval_free (approval);
647 cdo_invalidated_cb (TpProxy *cdo,
651 EventManagerApproval *approval)
653 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
655 event_manager_approval_done (approval);
659 event_manager_call_state_changed_cb (TpCallChannel *call,
662 TpCallStateReason *reason,
664 EventManagerApproval *approval)
666 if (state == TP_CALL_STATE_ENDED)
668 DEBUG ("Call ended, seems we missed it :/");
669 reject_approval (approval);
674 invite_dialog_response_cb (GtkDialog *dialog,
676 EventManagerApproval *approval)
678 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
679 approval->dialog = NULL;
681 if (response != GTK_RESPONSE_OK)
684 DEBUG ("Muc invitation rejected");
686 reject_approval (approval);
691 DEBUG ("Muc invitation accepted");
693 /* We'll join the room when handling the channel */
694 event_manager_approval_approve (approval);
698 event_room_channel_process_func (EventPriv *event)
700 GtkWidget *dialog, *button, *image;
701 TpChannel *channel = event->approval->main_channel;
704 if (event->approval->dialog != NULL)
706 gtk_window_present (GTK_WINDOW (event->approval->dialog));
711 dialog = gtk_message_dialog_new (NULL, 0,
712 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
714 title = g_strdup_printf (_("Invitation to join %s"),
715 tp_channel_get_identifier (channel));
717 gtk_window_set_title (GTK_WINDOW (dialog), title);
720 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
721 _("%s is inviting you to join %s"),
722 empathy_contact_get_alias (event->approval->contact),
723 tp_channel_get_identifier (channel));
725 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
728 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
729 _("_Decline"), GTK_RESPONSE_CANCEL);
730 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
731 gtk_button_set_image (GTK_BUTTON (button), image);
733 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
734 _("_Join"), GTK_RESPONSE_OK);
735 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
736 gtk_button_set_image (GTK_BUTTON (button), image);
738 g_signal_connect (dialog, "response",
739 G_CALLBACK (invite_dialog_response_cb), event->approval);
741 gtk_widget_show (dialog);
743 event->approval->dialog = dialog;
747 display_invite_room_dialog (EventManagerApproval *approval)
749 const gchar *invite_msg;
751 TpContact *self_contact;
752 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
754 self_contact = tp_channel_group_get_self_contact (approval->main_channel);
755 tp_channel_group_get_local_pending_contact_info (approval->main_channel,
756 self_contact, NULL, NULL, &invite_msg);
758 if (approval->contact != NULL)
760 msg = g_strdup_printf (_("%s invited you to join %s"),
761 empathy_contact_get_alias (approval->contact),
762 tp_channel_get_identifier (approval->main_channel));
766 msg = g_strdup_printf (_("You have been invited to join %s"),
767 tp_channel_get_identifier (approval->main_channel));
770 event_manager_add (approval->manager, NULL,
771 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
772 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
773 event_room_channel_process_func, NULL);
775 empathy_sound_manager_play (priv->sound_mgr, NULL,
776 EMPATHY_SOUND_CONVERSATION_NEW);
782 event_manager_auth_process_func (EventPriv *event)
784 empathy_event_approve ((EmpathyEvent *) event);
787 /* If there is a file-transfer, media, or auth channel consider it as
790 find_main_channel (GList *channels)
793 TpChannel *text = NULL;
795 for (l = channels; l != NULL; l = g_list_next (l))
797 TpChannel *channel = l->data;
800 if (tp_proxy_get_invalidated (channel) != NULL)
803 channel_type = tp_channel_get_channel_type_id (channel);
805 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
806 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
807 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
808 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
811 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
819 approve_text_channel (EmpathyEventManager *self,
820 EventManagerApproval *approval,
821 TpAddDispatchOperationContext *context,
822 EmpathyTpChat *tp_chat)
826 approval->handler_instance = g_object_ref (tp_chat);
828 if (tp_proxy_has_interface (tp_chat, TP_IFACE_CHANNEL_INTERFACE_GROUP))
830 /* Are we in local-pending ? */
833 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
835 /* We are invited to a room */
836 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
837 tp_channel_get_identifier (TP_CHANNEL_GROUP_CHANGE_REASON_NONE));
841 approval->contact = empathy_contact_dup_from_tp_contact (
845 display_invite_room_dialog (approval);
846 tp_add_dispatch_operation_context_accept (context);
851 /* We are not invited, approve the channel right now */
852 tp_add_dispatch_operation_context_accept (context);
854 approval->auto_approved = TRUE;
855 event_manager_approval_approve (approval);
859 /* 1-1 text channel, wait for the first message */
860 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
861 G_CALLBACK (event_manager_chat_message_received_cb), approval);
863 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
864 for (l = messages; l != NULL; l = g_list_next (l))
866 EmpathyMessage *msg = l->data;
868 event_manager_chat_message_received_cb (tp_chat, msg, approval);
871 tp_add_dispatch_operation_context_accept (context);
875 approval_set_target_contact (EventManagerApproval *approval,
880 contact = tp_channel_get_target_contact (channel);
881 approval->contact = empathy_contact_dup_from_tp_contact (contact);
885 approve_call_channel (EmpathyEventManager *self,
886 EventManagerApproval *approval,
887 TpAddDispatchOperationContext *context,
890 TpChannel *channel = TP_CHANNEL (call);
891 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
895 approval->handler_instance = g_object_ref (call);
896 approval_set_target_contact (approval, channel);
898 tp_add_dispatch_operation_context_accept (context);
900 if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
902 DEBUG ("Call already ended, seems we missed it :/");
903 reject_approval (approval);
907 approval->handler = g_signal_connect (call, "state-changed",
908 G_CALLBACK (event_manager_call_state_changed_cb), approval);
910 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
912 header = g_strdup_printf (
913 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
914 empathy_contact_get_alias (approval->contact));
916 event_manager_add (approval->manager, NULL,
917 approval->contact, EMPATHY_EVENT_TYPE_CALL,
918 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
919 header, NULL, approval,
920 event_channel_process_voip_func, NULL);
925 if (priv->ringing == 1)
926 empathy_sound_manager_start_playing (priv->sound_mgr, NULL,
927 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
931 approve_ft_channel (EmpathyEventManager *self,
932 EventManagerApproval *approval,
933 TpAddDispatchOperationContext *context,
934 TpFileTransferChannel *ft)
936 TpChannel *channel = TP_CHANNEL (ft);
937 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
940 approval->handler_instance = g_object_ref (ft);
941 approval_set_target_contact (approval, channel);
943 tp_add_dispatch_operation_context_accept (context);
945 header = g_strdup_printf (_("Incoming file transfer from %s"),
946 empathy_contact_get_alias (approval->contact));
948 event_manager_add (approval->manager, NULL,
949 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
950 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
951 approval, event_channel_process_func, NULL);
953 /* FIXME better sound for incoming file transfers ?*/
954 empathy_sound_manager_play (priv->sound_mgr, NULL,
955 EMPATHY_SOUND_CONVERSATION_NEW);
961 approve_sasl_channel (EmpathyEventManager *self,
963 EventManagerApproval *approval,
964 TpAddDispatchOperationContext *context,
967 if (empathy_sasl_channel_supports_mechanism (channel, "X-TELEPATHY-PASSWORD"))
969 event_manager_add (approval->manager, account, NULL,
970 EMPATHY_EVENT_TYPE_AUTH,
971 GTK_STOCK_DIALOG_AUTHENTICATION,
972 tp_account_get_display_name (account),
973 _("Password required"), approval,
974 event_manager_auth_process_func, NULL);
978 GError error = { TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
979 "Support only X-TELEPATHY-PASSWORD auth method" };
981 tp_add_dispatch_operation_context_fail (context, &error);
985 tp_add_dispatch_operation_context_accept (context);
989 approve_channels (TpSimpleApprover *approver,
991 TpConnection *connection,
993 TpChannelDispatchOperation *dispatch_operation,
994 TpAddDispatchOperationContext *context,
997 EmpathyEventManager *self = user_data;
998 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1000 EventManagerApproval *approval;
1001 GQuark channel_type;
1003 channel = find_main_channel (channels);
1004 if (channel == NULL)
1006 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1007 "Unknown channel type" };
1009 DEBUG ("Failed to find the main channel; ignoring");
1011 tp_add_dispatch_operation_context_fail (context, &error);
1015 approval = event_manager_approval_new (self, dispatch_operation, channel);
1016 priv->approvals = g_slist_prepend (priv->approvals, approval);
1018 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1019 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1021 channel_type = tp_channel_get_channel_type_id (channel);
1023 if (EMPATHY_IS_TP_CHAT (channel))
1025 approve_text_channel (self, approval, context, EMPATHY_TP_CHAT (channel));
1027 else if (TP_IS_CALL_CHANNEL (channel))
1029 approve_call_channel (self, approval, context, TP_CALL_CHANNEL (channel));
1031 else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
1033 approve_ft_channel (self, approval, context,
1034 TP_FILE_TRANSFER_CHANNEL (channel));
1036 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1038 approve_sasl_channel (self, account, approval, context, channel);
1042 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
1043 "Invalid channel type" };
1045 DEBUG ("Unknown channel type (%s), ignoring..",
1046 g_quark_to_string (channel_type));
1048 tp_add_dispatch_operation_context_fail (context, &error);
1054 event_pending_subscribe_func (EventPriv *event)
1057 FolksIndividual *individual;
1059 individual = empathy_ensure_individual_from_tp_contact (
1060 empathy_contact_get_tp_contact (event->public.contact));
1062 dialog = empathy_subscription_dialog_new (individual, event->public.message);
1063 gtk_window_present (GTK_WINDOW (dialog));
1065 event_remove (event);
1067 g_object_unref (individual);
1071 check_publish_state (EmpathyEventManager *self,
1072 TpContact *tp_contact)
1074 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1075 gchar *header, *event_msg;
1076 TpSubscriptionState state;
1077 EmpathyContact *contact;
1078 const gchar *message;
1080 state = tp_contact_get_publish_state (tp_contact);
1082 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1084 if (state != TP_SUBSCRIPTION_STATE_ASK)
1088 for (l = priv->events; l; l = l->next)
1090 EventPriv *event = l->data;
1092 if (event->public.contact == contact &&
1093 event->func == event_pending_subscribe_func)
1095 event_remove (event);
1103 header = g_strdup_printf (
1104 _("%s would like permission to see when you are online"),
1105 empathy_contact_get_alias (contact));
1107 message = tp_contact_get_publish_request (tp_contact);
1109 if (!EMP_STR_EMPTY (message))
1110 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1114 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1115 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1116 event_pending_subscribe_func, NULL);
1122 g_object_unref (contact);
1126 event_manager_publish_state_changed_cb (TpContact *contact,
1128 EmpathyEventManager *self)
1130 check_publish_state (self, contact);
1134 event_manager_presence_changed_cb (EmpathyContact *contact,
1135 TpConnectionPresenceType current,
1136 TpConnectionPresenceType previous,
1137 EmpathyEventManager *manager)
1139 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1141 EmpathyPresenceManager *presence_mgr;
1143 account = empathy_contact_get_account (contact);
1144 presence_mgr = empathy_presence_manager_dup_singleton ();
1146 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1149 if (tp_connection_presence_type_cmp_availability (previous,
1150 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1152 /* contact was online */
1153 if (tp_connection_presence_type_cmp_availability (current,
1154 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1156 /* someone is logging off */
1157 empathy_sound_manager_play (priv->sound_mgr, NULL,
1158 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1160 if (g_settings_get_boolean (priv->gsettings_notif,
1161 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1163 event_manager_add (manager, NULL, contact,
1164 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1165 EMPATHY_IMAGE_AVATAR_DEFAULT,
1166 empathy_contact_get_alias (contact), _("Disconnected"),
1173 /* contact was offline */
1174 if (tp_connection_presence_type_cmp_availability (current,
1175 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1177 /* someone is logging in */
1178 empathy_sound_manager_play (priv->sound_mgr, NULL,
1179 EMPATHY_SOUND_CONTACT_CONNECTED);
1181 if (g_settings_get_boolean (priv->gsettings_notif,
1182 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1184 event_manager_add (manager, NULL, contact,
1185 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1186 EMPATHY_IMAGE_AVATAR_DEFAULT,
1187 empathy_contact_get_alias (contact), _("Connected"),
1194 g_object_unref (presence_mgr);
1198 event_manager_constructor (GType type,
1200 GObjectConstructParam *props)
1204 if (manager_singleton) {
1205 retval = g_object_ref (manager_singleton);
1207 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1208 (type, n_props, props);
1210 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1211 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1218 event_manager_finalize (GObject *object)
1220 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1222 if (priv->ringing > 0)
1223 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1225 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1226 g_slist_free (priv->events);
1227 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1228 g_slist_free (priv->approvals);
1229 g_object_unref (priv->conn_aggregator);
1230 g_object_unref (priv->approver);
1231 g_object_unref (priv->auth_approver);
1232 g_object_unref (priv->gsettings_notif);
1233 g_object_unref (priv->gsettings_ui);
1234 g_object_unref (priv->sound_mgr);
1235 g_hash_table_unref (priv->contacts);
1239 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1241 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1243 object_class->finalize = event_manager_finalize;
1244 object_class->constructor = event_manager_constructor;
1246 signals[EVENT_ADDED] =
1247 g_signal_new ("event-added",
1248 G_TYPE_FROM_CLASS (klass),
1252 g_cclosure_marshal_generic,
1256 signals[EVENT_REMOVED] =
1257 g_signal_new ("event-removed",
1258 G_TYPE_FROM_CLASS (klass),
1262 g_cclosure_marshal_generic,
1263 G_TYPE_NONE, 1, G_TYPE_POINTER);
1265 signals[EVENT_UPDATED] =
1266 g_signal_new ("event-updated",
1267 G_TYPE_FROM_CLASS (klass),
1271 g_cclosure_marshal_generic,
1272 G_TYPE_NONE, 1, G_TYPE_POINTER);
1274 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1278 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1281 EmpathyEventManager *self)
1283 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1286 for (i = 0; i < added->len; i++)
1288 TpContact *tp_contact = g_ptr_array_index (added, i);
1289 EmpathyContact *contact;
1291 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1294 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1296 tp_g_signal_connect_object (contact, "presence-changed",
1297 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1299 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1300 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1302 check_publish_state (self, tp_contact);
1304 /* Pass ownership to the hash table */
1305 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1308 for (i = 0; i < removed->len; i++)
1310 TpContact *tp_contact = g_ptr_array_index (removed, i);
1311 EmpathyContact *contact;
1313 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1314 if (contact == NULL)
1317 g_signal_handlers_disconnect_by_func (contact,
1318 event_manager_presence_changed_cb, self);
1320 g_signal_handlers_disconnect_by_func (tp_contact,
1321 event_manager_publish_state_changed_cb, self);
1323 g_hash_table_remove (priv->contacts, tp_contact);
1328 empathy_event_manager_init (EmpathyEventManager *manager)
1330 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1331 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1332 GError *error = NULL;
1333 TpAccountManager *am;
1334 GPtrArray *contacts, *empty;
1336 manager->priv = priv;
1338 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1339 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1341 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1343 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1346 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1348 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1349 G_CALLBACK (contact_list_changed_cb), manager, 0);
1351 contacts = empathy_connection_aggregator_dup_all_contacts (
1352 priv->conn_aggregator);
1354 empty = g_ptr_array_new ();
1356 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1358 g_ptr_array_unref (contacts);
1359 g_ptr_array_unref (empty);
1361 am = tp_account_manager_dup ();
1363 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1364 FALSE, approve_channels, manager, NULL);
1366 /* Private text channels */
1367 tp_base_client_take_approver_filter (priv->approver,
1369 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1370 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1373 /* Muc text channels */
1374 tp_base_client_take_approver_filter (priv->approver,
1376 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1377 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1381 tp_base_client_take_approver_filter (priv->approver,
1383 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1384 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1385 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_STREAMED_MEDIA,
1393 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1395 tp_base_client_take_approver_filter (priv->approver,
1397 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1398 TP_IFACE_CHANNEL_TYPE_CALL,
1399 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1402 /* I don't feel good about doing this, and I'm sorry, but the
1403 * capabilities connection feature is added earlier because it's
1404 * needed for EmpathyTpChat. If the capabilities feature is required
1405 * then preparing an auth channel (which of course appears in the
1406 * CONNECTING state) will never be prepared. So the options are
1407 * either to create another approver like I've done, or to port
1408 * EmpathyTpChat and its users to not depend on the connection being
1409 * prepared with capabilities. I chose the former, obviously. :-) */
1411 priv->auth_approver = tp_simple_approver_new_with_am (am,
1412 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1415 /* SASL auth channels */
1416 tp_base_client_take_approver_filter (priv->auth_approver,
1418 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1419 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1420 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1422 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1425 if (!tp_base_client_register (priv->approver, &error))
1427 DEBUG ("Failed to register Approver: %s", error->message);
1428 g_error_free (error);
1431 if (!tp_base_client_register (priv->auth_approver, &error))
1433 DEBUG ("Failed to register auth Approver: %s", error->message);
1434 g_error_free (error);
1437 g_object_unref (am);
1440 EmpathyEventManager *
1441 empathy_event_manager_dup_singleton (void)
1443 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1447 empathy_event_manager_get_events (EmpathyEventManager *manager)
1449 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1451 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1453 return priv->events;
1457 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1459 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1461 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1463 return priv->events ? priv->events->data : NULL;
1467 empathy_event_activate (EmpathyEvent *event_public)
1469 EventPriv *event = (EventPriv *) event_public;
1471 g_return_if_fail (event_public != NULL);
1474 event->func (event);
1476 event_remove (event);
1480 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1482 EventPriv *event = (EventPriv *) event_public;
1484 g_return_if_fail (event_public != NULL);
1486 event->inhibit = TRUE;
1490 empathy_event_approve (EmpathyEvent *event_public)
1492 EventPriv *event = (EventPriv *) event_public;
1494 g_return_if_fail (event_public != NULL);
1496 event_manager_approval_approve (event->approval);
1500 empathy_event_decline (EmpathyEvent *event_public)
1502 EventPriv *event = (EventPriv *) event_public;
1504 g_return_if_fail (event_public != NULL);
1506 reject_approval (event->approval);