2 * Copyright (C) 2007-2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
25 #include <glib/gi18n.h>
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/simple-approver.h>
32 #include <libempathy/empathy-presence-manager.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-streamed-media.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-gsettings.h>
41 #include <extensions/extensions.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-contact-dialogs.h>
45 #include <libempathy-gtk/empathy-sound-manager.h>
47 #include "empathy-event-manager.h"
48 #include "empathy-main-window.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
51 #include <libempathy/empathy-debug.h>
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
55 #define NOTIFICATION_TIMEOUT 2 /* seconds */
57 /* The time interval in milliseconds between 2 incoming rings */
58 #define MS_BETWEEN_RING 500
61 EmpathyEventManager *manager;
62 TpChannelDispatchOperation *operation;
63 gulong invalidated_handler;
64 /* Remove contact if applicable */
65 EmpathyContact *contact;
66 /* option signal handler and it's instance */
68 GObject *handler_instance;
69 /* optional accept widget */
71 /* Channel of the CDO that will be used during the approval */
72 TpChannel *main_channel;
73 gboolean auto_approved;
74 } EventManagerApproval;
77 TpBaseClient *approver;
78 TpBaseClient *auth_approver;
79 EmpathyContactManager *contact_manager;
81 /* Ongoing approvals */
86 GSettings *gsettings_notif;
87 GSettings *gsettings_ui;
89 EmpathySoundManager *sound_mgr;
90 } EmpathyEventManagerPriv;
92 typedef struct _EventPriv EventPriv;
93 typedef void (*EventFunc) (EventPriv *event);
97 EmpathyEventManager *manager;
98 EventManagerApproval *approval;
102 guint autoremove_timeout_id;
112 static guint signals[LAST_SIGNAL];
114 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
116 static EmpathyEventManager * manager_singleton = NULL;
118 static EventManagerApproval *
119 event_manager_approval_new (EmpathyEventManager *manager,
120 TpChannelDispatchOperation *operation,
121 TpChannel *main_channel)
123 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
124 result->operation = g_object_ref (operation);
125 result->manager = manager;
126 result->main_channel = g_object_ref (main_channel);
132 event_manager_approval_free (EventManagerApproval *approval)
134 g_signal_handler_disconnect (approval->operation,
135 approval->invalidated_handler);
136 g_object_unref (approval->operation);
138 g_object_unref (approval->main_channel);
140 if (approval->handler != 0)
141 g_signal_handler_disconnect (approval->handler_instance,
144 if (approval->handler_instance != NULL)
145 g_object_unref (approval->handler_instance);
147 if (approval->contact != NULL)
148 g_object_unref (approval->contact);
150 if (approval->dialog != NULL)
152 gtk_widget_destroy (approval->dialog);
155 g_slice_free (EventManagerApproval, approval);
159 event_free (EventPriv *event)
161 g_free (event->public.icon_name);
162 g_free (event->public.header);
163 g_free (event->public.message);
165 if (event->autoremove_timeout_id != 0)
166 g_source_remove (event->autoremove_timeout_id);
168 tp_clear_object (&(event->public.contact));
169 tp_clear_object (&(event->public.account));
171 g_slice_free (EventPriv, event);
175 event_remove (EventPriv *event)
177 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
179 DEBUG ("Removing event %p", event);
181 priv->events = g_slist_remove (priv->events, event);
182 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
187 empathy_event_remove (EmpathyEvent *event_public)
189 EventPriv *event = (EventPriv *) event_public;
191 event_remove (event);
195 autoremove_event_timeout_cb (EventPriv *event)
197 event->autoremove_timeout_id = 0;
198 event_remove (event);
203 display_notify_area (EmpathyEventManager *self)
205 EmpathyEventManagerPriv *priv = GET_PRIV (self);
207 return g_settings_get_boolean (priv->gsettings_ui,
208 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
212 event_manager_add (EmpathyEventManager *manager,
214 EmpathyContact *contact,
215 EmpathyEventType type,
216 const gchar *icon_name,
218 const gchar *message,
219 EventManagerApproval *approval,
223 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
226 event = g_slice_new0 (EventPriv);
227 event->public.account = account != NULL ? g_object_ref (account) : NULL;
228 event->public.contact = contact ? g_object_ref (contact) : NULL;
229 event->public.type = type;
230 event->public.icon_name = g_strdup (icon_name);
231 event->public.header = g_strdup (header);
232 event->public.message = g_strdup (message);
233 event->public.must_ack = (func != NULL);
234 event->inhibit = FALSE;
236 event->user_data = user_data;
237 event->manager = manager;
238 event->approval = approval;
240 DEBUG ("Adding event %p", event);
241 priv->events = g_slist_prepend (priv->events, event);
243 if (!display_notify_area (manager))
245 /* Don't fire the 'event-added' signal as we activate the event now */
246 if (approval != NULL)
247 approval->auto_approved = TRUE;
249 empathy_event_activate (&event->public);
253 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
255 if (!event->public.must_ack)
257 event->autoremove_timeout_id = g_timeout_add_seconds (
258 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
264 handle_with_cb (GObject *source,
265 GAsyncResult *result,
268 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
269 GError *error = NULL;
271 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
273 DEBUG ("HandleWith failed: %s\n", error->message);
274 g_error_free (error);
279 handle_with_time_cb (GObject *source,
280 GAsyncResult *result,
283 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
284 GError *error = NULL;
286 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
289 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
291 EventManagerApproval *approval = user_data;
293 DEBUG ("HandleWithTime() is not implemented, falling back to "
294 "HandleWith(). Please upgrade to telepathy-mission-control "
297 tp_channel_dispatch_operation_handle_with_async (approval->operation,
298 NULL, handle_with_cb, approval);
302 DEBUG ("HandleWithTime failed: %s\n", error->message);
304 g_error_free (error);
309 event_manager_approval_approve (EventManagerApproval *approval)
313 if (approval->auto_approved)
315 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
319 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
322 g_assert (approval->operation != NULL);
324 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
325 NULL, timestamp, handle_with_time_cb, approval);
329 event_channel_process_func (EventPriv *event)
331 event_manager_approval_approve (event->approval);
335 event_text_channel_process_func (EventPriv *event)
337 EmpathyTpChat *tp_chat;
340 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
342 if (event->approval->handler != 0)
344 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
346 g_signal_handler_disconnect (tp_chat, event->approval->handler);
347 event->approval->handler = 0;
350 event_manager_approval_approve (event->approval);
354 event_lookup_by_approval (EmpathyEventManager *manager,
355 EventManagerApproval *approval)
357 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
359 EventPriv *retval = NULL;
361 for (l = priv->events; l; l = l->next)
363 EventPriv *event = l->data;
365 if (event->approval == approval)
376 event_update (EmpathyEventManager *manager, EventPriv *event,
377 const char *icon_name, const char *header, const char *msg)
379 g_free (event->public.icon_name);
380 g_free (event->public.header);
381 g_free (event->public.message);
383 event->public.icon_name = g_strdup (icon_name);
384 event->public.header = g_strdup (header);
385 event->public.message = g_strdup (msg);
387 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
391 reject_channel_claim_cb (GObject *source,
392 GAsyncResult *result,
395 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
396 GError *error = NULL;
398 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
400 DEBUG ("Failed to claim channel: %s", error->message);
402 g_error_free (error);
406 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
408 empathy_tp_streamed_media_close (user_data);
410 else if (EMPATHY_IS_TP_CHAT (user_data))
412 empathy_tp_chat_leave (user_data, "");
414 else if (EMPATHY_IS_TP_FILE (user_data))
416 empathy_tp_file_close (user_data);
420 g_object_unref (user_data);
424 reject_auth_channel_claim_cb (GObject *source,
425 GAsyncResult *result,
428 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
429 GError *error = NULL;
431 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
433 DEBUG ("Failed to claim channel: %s", error->message);
435 g_error_free (error);
439 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
440 NULL, NULL, NULL, NULL);
444 reject_approval (EventManagerApproval *approval)
446 /* We have to claim the channel before closing it */
448 /* Unfortunately, we need to special case the auth channels for the
449 * time being as they don't have a wrapper object handler in
450 * approval->handler_instance as they're not actually handled by
451 * this process, so we can just use a noddy callback to call Close()
453 if (approval->handler_instance != NULL)
455 tp_channel_dispatch_operation_claim_async (approval->operation,
456 reject_channel_claim_cb, g_object_ref (approval->handler_instance));
458 else if (tp_channel_get_channel_type_id (approval->main_channel)
459 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
461 tp_channel_dispatch_operation_claim_async (approval->operation,
462 reject_auth_channel_claim_cb, approval->main_channel);
467 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
468 gint response, gpointer user_data)
470 EventManagerApproval *approval = user_data;
472 gtk_widget_destroy (approval->dialog);
473 approval->dialog = NULL;
475 if (response != GTK_RESPONSE_ACCEPT)
477 reject_approval (approval);
481 event_manager_approval_approve (approval);
486 event_channel_process_voip_func (EventPriv *event)
491 EmpathyTpStreamedMedia *call;
495 if (event->approval->dialog != NULL)
497 gtk_window_present (GTK_WINDOW (event->approval->dialog));
501 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
503 video = empathy_tp_streamed_media_has_initial_video (call);
505 dialog = gtk_message_dialog_new (NULL, 0,
506 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
507 video ? _("Incoming video call"): _("Incoming call"));
509 gtk_message_dialog_format_secondary_text (
510 GTK_MESSAGE_DIALOG (dialog), video ?
511 _("%s is video calling you. Do you want to answer?"):
512 _("%s is calling you. Do you want to answer?"),
513 empathy_contact_get_alias (event->approval->contact));
515 title = g_strdup_printf (_("Incoming call from %s"),
516 empathy_contact_get_alias (event->approval->contact));
518 gtk_window_set_title (GTK_WINDOW (dialog), title);
521 /* Set image of the dialog */
524 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
525 GTK_ICON_SIZE_DIALOG);
529 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
530 GTK_ICON_SIZE_DIALOG);
533 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
534 gtk_widget_show (image);
536 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
539 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
540 _("_Reject"), GTK_RESPONSE_REJECT);
541 image = gtk_image_new_from_icon_name ("call-stop",
542 GTK_ICON_SIZE_BUTTON);
543 gtk_button_set_image (GTK_BUTTON (button), image);
545 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
546 _("_Answer"), GTK_RESPONSE_ACCEPT);
548 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
549 gtk_button_set_image (GTK_BUTTON (button), image);
551 g_signal_connect (dialog, "response",
552 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
555 gtk_widget_show (dialog);
557 event->approval->dialog = dialog;
561 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
562 EmpathyMessage *message,
563 EventManagerApproval *approval)
566 EmpathyContact *sender;
571 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
573 /* try to update the event if it's referring to a chat which is already in the
575 event = event_lookup_by_approval (approval->manager, approval);
577 sender = empathy_message_get_sender (message);
579 /* We only want to show incoming messages */
580 if (empathy_contact_is_user (sender))
583 header = empathy_contact_get_alias (sender);
584 msg = empathy_message_get_body (message);
586 channel = empathy_tp_chat_get_channel (tp_chat);
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 window = empathy_main_window_dup ();
598 empathy_sound_manager_play (priv->sound_mgr, window,
599 EMPATHY_SOUND_CONVERSATION_NEW);
601 g_object_unref (window);
605 event_manager_approval_done (EventManagerApproval *approval)
607 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
610 if (approval->operation != NULL)
614 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
616 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
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_media_channel_got_contact (EventManagerApproval *approval)
656 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
657 GtkWidget *window = empathy_main_window_dup ();
659 EmpathyTpStreamedMedia *call;
662 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
664 video = empathy_tp_streamed_media_has_initial_video (call);
666 header = g_strdup_printf (
667 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
668 empathy_contact_get_alias (approval->contact));
670 event_manager_add (approval->manager, NULL,
671 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
672 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
673 header, NULL, approval,
674 event_channel_process_voip_func, NULL);
679 if (priv->ringing == 1)
680 empathy_sound_manager_start_playing (priv->sound_mgr, window,
681 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
683 g_object_unref (window);
687 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
688 GParamSpec *param, EventManagerApproval *approval)
690 EmpathyContact *contact;
692 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
697 approval->contact = contact;
698 event_manager_media_channel_got_contact (approval);
702 invite_dialog_response_cb (GtkDialog *dialog,
704 EventManagerApproval *approval)
706 EmpathyTpChat *tp_chat;
708 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
709 approval->dialog = NULL;
711 tp_chat = EMPATHY_TP_CHAT (approval->handler_instance);
713 if (response != GTK_RESPONSE_OK)
716 DEBUG ("Muc invitation rejected");
718 reject_approval (approval);
723 DEBUG ("Muc invitation accepted");
725 /* We'll join the room when handling the channel */
726 event_manager_approval_approve (approval);
730 event_room_channel_process_func (EventPriv *event)
732 GtkWidget *dialog, *button, *image;
733 TpChannel *channel = event->approval->main_channel;
736 if (event->approval->dialog != NULL)
738 gtk_window_present (GTK_WINDOW (event->approval->dialog));
743 dialog = gtk_message_dialog_new (NULL, 0,
744 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
746 title = g_strdup_printf (_("Invitation to join %s"),
747 tp_channel_get_identifier (channel));
749 gtk_window_set_title (GTK_WINDOW (dialog), title);
752 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
753 _("%s is inviting you to join %s"),
754 empathy_contact_get_alias (event->approval->contact),
755 tp_channel_get_identifier (channel));
757 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
760 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
761 _("_Decline"), GTK_RESPONSE_CANCEL);
762 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
763 gtk_button_set_image (GTK_BUTTON (button), image);
765 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
766 _("_Join"), GTK_RESPONSE_OK);
767 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
768 gtk_button_set_image (GTK_BUTTON (button), image);
770 g_signal_connect (dialog, "response",
771 G_CALLBACK (invite_dialog_response_cb), event->approval);
773 gtk_widget_show (dialog);
775 event->approval->dialog = dialog;
779 display_invite_room_dialog (EventManagerApproval *approval)
781 GtkWidget *window = empathy_main_window_dup ();
782 const gchar *invite_msg;
784 TpHandle self_handle;
785 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
787 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
788 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
789 NULL, NULL, &invite_msg);
791 if (approval->contact != NULL)
793 msg = g_strdup_printf (_("%s invited you to join %s"),
794 empathy_contact_get_alias (approval->contact),
795 tp_channel_get_identifier (approval->main_channel));
799 msg = g_strdup_printf (_("You have been invited to join %s"),
800 tp_channel_get_identifier (approval->main_channel));
803 event_manager_add (approval->manager, NULL,
804 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
805 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
806 event_room_channel_process_func, NULL);
808 empathy_sound_manager_play (priv->sound_mgr, window,
809 EMPATHY_SOUND_CONVERSATION_NEW);
812 g_object_unref (window);
816 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
817 EmpathyContact *contact,
822 EventManagerApproval *approval = (EventManagerApproval *) user_data;
826 DEBUG ("Error: %s", error->message);
830 approval->contact = g_object_ref (contact);
833 display_invite_room_dialog (approval);
837 event_manager_ft_got_contact_cb (TpConnection *connection,
838 EmpathyContact *contact,
843 EventManagerApproval *approval = (EventManagerApproval *) user_data;
844 GtkWidget *window = empathy_main_window_dup ();
846 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
848 approval->contact = g_object_ref (contact);
850 header = g_strdup_printf (_("Incoming file transfer from %s"),
851 empathy_contact_get_alias (approval->contact));
853 event_manager_add (approval->manager, NULL,
854 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
855 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
856 approval, event_channel_process_func, NULL);
858 /* FIXME better sound for incoming file transfers ?*/
859 empathy_sound_manager_play (priv->sound_mgr, window,
860 EMPATHY_SOUND_CONVERSATION_NEW);
863 g_object_unref (window);
867 event_manager_auth_process_func (EventPriv *event)
869 empathy_event_approve ((EmpathyEvent *) event);
872 /* If there is a file-transfer, media, or auth channel consider it as
875 find_main_channel (GList *channels)
878 TpChannel *text = NULL;
880 for (l = channels; l != NULL; l = g_list_next (l))
882 TpChannel *channel = l->data;
885 if (tp_proxy_get_invalidated (channel) != NULL)
888 channel_type = tp_channel_get_channel_type_id (channel);
890 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
891 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
892 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
895 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
903 approve_channels (TpSimpleApprover *approver,
905 TpConnection *connection,
907 TpChannelDispatchOperation *dispatch_operation,
908 TpAddDispatchOperationContext *context,
911 EmpathyEventManager *self = user_data;
912 EmpathyEventManagerPriv *priv = GET_PRIV (self);
914 EventManagerApproval *approval;
917 channel = find_main_channel (channels);
920 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
921 "Unknown channel type" };
923 DEBUG ("Failed to find the main channel; ignoring");
925 tp_add_dispatch_operation_context_fail (context, &error);
929 approval = event_manager_approval_new (self, dispatch_operation, channel);
930 priv->approvals = g_slist_prepend (priv->approvals, approval);
932 approval->invalidated_handler = g_signal_connect (dispatch_operation,
933 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
935 channel_type = tp_channel_get_channel_type_id (channel);
937 if (TP_IS_TEXT_CHANNEL (channel))
939 EmpathyTpChat *tp_chat;
941 tp_chat = empathy_tp_chat_new (account, channel);
942 approval->handler_instance = G_OBJECT (tp_chat);
944 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
946 /* Are we in local-pending ? */
949 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
951 /* We are invited to a room */
952 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
953 tp_channel_get_identifier (channel));
957 empathy_tp_contact_factory_get_from_handle (connection,
958 inviter, event_manager_muc_invite_got_contact_cb,
959 approval, NULL, G_OBJECT (self));
963 display_invite_room_dialog (approval);
969 /* We are not invited, approve the channel right now */
970 tp_add_dispatch_operation_context_accept (context);
972 approval->auto_approved = TRUE;
973 event_manager_approval_approve (approval);
977 /* 1-1 text channel, wait for the first message */
978 approval->handler = g_signal_connect (tp_chat, "message-received",
979 G_CALLBACK (event_manager_chat_message_received_cb), approval);
981 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
983 EmpathyContact *contact;
984 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account, channel);
986 approval->handler_instance = G_OBJECT (call);
988 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
992 g_signal_connect (call, "notify::contact",
993 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
998 approval->contact = contact;
999 event_manager_media_channel_got_contact (approval);
1003 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1006 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1008 approval->handler_instance = G_OBJECT (tp_file);
1010 handle = tp_channel_get_handle (channel, NULL);
1012 connection = tp_channel_borrow_connection (channel);
1013 empathy_tp_contact_factory_get_from_handle (connection, handle,
1014 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1016 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1018 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1019 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1020 _("Password required"), approval,
1021 event_manager_auth_process_func, NULL);
1025 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1026 "Invalid channel type" };
1028 DEBUG ("Unknown channel type (%s), ignoring..",
1029 g_quark_to_string (channel_type));
1031 tp_add_dispatch_operation_context_fail (context, &error);
1036 tp_add_dispatch_operation_context_accept (context);
1040 event_pending_subscribe_func (EventPriv *event)
1042 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1044 event_remove (event);
1048 event_manager_pendings_changed_cb (EmpathyContactList *list,
1049 EmpathyContact *contact, EmpathyContact *actor,
1050 guint reason, gchar *message, gboolean is_pending,
1051 EmpathyEventManager *manager)
1053 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1054 gchar *header, *event_msg;
1060 for (l = priv->events; l; l = l->next)
1062 EventPriv *event = l->data;
1064 if (event->public.contact == contact &&
1065 event->func == event_pending_subscribe_func)
1067 event_remove (event);
1075 header = g_strdup_printf (
1076 _("%s would like permission to see when you are online"),
1077 empathy_contact_get_alias (contact));
1079 if (!EMP_STR_EMPTY (message))
1080 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1084 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1085 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1086 event_pending_subscribe_func, NULL);
1093 event_manager_presence_changed_cb (EmpathyContact *contact,
1094 TpConnectionPresenceType current,
1095 TpConnectionPresenceType previous,
1096 EmpathyEventManager *manager)
1098 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1100 EmpathyPresenceManager *presence_mgr;
1101 GtkWidget *window = empathy_main_window_dup ();
1103 account = empathy_contact_get_account (contact);
1104 presence_mgr = empathy_presence_manager_dup_singleton ();
1106 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1109 if (tp_connection_presence_type_cmp_availability (previous,
1110 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1112 /* contact was online */
1113 if (tp_connection_presence_type_cmp_availability (current,
1114 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1116 /* someone is logging off */
1117 empathy_sound_manager_play (priv->sound_mgr, window,
1118 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1120 if (g_settings_get_boolean (priv->gsettings_notif,
1121 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1123 event_manager_add (manager, NULL, contact,
1124 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1125 EMPATHY_IMAGE_AVATAR_DEFAULT,
1126 empathy_contact_get_alias (contact), _("Disconnected"),
1133 /* contact was offline */
1134 if (tp_connection_presence_type_cmp_availability (current,
1135 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1137 /* someone is logging in */
1138 empathy_sound_manager_play (priv->sound_mgr, window,
1139 EMPATHY_SOUND_CONTACT_CONNECTED);
1141 if (g_settings_get_boolean (priv->gsettings_notif,
1142 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1144 event_manager_add (manager, NULL, contact,
1145 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1146 EMPATHY_IMAGE_AVATAR_DEFAULT,
1147 empathy_contact_get_alias (contact), _("Connected"),
1154 g_object_unref (presence_mgr);
1155 g_object_unref (window);
1159 event_manager_members_changed_cb (EmpathyContactList *list,
1160 EmpathyContact *contact,
1161 EmpathyContact *actor,
1165 EmpathyEventManager *manager)
1168 g_signal_connect (contact, "presence-changed",
1169 G_CALLBACK (event_manager_presence_changed_cb), manager);
1171 g_signal_handlers_disconnect_by_func (contact,
1172 event_manager_presence_changed_cb, manager);
1176 event_manager_constructor (GType type,
1178 GObjectConstructParam *props)
1182 if (manager_singleton) {
1183 retval = g_object_ref (manager_singleton);
1185 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1186 (type, n_props, props);
1188 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1189 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1196 event_manager_finalize (GObject *object)
1198 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1200 if (priv->ringing > 0)
1201 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1203 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1204 g_slist_free (priv->events);
1205 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1206 g_slist_free (priv->approvals);
1207 g_object_unref (priv->contact_manager);
1208 g_object_unref (priv->approver);
1209 g_object_unref (priv->auth_approver);
1210 g_object_unref (priv->gsettings_notif);
1211 g_object_unref (priv->gsettings_ui);
1212 g_object_unref (priv->sound_mgr);
1216 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1218 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1220 object_class->finalize = event_manager_finalize;
1221 object_class->constructor = event_manager_constructor;
1223 signals[EVENT_ADDED] =
1224 g_signal_new ("event-added",
1225 G_TYPE_FROM_CLASS (klass),
1229 g_cclosure_marshal_VOID__POINTER,
1233 signals[EVENT_REMOVED] =
1234 g_signal_new ("event-removed",
1235 G_TYPE_FROM_CLASS (klass),
1239 g_cclosure_marshal_VOID__POINTER,
1240 G_TYPE_NONE, 1, G_TYPE_POINTER);
1242 signals[EVENT_UPDATED] =
1243 g_signal_new ("event-updated",
1244 G_TYPE_FROM_CLASS (klass),
1248 g_cclosure_marshal_VOID__POINTER,
1249 G_TYPE_NONE, 1, G_TYPE_POINTER);
1251 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1255 empathy_event_manager_init (EmpathyEventManager *manager)
1257 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1258 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1260 GError *error = NULL;
1262 manager->priv = priv;
1264 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1265 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1267 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1269 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1270 g_signal_connect (priv->contact_manager, "pendings-changed",
1271 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1273 g_signal_connect (priv->contact_manager, "members-changed",
1274 G_CALLBACK (event_manager_members_changed_cb), manager);
1276 dbus = tp_dbus_daemon_dup (&error);
1279 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1280 g_error_free (error);
1284 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1285 approve_channels, manager, NULL);
1287 /* EmpathyTpChat relies on this feature being prepared */
1288 tp_base_client_add_connection_features_varargs (priv->approver,
1289 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1291 /* Private text channels */
1292 tp_base_client_take_approver_filter (priv->approver,
1294 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1295 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1298 /* Muc text channels */
1299 tp_base_client_take_approver_filter (priv->approver,
1301 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1302 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1306 tp_base_client_take_approver_filter (priv->approver,
1308 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1309 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1310 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1314 tp_base_client_take_approver_filter (priv->approver,
1316 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1317 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1318 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1321 /* I don't feel good about doing this, and I'm sorry, but the
1322 * capabilities connection feature is added earlier because it's
1323 * needed for EmpathyTpChat. If the capabilities feature is required
1324 * then preparing an auth channel (which of course appears in the
1325 * CONNECTING state) will never be prepared. So the options are
1326 * either to create another approver like I've done, or to port
1327 * EmpathyTpChat and its users to not depend on the connection being
1328 * prepared with capabilities. I chose the former, obviously. :-) */
1330 priv->auth_approver = tp_simple_approver_new (dbus,
1331 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1334 /* SASL auth channels */
1335 tp_base_client_take_approver_filter (priv->auth_approver,
1337 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1338 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1339 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1341 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1344 if (!tp_base_client_register (priv->approver, &error))
1346 DEBUG ("Failed to register Approver: %s", error->message);
1347 g_error_free (error);
1350 if (!tp_base_client_register (priv->auth_approver, &error))
1352 DEBUG ("Failed to register auth Approver: %s", error->message);
1353 g_error_free (error);
1356 g_object_unref (dbus);
1359 EmpathyEventManager *
1360 empathy_event_manager_dup_singleton (void)
1362 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1366 empathy_event_manager_get_events (EmpathyEventManager *manager)
1368 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1370 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1372 return priv->events;
1376 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1378 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1380 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1382 return priv->events ? priv->events->data : NULL;
1386 empathy_event_activate (EmpathyEvent *event_public)
1388 EventPriv *event = (EventPriv *) event_public;
1390 g_return_if_fail (event_public != NULL);
1393 event->func (event);
1395 event_remove (event);
1399 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1401 EventPriv *event = (EventPriv *) event_public;
1403 g_return_if_fail (event_public != NULL);
1405 event->inhibit = TRUE;
1409 empathy_event_approve (EmpathyEvent *event_public)
1411 EventPriv *event = (EventPriv *) event_public;
1413 g_return_if_fail (event_public != NULL);
1415 event_manager_approval_approve (event->approval);
1419 empathy_event_decline (EmpathyEvent *event_public)
1421 EventPriv *event = (EventPriv *) event_public;
1423 g_return_if_fail (event_public != NULL);
1425 reject_approval (event->approval);