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-connection-aggregator.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-gsettings.h>
39 #include <extensions/extensions.h>
41 #include <libempathy-gtk/empathy-images.h>
42 #include <libempathy-gtk/empathy-contact-dialogs.h>
43 #include <libempathy-gtk/empathy-sound-manager.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-call-utils.h>
47 #include "empathy-event-manager.h"
48 #include "empathy-roster-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 #define ACCEPT_WITHOUT_VIDEO 1
59 /* The time interval in milliseconds between 2 incoming rings */
60 #define MS_BETWEEN_RING 500
63 EmpathyEventManager *manager;
64 TpChannelDispatchOperation *operation;
65 gulong invalidated_handler;
66 /* Remove contact if applicable */
67 EmpathyContact *contact;
68 /* option signal handler and it's instance */
70 GObject *handler_instance;
71 /* optional accept widget */
73 /* Channel of the CDO that will be used during the approval */
74 TpChannel *main_channel;
75 gboolean auto_approved;
76 } EventManagerApproval;
79 TpBaseClient *approver;
80 TpBaseClient *auth_approver;
81 EmpathyConnectionAggregator *conn_aggregator;
83 /* Ongoing approvals */
88 GSettings *gsettings_notif;
89 GSettings *gsettings_ui;
91 EmpathySoundManager *sound_mgr;
93 /* TpContact -> EmpathyContact */
95 } EmpathyEventManagerPriv;
97 typedef struct _EventPriv EventPriv;
98 typedef void (*EventFunc) (EventPriv *event);
102 EmpathyEventManager *manager;
103 EventManagerApproval *approval;
107 guint autoremove_timeout_id;
117 static guint signals[LAST_SIGNAL];
119 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
121 static EmpathyEventManager * manager_singleton = NULL;
123 static EventManagerApproval *
124 event_manager_approval_new (EmpathyEventManager *manager,
125 TpChannelDispatchOperation *operation,
126 TpChannel *main_channel)
128 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
129 result->operation = g_object_ref (operation);
130 result->manager = manager;
131 result->main_channel = g_object_ref (main_channel);
137 event_manager_approval_free (EventManagerApproval *approval)
139 g_signal_handler_disconnect (approval->operation,
140 approval->invalidated_handler);
141 g_object_unref (approval->operation);
143 g_object_unref (approval->main_channel);
145 if (approval->handler != 0)
146 g_signal_handler_disconnect (approval->handler_instance,
149 if (approval->handler_instance != NULL)
150 g_object_unref (approval->handler_instance);
152 if (approval->contact != NULL)
153 g_object_unref (approval->contact);
155 if (approval->dialog != NULL)
157 gtk_widget_destroy (approval->dialog);
160 g_slice_free (EventManagerApproval, approval);
164 event_free (EventPriv *event)
166 g_free (event->public.icon_name);
167 g_free (event->public.header);
168 g_free (event->public.message);
170 if (event->autoremove_timeout_id != 0)
171 g_source_remove (event->autoremove_timeout_id);
173 tp_clear_object (&(event->public.contact));
174 tp_clear_object (&(event->public.account));
176 g_slice_free (EventPriv, event);
180 event_remove (EventPriv *event)
182 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
184 DEBUG ("Removing event %p", event);
186 priv->events = g_slist_remove (priv->events, event);
187 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
192 empathy_event_remove (EmpathyEvent *event_public)
194 EventPriv *event = (EventPriv *) event_public;
196 event_remove (event);
200 autoremove_event_timeout_cb (EventPriv *event)
202 event->autoremove_timeout_id = 0;
203 event_remove (event);
208 display_notify_area (EmpathyEventManager *self)
210 EmpathyEventManagerPriv *priv = GET_PRIV (self);
212 return g_settings_get_boolean (priv->gsettings_ui,
213 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
217 event_manager_add (EmpathyEventManager *manager,
219 EmpathyContact *contact,
220 EmpathyEventType type,
221 const gchar *icon_name,
223 const gchar *message,
224 EventManagerApproval *approval,
228 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
231 event = g_slice_new0 (EventPriv);
232 event->public.account = account != NULL ? g_object_ref (account) : NULL;
233 event->public.contact = contact ? g_object_ref (contact) : NULL;
234 event->public.type = type;
235 event->public.icon_name = g_strdup (icon_name);
236 event->public.header = g_strdup (header);
237 event->public.message = g_strdup (message);
238 event->public.must_ack = (func != NULL);
239 if (approval != NULL)
240 event->public.handler_instance = approval->handler_instance;
241 event->inhibit = FALSE;
243 event->user_data = user_data;
244 event->manager = manager;
245 event->approval = approval;
247 DEBUG ("Adding event %p", event);
248 priv->events = g_slist_prepend (priv->events, event);
250 if (!display_notify_area (manager))
252 /* Don't fire the 'event-added' signal as we activate the event now */
253 if (approval != NULL)
254 approval->auto_approved = TRUE;
256 empathy_event_activate (&event->public);
260 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
262 if (!event->public.must_ack)
264 event->autoremove_timeout_id = g_timeout_add_seconds (
265 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
271 handle_with_cb (GObject *source,
272 GAsyncResult *result,
275 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
276 GError *error = NULL;
278 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
280 DEBUG ("HandleWith failed: %s\n", error->message);
281 g_error_free (error);
286 handle_with_time_cb (GObject *source,
287 GAsyncResult *result,
290 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
291 GError *error = NULL;
293 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
296 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
298 EventManagerApproval *approval = user_data;
300 DEBUG ("HandleWithTime() is not implemented, falling back to "
301 "HandleWith(). Please upgrade to telepathy-mission-control "
304 tp_channel_dispatch_operation_handle_with_async (approval->operation,
305 NULL, handle_with_cb, approval);
309 DEBUG ("HandleWithTime failed: %s\n", error->message);
311 g_error_free (error);
316 event_manager_approval_approve (EventManagerApproval *approval)
320 if (approval->auto_approved)
322 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
326 timestamp = empathy_get_current_action_time ();
329 g_assert (approval->operation != NULL);
331 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
332 NULL, timestamp, handle_with_time_cb, approval);
336 event_channel_process_func (EventPriv *event)
338 event_manager_approval_approve (event->approval);
342 event_text_channel_process_func (EventPriv *event)
344 EmpathyTpChat *tp_chat;
346 if (event->approval->handler != 0)
348 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
350 g_signal_handler_disconnect (tp_chat, event->approval->handler);
351 event->approval->handler = 0;
354 event_manager_approval_approve (event->approval);
358 event_lookup_by_approval (EmpathyEventManager *manager,
359 EventManagerApproval *approval)
361 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
363 EventPriv *retval = NULL;
365 for (l = priv->events; l; l = l->next)
367 EventPriv *event = l->data;
369 if (event->approval == approval)
380 event_update (EmpathyEventManager *manager, EventPriv *event,
381 const char *icon_name, const char *header, const char *msg)
383 g_free (event->public.icon_name);
384 g_free (event->public.header);
385 g_free (event->public.message);
387 event->public.icon_name = g_strdup (icon_name);
388 event->public.header = g_strdup (header);
389 event->public.message = g_strdup (msg);
391 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
395 reject_channel_claim_cb (GObject *source,
396 GAsyncResult *result,
399 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
400 GError *error = NULL;
402 if (!tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error))
404 DEBUG ("Failed to claim channel: %s", error->message);
406 g_error_free (error);
410 if (TP_IS_CALL_CHANNEL (user_data))
412 tp_call_channel_hangup_async (user_data,
413 TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
415 tp_channel_close_async (user_data, NULL, NULL);
417 else if (EMPATHY_IS_TP_CHAT (user_data))
419 empathy_tp_chat_leave (user_data, "");
421 else if (TP_IS_FILE_TRANSFER_CHANNEL (user_data))
423 tp_channel_close_async (user_data, NULL, NULL);
427 g_object_unref (user_data);
431 reject_approval (EventManagerApproval *approval)
433 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
435 /* We have to claim the channel before closing it */
437 /* Unfortunately, we need to special case the auth channels for the
438 * time being as they don't have a wrapper object handler in
439 * approval->handler_instance as they're not actually handled by
440 * this process, so we can just use a noddy callback to call Close()
442 if (approval->handler_instance != NULL)
444 tp_channel_dispatch_operation_claim_with_async (approval->operation,
445 priv->approver, reject_channel_claim_cb,
446 g_object_ref (approval->handler_instance));
448 else if (tp_channel_get_channel_type_id (approval->main_channel)
449 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
451 tp_channel_dispatch_operation_close_channels_async (approval->operation,
457 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
458 gint response, gpointer user_data)
460 EventManagerApproval *approval = user_data;
462 gtk_widget_destroy (approval->dialog);
463 approval->dialog = NULL;
465 if (response == GTK_RESPONSE_ACCEPT)
467 event_manager_approval_approve (approval);
469 else if (response == ACCEPT_WITHOUT_VIDEO)
471 empathy_call_channel_send_video (TP_CALL_CHANNEL (approval->main_channel),
473 event_manager_approval_approve (approval);
477 reject_approval (approval);
482 event_channel_process_voip_func (EventPriv *event)
489 EmpathyEventType etype = event->public.type;
491 if (event->approval->dialog != NULL)
493 gtk_window_present (GTK_WINDOW (event->approval->dialog));
497 if (etype == EMPATHY_EVENT_TYPE_CALL)
500 call = TP_CALL_CHANNEL (event->approval->handler_instance);
501 video = tp_call_channel_has_initial_video (call, NULL);
505 g_warning ("Unknown event type: %d", event->public.type);
509 dialog = gtk_message_dialog_new (NULL, 0,
510 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
511 video ? _("Incoming video call"): _("Incoming call"));
513 gtk_message_dialog_format_secondary_text (
514 GTK_MESSAGE_DIALOG (dialog), video ?
515 _("%s is video calling you. Do you want to answer?"):
516 _("%s is calling you. Do you want to answer?"),
517 empathy_contact_get_alias (event->approval->contact));
519 title = g_strdup_printf (_("Incoming call from %s"),
520 empathy_contact_get_alias (event->approval->contact));
522 gtk_window_set_title (GTK_WINDOW (dialog), title);
525 /* Set image of the dialog */
528 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
529 GTK_ICON_SIZE_DIALOG);
533 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
534 GTK_ICON_SIZE_DIALOG);
537 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
538 gtk_widget_show (image);
540 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
543 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
544 _("_Reject"), GTK_RESPONSE_REJECT);
545 image = gtk_image_new_from_icon_name ("call-stop",
546 GTK_ICON_SIZE_BUTTON);
547 gtk_button_set_image (GTK_BUTTON (button), image);
549 if (video && etype == EMPATHY_EVENT_TYPE_CALL)
551 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
552 _("_Answer"), ACCEPT_WITHOUT_VIDEO);
554 image = gtk_image_new_from_icon_name ("call-start",
555 GTK_ICON_SIZE_BUTTON);
556 gtk_button_set_image (GTK_BUTTON (button), image);
559 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
560 video ? _("_Answer with video") : _("_Answer"), GTK_RESPONSE_ACCEPT);
562 image = gtk_image_new_from_icon_name ("call-start",
563 GTK_ICON_SIZE_BUTTON);
564 gtk_button_set_image (GTK_BUTTON (button), image);
566 g_signal_connect (dialog, "response",
567 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
570 gtk_widget_show (dialog);
572 event->approval->dialog = dialog;
576 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
577 EmpathyMessage *message,
578 EventManagerApproval *approval)
581 EmpathyContact *sender;
585 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
587 /* try to update the event if it's referring to a chat which is already in the
589 event = event_lookup_by_approval (approval->manager, approval);
591 sender = empathy_message_get_sender (message);
593 /* We only want to show incoming messages */
594 if (empathy_contact_is_user (sender))
597 header = empathy_contact_get_alias (sender);
598 msg = empathy_message_get_body (message);
601 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
604 event_manager_add (approval->manager, NULL, sender,
605 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
606 approval, event_text_channel_process_func, NULL);
608 window = empathy_roster_window_dup ();
610 empathy_sound_manager_play (priv->sound_mgr, window,
611 EMPATHY_SOUND_CONVERSATION_NEW);
613 g_object_unref (window);
617 event_manager_approval_done (EventManagerApproval *approval)
619 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
622 if (approval->operation != NULL)
626 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
628 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
629 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
632 if (priv->ringing == 0)
633 empathy_sound_manager_stop (priv->sound_mgr,
634 EMPATHY_SOUND_PHONE_INCOMING);
638 priv->approvals = g_slist_remove (priv->approvals, approval);
640 for (l = priv->events; l; l = l->next)
642 EventPriv *event = l->data;
644 if (event->approval == approval)
646 event_remove (event);
651 event_manager_approval_free (approval);
655 cdo_invalidated_cb (TpProxy *cdo,
659 EventManagerApproval *approval)
661 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
663 event_manager_approval_done (approval);
667 event_manager_call_state_changed_cb (TpCallChannel *call,
670 TpCallStateReason *reason,
672 EventManagerApproval *approval)
674 if (state == TP_CALL_STATE_ENDED)
676 DEBUG ("Call ended, seems we missed it :/");
677 reject_approval (approval);
682 event_manager_call_channel_got_contact_cb (TpConnection *connection,
683 EmpathyContact *contact,
688 EventManagerApproval *approval = (EventManagerApproval *) user_data;
689 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
695 call = TP_CALL_CHANNEL (approval->handler_instance);
699 DEBUG ("Can't get the contact for the call.. Rejecting?");
700 reject_approval (approval);
704 if (tp_call_channel_get_state (call, NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
706 DEBUG ("Call already ended, seems we missed it :/");
707 reject_approval (approval);
711 approval->handler = g_signal_connect (call, "state-changed",
712 G_CALLBACK (event_manager_call_state_changed_cb), approval);
714 window = empathy_roster_window_dup ();
715 approval->contact = g_object_ref (contact);
717 g_object_get (G_OBJECT (call), "initial-video", &video, NULL);
719 header = g_strdup_printf (
720 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
721 empathy_contact_get_alias (approval->contact));
723 event_manager_add (approval->manager, NULL,
724 approval->contact, EMPATHY_EVENT_TYPE_CALL,
725 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
726 header, NULL, approval,
727 event_channel_process_voip_func, NULL);
732 if (priv->ringing == 1)
733 empathy_sound_manager_start_playing (priv->sound_mgr, window,
734 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
736 g_object_unref (window);
740 invite_dialog_response_cb (GtkDialog *dialog,
742 EventManagerApproval *approval)
744 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
745 approval->dialog = NULL;
747 if (response != GTK_RESPONSE_OK)
750 DEBUG ("Muc invitation rejected");
752 reject_approval (approval);
757 DEBUG ("Muc invitation accepted");
759 /* We'll join the room when handling the channel */
760 event_manager_approval_approve (approval);
764 event_room_channel_process_func (EventPriv *event)
766 GtkWidget *dialog, *button, *image;
767 TpChannel *channel = event->approval->main_channel;
770 if (event->approval->dialog != NULL)
772 gtk_window_present (GTK_WINDOW (event->approval->dialog));
777 dialog = gtk_message_dialog_new (NULL, 0,
778 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
780 title = g_strdup_printf (_("Invitation to join %s"),
781 tp_channel_get_identifier (channel));
783 gtk_window_set_title (GTK_WINDOW (dialog), title);
786 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
787 _("%s is inviting you to join %s"),
788 empathy_contact_get_alias (event->approval->contact),
789 tp_channel_get_identifier (channel));
791 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
794 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
795 _("_Decline"), GTK_RESPONSE_CANCEL);
796 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
797 gtk_button_set_image (GTK_BUTTON (button), image);
799 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
800 _("_Join"), GTK_RESPONSE_OK);
801 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
802 gtk_button_set_image (GTK_BUTTON (button), image);
804 g_signal_connect (dialog, "response",
805 G_CALLBACK (invite_dialog_response_cb), event->approval);
807 gtk_widget_show (dialog);
809 event->approval->dialog = dialog;
813 display_invite_room_dialog (EventManagerApproval *approval)
815 GtkWidget *window = empathy_roster_window_dup ();
816 const gchar *invite_msg;
818 TpHandle self_handle;
819 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
821 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
822 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
823 NULL, NULL, &invite_msg);
825 if (approval->contact != NULL)
827 msg = g_strdup_printf (_("%s invited you to join %s"),
828 empathy_contact_get_alias (approval->contact),
829 tp_channel_get_identifier (approval->main_channel));
833 msg = g_strdup_printf (_("You have been invited to join %s"),
834 tp_channel_get_identifier (approval->main_channel));
837 event_manager_add (approval->manager, NULL,
838 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
839 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
840 event_room_channel_process_func, NULL);
842 empathy_sound_manager_play (priv->sound_mgr, window,
843 EMPATHY_SOUND_CONVERSATION_NEW);
846 g_object_unref (window);
850 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
851 EmpathyContact *contact,
856 EventManagerApproval *approval = (EventManagerApproval *) user_data;
860 DEBUG ("Error: %s", error->message);
864 approval->contact = g_object_ref (contact);
867 display_invite_room_dialog (approval);
871 event_manager_ft_got_contact_cb (TpConnection *connection,
872 EmpathyContact *contact,
877 EventManagerApproval *approval = (EventManagerApproval *) user_data;
878 GtkWidget *window = empathy_roster_window_dup ();
880 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
882 approval->contact = g_object_ref (contact);
884 header = g_strdup_printf (_("Incoming file transfer from %s"),
885 empathy_contact_get_alias (approval->contact));
887 event_manager_add (approval->manager, NULL,
888 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
889 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
890 approval, event_channel_process_func, NULL);
892 /* FIXME better sound for incoming file transfers ?*/
893 empathy_sound_manager_play (priv->sound_mgr, window,
894 EMPATHY_SOUND_CONVERSATION_NEW);
897 g_object_unref (window);
901 event_manager_auth_process_func (EventPriv *event)
903 empathy_event_approve ((EmpathyEvent *) event);
906 /* If there is a file-transfer, media, or auth channel consider it as
909 find_main_channel (GList *channels)
912 TpChannel *text = NULL;
914 for (l = channels; l != NULL; l = g_list_next (l))
916 TpChannel *channel = l->data;
919 if (tp_proxy_get_invalidated (channel) != NULL)
922 channel_type = tp_channel_get_channel_type_id (channel);
924 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
925 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL ||
926 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
927 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
930 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
938 approve_channels (TpSimpleApprover *approver,
940 TpConnection *connection,
942 TpChannelDispatchOperation *dispatch_operation,
943 TpAddDispatchOperationContext *context,
946 EmpathyEventManager *self = user_data;
947 EmpathyEventManagerPriv *priv = GET_PRIV (self);
949 EventManagerApproval *approval;
952 channel = find_main_channel (channels);
955 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
956 "Unknown channel type" };
958 DEBUG ("Failed to find the main channel; ignoring");
960 tp_add_dispatch_operation_context_fail (context, &error);
964 approval = event_manager_approval_new (self, dispatch_operation, channel);
965 priv->approvals = g_slist_prepend (priv->approvals, approval);
967 approval->invalidated_handler = g_signal_connect (dispatch_operation,
968 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
970 channel_type = tp_channel_get_channel_type_id (channel);
972 if (TP_IS_TEXT_CHANNEL (channel))
974 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (channel);
977 approval->handler_instance = g_object_ref (tp_chat);
979 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
981 /* Are we in local-pending ? */
984 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
986 /* We are invited to a room */
987 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
988 tp_channel_get_identifier (channel));
992 empathy_tp_contact_factory_get_from_handle (connection,
993 inviter, event_manager_muc_invite_got_contact_cb,
994 approval, NULL, G_OBJECT (self));
998 display_invite_room_dialog (approval);
1004 /* We are not invited, approve the channel right now */
1005 tp_add_dispatch_operation_context_accept (context);
1007 approval->auto_approved = TRUE;
1008 event_manager_approval_approve (approval);
1012 /* 1-1 text channel, wait for the first message */
1013 approval->handler = g_signal_connect (tp_chat, "message-received-empathy",
1014 G_CALLBACK (event_manager_chat_message_received_cb), approval);
1016 messages = (GList *) empathy_tp_chat_get_pending_messages (tp_chat);
1017 for (l = messages; l != NULL; l = g_list_next (l))
1019 EmpathyMessage *msg = l->data;
1021 event_manager_chat_message_received_cb (tp_chat, msg, approval);
1024 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
1026 TpCallChannel *call = TP_CALL_CHANNEL (channel);
1029 approval->handler_instance = g_object_ref (call);
1031 id = tp_channel_get_identifier (channel);
1033 empathy_tp_contact_factory_get_from_id (connection, id,
1034 event_manager_call_channel_got_contact_cb,
1035 approval, NULL, G_OBJECT (self));
1037 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1041 approval->handler_instance = g_object_ref (channel);
1043 handle = tp_channel_get_handle (channel, NULL);
1045 empathy_tp_contact_factory_get_from_handle (connection, handle,
1046 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1048 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1051 const gchar * const *available_mechanisms;
1053 props = tp_channel_borrow_immutable_properties (channel);
1054 available_mechanisms = tp_asv_get_boxed (props,
1055 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1058 if (tp_strv_contains (available_mechanisms, "X-TELEPATHY-PASSWORD"))
1060 event_manager_add (approval->manager, account, NULL,
1061 EMPATHY_EVENT_TYPE_AUTH,
1062 GTK_STOCK_DIALOG_AUTHENTICATION,
1063 tp_account_get_display_name (account),
1064 _("Password required"), approval,
1065 event_manager_auth_process_func, NULL);
1069 GError error = { TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
1070 "Support only X-TELEPATHY-PASSWORD auth method" };
1072 tp_add_dispatch_operation_context_fail (context, &error);
1078 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1079 "Invalid channel type" };
1081 DEBUG ("Unknown channel type (%s), ignoring..",
1082 g_quark_to_string (channel_type));
1084 tp_add_dispatch_operation_context_fail (context, &error);
1089 tp_add_dispatch_operation_context_accept (context);
1093 event_pending_subscribe_func (EventPriv *event)
1095 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1097 event_remove (event);
1101 check_publish_state (EmpathyEventManager *self,
1102 TpContact *tp_contact)
1104 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1105 gchar *header, *event_msg;
1106 TpSubscriptionState state;
1107 EmpathyContact *contact;
1108 const gchar *message;
1110 state = tp_contact_get_publish_state (tp_contact);
1112 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1114 if (state != TP_SUBSCRIPTION_STATE_ASK)
1118 for (l = priv->events; l; l = l->next)
1120 EventPriv *event = l->data;
1122 if (event->public.contact == contact &&
1123 event->func == event_pending_subscribe_func)
1125 event_remove (event);
1133 header = g_strdup_printf (
1134 _("%s would like permission to see when you are online"),
1135 empathy_contact_get_alias (contact));
1137 message = tp_contact_get_publish_request (tp_contact);
1139 if (!EMP_STR_EMPTY (message))
1140 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1144 event_manager_add (self, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1145 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1146 event_pending_subscribe_func, NULL);
1152 g_object_unref (contact);
1156 event_manager_publish_state_changed_cb (TpContact *contact,
1158 EmpathyEventManager *self)
1160 check_publish_state (self, contact);
1164 event_manager_presence_changed_cb (EmpathyContact *contact,
1165 TpConnectionPresenceType current,
1166 TpConnectionPresenceType previous,
1167 EmpathyEventManager *manager)
1169 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1171 EmpathyPresenceManager *presence_mgr;
1172 GtkWidget *window = empathy_roster_window_dup ();
1174 account = empathy_contact_get_account (contact);
1175 presence_mgr = empathy_presence_manager_dup_singleton ();
1177 if (empathy_presence_manager_account_is_just_connected (presence_mgr, account))
1180 if (tp_connection_presence_type_cmp_availability (previous,
1181 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1183 /* contact was online */
1184 if (tp_connection_presence_type_cmp_availability (current,
1185 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1187 /* someone is logging off */
1188 empathy_sound_manager_play (priv->sound_mgr, window,
1189 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1191 if (g_settings_get_boolean (priv->gsettings_notif,
1192 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1194 event_manager_add (manager, NULL, contact,
1195 EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE,
1196 EMPATHY_IMAGE_AVATAR_DEFAULT,
1197 empathy_contact_get_alias (contact), _("Disconnected"),
1204 /* contact was offline */
1205 if (tp_connection_presence_type_cmp_availability (current,
1206 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1208 /* someone is logging in */
1209 empathy_sound_manager_play (priv->sound_mgr, window,
1210 EMPATHY_SOUND_CONTACT_CONNECTED);
1212 if (g_settings_get_boolean (priv->gsettings_notif,
1213 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1215 event_manager_add (manager, NULL, contact,
1216 EMPATHY_EVENT_TYPE_PRESENCE_ONLINE,
1217 EMPATHY_IMAGE_AVATAR_DEFAULT,
1218 empathy_contact_get_alias (contact), _("Connected"),
1225 g_object_unref (presence_mgr);
1226 g_object_unref (window);
1230 event_manager_constructor (GType type,
1232 GObjectConstructParam *props)
1236 if (manager_singleton) {
1237 retval = g_object_ref (manager_singleton);
1239 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1240 (type, n_props, props);
1242 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1243 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1250 event_manager_finalize (GObject *object)
1252 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1254 if (priv->ringing > 0)
1255 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1257 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1258 g_slist_free (priv->events);
1259 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1260 g_slist_free (priv->approvals);
1261 g_object_unref (priv->conn_aggregator);
1262 g_object_unref (priv->approver);
1263 g_object_unref (priv->auth_approver);
1264 g_object_unref (priv->gsettings_notif);
1265 g_object_unref (priv->gsettings_ui);
1266 g_object_unref (priv->sound_mgr);
1267 g_hash_table_unref (priv->contacts);
1271 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1273 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1275 object_class->finalize = event_manager_finalize;
1276 object_class->constructor = event_manager_constructor;
1278 signals[EVENT_ADDED] =
1279 g_signal_new ("event-added",
1280 G_TYPE_FROM_CLASS (klass),
1284 g_cclosure_marshal_generic,
1288 signals[EVENT_REMOVED] =
1289 g_signal_new ("event-removed",
1290 G_TYPE_FROM_CLASS (klass),
1294 g_cclosure_marshal_generic,
1295 G_TYPE_NONE, 1, G_TYPE_POINTER);
1297 signals[EVENT_UPDATED] =
1298 g_signal_new ("event-updated",
1299 G_TYPE_FROM_CLASS (klass),
1303 g_cclosure_marshal_generic,
1304 G_TYPE_NONE, 1, G_TYPE_POINTER);
1306 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1310 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
1313 EmpathyEventManager *self)
1315 EmpathyEventManagerPriv *priv = GET_PRIV (self);
1318 for (i = 0; i < added->len; i++)
1320 TpContact *tp_contact = g_ptr_array_index (added, i);
1321 EmpathyContact *contact;
1323 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
1326 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1328 tp_g_signal_connect_object (contact, "presence-changed",
1329 G_CALLBACK (event_manager_presence_changed_cb), self, 0);
1331 tp_g_signal_connect_object (tp_contact, "notify::publish-state",
1332 G_CALLBACK (event_manager_publish_state_changed_cb), self, 0);
1334 check_publish_state (self, tp_contact);
1336 /* Pass ownership to the hash table */
1337 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact), contact);
1340 for (i = 0; i < removed->len; i++)
1342 TpContact *tp_contact = g_ptr_array_index (removed, i);
1343 EmpathyContact *contact;
1345 contact = g_hash_table_lookup (priv->contacts, tp_contact);
1346 if (contact == NULL)
1349 g_signal_handlers_disconnect_by_func (contact,
1350 event_manager_presence_changed_cb, self);
1352 g_signal_handlers_disconnect_by_func (tp_contact,
1353 event_manager_publish_state_changed_cb, self);
1355 g_hash_table_remove (priv->contacts, tp_contact);
1360 empathy_event_manager_init (EmpathyEventManager *manager)
1362 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1363 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1364 GError *error = NULL;
1365 TpAccountManager *am;
1366 GPtrArray *contacts, *empty;
1368 manager->priv = priv;
1370 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1371 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1373 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1375 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
1378 priv->conn_aggregator = empathy_connection_aggregator_dup_singleton ();
1380 tp_g_signal_connect_object (priv->conn_aggregator, "contact-list-changed",
1381 G_CALLBACK (contact_list_changed_cb), manager, 0);
1383 contacts = empathy_connection_aggregator_dup_all_contacts (
1384 priv->conn_aggregator);
1386 empty = g_ptr_array_new ();
1388 contact_list_changed_cb (priv->conn_aggregator, contacts, empty, manager);
1390 g_ptr_array_unref (contacts);
1391 g_ptr_array_unref (empty);
1393 am = tp_account_manager_dup ();
1395 priv->approver = tp_simple_approver_new_with_am (am, "Empathy.EventManager",
1396 FALSE, approve_channels, manager, NULL);
1398 /* Private text channels */
1399 tp_base_client_take_approver_filter (priv->approver,
1401 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1402 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1405 /* Muc text channels */
1406 tp_base_client_take_approver_filter (priv->approver,
1408 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1409 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1413 tp_base_client_take_approver_filter (priv->approver,
1415 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1416 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1417 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1421 tp_base_client_take_approver_filter (priv->approver,
1423 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1424 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1425 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1427 tp_base_client_take_approver_filter (priv->approver,
1429 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1430 TP_IFACE_CHANNEL_TYPE_CALL,
1431 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1434 /* I don't feel good about doing this, and I'm sorry, but the
1435 * capabilities connection feature is added earlier because it's
1436 * needed for EmpathyTpChat. If the capabilities feature is required
1437 * then preparing an auth channel (which of course appears in the
1438 * CONNECTING state) will never be prepared. So the options are
1439 * either to create another approver like I've done, or to port
1440 * EmpathyTpChat and its users to not depend on the connection being
1441 * prepared with capabilities. I chose the former, obviously. :-) */
1443 priv->auth_approver = tp_simple_approver_new_with_am (am,
1444 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1447 /* SASL auth channels */
1448 tp_base_client_take_approver_filter (priv->auth_approver,
1450 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1451 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1452 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1454 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1457 if (!tp_base_client_register (priv->approver, &error))
1459 DEBUG ("Failed to register Approver: %s", error->message);
1460 g_error_free (error);
1463 if (!tp_base_client_register (priv->auth_approver, &error))
1465 DEBUG ("Failed to register auth Approver: %s", error->message);
1466 g_error_free (error);
1469 g_object_unref (am);
1472 EmpathyEventManager *
1473 empathy_event_manager_dup_singleton (void)
1475 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1479 empathy_event_manager_get_events (EmpathyEventManager *manager)
1481 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1483 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1485 return priv->events;
1489 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1491 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1493 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1495 return priv->events ? priv->events->data : NULL;
1499 empathy_event_activate (EmpathyEvent *event_public)
1501 EventPriv *event = (EventPriv *) event_public;
1503 g_return_if_fail (event_public != NULL);
1506 event->func (event);
1508 event_remove (event);
1512 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1514 EventPriv *event = (EventPriv *) event_public;
1516 g_return_if_fail (event_public != NULL);
1518 event->inhibit = TRUE;
1522 empathy_event_approve (EmpathyEvent *event_public)
1524 EventPriv *event = (EventPriv *) event_public;
1526 g_return_if_fail (event_public != NULL);
1528 event_manager_approval_approve (event->approval);
1532 empathy_event_decline (EmpathyEvent *event_public)
1534 EventPriv *event = (EventPriv *) event_public;
1536 g_return_if_fail (event_public != NULL);
1538 reject_approval (event->approval);