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-ui-utils.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 */
58 EmpathyEventManager *manager;
59 EmpathyDispatchOperation *operation;
60 gulong approved_handler;
61 gulong claimed_handler;
62 gulong invalidated_handler;
63 /* Remove contact if applicable */
64 EmpathyContact *contact;
65 /* Tube dispatcher if applicable */
66 EmpathyTubeDispatch *tube_dispatch;
67 /* option signal handler and it's instance */
69 GObject *handler_instance;
70 /* optional accept widget */
72 } EventManagerApproval;
75 EmpathyDispatcher *dispatcher;
76 EmpathyContactManager *contact_manager;
78 /* Ongoing approvals */
81 /* voip ringing sound */
84 } EmpathyEventManagerPriv;
86 typedef struct _EventPriv EventPriv;
87 typedef void (*EventFunc) (EventPriv *event);
91 EmpathyEventManager *manager;
92 EventManagerApproval *approval;
105 static guint signals[LAST_SIGNAL];
107 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
109 static EmpathyEventManager * manager_singleton = NULL;
111 static EventManagerApproval *
112 event_manager_approval_new (EmpathyEventManager *manager,
113 EmpathyDispatchOperation *operation)
115 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
116 result->operation = g_object_ref (operation);
117 result->manager = manager;
123 event_manager_approval_free (EventManagerApproval *approval)
125 g_signal_handler_disconnect (approval->operation,
126 approval->approved_handler);
127 g_signal_handler_disconnect (approval->operation,
128 approval->claimed_handler);
129 g_signal_handler_disconnect (approval->operation,
130 approval->invalidated_handler);
131 g_object_unref (approval->operation);
133 if (approval->handler != 0)
134 g_signal_handler_disconnect (approval->handler_instance,
137 if (approval->contact != NULL)
138 g_object_unref (approval->contact);
140 if (approval->tube_dispatch != NULL)
141 g_object_unref (approval->tube_dispatch);
143 if (approval->dialog != NULL)
145 gtk_widget_destroy (approval->dialog);
148 g_slice_free (EventManagerApproval, approval);
151 static void event_remove (EventPriv *event);
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->public.contact)
162 g_object_unref (event->public.contact);
165 g_slice_free (EventPriv, event);
168 static void event_manager_ringing_finished_cb (ca_context *c, guint id,
169 int error_code, gpointer user_data);
172 event_manager_ringing_timeout_cb (gpointer data)
174 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
175 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
177 priv->voip_timeout = 0;
179 empathy_sound_play_full (empathy_main_window_get (),
180 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
187 event_manager_ringing_idle_cb (gpointer data)
189 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
190 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
192 if (priv->ringing > 0)
193 priv->voip_timeout = g_timeout_add (500, event_manager_ringing_timeout_cb,
200 event_manager_ringing_finished_cb (ca_context *c, guint id, int error_code,
203 if (error_code == CA_ERROR_CANCELED)
206 g_idle_add (event_manager_ringing_idle_cb, user_data);
210 event_manager_start_ringing (EmpathyEventManager *manager)
212 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
216 if (priv->ringing == 1)
218 empathy_sound_play_full (empathy_main_window_get (),
219 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
225 event_manager_stop_ringing (EmpathyEventManager *manager)
227 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
231 if (priv->ringing > 0)
234 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
236 if (priv->voip_timeout != 0)
238 g_source_remove (priv->voip_timeout);
239 priv->voip_timeout = 0;
244 event_remove (EventPriv *event)
246 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
248 DEBUG ("Removing event %p", event);
249 priv->events = g_slist_remove (priv->events, event);
250 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
255 autoremove_event_timeout_cb (EventPriv *event)
257 event_remove (event);
262 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
263 const gchar *icon_name, const gchar *header, const gchar *message,
264 EventManagerApproval *approval, EventFunc func, gpointer user_data)
266 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
269 event = g_slice_new0 (EventPriv);
270 event->public.contact = contact ? g_object_ref (contact) : NULL;
271 event->public.icon_name = g_strdup (icon_name);
272 event->public.header = g_strdup (header);
273 event->public.message = g_strdup (message);
274 event->public.must_ack = (func != NULL);
275 event->inhibit = FALSE;
277 event->user_data = user_data;
278 event->manager = manager;
279 event->approval = approval;
281 DEBUG ("Adding event %p", event);
282 priv->events = g_slist_prepend (priv->events, event);
283 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
285 if (!event->public.must_ack)
287 g_timeout_add_seconds (NOTIFICATION_TIMEOUT,
288 (GSourceFunc) autoremove_event_timeout_cb, event);
293 event_channel_process_func (EventPriv *event)
295 empathy_dispatch_operation_approve (event->approval->operation);
299 event_text_channel_process_func (EventPriv *event)
301 EmpathyTpChat *tp_chat;
303 if (event->approval->handler != 0)
305 tp_chat = EMPATHY_TP_CHAT
306 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
308 g_signal_handler_disconnect (tp_chat, event->approval->handler);
309 event->approval->handler = 0;
312 empathy_dispatch_operation_approve (event->approval->operation);
316 event_lookup_by_approval (EmpathyEventManager *manager,
317 EventManagerApproval *approval)
319 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
321 EventPriv *retval = NULL;
323 for (l = priv->events; l; l = l->next)
325 EventPriv *event = l->data;
327 if (event->approval == approval)
338 event_update (EmpathyEventManager *manager, EventPriv *event,
339 const char *icon_name, const char *header, const char *msg)
341 g_free (event->public.icon_name);
342 g_free (event->public.header);
343 g_free (event->public.message);
345 event->public.icon_name = g_strdup (icon_name);
346 event->public.header = g_strdup (header);
347 event->public.message = g_strdup (msg);
349 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
353 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
354 gint response, gpointer user_data)
356 EventManagerApproval *approval = user_data;
358 gtk_widget_destroy (approval->dialog);
359 approval->dialog = NULL;
361 if (response != GTK_RESPONSE_ACCEPT)
363 EmpathyTpCall *call =
365 empathy_dispatch_operation_get_channel_wrapper (
366 approval->operation));
369 if (empathy_dispatch_operation_claim (approval->operation))
370 empathy_tp_call_close (call);
371 g_object_unref (call);
376 EmpathyCallFactory *factory = empathy_call_factory_get ();
377 empathy_call_factory_claim_channel (factory, approval->operation);
382 event_channel_process_voip_func (EventPriv *event)
388 if (event->approval->dialog != NULL)
390 gtk_window_present (GTK_WINDOW (event->approval->dialog));
394 dialog = gtk_message_dialog_new (NULL, 0,
395 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
396 gtk_message_dialog_format_secondary_text (
397 GTK_MESSAGE_DIALOG (dialog),
398 _("%s is calling you, do you want to answer?"),
399 empathy_contact_get_name (event->approval->contact));
401 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
404 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
405 _("_Reject"), GTK_RESPONSE_REJECT);
406 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
407 GTK_ICON_SIZE_BUTTON);
408 gtk_button_set_image (GTK_BUTTON (button), image);
410 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
411 _("_Answer"), GTK_RESPONSE_ACCEPT);
413 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
414 gtk_button_set_image (GTK_BUTTON (button), image);
416 g_signal_connect (dialog, "response",
417 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
420 gtk_widget_show (dialog);
422 event->approval->dialog = dialog;
426 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
427 EmpathyMessage *message, EventManagerApproval *approval)
429 EmpathyContact *sender;
435 /* try to update the event if it's referring to a chat which is already in the
437 event = event_lookup_by_approval (approval->manager, approval);
439 if (event != NULL && event->inhibit && approval->handler != 0)
441 g_signal_handler_disconnect (tp_chat, approval->handler);
442 approval->handler = 0;
446 sender = empathy_message_get_sender (message);
447 header = empathy_contact_get_name (sender);
448 msg = empathy_message_get_body (message);
450 channel = empathy_tp_chat_get_channel (tp_chat);
453 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
455 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
456 msg, approval, event_text_channel_process_func, NULL);
458 empathy_sound_play (empathy_main_window_get (),
459 EMPATHY_SOUND_CONVERSATION_NEW);
463 event_manager_approval_done (EventManagerApproval *approval)
465 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
468 if (approval->operation != NULL)
472 channel_type = empathy_dispatch_operation_get_channel_type_id (
473 approval->operation);
474 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
476 event_manager_stop_ringing (approval->manager);
480 priv->approvals = g_slist_remove (priv->approvals, approval);
482 for (l = priv->events; l; l = l->next)
484 EventPriv *event = l->data;
486 if (event->approval == approval)
488 event_remove (event);
493 event_manager_approval_free (approval);
497 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
498 EventManagerApproval *approval)
500 event_manager_approval_done (approval);
504 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
505 EventManagerApproval *approval)
507 event_manager_approval_done (approval);
511 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
512 guint domain, gint code, gchar *message,
513 EventManagerApproval *approval)
515 event_manager_approval_done (approval);
519 event_manager_media_channel_got_contact (EventManagerApproval *approval)
523 header = g_strdup_printf (_("Incoming call from %s"),
524 empathy_contact_get_name (approval->contact));
526 event_manager_add (approval->manager,
527 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
528 approval, event_channel_process_voip_func, NULL);
531 event_manager_start_ringing (approval->manager);
535 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
536 GParamSpec *param, EventManagerApproval *approval)
538 EmpathyContact *contact;
540 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
545 approval->contact = contact;
546 event_manager_media_channel_got_contact (approval);
550 event_manager_tube_approved_cb (EventPriv *event)
552 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
556 event_manager_add_tube_approval (EventManagerApproval *approval,
557 EmpathyTubeDispatchAbility ability)
559 const gchar *icon_name;
563 header = g_strdup_printf (_("%s is offering you an invitation"),
564 empathy_contact_get_name (approval->contact));
566 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
568 icon_name = GTK_STOCK_EXECUTE;
569 msg = _("An external application will be started to handle it.");
573 icon_name = GTK_STOCK_DIALOG_ERROR;
574 msg = _("You don't have the needed external "
575 "application to handle it.");
578 event_manager_add (approval->manager, approval->contact, icon_name, header,
579 msg, approval, event_manager_tube_approved_cb, approval);
582 /* FIXME better sound for incoming tubes ? */
583 empathy_sound_play (empathy_main_window_get (),
584 EMPATHY_SOUND_CONVERSATION_NEW);
588 event_manager_tube_dispatch_ability_cb (GObject *object,
589 GParamSpec *spec, gpointer user_data)
591 EventManagerApproval *approval = (EventManagerApproval *) user_data;
592 EmpathyTubeDispatchAbility dispatchability;
595 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
597 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
599 event_manager_add_tube_approval (approval, dispatchability);
600 g_signal_handler_disconnect (object, approval->handler);
601 approval->handler = 0;
606 event_manager_tube_got_contact_cb (EmpathyTpContactFactory *factory,
607 EmpathyContact *contact,
612 EventManagerApproval *approval = (EventManagerApproval *) user_data;
613 EmpathyTubeDispatchAbility dispatchability;
617 /* FIXME: We should probably still display the event */
618 DEBUG ("Error: %s", error->message);
622 approval->contact = g_object_ref (contact);
624 dispatchability = empathy_tube_dispatch_is_dispatchable
625 (approval->tube_dispatch);
627 switch (dispatchability)
629 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
630 approval->handler = g_signal_connect (approval->tube_dispatch,
631 "notify::dispatchability",
632 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
633 approval->handler_instance = G_OBJECT (approval->tube_dispatch);
635 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
637 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
638 event_manager_add_tube_approval (approval, dispatchability);
644 invite_dialog_response_cb (GtkDialog *dialog,
646 EventManagerApproval *approval)
648 EmpathyTpChat *tp_chat;
650 TpHandle self_handle;
653 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
654 approval->dialog = NULL;
656 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
657 approval->operation));
659 if (response != GTK_RESPONSE_OK)
662 DEBUG ("Muc invitation rejected");
664 if (empathy_dispatch_operation_claim (approval->operation))
665 empathy_tp_chat_close (tp_chat);
669 DEBUG ("Muc invitation accepted");
672 channel = empathy_tp_chat_get_channel (tp_chat);
674 self_handle = tp_channel_group_get_self_handle (channel);
675 members = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
676 g_array_append_val (members, self_handle);
678 tp_cli_channel_interface_group_call_add_members (channel, -1, members,
679 "", NULL, NULL, NULL, NULL);
681 empathy_dispatch_operation_approve (approval->operation);
683 g_array_free (members, TRUE);
687 event_room_channel_process_func (EventPriv *event)
689 GtkWidget *dialog, *button, *image;
690 TpChannel *channel = empathy_dispatch_operation_get_channel (
691 event->approval->operation);
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 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
704 _("%s is inviting you to join %s"),
705 empathy_contact_get_name (event->approval->contact),
706 tp_channel_get_identifier (channel));
708 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
711 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
712 _("_Decline"), GTK_RESPONSE_CANCEL);
713 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
714 gtk_button_set_image (GTK_BUTTON (button), image);
716 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
717 _("_Join"), GTK_RESPONSE_OK);
718 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
719 gtk_button_set_image (GTK_BUTTON (button), image);
721 g_signal_connect (dialog, "response",
722 G_CALLBACK (invite_dialog_response_cb), event->approval);
724 gtk_widget_show (dialog);
726 event->approval->dialog = dialog;
730 event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory,
731 EmpathyContact *contact,
736 EventManagerApproval *approval = (EventManagerApproval *) user_data;
738 const gchar *invite_msg;
740 TpHandle self_handle;
744 /* FIXME: We should probably still display the event */
745 DEBUG ("Error: %s", error->message);
749 approval->contact = g_object_ref (contact);
750 channel = empathy_dispatch_operation_get_channel (approval->operation);
752 self_handle = tp_channel_group_get_self_handle (channel);
753 tp_channel_group_get_local_pending_info (channel, self_handle, NULL, NULL,
756 msg = g_strdup_printf (_("%s invited you to join %s"),
757 empathy_contact_get_name (approval->contact),
758 tp_channel_get_identifier (channel));
760 event_manager_add (approval->manager,
761 approval->contact, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
762 approval, event_room_channel_process_func, NULL);
764 empathy_sound_play (empathy_main_window_get (),
765 EMPATHY_SOUND_CONVERSATION_NEW);
771 event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory,
772 EmpathyContact *contact,
777 EventManagerApproval *approval = (EventManagerApproval *) user_data;
780 approval->contact = contact;
782 header = g_strdup_printf (_("Incoming file transfer from %s"),
783 empathy_contact_get_name (approval->contact));
785 event_manager_add (approval->manager, approval->contact,
786 EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL, approval,
787 event_channel_process_func, NULL);
789 /* FIXME better sound for incoming file transfers ?*/
790 empathy_sound_play (empathy_main_window_get (),
791 EMPATHY_SOUND_CONVERSATION_NEW);
797 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
798 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
800 const gchar *channel_type;
801 EventManagerApproval *approval;
802 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
804 channel_type = empathy_dispatch_operation_get_channel_type (operation);
806 approval = event_manager_approval_new (manager, operation);
807 priv->approvals = g_slist_prepend (priv->approvals, approval);
809 approval->approved_handler = g_signal_connect (operation, "approved",
810 G_CALLBACK (event_manager_operation_approved_cb), approval);
812 approval->claimed_handler = g_signal_connect (operation, "claimed",
813 G_CALLBACK (event_manager_operation_claimed_cb), approval);
815 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
816 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
818 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
820 EmpathyTpChat *tp_chat =
822 empathy_dispatch_operation_get_channel_wrapper (operation));
823 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
825 if (tp_proxy_has_interface (channel, TP_IFACE_CHANNEL_INTERFACE_GROUP))
827 /* Are we in local-pending ? */
828 TpHandle self_handle, inviter;
830 self_handle = tp_channel_group_get_self_handle (channel);
832 if (self_handle != 0 && tp_channel_group_get_local_pending_info (
833 channel, self_handle, &inviter, NULL, NULL))
835 /* We are invited to a room */
836 EmpathyTpContactFactory *factory;
837 TpConnection *connection;
839 DEBUG ("Have been invited to %s. Ask user if he wants to accept",
840 tp_channel_get_identifier (channel));
842 connection = empathy_tp_chat_get_connection (tp_chat);
843 factory = empathy_tp_contact_factory_dup_singleton (connection);
845 empathy_tp_contact_factory_get_from_handle (factory,
846 inviter, event_manager_muc_invite_got_contact_cb,
847 approval, NULL, G_OBJECT (manager));
849 g_object_unref (factory);
853 /* if we are not invited, let's wait for the first message */
856 /* 1-1 text channel, wait for the first message */
857 approval->handler = g_signal_connect (tp_chat, "message-received",
858 G_CALLBACK (event_manager_chat_message_received_cb), approval);
859 approval->handler_instance = G_OBJECT (tp_chat);
861 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
863 EmpathyContact *contact;
864 EmpathyTpCall *call = EMPATHY_TP_CALL (
865 empathy_dispatch_operation_get_channel_wrapper (operation));
867 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
871 g_signal_connect (call, "notify::contact",
872 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
877 approval->contact = contact;
878 event_manager_media_channel_got_contact (approval);
882 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
885 TpConnection *connection;
887 EmpathyTpContactFactory *factory;
889 channel = empathy_dispatch_operation_get_channel (operation);
890 handle = tp_channel_get_handle (channel, NULL);
892 connection = tp_channel_borrow_connection (channel);
893 factory = empathy_tp_contact_factory_dup_singleton (connection);
894 empathy_tp_contact_factory_get_from_handle (factory, handle,
895 event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager));
897 g_object_unref (factory);
899 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
900 !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
904 TpHandleType handle_type;
905 TpConnection *connection;
906 EmpathyTpContactFactory *factory;
908 channel = empathy_dispatch_operation_get_channel (operation);
909 handle = tp_channel_get_handle (channel, &handle_type);
911 /* Only understand p2p tubes */
912 if (handle_type != TP_HANDLE_TYPE_CONTACT)
915 approval->tube_dispatch = empathy_tube_dispatch_new (operation);
916 connection = tp_channel_borrow_connection (channel);
917 factory = empathy_tp_contact_factory_dup_singleton (connection);
918 empathy_tp_contact_factory_get_from_handle (factory, handle,
919 event_manager_tube_got_contact_cb, approval, NULL, G_OBJECT (manager));
923 DEBUG ("Unknown channel type (%s), ignoring..", channel_type);
928 event_pending_subscribe_func (EventPriv *event)
930 empathy_subscription_dialog_show (event->public.contact, NULL);
931 event_remove (event);
935 event_manager_pendings_changed_cb (EmpathyContactList *list,
936 EmpathyContact *contact, EmpathyContact *actor,
937 guint reason, gchar *message, gboolean is_pending,
938 EmpathyEventManager *manager)
940 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
941 gchar *header, *event_msg;
947 for (l = priv->events; l; l = l->next)
949 EventPriv *event = l->data;
951 if (event->public.contact == contact &&
952 event->func == event_pending_subscribe_func)
954 event_remove (event);
962 header = g_strdup_printf (_("Subscription requested by %s"),
963 empathy_contact_get_name (contact));
965 if (!EMP_STR_EMPTY (message))
966 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
970 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
971 event_msg, NULL, event_pending_subscribe_func, NULL);
978 event_manager_presence_changed_cb (EmpathyContactMonitor *monitor,
979 EmpathyContact *contact,
980 TpConnectionPresenceType current,
981 TpConnectionPresenceType previous,
982 EmpathyEventManager *manager)
985 gboolean just_connected;
986 EmpathyAccountManager *account_manager;
987 gchar *header = NULL;
988 gboolean preference = FALSE;
990 account = empathy_contact_get_account (contact);
991 account_manager = empathy_account_manager_dup_singleton ();
992 just_connected = empathy_account_manager_is_account_just_connected (
993 account_manager, account);
995 g_object_unref (account_manager);
999 if (tp_connection_presence_type_cmp_availability (previous,
1000 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1002 /* contact was online */
1003 empathy_conf_get_bool (empathy_conf_get (),
1004 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT, &preference);
1005 if (preference && tp_connection_presence_type_cmp_availability (current,
1006 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
1008 /* someone is logging off */
1009 header = g_strdup_printf (_("%s signed out."),
1010 empathy_contact_get_name (contact));
1012 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
1013 NULL, NULL, NULL, NULL);
1018 /* contact was offline */
1019 empathy_conf_get_bool (empathy_conf_get (),
1020 EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN, &preference);
1021 if (preference && tp_connection_presence_type_cmp_availability (current,
1022 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
1024 /* someone is logging in */
1025 header = g_strdup_printf (_("%s signed in."),
1026 empathy_contact_get_name (contact));
1028 event_manager_add (manager, contact, GTK_STOCK_DIALOG_INFO, header,
1029 NULL, NULL, NULL, NULL);
1037 event_manager_constructor (GType type,
1039 GObjectConstructParam *props)
1043 if (manager_singleton) {
1044 retval = g_object_ref (manager_singleton);
1046 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
1047 (type, n_props, props);
1049 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
1050 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1057 event_manager_finalize (GObject *object)
1059 EmpathyEventManagerPriv *priv = GET_PRIV (object);
1061 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
1062 g_slist_free (priv->events);
1063 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
1064 g_slist_free (priv->approvals);
1065 g_object_unref (priv->contact_manager);
1066 g_object_unref (priv->dispatcher);
1070 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
1072 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1074 object_class->finalize = event_manager_finalize;
1075 object_class->constructor = event_manager_constructor;
1077 signals[EVENT_ADDED] =
1078 g_signal_new ("event-added",
1079 G_TYPE_FROM_CLASS (klass),
1083 g_cclosure_marshal_VOID__POINTER,
1087 signals[EVENT_REMOVED] =
1088 g_signal_new ("event-removed",
1089 G_TYPE_FROM_CLASS (klass),
1093 g_cclosure_marshal_VOID__POINTER,
1094 G_TYPE_NONE, 1, G_TYPE_POINTER);
1096 signals[EVENT_UPDATED] =
1097 g_signal_new ("event-updated",
1098 G_TYPE_FROM_CLASS (klass),
1102 g_cclosure_marshal_VOID__POINTER,
1103 G_TYPE_NONE, 1, G_TYPE_POINTER);
1106 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1110 empathy_event_manager_init (EmpathyEventManager *manager)
1112 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1113 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1114 EmpathyContactMonitor *monitor;
1115 EmpathyContactList *list_iface;
1117 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1118 monitor = empathy_contact_list_get_monitor (list_iface);
1119 g_object_unref (list_iface);
1121 manager->priv = priv;
1123 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1124 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1125 g_signal_connect (priv->dispatcher, "approve",
1126 G_CALLBACK (event_manager_approve_channel_cb), manager);
1127 g_signal_connect (priv->contact_manager, "pendings-changed",
1128 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1129 g_signal_connect (monitor, "contact-presence-changed",
1130 G_CALLBACK (event_manager_presence_changed_cb), manager);
1133 EmpathyEventManager *
1134 empathy_event_manager_dup_singleton (void)
1136 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1140 empathy_event_manager_get_events (EmpathyEventManager *manager)
1142 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1144 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1146 return priv->events;
1150 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1152 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1154 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1156 return priv->events ? priv->events->data : NULL;
1160 empathy_event_activate (EmpathyEvent *event_public)
1162 EventPriv *event = (EventPriv *) event_public;
1164 g_return_if_fail (event_public != NULL);
1167 event->func (event);
1169 event_remove (event);
1173 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1175 EventPriv *event = (EventPriv *) event_public;
1177 g_return_if_fail (event_public != NULL);
1179 event->inhibit = TRUE;