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>
46 #include <libempathy-gtk/empathy-ui-utils.h>
48 #include "empathy-event-manager.h"
49 #include "empathy-main-window.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
52 #include <libempathy/empathy-debug.h>
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
56 #define NOTIFICATION_TIMEOUT 2 /* seconds */
58 /* The time interval in milliseconds between 2 incoming rings */
59 #define MS_BETWEEN_RING 500
62 EmpathyEventManager *manager;
63 TpChannelDispatchOperation *operation;
64 gulong invalidated_handler;
65 /* Remove contact if applicable */
66 EmpathyContact *contact;
67 /* option signal handler and it's instance */
69 GObject *handler_instance;
70 /* optional accept widget */
72 /* Channel of the CDO that will be used during the approval */
73 TpChannel *main_channel;
74 gboolean auto_approved;
75 } EventManagerApproval;
78 TpBaseClient *approver;
79 TpBaseClient *auth_approver;
80 EmpathyContactManager *contact_manager;
82 /* Ongoing approvals */
87 GSettings *gsettings_notif;
88 GSettings *gsettings_ui;
90 EmpathySoundManager *sound_mgr;
91 } EmpathyEventManagerPriv;
93 typedef struct _EventPriv EventPriv;
94 typedef void (*EventFunc) (EventPriv *event);
98 EmpathyEventManager *manager;
99 EventManagerApproval *approval;
103 guint autoremove_timeout_id;
113 static guint signals[LAST_SIGNAL];
115 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
117 static EmpathyEventManager * manager_singleton = NULL;
119 static EventManagerApproval *
120 event_manager_approval_new (EmpathyEventManager *manager,
121 TpChannelDispatchOperation *operation,
122 TpChannel *main_channel)
124 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
125 result->operation = g_object_ref (operation);
126 result->manager = manager;
127 result->main_channel = g_object_ref (main_channel);
133 event_manager_approval_free (EventManagerApproval *approval)
135 g_signal_handler_disconnect (approval->operation,
136 approval->invalidated_handler);
137 g_object_unref (approval->operation);
139 g_object_unref (approval->main_channel);
141 if (approval->handler != 0)
142 g_signal_handler_disconnect (approval->handler_instance,
145 if (approval->handler_instance != NULL)
146 g_object_unref (approval->handler_instance);
148 if (approval->contact != NULL)
149 g_object_unref (approval->contact);
151 if (approval->dialog != NULL)
153 gtk_widget_destroy (approval->dialog);
156 g_slice_free (EventManagerApproval, approval);
160 event_free (EventPriv *event)
162 g_free (event->public.icon_name);
163 g_free (event->public.header);
164 g_free (event->public.message);
166 if (event->autoremove_timeout_id != 0)
167 g_source_remove (event->autoremove_timeout_id);
169 tp_clear_object (&(event->public.contact));
170 tp_clear_object (&(event->public.account));
172 g_slice_free (EventPriv, event);
176 event_remove (EventPriv *event)
178 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
180 DEBUG ("Removing event %p", event);
182 priv->events = g_slist_remove (priv->events, event);
183 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
188 empathy_event_remove (EmpathyEvent *event_public)
190 EventPriv *event = (EventPriv *) event_public;
192 event_remove (event);
196 autoremove_event_timeout_cb (EventPriv *event)
198 event->autoremove_timeout_id = 0;
199 event_remove (event);
204 display_notify_area (EmpathyEventManager *self)
206 EmpathyEventManagerPriv *priv = GET_PRIV (self);
208 return g_settings_get_boolean (priv->gsettings_ui,
209 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
213 event_manager_add (EmpathyEventManager *manager,
215 EmpathyContact *contact,
216 EmpathyEventType type,
217 const gchar *icon_name,
219 const gchar *message,
220 EventManagerApproval *approval,
224 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
227 event = g_slice_new0 (EventPriv);
228 event->public.account = account != NULL ? g_object_ref (account) : NULL;
229 event->public.contact = contact ? g_object_ref (contact) : NULL;
230 event->public.type = type;
231 event->public.icon_name = g_strdup (icon_name);
232 event->public.header = g_strdup (header);
233 event->public.message = g_strdup (message);
234 event->public.must_ack = (func != NULL);
235 event->inhibit = FALSE;
237 event->user_data = user_data;
238 event->manager = manager;
239 event->approval = approval;
241 DEBUG ("Adding event %p", event);
242 priv->events = g_slist_prepend (priv->events, event);
244 if (!display_notify_area (manager))
246 /* Don't fire the 'event-added' signal as we activate the event now */
247 if (approval != NULL)
248 approval->auto_approved = TRUE;
250 empathy_event_activate (&event->public);
254 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
256 if (!event->public.must_ack)
258 event->autoremove_timeout_id = g_timeout_add_seconds (
259 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
265 handle_with_cb (GObject *source,
266 GAsyncResult *result,
269 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
270 GError *error = NULL;
272 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
274 DEBUG ("HandleWith failed: %s\n", error->message);
275 g_error_free (error);
280 handle_with_time_cb (GObject *source,
281 GAsyncResult *result,
284 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
285 GError *error = NULL;
287 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
290 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
292 EventManagerApproval *approval = user_data;
294 DEBUG ("HandleWithTime() is not implemented, falling back to "
295 "HandleWith(). Please upgrade to telepathy-mission-control "
298 tp_channel_dispatch_operation_handle_with_async (approval->operation,
299 NULL, handle_with_cb, approval);
303 DEBUG ("HandleWithTime failed: %s\n", error->message);
305 g_error_free (error);
310 event_manager_approval_approve (EventManagerApproval *approval)
314 if (approval->auto_approved)
316 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
320 timestamp = empathy_get_current_action_time ();
323 g_assert (approval->operation != NULL);
325 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
326 NULL, timestamp, handle_with_time_cb, approval);
330 event_channel_process_func (EventPriv *event)
332 event_manager_approval_approve (event->approval);
336 event_text_channel_process_func (EventPriv *event)
338 EmpathyTpChat *tp_chat;
340 if (event->approval->handler != 0)
342 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
344 g_signal_handler_disconnect (tp_chat, event->approval->handler);
345 event->approval->handler = 0;
348 event_manager_approval_approve (event->approval);
352 event_lookup_by_approval (EmpathyEventManager *manager,
353 EventManagerApproval *approval)
355 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
357 EventPriv *retval = NULL;
359 for (l = priv->events; l; l = l->next)
361 EventPriv *event = l->data;
363 if (event->approval == approval)
374 event_update (EmpathyEventManager *manager, EventPriv *event,
375 const char *icon_name, const char *header, const char *msg)
377 g_free (event->public.icon_name);
378 g_free (event->public.header);
379 g_free (event->public.message);
381 event->public.icon_name = g_strdup (icon_name);
382 event->public.header = g_strdup (header);
383 event->public.message = g_strdup (msg);
385 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
389 reject_channel_claim_cb (GObject *source,
390 GAsyncResult *result,
393 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
394 GError *error = NULL;
396 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
398 DEBUG ("Failed to claim channel: %s", error->message);
400 g_error_free (error);
404 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
406 empathy_tp_streamed_media_close (user_data);
408 else if (EMPATHY_IS_TP_CHAT (user_data))
410 empathy_tp_chat_leave (user_data, "");
412 else if (EMPATHY_IS_TP_FILE (user_data))
414 empathy_tp_file_close (user_data);
418 g_object_unref (user_data);
422 reject_auth_channel_claim_cb (GObject *source,
423 GAsyncResult *result,
426 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
427 GError *error = NULL;
429 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
431 DEBUG ("Failed to claim channel: %s", error->message);
433 g_error_free (error);
437 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
438 NULL, NULL, NULL, NULL);
442 reject_approval (EventManagerApproval *approval)
444 /* We have to claim the channel before closing it */
446 /* Unfortunately, we need to special case the auth channels for the
447 * time being as they don't have a wrapper object handler in
448 * approval->handler_instance as they're not actually handled by
449 * this process, so we can just use a noddy callback to call Close()
451 if (approval->handler_instance != NULL)
453 tp_channel_dispatch_operation_claim_async (approval->operation,
454 reject_channel_claim_cb, g_object_ref (approval->handler_instance));
456 else if (tp_channel_get_channel_type_id (approval->main_channel)
457 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
459 tp_channel_dispatch_operation_claim_async (approval->operation,
460 reject_auth_channel_claim_cb, approval->main_channel);
465 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
466 gint response, gpointer user_data)
468 EventManagerApproval *approval = user_data;
470 gtk_widget_destroy (approval->dialog);
471 approval->dialog = NULL;
473 if (response != GTK_RESPONSE_ACCEPT)
475 reject_approval (approval);
479 event_manager_approval_approve (approval);
484 event_channel_process_voip_func (EventPriv *event)
489 EmpathyTpStreamedMedia *call;
493 if (event->approval->dialog != NULL)
495 gtk_window_present (GTK_WINDOW (event->approval->dialog));
499 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
501 video = empathy_tp_streamed_media_has_initial_video (call);
503 dialog = gtk_message_dialog_new (NULL, 0,
504 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
505 video ? _("Incoming video call"): _("Incoming call"));
507 gtk_message_dialog_format_secondary_text (
508 GTK_MESSAGE_DIALOG (dialog), video ?
509 _("%s is video calling you. Do you want to answer?"):
510 _("%s is calling you. Do you want to answer?"),
511 empathy_contact_get_alias (event->approval->contact));
513 title = g_strdup_printf (_("Incoming call from %s"),
514 empathy_contact_get_alias (event->approval->contact));
516 gtk_window_set_title (GTK_WINDOW (dialog), title);
519 /* Set image of the dialog */
522 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
523 GTK_ICON_SIZE_DIALOG);
527 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
528 GTK_ICON_SIZE_DIALOG);
531 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
532 gtk_widget_show (image);
534 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
537 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
538 _("_Reject"), GTK_RESPONSE_REJECT);
539 image = gtk_image_new_from_icon_name ("call-stop",
540 GTK_ICON_SIZE_BUTTON);
541 gtk_button_set_image (GTK_BUTTON (button), image);
543 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
544 _("_Answer"), GTK_RESPONSE_ACCEPT);
546 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
547 gtk_button_set_image (GTK_BUTTON (button), image);
549 g_signal_connect (dialog, "response",
550 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
553 gtk_widget_show (dialog);
555 event->approval->dialog = dialog;
559 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
560 EmpathyMessage *message,
561 EventManagerApproval *approval)
564 EmpathyContact *sender;
568 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
570 /* try to update the event if it's referring to a chat which is already in the
572 event = event_lookup_by_approval (approval->manager, approval);
574 sender = empathy_message_get_sender (message);
576 /* We only want to show incoming messages */
577 if (empathy_contact_is_user (sender))
580 header = empathy_contact_get_alias (sender);
581 msg = empathy_message_get_body (message);
584 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
587 event_manager_add (approval->manager, NULL, sender,
588 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
589 approval, event_text_channel_process_func, NULL);
591 window = empathy_main_window_dup ();
593 empathy_sound_manager_play (priv->sound_mgr, window,
594 EMPATHY_SOUND_CONVERSATION_NEW);
596 g_object_unref (window);
600 event_manager_approval_done (EventManagerApproval *approval)
602 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
605 if (approval->operation != NULL)
609 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
611 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
614 if (priv->ringing == 0)
615 empathy_sound_manager_stop (priv->sound_mgr,
616 EMPATHY_SOUND_PHONE_INCOMING);
620 priv->approvals = g_slist_remove (priv->approvals, approval);
622 for (l = priv->events; l; l = l->next)
624 EventPriv *event = l->data;
626 if (event->approval == approval)
628 event_remove (event);
633 event_manager_approval_free (approval);
637 cdo_invalidated_cb (TpProxy *cdo,
641 EventManagerApproval *approval)
643 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
645 event_manager_approval_done (approval);
649 event_manager_media_channel_got_contact (EventManagerApproval *approval)
651 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
652 GtkWidget *window = empathy_main_window_dup ();
654 EmpathyTpStreamedMedia *call;
657 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
659 video = empathy_tp_streamed_media_has_initial_video (call);
661 header = g_strdup_printf (
662 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
663 empathy_contact_get_alias (approval->contact));
665 event_manager_add (approval->manager, NULL,
666 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
667 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
668 header, NULL, approval,
669 event_channel_process_voip_func, NULL);
674 if (priv->ringing == 1)
675 empathy_sound_manager_start_playing (priv->sound_mgr, window,
676 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
678 g_object_unref (window);
682 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
683 GParamSpec *param, EventManagerApproval *approval)
685 EmpathyContact *contact;
687 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
692 approval->contact = contact;
693 event_manager_media_channel_got_contact (approval);
697 invite_dialog_response_cb (GtkDialog *dialog,
699 EventManagerApproval *approval)
701 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
702 approval->dialog = NULL;
704 if (response != GTK_RESPONSE_OK)
707 DEBUG ("Muc invitation rejected");
709 reject_approval (approval);
714 DEBUG ("Muc invitation accepted");
716 /* We'll join the room when handling the channel */
717 event_manager_approval_approve (approval);
721 event_room_channel_process_func (EventPriv *event)
723 GtkWidget *dialog, *button, *image;
724 TpChannel *channel = event->approval->main_channel;
727 if (event->approval->dialog != NULL)
729 gtk_window_present (GTK_WINDOW (event->approval->dialog));
734 dialog = gtk_message_dialog_new (NULL, 0,
735 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
737 title = g_strdup_printf (_("Invitation to join %s"),
738 tp_channel_get_identifier (channel));
740 gtk_window_set_title (GTK_WINDOW (dialog), title);
743 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
744 _("%s is inviting you to join %s"),
745 empathy_contact_get_alias (event->approval->contact),
746 tp_channel_get_identifier (channel));
748 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
751 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
752 _("_Decline"), GTK_RESPONSE_CANCEL);
753 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
754 gtk_button_set_image (GTK_BUTTON (button), image);
756 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
757 _("_Join"), GTK_RESPONSE_OK);
758 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
759 gtk_button_set_image (GTK_BUTTON (button), image);
761 g_signal_connect (dialog, "response",
762 G_CALLBACK (invite_dialog_response_cb), event->approval);
764 gtk_widget_show (dialog);
766 event->approval->dialog = dialog;
770 display_invite_room_dialog (EventManagerApproval *approval)
772 GtkWidget *window = empathy_main_window_dup ();
773 const gchar *invite_msg;
775 TpHandle self_handle;
776 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
778 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
779 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
780 NULL, NULL, &invite_msg);
782 if (approval->contact != NULL)
784 msg = g_strdup_printf (_("%s invited you to join %s"),
785 empathy_contact_get_alias (approval->contact),
786 tp_channel_get_identifier (approval->main_channel));
790 msg = g_strdup_printf (_("You have been invited to join %s"),
791 tp_channel_get_identifier (approval->main_channel));
794 event_manager_add (approval->manager, NULL,
795 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
796 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
797 event_room_channel_process_func, NULL);
799 empathy_sound_manager_play (priv->sound_mgr, window,
800 EMPATHY_SOUND_CONVERSATION_NEW);
803 g_object_unref (window);
807 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
808 EmpathyContact *contact,
813 EventManagerApproval *approval = (EventManagerApproval *) user_data;
817 DEBUG ("Error: %s", error->message);
821 approval->contact = g_object_ref (contact);
824 display_invite_room_dialog (approval);
828 event_manager_ft_got_contact_cb (TpConnection *connection,
829 EmpathyContact *contact,
834 EventManagerApproval *approval = (EventManagerApproval *) user_data;
835 GtkWidget *window = empathy_main_window_dup ();
837 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
839 approval->contact = g_object_ref (contact);
841 header = g_strdup_printf (_("Incoming file transfer from %s"),
842 empathy_contact_get_alias (approval->contact));
844 event_manager_add (approval->manager, NULL,
845 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
846 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
847 approval, event_channel_process_func, NULL);
849 /* FIXME better sound for incoming file transfers ?*/
850 empathy_sound_manager_play (priv->sound_mgr, window,
851 EMPATHY_SOUND_CONVERSATION_NEW);
854 g_object_unref (window);
858 event_manager_auth_process_func (EventPriv *event)
860 empathy_event_approve ((EmpathyEvent *) event);
863 /* If there is a file-transfer, media, or auth channel consider it as
866 find_main_channel (GList *channels)
869 TpChannel *text = NULL;
871 for (l = channels; l != NULL; l = g_list_next (l))
873 TpChannel *channel = l->data;
876 if (tp_proxy_get_invalidated (channel) != NULL)
879 channel_type = tp_channel_get_channel_type_id (channel);
881 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
882 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
883 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
886 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
894 approve_channels (TpSimpleApprover *approver,
896 TpConnection *connection,
898 TpChannelDispatchOperation *dispatch_operation,
899 TpAddDispatchOperationContext *context,
902 EmpathyEventManager *self = user_data;
903 EmpathyEventManagerPriv *priv = GET_PRIV (self);
905 EventManagerApproval *approval;
908 channel = find_main_channel (channels);
911 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
912 "Unknown channel type" };
914 DEBUG ("Failed to find the main channel; ignoring");
916 tp_add_dispatch_operation_context_fail (context, &error);
920 approval = event_manager_approval_new (self, dispatch_operation, channel);
921 priv->approvals = g_slist_prepend (priv->approvals, approval);
923 approval->invalidated_handler = g_signal_connect (dispatch_operation,
924 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
926 channel_type = tp_channel_get_channel_type_id (channel);
928 if (TP_IS_TEXT_CHANNEL (channel))
930 EmpathyTpChat *tp_chat;
932 tp_chat = empathy_tp_chat_new (account, channel);
933 approval->handler_instance = G_OBJECT (tp_chat);
935 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
937 /* Are we in local-pending ? */
940 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
942 /* We are invited to a room */
943 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
944 tp_channel_get_identifier (channel));
948 empathy_tp_contact_factory_get_from_handle (connection,
949 inviter, event_manager_muc_invite_got_contact_cb,
950 approval, NULL, G_OBJECT (self));
954 display_invite_room_dialog (approval);
960 /* We are not invited, approve the channel right now */
961 tp_add_dispatch_operation_context_accept (context);
963 approval->auto_approved = TRUE;
964 event_manager_approval_approve (approval);
968 /* 1-1 text channel, wait for the first message */
969 approval->handler = g_signal_connect (tp_chat, "message-received",
970 G_CALLBACK (event_manager_chat_message_received_cb), approval);
972 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
974 EmpathyContact *contact;
975 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account, channel);
977 approval->handler_instance = G_OBJECT (call);
979 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
983 g_signal_connect (call, "notify::contact",
984 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
989 approval->contact = contact;
990 event_manager_media_channel_got_contact (approval);
994 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
997 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
999 approval->handler_instance = G_OBJECT (tp_file);
1001 handle = tp_channel_get_handle (channel, NULL);
1003 connection = tp_channel_borrow_connection (channel);
1004 empathy_tp_contact_factory_get_from_handle (connection, handle,
1005 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1007 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1009 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1010 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1011 _("Password required"), approval,
1012 event_manager_auth_process_func, NULL);
1016 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1017 "Invalid channel type" };
1019 DEBUG ("Unknown channel type (%s), ignoring..",
1020 g_quark_to_string (channel_type));
1022 tp_add_dispatch_operation_context_fail (context, &error);
1027 tp_add_dispatch_operation_context_accept (context);
1031 event_pending_subscribe_func (EventPriv *event)
1033 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1035 event_remove (event);
1039 event_manager_pendings_changed_cb (EmpathyContactList *list,
1040 EmpathyContact *contact, EmpathyContact *actor,
1041 guint reason, gchar *message, gboolean is_pending,
1042 EmpathyEventManager *manager)
1044 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1045 gchar *header, *event_msg;
1051 for (l = priv->events; l; l = l->next)
1053 EventPriv *event = l->data;
1055 if (event->public.contact == contact &&
1056 event->func == event_pending_subscribe_func)
1058 event_remove (event);
1066 header = g_strdup_printf (
1067 _("%s would like permission to see when you are online"),
1068 empathy_contact_get_alias (contact));
1070 if (!EMP_STR_EMPTY (message))
1071 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1075 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1076 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1077 event_pending_subscribe_func, NULL);
1084 event_manager_presence_changed_cb (EmpathyContact *contact,
1085 TpConnectionPresenceType current,
1086 TpConnectionPresenceType previous,
1087 EmpathyEventManager *manager)
1089 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1091 EmpathyPresenceManager *presence_mgr;
1092 GtkWidget *window = empathy_main_window_dup ();
1094 account = empathy_contact_get_account (contact);
1095 presence_mgr = empathy_presence_manager_dup_singleton ();
1097 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1100 if (tp_connection_presence_type_cmp_availability (previous,
1101 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1103 /* contact was online */
1104 if (tp_connection_presence_type_cmp_availability (current,
1105 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1107 /* someone is logging off */
1108 empathy_sound_manager_play (priv->sound_mgr, window,
1109 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1111 if (g_settings_get_boolean (priv->gsettings_notif,
1112 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1114 event_manager_add (manager, NULL, contact,
1115 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1116 EMPATHY_IMAGE_AVATAR_DEFAULT,
1117 empathy_contact_get_alias (contact), _("Disconnected"),
1124 /* contact was offline */
1125 if (tp_connection_presence_type_cmp_availability (current,
1126 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1128 /* someone is logging in */
1129 empathy_sound_manager_play (priv->sound_mgr, window,
1130 EMPATHY_SOUND_CONTACT_CONNECTED);
1132 if (g_settings_get_boolean (priv->gsettings_notif,
1133 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1135 event_manager_add (manager, NULL, contact,
1136 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1137 EMPATHY_IMAGE_AVATAR_DEFAULT,
1138 empathy_contact_get_alias (contact), _("Connected"),
1145 g_object_unref (presence_mgr);
1146 g_object_unref (window);
1150 event_manager_members_changed_cb (EmpathyContactList *list,
1151 EmpathyContact *contact,
1152 EmpathyContact *actor,
1156 EmpathyEventManager *manager)
1159 g_signal_connect (contact, "presence-changed",
1160 G_CALLBACK (event_manager_presence_changed_cb), manager);
1162 g_signal_handlers_disconnect_by_func (contact,
1163 event_manager_presence_changed_cb, manager);
1167 event_manager_constructor (GType type,
1169 GObjectConstructParam *props)
1173 if (manager_singleton) {
1174 retval = g_object_ref (manager_singleton);
1176 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1177 (type, n_props, props);
1179 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1180 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1187 event_manager_finalize (GObject *object)
1189 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1191 if (priv->ringing > 0)
1192 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1194 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1195 g_slist_free (priv->events);
1196 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1197 g_slist_free (priv->approvals);
1198 g_object_unref (priv->contact_manager);
1199 g_object_unref (priv->approver);
1200 g_object_unref (priv->auth_approver);
1201 g_object_unref (priv->gsettings_notif);
1202 g_object_unref (priv->gsettings_ui);
1203 g_object_unref (priv->sound_mgr);
1207 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1209 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1211 object_class->finalize = event_manager_finalize;
1212 object_class->constructor = event_manager_constructor;
1214 signals[EVENT_ADDED] =
1215 g_signal_new ("event-added",
1216 G_TYPE_FROM_CLASS (klass),
1220 g_cclosure_marshal_VOID__POINTER,
1224 signals[EVENT_REMOVED] =
1225 g_signal_new ("event-removed",
1226 G_TYPE_FROM_CLASS (klass),
1230 g_cclosure_marshal_VOID__POINTER,
1231 G_TYPE_NONE, 1, G_TYPE_POINTER);
1233 signals[EVENT_UPDATED] =
1234 g_signal_new ("event-updated",
1235 G_TYPE_FROM_CLASS (klass),
1239 g_cclosure_marshal_VOID__POINTER,
1240 G_TYPE_NONE, 1, G_TYPE_POINTER);
1242 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1246 empathy_event_manager_init (EmpathyEventManager *manager)
1248 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1249 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1251 GError *error = NULL;
1253 manager->priv = priv;
1255 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1256 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1258 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1260 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1261 g_signal_connect (priv->contact_manager, "pendings-changed",
1262 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1264 g_signal_connect (priv->contact_manager, "members-changed",
1265 G_CALLBACK (event_manager_members_changed_cb), manager);
1267 dbus = tp_dbus_daemon_dup (&error);
1270 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1271 g_error_free (error);
1275 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1276 approve_channels, manager, NULL);
1278 /* EmpathyTpChat relies on this feature being prepared */
1279 tp_base_client_add_connection_features_varargs (priv->approver,
1280 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1282 /* Private text channels */
1283 tp_base_client_take_approver_filter (priv->approver,
1285 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1286 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1289 /* Muc text channels */
1290 tp_base_client_take_approver_filter (priv->approver,
1292 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1293 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1297 tp_base_client_take_approver_filter (priv->approver,
1299 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1300 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1301 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1305 tp_base_client_take_approver_filter (priv->approver,
1307 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1308 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1309 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1312 /* I don't feel good about doing this, and I'm sorry, but the
1313 * capabilities connection feature is added earlier because it's
1314 * needed for EmpathyTpChat. If the capabilities feature is required
1315 * then preparing an auth channel (which of course appears in the
1316 * CONNECTING state) will never be prepared. So the options are
1317 * either to create another approver like I've done, or to port
1318 * EmpathyTpChat and its users to not depend on the connection being
1319 * prepared with capabilities. I chose the former, obviously. :-) */
1321 priv->auth_approver = tp_simple_approver_new (dbus,
1322 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1325 /* SASL auth channels */
1326 tp_base_client_take_approver_filter (priv->auth_approver,
1328 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1329 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1330 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1332 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1335 if (!tp_base_client_register (priv->approver, &error))
1337 DEBUG ("Failed to register Approver: %s", error->message);
1338 g_error_free (error);
1341 if (!tp_base_client_register (priv->auth_approver, &error))
1343 DEBUG ("Failed to register auth Approver: %s", error->message);
1344 g_error_free (error);
1347 g_object_unref (dbus);
1350 EmpathyEventManager *
1351 empathy_event_manager_dup_singleton (void)
1353 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1357 empathy_event_manager_get_events (EmpathyEventManager *manager)
1359 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1361 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1363 return priv->events;
1367 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1369 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1371 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1373 return priv->events ? priv->events->data : NULL;
1377 empathy_event_activate (EmpathyEvent *event_public)
1379 EventPriv *event = (EventPriv *) event_public;
1381 g_return_if_fail (event_public != NULL);
1384 event->func (event);
1386 event_remove (event);
1390 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1392 EventPriv *event = (EventPriv *) event_public;
1394 g_return_if_fail (event_public != NULL);
1396 event->inhibit = TRUE;
1400 empathy_event_approve (EmpathyEvent *event_public)
1402 EventPriv *event = (EventPriv *) event_public;
1404 g_return_if_fail (event_public != NULL);
1406 event_manager_approval_approve (event->approval);
1410 empathy_event_decline (EmpathyEvent *event_public)
1412 EventPriv *event = (EventPriv *) event_public;
1414 g_return_if_fail (event_public != NULL);
1416 reject_approval (event->approval);