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-contact-manager.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 EmpathyContactManager *contact_manager;
85 /* Ongoing approvals */
90 GSettings *gsettings_notif;
91 GSettings *gsettings_ui;
93 EmpathySoundManager *sound_mgr;
94 } EmpathyEventManagerPriv;
96 typedef struct _EventPriv EventPriv;
97 typedef void (*EventFunc) (EventPriv *event);
101 EmpathyEventManager *manager;
102 EventManagerApproval *approval;
106 guint autoremove_timeout_id;
116 static guint signals[LAST_SIGNAL];
118 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
120 static EmpathyEventManager * manager_singleton = NULL;
122 static EventManagerApproval *
123 event_manager_approval_new (EmpathyEventManager *manager,
124 TpChannelDispatchOperation *operation,
125 TpChannel *main_channel)
127 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
128 result->operation = g_object_ref (operation);
129 result->manager = manager;
130 result->main_channel = g_object_ref (main_channel);
136 event_manager_approval_free (EventManagerApproval *approval)
138 g_signal_handler_disconnect (approval->operation,
139 approval->invalidated_handler);
140 g_object_unref (approval->operation);
142 g_object_unref (approval->main_channel);
144 if (approval->handler != 0)
145 g_signal_handler_disconnect (approval->handler_instance,
148 if (approval->handler_instance != NULL)
149 g_object_unref (approval->handler_instance);
151 if (approval->contact != NULL)
152 g_object_unref (approval->contact);
154 if (approval->dialog != NULL)
156 gtk_widget_destroy (approval->dialog);
159 g_slice_free (EventManagerApproval, approval);
163 event_free (EventPriv *event)
165 g_free (event->public.icon_name);
166 g_free (event->public.header);
167 g_free (event->public.message);
169 if (event->autoremove_timeout_id != 0)
170 g_source_remove (event->autoremove_timeout_id);
172 tp_clear_object (&(event->public.contact));
173 tp_clear_object (&(event->public.account));
175 g_slice_free (EventPriv, event);
179 event_remove (EventPriv *event)
181 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
183 DEBUG ("Removing event %p", event);
185 priv->events = g_slist_remove (priv->events, event);
186 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
191 empathy_event_remove (EmpathyEvent *event_public)
193 EventPriv *event = (EventPriv *) event_public;
195 event_remove (event);
199 autoremove_event_timeout_cb (EventPriv *event)
201 event->autoremove_timeout_id = 0;
202 event_remove (event);
207 display_notify_area (EmpathyEventManager *self)
209 EmpathyEventManagerPriv *priv = GET_PRIV (self);
211 return g_settings_get_boolean (priv->gsettings_ui,
212 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
216 event_manager_add (EmpathyEventManager *manager,
218 EmpathyContact *contact,
219 EmpathyEventType type,
220 const gchar *icon_name,
222 const gchar *message,
223 EventManagerApproval *approval,
227 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
230 event = g_slice_new0 (EventPriv);
231 event->public.account = account != NULL ? g_object_ref (account) : NULL;
232 event->public.contact = contact ? g_object_ref (contact) : NULL;
233 event->public.type = type;
234 event->public.icon_name = g_strdup (icon_name);
235 event->public.header = g_strdup (header);
236 event->public.message = g_strdup (message);
237 event->public.must_ack = (func != NULL);
238 if (approval != NULL)
239 event->public.handler_instance = approval->handler_instance;
240 event->inhibit = FALSE;
242 event->user_data = user_data;
243 event->manager = manager;
244 event->approval = approval;
246 DEBUG ("Adding event %p", event);
247 priv->events = g_slist_prepend (priv->events, event);
249 if (!display_notify_area (manager))
251 /* Don't fire the 'event-added' signal as we activate the event now */
252 if (approval != NULL)
253 approval->auto_approved = TRUE;
255 empathy_event_activate (&event->public);
259 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
261 if (!event->public.must_ack)
263 event->autoremove_timeout_id = g_timeout_add_seconds (
264 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
270 handle_with_cb (GObject *source,
271 GAsyncResult *result,
274 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
275 GError *error = NULL;
277 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
279 DEBUG ("HandleWith failed: %s\n", error->message);
280 g_error_free (error);
285 handle_with_time_cb (GObject *source,
286 GAsyncResult *result,
289 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
290 GError *error = NULL;
292 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
295 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
297 EventManagerApproval *approval = user_data;
299 DEBUG ("HandleWithTime() is not implemented, falling back to "
300 "HandleWith(). Please upgrade to telepathy-mission-control "
303 tp_channel_dispatch_operation_handle_with_async (approval->operation,
304 NULL, handle_with_cb, approval);
308 DEBUG ("HandleWithTime failed: %s\n", error->message);
310 g_error_free (error);
315 event_manager_approval_approve (EventManagerApproval *approval)
319 if (approval->auto_approved)
321 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
325 timestamp = empathy_get_current_action_time ();
328 g_assert (approval->operation != NULL);
330 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
331 NULL, timestamp, handle_with_time_cb, approval);
335 event_channel_process_func (EventPriv *event)
337 event_manager_approval_approve (event->approval);
341 event_text_channel_process_func (EventPriv *event)
343 EmpathyTpChat *tp_chat;
345 if (event->approval->handler != 0)
347 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
349 g_signal_handler_disconnect (tp_chat, event->approval->handler);
350 event->approval->handler = 0;
353 event_manager_approval_approve (event->approval);
357 event_lookup_by_approval (EmpathyEventManager *manager,
358 EventManagerApproval *approval)
360 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
362 EventPriv *retval = NULL;
364 for (l = priv->events; l; l = l->next)
366 EventPriv *event = l->data;
368 if (event->approval == approval)
379 event_update (EmpathyEventManager *manager, EventPriv *event,
380 const char *icon_name, const char *header, const char *msg)
382 g_free (event->public.icon_name);
383 g_free (event->public.header);
384 g_free (event->public.message);
386 event->public.icon_name = g_strdup (icon_name);
387 event->public.header = g_strdup (header);
388 event->public.message = g_strdup (msg);
390 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
394 reject_channel_claim_cb (GObject *source,
395 GAsyncResult *result,
398 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
399 GError *error = NULL;
401 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
403 DEBUG ("Failed to claim channel: %s", error->message);
405 g_error_free (error);
409 if (EMPATHY_IS_TP_STREAMED_MEDIA (user_data))
411 empathy_tp_streamed_media_close (user_data);
413 else if (TPY_IS_CALL_CHANNEL (user_data))
415 tpy_call_channel_hangup_async (user_data,
416 TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
418 tp_channel_close_async (user_data, NULL, NULL);
420 else if (EMPATHY_IS_TP_CHAT (user_data))
422 empathy_tp_chat_leave (user_data, "");
424 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
426 tp_channel_close_async (user_data, NULL, NULL);
430 g_object_unref (user_data);
434 reject_approval (EventManagerApproval *approval)
436 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
438 /* We have to claim the channel before closing it */
440 /* Unfortunately, we need to special case the auth channels for the
441 * time being as they don't have a wrapper object handler in
442 * approval->handler_instance as they're not actually handled by
443 * this process, so we can just use a noddy callback to call Close()
445 if (approval->handler_instance != NULL)
447 tp_channel_dispatch_operation_claim_with_async (approval->operation,
448 priv->approver, reject_channel_claim_cb,
449 g_object_ref (approval->handler_instance));
451 else if (tp_channel_get_channel_type_id (approval->main_channel)
452 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
454 tp_channel_dispatch_operation_close_channels_async (approval->operation,
460 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
461 gint response, gpointer user_data)
463 EventManagerApproval *approval = user_data;
465 gtk_widget_destroy (approval->dialog);
466 approval->dialog = NULL;
468 if (response == GTK_RESPONSE_ACCEPT)
470 event_manager_approval_approve (approval);
472 else if (response == ACCEPT_WITHOUT_VIDEO)
474 tpy_call_channel_send_video (TPY_CALL_CHANNEL (approval->main_channel),
476 event_manager_approval_approve (approval);
480 reject_approval (approval);
485 event_channel_process_voip_func (EventPriv *event)
492 EmpathyEventType etype = event->public.type;
494 if (event->approval->dialog != NULL)
496 gtk_window_present (GTK_WINDOW (event->approval->dialog));
500 if (etype == EMPATHY_EVENT_TYPE_VOIP)
502 EmpathyTpStreamedMedia *call;
503 call = EMPATHY_TP_STREAMED_MEDIA (event->approval->handler_instance);
504 video = empathy_tp_streamed_media_has_initial_video (call);
506 else if (etype == EMPATHY_EVENT_TYPE_CALL)
508 TpyCallChannel *call;
509 call = TPY_CALL_CHANNEL (event->approval->handler_instance);
510 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
514 g_warning ("Unknown event type: %d", event->public.type);
518 dialog = gtk_message_dialog_new (NULL, 0,
519 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
520 video ? _("Incoming video call"): _("Incoming call"));
522 gtk_message_dialog_format_secondary_text (
523 GTK_MESSAGE_DIALOG (dialog), video ?
524 _("%s is video calling you. Do you want to answer?"):
525 _("%s is calling you. Do you want to answer?"),
526 empathy_contact_get_alias (event->approval->contact));
528 title = g_strdup_printf (_("Incoming call from %s"),
529 empathy_contact_get_alias (event->approval->contact));
531 gtk_window_set_title (GTK_WINDOW (dialog), title);
534 /* Set image of the dialog */
537 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
538 GTK_ICON_SIZE_DIALOG);
542 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
543 GTK_ICON_SIZE_DIALOG);
546 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
547 gtk_widget_show (image);
549 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
552 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
553 _("_Reject"), GTK_RESPONSE_REJECT);
554 image = gtk_image_new_from_icon_name ("call-stop",
555 GTK_ICON_SIZE_BUTTON);
556 gtk_button_set_image (GTK_BUTTON (button), image);
558 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
560 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
561 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
563 image = gtk_image_new_from_icon_name ("call-start",
564 GTK_ICON_SIZE_BUTTON);
565 gtk_button_set_image (GTK_BUTTON (button), image);
568 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
569 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
571 image = gtk_image_new_from_icon_name ("call-start",
572 GTK_ICON_SIZE_BUTTON);
573 gtk_button_set_image (GTK_BUTTON (button), image);
575 g_signal_connect (dialog, "response",
576 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
579 gtk_widget_show (dialog);
581 event->approval->dialog = dialog;
585 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
586 EmpathyMessage *message,
587 EventManagerApproval *approval)
590 EmpathyContact *sender;
594 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
596 /* try to update the event if it's referring to a chat which is already in the
598 event = event_lookup_by_approval (approval->manager, approval);
600 sender = empathy_message_get_sender (message);
602 /* We only want to show incoming messages */
603 if (empathy_contact_is_user (sender))
606 header = empathy_contact_get_alias (sender);
607 msg = empathy_message_get_body (message);
610 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
613 event_manager_add (approval->manager, NULL, sender,
614 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
615 approval, event_text_channel_process_func, NULL);
617 window = empathy_main_window_dup ();
619 empathy_sound_manager_play (priv->sound_mgr, window,
620 EMPATHY_SOUND_CONVERSATION_NEW);
622 g_object_unref (window);
626 event_manager_approval_done (EventManagerApproval *approval)
628 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
631 if (approval->operation != NULL)
635 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
637 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
638 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
641 if (priv->ringing == 0)
642 empathy_sound_manager_stop (priv->sound_mgr,
643 EMPATHY_SOUND_PHONE_INCOMING);
647 priv->approvals = g_slist_remove (priv->approvals, approval);
649 for (l = priv->events; l; l = l->next)
651 EventPriv *event = l->data;
653 if (event->approval == approval)
655 event_remove (event);
660 event_manager_approval_free (approval);
664 cdo_invalidated_cb (TpProxy *cdo,
668 EventManagerApproval *approval)
670 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
672 event_manager_approval_done (approval);
676 event_manager_call_state_changed_cb (TpyCallChannel *call,
679 const GValueArray *call_state_reason,
680 GHashTable *call_state_details,
681 EventManagerApproval *approval)
683 if (state == TPY_CALL_STATE_ENDED)
685 DEBUG ("Call ended, seems we missed it :/");
686 reject_approval (approval);
691 event_manager_call_channel_got_contact_cb (TpConnection *connection,
692 EmpathyContact *contact,
697 EventManagerApproval *approval = (EventManagerApproval *) user_data;
698 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
700 TpyCallChannel *call;
704 call = TPY_CALL_CHANNEL (approval->handler_instance);
708 DEBUG ("Can't get the contact for the call.. Rejecting?");
709 reject_approval (approval);
713 if (tpy_call_channel_get_state (call, NULL, NULL) == TPY_CALL_STATE_ENDED)
715 DEBUG ("Call already ended, seems we missed it :/");
716 reject_approval (approval);
720 approval->handler = g_signal_connect (call, "state-changed",
721 G_CALLBACK (event_manager_call_state_changed_cb), approval);
723 window = empathy_main_window_dup ();
724 approval->contact = g_object_ref (contact);
726 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
728 header = g_strdup_printf (
729 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
730 empathy_contact_get_alias (approval->contact));
732 event_manager_add (approval->manager, NULL,
733 approval->contact, EMPATHY_EVENT_TYPE_CALL,
734 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
735 header, NULL, approval,
736 event_channel_process_voip_func, NULL);
741 if (priv->ringing == 1)
742 empathy_sound_manager_start_playing (priv->sound_mgr, window,
743 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
745 g_object_unref (window);
749 event_manager_media_channel_got_contact (EventManagerApproval *approval)
751 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
752 GtkWidget *window = empathy_main_window_dup ();
754 EmpathyTpStreamedMedia *call;
757 call = EMPATHY_TP_STREAMED_MEDIA (approval->handler_instance);
759 video = empathy_tp_streamed_media_has_initial_video (call);
761 header = g_strdup_printf (
762 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
763 empathy_contact_get_alias (approval->contact));
765 event_manager_add (approval->manager, NULL,
766 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
767 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
768 header, NULL, approval,
769 event_channel_process_voip_func, NULL);
774 if (priv->ringing == 1)
775 empathy_sound_manager_start_playing (priv->sound_mgr, window,
776 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
778 g_object_unref (window);
782 event_manager_media_channel_contact_changed_cb (EmpathyTpStreamedMedia *call,
783 GParamSpec *param, EventManagerApproval *approval)
785 EmpathyContact *contact;
787 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
792 approval->contact = contact;
793 event_manager_media_channel_got_contact (approval);
797 invite_dialog_response_cb (GtkDialog *dialog,
799 EventManagerApproval *approval)
801 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
802 approval->dialog = NULL;
804 if (response != GTK_RESPONSE_OK)
807 DEBUG ("Muc invitation rejected");
809 reject_approval (approval);
814 DEBUG ("Muc invitation accepted");
816 /* We'll join the room when handling the channel */
817 event_manager_approval_approve (approval);
821 event_room_channel_process_func (EventPriv *event)
823 GtkWidget *dialog, *button, *image;
824 TpChannel *channel = event->approval->main_channel;
827 if (event->approval->dialog != NULL)
829 gtk_window_present (GTK_WINDOW (event->approval->dialog));
834 dialog = gtk_message_dialog_new (NULL, 0,
835 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
837 title = g_strdup_printf (_("Invitation to join %s"),
838 tp_channel_get_identifier (channel));
840 gtk_window_set_title (GTK_WINDOW (dialog), title);
843 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
844 _("%s is inviting you to join %s"),
845 empathy_contact_get_alias (event->approval->contact),
846 tp_channel_get_identifier (channel));
848 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
851 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
852 _("_Decline"), GTK_RESPONSE_CANCEL);
853 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
854 gtk_button_set_image (GTK_BUTTON (button), image);
856 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
857 _("_Join"), GTK_RESPONSE_OK);
858 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
859 gtk_button_set_image (GTK_BUTTON (button), image);
861 g_signal_connect (dialog, "response",
862 G_CALLBACK (invite_dialog_response_cb), event->approval);
864 gtk_widget_show (dialog);
866 event->approval->dialog = dialog;
870 display_invite_room_dialog (EventManagerApproval *approval)
872 GtkWidget *window = empathy_main_window_dup ();
873 const gchar *invite_msg;
875 TpHandle self_handle;
876 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
878 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
879 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
880 NULL, NULL, &invite_msg);
882 if (approval->contact != NULL)
884 msg = g_strdup_printf (_("%s invited you to join %s"),
885 empathy_contact_get_alias (approval->contact),
886 tp_channel_get_identifier (approval->main_channel));
890 msg = g_strdup_printf (_("You have been invited to join %s"),
891 tp_channel_get_identifier (approval->main_channel));
894 event_manager_add (approval->manager, NULL,
895 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
896 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
897 event_room_channel_process_func, NULL);
899 empathy_sound_manager_play (priv->sound_mgr, window,
900 EMPATHY_SOUND_CONVERSATION_NEW);
903 g_object_unref (window);
907 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
908 EmpathyContact *contact,
913 EventManagerApproval *approval = (EventManagerApproval *) user_data;
917 DEBUG ("Error: %s", error->message);
921 approval->contact = g_object_ref (contact);
924 display_invite_room_dialog (approval);
928 event_manager_ft_got_contact_cb (TpConnection *connection,
929 EmpathyContact *contact,
934 EventManagerApproval *approval = (EventManagerApproval *) user_data;
935 GtkWidget *window = empathy_main_window_dup ();
937 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
939 approval->contact = g_object_ref (contact);
941 header = g_strdup_printf (_("Incoming file transfer from %s"),
942 empathy_contact_get_alias (approval->contact));
944 event_manager_add (approval->manager, NULL,
945 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
946 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
947 approval, event_channel_process_func, NULL);
949 /* FIXME better sound for incoming file transfers ?*/
950 empathy_sound_manager_play (priv->sound_mgr, window,
951 EMPATHY_SOUND_CONVERSATION_NEW);
954 g_object_unref (window);
958 event_manager_auth_process_func (EventPriv *event)
960 empathy_event_approve ((EmpathyEvent *) event);
963 /* If there is a file-transfer, media, or auth channel consider it as
966 find_main_channel (GList *channels)
969 TpChannel *text = NULL;
971 for (l = channels; l != NULL; l = g_list_next (l))
973 TpChannel *channel = l->data;
976 if (tp_proxy_get_invalidated (channel) != NULL)
979 channel_type = tp_channel_get_channel_type_id (channel);
981 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
982 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL ||
983 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
984 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
987 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
995 approve_channels (TpSimpleApprover *approver,
997 TpConnection *connection,
999 TpChannelDispatchOperation *dispatch_operation,
1000 TpAddDispatchOperationContext *context,
1003 EmpathyEventManager *self = user_data;
1004 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1006 EventManagerApproval *approval;
1007 GQuark channel_type;
1009 channel = find_main_channel (channels);
1010 if (channel == NULL)
1012 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1013 "Unknown channel type" };
1015 DEBUG ("Failed to find the main channel; ignoring");
1017 tp_add_dispatch_operation_context_fail (context, &error);
1021 approval = event_manager_approval_new (self, dispatch_operation, channel);
1022 priv->approvals = g_slist_prepend (priv->approvals, approval);
1024 approval->invalidated_handler = g_signal_connect (dispatch_operation,
1025 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
1027 channel_type = tp_channel_get_channel_type_id (channel);
1029 if (TP_IS_TEXT_CHANNEL (channel))
1031 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
1032 GList *messages, *l;
1034 approval->handler_instance = g_object_ref (tp_chat);
1036 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
1038 /* Are we in local-pending ? */
1041 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
1043 /* We are invited to a room */
1044 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
1045 tp_channel_get_identifier (channel));
1049 empathy_tp_contact_factory_get_from_handle (connection,
1050 inviter, event_manager_muc_invite_got_contact_cb,
1051 approval, NULL, G_OBJECT (self));
1055 display_invite_room_dialog (approval);
1061 /* We are not invited, approve the channel right now */
1062 tp_add_dispatch_operation_context_accept (context);
1064 approval->auto_approved = TRUE;
1065 event_manager_approval_approve (approval);
1069 /* 1-1 text channel, wait for the first message */
1070 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
1071 G_CALLBACK (event_manager_chat_message_received_cb), approval);
1073 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
1074 for (l = messages; l != NULL; l = g_list_next (l))
1076 EmpathyMessage *msg = l->data;
1078 event_manager_chat_message_received_cb (tp_chat, msg, approval);
1081 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
1083 EmpathyContact *contact;
1084 EmpathyTpStreamedMedia *call = empathy_tp_streamed_media_new (account,
1087 approval->handler_instance = G_OBJECT (call);
1089 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
1091 if (contact == NULL)
1093 g_signal_connect (call, "notify::contact",
1094 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
1099 approval->contact = contact;
1100 event_manager_media_channel_got_contact (approval);
1104 else if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
1106 TpyCallChannel *call = TPY_CALL_CHANNEL (channel);
1109 approval->handler_instance = g_object_ref (call);
1111 id = tp_channel_get_identifier (channel);
1113 empathy_tp_contact_factory_get_from_id (connection, id,
1114 event_manager_call_channel_got_contact_cb,
1115 approval, NULL, G_OBJECT (self));
1117 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1121 approval->handler_instance = g_object_ref (channel);
1123 handle = tp_channel_get_handle (channel, NULL);
1125 empathy_tp_contact_factory_get_from_handle (connection, handle,
1126 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1128 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1131 const gchar * const *available_mechanisms;
1133 props = tp_channel_borrow_immutable_properties (channel);
1134 available_mechanisms = tp_asv_get_boxed (props,
1135 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1138 if (tp_strv_contains (available_mechanisms, "X-TELEPATHY-PASSWORD"))
1140 event_manager_add (approval->manager, account, NULL,
1141 EMPATHY_EVENT_TYPE_AUTH,
1142 GTK_STOCK_DIALOG_AUTHENTICATION,
1143 tp_account_get_display_name (account),
1144 _("Password required"), approval,
1145 event_manager_auth_process_func, NULL);
1149 GError error = { TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
1150 "Support only X-TELEPATHY-PASSWORD auth method" };
1152 tp_add_dispatch_operation_context_fail (context, &error);
1158 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1159 "Invalid channel type" };
1161 DEBUG ("Unknown channel type (%s), ignoring..",
1162 g_quark_to_string (channel_type));
1164 tp_add_dispatch_operation_context_fail (context, &error);
1169 tp_add_dispatch_operation_context_accept (context);
1173 event_pending_subscribe_func (EventPriv *event)
1175 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1177 event_remove (event);
1181 event_manager_pendings_changed_cb (EmpathyContactList *list,
1182 EmpathyContact *contact, EmpathyContact *actor,
1183 guint reason, gchar *message, gboolean is_pending,
1184 EmpathyEventManager *manager)
1186 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1187 gchar *header, *event_msg;
1193 for (l = priv->events; l; l = l->next)
1195 EventPriv *event = l->data;
1197 if (event->public.contact == contact &&
1198 event->func == event_pending_subscribe_func)
1200 event_remove (event);
1208 header = g_strdup_printf (
1209 _("%s would like permission to see when you are online"),
1210 empathy_contact_get_alias (contact));
1212 if (!EMP_STR_EMPTY (message))
1213 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1217 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1218 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1219 event_pending_subscribe_func, NULL);
1226 event_manager_presence_changed_cb (EmpathyContact *contact,
1227 TpConnectionPresenceType current,
1228 TpConnectionPresenceType previous,
1229 EmpathyEventManager *manager)
1231 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1233 EmpathyPresenceManager *presence_mgr;
1234 GtkWidget *window = empathy_main_window_dup ();
1236 account = empathy_contact_get_account (contact);
1237 presence_mgr = empathy_presence_manager_dup_singleton ();
1239 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1242 if (tp_connection_presence_type_cmp_availability (previous,
1243 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1245 /* contact was online */
1246 if (tp_connection_presence_type_cmp_availability (current,
1247 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1249 /* someone is logging off */
1250 empathy_sound_manager_play (priv->sound_mgr, window,
1251 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1253 if (g_settings_get_boolean (priv->gsettings_notif,
1254 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1256 event_manager_add (manager, NULL, contact,
1257 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1258 EMPATHY_IMAGE_AVATAR_DEFAULT,
1259 empathy_contact_get_alias (contact), _("Disconnected"),
1266 /* contact was offline */
1267 if (tp_connection_presence_type_cmp_availability (current,
1268 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1270 /* someone is logging in */
1271 empathy_sound_manager_play (priv->sound_mgr, window,
1272 EMPATHY_SOUND_CONTACT_CONNECTED);
1274 if (g_settings_get_boolean (priv->gsettings_notif,
1275 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1277 event_manager_add (manager, NULL, contact,
1278 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1279 EMPATHY_IMAGE_AVATAR_DEFAULT,
1280 empathy_contact_get_alias (contact), _("Connected"),
1287 g_object_unref (presence_mgr);
1288 g_object_unref (window);
1292 event_manager_members_changed_cb (EmpathyContactList *list,
1293 EmpathyContact *contact,
1294 EmpathyContact *actor,
1298 EmpathyEventManager *manager)
1301 g_signal_connect (contact, "presence-changed",
1302 G_CALLBACK (event_manager_presence_changed_cb), manager);
1304 g_signal_handlers_disconnect_by_func (contact,
1305 event_manager_presence_changed_cb, manager);
1309 event_manager_constructor (GType type,
1311 GObjectConstructParam *props)
1315 if (manager_singleton) {
1316 retval = g_object_ref (manager_singleton);
1318 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1319 (type, n_props, props);
1321 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1322 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1329 event_manager_finalize (GObject *object)
1331 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1333 if (priv->ringing > 0)
1334 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1336 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1337 g_slist_free (priv->events);
1338 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1339 g_slist_free (priv->approvals);
1340 g_object_unref (priv->contact_manager);
1341 g_object_unref (priv->approver);
1342 g_object_unref (priv->auth_approver);
1343 g_object_unref (priv->gsettings_notif);
1344 g_object_unref (priv->gsettings_ui);
1345 g_object_unref (priv->sound_mgr);
1349 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1351 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1353 object_class->finalize = event_manager_finalize;
1354 object_class->constructor = event_manager_constructor;
1356 signals[EVENT_ADDED] =
1357 g_signal_new ("event-added",
1358 G_TYPE_FROM_CLASS (klass),
1362 g_cclosure_marshal_generic,
1366 signals[EVENT_REMOVED] =
1367 g_signal_new ("event-removed",
1368 G_TYPE_FROM_CLASS (klass),
1372 g_cclosure_marshal_generic,
1373 G_TYPE_NONE, 1, G_TYPE_POINTER);
1375 signals[EVENT_UPDATED] =
1376 g_signal_new ("event-updated",
1377 G_TYPE_FROM_CLASS (klass),
1381 g_cclosure_marshal_generic,
1382 G_TYPE_NONE, 1, G_TYPE_POINTER);
1384 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1388 empathy_event_manager_init (EmpathyEventManager *manager)
1390 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1391 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1392 GError *error = NULL;
1393 TpAccountManager *am;
1395 manager->priv = priv;
1397 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1398 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1400 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1402 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1403 g_signal_connect (priv->contact_manager, "pendings-changed",
1404 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1406 g_signal_connect (priv->contact_manager, "members-changed",
1407 G_CALLBACK (event_manager_members_changed_cb), manager);
1409 am = tp_account_manager_dup ();
1411 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1412 FALSE, approve_channels, manager, NULL);
1414 /* Private text channels */
1415 tp_base_client_take_approver_filter (priv->approver,
1417 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1418 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1421 /* Muc text channels */
1422 tp_base_client_take_approver_filter (priv->approver,
1424 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1425 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1429 tp_base_client_take_approver_filter (priv->approver,
1431 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1432 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1433 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1437 tp_base_client_take_approver_filter (priv->approver,
1439 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1440 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1441 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1443 tp_base_client_take_approver_filter (priv->approver,
1445 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1446 TPY_IFACE_CHANNEL_TYPE_CALL,
1447 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1450 /* I don't feel good about doing this, and I'm sorry, but the
1451 * capabilities connection feature is added earlier because it's
1452 * needed for EmpathyTpChat. If the capabilities feature is required
1453 * then preparing an auth channel (which of course appears in the
1454 * CONNECTING state) will never be prepared. So the options are
1455 * either to create another approver like I've done, or to port
1456 * EmpathyTpChat and its users to not depend on the connection being
1457 * prepared with capabilities. I chose the former, obviously. :-) */
1459 priv->auth_approver = tp_simple_approver_new_with_am (am,
1460 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1463 /* SASL auth channels */
1464 tp_base_client_take_approver_filter (priv->auth_approver,
1466 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1467 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1468 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1470 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1473 if (!tp_base_client_register (priv->approver, &error))
1475 DEBUG ("Failed to register Approver: %s", error->message);
1476 g_error_free (error);
1479 if (!tp_base_client_register (priv->auth_approver, &error))
1481 DEBUG ("Failed to register auth Approver: %s", error->message);
1482 g_error_free (error);
1485 g_object_unref (am);
1488 EmpathyEventManager *
1489 empathy_event_manager_dup_singleton (void)
1491 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1495 empathy_event_manager_get_events (EmpathyEventManager *manager)
1497 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1499 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1501 return priv->events;
1505 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1507 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1509 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1511 return priv->events ? priv->events->data : NULL;
1515 empathy_event_activate (EmpathyEvent *event_public)
1517 EventPriv *event = (EventPriv *) event_public;
1519 g_return_if_fail (event_public != NULL);
1522 event->func (event);
1524 event_remove (event);
1528 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1530 EventPriv *event = (EventPriv *) event_public;
1532 g_return_if_fail (event_public != NULL);
1534 event->inhibit = TRUE;
1538 empathy_event_approve (EmpathyEvent *event_public)
1540 EventPriv *event = (EventPriv *) event_public;
1542 g_return_if_fail (event_public != NULL);
1544 event_manager_approval_approve (event->approval);
1548 empathy_event_decline (EmpathyEvent *event_public)
1550 EventPriv *event = (EventPriv *) event_public;
1552 g_return_if_fail (event_public != NULL);
1554 reject_approval (event->approval);