2 * Copyright (C) 2007-2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
25 #include <glib/gi18n.h>
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/simple-approver.h>
32 #include <libempathy/empathy-presence-manager.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-streamed-media.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-gsettings.h>
41 #include <extensions/extensions.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-contact-dialogs.h>
45 #include <libempathy-gtk/empathy-sound-manager.h>
47 #include "empathy-event-manager.h"
48 #include "empathy-main-window.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
51 #include <libempathy/empathy-debug.h>
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
55 #define NOTIFICATION_TIMEOUT 2 /* seconds */
57 /* The time interval in milliseconds between 2 incoming rings */
58 #define MS_BETWEEN_RING 500
61 EmpathyEventManager *manager;
62 TpChannelDispatchOperation *operation;
63 gulong invalidated_handler;
64 /* Remove contact if applicable */
65 EmpathyContact *contact;
66 /* option signal handler and it's instance */
68 GObject *handler_instance;
69 /* optional accept widget */
71 /* Channel of the CDO that will be used during the approval */
72 TpChannel *main_channel;
73 gboolean auto_approved;
74 } EventManagerApproval;
77 TpBaseClient *approver;
78 TpBaseClient *auth_approver;
79 EmpathyContactManager *contact_manager;
81 /* Ongoing approvals */
86 GSettings *gsettings_notif;
87 GSettings *gsettings_ui;
89 EmpathySoundManager *sound_mgr;
90 } EmpathyEventManagerPriv;
92 typedef struct _EventPriv EventPriv;
93 typedef void (*EventFunc) (EventPriv *event);
97 EmpathyEventManager *manager;
98 EventManagerApproval *approval;
102 guint autoremove_timeout_id;
112 static guint signals[LAST_SIGNAL];
114 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
116 static EmpathyEventManager * manager_singleton = NULL;
118 static EventManagerApproval *
119 event_manager_approval_new (EmpathyEventManager *manager,
120 TpChannelDispatchOperation *operation,
121 TpChannel *main_channel)
123 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
124 result->operation = g_object_ref (operation);
125 result->manager = manager;
126 result->main_channel = g_object_ref (main_channel);
132 event_manager_approval_free (EventManagerApproval *approval)
134 g_signal_handler_disconnect (approval->operation,
135 approval->invalidated_handler);
136 g_object_unref (approval->operation);
138 g_object_unref (approval->main_channel);
140 if (approval->handler != 0)
141 g_signal_handler_disconnect (approval->handler_instance,
144 if (approval->handler_instance != NULL)
145 g_object_unref (approval->handler_instance);
147 if (approval->contact != NULL)
148 g_object_unref (approval->contact);
150 if (approval->dialog != NULL)
152 gtk_widget_destroy (approval->dialog);
155 g_slice_free (EventManagerApproval, approval);
159 event_free (EventPriv *event)
161 g_free (event->public.icon_name);
162 g_free (event->public.header);
163 g_free (event->public.message);
165 if (event->autoremove_timeout_id != 0)
166 g_source_remove (event->autoremove_timeout_id);
168 tp_clear_object (&(event->public.contact));
169 tp_clear_object (&(event->public.account));
171 g_slice_free (EventPriv, event);
175 event_remove (EventPriv *event)
177 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
179 DEBUG ("Removing event %p", event);
181 priv->events = g_slist_remove (priv->events, event);
182 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
187 empathy_event_remove (EmpathyEvent *event_public)
189 EventPriv *event = (EventPriv *) event_public;
191 event_remove (event);
195 autoremove_event_timeout_cb (EventPriv *event)
197 event->autoremove_timeout_id = 0;
198 event_remove (event);
203 display_notify_area (EmpathyEventManager *self)
205 EmpathyEventManagerPriv *priv = GET_PRIV (self);
207 return g_settings_get_boolean (priv->gsettings_ui,
208 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
212 event_manager_add (EmpathyEventManager *manager,
214 EmpathyContact *contact,
215 EmpathyEventType type,
216 const gchar *icon_name,
218 const gchar *message,
219 EventManagerApproval *approval,
223 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
226 event = g_slice_new0 (EventPriv);
227 event->public.account = account != NULL ? g_object_ref (account) : NULL;
228 event->public.contact = contact ? g_object_ref (contact) : NULL;
229 event->public.type = type;
230 event->public.icon_name = g_strdup (icon_name);
231 event->public.header = g_strdup (header);
232 event->public.message = g_strdup (message);
233 event->public.must_ack = (func != NULL);
234 event->inhibit = FALSE;
236 event->user_data = user_data;
237 event->manager = manager;
238 event->approval = approval;
240 DEBUG ("Adding event %p", event);
241 priv->events = g_slist_prepend (priv->events, event);
243 if (!display_notify_area (manager))
245 /* Don't fire the 'event-added' signal as we activate the event now */
246 if (approval != NULL)
247 approval->auto_approved = TRUE;
249 empathy_event_activate (&event->public);
253 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
255 if (!event->public.must_ack)
257 event->autoremove_timeout_id = g_timeout_add_seconds (
258 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
264 handle_with_cb (GObject *source,
265 GAsyncResult *result,
268 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
269 GError *error = NULL;
271 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
273 DEBUG ("HandleWith failed: %s\n", error->message);
274 g_error_free (error);
279 handle_with_time_cb (GObject *source,
280 GAsyncResult *result,
283 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
284 GError *error = NULL;
286 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
289 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
291 EventManagerApproval *approval = user_data;
293 DEBUG ("HandleWithTime() is not implemented, falling back to "
294 "HandleWith(). Please upgrade to telepathy-mission-control "
297 tp_channel_dispatch_operation_handle_with_async (approval->operation,
298 NULL, handle_with_cb, approval);
302 DEBUG ("HandleWithTime failed: %s\n", error->message);
304 g_error_free (error);
309 event_manager_approval_approve (EventManagerApproval *approval)
313 if (approval->auto_approved)
315 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
319 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
322 g_assert (approval->operation != NULL);
324 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
325 NULL, timestamp, handle_with_time_cb, approval);
329 event_channel_process_func (EventPriv *event)
331 event_manager_approval_approve (event->approval);
335 event_text_channel_process_func (EventPriv *event)
337 EmpathyTpChat *tp_chat;
339 if (event->approval->handler != 0)
341 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
343 g_signal_handler_disconnect (tp_chat, event->approval->handler);
344 event->approval->handler = 0;
347 event_manager_approval_approve (event->approval);
351 event_lookup_by_approval (EmpathyEventManager *manager,
352 EventManagerApproval *approval)
354 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
356 EventPriv *retval = NULL;
358 for (l = priv->events; l; l = l->next)
360 EventPriv *event = l->data;
362 if (event->approval == approval)
373 event_update (EmpathyEventManager *manager, EventPriv *event,
374 const char *icon_name, const char *header, const char *msg)
376 g_free (event->public.icon_name);
377 g_free (event->public.header);
378 g_free (event->public.message);
380 event->public.icon_name = g_strdup (icon_name);
381 event->public.header = g_strdup (header);
382 event->public.message = g_strdup (msg);
384 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
388 reject_channel_claim_cb (GObject *source,
389 GAsyncResult *result,
392 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
393 GError *error = NULL;
395 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
397 DEBUG ("Failed to claim channel: %s", error->message);
399 g_error_free (error);
403 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
405 empathy_tp_streamed_media_close (user_data);
407 else if (EMPATHY_IS_TP_CHAT (user_data))
409 empathy_tp_chat_leave (user_data, "");
411 else if (EMPATHY_IS_TP_FILE (user_data))
413 empathy_tp_file_close (user_data);
417 g_object_unref (user_data);
421 reject_auth_channel_claim_cb (GObject *source,
422 GAsyncResult *result,
425 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
426 GError *error = NULL;
428 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
430 DEBUG ("Failed to claim channel: %s", error->message);
432 g_error_free (error);
436 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
437 NULL, NULL, NULL, NULL);
441 reject_approval (EventManagerApproval *approval)
443 /* We have to claim the channel before closing it */
445 /* Unfortunately, we need to special case the auth channels for the
446 * time being as they don't have a wrapper object handler in
447 * approval->handler_instance as they're not actually handled by
448 * this process, so we can just use a noddy callback to call Close()
450 if (approval->handler_instance != NULL)
452 tp_channel_dispatch_operation_claim_async (approval->operation,
453 reject_channel_claim_cb, g_object_ref (approval->handler_instance));
455 else if (tp_channel_get_channel_type_id (approval->main_channel)
456 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
458 tp_channel_dispatch_operation_claim_async (approval->operation,
459 reject_auth_channel_claim_cb, approval->main_channel);
464 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
465 gint response, gpointer user_data)
467 EventManagerApproval *approval = user_data;
469 gtk_widget_destroy (approval->dialog);
470 approval->dialog = NULL;
472 if (response != GTK_RESPONSE_ACCEPT)
474 reject_approval (approval);
478 event_manager_approval_approve (approval);
483 event_channel_process_voip_func (EventPriv *event)
488 EmpathyTpStreamedMedia *call;
492 if (event->approval->dialog != NULL)
494 gtk_window_present (GTK_WINDOW (event->approval->dialog));
498 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
500 video = empathy_tp_streamed_media_has_initial_video (call);
502 dialog = gtk_message_dialog_new (NULL, 0,
503 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
504 video ? _("Incoming video call"): _("Incoming call"));
506 gtk_message_dialog_format_secondary_text (
507 GTK_MESSAGE_DIALOG (dialog), video ?
508 _("%s is video calling you. Do you want to answer?"):
509 _("%s is calling you. Do you want to answer?"),
510 empathy_contact_get_alias (event->approval->contact));
512 title = g_strdup_printf (_("Incoming call from %s"),
513 empathy_contact_get_alias (event->approval->contact));
515 gtk_window_set_title (GTK_WINDOW (dialog), title);
518 /* Set image of the dialog */
521 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
522 GTK_ICON_SIZE_DIALOG);
526 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
527 GTK_ICON_SIZE_DIALOG);
530 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
531 gtk_widget_show (image);
533 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
536 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
537 _("_Reject"), GTK_RESPONSE_REJECT);
538 image = gtk_image_new_from_icon_name ("call-stop",
539 GTK_ICON_SIZE_BUTTON);
540 gtk_button_set_image (GTK_BUTTON (button), image);
542 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
543 _("_Answer"), GTK_RESPONSE_ACCEPT);
545 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
546 gtk_button_set_image (GTK_BUTTON (button), image);
548 g_signal_connect (dialog, "response",
549 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
552 gtk_widget_show (dialog);
554 event->approval->dialog = dialog;
558 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
559 EmpathyMessage *message,
560 EventManagerApproval *approval)
563 EmpathyContact *sender;
567 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
569 /* try to update the event if it's referring to a chat which is already in the
571 event = event_lookup_by_approval (approval->manager, approval);
573 sender = empathy_message_get_sender (message);
575 /* We only want to show incoming messages */
576 if (empathy_contact_is_user (sender))
579 header = empathy_contact_get_alias (sender);
580 msg = empathy_message_get_body (message);
583 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
586 event_manager_add (approval->manager, NULL, sender,
587 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
588 approval, event_text_channel_process_func, NULL);
590 window = empathy_main_window_dup ();
592 empathy_sound_manager_play (priv->sound_mgr, window,
593 EMPATHY_SOUND_CONVERSATION_NEW);
595 g_object_unref (window);
599 event_manager_approval_done (EventManagerApproval *approval)
601 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
604 if (approval->operation != NULL)
608 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
610 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
613 if (priv->ringing == 0)
614 empathy_sound_manager_stop (priv->sound_mgr,
615 EMPATHY_SOUND_PHONE_INCOMING);
619 priv->approvals = g_slist_remove (priv->approvals, approval);
621 for (l = priv->events; l; l = l->next)
623 EventPriv *event = l->data;
625 if (event->approval == approval)
627 event_remove (event);
632 event_manager_approval_free (approval);
636 cdo_invalidated_cb (TpProxy *cdo,
640 EventManagerApproval *approval)
642 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
644 event_manager_approval_done (approval);
648 event_manager_media_channel_got_contact (EventManagerApproval *approval)
650 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
651 GtkWidget *window = empathy_main_window_dup ();
653 EmpathyTpStreamedMedia *call;
656 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
658 video = empathy_tp_streamed_media_has_initial_video (call);
660 header = g_strdup_printf (
661 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
662 empathy_contact_get_alias (approval->contact));
664 event_manager_add (approval->manager, NULL,
665 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
666 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
667 header, NULL, approval,
668 event_channel_process_voip_func, NULL);
673 if (priv->ringing == 1)
674 empathy_sound_manager_start_playing (priv->sound_mgr, window,
675 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
677 g_object_unref (window);
681 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
682 GParamSpec *param, EventManagerApproval *approval)
684 EmpathyContact *contact;
686 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
691 approval->contact = contact;
692 event_manager_media_channel_got_contact (approval);
696 invite_dialog_response_cb (GtkDialog *dialog,
698 EventManagerApproval *approval)
700 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
701 approval->dialog = NULL;
703 if (response != GTK_RESPONSE_OK)
706 DEBUG ("Muc invitation rejected");
708 reject_approval (approval);
713 DEBUG ("Muc invitation accepted");
715 /* We'll join the room when handling the channel */
716 event_manager_approval_approve (approval);
720 event_room_channel_process_func (EventPriv *event)
722 GtkWidget *dialog, *button, *image;
723 TpChannel *channel = event->approval->main_channel;
726 if (event->approval->dialog != NULL)
728 gtk_window_present (GTK_WINDOW (event->approval->dialog));
733 dialog = gtk_message_dialog_new (NULL, 0,
734 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
736 title = g_strdup_printf (_("Invitation to join %s"),
737 tp_channel_get_identifier (channel));
739 gtk_window_set_title (GTK_WINDOW (dialog), title);
742 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
743 _("%s is inviting you to join %s"),
744 empathy_contact_get_alias (event->approval->contact),
745 tp_channel_get_identifier (channel));
747 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
750 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
751 _("_Decline"), GTK_RESPONSE_CANCEL);
752 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
753 gtk_button_set_image (GTK_BUTTON (button), image);
755 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
756 _("_Join"), GTK_RESPONSE_OK);
757 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
758 gtk_button_set_image (GTK_BUTTON (button), image);
760 g_signal_connect (dialog, "response",
761 G_CALLBACK (invite_dialog_response_cb), event->approval);
763 gtk_widget_show (dialog);
765 event->approval->dialog = dialog;
769 display_invite_room_dialog (EventManagerApproval *approval)
771 GtkWidget *window = empathy_main_window_dup ();
772 const gchar *invite_msg;
774 TpHandle self_handle;
775 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
777 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
778 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
779 NULL, NULL, &invite_msg);
781 if (approval->contact != NULL)
783 msg = g_strdup_printf (_("%s invited you to join %s"),
784 empathy_contact_get_alias (approval->contact),
785 tp_channel_get_identifier (approval->main_channel));
789 msg = g_strdup_printf (_("You have been invited to join %s"),
790 tp_channel_get_identifier (approval->main_channel));
793 event_manager_add (approval->manager, NULL,
794 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
795 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
796 event_room_channel_process_func, NULL);
798 empathy_sound_manager_play (priv->sound_mgr, window,
799 EMPATHY_SOUND_CONVERSATION_NEW);
802 g_object_unref (window);
806 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
807 EmpathyContact *contact,
812 EventManagerApproval *approval = (EventManagerApproval *) user_data;
816 DEBUG ("Error: %s", error->message);
820 approval->contact = g_object_ref (contact);
823 display_invite_room_dialog (approval);
827 event_manager_ft_got_contact_cb (TpConnection *connection,
828 EmpathyContact *contact,
833 EventManagerApproval *approval = (EventManagerApproval *) user_data;
834 GtkWidget *window = empathy_main_window_dup ();
836 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
838 approval->contact = g_object_ref (contact);
840 header = g_strdup_printf (_("Incoming file transfer from %s"),
841 empathy_contact_get_alias (approval->contact));
843 event_manager_add (approval->manager, NULL,
844 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
845 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
846 approval, event_channel_process_func, NULL);
848 /* FIXME better sound for incoming file transfers ?*/
849 empathy_sound_manager_play (priv->sound_mgr, window,
850 EMPATHY_SOUND_CONVERSATION_NEW);
853 g_object_unref (window);
857 event_manager_auth_process_func (EventPriv *event)
859 empathy_event_approve ((EmpathyEvent *) event);
862 /* If there is a file-transfer, media, or auth channel consider it as
865 find_main_channel (GList *channels)
868 TpChannel *text = NULL;
870 for (l = channels; l != NULL; l = g_list_next (l))
872 TpChannel *channel = l->data;
875 if (tp_proxy_get_invalidated (channel) != NULL)
878 channel_type = tp_channel_get_channel_type_id (channel);
880 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
881 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
882 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
885 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
893 approve_channels (TpSimpleApprover *approver,
895 TpConnection *connection,
897 TpChannelDispatchOperation *dispatch_operation,
898 TpAddDispatchOperationContext *context,
901 EmpathyEventManager *self = user_data;
902 EmpathyEventManagerPriv *priv = GET_PRIV (self);
904 EventManagerApproval *approval;
907 channel = find_main_channel (channels);
910 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
911 "Unknown channel type" };
913 DEBUG ("Failed to find the main channel; ignoring");
915 tp_add_dispatch_operation_context_fail (context, &error);
919 approval = event_manager_approval_new (self, dispatch_operation, channel);
920 priv->approvals = g_slist_prepend (priv->approvals, approval);
922 approval->invalidated_handler = g_signal_connect (dispatch_operation,
923 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
925 channel_type = tp_channel_get_channel_type_id (channel);
927 if (TP_IS_TEXT_CHANNEL (channel))
929 EmpathyTpChat *tp_chat;
931 tp_chat = empathy_tp_chat_new (account, channel);
932 approval->handler_instance = G_OBJECT (tp_chat);
934 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
936 /* Are we in local-pending ? */
939 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
941 /* We are invited to a room */
942 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
943 tp_channel_get_identifier (channel));
947 empathy_tp_contact_factory_get_from_handle (connection,
948 inviter, event_manager_muc_invite_got_contact_cb,
949 approval, NULL, G_OBJECT (self));
953 display_invite_room_dialog (approval);
959 /* We are not invited, approve the channel right now */
960 tp_add_dispatch_operation_context_accept (context);
962 approval->auto_approved = TRUE;
963 event_manager_approval_approve (approval);
967 /* 1-1 text channel, wait for the first message */
968 approval->handler = g_signal_connect (tp_chat, "message-received",
969 G_CALLBACK (event_manager_chat_message_received_cb), approval);
971 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
973 EmpathyContact *contact;
974 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account, channel);
976 approval->handler_instance = G_OBJECT (call);
978 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
982 g_signal_connect (call, "notify::contact",
983 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
988 approval->contact = contact;
989 event_manager_media_channel_got_contact (approval);
993 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
996 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
998 approval->handler_instance = G_OBJECT (tp_file);
1000 handle = tp_channel_get_handle (channel, NULL);
1002 connection = tp_channel_borrow_connection (channel);
1003 empathy_tp_contact_factory_get_from_handle (connection, handle,
1004 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1006 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1008 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1009 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1010 _("Password required"), approval,
1011 event_manager_auth_process_func, NULL);
1015 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1016 "Invalid channel type" };
1018 DEBUG ("Unknown channel type (%s), ignoring..",
1019 g_quark_to_string (channel_type));
1021 tp_add_dispatch_operation_context_fail (context, &error);
1026 tp_add_dispatch_operation_context_accept (context);
1030 event_pending_subscribe_func (EventPriv *event)
1032 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1034 event_remove (event);
1038 event_manager_pendings_changed_cb (EmpathyContactList *list,
1039 EmpathyContact *contact, EmpathyContact *actor,
1040 guint reason, gchar *message, gboolean is_pending,
1041 EmpathyEventManager *manager)
1043 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1044 gchar *header, *event_msg;
1050 for (l = priv->events; l; l = l->next)
1052 EventPriv *event = l->data;
1054 if (event->public.contact == contact &&
1055 event->func == event_pending_subscribe_func)
1057 event_remove (event);
1065 header = g_strdup_printf (
1066 _("%s would like permission to see when you are online"),
1067 empathy_contact_get_alias (contact));
1069 if (!EMP_STR_EMPTY (message))
1070 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1074 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1075 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1076 event_pending_subscribe_func, NULL);
1083 event_manager_presence_changed_cb (EmpathyContact *contact,
1084 TpConnectionPresenceType current,
1085 TpConnectionPresenceType previous,
1086 EmpathyEventManager *manager)
1088 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1090 EmpathyPresenceManager *presence_mgr;
1091 GtkWidget *window = empathy_main_window_dup ();
1093 account = empathy_contact_get_account (contact);
1094 presence_mgr = empathy_presence_manager_dup_singleton ();
1096 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1099 if (tp_connection_presence_type_cmp_availability (previous,
1100 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1102 /* contact was online */
1103 if (tp_connection_presence_type_cmp_availability (current,
1104 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1106 /* someone is logging off */
1107 empathy_sound_manager_play (priv->sound_mgr, window,
1108 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1110 if (g_settings_get_boolean (priv->gsettings_notif,
1111 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1113 event_manager_add (manager, NULL, contact,
1114 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1115 EMPATHY_IMAGE_AVATAR_DEFAULT,
1116 empathy_contact_get_alias (contact), _("Disconnected"),
1123 /* contact was offline */
1124 if (tp_connection_presence_type_cmp_availability (current,
1125 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1127 /* someone is logging in */
1128 empathy_sound_manager_play (priv->sound_mgr, window,
1129 EMPATHY_SOUND_CONTACT_CONNECTED);
1131 if (g_settings_get_boolean (priv->gsettings_notif,
1132 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1134 event_manager_add (manager, NULL, contact,
1135 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1136 EMPATHY_IMAGE_AVATAR_DEFAULT,
1137 empathy_contact_get_alias (contact), _("Connected"),
1144 g_object_unref (presence_mgr);
1145 g_object_unref (window);
1149 event_manager_members_changed_cb (EmpathyContactList *list,
1150 EmpathyContact *contact,
1151 EmpathyContact *actor,
1155 EmpathyEventManager *manager)
1158 g_signal_connect (contact, "presence-changed",
1159 G_CALLBACK (event_manager_presence_changed_cb), manager);
1161 g_signal_handlers_disconnect_by_func (contact,
1162 event_manager_presence_changed_cb, manager);
1166 event_manager_constructor (GType type,
1168 GObjectConstructParam *props)
1172 if (manager_singleton) {
1173 retval = g_object_ref (manager_singleton);
1175 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1176 (type, n_props, props);
1178 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1179 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1186 event_manager_finalize (GObject *object)
1188 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1190 if (priv->ringing > 0)
1191 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1193 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1194 g_slist_free (priv->events);
1195 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1196 g_slist_free (priv->approvals);
1197 g_object_unref (priv->contact_manager);
1198 g_object_unref (priv->approver);
1199 g_object_unref (priv->auth_approver);
1200 g_object_unref (priv->gsettings_notif);
1201 g_object_unref (priv->gsettings_ui);
1202 g_object_unref (priv->sound_mgr);
1206 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1208 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1210 object_class->finalize = event_manager_finalize;
1211 object_class->constructor = event_manager_constructor;
1213 signals[EVENT_ADDED] =
1214 g_signal_new ("event-added",
1215 G_TYPE_FROM_CLASS (klass),
1219 g_cclosure_marshal_VOID__POINTER,
1223 signals[EVENT_REMOVED] =
1224 g_signal_new ("event-removed",
1225 G_TYPE_FROM_CLASS (klass),
1229 g_cclosure_marshal_VOID__POINTER,
1230 G_TYPE_NONE, 1, G_TYPE_POINTER);
1232 signals[EVENT_UPDATED] =
1233 g_signal_new ("event-updated",
1234 G_TYPE_FROM_CLASS (klass),
1238 g_cclosure_marshal_VOID__POINTER,
1239 G_TYPE_NONE, 1, G_TYPE_POINTER);
1241 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1245 empathy_event_manager_init (EmpathyEventManager *manager)
1247 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1248 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1250 GError *error = NULL;
1252 manager->priv = priv;
1254 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1255 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1257 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1259 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1260 g_signal_connect (priv->contact_manager, "pendings-changed",
1261 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1263 g_signal_connect (priv->contact_manager, "members-changed",
1264 G_CALLBACK (event_manager_members_changed_cb), manager);
1266 dbus = tp_dbus_daemon_dup (&error);
1269 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1270 g_error_free (error);
1274 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1275 approve_channels, manager, NULL);
1277 /* EmpathyTpChat relies on this feature being prepared */
1278 tp_base_client_add_connection_features_varargs (priv->approver,
1279 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1281 /* Private text channels */
1282 tp_base_client_take_approver_filter (priv->approver,
1284 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1285 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1288 /* Muc text channels */
1289 tp_base_client_take_approver_filter (priv->approver,
1291 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1292 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1296 tp_base_client_take_approver_filter (priv->approver,
1298 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1299 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1300 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1304 tp_base_client_take_approver_filter (priv->approver,
1306 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1307 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1308 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1311 /* I don't feel good about doing this, and I'm sorry, but the
1312 * capabilities connection feature is added earlier because it's
1313 * needed for EmpathyTpChat. If the capabilities feature is required
1314 * then preparing an auth channel (which of course appears in the
1315 * CONNECTING state) will never be prepared. So the options are
1316 * either to create another approver like I've done, or to port
1317 * EmpathyTpChat and its users to not depend on the connection being
1318 * prepared with capabilities. I chose the former, obviously. :-) */
1320 priv->auth_approver = tp_simple_approver_new (dbus,
1321 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1324 /* SASL auth channels */
1325 tp_base_client_take_approver_filter (priv->auth_approver,
1327 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1328 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1329 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1331 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1334 if (!tp_base_client_register (priv->approver, &error))
1336 DEBUG ("Failed to register Approver: %s", error->message);
1337 g_error_free (error);
1340 if (!tp_base_client_register (priv->auth_approver, &error))
1342 DEBUG ("Failed to register auth Approver: %s", error->message);
1343 g_error_free (error);
1346 g_object_unref (dbus);
1349 EmpathyEventManager *
1350 empathy_event_manager_dup_singleton (void)
1352 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1356 empathy_event_manager_get_events (EmpathyEventManager *manager)
1358 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1360 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1362 return priv->events;
1366 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1368 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1370 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1372 return priv->events ? priv->events->data : NULL;
1376 empathy_event_activate (EmpathyEvent *event_public)
1378 EventPriv *event = (EventPriv *) event_public;
1380 g_return_if_fail (event_public != NULL);
1383 event->func (event);
1385 event_remove (event);
1389 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1391 EventPriv *event = (EventPriv *) event_public;
1393 g_return_if_fail (event_public != NULL);
1395 event->inhibit = TRUE;
1399 empathy_event_approve (EmpathyEvent *event_public)
1401 EventPriv *event = (EventPriv *) event_public;
1403 g_return_if_fail (event_public != NULL);
1405 event_manager_approval_approve (event->approval);
1409 empathy_event_decline (EmpathyEvent *event_public)
1411 EventPriv *event = (EventPriv *) event_public;
1413 g_return_if_fail (event_public != NULL);
1415 reject_approval (event->approval);