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, room_ids[0]);
475 empathy_chatroom_manager_add (mgr, chatroom);
479 g_object_ref (chatroom);
482 g_object_set (chatroom, "tp-channel", channel, NULL);
484 g_signal_connect (channel, "invalidated",
485 G_CALLBACK (chatroom_invalidated_cb), chatroom);
487 g_free (room_ids[0]);
489 g_array_free (handles, TRUE);
491 g_object_unref (account);
492 g_object_unref (chatroom);
493 g_object_unref (mgr);
496 if (suppress_handler) {
497 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
499 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
502 g_object_unref (channel);
506 dispatcher_connection_list_channels_cb (TpConnection *connection,
507 const GPtrArray *channels,
515 DEBUG ("Error: %s", error->message);
519 g_object_set_data (G_OBJECT (connection), "had-channels",
520 GUINT_TO_POINTER (1));
522 for (i = 0; i < channels->len; i++) {
525 values = g_ptr_array_index (channels, i);
526 dispatcher_connection_new_channel_cb (connection,
527 g_value_get_boxed (g_value_array_get_nth (values, 0)),
528 g_value_get_string (g_value_array_get_nth (values, 1)),
529 g_value_get_uint (g_value_array_get_nth (values, 2)),
530 g_value_get_uint (g_value_array_get_nth (values, 3)),
531 FALSE, user_data, dispatcher);
536 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
537 const GPtrArray *capabilities,
543 DEBUG ("Error: %s", error->message);
548 dispatcher_connection_ready_cb (TpConnection *connection,
552 GPtrArray *capabilities;
555 const gchar *remove = NULL;
558 dispatcher_connection_invalidated_cb (connection,
566 g_signal_connect (connection, "invalidated",
567 G_CALLBACK (dispatcher_connection_invalidated_cb),
569 tp_cli_connection_connect_to_new_channel (connection,
570 dispatcher_connection_new_channel_cb,
572 G_OBJECT (dispatcher), NULL);
573 tp_cli_connection_call_list_channels (connection, -1,
574 dispatcher_connection_list_channels_cb,
576 G_OBJECT (dispatcher));
578 /* Advertise VoIP capabilities */
579 capabilities = g_ptr_array_sized_new (1);
580 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
581 G_TYPE_UINT, G_TYPE_INVALID);
582 g_value_init (&cap, cap_type);
583 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
584 dbus_g_type_struct_set (&cap,
585 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
586 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
587 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
588 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
589 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
591 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
593 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
595 capabilities, &remove,
596 dispatcher_connection_advertise_capabilities_cb,
597 NULL, NULL, G_OBJECT (dispatcher));
598 /* FIXME: Is that leaked? */
602 dispatcher_update_account (EmpathyDispatcher *dispatcher,
605 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
606 TpConnection *connection;
608 connection = g_hash_table_lookup (priv->connections, account);
613 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
618 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
619 tp_connection_call_when_ready (connection,
620 dispatcher_connection_ready_cb,
625 dispatcher_status_changed_cb (MissionControl *mc,
626 TpConnectionStatus status,
628 TpConnectionStatusReason reason,
629 const gchar *unique_name,
630 EmpathyDispatcher *dispatcher)
634 account = mc_account_lookup (unique_name);
635 dispatcher_update_account (dispatcher, account);
636 g_object_unref (account);
640 dispatcher_finalize (GObject *object)
642 EmpathyDispatcherPriv *priv = GET_PRIV (object);
645 empathy_disconnect_account_status_changed (priv->token);
646 g_object_unref (priv->mc);
648 for (l = priv->tubes; l; l = l->next) {
649 g_signal_handlers_disconnect_by_func (l->data,
650 dispatcher_tubes_channel_invalidated_cb,
652 g_object_unref (l->data);
654 g_slist_free (priv->tubes);
656 g_hash_table_destroy (priv->connections);
660 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
662 GObjectClass *object_class = G_OBJECT_CLASS (klass);
664 object_class->finalize = dispatcher_finalize;
666 signals[DISPATCH_CHANNEL] =
667 g_signal_new ("dispatch-channel",
668 G_TYPE_FROM_CLASS (klass),
672 g_cclosure_marshal_VOID__OBJECT,
675 signals[FILTER_CHANNEL] =
676 g_signal_new ("filter-channel",
677 G_TYPE_FROM_CLASS (klass),
681 g_cclosure_marshal_VOID__OBJECT,
684 signals[FILTER_TUBE] =
685 g_signal_new ("filter-tube",
686 G_TYPE_FROM_CLASS (klass),
690 g_cclosure_marshal_VOID__BOXED,
692 1, EMPATHY_TYPE_DISPATCHER_TUBE);
694 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
698 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
701 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
702 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
704 dispatcher->priv = priv;
705 priv->mc = empathy_mission_control_new ();
706 priv->token = empathy_connect_to_account_status_changed (priv->mc,
707 G_CALLBACK (dispatcher_status_changed_cb),
710 priv->connections = g_hash_table_new_full (empathy_account_hash,
711 empathy_account_equal,
714 accounts = mc_accounts_list_by_enabled (TRUE);
715 for (l = accounts; l; l = l->next) {
716 dispatcher_update_account (dispatcher, l->data);
717 g_object_unref (l->data);
719 g_list_free (accounts);
723 empathy_dispatcher_new (void)
726 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
727 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
729 g_object_ref (dispatcher);
736 const gchar *channel_type;
739 } DispatcherRequestData;
742 dispatcher_request_channel_cb (TpConnection *connection,
743 const gchar *object_path,
746 GObject *weak_object)
748 DispatcherRequestData *data = (DispatcherRequestData*) user_data;
751 DEBUG ("Error: %s", error->message);
758 channel = tp_channel_new (connection, object_path,
763 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
768 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
772 TpConnection *connection;
775 EmpathyContactFactory *factory;
776 EmpathyTpGroup *group;
777 EmpathyContact *self_contact;
778 GError *error = NULL;
780 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
782 mc = empathy_mission_control_new ();
783 account = empathy_contact_get_account (contact);
784 connection = mission_control_get_tpconnection (mc, account, NULL);
785 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
788 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
789 * will be catched in EmpathyFilter */
790 if (!tp_cli_connection_run_request_channel (connection, -1,
791 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
798 DEBUG ("Couldn't request channel: %s",
799 error ? error->message : "No error given");
800 g_clear_error (&error);
801 g_object_unref (connection);
805 channel = tp_channel_new (connection,
806 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
807 TP_HANDLE_TYPE_NONE, 0, NULL);
809 group = empathy_tp_group_new (channel);
810 empathy_run_until_ready (group);
812 factory = empathy_contact_factory_new ();
813 self_contact = empathy_contact_factory_get_user (factory, account);
814 empathy_contact_run_until_ready (self_contact,
815 EMPATHY_CONTACT_READY_HANDLE,
818 empathy_tp_group_add_member (group, contact, "");
819 empathy_tp_group_add_member (group, self_contact, "");
821 g_object_unref (factory);
822 g_object_unref (self_contact);
823 g_object_unref (group);
824 g_object_unref (connection);
825 g_object_unref (channel);
826 g_free (object_path);
830 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
832 EmpathyContactFactory *factory;
833 EmpathyContact *contact;
835 factory = empathy_contact_factory_new ();
836 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
837 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
839 empathy_dispatcher_call_with_contact (contact);
841 g_object_unref (contact);
842 g_object_unref (factory);
846 empathy_dispatcher_chat_with_contact (EmpathyContact *contact)
850 TpConnection *connection;
851 DispatcherRequestData *data;
853 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
855 mc = empathy_mission_control_new ();
856 account = empathy_contact_get_account (contact);
857 connection = mission_control_get_tpconnection (mc, account, NULL);
858 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
861 /* We abuse of suppress_handler, TRUE means OUTGOING. */
862 data = g_new (DispatcherRequestData, 1);
863 data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
864 data->handle_type = TP_HANDLE_TYPE_CONTACT;
865 data->handle = empathy_contact_get_handle (contact);
866 tp_cli_connection_call_request_channel (connection, -1,
871 dispatcher_request_channel_cb,
874 g_object_unref (connection);
878 empathy_dispatcher_chat_with_contact_id (McAccount *account,
879 const gchar *contact_id)
881 EmpathyContactFactory *factory;
882 EmpathyContact *contact;
884 factory = empathy_contact_factory_new ();
885 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
886 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
888 empathy_dispatcher_chat_with_contact (contact);
890 g_object_unref (contact);
891 g_object_unref (factory);