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"
43 #include "empathy-tp-group.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
50 GHashTable *connections;
54 } EmpathyDispatcherPriv;
56 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
65 static guint signals[LAST_SIGNAL];
66 static EmpathyDispatcher *dispatcher = NULL;
69 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
72 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
76 EmpathyDispatcherTube public;
77 EmpathyContactFactory *factory;
85 empathy_dispatcher_tube_get_type (void)
87 static GType type_id = 0;
90 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
91 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
92 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
98 EmpathyDispatcherTube *
99 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
101 DispatcherTube *tube = (DispatcherTube*) data;
103 g_return_val_if_fail (tube != NULL, NULL);
111 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
113 DispatcherTube *tube = (DispatcherTube*) data;
115 g_return_if_fail (tube != NULL);
117 if (--tube->ref_count == 0) {
118 if (!tube->handled) {
119 DEBUG ("Tube can't be handled, closing");
120 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
126 g_free (tube->bus_name);
127 g_free (tube->object_path);
128 g_object_unref (tube->factory);
129 g_object_unref (tube->public.channel);
130 g_object_unref (tube->public.initiator);
131 g_slice_free (DispatcherTube, tube);
136 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
141 DispatcherTube *tube = user_data;
144 DEBUG ("Error: %s", error->message);
146 tube->handled = TRUE;
151 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
152 EmpathyDispatcherTube *user_data)
154 DispatcherTube *tube = (DispatcherTube*) user_data;
156 if (tube->public.activatable) {
163 /* Create the proxy for the tube handler */
164 thandler = g_object_new (TP_TYPE_PROXY,
165 "dbus-connection", tp_get_bus (),
166 "bus-name", tube->bus_name,
167 "object-path", tube->object_path,
169 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
171 /* Give the tube to the handler */
172 g_object_get (tube->public.channel,
173 "connection", &connection,
174 "object-path", &object_path,
175 "handle_type", &handle_type,
179 DEBUG ("Dispatching tube");
180 emp_cli_tube_handler_call_handle_tube (thandler, -1,
181 connection->bus_name,
182 connection->object_path,
183 object_path, handle_type,
184 handle, tube->public.id,
185 dispatcher_tubes_handle_tube_cb,
186 empathy_dispatcher_tube_ref (user_data),
187 (GDestroyNotify) empathy_dispatcher_tube_unref,
188 G_OBJECT (dispatcher));
190 g_object_unref (thandler);
191 g_object_unref (connection);
192 g_free (object_path);
197 dispatcher_tubes_new_tube_cb (TpChannel *channel,
201 const gchar *service,
202 GHashTable *parameters,
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_object_get_data (G_OBJECT (channel), "tube-count"));
217 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
218 DEBUG ("Increased tube count for channel %p: %d", channel, number);
220 /* We dispatch only local pending tubes */
221 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
226 daemon = tp_dbus_daemon_new (tp_get_bus ());
229 account = empathy_channel_get_account (channel);
230 tube = g_slice_new (DispatcherTube);
232 tube->handled = FALSE;
233 tube->factory = empathy_contact_factory_new ();
234 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
235 tube->object_path = empathy_tube_handler_build_object_path (type, service);
236 tube->public.activatable = FALSE;
237 tube->public.id = id;
238 tube->public.channel = g_object_ref (channel);
239 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
242 g_object_unref (account);
244 /* Check if that bus-name has an owner, if it has one that means the
245 * app is already running and we can directly give the channel. */
246 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
247 &running, NULL, NULL);
249 DEBUG ("Tube handler running");
250 tube->public.activatable = TRUE;
251 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
252 (EmpathyDispatcherTube*) tube);
253 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
257 /* Check if that bus-name is activatable, if not that means the
258 * application needed to handle this tube isn't installed. */
259 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
262 DEBUG ("Error listing activatable names: %s", error->message);
263 g_clear_error (&error);
267 for (name = names; *name; name++) {
268 if (!tp_strdiff (*name, tube->bus_name)) {
269 tube->public.activatable = TRUE;
276 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
277 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
281 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
282 const GPtrArray *tubes,
290 DEBUG ("Error: %s", error->message);
294 for (i = 0; i < tubes->len; i++) {
297 values = g_ptr_array_index (tubes, i);
298 dispatcher_tubes_new_tube_cb (channel,
299 g_value_get_uint (g_value_array_get_nth (values, 0)),
300 g_value_get_uint (g_value_array_get_nth (values, 1)),
301 g_value_get_uint (g_value_array_get_nth (values, 2)),
302 g_value_get_string (g_value_array_get_nth (values, 3)),
303 g_value_get_boxed (g_value_array_get_nth (values, 4)),
304 g_value_get_uint (g_value_array_get_nth (values, 5)),
305 user_data, dispatcher);
310 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
314 EmpathyDispatcher *dispatcher)
316 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
318 DEBUG ("%s", message);
320 priv->tubes = g_slist_remove (priv->tubes, proxy);
321 g_object_unref (proxy);
325 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
332 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
334 DEBUG ("No more tube, closing channel");
335 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
337 else if (number > 1) {
338 DEBUG ("Decrease tube count: %d", number);
339 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
344 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
347 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
351 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
352 g_signal_connect (channel, "invalidated",
353 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
356 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
357 dispatcher_tubes_tube_closed_cb,
359 G_OBJECT (dispatcher), NULL);
360 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
361 dispatcher_tubes_new_tube_cb,
363 G_OBJECT (dispatcher), NULL);
364 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
365 dispatcher_tubes_list_tubes_cb,
367 G_OBJECT (dispatcher));
371 dispatcher_connection_invalidated_cb (TpConnection *connection,
375 EmpathyDispatcher *dispatcher)
377 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
381 DEBUG ("Error: %s", message);
383 g_hash_table_iter_init (&iter, priv->connections);
384 while (g_hash_table_iter_next (&iter, &key, &value)) {
385 if (value == connection) {
386 g_hash_table_remove (priv->connections, key);
393 dispatcher_connection_new_channel_cb (TpConnection *connection,
394 const gchar *object_path,
395 const gchar *channel_type,
398 gboolean suppress_handler,
402 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
404 gpointer had_channels;
406 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
407 if (had_channels == NULL) {
408 /* ListChannels didn't return yet, return to avoid duplicate
413 channel = tp_channel_new (connection, object_path, channel_type,
414 handle_type, handle, NULL);
415 tp_channel_run_until_ready (channel, NULL, NULL);
417 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
418 dispatcher_tubes_handle_channel (dispatcher, channel);
421 if (suppress_handler) {
422 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
424 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
427 g_object_unref (channel);
431 dispatcher_connection_list_channels_cb (TpConnection *connection,
432 const GPtrArray *channels,
440 DEBUG ("Error: %s", error->message);
444 g_object_set_data (G_OBJECT (connection), "had-channels",
445 GUINT_TO_POINTER (1));
447 for (i = 0; i < channels->len; i++) {
450 values = g_ptr_array_index (channels, i);
451 dispatcher_connection_new_channel_cb (connection,
452 g_value_get_boxed (g_value_array_get_nth (values, 0)),
453 g_value_get_string (g_value_array_get_nth (values, 1)),
454 g_value_get_uint (g_value_array_get_nth (values, 2)),
455 g_value_get_uint (g_value_array_get_nth (values, 3)),
456 FALSE, user_data, dispatcher);
461 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
462 const GPtrArray *capabilities,
468 DEBUG ("Error: %s", error->message);
473 dispatcher_connection_ready_cb (TpConnection *connection,
477 GPtrArray *capabilities;
480 const gchar *remove = NULL;
483 dispatcher_connection_invalidated_cb (connection,
491 g_signal_connect (connection, "invalidated",
492 G_CALLBACK (dispatcher_connection_invalidated_cb),
494 tp_cli_connection_connect_to_new_channel (connection,
495 dispatcher_connection_new_channel_cb,
497 G_OBJECT (dispatcher), NULL);
498 tp_cli_connection_call_list_channels (connection, -1,
499 dispatcher_connection_list_channels_cb,
501 G_OBJECT (dispatcher));
503 /* Advertise VoIP capabilities */
504 capabilities = g_ptr_array_sized_new (1);
505 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
506 G_TYPE_UINT, G_TYPE_INVALID);
507 g_value_init (&cap, cap_type);
508 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
509 dbus_g_type_struct_set (&cap,
510 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
511 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
512 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
513 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
514 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
516 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
518 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
520 capabilities, &remove,
521 dispatcher_connection_advertise_capabilities_cb,
522 NULL, NULL, G_OBJECT (dispatcher));
523 /* FIXME: Is that leaked? */
527 dispatcher_update_account (EmpathyDispatcher *dispatcher,
530 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
531 TpConnection *connection;
533 connection = g_hash_table_lookup (priv->connections, account);
538 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
543 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
544 tp_connection_call_when_ready (connection,
545 dispatcher_connection_ready_cb,
550 dispatcher_status_changed_cb (MissionControl *mc,
551 TpConnectionStatus status,
553 TpConnectionStatusReason reason,
554 const gchar *unique_name,
555 EmpathyDispatcher *dispatcher)
559 account = mc_account_lookup (unique_name);
560 dispatcher_update_account (dispatcher, account);
561 g_object_unref (account);
565 dispatcher_finalize (GObject *object)
567 EmpathyDispatcherPriv *priv = GET_PRIV (object);
570 empathy_disconnect_account_status_changed (priv->token);
571 g_object_unref (priv->mc);
573 for (l = priv->tubes; l; l = l->next) {
574 g_signal_handlers_disconnect_by_func (l->data,
575 dispatcher_tubes_channel_invalidated_cb,
577 g_object_unref (l->data);
579 g_slist_free (priv->tubes);
581 g_hash_table_destroy (priv->connections);
585 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
587 GObjectClass *object_class = G_OBJECT_CLASS (klass);
589 object_class->finalize = dispatcher_finalize;
591 signals[DISPATCH_CHANNEL] =
592 g_signal_new ("dispatch-channel",
593 G_TYPE_FROM_CLASS (klass),
597 g_cclosure_marshal_VOID__OBJECT,
600 signals[FILTER_CHANNEL] =
601 g_signal_new ("filter-channel",
602 G_TYPE_FROM_CLASS (klass),
606 g_cclosure_marshal_VOID__OBJECT,
609 signals[FILTER_TUBE] =
610 g_signal_new ("filter-tube",
611 G_TYPE_FROM_CLASS (klass),
615 g_cclosure_marshal_VOID__BOXED,
617 1, EMPATHY_TYPE_DISPATCHER_TUBE);
619 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
623 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
626 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
627 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
629 dispatcher->priv = priv;
630 priv->mc = empathy_mission_control_new ();
631 priv->token = empathy_connect_to_account_status_changed (priv->mc,
632 G_CALLBACK (dispatcher_status_changed_cb),
635 priv->connections = g_hash_table_new_full (empathy_account_hash,
636 empathy_account_equal,
639 accounts = mc_accounts_list_by_enabled (TRUE);
640 for (l = accounts; l; l = l->next) {
641 dispatcher_update_account (dispatcher, l->data);
642 g_object_unref (l->data);
644 g_list_free (accounts);
648 empathy_dispatcher_new (void)
651 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
652 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
654 g_object_ref (dispatcher);
661 const gchar *channel_type;
664 } DispatcherRequestData;
667 dispatcher_request_channel_cb (TpConnection *connection,
668 const gchar *object_path,
671 GObject *weak_object)
673 DispatcherRequestData *data = (DispatcherRequestData*) user_data;
676 DEBUG ("Error: %s", error->message);
683 channel = tp_channel_new (connection, object_path,
688 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
693 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
697 TpConnection *connection;
700 EmpathyContactFactory *factory;
701 EmpathyTpGroup *group;
702 EmpathyContact *self_contact;
703 GError *error = NULL;
705 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
707 mc = empathy_mission_control_new ();
708 account = empathy_contact_get_account (contact);
709 connection = mission_control_get_tpconnection (mc, account, NULL);
710 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
713 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
714 * will be catched in EmpathyFilter */
715 if (!tp_cli_connection_run_request_channel (connection, -1,
716 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
723 DEBUG ("Couldn't request channel: %s",
724 error ? error->message : "No error given");
725 g_clear_error (&error);
726 g_object_unref (connection);
730 channel = tp_channel_new (connection,
731 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
732 TP_HANDLE_TYPE_NONE, 0, NULL);
734 group = empathy_tp_group_new (channel);
735 empathy_run_until_ready (group);
737 factory = empathy_contact_factory_new ();
738 self_contact = empathy_contact_factory_get_user (factory, account);
739 empathy_contact_run_until_ready (self_contact,
740 EMPATHY_CONTACT_READY_HANDLE,
743 empathy_tp_group_add_member (group, contact, "");
744 empathy_tp_group_add_member (group, self_contact, "");
746 g_object_unref (factory);
747 g_object_unref (self_contact);
748 g_object_unref (group);
749 g_object_unref (connection);
750 g_object_unref (channel);
751 g_free (object_path);
755 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
757 EmpathyContactFactory *factory;
758 EmpathyContact *contact;
760 factory = empathy_contact_factory_new ();
761 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
762 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
764 empathy_dispatcher_call_with_contact (contact);
766 g_object_unref (contact);
767 g_object_unref (factory);
771 empathy_dispatcher_chat_with_contact (EmpathyContact *contact)
775 TpConnection *connection;
776 DispatcherRequestData *data;
778 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
780 mc = empathy_mission_control_new ();
781 account = empathy_contact_get_account (contact);
782 connection = mission_control_get_tpconnection (mc, account, NULL);
783 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
786 /* We abuse of suppress_handler, TRUE means OUTGOING. */
787 data = g_new (DispatcherRequestData, 1);
788 data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
789 data->handle_type = TP_HANDLE_TYPE_CONTACT;
790 data->handle = empathy_contact_get_handle (contact);
791 tp_cli_connection_call_request_channel (connection, -1,
796 dispatcher_request_channel_cb,
799 g_object_unref (connection);
803 empathy_dispatcher_chat_with_contact_id (McAccount *account,
804 const gchar *contact_id)
806 EmpathyContactFactory *factory;
807 EmpathyContact *contact;
809 factory = empathy_contact_factory_new ();
810 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
811 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
813 empathy_dispatcher_chat_with_contact (contact);
815 g_object_unref (contact);
816 g_object_unref (factory);