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-idle.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-call.h>
37 #include <libempathy/empathy-tp-file.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-call-factory.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>
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 if (event->public.contact)
171 g_object_unref (event->public.contact);
174 if (event->public.account)
176 g_object_unref (event->public.account);
179 g_slice_free (EventPriv, event);
183 event_remove (EventPriv *event)
185 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
187 DEBUG ("Removing event %p", event);
189 priv->events = g_slist_remove (priv->events, event);
190 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
195 empathy_event_remove (EmpathyEvent *event_public)
197 EventPriv *event = (EventPriv *) event_public;
199 event_remove (event);
203 autoremove_event_timeout_cb (EventPriv *event)
205 event->autoremove_timeout_id = 0;
206 event_remove (event);
211 display_notify_area (EmpathyEventManager *self)
213 EmpathyEventManagerPriv *priv = GET_PRIV (self);
215 return g_settings_get_boolean (priv->gsettings_ui,
216 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
220 event_manager_add (EmpathyEventManager *manager,
222 EmpathyContact *contact,
223 EmpathyEventType type,
224 const gchar *icon_name,
226 const gchar *message,
227 EventManagerApproval *approval,
231 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
234 event = g_slice_new0 (EventPriv);
235 event->public.account = account != NULL ? g_object_ref (account) : NULL;
236 event->public.contact = contact ? g_object_ref (contact) : NULL;
237 event->public.type = type;
238 event->public.icon_name = g_strdup (icon_name);
239 event->public.header = g_strdup (header);
240 event->public.message = g_strdup (message);
241 event->public.must_ack = (func != NULL);
242 event->inhibit = FALSE;
244 event->user_data = user_data;
245 event->manager = manager;
246 event->approval = approval;
248 DEBUG ("Adding event %p", event);
249 priv->events = g_slist_prepend (priv->events, event);
251 if (!display_notify_area (manager))
253 /* Don't fire the 'event-added' signal as we activate the event now */
254 if (approval != NULL)
255 approval->auto_approved = TRUE;
257 empathy_event_activate (&event->public);
261 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
263 if (!event->public.must_ack)
265 event->autoremove_timeout_id = g_timeout_add_seconds (
266 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
272 handle_with_cb (GObject *source,
273 GAsyncResult *result,
276 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
277 GError *error = NULL;
279 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
281 DEBUG ("HandleWith failed: %s\n", error->message);
282 g_error_free (error);
287 handle_with_time_cb (GObject *source,
288 GAsyncResult *result,
291 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
292 GError *error = NULL;
294 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
297 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
299 EventManagerApproval *approval = user_data;
301 DEBUG ("HandleWithTime() is not implemented, falling back to "
302 "HandleWith(). Please upgrade to telepathy-mission-control "
305 tp_channel_dispatch_operation_handle_with_async (approval->operation,
306 NULL, handle_with_cb, approval);
310 DEBUG ("HandleWithTime failed: %s\n", error->message);
312 g_error_free (error);
317 event_manager_approval_approve (EventManagerApproval *approval)
321 if (approval->auto_approved)
323 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
327 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
330 g_assert (approval->operation != NULL);
332 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
333 NULL, timestamp, handle_with_time_cb, approval);
337 event_channel_process_func (EventPriv *event)
339 event_manager_approval_approve (event->approval);
343 event_text_channel_process_func (EventPriv *event)
345 EmpathyTpChat *tp_chat;
348 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
350 if (event->approval->handler != 0)
352 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
354 g_signal_handler_disconnect (tp_chat, event->approval->handler);
355 event->approval->handler = 0;
358 event_manager_approval_approve (event->approval);
362 event_lookup_by_approval (EmpathyEventManager *manager,
363 EventManagerApproval *approval)
365 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
367 EventPriv *retval = NULL;
369 for (l = priv->events; l; l = l->next)
371 EventPriv *event = l->data;
373 if (event->approval == approval)
384 event_update (EmpathyEventManager *manager, EventPriv *event,
385 const char *icon_name, const char *header, const char *msg)
387 g_free (event->public.icon_name);
388 g_free (event->public.header);
389 g_free (event->public.message);
391 event->public.icon_name = g_strdup (icon_name);
392 event->public.header = g_strdup (header);
393 event->public.message = g_strdup (msg);
395 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
399 reject_channel_claim_cb (GObject *source,
400 GAsyncResult *result,
403 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
404 GError *error = NULL;
406 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
408 DEBUG ("Failed to claim channel: %s", error->message);
410 g_error_free (error);
414 if (EMPATHY_IS_TP_CALL (user_data))
416 empathy_tp_call_close (user_data);
418 else if (EMPATHY_IS_TP_CHAT (user_data))
420 empathy_tp_chat_leave (user_data);
422 else if (EMPATHY_IS_TP_FILE (user_data))
424 empathy_tp_file_close (user_data);
428 g_object_unref (user_data);
432 reject_auth_channel_claim_cb (GObject *source,
433 GAsyncResult *result,
436 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
437 GError *error = NULL;
439 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
441 DEBUG ("Failed to claim channel: %s", error->message);
443 g_error_free (error);
447 tp_cli_channel_call_close (TP_CHANNEL (user_data), -1,
448 NULL, NULL, NULL, NULL);
452 reject_approval (EventManagerApproval *approval)
454 /* We have to claim the channel before closing it */
456 /* Unfortunately, we need to special case the auth channels for the
457 * time being as they don't have a wrapper object handler in
458 * approval->handler_instance as they're not actually handled by
459 * this process, so we can just use a noddy callback to call Close()
461 if (approval->handler_instance != NULL)
463 tp_channel_dispatch_operation_claim_async (approval->operation,
464 reject_channel_claim_cb, g_object_ref (approval->handler_instance));
466 else if (tp_channel_get_channel_type_id (approval->main_channel)
467 == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
469 tp_channel_dispatch_operation_claim_async (approval->operation,
470 reject_auth_channel_claim_cb, approval->main_channel);
475 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
476 gint response, gpointer user_data)
478 EventManagerApproval *approval = user_data;
480 gtk_widget_destroy (approval->dialog);
481 approval->dialog = NULL;
483 if (response != GTK_RESPONSE_ACCEPT)
485 reject_approval (approval);
489 event_manager_approval_approve (approval);
494 event_channel_process_voip_func (EventPriv *event)
503 if (event->approval->dialog != NULL)
505 gtk_window_present (GTK_WINDOW (event->approval->dialog));
509 call = EMPATHY_TP_CALL (event->approval->handler_instance);
511 video = empathy_tp_call_has_initial_video (call);
513 dialog = gtk_message_dialog_new (NULL, 0,
514 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
515 video ? _("Incoming video call"): _("Incoming call"));
517 gtk_message_dialog_format_secondary_text (
518 GTK_MESSAGE_DIALOG (dialog), video ?
519 _("%s is video calling you. Do you want to answer?"):
520 _("%s is calling you. Do you want to answer?"),
521 empathy_contact_get_alias (event->approval->contact));
523 title = g_strdup_printf (_("Incoming call from %s"),
524 empathy_contact_get_alias (event->approval->contact));
526 gtk_window_set_title (GTK_WINDOW (dialog), title);
529 /* Set image of the dialog */
532 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
533 GTK_ICON_SIZE_DIALOG);
537 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
538 GTK_ICON_SIZE_DIALOG);
541 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
542 gtk_widget_show (image);
544 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
547 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
548 _("_Reject"), GTK_RESPONSE_REJECT);
549 image = gtk_image_new_from_icon_name ("call-stop",
550 GTK_ICON_SIZE_BUTTON);
551 gtk_button_set_image (GTK_BUTTON (button), image);
553 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
554 _("_Answer"), GTK_RESPONSE_ACCEPT);
556 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
557 gtk_button_set_image (GTK_BUTTON (button), image);
559 g_signal_connect (dialog, "response",
560 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
563 gtk_widget_show (dialog);
565 event->approval->dialog = dialog;
569 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
570 EmpathyMessage *message,
571 EventManagerApproval *approval)
573 GtkWidget *window = empathy_main_window_dup ();
574 EmpathyContact *sender;
579 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
581 /* try to update the event if it's referring to a chat which is already in the
583 event = event_lookup_by_approval (approval->manager, approval);
585 sender = empathy_message_get_sender (message);
586 header = empathy_contact_get_alias (sender);
587 msg = empathy_message_get_body (message);
589 channel = empathy_tp_chat_get_channel (tp_chat);
592 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
595 event_manager_add (approval->manager, NULL, sender,
596 EMPATHY_EVENT_TYPE_CHAT, EMPATHY_IMAGE_NEW_MESSAGE, header, msg,
597 approval, event_text_channel_process_func, NULL);
599 empathy_sound_manager_play (priv->sound_mgr, window,
600 EMPATHY_SOUND_CONVERSATION_NEW);
602 g_object_unref (window);
606 event_manager_approval_done (EventManagerApproval *approval)
608 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
611 if (approval->operation != NULL)
615 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
617 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
620 if (priv->ringing == 0)
621 empathy_sound_manager_stop (priv->sound_mgr,
622 EMPATHY_SOUND_PHONE_INCOMING);
626 priv->approvals = g_slist_remove (priv->approvals, approval);
628 for (l = priv->events; l; l = l->next)
630 EventPriv *event = l->data;
632 if (event->approval == approval)
634 event_remove (event);
639 event_manager_approval_free (approval);
643 cdo_invalidated_cb (TpProxy *cdo,
647 EventManagerApproval *approval)
649 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
651 event_manager_approval_done (approval);
655 event_manager_media_channel_got_contact (EventManagerApproval *approval)
657 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
658 GtkWidget *window = empathy_main_window_dup ();
663 call = EMPATHY_TP_CALL (approval->handler_instance);
665 video = empathy_tp_call_has_initial_video (call);
667 header = g_strdup_printf (
668 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
669 empathy_contact_get_alias (approval->contact));
671 event_manager_add (approval->manager, NULL,
672 approval->contact, EMPATHY_EVENT_TYPE_VOIP,
673 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
674 header, NULL, approval,
675 event_channel_process_voip_func, NULL);
680 if (priv->ringing == 1)
681 empathy_sound_manager_start_playing (priv->sound_mgr, window,
682 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
684 g_object_unref (window);
688 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
689 GParamSpec *param, EventManagerApproval *approval)
691 EmpathyContact *contact;
693 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
698 approval->contact = contact;
699 event_manager_media_channel_got_contact (approval);
703 invite_dialog_response_cb (GtkDialog *dialog,
705 EventManagerApproval *approval)
707 EmpathyTpChat *tp_chat;
709 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
710 approval->dialog = NULL;
712 tp_chat = EMPATHY_TP_CHAT (approval->handler_instance);
714 if (response != GTK_RESPONSE_OK)
717 DEBUG ("Muc invitation rejected");
719 reject_approval (approval);
724 DEBUG ("Muc invitation accepted");
726 /* We'll join the room when handling the channel */
727 event_manager_approval_approve (approval);
731 event_room_channel_process_func (EventPriv *event)
733 GtkWidget *dialog, *button, *image;
734 TpChannel *channel = event->approval->main_channel;
737 if (event->approval->dialog != NULL)
739 gtk_window_present (GTK_WINDOW (event->approval->dialog));
744 dialog = gtk_message_dialog_new (NULL, 0,
745 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
747 title = g_strdup_printf (_("Invitation to join %s"),
748 tp_channel_get_identifier (channel));
750 gtk_window_set_title (GTK_WINDOW (dialog), title);
753 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
754 _("%s is inviting you to join %s"),
755 empathy_contact_get_alias (event->approval->contact),
756 tp_channel_get_identifier (channel));
758 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
761 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
762 _("_Decline"), GTK_RESPONSE_CANCEL);
763 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
764 gtk_button_set_image (GTK_BUTTON (button), image);
766 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
767 _("_Join"), GTK_RESPONSE_OK);
768 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
769 gtk_button_set_image (GTK_BUTTON (button), image);
771 g_signal_connect (dialog, "response",
772 G_CALLBACK (invite_dialog_response_cb), event->approval);
774 gtk_widget_show (dialog);
776 event->approval->dialog = dialog;
780 display_invite_room_dialog (EventManagerApproval *approval)
782 GtkWidget *window = empathy_main_window_dup ();
783 const gchar *invite_msg;
785 TpHandle self_handle;
786 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
788 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
789 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
790 NULL, NULL, &invite_msg);
792 if (approval->contact != NULL)
794 msg = g_strdup_printf (_("%s invited you to join %s"),
795 empathy_contact_get_alias (approval->contact),
796 tp_channel_get_identifier (approval->main_channel));
800 msg = g_strdup_printf (_("You have been invited to join %s"),
801 tp_channel_get_identifier (approval->main_channel));
804 event_manager_add (approval->manager, NULL,
805 approval->contact, EMPATHY_EVENT_TYPE_INVITATION,
806 EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg, approval,
807 event_room_channel_process_func, NULL);
809 empathy_sound_manager_play (priv->sound_mgr, window,
810 EMPATHY_SOUND_CONVERSATION_NEW);
813 g_object_unref (window);
817 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
818 EmpathyContact *contact,
823 EventManagerApproval *approval = (EventManagerApproval *) user_data;
827 DEBUG ("Error: %s", error->message);
831 approval->contact = g_object_ref (contact);
834 display_invite_room_dialog (approval);
838 event_manager_ft_got_contact_cb (TpConnection *connection,
839 EmpathyContact *contact,
844 EventManagerApproval *approval = (EventManagerApproval *) user_data;
845 GtkWidget *window = empathy_main_window_dup ();
847 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
849 approval->contact = g_object_ref (contact);
851 header = g_strdup_printf (_("Incoming file transfer from %s"),
852 empathy_contact_get_alias (approval->contact));
854 event_manager_add (approval->manager, NULL,
855 approval->contact, EMPATHY_EVENT_TYPE_TRANSFER,
856 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
857 approval, event_channel_process_func, NULL);
859 /* FIXME better sound for incoming file transfers ?*/
860 empathy_sound_manager_play (priv->sound_mgr, window,
861 EMPATHY_SOUND_CONVERSATION_NEW);
864 g_object_unref (window);
868 event_manager_auth_process_func (EventPriv *event)
870 empathy_event_approve ((EmpathyEvent *) event);
873 /* If there is a file-transfer, media, or auth channel consider it as
876 find_main_channel (GList *channels)
879 TpChannel *text = NULL;
881 for (l = channels; l != NULL; l = g_list_next (l))
883 TpChannel *channel = l->data;
886 if (tp_proxy_get_invalidated (channel) != NULL)
889 channel_type = tp_channel_get_channel_type_id (channel);
891 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
892 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER ||
893 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
896 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
904 approve_channels (TpSimpleApprover *approver,
906 TpConnection *connection,
908 TpChannelDispatchOperation *dispatch_operation,
909 TpAddDispatchOperationContext *context,
912 EmpathyEventManager *self = user_data;
913 EmpathyEventManagerPriv *priv = GET_PRIV (self);
915 EventManagerApproval *approval;
918 channel = find_main_channel (channels);
921 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
922 "Unknown channel type" };
924 DEBUG ("Failed to find the main channel; ignoring");
926 tp_add_dispatch_operation_context_fail (context, &error);
930 approval = event_manager_approval_new (self, dispatch_operation, channel);
931 priv->approvals = g_slist_prepend (priv->approvals, approval);
933 approval->invalidated_handler = g_signal_connect (dispatch_operation,
934 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
936 channel_type = tp_channel_get_channel_type_id (channel);
938 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
940 EmpathyTpChat *tp_chat;
942 tp_chat = empathy_tp_chat_new (account, channel);
943 approval->handler_instance = G_OBJECT (tp_chat);
945 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
947 /* Are we in local-pending ? */
950 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
952 /* We are invited to a room */
953 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
954 tp_channel_get_identifier (channel));
958 empathy_tp_contact_factory_get_from_handle (connection,
959 inviter, event_manager_muc_invite_got_contact_cb,
960 approval, NULL, G_OBJECT (self));
964 display_invite_room_dialog (approval);
970 /* We are not invited, approve the channel right now */
971 tp_add_dispatch_operation_context_accept (context);
973 approval->auto_approved = TRUE;
974 event_manager_approval_approve (approval);
978 /* 1-1 text channel, wait for the first message */
979 approval->handler = g_signal_connect (tp_chat, "message-received",
980 G_CALLBACK (event_manager_chat_message_received_cb), approval);
982 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
984 EmpathyContact *contact;
985 EmpathyTpCall *call = empathy_tp_call_new (account, channel);
987 approval->handler_instance = G_OBJECT (call);
989 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
993 g_signal_connect (call, "notify::contact",
994 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
999 approval->contact = contact;
1000 event_manager_media_channel_got_contact (approval);
1004 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
1007 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
1009 approval->handler_instance = G_OBJECT (tp_file);
1011 handle = tp_channel_get_handle (channel, NULL);
1013 connection = tp_channel_borrow_connection (channel);
1014 empathy_tp_contact_factory_get_from_handle (connection, handle,
1015 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
1017 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_SERVER_AUTHENTICATION)
1019 event_manager_add (approval->manager, account, NULL, EMPATHY_EVENT_TYPE_AUTH,
1020 GTK_STOCK_DIALOG_AUTHENTICATION, tp_account_get_display_name (account),
1021 _("Password required"), approval,
1022 event_manager_auth_process_func, NULL);
1026 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1027 "Invalid channel type" };
1029 DEBUG ("Unknown channel type (%s), ignoring..",
1030 g_quark_to_string (channel_type));
1032 tp_add_dispatch_operation_context_fail (context, &error);
1037 tp_add_dispatch_operation_context_accept (context);
1041 event_pending_subscribe_func (EventPriv *event)
1043 empathy_subscription_dialog_show (event->public.contact, event->public.header,
1045 event_remove (event);
1049 event_manager_pendings_changed_cb (EmpathyContactList *list,
1050 EmpathyContact *contact, EmpathyContact *actor,
1051 guint reason, gchar *message, gboolean is_pending,
1052 EmpathyEventManager *manager)
1054 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1055 gchar *header, *event_msg;
1061 for (l = priv->events; l; l = l->next)
1063 EventPriv *event = l->data;
1065 if (event->public.contact == contact &&
1066 event->func == event_pending_subscribe_func)
1068 event_remove (event);
1076 header = g_strdup_printf (
1077 _("%s would like permission to see when you are available"),
1078 empathy_contact_get_alias (contact));
1080 if (!EMP_STR_EMPTY (message))
1081 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1085 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1086 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1087 event_pending_subscribe_func, NULL);
1094 event_manager_presence_changed_cb (EmpathyContact *contact,
1095 TpConnectionPresenceType current,
1096 TpConnectionPresenceType previous,
1097 EmpathyEventManager *manager)
1099 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1101 gchar *header = NULL;
1103 GtkWidget *window = empathy_main_window_dup ();
1105 account = empathy_contact_get_account (contact);
1106 idle = empathy_idle_dup_singleton ();
1108 if (empathy_idle_account_is_just_connected (idle, account))
1111 if (tp_connection_presence_type_cmp_availability (previous,
1112 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1114 /* contact was online */
1115 if (tp_connection_presence_type_cmp_availability (current,
1116 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1118 /* someone is logging off */
1119 empathy_sound_manager_play (priv->sound_mgr, window,
1120 EMPATHY_SOUND_CONTACT_DISCONNECTED);
1122 if (g_settings_get_boolean (priv->gsettings_notif,
1123 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1125 header = g_strdup_printf (_("%s is now offline."),
1126 empathy_contact_get_alias (contact));
1128 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1129 EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1135 /* contact was offline */
1136 if (tp_connection_presence_type_cmp_availability (current,
1137 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1139 /* someone is logging in */
1140 empathy_sound_manager_play (priv->sound_mgr, window,
1141 EMPATHY_SOUND_CONTACT_CONNECTED);
1143 if (g_settings_get_boolean (priv->gsettings_notif,
1144 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1146 header = g_strdup_printf (_("%s is now online."),
1147 empathy_contact_get_alias (contact));
1149 event_manager_add (manager, NULL, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1150 EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1157 g_object_unref (idle);
1158 g_object_unref (window);
1162 event_manager_members_changed_cb (EmpathyContactList *list,
1163 EmpathyContact *contact,
1164 EmpathyContact *actor,
1168 EmpathyEventManager *manager)
1171 g_signal_connect (contact, "presence-changed",
1172 G_CALLBACK (event_manager_presence_changed_cb), manager);
1174 g_signal_handlers_disconnect_by_func (contact,
1175 event_manager_presence_changed_cb, manager);
1179 event_manager_constructor (GType type,
1181 GObjectConstructParam *props)
1185 if (manager_singleton) {
1186 retval = g_object_ref (manager_singleton);
1188 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1189 (type, n_props, props);
1191 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1192 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1199 event_manager_finalize (GObject *object)
1201 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1203 if (priv->ringing > 0)
1204 empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_INCOMING);
1206 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1207 g_slist_free (priv->events);
1208 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1209 g_slist_free (priv->approvals);
1210 g_object_unref (priv->contact_manager);
1211 g_object_unref (priv->approver);
1212 g_object_unref (priv->auth_approver);
1213 g_object_unref (priv->gsettings_notif);
1214 g_object_unref (priv->gsettings_ui);
1215 g_object_unref (priv->sound_mgr);
1219 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1223 object_class->finalize = event_manager_finalize;
1224 object_class->constructor = event_manager_constructor;
1226 signals[EVENT_ADDED] =
1227 g_signal_new ("event-added",
1228 G_TYPE_FROM_CLASS (klass),
1232 g_cclosure_marshal_VOID__POINTER,
1236 signals[EVENT_REMOVED] =
1237 g_signal_new ("event-removed",
1238 G_TYPE_FROM_CLASS (klass),
1242 g_cclosure_marshal_VOID__POINTER,
1243 G_TYPE_NONE, 1, G_TYPE_POINTER);
1245 signals[EVENT_UPDATED] =
1246 g_signal_new ("event-updated",
1247 G_TYPE_FROM_CLASS (klass),
1251 g_cclosure_marshal_VOID__POINTER,
1252 G_TYPE_NONE, 1, G_TYPE_POINTER);
1254 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1258 empathy_event_manager_init (EmpathyEventManager *manager)
1260 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1261 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1263 GError *error = NULL;
1265 manager->priv = priv;
1267 priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1268 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1270 priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1272 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1273 g_signal_connect (priv->contact_manager, "pendings-changed",
1274 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1276 g_signal_connect (priv->contact_manager, "members-changed",
1277 G_CALLBACK (event_manager_members_changed_cb), manager);
1279 dbus = tp_dbus_daemon_dup (&error);
1282 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1283 g_error_free (error);
1287 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1288 approve_channels, manager, NULL);
1290 /* EmpathyTpChat relies on this feature being prepared */
1291 tp_base_client_add_connection_features_varargs (priv->approver,
1292 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1294 /* Private text channels */
1295 tp_base_client_take_approver_filter (priv->approver,
1297 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1298 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1301 /* Muc text channels */
1302 tp_base_client_take_approver_filter (priv->approver,
1304 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1305 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1309 tp_base_client_take_approver_filter (priv->approver,
1311 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1312 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1313 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1317 tp_base_client_take_approver_filter (priv->approver,
1319 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1320 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1321 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1324 /* I don't feel good about doing this, and I'm sorry, but the
1325 * capabilities connection feature is added earlier because it's
1326 * needed for EmpathyTpChat. If the capabilities feature is required
1327 * then preparing an auth channel (which of course appears in the
1328 * CONNECTING state) will never be prepared. So the options are
1329 * either to create another approver like I've done, or to port
1330 * EmpathyTpChat and its users to not depend on the connection being
1331 * prepared with capabilities. I chose the former, obviously. :-) */
1333 priv->auth_approver = tp_simple_approver_new (dbus,
1334 "Empathy.AuthEventManager", FALSE, approve_channels, manager,
1337 /* SASL auth channels */
1338 tp_base_client_take_approver_filter (priv->auth_approver,
1340 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1341 TP_IFACE_CHANNEL_TYPE_SERVER_AUTHENTICATION,
1342 TP_PROP_CHANNEL_TYPE_SERVER_AUTHENTICATION_AUTHENTICATION_METHOD,
1344 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
1347 if (!tp_base_client_register (priv->approver, &error))
1349 DEBUG ("Failed to register Approver: %s", error->message);
1350 g_error_free (error);
1353 if (!tp_base_client_register (priv->auth_approver, &error))
1355 DEBUG ("Failed to register auth Approver: %s", error->message);
1356 g_error_free (error);
1359 g_object_unref (dbus);
1362 EmpathyEventManager *
1363 empathy_event_manager_dup_singleton (void)
1365 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1369 empathy_event_manager_get_events (EmpathyEventManager *manager)
1371 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1373 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1375 return priv->events;
1379 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1381 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1383 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1385 return priv->events ? priv->events->data : NULL;
1389 empathy_event_activate (EmpathyEvent *event_public)
1391 EventPriv *event = (EventPriv *) event_public;
1393 g_return_if_fail (event_public != NULL);
1396 event->func (event);
1398 event_remove (event);
1402 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1404 EventPriv *event = (EventPriv *) event_public;
1406 g_return_if_fail (event_public != NULL);
1408 event->inhibit = TRUE;
1412 empathy_event_approve (EmpathyEvent *event_public)
1414 EventPriv *event = (EventPriv *) event_public;
1416 g_return_if_fail (event_public != NULL);
1418 event_manager_approval_approve (event->approval);
1422 empathy_event_decline (EmpathyEvent *event_public)
1424 EventPriv *event = (EventPriv *) event_public;
1426 g_return_if_fail (event_public != NULL);
1428 reject_approval (event->approval);