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 /* The time interval in milliseconds between 2 incoming rings */
62 #define MS_BETWEEN_RING 500
65 EmpathyEventManager *manager;
66 TpChannelDispatchOperation *operation;
67 gulong invalidated_handler;
68 /* Remove contact if applicable */
69 EmpathyContact *contact;
70 /* option signal handler and it's instance */
72 GObject *handler_instance;
73 /* optional accept widget */
75 /* Channel of the CDO that will be used during the approval */
76 TpChannel *main_channel;
77 gboolean auto_approved;
78 } EventManagerApproval;
81 TpBaseClient *approver;
82 TpBaseClient *auth_approver;
83 EmpathyContactManager *contact_manager;
85 /* Ongoing approvals */
90 GSettings *gsettings_notif;
91 GSettings *gsettings_ui;
93 EmpathySoundManager *sound_mgr;
94 } EmpathyEventManagerPriv;
96 typedef struct _EventPriv EventPriv;
97 typedef void (*EventFunc) (EventPriv *event);
101 EmpathyEventManager *manager;
102 EventManagerApproval *approval;
106 guint autoremove_timeout_id;
116 static guint signals[LAST_SIGNAL];
118 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
120 static EmpathyEventManager * manager_singleton = NULL;
122 static EventManagerApproval *
123 event_manager_approval_new (EmpathyEventManager *manager,
124 TpChannelDispatchOperation *operation,
125 TpChannel *main_channel)
127 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
128 result->operation = g_object_ref (operation);
129 result->manager = manager;
130 result->main_channel = g_object_ref (main_channel);
136 event_manager_approval_free (EventManagerApproval *approval)
138 g_signal_handler_disconnect (approval->operation,
139 approval->invalidated_handler);
140 g_object_unref (approval->operation);
142 g_object_unref (approval->main_channel);
144 if (approval->handler != 0)
145 g_signal_handler_disconnect (approval->handler_instance,
148 if (approval->handler_instance != NULL)
149 g_object_unref (approval->handler_instance);
151 if (approval->contact != NULL)
152 g_object_unref (approval->contact);
154 if (approval->dialog != NULL)
156 gtk_widget_destroy (approval->dialog);
159 g_slice_free (EventManagerApproval, approval);
163 event_free (EventPriv *event)
165 g_free (event->public.icon_name);
166 g_free (event->public.header);
167 g_free (event->public.message);
169 if (event->autoremove_timeout_id != 0)
170 g_source_remove (event->autoremove_timeout_id);
172 tp_clear_object (&(event->public.contact));
173 tp_clear_object (&(event->public.account));
175 g_slice_free (EventPriv, event);
179 event_remove (EventPriv *event)
181 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
183 DEBUG ("Removing event %p", event);
185 priv->events = g_slist_remove (priv->events, event);
186 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
191 empathy_event_remove (EmpathyEvent *event_public)
193 EventPriv *event = (EventPriv *) event_public;
195 event_remove (event);
199 autoremove_event_timeout_cb (EventPriv *event)
201 event->autoremove_timeout_id = 0;
202 event_remove (event);
207 display_notify_area (EmpathyEventManager *self)
209 EmpathyEventManagerPriv *priv = GET_PRIV (self);
211 return g_settings_get_boolean (priv->gsettings_ui,
212 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
216 event_manager_add (EmpathyEventManager *manager,
218 EmpathyContact *contact,
219 EmpathyEventType type,
220 const gchar *icon_name,
222 const gchar *message,
223 EventManagerApproval *approval,
227 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
230 event = g_slice_new0 (EventPriv);
231 event->public.account = account != NULL ? g_object_ref (account) : NULL;
232 event->public.contact = contact ? g_object_ref (contact) : NULL;
233 event->public.type = type;
234 event->public.icon_name = g_strdup (icon_name);
235 event->public.header = g_strdup (header);
236 event->public.message = g_strdup (message);
237 event->public.must_ack = (func != NULL);
238 event->inhibit = FALSE;
240 event->user_data = user_data;
241 event->manager = manager;
242 event->approval = approval;
244 DEBUG ("Adding event %p", event);
245 priv->events = g_slist_prepend (priv->events, event);
247 if (!display_notify_area (manager))
249 /* Don't fire the 'event-added' signal as we activate the event now */
250 if (approval != NULL)
251 approval->auto_approved = TRUE;
253 empathy_event_activate (&event->public);
257 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
259 if (!event->public.must_ack)
261 event->autoremove_timeout_id = g_timeout_add_seconds (
262 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
268 handle_with_cb (GObject *source,
269 GAsyncResult *result,
272 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
273 GError *error = NULL;
275 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
277 DEBUG ("HandleWith failed: %s\n", error->message);
278 g_error_free (error);
283 handle_with_time_cb (GObject *source,
284 GAsyncResult *result,
287 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
288 GError *error = NULL;
290 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
293 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
295 EventManagerApproval *approval = user_data;
297 DEBUG ("HandleWithTime() is not implemented, falling back to "
298 "HandleWith(). Please upgrade to telepathy-mission-control "
301 tp_channel_dispatch_operation_handle_with_async (approval->operation,
302 NULL, handle_with_cb, approval);
306 DEBUG ("HandleWithTime failed: %s\n", error->message);
308 g_error_free (error);
313 event_manager_approval_approve (EventManagerApproval *approval)
317 if (approval->auto_approved)
319 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
323 timestamp = empathy_get_current_action_time ();
326 g_assert (approval->operation != NULL);
328 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
329 NULL, timestamp, handle_with_time_cb, approval);
333 event_channel_process_func (EventPriv *event)
335 event_manager_approval_approve (event->approval);
339 event_text_channel_process_func (EventPriv *event)
341 EmpathyTpChat *tp_chat;
343 if (event->approval->handler != 0)
345 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
347 g_signal_handler_disconnect (tp_chat, event->approval->handler);
348 event->approval->handler = 0;
351 event_manager_approval_approve (event->approval);
355 event_lookup_by_approval (EmpathyEventManager *manager,
356 EventManagerApproval *approval)
358 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
360 EventPriv *retval = NULL;
362 for (l = priv->events; l; l = l->next)
364 EventPriv *event = l->data;
366 if (event->approval == approval)
377 event_update (EmpathyEventManager *manager, EventPriv *event,
378 const char *icon_name, const char *header, const char *msg)
380 g_free (event->public.icon_name);
381 g_free (event->public.header);
382 g_free (event->public.message);
384 event->public.icon_name = g_strdup (icon_name);
385 event->public.header = g_strdup (header);
386 event->public.message = g_strdup (msg);
388 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
392 reject_channel_claim_cb (GObject *source,
393 GAsyncResult *result,
396 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
397 GError *error = NULL;
399 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
401 DEBUG ("Failed to claim channel: %s", error->message);
403 g_error_free (error);
407 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
409 empathy_tp_streamed_media_close (user_data);
411 else if (TPY_IS_CALL_CHANNEL (user_data))
413 tpy_call_channel_hangup_async (user_data,
414 TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
416 tp_channel_close_async (user_data, NULL, NULL);
418 else if (EMPATHY_IS_TP_CHAT (user_data))
420 empathy_tp_chat_leave (user_data, "");
422 else if (EMPATHY_IS_TP_FILE (user_data))
424 empathy_tp_file_close (user_data);
428 g_object_unref (user_data);
432 reject_auth_channel_claim_cb (GObject *source,
433 GAsyncResult *result,
436 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
437 GError *error = NULL;
439 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
441 DEBUG ("Failed to claim channel: %s", error->message);
443 g_error_free (error);
447 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
448 NULL, NULL, NULL, NULL);
452 reject_approval (EventManagerApproval *approval)
454 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
456 /* We have to claim the channel before closing it */
458 /* Unfortunately, we need to special case the auth channels for the
459 * time being as they don't have a wrapper object handler in
460 * approval->handler_instance as they're not actually handled by
461 * this process, so we can just use a noddy callback to call Close()
463 if (approval->handler_instance != NULL)
465 tp_channel_dispatch_operation_claim_with_async (approval->operation,
466 priv->approver, reject_channel_claim_cb,
467 g_object_ref (approval->handler_instance));
469 else if (tp_channel_get_channel_type_id (approval->main_channel)
470 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
472 tp_channel_dispatch_operation_claim_with_async (approval->operation,
473 priv->auth_approver, reject_auth_channel_claim_cb,
474 approval->main_channel);
479 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
480 gint response, gpointer user_data)
482 EventManagerApproval *approval = user_data;
484 gtk_widget_destroy (approval->dialog);
485 approval->dialog = NULL;
487 if (response != GTK_RESPONSE_ACCEPT)
489 reject_approval (approval);
493 event_manager_approval_approve (approval);
498 event_channel_process_voip_func (EventPriv *event)
506 if (event->approval->dialog != NULL)
508 gtk_window_present (GTK_WINDOW (event->approval->dialog));
512 if (event->public.type == EMPATHY_EVENT_TYPE_VOIP)
514 EmpathyTpStreamedMedia *call;
515 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
516 video = empathy_tp_streamed_media_has_initial_video (call);
518 else if (event->public.type == EMPATHY_EVENT_TYPE_CALL)
520 TpyCallChannel *call;
521 call = TPY_CALL_CHANNEL (event->approval->handler_instance);
522 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
526 g_warning ("Unknown event type: %d", event->public.type);
530 dialog = gtk_message_dialog_new (NULL, 0,
531 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
532 video ? _("Incoming video call"): _("Incoming call"));
534 gtk_message_dialog_format_secondary_text (
535 GTK_MESSAGE_DIALOG (dialog), video ?
536 _("%s is video calling you. Do you want to answer?"):
537 _("%s is calling you. Do you want to answer?"),
538 empathy_contact_get_alias (event->approval->contact));
540 title = g_strdup_printf (_("Incoming call from %s"),
541 empathy_contact_get_alias (event->approval->contact));
543 gtk_window_set_title (GTK_WINDOW (dialog), title);
546 /* Set image of the dialog */
549 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
550 GTK_ICON_SIZE_DIALOG);
554 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
555 GTK_ICON_SIZE_DIALOG);
558 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
559 gtk_widget_show (image);
561 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
564 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
565 _("_Reject"), GTK_RESPONSE_REJECT);
566 image = gtk_image_new_from_icon_name ("call-stop",
567 GTK_ICON_SIZE_BUTTON);
568 gtk_button_set_image (GTK_BUTTON (button), image);
570 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
571 _("_Answer"), GTK_RESPONSE_ACCEPT);
573 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
574 gtk_button_set_image (GTK_BUTTON (button), image);
576 g_signal_connect (dialog, "response",
577 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
580 gtk_widget_show (dialog);
582 event->approval->dialog = dialog;
586 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
587 EmpathyMessage *message,
588 EventManagerApproval *approval)
591 EmpathyContact *sender;
595 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
597 /* try to update the event if it's referring to a chat which is already in the
599 event = event_lookup_by_approval (approval->manager, approval);
601 sender = empathy_message_get_sender (message);
603 /* We only want to show incoming messages */
604 if (empathy_contact_is_user (sender))
607 header = empathy_contact_get_alias (sender);
608 msg = empathy_message_get_body (message);
611 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
614 event_manager_add (approval->manager, NULL, sender,
615 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
616 approval, event_text_channel_process_func, NULL);
618 window = empathy_main_window_dup ();
620 empathy_sound_manager_play (priv->sound_mgr, window,
621 EMPATHY_SOUND_CONVERSATION_NEW);
623 g_object_unref (window);
627 event_manager_approval_done (EventManagerApproval *approval)
629 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
632 if (approval->operation != NULL)
636 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
638 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
639 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
642 if (priv->ringing == 0)
643 empathy_sound_manager_stop (priv->sound_mgr,
644 EMPATHY_SOUND_PHONE_INCOMING);
648 priv->approvals = g_slist_remove (priv->approvals, approval);
650 for (l = priv->events; l; l = l->next)
652 EventPriv *event = l->data;
654 if (event->approval == approval)
656 event_remove (event);
661 event_manager_approval_free (approval);
665 cdo_invalidated_cb (TpProxy *cdo,
669 EventManagerApproval *approval)
671 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
673 event_manager_approval_done (approval);
677 event_manager_call_state_changed_cb (TpyCallChannel *call,
680 const GValueArray *call_state_reason,
681 GHashTable *call_state_details,
682 EventManagerApproval *approval)
684 if (state == TPY_CALL_STATE_ENDED)
686 DEBUG ("Call ended, seems we missed it :/");
687 reject_approval (approval);
692 event_manager_call_channel_got_contact_cb (TpConnection *connection,
693 EmpathyContact *contact,
698 EventManagerApproval *approval = (EventManagerApproval *) user_data;
699 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
701 TpyCallChannel *call;
705 call = TPY_CALL_CHANNEL (approval->handler_instance);
709 DEBUG ("Can't get the contact for the call.. Rejecting?");
710 reject_approval (approval);
714 if (tpy_call_channel_get_state (call, NULL, NULL) == TPY_CALL_STATE_ENDED)
716 DEBUG ("Call already ended, seems we missed it :/");
717 reject_approval (approval);
721 approval->handler = g_signal_connect (call, "state-changed",
722 G_CALLBACK (event_manager_call_state_changed_cb), approval);
724 window = empathy_main_window_dup ();
725 approval->contact = g_object_ref (contact);
727 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
729 header = g_strdup_printf (
730 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
731 empathy_contact_get_alias (approval->contact));
733 event_manager_add (approval->manager, NULL,
734 approval->contact, EMPATHY_EVENT_TYPE_CALL,
735 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
736 header, NULL, approval,
737 event_channel_process_voip_func, NULL);
742 if (priv->ringing == 1)
743 empathy_sound_manager_start_playing (priv->sound_mgr, window,
744 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
746 g_object_unref (window);
750 event_manager_media_channel_got_contact (EventManagerApproval *approval)
752 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
753 GtkWidget *window = empathy_main_window_dup ();
755 EmpathyTpStreamedMedia *call;
758 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
760 video = empathy_tp_streamed_media_has_initial_video (call);
762 header = g_strdup_printf (
763 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
764 empathy_contact_get_alias (approval->contact));
766 event_manager_add (approval->manager, NULL,
767 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
768 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
769 header, NULL, approval,
770 event_channel_process_voip_func, NULL);
775 if (priv->ringing == 1)
776 empathy_sound_manager_start_playing (priv->sound_mgr, window,
777 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
779 g_object_unref (window);
783 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
784 GParamSpec *param, EventManagerApproval *approval)
786 EmpathyContact *contact;
788 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
793 approval->contact = contact;
794 event_manager_media_channel_got_contact (approval);
798 invite_dialog_response_cb (GtkDialog *dialog,
800 EventManagerApproval *approval)
802 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
803 approval->dialog = NULL;
805 if (response != GTK_RESPONSE_OK)
808 DEBUG ("Muc invitation rejected");
810 reject_approval (approval);
815 DEBUG ("Muc invitation accepted");
817 /* We'll join the room when handling the channel */
818 event_manager_approval_approve (approval);
822 event_room_channel_process_func (EventPriv *event)
824 GtkWidget *dialog, *button, *image;
825 TpChannel *channel = event->approval->main_channel;
828 if (event->approval->dialog != NULL)
830 gtk_window_present (GTK_WINDOW (event->approval->dialog));
835 dialog = gtk_message_dialog_new (NULL, 0,
836 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
838 title = g_strdup_printf (_("Invitation to join %s"),
839 tp_channel_get_identifier (channel));
841 gtk_window_set_title (GTK_WINDOW (dialog), title);
844 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
845 _("%s is inviting you to join %s"),
846 empathy_contact_get_alias (event->approval->contact),
847 tp_channel_get_identifier (channel));
849 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
852 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
853 _("_Decline"), GTK_RESPONSE_CANCEL);
854 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
855 gtk_button_set_image (GTK_BUTTON (button), image);
857 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
858 _("_Join"), GTK_RESPONSE_OK);
859 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
860 gtk_button_set_image (GTK_BUTTON (button), image);
862 g_signal_connect (dialog, "response",
863 G_CALLBACK (invite_dialog_response_cb), event->approval);
865 gtk_widget_show (dialog);
867 event->approval->dialog = dialog;
871 display_invite_room_dialog (EventManagerApproval *approval)
873 GtkWidget *window = empathy_main_window_dup ();
874 const gchar *invite_msg;
876 TpHandle self_handle;
877 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
879 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
880 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
881 NULL, NULL, &invite_msg);
883 if (approval->contact != NULL)
885 msg = g_strdup_printf (_("%s invited you to join %s"),
886 empathy_contact_get_alias (approval->contact),
887 tp_channel_get_identifier (approval->main_channel));
891 msg = g_strdup_printf (_("You have been invited to join %s"),
892 tp_channel_get_identifier (approval->main_channel));
895 event_manager_add (approval->manager, NULL,
896 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
897 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
898 event_room_channel_process_func, NULL);
900 empathy_sound_manager_play (priv->sound_mgr, window,
901 EMPATHY_SOUND_CONVERSATION_NEW);
904 g_object_unref (window);
908 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
909 EmpathyContact *contact,
914 EventManagerApproval *approval = (EventManagerApproval *) user_data;
918 DEBUG ("Error: %s", error->message);
922 approval->contact = g_object_ref (contact);
925 display_invite_room_dialog (approval);
929 event_manager_ft_got_contact_cb (TpConnection *connection,
930 EmpathyContact *contact,
935 EventManagerApproval *approval = (EventManagerApproval *) user_data;
936 GtkWidget *window = empathy_main_window_dup ();
938 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
940 approval->contact = g_object_ref (contact);
942 header = g_strdup_printf (_("Incoming file transfer from %s"),
943 empathy_contact_get_alias (approval->contact));
945 event_manager_add (approval->manager, NULL,
946 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
947 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
948 approval, event_channel_process_func, NULL);
950 /* FIXME better sound for incoming file transfers ?*/
951 empathy_sound_manager_play (priv->sound_mgr, window,
952 EMPATHY_SOUND_CONVERSATION_NEW);
955 g_object_unref (window);
959 event_manager_auth_process_func (EventPriv *event)
961 empathy_event_approve ((EmpathyEvent *) event);
964 /* If there is a file-transfer, media, or auth channel consider it as
967 find_main_channel (GList *channels)
970 TpChannel *text = NULL;
972 for (l = channels; l != NULL; l = g_list_next (l))
974 TpChannel *channel = l->data;
977 if (tp_proxy_get_invalidated (channel) != NULL)
980 channel_type = tp_channel_get_channel_type_id (channel);
982 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
983 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL ||
984 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
985 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
988 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
996 approve_channels (TpSimpleApprover *approver,
998 TpConnection *connection,
1000 TpChannelDispatchOperation *dispatch_operation,
1001 TpAddDispatchOperationContext *context,
1004 EmpathyEventManager *self = user_data;
1005 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1007 EventManagerApproval *approval;
1008 GQuark channel_type;
1010 channel = find_main_channel (channels);
1011 if (channel == NULL)
1013 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1014 "Unknown channel type" };
1016 DEBUG ("Failed to find the main channel; ignoring");
1018 tp_add_dispatch_operation_context_fail (context, &error);
1022 approval = event_manager_approval_new (self, dispatch_operation, channel);
1023 priv->approvals = g_slist_prepend (priv->approvals, approval);
1025 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1026 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1028 channel_type = tp_channel_get_channel_type_id (channel);
1030 if (TP_IS_TEXT_CHANNEL (channel))
1032 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
1033 GList *messages, *l;
1035 approval->handler_instance = g_object_ref (tp_chat);
1037 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1039 /* Are we in local-pending ? */
1042 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
1044 /* We are invited to a room */
1045 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
1046 tp_channel_get_identifier (channel));
1050 empathy_tp_contact_factory_get_from_handle (connection,
1051 inviter, event_manager_muc_invite_got_contact_cb,
1052 approval, NULL, G_OBJECT (self));
1056 display_invite_room_dialog (approval);
1062 /* We are not invited, approve the channel right now */
1063 tp_add_dispatch_operation_context_accept (context);
1065 approval->auto_approved = TRUE;
1066 event_manager_approval_approve (approval);
1070 /* 1-1 text channel, wait for the first message */
1071 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
1072 G_CALLBACK (event_manager_chat_message_received_cb), approval);
1074 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
1075 for (l = messages; l != NULL; l = g_list_next (l))
1077 EmpathyMessage *msg = l->data;
1079 event_manager_chat_message_received_cb (tp_chat, msg, approval);
1082 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
1084 EmpathyContact *contact;
1085 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account,
1088 approval->handler_instance = G_OBJECT (call);
1090 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
1092 if (contact == NULL)
1094 g_signal_connect (call, "notify::contact",
1095 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
1100 approval->contact = contact;
1101 event_manager_media_channel_got_contact (approval);
1105 else if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
1107 TpyCallChannel *call = TPY_CALL_CHANNEL (channel);
1110 approval->handler_instance = g_object_ref (call);
1112 id = tp_channel_get_identifier (channel);
1114 empathy_tp_contact_factory_get_from_id (connection, id,
1115 event_manager_call_channel_got_contact_cb,
1116 approval, NULL, G_OBJECT (self));
1118 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1121 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1123 approval->handler_instance = G_OBJECT (tp_file);
1125 handle = tp_channel_get_handle (channel, NULL);
1127 empathy_tp_contact_factory_get_from_handle (connection, handle,
1128 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1130 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1132 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1133 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1134 _("Password required"), approval,
1135 event_manager_auth_process_func, NULL);
1139 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1140 "Invalid channel type" };
1142 DEBUG ("Unknown channel type (%s), ignoring..",
1143 g_quark_to_string (channel_type));
1145 tp_add_dispatch_operation_context_fail (context, &error);
1150 tp_add_dispatch_operation_context_accept (context);
1154 event_pending_subscribe_func (EventPriv *event)
1156 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1158 event_remove (event);
1162 event_manager_pendings_changed_cb (EmpathyContactList *list,
1163 EmpathyContact *contact, EmpathyContact *actor,
1164 guint reason, gchar *message, gboolean is_pending,
1165 EmpathyEventManager *manager)
1167 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1168 gchar *header, *event_msg;
1174 for (l = priv->events; l; l = l->next)
1176 EventPriv *event = l->data;
1178 if (event->public.contact == contact &&
1179 event->func == event_pending_subscribe_func)
1181 event_remove (event);
1189 header = g_strdup_printf (
1190 _("%s would like permission to see when you are online"),
1191 empathy_contact_get_alias (contact));
1193 if (!EMP_STR_EMPTY (message))
1194 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1198 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1199 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1200 event_pending_subscribe_func, NULL);
1207 event_manager_presence_changed_cb (EmpathyContact *contact,
1208 TpConnectionPresenceType current,
1209 TpConnectionPresenceType previous,
1210 EmpathyEventManager *manager)
1212 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1214 EmpathyPresenceManager *presence_mgr;
1215 GtkWidget *window = empathy_main_window_dup ();
1217 account = empathy_contact_get_account (contact);
1218 presence_mgr = empathy_presence_manager_dup_singleton ();
1220 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1223 if (tp_connection_presence_type_cmp_availability (previous,
1224 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1226 /* contact was online */
1227 if (tp_connection_presence_type_cmp_availability (current,
1228 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1230 /* someone is logging off */
1231 empathy_sound_manager_play (priv->sound_mgr, window,
1232 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1234 if (g_settings_get_boolean (priv->gsettings_notif,
1235 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1237 event_manager_add (manager, NULL, contact,
1238 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1239 EMPATHY_IMAGE_AVATAR_DEFAULT,
1240 empathy_contact_get_alias (contact), _("Disconnected"),
1247 /* contact was offline */
1248 if (tp_connection_presence_type_cmp_availability (current,
1249 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1251 /* someone is logging in */
1252 empathy_sound_manager_play (priv->sound_mgr, window,
1253 EMPATHY_SOUND_CONTACT_CONNECTED);
1255 if (g_settings_get_boolean (priv->gsettings_notif,
1256 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1258 event_manager_add (manager, NULL, contact,
1259 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1260 EMPATHY_IMAGE_AVATAR_DEFAULT,
1261 empathy_contact_get_alias (contact), _("Connected"),
1268 g_object_unref (presence_mgr);
1269 g_object_unref (window);
1273 event_manager_members_changed_cb (EmpathyContactList *list,
1274 EmpathyContact *contact,
1275 EmpathyContact *actor,
1279 EmpathyEventManager *manager)
1282 g_signal_connect (contact, "presence-changed",
1283 G_CALLBACK (event_manager_presence_changed_cb), manager);
1285 g_signal_handlers_disconnect_by_func (contact,
1286 event_manager_presence_changed_cb, manager);
1290 event_manager_constructor (GType type,
1292 GObjectConstructParam *props)
1296 if (manager_singleton) {
1297 retval = g_object_ref (manager_singleton);
1299 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1300 (type, n_props, props);
1302 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1303 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1310 event_manager_finalize (GObject *object)
1312 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1314 if (priv->ringing > 0)
1315 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1317 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1318 g_slist_free (priv->events);
1319 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1320 g_slist_free (priv->approvals);
1321 g_object_unref (priv->contact_manager);
1322 g_object_unref (priv->approver);
1323 g_object_unref (priv->auth_approver);
1324 g_object_unref (priv->gsettings_notif);
1325 g_object_unref (priv->gsettings_ui);
1326 g_object_unref (priv->sound_mgr);
1330 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1332 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1334 object_class->finalize = event_manager_finalize;
1335 object_class->constructor = event_manager_constructor;
1337 signals[EVENT_ADDED] =
1338 g_signal_new ("event-added",
1339 G_TYPE_FROM_CLASS (klass),
1343 g_cclosure_marshal_VOID__POINTER,
1347 signals[EVENT_REMOVED] =
1348 g_signal_new ("event-removed",
1349 G_TYPE_FROM_CLASS (klass),
1353 g_cclosure_marshal_VOID__POINTER,
1354 G_TYPE_NONE, 1, G_TYPE_POINTER);
1356 signals[EVENT_UPDATED] =
1357 g_signal_new ("event-updated",
1358 G_TYPE_FROM_CLASS (klass),
1362 g_cclosure_marshal_VOID__POINTER,
1363 G_TYPE_NONE, 1, G_TYPE_POINTER);
1365 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1369 empathy_event_manager_init (EmpathyEventManager *manager)
1371 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1372 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1374 GError *error = NULL;
1375 EmpathyChannelFactory *factory;
1377 manager->priv = priv;
1379 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1380 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1382 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1384 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1385 g_signal_connect (priv->contact_manager, "pendings-changed",
1386 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1388 g_signal_connect (priv->contact_manager, "members-changed",
1389 G_CALLBACK (event_manager_members_changed_cb), manager);
1391 dbus = tp_dbus_daemon_dup (&error);
1394 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1395 g_error_free (error);
1399 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1400 approve_channels, manager, NULL);
1402 /* EmpathyTpChat relies on this feature being prepared */
1403 tp_base_client_add_connection_features_varargs (priv->approver,
1404 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1406 /* Private text channels */
1407 tp_base_client_take_approver_filter (priv->approver,
1409 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1410 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1413 /* Muc text channels */
1414 tp_base_client_take_approver_filter (priv->approver,
1416 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1417 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1421 tp_base_client_take_approver_filter (priv->approver,
1423 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1424 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1425 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1429 tp_base_client_take_approver_filter (priv->approver,
1431 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1432 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1433 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1435 tp_base_client_take_approver_filter (priv->approver,
1437 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1438 TPY_IFACE_CHANNEL_TYPE_CALL,
1439 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1442 /* I don't feel good about doing this, and I'm sorry, but the
1443 * capabilities connection feature is added earlier because it's
1444 * needed for EmpathyTpChat. If the capabilities feature is required
1445 * then preparing an auth channel (which of course appears in the
1446 * CONNECTING state) will never be prepared. So the options are
1447 * either to create another approver like I've done, or to port
1448 * EmpathyTpChat and its users to not depend on the connection being
1449 * prepared with capabilities. I chose the former, obviously. :-) */
1451 priv->auth_approver = tp_simple_approver_new (dbus,
1452 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1455 /* SASL auth channels */
1456 tp_base_client_take_approver_filter (priv->auth_approver,
1458 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1459 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1460 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1462 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1465 factory = empathy_channel_factory_dup ();
1467 tp_base_client_set_channel_factory (priv->approver,
1468 TP_CLIENT_CHANNEL_FACTORY (factory));
1470 if (!tp_base_client_register (priv->approver, &error))
1472 DEBUG ("Failed to register Approver: %s", error->message);
1473 g_error_free (error);
1476 if (!tp_base_client_register (priv->auth_approver, &error))
1478 DEBUG ("Failed to register auth Approver: %s", error->message);
1479 g_error_free (error);
1482 g_object_unref (factory);
1483 g_object_unref (dbus);
1486 EmpathyEventManager *
1487 empathy_event_manager_dup_singleton (void)
1489 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1493 empathy_event_manager_get_events (EmpathyEventManager *manager)
1495 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1497 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1499 return priv->events;
1503 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1505 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1507 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1509 return priv->events ? priv->events->data : NULL;
1513 empathy_event_activate (EmpathyEvent *event_public)
1515 EventPriv *event = (EventPriv *) event_public;
1517 g_return_if_fail (event_public != NULL);
1520 event->func (event);
1522 event_remove (event);
1526 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1528 EventPriv *event = (EventPriv *) event_public;
1530 g_return_if_fail (event_public != NULL);
1532 event->inhibit = TRUE;
1536 empathy_event_approve (EmpathyEvent *event_public)
1538 EventPriv *event = (EventPriv *) event_public;
1540 g_return_if_fail (event_public != NULL);
1542 event_manager_approval_approve (event->approval);
1546 empathy_event_decline (EmpathyEvent *event_public)
1548 EventPriv *event = (EventPriv *) event_public;
1550 g_return_if_fail (event_public != NULL);
1552 reject_approval (event->approval);