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/util.h>
29 #include <libempathy/empathy-account-manager.h>
30 #include <libempathy/empathy-dispatcher.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-tp-chat.h>
34 #include <libempathy/empathy-tp-call.h>
35 #include <libempathy/empathy-tp-file.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-call-factory.h>
39 #include <extensions/extensions.h>
41 #include <libempathy-gtk/empathy-conf.h>
42 #include <libempathy-gtk/empathy-images.h>
43 #include <libempathy-gtk/empathy-contact-dialogs.h>
44 #include <libempathy-gtk/empathy-sound.h>
46 #include "empathy-event-manager.h"
47 #include "empathy-main-window.h"
48 #include "empathy-tube-dispatch.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 /* The time interval in milliseconds between 2 incoming rings */
58 #define MS_BETWEEN_RING 500
61 EmpathyEventManager *manager;
62 EmpathyDispatchOperation *operation;
63 gulong approved_handler;
64 gulong claimed_handler;
65 gulong invalidated_handler;
66 /* Remove contact if applicable */
67 EmpathyContact *contact;
68 /* Tube dispatcher if applicable */
69 EmpathyTubeDispatch *tube_dispatch;
70 /* option signal handler and it's instance */
72 GObject *handler_instance;
73 /* optional accept widget */
75 } EventManagerApproval;
78 EmpathyDispatcher *dispatcher;
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;
106 static guint signals[LAST_SIGNAL];
108 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
110 static EmpathyEventManager * manager_singleton = NULL;
112 static EventManagerApproval *
113 event_manager_approval_new (EmpathyEventManager *manager,
114 EmpathyDispatchOperation *operation)
116 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
117 result->operation = g_object_ref (operation);
118 result->manager = manager;
124 event_manager_approval_free (EventManagerApproval *approval)
126 g_signal_handler_disconnect (approval->operation,
127 approval->approved_handler);
128 g_signal_handler_disconnect (approval->operation,
129 approval->claimed_handler);
130 g_signal_handler_disconnect (approval->operation,
131 approval->invalidated_handler);
132 g_object_unref (approval->operation);
134 if (approval->handler != 0)
135 g_signal_handler_disconnect (approval->handler_instance,
138 if (approval->contact != NULL)
139 g_object_unref (approval->contact);
141 if (approval->tube_dispatch != NULL)
142 g_object_unref (approval->tube_dispatch);
144 if (approval->dialog != NULL)
146 gtk_widget_destroy (approval->dialog);
149 g_slice_free (EventManagerApproval, approval);
152 static void event_remove (EventPriv *event);
155 event_free (EventPriv *event)
157 g_free (event->public.icon_name);
158 g_free (event->public.header);
159 g_free (event->public.message);
161 if (event->public.contact)
163 g_object_unref (event->public.contact);
166 g_slice_free (EventPriv, event);
170 event_remove (EventPriv *event)
172 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
174 DEBUG ("Removing event %p", event);
175 priv->events = g_slist_remove (priv->events, event);
176 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
181 autoremove_event_timeout_cb (EventPriv *event)
183 event_remove (event);
188 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
189 const gchar *icon_name, const gchar *header, const gchar *message,
190 EventManagerApproval *approval, EventFunc func, gpointer user_data)
192 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
195 event = g_slice_new0 (EventPriv);
196 event->public.contact = contact ? g_object_ref (contact) : NULL;
197 event->public.icon_name = g_strdup (icon_name);
198 event->public.header = g_strdup (header);
199 event->public.message = g_strdup (message);
200 event->public.must_ack = (func != NULL);
201 event->inhibit = FALSE;
203 event->user_data = user_data;
204 event->manager = manager;
205 event->approval = approval;
207 DEBUG ("Adding event %p", event);
208 priv->events = g_slist_prepend (priv->events, event);
209 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
211 if (!event->public.must_ack)
213 g_timeout_add_seconds (NOTIFICATION_TIMEOUT,
214 (GSourceFunc) autoremove_event_timeout_cb, event);
219 event_channel_process_func (EventPriv *event)
221 empathy_dispatch_operation_approve (event->approval->operation);
225 event_text_channel_process_func (EventPriv *event)
227 EmpathyTpChat *tp_chat;
229 if (event->approval->handler != 0)
231 tp_chat = EMPATHY_TP_CHAT
232 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
234 g_signal_handler_disconnect (tp_chat, event->approval->handler);
235 event->approval->handler = 0;
238 empathy_dispatch_operation_approve (event->approval->operation);
242 event_lookup_by_approval (EmpathyEventManager *manager,
243 EventManagerApproval *approval)
245 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
247 EventPriv *retval = NULL;
249 for (l = priv->events; l; l = l->next)
251 EventPriv *event = l->data;
253 if (event->approval == approval)
264 event_update (EmpathyEventManager *manager, EventPriv *event,
265 const char *icon_name, const char *header, const char *msg)
267 g_free (event->public.icon_name);
268 g_free (event->public.header);
269 g_free (event->public.message);
271 event->public.icon_name = g_strdup (icon_name);
272 event->public.header = g_strdup (header);
273 event->public.message = g_strdup (msg);
275 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
279 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
280 gint response, gpointer user_data)
282 EventManagerApproval *approval = user_data;
284 gtk_widget_destroy (approval->dialog);
285 approval->dialog = NULL;
287 if (response != GTK_RESPONSE_ACCEPT)
289 EmpathyTpCall *call =
291 empathy_dispatch_operation_get_channel_wrapper (
292 approval->operation));
295 if (empathy_dispatch_operation_claim (approval->operation))
296 empathy_tp_call_close (call);
297 g_object_unref (call);
302 EmpathyCallFactory *factory = empathy_call_factory_get ();
303 empathy_call_factory_claim_channel (factory, approval->operation);
308 event_channel_process_voip_func (EventPriv *event)
314 if (event->approval->dialog != NULL)
316 gtk_window_present (GTK_WINDOW (event->approval->dialog));
320 dialog = gtk_message_dialog_new (NULL, 0,
321 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
322 gtk_message_dialog_format_secondary_text (
323 GTK_MESSAGE_DIALOG (dialog),
324 _("%s is calling you, do you want to answer?"),
325 empathy_contact_get_name (event->approval->contact));
327 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
330 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
331 _("_Reject"), GTK_RESPONSE_REJECT);
332 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
333 GTK_ICON_SIZE_BUTTON);
334 gtk_button_set_image (GTK_BUTTON (button), image);
336 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
337 _("_Answer"), GTK_RESPONSE_ACCEPT);
339 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
340 gtk_button_set_image (GTK_BUTTON (button), image);
342 g_signal_connect (dialog, "response",
343 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
346 gtk_widget_show (dialog);
348 event->approval->dialog = dialog;
352 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
353 EmpathyMessage *message, EventManagerApproval *approval)
355 EmpathyContact *sender;
361 /* try to update the event if it's referring to a chat which is already in the
363 event = event_lookup_by_approval (approval->manager, approval);
365 if (event != NULL && event->inhibit && approval->handler != 0)
367 g_signal_handler_disconnect (tp_chat, approval->handler);
368 approval->handler = 0;
372 sender = empathy_message_get_sender (message);
373 header = empathy_contact_get_name (sender);
374 msg = empathy_message_get_body (message);
376 channel = empathy_tp_chat_get_channel (tp_chat);
379 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
381 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
382 msg, approval, event_text_channel_process_func, NULL);
384 empathy_sound_play (empathy_main_window_get (),
385 EMPATHY_SOUND_CONVERSATION_NEW);
389 event_manager_approval_done (EventManagerApproval *approval)
391 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
394 if (approval->operation != NULL)
398 channel_type = empathy_dispatch_operation_get_channel_type_id (
399 approval->operation);
400 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
403 if (priv->ringing == 0)
404 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
408 priv->approvals = g_slist_remove (priv->approvals, approval);
410 for (l = priv->events; l; l = l->next)
412 EventPriv *event = l->data;
414 if (event->approval == approval)
416 event_remove (event);
421 event_manager_approval_free (approval);
425 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
426 EventManagerApproval *approval)
428 event_manager_approval_done (approval);
432 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
433 EventManagerApproval *approval)
435 event_manager_approval_done (approval);
439 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
440 guint domain, gint code, gchar *message,
441 EventManagerApproval *approval)
443 event_manager_approval_done (approval);
447 event_manager_media_channel_got_contact (EventManagerApproval *approval)
449 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
452 header = g_strdup_printf (_("Incoming call from %s"),
453 empathy_contact_get_name (approval->contact));
455 event_manager_add (approval->manager,
456 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
457 approval, event_channel_process_voip_func, NULL);
462 if (priv->ringing == 1)
463 empathy_sound_start_playing (empathy_main_window_get (),
464 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
468 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
469 GParamSpec *param, EventManagerApproval *approval)
471 EmpathyContact *contact;
473 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
478 approval->contact = contact;
479 event_manager_media_channel_got_contact (approval);
483 event_manager_tube_approved_cb (EventPriv *event)
485 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
489 event_manager_add_tube_approval (EventManagerApproval *approval,
490 EmpathyTubeDispatchAbility ability)
492 const gchar *icon_name;
496 header = g_strdup_printf (_("%s is offering you an invitation"),
497 empathy_contact_get_name (approval->contact));
499 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
501 icon_name = GTK_STOCK_EXECUTE;
502 msg = _("An external application will be started to handle it.");
506 icon_name = GTK_STOCK_DIALOG_ERROR;
507 msg = _("You don't have the needed external "
508 "application to handle it.");
511 event_manager_add (approval->manager, approval->contact, icon_name, header,
512 msg, approval, event_manager_tube_approved_cb, approval);
515 /* FIXME better sound for incoming tubes ? */
516 empathy_sound_play (empathy_main_window_get (),
517 EMPATHY_SOUND_CONVERSATION_NEW);
521 event_manager_tube_dispatch_ability_cb (GObject *object,
522 GParamSpec *spec, gpointer user_data)
524 EventManagerApproval *approval = (EventManagerApproval *) user_data;
525 EmpathyTubeDispatchAbility dispatchability;
528 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
530 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
532 event_manager_add_tube_approval (approval, dispatchability);
533 g_signal_handler_disconnect (object, approval->handler);
534 approval->handler = 0;
539 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
540 EmpathyContact *contact,
545 EventManagerApproval *approval = (EventManagerApproval *) user_data;
546 EmpathyTubeDispatchAbility dispatchability;
550 /* FIXME: We should probably still display the event */
551 DEBUG ("Error: %s", error->message);
555 approval->contact = g_object_ref (contact);
557 dispatchability = empathy_tube_dispatch_is_dispatchable
558 (approval->tube_dispatch);
560 switch (dispatchability)
562 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
563 approval->handler = g_signal_connect (approval->tube_dispatch,
564 "notify::dispatchability",
565 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
566 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
568 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
570 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
571 event_manager_add_tube_approval (approval, dispatchability);
577 invite_dialog_response_cb (GtkDialog *dialog,
579 EventManagerApproval *approval)
581 EmpathyTpChat *tp_chat;
583 TpHandle self_handle;
586 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
587 approval->dialog = NULL;
589 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
590 approval->operation));
592 if (response != GTK_RESPONSE_OK)
595 DEBUG ("Muc invitation rejected");
597 if (empathy_dispatch_operation_claim (approval->operation))
598 empathy_tp_chat_close (tp_chat);
602 DEBUG ("Muc invitation accepted");
605 channel = empathy_tp_chat_get_channel (tp_chat);
607 self_handle = tp_channel_group_get_self_handle (channel);
608 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
609 g_array_append_val (members, self_handle);
611 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
612 "", NULL, NULL, NULL, NULL);
614 empathy_dispatch_operation_approve (approval->operation);
616 g_array_free (members, TRUE);
620 event_room_channel_process_func (EventPriv *event)
622 GtkWidget *dialog, *button, *image;
623 TpChannel *channel = empathy_dispatch_operation_get_channel (
624 event->approval->operation);
626 if (event->approval->dialog != NULL)
628 gtk_window_present (GTK_WINDOW (event->approval->dialog));
633 dialog = gtk_message_dialog_new (NULL, 0,
634 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
636 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
637 _("%s is inviting you to join %s"),
638 empathy_contact_get_name (event->approval->contact),
639 tp_channel_get_identifier (channel));
641 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
644 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
645 _("_Decline"), GTK_RESPONSE_CANCEL);
646 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
647 gtk_button_set_image (GTK_BUTTON (button), image);
649 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
650 _("_Join"), GTK_RESPONSE_OK);
651 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
652 gtk_button_set_image (GTK_BUTTON (button), image);
654 g_signal_connect (dialog, "response",
655 G_CALLBACK (invite_dialog_response_cb), event->approval);
657 gtk_widget_show (dialog);
659 event->approval->dialog = dialog;
663 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
664 EmpathyContact *contact,
669 EventManagerApproval *approval = (EventManagerApproval *) user_data;
671 const gchar *invite_msg;
673 TpHandle self_handle;
677 /* FIXME: We should probably still display the event */
678 DEBUG ("Error: %s", error->message);
682 approval->contact = g_object_ref (contact);
683 channel = empathy_dispatch_operation_get_channel (approval->operation);
685 self_handle = tp_channel_group_get_self_handle (channel);
686 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
689 msg = g_strdup_printf (_("%s invited you to join %s"),
690 empathy_contact_get_name (approval->contact),
691 tp_channel_get_identifier (channel));
693 event_manager_add (approval->manager,
694 approval->contact, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
695 approval, event_room_channel_process_func, NULL);
697 empathy_sound_play (empathy_main_window_get (),
698 EMPATHY_SOUND_CONVERSATION_NEW);
704 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
705 EmpathyContact *contact,
710 EventManagerApproval *approval = (EventManagerApproval *) user_data;
713 approval->contact = contact;
715 header = g_strdup_printf (_("Incoming file transfer from %s"),
716 empathy_contact_get_name (approval->contact));
718 event_manager_add (approval->manager, approval->contact,
719 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL, approval,
720 event_channel_process_func, NULL);
722 /* FIXME better sound for incoming file transfers ?*/
723 empathy_sound_play (empathy_main_window_get (),
724 EMPATHY_SOUND_CONVERSATION_NEW);
730 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
731 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
733 const gchar *channel_type;
734 EventManagerApproval *approval;
735 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
737 channel_type = empathy_dispatch_operation_get_channel_type (operation);
739 approval = event_manager_approval_new (manager, operation);
740 priv->approvals = g_slist_prepend (priv->approvals, approval);
742 approval->approved_handler = g_signal_connect (operation, "approved",
743 G_CALLBACK (event_manager_operation_approved_cb), approval);
745 approval->claimed_handler = g_signal_connect (operation, "claimed",
746 G_CALLBACK (event_manager_operation_claimed_cb), approval);
748 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
749 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
751 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
753 EmpathyTpChat *tp_chat =
755 empathy_dispatch_operation_get_channel_wrapper (operation));
756 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
758 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
760 /* Are we in local-pending ? */
761 TpHandle self_handle, inviter;
763 self_handle = tp_channel_group_get_self_handle (channel);
765 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
766 channel, self_handle, &inviter, NULL, NULL))
768 /* We are invited to a room */
769 EmpathyTpContactFactory *factory;
770 TpConnection *connection;
772 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
773 tp_channel_get_identifier (channel));
775 connection = empathy_tp_chat_get_connection (tp_chat);
776 factory = empathy_tp_contact_factory_dup_singleton (connection);
778 empathy_tp_contact_factory_get_from_handle (factory,
779 inviter, event_manager_muc_invite_got_contact_cb,
780 approval, NULL, G_OBJECT (manager));
782 g_object_unref (factory);
786 /* if we are not invited, let's wait for the first message */
789 /* 1-1 text channel, wait for the first message */
790 approval->handler = g_signal_connect (tp_chat, "message-received",
791 G_CALLBACK (event_manager_chat_message_received_cb), approval);
792 approval->handler_instance = G_OBJECT (tp_chat);
794 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
796 EmpathyContact *contact;
797 EmpathyTpCall *call = EMPATHY_TP_CALL (
798 empathy_dispatch_operation_get_channel_wrapper (operation));
800 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
804 g_signal_connect (call, "notify::contact",
805 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
810 approval->contact = contact;
811 event_manager_media_channel_got_contact (approval);
815 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
818 TpConnection *connection;
820 EmpathyTpContactFactory *factory;
822 channel = empathy_dispatch_operation_get_channel (operation);
823 handle = tp_channel_get_handle (channel, NULL);
825 connection = tp_channel_borrow_connection (channel);
826 factory = empathy_tp_contact_factory_dup_singleton (connection);
827 empathy_tp_contact_factory_get_from_handle (factory, handle,
828 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
830 g_object_unref (factory);
832 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
833 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
837 TpHandleType handle_type;
838 TpConnection *connection;
839 EmpathyTpContactFactory *factory;
841 channel = empathy_dispatch_operation_get_channel (operation);
842 handle = tp_channel_get_handle (channel, &handle_type);
844 /* Only understand p2p tubes */
845 if (handle_type != TP_HANDLE_TYPE_CONTACT)
848 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
849 connection = tp_channel_borrow_connection (channel);
850 factory = empathy_tp_contact_factory_dup_singleton (connection);
851 empathy_tp_contact_factory_get_from_handle (factory, handle,
852 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
856 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
861 event_pending_subscribe_func (EventPriv *event)
863 empathy_subscription_dialog_show (event->public.contact, NULL);
864 event_remove (event);
868 event_manager_pendings_changed_cb (EmpathyContactList *list,
869 EmpathyContact *contact, EmpathyContact *actor,
870 guint reason, gchar *message, gboolean is_pending,
871 EmpathyEventManager *manager)
873 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
874 gchar *header, *event_msg;
880 for (l = priv->events; l; l = l->next)
882 EventPriv *event = l->data;
884 if (event->public.contact == contact &&
885 event->func == event_pending_subscribe_func)
887 event_remove (event);
895 header = g_strdup_printf (_("Subscription requested by %s"),
896 empathy_contact_get_name (contact));
898 if (!EMP_STR_EMPTY (message))
899 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
903 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
904 event_msg, NULL, event_pending_subscribe_func, NULL);
911 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
912 EmpathyContact *contact,
913 TpConnectionPresenceType current,
914 TpConnectionPresenceType previous,
915 EmpathyEventManager *manager)
917 EmpathyAccount *account;
918 gchar *header = NULL;
919 gboolean preference = FALSE;
921 account = empathy_contact_get_account (contact);
922 if (empathy_account_is_just_connected (account))
925 if (tp_connection_presence_type_cmp_availability (previous,
926 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
928 /* contact was online */
929 empathy_conf_get_bool (empathy_conf_get (),
930 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
931 if (preference && tp_connection_presence_type_cmp_availability (current,
932 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
934 /* someone is logging off */
935 header = g_strdup_printf (_("%s is now offline."),
936 empathy_contact_get_name (contact));
938 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
939 NULL, NULL, NULL, NULL);
944 /* contact was offline */
945 empathy_conf_get_bool (empathy_conf_get (),
946 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
947 if (preference && tp_connection_presence_type_cmp_availability (current,
948 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
950 /* someone is logging in */
951 header = g_strdup_printf (_("%s is now online."),
952 empathy_contact_get_name (contact));
954 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
955 NULL, NULL, NULL, NULL);
963 event_manager_constructor (GType type,
965 GObjectConstructParam *props)
969 if (manager_singleton) {
970 retval = g_object_ref (manager_singleton);
972 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
973 (type, n_props, props);
975 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
976 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
983 event_manager_finalize (GObject *object)
985 EmpathyEventManagerPriv *priv = GET_PRIV (object);
987 if (priv->ringing > 0)
988 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
990 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
991 g_slist_free (priv->events);
992 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
993 g_slist_free (priv->approvals);
994 g_object_unref (priv->contact_manager);
995 g_object_unref (priv->dispatcher);
999 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1001 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1003 object_class->finalize = event_manager_finalize;
1004 object_class->constructor = event_manager_constructor;
1006 signals[EVENT_ADDED] =
1007 g_signal_new ("event-added",
1008 G_TYPE_FROM_CLASS (klass),
1012 g_cclosure_marshal_VOID__POINTER,
1016 signals[EVENT_REMOVED] =
1017 g_signal_new ("event-removed",
1018 G_TYPE_FROM_CLASS (klass),
1022 g_cclosure_marshal_VOID__POINTER,
1023 G_TYPE_NONE, 1, G_TYPE_POINTER);
1025 signals[EVENT_UPDATED] =
1026 g_signal_new ("event-updated",
1027 G_TYPE_FROM_CLASS (klass),
1031 g_cclosure_marshal_VOID__POINTER,
1032 G_TYPE_NONE, 1, G_TYPE_POINTER);
1035 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1039 empathy_event_manager_init (EmpathyEventManager *manager)
1041 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1042 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1043 EmpathyContactMonitor *monitor;
1044 EmpathyContactList *list_iface;
1046 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1047 monitor = empathy_contact_list_get_monitor (list_iface);
1048 g_object_unref (list_iface);
1050 manager->priv = priv;
1052 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1053 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1054 g_signal_connect (priv->dispatcher, "approve",
1055 G_CALLBACK (event_manager_approve_channel_cb), manager);
1056 g_signal_connect (priv->contact_manager, "pendings-changed",
1057 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1058 g_signal_connect (monitor, "contact-presence-changed",
1059 G_CALLBACK (event_manager_presence_changed_cb), manager);
1062 EmpathyEventManager *
1063 empathy_event_manager_dup_singleton (void)
1065 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1069 empathy_event_manager_get_events (EmpathyEventManager *manager)
1071 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1073 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1075 return priv->events;
1079 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1081 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1083 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1085 return priv->events ? priv->events->data : NULL;
1089 empathy_event_activate (EmpathyEvent *event_public)
1091 EventPriv *event = (EventPriv *) event_public;
1093 g_return_if_fail (event_public != NULL);
1096 event->func (event);
1098 event_remove (event);
1102 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1104 EventPriv *event = (EventPriv *) event_public;
1106 g_return_if_fail (event_public != NULL);
1108 event->inhibit = TRUE;