1 /* * Copyright (C) 2007-2008 Collabora Ltd.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <glib/gi18n-lib.h>
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/proxy-subclass.h>
32 #include <libmissioncontrol/mission-control.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include <extensions/extensions.h>
37 #include "empathy-dispatcher.h"
38 #include "empathy-utils.h"
39 #include "empathy-tube-handler.h"
40 #include "empathy-account-manager.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-tp-group.h"
43 #include "empathy-tp-file.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 EmpathyAccountManager *account_manager;
54 /* connection to connection data mapping */
55 GHashTable *connections;
56 /* accounts to connection mapping */
60 EmpathyChatroomManager *chatroom_mgr;
61 } EmpathyDispatcherPriv;
63 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
72 static guint signals[LAST_SIGNAL];
73 static EmpathyDispatcher *dispatcher = NULL;
76 EmpathyDispatcherTube public;
77 EmpathyContactFactory *factory;
85 EmpathyDispatcher *dispatcher;
86 EmpathyDispatchOperation *operation;
87 TpConnection *connection;
91 EmpathyContact *contact;
92 /* Properties to pass to the channel when requesting it */
94 EmpathyDispatcherRequestCb *cb;
96 gpointer *request_data;
97 } DispatcherRequestData;
101 /* Channel type specific wrapper object */
102 GObject *channel_wrapper;
107 /* ObjectPath => DispatchData.. */
108 GHashTable *dispatched_channels;
109 /* ObjectPath -> EmpathyDispatchOperations */
110 GHashTable *dispatching_channels;
111 /* ObjectPath -> EmpathyDispatchOperations */
112 GHashTable *outstanding_channels;
113 /* List of DispatcherRequestData */
114 GList *outstanding_requests;
117 static DispatchData *
118 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
120 DispatchData *d = g_slice_new0 (DispatchData);
121 d->channel = channel;
122 d->channel_wrapper = channel_wrapper;
128 free_dispatch_data (DispatchData *data)
130 g_object_unref (data->channel);
131 g_object_unref (data->channel_wrapper);
133 g_slice_free (DispatchData, data);
137 static DispatcherRequestData *
138 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
139 TpConnection *connection, const gchar *channel_type, guint handle_type,
140 guint handle, GHashTable *request,
141 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
143 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
145 result->dispatcher = dispatcher;
146 result->connection = connection;
148 result->channel_type = g_strdup (channel_type);
149 result->handle_type = handle_type;
150 result->handle = handle;
151 result->request = request;
154 result->contact = g_object_ref (contact);
157 result->user_data = user_data;
163 free_dispatcher_request_data (DispatcherRequestData *r)
165 g_free (r->channel_type);
167 if (r->contact != NULL)
168 g_object_unref (r->contact);
170 if (r->request != NULL)
171 g_hash_table_unref (r->request);
173 g_slice_free (DispatcherRequestData, r);
176 static ConnectionData *
177 new_connection_data (McAccount *account)
179 ConnectionData *cd = g_slice_new0 (ConnectionData);
180 cd->account = g_object_ref (account);
182 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
183 g_free, (GDestroyNotify) free_dispatch_data);
185 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
186 g_free, g_object_unref);
188 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
195 free_connection_data (ConnectionData *cd)
198 g_object_unref (cd->account);
199 g_hash_table_destroy (cd->dispatched_channels);
200 g_hash_table_destroy (cd->dispatching_channels);
202 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
204 free_dispatcher_request_data (l->data);
210 empathy_dispatcher_tube_get_type (void)
212 static GType type_id = 0;
215 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
216 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
217 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
223 EmpathyDispatcherTube *
224 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
226 DispatcherTube *tube = (DispatcherTube*) data;
228 g_return_val_if_fail (tube != NULL, NULL);
236 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
238 DispatcherTube *tube = (DispatcherTube*) data;
240 g_return_if_fail (tube != NULL);
242 if (--tube->ref_count == 0) {
243 if (!tube->handled) {
244 DEBUG ("Tube can't be handled, closing");
245 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
251 g_free (tube->bus_name);
252 g_free (tube->object_path);
253 g_object_unref (tube->factory);
254 g_object_unref (tube->public.channel);
255 g_object_unref (tube->public.initiator);
256 g_slice_free (DispatcherTube, tube);
261 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
266 DispatcherTube *tube = user_data;
269 DEBUG ("Error: %s", error->message);
271 tube->handled = TRUE;
277 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
278 EmpathyDispatcherTube *etube)
280 DispatcherTube *tube = (DispatcherTube*) etube;
282 if (tube->public.activatable) {
289 /* Create the proxy for the tube handler */
290 thandler = g_object_new (TP_TYPE_PROXY,
291 "dbus-connection", tp_get_bus (),
292 "bus-name", tube->bus_name,
293 "object-path", tube->object_path,
295 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
297 /* Give the tube to the handler */
298 g_object_get (tube->public.channel,
299 "connection", &connection,
300 "object-path", &object_path,
301 "handle_type", &handle_type,
305 DEBUG ("Dispatching tube");
306 emp_cli_tube_handler_call_handle_tube (thandler, -1,
307 connection->bus_name,
308 connection->object_path,
309 object_path, handle_type,
310 handle, tube->public.id,
311 dispatcher_tubes_handle_tube_cb,
312 empathy_dispatcher_tube_ref (etube),
313 (GDestroyNotify) empathy_dispatcher_tube_unref,
314 G_OBJECT (dispatcher));
316 g_object_unref (thandler);
317 g_object_unref (connection);
318 g_free (object_path);
323 dispatcher_tubes_new_tube_cb (TpChannel *channel,
327 const gchar *service,
328 GHashTable *parameters,
333 static TpDBusDaemon *daemon = NULL;
334 DispatcherTube *tube;
338 gboolean running = FALSE;
339 GError *error = NULL;
341 /* Increase tube count */
342 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
343 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
344 DEBUG ("Increased tube count for channel %p: %d", channel, number);
346 /* We dispatch only local pending tubes */
347 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
352 daemon = tp_dbus_daemon_new (tp_get_bus ());
355 account = empathy_channel_get_account (channel);
356 tube = g_slice_new (DispatcherTube);
358 tube->handled = FALSE;
359 tube->factory = empathy_contact_factory_dup_singleton ();
360 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
361 tube->object_path = empathy_tube_handler_build_object_path (type, service);
362 tube->public.activatable = FALSE;
363 tube->public.id = id;
364 tube->public.channel = g_object_ref (channel);
365 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
368 g_object_unref (account);
370 DEBUG ("Looking for tube handler: %s", tube->bus_name);
371 /* Check if that bus-name has an owner, if it has one that means the
372 * app is already running and we can directly give the channel. */
373 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
374 &running, NULL, NULL);
376 DEBUG ("Tube handler running");
377 tube->public.activatable = TRUE;
378 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
379 (EmpathyDispatcherTube*) tube);
380 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
384 DEBUG ("Tube handler is not running. Try to activate it");
385 /* Check if that bus-name is activatable, if not that means the
386 * application needed to handle this tube isn't installed. */
387 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
390 DEBUG ("Error listing activatable names: %s", error->message);
391 g_clear_error (&error);
395 for (name = names; *name; name++) {
396 if (!tp_strdiff (*name, tube->bus_name)) {
397 DEBUG ("Found tube handler");
398 tube->public.activatable = TRUE;
405 if (!tube->public.activatable)
406 DEBUG ("Didn't find tube handler");
408 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
409 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
413 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
414 const GPtrArray *tubes,
422 DEBUG ("Error: %s", error->message);
426 for (i = 0; i < tubes->len; i++) {
429 values = g_ptr_array_index (tubes, i);
430 dispatcher_tubes_new_tube_cb (channel,
431 g_value_get_uint (g_value_array_get_nth (values, 0)),
432 g_value_get_uint (g_value_array_get_nth (values, 1)),
433 g_value_get_uint (g_value_array_get_nth (values, 2)),
434 g_value_get_string (g_value_array_get_nth (values, 3)),
435 g_value_get_boxed (g_value_array_get_nth (values, 4)),
436 g_value_get_uint (g_value_array_get_nth (values, 5)),
437 user_data, dispatcher);
442 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
446 EmpathyDispatcher *dispatcher)
448 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
450 DEBUG ("%s", message);
452 priv->tubes = g_slist_remove (priv->tubes, proxy);
453 g_object_unref (proxy);
457 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
464 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
466 DEBUG ("No more tube, closing channel");
467 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
469 else if (number > 1) {
470 DEBUG ("Decrease tube count: %d", number);
471 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
477 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
480 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
484 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
485 g_signal_connect (channel, "invalidated",
486 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
489 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
490 dispatcher_tubes_tube_closed_cb,
492 G_OBJECT (dispatcher), NULL);
493 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
494 dispatcher_tubes_new_tube_cb,
496 G_OBJECT (dispatcher), NULL);
497 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
498 dispatcher_tubes_list_tubes_cb,
500 G_OBJECT (dispatcher));
506 dispatcher_connection_invalidated_cb (TpConnection *connection,
507 guint domain, gint code, gchar *message,
508 EmpathyDispatcher *dispatcher)
510 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
513 DEBUG ("Error: %s", message);
514 cd = g_hash_table_lookup (priv->connections, connection);
516 g_hash_table_remove (priv->accounts, cd->account);
517 g_hash_table_remove (priv->connections, connection);
524 EmpathyDispatcher *self;
525 EmpathyChatroom *chatroom;
526 } dispatcher_connection_invalidated_cb_ctx;
528 static dispatcher_connection_invalidated_cb_ctx *
529 dispatcher_connection_invalidated_cb_ctx_new (EmpathyDispatcher *dispatcher,
530 EmpathyChatroom *chatroom)
532 dispatcher_connection_invalidated_cb_ctx *ctx;
534 ctx = g_slice_new (dispatcher_connection_invalidated_cb_ctx);
536 ctx->self = g_object_ref (dispatcher);
537 ctx->chatroom = g_object_ref (chatroom);
543 dispatcher_connection_invalidated_cb_ctx_free (
544 dispatcher_connection_invalidated_cb_ctx *ctx)
546 g_object_unref (ctx->self);
547 g_object_unref (ctx->chatroom);
549 g_slice_free (dispatcher_connection_invalidated_cb_ctx, ctx);
552 static void dispatcher_chatroom_invalidated_cb (
557 dispatcher_connection_invalidated_cb_ctx *ctx)
559 EmpathyDispatcherPriv *priv = GET_PRIV (ctx->self);
562 g_object_get (ctx->chatroom, "favorite", &favorite, NULL);
566 /* Chatroom is in favorites so don't remove it from the manager */
567 g_object_set (ctx->chatroom, "tp-channel", NULL, NULL);
571 empathy_chatroom_manager_remove (priv->chatroom_mgr, ctx->chatroom);
578 /********************* Sanity from here at some point *********/
580 dispatcher_operation_can_start (EmpathyDispatcher *self,
581 EmpathyDispatchOperation *operation, ConnectionData *cd)
584 const gchar *channel_type =
585 empathy_dispatch_operation_get_channel_type (operation);
587 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
589 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
591 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
601 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
602 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
606 l = cd->outstanding_requests;
609 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
614 if (d->operation == operation)
619 d->cb (NULL, error, d->user_data);
621 d->cb (operation, NULL, d->user_data);
624 cd->outstanding_requests = g_list_delete_link
625 (cd->outstanding_requests, lt);
627 free_dispatcher_request_data (d);
633 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
634 gchar *message, EmpathyDispatcher *dispatcher)
636 /* Channel went away... */
637 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
638 TpConnection *connection;
639 EmpathyDispatchOperation *operation;
641 const gchar *object_path;
643 g_object_get (G_OBJECT (proxy), "connection", &connection, NULL);
645 cd = g_hash_table_lookup (priv->connections, connection);
646 /* Connection itself invalidated? */
650 object_path = tp_proxy_get_object_path (proxy);
652 DEBUG ("Channel %s invalidated", object_path);
654 g_hash_table_remove (cd->dispatched_channels, object_path);
655 g_hash_table_remove (cd->dispatching_channels, object_path);
657 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
658 if (operation != NULL)
660 GError error = { domain, code, message };
661 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
662 g_hash_table_remove (cd->outstanding_channels, object_path);
663 g_object_unref (operation);
668 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
669 EmpathyDispatcher *dispatcher)
671 g_assert (empathy_dispatch_operation_is_incoming (operation));
672 DEBUG ("Send of for dispatching: %s",
673 empathy_dispatch_operation_get_object_path (operation));
674 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
678 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
679 EmpathyDispatcher *dispatcher)
681 /* Our job is done, remove the dispatch operation and mark the channel as
683 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
684 TpConnection *connection;
686 const gchar *object_path;
688 connection = empathy_dispatch_operation_get_tp_connection (operation);
689 cd = g_hash_table_lookup (priv->connections, connection);
690 g_assert (cd != NULL);
691 g_object_unref (G_OBJECT (connection));
693 object_path = empathy_dispatch_operation_get_object_path (operation);
695 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
698 d = new_dispatch_data (
699 empathy_dispatch_operation_get_channel (operation),
700 empathy_dispatch_operation_get_channel_wrapper (operation));
701 g_hash_table_insert (cd->dispatched_channels,
702 g_strdup (object_path), d);
704 g_hash_table_remove (cd->dispatching_channels, object_path);
706 DEBUG ("Channel claimed: %s", object_path);
710 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
711 EmpathyDispatcher *dispatcher)
713 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
714 TpConnection *connection;
716 EmpathyDispatchOperationState status;
718 g_signal_connect (operation, "approved",
719 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
721 g_signal_connect (operation, "claimed",
722 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
724 /* Signal the observers */
725 DEBUG ("Send to observers: %s",
726 empathy_dispatch_operation_get_object_path (operation));
727 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
729 empathy_dispatch_operation_start (operation);
731 /* Signal potential requestors */
732 connection = empathy_dispatch_operation_get_tp_connection (operation);
733 cd = g_hash_table_lookup (priv->connections, connection);
734 g_assert (cd != NULL);
735 g_object_unref (G_OBJECT (connection));
737 g_object_ref (operation);
739 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
740 status = empathy_dispatch_operation_get_status (operation);
741 g_object_unref (operation);
743 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
746 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
748 DEBUG ("Send to approvers: %s",
749 empathy_dispatch_operation_get_object_path (operation));
750 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
754 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
755 DEBUG ("Send of for dispatching: %s",
756 empathy_dispatch_operation_get_object_path (operation));
757 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
763 dispatcher_start_dispatching (EmpathyDispatcher *self,
764 EmpathyDispatchOperation *operation, ConnectionData *cd)
766 const gchar *object_path =
767 empathy_dispatch_operation_get_object_path (operation);
769 DEBUG ("Dispatching process started for %s", object_path);
771 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
773 g_assert (g_hash_table_lookup (cd->outstanding_channels,
774 object_path) == NULL);
776 g_hash_table_insert (cd->dispatching_channels,
777 g_strdup (object_path), operation);
779 switch (empathy_dispatch_operation_get_status (operation))
781 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
782 g_signal_connect (operation, "ready",
783 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
785 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
786 dispatch_operation_ready_cb (operation, dispatcher);
789 g_assert_not_reached();
793 else if (empathy_dispatch_operation_get_status (operation) >=
794 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
796 /* Already dispatching and the operation is pending, thus the observers
797 * have seen it (if applicable), so we can flush the request right away.
799 dispatch_operation_flush_requests (self, operation, NULL, cd);
805 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
811 g_hash_table_iter_init (&iter, cd->outstanding_channels);
812 while (g_hash_table_iter_next (&iter, NULL, &value))
814 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
816 if (dispatcher_operation_can_start (self, operation, cd))
818 dispatcher_start_dispatching (dispatcher, operation, cd);
819 g_hash_table_iter_remove (&iter);
826 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
827 TpConnection *connection,
828 const gchar *object_path, const gchar *channel_type,
829 guint handle_type, guint handle, GHashTable *properties,
832 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
835 EmpathyDispatchOperation *operation;
836 EmpathyContact *contact = NULL;
838 cd = g_hash_table_lookup (priv->connections, connection);
840 /* Don't bother with channels we have already dispatched or are dispatching
841 * currently. This can happen when NewChannel(s) is fired after
842 * RequestChannel/CreateChannel/EnsureChannel */
843 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
846 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
849 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
851 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
854 DEBUG ("New channel of type %s on %s",
855 channel_type, object_path);
857 if (properties == NULL)
858 channel = tp_channel_new (connection, object_path, channel_type,
859 handle_type, handle, NULL);
861 channel = tp_channel_new_from_properties (connection, object_path,
864 g_signal_connect (channel, "invalidated",
865 G_CALLBACK (dispatcher_channel_invalidated_cb),
868 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
870 EmpathyContactFactory *factory = empathy_contact_factory_new ();
871 contact = empathy_contact_factory_get_from_handle (factory,
872 cd->account, handle);
873 g_object_unref (factory);
876 operation = empathy_dispatch_operation_new (connection, channel, contact,
879 g_object_unref (channel);
883 /* Request could either be by us or by a remote party. If there are no
884 * outstanding requests for this channel type we can assume it's remote.
885 * Otherwise we wait untill they are all satisfied */
886 if (dispatcher_operation_can_start (dispatcher, operation, cd))
887 dispatcher_start_dispatching (dispatcher, operation, cd);
889 g_hash_table_insert (cd->outstanding_channels,
890 g_strdup (object_path), operation);
894 dispatcher_start_dispatching (dispatcher, operation, cd);
899 dispatcher_connection_new_channel_cb (TpConnection *connection,
900 const gchar *object_path, const gchar *channel_type,
901 guint handle_type, guint handle,
902 gboolean suppress_handler, gpointer user_data,
905 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
907 /* Empathy heavily abuses surpress handler (don't try this at home), if
908 * surpress handler is true then it is an outgoing channel, which is
909 * requested either by us or some other party (like the megaphone applet).
910 * Otherwise it's an incoming channel */
911 dispatcher_connection_new_channel (dispatcher, connection,
912 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
916 dispatcher_connection_new_channel_with_properties (
917 EmpathyDispatcher *dispatcher, TpConnection *connection,
918 const gchar *object_path, GHashTable *properties)
920 const gchar *channel_type;
927 channel_type = tp_asv_get_string (properties,
928 TP_IFACE_CHANNEL ".ChannelType");
929 if (channel_type == NULL)
931 g_message ("%s had an invalid ChannelType property", object_path);
935 handle_type = tp_asv_get_uint32 (properties,
936 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
939 g_message ("%s had an invalid TargetHandleType property", object_path);
943 handle = tp_asv_get_uint32 (properties,
944 TP_IFACE_CHANNEL ".TargetHandle", &valid);
947 g_message ("%s had an invalid TargetHandle property", object_path);
951 /* We assume there is no channel dispather, so we're the only one dispatching
952 * it. Which means that a requested channel it is outgoing one */
953 requested = tp_asv_get_boolean (properties,
954 TP_IFACE_CHANNEL ".Requested", &valid);
957 g_message ("%s had an invalid Requested property", object_path);
961 dispatcher_connection_new_channel (dispatcher, connection,
962 object_path, channel_type, handle_type, handle, properties, !requested);
967 dispatcher_connection_new_channels_cb (
968 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
971 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
974 for (i = 0; i < channels->len ; i++)
976 GValueArray *arr = g_ptr_array_index (channels, i);
977 const gchar *object_path;
978 GHashTable *properties;
980 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
981 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
983 dispatcher_connection_new_channel_with_properties (dispatcher,
984 connection, object_path, properties);
988 #if 0 /* old dispatching */
989 channel = tp_channel_new (connection, object_path, channel_type,
990 handle_type, handle, NULL);
991 tp_channel_run_until_ready (channel, NULL, NULL);
993 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
994 dispatcher_tubes_handle_channel (dispatcher, channel);
997 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
998 handle_type == TP_HANDLE_TYPE_ROOM)
1000 /* Add the chatroom to the chatroom manager */
1001 EmpathyChatroom *chatroom;
1006 dispatcher_connection_invalidated_cb_ctx *ctx;
1008 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1009 g_array_append_val (handles, handle);
1011 tp_cli_connection_run_inspect_handles (connection, -1,
1012 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
1014 mc = empathy_mission_control_new ();
1015 account = mission_control_get_account_for_tpconnection (mc, connection,
1018 chatroom = empathy_chatroom_manager_find (priv->chatroom_mgr, account,
1020 if (chatroom == NULL)
1022 chatroom = empathy_chatroom_new (account);
1023 empathy_chatroom_set_name (chatroom, room_ids[0]);
1024 empathy_chatroom_set_room (chatroom, room_ids[0]);
1025 empathy_chatroom_manager_add (priv->chatroom_mgr, chatroom);
1029 g_object_ref (chatroom);
1032 g_object_set (chatroom, "tp-channel", channel, NULL);
1034 ctx = dispatcher_connection_invalidated_cb_ctx_new (dispatcher, chatroom);
1036 g_signal_connect_data (channel, "invalidated",
1037 G_CALLBACK (dispatcher_chatroom_invalidated_cb), ctx,
1038 (GClosureNotify) dispatcher_connection_invalidated_cb_ctx_free, 0);
1040 g_free (room_ids[0]);
1042 g_array_free (handles, TRUE);
1043 g_object_unref (mc);
1044 g_object_unref (account);
1045 g_object_unref (chatroom);
1048 if (suppress_handler) {
1049 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
1051 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
1054 g_object_unref (channel);
1060 dispatcher_connection_got_channels_property (TpProxy *proxy,
1061 const GValue *channels_prop, const GError *error, gpointer user_data,
1064 GPtrArray *channels;
1067 DEBUG ("Error: %s", error->message);
1071 channels = g_value_get_boxed (channels_prop);
1072 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
1073 channels, NULL, object);
1077 dispatcher_connection_list_channels_cb (TpConnection *connection,
1078 const GPtrArray *channels,
1079 const GError *error,
1081 GObject *dispatcher)
1086 DEBUG ("Error: %s", error->message);
1090 for (i = 0; i < channels->len; i++) {
1091 GValueArray *values;
1093 values = g_ptr_array_index (channels, i);
1094 /* We don't have any extra info, so assume already existing channels are
1096 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
1098 g_value_get_boxed (g_value_array_get_nth (values, 0)),
1099 g_value_get_string (g_value_array_get_nth (values, 1)),
1100 g_value_get_uint (g_value_array_get_nth (values, 2)),
1101 g_value_get_uint (g_value_array_get_nth (values, 3)),
1107 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
1108 const GPtrArray *capabilities,
1109 const GError *error,
1111 GObject *dispatcher)
1114 DEBUG ("Error: %s", error->message);
1119 dispatcher_connection_ready_cb (TpConnection *connection,
1120 const GError *error, gpointer dispatcher)
1122 GPtrArray *capabilities;
1125 const gchar *remove = NULL;
1129 dispatcher_connection_invalidated_cb (connection, error->domain,
1130 error->code, error->message, dispatcher);
1134 g_signal_connect (connection, "invalidated",
1135 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
1138 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
1139 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1141 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
1142 dispatcher_connection_new_channels_cb,
1143 NULL, NULL, G_OBJECT (dispatcher), NULL);
1145 tp_cli_dbus_properties_call_get (connection, -1,
1146 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
1147 dispatcher_connection_got_channels_property,
1148 NULL, NULL, dispatcher);
1152 tp_cli_connection_connect_to_new_channel (connection,
1153 dispatcher_connection_new_channel_cb,
1154 NULL, NULL, G_OBJECT (dispatcher), NULL);
1156 tp_cli_connection_call_list_channels (connection, -1,
1157 dispatcher_connection_list_channels_cb, NULL, NULL,
1158 G_OBJECT (dispatcher));
1162 /* Advertise VoIP capabilities */
1163 capabilities = g_ptr_array_sized_new (1);
1164 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
1165 G_TYPE_UINT, G_TYPE_INVALID);
1166 g_value_init (&cap, cap_type);
1167 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
1168 dbus_g_type_struct_set (&cap,
1169 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1170 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
1171 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
1172 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
1173 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
1174 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
1176 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
1177 connection, -1, capabilities, &remove,
1178 dispatcher_connection_advertise_capabilities_cb,
1179 NULL, NULL, G_OBJECT (dispatcher));
1183 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
1185 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1186 TpConnection *connection;
1188 connection = g_hash_table_lookup (priv->accounts, account);
1189 if (connection != NULL)
1192 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
1193 if (connection == NULL)
1196 g_hash_table_insert (priv->connections, g_object_ref (connection),
1197 new_connection_data (account));
1199 g_hash_table_insert (priv->accounts, g_object_ref (account),
1200 g_object_ref (connection));
1202 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
1207 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
1208 McAccount *account, TpConnectionStatusReason reason,
1209 TpConnectionStatus status, TpConnectionStatus previous,
1210 EmpathyDispatcher *dispatcher)
1212 dispatcher_update_account (dispatcher, account);
1216 dispatcher_finalize (GObject *object)
1218 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1220 g_signal_handlers_disconnect_by_func (priv->account_manager,
1221 dispatcher_account_connection_cb, object);
1223 g_object_unref (priv->account_manager);
1224 g_object_unref (priv->mc);
1226 g_hash_table_destroy (priv->connections);
1228 g_object_unref (priv->chatroom_mgr);
1232 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1234 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1236 object_class->finalize = dispatcher_finalize;
1239 g_signal_new ("observe",
1240 G_TYPE_FROM_CLASS (klass),
1244 g_cclosure_marshal_VOID__OBJECT,
1246 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1249 g_signal_new ("approve",
1250 G_TYPE_FROM_CLASS (klass),
1254 g_cclosure_marshal_VOID__OBJECT,
1256 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1259 g_signal_new ("dispatch",
1260 G_TYPE_FROM_CLASS (klass),
1264 g_cclosure_marshal_VOID__OBJECT,
1266 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1268 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1273 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1275 GList *accounts, *l;
1276 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1277 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1279 dispatcher->priv = priv;
1280 priv->mc = empathy_mission_control_new ();
1281 priv->account_manager = empathy_account_manager_dup_singleton ();
1283 g_signal_connect (priv->account_manager,
1284 "account-connection-changed",
1285 G_CALLBACK (dispatcher_account_connection_cb),
1288 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1289 empathy_account_equal, g_object_unref, g_object_unref);
1291 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1292 g_object_unref, (GDestroyNotify) free_connection_data);
1294 accounts = mc_accounts_list_by_enabled (TRUE);
1296 for (l = accounts; l; l = l->next) {
1297 dispatcher_update_account (dispatcher, l->data);
1298 g_object_unref (l->data);
1300 g_list_free (accounts);
1302 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
1306 empathy_get_dispatcher (void)
1309 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
1310 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
1312 g_object_ref (dispatcher);
1319 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1320 DispatcherRequestData *request_data, const gchar *object_path,
1321 GHashTable *properties, const GError *error)
1323 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1324 EmpathyDispatchOperation *operation = NULL;
1325 ConnectionData *conn_data;
1327 conn_data = g_hash_table_lookup (priv->connections,
1328 request_data->connection);
1332 DEBUG ("Channel request failed: %s", error->message);
1334 if (request_data->cb != NULL)
1335 request_data->cb (NULL, error, request_data->user_data);
1337 conn_data->outstanding_requests =
1338 g_list_remove (conn_data->outstanding_requests, request_data);
1339 free_dispatcher_request_data (request_data);
1344 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1347 if (operation != NULL)
1348 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1350 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1353 if (operation == NULL)
1355 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1360 operation = empathy_dispatch_operation_new_with_wrapper (
1361 request_data->connection,
1362 data->channel, request_data->contact, FALSE,
1363 data->channel_wrapper);
1369 if (properties != NULL)
1370 channel = tp_channel_new_from_properties (request_data->connection,
1371 object_path, properties, NULL);
1373 channel = tp_channel_new (request_data->connection, object_path,
1374 request_data->channel_type, request_data->handle_type,
1375 request_data->handle, NULL);
1377 g_signal_connect (channel, "invalidated",
1378 G_CALLBACK (dispatcher_channel_invalidated_cb),
1379 request_data->dispatcher);
1381 operation = empathy_dispatch_operation_new (request_data->connection,
1382 channel, request_data->contact, FALSE);
1383 g_object_unref (channel);
1388 /* Already existed set potential extra information */
1389 g_object_set (G_OBJECT (operation),
1390 "contact", request_data->contact,
1394 request_data->operation = operation;
1396 /* (pre)-approve this right away as we requested it */
1397 empathy_dispatch_operation_approve (operation);
1399 dispatcher_start_dispatching (request_data->dispatcher, operation,
1402 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1407 dispatcher_request_channel_cb (TpConnection *connection,
1408 const gchar *object_path, const GError *error,
1409 gpointer user_data, GObject *weak_object)
1411 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1412 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1414 dispatcher_connection_new_requested_channel (dispatcher,
1415 request_data, object_path, NULL, error);
1419 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1420 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1422 g_assert_not_reached ();
1426 empathy_dispatcher_call_with_contact_id (McAccount *account,
1427 const gchar *contact_id, EmpathyDispatcherRequestCb *callback,
1430 g_assert_not_reached ();
1434 dispatcher_request_channel (DispatcherRequestData *request_data)
1436 tp_cli_connection_call_request_channel (request_data->connection, -1,
1437 request_data->channel_type,
1438 request_data->handle_type,
1439 request_data->handle,
1440 TRUE, dispatcher_request_channel_cb,
1441 request_data, NULL, G_OBJECT (request_data->dispatcher));
1445 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1447 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1449 request_data->handle = empathy_contact_get_handle (contact);
1451 dispatcher_request_channel (request_data);
1455 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1456 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1458 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1459 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1460 McAccount *account = empathy_contact_get_account (contact);
1461 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1462 ConnectionData *connection_data =
1463 g_hash_table_lookup (priv->connections, connection);
1464 DispatcherRequestData *request_data;
1466 /* The contact handle might not be known yet */
1467 request_data = new_dispatcher_request_data (dispatcher, connection,
1468 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1469 contact, callback, user_data);
1471 connection_data->outstanding_requests = g_list_prepend
1472 (connection_data->outstanding_requests, request_data);
1474 empathy_contact_call_when_ready (contact,
1475 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1478 g_object_unref (dispatcher);
1482 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1483 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1485 EmpathyDispatcher *dispatcher = empathy_get_dispatcher ();
1486 EmpathyContactFactory *factory;
1487 EmpathyContact *contact;
1489 factory = empathy_contact_factory_dup_singleton ();
1490 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1492 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1494 g_object_unref (contact);
1495 g_object_unref (factory);
1496 g_object_unref (dispatcher);
1501 dispatcher_request_handles_cb (TpConnection *connection,
1502 const GArray *handles, const GError *error, gpointer user_data,
1505 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1509 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1510 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1513 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1515 if (request_data->cb)
1516 request_data->cb (NULL, error, request_data->user_data);
1518 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1521 free_dispatcher_request_data (request_data);
1523 dispatcher_flush_outstanding_operations (dispatcher, cd);
1527 request_data->handle = g_array_index (handles, guint, 0);
1528 dispatcher_request_channel (request_data);
1532 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1533 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1535 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1536 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1537 DispatcherRequestData *request_data;
1538 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1539 ConnectionData *connection_data =
1540 g_hash_table_lookup (priv->connections, connection);
1541 const gchar *names[] = { roomname, NULL };
1543 /* Don't know the room handle yet */
1544 request_data = new_dispatcher_request_data (dispatcher, connection,
1545 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1546 NULL, callback, user_data);
1548 connection_data->outstanding_requests = g_list_prepend
1549 (connection_data->outstanding_requests, request_data);
1551 tp_cli_connection_call_request_handles (connection, -1,
1552 TP_HANDLE_TYPE_ROOM, names,
1553 dispatcher_request_handles_cb, request_data, NULL,
1554 G_OBJECT (dispatcher));
1556 g_object_unref (dispatcher);
1563 } FileChannelRequest;
1566 tp_file_state_notify_cb (EmpathyTpFile *tp_file)
1568 EmpFileTransferState state;
1570 state = empathy_tp_file_get_state (tp_file, NULL);
1571 if (state == EMP_FILE_TRANSFER_STATE_COMPLETED ||
1572 state == EMP_FILE_TRANSFER_STATE_CANCELLED) {
1573 DEBUG ("Transfer is done, unref the object");
1574 g_object_unref (tp_file);
1579 file_channel_create_cb (TpConnection *connection,
1580 const gchar *object_path,
1581 GHashTable *properties,
1582 const GError *error,
1584 GObject *weak_object)
1587 EmpathyTpFile *tp_file;
1588 FileChannelRequest *request = (FileChannelRequest *) user_data;
1591 DEBUG ("Couldn't request channel: %s", error->message);
1595 channel = tp_channel_new (connection,
1597 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1598 TP_HANDLE_TYPE_CONTACT,
1602 /* We give the ref to the callback, it is responsible to unref the
1603 * object once the transfer is done. */
1604 tp_file = empathy_tp_file_new (channel);
1605 empathy_tp_file_offer (tp_file, request->gfile, NULL);
1606 g_signal_connect (tp_file, "notify::state",
1607 G_CALLBACK (tp_file_state_notify_cb),
1610 g_object_unref (request->gfile);
1611 g_slice_free (FileChannelRequest, request);
1612 g_object_unref (channel);
1619 dispatcher_create_channel_cb (TpConnection *connect,
1620 const gchar *object_path, GHashTable *properties, const GError *error,
1621 gpointer user_data, GObject *weak_object)
1623 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1624 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1626 dispatcher_connection_new_requested_channel (dispatcher,
1627 request_data, object_path, properties, error);
1632 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1635 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1636 GValue *target_handle;
1638 g_assert (request_data->request);
1640 request_data->handle = empathy_contact_get_handle (contact);
1642 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1643 g_value_set_uint (target_handle, request_data->handle);
1644 g_hash_table_insert (request_data->request,
1645 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1647 tp_cli_connection_interface_requests_call_create_channel (
1648 request_data->connection, -1,
1649 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1650 G_OBJECT (request_data->dispatcher));
1654 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1655 const gchar *filename, guint64 size, guint64 date,
1656 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1659 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1660 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1661 McAccount *account = empathy_contact_get_account (contact);
1662 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1663 ConnectionData *connection_data =
1664 g_hash_table_lookup (priv->connections, connection);
1665 DispatcherRequestData *request_data;
1667 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1668 (GDestroyNotify) tp_g_value_slice_free);
1670 /* org.freedesktop.Telepathy.Channel.ChannelType */
1671 value = tp_g_value_slice_new (G_TYPE_STRING);
1672 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1673 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1675 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1676 value = tp_g_value_slice_new (G_TYPE_UINT);
1677 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1678 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1680 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1681 value = tp_g_value_slice_new (G_TYPE_STRING);
1682 g_value_set_string (value, content_type);
1683 g_hash_table_insert (request,
1684 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1686 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1687 value = tp_g_value_slice_new (G_TYPE_STRING);
1688 g_value_set_string (value, filename);
1689 g_hash_table_insert (request,
1690 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1692 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1693 value = tp_g_value_slice_new (G_TYPE_UINT64);
1694 g_value_set_uint64 (value, size);
1695 g_hash_table_insert (request,
1696 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1698 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1699 value = tp_g_value_slice_new (G_TYPE_UINT64);
1700 g_value_set_uint64 (value, date);
1701 g_hash_table_insert (request,
1702 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1705 /* The contact handle might not be known yet */
1706 request_data = new_dispatcher_request_data (dispatcher, connection,
1707 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1708 contact, callback, user_data);
1709 connection_data->outstanding_requests = g_list_prepend
1710 (connection_data->outstanding_requests, request_data);
1712 empathy_contact_call_when_ready (contact,
1713 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1716 g_object_unref (dispatcher);
1722 TpConnection *connection;
1724 FileChannelRequest *request;
1729 GTimeVal last_modif;
1730 GError *error = NULL;
1732 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1733 g_return_if_fail (G_IS_FILE (gfile));
1735 info = g_file_query_info (gfile,
1736 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1737 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1738 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1742 DEBUG ("Can't get info about the file: %s", error->message);
1743 g_clear_error (&error);
1747 mc = empathy_mission_control_new ();
1748 account = empathy_contact_get_account (contact);
1749 connection = mission_control_get_tpconnection (mc, account, NULL);
1750 handle = empathy_contact_get_handle (contact);
1752 request = g_slice_new0 (FileChannelRequest);
1753 request->gfile = g_object_ref (gfile);
1754 request->handle = handle;
1756 filename = g_file_get_basename (request->gfile);
1757 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
1759 DEBUG ("Sending %s from a stream to %s (size %"G_GINT64_FORMAT", content-type %s)",
1760 filename, empathy_contact_get_name (contact),
1761 g_file_info_get_size (info),
1762 g_file_info_get_content_type (info));
1764 args = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1765 (GDestroyNotify) tp_g_value_slice_free);
1767 /* org.freedesktop.Telepathy.Channel.ChannelType */
1768 value = tp_g_value_slice_new (G_TYPE_STRING);
1769 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1770 g_hash_table_insert (args, TP_IFACE_CHANNEL ".ChannelType", value);
1772 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1773 value = tp_g_value_slice_new (G_TYPE_UINT);
1774 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1775 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandleType", value);
1777 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1778 value = tp_g_value_slice_new (G_TYPE_UINT);
1779 g_value_set_uint (value, handle);
1780 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandle", value);
1782 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1783 value = tp_g_value_slice_new (G_TYPE_STRING);
1784 g_value_set_string (value, g_file_info_get_content_type (info));
1785 g_hash_table_insert (args,
1786 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1788 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1789 value = tp_g_value_slice_new (G_TYPE_STRING);
1790 g_value_set_string (value, g_filename_display_basename (filename));
1791 g_hash_table_insert (args,
1792 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1794 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1795 value = tp_g_value_slice_new (G_TYPE_UINT64);
1796 g_value_set_uint64 (value, g_file_info_get_size (info));
1797 g_hash_table_insert (args,
1798 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1800 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1801 g_file_info_get_modification_time (info, &last_modif);
1802 value = tp_g_value_slice_new (G_TYPE_UINT64);
1803 g_value_set_uint64 (value, last_modif.tv_sec);
1804 g_hash_table_insert (args,
1805 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1807 /* FIXME: Description ? */
1808 /* FIXME: ContentHashType and ContentHash ? */
1810 tp_cli_connection_interface_requests_call_create_channel (connection, -1,
1811 args, file_channel_create_cb, request, NULL, NULL);
1813 g_hash_table_destroy (args);
1815 g_object_unref (mc);
1816 g_object_unref (connection);