1 /* * Copyright (C) 2007-2009 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>
18 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 #define DISPATCHER_BUS_NAME TP_CLIENT_BUS_NAME_BASE "Empathy"
23 #define DISPATCHER_OBJECT_PATH TP_CLIENT_OBJECT_PATH_BASE "Empathy"
29 #include <glib/gi18n-lib.h>
31 #include <telepathy-glib/enums.h>
32 #include <telepathy-glib/connection.h>
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/dbus.h>
35 #include <telepathy-glib/proxy-subclass.h>
36 #include <telepathy-glib/gtypes.h>
37 #include <telepathy-glib/defs.h>
38 #include <telepathy-glib/svc-client.h>
39 #include <telepathy-glib/svc-generic.h>
40 #include <telepathy-glib/interfaces.h>
42 #include <extensions/extensions.h>
44 #include "empathy-dispatcher.h"
45 #include "empathy-handler.h"
46 #include "empathy-utils.h"
47 #include "empathy-tube-handler.h"
48 #include "empathy-account-manager.h"
49 #include "empathy-tp-contact-factory.h"
50 #include "empathy-chatroom-manager.h"
51 #include "empathy-utils.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
59 gboolean dispose_has_run;
61 EmpathyAccountManager *account_manager;
62 /* connection to connection data mapping */
63 GHashTable *connections;
64 GHashTable *outstanding_classes_requests;
68 /* channels which the dispatcher is listening "invalidated" */
73 EmpathyHandler *handler;
78 GHashTable *request_channel_class_async_ids;
79 } EmpathyDispatcherPriv;
82 empathy_dispatcher_get_channels (EmpathyHandler *handler,
86 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
87 const gchar *account_path,
88 const gchar *connection_path,
89 const GPtrArray *channels,
90 const GPtrArray *requests_satisfied,
92 GHashTable *handler_info,
96 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
112 static guint signals[LAST_SIGNAL];
113 static EmpathyDispatcher *dispatcher = NULL;
115 static void dispatcher_init_connection_if_needed (
116 EmpathyDispatcher *dispatcher,
117 TpConnection *connection);
119 static GList * empathy_dispatcher_find_channel_classes
120 (EmpathyDispatcher *dispatcher, TpConnection *connection,
121 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
126 EmpathyDispatcher *dispatcher;
127 EmpathyDispatchOperation *operation;
128 TpConnection *connection;
129 gboolean should_ensure;
133 EmpathyContact *contact;
134 TpProxyPendingCall *pending_call;
136 /* Properties to pass to the channel when requesting it */
138 EmpathyDispatcherRequestCb *cb;
140 gpointer *request_data;
141 } DispatcherRequestData;
146 /* Channel type specific wrapper object */
147 GObject *channel_wrapper;
152 /* ObjectPath => DispatchData.. */
153 GHashTable *dispatched_channels;
154 /* ObjectPath -> EmpathyDispatchOperations */
155 GHashTable *dispatching_channels;
157 /* ObjectPath -> EmpathyDispatchOperations
159 * This holds channels which were announced with NewChannel while we have an
160 * outstanding channel request for a channel of this type. On the Requests
161 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
162 * return before NewChannels is emitted, but there was no guarantee of the
163 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
164 * held in limbo here until we know whether they were requested.
166 GHashTable *outstanding_channels;
167 /* List of DispatcherRequestData */
168 GList *outstanding_requests;
169 /* List of requestable channel classes */
170 GPtrArray *requestable_channels;
175 EmpathyDispatcher *dispatcher;
176 TpConnection *connection;
180 EmpathyDispatcherFindChannelClassCb *callback;
182 } FindChannelRequest;
185 empathy_dispatcher_call_create_or_ensure_channel (
186 EmpathyDispatcher *dispatcher,
187 DispatcherRequestData *request_data);
189 static DispatchData *
190 new_dispatch_data (TpChannel *channel,
191 GObject *channel_wrapper)
193 DispatchData *d = g_slice_new0 (DispatchData);
194 d->channel = g_object_ref (channel);
195 if (channel_wrapper != NULL)
196 d->channel_wrapper = g_object_ref (channel_wrapper);
202 free_dispatch_data (DispatchData *data)
204 g_object_unref (data->channel);
205 if (data->channel_wrapper != NULL)
206 g_object_unref (data->channel_wrapper);
208 g_slice_free (DispatchData, data);
211 static DispatcherRequestData *
212 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
213 TpConnection *connection,
214 const gchar *channel_type,
218 EmpathyContact *contact,
219 EmpathyDispatcherRequestCb *cb,
222 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
224 result->dispatcher = g_object_ref (dispatcher);
225 result->connection = connection;
227 result->should_ensure = FALSE;
229 result->channel_type = g_strdup (channel_type);
230 result->handle_type = handle_type;
231 result->handle = handle;
232 result->request = request;
235 result->contact = g_object_ref (contact);
238 result->user_data = user_data;
244 free_dispatcher_request_data (DispatcherRequestData *r)
246 g_free (r->channel_type);
248 if (r->dispatcher != NULL)
249 g_object_unref (r->dispatcher);
251 if (r->contact != NULL)
252 g_object_unref (r->contact);
254 if (r->request != NULL)
255 g_hash_table_unref (r->request);
257 g_slice_free (DispatcherRequestData, r);
260 static ConnectionData *
261 new_connection_data (void)
263 ConnectionData *cd = g_slice_new0 (ConnectionData);
265 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
266 g_free, (GDestroyNotify) free_dispatch_data);
268 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
269 g_free, g_object_unref);
271 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
278 free_connection_data (ConnectionData *cd)
282 g_hash_table_destroy (cd->dispatched_channels);
283 g_hash_table_destroy (cd->dispatching_channels);
286 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
288 DispatcherRequestData *data = l->data;
289 tp_proxy_pending_call_cancel (data->pending_call);
290 free_dispatcher_request_data (l->data);
293 if (cd->requestable_channels != NULL)
295 for (i = 0 ; i < cd->requestable_channels->len ; i++)
297 g_ptr_array_index (cd->requestable_channels, i));
298 g_ptr_array_free (cd->requestable_channels, TRUE);
303 free_find_channel_request (FindChannelRequest *r)
308 g_object_unref (r->dispatcher);
309 g_free (r->channel_type);
311 if (r->properties != NULL)
313 for (idx = 0; idx < r->properties->len ; idx++)
315 str = g_array_index (r->properties, char *, idx);
319 g_array_free (r->properties, TRUE);
322 g_slice_free (FindChannelRequest, r);
326 dispatcher_connection_invalidated_cb (TpConnection *connection,
330 EmpathyDispatcher *dispatcher)
332 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
334 DEBUG ("Error: %s", message);
335 g_hash_table_remove (priv->connections, connection);
339 dispatcher_operation_can_start (EmpathyDispatcher *self,
340 EmpathyDispatchOperation *operation,
344 const gchar *channel_type =
345 empathy_dispatch_operation_get_channel_type (operation);
347 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
349 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
351 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
361 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
362 EmpathyDispatchOperation *operation,
368 l = cd->outstanding_requests;
371 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
376 if (d->operation == operation)
381 d->cb (NULL, error, d->user_data);
383 d->cb (operation, NULL, d->user_data);
386 cd->outstanding_requests = g_list_delete_link
387 (cd->outstanding_requests, lt);
389 free_dispatcher_request_data (d);
395 dispatcher_channel_invalidated_cb (TpProxy *proxy,
399 EmpathyDispatcher *dispatcher)
401 /* Channel went away... */
402 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
403 TpConnection *connection;
404 EmpathyDispatchOperation *operation;
406 const gchar *object_path;
408 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
410 cd = g_hash_table_lookup (priv->connections, connection);
411 /* Connection itself invalidated? */
415 object_path = tp_proxy_get_object_path (proxy);
417 DEBUG ("Channel %s invalidated", object_path);
419 g_hash_table_remove (cd->dispatched_channels, object_path);
420 g_hash_table_remove (cd->dispatching_channels, object_path);
422 priv->channels = g_list_remove (priv->channels, proxy);
424 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
425 if (operation != NULL)
427 GError error = { domain, code, message };
428 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
429 g_hash_table_remove (cd->outstanding_channels, object_path);
430 g_object_unref (operation);
435 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
436 EmpathyDispatcher *dispatcher)
438 g_assert (empathy_dispatch_operation_is_incoming (operation));
439 DEBUG ("Send of for dispatching: %s",
440 empathy_dispatch_operation_get_object_path (operation));
441 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
445 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
446 EmpathyDispatcher *dispatcher)
448 /* Our job is done, remove the dispatch operation and mark the channel as
450 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
451 TpConnection *connection;
453 const gchar *object_path;
455 connection = empathy_dispatch_operation_get_tp_connection (operation);
456 cd = g_hash_table_lookup (priv->connections, connection);
457 g_assert (cd != NULL);
459 object_path = empathy_dispatch_operation_get_object_path (operation);
461 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
464 d = new_dispatch_data (
465 empathy_dispatch_operation_get_channel (operation),
466 empathy_dispatch_operation_get_channel_wrapper (operation));
467 g_hash_table_insert (cd->dispatched_channels,
468 g_strdup (object_path), d);
470 g_hash_table_remove (cd->dispatching_channels, object_path);
472 DEBUG ("Channel claimed: %s", object_path);
476 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
477 EmpathyDispatcher *dispatcher)
479 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
480 TpConnection *connection;
482 EmpathyDispatchOperationState status;
484 g_signal_connect (operation, "approved",
485 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
487 g_signal_connect (operation, "claimed",
488 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
490 /* Signal the observers */
491 DEBUG ("Send to observers: %s",
492 empathy_dispatch_operation_get_object_path (operation));
493 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
495 empathy_dispatch_operation_start (operation);
497 /* Signal potential requestors */
498 connection = empathy_dispatch_operation_get_tp_connection (operation);
499 cd = g_hash_table_lookup (priv->connections, connection);
500 g_assert (cd != NULL);
502 g_object_ref (operation);
503 g_object_ref (dispatcher);
505 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
506 status = empathy_dispatch_operation_get_status (operation);
507 g_object_unref (operation);
509 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
512 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
514 DEBUG ("Send to approvers: %s",
515 empathy_dispatch_operation_get_object_path (operation));
516 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
520 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
521 DEBUG ("Send of for dispatching: %s",
522 empathy_dispatch_operation_get_object_path (operation));
523 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
526 g_object_unref (dispatcher);
530 dispatcher_start_dispatching (EmpathyDispatcher *self,
531 EmpathyDispatchOperation *operation,
534 const gchar *object_path =
535 empathy_dispatch_operation_get_object_path (operation);
537 DEBUG ("Dispatching process started for %s", object_path);
539 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
541 g_assert (g_hash_table_lookup (cd->outstanding_channels,
542 object_path) == NULL);
544 g_hash_table_insert (cd->dispatching_channels,
545 g_strdup (object_path), operation);
547 switch (empathy_dispatch_operation_get_status (operation))
549 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
550 g_signal_connect (operation, "ready",
551 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
553 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
554 dispatch_operation_ready_cb (operation, dispatcher);
557 g_assert_not_reached ();
561 else if (empathy_dispatch_operation_get_status (operation) >=
562 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
564 /* Already dispatching and the operation is pending, thus the observers
565 * have seen it (if applicable), so we can flush the request right away.
567 dispatch_operation_flush_requests (self, operation, NULL, cd);
572 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
578 g_hash_table_iter_init (&iter, cd->outstanding_channels);
579 while (g_hash_table_iter_next (&iter, NULL, &value))
581 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
583 if (dispatcher_operation_can_start (self, operation, cd))
585 g_hash_table_iter_remove (&iter);
586 dispatcher_start_dispatching (dispatcher, operation, cd);
592 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
593 TpConnection *connection,
594 const gchar *object_path,
595 const gchar *channel_type,
598 GHashTable *properties,
601 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
604 EmpathyDispatchOperation *operation;
606 /* Channel types we never want to dispatch because they're either deprecated
607 * or can't sensibly be dispatch (e.g. channels that should always be
609 const char *blacklist[] = {
610 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
611 TP_IFACE_CHANNEL_TYPE_TUBES,
612 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
616 dispatcher_init_connection_if_needed (dispatcher, connection);
618 cd = g_hash_table_lookup (priv->connections, connection);
620 /* Don't bother with channels we have already dispatched or are dispatching
621 * currently. This can happen when NewChannel(s) is fired after
622 * RequestChannel/CreateChannel/EnsureChannel */
623 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
626 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
629 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
631 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
634 /* Only pick up non-requested text and file channels. For all other it
635 * doesn't make sense to handle it if we didn't request it. The same goes
636 * for channels we discovered by the Channels property or ListChannels */
637 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
638 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
640 DEBUG ("Ignoring incoming channel of type %s on %s",
641 channel_type, object_path);
645 for (i = 0 ; blacklist[i] != NULL; i++)
647 if (!tp_strdiff (channel_type, blacklist[i]))
649 DEBUG ("Ignoring blacklisted channel type %s on %s",
650 channel_type, object_path);
655 DEBUG ("New channel of type %s on %s", channel_type, object_path);
657 if (properties == NULL)
658 channel = tp_channel_new (connection, object_path, channel_type,
659 handle_type, handle, NULL);
661 channel = tp_channel_new_from_properties (connection, object_path,
664 g_signal_connect (channel, "invalidated",
665 G_CALLBACK (dispatcher_channel_invalidated_cb),
668 priv->channels = g_list_prepend (priv->channels, channel);
670 operation = empathy_dispatch_operation_new (connection, channel, NULL,
673 g_object_unref (channel);
677 /* Request could either be by us or by a remote party. If there are no
678 * outstanding requests for this channel type we can assume it's remote.
679 * Otherwise we wait untill they are all satisfied */
680 if (dispatcher_operation_can_start (dispatcher, operation, cd))
681 dispatcher_start_dispatching (dispatcher, operation, cd);
683 g_hash_table_insert (cd->outstanding_channels,
684 g_strdup (object_path), operation);
688 dispatcher_start_dispatching (dispatcher, operation, cd);
693 dispatcher_connection_new_channel_with_properties (
694 EmpathyDispatcher *dispatcher,
695 TpConnection *connection,
696 const gchar *object_path,
697 GHashTable *properties)
699 const gchar *channel_type;
706 channel_type = tp_asv_get_string (properties,
707 TP_IFACE_CHANNEL ".ChannelType");
708 if (channel_type == NULL)
710 g_message ("%s had an invalid ChannelType property", object_path);
714 handle_type = tp_asv_get_uint32 (properties,
715 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
718 g_message ("%s had an invalid TargetHandleType property", object_path);
722 handle = tp_asv_get_uint32 (properties,
723 TP_IFACE_CHANNEL ".TargetHandle", &valid);
726 g_message ("%s had an invalid TargetHandle property", object_path);
730 /* We assume there is no channel dispather, so we're the only one dispatching
731 * it. Which means that a requested channel it is outgoing one */
732 requested = tp_asv_get_boolean (properties,
733 TP_IFACE_CHANNEL ".Requested", &valid);
736 g_message ("%s had an invalid Requested property", object_path);
740 dispatcher_connection_new_channel (dispatcher, connection,
741 object_path, channel_type, handle_type, handle, properties, !requested);
745 dispatcher_connection_got_all (TpProxy *proxy,
746 GHashTable *properties,
751 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
752 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
753 GPtrArray *requestable_channels;
754 GPtrArray *existing_channels;
757 DEBUG ("Error: %s", error->message);
761 requestable_channels = tp_asv_get_boxed (properties,
762 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
764 if (requestable_channels == NULL)
765 DEBUG ("No RequestableChannelClasses property !?! on connection");
770 FindChannelRequest *request;
773 cd = g_hash_table_lookup (priv->connections, proxy);
774 g_assert (cd != NULL);
776 cd->requestable_channels = g_boxed_copy (
777 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
779 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
782 for (l = requests; l != NULL; l = l->next)
786 retval = empathy_dispatcher_find_channel_classes (dispatcher,
787 TP_CONNECTION (proxy), request->channel_type,
788 request->handle_type, request->properties);
789 request->callback (retval, request->user_data);
791 free_find_channel_request (request);
792 g_list_free (retval);
795 g_list_free (requests);
797 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
800 existing_channels = tp_asv_get_boxed (properties,
801 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
803 if (existing_channels != NULL)
807 for (idx = 0; idx < existing_channels->len; idx++)
809 GValueArray *values = g_ptr_array_index (existing_channels, idx);
810 const gchar *object_path;
811 GHashTable *properties;
813 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
814 properties = g_value_get_boxed (g_value_array_get_nth (values, 1));
816 if (tp_strdiff (tp_asv_get_string (properties,
817 TP_IFACE_CHANNEL ".ChannelType"),
818 TP_IFACE_CHANNEL_TYPE_TEXT))
821 dispatcher_connection_new_channel_with_properties (dispatcher,
822 TP_CONNECTION (proxy), object_path, properties);
828 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
829 const GPtrArray *capabilities,
835 DEBUG ("Error: %s", error->message);
839 dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
840 TpConnection *connection)
842 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
843 GPtrArray *capabilities;
846 const gchar *remove = NULL;
848 if (g_hash_table_lookup (priv->connections, connection) != NULL)
851 g_hash_table_insert (priv->connections, g_object_ref (connection),
852 new_connection_data ());
854 g_signal_connect (connection, "invalidated",
855 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
857 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
858 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
860 tp_cli_dbus_properties_call_get_all (connection, -1,
861 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
862 dispatcher_connection_got_all,
863 NULL, NULL, G_OBJECT (dispatcher));
866 /* Advertise VoIP capabilities */
867 capabilities = g_ptr_array_sized_new (1);
868 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
869 G_TYPE_UINT, G_TYPE_INVALID);
870 g_value_init (&cap, cap_type);
871 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
872 dbus_g_type_struct_set (&cap,
873 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
874 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
875 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
876 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
877 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
878 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
880 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
882 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
883 connection, -1, capabilities, &remove,
884 dispatcher_connection_advertise_capabilities_cb,
885 NULL, NULL, G_OBJECT (dispatcher));
887 g_value_unset (&cap);
888 g_ptr_array_free (capabilities, TRUE);
892 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
893 TpConnection *connection,
894 EmpathyDispatcher *dispatcher)
896 dispatcher_init_connection_if_needed (dispatcher, connection);
900 remove_idle_handlers (gpointer key,
906 source_id = GPOINTER_TO_UINT (value);
907 g_source_remove (source_id);
911 dispatcher_constructor (GType type,
912 guint n_construct_params,
913 GObjectConstructParam *construct_params)
916 EmpathyDispatcherPriv *priv;
918 if (dispatcher != NULL)
919 return g_object_ref (dispatcher);
921 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
922 (type, n_construct_params, construct_params);
924 dispatcher = EMPATHY_DISPATCHER (retval);
925 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
927 priv = GET_PRIV (dispatcher);
929 if (priv->handler == NULL)
930 priv->handler = empathy_handler_new (NULL, NULL, NULL);
932 empathy_handler_set_handle_channels_func (priv->handler,
933 empathy_dispatcher_handle_channels,
936 empathy_handler_set_channels_func (priv->handler,
937 empathy_dispatcher_get_channels,
944 dispatcher_dispose (GObject *object)
946 EmpathyDispatcherPriv *priv = GET_PRIV (object);
949 if (priv->dispose_has_run)
952 priv->dispose_has_run = TRUE;
954 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
955 g_object_unref (G_OBJECT (l->data));
957 g_list_free (priv->handlers);
958 priv->handlers = NULL;
960 if (priv->handler != NULL)
961 g_object_unref (priv->handler);
962 priv->handler = NULL;
964 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
968 dispatcher_finalize (GObject *object)
970 EmpathyDispatcherPriv *priv = GET_PRIV (object);
976 if (priv->request_channel_class_async_ids != NULL)
978 g_hash_table_foreach (priv->request_channel_class_async_ids,
979 remove_idle_handlers, NULL);
980 g_hash_table_destroy (priv->request_channel_class_async_ids);
983 g_signal_handlers_disconnect_by_func (priv->account_manager,
984 dispatcher_new_connection_cb, object);
986 for (l = priv->channels; l; l = l->next)
988 g_signal_handlers_disconnect_by_func (l->data,
989 dispatcher_channel_invalidated_cb, object);
992 g_list_free (priv->channels);
994 g_hash_table_iter_init (&iter, priv->connections);
995 while (g_hash_table_iter_next (&iter, &connection, NULL))
997 g_signal_handlers_disconnect_by_func (connection,
998 dispatcher_connection_invalidated_cb, object);
1001 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1002 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1004 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1008 g_object_unref (priv->account_manager);
1010 g_hash_table_destroy (priv->connections);
1011 g_hash_table_destroy (priv->outstanding_classes_requests);
1015 dispatcher_set_property (GObject *object,
1017 const GValue *value,
1020 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1021 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1023 switch (property_id)
1026 priv->handler = g_value_dup_object (value);
1029 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1035 dispatcher_get_property (GObject *object,
1040 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1041 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1043 switch (property_id)
1046 g_value_set_object (value, priv->handler);
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1055 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1057 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1058 GParamSpec *param_spec;
1060 object_class->dispose = dispatcher_dispose;
1061 object_class->finalize = dispatcher_finalize;
1062 object_class->constructor = dispatcher_constructor;
1064 object_class->get_property = dispatcher_get_property;
1065 object_class->set_property = dispatcher_set_property;
1067 param_spec = g_param_spec_object ("handler", "handler",
1068 "The main Telepathy Client Hander object",
1069 EMPATHY_TYPE_HANDLER,
1070 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1071 g_object_class_install_property (object_class,
1072 PROP_HANDLER, param_spec);
1075 g_signal_new ("observe",
1076 G_TYPE_FROM_CLASS (klass),
1080 g_cclosure_marshal_VOID__OBJECT,
1082 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1085 g_signal_new ("approve",
1086 G_TYPE_FROM_CLASS (klass),
1090 g_cclosure_marshal_VOID__OBJECT,
1092 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1095 g_signal_new ("dispatch",
1096 G_TYPE_FROM_CLASS (klass),
1100 g_cclosure_marshal_VOID__OBJECT,
1102 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1105 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1109 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1111 GList *connections, *l;
1112 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1113 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1115 dispatcher->priv = priv;
1116 priv->account_manager = empathy_account_manager_dup_singleton ();
1118 g_signal_connect (priv->account_manager, "new-connection",
1119 G_CALLBACK (dispatcher_new_connection_cb),
1122 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1123 g_object_unref, (GDestroyNotify) free_connection_data);
1125 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1126 g_direct_equal, g_object_unref, NULL);
1128 priv->channels = NULL;
1130 connections = empathy_account_manager_dup_connections (
1131 priv->account_manager);
1132 for (l = connections; l; l = l->next)
1134 dispatcher_new_connection_cb (priv->account_manager, l->data,
1136 g_object_unref (l->data);
1138 g_list_free (connections);
1140 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1145 empathy_dispatcher_new (const gchar *name,
1149 g_assert (dispatcher == NULL);
1150 EmpathyHandler *handler;
1151 EmpathyDispatcher *ret;
1153 handler = empathy_handler_new (name, filters, capabilities);
1155 ret = EMPATHY_DISPATCHER (
1156 g_object_new (EMPATHY_TYPE_DISPATCHER,
1159 g_object_unref (handler);
1165 empathy_dispatcher_dup_singleton (void)
1167 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1171 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1172 DispatcherRequestData *request_data,
1173 const GError *error)
1175 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1176 ConnectionData *conn_data;
1178 conn_data = g_hash_table_lookup (priv->connections,
1179 request_data->connection);
1180 if (request_data->cb != NULL)
1181 request_data->cb (NULL, error, request_data->user_data);
1183 if (conn_data != NULL)
1185 conn_data->outstanding_requests =
1186 g_list_remove (conn_data->outstanding_requests, request_data);
1188 /* else Connection has been invalidated */
1190 free_dispatcher_request_data (request_data);
1194 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1195 DispatcherRequestData *request_data,
1196 const gchar *object_path,
1197 GHashTable *properties,
1198 const GError *error)
1200 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1201 EmpathyDispatchOperation *operation = NULL;
1202 ConnectionData *conn_data;
1204 conn_data = g_hash_table_lookup (priv->connections,
1205 request_data->connection);
1209 DEBUG ("Channel request failed: %s", error->message);
1211 dispatcher_request_failed (dispatcher, request_data, error);
1216 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1219 if (operation != NULL)
1220 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1222 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1225 if (operation == NULL)
1227 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1232 operation = empathy_dispatch_operation_new_with_wrapper (
1233 request_data->connection,
1234 data->channel, request_data->contact, FALSE,
1235 data->channel_wrapper);
1241 if (properties != NULL)
1242 channel = tp_channel_new_from_properties (request_data->connection,
1243 object_path, properties, NULL);
1245 channel = tp_channel_new (request_data->connection, object_path,
1246 request_data->channel_type, request_data->handle_type,
1247 request_data->handle, NULL);
1249 g_signal_connect (channel, "invalidated",
1250 G_CALLBACK (dispatcher_channel_invalidated_cb),
1251 request_data->dispatcher);
1253 priv->channels = g_list_prepend (priv->channels, channel);
1255 operation = empathy_dispatch_operation_new (request_data->connection,
1256 channel, request_data->contact, FALSE);
1257 g_object_unref (channel);
1262 /* Already existed set potential extra information */
1263 g_object_set (G_OBJECT (operation),
1264 "contact", request_data->contact,
1268 request_data->operation = operation;
1270 /* (pre)-approve this right away as we requested it
1271 * This might cause the channel to be claimed, in which case the operation
1272 * will disappear. So ref it, and check the status before starting the
1275 g_object_ref (operation);
1276 empathy_dispatch_operation_approve (operation);
1278 if (empathy_dispatch_operation_get_status (operation) <
1279 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1280 dispatcher_start_dispatching (request_data->dispatcher, operation,
1283 g_object_unref (operation);
1286 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1291 dispatcher_request_channel_cb (TpConnection *connection,
1292 const gchar *object_path,
1293 const GError *error,
1295 GObject *weak_object)
1297 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1298 EmpathyDispatcher *dispatcher =
1299 EMPATHY_DISPATCHER (request_data->dispatcher);
1301 dispatcher_connection_new_requested_channel (dispatcher,
1302 request_data, object_path, NULL, error);
1306 dispatcher_request_channel (DispatcherRequestData *request_data)
1308 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1309 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1311 /* Extend the request_data to be a valid request */
1312 g_assert (request_data->request == NULL);
1313 request_data->request = tp_asv_new (
1314 TP_IFACE_CHANNEL ".ChannelType",
1315 G_TYPE_STRING, request_data->channel_type,
1316 TP_IFACE_CHANNEL ".TargetHandleType",
1317 G_TYPE_UINT, request_data->handle_type,
1320 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1321 tp_asv_set_uint32 (request_data->request,
1322 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1324 empathy_dispatcher_call_create_or_ensure_channel (
1325 request_data->dispatcher, request_data);
1329 request_data->pending_call = tp_cli_connection_call_request_channel (
1330 request_data->connection, -1,
1331 request_data->channel_type,
1332 request_data->handle_type,
1333 request_data->handle,
1334 TRUE, dispatcher_request_channel_cb,
1335 request_data, NULL, NULL);
1340 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1341 EmpathyDispatcherRequestCb *callback,
1344 EmpathyDispatcher *dispatcher;
1345 EmpathyDispatcherPriv *priv;
1346 TpConnection *connection;
1347 ConnectionData *connection_data;
1348 DispatcherRequestData *request_data;
1350 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1352 dispatcher = empathy_dispatcher_dup_singleton ();
1353 priv = GET_PRIV (dispatcher);
1355 connection = empathy_contact_get_connection (contact);
1356 connection_data = g_hash_table_lookup (priv->connections, connection);
1358 /* The contact handle might not be known yet */
1359 request_data = new_dispatcher_request_data (dispatcher, connection,
1360 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1361 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1362 request_data->should_ensure = TRUE;
1364 connection_data->outstanding_requests = g_list_prepend
1365 (connection_data->outstanding_requests, request_data);
1367 dispatcher_request_channel (request_data);
1369 g_object_unref (dispatcher);
1374 EmpathyDispatcher *dispatcher;
1375 EmpathyDispatcherRequestCb *callback;
1377 } ChatWithContactIdData;
1380 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1381 EmpathyContact *contact,
1382 const GError *error,
1384 GObject *weak_object)
1386 ChatWithContactIdData *data = user_data;
1390 /* FIXME: Should call data->callback with the error */
1391 DEBUG ("Error: %s", error->message);
1395 empathy_dispatcher_chat_with_contact (contact, data->callback,
1399 g_object_unref (data->dispatcher);
1400 g_slice_free (ChatWithContactIdData, data);
1404 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1405 const gchar *contact_id,
1406 EmpathyDispatcherRequestCb *callback,
1409 EmpathyDispatcher *dispatcher;
1410 EmpathyTpContactFactory *factory;
1411 ChatWithContactIdData *data;
1413 g_return_if_fail (TP_IS_CONNECTION (connection));
1414 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1416 dispatcher = empathy_dispatcher_dup_singleton ();
1417 factory = empathy_tp_contact_factory_dup_singleton (connection);
1418 data = g_slice_new0 (ChatWithContactIdData);
1419 data->dispatcher = dispatcher;
1420 data->callback = callback;
1421 data->user_data = user_data;
1422 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1423 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1425 g_object_unref (factory);
1429 dispatcher_request_handles_cb (TpConnection *connection,
1430 const GArray *handles,
1431 const GError *error,
1435 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1439 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1440 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1443 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1445 if (request_data->cb)
1446 request_data->cb (NULL, error, request_data->user_data);
1448 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1451 free_dispatcher_request_data (request_data);
1453 dispatcher_flush_outstanding_operations (dispatcher, cd);
1457 request_data->handle = g_array_index (handles, guint, 0);
1458 dispatcher_request_channel (request_data);
1462 empathy_dispatcher_join_muc (TpConnection *connection,
1463 const gchar *roomname,
1464 EmpathyDispatcherRequestCb *callback,
1467 EmpathyDispatcher *dispatcher;
1468 EmpathyDispatcherPriv *priv;
1469 DispatcherRequestData *request_data;
1470 ConnectionData *connection_data;
1471 const gchar *names[] = { roomname, NULL };
1473 g_return_if_fail (TP_IS_CONNECTION (connection));
1474 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1476 dispatcher = empathy_dispatcher_dup_singleton ();
1477 priv = GET_PRIV (dispatcher);
1479 connection_data = g_hash_table_lookup (priv->connections, connection);
1481 /* Don't know the room handle yet */
1482 request_data = new_dispatcher_request_data (dispatcher, connection,
1483 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1484 NULL, callback, user_data);
1486 connection_data->outstanding_requests = g_list_prepend
1487 (connection_data->outstanding_requests, request_data);
1489 request_data->pending_call = tp_cli_connection_call_request_handles (
1491 TP_HANDLE_TYPE_ROOM, names,
1492 dispatcher_request_handles_cb, request_data, NULL, NULL);
1494 g_object_unref (dispatcher);
1498 dispatcher_create_channel_cb (TpConnection *connect,
1499 const gchar *object_path,
1500 GHashTable *properties,
1501 const GError *error,
1503 GObject *weak_object)
1505 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1506 EmpathyDispatcher *dispatcher =
1507 EMPATHY_DISPATCHER (request_data->dispatcher);
1509 dispatcher_connection_new_requested_channel (dispatcher,
1510 request_data, object_path, properties, error);
1514 dispatcher_ensure_channel_cb (TpConnection *connect,
1516 const gchar *object_path,
1517 GHashTable *properties,
1518 const GError *error,
1520 GObject *weak_object)
1522 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1523 EmpathyDispatcher *dispatcher =
1524 EMPATHY_DISPATCHER (request_data->dispatcher);
1526 dispatcher_connection_new_requested_channel (dispatcher,
1527 request_data, object_path, properties, error);
1531 empathy_dispatcher_call_create_or_ensure_channel (
1532 EmpathyDispatcher *dispatcher,
1533 DispatcherRequestData *request_data)
1535 if (request_data->should_ensure)
1537 request_data->pending_call =
1538 tp_cli_connection_interface_requests_call_ensure_channel (
1539 request_data->connection, -1,
1540 request_data->request, dispatcher_ensure_channel_cb,
1541 request_data, NULL, NULL);
1545 request_data->pending_call =
1546 tp_cli_connection_interface_requests_call_create_channel (
1547 request_data->connection, -1,
1548 request_data->request, dispatcher_create_channel_cb,
1549 request_data, NULL, NULL);
1554 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1555 TpConnection *connection,
1556 GHashTable *request,
1557 EmpathyDispatcherRequestCb *callback,
1560 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1561 ConnectionData *connection_data;
1562 DispatcherRequestData *request_data;
1563 const gchar *channel_type;
1568 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1569 g_return_if_fail (TP_IS_CONNECTION (connection));
1570 g_return_if_fail (request != NULL);
1572 connection_data = g_hash_table_lookup (priv->connections, connection);
1573 g_assert (connection_data != NULL);
1575 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1577 handle_type = tp_asv_get_uint32 (request,
1578 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1580 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1582 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1584 request_data = new_dispatcher_request_data (dispatcher, connection,
1585 channel_type, handle_type, handle, request,
1586 NULL, callback, user_data);
1588 connection_data->outstanding_requests = g_list_prepend
1589 (connection_data->outstanding_requests, request_data);
1591 empathy_dispatcher_call_create_or_ensure_channel (dispatcher, request_data);
1595 channel_class_matches (GValueArray *class,
1596 const char *channel_type,
1598 GArray *fixed_properties)
1606 v = g_value_array_get_nth (class, 0);
1608 /* if the class doesn't match channel type discard it. */
1609 fprops = g_value_get_boxed (v);
1610 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1612 if (tp_strdiff (channel_type, c_type))
1615 /* we have the right channel type, see if the handle type matches */
1616 h_type = tp_asv_get_uint32 (fprops,
1617 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1619 if (!valid || handle_type != h_type)
1622 if (fixed_properties != NULL)
1624 gpointer h_key, h_val;
1626 GHashTableIter iter;
1629 g_hash_table_iter_init (&iter, fprops);
1631 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1633 /* discard ChannelType and TargetHandleType, as we already
1636 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1638 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1643 for (idx = 0; idx < fixed_properties->len; idx++)
1645 /* if |key| doesn't exist in |fixed_properties|, discard
1650 g_array_index (fixed_properties, char *, idx)))
1653 /* exit the for() loop */
1664 /* if no fixed_properties are specified, discard the classes
1665 * with some fixed properties other than the two we already
1668 if (g_hash_table_size (fprops) > 2)
1676 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher,
1677 TpConnection *connection,
1678 const gchar *channel_type,
1680 GArray *fixed_properties)
1682 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1685 GList *matching_classes;
1689 g_return_val_if_fail (channel_type != NULL, NULL);
1690 g_return_val_if_fail (handle_type != 0, NULL);
1692 cd = g_hash_table_lookup (priv->connections, connection);
1697 classes = cd->requestable_channels;
1698 if (classes == NULL)
1701 matching_classes = NULL;
1703 for (i = 0; i < classes->len; i++)
1705 class = g_ptr_array_index (classes, i);
1707 if (!channel_class_matches
1708 (class, channel_type, handle_type, fixed_properties))
1711 matching_classes = g_list_prepend (matching_classes, class);
1714 return matching_classes;
1718 find_channel_class_idle_cb (gpointer user_data)
1722 FindChannelRequest *request = user_data;
1724 gboolean is_ready = TRUE;
1725 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1727 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1729 cd = g_hash_table_lookup (priv->connections, request->connection);
1733 else if (cd->requestable_channels == NULL)
1738 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1739 request->connection, request->channel_type, request->handle_type,
1740 request->properties);
1742 request->callback (retval, request->user_data);
1743 free_find_channel_request (request);
1744 g_list_free (retval);
1749 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1750 request->connection);
1751 requests = g_list_prepend (requests, request);
1753 g_hash_table_insert (priv->outstanding_classes_requests,
1754 request->connection, requests);
1760 setup_varargs (va_list var_args,
1761 const char *channel_namespace,
1762 const char *first_property_name)
1768 if (first_property_name == NULL)
1771 name = first_property_name;
1772 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1774 while (name != NULL)
1776 name_full = g_strdup (name);
1777 properties = g_array_append_val (properties, name_full);
1778 name = va_arg (var_args, char *);
1785 * empathy_dispatcher_find_requestable_channel_classes:
1786 * @dispatcher: an #EmpathyDispatcher
1787 * @connection: a #TpConnection
1788 * @channel_type: a string identifying the type of the channel to lookup
1789 * @handle_type: the handle type for the channel
1790 * @first_property_name: %NULL, or the name of the first fixed property,
1791 * followed optionally by more names, followed by %NULL.
1793 * Returns all the channel classes that a client can request for the connection
1794 * @connection, of the type identified by @channel_type, @handle_type and the
1795 * fixed properties list.
1796 * The classes which are compatible with a fixed properties list (i.e. those
1797 * that will be returned by this function) are intended as those that do not
1798 * contain any fixed property other than those in the list; note that this
1799 * doesn't guarantee that all the classes compatible with the list will contain
1800 * all the requested fixed properties, so the clients will have to filter
1801 * the returned list themselves.
1802 * If @first_property_name is %NULL, only the classes with no other fixed
1803 * properties than ChannelType and TargetHandleType will be returned.
1804 * Note that this function may return %NULL without performing any lookup if
1805 * @connection is not ready. To ensure that @connection is always ready,
1806 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1808 * Return value: a #GList of #GValueArray objects, where the first element in
1809 * the array is a #GHashTable of the fixed properties, and the second is
1810 * a #GStrv of the allowed properties for the class. The list should be free'd
1811 * with g_list_free() when done, but the objects inside the list are owned
1812 * by the #EmpathyDispatcher and must not be modified.
1815 empathy_dispatcher_find_requestable_channel_classes
1816 (EmpathyDispatcher *dispatcher,
1817 TpConnection *connection,
1818 const gchar *channel_type,
1820 const char *first_property_name,
1825 EmpathyDispatcherPriv *priv;
1830 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL);
1831 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1832 g_return_val_if_fail (channel_type != NULL, NULL);
1833 g_return_val_if_fail (handle_type != 0, NULL);
1835 priv = GET_PRIV (dispatcher);
1837 va_start (var_args, first_property_name);
1839 properties = setup_varargs (var_args, channel_type, first_property_name);
1843 retval = empathy_dispatcher_find_channel_classes (dispatcher, connection,
1844 channel_type, handle_type, properties);
1846 if (properties != NULL)
1848 /* free the properties array */
1849 for (idx = 0; idx < properties->len ; idx++)
1851 str = g_array_index (properties, char *, idx);
1855 g_array_free (properties, TRUE);
1862 * empathy_dispatcher_find_requestable_channel_classes_async:
1863 * @dispatcher: an #EmpathyDispatcher
1864 * @connection: a #TpConnection
1865 * @channel_type: a string identifying the type of the channel to lookup
1866 * @handle_type: the handle type for the channel
1867 * @callback: the callback to call when @connection is ready
1868 * @user_data: the user data to pass to @callback
1869 * @first_property_name: %NULL, or the name of the first fixed property,
1870 * followed optionally by more names, followed by %NULL.
1872 * Please see the documentation of
1873 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1874 * description of this function.
1877 empathy_dispatcher_find_requestable_channel_classes_async
1878 (EmpathyDispatcher *dispatcher,
1879 TpConnection *connection,
1880 const gchar *channel_type,
1882 EmpathyDispatcherFindChannelClassCb callback,
1884 const char *first_property_name,
1889 FindChannelRequest *request;
1890 EmpathyDispatcherPriv *priv;
1893 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1894 g_return_if_fail (TP_IS_CONNECTION (connection));
1895 g_return_if_fail (channel_type != NULL);
1896 g_return_if_fail (handle_type != 0);
1898 priv = GET_PRIV (dispatcher);
1900 va_start (var_args, first_property_name);
1902 properties = setup_varargs (var_args, channel_type, first_property_name);
1906 /* append another request for this connection */
1907 request = g_slice_new0 (FindChannelRequest);
1908 request->dispatcher = g_object_ref (dispatcher);
1909 request->channel_type = g_strdup (channel_type);
1910 request->handle_type = handle_type;
1911 request->connection = connection;
1912 request->callback = callback;
1913 request->user_data = user_data;
1914 request->properties = properties;
1916 source_id = g_idle_add (find_channel_class_idle_cb, request);
1918 g_hash_table_insert (priv->request_channel_class_async_ids,
1919 request, GUINT_TO_POINTER (source_id));
1923 empathy_dispatcher_get_channels (EmpathyHandler *handler,
1926 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1927 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1929 return priv->channels;
1933 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
1934 const gchar *account_path,
1935 const gchar *connection_path,
1936 const GPtrArray *channels,
1937 const GPtrArray *requests_satisfied,
1939 GHashTable *handler_info,
1943 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1944 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1946 EmpathyAccount *account;
1947 TpConnection *connection;
1949 account = empathy_account_manager_ensure_account (priv->account_manager,
1951 g_assert (account != NULL);
1953 connection = empathy_account_get_connection_for_path (account,
1955 if (connection == NULL)
1957 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1958 "Invalid connection argument");
1962 for (i = 0; i < channels->len ; i++)
1964 GValueArray *arr = g_ptr_array_index (channels, i);
1965 const gchar *object_path;
1966 GHashTable *properties;
1968 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
1969 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
1971 dispatcher_connection_new_channel_with_properties (dispatcher,
1972 connection, object_path, properties);
1980 empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
1985 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1986 EmpathyHandler *handler;
1988 handler = empathy_handler_new (name, filters, capabilities);
1989 priv->handlers = g_list_prepend (priv->handlers, handler);
1991 /* Only set the handle_channels function, the Channel property on the main
1992 * handler will always report all dispatched channels even if they came from
1993 * a different Handler */
1994 empathy_handler_set_handle_channels_func (handler,
1995 empathy_dispatcher_handle_channels,
2002 empathy_dispatcher_remove_handler (EmpathyDispatcher *dispatcher,
2003 EmpathyHandler *handler)
2005 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
2008 h = g_list_find (priv->handlers, handler);
2009 g_return_if_fail (h != NULL);
2011 priv->handlers = g_list_delete_link (priv->handlers, h);
2013 g_object_unref (handler);