1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n.h>
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/channel.h>
30 #include <telepathy-glib/connection.h>
31 #include <telepathy-glib/util.h>
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/proxy-subclass.h>
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
38 #include <extensions/extensions.h>
40 #include <libempathy/empathy-tp-chat.h>
41 #include <libempathy/empathy-tp-call.h>
42 #include <libempathy/empathy-tp-group.h>
43 #include <libempathy/empathy-utils.h>
44 #include <libempathy/empathy-debug.h>
45 #include <libempathy/empathy-tube-handler.h>
47 #include <libempathy-gtk/empathy-chat.h>
48 #include <libempathy-gtk/empathy-images.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
51 #include "empathy-filter.h"
52 #include "empathy-chat-window.h"
53 #include "empathy-call-window.h"
55 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
56 EMPATHY_TYPE_FILTER, EmpathyFilterPriv))
58 #define DEBUG_DOMAIN "Filter"
60 struct _EmpathyFilterPriv {
68 static void empathy_filter_class_init (EmpathyFilterClass *klass);
69 static void empathy_filter_init (EmpathyFilter *filter);
71 G_DEFINE_TYPE (EmpathyFilter, empathy_filter, G_TYPE_OBJECT);
78 typedef void (*FilterFunc) (EmpathyFilter *filter,
82 EmpathyFilterEvent public;
85 } EmpathyFilterEventExt;
88 filter_channel_hash (gconstpointer key)
90 TpProxy *channel = TP_PROXY (key);
92 return g_str_hash (channel->object_path);
96 filter_channel_equal (gconstpointer a,
99 TpProxy *channel_a = TP_PROXY (a);
100 TpProxy *channel_b = TP_PROXY (b);
102 return g_str_equal (channel_a->object_path, channel_b->object_path);
106 filter_event_free (EmpathyFilterEventExt *event)
108 g_free (event->public.icon_name);
109 g_free (event->public.message);
110 g_slice_free (EmpathyFilterEventExt, event);
114 filter_emit_event (EmpathyFilter *filter,
115 const gchar *icon_name,
116 const gchar *message,
120 EmpathyFilterPriv *priv = GET_PRIV (filter);
121 EmpathyFilterEventExt *event;
123 empathy_debug (DEBUG_DOMAIN, "Emit event, icon_name=%s message='%s'",
126 event = g_slice_new0 (EmpathyFilterEventExt);
128 event->user_data = user_data;
129 event->public.icon_name = g_strdup (icon_name);
130 event->public.message = g_strdup (message);
132 priv->events = g_slist_append (priv->events, event);
133 if (priv->events->data == event) {
134 g_object_notify (G_OBJECT (filter), "top-event");
139 empathy_filter_activate_event (EmpathyFilter *filter,
140 EmpathyFilterEvent *event)
142 EmpathyFilterPriv *priv = GET_PRIV (filter);
143 EmpathyFilterEventExt *event_ext;
147 g_return_if_fail (EMPATHY_IS_FILTER (filter));
148 g_return_if_fail (event != NULL);
150 if (!(l = g_slist_find (priv->events, event))) {
154 empathy_debug (DEBUG_DOMAIN, "Activating event");
156 event_ext = (EmpathyFilterEventExt*) event;
157 if (event_ext->func) {
158 event_ext->func (filter, event_ext->user_data);
161 is_top = (l == priv->events);
162 priv->events = g_slist_delete_link (priv->events, l);
164 g_object_notify (G_OBJECT (filter), "top-event");
167 filter_event_free (event_ext);
171 empathy_filter_get_top_event (EmpathyFilter *filter)
173 EmpathyFilterPriv *priv = GET_PRIV (filter);
175 g_return_val_if_fail (EMPATHY_IS_FILTER (filter), NULL);
177 return priv->events ? priv->events->data : NULL;
181 filter_chat_dispatch (EmpathyFilter *filter,
184 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (user_data);
189 id = empathy_tp_chat_get_id (tp_chat);
190 account = empathy_tp_chat_get_account (tp_chat);
191 chat = empathy_chat_window_find_chat (account, id);
194 empathy_chat_set_tp_chat (chat, tp_chat);
196 chat = empathy_chat_new (tp_chat);
199 empathy_chat_window_present_chat (chat);
200 g_object_unref (tp_chat);
204 filter_chat_message_received_cb (EmpathyTpChat *tp_chat,
205 EmpathyMessage *message,
206 EmpathyFilter *filter)
208 EmpathyContact *sender;
211 g_signal_handlers_disconnect_by_func (tp_chat,
212 filter_chat_message_received_cb,
215 sender = empathy_message_get_sender (message);
216 msg = g_strdup_printf (_("New message from %s:\n%s"),
217 empathy_contact_get_name (sender),
218 empathy_message_get_body (message));
220 filter_emit_event (filter, EMPATHY_IMAGE_NEW_MESSAGE, msg,
221 filter_chat_dispatch, tp_chat);
227 filter_chat_handle_channel (EmpathyFilter *filter,
229 gboolean is_incoming)
231 EmpathyTpChat *tp_chat;
233 empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered: %p",
236 tp_chat = empathy_tp_chat_new (channel, FALSE);
238 filter_chat_dispatch (filter, tp_chat);
240 g_signal_connect (tp_chat, "message-received",
241 G_CALLBACK (filter_chat_message_received_cb),
248 filter_call_dispatch (EmpathyFilter *filter,
251 EmpathyTpCall *call = EMPATHY_TP_CALL (user_data);
253 empathy_call_window_new (call);
254 g_object_unref (call);
258 filter_call_contact_notify_cb (EmpathyTpCall *call,
260 EmpathyFilter *filter)
262 EmpathyContact *contact;
265 g_object_get (call, "contact", &contact, NULL);
270 empathy_contact_run_until_ready (contact,
271 EMPATHY_CONTACT_READY_NAME,
274 msg = g_strdup_printf (_("Incoming call from %s"),
275 empathy_contact_get_name (contact));
277 filter_emit_event (filter, EMPATHY_IMAGE_VOIP, msg,
278 filter_call_dispatch, call);
281 g_object_unref (contact);
285 filter_call_handle_channel (EmpathyFilter *filter,
287 gboolean is_incoming)
291 empathy_debug (DEBUG_DOMAIN, "New media channel to be filtered: %p",
294 call = empathy_tp_call_new (channel);
296 filter_call_dispatch (filter, call);
298 g_signal_connect (call, "notify::contact",
299 G_CALLBACK (filter_call_contact_notify_cb),
306 filter_contact_list_subscribe (EmpathyFilter *filter,
309 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
311 empathy_subscription_dialog_show (contact, NULL);
312 g_object_unref (contact);
316 filter_contact_list_local_pending_cb (EmpathyTpGroup *group,
317 EmpathyContact *contact,
318 EmpathyContact *actor,
321 EmpathyFilter *filter)
325 empathy_debug (DEBUG_DOMAIN, "New local pending contact");
327 empathy_contact_run_until_ready (contact,
328 EMPATHY_CONTACT_READY_NAME,
331 str = g_string_new (NULL);
332 g_string_printf (str, _("Subscription requested by %s"),
333 empathy_contact_get_name (contact));
334 if (!G_STR_EMPTY (message)) {
335 g_string_append_printf (str, _("\nMessage: %s"), message);
338 filter_emit_event (filter, GTK_STOCK_DIALOG_QUESTION, str->str,
339 filter_contact_list_subscribe,
340 g_object_ref (contact));
342 g_string_free (str, TRUE);
346 filter_contact_list_ready_cb (EmpathyTpGroup *group,
348 EmpathyFilter *filter)
352 if (tp_strdiff ("publish", empathy_tp_group_get_name (group))) {
353 g_object_unref (group);
357 empathy_debug (DEBUG_DOMAIN, "Publish contact list ready");
359 g_signal_connect (group, "local-pending",
360 G_CALLBACK (filter_contact_list_local_pending_cb),
363 pendings = empathy_tp_group_get_local_pendings (group);
364 for (l = pendings; l; l = l->next) {
365 EmpathyPendingInfo *info = l->data;
367 filter_contact_list_local_pending_cb (group, info->member,
368 info->actor, info->reason,
369 info->message, filter);
370 empathy_pending_info_free (info);
372 g_list_free (pendings);
376 filter_tubes_async_cb (TpProxy *channel,
382 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
383 user_data, error->message);
395 filter_tubes_dispatch (EmpathyFilter *filter,
398 FilterTubeData *data = user_data;
404 gchar *thandler_bus_name;
405 gchar *thandler_object_path;
407 thandler_bus_name = empathy_tube_handler_build_bus_name (data->type, data->service);
408 thandler_object_path = empathy_tube_handler_build_object_path (data->type, data->service);
410 /* Create the proxy for the tube handler */
411 thandler = g_object_new (TP_TYPE_PROXY,
412 "dbus-connection", tp_get_bus (),
413 "bus-name", thandler_bus_name,
414 "object-path", thandler_object_path,
416 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
418 /* Give the tube to the handler */
419 g_object_get (data->channel,
420 "connection", &connection,
421 "object-path", &object_path,
422 "handle_type", &handle_type,
426 emp_cli_tube_handler_call_handle_tube (thandler, -1,
427 connection->bus_name,
428 connection->object_path,
429 object_path, handle_type, handle,
431 filter_tubes_async_cb,
432 "handling tube", NULL,
435 g_free (thandler_bus_name);
436 g_free (thandler_object_path);
437 g_object_unref (thandler);
438 g_object_unref (connection);
439 g_free (object_path);
441 g_free (data->service);
442 g_object_unref (data->channel);
443 g_slice_free (FilterTubeData, data);
448 filter_tubes_new_tube_cb (TpChannel *channel,
452 const gchar *service,
453 GHashTable *parameters,
458 EmpathyFilterPriv *priv = GET_PRIV (filter);
461 FilterTubeData *data;
463 /* Increase tube count */
464 number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel));
465 g_hash_table_replace (priv->tubes, g_object_ref (channel),
466 GUINT_TO_POINTER (++number));
467 empathy_debug (DEBUG_DOMAIN, "Increased tube count for channel %p: %d",
470 /* We dispatch only local pending tubes */
471 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
475 data = g_slice_new (FilterTubeData);
476 data->channel = g_object_ref (channel);
477 data->service = g_strdup (service);
480 msg = g_strdup_printf (_("Incoming tube for application %s"), service);
481 filter_emit_event (EMPATHY_FILTER (filter), GTK_STOCK_EXECUTE, msg,
482 filter_tubes_dispatch,
488 filter_tubes_list_tubes_cb (TpChannel *channel,
489 const GPtrArray *tubes,
497 empathy_debug (DEBUG_DOMAIN, "Error listing tubes: %s",
502 for (i = 0; i < tubes->len; i++) {
505 values = g_ptr_array_index (tubes, i);
506 filter_tubes_new_tube_cb (channel,
507 g_value_get_uint (g_value_array_get_nth (values, 0)),
508 g_value_get_uint (g_value_array_get_nth (values, 1)),
509 g_value_get_uint (g_value_array_get_nth (values, 2)),
510 g_value_get_string (g_value_array_get_nth (values, 3)),
511 g_value_get_boxed (g_value_array_get_nth (values, 4)),
512 g_value_get_uint (g_value_array_get_nth (values, 5)),
518 filter_tubes_channel_invalidated_cb (TpProxy *proxy,
522 EmpathyFilter *filter)
524 EmpathyFilterPriv *priv = GET_PRIV (filter);
526 empathy_debug (DEBUG_DOMAIN, "Channel %p invalidated: %s", proxy, message);
528 g_hash_table_remove (priv->tubes, proxy);
532 filter_tubes_tube_closed_cb (TpChannel *channel,
537 EmpathyFilterPriv *priv = GET_PRIV (filter);
540 number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel));
542 empathy_debug (DEBUG_DOMAIN, "Ended tube count for channel %p, "
543 "closing channel", channel);
544 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
546 else if (number > 1) {
547 empathy_debug (DEBUG_DOMAIN, "Decrease tube count for channel %p: %d",
549 g_hash_table_replace (priv->tubes, g_object_ref (channel),
550 GUINT_TO_POINTER (--number));
555 filter_tubes_handle_channel (EmpathyFilter *filter,
557 gboolean is_incoming)
559 EmpathyFilterPriv *priv = GET_PRIV (filter);
561 if (g_hash_table_lookup (priv->tubes, channel)) {
565 empathy_debug (DEBUG_DOMAIN, "Handling new channel");
567 g_hash_table_insert (priv->tubes, g_object_ref (channel),
568 GUINT_TO_POINTER (0));
570 g_signal_connect (channel, "invalidated",
571 G_CALLBACK (filter_tubes_channel_invalidated_cb),
574 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
575 filter_tubes_tube_closed_cb,
577 G_OBJECT (filter), NULL);
578 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
579 filter_tubes_new_tube_cb,
581 G_OBJECT (filter), NULL);
582 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
583 filter_tubes_list_tubes_cb,
589 filter_contact_list_destroy_cb (EmpathyTpGroup *group,
590 EmpathyFilter *filter)
592 g_object_unref (group);
596 filter_contact_list_handle_channel (EmpathyFilter *filter,
598 gboolean is_incoming)
600 EmpathyTpGroup *group;
602 group = empathy_tp_group_new (channel);
603 g_signal_connect (group, "notify::ready",
604 G_CALLBACK (filter_contact_list_ready_cb),
606 g_signal_connect (group, "destroy",
607 G_CALLBACK (filter_contact_list_destroy_cb),
612 filter_connection_invalidated_cb (TpConnection *connection,
616 EmpathyFilter *filter)
618 EmpathyFilterPriv *priv = GET_PRIV (filter);
622 empathy_debug (DEBUG_DOMAIN, "connection invalidated: %s", message);
624 g_hash_table_iter_init (&iter, priv->accounts);
625 while (g_hash_table_iter_next (&iter, &key, &value)) {
626 if (value == connection) {
627 g_hash_table_remove (priv->accounts, key);
633 typedef void (*HandleChannelFunc) (EmpathyFilter *filter,
635 gboolean is_incoming);
638 filter_conection_new_channel_cb (TpConnection *connection,
639 const gchar *object_path,
640 const gchar *channel_type,
643 gboolean suppress_handler,
647 HandleChannelFunc func = NULL;
649 gpointer had_channels;
651 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
652 if (had_channels == NULL) {
656 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
657 func = filter_chat_handle_channel;
660 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
661 func = filter_call_handle_channel;
664 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST)) {
665 func = filter_contact_list_handle_channel;
667 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
668 func = filter_tubes_handle_channel;
670 empathy_debug (DEBUG_DOMAIN, "Unknown channel type %s",
675 channel = tp_channel_new (connection, object_path, channel_type,
676 handle_type, handle, NULL);
677 tp_channel_run_until_ready (channel, NULL, NULL);
679 /* We abuse of suppress_handler, TRUE means OUTGOING */
680 func (EMPATHY_FILTER (filter), channel, suppress_handler);
682 g_object_unref (channel);
686 filter_connection_list_channels_cb (TpConnection *connection,
687 const GPtrArray *channels,
694 g_object_set_data (G_OBJECT (connection), "had-channels",
695 GUINT_TO_POINTER (1));
697 for (i = 0; i < channels->len; i++) {
700 values = g_ptr_array_index (channels, i);
701 filter_conection_new_channel_cb (connection,
702 g_value_get_boxed (g_value_array_get_nth (values, 0)),
703 g_value_get_string (g_value_array_get_nth (values, 1)),
704 g_value_get_uint (g_value_array_get_nth (values, 2)),
705 g_value_get_uint (g_value_array_get_nth (values, 3)),
706 TRUE, user_data, filter);
712 filter_connection_advertise_capabilities_cb (TpConnection *connection,
713 const GPtrArray *capabilities,
719 empathy_debug (DEBUG_DOMAIN, "Error advertising capabilities: %s",
726 filter_connection_ready_cb (TpConnection *connection,
728 EmpathyFilter *filter)
731 GPtrArray *capabilities;
734 const gchar *remove = NULL;
737 empathy_debug (DEBUG_DOMAIN, "Connection ready, accepting new channels");
739 tp_cli_connection_connect_to_new_channel (connection,
740 filter_conection_new_channel_cb,
742 G_OBJECT (filter), NULL);
743 tp_cli_connection_call_list_channels (connection, -1,
744 filter_connection_list_channels_cb,
749 /* Advertise VoIP capabilities */
750 capabilities = g_ptr_array_sized_new (1);
751 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
752 G_TYPE_UINT, G_TYPE_INVALID);
753 g_value_init (&cap, cap_type);
754 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
755 dbus_g_type_struct_set (&cap,
756 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
757 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
758 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
759 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
760 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
762 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
764 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
766 capabilities, &remove,
767 filter_connection_advertise_capabilities_cb,
768 NULL, NULL, G_OBJECT (filter));
773 filter_update_account (EmpathyFilter *filter,
776 EmpathyFilterPriv *priv = GET_PRIV (filter);
777 TpConnection *connection;
780 connection = g_hash_table_lookup (priv->accounts, account);
785 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
790 g_hash_table_insert (priv->accounts, g_object_ref (account), connection);
791 g_signal_connect (connection, "invalidated",
792 G_CALLBACK (filter_connection_invalidated_cb),
795 g_object_get (connection, "connection-ready", &ready, NULL);
797 filter_connection_ready_cb (connection, NULL, filter);
799 g_signal_connect (connection, "notify::connection-ready",
800 G_CALLBACK (filter_connection_ready_cb),
806 filter_status_changed_cb (MissionControl *mc,
807 TpConnectionStatus status,
809 TpConnectionStatusReason reason,
810 const gchar *unique_name,
811 EmpathyFilter *filter)
815 account = mc_account_lookup (unique_name);
816 filter_update_account (filter, account);
817 g_object_unref (account);
821 filter_finalize (GObject *object)
823 EmpathyFilterPriv *priv = GET_PRIV (object);
825 empathy_disconnect_account_status_changed (priv->token);
826 g_object_unref (priv->mc);
828 g_slist_foreach (priv->events, (GFunc) filter_event_free, NULL);
829 g_slist_free (priv->events);
831 g_hash_table_destroy (priv->accounts);
832 g_hash_table_destroy (priv->tubes);
836 filter_get_property (GObject *object,
841 EmpathyFilterPriv *priv = GET_PRIV (object);
845 g_value_set_pointer (value, priv->events ? priv->events->data : NULL);
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
854 empathy_filter_class_init (EmpathyFilterClass *klass)
856 GObjectClass *object_class = G_OBJECT_CLASS (klass);
858 object_class->finalize = filter_finalize;
859 object_class->get_property = filter_get_property;
861 g_object_class_install_property (object_class,
863 g_param_spec_pointer ("top-event",
865 "The first event in the events list",
868 g_type_class_add_private (object_class, sizeof (EmpathyFilterPriv));
872 empathy_filter_init (EmpathyFilter *filter)
874 EmpathyFilterPriv *priv = GET_PRIV (filter);
877 priv->tubes = g_hash_table_new_full (filter_channel_hash,
878 filter_channel_equal,
879 g_object_unref, NULL);
881 priv->mc = empathy_mission_control_new ();
882 priv->token = empathy_connect_to_account_status_changed (priv->mc,
883 G_CALLBACK (filter_status_changed_cb),
886 priv->accounts = g_hash_table_new_full (empathy_account_hash,
887 empathy_account_equal,
890 accounts = mc_accounts_list_by_enabled (TRUE);
891 for (l = accounts; l; l = l->next) {
892 filter_update_account (filter, l->data);
893 g_object_unref (l->data);
895 g_list_free (accounts);
899 empathy_filter_new (void)
901 static EmpathyFilter *filter = NULL;
904 filter = g_object_new (EMPATHY_TYPE_FILTER, NULL);
905 g_object_add_weak_pointer (G_OBJECT (filter), (gpointer) &filter);
907 g_object_ref (filter);