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>
28 #include <telepathy-glib/interfaces.h>
30 #include <libempathy/empathy-account-manager.h>
31 #include <libempathy/empathy-dispatcher.h>
32 #include <libempathy/empathy-tp-contact-factory.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-tp-chat.h>
35 #include <libempathy/empathy-tp-call.h>
36 #include <libempathy/empathy-tp-file.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-call-factory.h>
40 #include <extensions/extensions.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-contact-dialogs.h>
45 #include <libempathy-gtk/empathy-sound.h>
47 #include "empathy-event-manager.h"
48 #include "empathy-main-window.h"
49 #include "empathy-tube-dispatch.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 EmpathyDispatchOperation *operation;
64 gulong approved_handler;
65 gulong claimed_handler;
66 gulong invalidated_handler;
67 /* Remove contact if applicable */
68 EmpathyContact *contact;
69 /* Tube dispatcher if applicable */
70 EmpathyTubeDispatch *tube_dispatch;
71 /* option signal handler and it's instance */
73 GObject *handler_instance;
74 /* optional accept widget */
76 } EventManagerApproval;
79 EmpathyDispatcher *dispatcher;
80 EmpathyContactManager *contact_manager;
82 /* Ongoing approvals */
86 } EmpathyEventManagerPriv;
88 typedef struct _EventPriv EventPriv;
89 typedef void (*EventFunc) (EventPriv *event);
93 EmpathyEventManager *manager;
94 EventManagerApproval *approval;
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 EmpathyDispatchOperation *operation)
117 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
118 result->operation = g_object_ref (operation);
119 result->manager = manager;
125 event_manager_approval_free (EventManagerApproval *approval)
127 g_signal_handler_disconnect (approval->operation,
128 approval->approved_handler);
129 g_signal_handler_disconnect (approval->operation,
130 approval->claimed_handler);
131 g_signal_handler_disconnect (approval->operation,
132 approval->invalidated_handler);
133 g_object_unref (approval->operation);
135 if (approval->handler != 0)
136 g_signal_handler_disconnect (approval->handler_instance,
139 if (approval->contact != NULL)
140 g_object_unref (approval->contact);
142 if (approval->tube_dispatch != NULL)
143 g_object_unref (approval->tube_dispatch);
145 if (approval->dialog != NULL)
147 gtk_widget_destroy (approval->dialog);
150 g_slice_free (EventManagerApproval, approval);
153 static void event_remove (EventPriv *event);
156 event_free (EventPriv *event)
158 g_free (event->public.icon_name);
159 g_free (event->public.header);
160 g_free (event->public.message);
162 if (event->public.contact)
164 g_object_unref (event->public.contact);
167 g_slice_free (EventPriv, event);
171 event_remove (EventPriv *event)
173 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
175 DEBUG ("Removing event %p", event);
176 priv->events = g_slist_remove (priv->events, event);
177 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
182 autoremove_event_timeout_cb (EventPriv *event)
184 event_remove (event);
189 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
190 const gchar *icon_name, const gchar *header, const gchar *message,
191 EventManagerApproval *approval, EventFunc func, gpointer user_data)
193 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
196 event = g_slice_new0 (EventPriv);
197 event->public.contact = contact ? g_object_ref (contact) : NULL;
198 event->public.icon_name = g_strdup (icon_name);
199 event->public.header = g_strdup (header);
200 event->public.message = g_strdup (message);
201 event->public.must_ack = (func != NULL);
202 event->inhibit = FALSE;
204 event->user_data = user_data;
205 event->manager = manager;
206 event->approval = approval;
208 DEBUG ("Adding event %p", event);
209 priv->events = g_slist_prepend (priv->events, event);
210 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
212 if (!event->public.must_ack)
214 g_timeout_add_seconds (NOTIFICATION_TIMEOUT,
215 (GSourceFunc) autoremove_event_timeout_cb, event);
220 event_channel_process_func (EventPriv *event)
222 empathy_dispatch_operation_approve (event->approval->operation);
226 event_text_channel_process_func (EventPriv *event)
228 EmpathyTpChat *tp_chat;
230 if (event->approval->handler != 0)
232 tp_chat = EMPATHY_TP_CHAT
233 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
235 g_signal_handler_disconnect (tp_chat, event->approval->handler);
236 event->approval->handler = 0;
239 empathy_dispatch_operation_approve (event->approval->operation);
243 event_lookup_by_approval (EmpathyEventManager *manager,
244 EventManagerApproval *approval)
246 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
248 EventPriv *retval = NULL;
250 for (l = priv->events; l; l = l->next)
252 EventPriv *event = l->data;
254 if (event->approval == approval)
265 event_update (EmpathyEventManager *manager, EventPriv *event,
266 const char *icon_name, const char *header, const char *msg)
268 g_free (event->public.icon_name);
269 g_free (event->public.header);
270 g_free (event->public.message);
272 event->public.icon_name = g_strdup (icon_name);
273 event->public.header = g_strdup (header);
274 event->public.message = g_strdup (msg);
276 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
280 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
281 gint response, gpointer user_data)
283 EventManagerApproval *approval = user_data;
285 gtk_widget_destroy (approval->dialog);
286 approval->dialog = NULL;
288 if (response != GTK_RESPONSE_ACCEPT)
290 EmpathyTpCall *call =
292 empathy_dispatch_operation_get_channel_wrapper (
293 approval->operation));
296 if (empathy_dispatch_operation_claim (approval->operation))
297 empathy_tp_call_close (call);
298 g_object_unref (call);
303 EmpathyCallFactory *factory = empathy_call_factory_get ();
304 empathy_call_factory_claim_channel (factory, approval->operation);
309 event_channel_process_voip_func (EventPriv *event)
315 if (event->approval->dialog != NULL)
317 gtk_window_present (GTK_WINDOW (event->approval->dialog));
321 dialog = gtk_message_dialog_new (NULL, 0,
322 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
323 gtk_message_dialog_format_secondary_text (
324 GTK_MESSAGE_DIALOG (dialog),
325 _("%s is calling you, do you want to answer?"),
326 empathy_contact_get_name (event->approval->contact));
328 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
331 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
332 _("_Reject"), GTK_RESPONSE_REJECT);
333 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
334 GTK_ICON_SIZE_BUTTON);
335 gtk_button_set_image (GTK_BUTTON (button), image);
337 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
338 _("_Answer"), GTK_RESPONSE_ACCEPT);
340 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
341 gtk_button_set_image (GTK_BUTTON (button), image);
343 g_signal_connect (dialog, "response",
344 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
347 gtk_widget_show (dialog);
349 event->approval->dialog = dialog;
353 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
354 EmpathyMessage *message, EventManagerApproval *approval)
356 EmpathyContact *sender;
362 /* try to update the event if it's referring to a chat which is already in the
364 event = event_lookup_by_approval (approval->manager, approval);
366 if (event != NULL && event->inhibit && approval->handler != 0)
368 g_signal_handler_disconnect (tp_chat, approval->handler);
369 approval->handler = 0;
373 sender = empathy_message_get_sender (message);
374 header = empathy_contact_get_name (sender);
375 msg = empathy_message_get_body (message);
377 channel = empathy_tp_chat_get_channel (tp_chat);
380 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
382 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
383 msg, approval, event_text_channel_process_func, NULL);
385 empathy_sound_play (empathy_main_window_get (),
386 EMPATHY_SOUND_CONVERSATION_NEW);
390 event_manager_approval_done (EventManagerApproval *approval)
392 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
395 if (approval->operation != NULL)
399 channel_type = empathy_dispatch_operation_get_channel_type_id (
400 approval->operation);
401 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
404 if (priv->ringing == 0)
405 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
409 priv->approvals = g_slist_remove (priv->approvals, approval);
411 for (l = priv->events; l; l = l->next)
413 EventPriv *event = l->data;
415 if (event->approval == approval)
417 event_remove (event);
422 event_manager_approval_free (approval);
426 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
427 EventManagerApproval *approval)
429 event_manager_approval_done (approval);
433 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
434 EventManagerApproval *approval)
436 event_manager_approval_done (approval);
440 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
441 guint domain, gint code, gchar *message,
442 EventManagerApproval *approval)
444 event_manager_approval_done (approval);
448 event_manager_media_channel_got_contact (EventManagerApproval *approval)
450 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
453 header = g_strdup_printf (_("Incoming call from %s"),
454 empathy_contact_get_name (approval->contact));
456 event_manager_add (approval->manager,
457 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
458 approval, event_channel_process_voip_func, NULL);
463 if (priv->ringing == 1)
464 empathy_sound_start_playing (empathy_main_window_get (),
465 EMPATHY_SOUND_PHONE_INCOMING, MS_BETWEEN_RING);
469 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
470 GParamSpec *param, EventManagerApproval *approval)
472 EmpathyContact *contact;
474 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
479 approval->contact = contact;
480 event_manager_media_channel_got_contact (approval);
484 event_manager_tube_approved_cb (EventPriv *event)
486 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
490 event_manager_add_tube_approval (EventManagerApproval *approval,
491 EmpathyTubeDispatchAbility ability)
493 const gchar *icon_name;
497 header = g_strdup_printf (_("%s is offering you an invitation"),
498 empathy_contact_get_name (approval->contact));
500 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
502 icon_name = GTK_STOCK_EXECUTE;
503 msg = _("An external application will be started to handle it.");
507 icon_name = GTK_STOCK_DIALOG_ERROR;
508 msg = _("You don't have the needed external "
509 "application to handle it.");
512 event_manager_add (approval->manager, approval->contact, icon_name, header,
513 msg, approval, event_manager_tube_approved_cb, approval);
516 /* FIXME better sound for incoming tubes ? */
517 empathy_sound_play (empathy_main_window_get (),
518 EMPATHY_SOUND_CONVERSATION_NEW);
522 event_manager_tube_dispatch_ability_cb (GObject *object,
523 GParamSpec *spec, gpointer user_data)
525 EventManagerApproval *approval = (EventManagerApproval *) user_data;
526 EmpathyTubeDispatchAbility dispatchability;
529 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
531 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
533 event_manager_add_tube_approval (approval, dispatchability);
534 g_signal_handler_disconnect (object, approval->handler);
535 approval->handler = 0;
540 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
541 EmpathyContact *contact,
546 EventManagerApproval *approval = (EventManagerApproval *) user_data;
547 EmpathyTubeDispatchAbility dispatchability;
551 /* FIXME: We should probably still display the event */
552 DEBUG ("Error: %s", error->message);
556 approval->contact = g_object_ref (contact);
558 dispatchability = empathy_tube_dispatch_is_dispatchable
559 (approval->tube_dispatch);
561 switch (dispatchability)
563 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
564 approval->handler = g_signal_connect (approval->tube_dispatch,
565 "notify::dispatchability",
566 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
567 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
569 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
571 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
572 event_manager_add_tube_approval (approval, dispatchability);
578 invite_dialog_response_cb (GtkDialog *dialog,
580 EventManagerApproval *approval)
582 EmpathyTpChat *tp_chat;
584 TpHandle self_handle;
587 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
588 approval->dialog = NULL;
590 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
591 approval->operation));
593 if (response != GTK_RESPONSE_OK)
596 DEBUG ("Muc invitation rejected");
598 if (empathy_dispatch_operation_claim (approval->operation))
599 empathy_tp_chat_close (tp_chat);
603 DEBUG ("Muc invitation accepted");
606 channel = empathy_tp_chat_get_channel (tp_chat);
608 self_handle = tp_channel_group_get_self_handle (channel);
609 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
610 g_array_append_val (members, self_handle);
612 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
613 "", NULL, NULL, NULL, NULL);
615 empathy_dispatch_operation_approve (approval->operation);
617 g_array_free (members, TRUE);
621 event_room_channel_process_func (EventPriv *event)
623 GtkWidget *dialog, *button, *image;
624 TpChannel *channel = empathy_dispatch_operation_get_channel (
625 event->approval->operation);
627 if (event->approval->dialog != NULL)
629 gtk_window_present (GTK_WINDOW (event->approval->dialog));
634 dialog = gtk_message_dialog_new (NULL, 0,
635 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
637 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
638 _("%s is inviting you to join %s"),
639 empathy_contact_get_name (event->approval->contact),
640 tp_channel_get_identifier (channel));
642 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
645 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
646 _("_Decline"), GTK_RESPONSE_CANCEL);
647 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
648 gtk_button_set_image (GTK_BUTTON (button), image);
650 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
651 _("_Join"), GTK_RESPONSE_OK);
652 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
653 gtk_button_set_image (GTK_BUTTON (button), image);
655 g_signal_connect (dialog, "response",
656 G_CALLBACK (invite_dialog_response_cb), event->approval);
658 gtk_widget_show (dialog);
660 event->approval->dialog = dialog;
664 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
665 EmpathyContact *contact,
670 EventManagerApproval *approval = (EventManagerApproval *) user_data;
672 const gchar *invite_msg;
674 TpHandle self_handle;
678 /* FIXME: We should probably still display the event */
679 DEBUG ("Error: %s", error->message);
683 approval->contact = g_object_ref (contact);
684 channel = empathy_dispatch_operation_get_channel (approval->operation);
686 self_handle = tp_channel_group_get_self_handle (channel);
687 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
690 msg = g_strdup_printf (_("%s invited you to join %s"),
691 empathy_contact_get_name (approval->contact),
692 tp_channel_get_identifier (channel));
694 event_manager_add (approval->manager,
695 approval->contact, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
696 approval, event_room_channel_process_func, NULL);
698 empathy_sound_play (empathy_main_window_get (),
699 EMPATHY_SOUND_CONVERSATION_NEW);
705 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
706 EmpathyContact *contact,
711 EventManagerApproval *approval = (EventManagerApproval *) user_data;
714 approval->contact = contact;
716 header = g_strdup_printf (_("Incoming file transfer from %s"),
717 empathy_contact_get_name (approval->contact));
719 event_manager_add (approval->manager, approval->contact,
720 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL, approval,
721 event_channel_process_func, NULL);
723 /* FIXME better sound for incoming file transfers ?*/
724 empathy_sound_play (empathy_main_window_get (),
725 EMPATHY_SOUND_CONVERSATION_NEW);
731 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
732 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
734 const gchar *channel_type;
735 EventManagerApproval *approval;
736 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
738 channel_type = empathy_dispatch_operation_get_channel_type (operation);
740 approval = event_manager_approval_new (manager, operation);
741 priv->approvals = g_slist_prepend (priv->approvals, approval);
743 approval->approved_handler = g_signal_connect (operation, "approved",
744 G_CALLBACK (event_manager_operation_approved_cb), approval);
746 approval->claimed_handler = g_signal_connect (operation, "claimed",
747 G_CALLBACK (event_manager_operation_claimed_cb), approval);
749 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
750 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
752 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
754 EmpathyTpChat *tp_chat =
756 empathy_dispatch_operation_get_channel_wrapper (operation));
757 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
759 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
761 /* Are we in local-pending ? */
762 TpHandle self_handle, inviter;
764 self_handle = tp_channel_group_get_self_handle (channel);
766 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
767 channel, self_handle, &inviter, NULL, NULL))
769 /* We are invited to a room */
770 EmpathyTpContactFactory *factory;
771 TpConnection *connection;
773 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
774 tp_channel_get_identifier (channel));
776 connection = empathy_tp_chat_get_connection (tp_chat);
777 factory = empathy_tp_contact_factory_dup_singleton (connection);
779 empathy_tp_contact_factory_get_from_handle (factory,
780 inviter, event_manager_muc_invite_got_contact_cb,
781 approval, NULL, G_OBJECT (manager));
783 g_object_unref (factory);
787 /* if we are not invited, let's wait for the first message */
790 /* 1-1 text channel, wait for the first message */
791 approval->handler = g_signal_connect (tp_chat, "message-received",
792 G_CALLBACK (event_manager_chat_message_received_cb), approval);
793 approval->handler_instance = G_OBJECT (tp_chat);
795 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
797 EmpathyContact *contact;
798 EmpathyTpCall *call = EMPATHY_TP_CALL (
799 empathy_dispatch_operation_get_channel_wrapper (operation));
801 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
805 g_signal_connect (call, "notify::contact",
806 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
811 approval->contact = contact;
812 event_manager_media_channel_got_contact (approval);
816 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
819 TpConnection *connection;
821 EmpathyTpContactFactory *factory;
823 channel = empathy_dispatch_operation_get_channel (operation);
824 handle = tp_channel_get_handle (channel, NULL);
826 connection = tp_channel_borrow_connection (channel);
827 factory = empathy_tp_contact_factory_dup_singleton (connection);
828 empathy_tp_contact_factory_get_from_handle (factory, handle,
829 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
831 g_object_unref (factory);
833 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
834 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
838 TpHandleType handle_type;
839 TpConnection *connection;
840 EmpathyTpContactFactory *factory;
842 channel = empathy_dispatch_operation_get_channel (operation);
843 handle = tp_channel_get_handle (channel, &handle_type);
845 /* Only understand p2p tubes */
846 if (handle_type != TP_HANDLE_TYPE_CONTACT)
849 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
850 connection = tp_channel_borrow_connection (channel);
851 factory = empathy_tp_contact_factory_dup_singleton (connection);
852 empathy_tp_contact_factory_get_from_handle (factory, handle,
853 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
857 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
862 event_pending_subscribe_func (EventPriv *event)
864 empathy_subscription_dialog_show (event->public.contact, NULL);
865 event_remove (event);
869 event_manager_pendings_changed_cb (EmpathyContactList *list,
870 EmpathyContact *contact, EmpathyContact *actor,
871 guint reason, gchar *message, gboolean is_pending,
872 EmpathyEventManager *manager)
874 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
875 gchar *header, *event_msg;
881 for (l = priv->events; l; l = l->next)
883 EventPriv *event = l->data;
885 if (event->public.contact == contact &&
886 event->func == event_pending_subscribe_func)
888 event_remove (event);
896 header = g_strdup_printf (_("Subscription requested by %s"),
897 empathy_contact_get_name (contact));
899 if (!EMP_STR_EMPTY (message))
900 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
904 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
905 event_msg, NULL, event_pending_subscribe_func, NULL);
912 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
913 EmpathyContact *contact,
914 TpConnectionPresenceType current,
915 TpConnectionPresenceType previous,
916 EmpathyEventManager *manager)
918 EmpathyAccount *account;
919 gchar *header = NULL;
920 gboolean preference = FALSE;
922 account = empathy_contact_get_account (contact);
923 if (empathy_account_is_just_connected (account))
926 if (tp_connection_presence_type_cmp_availability (previous,
927 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
929 /* contact was online */
930 empathy_conf_get_bool (empathy_conf_get (),
931 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
932 if (preference && tp_connection_presence_type_cmp_availability (current,
933 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
935 /* someone is logging off */
936 header = g_strdup_printf (_("%s is now offline."),
937 empathy_contact_get_name (contact));
939 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
940 NULL, NULL, NULL, NULL);
945 /* contact was offline */
946 empathy_conf_get_bool (empathy_conf_get (),
947 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
948 if (preference && tp_connection_presence_type_cmp_availability (current,
949 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
951 /* someone is logging in */
952 header = g_strdup_printf (_("%s is now online."),
953 empathy_contact_get_name (contact));
955 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
956 NULL, NULL, NULL, NULL);
964 event_manager_constructor (GType type,
966 GObjectConstructParam *props)
970 if (manager_singleton) {
971 retval = g_object_ref (manager_singleton);
973 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
974 (type, n_props, props);
976 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
977 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
984 event_manager_finalize (GObject *object)
986 EmpathyEventManagerPriv *priv = GET_PRIV (object);
988 if (priv->ringing > 0)
989 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
991 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
992 g_slist_free (priv->events);
993 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
994 g_slist_free (priv->approvals);
995 g_object_unref (priv->contact_manager);
996 g_object_unref (priv->dispatcher);
1000 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1002 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1004 object_class->finalize = event_manager_finalize;
1005 object_class->constructor = event_manager_constructor;
1007 signals[EVENT_ADDED] =
1008 g_signal_new ("event-added",
1009 G_TYPE_FROM_CLASS (klass),
1013 g_cclosure_marshal_VOID__POINTER,
1017 signals[EVENT_REMOVED] =
1018 g_signal_new ("event-removed",
1019 G_TYPE_FROM_CLASS (klass),
1023 g_cclosure_marshal_VOID__POINTER,
1024 G_TYPE_NONE, 1, G_TYPE_POINTER);
1026 signals[EVENT_UPDATED] =
1027 g_signal_new ("event-updated",
1028 G_TYPE_FROM_CLASS (klass),
1032 g_cclosure_marshal_VOID__POINTER,
1033 G_TYPE_NONE, 1, G_TYPE_POINTER);
1036 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1040 empathy_event_manager_init (EmpathyEventManager *manager)
1042 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1043 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1044 EmpathyContactMonitor *monitor;
1045 EmpathyContactList *list_iface;
1047 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1048 monitor = empathy_contact_list_get_monitor (list_iface);
1049 g_object_unref (list_iface);
1051 manager->priv = priv;
1053 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1054 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1055 g_signal_connect (priv->dispatcher, "approve",
1056 G_CALLBACK (event_manager_approve_channel_cb), manager);
1057 g_signal_connect (priv->contact_manager, "pendings-changed",
1058 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1059 g_signal_connect (monitor, "contact-presence-changed",
1060 G_CALLBACK (event_manager_presence_changed_cb), manager);
1063 EmpathyEventManager *
1064 empathy_event_manager_dup_singleton (void)
1066 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1070 empathy_event_manager_get_events (EmpathyEventManager *manager)
1072 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1074 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1076 return priv->events;
1080 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1082 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1084 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1086 return priv->events ? priv->events->data : NULL;
1090 empathy_event_activate (EmpathyEvent *event_public)
1092 EventPriv *event = (EventPriv *) event_public;
1094 g_return_if_fail (event_public != NULL);
1097 event->func (event);
1099 event_remove (event);
1103 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1105 EventPriv *event = (EventPriv *) event_public;
1107 g_return_if_fail (event_public != NULL);
1109 event->inhibit = TRUE;