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/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/proxy-subclass.h>
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <extensions/extensions.h>
39 #include "empathy-dispatcher.h"
40 #include "empathy-utils.h"
41 #include "empathy-tube-handler.h"
42 #include "empathy-contact-factory.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
45 #include <libempathy/empathy-debug.h>
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
49 GHashTable *connections;
53 } EmpathyDispatcherPriv;
55 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
64 static guint signals[LAST_SIGNAL];
65 static EmpathyDispatcher *dispatcher = NULL;
68 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
71 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
75 EmpathyDispatcherTube public;
76 EmpathyContactFactory *factory;
84 empathy_dispatcher_tube_get_type (void)
86 static GType type_id = 0;
89 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
90 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
91 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
97 EmpathyDispatcherTube *
98 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
100 DispatcherTube *tube = (DispatcherTube*) data;
102 g_return_val_if_fail (tube != NULL, NULL);
110 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
112 DispatcherTube *tube = (DispatcherTube*) data;
114 g_return_if_fail (tube != NULL);
116 if (--tube->ref_count == 0) {
117 if (!tube->handled) {
118 DEBUG ("Tube can't be handled, closing");
119 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
125 g_free (tube->bus_name);
126 g_free (tube->object_path);
127 g_object_unref (tube->factory);
128 g_object_unref (tube->public.channel);
129 g_object_unref (tube->public.initiator);
130 g_slice_free (DispatcherTube, tube);
135 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
140 DispatcherTube *tube = user_data;
143 DEBUG ("Error: %s", error->message);
145 tube->handled = TRUE;
150 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
151 EmpathyDispatcherTube *user_data)
153 DispatcherTube *tube = (DispatcherTube*) user_data;
155 if (tube->public.activatable) {
162 /* Create the proxy for the tube handler */
163 thandler = g_object_new (TP_TYPE_PROXY,
164 "dbus-connection", tp_get_bus (),
165 "bus-name", tube->bus_name,
166 "object-path", tube->object_path,
168 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
170 /* Give the tube to the handler */
171 g_object_get (tube->public.channel,
172 "connection", &connection,
173 "object-path", &object_path,
174 "handle_type", &handle_type,
178 DEBUG ("Dispatching tube");
179 emp_cli_tube_handler_call_handle_tube (thandler, -1,
180 connection->bus_name,
181 connection->object_path,
182 object_path, handle_type,
183 handle, tube->public.id,
184 dispatcher_tubes_handle_tube_cb,
185 empathy_dispatcher_tube_ref (user_data),
186 (GDestroyNotify) empathy_dispatcher_tube_unref,
187 G_OBJECT (dispatcher));
189 g_object_unref (thandler);
190 g_object_unref (connection);
191 g_free (object_path);
196 dispatcher_tubes_new_tube_cb (TpChannel *channel,
200 const gchar *service,
201 GHashTable *parameters,
206 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
207 static TpDBusDaemon *daemon = NULL;
208 DispatcherTube *tube;
212 gboolean running = FALSE;
213 GError *error = NULL;
215 /* Increase tube count */
216 number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel));
217 g_hash_table_replace (priv->tubes, g_object_ref (channel),
218 GUINT_TO_POINTER (++number));
219 DEBUG ("Increased tube count for channel %p: %d", channel, number);
221 /* We dispatch only local pending tubes */
222 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
227 daemon = tp_dbus_daemon_new (tp_get_bus ());
230 account = empathy_channel_get_account (channel);
231 tube = g_slice_new (DispatcherTube);
233 tube->handled = FALSE;
234 tube->factory = empathy_contact_factory_new ();
235 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
236 tube->object_path = empathy_tube_handler_build_object_path (type, service);
237 tube->public.activatable = FALSE;
238 tube->public.id = id;
239 tube->public.channel = g_object_ref (channel);
240 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
243 g_object_unref (account);
245 /* Check if that bus-name has an owner, if it has one that means the
246 * app is already running and we can directly give the channel. */
247 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
248 &running, NULL, NULL);
250 DEBUG ("Tube handler running");
251 tube->public.activatable = TRUE;
252 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
253 (EmpathyDispatcherTube*) tube);
254 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
258 /* Check if that bus-name is activatable, if not that means the
259 * application needed to handle this tube isn't installed. */
260 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
263 DEBUG ("Error listing activatable names: %s", error->message);
264 g_clear_error (&error);
268 for (name = names; *name; name++) {
269 if (!tp_strdiff (*name, tube->bus_name)) {
270 tube->public.activatable = TRUE;
277 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
278 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
282 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
283 const GPtrArray *tubes,
291 DEBUG ("Error: %s", error->message);
295 for (i = 0; i < tubes->len; i++) {
298 values = g_ptr_array_index (tubes, i);
299 dispatcher_tubes_new_tube_cb (channel,
300 g_value_get_uint (g_value_array_get_nth (values, 0)),
301 g_value_get_uint (g_value_array_get_nth (values, 1)),
302 g_value_get_uint (g_value_array_get_nth (values, 2)),
303 g_value_get_string (g_value_array_get_nth (values, 3)),
304 g_value_get_boxed (g_value_array_get_nth (values, 4)),
305 g_value_get_uint (g_value_array_get_nth (values, 5)),
306 user_data, dispatcher);
311 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
315 EmpathyDispatcher *dispatcher)
317 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319 DEBUG ("Error: %s", message);
321 g_hash_table_remove (priv->tubes, proxy);
325 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
330 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
333 number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel));
335 DEBUG ("No more tube, closing channel");
336 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
338 else if (number > 1) {
339 DEBUG ("Decrease tube count: %d", number);
340 g_hash_table_replace (priv->tubes, g_object_ref (channel),
341 GUINT_TO_POINTER (--number));
346 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
349 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
351 if (g_hash_table_lookup (priv->tubes, channel)) {
355 DEBUG ("Handling new channel");
357 g_hash_table_insert (priv->tubes, g_object_ref (channel),
358 GUINT_TO_POINTER (0));
360 g_signal_connect (channel, "invalidated",
361 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
364 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
365 dispatcher_tubes_tube_closed_cb,
367 G_OBJECT (dispatcher), NULL);
368 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
369 dispatcher_tubes_new_tube_cb,
371 G_OBJECT (dispatcher), NULL);
372 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
373 dispatcher_tubes_list_tubes_cb,
375 G_OBJECT (dispatcher));
379 dispatcher_connection_invalidated_cb (TpConnection *connection,
383 EmpathyDispatcher *dispatcher)
385 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
389 DEBUG ("Error: %s", message);
391 g_hash_table_iter_init (&iter, priv->connections);
392 while (g_hash_table_iter_next (&iter, &key, &value)) {
393 if (value == connection) {
394 g_hash_table_remove (priv->connections, key);
401 dispatcher_connection_new_channel_cb (TpConnection *connection,
402 const gchar *object_path,
403 const gchar *channel_type,
406 gboolean suppress_handler,
410 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
412 gpointer had_channels;
414 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
415 if (had_channels == NULL) {
416 /* ListChannels didn't return yet, return to avoid duplicate
421 channel = tp_channel_new (connection, object_path, channel_type,
422 handle_type, handle, NULL);
423 tp_channel_run_until_ready (channel, NULL, NULL);
425 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
426 dispatcher_tubes_handle_channel (dispatcher, channel);
429 if (suppress_handler) {
430 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
432 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
435 g_object_unref (channel);
439 dispatcher_connection_list_channels_cb (TpConnection *connection,
440 const GPtrArray *channels,
448 DEBUG ("Error: %s", error->message);
452 g_object_set_data (G_OBJECT (connection), "had-channels",
453 GUINT_TO_POINTER (1));
455 for (i = 0; i < channels->len; i++) {
458 values = g_ptr_array_index (channels, i);
459 dispatcher_connection_new_channel_cb (connection,
460 g_value_get_boxed (g_value_array_get_nth (values, 0)),
461 g_value_get_string (g_value_array_get_nth (values, 1)),
462 g_value_get_uint (g_value_array_get_nth (values, 2)),
463 g_value_get_uint (g_value_array_get_nth (values, 3)),
464 FALSE, user_data, dispatcher);
469 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
470 const GPtrArray *capabilities,
476 DEBUG ("Error: %s", error->message);
481 dispatcher_connection_ready_cb (TpConnection *connection,
485 GPtrArray *capabilities;
488 const gchar *remove = NULL;
491 dispatcher_connection_invalidated_cb (connection,
499 g_signal_connect (connection, "invalidated",
500 G_CALLBACK (dispatcher_connection_invalidated_cb),
502 tp_cli_connection_connect_to_new_channel (connection,
503 dispatcher_connection_new_channel_cb,
505 G_OBJECT (dispatcher), NULL);
506 tp_cli_connection_call_list_channels (connection, -1,
507 dispatcher_connection_list_channels_cb,
509 G_OBJECT (dispatcher));
511 /* Advertise VoIP capabilities */
512 capabilities = g_ptr_array_sized_new (1);
513 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
514 G_TYPE_UINT, G_TYPE_INVALID);
515 g_value_init (&cap, cap_type);
516 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
517 dbus_g_type_struct_set (&cap,
518 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
519 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
520 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
521 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
522 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
524 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
526 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
528 capabilities, &remove,
529 dispatcher_connection_advertise_capabilities_cb,
530 NULL, NULL, G_OBJECT (dispatcher));
531 /* FIXME: Is that leaked? */
535 dispatcher_update_account (EmpathyDispatcher *dispatcher,
538 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
539 TpConnection *connection;
541 connection = g_hash_table_lookup (priv->connections, account);
546 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
551 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
552 tp_connection_call_when_ready (connection,
553 dispatcher_connection_ready_cb,
558 dispatcher_status_changed_cb (MissionControl *mc,
559 TpConnectionStatus status,
561 TpConnectionStatusReason reason,
562 const gchar *unique_name,
563 EmpathyDispatcher *dispatcher)
567 account = mc_account_lookup (unique_name);
568 dispatcher_update_account (dispatcher, account);
569 g_object_unref (account);
573 dispatcher_channel_hash (gconstpointer key)
575 TpProxy *channel = TP_PROXY (key);
577 return g_str_hash (channel->object_path);
581 dispatcher_channel_equal (gconstpointer a,
584 TpProxy *channel_a = TP_PROXY (a);
585 TpProxy *channel_b = TP_PROXY (b);
587 return g_str_equal (channel_a->object_path, channel_b->object_path);
591 dispatcher_finalize (GObject *object)
593 EmpathyDispatcherPriv *priv = GET_PRIV (object);
595 empathy_disconnect_account_status_changed (priv->token);
596 g_object_unref (priv->mc);
598 g_hash_table_destroy (priv->connections);
599 g_hash_table_destroy (priv->tubes);
603 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
605 GObjectClass *object_class = G_OBJECT_CLASS (klass);
607 object_class->finalize = dispatcher_finalize;
609 signals[DISPATCH_CHANNEL] =
610 g_signal_new ("dispatch-channel",
611 G_TYPE_FROM_CLASS (klass),
615 g_cclosure_marshal_VOID__OBJECT,
618 signals[FILTER_CHANNEL] =
619 g_signal_new ("filter-channel",
620 G_TYPE_FROM_CLASS (klass),
624 g_cclosure_marshal_VOID__OBJECT,
627 signals[FILTER_TUBE] =
628 g_signal_new ("filter-tube",
629 G_TYPE_FROM_CLASS (klass),
633 g_cclosure_marshal_VOID__BOXED,
635 1, EMPATHY_TYPE_DISPATCHER_TUBE);
637 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
641 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
644 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
645 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
647 dispatcher->priv = priv;
648 priv->tubes = g_hash_table_new_full (dispatcher_channel_hash,
649 dispatcher_channel_equal,
650 g_object_unref, NULL);
652 priv->mc = empathy_mission_control_new ();
653 priv->token = empathy_connect_to_account_status_changed (priv->mc,
654 G_CALLBACK (dispatcher_status_changed_cb),
657 priv->connections = g_hash_table_new_full (empathy_account_hash,
658 empathy_account_equal,
661 accounts = mc_accounts_list_by_enabled (TRUE);
662 for (l = accounts; l; l = l->next) {
663 dispatcher_update_account (dispatcher, l->data);
664 g_object_unref (l->data);
666 g_list_free (accounts);
670 empathy_dispatcher_new (void)
673 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
674 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
676 g_object_ref (dispatcher);