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-dispatcher.h>
30 #include <libempathy/empathy-contact-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-tp-chat.h>
33 #include <libempathy/empathy-tp-call.h>
34 #include <libempathy/empathy-tp-group.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-call-factory.h>
38 #include <extensions/extensions.h>
40 #include <libempathy-gtk/empathy-images.h>
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include "empathy-event-manager.h"
45 #include "empathy-main-window.h"
46 #include "empathy-tube-dispatch.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
49 #include <libempathy/empathy-debug.h>
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
54 EmpathyEventManager *manager;
55 EmpathyDispatchOperation *operation;
56 gulong approved_handler;
57 gulong claimed_handler;
58 gulong invalidated_handler;
59 /* Remove contact if applicable */
60 EmpathyContact *contact;
61 /* Tube dispatcher if applicable */
62 EmpathyTubeDispatch *tube_dispatch;
63 /* option signal handler */
65 /* optional accept widget */
67 } EventManagerApproval;
70 EmpathyDispatcher *dispatcher;
71 EmpathyContactManager *contact_manager;
73 /* Ongoing approvals */
76 /* voip ringing sound */
79 } EmpathyEventManagerPriv;
81 typedef struct _EventPriv EventPriv;
82 typedef void (*EventFunc) (EventPriv *event);
86 EmpathyEventManager *manager;
87 EventManagerApproval *approval;
100 static guint signals[LAST_SIGNAL];
102 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
104 static EmpathyEventManager * manager_singleton = NULL;
106 static EventManagerApproval *
107 event_manager_approval_new (EmpathyEventManager *manager,
108 EmpathyDispatchOperation *operation)
110 EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
111 result->operation = g_object_ref (operation);
112 result->manager = manager;
118 event_manager_approval_free (EventManagerApproval *approval)
120 g_signal_handler_disconnect (approval->operation,
121 approval->approved_handler);
122 g_signal_handler_disconnect (approval->operation,
123 approval->claimed_handler);
124 g_signal_handler_disconnect (approval->operation,
125 approval->invalidated_handler);
126 g_object_unref (approval->operation);
128 if (approval->contact != NULL)
129 g_object_unref (approval->contact);
131 if (approval->tube_dispatch != NULL)
132 g_object_unref (approval->tube_dispatch);
134 if (approval->dialog != NULL)
136 gtk_widget_destroy (approval->dialog);
139 g_slice_free (EventManagerApproval, approval);
142 static void event_remove (EventPriv *event);
145 event_free (EventPriv *event)
147 g_free (event->public.icon_name);
148 g_free (event->public.header);
149 g_free (event->public.message);
151 if (event->public.contact)
153 g_object_unref (event->public.contact);
156 g_slice_free (EventPriv, event);
159 static void event_manager_ringing_finished_cb (ca_context *c, guint id,
160 int error_code, gpointer user_data);
163 event_manager_ringing_timeout_cb (gpointer data)
165 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
166 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
168 priv->voip_timeout = 0;
170 empathy_sound_play_full (empathy_main_window_get (),
171 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
178 event_manager_ringing_idle_cb (gpointer data)
180 EmpathyEventManager *manager = EMPATHY_EVENT_MANAGER (data);
181 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
183 if (priv->ringing > 0)
184 priv->voip_timeout = g_timeout_add (500, event_manager_ringing_timeout_cb,
191 event_manager_ringing_finished_cb (ca_context *c, guint id, int error_code,
194 if (error_code == CA_ERROR_CANCELED)
197 g_idle_add (event_manager_ringing_idle_cb, user_data);
201 event_manager_start_ringing (EmpathyEventManager *manager)
203 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
207 if (priv->ringing == 1)
209 empathy_sound_play_full (empathy_main_window_get (),
210 EMPATHY_SOUND_PHONE_INCOMING, event_manager_ringing_finished_cb,
216 event_manager_stop_ringing (EmpathyEventManager *manager)
218 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
222 if (priv->ringing > 0)
225 empathy_sound_stop (EMPATHY_SOUND_PHONE_INCOMING);
227 if (priv->voip_timeout != 0)
229 g_source_remove (priv->voip_timeout);
230 priv->voip_timeout = 0;
235 event_remove (EventPriv *event)
237 EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
239 DEBUG ("Removing event %p", event);
240 priv->events = g_slist_remove (priv->events, event);
241 g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
246 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
247 const gchar *icon_name, const gchar *header, const gchar *message,
248 EventManagerApproval *approval, EventFunc func, gpointer user_data)
250 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
253 event = g_slice_new0 (EventPriv);
254 event->public.contact = contact ? g_object_ref (contact) : NULL;
255 event->public.icon_name = g_strdup (icon_name);
256 event->public.header = g_strdup (header);
257 event->public.message = g_strdup (message);
258 event->inhibit = FALSE;
260 event->user_data = user_data;
261 event->manager = manager;
262 event->approval = approval;
264 DEBUG ("Adding event %p", event);
265 priv->events = g_slist_prepend (priv->events, event);
266 g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
270 event_channel_process_func (EventPriv *event)
272 empathy_dispatch_operation_approve (event->approval->operation);
276 event_text_channel_process_func (EventPriv *event)
278 EmpathyTpChat *tp_chat;
280 if (event->approval->handler != 0)
282 tp_chat = EMPATHY_TP_CHAT
283 (empathy_dispatch_operation_get_channel_wrapper (event->approval->operation));
285 g_signal_handler_disconnect (tp_chat, event->approval->handler);
286 event->approval->handler = 0;
289 empathy_dispatch_operation_approve (event->approval->operation);
293 event_lookup_by_approval (EmpathyEventManager *manager,
294 EventManagerApproval *approval)
296 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
298 EventPriv *retval = NULL;
300 for (l = priv->events; l; l = l->next)
302 EventPriv *event = l->data;
304 if (event->approval == approval)
315 event_update (EmpathyEventManager *manager, EventPriv *event,
316 const char *icon_name, const char *header, const char *msg)
318 g_free (event->public.icon_name);
319 g_free (event->public.header);
320 g_free (event->public.message);
322 event->public.icon_name = g_strdup (icon_name);
323 event->public.header = g_strdup (header);
324 event->public.message = g_strdup (msg);
326 g_signal_emit (manager, signals[EVENT_UPDATED], 0, event);
330 event_manager_call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
331 gint response, gpointer user_data)
333 EventManagerApproval *approval = user_data;
335 gtk_widget_destroy (approval->dialog);
336 approval->dialog = NULL;
338 if (response != GTK_RESPONSE_ACCEPT)
340 EmpathyTpCall *call =
342 empathy_dispatch_operation_get_channel_wrapper (
343 approval->operation));
346 if (empathy_dispatch_operation_claim (approval->operation))
347 empathy_tp_call_close (call);
348 g_object_unref (call);
353 EmpathyCallFactory *factory = empathy_call_factory_get ();
354 empathy_call_factory_claim_channel (factory, approval->operation);
359 event_channel_process_voip_func (EventPriv *event)
365 if (event->approval->dialog != NULL)
367 gtk_window_present (GTK_WINDOW (event->approval->dialog));
371 dialog = gtk_message_dialog_new (GTK_WINDOW (empathy_main_window_get()),
372 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
373 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
374 gtk_message_dialog_format_secondary_text (
375 GTK_MESSAGE_DIALOG (dialog),
376 _("%s is calling you, do you want to answer?"),
377 empathy_contact_get_name (event->approval->contact));
379 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
382 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
383 _("_Reject"), GTK_RESPONSE_REJECT);
384 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL,
385 GTK_ICON_SIZE_BUTTON);
386 gtk_button_set_image (GTK_BUTTON (button), image);
388 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
389 _("_Answer"), GTK_RESPONSE_ACCEPT);
391 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
392 gtk_button_set_image (GTK_BUTTON (button), image);
394 g_signal_connect (dialog, "response",
395 G_CALLBACK (event_manager_call_window_confirmation_dialog_response_cb),
398 gtk_widget_show (dialog);
400 event->approval->dialog = dialog;
404 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
405 EmpathyMessage *message, EventManagerApproval *approval)
407 EmpathyContact *sender;
413 /* try to update the event if it's referring to a chat which is already in the
415 event = event_lookup_by_approval (approval->manager, approval);
417 if (event != NULL && event->inhibit && approval->handler != 0)
419 g_signal_handler_disconnect (tp_chat, approval->handler);
420 approval->handler = 0;
424 sender = empathy_message_get_sender (message);
425 header = g_strdup_printf (_("New message from %s"),
426 empathy_contact_get_name (sender));
427 msg = empathy_message_get_body (message);
429 channel = empathy_tp_chat_get_channel (tp_chat);
432 event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg);
434 event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header,
435 msg, approval, event_text_channel_process_func, NULL);
438 empathy_sound_play (empathy_main_window_get (),
439 EMPATHY_SOUND_CONVERSATION_NEW);
443 event_manager_approval_done (EventManagerApproval *approval)
445 EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
448 if (approval->operation != NULL)
452 channel_type = empathy_dispatch_operation_get_channel_type_id (
453 approval->operation);
454 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
456 event_manager_stop_ringing (approval->manager);
460 priv->approvals = g_slist_remove (priv->approvals, approval);
462 for (l = priv->events; l; l = l->next)
464 EventPriv *event = l->data;
466 if (event->approval == approval)
468 event_remove (event);
473 event_manager_approval_free (approval);
477 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
478 EventManagerApproval *approval)
480 event_manager_approval_done (approval);
484 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
485 EventManagerApproval *approval)
487 event_manager_approval_done (approval);
491 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
492 guint domain, gint code, gchar *message,
493 EventManagerApproval *approval)
495 event_manager_approval_done (approval);
499 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
500 const GError *error, gpointer user_data, GObject *object)
502 EventManagerApproval *approval = user_data;
507 /* FIXME just returning assuming the operation will be invalidated as
512 header = g_strdup_printf (_("Incoming call from %s"),
513 empathy_contact_get_name (contact));
515 event_manager_add (approval->manager,
516 approval->contact, EMPATHY_IMAGE_VOIP, header, NULL,
517 approval, event_channel_process_voip_func, NULL);
520 event_manager_start_ringing (approval->manager);
524 event_manager_media_channel_got_contact (EventManagerApproval *approval)
526 empathy_contact_call_when_ready (approval->contact,
527 EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
528 approval, NULL, G_OBJECT (approval->manager));
532 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
533 GParamSpec *param, EventManagerApproval *approval)
535 EmpathyContact *contact;
537 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
542 approval->contact = contact;
543 event_manager_media_channel_got_contact (approval);
547 event_manager_tube_approved_cb (EventPriv *event)
549 empathy_tube_dispatch_handle (event->approval->tube_dispatch);
553 event_manager_add_tube_approval (EventManagerApproval *approval,
554 EmpathyTubeDispatchAbility ability)
556 const gchar *icon_name;
560 header = g_strdup_printf (_("%s is offering you an invitation"),
561 empathy_contact_get_name (approval->contact));
563 if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
565 icon_name = GTK_STOCK_EXECUTE;
566 msg = _("An external application will be started to handle it.");
570 icon_name = GTK_STOCK_DIALOG_ERROR;
571 msg = _("You don't have the needed external "
572 "application to handle it.");
575 event_manager_add (approval->manager, approval->contact, icon_name, header,
576 msg, approval, event_manager_tube_approved_cb, approval);
579 /* FIXME better sound for incoming tubes ? */
580 empathy_sound_play (empathy_main_window_get (),
581 EMPATHY_SOUND_CONVERSATION_NEW);
585 event_manager_tube_dispatch_ability_cb (GObject *object,
586 GParamSpec *spec, gpointer user_data)
588 EventManagerApproval *approval = (EventManagerApproval *)user_data;
589 EmpathyTubeDispatchAbility dispatchability;
592 empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
594 if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
596 event_manager_add_tube_approval (approval, dispatchability);
597 g_signal_handler_disconnect (object, approval->handler);
598 approval->handler = 0;
603 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
604 const GError *error, gpointer user_data, GObject *object)
606 EventManagerApproval *approval = (EventManagerApproval *)user_data;
607 EmpathyTubeDispatchAbility dispatchability;
611 /* FIXME?, we assume that the operation gets invalidated as well (if it
616 dispatchability = empathy_tube_dispatch_is_dispatchable
617 (approval->tube_dispatch);
620 switch (dispatchability)
622 case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
623 approval->handler = g_signal_connect (approval->tube_dispatch,
624 "notify::dispatchability",
625 G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
627 case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
629 case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
630 event_manager_add_tube_approval (approval, dispatchability);
636 invite_dialog_response_cb (GtkDialog *dialog,
638 EventManagerApproval *approval)
640 EmpathyTpChat *tp_chat;
642 EmpathyTpGroup *group;
643 EmpathyContact *self_contact;
645 gtk_widget_destroy (GTK_WIDGET (approval->dialog));
646 approval->dialog = NULL;
648 tp_chat = EMPATHY_TP_CHAT (empathy_dispatch_operation_get_channel_wrapper (
649 approval->operation));
651 if (response != GTK_RESPONSE_OK)
654 DEBUG ("Muc invitation rejected");
656 if (empathy_dispatch_operation_claim (approval->operation))
657 empathy_tp_chat_close (tp_chat);
658 empathy_tp_chat_close (tp_chat);
662 DEBUG ("Muc invitation accepted");
665 channel = empathy_tp_chat_get_channel (tp_chat);
667 group = empathy_tp_group_new (channel);
668 empathy_run_until_ready (group);
670 self_contact = empathy_tp_group_get_self_contact (group);
671 empathy_tp_group_add_member (group, self_contact, NULL);
673 empathy_dispatch_operation_approve (approval->operation);
675 g_object_unref (group);
676 g_object_unref (self_contact);
680 event_room_channel_process_func (EventPriv *event)
682 GtkWidget *dialog, *button, *image;
683 TpChannel *channel = empathy_dispatch_operation_get_channel (
684 event->approval->operation);
686 if (event->approval->dialog != NULL)
688 gtk_window_present (GTK_WINDOW (event->approval->dialog));
693 dialog = gtk_message_dialog_new (NULL, 0,
694 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Room invitation"));
696 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
697 _("%s is inviting you to join %s"),
698 empathy_contact_get_name (event->approval->contact),
699 tp_channel_get_identifier (channel));
701 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
704 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
705 _("_Decline"), GTK_RESPONSE_CANCEL);
706 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
707 gtk_button_set_image (GTK_BUTTON (button), image);
709 button = gtk_dialog_add_button (GTK_DIALOG (dialog),
710 _("_Join"), GTK_RESPONSE_OK);
711 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
712 gtk_button_set_image (GTK_BUTTON (button), image);
714 g_signal_connect (dialog, "response",
715 G_CALLBACK (invite_dialog_response_cb), event->approval);
717 gtk_widget_show (dialog);
719 event->approval->dialog = dialog;
723 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
724 EmpathyDispatchOperation *operation, EmpathyEventManager *manager)
726 const gchar *channel_type;
727 EventManagerApproval *approval;
728 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
730 channel_type = empathy_dispatch_operation_get_channel_type (operation);
732 approval = event_manager_approval_new (manager, operation);
733 priv->approvals = g_slist_prepend (priv->approvals, approval);
735 approval->approved_handler = g_signal_connect (operation, "approved",
736 G_CALLBACK (event_manager_operation_approved_cb), approval);
738 approval->claimed_handler = g_signal_connect (operation, "claimed",
739 G_CALLBACK (event_manager_operation_claimed_cb), approval);
741 approval->invalidated_handler = g_signal_connect (operation, "invalidated",
742 G_CALLBACK (event_manager_operation_invalidated_cb), approval);
744 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
746 EmpathyTpChat *tp_chat =
748 empathy_dispatch_operation_get_channel_wrapper (operation));
749 TpChannel *channel = empathy_tp_chat_get_channel (tp_chat);
751 TpHandleType handle_type;
753 handle = tp_channel_get_handle (channel, &handle_type);
755 if (handle_type == TP_HANDLE_TYPE_CONTACT)
757 /* 1-1 text channel, wait for the first message */
758 approval->handler = g_signal_connect (tp_chat, "message-received",
759 G_CALLBACK (event_manager_chat_message_received_cb), approval);
761 else if (handle_type == TP_HANDLE_TYPE_ROOM)
764 const gchar *invite_msg;
765 TpHandle self_handle, inviter;
766 EmpathyContactFactory *contact_factory;
769 self_handle = tp_channel_group_get_self_handle (channel);
771 if (self_handle == 0 || !tp_channel_group_get_local_pending_info (
772 channel, self_handle, &inviter, NULL, &invite_msg))
774 DEBUG ("can't handle a incoming muc to which we have not been "
777 if (empathy_dispatch_operation_claim (approval->operation))
778 empathy_tp_chat_close (tp_chat);
782 account = empathy_tp_chat_get_account (tp_chat);
783 contact_factory = empathy_contact_factory_dup_singleton ();
785 approval->contact = empathy_contact_factory_get_from_handle (
786 contact_factory, account, inviter);
788 /* We are invited to a room */
789 msg = g_strdup_printf ("%s invited you to join %s",
790 empathy_contact_get_name (approval->contact),
791 tp_channel_get_identifier (channel));
793 event_manager_add (approval->manager,
794 approval->contact, EMPATHY_IMAGE_GROUP_MESSAGE, msg, invite_msg,
795 approval, event_room_channel_process_func, NULL);
797 empathy_sound_play (empathy_main_window_get (),
798 EMPATHY_SOUND_CONVERSATION_NEW);
800 g_object_unref (contact_factory);
801 g_object_unref (account);
804 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
806 EmpathyContact *contact;
807 EmpathyTpCall *call = EMPATHY_TP_CALL (
808 empathy_dispatch_operation_get_channel_wrapper (operation));
810 g_object_get (G_OBJECT (call), "contact", &contact, NULL);
814 g_signal_connect (call, "notify::contact",
815 G_CALLBACK (event_manager_media_channel_contact_changed_cb),
820 approval->contact = contact;
821 event_manager_media_channel_got_contact (approval);
825 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
827 EmpathyContact *contact;
831 EmpathyContactFactory *factory;
832 TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
834 factory = empathy_contact_factory_dup_singleton ();
835 handle = tp_channel_get_handle (channel, NULL);
836 account = empathy_channel_get_account (channel);
838 contact = empathy_contact_factory_get_from_handle (factory, account,
841 empathy_contact_run_until_ready (contact,
842 EMPATHY_CONTACT_READY_NAME, NULL);
844 header = g_strdup_printf (_("Incoming file transfer from %s"),
845 empathy_contact_get_name (contact));
847 event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
848 header, NULL, approval, event_channel_process_func, NULL);
850 /* FIXME better sound for incoming file transfers ?*/
851 empathy_sound_play (empathy_main_window_get (),
852 EMPATHY_SOUND_CONVERSATION_NEW);
854 g_object_unref (factory);
855 g_object_unref (account);
858 else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
859 !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
861 EmpathyContact *contact;
863 TpHandleType handle_type;
865 EmpathyContactFactory *factory;
866 EmpathyTubeDispatch *tube_dispatch;
869 channel = empathy_dispatch_operation_get_channel (operation);
871 handle = tp_channel_get_handle (channel, &handle_type);
873 /* Only understand p2p tubes */
874 if (handle_type != TP_HANDLE_TYPE_CONTACT)
877 factory = empathy_contact_factory_dup_singleton ();
878 account = empathy_channel_get_account (channel);
880 contact = empathy_contact_factory_get_from_handle (factory, account,
883 tube_dispatch = empathy_tube_dispatch_new (operation);
885 approval->contact = contact;
886 approval->tube_dispatch = tube_dispatch;
888 empathy_contact_call_when_ready (contact,
889 EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
890 approval, NULL, G_OBJECT (manager));
892 g_object_unref (factory);
893 g_object_unref (account);
897 DEBUG ("Unknown channel type, ignoring..");
902 event_pending_subscribe_func (EventPriv *event)
904 empathy_subscription_dialog_show (event->public.contact, NULL);
905 event_remove (event);
909 event_manager_pendings_changed_cb (EmpathyContactList *list,
910 EmpathyContact *contact, EmpathyContact *actor,
911 guint reason, gchar *message, gboolean is_pending,
912 EmpathyEventManager *manager)
914 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
915 gchar *header, *event_msg;
921 for (l = priv->events; l; l = l->next)
923 EventPriv *event = l->data;
925 if (event->public.contact == contact &&
926 event->func == event_pending_subscribe_func)
928 event_remove (event);
936 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
938 header = g_strdup_printf (_("Subscription requested by %s"),
939 empathy_contact_get_name (contact));
941 if (!EMP_STR_EMPTY (message))
942 event_msg = g_strdup_printf (_("\nMessage: %s"), message);
946 event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, header,
947 event_msg, NULL, event_pending_subscribe_func, NULL);
954 event_manager_constructor (GType type,
956 GObjectConstructParam *props)
960 if (manager_singleton) {
961 retval = g_object_ref (manager_singleton);
963 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
964 (type, n_props, props);
966 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
967 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
974 event_manager_finalize (GObject *object)
976 EmpathyEventManagerPriv *priv = GET_PRIV (object);
978 g_slist_foreach (priv->events, (GFunc) event_free, NULL);
979 g_slist_free (priv->events);
980 g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
981 g_slist_free (priv->approvals);
982 g_object_unref (priv->contact_manager);
983 g_object_unref (priv->dispatcher);
987 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
989 GObjectClass *object_class = G_OBJECT_CLASS (klass);
991 object_class->finalize = event_manager_finalize;
992 object_class->constructor = event_manager_constructor;
994 signals[EVENT_ADDED] =
995 g_signal_new ("event-added",
996 G_TYPE_FROM_CLASS (klass),
1000 g_cclosure_marshal_VOID__POINTER,
1004 signals[EVENT_REMOVED] =
1005 g_signal_new ("event-removed",
1006 G_TYPE_FROM_CLASS (klass),
1010 g_cclosure_marshal_VOID__POINTER,
1011 G_TYPE_NONE, 1, G_TYPE_POINTER);
1013 signals[EVENT_UPDATED] =
1014 g_signal_new ("event-updated",
1015 G_TYPE_FROM_CLASS (klass),
1019 g_cclosure_marshal_VOID__POINTER,
1020 G_TYPE_NONE, 1, G_TYPE_POINTER);
1023 g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
1027 empathy_event_manager_init (EmpathyEventManager *manager)
1029 EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
1030 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
1032 manager->priv = priv;
1034 priv->dispatcher = empathy_dispatcher_dup_singleton ();
1035 priv->contact_manager = empathy_contact_manager_dup_singleton ();
1036 g_signal_connect (priv->dispatcher, "approve",
1037 G_CALLBACK (event_manager_approve_channel_cb), manager);
1038 g_signal_connect (priv->contact_manager, "pendings-changed",
1039 G_CALLBACK (event_manager_pendings_changed_cb), manager);
1042 EmpathyEventManager *
1043 empathy_event_manager_dup_singleton (void)
1045 return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
1049 empathy_event_manager_get_events (EmpathyEventManager *manager)
1051 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1053 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1055 return priv->events;
1059 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
1061 EmpathyEventManagerPriv *priv = GET_PRIV (manager);
1063 g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
1065 return priv->events ? priv->events->data : NULL;
1069 empathy_event_activate (EmpathyEvent *event_public)
1071 EventPriv *event = (EventPriv*) event_public;
1073 g_return_if_fail (event_public != NULL);
1076 event->func (event);
1078 event_remove (event);
1082 empathy_event_inhibit_updates (EmpathyEvent *event_public)
1084 EventPriv *event = (EventPriv *) event_public;
1086 g_return_if_fail (event_public != NULL);
1088 event->inhibit = TRUE;