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"
44 #include "empathy-chatroom-manager.h"
45 #include "empathy-utils.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
48 #include <libempathy/empathy-debug.h>
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
52 GHashTable *connections;
56 } EmpathyDispatcherPriv;
58 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
67 static guint signals[LAST_SIGNAL];
68 static EmpathyDispatcher *dispatcher = NULL;
71 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
74 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
78 EmpathyDispatcherTube public;
79 EmpathyContactFactory *factory;
87 empathy_dispatcher_tube_get_type (void)
89 static GType type_id = 0;
92 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
93 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
94 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
100 EmpathyDispatcherTube *
101 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
103 DispatcherTube *tube = (DispatcherTube*) data;
105 g_return_val_if_fail (tube != NULL, NULL);
113 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
115 DispatcherTube *tube = (DispatcherTube*) data;
117 g_return_if_fail (tube != NULL);
119 if (--tube->ref_count == 0) {
120 if (!tube->handled) {
121 DEBUG ("Tube can't be handled, closing");
122 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
128 g_free (tube->bus_name);
129 g_free (tube->object_path);
130 g_object_unref (tube->factory);
131 g_object_unref (tube->public.channel);
132 g_object_unref (tube->public.initiator);
133 g_slice_free (DispatcherTube, tube);
138 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
143 DispatcherTube *tube = user_data;
146 DEBUG ("Error: %s", error->message);
148 tube->handled = TRUE;
153 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
154 EmpathyDispatcherTube *user_data)
156 DispatcherTube *tube = (DispatcherTube*) user_data;
158 if (tube->public.activatable) {
165 /* Create the proxy for the tube handler */
166 thandler = g_object_new (TP_TYPE_PROXY,
167 "dbus-connection", tp_get_bus (),
168 "bus-name", tube->bus_name,
169 "object-path", tube->object_path,
171 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
173 /* Give the tube to the handler */
174 g_object_get (tube->public.channel,
175 "connection", &connection,
176 "object-path", &object_path,
177 "handle_type", &handle_type,
181 DEBUG ("Dispatching tube");
182 emp_cli_tube_handler_call_handle_tube (thandler, -1,
183 connection->bus_name,
184 connection->object_path,
185 object_path, handle_type,
186 handle, tube->public.id,
187 dispatcher_tubes_handle_tube_cb,
188 empathy_dispatcher_tube_ref (user_data),
189 (GDestroyNotify) empathy_dispatcher_tube_unref,
190 G_OBJECT (dispatcher));
192 g_object_unref (thandler);
193 g_object_unref (connection);
194 g_free (object_path);
199 dispatcher_tubes_new_tube_cb (TpChannel *channel,
203 const gchar *service,
204 GHashTable *parameters,
209 static TpDBusDaemon *daemon = NULL;
210 DispatcherTube *tube;
214 gboolean running = FALSE;
215 GError *error = NULL;
217 /* Increase tube count */
218 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
219 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
220 DEBUG ("Increased tube count for channel %p: %d", channel, number);
222 /* We dispatch only local pending tubes */
223 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
228 daemon = tp_dbus_daemon_new (tp_get_bus ());
231 account = empathy_channel_get_account (channel);
232 tube = g_slice_new (DispatcherTube);
234 tube->handled = FALSE;
235 tube->factory = empathy_contact_factory_new ();
236 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
237 tube->object_path = empathy_tube_handler_build_object_path (type, service);
238 tube->public.activatable = FALSE;
239 tube->public.id = id;
240 tube->public.channel = g_object_ref (channel);
241 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
244 g_object_unref (account);
246 /* Check if that bus-name has an owner, if it has one that means the
247 * app is already running and we can directly give the channel. */
248 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
249 &running, NULL, NULL);
251 DEBUG ("Tube handler running");
252 tube->public.activatable = TRUE;
253 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
254 (EmpathyDispatcherTube*) tube);
255 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
259 /* Check if that bus-name is activatable, if not that means the
260 * application needed to handle this tube isn't installed. */
261 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
264 DEBUG ("Error listing activatable names: %s", error->message);
265 g_clear_error (&error);
269 for (name = names; *name; name++) {
270 if (!tp_strdiff (*name, tube->bus_name)) {
271 tube->public.activatable = TRUE;
278 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
279 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
283 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
284 const GPtrArray *tubes,
292 DEBUG ("Error: %s", error->message);
296 for (i = 0; i < tubes->len; i++) {
299 values = g_ptr_array_index (tubes, i);
300 dispatcher_tubes_new_tube_cb (channel,
301 g_value_get_uint (g_value_array_get_nth (values, 0)),
302 g_value_get_uint (g_value_array_get_nth (values, 1)),
303 g_value_get_uint (g_value_array_get_nth (values, 2)),
304 g_value_get_string (g_value_array_get_nth (values, 3)),
305 g_value_get_boxed (g_value_array_get_nth (values, 4)),
306 g_value_get_uint (g_value_array_get_nth (values, 5)),
307 user_data, dispatcher);
312 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
316 EmpathyDispatcher *dispatcher)
318 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
320 DEBUG ("%s", message);
322 priv->tubes = g_slist_remove (priv->tubes, proxy);
323 g_object_unref (proxy);
327 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
334 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
336 DEBUG ("No more tube, closing channel");
337 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
339 else if (number > 1) {
340 DEBUG ("Decrease tube count: %d", number);
341 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
346 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
349 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
353 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
354 g_signal_connect (channel, "invalidated",
355 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
358 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
359 dispatcher_tubes_tube_closed_cb,
361 G_OBJECT (dispatcher), NULL);
362 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
363 dispatcher_tubes_new_tube_cb,
365 G_OBJECT (dispatcher), NULL);
366 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
367 dispatcher_tubes_list_tubes_cb,
369 G_OBJECT (dispatcher));
373 dispatcher_connection_invalidated_cb (TpConnection *connection,
377 EmpathyDispatcher *dispatcher)
379 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
383 DEBUG ("Error: %s", message);
385 g_hash_table_iter_init (&iter, priv->connections);
386 while (g_hash_table_iter_next (&iter, &key, &value)) {
387 if (value == connection) {
388 g_hash_table_remove (priv->connections, key);
394 static void chatroom_invalidated_cb (TpProxy *channel,
398 EmpathyChatroom *chatroom)
400 EmpathyChatroomManager *mgr;
401 mgr = empathy_chatroom_manager_new ();
404 g_object_get (chatroom, "favorite", &favorite, NULL);
408 /* Chatroom is in favorites so don't remove it from the manager */
409 g_object_set (chatroom, "tp-channel", NULL, NULL);
413 empathy_chatroom_manager_remove (mgr, chatroom);
416 g_object_unref (mgr);
420 dispatcher_connection_new_channel_cb (TpConnection *connection,
421 const gchar *object_path,
422 const gchar *channel_type,
425 gboolean suppress_handler,
429 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
431 gpointer had_channels;
433 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
434 if (had_channels == NULL) {
435 /* ListChannels didn't return yet, return to avoid duplicate
440 channel = tp_channel_new (connection, object_path, channel_type,
441 handle_type, handle, NULL);
442 tp_channel_run_until_ready (channel, NULL, NULL);
444 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
445 dispatcher_tubes_handle_channel (dispatcher, channel);
448 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
449 handle_type == TP_HANDLE_TYPE_ROOM)
451 /* Add the chatroom to the chatroom manager */
452 EmpathyChatroomManager *mgr;
453 EmpathyChatroom *chatroom;
459 mgr = empathy_chatroom_manager_new ();
461 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
462 g_array_append_val (handles, handle);
464 tp_cli_connection_run_inspect_handles (connection, -1,
465 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
467 mc = empathy_mission_control_new ();
468 account = mission_control_get_account_for_tpconnection (mc, connection,
471 chatroom = empathy_chatroom_manager_find (mgr, account, room_ids[0]);
472 if (chatroom == NULL)
474 chatroom = empathy_chatroom_new (account);
475 empathy_chatroom_set_name (chatroom, room_ids[0]);
476 empathy_chatroom_manager_add (mgr, chatroom);
480 g_object_ref (chatroom);
483 g_object_set (chatroom, "tp-channel", channel, NULL);
485 g_signal_connect (channel, "invalidated",
486 G_CALLBACK (chatroom_invalidated_cb), chatroom);
488 g_free (room_ids[0]);
490 g_array_free (handles, TRUE);
492 g_object_unref (account);
493 g_object_unref (chatroom);
494 g_object_unref (mgr);
497 if (suppress_handler) {
498 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
500 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
503 g_object_unref (channel);
507 dispatcher_connection_list_channels_cb (TpConnection *connection,
508 const GPtrArray *channels,
516 DEBUG ("Error: %s", error->message);
520 g_object_set_data (G_OBJECT (connection), "had-channels",
521 GUINT_TO_POINTER (1));
523 for (i = 0; i < channels->len; i++) {
526 values = g_ptr_array_index (channels, i);
527 dispatcher_connection_new_channel_cb (connection,
528 g_value_get_boxed (g_value_array_get_nth (values, 0)),
529 g_value_get_string (g_value_array_get_nth (values, 1)),
530 g_value_get_uint (g_value_array_get_nth (values, 2)),
531 g_value_get_uint (g_value_array_get_nth (values, 3)),
532 FALSE, user_data, dispatcher);
537 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
538 const GPtrArray *capabilities,
544 DEBUG ("Error: %s", error->message);
549 dispatcher_connection_ready_cb (TpConnection *connection,
553 GPtrArray *capabilities;
556 const gchar *remove = NULL;
559 dispatcher_connection_invalidated_cb (connection,
567 g_signal_connect (connection, "invalidated",
568 G_CALLBACK (dispatcher_connection_invalidated_cb),
570 tp_cli_connection_connect_to_new_channel (connection,
571 dispatcher_connection_new_channel_cb,
573 G_OBJECT (dispatcher), NULL);
574 tp_cli_connection_call_list_channels (connection, -1,
575 dispatcher_connection_list_channels_cb,
577 G_OBJECT (dispatcher));
579 /* Advertise VoIP capabilities */
580 capabilities = g_ptr_array_sized_new (1);
581 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
582 G_TYPE_UINT, G_TYPE_INVALID);
583 g_value_init (&cap, cap_type);
584 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
585 dbus_g_type_struct_set (&cap,
586 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
587 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
588 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
589 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
590 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
592 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
594 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
596 capabilities, &remove,
597 dispatcher_connection_advertise_capabilities_cb,
598 NULL, NULL, G_OBJECT (dispatcher));
599 /* FIXME: Is that leaked? */
603 dispatcher_update_account (EmpathyDispatcher *dispatcher,
606 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
607 TpConnection *connection;
609 connection = g_hash_table_lookup (priv->connections, account);
614 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
619 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
620 tp_connection_call_when_ready (connection,
621 dispatcher_connection_ready_cb,
626 dispatcher_status_changed_cb (MissionControl *mc,
627 TpConnectionStatus status,
629 TpConnectionStatusReason reason,
630 const gchar *unique_name,
631 EmpathyDispatcher *dispatcher)
635 account = mc_account_lookup (unique_name);
636 dispatcher_update_account (dispatcher, account);
637 g_object_unref (account);
641 dispatcher_finalize (GObject *object)
643 EmpathyDispatcherPriv *priv = GET_PRIV (object);
646 empathy_disconnect_account_status_changed (priv->token);
647 g_object_unref (priv->mc);
649 for (l = priv->tubes; l; l = l->next) {
650 g_signal_handlers_disconnect_by_func (l->data,
651 dispatcher_tubes_channel_invalidated_cb,
653 g_object_unref (l->data);
655 g_slist_free (priv->tubes);
657 g_hash_table_destroy (priv->connections);
661 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
663 GObjectClass *object_class = G_OBJECT_CLASS (klass);
665 object_class->finalize = dispatcher_finalize;
667 signals[DISPATCH_CHANNEL] =
668 g_signal_new ("dispatch-channel",
669 G_TYPE_FROM_CLASS (klass),
673 g_cclosure_marshal_VOID__OBJECT,
676 signals[FILTER_CHANNEL] =
677 g_signal_new ("filter-channel",
678 G_TYPE_FROM_CLASS (klass),
682 g_cclosure_marshal_VOID__OBJECT,
685 signals[FILTER_TUBE] =
686 g_signal_new ("filter-tube",
687 G_TYPE_FROM_CLASS (klass),
691 g_cclosure_marshal_VOID__BOXED,
693 1, EMPATHY_TYPE_DISPATCHER_TUBE);
695 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
699 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
702 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
703 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
705 dispatcher->priv = priv;
706 priv->mc = empathy_mission_control_new ();
707 priv->token = empathy_connect_to_account_status_changed (priv->mc,
708 G_CALLBACK (dispatcher_status_changed_cb),
711 priv->connections = g_hash_table_new_full (empathy_account_hash,
712 empathy_account_equal,
715 accounts = mc_accounts_list_by_enabled (TRUE);
716 for (l = accounts; l; l = l->next) {
717 dispatcher_update_account (dispatcher, l->data);
718 g_object_unref (l->data);
720 g_list_free (accounts);
724 empathy_dispatcher_new (void)
727 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
728 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
730 g_object_ref (dispatcher);
737 const gchar *channel_type;
740 } DispatcherRequestData;
743 dispatcher_request_channel_cb (TpConnection *connection,
744 const gchar *object_path,
747 GObject *weak_object)
749 DispatcherRequestData *data = (DispatcherRequestData*) user_data;
752 DEBUG ("Error: %s", error->message);
759 channel = tp_channel_new (connection, object_path,
764 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
769 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
773 TpConnection *connection;
776 EmpathyContactFactory *factory;
777 EmpathyTpGroup *group;
778 EmpathyContact *self_contact;
779 GError *error = NULL;
781 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
783 mc = empathy_mission_control_new ();
784 account = empathy_contact_get_account (contact);
785 connection = mission_control_get_tpconnection (mc, account, NULL);
786 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
789 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
790 * will be catched in EmpathyFilter */
791 if (!tp_cli_connection_run_request_channel (connection, -1,
792 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
799 DEBUG ("Couldn't request channel: %s",
800 error ? error->message : "No error given");
801 g_clear_error (&error);
802 g_object_unref (connection);
806 channel = tp_channel_new (connection,
807 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
808 TP_HANDLE_TYPE_NONE, 0, NULL);
810 group = empathy_tp_group_new (channel);
811 empathy_run_until_ready (group);
813 factory = empathy_contact_factory_new ();
814 self_contact = empathy_contact_factory_get_user (factory, account);
815 empathy_contact_run_until_ready (self_contact,
816 EMPATHY_CONTACT_READY_HANDLE,
819 empathy_tp_group_add_member (group, contact, "");
820 empathy_tp_group_add_member (group, self_contact, "");
822 g_object_unref (factory);
823 g_object_unref (self_contact);
824 g_object_unref (group);
825 g_object_unref (connection);
826 g_object_unref (channel);
827 g_free (object_path);
831 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
833 EmpathyContactFactory *factory;
834 EmpathyContact *contact;
836 factory = empathy_contact_factory_new ();
837 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
838 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
840 empathy_dispatcher_call_with_contact (contact);
842 g_object_unref (contact);
843 g_object_unref (factory);
847 empathy_dispatcher_chat_with_contact (EmpathyContact *contact)
851 TpConnection *connection;
852 DispatcherRequestData *data;
854 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
856 mc = empathy_mission_control_new ();
857 account = empathy_contact_get_account (contact);
858 connection = mission_control_get_tpconnection (mc, account, NULL);
859 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
862 /* We abuse of suppress_handler, TRUE means OUTGOING. */
863 data = g_new (DispatcherRequestData, 1);
864 data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
865 data->handle_type = TP_HANDLE_TYPE_CONTACT;
866 data->handle = empathy_contact_get_handle (contact);
867 tp_cli_connection_call_request_channel (connection, -1,
872 dispatcher_request_channel_cb,
875 g_object_unref (connection);
879 empathy_dispatcher_chat_with_contact_id (McAccount *account,
880 const gchar *contact_id)
882 EmpathyContactFactory *factory;
883 EmpathyContact *contact;
885 factory = empathy_contact_factory_new ();
886 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
887 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
889 empathy_dispatcher_chat_with_contact (contact);
891 g_object_unref (contact);
892 g_object_unref (factory);