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.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 EmpathyContactManager *contact_manager;
81 /* Ongoing approvals */
85 } EmpathyEventManagerPriv;
87 typedef struct _EventPriv EventPriv;
88 typedef void (*EventFunc) (EventPriv *event);
92 EmpathyEventManager *manager;
93 EventManagerApproval *approval;
97 guint autoremove_timeout_id;
107 static guint signals[LAST_SIGNAL];
109 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
111 static EmpathyEventManager * manager_singleton = NULL;
113 static EventManagerApproval *
114 event_manager_approval_new (EmpathyEventManager *manager,
115 TpChannelDispatchOperation *operation,
116 TpChannel *main_channel)
118 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
119 result->operation = g_object_ref (operation);
120 result->manager = manager;
121 result->main_channel = g_object_ref (main_channel);
127 event_manager_approval_free (EventManagerApproval *approval)
129 g_signal_handler_disconnect (approval->operation,
130 approval->invalidated_handler);
131 g_object_unref (approval->operation);
133 g_object_unref (approval->main_channel);
135 if (approval->handler != 0)
136 g_signal_handler_disconnect (approval->handler_instance,
139 if (approval->handler_instance != NULL)
140 g_object_unref (approval->handler_instance);
142 if (approval->contact != NULL)
143 g_object_unref (approval->contact);
145 if (approval->dialog != NULL)
147 gtk_widget_destroy (approval->dialog);
150 g_slice_free (EventManagerApproval, approval);
154 event_free (EventPriv *event)
156 g_free (event->public.icon_name);
157 g_free (event->public.header);
158 g_free (event->public.message);
160 if (event->autoremove_timeout_id != 0)
161 g_source_remove (event->autoremove_timeout_id);
163 if (event->public.contact)
165 g_object_unref (event->public.contact);
168 g_slice_free (EventPriv, event);
172 event_remove (EventPriv *event)
174 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
176 DEBUG ("Removing event %p", event);
178 priv->events = g_slist_remove (priv->events, event);
179 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
184 empathy_event_remove (EmpathyEvent *event_public)
186 EventPriv *event = (EventPriv *) event_public;
188 event_remove (event);
192 autoremove_event_timeout_cb (EventPriv *event)
194 event->autoremove_timeout_id = 0;
195 event_remove (event);
200 display_notify_area (void)
203 GSettings *gsettings;
206 gsettings = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
208 result = g_settings_get_boolean (gsettings,
209 EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA);
210 g_object_unref (gsettings);
216 event_manager_add (EmpathyEventManager *manager,
217 EmpathyContact *contact,
218 EmpathyEventType type,
219 const gchar *icon_name,
221 const gchar *message,
222 EventManagerApproval *approval,
226 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
229 event = g_slice_new0 (EventPriv);
230 event->public.contact = contact ? g_object_ref (contact) : NULL;
231 event->public.type = type;
232 event->public.icon_name = g_strdup (icon_name);
233 event->public.header = g_strdup (header);
234 event->public.message = g_strdup (message);
235 event->public.must_ack = (func != NULL);
236 event->inhibit = FALSE;
238 event->user_data = user_data;
239 event->manager = manager;
240 event->approval = approval;
242 DEBUG ("Adding event %p", event);
243 priv->events = g_slist_prepend (priv->events, event);
245 if (!display_notify_area ())
247 /* Don't fire the 'event-added' signal as we activate the event now */
248 if (approval != NULL)
249 approval->auto_approved = TRUE;
251 empathy_event_activate (&event->public);
255 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
257 if (!event->public.must_ack)
259 event->autoremove_timeout_id = g_timeout_add_seconds (
260 NOTIFICATION_TIMEOUT, (GSourceFunc) autoremove_event_timeout_cb,
266 handle_with_cb (GObject *source,
267 GAsyncResult *result,
270 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
271 GError *error = NULL;
273 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
275 DEBUG ("HandleWith failed: %s\n", error->message);
276 g_error_free (error);
281 handle_with_time_cb (GObject *source,
282 GAsyncResult *result,
285 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
286 GError *error = NULL;
288 if (!tp_channel_dispatch_operation_handle_with_time_finish (cdo, result,
291 if (g_error_matches (error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED))
293 EventManagerApproval *approval = user_data;
295 DEBUG ("HandleWithTime() is not implemented, falling back to "
296 "HandleWith(). Please upgrade to telepathy-mission-control "
299 tp_channel_dispatch_operation_handle_with_async (approval->operation,
300 NULL, handle_with_cb, approval);
304 DEBUG ("HandleWithTime failed: %s\n", error->message);
306 g_error_free (error);
311 event_manager_approval_approve (EventManagerApproval *approval)
315 if (approval->auto_approved)
317 timestamp = TP_USER_ACTION_TIME_NOT_USER_ACTION;
321 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
324 g_assert (approval->operation != NULL);
326 tp_channel_dispatch_operation_handle_with_time_async (approval->operation,
327 NULL, timestamp, handle_with_time_cb, approval);
331 event_channel_process_func (EventPriv *event)
333 event_manager_approval_approve (event->approval);
337 event_text_channel_process_func (EventPriv *event)
339 EmpathyTpChat *tp_chat;
342 timestamp = tp_user_action_time_from_x11 (gtk_get_current_event_time ());
344 if (event->approval->handler != 0)
346 tp_chat = EMPATHY_TP_CHAT (event->approval->handler_instance);
348 g_signal_handler_disconnect (tp_chat, event->approval->handler);
349 event->approval->handler = 0;
352 event_manager_approval_approve (event->approval);
356 event_lookup_by_approval (EmpathyEventManager *manager,
357 EventManagerApproval *approval)
359 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
361 EventPriv *retval = NULL;
363 for (l = priv->events; l; l = l->next)
365 EventPriv *event = l->data;
367 if (event->approval == approval)
378 event_update (EmpathyEventManager *manager, EventPriv *event,
379 const char *icon_name, const char *header, const char *msg)
381 g_free (event->public.icon_name);
382 g_free (event->public.header);
383 g_free (event->public.message);
385 event->public.icon_name = g_strdup (icon_name);
386 event->public.header = g_strdup (header);
387 event->public.message = g_strdup (msg);
389 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
393 reject_channel_claim_cb (GObject *source,
394 GAsyncResult *result,
397 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
398 GError *error = NULL;
400 if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
402 DEBUG ("Failed to claim channel: %s", error->message);
404 g_error_free (error);
408 if (EMPATHY_IS_TP_CALL (user_data))
410 empathy_tp_call_close (user_data);
412 else if (EMPATHY_IS_TP_CHAT (user_data))
414 empathy_tp_chat_leave (user_data);
416 else if (EMPATHY_IS_TP_FILE (user_data))
418 empathy_tp_file_close (user_data);
422 g_object_unref (user_data);
426 reject_approval (EventManagerApproval *approval)
428 /* We have to claim the channel before closing it */
429 tp_channel_dispatch_operation_claim_async (approval->operation,
430 reject_channel_claim_cb, g_object_ref (approval->handler_instance));
434 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
435 gint response, gpointer user_data)
437 EventManagerApproval *approval = user_data;
439 gtk_widget_destroy (approval->dialog);
440 approval->dialog = NULL;
442 if (response != GTK_RESPONSE_ACCEPT)
444 reject_approval (approval);
448 event_manager_approval_approve (approval);
453 event_channel_process_voip_func (EventPriv *event)
462 if (event->approval->dialog != NULL)
464 gtk_window_present (GTK_WINDOW (event->approval->dialog));
468 call = EMPATHY_TP_CALL (event->approval->handler_instance);
470 video = empathy_tp_call_has_initial_video (call);
472 dialog = gtk_message_dialog_new (NULL, 0,
473 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
474 video ? _("Incoming video call"): _("Incoming call"));
476 gtk_message_dialog_format_secondary_text (
477 GTK_MESSAGE_DIALOG (dialog), video ?
478 _("%s is video calling you. Do you want to answer?"):
479 _("%s is calling you. Do you want to answer?"),
480 empathy_contact_get_alias (event->approval->contact));
482 title = g_strdup_printf (_("Incoming call from %s"),
483 empathy_contact_get_alias (event->approval->contact));
485 gtk_window_set_title (GTK_WINDOW (dialog), title);
488 /* Set image of the dialog */
491 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
492 GTK_ICON_SIZE_DIALOG);
496 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
497 GTK_ICON_SIZE_DIALOG);
500 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
501 gtk_widget_show (image);
503 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
506 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
507 _("_Reject"), GTK_RESPONSE_REJECT);
508 image = gtk_image_new_from_icon_name ("call-stop",
509 GTK_ICON_SIZE_BUTTON);
510 gtk_button_set_image (GTK_BUTTON (button), image);
512 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
513 _("_Answer"), GTK_RESPONSE_ACCEPT);
515 image = gtk_image_new_from_icon_name ("call-start", GTK_ICON_SIZE_BUTTON);
516 gtk_button_set_image (GTK_BUTTON (button), image);
518 g_signal_connect (dialog, "response",
519 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
522 gtk_widget_show (dialog);
524 event->approval->dialog = dialog;
528 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
529 EmpathyMessage *message,
530 EventManagerApproval *approval)
532 GtkWidget *window = empathy_main_window_dup ();
533 EmpathyContact *sender;
539 /* try to update the event if it's referring to a chat which is already in the
541 event = event_lookup_by_approval (approval->manager, approval);
543 sender = empathy_message_get_sender (message);
544 header = empathy_contact_get_alias (sender);
545 msg = empathy_message_get_body (message);
547 channel = empathy_tp_chat_get_channel (tp_chat);
550 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header,
553 event_manager_add (approval->manager, sender, EMPATHY_EVENT_TYPE_CHAT,
554 EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval,
555 event_text_channel_process_func, NULL);
557 empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
559 g_object_unref (window);
563 event_manager_approval_done (EventManagerApproval *approval)
565 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
568 if (approval->operation != NULL)
572 channel_type = tp_channel_get_channel_type_id (approval->main_channel);
574 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
577 if (priv->ringing == 0)
578 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
582 priv->approvals = g_slist_remove (priv->approvals, approval);
584 for (l = priv->events; l; l = l->next)
586 EventPriv *event = l->data;
588 if (event->approval == approval)
590 event_remove (event);
595 event_manager_approval_free (approval);
599 cdo_invalidated_cb (TpProxy *cdo,
603 EventManagerApproval *approval)
605 DEBUG ("ChannelDispatchOperation has been invalidated: %s", message);
607 event_manager_approval_done (approval);
611 event_manager_media_channel_got_contact (EventManagerApproval *approval)
613 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
614 GtkWidget *window = empathy_main_window_dup ();
619 call = EMPATHY_TP_CALL (approval->handler_instance);
621 video = empathy_tp_call_has_initial_video (call);
623 header = g_strdup_printf (
624 video ? _("Incoming video call from %s") :_("Incoming call from %s"),
625 empathy_contact_get_alias (approval->contact));
627 event_manager_add (approval->manager, approval->contact,
628 EMPATHY_EVENT_TYPE_VOIP,
629 video ? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
630 header, NULL, approval,
631 event_channel_process_voip_func, NULL);
636 if (priv->ringing == 1)
637 empathy_sound_start_playing (window,
638 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
640 g_object_unref (window);
644 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
645 GParamSpec *param, EventManagerApproval *approval)
647 EmpathyContact *contact;
649 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
654 approval->contact = contact;
655 event_manager_media_channel_got_contact (approval);
659 invite_dialog_response_cb (GtkDialog *dialog,
661 EventManagerApproval *approval)
663 EmpathyTpChat *tp_chat;
665 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
666 approval->dialog = NULL;
668 tp_chat = EMPATHY_TP_CHAT (approval->handler_instance);
670 if (response != GTK_RESPONSE_OK)
673 DEBUG ("Muc invitation rejected");
675 reject_approval (approval);
680 DEBUG ("Muc invitation accepted");
682 /* We'll join the room when handling the channel */
683 event_manager_approval_approve (approval);
687 event_room_channel_process_func (EventPriv *event)
689 GtkWidget *dialog, *button, *image;
690 TpChannel *channel = event->approval->main_channel;
693 if (event->approval->dialog != NULL)
695 gtk_window_present (GTK_WINDOW (event->approval->dialog));
700 dialog = gtk_message_dialog_new (NULL, 0,
701 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
703 title = g_strdup_printf (_("Invitation to join %s"),
704 tp_channel_get_identifier (channel));
706 gtk_window_set_title (GTK_WINDOW (dialog), title);
709 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
710 _("%s is inviting you to join %s"),
711 empathy_contact_get_alias (event->approval->contact),
712 tp_channel_get_identifier (channel));
714 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
717 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
718 _("_Decline"), GTK_RESPONSE_CANCEL);
719 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
720 gtk_button_set_image (GTK_BUTTON (button), image);
722 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
723 _("_Join"), GTK_RESPONSE_OK);
724 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
725 gtk_button_set_image (GTK_BUTTON (button), image);
727 g_signal_connect (dialog, "response",
728 G_CALLBACK (invite_dialog_response_cb), event->approval);
730 gtk_widget_show (dialog);
732 event->approval->dialog = dialog;
736 display_invite_room_dialog (EventManagerApproval *approval)
738 GtkWidget *window = empathy_main_window_dup ();
739 const gchar *invite_msg;
741 TpHandle self_handle;
743 self_handle = tp_channel_group_get_self_handle (approval->main_channel);
744 tp_channel_group_get_local_pending_info (approval->main_channel, self_handle,
745 NULL, NULL, &invite_msg);
747 if (approval->contact != NULL)
749 msg = g_strdup_printf (_("%s invited you to join %s"),
750 empathy_contact_get_alias (approval->contact),
751 tp_channel_get_identifier (approval->main_channel));
755 msg = g_strdup_printf (_("You have been invited to join %s"),
756 tp_channel_get_identifier (approval->main_channel));
759 event_manager_add (approval->manager, approval->contact,
760 EMPATHY_EVENT_TYPE_INVITATION, EMPATHY_IMAGE_GROUP_MESSAGE, msg,
761 invite_msg, approval, event_room_channel_process_func, NULL);
763 empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
766 g_object_unref (window);
770 event_manager_muc_invite_got_contact_cb (TpConnection *connection,
771 EmpathyContact *contact,
776 EventManagerApproval *approval = (EventManagerApproval *) user_data;
780 DEBUG ("Error: %s", error->message);
784 approval->contact = g_object_ref (contact);
787 display_invite_room_dialog (approval);
791 event_manager_ft_got_contact_cb (TpConnection *connection,
792 EmpathyContact *contact,
797 EventManagerApproval *approval = (EventManagerApproval *) user_data;
798 GtkWidget *window = empathy_main_window_dup ();
801 approval->contact = g_object_ref (contact);
803 header = g_strdup_printf (_("Incoming file transfer from %s"),
804 empathy_contact_get_alias (approval->contact));
806 event_manager_add (approval->manager, approval->contact,
807 EMPATHY_EVENT_TYPE_TRANSFER, EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL,
808 approval, event_channel_process_func, NULL);
810 /* FIXME better sound for incoming file transfers ?*/
811 empathy_sound_play (window, EMPATHY_SOUND_CONVERSATION_NEW);
814 g_object_unref (window);
817 /* If there is a file-transfer or media channel consider it as the
820 find_main_channel (GList *channels)
823 TpChannel *text = NULL;
825 for (l = channels; l != NULL; l = g_list_next (l))
827 TpChannel *channel = l->data;
830 if (tp_proxy_get_invalidated (channel) != NULL)
833 channel_type = tp_channel_get_channel_type_id (channel);
835 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
836 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
839 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
847 approve_channels (TpSimpleApprover *approver,
849 TpConnection *connection,
851 TpChannelDispatchOperation *dispatch_operation,
852 TpAddDispatchOperationContext *context,
855 EmpathyEventManager *self = user_data;
856 EmpathyEventManagerPriv *priv = GET_PRIV (self);
858 EventManagerApproval *approval;
861 channel = find_main_channel (channels);
864 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
865 "Unknown channel type" };
867 DEBUG ("Failed to find the main channel; ignoring");
869 tp_add_dispatch_operation_context_fail (context, &error);
873 approval = event_manager_approval_new (self, dispatch_operation, channel);
874 priv->approvals = g_slist_prepend (priv->approvals, approval);
876 approval->invalidated_handler = g_signal_connect (dispatch_operation,
877 "invalidated", G_CALLBACK (cdo_invalidated_cb), approval);
879 channel_type = tp_channel_get_channel_type_id (channel);
881 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
883 EmpathyTpChat *tp_chat;
885 tp_chat = empathy_tp_chat_new (account, channel);
886 approval->handler_instance = G_OBJECT (tp_chat);
888 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
890 /* Are we in local-pending ? */
893 if (empathy_tp_chat_is_invited (tp_chat, &inviter))
895 /* We are invited to a room */
896 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
897 tp_channel_get_identifier (channel));
901 empathy_tp_contact_factory_get_from_handle (connection,
902 inviter, event_manager_muc_invite_got_contact_cb,
903 approval, NULL, G_OBJECT (self));
907 display_invite_room_dialog (approval);
913 /* We are not invited, approve the channel right now */
914 tp_add_dispatch_operation_context_accept (context);
916 approval->auto_approved = TRUE;
917 event_manager_approval_approve (approval);
921 /* 1-1 text channel, wait for the first message */
922 approval->handler = g_signal_connect (tp_chat, "message-received",
923 G_CALLBACK (event_manager_chat_message_received_cb), approval);
925 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
927 EmpathyContact *contact;
928 EmpathyTpCall *call = empathy_tp_call_new (account, channel);
930 approval->handler_instance = G_OBJECT (call);
932 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
936 g_signal_connect (call, "notify::contact",
937 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
942 approval->contact = contact;
943 event_manager_media_channel_got_contact (approval);
947 else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
950 EmpathyTpFile *tp_file = empathy_tp_file_new (channel);
952 approval->handler_instance = G_OBJECT (tp_file);
954 handle = tp_channel_get_handle (channel, NULL);
956 connection = tp_channel_borrow_connection (channel);
957 empathy_tp_contact_factory_get_from_handle (connection, handle,
958 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (self));
962 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
963 "Invalid channel type" };
965 DEBUG ("Unknown channel type (%s), ignoring..",
966 g_quark_to_string (channel_type));
968 tp_add_dispatch_operation_context_fail (context, &error);
973 tp_add_dispatch_operation_context_accept (context);
977 event_pending_subscribe_func (EventPriv *event)
979 empathy_subscription_dialog_show (event->public.contact, NULL, NULL);
980 event_remove (event);
984 event_manager_pendings_changed_cb (EmpathyContactList *list,
985 EmpathyContact *contact, EmpathyContact *actor,
986 guint reason, gchar *message, gboolean is_pending,
987 EmpathyEventManager *manager)
989 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
990 gchar *header, *event_msg;
996 for (l = priv->events; l; l = l->next)
998 EventPriv *event = l->data;
1000 if (event->public.contact == contact &&
1001 event->func == event_pending_subscribe_func)
1003 event_remove (event);
1011 header = g_strdup_printf (
1012 _("%s would like permission to see when you are available"),
1013 empathy_contact_get_alias (contact));
1015 if (!EMP_STR_EMPTY (message))
1016 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
1020 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_SUBSCRIPTION,
1021 GTK_STOCK_DIALOG_QUESTION, header, event_msg, NULL,
1022 event_pending_subscribe_func, NULL);
1029 event_manager_presence_changed_cb (EmpathyContact *contact,
1030 TpConnectionPresenceType current,
1031 TpConnectionPresenceType previous,
1032 EmpathyEventManager *manager)
1035 gchar *header = NULL;
1037 GSettings *gsettings = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1038 GtkWidget *window = empathy_main_window_dup ();
1040 account = empathy_contact_get_account (contact);
1041 idle = empathy_idle_dup_singleton ();
1043 if (empathy_idle_account_is_just_connected (idle, account))
1046 if (tp_connection_presence_type_cmp_availability (previous,
1047 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1049 /* contact was online */
1050 if (tp_connection_presence_type_cmp_availability (current,
1051 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1053 /* someone is logging off */
1054 empathy_sound_play (window, EMPATHY_SOUND_CONTACT_DISCONNECTED);
1056 if (g_settings_get_boolean (gsettings,
1057 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT))
1059 header = g_strdup_printf (_("%s is now offline."),
1060 empathy_contact_get_alias (contact));
1062 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1063 EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1069 /* contact was offline */
1070 if (tp_connection_presence_type_cmp_availability (current,
1071 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1073 /* someone is logging in */
1074 empathy_sound_play (window, EMPATHY_SOUND_CONTACT_CONNECTED);
1076 if (g_settings_get_boolean (gsettings,
1077 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN))
1079 header = g_strdup_printf (_("%s is now online."),
1080 empathy_contact_get_alias (contact));
1082 event_manager_add (manager, contact, EMPATHY_EVENT_TYPE_PRESENCE,
1083 EMPATHY_IMAGE_AVATAR_DEFAULT, header, NULL, NULL, NULL, NULL);
1090 g_object_unref (idle);
1091 g_object_unref (gsettings);
1092 g_object_unref (window);
1096 event_manager_members_changed_cb (EmpathyContactList *list,
1097 EmpathyContact *contact,
1098 EmpathyContact *actor,
1102 EmpathyEventManager *manager)
1105 g_signal_connect (contact, "presence-changed",
1106 G_CALLBACK (event_manager_presence_changed_cb), manager);
1108 g_signal_handlers_disconnect_by_func (contact,
1109 event_manager_presence_changed_cb, manager);
1113 event_manager_constructor (GType type,
1115 GObjectConstructParam *props)
1119 if (manager_singleton) {
1120 retval = g_object_ref (manager_singleton);
1122 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1123 (type, n_props, props);
1125 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1126 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1133 event_manager_finalize (GObject *object)
1135 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1137 if (priv->ringing > 0)
1138 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
1140 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1141 g_slist_free (priv->events);
1142 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1143 g_slist_free (priv->approvals);
1144 g_object_unref (priv->contact_manager);
1145 g_object_unref (priv->approver);
1149 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1151 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1153 object_class->finalize = event_manager_finalize;
1154 object_class->constructor = event_manager_constructor;
1156 signals[EVENT_ADDED] =
1157 g_signal_new ("event-added",
1158 G_TYPE_FROM_CLASS (klass),
1162 g_cclosure_marshal_VOID__POINTER,
1166 signals[EVENT_REMOVED] =
1167 g_signal_new ("event-removed",
1168 G_TYPE_FROM_CLASS (klass),
1172 g_cclosure_marshal_VOID__POINTER,
1173 G_TYPE_NONE, 1, G_TYPE_POINTER);
1175 signals[EVENT_UPDATED] =
1176 g_signal_new ("event-updated",
1177 G_TYPE_FROM_CLASS (klass),
1181 g_cclosure_marshal_VOID__POINTER,
1182 G_TYPE_NONE, 1, G_TYPE_POINTER);
1185 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1189 empathy_event_manager_init (EmpathyEventManager *manager)
1191 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1192 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1194 GError *error = NULL;
1196 manager->priv = priv;
1198 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1199 g_signal_connect (priv->contact_manager, "pendings-changed",
1200 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1202 g_signal_connect (priv->contact_manager, "members-changed",
1203 G_CALLBACK (event_manager_members_changed_cb), manager);
1205 dbus = tp_dbus_daemon_dup (&error);
1208 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
1209 g_error_free (error);
1213 priv->approver = tp_simple_approver_new (dbus, "Empathy.EventManager", FALSE,
1214 approve_channels, manager, NULL);
1216 /* EmpathyTpChat relies on this feature being prepared */
1217 tp_base_client_add_connection_features_varargs (priv->approver,
1218 TP_CONNECTION_FEATURE_CAPABILITIES, 0);
1220 /* Private text channels */
1221 tp_base_client_take_approver_filter (priv->approver,
1223 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1224 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1227 /* Muc text channels */
1228 tp_base_client_take_approver_filter (priv->approver,
1230 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
1231 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
1235 tp_base_client_take_approver_filter (priv->approver,
1237 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1238 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1239 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1243 tp_base_client_take_approver_filter (priv->approver,
1245 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
1246 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1247 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
1250 if (!tp_base_client_register (priv->approver, &error))
1252 DEBUG ("Failed to register Approver: %s", error->message);
1253 g_error_free (error);
1256 g_object_unref (dbus);
1259 EmpathyEventManager *
1260 empathy_event_manager_dup_singleton (void)
1262 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1266 empathy_event_manager_get_events (EmpathyEventManager *manager)
1268 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1270 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1272 return priv->events;
1276 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1278 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1280 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1282 return priv->events ? priv->events->data : NULL;
1286 empathy_event_activate (EmpathyEvent *event_public)
1288 EventPriv *event = (EventPriv *) event_public;
1290 g_return_if_fail (event_public != NULL);
1293 event->func (event);
1295 event_remove (event);
1299 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1301 EventPriv *event = (EventPriv *) event_public;
1303 g_return_if_fail (event_public != NULL);
1305 event->inhibit = TRUE;
1309 empathy_event_approve (EmpathyEvent *event_public)
1311 EventPriv *event = (EventPriv *) event_public;
1313 g_return_if_fail (event_public != NULL);
1315 event_manager_approval_approve (event->approval);
1319 empathy_event_decline (EmpathyEvent *event_public)
1321 EventPriv *event = (EventPriv *) event_public;
1323 g_return_if_fail (event_public != NULL);
1325 reject_approval (event->approval);