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);
357 object_path = empathy_dispatch_operation_get_object_path (operation);
359 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
362 d = new_dispatch_data (
363 empathy_dispatch_operation_get_channel (operation),
364 empathy_dispatch_operation_get_channel_wrapper (operation));
365 g_hash_table_insert (cd->dispatched_channels,
366 g_strdup (object_path), d);
368 g_hash_table_remove (cd->dispatching_channels, object_path);
370 DEBUG ("Channel claimed: %s", object_path);
374 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
375 EmpathyDispatcher *dispatcher)
377 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
378 TpConnection *connection;
380 EmpathyDispatchOperationState status;
382 g_signal_connect (operation, "approved",
383 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
385 g_signal_connect (operation, "claimed",
386 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
388 /* Signal the observers */
389 DEBUG ("Send to observers: %s",
390 empathy_dispatch_operation_get_object_path (operation));
391 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
393 empathy_dispatch_operation_start (operation);
395 /* Signal potential requestors */
396 connection = empathy_dispatch_operation_get_tp_connection (operation);
397 cd = g_hash_table_lookup (priv->connections, connection);
398 g_assert (cd != NULL);
400 g_object_ref (operation);
401 g_object_ref (dispatcher);
403 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
404 status = empathy_dispatch_operation_get_status (operation);
405 g_object_unref (operation);
407 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
410 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
412 DEBUG ("Send to approvers: %s",
413 empathy_dispatch_operation_get_object_path (operation));
414 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
418 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
419 DEBUG ("Send of for dispatching: %s",
420 empathy_dispatch_operation_get_object_path (operation));
421 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
424 g_object_unref (dispatcher);
428 dispatcher_start_dispatching (EmpathyDispatcher *self,
429 EmpathyDispatchOperation *operation,
432 const gchar *object_path =
433 empathy_dispatch_operation_get_object_path (operation);
435 DEBUG ("Dispatching process started for %s", object_path);
437 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
439 g_assert (g_hash_table_lookup (cd->outstanding_channels,
440 object_path) == NULL);
442 g_hash_table_insert (cd->dispatching_channels,
443 g_strdup (object_path), operation);
445 switch (empathy_dispatch_operation_get_status (operation))
447 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
448 g_signal_connect (operation, "ready",
449 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
451 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
452 dispatch_operation_ready_cb (operation, dispatcher);
455 g_assert_not_reached ();
459 else if (empathy_dispatch_operation_get_status (operation) >=
460 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
462 /* Already dispatching and the operation is pending, thus the observers
463 * have seen it (if applicable), so we can flush the request right away.
465 dispatch_operation_flush_requests (self, operation, NULL, cd);
470 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
476 g_hash_table_iter_init (&iter, cd->outstanding_channels);
477 while (g_hash_table_iter_next (&iter, NULL, &value))
479 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
481 if (dispatcher_operation_can_start (self, operation, cd))
483 dispatcher_start_dispatching (dispatcher, operation, cd);
484 g_hash_table_iter_remove (&iter);
490 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
491 TpConnection *connection,
492 const gchar *object_path,
493 const gchar *channel_type,
496 GHashTable *properties,
499 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
502 EmpathyDispatchOperation *operation;
504 /* Channel types we never want to dispatch because they're either deprecated
505 * or can't sensibly be dispatch (e.g. channels that should always be
507 const char *blacklist[] = {
508 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
509 TP_IFACE_CHANNEL_TYPE_TUBES,
510 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
514 cd = g_hash_table_lookup (priv->connections, connection);
516 /* Don't bother with channels we have already dispatched or are dispatching
517 * currently. This can happen when NewChannel(s) is fired after
518 * RequestChannel/CreateChannel/EnsureChannel */
519 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
522 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
525 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
527 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
530 /* Only pick up non-requested text and file channels. For all other it
531 * doesn't make sense to handle it if we didn't request it. The same goes
532 * for channels we discovered by the Channels property or ListChannels */
533 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
534 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
536 DEBUG ("Ignoring incoming channel of type %s on %s",
537 channel_type, object_path);
541 for (i = 0 ; blacklist[i] != NULL; i++)
543 if (!tp_strdiff (channel_type, blacklist[i]))
545 DEBUG ("Ignoring blacklisted channel type %s on %s",
546 channel_type, object_path);
551 DEBUG ("New channel of type %s on %s", channel_type, object_path);
553 if (properties == NULL)
554 channel = tp_channel_new (connection, object_path, channel_type,
555 handle_type, handle, NULL);
557 channel = tp_channel_new_from_properties (connection, object_path,
560 g_signal_connect (channel, "invalidated",
561 G_CALLBACK (dispatcher_channel_invalidated_cb),
564 priv->channels = g_list_prepend (priv->channels, channel);
566 operation = empathy_dispatch_operation_new (connection, channel, NULL,
569 g_object_unref (channel);
573 /* Request could either be by us or by a remote party. If there are no
574 * outstanding requests for this channel type we can assume it's remote.
575 * Otherwise we wait untill they are all satisfied */
576 if (dispatcher_operation_can_start (dispatcher, operation, cd))
577 dispatcher_start_dispatching (dispatcher, operation, cd);
579 g_hash_table_insert (cd->outstanding_channels,
580 g_strdup (object_path), operation);
584 dispatcher_start_dispatching (dispatcher, operation, cd);
589 dispatcher_connection_new_channel_cb (TpConnection *connection,
590 const gchar *object_path,
591 const gchar *channel_type,
594 gboolean suppress_handler,
598 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
600 /* Empathy heavily abuses surpress handler (don't try this at home), if
601 * surpress handler is true then it is an outgoing channel, which is
602 * requested either by us or some other party (like the megaphone applet).
603 * Otherwise it's an incoming channel */
604 dispatcher_connection_new_channel (dispatcher, connection,
605 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
609 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
610 TpConnection *connection,
611 const gchar *object_path,
612 GHashTable *properties)
614 const gchar *channel_type;
621 channel_type = tp_asv_get_string (properties,
622 TP_IFACE_CHANNEL ".ChannelType");
623 if (channel_type == NULL)
625 g_message ("%s had an invalid ChannelType property", object_path);
629 handle_type = tp_asv_get_uint32 (properties,
630 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
633 g_message ("%s had an invalid TargetHandleType property", object_path);
637 handle = tp_asv_get_uint32 (properties,
638 TP_IFACE_CHANNEL ".TargetHandle", &valid);
641 g_message ("%s had an invalid TargetHandle property", object_path);
645 /* We assume there is no channel dispather, so we're the only one dispatching
646 * it. Which means that a requested channel it is outgoing one */
647 requested = tp_asv_get_boolean (properties,
648 TP_IFACE_CHANNEL ".Requested", &valid);
651 g_message ("%s had an invalid Requested property", object_path);
655 dispatcher_connection_new_channel (dispatcher, connection,
656 object_path, channel_type, handle_type, handle, properties, !requested);
660 dispatcher_connection_new_channels_cb (TpConnection *connection,
661 const GPtrArray *channels,
665 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
668 for (i = 0; i < channels->len ; i++)
670 GValueArray *arr = g_ptr_array_index (channels, i);
671 const gchar *object_path;
672 GHashTable *properties;
674 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
675 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
677 dispatcher_connection_new_channel_with_properties (dispatcher,
678 connection, object_path, properties);
683 dispatcher_connection_got_all (TpProxy *proxy,
684 GHashTable *properties,
689 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
690 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
692 GPtrArray *requestable_channels;
695 DEBUG ("Error: %s", error->message);
699 channels = tp_asv_get_boxed (properties, "Channels",
700 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
702 if (channels == NULL)
703 DEBUG ("No Channels property !?! on connection");
705 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
706 channels, NULL, object);
708 requestable_channels = tp_asv_get_boxed (properties,
709 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
711 if (requestable_channels == NULL)
712 DEBUG ("No RequestableChannelClasses property !?! on connection");
717 cd = g_hash_table_lookup (priv->connections, proxy);
718 g_assert (cd != NULL);
720 cd->requestable_channels = g_boxed_copy (
721 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
726 dispatcher_connection_list_channels_cb (TpConnection *connection,
727 const GPtrArray *channels,
736 DEBUG ("Error: %s", error->message);
740 for (i = 0; i < channels->len; i++)
744 values = g_ptr_array_index (channels, i);
745 /* We don't have any extra info, so assume already existing channels are
747 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
749 g_value_get_boxed (g_value_array_get_nth (values, 0)),
750 g_value_get_string (g_value_array_get_nth (values, 1)),
751 g_value_get_uint (g_value_array_get_nth (values, 2)),
752 g_value_get_uint (g_value_array_get_nth (values, 3)),
758 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
759 const GPtrArray *capabilities,
765 DEBUG ("Error: %s", error->message);
769 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
770 TpConnection *connection,
771 EmpathyDispatcher *dispatcher)
773 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
774 GPtrArray *capabilities;
777 const gchar *remove = NULL;
779 if (g_hash_table_lookup (priv->connections, connection) != NULL)
782 g_hash_table_insert (priv->connections, g_object_ref (connection),
783 new_connection_data ());
785 g_signal_connect (connection, "invalidated",
786 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
788 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
789 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
791 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
792 dispatcher_connection_new_channels_cb,
793 NULL, NULL, G_OBJECT (dispatcher), NULL);
795 tp_cli_dbus_properties_call_get_all (connection, -1,
796 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
797 dispatcher_connection_got_all,
798 NULL, NULL, G_OBJECT (dispatcher));
802 tp_cli_connection_connect_to_new_channel (connection,
803 dispatcher_connection_new_channel_cb,
804 NULL, NULL, G_OBJECT (dispatcher), NULL);
806 tp_cli_connection_call_list_channels (connection, -1,
807 dispatcher_connection_list_channels_cb, NULL, NULL,
808 G_OBJECT (dispatcher));
812 /* Advertise VoIP capabilities */
813 capabilities = g_ptr_array_sized_new (1);
814 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
815 G_TYPE_UINT, G_TYPE_INVALID);
816 g_value_init (&cap, cap_type);
817 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
818 dbus_g_type_struct_set (&cap,
819 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
820 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
821 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
822 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
823 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
824 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
826 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
827 connection, -1, capabilities, &remove,
828 dispatcher_connection_advertise_capabilities_cb,
829 NULL, NULL, G_OBJECT (dispatcher));
831 g_value_unset (&cap);
832 g_ptr_array_free (capabilities, TRUE);
836 dispatcher_constructor (GType type,
837 guint n_construct_params,
838 GObjectConstructParam *construct_params)
842 if (dispatcher == NULL)
844 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
845 (type, n_construct_params, construct_params);
847 dispatcher = EMPATHY_DISPATCHER (retval);
848 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
852 retval = g_object_ref (dispatcher);
859 dispatcher_finalize (GObject *object)
861 EmpathyDispatcherPriv *priv = GET_PRIV (object);
866 g_signal_handlers_disconnect_by_func (priv->account_manager,
867 dispatcher_new_connection_cb, object);
869 for (l = priv->channels; l; l = l->next)
871 g_signal_handlers_disconnect_by_func (l->data,
872 dispatcher_channel_invalidated_cb, object);
875 g_list_free (priv->channels);
877 g_hash_table_iter_init (&iter, priv->connections);
878 while (g_hash_table_iter_next (&iter, &connection, NULL))
880 g_signal_handlers_disconnect_by_func (connection,
881 dispatcher_connection_invalidated_cb, object);
884 g_object_unref (priv->account_manager);
885 g_object_unref (priv->mc);
887 g_hash_table_destroy (priv->connections);
891 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
893 GObjectClass *object_class = G_OBJECT_CLASS (klass);
895 object_class->finalize = dispatcher_finalize;
896 object_class->constructor = dispatcher_constructor;
899 g_signal_new ("observe",
900 G_TYPE_FROM_CLASS (klass),
904 g_cclosure_marshal_VOID__OBJECT,
906 1, EMPATHY_TYPE_DISPATCH_OPERATION);
909 g_signal_new ("approve",
910 G_TYPE_FROM_CLASS (klass),
914 g_cclosure_marshal_VOID__OBJECT,
916 1, EMPATHY_TYPE_DISPATCH_OPERATION);
919 g_signal_new ("dispatch",
920 G_TYPE_FROM_CLASS (klass),
924 g_cclosure_marshal_VOID__OBJECT,
926 1, EMPATHY_TYPE_DISPATCH_OPERATION);
928 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
933 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
935 GList *connections, *l;
936 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
937 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
939 dispatcher->priv = priv;
940 priv->mc = empathy_mission_control_dup_singleton ();
941 priv->account_manager = empathy_account_manager_dup_singleton ();
943 g_signal_connect (priv->account_manager, "new-connection",
944 G_CALLBACK (dispatcher_new_connection_cb),
947 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
948 g_object_unref, (GDestroyNotify) free_connection_data);
950 priv->channels = NULL;
952 connections = empathy_account_manager_dup_connections (priv->account_manager);
953 for (l = connections; l; l = l->next)
955 dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
956 g_object_unref (l->data);
958 g_list_free (connections);
962 empathy_dispatcher_dup_singleton (void)
964 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
968 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
969 DispatcherRequestData *request_data,
972 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
973 ConnectionData *conn_data;
975 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
976 if (request_data->cb != NULL)
977 request_data->cb (NULL, error, request_data->user_data);
979 conn_data->outstanding_requests =
980 g_list_remove (conn_data->outstanding_requests, request_data);
981 free_dispatcher_request_data (request_data);
985 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
986 DispatcherRequestData *request_data,
987 const gchar *object_path,
988 GHashTable *properties,
991 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
992 EmpathyDispatchOperation *operation = NULL;
993 ConnectionData *conn_data;
995 conn_data = g_hash_table_lookup (priv->connections,
996 request_data->connection);
1000 DEBUG ("Channel request failed: %s", error->message);
1002 dispatcher_request_failed (dispatcher, request_data, error);
1007 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1010 if (operation != NULL)
1011 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1013 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1016 if (operation == NULL)
1018 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1023 operation = empathy_dispatch_operation_new_with_wrapper (
1024 request_data->connection,
1025 data->channel, request_data->contact, FALSE,
1026 data->channel_wrapper);
1032 if (properties != NULL)
1033 channel = tp_channel_new_from_properties (request_data->connection,
1034 object_path, properties, NULL);
1036 channel = tp_channel_new (request_data->connection, object_path,
1037 request_data->channel_type, request_data->handle_type,
1038 request_data->handle, NULL);
1040 g_signal_connect (channel, "invalidated",
1041 G_CALLBACK (dispatcher_channel_invalidated_cb),
1042 request_data->dispatcher);
1044 priv->channels = g_list_prepend (priv->channels, channel);
1046 operation = empathy_dispatch_operation_new (request_data->connection,
1047 channel, request_data->contact, FALSE);
1048 g_object_unref (channel);
1053 /* Already existed set potential extra information */
1054 g_object_set (G_OBJECT (operation),
1055 "contact", request_data->contact,
1059 request_data->operation = operation;
1061 /* (pre)-approve this right away as we requested it
1062 * This might cause the channel to be claimed, in which case the operation
1063 * will disappear. So ref it, and check the status before starting the
1066 g_object_ref (operation);
1067 empathy_dispatch_operation_approve (operation);
1069 if (empathy_dispatch_operation_get_status (operation) <
1070 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1071 dispatcher_start_dispatching (request_data->dispatcher, operation,
1074 g_object_unref (operation);
1077 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1082 dispatcher_request_channel_cb (TpConnection *connection,
1083 const gchar *object_path,
1084 const GError *error,
1086 GObject *weak_object)
1088 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1089 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1091 dispatcher_connection_new_requested_channel (dispatcher,
1092 request_data, object_path, NULL, error);
1096 dispatcher_request_channel (DispatcherRequestData *request_data)
1098 tp_cli_connection_call_request_channel (request_data->connection, -1,
1099 request_data->channel_type,
1100 request_data->handle_type,
1101 request_data->handle,
1102 TRUE, dispatcher_request_channel_cb,
1103 request_data, NULL, G_OBJECT (request_data->dispatcher));
1107 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1108 EmpathyDispatcherRequestCb *callback,
1111 EmpathyDispatcher *dispatcher;
1112 EmpathyDispatcherPriv *priv;
1113 TpConnection *connection;
1114 ConnectionData *connection_data;
1115 DispatcherRequestData *request_data;
1117 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1119 dispatcher = empathy_dispatcher_dup_singleton ();
1120 priv = GET_PRIV (dispatcher);
1122 connection = empathy_contact_get_connection (contact);
1123 connection_data = g_hash_table_lookup (priv->connections, connection);
1125 /* The contact handle might not be known yet */
1126 request_data = new_dispatcher_request_data (dispatcher, connection,
1127 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1128 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1130 connection_data->outstanding_requests = g_list_prepend
1131 (connection_data->outstanding_requests, request_data);
1133 dispatcher_request_channel (request_data);
1135 g_object_unref (dispatcher);
1140 EmpathyDispatcher *dispatcher;
1141 EmpathyDispatcherRequestCb *callback;
1143 } ChatWithContactIdData;
1146 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1147 EmpathyContact *contact,
1148 const GError *error,
1150 GObject *weak_object)
1152 ChatWithContactIdData *data = user_data;
1156 /* FIXME: Should call data->callback with the error */
1157 DEBUG ("Error: %s", error->message);
1161 empathy_dispatcher_chat_with_contact (contact, data->callback, data->user_data);
1164 g_object_unref (data->dispatcher);
1165 g_slice_free (ChatWithContactIdData, data);
1169 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1170 const gchar *contact_id,
1171 EmpathyDispatcherRequestCb *callback,
1174 EmpathyDispatcher *dispatcher;
1175 EmpathyTpContactFactory *factory;
1176 ChatWithContactIdData *data;
1178 g_return_if_fail (TP_IS_CONNECTION (connection));
1179 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1181 dispatcher = empathy_dispatcher_dup_singleton ();
1182 factory = empathy_tp_contact_factory_dup_singleton (connection);
1183 data = g_slice_new0 (ChatWithContactIdData);
1184 data->dispatcher = dispatcher;
1185 data->callback = callback;
1186 data->user_data = user_data;
1187 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1188 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1190 g_object_unref (factory);
1194 dispatcher_request_handles_cb (TpConnection *connection,
1195 const GArray *handles,
1196 const GError *error,
1200 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1204 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1205 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1208 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1210 if (request_data->cb)
1211 request_data->cb (NULL, error, request_data->user_data);
1213 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1216 free_dispatcher_request_data (request_data);
1218 dispatcher_flush_outstanding_operations (dispatcher, cd);
1222 request_data->handle = g_array_index (handles, guint, 0);
1223 dispatcher_request_channel (request_data);
1227 empathy_dispatcher_join_muc (TpConnection *connection,
1228 const gchar *roomname,
1229 EmpathyDispatcherRequestCb *callback,
1232 EmpathyDispatcher *dispatcher;
1233 EmpathyDispatcherPriv *priv;
1234 DispatcherRequestData *request_data;
1235 ConnectionData *connection_data;
1236 const gchar *names[] = { roomname, NULL };
1238 g_return_if_fail (TP_IS_CONNECTION (connection));
1239 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1241 dispatcher = empathy_dispatcher_dup_singleton ();
1242 priv = GET_PRIV (dispatcher);
1244 connection_data = g_hash_table_lookup (priv->connections, connection);
1246 /* Don't know the room handle yet */
1247 request_data = new_dispatcher_request_data (dispatcher, connection,
1248 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1249 NULL, callback, user_data);
1251 connection_data->outstanding_requests = g_list_prepend
1252 (connection_data->outstanding_requests, request_data);
1254 tp_cli_connection_call_request_handles (connection, -1,
1255 TP_HANDLE_TYPE_ROOM, names,
1256 dispatcher_request_handles_cb, request_data, NULL,
1257 G_OBJECT (dispatcher));
1259 g_object_unref (dispatcher);
1263 dispatcher_create_channel_cb (TpConnection *connect,
1264 const gchar *object_path,
1265 GHashTable *properties,
1266 const GError *error,
1268 GObject *weak_object)
1270 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1271 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1273 dispatcher_connection_new_requested_channel (dispatcher,
1274 request_data, object_path, properties, error);
1278 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1279 TpConnection *connection,
1280 GHashTable *request,
1281 EmpathyDispatcherRequestCb *callback,
1284 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1285 ConnectionData *connection_data;
1286 DispatcherRequestData *request_data;
1287 const gchar *channel_type;
1292 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1293 g_return_if_fail (TP_IS_CONNECTION (connection));
1294 g_return_if_fail (request != NULL);
1296 connection_data = g_hash_table_lookup (priv->connections, connection);
1297 g_assert (connection_data != NULL);
1299 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1301 handle_type = tp_asv_get_uint32 (request,
1302 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1304 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1306 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1308 request_data = new_dispatcher_request_data (dispatcher, connection,
1309 channel_type, handle_type, handle, request,
1310 NULL, callback, user_data);
1312 connection_data->outstanding_requests = g_list_prepend
1313 (connection_data->outstanding_requests, request_data);
1315 tp_cli_connection_interface_requests_call_create_channel (
1316 request_data->connection, -1,
1317 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1318 G_OBJECT (request_data->dispatcher));
1322 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1323 const gchar *filename,
1326 const gchar *content_type,
1327 EmpathyDispatcherRequestCb *callback,
1330 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1331 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1332 TpConnection *connection = empathy_contact_get_connection (contact);
1333 ConnectionData *connection_data =
1334 g_hash_table_lookup (priv->connections, connection);
1335 DispatcherRequestData *request_data;
1337 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1338 (GDestroyNotify) tp_g_value_slice_free);
1340 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1341 g_return_if_fail (!EMP_STR_EMPTY (filename));
1342 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1344 /* org.freedesktop.Telepathy.Channel.ChannelType */
1345 value = tp_g_value_slice_new (G_TYPE_STRING);
1346 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1347 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1349 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1350 value = tp_g_value_slice_new (G_TYPE_UINT);
1351 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1352 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1354 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1355 value = tp_g_value_slice_new (G_TYPE_UINT);
1356 g_value_set_uint (value, empathy_contact_get_handle (contact));
1357 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
1359 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1360 value = tp_g_value_slice_new (G_TYPE_STRING);
1361 g_value_set_string (value, content_type);
1362 g_hash_table_insert (request,
1363 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1365 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1366 value = tp_g_value_slice_new (G_TYPE_STRING);
1367 g_value_set_string (value, filename);
1368 g_hash_table_insert (request,
1369 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1371 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1372 value = tp_g_value_slice_new (G_TYPE_UINT64);
1373 g_value_set_uint64 (value, size);
1374 g_hash_table_insert (request,
1375 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1377 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1378 value = tp_g_value_slice_new (G_TYPE_UINT64);
1379 g_value_set_uint64 (value, date);
1380 g_hash_table_insert (request,
1381 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1383 request_data = new_dispatcher_request_data (dispatcher, connection,
1384 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
1385 empathy_contact_get_handle (contact), request, contact, callback,
1387 connection_data->outstanding_requests = g_list_prepend
1388 (connection_data->outstanding_requests, request_data);
1390 tp_cli_connection_interface_requests_call_create_channel (
1391 request_data->connection, -1,
1392 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1393 G_OBJECT (request_data->dispatcher));
1395 g_object_unref (dispatcher);
1399 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1400 TpConnection *connection,
1401 const gchar *channel_type,
1404 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1409 g_return_val_if_fail (channel_type != NULL, NULL);
1410 g_return_val_if_fail (handle_type != 0, NULL);
1412 cd = g_hash_table_lookup (priv->connections, connection);
1417 classes = cd->requestable_channels;
1418 if (classes == NULL)
1421 for (i = 0; i < classes->len; i++)
1427 const gchar *c_type;
1431 class = g_ptr_array_index (classes, i);
1432 fixed = g_value_array_get_nth (class, 0);
1434 fprops = g_value_get_boxed (fixed);
1435 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1437 if (tp_strdiff (channel_type, c_type))
1440 h_type = tp_asv_get_uint32 (fprops,
1441 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1443 if (!valid || handle_type != h_type)
1446 allowed = g_value_array_get_nth (class, 1);
1448 return g_value_get_boxed (allowed);