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>
31 #include <telepathy-glib/gtypes.h>
33 #include <libmissioncontrol/mission-control.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-tp-contact-factory.h"
42 #include "empathy-tp-file.h"
43 #include "empathy-chatroom-manager.h"
44 #include "empathy-utils.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
52 EmpathyAccountManager *account_manager;
54 /* connection to connection data mapping */
55 GHashTable *connections;
59 /* channels which the dispatcher is listening "invalidated" */
61 } EmpathyDispatcherPriv;
63 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
73 static guint signals[LAST_SIGNAL];
74 static EmpathyDispatcher *dispatcher = NULL;
78 EmpathyDispatcher *dispatcher;
79 EmpathyDispatchOperation *operation;
80 TpConnection *connection;
84 EmpathyContact *contact;
86 /* Properties to pass to the channel when requesting it */
88 EmpathyDispatcherRequestCb *cb;
90 gpointer *request_data;
91 } DispatcherRequestData;
96 /* Channel type specific wrapper object */
97 GObject *channel_wrapper;
102 /* ObjectPath => DispatchData.. */
103 GHashTable *dispatched_channels;
104 /* ObjectPath -> EmpathyDispatchOperations */
105 GHashTable *dispatching_channels;
106 /* ObjectPath -> EmpathyDispatchOperations */
107 GHashTable *outstanding_channels;
108 /* List of DispatcherRequestData */
109 GList *outstanding_requests;
110 /* List of requestable channel classes */
111 GPtrArray *requestable_channels;
114 static DispatchData *
115 new_dispatch_data (TpChannel *channel,
116 GObject *channel_wrapper)
118 DispatchData *d = g_slice_new0 (DispatchData);
119 d->channel = g_object_ref (channel);
120 if (channel_wrapper != NULL)
121 d->channel_wrapper = g_object_ref (channel_wrapper);
127 free_dispatch_data (DispatchData *data)
129 g_object_unref (data->channel);
130 if (data->channel_wrapper != NULL)
131 g_object_unref (data->channel_wrapper);
133 g_slice_free (DispatchData, data);
136 static DispatcherRequestData *
137 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
138 TpConnection *connection,
139 const gchar *channel_type,
143 EmpathyContact *contact,
144 EmpathyDispatcherRequestCb *cb,
147 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
149 result->dispatcher = g_object_ref (dispatcher);
150 result->connection = connection;
152 result->channel_type = g_strdup (channel_type);
153 result->handle_type = handle_type;
154 result->handle = handle;
155 result->request = request;
158 result->contact = g_object_ref (contact);
161 result->user_data = user_data;
167 free_dispatcher_request_data (DispatcherRequestData *r)
169 g_free (r->channel_type);
171 if (r->dispatcher != NULL)
172 g_object_unref (r->dispatcher);
174 if (r->contact != NULL)
175 g_object_unref (r->contact);
177 if (r->request != NULL)
178 g_hash_table_unref (r->request);
180 g_slice_free (DispatcherRequestData, r);
183 static ConnectionData *
184 new_connection_data (void)
186 ConnectionData *cd = g_slice_new0 (ConnectionData);
188 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
189 g_free, (GDestroyNotify) free_dispatch_data);
191 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
192 g_free, g_object_unref);
194 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
201 free_connection_data (ConnectionData *cd)
205 g_hash_table_destroy (cd->dispatched_channels);
206 g_hash_table_destroy (cd->dispatching_channels);
209 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
211 free_dispatcher_request_data (l->data);
214 if (cd->requestable_channels != NULL)
216 for (i = 0 ; i < cd->requestable_channels->len ; i++)
218 g_ptr_array_index (cd->requestable_channels, i));
219 g_ptr_array_free (cd->requestable_channels, TRUE);
224 dispatcher_connection_invalidated_cb (TpConnection *connection,
228 EmpathyDispatcher *dispatcher)
230 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
232 DEBUG ("Error: %s", message);
233 g_hash_table_remove (priv->connections, connection);
237 dispatcher_operation_can_start (EmpathyDispatcher *self,
238 EmpathyDispatchOperation *operation,
242 const gchar *channel_type =
243 empathy_dispatch_operation_get_channel_type (operation);
245 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
247 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
249 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
259 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
260 EmpathyDispatchOperation *operation,
266 l = cd->outstanding_requests;
269 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
274 if (d->operation == operation)
279 d->cb (NULL, error, d->user_data);
281 d->cb (operation, NULL, d->user_data);
284 cd->outstanding_requests = g_list_delete_link
285 (cd->outstanding_requests, lt);
287 free_dispatcher_request_data (d);
293 dispatcher_channel_invalidated_cb (TpProxy *proxy,
297 EmpathyDispatcher *dispatcher)
299 /* Channel went away... */
300 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
301 TpConnection *connection;
302 EmpathyDispatchOperation *operation;
304 const gchar *object_path;
306 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
308 cd = g_hash_table_lookup (priv->connections, connection);
309 /* Connection itself invalidated? */
313 object_path = tp_proxy_get_object_path (proxy);
315 DEBUG ("Channel %s invalidated", object_path);
317 g_hash_table_remove (cd->dispatched_channels, object_path);
318 g_hash_table_remove (cd->dispatching_channels, object_path);
320 priv->channels = g_list_remove (priv->channels, proxy);
322 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
323 if (operation != NULL)
325 GError error = { domain, code, message };
326 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
327 g_hash_table_remove (cd->outstanding_channels, object_path);
328 g_object_unref (operation);
333 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
334 EmpathyDispatcher *dispatcher)
336 g_assert (empathy_dispatch_operation_is_incoming (operation));
337 DEBUG ("Send of for dispatching: %s",
338 empathy_dispatch_operation_get_object_path (operation));
339 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
343 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
344 EmpathyDispatcher *dispatcher)
346 /* Our job is done, remove the dispatch operation and mark the channel as
348 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
349 TpConnection *connection;
351 const gchar *object_path;
353 connection = empathy_dispatch_operation_get_tp_connection (operation);
354 cd = g_hash_table_lookup (priv->connections, connection);
355 g_assert (cd != NULL);
356 g_object_unref (G_OBJECT (connection));
358 object_path = empathy_dispatch_operation_get_object_path (operation);
360 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
363 d = new_dispatch_data (
364 empathy_dispatch_operation_get_channel (operation),
365 empathy_dispatch_operation_get_channel_wrapper (operation));
366 g_hash_table_insert (cd->dispatched_channels,
367 g_strdup (object_path), d);
369 g_hash_table_remove (cd->dispatching_channels, object_path);
371 DEBUG ("Channel claimed: %s", object_path);
375 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
376 EmpathyDispatcher *dispatcher)
378 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
379 TpConnection *connection;
381 EmpathyDispatchOperationState status;
383 g_signal_connect (operation, "approved",
384 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
386 g_signal_connect (operation, "claimed",
387 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
389 /* Signal the observers */
390 DEBUG ("Send to observers: %s",
391 empathy_dispatch_operation_get_object_path (operation));
392 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
394 empathy_dispatch_operation_start (operation);
396 /* Signal potential requestors */
397 connection = empathy_dispatch_operation_get_tp_connection (operation);
398 cd = g_hash_table_lookup (priv->connections, connection);
399 g_assert (cd != NULL);
400 g_object_unref (G_OBJECT (connection));
402 g_object_ref (operation);
403 g_object_ref (dispatcher);
405 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
406 status = empathy_dispatch_operation_get_status (operation);
407 g_object_unref (operation);
409 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
412 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
414 DEBUG ("Send to approvers: %s",
415 empathy_dispatch_operation_get_object_path (operation));
416 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
420 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
421 DEBUG ("Send of for dispatching: %s",
422 empathy_dispatch_operation_get_object_path (operation));
423 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
426 g_object_unref (dispatcher);
430 dispatcher_start_dispatching (EmpathyDispatcher *self,
431 EmpathyDispatchOperation *operation,
434 const gchar *object_path =
435 empathy_dispatch_operation_get_object_path (operation);
437 DEBUG ("Dispatching process started for %s", object_path);
439 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
441 g_assert (g_hash_table_lookup (cd->outstanding_channels,
442 object_path) == NULL);
444 g_hash_table_insert (cd->dispatching_channels,
445 g_strdup (object_path), operation);
447 switch (empathy_dispatch_operation_get_status (operation))
449 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
450 g_signal_connect (operation, "ready",
451 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
453 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
454 dispatch_operation_ready_cb (operation, dispatcher);
457 g_assert_not_reached();
461 else if (empathy_dispatch_operation_get_status (operation) >=
462 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
464 /* Already dispatching and the operation is pending, thus the observers
465 * have seen it (if applicable), so we can flush the request right away.
467 dispatch_operation_flush_requests (self, operation, NULL, cd);
472 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
478 g_hash_table_iter_init (&iter, cd->outstanding_channels);
479 while (g_hash_table_iter_next (&iter, NULL, &value))
481 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
483 if (dispatcher_operation_can_start (self, operation, cd))
485 dispatcher_start_dispatching (dispatcher, operation, cd);
486 g_hash_table_iter_remove (&iter);
492 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
493 TpConnection *connection,
494 const gchar *object_path,
495 const gchar *channel_type,
498 GHashTable *properties,
501 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
504 EmpathyDispatchOperation *operation;
506 /* Channel types we never want to dispatch because they're either deprecated
507 * or can't sensibly be dispatch (e.g. channels that should always be
509 const char *blacklist[] = {
510 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
511 TP_IFACE_CHANNEL_TYPE_TUBES,
512 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
516 cd = g_hash_table_lookup (priv->connections, connection);
518 /* Don't bother with channels we have already dispatched or are dispatching
519 * currently. This can happen when NewChannel(s) is fired after
520 * RequestChannel/CreateChannel/EnsureChannel */
521 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
524 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
527 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
529 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
532 /* Only pick up non-requested text and file channels. For all other it
533 * doesn't make sense to handle it if we didn't request it. The same goes
534 * for channels we discovered by the Channels property or ListChannels */
535 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
536 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
538 DEBUG ("Ignoring incoming channel of type %s on %s",
539 channel_type, object_path);
543 for (i = 0 ; blacklist[i] != NULL; i++)
545 if (!tp_strdiff (channel_type, blacklist[i]))
547 DEBUG ("Ignoring blacklisted channel type %s on %s",
548 channel_type, object_path);
553 DEBUG ("New channel of type %s on %s", channel_type, object_path);
555 if (properties == NULL)
556 channel = tp_channel_new (connection, object_path, channel_type,
557 handle_type, handle, NULL);
559 channel = tp_channel_new_from_properties (connection, object_path,
562 g_signal_connect (channel, "invalidated",
563 G_CALLBACK (dispatcher_channel_invalidated_cb),
566 priv->channels = g_list_prepend (priv->channels, channel);
568 operation = empathy_dispatch_operation_new (connection, channel, NULL,
571 g_object_unref (channel);
575 /* Request could either be by us or by a remote party. If there are no
576 * outstanding requests for this channel type we can assume it's remote.
577 * Otherwise we wait untill they are all satisfied */
578 if (dispatcher_operation_can_start (dispatcher, operation, cd))
579 dispatcher_start_dispatching (dispatcher, operation, cd);
581 g_hash_table_insert (cd->outstanding_channels,
582 g_strdup (object_path), operation);
586 dispatcher_start_dispatching (dispatcher, operation, cd);
591 dispatcher_connection_new_channel_cb (TpConnection *connection,
592 const gchar *object_path,
593 const gchar *channel_type,
596 gboolean suppress_handler,
600 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
602 /* Empathy heavily abuses surpress handler (don't try this at home), if
603 * surpress handler is true then it is an outgoing channel, which is
604 * requested either by us or some other party (like the megaphone applet).
605 * Otherwise it's an incoming channel */
606 dispatcher_connection_new_channel (dispatcher, connection,
607 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
611 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
612 TpConnection *connection,
613 const gchar *object_path,
614 GHashTable *properties)
616 const gchar *channel_type;
623 channel_type = tp_asv_get_string (properties,
624 TP_IFACE_CHANNEL ".ChannelType");
625 if (channel_type == NULL)
627 g_message ("%s had an invalid ChannelType property", object_path);
631 handle_type = tp_asv_get_uint32 (properties,
632 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
635 g_message ("%s had an invalid TargetHandleType property", object_path);
639 handle = tp_asv_get_uint32 (properties,
640 TP_IFACE_CHANNEL ".TargetHandle", &valid);
643 g_message ("%s had an invalid TargetHandle property", object_path);
647 /* We assume there is no channel dispather, so we're the only one dispatching
648 * it. Which means that a requested channel it is outgoing one */
649 requested = tp_asv_get_boolean (properties,
650 TP_IFACE_CHANNEL ".Requested", &valid);
653 g_message ("%s had an invalid Requested property", object_path);
657 dispatcher_connection_new_channel (dispatcher, connection,
658 object_path, channel_type, handle_type, handle, properties, !requested);
662 dispatcher_connection_new_channels_cb (TpConnection *connection,
663 const GPtrArray *channels,
667 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
670 for (i = 0; i < channels->len ; i++)
672 GValueArray *arr = g_ptr_array_index (channels, i);
673 const gchar *object_path;
674 GHashTable *properties;
676 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
677 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
679 dispatcher_connection_new_channel_with_properties (dispatcher,
680 connection, object_path, properties);
685 dispatcher_connection_got_all (TpProxy *proxy,
686 GHashTable *properties,
691 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
692 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
694 GPtrArray *requestable_channels;
697 DEBUG ("Error: %s", error->message);
701 channels = tp_asv_get_boxed (properties, "Channels",
702 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
704 if (channels == NULL)
705 DEBUG ("No Channels property !?! on connection");
707 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
708 channels, NULL, object);
710 requestable_channels = tp_asv_get_boxed (properties,
711 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
713 if (requestable_channels == NULL)
714 DEBUG ("No RequestableChannelClasses property !?! on connection");
719 cd = g_hash_table_lookup (priv->connections, proxy);
720 g_assert (cd != NULL);
722 cd->requestable_channels = g_boxed_copy (
723 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
728 dispatcher_connection_list_channels_cb (TpConnection *connection,
729 const GPtrArray *channels,
738 DEBUG ("Error: %s", error->message);
742 for (i = 0; i < channels->len; i++)
746 values = g_ptr_array_index (channels, i);
747 /* We don't have any extra info, so assume already existing channels are
749 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
751 g_value_get_boxed (g_value_array_get_nth (values, 0)),
752 g_value_get_string (g_value_array_get_nth (values, 1)),
753 g_value_get_uint (g_value_array_get_nth (values, 2)),
754 g_value_get_uint (g_value_array_get_nth (values, 3)),
760 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
761 const GPtrArray *capabilities,
767 DEBUG ("Error: %s", error->message);
771 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
772 TpConnection *connection,
773 EmpathyDispatcher *dispatcher)
775 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
776 GPtrArray *capabilities;
779 const gchar *remove = NULL;
781 if (g_hash_table_lookup (priv->connections, connection) != NULL)
784 g_hash_table_insert (priv->connections, g_object_ref (connection),
785 new_connection_data ());
787 g_signal_connect (connection, "invalidated",
788 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
790 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
791 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
793 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
794 dispatcher_connection_new_channels_cb,
795 NULL, NULL, G_OBJECT (dispatcher), NULL);
797 tp_cli_dbus_properties_call_get_all (connection, -1,
798 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
799 dispatcher_connection_got_all,
800 NULL, NULL, G_OBJECT (dispatcher));
804 tp_cli_connection_connect_to_new_channel (connection,
805 dispatcher_connection_new_channel_cb,
806 NULL, NULL, G_OBJECT (dispatcher), NULL);
808 tp_cli_connection_call_list_channels (connection, -1,
809 dispatcher_connection_list_channels_cb, NULL, NULL,
810 G_OBJECT (dispatcher));
814 /* Advertise VoIP capabilities */
815 capabilities = g_ptr_array_sized_new (1);
816 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
817 G_TYPE_UINT, G_TYPE_INVALID);
818 g_value_init (&cap, cap_type);
819 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
820 dbus_g_type_struct_set (&cap,
821 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
822 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
823 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
824 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
825 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
826 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
828 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
829 connection, -1, capabilities, &remove,
830 dispatcher_connection_advertise_capabilities_cb,
831 NULL, NULL, G_OBJECT (dispatcher));
833 g_value_unset (&cap);
834 g_ptr_array_free (capabilities, TRUE);
838 dispatcher_constructor (GType type,
839 guint n_construct_params,
840 GObjectConstructParam *construct_params)
844 if (dispatcher == NULL)
846 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
847 (type, n_construct_params, construct_params);
849 dispatcher = EMPATHY_DISPATCHER (retval);
850 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
854 retval = g_object_ref (dispatcher);
861 dispatcher_finalize (GObject *object)
863 EmpathyDispatcherPriv *priv = GET_PRIV (object);
868 g_signal_handlers_disconnect_by_func (priv->account_manager,
869 dispatcher_new_connection_cb, object);
871 for (l = priv->channels; l; l = l->next)
873 g_signal_handlers_disconnect_by_func (l->data,
874 dispatcher_channel_invalidated_cb, object);
877 g_list_free (priv->channels);
879 g_hash_table_iter_init (&iter, priv->connections);
880 while (g_hash_table_iter_next (&iter, &connection, NULL))
882 g_signal_handlers_disconnect_by_func (connection,
883 dispatcher_connection_invalidated_cb, object);
886 g_object_unref (priv->account_manager);
887 g_object_unref (priv->mc);
889 g_hash_table_destroy (priv->connections);
893 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
895 GObjectClass *object_class = G_OBJECT_CLASS (klass);
897 object_class->finalize = dispatcher_finalize;
898 object_class->constructor = dispatcher_constructor;
901 g_signal_new ("observe",
902 G_TYPE_FROM_CLASS (klass),
906 g_cclosure_marshal_VOID__OBJECT,
908 1, EMPATHY_TYPE_DISPATCH_OPERATION);
911 g_signal_new ("approve",
912 G_TYPE_FROM_CLASS (klass),
916 g_cclosure_marshal_VOID__OBJECT,
918 1, EMPATHY_TYPE_DISPATCH_OPERATION);
921 g_signal_new ("dispatch",
922 G_TYPE_FROM_CLASS (klass),
926 g_cclosure_marshal_VOID__OBJECT,
928 1, EMPATHY_TYPE_DISPATCH_OPERATION);
930 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
935 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
937 GList *connections, *l;
938 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
939 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
941 dispatcher->priv = priv;
942 priv->mc = empathy_mission_control_dup_singleton ();
943 priv->account_manager = empathy_account_manager_dup_singleton ();
945 g_signal_connect (priv->account_manager, "new-connection",
946 G_CALLBACK (dispatcher_new_connection_cb),
949 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
950 g_object_unref, (GDestroyNotify) free_connection_data);
952 priv->channels = NULL;
954 connections = empathy_account_manager_dup_connections (priv->account_manager);
955 for (l = connections; l; l = l->next)
957 dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
958 g_object_unref (l->data);
960 g_list_free (connections);
964 empathy_dispatcher_dup_singleton (void)
966 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
970 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
971 DispatcherRequestData *request_data,
974 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
975 ConnectionData *conn_data;
977 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
978 if (request_data->cb != NULL)
979 request_data->cb (NULL, error, request_data->user_data);
981 conn_data->outstanding_requests =
982 g_list_remove (conn_data->outstanding_requests, request_data);
983 free_dispatcher_request_data (request_data);
987 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
988 DispatcherRequestData *request_data,
989 const gchar *object_path,
990 GHashTable *properties,
993 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
994 EmpathyDispatchOperation *operation = NULL;
995 ConnectionData *conn_data;
997 conn_data = g_hash_table_lookup (priv->connections,
998 request_data->connection);
1002 DEBUG ("Channel request failed: %s", error->message);
1004 dispatcher_request_failed (dispatcher, request_data, error);
1009 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1012 if (operation != NULL)
1013 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1015 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1018 if (operation == NULL)
1020 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1025 operation = empathy_dispatch_operation_new_with_wrapper (
1026 request_data->connection,
1027 data->channel, request_data->contact, FALSE,
1028 data->channel_wrapper);
1034 if (properties != NULL)
1035 channel = tp_channel_new_from_properties (request_data->connection,
1036 object_path, properties, NULL);
1038 channel = tp_channel_new (request_data->connection, object_path,
1039 request_data->channel_type, request_data->handle_type,
1040 request_data->handle, NULL);
1042 g_signal_connect (channel, "invalidated",
1043 G_CALLBACK (dispatcher_channel_invalidated_cb),
1044 request_data->dispatcher);
1046 priv->channels = g_list_prepend (priv->channels, channel);
1048 operation = empathy_dispatch_operation_new (request_data->connection,
1049 channel, request_data->contact, FALSE);
1050 g_object_unref (channel);
1055 /* Already existed set potential extra information */
1056 g_object_set (G_OBJECT (operation),
1057 "contact", request_data->contact,
1061 request_data->operation = operation;
1063 /* (pre)-approve this right away as we requested it
1064 * This might cause the channel to be claimed, in which case the operation
1065 * will disappear. So ref it, and check the status before starting the
1068 g_object_ref (operation);
1069 empathy_dispatch_operation_approve (operation);
1071 if (empathy_dispatch_operation_get_status (operation) <
1072 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1073 dispatcher_start_dispatching (request_data->dispatcher, operation,
1076 g_object_unref (operation);
1079 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1084 dispatcher_request_channel_cb (TpConnection *connection,
1085 const gchar *object_path,
1086 const GError *error,
1088 GObject *weak_object)
1090 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1091 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1093 dispatcher_connection_new_requested_channel (dispatcher,
1094 request_data, object_path, NULL, error);
1098 dispatcher_request_channel (DispatcherRequestData *request_data)
1100 tp_cli_connection_call_request_channel (request_data->connection, -1,
1101 request_data->channel_type,
1102 request_data->handle_type,
1103 request_data->handle,
1104 TRUE, dispatcher_request_channel_cb,
1105 request_data, NULL, G_OBJECT (request_data->dispatcher));
1109 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1110 EmpathyDispatcherRequestCb *callback,
1113 EmpathyDispatcher *dispatcher;
1114 EmpathyDispatcherPriv *priv;
1115 TpConnection *connection;
1116 ConnectionData *connection_data;
1117 DispatcherRequestData *request_data;
1119 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1121 dispatcher = empathy_dispatcher_dup_singleton();
1122 priv = GET_PRIV (dispatcher);
1124 connection = empathy_contact_get_connection (contact);
1125 connection_data = g_hash_table_lookup (priv->connections, connection);
1127 /* The contact handle might not be known yet */
1128 request_data = new_dispatcher_request_data (dispatcher, connection,
1129 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1130 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1132 connection_data->outstanding_requests = g_list_prepend
1133 (connection_data->outstanding_requests, request_data);
1135 dispatcher_request_channel (request_data);
1137 g_object_unref (dispatcher);
1142 EmpathyDispatcher *dispatcher;
1143 EmpathyDispatcherRequestCb *callback;
1145 } ChatWithContactIdData;
1148 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1149 EmpathyContact *contact,
1150 const GError *error,
1152 GObject *weak_object)
1154 ChatWithContactIdData *data = user_data;
1158 /* FIXME: Should call data->callback with the error */
1159 DEBUG ("Error: %s", error->message);
1163 empathy_dispatcher_chat_with_contact (contact, data->callback, data->user_data);
1166 g_object_unref (data->dispatcher);
1167 g_slice_free (ChatWithContactIdData, data);
1171 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1172 const gchar *contact_id,
1173 EmpathyDispatcherRequestCb *callback,
1176 EmpathyDispatcher *dispatcher;
1177 EmpathyTpContactFactory *factory;
1178 ChatWithContactIdData *data;
1180 g_return_if_fail (TP_IS_CONNECTION (connection));
1181 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1183 dispatcher = empathy_dispatcher_dup_singleton ();
1184 factory = empathy_tp_contact_factory_dup_singleton (connection);
1185 data = g_slice_new0 (ChatWithContactIdData);
1186 data->dispatcher = dispatcher;
1187 data->callback = callback;
1188 data->user_data = user_data;
1189 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1190 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1192 g_object_unref (factory);
1196 dispatcher_request_handles_cb (TpConnection *connection,
1197 const GArray *handles,
1198 const GError *error,
1202 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1206 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1207 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1210 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1212 if (request_data->cb)
1213 request_data->cb (NULL, error, request_data->user_data);
1215 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1218 free_dispatcher_request_data (request_data);
1220 dispatcher_flush_outstanding_operations (dispatcher, cd);
1224 request_data->handle = g_array_index (handles, guint, 0);
1225 dispatcher_request_channel (request_data);
1229 empathy_dispatcher_join_muc (TpConnection *connection,
1230 const gchar *roomname,
1231 EmpathyDispatcherRequestCb *callback,
1234 EmpathyDispatcher *dispatcher;
1235 EmpathyDispatcherPriv *priv;
1236 DispatcherRequestData *request_data;
1237 ConnectionData *connection_data;
1238 const gchar *names[] = { roomname, NULL };
1240 g_return_if_fail (TP_IS_CONNECTION (connection));
1241 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1243 dispatcher = empathy_dispatcher_dup_singleton();
1244 priv = GET_PRIV (dispatcher);
1246 connection_data = g_hash_table_lookup (priv->connections, connection);
1248 /* Don't know the room handle yet */
1249 request_data = new_dispatcher_request_data (dispatcher, connection,
1250 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1251 NULL, callback, user_data);
1253 connection_data->outstanding_requests = g_list_prepend
1254 (connection_data->outstanding_requests, request_data);
1256 tp_cli_connection_call_request_handles (connection, -1,
1257 TP_HANDLE_TYPE_ROOM, names,
1258 dispatcher_request_handles_cb, request_data, NULL,
1259 G_OBJECT (dispatcher));
1261 g_object_unref (dispatcher);
1265 dispatcher_create_channel_cb (TpConnection *connect,
1266 const gchar *object_path,
1267 GHashTable *properties,
1268 const GError *error,
1270 GObject *weak_object)
1272 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1273 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1275 dispatcher_connection_new_requested_channel (dispatcher,
1276 request_data, object_path, properties, error);
1280 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1281 TpConnection *connection,
1282 GHashTable *request,
1283 EmpathyDispatcherRequestCb *callback,
1286 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1287 ConnectionData *connection_data;
1288 DispatcherRequestData *request_data;
1289 const gchar *channel_type;
1294 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1295 g_return_if_fail (TP_IS_CONNECTION (connection));
1296 g_return_if_fail (request != NULL);
1298 connection_data = g_hash_table_lookup (priv->connections, connection);
1299 g_assert (connection_data != NULL);
1301 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1303 handle_type = tp_asv_get_uint32 (request,
1304 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1306 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1308 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1310 request_data = new_dispatcher_request_data (dispatcher, connection,
1311 channel_type, handle_type, handle, request,
1312 NULL, callback, user_data);
1314 connection_data->outstanding_requests = g_list_prepend
1315 (connection_data->outstanding_requests, request_data);
1317 tp_cli_connection_interface_requests_call_create_channel (
1318 request_data->connection, -1,
1319 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1320 G_OBJECT (request_data->dispatcher));
1324 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1325 const gchar *filename,
1328 const gchar *content_type,
1329 EmpathyDispatcherRequestCb *callback,
1332 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1333 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1334 TpConnection *connection = empathy_contact_get_connection (contact);
1335 ConnectionData *connection_data =
1336 g_hash_table_lookup (priv->connections, connection);
1337 DispatcherRequestData *request_data;
1339 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1340 (GDestroyNotify) tp_g_value_slice_free);
1342 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1343 g_return_if_fail (!EMP_STR_EMPTY (filename));
1344 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1346 /* org.freedesktop.Telepathy.Channel.ChannelType */
1347 value = tp_g_value_slice_new (G_TYPE_STRING);
1348 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1349 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1351 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1352 value = tp_g_value_slice_new (G_TYPE_UINT);
1353 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1354 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1356 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1357 value = tp_g_value_slice_new (G_TYPE_UINT);
1358 g_value_set_uint (value, empathy_contact_get_handle (contact));
1359 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
1361 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1362 value = tp_g_value_slice_new (G_TYPE_STRING);
1363 g_value_set_string (value, content_type);
1364 g_hash_table_insert (request,
1365 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1367 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1368 value = tp_g_value_slice_new (G_TYPE_STRING);
1369 g_value_set_string (value, filename);
1370 g_hash_table_insert (request,
1371 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1373 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1374 value = tp_g_value_slice_new (G_TYPE_UINT64);
1375 g_value_set_uint64 (value, size);
1376 g_hash_table_insert (request,
1377 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1379 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1380 value = tp_g_value_slice_new (G_TYPE_UINT64);
1381 g_value_set_uint64 (value, date);
1382 g_hash_table_insert (request,
1383 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1385 request_data = new_dispatcher_request_data (dispatcher, connection,
1386 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
1387 empathy_contact_get_handle (contact), request, contact, callback,
1389 connection_data->outstanding_requests = g_list_prepend
1390 (connection_data->outstanding_requests, request_data);
1392 tp_cli_connection_interface_requests_call_create_channel (
1393 request_data->connection, -1,
1394 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1395 G_OBJECT (request_data->dispatcher));
1397 g_object_unref (dispatcher);
1401 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1402 TpConnection *connection,
1403 const gchar *channel_type,
1406 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1411 g_return_val_if_fail (channel_type != NULL, NULL);
1412 g_return_val_if_fail (handle_type != 0, NULL);
1414 cd = g_hash_table_lookup (priv->connections, connection);
1419 classes = cd->requestable_channels;
1420 if (classes == NULL)
1423 for (i = 0; i < classes->len; i++)
1429 const gchar *c_type;
1433 class = g_ptr_array_index (classes, i);
1434 fixed = g_value_array_get_nth (class, 0);
1436 fprops = g_value_get_boxed (fixed);
1437 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1439 if (tp_strdiff (channel_type, c_type))
1442 h_type = tp_asv_get_uint32 (fprops,
1443 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1445 if (!valid || handle_type != h_type)
1448 allowed = g_value_array_get_nth (class, 1);
1450 return g_value_get_boxed (allowed);