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;
93 /* Properties to pass to the channel when requesting it */
95 EmpathyDispatcherRequestCb *cb;
97 gpointer *request_data;
98 } DispatcherRequestData;
102 /* Channel type specific wrapper object */
103 GObject *channel_wrapper;
108 /* ObjectPath => DispatchData.. */
109 GHashTable *dispatched_channels;
110 /* ObjectPath -> EmpathyDispatchOperations */
111 GHashTable *dispatching_channels;
112 /* ObjectPath -> EmpathyDispatchOperations */
113 GHashTable *outstanding_channels;
114 /* List of DispatcherRequestData */
115 GList *outstanding_requests;
118 static DispatchData *
119 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
121 DispatchData *d = g_slice_new0 (DispatchData);
122 d->channel = channel;
123 d->channel_wrapper = channel_wrapper;
129 free_dispatch_data (DispatchData *data)
131 g_object_unref (data->channel);
132 g_object_unref (data->channel_wrapper);
134 g_slice_free (DispatchData, data);
138 static DispatcherRequestData *
139 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
140 TpConnection *connection, const gchar *channel_type, guint handle_type,
141 guint handle, GHashTable *request,
142 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
144 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
146 result->dispatcher = dispatcher;
147 result->connection = connection;
149 result->channel_type = g_strdup (channel_type);
150 result->handle_type = handle_type;
151 result->handle = handle;
152 result->request = request;
155 result->contact = g_object_ref (contact);
158 result->user_data = user_data;
164 free_dispatcher_request_data (DispatcherRequestData *r)
166 g_free (r->channel_type);
168 if (r->contact != NULL)
169 g_object_unref (r->contact);
171 if (r->request != NULL)
172 g_hash_table_unref (r->request);
174 g_slice_free (DispatcherRequestData, r);
177 static ConnectionData *
178 new_connection_data (McAccount *account)
180 ConnectionData *cd = g_slice_new0 (ConnectionData);
181 cd->account = g_object_ref (account);
183 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
184 g_free, (GDestroyNotify) free_dispatch_data);
186 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
187 g_free, g_object_unref);
189 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
196 free_connection_data (ConnectionData *cd)
199 g_object_unref (cd->account);
200 g_hash_table_destroy (cd->dispatched_channels);
201 g_hash_table_destroy (cd->dispatching_channels);
203 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
205 free_dispatcher_request_data (l->data);
211 empathy_dispatcher_tube_get_type (void)
213 static GType type_id = 0;
216 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
217 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
218 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
224 EmpathyDispatcherTube *
225 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
227 DispatcherTube *tube = (DispatcherTube*) data;
229 g_return_val_if_fail (tube != NULL, NULL);
237 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
239 DispatcherTube *tube = (DispatcherTube*) data;
241 g_return_if_fail (tube != NULL);
243 if (--tube->ref_count == 0) {
244 if (!tube->handled) {
245 DEBUG ("Tube can't be handled, closing");
246 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
252 g_free (tube->bus_name);
253 g_free (tube->object_path);
254 g_object_unref (tube->factory);
255 g_object_unref (tube->public.channel);
256 g_object_unref (tube->public.initiator);
257 g_slice_free (DispatcherTube, tube);
262 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
267 DispatcherTube *tube = user_data;
270 DEBUG ("Error: %s", error->message);
272 tube->handled = TRUE;
278 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
279 EmpathyDispatcherTube *etube)
281 DispatcherTube *tube = (DispatcherTube*) etube;
283 if (tube->public.activatable) {
290 /* Create the proxy for the tube handler */
291 thandler = g_object_new (TP_TYPE_PROXY,
292 "dbus-connection", tp_get_bus (),
293 "bus-name", tube->bus_name,
294 "object-path", tube->object_path,
296 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
298 /* Give the tube to the handler */
299 g_object_get (tube->public.channel,
300 "connection", &connection,
301 "object-path", &object_path,
302 "handle_type", &handle_type,
306 DEBUG ("Dispatching tube");
307 emp_cli_tube_handler_call_handle_tube (thandler, -1,
308 connection->bus_name,
309 connection->object_path,
310 object_path, handle_type,
311 handle, tube->public.id,
312 dispatcher_tubes_handle_tube_cb,
313 empathy_dispatcher_tube_ref (etube),
314 (GDestroyNotify) empathy_dispatcher_tube_unref,
315 G_OBJECT (dispatcher));
317 g_object_unref (thandler);
318 g_object_unref (connection);
319 g_free (object_path);
324 dispatcher_tubes_new_tube_cb (TpChannel *channel,
328 const gchar *service,
329 GHashTable *parameters,
334 static TpDBusDaemon *daemon = NULL;
335 DispatcherTube *tube;
339 gboolean running = FALSE;
340 GError *error = NULL;
342 /* Increase tube count */
343 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
344 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
345 DEBUG ("Increased tube count for channel %p: %d", channel, number);
347 /* We dispatch only local pending tubes */
348 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
353 daemon = tp_dbus_daemon_new (tp_get_bus ());
356 account = empathy_channel_get_account (channel);
357 tube = g_slice_new (DispatcherTube);
359 tube->handled = FALSE;
360 tube->factory = empathy_contact_factory_dup_singleton ();
361 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
362 tube->object_path = empathy_tube_handler_build_object_path (type, service);
363 tube->public.activatable = FALSE;
364 tube->public.id = id;
365 tube->public.channel = g_object_ref (channel);
366 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
369 g_object_unref (account);
371 DEBUG ("Looking for tube handler: %s", tube->bus_name);
372 /* Check if that bus-name has an owner, if it has one that means the
373 * app is already running and we can directly give the channel. */
374 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
375 &running, NULL, NULL);
377 DEBUG ("Tube handler running");
378 tube->public.activatable = TRUE;
379 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
380 (EmpathyDispatcherTube*) tube);
381 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
385 DEBUG ("Tube handler is not running. Try to activate it");
386 /* Check if that bus-name is activatable, if not that means the
387 * application needed to handle this tube isn't installed. */
388 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
391 DEBUG ("Error listing activatable names: %s", error->message);
392 g_clear_error (&error);
396 for (name = names; *name; name++) {
397 if (!tp_strdiff (*name, tube->bus_name)) {
398 DEBUG ("Found tube handler");
399 tube->public.activatable = TRUE;
406 if (!tube->public.activatable)
407 DEBUG ("Didn't find tube handler");
409 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
410 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
414 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
415 const GPtrArray *tubes,
423 DEBUG ("Error: %s", error->message);
427 for (i = 0; i < tubes->len; i++) {
430 values = g_ptr_array_index (tubes, i);
431 dispatcher_tubes_new_tube_cb (channel,
432 g_value_get_uint (g_value_array_get_nth (values, 0)),
433 g_value_get_uint (g_value_array_get_nth (values, 1)),
434 g_value_get_uint (g_value_array_get_nth (values, 2)),
435 g_value_get_string (g_value_array_get_nth (values, 3)),
436 g_value_get_boxed (g_value_array_get_nth (values, 4)),
437 g_value_get_uint (g_value_array_get_nth (values, 5)),
438 user_data, dispatcher);
443 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
447 EmpathyDispatcher *dispatcher)
449 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
451 DEBUG ("%s", message);
453 priv->tubes = g_slist_remove (priv->tubes, proxy);
454 g_object_unref (proxy);
458 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
465 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
467 DEBUG ("No more tube, closing channel");
468 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
470 else if (number > 1) {
471 DEBUG ("Decrease tube count: %d", number);
472 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
478 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
481 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
485 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
486 g_signal_connect (channel, "invalidated",
487 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
490 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
491 dispatcher_tubes_tube_closed_cb,
493 G_OBJECT (dispatcher), NULL);
494 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
495 dispatcher_tubes_new_tube_cb,
497 G_OBJECT (dispatcher), NULL);
498 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
499 dispatcher_tubes_list_tubes_cb,
501 G_OBJECT (dispatcher));
507 dispatcher_connection_invalidated_cb (TpConnection *connection,
508 guint domain, gint code, gchar *message,
509 EmpathyDispatcher *dispatcher)
511 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
514 DEBUG ("Error: %s", message);
515 cd = g_hash_table_lookup (priv->connections, connection);
517 g_hash_table_remove (priv->accounts, cd->account);
518 g_hash_table_remove (priv->connections, connection);
525 EmpathyDispatcher *self;
526 EmpathyChatroom *chatroom;
527 } dispatcher_connection_invalidated_cb_ctx;
529 static dispatcher_connection_invalidated_cb_ctx *
530 dispatcher_connection_invalidated_cb_ctx_new (EmpathyDispatcher *dispatcher,
531 EmpathyChatroom *chatroom)
533 dispatcher_connection_invalidated_cb_ctx *ctx;
535 ctx = g_slice_new (dispatcher_connection_invalidated_cb_ctx);
537 ctx->self = g_object_ref (dispatcher);
538 ctx->chatroom = g_object_ref (chatroom);
544 dispatcher_connection_invalidated_cb_ctx_free (
545 dispatcher_connection_invalidated_cb_ctx *ctx)
547 g_object_unref (ctx->self);
548 g_object_unref (ctx->chatroom);
550 g_slice_free (dispatcher_connection_invalidated_cb_ctx, ctx);
553 static void dispatcher_chatroom_invalidated_cb (
558 dispatcher_connection_invalidated_cb_ctx *ctx)
560 EmpathyDispatcherPriv *priv = GET_PRIV (ctx->self);
563 g_object_get (ctx->chatroom, "favorite", &favorite, NULL);
567 /* Chatroom is in favorites so don't remove it from the manager */
568 g_object_set (ctx->chatroom, "tp-channel", NULL, NULL);
572 empathy_chatroom_manager_remove (priv->chatroom_mgr, ctx->chatroom);
579 /********************* Sanity from here at some point *********/
581 dispatcher_operation_can_start (EmpathyDispatcher *self,
582 EmpathyDispatchOperation *operation, ConnectionData *cd)
585 const gchar *channel_type =
586 empathy_dispatch_operation_get_channel_type (operation);
588 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
590 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
592 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
602 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
603 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
607 l = cd->outstanding_requests;
610 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
615 if (d->operation == operation)
620 d->cb (NULL, error, d->user_data);
622 d->cb (operation, NULL, d->user_data);
625 cd->outstanding_requests = g_list_delete_link
626 (cd->outstanding_requests, lt);
628 free_dispatcher_request_data (d);
634 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
635 gchar *message, EmpathyDispatcher *dispatcher)
637 /* Channel went away... */
638 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
639 TpConnection *connection;
640 EmpathyDispatchOperation *operation;
642 const gchar *object_path;
644 g_object_get (G_OBJECT (proxy), "connection", &connection, NULL);
646 cd = g_hash_table_lookup (priv->connections, connection);
647 /* Connection itself invalidated? */
651 object_path = tp_proxy_get_object_path (proxy);
653 DEBUG ("Channel %s invalidated", object_path);
655 g_hash_table_remove (cd->dispatched_channels, object_path);
656 g_hash_table_remove (cd->dispatching_channels, object_path);
658 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
659 if (operation != NULL)
661 GError error = { domain, code, message };
662 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
663 g_hash_table_remove (cd->outstanding_channels, object_path);
664 g_object_unref (operation);
669 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
670 EmpathyDispatcher *dispatcher)
672 g_assert (empathy_dispatch_operation_is_incoming (operation));
673 DEBUG ("Send of for dispatching: %s",
674 empathy_dispatch_operation_get_object_path (operation));
675 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
679 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
680 EmpathyDispatcher *dispatcher)
682 /* Our job is done, remove the dispatch operation and mark the channel as
684 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
685 TpConnection *connection;
687 const gchar *object_path;
689 connection = empathy_dispatch_operation_get_tp_connection (operation);
690 cd = g_hash_table_lookup (priv->connections, connection);
691 g_assert (cd != NULL);
692 g_object_unref (G_OBJECT (connection));
694 object_path = empathy_dispatch_operation_get_object_path (operation);
696 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
699 d = new_dispatch_data (
700 empathy_dispatch_operation_get_channel (operation),
701 empathy_dispatch_operation_get_channel_wrapper (operation));
702 g_hash_table_insert (cd->dispatched_channels,
703 g_strdup (object_path), d);
705 g_hash_table_remove (cd->dispatching_channels, object_path);
707 DEBUG ("Channel claimed: %s", object_path);
711 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
712 EmpathyDispatcher *dispatcher)
714 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
715 TpConnection *connection;
717 EmpathyDispatchOperationState status;
719 g_signal_connect (operation, "approved",
720 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
722 g_signal_connect (operation, "claimed",
723 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
725 /* Signal the observers */
726 DEBUG ("Send to observers: %s",
727 empathy_dispatch_operation_get_object_path (operation));
728 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
730 empathy_dispatch_operation_start (operation);
732 /* Signal potential requestors */
733 connection = empathy_dispatch_operation_get_tp_connection (operation);
734 cd = g_hash_table_lookup (priv->connections, connection);
735 g_assert (cd != NULL);
736 g_object_unref (G_OBJECT (connection));
738 g_object_ref (operation);
740 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
741 status = empathy_dispatch_operation_get_status (operation);
742 g_object_unref (operation);
744 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
747 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
749 DEBUG ("Send to approvers: %s",
750 empathy_dispatch_operation_get_object_path (operation));
751 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
755 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
756 DEBUG ("Send of for dispatching: %s",
757 empathy_dispatch_operation_get_object_path (operation));
758 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
764 dispatcher_start_dispatching (EmpathyDispatcher *self,
765 EmpathyDispatchOperation *operation, ConnectionData *cd)
767 const gchar *object_path =
768 empathy_dispatch_operation_get_object_path (operation);
770 DEBUG ("Dispatching process started for %s", object_path);
772 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
774 g_assert (g_hash_table_lookup (cd->outstanding_channels,
775 object_path) == NULL);
777 g_hash_table_insert (cd->dispatching_channels,
778 g_strdup (object_path), operation);
780 switch (empathy_dispatch_operation_get_status (operation))
782 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
783 g_signal_connect (operation, "ready",
784 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
786 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
787 dispatch_operation_ready_cb (operation, dispatcher);
790 g_assert_not_reached();
794 else if (empathy_dispatch_operation_get_status (operation) >=
795 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
797 /* Already dispatching and the operation is pending, thus the observers
798 * have seen it (if applicable), so we can flush the request right away.
800 dispatch_operation_flush_requests (self, operation, NULL, cd);
806 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
812 g_hash_table_iter_init (&iter, cd->outstanding_channels);
813 while (g_hash_table_iter_next (&iter, NULL, &value))
815 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
817 if (dispatcher_operation_can_start (self, operation, cd))
819 dispatcher_start_dispatching (dispatcher, operation, cd);
820 g_hash_table_iter_remove (&iter);
827 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
828 TpConnection *connection,
829 const gchar *object_path, const gchar *channel_type,
830 guint handle_type, guint handle, GHashTable *properties,
833 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
836 EmpathyDispatchOperation *operation;
837 EmpathyContact *contact = NULL;
839 cd = g_hash_table_lookup (priv->connections, connection);
841 /* Don't bother with channels we have already dispatched or are dispatching
842 * currently. This can happen when NewChannel(s) is fired after
843 * RequestChannel/CreateChannel/EnsureChannel */
844 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
847 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
850 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
852 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
855 DEBUG ("New channel of type %s on %s",
856 channel_type, object_path);
858 if (properties == NULL)
859 channel = tp_channel_new (connection, object_path, channel_type,
860 handle_type, handle, NULL);
862 channel = tp_channel_new_from_properties (connection, object_path,
865 g_signal_connect (channel, "invalidated",
866 G_CALLBACK (dispatcher_channel_invalidated_cb),
869 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
871 EmpathyContactFactory *factory = empathy_contact_factory_new ();
872 contact = empathy_contact_factory_get_from_handle (factory,
873 cd->account, handle);
874 g_object_unref (factory);
877 operation = empathy_dispatch_operation_new (connection, channel, contact,
880 g_object_unref (channel);
884 /* Request could either be by us or by a remote party. If there are no
885 * outstanding requests for this channel type we can assume it's remote.
886 * Otherwise we wait untill they are all satisfied */
887 if (dispatcher_operation_can_start (dispatcher, operation, cd))
888 dispatcher_start_dispatching (dispatcher, operation, cd);
890 g_hash_table_insert (cd->outstanding_channels,
891 g_strdup (object_path), operation);
895 dispatcher_start_dispatching (dispatcher, operation, cd);
900 dispatcher_connection_new_channel_cb (TpConnection *connection,
901 const gchar *object_path, const gchar *channel_type,
902 guint handle_type, guint handle,
903 gboolean suppress_handler, gpointer user_data,
906 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
908 /* Empathy heavily abuses surpress handler (don't try this at home), if
909 * surpress handler is true then it is an outgoing channel, which is
910 * requested either by us or some other party (like the megaphone applet).
911 * Otherwise it's an incoming channel */
912 dispatcher_connection_new_channel (dispatcher, connection,
913 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
917 dispatcher_connection_new_channel_with_properties (
918 EmpathyDispatcher *dispatcher, TpConnection *connection,
919 const gchar *object_path, GHashTable *properties)
921 const gchar *channel_type;
928 channel_type = tp_asv_get_string (properties,
929 TP_IFACE_CHANNEL ".ChannelType");
930 if (channel_type == NULL)
932 g_message ("%s had an invalid ChannelType property", object_path);
936 handle_type = tp_asv_get_uint32 (properties,
937 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
940 g_message ("%s had an invalid TargetHandleType property", object_path);
944 handle = tp_asv_get_uint32 (properties,
945 TP_IFACE_CHANNEL ".TargetHandle", &valid);
948 g_message ("%s had an invalid TargetHandle property", object_path);
952 /* We assume there is no channel dispather, so we're the only one dispatching
953 * it. Which means that a requested channel it is outgoing one */
954 requested = tp_asv_get_boolean (properties,
955 TP_IFACE_CHANNEL ".Requested", &valid);
958 g_message ("%s had an invalid Requested property", object_path);
962 dispatcher_connection_new_channel (dispatcher, connection,
963 object_path, channel_type, handle_type, handle, properties, !requested);
968 dispatcher_connection_new_channels_cb (
969 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
972 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
975 for (i = 0; i < channels->len ; i++)
977 GValueArray *arr = g_ptr_array_index (channels, i);
978 const gchar *object_path;
979 GHashTable *properties;
981 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
982 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
984 dispatcher_connection_new_channel_with_properties (dispatcher,
985 connection, object_path, properties);
989 #if 0 /* old dispatching */
990 channel = tp_channel_new (connection, object_path, channel_type,
991 handle_type, handle, NULL);
992 tp_channel_run_until_ready (channel, NULL, NULL);
994 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
995 dispatcher_tubes_handle_channel (dispatcher, channel);
998 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
999 handle_type == TP_HANDLE_TYPE_ROOM)
1001 /* Add the chatroom to the chatroom manager */
1002 EmpathyChatroom *chatroom;
1007 dispatcher_connection_invalidated_cb_ctx *ctx;
1009 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1010 g_array_append_val (handles, handle);
1012 tp_cli_connection_run_inspect_handles (connection, -1,
1013 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
1015 mc = empathy_mission_control_new ();
1016 account = mission_control_get_account_for_tpconnection (mc, connection,
1019 chatroom = empathy_chatroom_manager_find (priv->chatroom_mgr, account,
1021 if (chatroom == NULL)
1023 chatroom = empathy_chatroom_new (account);
1024 empathy_chatroom_set_name (chatroom, room_ids[0]);
1025 empathy_chatroom_set_room (chatroom, room_ids[0]);
1026 empathy_chatroom_manager_add (priv->chatroom_mgr, chatroom);
1030 g_object_ref (chatroom);
1033 g_object_set (chatroom, "tp-channel", channel, NULL);
1035 ctx = dispatcher_connection_invalidated_cb_ctx_new (dispatcher, chatroom);
1037 g_signal_connect_data (channel, "invalidated",
1038 G_CALLBACK (dispatcher_chatroom_invalidated_cb), ctx,
1039 (GClosureNotify) dispatcher_connection_invalidated_cb_ctx_free, 0);
1041 g_free (room_ids[0]);
1043 g_array_free (handles, TRUE);
1044 g_object_unref (mc);
1045 g_object_unref (account);
1046 g_object_unref (chatroom);
1049 if (suppress_handler) {
1050 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
1052 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
1055 g_object_unref (channel);
1061 dispatcher_connection_got_channels_property (TpProxy *proxy,
1062 const GValue *channels_prop, const GError *error, gpointer user_data,
1065 GPtrArray *channels;
1068 DEBUG ("Error: %s", error->message);
1072 channels = g_value_get_boxed (channels_prop);
1073 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
1074 channels, NULL, object);
1078 dispatcher_connection_list_channels_cb (TpConnection *connection,
1079 const GPtrArray *channels,
1080 const GError *error,
1082 GObject *dispatcher)
1087 DEBUG ("Error: %s", error->message);
1091 for (i = 0; i < channels->len; i++) {
1092 GValueArray *values;
1094 values = g_ptr_array_index (channels, i);
1095 /* We don't have any extra info, so assume already existing channels are
1097 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
1099 g_value_get_boxed (g_value_array_get_nth (values, 0)),
1100 g_value_get_string (g_value_array_get_nth (values, 1)),
1101 g_value_get_uint (g_value_array_get_nth (values, 2)),
1102 g_value_get_uint (g_value_array_get_nth (values, 3)),
1108 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
1109 const GPtrArray *capabilities,
1110 const GError *error,
1112 GObject *dispatcher)
1115 DEBUG ("Error: %s", error->message);
1120 dispatcher_connection_ready_cb (TpConnection *connection,
1121 const GError *error, gpointer dispatcher)
1123 GPtrArray *capabilities;
1126 const gchar *remove = NULL;
1130 dispatcher_connection_invalidated_cb (connection, error->domain,
1131 error->code, error->message, dispatcher);
1135 g_signal_connect (connection, "invalidated",
1136 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
1139 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
1140 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1142 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
1143 dispatcher_connection_new_channels_cb,
1144 NULL, NULL, G_OBJECT (dispatcher), NULL);
1146 tp_cli_dbus_properties_call_get (connection, -1,
1147 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
1148 dispatcher_connection_got_channels_property,
1149 NULL, NULL, dispatcher);
1153 tp_cli_connection_connect_to_new_channel (connection,
1154 dispatcher_connection_new_channel_cb,
1155 NULL, NULL, G_OBJECT (dispatcher), NULL);
1157 tp_cli_connection_call_list_channels (connection, -1,
1158 dispatcher_connection_list_channels_cb, NULL, NULL,
1159 G_OBJECT (dispatcher));
1163 /* Advertise VoIP capabilities */
1164 capabilities = g_ptr_array_sized_new (1);
1165 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
1166 G_TYPE_UINT, G_TYPE_INVALID);
1167 g_value_init (&cap, cap_type);
1168 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
1169 dbus_g_type_struct_set (&cap,
1170 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
1171 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
1172 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
1173 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
1174 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
1175 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
1177 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
1178 connection, -1, capabilities, &remove,
1179 dispatcher_connection_advertise_capabilities_cb,
1180 NULL, NULL, G_OBJECT (dispatcher));
1184 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
1186 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1187 TpConnection *connection;
1189 connection = g_hash_table_lookup (priv->accounts, account);
1190 if (connection != NULL)
1193 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
1194 if (connection == NULL)
1197 g_hash_table_insert (priv->connections, g_object_ref (connection),
1198 new_connection_data (account));
1200 g_hash_table_insert (priv->accounts, g_object_ref (account),
1201 g_object_ref (connection));
1203 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
1208 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
1209 McAccount *account, TpConnectionStatusReason reason,
1210 TpConnectionStatus status, TpConnectionStatus previous,
1211 EmpathyDispatcher *dispatcher)
1213 dispatcher_update_account (dispatcher, account);
1217 dispatcher_finalize (GObject *object)
1219 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1221 g_signal_handlers_disconnect_by_func (priv->account_manager,
1222 dispatcher_account_connection_cb, object);
1224 g_object_unref (priv->account_manager);
1225 g_object_unref (priv->mc);
1227 g_hash_table_destroy (priv->connections);
1229 g_object_unref (priv->chatroom_mgr);
1233 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1235 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1237 object_class->finalize = dispatcher_finalize;
1240 g_signal_new ("observe",
1241 G_TYPE_FROM_CLASS (klass),
1245 g_cclosure_marshal_VOID__OBJECT,
1247 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1250 g_signal_new ("approve",
1251 G_TYPE_FROM_CLASS (klass),
1255 g_cclosure_marshal_VOID__OBJECT,
1257 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1260 g_signal_new ("dispatch",
1261 G_TYPE_FROM_CLASS (klass),
1265 g_cclosure_marshal_VOID__OBJECT,
1267 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1269 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1274 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1276 GList *accounts, *l;
1277 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1278 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1280 dispatcher->priv = priv;
1281 priv->mc = empathy_mission_control_new ();
1282 priv->account_manager = empathy_account_manager_dup_singleton ();
1284 g_signal_connect (priv->account_manager,
1285 "account-connection-changed",
1286 G_CALLBACK (dispatcher_account_connection_cb),
1289 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1290 empathy_account_equal, g_object_unref, g_object_unref);
1292 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1293 g_object_unref, (GDestroyNotify) free_connection_data);
1295 accounts = mc_accounts_list_by_enabled (TRUE);
1297 for (l = accounts; l; l = l->next) {
1298 dispatcher_update_account (dispatcher, l->data);
1299 g_object_unref (l->data);
1301 g_list_free (accounts);
1303 /* FIXME thisshould probably be created by another object.. */
1304 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
1305 empathy_chatroom_manager_observe (priv->chatroom_mgr, dispatcher);
1309 empathy_get_dispatcher (void)
1312 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
1313 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
1315 g_object_ref (dispatcher);
1322 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1323 DispatcherRequestData *request_data, const gchar *object_path,
1324 GHashTable *properties, const GError *error)
1326 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1327 EmpathyDispatchOperation *operation = NULL;
1328 ConnectionData *conn_data;
1330 conn_data = g_hash_table_lookup (priv->connections,
1331 request_data->connection);
1335 DEBUG ("Channel request failed: %s", error->message);
1337 if (request_data->cb != NULL)
1338 request_data->cb (NULL, error, request_data->user_data);
1340 conn_data->outstanding_requests =
1341 g_list_remove (conn_data->outstanding_requests, request_data);
1342 free_dispatcher_request_data (request_data);
1347 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1350 if (operation != NULL)
1351 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1353 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1356 if (operation == NULL)
1358 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1363 operation = empathy_dispatch_operation_new_with_wrapper (
1364 request_data->connection,
1365 data->channel, request_data->contact, FALSE,
1366 data->channel_wrapper);
1372 if (properties != NULL)
1373 channel = tp_channel_new_from_properties (request_data->connection,
1374 object_path, properties, NULL);
1376 channel = tp_channel_new (request_data->connection, object_path,
1377 request_data->channel_type, request_data->handle_type,
1378 request_data->handle, NULL);
1380 g_signal_connect (channel, "invalidated",
1381 G_CALLBACK (dispatcher_channel_invalidated_cb),
1382 request_data->dispatcher);
1384 operation = empathy_dispatch_operation_new (request_data->connection,
1385 channel, request_data->contact, FALSE);
1386 g_object_unref (channel);
1391 /* Already existed set potential extra information */
1392 g_object_set (G_OBJECT (operation),
1393 "contact", request_data->contact,
1397 request_data->operation = operation;
1399 /* (pre)-approve this right away as we requested it */
1400 empathy_dispatch_operation_approve (operation);
1402 dispatcher_start_dispatching (request_data->dispatcher, operation,
1405 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1410 dispatcher_request_channel_cb (TpConnection *connection,
1411 const gchar *object_path, const GError *error,
1412 gpointer user_data, GObject *weak_object)
1414 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1415 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1417 dispatcher_connection_new_requested_channel (dispatcher,
1418 request_data, object_path, NULL, error);
1422 dispatcher_request_channel (DispatcherRequestData *request_data)
1424 tp_cli_connection_call_request_channel (request_data->connection, -1,
1425 request_data->channel_type,
1426 request_data->handle_type,
1427 request_data->handle,
1428 TRUE, dispatcher_request_channel_cb,
1429 request_data, NULL, G_OBJECT (request_data->dispatcher));
1433 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1434 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1436 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1437 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1439 TpConnection *connection;
1441 DispatcherRequestData *request_data;
1443 account = empathy_contact_get_account (contact);
1444 connection = g_hash_table_lookup (priv->accounts, account);
1446 g_assert (connection != NULL);
1447 cd = g_hash_table_lookup (priv->connections, connection);
1448 request_data = new_dispatcher_request_data (dispatcher, connection,
1449 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1450 contact, callback, user_data);
1452 cd->outstanding_requests = g_list_prepend
1453 (cd->outstanding_requests, request_data);
1455 dispatcher_request_channel (request_data);
1457 g_object_unref (dispatcher);
1462 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1464 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1466 request_data->handle = empathy_contact_get_handle (contact);
1468 dispatcher_request_channel (request_data);
1472 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1473 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1475 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1476 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1477 McAccount *account = empathy_contact_get_account (contact);
1478 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1479 ConnectionData *connection_data =
1480 g_hash_table_lookup (priv->connections, connection);
1481 DispatcherRequestData *request_data;
1483 /* The contact handle might not be known yet */
1484 request_data = new_dispatcher_request_data (dispatcher, connection,
1485 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1486 contact, callback, user_data);
1488 connection_data->outstanding_requests = g_list_prepend
1489 (connection_data->outstanding_requests, request_data);
1491 empathy_contact_call_when_ready (contact,
1492 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1495 g_object_unref (dispatcher);
1499 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1500 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1502 EmpathyDispatcher *dispatcher = empathy_get_dispatcher ();
1503 EmpathyContactFactory *factory;
1504 EmpathyContact *contact;
1506 factory = empathy_contact_factory_dup_singleton ();
1507 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1509 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1511 g_object_unref (contact);
1512 g_object_unref (factory);
1513 g_object_unref (dispatcher);
1518 dispatcher_request_handles_cb (TpConnection *connection,
1519 const GArray *handles, const GError *error, gpointer user_data,
1522 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1526 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1527 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1530 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1532 if (request_data->cb)
1533 request_data->cb (NULL, error, request_data->user_data);
1535 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1538 free_dispatcher_request_data (request_data);
1540 dispatcher_flush_outstanding_operations (dispatcher, cd);
1544 request_data->handle = g_array_index (handles, guint, 0);
1545 dispatcher_request_channel (request_data);
1549 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1550 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1552 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1553 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1554 DispatcherRequestData *request_data;
1555 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1556 ConnectionData *connection_data =
1557 g_hash_table_lookup (priv->connections, connection);
1558 const gchar *names[] = { roomname, NULL };
1560 /* Don't know the room handle yet */
1561 request_data = new_dispatcher_request_data (dispatcher, connection,
1562 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1563 NULL, callback, user_data);
1565 connection_data->outstanding_requests = g_list_prepend
1566 (connection_data->outstanding_requests, request_data);
1568 tp_cli_connection_call_request_handles (connection, -1,
1569 TP_HANDLE_TYPE_ROOM, names,
1570 dispatcher_request_handles_cb, request_data, NULL,
1571 G_OBJECT (dispatcher));
1573 g_object_unref (dispatcher);
1580 } FileChannelRequest;
1583 tp_file_state_notify_cb (EmpathyTpFile *tp_file)
1585 EmpFileTransferState state;
1587 state = empathy_tp_file_get_state (tp_file, NULL);
1588 if (state == EMP_FILE_TRANSFER_STATE_COMPLETED ||
1589 state == EMP_FILE_TRANSFER_STATE_CANCELLED) {
1590 DEBUG ("Transfer is done, unref the object");
1591 g_object_unref (tp_file);
1596 file_channel_create_cb (TpConnection *connection,
1597 const gchar *object_path,
1598 GHashTable *properties,
1599 const GError *error,
1601 GObject *weak_object)
1604 EmpathyTpFile *tp_file;
1605 FileChannelRequest *request = (FileChannelRequest *) user_data;
1608 DEBUG ("Couldn't request channel: %s", error->message);
1612 channel = tp_channel_new (connection,
1614 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1615 TP_HANDLE_TYPE_CONTACT,
1619 /* We give the ref to the callback, it is responsible to unref the
1620 * object once the transfer is done. */
1621 tp_file = empathy_tp_file_new (channel);
1622 empathy_tp_file_offer (tp_file, request->gfile, NULL);
1623 g_signal_connect (tp_file, "notify::state",
1624 G_CALLBACK (tp_file_state_notify_cb),
1627 g_object_unref (request->gfile);
1628 g_slice_free (FileChannelRequest, request);
1629 g_object_unref (channel);
1636 dispatcher_create_channel_cb (TpConnection *connect,
1637 const gchar *object_path, GHashTable *properties, const GError *error,
1638 gpointer user_data, GObject *weak_object)
1640 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1641 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1643 dispatcher_connection_new_requested_channel (dispatcher,
1644 request_data, object_path, properties, error);
1649 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1652 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1653 GValue *target_handle;
1655 g_assert (request_data->request);
1657 request_data->handle = empathy_contact_get_handle (contact);
1659 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1660 g_value_set_uint (target_handle, request_data->handle);
1661 g_hash_table_insert (request_data->request,
1662 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1664 tp_cli_connection_interface_requests_call_create_channel (
1665 request_data->connection, -1,
1666 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1667 G_OBJECT (request_data->dispatcher));
1671 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1672 const gchar *filename, guint64 size, guint64 date,
1673 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1676 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1677 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1678 McAccount *account = empathy_contact_get_account (contact);
1679 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1680 ConnectionData *connection_data =
1681 g_hash_table_lookup (priv->connections, connection);
1682 DispatcherRequestData *request_data;
1684 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1685 (GDestroyNotify) tp_g_value_slice_free);
1687 /* org.freedesktop.Telepathy.Channel.ChannelType */
1688 value = tp_g_value_slice_new (G_TYPE_STRING);
1689 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1690 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1692 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1693 value = tp_g_value_slice_new (G_TYPE_UINT);
1694 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1695 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1697 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1698 value = tp_g_value_slice_new (G_TYPE_STRING);
1699 g_value_set_string (value, content_type);
1700 g_hash_table_insert (request,
1701 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1703 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1704 value = tp_g_value_slice_new (G_TYPE_STRING);
1705 g_value_set_string (value, filename);
1706 g_hash_table_insert (request,
1707 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1709 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1710 value = tp_g_value_slice_new (G_TYPE_UINT64);
1711 g_value_set_uint64 (value, size);
1712 g_hash_table_insert (request,
1713 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1715 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1716 value = tp_g_value_slice_new (G_TYPE_UINT64);
1717 g_value_set_uint64 (value, date);
1718 g_hash_table_insert (request,
1719 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1722 /* The contact handle might not be known yet */
1723 request_data = new_dispatcher_request_data (dispatcher, connection,
1724 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1725 contact, callback, user_data);
1726 connection_data->outstanding_requests = g_list_prepend
1727 (connection_data->outstanding_requests, request_data);
1729 empathy_contact_call_when_ready (contact,
1730 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1733 g_object_unref (dispatcher);
1739 TpConnection *connection;
1741 FileChannelRequest *request;
1746 GTimeVal last_modif;
1747 GError *error = NULL;
1749 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1750 g_return_if_fail (G_IS_FILE (gfile));
1752 info = g_file_query_info (gfile,
1753 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1754 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1755 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1759 DEBUG ("Can't get info about the file: %s", error->message);
1760 g_clear_error (&error);
1764 mc = empathy_mission_control_new ();
1765 account = empathy_contact_get_account (contact);
1766 connection = mission_control_get_tpconnection (mc, account, NULL);
1767 handle = empathy_contact_get_handle (contact);
1769 request = g_slice_new0 (FileChannelRequest);
1770 request->gfile = g_object_ref (gfile);
1771 request->handle = handle;
1773 filename = g_file_get_basename (request->gfile);
1774 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
1776 DEBUG ("Sending %s from a stream to %s (size %"G_GINT64_FORMAT", content-type %s)",
1777 filename, empathy_contact_get_name (contact),
1778 g_file_info_get_size (info),
1779 g_file_info_get_content_type (info));
1781 args = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1782 (GDestroyNotify) tp_g_value_slice_free);
1784 /* org.freedesktop.Telepathy.Channel.ChannelType */
1785 value = tp_g_value_slice_new (G_TYPE_STRING);
1786 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1787 g_hash_table_insert (args, TP_IFACE_CHANNEL ".ChannelType", value);
1789 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1790 value = tp_g_value_slice_new (G_TYPE_UINT);
1791 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1792 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandleType", value);
1794 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1795 value = tp_g_value_slice_new (G_TYPE_UINT);
1796 g_value_set_uint (value, handle);
1797 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandle", value);
1799 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1800 value = tp_g_value_slice_new (G_TYPE_STRING);
1801 g_value_set_string (value, g_file_info_get_content_type (info));
1802 g_hash_table_insert (args,
1803 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1805 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1806 value = tp_g_value_slice_new (G_TYPE_STRING);
1807 g_value_set_string (value, g_filename_display_basename (filename));
1808 g_hash_table_insert (args,
1809 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1811 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1812 value = tp_g_value_slice_new (G_TYPE_UINT64);
1813 g_value_set_uint64 (value, g_file_info_get_size (info));
1814 g_hash_table_insert (args,
1815 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1817 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1818 g_file_info_get_modification_time (info, &last_modif);
1819 value = tp_g_value_slice_new (G_TYPE_UINT64);
1820 g_value_set_uint64 (value, last_modif.tv_sec);
1821 g_hash_table_insert (args,
1822 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1824 /* FIXME: Description ? */
1825 /* FIXME: ContentHashType and ContentHash ? */
1827 tp_cli_connection_interface_requests_call_create_channel (connection, -1,
1828 args, file_channel_create_cb, request, NULL, NULL);
1830 g_hash_table_destroy (args);
1832 g_object_unref (mc);
1833 g_object_unref (connection);