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-presence-manager.h>
35 #include <libempathy/empathy-tp-contact-factory.h>
36 #include <libempathy/empathy-connection-aggregator.h>
37 #include <libempathy/empathy-tp-chat.h>
38 #include <libempathy/empathy-tp-streamed-media.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 #define ACCEPT_WITHOUT_VIDEO 1
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 EmpathyConnectionAggregator *conn_aggregator;
85 /* Ongoing approvals */
90 GSettings *gsettings_notif;
91 GSettings *gsettings_ui;
93 EmpathySoundManager *sound_mgr;
95 /* TpContact -> EmpathyContact */
97 } EmpathyEventManagerPriv;
99 typedef struct _EventPriv EventPriv;
100 typedef void (*EventFunc) (EventPriv *event);
104 EmpathyEventManager *manager;
105 EventManagerApproval *approval;
109 guint autoremove_timeout_id;
119 static guint signals[LAST_SIGNAL];
121 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
123 static EmpathyEventManager * manager_singleton = NULL;
125 static EventManagerApproval *
126 event_manager_approval_new (EmpathyEventManager *manager,
127 TpChannelDispatchOperation *operation,
128 TpChannel *main_channel)
130 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
131 result->operation = g_object_ref (operation);
132 result->manager = manager;
133 result->main_channel = g_object_ref (main_channel);
139 event_manager_approval_free (EventManagerApproval *approval)
141 g_signal_handler_disconnect (approval->operation,
142 approval->invalidated_handler);
143 g_object_unref (approval->operation);
145 g_object_unref (approval->main_channel);
147 if (approval->handler != 0)
148 g_signal_handler_disconnect (approval->handler_instance,
151 if (approval->handler_instance != NULL)
152 g_object_unref (approval->handler_instance);
154 if (approval->contact != NULL)
155 g_object_unref (approval->contact);
157 if (approval->dialog != NULL)
159 gtk_widget_destroy (approval->dialog);
162 g_slice_free (EventManagerApproval, approval);
166 event_free (EventPriv *event)
168 g_free (event->public.icon_name);
169 g_free (event->public.header);
170 g_free (event->public.message);
172 if (event->autoremove_timeout_id != 0)
173 g_source_remove (event->autoremove_timeout_id);
175 tp_clear_object (&(event->public.contact));
176 tp_clear_object (&(event->public.account));
178 g_slice_free (EventPriv, event);
182 event_remove (EventPriv *event)
184 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
186 DEBUG ("Removing event %p", event);
188 priv->events = g_slist_remove (priv->events, event);
189 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
194 empathy_event_remove (EmpathyEvent *event_public)
196 EventPriv *event = (EventPriv *) event_public;
198 event_remove (event);
202 autoremove_event_timeout_cb (EventPriv *event)
204 event->autoremove_timeout_id = 0;
205 event_remove (event);
210 display_notify_area (EmpathyEventManager *self)
212 EmpathyEventManagerPriv *priv = GET_PRIV (self);
214 return g_settings_get_boolean (priv->gsettings_ui,
215 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
219 event_manager_add (EmpathyEventManager *manager,
221 EmpathyContact *contact,
222 EmpathyEventType type,
223 const gchar *icon_name,
225 const gchar *message,
226 EventManagerApproval *approval,
230 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
233 event = g_slice_new0 (EventPriv);
234 event->public.account = account != NULL ? g_object_ref (account) : NULL;
235 event->public.contact = contact ? g_object_ref (contact) : NULL;
236 event->public.type = type;
237 event->public.icon_name = g_strdup (icon_name);
238 event->public.header = g_strdup (header);
239 event->public.message = g_strdup (message);
240 event->public.must_ack = (func != NULL);
241 if (approval != NULL)
242 event->public.handler_instance = approval->handler_instance;
243 event->inhibit = FALSE;
245 event->user_data = user_data;
246 event->manager = manager;
247 event->approval = approval;
249 DEBUG ("Adding event %p", event);
250 priv->events = g_slist_prepend (priv->events, event);
252 if (!display_notify_area (manager))
254 /* Don't fire the 'event-added' signal as we activate the event now */
255 if (approval != NULL)
256 approval->auto_approved = TRUE;
258 empathy_event_activate (&event->public);
262 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
264 if (!event->public.must_ack)
266 event->autoremove_timeout_id = g_timeout_add_seconds (
267 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
273 handle_with_cb (GObject *source,
274 GAsyncResult *result,
277 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
278 GError *error = NULL;
280 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
282 DEBUG ("HandleWith failed: %s\n", error->message);
283 g_error_free (error);
288 handle_with_time_cb (GObject *source,
289 GAsyncResult *result,
292 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
293 GError *error = NULL;
295 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
298 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
300 EventManagerApproval *approval = user_data;
302 DEBUG ("HandleWithTime() is not implemented, falling back to "
303 "HandleWith(). Please upgrade to telepathy-mission-control "
306 tp_channel_dispatch_operation_handle_with_async (approval->operation,
307 NULL, handle_with_cb, approval);
311 DEBUG ("HandleWithTime failed: %s\n", error->message);
313 g_error_free (error);
318 event_manager_approval_approve (EventManagerApproval *approval)
322 if (approval->auto_approved)
324 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
328 timestamp = empathy_get_current_action_time ();
331 g_assert (approval->operation != NULL);
333 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
334 NULL, timestamp, handle_with_time_cb, approval);
338 event_channel_process_func (EventPriv *event)
340 event_manager_approval_approve (event->approval);
344 event_text_channel_process_func (EventPriv *event)
346 EmpathyTpChat *tp_chat;
348 if (event->approval->handler != 0)
350 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
352 g_signal_handler_disconnect (tp_chat, event->approval->handler);
353 event->approval->handler = 0;
356 event_manager_approval_approve (event->approval);
360 event_lookup_by_approval (EmpathyEventManager *manager,
361 EventManagerApproval *approval)
363 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
365 EventPriv *retval = NULL;
367 for (l = priv->events; l; l = l->next)
369 EventPriv *event = l->data;
371 if (event->approval == approval)
382 event_update (EmpathyEventManager *manager, EventPriv *event,
383 const char *icon_name, const char *header, const char *msg)
385 g_free (event->public.icon_name);
386 g_free (event->public.header);
387 g_free (event->public.message);
389 event->public.icon_name = g_strdup (icon_name);
390 event->public.header = g_strdup (header);
391 event->public.message = g_strdup (msg);
393 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
397 reject_channel_claim_cb (GObject *source,
398 GAsyncResult *result,
401 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
402 GError *error = NULL;
404 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
406 DEBUG ("Failed to claim channel: %s", error->message);
408 g_error_free (error);
412 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
414 empathy_tp_streamed_media_close (user_data);
416 else if (TPY_IS_CALL_CHANNEL (user_data))
418 tpy_call_channel_hangup_async (user_data,
419 TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
421 tp_channel_close_async (user_data, NULL, NULL);
423 else if (EMPATHY_IS_TP_CHAT (user_data))
425 empathy_tp_chat_leave (user_data, "");
427 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
429 tp_channel_close_async (user_data, NULL, NULL);
433 g_object_unref (user_data);
437 reject_approval (EventManagerApproval *approval)
439 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
441 /* We have to claim the channel before closing it */
443 /* Unfortunately, we need to special case the auth channels for the
444 * time being as they don't have a wrapper object handler in
445 * approval->handler_instance as they're not actually handled by
446 * this process, so we can just use a noddy callback to call Close()
448 if (approval->handler_instance != NULL)
450 tp_channel_dispatch_operation_claim_with_async (approval->operation,
451 priv->approver, reject_channel_claim_cb,
452 g_object_ref (approval->handler_instance));
454 else if (tp_channel_get_channel_type_id (approval->main_channel)
455 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
457 tp_channel_dispatch_operation_close_channels_async (approval->operation,
463 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
464 gint response, gpointer user_data)
466 EventManagerApproval *approval = user_data;
468 gtk_widget_destroy (approval->dialog);
469 approval->dialog = NULL;
471 if (response == GTK_RESPONSE_ACCEPT)
473 event_manager_approval_approve (approval);
475 else if (response == ACCEPT_WITHOUT_VIDEO)
477 tpy_call_channel_send_video (TPY_CALL_CHANNEL (approval->main_channel),
479 event_manager_approval_approve (approval);
483 reject_approval (approval);
488 event_channel_process_voip_func (EventPriv *event)
495 EmpathyEventType etype = event->public.type;
497 if (event->approval->dialog != NULL)
499 gtk_window_present (GTK_WINDOW (event->approval->dialog));
503 if (etype == EMPATHY_EVENT_TYPE_VOIP)
505 EmpathyTpStreamedMedia *call;
506 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
507 video = empathy_tp_streamed_media_has_initial_video (call);
509 else if (etype == EMPATHY_EVENT_TYPE_CALL)
511 TpyCallChannel *call;
512 call = TPY_CALL_CHANNEL (event->approval->handler_instance);
513 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
517 g_warning ("Unknown event type: %d", event->public.type);
521 dialog = gtk_message_dialog_new (NULL, 0,
522 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
523 video ? _("Incoming video call"): _("Incoming call"));
525 gtk_message_dialog_format_secondary_text (
526 GTK_MESSAGE_DIALOG (dialog), video ?
527 _("%s is video calling you. Do you want to answer?"):
528 _("%s is calling you. Do you want to answer?"),
529 empathy_contact_get_alias (event->approval->contact));
531 title = g_strdup_printf (_("Incoming call from %s"),
532 empathy_contact_get_alias (event->approval->contact));
534 gtk_window_set_title (GTK_WINDOW (dialog), title);
537 /* Set image of the dialog */
540 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
541 GTK_ICON_SIZE_DIALOG);
545 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
546 GTK_ICON_SIZE_DIALOG);
549 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
550 gtk_widget_show (image);
552 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
555 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
556 _("_Reject"), GTK_RESPONSE_REJECT);
557 image = gtk_image_new_from_icon_name ("call-stop",
558 GTK_ICON_SIZE_BUTTON);
559 gtk_button_set_image (GTK_BUTTON (button), image);
561 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
563 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
564 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
566 image = gtk_image_new_from_icon_name ("call-start",
567 GTK_ICON_SIZE_BUTTON);
568 gtk_button_set_image (GTK_BUTTON (button), image);
571 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
572 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
574 image = gtk_image_new_from_icon_name ("call-start",
575 GTK_ICON_SIZE_BUTTON);
576 gtk_button_set_image (GTK_BUTTON (button), image);
578 g_signal_connect (dialog, "response",
579 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
582 gtk_widget_show (dialog);
584 event->approval->dialog = dialog;
588 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
589 EmpathyMessage *message,
590 EventManagerApproval *approval)
593 EmpathyContact *sender;
597 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
599 /* try to update the event if it's referring to a chat which is already in the
601 event = event_lookup_by_approval (approval->manager, approval);
603 sender = empathy_message_get_sender (message);
605 /* We only want to show incoming messages */
606 if (empathy_contact_is_user (sender))
609 header = empathy_contact_get_alias (sender);
610 msg = empathy_message_get_body (message);
613 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
616 event_manager_add (approval->manager, NULL, sender,
617 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
618 approval, event_text_channel_process_func, NULL);
620 window = empathy_main_window_dup ();
622 empathy_sound_manager_play (priv->sound_mgr, window,
623 EMPATHY_SOUND_CONVERSATION_NEW);
625 g_object_unref (window);
629 event_manager_approval_done (EventManagerApproval *approval)
631 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
634 if (approval->operation != NULL)
638 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
640 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
641 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
644 if (priv->ringing == 0)
645 empathy_sound_manager_stop (priv->sound_mgr,
646 EMPATHY_SOUND_PHONE_INCOMING);
650 priv->approvals = g_slist_remove (priv->approvals, approval);
652 for (l = priv->events; l; l = l->next)
654 EventPriv *event = l->data;
656 if (event->approval == approval)
658 event_remove (event);
663 event_manager_approval_free (approval);
667 cdo_invalidated_cb (TpProxy *cdo,
671 EventManagerApproval *approval)
673 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
675 event_manager_approval_done (approval);
679 event_manager_call_state_changed_cb (TpyCallChannel *call,
682 const GValueArray *call_state_reason,
683 GHashTable *call_state_details,
684 EventManagerApproval *approval)
686 if (state == TPY_CALL_STATE_ENDED)
688 DEBUG ("Call ended, seems we missed it :/");
689 reject_approval (approval);
694 event_manager_call_channel_got_contact_cb (TpConnection *connection,
695 EmpathyContact *contact,
700 EventManagerApproval *approval = (EventManagerApproval *) user_data;
701 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
703 TpyCallChannel *call;
707 call = TPY_CALL_CHANNEL (approval->handler_instance);
711 DEBUG ("Can't get the contact for the call.. Rejecting?");
712 reject_approval (approval);
716 if (tpy_call_channel_get_state (call, NULL, NULL) == TPY_CALL_STATE_ENDED)
718 DEBUG ("Call already ended, seems we missed it :/");
719 reject_approval (approval);
723 approval->handler = g_signal_connect (call, "state-changed",
724 G_CALLBACK (event_manager_call_state_changed_cb), approval);
726 window = empathy_main_window_dup ();
727 approval->contact = g_object_ref (contact);
729 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
731 header = g_strdup_printf (
732 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
733 empathy_contact_get_alias (approval->contact));
735 event_manager_add (approval->manager, NULL,
736 approval->contact, EMPATHY_EVENT_TYPE_CALL,
737 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
738 header, NULL, approval,
739 event_channel_process_voip_func, NULL);
744 if (priv->ringing == 1)
745 empathy_sound_manager_start_playing (priv->sound_mgr, window,
746 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
748 g_object_unref (window);
752 event_manager_media_channel_got_contact (EventManagerApproval *approval)
754 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
755 GtkWidget *window = empathy_main_window_dup ();
757 EmpathyTpStreamedMedia *call;
760 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
762 video = empathy_tp_streamed_media_has_initial_video (call);
764 header = g_strdup_printf (
765 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
766 empathy_contact_get_alias (approval->contact));
768 event_manager_add (approval->manager, NULL,
769 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
770 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
771 header, NULL, approval,
772 event_channel_process_voip_func, NULL);
777 if (priv->ringing == 1)
778 empathy_sound_manager_start_playing (priv->sound_mgr, window,
779 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
781 g_object_unref (window);
785 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
786 GParamSpec *param, EventManagerApproval *approval)
788 EmpathyContact *contact;
790 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
795 approval->contact = contact;
796 event_manager_media_channel_got_contact (approval);
800 invite_dialog_response_cb (GtkDialog *dialog,
802 EventManagerApproval *approval)
804 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
805 approval->dialog = NULL;
807 if (response != GTK_RESPONSE_OK)
810 DEBUG ("Muc invitation rejected");
812 reject_approval (approval);
817 DEBUG ("Muc invitation accepted");
819 /* We'll join the room when handling the channel */
820 event_manager_approval_approve (approval);
824 event_room_channel_process_func (EventPriv *event)
826 GtkWidget *dialog, *button, *image;
827 TpChannel *channel = event->approval->main_channel;
830 if (event->approval->dialog != NULL)
832 gtk_window_present (GTK_WINDOW (event->approval->dialog));
837 dialog = gtk_message_dialog_new (NULL, 0,
838 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
840 title = g_strdup_printf (_("Invitation to join %s"),
841 tp_channel_get_identifier (channel));
843 gtk_window_set_title (GTK_WINDOW (dialog), title);
846 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
847 _("%s is inviting you to join %s"),
848 empathy_contact_get_alias (event->approval->contact),
849 tp_channel_get_identifier (channel));
851 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
854 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
855 _("_Decline"), GTK_RESPONSE_CANCEL);
856 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
857 gtk_button_set_image (GTK_BUTTON (button), image);
859 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
860 _("_Join"), GTK_RESPONSE_OK);
861 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
862 gtk_button_set_image (GTK_BUTTON (button), image);
864 g_signal_connect (dialog, "response",
865 G_CALLBACK (invite_dialog_response_cb), event->approval);
867 gtk_widget_show (dialog);
869 event->approval->dialog = dialog;
873 display_invite_room_dialog (EventManagerApproval *approval)
875 GtkWidget *window = empathy_main_window_dup ();
876 const gchar *invite_msg;
878 TpHandle self_handle;
879 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
881 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
882 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
883 NULL, NULL, &invite_msg);
885 if (approval->contact != NULL)
887 msg = g_strdup_printf (_("%s invited you to join %s"),
888 empathy_contact_get_alias (approval->contact),
889 tp_channel_get_identifier (approval->main_channel));
893 msg = g_strdup_printf (_("You have been invited to join %s"),
894 tp_channel_get_identifier (approval->main_channel));
897 event_manager_add (approval->manager, NULL,
898 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
899 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
900 event_room_channel_process_func, NULL);
902 empathy_sound_manager_play (priv->sound_mgr, window,
903 EMPATHY_SOUND_CONVERSATION_NEW);
906 g_object_unref (window);
910 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
911 EmpathyContact *contact,
916 EventManagerApproval *approval = (EventManagerApproval *) user_data;
920 DEBUG ("Error: %s", error->message);
924 approval->contact = g_object_ref (contact);
927 display_invite_room_dialog (approval);
931 event_manager_ft_got_contact_cb (TpConnection *connection,
932 EmpathyContact *contact,
937 EventManagerApproval *approval = (EventManagerApproval *) user_data;
938 GtkWidget *window = empathy_main_window_dup ();
940 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
942 approval->contact = g_object_ref (contact);
944 header = g_strdup_printf (_("Incoming file transfer from %s"),
945 empathy_contact_get_alias (approval->contact));
947 event_manager_add (approval->manager, NULL,
948 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
949 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
950 approval, event_channel_process_func, NULL);
952 /* FIXME better sound for incoming file transfers ?*/
953 empathy_sound_manager_play (priv->sound_mgr, window,
954 EMPATHY_SOUND_CONVERSATION_NEW);
957 g_object_unref (window);
961 event_manager_auth_process_func (EventPriv *event)
963 empathy_event_approve ((EmpathyEvent *) event);
966 /* If there is a file-transfer, media, or auth channel consider it as
969 find_main_channel (GList *channels)
972 TpChannel *text = NULL;
974 for (l = channels; l != NULL; l = g_list_next (l))
976 TpChannel *channel = l->data;
979 if (tp_proxy_get_invalidated (channel) != NULL)
982 channel_type = tp_channel_get_channel_type_id (channel);
984 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
985 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL ||
986 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
987 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
990 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
998 approve_channels (TpSimpleApprover *approver,
1000 TpConnection *connection,
1002 TpChannelDispatchOperation *dispatch_operation,
1003 TpAddDispatchOperationContext *context,
1006 EmpathyEventManager *self = user_data;
1007 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1009 EventManagerApproval *approval;
1010 GQuark channel_type;
1012 channel = find_main_channel (channels);
1013 if (channel == NULL)
1015 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1016 "Unknown channel type" };
1018 DEBUG ("Failed to find the main channel; ignoring");
1020 tp_add_dispatch_operation_context_fail (context, &error);
1024 approval = event_manager_approval_new (self, dispatch_operation, channel);
1025 priv->approvals = g_slist_prepend (priv->approvals, approval);
1027 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1028 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1030 channel_type = tp_channel_get_channel_type_id (channel);
1032 if (TP_IS_TEXT_CHANNEL (channel))
1034 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
1035 GList *messages, *l;
1037 approval->handler_instance = g_object_ref (tp_chat);
1039 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1041 /* Are we in local-pending ? */
1044 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
1046 /* We are invited to a room */
1047 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
1048 tp_channel_get_identifier (channel));
1052 empathy_tp_contact_factory_get_from_handle (connection,
1053 inviter, event_manager_muc_invite_got_contact_cb,
1054 approval, NULL, G_OBJECT (self));
1058 display_invite_room_dialog (approval);
1064 /* We are not invited, approve the channel right now */
1065 tp_add_dispatch_operation_context_accept (context);
1067 approval->auto_approved = TRUE;
1068 event_manager_approval_approve (approval);
1072 /* 1-1 text channel, wait for the first message */
1073 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
1074 G_CALLBACK (event_manager_chat_message_received_cb), approval);
1076 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
1077 for (l = messages; l != NULL; l = g_list_next (l))
1079 EmpathyMessage *msg = l->data;
1081 event_manager_chat_message_received_cb (tp_chat, msg, approval);
1084 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
1086 EmpathyContact *contact;
1087 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account,
1090 approval->handler_instance = G_OBJECT (call);
1092 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
1094 if (contact == NULL)
1096 g_signal_connect (call, "notify::contact",
1097 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
1102 approval->contact = contact;
1103 event_manager_media_channel_got_contact (approval);
1107 else if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
1109 TpyCallChannel *call = TPY_CALL_CHANNEL (channel);
1112 approval->handler_instance = g_object_ref (call);
1114 id = tp_channel_get_identifier (channel);
1116 empathy_tp_contact_factory_get_from_id (connection, id,
1117 event_manager_call_channel_got_contact_cb,
1118 approval, NULL, G_OBJECT (self));
1120 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1124 approval->handler_instance = g_object_ref (channel);
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)
1134 const gchar * const *available_mechanisms;
1136 props = tp_channel_borrow_immutable_properties (channel);
1137 available_mechanisms = tp_asv_get_boxed (props,
1138 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1141 if (tp_strv_contains (available_mechanisms, "X-TELEPATHY-PASSWORD"))
1143 event_manager_add (approval->manager, account, NULL,
1144 EMPATHY_EVENT_TYPE_AUTH,
1145 GTK_STOCK_DIALOG_AUTHENTICATION,
1146 tp_account_get_display_name (account),
1147 _("Password required"), approval,
1148 event_manager_auth_process_func, NULL);
1152 GError error = { TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
1153 "Support only X-TELEPATHY-PASSWORD auth method" };
1155 tp_add_dispatch_operation_context_fail (context, &error);
1161 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1162 "Invalid channel type" };
1164 DEBUG ("Unknown channel type (%s), ignoring..",
1165 g_quark_to_string (channel_type));
1167 tp_add_dispatch_operation_context_fail (context, &error);
1172 tp_add_dispatch_operation_context_accept (context);
1176 event_pending_subscribe_func (EventPriv *event)
1178 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1180 event_remove (event);
1184 check_publish_state (EmpathyEventManager *self,
1185 TpContact *tp_contact)
1187 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1188 gchar *header, *event_msg;
1189 TpSubscriptionState state;
1190 EmpathyContact *contact;
1191 const gchar *message;
1193 state = tp_contact_get_publish_state (tp_contact);
1195 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1197 if (state != TP_SUBSCRIPTION_STATE_ASK)
1201 for (l = priv->events; l; l = l->next)
1203 EventPriv *event = l->data;
1205 if (event->public.contact == contact &&
1206 event->func == event_pending_subscribe_func)
1208 event_remove (event);
1216 header = g_strdup_printf (
1217 _("%s would like permission to see when you are online"),
1218 empathy_contact_get_alias (contact));
1220 message = tp_contact_get_publish_request (tp_contact);
1222 if (!EMP_STR_EMPTY (message))
1223 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1227 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1228 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1229 event_pending_subscribe_func, NULL);
1235 g_object_unref (contact);
1239 event_manager_publish_state_changed_cb (TpContact *contact,
1241 EmpathyEventManager *self)
1243 check_publish_state (self, contact);
1247 event_manager_presence_changed_cb (EmpathyContact *contact,
1248 TpConnectionPresenceType current,
1249 TpConnectionPresenceType previous,
1250 EmpathyEventManager *manager)
1252 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1254 EmpathyPresenceManager *presence_mgr;
1255 GtkWidget *window = empathy_main_window_dup ();
1257 account = empathy_contact_get_account (contact);
1258 presence_mgr = empathy_presence_manager_dup_singleton ();
1260 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1263 if (tp_connection_presence_type_cmp_availability (previous,
1264 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1266 /* contact was online */
1267 if (tp_connection_presence_type_cmp_availability (current,
1268 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1270 /* someone is logging off */
1271 empathy_sound_manager_play (priv->sound_mgr, window,
1272 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1274 if (g_settings_get_boolean (priv->gsettings_notif,
1275 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1277 event_manager_add (manager, NULL, contact,
1278 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1279 EMPATHY_IMAGE_AVATAR_DEFAULT,
1280 empathy_contact_get_alias (contact), _("Disconnected"),
1287 /* contact was offline */
1288 if (tp_connection_presence_type_cmp_availability (current,
1289 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1291 /* someone is logging in */
1292 empathy_sound_manager_play (priv->sound_mgr, window,
1293 EMPATHY_SOUND_CONTACT_CONNECTED);
1295 if (g_settings_get_boolean (priv->gsettings_notif,
1296 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1298 event_manager_add (manager, NULL, contact,
1299 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1300 EMPATHY_IMAGE_AVATAR_DEFAULT,
1301 empathy_contact_get_alias (contact), _("Connected"),
1308 g_object_unref (presence_mgr);
1309 g_object_unref (window);
1313 event_manager_constructor (GType type,
1315 GObjectConstructParam *props)
1319 if (manager_singleton) {
1320 retval = g_object_ref (manager_singleton);
1322 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1323 (type, n_props, props);
1325 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1326 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1333 event_manager_finalize (GObject *object)
1335 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1337 if (priv->ringing > 0)
1338 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1340 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1341 g_slist_free (priv->events);
1342 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1343 g_slist_free (priv->approvals);
1344 g_object_unref (priv->conn_aggregator);
1345 g_object_unref (priv->approver);
1346 g_object_unref (priv->auth_approver);
1347 g_object_unref (priv->gsettings_notif);
1348 g_object_unref (priv->gsettings_ui);
1349 g_object_unref (priv->sound_mgr);
1350 g_hash_table_unref (priv->contacts);
1354 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1356 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1358 object_class->finalize = event_manager_finalize;
1359 object_class->constructor = event_manager_constructor;
1361 signals[EVENT_ADDED] =
1362 g_signal_new ("event-added",
1363 G_TYPE_FROM_CLASS (klass),
1367 g_cclosure_marshal_generic,
1371 signals[EVENT_REMOVED] =
1372 g_signal_new ("event-removed",
1373 G_TYPE_FROM_CLASS (klass),
1377 g_cclosure_marshal_generic,
1378 G_TYPE_NONE, 1, G_TYPE_POINTER);
1380 signals[EVENT_UPDATED] =
1381 g_signal_new ("event-updated",
1382 G_TYPE_FROM_CLASS (klass),
1386 g_cclosure_marshal_generic,
1387 G_TYPE_NONE, 1, G_TYPE_POINTER);
1389 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1393 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1396 EmpathyEventManager *self)
1398 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1401 for (i = 0; i < added->len; i++)
1403 TpContact *tp_contact = g_ptr_array_index (added, i);
1404 EmpathyContact *contact;
1406 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1409 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1411 tp_g_signal_connect_object (contact, "presence-changed",
1412 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1414 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1415 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1417 check_publish_state (self, tp_contact);
1419 /* Pass ownership to the hash table */
1420 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1423 for (i = 0; i < removed->len; i++)
1425 TpContact *tp_contact = g_ptr_array_index (removed, i);
1426 EmpathyContact *contact;
1428 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1429 if (contact == NULL)
1432 g_signal_handlers_disconnect_by_func (contact,
1433 event_manager_presence_changed_cb, self);
1435 g_signal_handlers_disconnect_by_func (tp_contact,
1436 event_manager_publish_state_changed_cb, self);
1438 g_hash_table_remove (priv->contacts, tp_contact);
1443 empathy_event_manager_init (EmpathyEventManager *manager)
1445 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1446 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1447 GError *error = NULL;
1448 TpAccountManager *am;
1449 GPtrArray *contacts, *empty;
1451 manager->priv = priv;
1453 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1454 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1456 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1458 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1461 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1463 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1464 G_CALLBACK (contact_list_changed_cb), manager, 0);
1466 contacts = empathy_connection_aggregator_dup_all_contacts (
1467 priv->conn_aggregator);
1469 empty = g_ptr_array_new ();
1471 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1473 g_ptr_array_unref (contacts);
1474 g_ptr_array_unref (empty);
1476 am = tp_account_manager_dup ();
1478 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1479 FALSE, approve_channels, manager, NULL);
1481 /* Private text channels */
1482 tp_base_client_take_approver_filter (priv->approver,
1484 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1485 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1488 /* Muc text channels */
1489 tp_base_client_take_approver_filter (priv->approver,
1491 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1492 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1496 tp_base_client_take_approver_filter (priv->approver,
1498 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1499 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1500 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1504 tp_base_client_take_approver_filter (priv->approver,
1506 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1507 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1508 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1510 tp_base_client_take_approver_filter (priv->approver,
1512 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1513 TPY_IFACE_CHANNEL_TYPE_CALL,
1514 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1517 /* I don't feel good about doing this, and I'm sorry, but the
1518 * capabilities connection feature is added earlier because it's
1519 * needed for EmpathyTpChat. If the capabilities feature is required
1520 * then preparing an auth channel (which of course appears in the
1521 * CONNECTING state) will never be prepared. So the options are
1522 * either to create another approver like I've done, or to port
1523 * EmpathyTpChat and its users to not depend on the connection being
1524 * prepared with capabilities. I chose the former, obviously. :-) */
1526 priv->auth_approver = tp_simple_approver_new_with_am (am,
1527 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1530 /* SASL auth channels */
1531 tp_base_client_take_approver_filter (priv->auth_approver,
1533 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1534 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1535 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1537 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1540 if (!tp_base_client_register (priv->approver, &error))
1542 DEBUG ("Failed to register Approver: %s", error->message);
1543 g_error_free (error);
1546 if (!tp_base_client_register (priv->auth_approver, &error))
1548 DEBUG ("Failed to register auth Approver: %s", error->message);
1549 g_error_free (error);
1552 g_object_unref (am);
1555 EmpathyEventManager *
1556 empathy_event_manager_dup_singleton (void)
1558 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1562 empathy_event_manager_get_events (EmpathyEventManager *manager)
1564 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1566 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1568 return priv->events;
1572 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1574 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1576 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1578 return priv->events ? priv->events->data : NULL;
1582 empathy_event_activate (EmpathyEvent *event_public)
1584 EventPriv *event = (EventPriv *) event_public;
1586 g_return_if_fail (event_public != NULL);
1589 event->func (event);
1591 event_remove (event);
1595 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1597 EventPriv *event = (EventPriv *) event_public;
1599 g_return_if_fail (event_public != NULL);
1601 event->inhibit = TRUE;
1605 empathy_event_approve (EmpathyEvent *event_public)
1607 EventPriv *event = (EventPriv *) event_public;
1609 g_return_if_fail (event_public != NULL);
1611 event_manager_approval_approve (event->approval);
1615 empathy_event_decline (EmpathyEvent *event_public)
1617 EventPriv *event = (EventPriv *) event_public;
1619 g_return_if_fail (event_public != NULL);
1621 reject_approval (event->approval);