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-channel-factory.h>
33 #include <libempathy/empathy-presence-manager.h>
34 #include <libempathy/empathy-tp-contact-factory.h>
35 #include <libempathy/empathy-contact-manager.h>
36 #include <libempathy/empathy-tp-chat.h>
37 #include <libempathy/empathy-tp-streamed-media.h>
38 #include <libempathy/empathy-tp-file.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-gsettings.h>
42 #include <extensions/extensions.h>
44 #include <libempathy-gtk/empathy-images.h>
45 #include <libempathy-gtk/empathy-contact-dialogs.h>
46 #include <libempathy-gtk/empathy-sound-manager.h>
47 #include <libempathy-gtk/empathy-ui-utils.h>
49 #include "empathy-event-manager.h"
50 #include "empathy-main-window.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
53 #include <libempathy/empathy-debug.h>
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
57 #define NOTIFICATION_TIMEOUT 2 /* seconds */
59 /* The time interval in milliseconds between 2 incoming rings */
60 #define MS_BETWEEN_RING 500
63 EmpathyEventManager *manager;
64 TpChannelDispatchOperation *operation;
65 gulong invalidated_handler;
66 /* Remove contact if applicable */
67 EmpathyContact *contact;
68 /* option signal handler and it's instance */
70 GObject *handler_instance;
71 /* optional accept widget */
73 /* Channel of the CDO that will be used during the approval */
74 TpChannel *main_channel;
75 gboolean auto_approved;
76 } EventManagerApproval;
79 TpBaseClient *approver;
80 TpBaseClient *auth_approver;
81 EmpathyContactManager *contact_manager;
83 /* Ongoing approvals */
88 GSettings *gsettings_notif;
89 GSettings *gsettings_ui;
91 EmpathySoundManager *sound_mgr;
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 event->inhibit = FALSE;
238 event->user_data = user_data;
239 event->manager = manager;
240 event->approval = approval;
242 DEBUG ("Adding event %p", event);
243 priv->events = g_slist_prepend (priv->events, event);
245 if (!display_notify_area (manager))
247 /* Don't fire the 'event-added' signal as we activate the event now */
248 if (approval != NULL)
249 approval->auto_approved = TRUE;
251 empathy_event_activate (&event->public);
255 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
257 if (!event->public.must_ack)
259 event->autoremove_timeout_id = g_timeout_add_seconds (
260 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
266 handle_with_cb (GObject *source,
267 GAsyncResult *result,
270 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
271 GError *error = NULL;
273 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
275 DEBUG ("HandleWith failed: %s\n", error->message);
276 g_error_free (error);
281 handle_with_time_cb (GObject *source,
282 GAsyncResult *result,
285 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
286 GError *error = NULL;
288 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
291 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
293 EventManagerApproval *approval = user_data;
295 DEBUG ("HandleWithTime() is not implemented, falling back to "
296 "HandleWith(). Please upgrade to telepathy-mission-control "
299 tp_channel_dispatch_operation_handle_with_async (approval->operation,
300 NULL, handle_with_cb, approval);
304 DEBUG ("HandleWithTime failed: %s\n", error->message);
306 g_error_free (error);
311 event_manager_approval_approve (EventManagerApproval *approval)
315 if (approval->auto_approved)
317 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
321 timestamp = empathy_get_current_action_time ();
324 g_assert (approval->operation != NULL);
326 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
327 NULL, timestamp, handle_with_time_cb, approval);
331 event_channel_process_func (EventPriv *event)
333 event_manager_approval_approve (event->approval);
337 event_text_channel_process_func (EventPriv *event)
339 EmpathyTpChat *tp_chat;
341 if (event->approval->handler != 0)
343 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
345 g_signal_handler_disconnect (tp_chat, event->approval->handler);
346 event->approval->handler = 0;
349 event_manager_approval_approve (event->approval);
353 event_lookup_by_approval (EmpathyEventManager *manager,
354 EventManagerApproval *approval)
356 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
358 EventPriv *retval = NULL;
360 for (l = priv->events; l; l = l->next)
362 EventPriv *event = l->data;
364 if (event->approval == approval)
375 event_update (EmpathyEventManager *manager, EventPriv *event,
376 const char *icon_name, const char *header, const char *msg)
378 g_free (event->public.icon_name);
379 g_free (event->public.header);
380 g_free (event->public.message);
382 event->public.icon_name = g_strdup (icon_name);
383 event->public.header = g_strdup (header);
384 event->public.message = g_strdup (msg);
386 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
390 reject_channel_claim_cb (GObject *source,
391 GAsyncResult *result,
394 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
395 GError *error = NULL;
397 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
399 DEBUG ("Failed to claim channel: %s", error->message);
401 g_error_free (error);
405 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
407 empathy_tp_streamed_media_close (user_data);
409 else if (EMPATHY_IS_TP_CHAT (user_data))
411 empathy_tp_chat_leave (user_data, "");
413 else if (EMPATHY_IS_TP_FILE (user_data))
415 empathy_tp_file_close (user_data);
419 g_object_unref (user_data);
423 reject_auth_channel_claim_cb (GObject *source,
424 GAsyncResult *result,
427 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
428 GError *error = NULL;
430 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
432 DEBUG ("Failed to claim channel: %s", error->message);
434 g_error_free (error);
438 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
439 NULL, NULL, NULL, NULL);
443 reject_approval (EventManagerApproval *approval)
445 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
447 /* We have to claim the channel before closing it */
449 /* Unfortunately, we need to special case the auth channels for the
450 * time being as they don't have a wrapper object handler in
451 * approval->handler_instance as they're not actually handled by
452 * this process, so we can just use a noddy callback to call Close()
454 if (approval->handler_instance != NULL)
456 tp_channel_dispatch_operation_claim_with_async (approval->operation,
457 priv->approver, reject_channel_claim_cb,
458 g_object_ref (approval->handler_instance));
460 else if (tp_channel_get_channel_type_id (approval->main_channel)
461 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
463 tp_channel_dispatch_operation_claim_with_async (approval->operation,
464 priv->auth_approver, reject_auth_channel_claim_cb,
465 approval->main_channel);
470 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
471 gint response, gpointer user_data)
473 EventManagerApproval *approval = user_data;
475 gtk_widget_destroy (approval->dialog);
476 approval->dialog = NULL;
478 if (response != GTK_RESPONSE_ACCEPT)
480 reject_approval (approval);
484 event_manager_approval_approve (approval);
489 event_channel_process_voip_func (EventPriv *event)
494 EmpathyTpStreamedMedia *call;
498 if (event->approval->dialog != NULL)
500 gtk_window_present (GTK_WINDOW (event->approval->dialog));
504 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
506 video = empathy_tp_streamed_media_has_initial_video (call);
508 dialog = gtk_message_dialog_new (NULL, 0,
509 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
510 video ? _("Incoming video call"): _("Incoming call"));
512 gtk_message_dialog_format_secondary_text (
513 GTK_MESSAGE_DIALOG (dialog), video ?
514 _("%s is video calling you. Do you want to answer?"):
515 _("%s is calling you. Do you want to answer?"),
516 empathy_contact_get_alias (event->approval->contact));
518 title = g_strdup_printf (_("Incoming call from %s"),
519 empathy_contact_get_alias (event->approval->contact));
521 gtk_window_set_title (GTK_WINDOW (dialog), title);
524 /* Set image of the dialog */
527 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
528 GTK_ICON_SIZE_DIALOG);
532 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
533 GTK_ICON_SIZE_DIALOG);
536 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
537 gtk_widget_show (image);
539 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
542 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
543 _("_Reject"), GTK_RESPONSE_REJECT);
544 image = gtk_image_new_from_icon_name ("call-stop",
545 GTK_ICON_SIZE_BUTTON);
546 gtk_button_set_image (GTK_BUTTON (button), image);
548 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
549 _("_Answer"), GTK_RESPONSE_ACCEPT);
551 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
552 gtk_button_set_image (GTK_BUTTON (button), image);
554 g_signal_connect (dialog, "response",
555 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
558 gtk_widget_show (dialog);
560 event->approval->dialog = dialog;
564 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
565 EmpathyMessage *message,
566 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 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 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
707 approval->dialog = NULL;
709 if (response != GTK_RESPONSE_OK)
712 DEBUG ("Muc invitation rejected");
714 reject_approval (approval);
719 DEBUG ("Muc invitation accepted");
721 /* We'll join the room when handling the channel */
722 event_manager_approval_approve (approval);
726 event_room_channel_process_func (EventPriv *event)
728 GtkWidget *dialog, *button, *image;
729 TpChannel *channel = event->approval->main_channel;
732 if (event->approval->dialog != NULL)
734 gtk_window_present (GTK_WINDOW (event->approval->dialog));
739 dialog = gtk_message_dialog_new (NULL, 0,
740 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
742 title = g_strdup_printf (_("Invitation to join %s"),
743 tp_channel_get_identifier (channel));
745 gtk_window_set_title (GTK_WINDOW (dialog), title);
748 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
749 _("%s is inviting you to join %s"),
750 empathy_contact_get_alias (event->approval->contact),
751 tp_channel_get_identifier (channel));
753 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
756 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
757 _("_Decline"), GTK_RESPONSE_CANCEL);
758 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
759 gtk_button_set_image (GTK_BUTTON (button), image);
761 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
762 _("_Join"), GTK_RESPONSE_OK);
763 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
764 gtk_button_set_image (GTK_BUTTON (button), image);
766 g_signal_connect (dialog, "response",
767 G_CALLBACK (invite_dialog_response_cb), event->approval);
769 gtk_widget_show (dialog);
771 event->approval->dialog = dialog;
775 display_invite_room_dialog (EventManagerApproval *approval)
777 GtkWidget *window = empathy_main_window_dup ();
778 const gchar *invite_msg;
780 TpHandle self_handle;
781 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
783 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
784 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
785 NULL, NULL, &invite_msg);
787 if (approval->contact != NULL)
789 msg = g_strdup_printf (_("%s invited you to join %s"),
790 empathy_contact_get_alias (approval->contact),
791 tp_channel_get_identifier (approval->main_channel));
795 msg = g_strdup_printf (_("You have been invited to join %s"),
796 tp_channel_get_identifier (approval->main_channel));
799 event_manager_add (approval->manager, NULL,
800 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
801 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
802 event_room_channel_process_func, NULL);
804 empathy_sound_manager_play (priv->sound_mgr, window,
805 EMPATHY_SOUND_CONVERSATION_NEW);
808 g_object_unref (window);
812 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
813 EmpathyContact *contact,
818 EventManagerApproval *approval = (EventManagerApproval *) user_data;
822 DEBUG ("Error: %s", error->message);
826 approval->contact = g_object_ref (contact);
829 display_invite_room_dialog (approval);
833 event_manager_ft_got_contact_cb (TpConnection *connection,
834 EmpathyContact *contact,
839 EventManagerApproval *approval = (EventManagerApproval *) user_data;
840 GtkWidget *window = empathy_main_window_dup ();
842 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
844 approval->contact = g_object_ref (contact);
846 header = g_strdup_printf (_("Incoming file transfer from %s"),
847 empathy_contact_get_alias (approval->contact));
849 event_manager_add (approval->manager, NULL,
850 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
851 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
852 approval, event_channel_process_func, NULL);
854 /* FIXME better sound for incoming file transfers ?*/
855 empathy_sound_manager_play (priv->sound_mgr, window,
856 EMPATHY_SOUND_CONVERSATION_NEW);
859 g_object_unref (window);
863 event_manager_auth_process_func (EventPriv *event)
865 empathy_event_approve ((EmpathyEvent *) event);
868 /* If there is a file-transfer, media, or auth channel consider it as
871 find_main_channel (GList *channels)
874 TpChannel *text = NULL;
876 for (l = channels; l != NULL; l = g_list_next (l))
878 TpChannel *channel = l->data;
881 if (tp_proxy_get_invalidated (channel) != NULL)
884 channel_type = tp_channel_get_channel_type_id (channel);
886 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
887 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
888 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
891 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
899 approve_channels (TpSimpleApprover *approver,
901 TpConnection *connection,
903 TpChannelDispatchOperation *dispatch_operation,
904 TpAddDispatchOperationContext *context,
907 EmpathyEventManager *self = user_data;
908 EmpathyEventManagerPriv *priv = GET_PRIV (self);
910 EventManagerApproval *approval;
913 channel = find_main_channel (channels);
916 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
917 "Unknown channel type" };
919 DEBUG ("Failed to find the main channel; ignoring");
921 tp_add_dispatch_operation_context_fail (context, &error);
925 approval = event_manager_approval_new (self, dispatch_operation, channel);
926 priv->approvals = g_slist_prepend (priv->approvals, approval);
928 approval->invalidated_handler = g_signal_connect (dispatch_operation,
929 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
931 channel_type = tp_channel_get_channel_type_id (channel);
933 if (TP_IS_TEXT_CHANNEL (channel))
935 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
938 approval->handler_instance = g_object_ref (tp_chat);
940 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
942 /* Are we in local-pending ? */
945 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
947 /* We are invited to a room */
948 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
949 tp_channel_get_identifier (channel));
953 empathy_tp_contact_factory_get_from_handle (connection,
954 inviter, event_manager_muc_invite_got_contact_cb,
955 approval, NULL, G_OBJECT (self));
959 display_invite_room_dialog (approval);
965 /* We are not invited, approve the channel right now */
966 tp_add_dispatch_operation_context_accept (context);
968 approval->auto_approved = TRUE;
969 event_manager_approval_approve (approval);
973 /* 1-1 text channel, wait for the first message */
974 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
975 G_CALLBACK (event_manager_chat_message_received_cb), approval);
977 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
978 for (l = messages; l != NULL; l = g_list_next (l))
980 EmpathyMessage *msg = l->data;
982 event_manager_chat_message_received_cb (tp_chat, msg, approval);
985 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
987 EmpathyContact *contact;
988 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account, channel);
990 approval->handler_instance = G_OBJECT (call);
992 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
996 g_signal_connect (call, "notify::contact",
997 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
1002 approval->contact = contact;
1003 event_manager_media_channel_got_contact (approval);
1007 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1010 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1012 approval->handler_instance = G_OBJECT (tp_file);
1014 handle = tp_channel_get_handle (channel, NULL);
1016 connection = tp_channel_borrow_connection (channel);
1017 empathy_tp_contact_factory_get_from_handle (connection, handle,
1018 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1020 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1022 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1023 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1024 _("Password required"), approval,
1025 event_manager_auth_process_func, NULL);
1029 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1030 "Invalid channel type" };
1032 DEBUG ("Unknown channel type (%s), ignoring..",
1033 g_quark_to_string (channel_type));
1035 tp_add_dispatch_operation_context_fail (context, &error);
1040 tp_add_dispatch_operation_context_accept (context);
1044 event_pending_subscribe_func (EventPriv *event)
1046 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1048 event_remove (event);
1052 event_manager_pendings_changed_cb (EmpathyContactList *list,
1053 EmpathyContact *contact, EmpathyContact *actor,
1054 guint reason, gchar *message, gboolean is_pending,
1055 EmpathyEventManager *manager)
1057 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1058 gchar *header, *event_msg;
1064 for (l = priv->events; l; l = l->next)
1066 EventPriv *event = l->data;
1068 if (event->public.contact == contact &&
1069 event->func == event_pending_subscribe_func)
1071 event_remove (event);
1079 header = g_strdup_printf (
1080 _("%s would like permission to see when you are online"),
1081 empathy_contact_get_alias (contact));
1083 if (!EMP_STR_EMPTY (message))
1084 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1088 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1089 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1090 event_pending_subscribe_func, NULL);
1097 event_manager_presence_changed_cb (EmpathyContact *contact,
1098 TpConnectionPresenceType current,
1099 TpConnectionPresenceType previous,
1100 EmpathyEventManager *manager)
1102 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1104 EmpathyPresenceManager *presence_mgr;
1105 GtkWidget *window = empathy_main_window_dup ();
1107 account = empathy_contact_get_account (contact);
1108 presence_mgr = empathy_presence_manager_dup_singleton ();
1110 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1113 if (tp_connection_presence_type_cmp_availability (previous,
1114 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1116 /* contact was online */
1117 if (tp_connection_presence_type_cmp_availability (current,
1118 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1120 /* someone is logging off */
1121 empathy_sound_manager_play (priv->sound_mgr, window,
1122 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1124 if (g_settings_get_boolean (priv->gsettings_notif,
1125 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1127 event_manager_add (manager, NULL, contact,
1128 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1129 EMPATHY_IMAGE_AVATAR_DEFAULT,
1130 empathy_contact_get_alias (contact), _("Disconnected"),
1137 /* contact was offline */
1138 if (tp_connection_presence_type_cmp_availability (current,
1139 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1141 /* someone is logging in */
1142 empathy_sound_manager_play (priv->sound_mgr, window,
1143 EMPATHY_SOUND_CONTACT_CONNECTED);
1145 if (g_settings_get_boolean (priv->gsettings_notif,
1146 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1148 event_manager_add (manager, NULL, contact,
1149 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1150 EMPATHY_IMAGE_AVATAR_DEFAULT,
1151 empathy_contact_get_alias (contact), _("Connected"),
1158 g_object_unref (presence_mgr);
1159 g_object_unref (window);
1163 event_manager_members_changed_cb (EmpathyContactList *list,
1164 EmpathyContact *contact,
1165 EmpathyContact *actor,
1169 EmpathyEventManager *manager)
1172 g_signal_connect (contact, "presence-changed",
1173 G_CALLBACK (event_manager_presence_changed_cb), manager);
1175 g_signal_handlers_disconnect_by_func (contact,
1176 event_manager_presence_changed_cb, manager);
1180 event_manager_constructor (GType type,
1182 GObjectConstructParam *props)
1186 if (manager_singleton) {
1187 retval = g_object_ref (manager_singleton);
1189 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1190 (type, n_props, props);
1192 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1193 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1200 event_manager_finalize (GObject *object)
1202 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1204 if (priv->ringing > 0)
1205 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1207 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1208 g_slist_free (priv->events);
1209 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1210 g_slist_free (priv->approvals);
1211 g_object_unref (priv->contact_manager);
1212 g_object_unref (priv->approver);
1213 g_object_unref (priv->auth_approver);
1214 g_object_unref (priv->gsettings_notif);
1215 g_object_unref (priv->gsettings_ui);
1216 g_object_unref (priv->sound_mgr);
1220 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1222 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1224 object_class->finalize = event_manager_finalize;
1225 object_class->constructor = event_manager_constructor;
1227 signals[EVENT_ADDED] =
1228 g_signal_new ("event-added",
1229 G_TYPE_FROM_CLASS (klass),
1233 g_cclosure_marshal_VOID__POINTER,
1237 signals[EVENT_REMOVED] =
1238 g_signal_new ("event-removed",
1239 G_TYPE_FROM_CLASS (klass),
1243 g_cclosure_marshal_VOID__POINTER,
1244 G_TYPE_NONE, 1, G_TYPE_POINTER);
1246 signals[EVENT_UPDATED] =
1247 g_signal_new ("event-updated",
1248 G_TYPE_FROM_CLASS (klass),
1252 g_cclosure_marshal_VOID__POINTER,
1253 G_TYPE_NONE, 1, G_TYPE_POINTER);
1255 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1259 empathy_event_manager_init (EmpathyEventManager *manager)
1261 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1262 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1264 GError *error = NULL;
1265 EmpathyChannelFactory *factory;
1267 manager->priv = priv;
1269 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1270 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1272 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1274 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1275 g_signal_connect (priv->contact_manager, "pendings-changed",
1276 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1278 g_signal_connect (priv->contact_manager, "members-changed",
1279 G_CALLBACK (event_manager_members_changed_cb), manager);
1281 dbus = tp_dbus_daemon_dup (&error);
1284 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1285 g_error_free (error);
1289 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1290 approve_channels, manager, NULL);
1292 /* EmpathyTpChat relies on this feature being prepared */
1293 tp_base_client_add_connection_features_varargs (priv->approver,
1294 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1296 /* Private text channels */
1297 tp_base_client_take_approver_filter (priv->approver,
1299 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1300 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1303 /* Muc text channels */
1304 tp_base_client_take_approver_filter (priv->approver,
1306 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1307 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1311 tp_base_client_take_approver_filter (priv->approver,
1313 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1314 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1315 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1319 tp_base_client_take_approver_filter (priv->approver,
1321 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1322 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1323 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1326 /* I don't feel good about doing this, and I'm sorry, but the
1327 * capabilities connection feature is added earlier because it's
1328 * needed for EmpathyTpChat. If the capabilities feature is required
1329 * then preparing an auth channel (which of course appears in the
1330 * CONNECTING state) will never be prepared. So the options are
1331 * either to create another approver like I've done, or to port
1332 * EmpathyTpChat and its users to not depend on the connection being
1333 * prepared with capabilities. I chose the former, obviously. :-) */
1335 priv->auth_approver = tp_simple_approver_new (dbus,
1336 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1339 /* SASL auth channels */
1340 tp_base_client_take_approver_filter (priv->auth_approver,
1342 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1343 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1344 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1346 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1349 factory = empathy_channel_factory_dup ();
1351 tp_base_client_set_channel_factory (priv->approver,
1352 TP_CLIENT_CHANNEL_FACTORY (factory));
1354 if (!tp_base_client_register (priv->approver, &error))
1356 DEBUG ("Failed to register Approver: %s", error->message);
1357 g_error_free (error);
1360 if (!tp_base_client_register (priv->auth_approver, &error))
1362 DEBUG ("Failed to register auth Approver: %s", error->message);
1363 g_error_free (error);
1366 g_object_unref (factory);
1367 g_object_unref (dbus);
1370 EmpathyEventManager *
1371 empathy_event_manager_dup_singleton (void)
1373 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1377 empathy_event_manager_get_events (EmpathyEventManager *manager)
1379 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1381 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1383 return priv->events;
1387 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1389 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1391 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1393 return priv->events ? priv->events->data : NULL;
1397 empathy_event_activate (EmpathyEvent *event_public)
1399 EventPriv *event = (EventPriv *) event_public;
1401 g_return_if_fail (event_public != NULL);
1404 event->func (event);
1406 event_remove (event);
1410 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1412 EventPriv *event = (EventPriv *) event_public;
1414 g_return_if_fail (event_public != NULL);
1416 event->inhibit = TRUE;
1420 empathy_event_approve (EmpathyEvent *event_public)
1422 EventPriv *event = (EventPriv *) event_public;
1424 g_return_if_fail (event_public != NULL);
1426 event_manager_approval_approve (event->approval);
1430 empathy_event_decline (EmpathyEvent *event_public)
1432 EventPriv *event = (EventPriv *) event_public;
1434 g_return_if_fail (event_public != NULL);
1436 reject_approval (event->approval);