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-chatroom-manager.h"
43 #include "empathy-utils.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
51 EmpathyAccountManager *account_manager;
53 /* connection to connection data mapping */
54 GHashTable *connections;
55 GHashTable *outstanding_classes_requests;
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;
116 EmpathyDispatcher *dispatcher;
117 TpConnection *connection;
120 EmpathyDispatcherFindChannelClassCb *callback;
122 } FindChannelRequest;
124 static DispatchData *
125 new_dispatch_data (TpChannel *channel,
126 GObject *channel_wrapper)
128 DispatchData *d = g_slice_new0 (DispatchData);
129 d->channel = g_object_ref (channel);
130 if (channel_wrapper != NULL)
131 d->channel_wrapper = g_object_ref (channel_wrapper);
137 free_dispatch_data (DispatchData *data)
139 g_object_unref (data->channel);
140 if (data->channel_wrapper != NULL)
141 g_object_unref (data->channel_wrapper);
143 g_slice_free (DispatchData, data);
146 static DispatcherRequestData *
147 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
148 TpConnection *connection,
149 const gchar *channel_type,
153 EmpathyContact *contact,
154 EmpathyDispatcherRequestCb *cb,
157 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
159 result->dispatcher = g_object_ref (dispatcher);
160 result->connection = connection;
162 result->channel_type = g_strdup (channel_type);
163 result->handle_type = handle_type;
164 result->handle = handle;
165 result->request = request;
168 result->contact = g_object_ref (contact);
171 result->user_data = user_data;
177 free_dispatcher_request_data (DispatcherRequestData *r)
179 g_free (r->channel_type);
181 if (r->dispatcher != NULL)
182 g_object_unref (r->dispatcher);
184 if (r->contact != NULL)
185 g_object_unref (r->contact);
187 if (r->request != NULL)
188 g_hash_table_unref (r->request);
190 g_slice_free (DispatcherRequestData, r);
193 static ConnectionData *
194 new_connection_data (void)
196 ConnectionData *cd = g_slice_new0 (ConnectionData);
198 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
199 g_free, (GDestroyNotify) free_dispatch_data);
201 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
202 g_free, g_object_unref);
204 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
211 free_connection_data (ConnectionData *cd)
215 g_hash_table_destroy (cd->dispatched_channels);
216 g_hash_table_destroy (cd->dispatching_channels);
219 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
221 free_dispatcher_request_data (l->data);
224 if (cd->requestable_channels != NULL)
226 for (i = 0 ; i < cd->requestable_channels->len ; i++)
228 g_ptr_array_index (cd->requestable_channels, i));
229 g_ptr_array_free (cd->requestable_channels, TRUE);
234 free_find_channel_request (FindChannelRequest *r)
236 g_object_unref (r->dispatcher);
237 g_free (r->channel_type);
238 g_slice_free (FindChannelRequest, r);
242 dispatcher_connection_invalidated_cb (TpConnection *connection,
246 EmpathyDispatcher *dispatcher)
248 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
250 DEBUG ("Error: %s", message);
251 g_hash_table_remove (priv->connections, connection);
255 dispatcher_operation_can_start (EmpathyDispatcher *self,
256 EmpathyDispatchOperation *operation,
260 const gchar *channel_type =
261 empathy_dispatch_operation_get_channel_type (operation);
263 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
265 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
267 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
277 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
278 EmpathyDispatchOperation *operation,
284 l = cd->outstanding_requests;
287 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
292 if (d->operation == operation)
297 d->cb (NULL, error, d->user_data);
299 d->cb (operation, NULL, d->user_data);
302 cd->outstanding_requests = g_list_delete_link
303 (cd->outstanding_requests, lt);
305 free_dispatcher_request_data (d);
311 dispatcher_channel_invalidated_cb (TpProxy *proxy,
315 EmpathyDispatcher *dispatcher)
317 /* Channel went away... */
318 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319 TpConnection *connection;
320 EmpathyDispatchOperation *operation;
322 const gchar *object_path;
324 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
326 cd = g_hash_table_lookup (priv->connections, connection);
327 /* Connection itself invalidated? */
331 object_path = tp_proxy_get_object_path (proxy);
333 DEBUG ("Channel %s invalidated", object_path);
335 g_hash_table_remove (cd->dispatched_channels, object_path);
336 g_hash_table_remove (cd->dispatching_channels, object_path);
338 priv->channels = g_list_remove (priv->channels, proxy);
340 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
341 if (operation != NULL)
343 GError error = { domain, code, message };
344 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
345 g_hash_table_remove (cd->outstanding_channels, object_path);
346 g_object_unref (operation);
351 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
352 EmpathyDispatcher *dispatcher)
354 g_assert (empathy_dispatch_operation_is_incoming (operation));
355 DEBUG ("Send of for dispatching: %s",
356 empathy_dispatch_operation_get_object_path (operation));
357 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
361 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
362 EmpathyDispatcher *dispatcher)
364 /* Our job is done, remove the dispatch operation and mark the channel as
366 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
367 TpConnection *connection;
369 const gchar *object_path;
371 connection = empathy_dispatch_operation_get_tp_connection (operation);
372 cd = g_hash_table_lookup (priv->connections, connection);
373 g_assert (cd != NULL);
375 object_path = empathy_dispatch_operation_get_object_path (operation);
377 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
380 d = new_dispatch_data (
381 empathy_dispatch_operation_get_channel (operation),
382 empathy_dispatch_operation_get_channel_wrapper (operation));
383 g_hash_table_insert (cd->dispatched_channels,
384 g_strdup (object_path), d);
386 g_hash_table_remove (cd->dispatching_channels, object_path);
388 DEBUG ("Channel claimed: %s", object_path);
392 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
393 EmpathyDispatcher *dispatcher)
395 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
396 TpConnection *connection;
398 EmpathyDispatchOperationState status;
400 g_signal_connect (operation, "approved",
401 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
403 g_signal_connect (operation, "claimed",
404 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
406 /* Signal the observers */
407 DEBUG ("Send to observers: %s",
408 empathy_dispatch_operation_get_object_path (operation));
409 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
411 empathy_dispatch_operation_start (operation);
413 /* Signal potential requestors */
414 connection = empathy_dispatch_operation_get_tp_connection (operation);
415 cd = g_hash_table_lookup (priv->connections, connection);
416 g_assert (cd != NULL);
418 g_object_ref (operation);
419 g_object_ref (dispatcher);
421 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
422 status = empathy_dispatch_operation_get_status (operation);
423 g_object_unref (operation);
425 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
428 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
430 DEBUG ("Send to approvers: %s",
431 empathy_dispatch_operation_get_object_path (operation));
432 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
436 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
437 DEBUG ("Send of for dispatching: %s",
438 empathy_dispatch_operation_get_object_path (operation));
439 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
442 g_object_unref (dispatcher);
446 dispatcher_start_dispatching (EmpathyDispatcher *self,
447 EmpathyDispatchOperation *operation,
450 const gchar *object_path =
451 empathy_dispatch_operation_get_object_path (operation);
453 DEBUG ("Dispatching process started for %s", object_path);
455 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
457 g_assert (g_hash_table_lookup (cd->outstanding_channels,
458 object_path) == NULL);
460 g_hash_table_insert (cd->dispatching_channels,
461 g_strdup (object_path), operation);
463 switch (empathy_dispatch_operation_get_status (operation))
465 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
466 g_signal_connect (operation, "ready",
467 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
469 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
470 dispatch_operation_ready_cb (operation, dispatcher);
473 g_assert_not_reached ();
477 else if (empathy_dispatch_operation_get_status (operation) >=
478 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
480 /* Already dispatching and the operation is pending, thus the observers
481 * have seen it (if applicable), so we can flush the request right away.
483 dispatch_operation_flush_requests (self, operation, NULL, cd);
488 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
494 g_hash_table_iter_init (&iter, cd->outstanding_channels);
495 while (g_hash_table_iter_next (&iter, NULL, &value))
497 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
499 if (dispatcher_operation_can_start (self, operation, cd))
501 dispatcher_start_dispatching (dispatcher, operation, cd);
502 g_hash_table_iter_remove (&iter);
508 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
509 TpConnection *connection,
510 const gchar *object_path,
511 const gchar *channel_type,
514 GHashTable *properties,
517 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
520 EmpathyDispatchOperation *operation;
522 /* Channel types we never want to dispatch because they're either deprecated
523 * or can't sensibly be dispatch (e.g. channels that should always be
525 const char *blacklist[] = {
526 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
527 TP_IFACE_CHANNEL_TYPE_TUBES,
528 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
532 cd = g_hash_table_lookup (priv->connections, connection);
534 /* Don't bother with channels we have already dispatched or are dispatching
535 * currently. This can happen when NewChannel(s) is fired after
536 * RequestChannel/CreateChannel/EnsureChannel */
537 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
540 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
543 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
545 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
548 /* Only pick up non-requested text and file channels. For all other it
549 * doesn't make sense to handle it if we didn't request it. The same goes
550 * for channels we discovered by the Channels property or ListChannels */
551 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
552 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
554 DEBUG ("Ignoring incoming channel of type %s on %s",
555 channel_type, object_path);
559 for (i = 0 ; blacklist[i] != NULL; i++)
561 if (!tp_strdiff (channel_type, blacklist[i]))
563 DEBUG ("Ignoring blacklisted channel type %s on %s",
564 channel_type, object_path);
569 DEBUG ("New channel of type %s on %s", channel_type, object_path);
571 if (properties == NULL)
572 channel = tp_channel_new (connection, object_path, channel_type,
573 handle_type, handle, NULL);
575 channel = tp_channel_new_from_properties (connection, object_path,
578 g_signal_connect (channel, "invalidated",
579 G_CALLBACK (dispatcher_channel_invalidated_cb),
582 priv->channels = g_list_prepend (priv->channels, channel);
584 operation = empathy_dispatch_operation_new (connection, channel, NULL,
587 g_object_unref (channel);
591 /* Request could either be by us or by a remote party. If there are no
592 * outstanding requests for this channel type we can assume it's remote.
593 * Otherwise we wait untill they are all satisfied */
594 if (dispatcher_operation_can_start (dispatcher, operation, cd))
595 dispatcher_start_dispatching (dispatcher, operation, cd);
597 g_hash_table_insert (cd->outstanding_channels,
598 g_strdup (object_path), operation);
602 dispatcher_start_dispatching (dispatcher, operation, cd);
607 dispatcher_connection_new_channel_cb (TpConnection *connection,
608 const gchar *object_path,
609 const gchar *channel_type,
612 gboolean suppress_handler,
616 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
618 /* Empathy heavily abuses surpress handler (don't try this at home), if
619 * surpress handler is true then it is an outgoing channel, which is
620 * requested either by us or some other party (like the megaphone applet).
621 * Otherwise it's an incoming channel */
622 dispatcher_connection_new_channel (dispatcher, connection,
623 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
627 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
628 TpConnection *connection,
629 const gchar *object_path,
630 GHashTable *properties)
632 const gchar *channel_type;
639 channel_type = tp_asv_get_string (properties,
640 TP_IFACE_CHANNEL ".ChannelType");
641 if (channel_type == NULL)
643 g_message ("%s had an invalid ChannelType property", object_path);
647 handle_type = tp_asv_get_uint32 (properties,
648 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
651 g_message ("%s had an invalid TargetHandleType property", object_path);
655 handle = tp_asv_get_uint32 (properties,
656 TP_IFACE_CHANNEL ".TargetHandle", &valid);
659 g_message ("%s had an invalid TargetHandle property", object_path);
663 /* We assume there is no channel dispather, so we're the only one dispatching
664 * it. Which means that a requested channel it is outgoing one */
665 requested = tp_asv_get_boolean (properties,
666 TP_IFACE_CHANNEL ".Requested", &valid);
669 g_message ("%s had an invalid Requested property", object_path);
673 dispatcher_connection_new_channel (dispatcher, connection,
674 object_path, channel_type, handle_type, handle, properties, !requested);
678 dispatcher_connection_new_channels_cb (TpConnection *connection,
679 const GPtrArray *channels,
683 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
686 for (i = 0; i < channels->len ; i++)
688 GValueArray *arr = g_ptr_array_index (channels, i);
689 const gchar *object_path;
690 GHashTable *properties;
692 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
693 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
695 dispatcher_connection_new_channel_with_properties (dispatcher,
696 connection, object_path, properties);
701 dispatcher_connection_got_all (TpProxy *proxy,
702 GHashTable *properties,
707 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
708 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
710 GPtrArray *requestable_channels;
713 DEBUG ("Error: %s", error->message);
717 channels = tp_asv_get_boxed (properties, "Channels",
718 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
720 if (channels == NULL)
721 DEBUG ("No Channels property !?! on connection");
723 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
724 channels, NULL, object);
726 requestable_channels = tp_asv_get_boxed (properties,
727 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
729 if (requestable_channels == NULL)
730 DEBUG ("No RequestableChannelClasses property !?! on connection");
735 FindChannelRequest *request;
738 cd = g_hash_table_lookup (priv->connections, proxy);
739 g_assert (cd != NULL);
741 cd->requestable_channels = g_boxed_copy (
742 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
744 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
747 for (l = requests; l != NULL; l = l->next)
751 retval = empathy_dispatcher_find_channel_class (dispatcher,
752 TP_CONNECTION (proxy), request->channel_type,
753 request->handle_type);
754 request->callback (retval, request->user_data);
756 free_find_channel_request (request);
759 g_list_free (requests);
761 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
766 dispatcher_connection_list_channels_cb (TpConnection *connection,
767 const GPtrArray *channels,
776 DEBUG ("Error: %s", error->message);
780 for (i = 0; i < channels->len; i++)
784 values = g_ptr_array_index (channels, i);
785 /* We don't have any extra info, so assume already existing channels are
787 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
789 g_value_get_boxed (g_value_array_get_nth (values, 0)),
790 g_value_get_string (g_value_array_get_nth (values, 1)),
791 g_value_get_uint (g_value_array_get_nth (values, 2)),
792 g_value_get_uint (g_value_array_get_nth (values, 3)),
798 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
799 const GPtrArray *capabilities,
805 DEBUG ("Error: %s", error->message);
809 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
810 TpConnection *connection,
811 EmpathyDispatcher *dispatcher)
813 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
814 GPtrArray *capabilities;
817 const gchar *remove = NULL;
819 if (g_hash_table_lookup (priv->connections, connection) != NULL)
822 g_hash_table_insert (priv->connections, g_object_ref (connection),
823 new_connection_data ());
825 g_signal_connect (connection, "invalidated",
826 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
828 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
829 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
831 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
832 dispatcher_connection_new_channels_cb,
833 NULL, NULL, G_OBJECT (dispatcher), NULL);
835 tp_cli_dbus_properties_call_get_all (connection, -1,
836 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
837 dispatcher_connection_got_all,
838 NULL, NULL, G_OBJECT (dispatcher));
842 tp_cli_connection_connect_to_new_channel (connection,
843 dispatcher_connection_new_channel_cb,
844 NULL, NULL, G_OBJECT (dispatcher), NULL);
846 tp_cli_connection_call_list_channels (connection, -1,
847 dispatcher_connection_list_channels_cb, NULL, NULL,
848 G_OBJECT (dispatcher));
852 /* Advertise VoIP capabilities */
853 capabilities = g_ptr_array_sized_new (1);
854 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
855 G_TYPE_UINT, G_TYPE_INVALID);
856 g_value_init (&cap, cap_type);
857 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
858 dbus_g_type_struct_set (&cap,
859 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
860 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
861 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
862 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
863 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
864 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
866 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
867 connection, -1, capabilities, &remove,
868 dispatcher_connection_advertise_capabilities_cb,
869 NULL, NULL, G_OBJECT (dispatcher));
871 g_value_unset (&cap);
872 g_ptr_array_free (capabilities, TRUE);
876 dispatcher_constructor (GType type,
877 guint n_construct_params,
878 GObjectConstructParam *construct_params)
882 if (dispatcher == NULL)
884 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
885 (type, n_construct_params, construct_params);
887 dispatcher = EMPATHY_DISPATCHER (retval);
888 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
892 retval = g_object_ref (dispatcher);
899 dispatcher_finalize (GObject *object)
901 EmpathyDispatcherPriv *priv = GET_PRIV (object);
907 g_signal_handlers_disconnect_by_func (priv->account_manager,
908 dispatcher_new_connection_cb, object);
910 for (l = priv->channels; l; l = l->next)
912 g_signal_handlers_disconnect_by_func (l->data,
913 dispatcher_channel_invalidated_cb, object);
916 g_list_free (priv->channels);
918 g_hash_table_iter_init (&iter, priv->connections);
919 while (g_hash_table_iter_next (&iter, &connection, NULL))
921 g_signal_handlers_disconnect_by_func (connection,
922 dispatcher_connection_invalidated_cb, object);
925 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
926 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
928 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
932 g_object_unref (priv->account_manager);
933 g_object_unref (priv->mc);
935 g_hash_table_destroy (priv->connections);
936 g_hash_table_destroy (priv->outstanding_classes_requests);
940 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
942 GObjectClass *object_class = G_OBJECT_CLASS (klass);
944 object_class->finalize = dispatcher_finalize;
945 object_class->constructor = dispatcher_constructor;
948 g_signal_new ("observe",
949 G_TYPE_FROM_CLASS (klass),
953 g_cclosure_marshal_VOID__OBJECT,
955 1, EMPATHY_TYPE_DISPATCH_OPERATION);
958 g_signal_new ("approve",
959 G_TYPE_FROM_CLASS (klass),
963 g_cclosure_marshal_VOID__OBJECT,
965 1, EMPATHY_TYPE_DISPATCH_OPERATION);
968 g_signal_new ("dispatch",
969 G_TYPE_FROM_CLASS (klass),
973 g_cclosure_marshal_VOID__OBJECT,
975 1, EMPATHY_TYPE_DISPATCH_OPERATION);
977 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
982 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
984 GList *connections, *l;
985 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
986 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
988 dispatcher->priv = priv;
989 priv->mc = empathy_mission_control_dup_singleton ();
990 priv->account_manager = empathy_account_manager_dup_singleton ();
992 g_signal_connect (priv->account_manager, "new-connection",
993 G_CALLBACK (dispatcher_new_connection_cb),
996 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
997 g_object_unref, (GDestroyNotify) free_connection_data);
999 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1000 g_direct_equal, g_object_unref, NULL);
1002 priv->channels = NULL;
1004 connections = empathy_account_manager_dup_connections (priv->account_manager);
1005 for (l = connections; l; l = l->next)
1007 dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
1008 g_object_unref (l->data);
1010 g_list_free (connections);
1014 empathy_dispatcher_dup_singleton (void)
1016 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1020 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1021 DispatcherRequestData *request_data,
1022 const GError *error)
1024 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1025 ConnectionData *conn_data;
1027 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1028 if (request_data->cb != NULL)
1029 request_data->cb (NULL, error, request_data->user_data);
1031 conn_data->outstanding_requests =
1032 g_list_remove (conn_data->outstanding_requests, request_data);
1033 free_dispatcher_request_data (request_data);
1037 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1038 DispatcherRequestData *request_data,
1039 const gchar *object_path,
1040 GHashTable *properties,
1041 const GError *error)
1043 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1044 EmpathyDispatchOperation *operation = NULL;
1045 ConnectionData *conn_data;
1047 conn_data = g_hash_table_lookup (priv->connections,
1048 request_data->connection);
1052 DEBUG ("Channel request failed: %s", error->message);
1054 dispatcher_request_failed (dispatcher, request_data, error);
1059 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1062 if (operation != NULL)
1063 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1065 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1068 if (operation == NULL)
1070 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1075 operation = empathy_dispatch_operation_new_with_wrapper (
1076 request_data->connection,
1077 data->channel, request_data->contact, FALSE,
1078 data->channel_wrapper);
1084 if (properties != NULL)
1085 channel = tp_channel_new_from_properties (request_data->connection,
1086 object_path, properties, NULL);
1088 channel = tp_channel_new (request_data->connection, object_path,
1089 request_data->channel_type, request_data->handle_type,
1090 request_data->handle, NULL);
1092 g_signal_connect (channel, "invalidated",
1093 G_CALLBACK (dispatcher_channel_invalidated_cb),
1094 request_data->dispatcher);
1096 priv->channels = g_list_prepend (priv->channels, channel);
1098 operation = empathy_dispatch_operation_new (request_data->connection,
1099 channel, request_data->contact, FALSE);
1100 g_object_unref (channel);
1105 /* Already existed set potential extra information */
1106 g_object_set (G_OBJECT (operation),
1107 "contact", request_data->contact,
1111 request_data->operation = operation;
1113 /* (pre)-approve this right away as we requested it
1114 * This might cause the channel to be claimed, in which case the operation
1115 * will disappear. So ref it, and check the status before starting the
1118 g_object_ref (operation);
1119 empathy_dispatch_operation_approve (operation);
1121 if (empathy_dispatch_operation_get_status (operation) <
1122 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1123 dispatcher_start_dispatching (request_data->dispatcher, operation,
1126 g_object_unref (operation);
1129 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1134 dispatcher_request_channel_cb (TpConnection *connection,
1135 const gchar *object_path,
1136 const GError *error,
1138 GObject *weak_object)
1140 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1141 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1143 dispatcher_connection_new_requested_channel (dispatcher,
1144 request_data, object_path, NULL, error);
1148 dispatcher_request_channel (DispatcherRequestData *request_data)
1150 tp_cli_connection_call_request_channel (request_data->connection, -1,
1151 request_data->channel_type,
1152 request_data->handle_type,
1153 request_data->handle,
1154 TRUE, dispatcher_request_channel_cb,
1155 request_data, NULL, G_OBJECT (request_data->dispatcher));
1159 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1160 EmpathyDispatcherRequestCb *callback,
1163 EmpathyDispatcher *dispatcher;
1164 EmpathyDispatcherPriv *priv;
1165 TpConnection *connection;
1166 ConnectionData *connection_data;
1167 DispatcherRequestData *request_data;
1169 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1171 dispatcher = empathy_dispatcher_dup_singleton ();
1172 priv = GET_PRIV (dispatcher);
1174 connection = empathy_contact_get_connection (contact);
1175 connection_data = g_hash_table_lookup (priv->connections, connection);
1177 /* The contact handle might not be known yet */
1178 request_data = new_dispatcher_request_data (dispatcher, connection,
1179 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1180 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1182 connection_data->outstanding_requests = g_list_prepend
1183 (connection_data->outstanding_requests, request_data);
1185 dispatcher_request_channel (request_data);
1187 g_object_unref (dispatcher);
1192 EmpathyDispatcher *dispatcher;
1193 EmpathyDispatcherRequestCb *callback;
1195 } ChatWithContactIdData;
1198 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1199 EmpathyContact *contact,
1200 const GError *error,
1202 GObject *weak_object)
1204 ChatWithContactIdData *data = user_data;
1208 /* FIXME: Should call data->callback with the error */
1209 DEBUG ("Error: %s", error->message);
1213 empathy_dispatcher_chat_with_contact (contact, data->callback, data->user_data);
1216 g_object_unref (data->dispatcher);
1217 g_slice_free (ChatWithContactIdData, data);
1221 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1222 const gchar *contact_id,
1223 EmpathyDispatcherRequestCb *callback,
1226 EmpathyDispatcher *dispatcher;
1227 EmpathyTpContactFactory *factory;
1228 ChatWithContactIdData *data;
1230 g_return_if_fail (TP_IS_CONNECTION (connection));
1231 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1233 dispatcher = empathy_dispatcher_dup_singleton ();
1234 factory = empathy_tp_contact_factory_dup_singleton (connection);
1235 data = g_slice_new0 (ChatWithContactIdData);
1236 data->dispatcher = dispatcher;
1237 data->callback = callback;
1238 data->user_data = user_data;
1239 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1240 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1242 g_object_unref (factory);
1246 dispatcher_request_handles_cb (TpConnection *connection,
1247 const GArray *handles,
1248 const GError *error,
1252 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1256 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1257 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1260 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1262 if (request_data->cb)
1263 request_data->cb (NULL, error, request_data->user_data);
1265 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1268 free_dispatcher_request_data (request_data);
1270 dispatcher_flush_outstanding_operations (dispatcher, cd);
1274 request_data->handle = g_array_index (handles, guint, 0);
1275 dispatcher_request_channel (request_data);
1279 empathy_dispatcher_join_muc (TpConnection *connection,
1280 const gchar *roomname,
1281 EmpathyDispatcherRequestCb *callback,
1284 EmpathyDispatcher *dispatcher;
1285 EmpathyDispatcherPriv *priv;
1286 DispatcherRequestData *request_data;
1287 ConnectionData *connection_data;
1288 const gchar *names[] = { roomname, NULL };
1290 g_return_if_fail (TP_IS_CONNECTION (connection));
1291 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1293 dispatcher = empathy_dispatcher_dup_singleton ();
1294 priv = GET_PRIV (dispatcher);
1296 connection_data = g_hash_table_lookup (priv->connections, connection);
1298 /* Don't know the room handle yet */
1299 request_data = new_dispatcher_request_data (dispatcher, connection,
1300 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1301 NULL, callback, user_data);
1303 connection_data->outstanding_requests = g_list_prepend
1304 (connection_data->outstanding_requests, request_data);
1306 tp_cli_connection_call_request_handles (connection, -1,
1307 TP_HANDLE_TYPE_ROOM, names,
1308 dispatcher_request_handles_cb, request_data, NULL,
1309 G_OBJECT (dispatcher));
1311 g_object_unref (dispatcher);
1315 dispatcher_create_channel_cb (TpConnection *connect,
1316 const gchar *object_path,
1317 GHashTable *properties,
1318 const GError *error,
1320 GObject *weak_object)
1322 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1323 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1325 dispatcher_connection_new_requested_channel (dispatcher,
1326 request_data, object_path, properties, error);
1330 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1331 TpConnection *connection,
1332 GHashTable *request,
1333 EmpathyDispatcherRequestCb *callback,
1336 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1337 ConnectionData *connection_data;
1338 DispatcherRequestData *request_data;
1339 const gchar *channel_type;
1344 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1345 g_return_if_fail (TP_IS_CONNECTION (connection));
1346 g_return_if_fail (request != NULL);
1348 connection_data = g_hash_table_lookup (priv->connections, connection);
1349 g_assert (connection_data != NULL);
1351 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1353 handle_type = tp_asv_get_uint32 (request,
1354 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1356 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1358 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1360 request_data = new_dispatcher_request_data (dispatcher, connection,
1361 channel_type, handle_type, handle, request,
1362 NULL, callback, user_data);
1364 connection_data->outstanding_requests = g_list_prepend
1365 (connection_data->outstanding_requests, request_data);
1367 tp_cli_connection_interface_requests_call_create_channel (
1368 request_data->connection, -1,
1369 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1370 G_OBJECT (request_data->dispatcher));
1374 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1375 TpConnection *connection,
1376 const gchar *channel_type,
1379 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1384 g_return_val_if_fail (channel_type != NULL, NULL);
1385 g_return_val_if_fail (handle_type != 0, NULL);
1387 cd = g_hash_table_lookup (priv->connections, connection);
1392 classes = cd->requestable_channels;
1393 if (classes == NULL)
1396 for (i = 0; i < classes->len; i++)
1402 const gchar *c_type;
1406 class = g_ptr_array_index (classes, i);
1407 fixed = g_value_array_get_nth (class, 0);
1409 fprops = g_value_get_boxed (fixed);
1410 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1412 if (tp_strdiff (channel_type, c_type))
1415 h_type = tp_asv_get_uint32 (fprops,
1416 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1418 if (!valid || handle_type != h_type)
1421 allowed = g_value_array_get_nth (class, 1);
1423 return g_value_get_boxed (allowed);
1430 find_channel_class_idle_cb (gpointer user_data)
1434 FindChannelRequest *request = user_data;
1436 gboolean is_ready = TRUE;
1437 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1439 cd = g_hash_table_lookup (priv->connections, request->connection);
1443 else if (cd->requestable_channels == NULL)
1448 retval = empathy_dispatcher_find_channel_class (request->dispatcher,
1449 request->connection, request->channel_type, request->handle_type);
1451 request->callback (retval, request->user_data);
1452 free_find_channel_request (request);
1456 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1457 request->connection);
1458 requests = g_list_prepend (requests, request);
1460 g_hash_table_insert (priv->outstanding_classes_requests,
1461 request->connection, requests);
1467 empathy_dispatcher_find_channel_class_async (EmpathyDispatcher *dispatcher,
1468 TpConnection *connection,
1469 const gchar *channel_type,
1471 EmpathyDispatcherFindChannelClassCb callback,
1474 FindChannelRequest *request;
1476 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1477 g_return_if_fail (TP_IS_CONNECTION (connection));
1478 g_return_if_fail (channel_type != NULL);
1479 g_return_if_fail (handle_type != 0);
1481 /* append another request for this connection */
1482 request = g_slice_new0 (FindChannelRequest);
1483 request->dispatcher = dispatcher;
1484 request->channel_type = g_strdup (channel_type);
1485 request->handle_type = handle_type;
1486 request->connection = connection;
1487 request->callback = callback;
1488 request->user_data = user_data;
1490 g_idle_add (find_channel_class_idle_cb, request);