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);
233 DEBUG ("Error: %s", message);
234 cd = g_hash_table_lookup (priv->connections, connection);
236 g_hash_table_remove (priv->connections, connection);
240 dispatcher_operation_can_start (EmpathyDispatcher *self,
241 EmpathyDispatchOperation *operation,
245 const gchar *channel_type =
246 empathy_dispatch_operation_get_channel_type (operation);
248 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
250 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
252 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
262 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
263 EmpathyDispatchOperation *operation,
269 l = cd->outstanding_requests;
272 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
277 if (d->operation == operation)
282 d->cb (NULL, error, d->user_data);
284 d->cb (operation, NULL, d->user_data);
287 cd->outstanding_requests = g_list_delete_link
288 (cd->outstanding_requests, lt);
290 free_dispatcher_request_data (d);
296 dispatcher_channel_invalidated_cb (TpProxy *proxy,
300 EmpathyDispatcher *dispatcher)
302 /* Channel went away... */
303 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
304 TpConnection *connection;
305 EmpathyDispatchOperation *operation;
307 const gchar *object_path;
309 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
311 cd = g_hash_table_lookup (priv->connections, connection);
312 /* Connection itself invalidated? */
316 object_path = tp_proxy_get_object_path (proxy);
318 DEBUG ("Channel %s invalidated", object_path);
320 g_hash_table_remove (cd->dispatched_channels, object_path);
321 g_hash_table_remove (cd->dispatching_channels, object_path);
323 priv->channels = g_list_remove (priv->channels, proxy);
325 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
326 if (operation != NULL)
328 GError error = { domain, code, message };
329 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
330 g_hash_table_remove (cd->outstanding_channels, object_path);
331 g_object_unref (operation);
336 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
337 EmpathyDispatcher *dispatcher)
339 g_assert (empathy_dispatch_operation_is_incoming (operation));
340 DEBUG ("Send of for dispatching: %s",
341 empathy_dispatch_operation_get_object_path (operation));
342 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
346 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
347 EmpathyDispatcher *dispatcher)
349 /* Our job is done, remove the dispatch operation and mark the channel as
351 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
352 TpConnection *connection;
354 const gchar *object_path;
356 connection = empathy_dispatch_operation_get_tp_connection (operation);
357 cd = g_hash_table_lookup (priv->connections, connection);
358 g_assert (cd != NULL);
359 g_object_unref (G_OBJECT (connection));
361 object_path = empathy_dispatch_operation_get_object_path (operation);
363 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
366 d = new_dispatch_data (
367 empathy_dispatch_operation_get_channel (operation),
368 empathy_dispatch_operation_get_channel_wrapper (operation));
369 g_hash_table_insert (cd->dispatched_channels,
370 g_strdup (object_path), d);
372 g_hash_table_remove (cd->dispatching_channels, object_path);
374 DEBUG ("Channel claimed: %s", object_path);
378 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
379 EmpathyDispatcher *dispatcher)
381 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
382 TpConnection *connection;
384 EmpathyDispatchOperationState status;
386 g_signal_connect (operation, "approved",
387 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
389 g_signal_connect (operation, "claimed",
390 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
392 /* Signal the observers */
393 DEBUG ("Send to observers: %s",
394 empathy_dispatch_operation_get_object_path (operation));
395 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
397 empathy_dispatch_operation_start (operation);
399 /* Signal potential requestors */
400 connection = empathy_dispatch_operation_get_tp_connection (operation);
401 cd = g_hash_table_lookup (priv->connections, connection);
402 g_assert (cd != NULL);
403 g_object_unref (G_OBJECT (connection));
405 g_object_ref (operation);
406 g_object_ref (dispatcher);
408 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
409 status = empathy_dispatch_operation_get_status (operation);
410 g_object_unref (operation);
412 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
415 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
417 DEBUG ("Send to approvers: %s",
418 empathy_dispatch_operation_get_object_path (operation));
419 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
423 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
424 DEBUG ("Send of for dispatching: %s",
425 empathy_dispatch_operation_get_object_path (operation));
426 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
429 g_object_unref (dispatcher);
433 dispatcher_start_dispatching (EmpathyDispatcher *self,
434 EmpathyDispatchOperation *operation,
437 const gchar *object_path =
438 empathy_dispatch_operation_get_object_path (operation);
440 DEBUG ("Dispatching process started for %s", object_path);
442 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
444 g_assert (g_hash_table_lookup (cd->outstanding_channels,
445 object_path) == NULL);
447 g_hash_table_insert (cd->dispatching_channels,
448 g_strdup (object_path), operation);
450 switch (empathy_dispatch_operation_get_status (operation))
452 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
453 g_signal_connect (operation, "ready",
454 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
456 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
457 dispatch_operation_ready_cb (operation, dispatcher);
460 g_assert_not_reached();
464 else if (empathy_dispatch_operation_get_status (operation) >=
465 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
467 /* Already dispatching and the operation is pending, thus the observers
468 * have seen it (if applicable), so we can flush the request right away.
470 dispatch_operation_flush_requests (self, operation, NULL, cd);
475 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
481 g_hash_table_iter_init (&iter, cd->outstanding_channels);
482 while (g_hash_table_iter_next (&iter, NULL, &value))
484 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
486 if (dispatcher_operation_can_start (self, operation, cd))
488 dispatcher_start_dispatching (dispatcher, operation, cd);
489 g_hash_table_iter_remove (&iter);
495 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
496 TpConnection *connection,
497 const gchar *object_path,
498 const gchar *channel_type,
501 GHashTable *properties,
504 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
507 EmpathyDispatchOperation *operation;
509 /* Channel types we never want to dispatch because they're either deprecated
510 * or can't sensibly be dispatch (e.g. channels that should always be
512 const char *blacklist[] = {
513 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
514 TP_IFACE_CHANNEL_TYPE_TUBES,
515 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
519 cd = g_hash_table_lookup (priv->connections, connection);
521 /* Don't bother with channels we have already dispatched or are dispatching
522 * currently. This can happen when NewChannel(s) is fired after
523 * RequestChannel/CreateChannel/EnsureChannel */
524 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
527 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
530 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
532 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
535 /* Only pick up non-requested text and file channels. For all other it
536 * doesn't make sense to handle it if we didn't request it. The same goes
537 * for channels we discovered by the Channels property or ListChannels */
538 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
539 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
541 DEBUG ("Ignoring incoming channel of type %s on %s",
542 channel_type, object_path);
546 for (i = 0 ; blacklist[i] != NULL; i++)
548 if (!tp_strdiff (channel_type, blacklist[i]))
550 DEBUG ("Ignoring blacklisted channel type %s on %s",
551 channel_type, object_path);
556 DEBUG ("New channel of type %s on %s", channel_type, object_path);
558 if (properties == NULL)
559 channel = tp_channel_new (connection, object_path, channel_type,
560 handle_type, handle, NULL);
562 channel = tp_channel_new_from_properties (connection, object_path,
565 g_signal_connect (channel, "invalidated",
566 G_CALLBACK (dispatcher_channel_invalidated_cb),
569 priv->channels = g_list_prepend (priv->channels, channel);
571 operation = empathy_dispatch_operation_new (connection, channel, NULL,
574 g_object_unref (channel);
578 /* Request could either be by us or by a remote party. If there are no
579 * outstanding requests for this channel type we can assume it's remote.
580 * Otherwise we wait untill they are all satisfied */
581 if (dispatcher_operation_can_start (dispatcher, operation, cd))
582 dispatcher_start_dispatching (dispatcher, operation, cd);
584 g_hash_table_insert (cd->outstanding_channels,
585 g_strdup (object_path), operation);
589 dispatcher_start_dispatching (dispatcher, operation, cd);
594 dispatcher_connection_new_channel_cb (TpConnection *connection,
595 const gchar *object_path,
596 const gchar *channel_type,
599 gboolean suppress_handler,
603 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
605 /* Empathy heavily abuses surpress handler (don't try this at home), if
606 * surpress handler is true then it is an outgoing channel, which is
607 * requested either by us or some other party (like the megaphone applet).
608 * Otherwise it's an incoming channel */
609 dispatcher_connection_new_channel (dispatcher, connection,
610 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
614 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
615 TpConnection *connection,
616 const gchar *object_path,
617 GHashTable *properties)
619 const gchar *channel_type;
626 channel_type = tp_asv_get_string (properties,
627 TP_IFACE_CHANNEL ".ChannelType");
628 if (channel_type == NULL)
630 g_message ("%s had an invalid ChannelType property", object_path);
634 handle_type = tp_asv_get_uint32 (properties,
635 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
638 g_message ("%s had an invalid TargetHandleType property", object_path);
642 handle = tp_asv_get_uint32 (properties,
643 TP_IFACE_CHANNEL ".TargetHandle", &valid);
646 g_message ("%s had an invalid TargetHandle property", object_path);
650 /* We assume there is no channel dispather, so we're the only one dispatching
651 * it. Which means that a requested channel it is outgoing one */
652 requested = tp_asv_get_boolean (properties,
653 TP_IFACE_CHANNEL ".Requested", &valid);
656 g_message ("%s had an invalid Requested property", object_path);
660 dispatcher_connection_new_channel (dispatcher, connection,
661 object_path, channel_type, handle_type, handle, properties, !requested);
665 dispatcher_connection_new_channels_cb (TpConnection *connection,
666 const GPtrArray *channels,
670 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
673 for (i = 0; i < channels->len ; i++)
675 GValueArray *arr = g_ptr_array_index (channels, i);
676 const gchar *object_path;
677 GHashTable *properties;
679 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
680 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
682 dispatcher_connection_new_channel_with_properties (dispatcher,
683 connection, object_path, properties);
688 dispatcher_connection_got_all (TpProxy *proxy,
689 GHashTable *properties,
694 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
695 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
697 GPtrArray *requestable_channels;
700 DEBUG ("Error: %s", error->message);
704 channels = tp_asv_get_boxed (properties, "Channels",
705 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
707 if (channels == NULL)
708 DEBUG ("No Channels property !?! on connection");
710 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
711 channels, NULL, object);
713 requestable_channels = tp_asv_get_boxed (properties,
714 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
716 if (requestable_channels == NULL)
717 DEBUG ("No RequestableChannelClasses property !?! on connection");
722 cd = g_hash_table_lookup (priv->connections, proxy);
723 g_assert (cd != NULL);
725 cd->requestable_channels = g_boxed_copy (
726 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
731 dispatcher_connection_list_channels_cb (TpConnection *connection,
732 const GPtrArray *channels,
741 DEBUG ("Error: %s", error->message);
745 for (i = 0; i < channels->len; i++)
749 values = g_ptr_array_index (channels, i);
750 /* We don't have any extra info, so assume already existing channels are
752 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
754 g_value_get_boxed (g_value_array_get_nth (values, 0)),
755 g_value_get_string (g_value_array_get_nth (values, 1)),
756 g_value_get_uint (g_value_array_get_nth (values, 2)),
757 g_value_get_uint (g_value_array_get_nth (values, 3)),
763 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
764 const GPtrArray *capabilities,
770 DEBUG ("Error: %s", error->message);
774 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
775 TpConnection *connection,
776 EmpathyDispatcher *dispatcher)
778 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
779 GPtrArray *capabilities;
782 const gchar *remove = NULL;
784 if (g_hash_table_lookup (priv->connections, connection) != NULL)
787 g_hash_table_insert (priv->connections, g_object_ref (connection),
788 new_connection_data ());
790 g_signal_connect (connection, "invalidated",
791 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
793 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
794 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
796 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
797 dispatcher_connection_new_channels_cb,
798 NULL, NULL, G_OBJECT (dispatcher), NULL);
800 tp_cli_dbus_properties_call_get_all (connection, -1,
801 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
802 dispatcher_connection_got_all,
803 NULL, NULL, G_OBJECT (dispatcher));
807 tp_cli_connection_connect_to_new_channel (connection,
808 dispatcher_connection_new_channel_cb,
809 NULL, NULL, G_OBJECT (dispatcher), NULL);
811 tp_cli_connection_call_list_channels (connection, -1,
812 dispatcher_connection_list_channels_cb, NULL, NULL,
813 G_OBJECT (dispatcher));
817 /* Advertise VoIP capabilities */
818 capabilities = g_ptr_array_sized_new (1);
819 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
820 G_TYPE_UINT, G_TYPE_INVALID);
821 g_value_init (&cap, cap_type);
822 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
823 dbus_g_type_struct_set (&cap,
824 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
825 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
826 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
827 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
828 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
829 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
831 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
832 connection, -1, capabilities, &remove,
833 dispatcher_connection_advertise_capabilities_cb,
834 NULL, NULL, G_OBJECT (dispatcher));
836 g_value_unset (&cap);
837 g_ptr_array_free (capabilities, TRUE);
841 dispatcher_constructor (GType type,
842 guint n_construct_params,
843 GObjectConstructParam *construct_params)
847 if (dispatcher == NULL)
849 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
850 (type, n_construct_params, construct_params);
852 dispatcher = EMPATHY_DISPATCHER (retval);
853 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
857 retval = g_object_ref (dispatcher);
864 dispatcher_finalize (GObject *object)
866 EmpathyDispatcherPriv *priv = GET_PRIV (object);
871 g_signal_handlers_disconnect_by_func (priv->account_manager,
872 dispatcher_new_connection_cb, object);
874 for (l = priv->channels; l; l = l->next)
876 g_signal_handlers_disconnect_by_func (l->data,
877 dispatcher_channel_invalidated_cb, object);
880 g_list_free (priv->channels);
882 g_hash_table_iter_init (&iter, priv->connections);
883 while (g_hash_table_iter_next (&iter, &connection, NULL))
885 g_signal_handlers_disconnect_by_func (connection,
886 dispatcher_connection_invalidated_cb, object);
889 g_object_unref (priv->account_manager);
890 g_object_unref (priv->mc);
892 g_hash_table_destroy (priv->connections);
896 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
898 GObjectClass *object_class = G_OBJECT_CLASS (klass);
900 object_class->finalize = dispatcher_finalize;
901 object_class->constructor = dispatcher_constructor;
904 g_signal_new ("observe",
905 G_TYPE_FROM_CLASS (klass),
909 g_cclosure_marshal_VOID__OBJECT,
911 1, EMPATHY_TYPE_DISPATCH_OPERATION);
914 g_signal_new ("approve",
915 G_TYPE_FROM_CLASS (klass),
919 g_cclosure_marshal_VOID__OBJECT,
921 1, EMPATHY_TYPE_DISPATCH_OPERATION);
924 g_signal_new ("dispatch",
925 G_TYPE_FROM_CLASS (klass),
929 g_cclosure_marshal_VOID__OBJECT,
931 1, EMPATHY_TYPE_DISPATCH_OPERATION);
933 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
938 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
940 GList *connections, *l;
941 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
942 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
944 dispatcher->priv = priv;
945 priv->mc = empathy_mission_control_dup_singleton ();
946 priv->account_manager = empathy_account_manager_dup_singleton ();
948 g_signal_connect (priv->account_manager, "new-connection",
949 G_CALLBACK (dispatcher_new_connection_cb),
952 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
953 g_object_unref, (GDestroyNotify) free_connection_data);
955 priv->channels = NULL;
957 connections = empathy_account_manager_dup_connections (priv->account_manager);
958 for (l = connections; l; l = l->next)
960 dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
961 g_object_unref (l->data);
963 g_list_free (connections);
967 empathy_dispatcher_dup_singleton (void)
969 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
973 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
974 DispatcherRequestData *request_data,
977 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
978 ConnectionData *conn_data;
980 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
981 if (request_data->cb != NULL)
982 request_data->cb (NULL, error, request_data->user_data);
984 conn_data->outstanding_requests =
985 g_list_remove (conn_data->outstanding_requests, request_data);
986 free_dispatcher_request_data (request_data);
990 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
991 DispatcherRequestData *request_data,
992 const gchar *object_path,
993 GHashTable *properties,
996 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
997 EmpathyDispatchOperation *operation = NULL;
998 ConnectionData *conn_data;
1000 conn_data = g_hash_table_lookup (priv->connections,
1001 request_data->connection);
1005 DEBUG ("Channel request failed: %s", error->message);
1007 dispatcher_request_failed (dispatcher, request_data, error);
1012 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1015 if (operation != NULL)
1016 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1018 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1021 if (operation == NULL)
1023 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1028 operation = empathy_dispatch_operation_new_with_wrapper (
1029 request_data->connection,
1030 data->channel, request_data->contact, FALSE,
1031 data->channel_wrapper);
1037 if (properties != NULL)
1038 channel = tp_channel_new_from_properties (request_data->connection,
1039 object_path, properties, NULL);
1041 channel = tp_channel_new (request_data->connection, object_path,
1042 request_data->channel_type, request_data->handle_type,
1043 request_data->handle, NULL);
1045 g_signal_connect (channel, "invalidated",
1046 G_CALLBACK (dispatcher_channel_invalidated_cb),
1047 request_data->dispatcher);
1049 priv->channels = g_list_prepend (priv->channels, channel);
1051 operation = empathy_dispatch_operation_new (request_data->connection,
1052 channel, request_data->contact, FALSE);
1053 g_object_unref (channel);
1058 /* Already existed set potential extra information */
1059 g_object_set (G_OBJECT (operation),
1060 "contact", request_data->contact,
1064 request_data->operation = operation;
1066 /* (pre)-approve this right away as we requested it
1067 * This might cause the channel to be claimed, in which case the operation
1068 * will disappear. So ref it, and check the status before starting the
1071 g_object_ref (operation);
1072 empathy_dispatch_operation_approve (operation);
1074 if (empathy_dispatch_operation_get_status (operation) <
1075 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1076 dispatcher_start_dispatching (request_data->dispatcher, operation,
1079 g_object_unref (operation);
1082 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1087 dispatcher_request_channel_cb (TpConnection *connection,
1088 const gchar *object_path,
1089 const GError *error,
1091 GObject *weak_object)
1093 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1094 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1096 dispatcher_connection_new_requested_channel (dispatcher,
1097 request_data, object_path, NULL, error);
1101 dispatcher_request_channel (DispatcherRequestData *request_data)
1103 tp_cli_connection_call_request_channel (request_data->connection, -1,
1104 request_data->channel_type,
1105 request_data->handle_type,
1106 request_data->handle,
1107 TRUE, dispatcher_request_channel_cb,
1108 request_data, NULL, G_OBJECT (request_data->dispatcher));
1112 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1113 EmpathyDispatcherRequestCb *callback,
1116 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1117 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1118 TpConnection *connection;
1120 DispatcherRequestData *request_data;
1122 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1124 connection = empathy_contact_get_connection (contact);
1125 cd = g_hash_table_lookup (priv->connections, connection);
1126 request_data = new_dispatcher_request_data (dispatcher, connection,
1127 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1128 contact, callback, user_data);
1130 cd->outstanding_requests = g_list_prepend
1131 (cd->outstanding_requests, request_data);
1133 dispatcher_request_channel (request_data);
1135 g_object_unref (dispatcher);
1139 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1140 EmpathyDispatcherRequestCb *callback,
1143 EmpathyDispatcher *dispatcher;
1144 EmpathyDispatcherPriv *priv;
1145 TpConnection *connection;
1146 ConnectionData *connection_data;
1147 DispatcherRequestData *request_data;
1149 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1151 dispatcher = empathy_dispatcher_dup_singleton();
1152 priv = GET_PRIV (dispatcher);
1154 connection = empathy_contact_get_connection (contact);
1155 connection_data = g_hash_table_lookup (priv->connections, connection);
1157 /* The contact handle might not be known yet */
1158 request_data = new_dispatcher_request_data (dispatcher, connection,
1159 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1160 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1162 connection_data->outstanding_requests = g_list_prepend
1163 (connection_data->outstanding_requests, request_data);
1165 dispatcher_request_channel (request_data);
1167 g_object_unref (dispatcher);
1172 EmpathyDispatcher *dispatcher;
1173 EmpathyDispatcherRequestCb *callback;
1175 } ChatWithContactIdData;
1178 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1181 GObject *weak_object)
1183 ChatWithContactIdData *data = user_data;
1185 empathy_dispatcher_chat_with_contact (contacts->data, data->callback, data->user_data);
1186 g_object_unref (data->dispatcher);
1187 g_slice_free (ChatWithContactIdData, data);
1191 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1192 const gchar *contact_id,
1193 EmpathyDispatcherRequestCb *callback,
1196 EmpathyDispatcher *dispatcher;
1197 EmpathyTpContactFactory *factory;
1198 ChatWithContactIdData *data;
1200 g_return_if_fail (TP_IS_CONNECTION (connection));
1201 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1203 dispatcher = empathy_dispatcher_dup_singleton ();
1204 factory = empathy_tp_contact_factory_dup_singleton (connection);
1205 data = g_slice_new0 (ChatWithContactIdData);
1206 data->dispatcher = dispatcher;
1207 data->callback = callback;
1208 data->user_data = user_data;
1209 empathy_tp_contact_factory_get_from_ids (factory, 1, &contact_id,
1210 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1212 g_object_unref (factory);
1216 dispatcher_request_handles_cb (TpConnection *connection,
1217 const GArray *handles,
1218 const GError *error,
1222 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1226 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1227 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1230 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1232 if (request_data->cb)
1233 request_data->cb (NULL, error, request_data->user_data);
1235 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1238 free_dispatcher_request_data (request_data);
1240 dispatcher_flush_outstanding_operations (dispatcher, cd);
1244 request_data->handle = g_array_index (handles, guint, 0);
1245 dispatcher_request_channel (request_data);
1249 empathy_dispatcher_join_muc (TpConnection *connection,
1250 const gchar *roomname,
1251 EmpathyDispatcherRequestCb *callback,
1254 EmpathyDispatcher *dispatcher;
1255 EmpathyDispatcherPriv *priv;
1256 DispatcherRequestData *request_data;
1257 ConnectionData *connection_data;
1258 const gchar *names[] = { roomname, NULL };
1260 g_return_if_fail (TP_IS_CONNECTION (connection));
1261 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1263 dispatcher = empathy_dispatcher_dup_singleton();
1264 priv = GET_PRIV (dispatcher);
1266 connection_data = g_hash_table_lookup (priv->connections, connection);
1268 /* Don't know the room handle yet */
1269 request_data = new_dispatcher_request_data (dispatcher, connection,
1270 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1271 NULL, callback, user_data);
1273 connection_data->outstanding_requests = g_list_prepend
1274 (connection_data->outstanding_requests, request_data);
1276 tp_cli_connection_call_request_handles (connection, -1,
1277 TP_HANDLE_TYPE_ROOM, names,
1278 dispatcher_request_handles_cb, request_data, NULL,
1279 G_OBJECT (dispatcher));
1281 g_object_unref (dispatcher);
1285 dispatcher_create_channel_cb (TpConnection *connect,
1286 const gchar *object_path,
1287 GHashTable *properties,
1288 const GError *error,
1290 GObject *weak_object)
1292 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1293 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1295 dispatcher_connection_new_requested_channel (dispatcher,
1296 request_data, object_path, properties, error);
1300 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1301 TpConnection *connection,
1302 GHashTable *request,
1303 EmpathyDispatcherRequestCb *callback,
1306 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1307 ConnectionData *connection_data;
1308 DispatcherRequestData *request_data;
1309 const gchar *channel_type;
1314 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1315 g_return_if_fail (TP_IS_CONNECTION (connection));
1316 g_return_if_fail (request != NULL);
1318 connection_data = g_hash_table_lookup (priv->connections, connection);
1319 g_assert (connection_data != NULL);
1321 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1323 handle_type = tp_asv_get_uint32 (request,
1324 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1326 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1328 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1330 request_data = new_dispatcher_request_data (dispatcher, connection,
1331 channel_type, handle_type, handle, request,
1332 NULL, callback, user_data);
1334 connection_data->outstanding_requests = g_list_prepend
1335 (connection_data->outstanding_requests, request_data);
1337 tp_cli_connection_interface_requests_call_create_channel (
1338 request_data->connection, -1,
1339 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1340 G_OBJECT (request_data->dispatcher));
1344 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1345 const gchar *filename,
1348 const gchar *content_type,
1349 EmpathyDispatcherRequestCb *callback,
1352 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1353 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1354 TpConnection *connection = empathy_contact_get_connection (contact);
1355 ConnectionData *connection_data =
1356 g_hash_table_lookup (priv->connections, connection);
1357 DispatcherRequestData *request_data;
1359 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1360 (GDestroyNotify) tp_g_value_slice_free);
1362 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1363 g_return_if_fail (!EMP_STR_EMPTY (filename));
1364 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1366 /* org.freedesktop.Telepathy.Channel.ChannelType */
1367 value = tp_g_value_slice_new (G_TYPE_STRING);
1368 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1369 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1371 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1372 value = tp_g_value_slice_new (G_TYPE_UINT);
1373 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1374 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1376 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1377 value = tp_g_value_slice_new (G_TYPE_UINT);
1378 g_value_set_uint (value, empathy_contact_get_handle (contact));
1379 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
1381 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1382 value = tp_g_value_slice_new (G_TYPE_STRING);
1383 g_value_set_string (value, content_type);
1384 g_hash_table_insert (request,
1385 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1387 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1388 value = tp_g_value_slice_new (G_TYPE_STRING);
1389 g_value_set_string (value, filename);
1390 g_hash_table_insert (request,
1391 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1393 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1394 value = tp_g_value_slice_new (G_TYPE_UINT64);
1395 g_value_set_uint64 (value, size);
1396 g_hash_table_insert (request,
1397 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1399 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1400 value = tp_g_value_slice_new (G_TYPE_UINT64);
1401 g_value_set_uint64 (value, date);
1402 g_hash_table_insert (request,
1403 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1405 request_data = new_dispatcher_request_data (dispatcher, connection,
1406 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
1407 empathy_contact_get_handle (contact), request, contact, callback,
1409 connection_data->outstanding_requests = g_list_prepend
1410 (connection_data->outstanding_requests, request_data);
1412 tp_cli_connection_interface_requests_call_create_channel (
1413 request_data->connection, -1,
1414 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1415 G_OBJECT (request_data->dispatcher));
1417 g_object_unref (dispatcher);
1421 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1422 TpConnection *connection,
1423 const gchar *channel_type,
1426 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1431 g_return_val_if_fail (channel_type != NULL, NULL);
1432 g_return_val_if_fail (handle_type != 0, NULL);
1434 cd = g_hash_table_lookup (priv->connections, connection);
1439 classes = cd->requestable_channels;
1440 if (classes == NULL)
1443 for (i = 0; i < classes->len; i++)
1449 const gchar *c_type;
1453 class = g_ptr_array_index (classes, i);
1454 fixed = g_value_array_get_nth (class, 0);
1456 fprops = g_value_get_boxed (fixed);
1457 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1459 if (tp_strdiff (channel_type, c_type))
1462 h_type = tp_asv_get_uint32 (fprops,
1463 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1465 if (!valid || handle_type != h_type)
1468 allowed = g_value_array_get_nth (class, 1);
1470 return g_value_get_boxed (allowed);