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 <telepathy-yell/telepathy-yell.h>
34 #include <libempathy/empathy-channel-factory.h>
35 #include <libempathy/empathy-presence-manager.h>
36 #include <libempathy/empathy-tp-contact-factory.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-tp-chat.h>
39 #include <libempathy/empathy-tp-streamed-media.h>
40 #include <libempathy/empathy-tp-file.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-gsettings.h>
44 #include <extensions/extensions.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-contact-dialogs.h>
48 #include <libempathy-gtk/empathy-sound-manager.h>
49 #include <libempathy-gtk/empathy-ui-utils.h>
51 #include "empathy-event-manager.h"
52 #include "empathy-main-window.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
55 #include <libempathy/empathy-debug.h>
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
59 #define NOTIFICATION_TIMEOUT 2 /* seconds */
61 #define ACCEPT_WITHOUT_VIDEO 1
63 /* The time interval in milliseconds between 2 incoming rings */
64 #define MS_BETWEEN_RING 500
67 EmpathyEventManager *manager;
68 TpChannelDispatchOperation *operation;
69 gulong invalidated_handler;
70 /* Remove contact if applicable */
71 EmpathyContact *contact;
72 /* option signal handler and it's instance */
74 GObject *handler_instance;
75 /* optional accept widget */
77 /* Channel of the CDO that will be used during the approval */
78 TpChannel *main_channel;
79 gboolean auto_approved;
80 } EventManagerApproval;
83 TpBaseClient *approver;
84 TpBaseClient *auth_approver;
85 EmpathyContactManager *contact_manager;
87 /* Ongoing approvals */
92 GSettings *gsettings_notif;
93 GSettings *gsettings_ui;
95 EmpathySoundManager *sound_mgr;
96 } EmpathyEventManagerPriv;
98 typedef struct _EventPriv EventPriv;
99 typedef void (*EventFunc) (EventPriv *event);
103 EmpathyEventManager *manager;
104 EventManagerApproval *approval;
108 guint autoremove_timeout_id;
118 static guint signals[LAST_SIGNAL];
120 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
122 static EmpathyEventManager * manager_singleton = NULL;
124 static EventManagerApproval *
125 event_manager_approval_new (EmpathyEventManager *manager,
126 TpChannelDispatchOperation *operation,
127 TpChannel *main_channel)
129 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
130 result->operation = g_object_ref (operation);
131 result->manager = manager;
132 result->main_channel = g_object_ref (main_channel);
138 event_manager_approval_free (EventManagerApproval *approval)
140 g_signal_handler_disconnect (approval->operation,
141 approval->invalidated_handler);
142 g_object_unref (approval->operation);
144 g_object_unref (approval->main_channel);
146 if (approval->handler != 0)
147 g_signal_handler_disconnect (approval->handler_instance,
150 if (approval->handler_instance != NULL)
151 g_object_unref (approval->handler_instance);
153 if (approval->contact != NULL)
154 g_object_unref (approval->contact);
156 if (approval->dialog != NULL)
158 gtk_widget_destroy (approval->dialog);
161 g_slice_free (EventManagerApproval, approval);
165 event_free (EventPriv *event)
167 g_free (event->public.icon_name);
168 g_free (event->public.header);
169 g_free (event->public.message);
171 if (event->autoremove_timeout_id != 0)
172 g_source_remove (event->autoremove_timeout_id);
174 tp_clear_object (&(event->public.contact));
175 tp_clear_object (&(event->public.account));
177 g_slice_free (EventPriv, event);
181 event_remove (EventPriv *event)
183 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
185 DEBUG ("Removing event %p", event);
187 priv->events = g_slist_remove (priv->events, event);
188 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
193 empathy_event_remove (EmpathyEvent *event_public)
195 EventPriv *event = (EventPriv *) event_public;
197 event_remove (event);
201 autoremove_event_timeout_cb (EventPriv *event)
203 event->autoremove_timeout_id = 0;
204 event_remove (event);
209 display_notify_area (EmpathyEventManager *self)
211 EmpathyEventManagerPriv *priv = GET_PRIV (self);
213 return g_settings_get_boolean (priv->gsettings_ui,
214 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
218 event_manager_add (EmpathyEventManager *manager,
220 EmpathyContact *contact,
221 EmpathyEventType type,
222 const gchar *icon_name,
224 const gchar *message,
225 EventManagerApproval *approval,
229 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
232 event = g_slice_new0 (EventPriv);
233 event->public.account = account != NULL ? g_object_ref (account) : NULL;
234 event->public.contact = contact ? g_object_ref (contact) : NULL;
235 event->public.type = type;
236 event->public.icon_name = g_strdup (icon_name);
237 event->public.header = g_strdup (header);
238 event->public.message = g_strdup (message);
239 event->public.must_ack = (func != NULL);
240 if (approval != NULL)
241 event->public.handler_instance = approval->handler_instance;
242 event->inhibit = FALSE;
244 event->user_data = user_data;
245 event->manager = manager;
246 event->approval = approval;
248 DEBUG ("Adding event %p", event);
249 priv->events = g_slist_prepend (priv->events, event);
251 if (!display_notify_area (manager))
253 /* Don't fire the 'event-added' signal as we activate the event now */
254 if (approval != NULL)
255 approval->auto_approved = TRUE;
257 empathy_event_activate (&event->public);
261 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
263 if (!event->public.must_ack)
265 event->autoremove_timeout_id = g_timeout_add_seconds (
266 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
272 handle_with_cb (GObject *source,
273 GAsyncResult *result,
276 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
277 GError *error = NULL;
279 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
281 DEBUG ("HandleWith failed: %s\n", error->message);
282 g_error_free (error);
287 handle_with_time_cb (GObject *source,
288 GAsyncResult *result,
291 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
292 GError *error = NULL;
294 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
297 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
299 EventManagerApproval *approval = user_data;
301 DEBUG ("HandleWithTime() is not implemented, falling back to "
302 "HandleWith(). Please upgrade to telepathy-mission-control "
305 tp_channel_dispatch_operation_handle_with_async (approval->operation,
306 NULL, handle_with_cb, approval);
310 DEBUG ("HandleWithTime failed: %s\n", error->message);
312 g_error_free (error);
317 event_manager_approval_approve (EventManagerApproval *approval)
321 if (approval->auto_approved)
323 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
327 timestamp = empathy_get_current_action_time ();
330 g_assert (approval->operation != NULL);
332 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
333 NULL, timestamp, handle_with_time_cb, approval);
337 event_channel_process_func (EventPriv *event)
339 event_manager_approval_approve (event->approval);
343 event_text_channel_process_func (EventPriv *event)
345 EmpathyTpChat *tp_chat;
347 if (event->approval->handler != 0)
349 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
351 g_signal_handler_disconnect (tp_chat, event->approval->handler);
352 event->approval->handler = 0;
355 event_manager_approval_approve (event->approval);
359 event_lookup_by_approval (EmpathyEventManager *manager,
360 EventManagerApproval *approval)
362 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
364 EventPriv *retval = NULL;
366 for (l = priv->events; l; l = l->next)
368 EventPriv *event = l->data;
370 if (event->approval == approval)
381 event_update (EmpathyEventManager *manager, EventPriv *event,
382 const char *icon_name, const char *header, const char *msg)
384 g_free (event->public.icon_name);
385 g_free (event->public.header);
386 g_free (event->public.message);
388 event->public.icon_name = g_strdup (icon_name);
389 event->public.header = g_strdup (header);
390 event->public.message = g_strdup (msg);
392 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
396 reject_channel_claim_cb (GObject *source,
397 GAsyncResult *result,
400 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
401 GError *error = NULL;
403 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
405 DEBUG ("Failed to claim channel: %s", error->message);
407 g_error_free (error);
411 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
413 empathy_tp_streamed_media_close (user_data);
415 else if (TPY_IS_CALL_CHANNEL (user_data))
417 tpy_call_channel_hangup_async (user_data,
418 TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
420 tp_channel_close_async (user_data, NULL, NULL);
422 else if (EMPATHY_IS_TP_CHAT (user_data))
424 empathy_tp_chat_leave (user_data, "");
426 else if (EMPATHY_IS_TP_FILE (user_data))
428 empathy_tp_file_close (user_data);
432 g_object_unref (user_data);
436 reject_approval (EventManagerApproval *approval)
438 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
440 /* We have to claim the channel before closing it */
442 /* Unfortunately, we need to special case the auth channels for the
443 * time being as they don't have a wrapper object handler in
444 * approval->handler_instance as they're not actually handled by
445 * this process, so we can just use a noddy callback to call Close()
447 if (approval->handler_instance != NULL)
449 tp_channel_dispatch_operation_claim_with_async (approval->operation,
450 priv->approver, reject_channel_claim_cb,
451 g_object_ref (approval->handler_instance));
453 else if (tp_channel_get_channel_type_id (approval->main_channel)
454 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
456 tp_channel_dispatch_operation_close_channels_async (approval->operation,
462 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
463 gint response, gpointer user_data)
465 EventManagerApproval *approval = user_data;
467 gtk_widget_destroy (approval->dialog);
468 approval->dialog = NULL;
470 if (response == GTK_RESPONSE_ACCEPT)
472 event_manager_approval_approve (approval);
474 else if (response == ACCEPT_WITHOUT_VIDEO)
476 tpy_call_channel_send_video (TPY_CALL_CHANNEL (approval->main_channel),
478 event_manager_approval_approve (approval);
482 reject_approval (approval);
487 event_channel_process_voip_func (EventPriv *event)
494 EmpathyEventType etype = event->public.type;
496 if (event->approval->dialog != NULL)
498 gtk_window_present (GTK_WINDOW (event->approval->dialog));
502 if (etype == EMPATHY_EVENT_TYPE_VOIP)
504 EmpathyTpStreamedMedia *call;
505 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
506 video = empathy_tp_streamed_media_has_initial_video (call);
508 else if (etype == EMPATHY_EVENT_TYPE_CALL)
510 TpyCallChannel *call;
511 call = TPY_CALL_CHANNEL (event->approval->handler_instance);
512 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
516 g_warning ("Unknown event type: %d", event->public.type);
520 dialog = gtk_message_dialog_new (NULL, 0,
521 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
522 video ? _("Incoming video call"): _("Incoming call"));
524 gtk_message_dialog_format_secondary_text (
525 GTK_MESSAGE_DIALOG (dialog), video ?
526 _("%s is video calling you. Do you want to answer?"):
527 _("%s is calling you. Do you want to answer?"),
528 empathy_contact_get_alias (event->approval->contact));
530 title = g_strdup_printf (_("Incoming call from %s"),
531 empathy_contact_get_alias (event->approval->contact));
533 gtk_window_set_title (GTK_WINDOW (dialog), title);
536 /* Set image of the dialog */
539 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
540 GTK_ICON_SIZE_DIALOG);
544 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
545 GTK_ICON_SIZE_DIALOG);
548 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
549 gtk_widget_show (image);
551 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
554 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
555 _("_Reject"), GTK_RESPONSE_REJECT);
556 image = gtk_image_new_from_icon_name ("call-stop",
557 GTK_ICON_SIZE_BUTTON);
558 gtk_button_set_image (GTK_BUTTON (button), image);
560 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
562 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
563 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
565 image = gtk_image_new_from_icon_name ("call-start",
566 GTK_ICON_SIZE_BUTTON);
567 gtk_button_set_image (GTK_BUTTON (button), image);
570 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
571 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
573 image = gtk_image_new_from_icon_name ("call-start",
574 GTK_ICON_SIZE_BUTTON);
575 gtk_button_set_image (GTK_BUTTON (button), image);
577 g_signal_connect (dialog, "response",
578 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
581 gtk_widget_show (dialog);
583 event->approval->dialog = dialog;
587 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
588 EmpathyMessage *message,
589 EventManagerApproval *approval)
592 EmpathyContact *sender;
596 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
598 /* try to update the event if it's referring to a chat which is already in the
600 event = event_lookup_by_approval (approval->manager, approval);
602 sender = empathy_message_get_sender (message);
604 /* We only want to show incoming messages */
605 if (empathy_contact_is_user (sender))
608 header = empathy_contact_get_alias (sender);
609 msg = empathy_message_get_body (message);
612 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
615 event_manager_add (approval->manager, NULL, sender,
616 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
617 approval, event_text_channel_process_func, NULL);
619 window = empathy_main_window_dup ();
621 empathy_sound_manager_play (priv->sound_mgr, window,
622 EMPATHY_SOUND_CONVERSATION_NEW);
624 g_object_unref (window);
628 event_manager_approval_done (EventManagerApproval *approval)
630 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
633 if (approval->operation != NULL)
637 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
639 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
640 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
643 if (priv->ringing == 0)
644 empathy_sound_manager_stop (priv->sound_mgr,
645 EMPATHY_SOUND_PHONE_INCOMING);
649 priv->approvals = g_slist_remove (priv->approvals, approval);
651 for (l = priv->events; l; l = l->next)
653 EventPriv *event = l->data;
655 if (event->approval == approval)
657 event_remove (event);
662 event_manager_approval_free (approval);
666 cdo_invalidated_cb (TpProxy *cdo,
670 EventManagerApproval *approval)
672 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
674 event_manager_approval_done (approval);
678 event_manager_call_state_changed_cb (TpyCallChannel *call,
681 const GValueArray *call_state_reason,
682 GHashTable *call_state_details,
683 EventManagerApproval *approval)
685 if (state == TPY_CALL_STATE_ENDED)
687 DEBUG ("Call ended, seems we missed it :/");
688 reject_approval (approval);
693 event_manager_call_channel_got_contact_cb (TpConnection *connection,
694 EmpathyContact *contact,
699 EventManagerApproval *approval = (EventManagerApproval *) user_data;
700 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
702 TpyCallChannel *call;
706 call = TPY_CALL_CHANNEL (approval->handler_instance);
710 DEBUG ("Can't get the contact for the call.. Rejecting?");
711 reject_approval (approval);
715 if (tpy_call_channel_get_state (call, NULL, NULL) == TPY_CALL_STATE_ENDED)
717 DEBUG ("Call already ended, seems we missed it :/");
718 reject_approval (approval);
722 approval->handler = g_signal_connect (call, "state-changed",
723 G_CALLBACK (event_manager_call_state_changed_cb), approval);
725 window = empathy_main_window_dup ();
726 approval->contact = g_object_ref (contact);
728 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
730 header = g_strdup_printf (
731 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
732 empathy_contact_get_alias (approval->contact));
734 event_manager_add (approval->manager, NULL,
735 approval->contact, EMPATHY_EVENT_TYPE_CALL,
736 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
737 header, NULL, approval,
738 event_channel_process_voip_func, NULL);
743 if (priv->ringing == 1)
744 empathy_sound_manager_start_playing (priv->sound_mgr, window,
745 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
747 g_object_unref (window);
751 event_manager_media_channel_got_contact (EventManagerApproval *approval)
753 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
754 GtkWidget *window = empathy_main_window_dup ();
756 EmpathyTpStreamedMedia *call;
759 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
761 video = empathy_tp_streamed_media_has_initial_video (call);
763 header = g_strdup_printf (
764 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
765 empathy_contact_get_alias (approval->contact));
767 event_manager_add (approval->manager, NULL,
768 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
769 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
770 header, NULL, approval,
771 event_channel_process_voip_func, NULL);
776 if (priv->ringing == 1)
777 empathy_sound_manager_start_playing (priv->sound_mgr, window,
778 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
780 g_object_unref (window);
784 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
785 GParamSpec *param, EventManagerApproval *approval)
787 EmpathyContact *contact;
789 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
794 approval->contact = contact;
795 event_manager_media_channel_got_contact (approval);
799 invite_dialog_response_cb (GtkDialog *dialog,
801 EventManagerApproval *approval)
803 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
804 approval->dialog = NULL;
806 if (response != GTK_RESPONSE_OK)
809 DEBUG ("Muc invitation rejected");
811 reject_approval (approval);
816 DEBUG ("Muc invitation accepted");
818 /* We'll join the room when handling the channel */
819 event_manager_approval_approve (approval);
823 event_room_channel_process_func (EventPriv *event)
825 GtkWidget *dialog, *button, *image;
826 TpChannel *channel = event->approval->main_channel;
829 if (event->approval->dialog != NULL)
831 gtk_window_present (GTK_WINDOW (event->approval->dialog));
836 dialog = gtk_message_dialog_new (NULL, 0,
837 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
839 title = g_strdup_printf (_("Invitation to join %s"),
840 tp_channel_get_identifier (channel));
842 gtk_window_set_title (GTK_WINDOW (dialog), title);
845 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
846 _("%s is inviting you to join %s"),
847 empathy_contact_get_alias (event->approval->contact),
848 tp_channel_get_identifier (channel));
850 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
853 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
854 _("_Decline"), GTK_RESPONSE_CANCEL);
855 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
856 gtk_button_set_image (GTK_BUTTON (button), image);
858 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
859 _("_Join"), GTK_RESPONSE_OK);
860 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
861 gtk_button_set_image (GTK_BUTTON (button), image);
863 g_signal_connect (dialog, "response",
864 G_CALLBACK (invite_dialog_response_cb), event->approval);
866 gtk_widget_show (dialog);
868 event->approval->dialog = dialog;
872 display_invite_room_dialog (EventManagerApproval *approval)
874 GtkWidget *window = empathy_main_window_dup ();
875 const gchar *invite_msg;
877 TpHandle self_handle;
878 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
880 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
881 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
882 NULL, NULL, &invite_msg);
884 if (approval->contact != NULL)
886 msg = g_strdup_printf (_("%s invited you to join %s"),
887 empathy_contact_get_alias (approval->contact),
888 tp_channel_get_identifier (approval->main_channel));
892 msg = g_strdup_printf (_("You have been invited to join %s"),
893 tp_channel_get_identifier (approval->main_channel));
896 event_manager_add (approval->manager, NULL,
897 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
898 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
899 event_room_channel_process_func, NULL);
901 empathy_sound_manager_play (priv->sound_mgr, window,
902 EMPATHY_SOUND_CONVERSATION_NEW);
905 g_object_unref (window);
909 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
910 EmpathyContact *contact,
915 EventManagerApproval *approval = (EventManagerApproval *) user_data;
919 DEBUG ("Error: %s", error->message);
923 approval->contact = g_object_ref (contact);
926 display_invite_room_dialog (approval);
930 event_manager_ft_got_contact_cb (TpConnection *connection,
931 EmpathyContact *contact,
936 EventManagerApproval *approval = (EventManagerApproval *) user_data;
937 GtkWidget *window = empathy_main_window_dup ();
939 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
941 approval->contact = g_object_ref (contact);
943 header = g_strdup_printf (_("Incoming file transfer from %s"),
944 empathy_contact_get_alias (approval->contact));
946 event_manager_add (approval->manager, NULL,
947 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
948 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
949 approval, event_channel_process_func, NULL);
951 /* FIXME better sound for incoming file transfers ?*/
952 empathy_sound_manager_play (priv->sound_mgr, window,
953 EMPATHY_SOUND_CONVERSATION_NEW);
956 g_object_unref (window);
960 event_manager_auth_process_func (EventPriv *event)
962 empathy_event_approve ((EmpathyEvent *) event);
965 /* If there is a file-transfer, media, or auth channel consider it as
968 find_main_channel (GList *channels)
971 TpChannel *text = NULL;
973 for (l = channels; l != NULL; l = g_list_next (l))
975 TpChannel *channel = l->data;
978 if (tp_proxy_get_invalidated (channel) != NULL)
981 channel_type = tp_channel_get_channel_type_id (channel);
983 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
984 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL ||
985 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
986 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
989 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
997 approve_channels (TpSimpleApprover *approver,
999 TpConnection *connection,
1001 TpChannelDispatchOperation *dispatch_operation,
1002 TpAddDispatchOperationContext *context,
1005 EmpathyEventManager *self = user_data;
1006 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1008 EventManagerApproval *approval;
1009 GQuark channel_type;
1011 channel = find_main_channel (channels);
1012 if (channel == NULL)
1014 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1015 "Unknown channel type" };
1017 DEBUG ("Failed to find the main channel; ignoring");
1019 tp_add_dispatch_operation_context_fail (context, &error);
1023 approval = event_manager_approval_new (self, dispatch_operation, channel);
1024 priv->approvals = g_slist_prepend (priv->approvals, approval);
1026 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1027 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1029 channel_type = tp_channel_get_channel_type_id (channel);
1031 if (TP_IS_TEXT_CHANNEL (channel))
1033 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
1034 GList *messages, *l;
1036 approval->handler_instance = g_object_ref (tp_chat);
1038 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1040 /* Are we in local-pending ? */
1043 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
1045 /* We are invited to a room */
1046 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
1047 tp_channel_get_identifier (channel));
1051 empathy_tp_contact_factory_get_from_handle (connection,
1052 inviter, event_manager_muc_invite_got_contact_cb,
1053 approval, NULL, G_OBJECT (self));
1057 display_invite_room_dialog (approval);
1063 /* We are not invited, approve the channel right now */
1064 tp_add_dispatch_operation_context_accept (context);
1066 approval->auto_approved = TRUE;
1067 event_manager_approval_approve (approval);
1071 /* 1-1 text channel, wait for the first message */
1072 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
1073 G_CALLBACK (event_manager_chat_message_received_cb), approval);
1075 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
1076 for (l = messages; l != NULL; l = g_list_next (l))
1078 EmpathyMessage *msg = l->data;
1080 event_manager_chat_message_received_cb (tp_chat, msg, approval);
1083 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
1085 EmpathyContact *contact;
1086 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account,
1089 approval->handler_instance = G_OBJECT (call);
1091 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
1093 if (contact == NULL)
1095 g_signal_connect (call, "notify::contact",
1096 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
1101 approval->contact = contact;
1102 event_manager_media_channel_got_contact (approval);
1106 else if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
1108 TpyCallChannel *call = TPY_CALL_CHANNEL (channel);
1111 approval->handler_instance = g_object_ref (call);
1113 id = tp_channel_get_identifier (channel);
1115 empathy_tp_contact_factory_get_from_id (connection, id,
1116 event_manager_call_channel_got_contact_cb,
1117 approval, NULL, G_OBJECT (self));
1119 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1122 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1124 approval->handler_instance = G_OBJECT (tp_file);
1126 handle = tp_channel_get_handle (channel, NULL);
1128 empathy_tp_contact_factory_get_from_handle (connection, handle,
1129 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1131 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1133 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1134 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1135 _("Password required"), approval,
1136 event_manager_auth_process_func, NULL);
1140 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1141 "Invalid channel type" };
1143 DEBUG ("Unknown channel type (%s), ignoring..",
1144 g_quark_to_string (channel_type));
1146 tp_add_dispatch_operation_context_fail (context, &error);
1151 tp_add_dispatch_operation_context_accept (context);
1155 event_pending_subscribe_func (EventPriv *event)
1157 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1159 event_remove (event);
1163 event_manager_pendings_changed_cb (EmpathyContactList *list,
1164 EmpathyContact *contact, EmpathyContact *actor,
1165 guint reason, gchar *message, gboolean is_pending,
1166 EmpathyEventManager *manager)
1168 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1169 gchar *header, *event_msg;
1175 for (l = priv->events; l; l = l->next)
1177 EventPriv *event = l->data;
1179 if (event->public.contact == contact &&
1180 event->func == event_pending_subscribe_func)
1182 event_remove (event);
1190 header = g_strdup_printf (
1191 _("%s would like permission to see when you are online"),
1192 empathy_contact_get_alias (contact));
1194 if (!EMP_STR_EMPTY (message))
1195 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1199 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1200 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1201 event_pending_subscribe_func, NULL);
1208 event_manager_presence_changed_cb (EmpathyContact *contact,
1209 TpConnectionPresenceType current,
1210 TpConnectionPresenceType previous,
1211 EmpathyEventManager *manager)
1213 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1215 EmpathyPresenceManager *presence_mgr;
1216 GtkWidget *window = empathy_main_window_dup ();
1218 account = empathy_contact_get_account (contact);
1219 presence_mgr = empathy_presence_manager_dup_singleton ();
1221 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1224 if (tp_connection_presence_type_cmp_availability (previous,
1225 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1227 /* contact was online */
1228 if (tp_connection_presence_type_cmp_availability (current,
1229 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1231 /* someone is logging off */
1232 empathy_sound_manager_play (priv->sound_mgr, window,
1233 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1235 if (g_settings_get_boolean (priv->gsettings_notif,
1236 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1238 event_manager_add (manager, NULL, contact,
1239 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1240 EMPATHY_IMAGE_AVATAR_DEFAULT,
1241 empathy_contact_get_alias (contact), _("Disconnected"),
1248 /* contact was offline */
1249 if (tp_connection_presence_type_cmp_availability (current,
1250 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1252 /* someone is logging in */
1253 empathy_sound_manager_play (priv->sound_mgr, window,
1254 EMPATHY_SOUND_CONTACT_CONNECTED);
1256 if (g_settings_get_boolean (priv->gsettings_notif,
1257 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1259 event_manager_add (manager, NULL, contact,
1260 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1261 EMPATHY_IMAGE_AVATAR_DEFAULT,
1262 empathy_contact_get_alias (contact), _("Connected"),
1269 g_object_unref (presence_mgr);
1270 g_object_unref (window);
1274 event_manager_members_changed_cb (EmpathyContactList *list,
1275 EmpathyContact *contact,
1276 EmpathyContact *actor,
1280 EmpathyEventManager *manager)
1283 g_signal_connect (contact, "presence-changed",
1284 G_CALLBACK (event_manager_presence_changed_cb), manager);
1286 g_signal_handlers_disconnect_by_func (contact,
1287 event_manager_presence_changed_cb, manager);
1291 event_manager_constructor (GType type,
1293 GObjectConstructParam *props)
1297 if (manager_singleton) {
1298 retval = g_object_ref (manager_singleton);
1300 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1301 (type, n_props, props);
1303 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1304 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1311 event_manager_finalize (GObject *object)
1313 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1315 if (priv->ringing > 0)
1316 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1318 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1319 g_slist_free (priv->events);
1320 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1321 g_slist_free (priv->approvals);
1322 g_object_unref (priv->contact_manager);
1323 g_object_unref (priv->approver);
1324 g_object_unref (priv->auth_approver);
1325 g_object_unref (priv->gsettings_notif);
1326 g_object_unref (priv->gsettings_ui);
1327 g_object_unref (priv->sound_mgr);
1331 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1333 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1335 object_class->finalize = event_manager_finalize;
1336 object_class->constructor = event_manager_constructor;
1338 signals[EVENT_ADDED] =
1339 g_signal_new ("event-added",
1340 G_TYPE_FROM_CLASS (klass),
1344 g_cclosure_marshal_VOID__POINTER,
1348 signals[EVENT_REMOVED] =
1349 g_signal_new ("event-removed",
1350 G_TYPE_FROM_CLASS (klass),
1354 g_cclosure_marshal_VOID__POINTER,
1355 G_TYPE_NONE, 1, G_TYPE_POINTER);
1357 signals[EVENT_UPDATED] =
1358 g_signal_new ("event-updated",
1359 G_TYPE_FROM_CLASS (klass),
1363 g_cclosure_marshal_VOID__POINTER,
1364 G_TYPE_NONE, 1, G_TYPE_POINTER);
1366 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1370 empathy_event_manager_init (EmpathyEventManager *manager)
1372 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1373 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1375 GError *error = NULL;
1376 EmpathyChannelFactory *factory;
1378 manager->priv = priv;
1380 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1381 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1383 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1385 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1386 g_signal_connect (priv->contact_manager, "pendings-changed",
1387 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1389 g_signal_connect (priv->contact_manager, "members-changed",
1390 G_CALLBACK (event_manager_members_changed_cb), manager);
1392 dbus = tp_dbus_daemon_dup (&error);
1395 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1396 g_error_free (error);
1400 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1401 approve_channels, manager, NULL);
1403 /* EmpathyTpChat relies on this feature being prepared */
1404 tp_base_client_add_connection_features_varargs (priv->approver,
1405 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1407 /* Private text channels */
1408 tp_base_client_take_approver_filter (priv->approver,
1410 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1411 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1414 /* Muc text channels */
1415 tp_base_client_take_approver_filter (priv->approver,
1417 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1418 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1422 tp_base_client_take_approver_filter (priv->approver,
1424 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1425 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1426 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1430 tp_base_client_take_approver_filter (priv->approver,
1432 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1433 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1434 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1436 tp_base_client_take_approver_filter (priv->approver,
1438 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1439 TPY_IFACE_CHANNEL_TYPE_CALL,
1440 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1443 /* I don't feel good about doing this, and I'm sorry, but the
1444 * capabilities connection feature is added earlier because it's
1445 * needed for EmpathyTpChat. If the capabilities feature is required
1446 * then preparing an auth channel (which of course appears in the
1447 * CONNECTING state) will never be prepared. So the options are
1448 * either to create another approver like I've done, or to port
1449 * EmpathyTpChat and its users to not depend on the connection being
1450 * prepared with capabilities. I chose the former, obviously. :-) */
1452 priv->auth_approver = tp_simple_approver_new (dbus,
1453 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1456 /* SASL auth channels */
1457 tp_base_client_take_approver_filter (priv->auth_approver,
1459 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1460 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1461 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1463 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1466 factory = empathy_channel_factory_dup ();
1468 tp_base_client_set_channel_factory (priv->approver,
1469 TP_CLIENT_CHANNEL_FACTORY (factory));
1471 if (!tp_base_client_register (priv->approver, &error))
1473 DEBUG ("Failed to register Approver: %s", error->message);
1474 g_error_free (error);
1477 if (!tp_base_client_register (priv->auth_approver, &error))
1479 DEBUG ("Failed to register auth Approver: %s", error->message);
1480 g_error_free (error);
1483 g_object_unref (factory);
1484 g_object_unref (dbus);
1487 EmpathyEventManager *
1488 empathy_event_manager_dup_singleton (void)
1490 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1494 empathy_event_manager_get_events (EmpathyEventManager *manager)
1496 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1498 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1500 return priv->events;
1504 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1506 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1508 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1510 return priv->events ? priv->events->data : NULL;
1514 empathy_event_activate (EmpathyEvent *event_public)
1516 EventPriv *event = (EventPriv *) event_public;
1518 g_return_if_fail (event_public != NULL);
1521 event->func (event);
1523 event_remove (event);
1527 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1529 EventPriv *event = (EventPriv *) event_public;
1531 g_return_if_fail (event_public != NULL);
1533 event->inhibit = TRUE;
1537 empathy_event_approve (EmpathyEvent *event_public)
1539 EventPriv *event = (EventPriv *) event_public;
1541 g_return_if_fail (event_public != NULL);
1543 event_manager_approval_approve (event->approval);
1547 empathy_event_decline (EmpathyEvent *event_public)
1549 EventPriv *event = (EventPriv *) event_public;
1551 g_return_if_fail (event_public != NULL);
1553 reject_approval (event->approval);