2 * Copyright (C) 2007-2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n-lib.h>
27 #include <telepathy-glib/enums.h>
28 #include <telepathy-glib/connection.h>
29 #include <telepathy-glib/util.h>
30 #include <telepathy-glib/dbus.h>
31 #include <telepathy-glib/proxy-subclass.h>
33 #include <libmissioncontrol/mission-control.h>
34 #include <libmissioncontrol/mc-account.h>
36 #include <extensions/extensions.h>
38 #include "empathy-dispatcher.h"
39 #include "empathy-utils.h"
40 #include "empathy-tube-handler.h"
41 #include "empathy-account-manager.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-tp-group.h"
44 #include "empathy-tp-file.h"
45 #include "empathy-chatroom-manager.h"
46 #include "empathy-utils.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
49 #include <libempathy/empathy-debug.h>
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
53 EmpathyAccountManager *account_manager;
55 /* connection to connection data mapping */
56 GHashTable *connections;
57 /* accounts to connection mapping */
61 EmpathyChatroomManager *chatroom_mgr;
62 } EmpathyDispatcherPriv;
64 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
73 static guint signals[LAST_SIGNAL];
74 static EmpathyDispatcher *dispatcher = NULL;
77 EmpathyDispatcherTube public;
78 EmpathyContactFactory *factory;
86 EmpathyDispatcher *dispatcher;
87 EmpathyDispatchOperation *operation;
88 TpConnection *connection;
92 EmpathyContact *contact;
93 EmpathyDispatcherRequestCb *cb;
95 gpointer *request_data;
96 } DispatcherRequestData;
100 /* Channel type specific wrapper object */
101 GObject *channel_wrapper;
106 /* ObjectPath => DispatchData.. */
107 GHashTable *dispatched_channels;
108 /* ObjectPath -> EmpathyDispatchOperations */
109 GHashTable *dispatching_channels;
110 /* ObjectPath -> EmpathyDispatchOperations */
111 GHashTable *outstanding_channels;
112 /* List of DispatcherRequestData */
113 GList *outstanding_requests;
116 static DispatchData *
117 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
119 DispatchData *d = g_slice_new0 (DispatchData);
120 d->channel = channel;
121 d->channel_wrapper = channel_wrapper;
127 free_dispatch_data (DispatchData *data)
129 g_object_unref (data->channel);
130 g_object_unref (data->channel_wrapper);
132 g_slice_free (DispatchData, data);
136 static DispatcherRequestData *
137 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
138 TpConnection *connection, const gchar *channel_type, guint handle_type,
139 guint handle, EmpathyContact *contact,
140 EmpathyDispatcherRequestCb *cb, gpointer user_data)
142 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
144 result->dispatcher = dispatcher;
145 result->connection = connection;
147 result->channel_type = g_strdup (channel_type);
148 result->handle_type = handle_type;
149 result->handle = handle;
152 result->contact = g_object_ref (contact);
155 result->user_data = user_data;
161 free_dispatcher_request_data (DispatcherRequestData *r)
163 g_free (r->channel_type);
165 if (r->contact != NULL)
166 g_object_unref (r->contact);
168 g_slice_free (DispatcherRequestData, r);
171 static ConnectionData *
172 new_connection_data (McAccount *account)
174 ConnectionData *cd = g_slice_new0 (ConnectionData);
175 cd->account = g_object_ref (account);
177 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
178 g_free, (GDestroyNotify) free_dispatch_data);
180 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
181 g_free, g_object_unref);
183 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
190 free_connection_data (ConnectionData *cd)
193 g_object_unref (cd->account);
194 g_hash_table_destroy (cd->dispatched_channels);
195 g_hash_table_destroy (cd->dispatching_channels);
197 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
199 free_dispatcher_request_data (l->data);
205 empathy_dispatcher_tube_get_type (void)
207 static GType type_id = 0;
210 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
211 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
212 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
218 EmpathyDispatcherTube *
219 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
221 DispatcherTube *tube = (DispatcherTube*) data;
223 g_return_val_if_fail (tube != NULL, NULL);
231 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
233 DispatcherTube *tube = (DispatcherTube*) data;
235 g_return_if_fail (tube != NULL);
237 if (--tube->ref_count == 0) {
238 if (!tube->handled) {
239 DEBUG ("Tube can't be handled, closing");
240 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
246 g_free (tube->bus_name);
247 g_free (tube->object_path);
248 g_object_unref (tube->factory);
249 g_object_unref (tube->public.channel);
250 g_object_unref (tube->public.initiator);
251 g_slice_free (DispatcherTube, tube);
256 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
261 DispatcherTube *tube = user_data;
264 DEBUG ("Error: %s", error->message);
266 tube->handled = TRUE;
272 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
273 EmpathyDispatcherTube *etube)
275 DispatcherTube *tube = (DispatcherTube*) etube;
277 if (tube->public.activatable) {
284 /* Create the proxy for the tube handler */
285 thandler = g_object_new (TP_TYPE_PROXY,
286 "dbus-connection", tp_get_bus (),
287 "bus-name", tube->bus_name,
288 "object-path", tube->object_path,
290 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
292 /* Give the tube to the handler */
293 g_object_get (tube->public.channel,
294 "connection", &connection,
295 "object-path", &object_path,
296 "handle_type", &handle_type,
300 DEBUG ("Dispatching tube");
301 emp_cli_tube_handler_call_handle_tube (thandler, -1,
302 connection->bus_name,
303 connection->object_path,
304 object_path, handle_type,
305 handle, tube->public.id,
306 dispatcher_tubes_handle_tube_cb,
307 empathy_dispatcher_tube_ref (etube),
308 (GDestroyNotify) empathy_dispatcher_tube_unref,
309 G_OBJECT (dispatcher));
311 g_object_unref (thandler);
312 g_object_unref (connection);
313 g_free (object_path);
318 dispatcher_tubes_new_tube_cb (TpChannel *channel,
322 const gchar *service,
323 GHashTable *parameters,
328 static TpDBusDaemon *daemon = NULL;
329 DispatcherTube *tube;
333 gboolean running = FALSE;
334 GError *error = NULL;
336 /* Increase tube count */
337 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
338 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
339 DEBUG ("Increased tube count for channel %p: %d", channel, number);
341 /* We dispatch only local pending tubes */
342 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
347 daemon = tp_dbus_daemon_new (tp_get_bus ());
350 account = empathy_channel_get_account (channel);
351 tube = g_slice_new (DispatcherTube);
353 tube->handled = FALSE;
354 tube->factory = empathy_contact_factory_dup_singleton ();
355 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
356 tube->object_path = empathy_tube_handler_build_object_path (type, service);
357 tube->public.activatable = FALSE;
358 tube->public.id = id;
359 tube->public.channel = g_object_ref (channel);
360 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
363 g_object_unref (account);
365 DEBUG ("Looking for tube handler: %s", tube->bus_name);
366 /* Check if that bus-name has an owner, if it has one that means the
367 * app is already running and we can directly give the channel. */
368 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
369 &running, NULL, NULL);
371 DEBUG ("Tube handler running");
372 tube->public.activatable = TRUE;
373 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
374 (EmpathyDispatcherTube*) tube);
375 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
379 DEBUG ("Tube handler is not running. Try to activate it");
380 /* Check if that bus-name is activatable, if not that means the
381 * application needed to handle this tube isn't installed. */
382 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
385 DEBUG ("Error listing activatable names: %s", error->message);
386 g_clear_error (&error);
390 for (name = names; *name; name++) {
391 if (!tp_strdiff (*name, tube->bus_name)) {
392 DEBUG ("Found tube handler");
393 tube->public.activatable = TRUE;
400 if (!tube->public.activatable)
401 DEBUG ("Didn't find tube handler");
403 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
404 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
408 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
409 const GPtrArray *tubes,
417 DEBUG ("Error: %s", error->message);
421 for (i = 0; i < tubes->len; i++) {
424 values = g_ptr_array_index (tubes, i);
425 dispatcher_tubes_new_tube_cb (channel,
426 g_value_get_uint (g_value_array_get_nth (values, 0)),
427 g_value_get_uint (g_value_array_get_nth (values, 1)),
428 g_value_get_uint (g_value_array_get_nth (values, 2)),
429 g_value_get_string (g_value_array_get_nth (values, 3)),
430 g_value_get_boxed (g_value_array_get_nth (values, 4)),
431 g_value_get_uint (g_value_array_get_nth (values, 5)),
432 user_data, dispatcher);
437 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
441 EmpathyDispatcher *dispatcher)
443 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
445 DEBUG ("%s", message);
447 priv->tubes = g_slist_remove (priv->tubes, proxy);
448 g_object_unref (proxy);
452 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
459 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
461 DEBUG ("No more tube, closing channel");
462 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
464 else if (number > 1) {
465 DEBUG ("Decrease tube count: %d", number);
466 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
472 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
475 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
479 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
480 g_signal_connect (channel, "invalidated",
481 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
484 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
485 dispatcher_tubes_tube_closed_cb,
487 G_OBJECT (dispatcher), NULL);
488 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
489 dispatcher_tubes_new_tube_cb,
491 G_OBJECT (dispatcher), NULL);
492 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
493 dispatcher_tubes_list_tubes_cb,
495 G_OBJECT (dispatcher));
501 dispatcher_connection_invalidated_cb (TpConnection *connection,
502 guint domain, gint code, gchar *message,
503 EmpathyDispatcher *dispatcher)
505 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
508 DEBUG ("Error: %s", message);
509 cd = g_hash_table_lookup (priv->connections, connection);
511 g_hash_table_remove (priv->accounts, cd->account);
512 g_hash_table_remove (priv->connections, connection);
519 EmpathyDispatcher *self;
520 EmpathyChatroom *chatroom;
521 } dispatcher_connection_invalidated_cb_ctx;
523 static dispatcher_connection_invalidated_cb_ctx *
524 dispatcher_connection_invalidated_cb_ctx_new (EmpathyDispatcher *dispatcher,
525 EmpathyChatroom *chatroom)
527 dispatcher_connection_invalidated_cb_ctx *ctx;
529 ctx = g_slice_new (dispatcher_connection_invalidated_cb_ctx);
531 ctx->self = g_object_ref (dispatcher);
532 ctx->chatroom = g_object_ref (chatroom);
538 dispatcher_connection_invalidated_cb_ctx_free (
539 dispatcher_connection_invalidated_cb_ctx *ctx)
541 g_object_unref (ctx->self);
542 g_object_unref (ctx->chatroom);
544 g_slice_free (dispatcher_connection_invalidated_cb_ctx, ctx);
547 static void dispatcher_chatroom_invalidated_cb (
552 dispatcher_connection_invalidated_cb_ctx *ctx)
554 EmpathyDispatcherPriv *priv = GET_PRIV (ctx->self);
557 g_object_get (ctx->chatroom, "favorite", &favorite, NULL);
561 /* Chatroom is in favorites so don't remove it from the manager */
562 g_object_set (ctx->chatroom, "tp-channel", NULL, NULL);
566 empathy_chatroom_manager_remove (priv->chatroom_mgr, ctx->chatroom);
573 /********************* Sanity from here at some point *********/
575 dispatcher_operation_can_start (EmpathyDispatcher *self,
576 EmpathyDispatchOperation *operation, ConnectionData *cd)
579 const gchar *channel_type =
580 empathy_dispatch_operation_get_channel_type (operation);
582 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
584 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
586 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
596 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
597 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
601 l = cd->outstanding_requests;
604 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
609 if (d->operation == operation)
614 d->cb (NULL, error, d->user_data);
616 d->cb (operation, NULL, d->user_data);
619 cd->outstanding_requests = g_list_delete_link
620 (cd->outstanding_requests, lt);
622 free_dispatcher_request_data (d);
628 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
629 gchar *message, EmpathyDispatcher *dispatcher)
631 /* Channel went away... */
632 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
633 TpConnection *connection;
634 EmpathyDispatchOperation *operation;
636 const gchar *object_path;
638 g_object_get (G_OBJECT (proxy), "connection", &connection, NULL);
640 cd = g_hash_table_lookup (priv->connections, connection);
642 object_path = tp_proxy_get_object_path (proxy);
644 DEBUG ("Channel %s invalidated", object_path);
646 g_hash_table_remove (cd->dispatched_channels, object_path);
647 g_hash_table_remove (cd->dispatching_channels, object_path);
649 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
650 if (operation != NULL)
652 GError error = { domain, code, message };
653 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
654 g_hash_table_remove (cd->outstanding_channels, object_path);
655 g_object_unref (operation);
660 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
661 EmpathyDispatcher *dispatcher)
663 g_assert (empathy_dispatch_operation_is_incoming (operation));
664 DEBUG ("Send of for dispatching: %s",
665 empathy_dispatch_operation_get_object_path (operation));
666 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
670 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
671 EmpathyDispatcher *dispatcher)
673 /* Our job is done, remove the dispatch operation and mark the channel as
675 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
676 TpConnection *connection;
678 const gchar *object_path;
680 connection = empathy_dispatch_operation_get_tp_connection (operation);
681 cd = g_hash_table_lookup (priv->connections, connection);
682 g_assert (cd != NULL);
683 g_object_unref (G_OBJECT (connection));
685 object_path = empathy_dispatch_operation_get_object_path (operation);
687 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
690 d = new_dispatch_data (
691 empathy_dispatch_operation_get_channel (operation),
692 empathy_dispatch_operation_get_channel_wrapper (operation));
693 g_hash_table_insert (cd->dispatched_channels,
694 g_strdup (object_path), d);
696 g_hash_table_remove (cd->dispatching_channels, object_path);
698 DEBUG ("Channel claimed: %s", object_path);
702 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
703 EmpathyDispatcher *dispatcher)
705 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
706 TpConnection *connection;
708 EmpathyDispatchOperationState status;
710 g_signal_connect (operation, "approved",
711 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
713 g_signal_connect (operation, "claimed",
714 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
716 /* Signal the observers */
717 DEBUG ("Send to observers: %s",
718 empathy_dispatch_operation_get_object_path (operation));
719 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
721 empathy_dispatch_operation_start (operation);
723 /* Signal potential requestors */
724 connection = empathy_dispatch_operation_get_tp_connection (operation);
725 cd = g_hash_table_lookup (priv->connections, connection);
726 g_assert (cd != NULL);
727 g_object_unref (G_OBJECT (connection));
729 g_object_ref (operation);
731 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
732 status = empathy_dispatch_operation_get_status (operation);
733 g_object_unref (operation);
735 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
738 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
740 DEBUG ("Send to approvers: %s",
741 empathy_dispatch_operation_get_object_path (operation));
742 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
746 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
747 DEBUG ("Send of for dispatching: %s",
748 empathy_dispatch_operation_get_object_path (operation));
749 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
755 dispatcher_start_dispatching (EmpathyDispatcher *self,
756 EmpathyDispatchOperation *operation, ConnectionData *cd)
758 const gchar *object_path =
759 empathy_dispatch_operation_get_object_path (operation);
761 DEBUG ("Dispatching process started for %s", object_path);
763 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
765 g_assert (g_hash_table_lookup (cd->outstanding_channels,
766 object_path) == NULL);
768 g_hash_table_insert (cd->dispatching_channels,
769 g_strdup (object_path), operation);
771 switch (empathy_dispatch_operation_get_status (operation))
773 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
774 g_signal_connect (operation, "ready",
775 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
777 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
778 dispatch_operation_ready_cb (operation, dispatcher);
781 g_assert_not_reached();
785 else if (empathy_dispatch_operation_get_status (operation) >=
786 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
788 /* Already dispatching and the operation is pending, thus the observers
789 * have seen it (if applicable), so we can flush the request right away.
791 dispatch_operation_flush_requests (self, operation, NULL, cd);
797 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
803 g_hash_table_iter_init (&iter, cd->outstanding_channels);
804 while (g_hash_table_iter_next (&iter, NULL, &value))
806 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
808 if (dispatcher_operation_can_start (self, operation, cd))
810 dispatcher_start_dispatching (dispatcher, operation, cd);
811 g_hash_table_iter_remove (&iter);
818 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
819 TpConnection *connection,
820 const gchar *object_path, const gchar *channel_type,
821 guint handle_type, guint handle, GHashTable *properties,
824 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
827 EmpathyDispatchOperation *operation;
828 EmpathyContact *contact = NULL;
830 cd = g_hash_table_lookup (priv->connections, connection);
832 /* Don't bother with channels we have already dispatched or are dispatching
833 * currently. This can happen when NewChannel(s) is fired after
834 * RequestChannel/CreateChannel/EnsureChannel */
835 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
838 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
841 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
843 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
846 DEBUG ("New channel of type %s on %s",
847 channel_type, object_path);
849 if (properties == NULL)
850 channel = tp_channel_new (connection, object_path, channel_type,
851 handle_type, handle, NULL);
853 channel = tp_channel_new_from_properties (connection, object_path,
856 g_signal_connect (channel, "invalidated",
857 G_CALLBACK (dispatcher_channel_invalidated_cb),
860 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
862 EmpathyContactFactory *factory = empathy_contact_factory_new ();
863 contact = empathy_contact_factory_get_from_handle (factory,
864 cd->account, handle);
865 g_object_unref (factory);
868 operation = empathy_dispatch_operation_new (connection, channel, contact,
871 g_object_unref (channel);
875 /* Request could either be by us or by a remote party. If there are no
876 * outstanding requests for this channel type we can assume it's remote.
877 * Otherwise we wait untill they are all satisfied */
878 if (dispatcher_operation_can_start (dispatcher, operation, cd))
879 dispatcher_start_dispatching (dispatcher, operation, cd);
881 g_hash_table_insert (cd->outstanding_channels,
882 g_strdup (object_path), operation);
886 dispatcher_start_dispatching (dispatcher, operation, cd);
891 dispatcher_connection_new_channel_cb (TpConnection *connection,
892 const gchar *object_path, const gchar *channel_type,
893 guint handle_type, guint handle,
894 gboolean suppress_handler, gpointer user_data,
897 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
899 /* Empathy heavily abuses surpress handler (don't try this at home), if
900 * surpress handler is true then it is an outgoing channel, which is
901 * requested either by us or some other party (like the megaphone applet).
902 * Otherwise it's an incoming channel */
903 dispatcher_connection_new_channel (dispatcher, connection,
904 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
908 dispatcher_connection_new_channel_with_properties (
909 EmpathyDispatcher *dispatcher, TpConnection *connection,
910 const gchar *object_path, GHashTable *properties)
912 const gchar *channel_type;
919 channel_type = tp_asv_get_string (properties,
920 TP_IFACE_CHANNEL ".ChannelType");
921 if (channel_type == NULL)
923 g_message ("%s had an invalid ChannelType property", object_path);
927 handle_type = tp_asv_get_uint32 (properties,
928 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
931 g_message ("%s had an invalid TargetHandleType property", object_path);
935 handle = tp_asv_get_uint32 (properties,
936 TP_IFACE_CHANNEL ".TargetHandle", &valid);
939 g_message ("%s had an invalid TargetHandle property", object_path);
943 /* We assume there is no channel dispather, so we're the only one dispatching
944 * it. Which means that a requested channel it is outgoing one */
945 requested = tp_asv_get_boolean (properties,
946 TP_IFACE_CHANNEL ".Requested", &valid);
949 g_message ("%s had an invalid Requested property", object_path);
953 dispatcher_connection_new_channel (dispatcher, connection,
954 object_path, channel_type, handle_type, handle, properties, !requested);
959 dispatcher_connection_new_channels_cb (
960 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
963 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
966 for (i = 0; i < channels->len ; i++)
968 GValueArray *arr = g_ptr_array_index (channels, i);
969 const gchar *object_path;
970 GHashTable *properties;
972 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
973 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
975 dispatcher_connection_new_channel_with_properties (dispatcher,
976 connection, object_path, properties);
980 #if 0 /* old dispatching */
981 channel = tp_channel_new (connection, object_path, channel_type,
982 handle_type, handle, NULL);
983 tp_channel_run_until_ready (channel, NULL, NULL);
985 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
986 dispatcher_tubes_handle_channel (dispatcher, channel);
989 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
990 handle_type == TP_HANDLE_TYPE_ROOM)
992 /* Add the chatroom to the chatroom manager */
993 EmpathyChatroom *chatroom;
998 dispatcher_connection_invalidated_cb_ctx *ctx;
1000 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1001 g_array_append_val (handles, handle);
1003 tp_cli_connection_run_inspect_handles (connection, -1,
1004 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
1006 mc = empathy_mission_control_new ();
1007 account = mission_control_get_account_for_tpconnection (mc, connection,
1010 chatroom = empathy_chatroom_manager_find (priv->chatroom_mgr, account,
1012 if (chatroom == NULL)
1014 chatroom = empathy_chatroom_new (account);
1015 empathy_chatroom_set_name (chatroom, room_ids[0]);
1016 empathy_chatroom_set_room (chatroom, room_ids[0]);
1017 empathy_chatroom_manager_add (priv->chatroom_mgr, chatroom);
1021 g_object_ref (chatroom);
1024 g_object_set (chatroom, "tp-channel", channel, NULL);
1026 ctx = dispatcher_connection_invalidated_cb_ctx_new (dispatcher, chatroom);
1028 g_signal_connect_data (channel, "invalidated",
1029 G_CALLBACK (dispatcher_chatroom_invalidated_cb), ctx,
1030 (GClosureNotify) dispatcher_connection_invalidated_cb_ctx_free, 0);
1032 g_free (room_ids[0]);
1034 g_array_free (handles, TRUE);
1035 g_object_unref (mc);
1036 g_object_unref (account);
1037 g_object_unref (chatroom);
1040 if (suppress_handler) {
1041 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
1043 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
1046 g_object_unref (channel);
1052 dispatcher_connection_got_channels_property (TpProxy *proxy,
1053 const GValue *channels_prop, const GError *error, gpointer user_data,
1056 GPtrArray *channels;
1059 DEBUG ("Error: %s", error->message);
1063 channels = g_value_get_boxed (channels_prop);
1064 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
1065 channels, NULL, object);
1069 dispatcher_connection_list_channels_cb (TpConnection *connection,
1070 const GPtrArray *channels,
1071 const GError *error,
1073 GObject *dispatcher)
1078 DEBUG ("Error: %s", error->message);
1082 for (i = 0; i < channels->len; i++) {
1083 GValueArray *values;
1085 values = g_ptr_array_index (channels, i);
1086 /* We don't have any extra info, so assume already existing channels are
1088 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
1090 g_value_get_boxed (g_value_array_get_nth (values, 0)),
1091 g_value_get_string (g_value_array_get_nth (values, 1)),
1092 g_value_get_uint (g_value_array_get_nth (values, 2)),
1093 g_value_get_uint (g_value_array_get_nth (values, 3)),
1099 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
1100 const GPtrArray *capabilities,
1101 const GError *error,
1103 GObject *dispatcher)
1106 DEBUG ("Error: %s", error->message);
1111 dispatcher_connection_ready_cb (TpConnection *connection,
1112 const GError *error, gpointer dispatcher)
1114 GPtrArray *capabilities;
1117 const gchar *remove = NULL;
1121 dispatcher_connection_invalidated_cb (connection, error->domain,
1122 error->code, error->message, dispatcher);
1126 g_signal_connect (connection, "invalidated",
1127 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
1130 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
1131 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1133 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
1134 dispatcher_connection_new_channels_cb,
1135 NULL, NULL, G_OBJECT (dispatcher), NULL);
1137 tp_cli_dbus_properties_call_get (connection, -1,
1138 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
1139 dispatcher_connection_got_channels_property,
1140 NULL, NULL, dispatcher);
1144 tp_cli_connection_connect_to_new_channel (connection,
1145 dispatcher_connection_new_channel_cb,
1146 NULL, NULL, G_OBJECT (dispatcher), NULL);
1148 tp_cli_connection_call_list_channels (connection, -1,
1149 dispatcher_connection_list_channels_cb, NULL, NULL,
1150 G_OBJECT (dispatcher));
1154 /* Advertise VoIP capabilities */
1155 capabilities = g_ptr_array_sized_new (1);
1156 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
1157 G_TYPE_UINT, G_TYPE_INVALID);
1158 g_value_init (&cap, cap_type);
1159 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
1160 dbus_g_type_struct_set (&cap,
1161 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1162 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
1163 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
1164 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
1165 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
1166 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
1168 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
1169 connection, -1, capabilities, &remove,
1170 dispatcher_connection_advertise_capabilities_cb,
1171 NULL, NULL, G_OBJECT (dispatcher));
1175 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
1177 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1178 TpConnection *connection;
1180 connection = g_hash_table_lookup (priv->accounts, account);
1181 if (connection != NULL)
1184 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
1185 if (connection == NULL)
1188 g_hash_table_insert (priv->connections, g_object_ref (connection),
1189 new_connection_data (account));
1191 g_hash_table_insert (priv->accounts, g_object_ref (account),
1192 g_object_ref (connection));
1194 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
1199 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
1200 McAccount *account, TpConnectionStatusReason reason,
1201 TpConnectionStatus status, TpConnectionStatus previous,
1202 EmpathyDispatcher *dispatcher)
1204 dispatcher_update_account (dispatcher, account);
1208 dispatcher_finalize (GObject *object)
1210 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1212 g_signal_handlers_disconnect_by_func (priv->account_manager,
1213 dispatcher_account_connection_cb, object);
1215 g_object_unref (priv->account_manager);
1216 g_object_unref (priv->mc);
1218 g_hash_table_destroy (priv->connections);
1220 g_object_unref (priv->chatroom_mgr);
1224 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1226 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1228 object_class->finalize = dispatcher_finalize;
1231 g_signal_new ("observe",
1232 G_TYPE_FROM_CLASS (klass),
1236 g_cclosure_marshal_VOID__OBJECT,
1238 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1241 g_signal_new ("approve",
1242 G_TYPE_FROM_CLASS (klass),
1246 g_cclosure_marshal_VOID__OBJECT,
1248 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1251 g_signal_new ("dispatch",
1252 G_TYPE_FROM_CLASS (klass),
1256 g_cclosure_marshal_VOID__OBJECT,
1258 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1260 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1265 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1267 GList *accounts, *l;
1268 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1269 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1271 dispatcher->priv = priv;
1272 priv->mc = empathy_mission_control_new ();
1273 priv->account_manager = empathy_account_manager_dup_singleton ();
1275 g_signal_connect (priv->account_manager,
1276 "account-connection-changed",
1277 G_CALLBACK (dispatcher_account_connection_cb),
1280 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1281 empathy_account_equal, g_object_unref, g_object_unref);
1283 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1284 g_object_unref, (GDestroyNotify) free_connection_data);
1286 accounts = mc_accounts_list_by_enabled (TRUE);
1288 for (l = accounts; l; l = l->next) {
1289 dispatcher_update_account (dispatcher, l->data);
1290 g_object_unref (l->data);
1292 g_list_free (accounts);
1294 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
1298 empathy_get_dispatcher (void)
1301 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
1302 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
1304 g_object_ref (dispatcher);
1311 dispatcher_request_channel_cb (TpConnection *connection,
1312 const gchar *object_path, const GError *error,
1313 gpointer user_data, GObject *weak_object)
1315 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1316 EmpathyDispatcherPriv *priv = GET_PRIV (request_data->dispatcher);
1317 EmpathyDispatchOperation *operation = NULL;
1318 ConnectionData *conn_data;
1320 conn_data = g_hash_table_lookup (priv->connections,
1321 request_data->connection);
1325 DEBUG ("Channel request failed: %s", error->message);
1327 if (request_data->cb != NULL)
1328 request_data->cb (NULL, error, request_data->user_data);
1330 conn_data->outstanding_requests =
1331 g_list_remove (conn_data->outstanding_requests, request_data);
1332 free_dispatcher_request_data (request_data);
1337 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1340 if (operation != NULL)
1341 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1343 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1346 /* FIXME check if we got an existing channel back */
1347 if (operation == NULL)
1349 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1354 operation = empathy_dispatch_operation_new_with_wrapper (connection,
1355 data->channel, request_data->contact, FALSE,
1356 data->channel_wrapper);
1360 TpChannel *channel = tp_channel_new (connection, object_path,
1361 request_data->channel_type, request_data->handle_type,
1362 request_data->handle, NULL);
1364 g_signal_connect (channel, "invalidated",
1365 G_CALLBACK (dispatcher_channel_invalidated_cb),
1366 request_data->dispatcher);
1368 operation = empathy_dispatch_operation_new (connection, channel,
1369 request_data->contact, FALSE);
1370 g_object_unref (channel);
1375 /* Already existed set potential extra information */
1376 g_object_set (G_OBJECT (operation),
1377 "contact", request_data->contact,
1381 request_data->operation = operation;
1383 /* (pre)-approve this right away as we requested it */
1384 empathy_dispatch_operation_approve (operation);
1386 dispatcher_start_dispatching (request_data->dispatcher, operation,
1389 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1394 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1395 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1397 g_assert_not_reached ();
1401 empathy_dispatcher_call_with_contact_id (McAccount *account,
1402 const gchar *contact_id, EmpathyDispatcherRequestCb *callback,
1405 g_assert_not_reached ();
1409 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1411 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1413 request_data->handle = empathy_contact_get_handle (contact);
1415 /* Note this does rape the surpress handler semantics */
1416 tp_cli_connection_call_request_channel (request_data->connection, -1,
1417 request_data->channel_type,
1418 request_data->handle_type,
1419 request_data->handle,
1420 TRUE, dispatcher_request_channel_cb,
1421 request_data, NULL, NULL);
1425 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1426 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1428 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1429 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1430 McAccount *account = empathy_contact_get_account (contact);
1431 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1432 ConnectionData *connection_data =
1433 g_hash_table_lookup (priv->connections, connection);
1434 DispatcherRequestData *request_data;
1436 /* The contact handle might not be known yet */
1437 request_data = new_dispatcher_request_data (dispatcher, connection,
1438 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, contact, callback,
1441 connection_data->outstanding_requests = g_list_prepend
1442 (connection_data->outstanding_requests, request_data);
1444 empathy_contact_call_when_ready (contact,
1445 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1448 g_object_unref (dispatcher);
1452 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1453 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1455 EmpathyDispatcher *dispatcher = empathy_get_dispatcher ();
1456 EmpathyContactFactory *factory;
1457 EmpathyContact *contact;
1459 factory = empathy_contact_factory_dup_singleton ();
1460 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1462 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1464 g_object_unref (contact);
1465 g_object_unref (factory);
1466 g_object_unref (dispatcher);
1473 } FileChannelRequest;
1476 tp_file_state_notify_cb (EmpathyTpFile *tp_file)
1478 EmpFileTransferState state;
1480 state = empathy_tp_file_get_state (tp_file, NULL);
1481 if (state == EMP_FILE_TRANSFER_STATE_COMPLETED ||
1482 state == EMP_FILE_TRANSFER_STATE_CANCELLED) {
1483 DEBUG ("Transfer is done, unref the object");
1484 g_object_unref (tp_file);
1489 file_channel_create_cb (TpConnection *connection,
1490 const gchar *object_path,
1491 GHashTable *properties,
1492 const GError *error,
1494 GObject *weak_object)
1497 EmpathyTpFile *tp_file;
1498 FileChannelRequest *request = (FileChannelRequest *) user_data;
1501 DEBUG ("Couldn't request channel: %s", error->message);
1505 channel = tp_channel_new (connection,
1507 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1508 TP_HANDLE_TYPE_CONTACT,
1512 /* We give the ref to the callback, it is responsible to unref the
1513 * object once the transfer is done. */
1514 tp_file = empathy_tp_file_new (channel);
1515 empathy_tp_file_offer (tp_file, request->gfile, NULL);
1516 g_signal_connect (tp_file, "notify::state",
1517 G_CALLBACK (tp_file_state_notify_cb),
1520 g_object_unref (request->gfile);
1521 g_slice_free (FileChannelRequest, request);
1522 g_object_unref (channel);
1528 empathy_dispatcher_send_file (EmpathyContact *contact,
1531 g_assert_not_reached();
1538 TpConnection *connection;
1540 FileChannelRequest *request;
1545 GTimeVal last_modif;
1546 GError *error = NULL;
1548 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1549 g_return_if_fail (G_IS_FILE (gfile));
1551 info = g_file_query_info (gfile,
1552 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1553 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1554 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1558 DEBUG ("Can't get info about the file: %s", error->message);
1559 g_clear_error (&error);
1563 mc = empathy_mission_control_new ();
1564 account = empathy_contact_get_account (contact);
1565 connection = mission_control_get_tpconnection (mc, account, NULL);
1566 handle = empathy_contact_get_handle (contact);
1568 request = g_slice_new0 (FileChannelRequest);
1569 request->gfile = g_object_ref (gfile);
1570 request->handle = handle;
1572 filename = g_file_get_basename (request->gfile);
1573 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
1575 DEBUG ("Sending %s from a stream to %s (size %"G_GINT64_FORMAT", content-type %s)",
1576 filename, empathy_contact_get_name (contact),
1577 g_file_info_get_size (info),
1578 g_file_info_get_content_type (info));
1580 args = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1581 (GDestroyNotify) tp_g_value_slice_free);
1583 /* org.freedesktop.Telepathy.Channel.ChannelType */
1584 value = tp_g_value_slice_new (G_TYPE_STRING);
1585 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1586 g_hash_table_insert (args, TP_IFACE_CHANNEL ".ChannelType", value);
1588 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1589 value = tp_g_value_slice_new (G_TYPE_UINT);
1590 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1591 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandleType", value);
1593 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1594 value = tp_g_value_slice_new (G_TYPE_UINT);
1595 g_value_set_uint (value, handle);
1596 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandle", value);
1598 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1599 value = tp_g_value_slice_new (G_TYPE_STRING);
1600 g_value_set_string (value, g_file_info_get_content_type (info));
1601 g_hash_table_insert (args,
1602 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1604 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1605 value = tp_g_value_slice_new (G_TYPE_STRING);
1606 g_value_set_string (value, g_filename_display_basename (filename));
1607 g_hash_table_insert (args,
1608 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1610 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1611 value = tp_g_value_slice_new (G_TYPE_UINT64);
1612 g_value_set_uint64 (value, g_file_info_get_size (info));
1613 g_hash_table_insert (args,
1614 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1616 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1617 g_file_info_get_modification_time (info, &last_modif);
1618 value = tp_g_value_slice_new (G_TYPE_UINT64);
1619 g_value_set_uint64 (value, last_modif.tv_sec);
1620 g_hash_table_insert (args,
1621 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1623 /* FIXME: Description ? */
1624 /* FIXME: ContentHashType and ContentHash ? */
1626 tp_cli_connection_interface_requests_call_create_channel (connection, -1,
1627 args, file_channel_create_cb, request, NULL, NULL);
1629 g_hash_table_destroy (args);
1631 g_object_unref (mc);
1632 g_object_unref (connection);