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/account-manager.h>
32 #include <telepathy-glib/enums.h>
33 #include <telepathy-glib/connection.h>
34 #include <telepathy-glib/util.h>
35 #include <telepathy-glib/dbus.h>
36 #include <telepathy-glib/proxy-subclass.h>
37 #include <telepathy-glib/gtypes.h>
38 #include <telepathy-glib/defs.h>
39 #include <telepathy-glib/svc-client.h>
40 #include <telepathy-glib/svc-generic.h>
41 #include <telepathy-glib/interfaces.h>
43 #include <extensions/extensions.h>
45 #include "empathy-dispatcher.h"
46 #include "empathy-handler.h"
47 #include "empathy-utils.h"
48 #include "empathy-tube-handler.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 TpAccountManager *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 *self,
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 (self);
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 if (r->pending_call != NULL)
258 tp_proxy_pending_call_cancel (r->pending_call);
260 g_slice_free (DispatcherRequestData, r);
263 static ConnectionData *
264 new_connection_data (void)
266 ConnectionData *cd = g_slice_new0 (ConnectionData);
268 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
269 g_free, (GDestroyNotify) free_dispatch_data);
271 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
272 g_free, g_object_unref);
274 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
281 free_connection_data (ConnectionData *cd)
286 g_hash_table_destroy (cd->dispatched_channels);
287 g_hash_table_destroy (cd->dispatching_channels);
289 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
291 free_dispatcher_request_data (l->data);
294 if (cd->requestable_channels != NULL)
296 for (i = 0 ; i < cd->requestable_channels->len ; i++)
298 g_ptr_array_index (cd->requestable_channels, i));
299 g_ptr_array_free (cd->requestable_channels, TRUE);
304 free_find_channel_request (FindChannelRequest *r)
309 g_object_unref (r->dispatcher);
310 g_free (r->channel_type);
312 if (r->properties != NULL)
314 for (idx = 0; idx < r->properties->len ; idx++)
316 str = g_array_index (r->properties, char *, idx);
320 g_array_free (r->properties, TRUE);
323 g_slice_free (FindChannelRequest, r);
327 dispatcher_connection_invalidated_cb (TpConnection *connection,
331 EmpathyDispatcher *self)
333 EmpathyDispatcherPriv *priv = GET_PRIV (self);
335 DEBUG ("Error: %s", message);
336 g_hash_table_remove (priv->connections, connection);
340 dispatcher_operation_can_start (EmpathyDispatcher *self,
341 EmpathyDispatchOperation *operation,
345 const gchar *channel_type =
346 empathy_dispatch_operation_get_channel_type (operation);
348 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
350 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
352 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
362 dispatch_operation_flush_requests (EmpathyDispatcher *self,
363 EmpathyDispatchOperation *operation,
369 l = cd->outstanding_requests;
372 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
377 if (d->operation == operation)
382 d->cb (NULL, error, d->user_data);
384 d->cb (operation, NULL, d->user_data);
387 cd->outstanding_requests = g_list_delete_link
388 (cd->outstanding_requests, lt);
390 free_dispatcher_request_data (d);
396 dispatcher_channel_invalidated_cb (TpProxy *proxy,
400 EmpathyDispatcher *self)
402 /* Channel went away... */
403 EmpathyDispatcherPriv *priv = GET_PRIV (self);
404 TpConnection *connection;
405 EmpathyDispatchOperation *operation;
407 const gchar *object_path;
409 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
411 cd = g_hash_table_lookup (priv->connections, connection);
412 /* Connection itself invalidated? */
416 object_path = tp_proxy_get_object_path (proxy);
418 DEBUG ("Channel %s invalidated", object_path);
420 g_hash_table_remove (cd->dispatched_channels, object_path);
421 g_hash_table_remove (cd->dispatching_channels, object_path);
423 priv->channels = g_list_remove (priv->channels, proxy);
425 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
426 if (operation != NULL)
428 GError error = { domain, code, message };
429 dispatch_operation_flush_requests (self, operation, &error, cd);
430 g_hash_table_remove (cd->outstanding_channels, object_path);
431 g_object_unref (operation);
436 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
437 EmpathyDispatcher *self)
439 g_assert (empathy_dispatch_operation_is_incoming (operation));
440 DEBUG ("Send of for dispatching: %s",
441 empathy_dispatch_operation_get_object_path (operation));
442 g_signal_emit (self, signals[DISPATCH], 0, operation);
446 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
447 EmpathyDispatcher *self)
449 /* Our job is done, remove the dispatch operation and mark the channel as
451 EmpathyDispatcherPriv *priv = GET_PRIV (self);
452 TpConnection *connection;
454 const gchar *object_path;
456 connection = empathy_dispatch_operation_get_tp_connection (operation);
457 cd = g_hash_table_lookup (priv->connections, connection);
458 g_assert (cd != NULL);
460 object_path = empathy_dispatch_operation_get_object_path (operation);
462 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
465 d = new_dispatch_data (
466 empathy_dispatch_operation_get_channel (operation),
467 empathy_dispatch_operation_get_channel_wrapper (operation));
468 g_hash_table_insert (cd->dispatched_channels,
469 g_strdup (object_path), d);
471 g_hash_table_remove (cd->dispatching_channels, object_path);
473 DEBUG ("Channel claimed: %s", object_path);
477 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
478 EmpathyDispatcher *self)
480 EmpathyDispatcherPriv *priv = GET_PRIV (self);
481 TpConnection *connection;
483 EmpathyDispatchOperationState status;
485 g_signal_connect (operation, "approved",
486 G_CALLBACK (dispatch_operation_approved_cb), self);
488 g_signal_connect (operation, "claimed",
489 G_CALLBACK (dispatch_operation_claimed_cb), self);
491 /* Signal the observers */
492 DEBUG ("Send to observers: %s",
493 empathy_dispatch_operation_get_object_path (operation));
494 g_signal_emit (self, signals[OBSERVE], 0, operation);
496 empathy_dispatch_operation_start (operation);
498 /* Signal potential requestors */
499 connection = empathy_dispatch_operation_get_tp_connection (operation);
500 cd = g_hash_table_lookup (priv->connections, connection);
501 g_assert (cd != NULL);
503 g_object_ref (operation);
506 dispatch_operation_flush_requests (self, operation, NULL, cd);
507 status = empathy_dispatch_operation_get_status (operation);
508 g_object_unref (operation);
510 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
513 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
515 DEBUG ("Send to approvers: %s",
516 empathy_dispatch_operation_get_object_path (operation));
517 g_signal_emit (self, signals[APPROVE], 0, operation);
521 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
522 DEBUG ("Send of for dispatching: %s",
523 empathy_dispatch_operation_get_object_path (operation));
524 g_signal_emit (self, signals[DISPATCH], 0, operation);
527 g_object_unref (self);
531 dispatcher_start_dispatching (EmpathyDispatcher *self,
532 EmpathyDispatchOperation *operation,
535 const gchar *object_path =
536 empathy_dispatch_operation_get_object_path (operation);
538 DEBUG ("Dispatching process started for %s", object_path);
540 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
542 g_assert (g_hash_table_lookup (cd->outstanding_channels,
543 object_path) == NULL);
545 g_hash_table_insert (cd->dispatching_channels,
546 g_strdup (object_path), operation);
548 switch (empathy_dispatch_operation_get_status (operation))
550 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
551 g_signal_connect (operation, "ready",
552 G_CALLBACK (dispatch_operation_ready_cb), self);
554 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
555 dispatch_operation_ready_cb (operation, self);
558 g_assert_not_reached ();
562 else if (empathy_dispatch_operation_get_status (operation) >=
563 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
565 /* Already dispatching and the operation is pending, thus the observers
566 * have seen it (if applicable), so we can flush the request right away.
568 dispatch_operation_flush_requests (self, operation, NULL, cd);
573 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
579 g_hash_table_iter_init (&iter, cd->outstanding_channels);
580 while (g_hash_table_iter_next (&iter, NULL, &value))
582 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
584 if (dispatcher_operation_can_start (self, operation, cd))
586 g_hash_table_iter_remove (&iter);
587 dispatcher_start_dispatching (self, operation, cd);
593 dispatcher_connection_new_channel (EmpathyDispatcher *self,
594 TpConnection *connection,
595 const gchar *object_path,
596 const gchar *channel_type,
599 GHashTable *properties,
602 EmpathyDispatcherPriv *priv = GET_PRIV (self);
605 EmpathyDispatchOperation *operation;
607 /* Channel types we never want to dispatch because they're either deprecated
608 * or can't sensibly be dispatch (e.g. channels that should always be
610 const char *blacklist[] = {
611 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
612 TP_IFACE_CHANNEL_TYPE_TUBES,
613 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
617 dispatcher_init_connection_if_needed (self, connection);
619 cd = g_hash_table_lookup (priv->connections, connection);
621 /* Don't bother with channels we have already dispatched or are dispatching
622 * currently. This can happen when NewChannel(s) is fired after
623 * RequestChannel/CreateChannel/EnsureChannel */
624 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
627 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
630 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
632 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
635 /* Only pick up non-requested text and file channels. For all other it
636 * doesn't make sense to handle it if we didn't request it. The same goes
637 * for channels we discovered by the Channels property or ListChannels */
638 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
639 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
641 DEBUG ("Ignoring incoming channel of type %s on %s",
642 channel_type, object_path);
646 for (i = 0 ; blacklist[i] != NULL; i++)
648 if (!tp_strdiff (channel_type, blacklist[i]))
650 DEBUG ("Ignoring blacklisted channel type %s on %s",
651 channel_type, object_path);
656 DEBUG ("New channel of type %s on %s", channel_type, object_path);
658 if (properties == NULL)
659 channel = tp_channel_new (connection, object_path, channel_type,
660 handle_type, handle, NULL);
662 channel = tp_channel_new_from_properties (connection, object_path,
665 g_signal_connect (channel, "invalidated",
666 G_CALLBACK (dispatcher_channel_invalidated_cb),
669 priv->channels = g_list_prepend (priv->channels, channel);
671 operation = empathy_dispatch_operation_new (connection, channel, NULL,
674 g_object_unref (channel);
678 /* Request could either be by us or by a remote party. If there are no
679 * outstanding requests for this channel type we can assume it's remote.
680 * Otherwise we wait untill they are all satisfied */
681 if (dispatcher_operation_can_start (self, operation, cd))
682 dispatcher_start_dispatching (self, operation, cd);
684 g_hash_table_insert (cd->outstanding_channels,
685 g_strdup (object_path), operation);
689 dispatcher_start_dispatching (self, operation, cd);
694 dispatcher_connection_new_channel_with_properties (
695 EmpathyDispatcher *self,
696 TpConnection *connection,
697 const gchar *object_path,
698 GHashTable *properties)
700 const gchar *channel_type;
707 channel_type = tp_asv_get_string (properties,
708 TP_IFACE_CHANNEL ".ChannelType");
709 if (channel_type == NULL)
711 g_message ("%s had an invalid ChannelType property", object_path);
715 handle_type = tp_asv_get_uint32 (properties,
716 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
719 g_message ("%s had an invalid TargetHandleType property", object_path);
723 handle = tp_asv_get_uint32 (properties,
724 TP_IFACE_CHANNEL ".TargetHandle", &valid);
727 g_message ("%s had an invalid TargetHandle property", object_path);
731 /* We assume there is no channel dispather, so we're the only one dispatching
732 * it. Which means that a requested channel it is outgoing one */
733 requested = tp_asv_get_boolean (properties,
734 TP_IFACE_CHANNEL ".Requested", &valid);
737 g_message ("%s had an invalid Requested property", object_path);
741 dispatcher_connection_new_channel (self, connection,
742 object_path, channel_type, handle_type, handle, properties, !requested);
746 dispatcher_connection_got_all (TpProxy *proxy,
747 GHashTable *properties,
752 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
753 EmpathyDispatcherPriv *priv = GET_PRIV (self);
754 GPtrArray *requestable_channels;
755 GPtrArray *existing_channels;
758 DEBUG ("Error: %s", error->message);
762 requestable_channels = tp_asv_get_boxed (properties,
763 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
765 if (requestable_channels == NULL)
766 DEBUG ("No RequestableChannelClasses property !?! on connection");
771 FindChannelRequest *request;
774 cd = g_hash_table_lookup (priv->connections, proxy);
775 g_assert (cd != NULL);
777 cd->requestable_channels = g_boxed_copy (
778 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
780 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
783 for (l = requests; l != NULL; l = l->next)
787 retval = empathy_dispatcher_find_channel_classes (self,
788 TP_CONNECTION (proxy), request->channel_type,
789 request->handle_type, request->properties);
790 request->callback (retval, request->user_data);
792 free_find_channel_request (request);
793 g_list_free (retval);
796 g_list_free (requests);
798 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
801 existing_channels = tp_asv_get_boxed (properties,
802 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
804 if (existing_channels != NULL)
808 for (idx = 0; idx < existing_channels->len; idx++)
810 GValueArray *values = g_ptr_array_index (existing_channels, idx);
811 const gchar *object_path;
814 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
815 props = g_value_get_boxed (g_value_array_get_nth (values, 1));
817 if (tp_strdiff (tp_asv_get_string (props,
818 TP_IFACE_CHANNEL ".ChannelType"),
819 TP_IFACE_CHANNEL_TYPE_TEXT))
822 dispatcher_connection_new_channel_with_properties (self,
823 TP_CONNECTION (proxy), object_path, props);
829 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
830 const GPtrArray *capabilities,
836 DEBUG ("Error: %s", error->message);
840 connection_ready_cb (TpConnection *connection,
844 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
845 GPtrArray *capabilities;
848 const gchar *remove_ = NULL;
852 DEBUG ("Error: %s", error->message);
856 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
857 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
859 tp_cli_dbus_properties_call_get_all (connection, -1,
860 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
861 dispatcher_connection_got_all,
862 NULL, NULL, G_OBJECT (self));
865 /* Advertise VoIP capabilities */
866 capabilities = g_ptr_array_sized_new (1);
867 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
868 G_TYPE_UINT, G_TYPE_INVALID);
869 g_value_init (&cap, cap_type);
870 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
871 dbus_g_type_struct_set (&cap,
872 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
873 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
874 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
875 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
876 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
877 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
879 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
881 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
882 connection, -1, capabilities, &remove_,
883 dispatcher_connection_advertise_capabilities_cb,
884 NULL, NULL, G_OBJECT (self));
886 g_value_unset (&cap);
887 g_ptr_array_free (capabilities, TRUE);
889 g_object_unref (self);
893 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
894 TpConnection *connection)
896 EmpathyDispatcherPriv *priv = GET_PRIV (self);
898 if (g_hash_table_lookup (priv->connections, connection) != NULL)
901 g_hash_table_insert (priv->connections, g_object_ref (connection),
902 new_connection_data ());
904 g_signal_connect (connection, "invalidated",
905 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
907 /* Ensure to keep the self object alive while the call_when_ready is
910 tp_connection_call_when_ready (connection, connection_ready_cb, self);
914 dispatcher_status_changed_cb (TpAccount *account,
918 gchar *dbus_error_name,
920 EmpathyDispatcher *self)
922 TpConnection *conn = tp_account_get_connection (account);
925 dispatcher_init_connection_if_needed (self, conn);
929 remove_idle_handlers (gpointer key,
935 source_id = GPOINTER_TO_UINT (value);
936 g_source_remove (source_id);
940 dispatcher_constructor (GType type,
941 guint n_construct_params,
942 GObjectConstructParam *construct_params)
945 EmpathyDispatcherPriv *priv;
947 if (dispatcher != NULL)
948 return g_object_ref (dispatcher);
950 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
951 (type, n_construct_params, construct_params);
953 dispatcher = EMPATHY_DISPATCHER (retval);
954 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
956 priv = GET_PRIV (dispatcher);
958 if (priv->handler == NULL)
959 priv->handler = empathy_handler_new (NULL, NULL, NULL);
961 empathy_handler_set_handle_channels_func (priv->handler,
962 empathy_dispatcher_handle_channels,
965 empathy_handler_set_channels_func (priv->handler,
966 empathy_dispatcher_get_channels,
973 dispatcher_dispose (GObject *object)
975 EmpathyDispatcherPriv *priv = GET_PRIV (object);
980 if (priv->dispose_has_run)
983 priv->dispose_has_run = TRUE;
985 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
986 g_object_unref (G_OBJECT (l->data));
988 g_list_free (priv->handlers);
989 priv->handlers = NULL;
991 if (priv->handler != NULL)
992 g_object_unref (priv->handler);
993 priv->handler = NULL;
995 g_hash_table_iter_init (&iter, priv->connections);
996 while (g_hash_table_iter_next (&iter, &connection, NULL))
998 g_signal_handlers_disconnect_by_func (connection,
999 dispatcher_connection_invalidated_cb, object);
1002 g_hash_table_destroy (priv->connections);
1003 priv->connections = NULL;
1005 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
1009 dispatcher_finalize (GObject *object)
1011 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1013 GHashTableIter iter;
1014 gpointer connection;
1017 if (priv->request_channel_class_async_ids != NULL)
1019 g_hash_table_foreach (priv->request_channel_class_async_ids,
1020 remove_idle_handlers, NULL);
1021 g_hash_table_destroy (priv->request_channel_class_async_ids);
1024 for (l = priv->channels; l; l = l->next)
1026 g_signal_handlers_disconnect_by_func (l->data,
1027 dispatcher_channel_invalidated_cb, object);
1030 g_list_free (priv->channels);
1032 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1033 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1035 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1039 g_object_unref (priv->account_manager);
1041 g_hash_table_destroy (priv->outstanding_classes_requests);
1045 dispatcher_set_property (GObject *object,
1047 const GValue *value,
1050 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1051 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1053 switch (property_id)
1056 priv->handler = g_value_dup_object (value);
1059 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1065 dispatcher_get_property (GObject *object,
1070 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1071 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1073 switch (property_id)
1076 g_value_set_object (value, priv->handler);
1079 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1085 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1087 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1088 GParamSpec *param_spec;
1090 object_class->dispose = dispatcher_dispose;
1091 object_class->finalize = dispatcher_finalize;
1092 object_class->constructor = dispatcher_constructor;
1094 object_class->get_property = dispatcher_get_property;
1095 object_class->set_property = dispatcher_set_property;
1097 param_spec = g_param_spec_object ("handler", "handler",
1098 "The main Telepathy Client Hander object",
1099 EMPATHY_TYPE_HANDLER,
1100 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1101 g_object_class_install_property (object_class,
1102 PROP_HANDLER, param_spec);
1105 g_signal_new ("observe",
1106 G_TYPE_FROM_CLASS (klass),
1110 g_cclosure_marshal_VOID__OBJECT,
1112 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1115 g_signal_new ("approve",
1116 G_TYPE_FROM_CLASS (klass),
1120 g_cclosure_marshal_VOID__OBJECT,
1122 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1125 g_signal_new ("dispatch",
1126 G_TYPE_FROM_CLASS (klass),
1130 g_cclosure_marshal_VOID__OBJECT,
1132 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1135 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1139 connect_account (EmpathyDispatcher *self,
1142 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1143 TpConnection *conn = tp_account_get_connection (account);
1145 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1147 id = g_signal_connect (account, "status-changed",
1148 G_CALLBACK (dispatcher_status_changed_cb), self);
1152 account_manager_prepared_cb (GObject *source_object,
1153 GAsyncResult *result,
1156 GList *accounts, *l;
1157 EmpathyDispatcher *self = user_data;
1158 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1159 GError *error = NULL;
1161 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1163 DEBUG ("Failed to prepare account manager: %s", error->message);
1164 g_error_free (error);
1168 accounts = tp_account_manager_get_valid_accounts (account_manager);
1169 for (l = accounts; l; l = l->next)
1171 TpAccount *a = l->data;
1173 connect_account (self, a);
1175 g_list_free (accounts);
1179 empathy_dispatcher_init (EmpathyDispatcher *self)
1181 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1182 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1185 priv->account_manager = tp_account_manager_dup ();
1187 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1188 g_object_unref, (GDestroyNotify) free_connection_data);
1190 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1191 g_direct_equal, g_object_unref, NULL);
1193 priv->channels = NULL;
1195 tp_account_manager_prepare_async (priv->account_manager, NULL,
1196 account_manager_prepared_cb, self);
1198 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1203 empathy_dispatcher_new (const gchar *name,
1207 EmpathyHandler *handler;
1208 EmpathyDispatcher *ret;
1210 g_assert (dispatcher == NULL);
1211 handler = empathy_handler_new (name, filters, capabilities);
1213 ret = EMPATHY_DISPATCHER (
1214 g_object_new (EMPATHY_TYPE_DISPATCHER,
1217 g_object_unref (handler);
1223 empathy_dispatcher_dup_singleton (void)
1225 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1229 dispatcher_request_failed (EmpathyDispatcher *self,
1230 DispatcherRequestData *request_data,
1231 const GError *error)
1233 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1234 ConnectionData *conn_data;
1236 conn_data = g_hash_table_lookup (priv->connections,
1237 request_data->connection);
1238 if (request_data->cb != NULL)
1239 request_data->cb (NULL, error, request_data->user_data);
1241 if (conn_data != NULL)
1243 conn_data->outstanding_requests =
1244 g_list_remove (conn_data->outstanding_requests, request_data);
1246 /* else Connection has been invalidated */
1248 free_dispatcher_request_data (request_data);
1252 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1253 DispatcherRequestData *request_data,
1254 const gchar *object_path,
1255 GHashTable *properties,
1256 const GError *error)
1258 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1259 EmpathyDispatchOperation *operation = NULL;
1260 ConnectionData *conn_data;
1262 conn_data = g_hash_table_lookup (priv->connections,
1263 request_data->connection);
1267 DEBUG ("Channel request failed: %s", error->message);
1269 dispatcher_request_failed (self, request_data, error);
1274 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1277 if (operation != NULL)
1278 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1280 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1283 if (operation == NULL)
1285 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1290 operation = empathy_dispatch_operation_new_with_wrapper (
1291 request_data->connection,
1292 data->channel, request_data->contact, FALSE,
1293 data->channel_wrapper);
1299 if (properties != NULL)
1300 channel = tp_channel_new_from_properties (request_data->connection,
1301 object_path, properties, NULL);
1303 channel = tp_channel_new (request_data->connection, object_path,
1304 request_data->channel_type, request_data->handle_type,
1305 request_data->handle, NULL);
1307 g_signal_connect (channel, "invalidated",
1308 G_CALLBACK (dispatcher_channel_invalidated_cb),
1309 request_data->dispatcher);
1311 priv->channels = g_list_prepend (priv->channels, channel);
1313 operation = empathy_dispatch_operation_new (request_data->connection,
1314 channel, request_data->contact, FALSE);
1315 g_object_unref (channel);
1320 /* Already existed set potential extra information */
1321 g_object_set (G_OBJECT (operation),
1322 "contact", request_data->contact,
1326 request_data->operation = operation;
1328 /* (pre)-approve this right away as we requested it
1329 * This might cause the channel to be claimed, in which case the operation
1330 * will disappear. So ref it, and check the status before starting the
1333 g_object_ref (operation);
1334 empathy_dispatch_operation_approve (operation);
1336 if (empathy_dispatch_operation_get_status (operation) <
1337 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1338 dispatcher_start_dispatching (request_data->dispatcher, operation,
1341 g_object_unref (operation);
1344 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1349 dispatcher_request_channel_cb (TpConnection *connection,
1350 const gchar *object_path,
1351 const GError *error,
1353 GObject *weak_object)
1355 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1356 EmpathyDispatcher *self =
1357 EMPATHY_DISPATCHER (request_data->dispatcher);
1359 request_data->pending_call = NULL;
1361 dispatcher_connection_new_requested_channel (self,
1362 request_data, object_path, NULL, error);
1366 dispatcher_request_channel (DispatcherRequestData *request_data)
1368 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1369 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1371 /* Extend the request_data to be a valid request */
1372 g_assert (request_data->request == NULL);
1373 request_data->request = tp_asv_new (
1374 TP_IFACE_CHANNEL ".ChannelType",
1375 G_TYPE_STRING, request_data->channel_type,
1376 TP_IFACE_CHANNEL ".TargetHandleType",
1377 G_TYPE_UINT, request_data->handle_type,
1380 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1381 tp_asv_set_uint32 (request_data->request,
1382 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1384 empathy_dispatcher_call_create_or_ensure_channel (
1385 request_data->dispatcher, request_data);
1389 request_data->pending_call = tp_cli_connection_call_request_channel (
1390 request_data->connection, -1,
1391 request_data->channel_type,
1392 request_data->handle_type,
1393 request_data->handle,
1394 TRUE, dispatcher_request_channel_cb,
1395 request_data, NULL, NULL);
1400 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1401 EmpathyDispatcherRequestCb *callback,
1404 EmpathyDispatcher *self;
1405 EmpathyDispatcherPriv *priv;
1406 TpConnection *connection;
1407 ConnectionData *connection_data;
1408 DispatcherRequestData *request_data;
1410 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1412 self = empathy_dispatcher_dup_singleton ();
1413 priv = GET_PRIV (self);
1415 connection = empathy_contact_get_connection (contact);
1416 connection_data = g_hash_table_lookup (priv->connections, connection);
1418 /* The contact handle might not be known yet */
1419 request_data = new_dispatcher_request_data (self, connection,
1420 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1421 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1422 request_data->should_ensure = TRUE;
1424 connection_data->outstanding_requests = g_list_prepend
1425 (connection_data->outstanding_requests, request_data);
1427 dispatcher_request_channel (request_data);
1429 g_object_unref (self);
1434 EmpathyDispatcher *dispatcher;
1435 EmpathyDispatcherRequestCb *callback;
1437 } ChatWithContactIdData;
1440 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1441 EmpathyContact *contact,
1442 const GError *error,
1444 GObject *weak_object)
1446 ChatWithContactIdData *data = user_data;
1450 /* FIXME: Should call data->callback with the error */
1451 DEBUG ("Error: %s", error->message);
1455 empathy_dispatcher_chat_with_contact (contact, data->callback,
1459 g_object_unref (data->dispatcher);
1460 g_slice_free (ChatWithContactIdData, data);
1464 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1465 const gchar *contact_id,
1466 EmpathyDispatcherRequestCb *callback,
1469 EmpathyDispatcher *self;
1470 EmpathyTpContactFactory *factory;
1471 ChatWithContactIdData *data;
1473 g_return_if_fail (TP_IS_CONNECTION (connection));
1474 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1476 self = empathy_dispatcher_dup_singleton ();
1477 factory = empathy_tp_contact_factory_dup_singleton (connection);
1478 data = g_slice_new0 (ChatWithContactIdData);
1479 data->dispatcher = self;
1480 data->callback = callback;
1481 data->user_data = user_data;
1482 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1483 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1485 g_object_unref (factory);
1489 dispatcher_request_handles_cb (TpConnection *connection,
1490 const GArray *handles,
1491 const GError *error,
1495 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1497 request_data->pending_call = NULL;
1501 EmpathyDispatcher *self = request_data->dispatcher;
1502 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1505 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1507 if (request_data->cb)
1508 request_data->cb (NULL, error, request_data->user_data);
1510 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1513 free_dispatcher_request_data (request_data);
1515 dispatcher_flush_outstanding_operations (self, cd);
1519 request_data->handle = g_array_index (handles, guint, 0);
1520 dispatcher_request_channel (request_data);
1524 empathy_dispatcher_join_muc (TpConnection *connection,
1525 const gchar *roomname,
1526 EmpathyDispatcherRequestCb *callback,
1529 EmpathyDispatcher *self;
1530 EmpathyDispatcherPriv *priv;
1531 DispatcherRequestData *request_data;
1532 ConnectionData *connection_data;
1533 const gchar *names[] = { roomname, NULL };
1535 g_return_if_fail (TP_IS_CONNECTION (connection));
1536 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1538 self = empathy_dispatcher_dup_singleton ();
1539 priv = GET_PRIV (self);
1541 connection_data = g_hash_table_lookup (priv->connections, connection);
1542 g_assert (connection_data != NULL);
1544 /* Don't know the room handle yet */
1545 request_data = new_dispatcher_request_data (self, connection,
1546 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1547 NULL, callback, user_data);
1548 request_data->should_ensure = TRUE;
1550 connection_data->outstanding_requests = g_list_prepend
1551 (connection_data->outstanding_requests, request_data);
1553 request_data->pending_call = tp_cli_connection_call_request_handles (
1555 TP_HANDLE_TYPE_ROOM, names,
1556 dispatcher_request_handles_cb, request_data, NULL, NULL);
1558 g_object_unref (self);
1562 dispatcher_create_channel_cb (TpConnection *connect,
1563 const gchar *object_path,
1564 GHashTable *properties,
1565 const GError *error,
1567 GObject *weak_object)
1569 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1570 EmpathyDispatcher *self =
1571 EMPATHY_DISPATCHER (request_data->dispatcher);
1573 request_data->pending_call = NULL;
1575 dispatcher_connection_new_requested_channel (self,
1576 request_data, object_path, properties, error);
1580 dispatcher_ensure_channel_cb (TpConnection *connect,
1582 const gchar *object_path,
1583 GHashTable *properties,
1584 const GError *error,
1586 GObject *weak_object)
1588 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1589 EmpathyDispatcher *self =
1590 EMPATHY_DISPATCHER (request_data->dispatcher);
1592 request_data->pending_call = NULL;
1594 dispatcher_connection_new_requested_channel (self,
1595 request_data, object_path, properties, error);
1599 empathy_dispatcher_call_create_or_ensure_channel (
1600 EmpathyDispatcher *self,
1601 DispatcherRequestData *request_data)
1603 if (request_data->should_ensure)
1605 request_data->pending_call =
1606 tp_cli_connection_interface_requests_call_ensure_channel (
1607 request_data->connection, -1,
1608 request_data->request, dispatcher_ensure_channel_cb,
1609 request_data, NULL, NULL);
1613 request_data->pending_call =
1614 tp_cli_connection_interface_requests_call_create_channel (
1615 request_data->connection, -1,
1616 request_data->request, dispatcher_create_channel_cb,
1617 request_data, NULL, NULL);
1622 * empathy_dispatcher_create_channel:
1623 * @self: the EmpathyDispatcher
1624 * @connection: the Connection to dispatch on
1625 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1626 * @callback: a callback for when the channel arrives (or NULL)
1627 * @user_data: optional user data (or NULL)
1629 * When calling this function, #EmpathyDispatcher takes ownership of your
1630 * reference to @request. DO NOT unref or destroy @request. When the request is
1631 * done, @request will be unreferenced. Take another reference if you want to
1635 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1636 TpConnection *connection,
1637 GHashTable *request,
1638 EmpathyDispatcherRequestCb *callback,
1641 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1642 ConnectionData *connection_data;
1643 DispatcherRequestData *request_data;
1644 const gchar *channel_type;
1649 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1650 g_return_if_fail (TP_IS_CONNECTION (connection));
1651 g_return_if_fail (request != NULL);
1653 connection_data = g_hash_table_lookup (priv->connections, connection);
1654 g_assert (connection_data != NULL);
1656 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1658 handle_type = tp_asv_get_uint32 (request,
1659 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1661 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1663 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1665 request_data = new_dispatcher_request_data (self, connection,
1666 channel_type, handle_type, handle, request,
1667 NULL, callback, user_data);
1669 connection_data->outstanding_requests = g_list_prepend
1670 (connection_data->outstanding_requests, request_data);
1672 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1676 channel_class_matches (GValueArray *class,
1677 const char *channel_type,
1679 GArray *fixed_properties)
1687 v = g_value_array_get_nth (class, 0);
1689 /* if the class doesn't match channel type discard it. */
1690 fprops = g_value_get_boxed (v);
1691 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1693 if (tp_strdiff (channel_type, c_type))
1696 /* we have the right channel type, see if the handle type matches */
1697 h_type = tp_asv_get_uint32 (fprops,
1698 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1700 if (!valid || handle_type != h_type)
1703 if (fixed_properties != NULL)
1705 gpointer h_key, h_val;
1707 GHashTableIter iter;
1710 g_hash_table_iter_init (&iter, fprops);
1712 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1714 /* discard ChannelType and TargetHandleType, as we already
1717 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1719 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1724 for (idx = 0; idx < fixed_properties->len; idx++)
1726 /* if |key| doesn't exist in |fixed_properties|, discard
1731 g_array_index (fixed_properties, char *, idx)))
1734 /* exit the for() loop */
1745 /* if no fixed_properties are specified, discard the classes
1746 * with some fixed properties other than the two we already
1749 if (g_hash_table_size (fprops) > 2)
1757 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1758 TpConnection *connection,
1759 const gchar *channel_type,
1761 GArray *fixed_properties)
1763 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1766 GList *matching_classes;
1770 g_return_val_if_fail (channel_type != NULL, NULL);
1771 g_return_val_if_fail (handle_type != 0, NULL);
1773 cd = g_hash_table_lookup (priv->connections, connection);
1778 classes = cd->requestable_channels;
1779 if (classes == NULL)
1782 matching_classes = NULL;
1784 for (i = 0; i < classes->len; i++)
1786 class = g_ptr_array_index (classes, i);
1788 if (!channel_class_matches
1789 (class, channel_type, handle_type, fixed_properties))
1792 matching_classes = g_list_prepend (matching_classes, class);
1795 return matching_classes;
1799 find_channel_class_idle_cb (gpointer user_data)
1803 FindChannelRequest *request = user_data;
1805 gboolean is_ready = TRUE;
1806 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1808 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1810 cd = g_hash_table_lookup (priv->connections, request->connection);
1814 else if (cd->requestable_channels == NULL)
1819 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1820 request->connection, request->channel_type, request->handle_type,
1821 request->properties);
1823 request->callback (retval, request->user_data);
1824 free_find_channel_request (request);
1825 g_list_free (retval);
1830 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1831 request->connection);
1832 requests = g_list_prepend (requests, request);
1834 g_hash_table_insert (priv->outstanding_classes_requests,
1835 request->connection, requests);
1841 setup_varargs (va_list var_args,
1842 const char *channel_namespace,
1843 const char *first_property_name)
1849 if (first_property_name == NULL)
1852 name = first_property_name;
1853 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1855 while (name != NULL)
1857 name_full = g_strdup (name);
1858 properties = g_array_append_val (properties, name_full);
1859 name = va_arg (var_args, char *);
1866 * empathy_dispatcher_find_requestable_channel_classes:
1867 * @dispatcher: an #EmpathyDispatcher
1868 * @connection: a #TpConnection
1869 * @channel_type: a string identifying the type of the channel to lookup
1870 * @handle_type: the handle type for the channel
1871 * @first_property_name: %NULL, or the name of the first fixed property,
1872 * followed optionally by more names, followed by %NULL.
1874 * Returns all the channel classes that a client can request for the connection
1875 * @connection, of the type identified by @channel_type, @handle_type and the
1876 * fixed properties list.
1877 * The classes which are compatible with a fixed properties list (i.e. those
1878 * that will be returned by this function) are intended as those that do not
1879 * contain any fixed property other than those in the list; note that this
1880 * doesn't guarantee that all the classes compatible with the list will contain
1881 * all the requested fixed properties, so the clients will have to filter
1882 * the returned list themselves.
1883 * If @first_property_name is %NULL, only the classes with no other fixed
1884 * properties than ChannelType and TargetHandleType will be returned.
1885 * Note that this function may return %NULL without performing any lookup if
1886 * @connection is not ready. To ensure that @connection is always ready,
1887 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1889 * Return value: a #GList of #GValueArray objects, where the first element in
1890 * the array is a #GHashTable of the fixed properties, and the second is
1891 * a #GStrv of the allowed properties for the class. The list should be free'd
1892 * with g_list_free() when done, but the objects inside the list are owned
1893 * by the #EmpathyDispatcher and must not be modified.
1896 empathy_dispatcher_find_requestable_channel_classes
1897 (EmpathyDispatcher *self,
1898 TpConnection *connection,
1899 const gchar *channel_type,
1901 const char *first_property_name,
1906 EmpathyDispatcherPriv *priv;
1911 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1912 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1913 g_return_val_if_fail (channel_type != NULL, NULL);
1914 g_return_val_if_fail (handle_type != 0, NULL);
1916 priv = GET_PRIV (self);
1918 va_start (var_args, first_property_name);
1920 properties = setup_varargs (var_args, channel_type, first_property_name);
1924 retval = empathy_dispatcher_find_channel_classes (self, connection,
1925 channel_type, handle_type, properties);
1927 if (properties != NULL)
1929 /* free the properties array */
1930 for (idx = 0; idx < properties->len ; idx++)
1932 str = g_array_index (properties, char *, idx);
1936 g_array_free (properties, TRUE);
1943 * empathy_dispatcher_find_requestable_channel_classes_async:
1944 * @dispatcher: an #EmpathyDispatcher
1945 * @connection: a #TpConnection
1946 * @channel_type: a string identifying the type of the channel to lookup
1947 * @handle_type: the handle type for the channel
1948 * @callback: the callback to call when @connection is ready
1949 * @user_data: the user data to pass to @callback
1950 * @first_property_name: %NULL, or the name of the first fixed property,
1951 * followed optionally by more names, followed by %NULL.
1953 * Please see the documentation of
1954 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1955 * description of this function.
1958 empathy_dispatcher_find_requestable_channel_classes_async
1959 (EmpathyDispatcher *self,
1960 TpConnection *connection,
1961 const gchar *channel_type,
1963 EmpathyDispatcherFindChannelClassCb callback,
1965 const char *first_property_name,
1970 FindChannelRequest *request;
1971 EmpathyDispatcherPriv *priv;
1974 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1975 g_return_if_fail (TP_IS_CONNECTION (connection));
1976 g_return_if_fail (channel_type != NULL);
1977 g_return_if_fail (handle_type != 0);
1979 priv = GET_PRIV (self);
1981 va_start (var_args, first_property_name);
1983 properties = setup_varargs (var_args, channel_type, first_property_name);
1987 /* append another request for this connection */
1988 request = g_slice_new0 (FindChannelRequest);
1989 request->dispatcher = g_object_ref (self);
1990 request->channel_type = g_strdup (channel_type);
1991 request->handle_type = handle_type;
1992 request->connection = connection;
1993 request->callback = callback;
1994 request->user_data = user_data;
1995 request->properties = properties;
1997 source_id = g_idle_add (find_channel_class_idle_cb, request);
1999 g_hash_table_insert (priv->request_channel_class_async_ids,
2000 request, GUINT_TO_POINTER (source_id));
2004 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2007 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2008 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2010 return priv->channels;
2014 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2015 const gchar *account_path,
2016 const gchar *connection_path,
2017 const GPtrArray *channels,
2018 const GPtrArray *requests_satisfied,
2020 GHashTable *handler_info,
2024 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2025 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2028 TpConnection *connection;
2030 /* FIXME: should probably find out whether the account manager is prepared
2031 * before ensuring. See bug #600111. */
2032 account = tp_account_manager_ensure_account (priv->account_manager,
2034 g_assert (account != NULL);
2036 connection = tp_account_ensure_connection (account, connection_path);
2037 if (connection == NULL)
2039 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2040 "Invalid connection argument");
2044 for (i = 0; i < channels->len ; i++)
2046 GValueArray *arr = g_ptr_array_index (channels, i);
2047 const gchar *object_path;
2048 GHashTable *properties;
2050 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2051 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2053 dispatcher_connection_new_channel_with_properties (self,
2054 connection, object_path, properties);
2062 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2067 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2068 EmpathyHandler *handler;
2070 handler = empathy_handler_new (name, filters, capabilities);
2071 priv->handlers = g_list_prepend (priv->handlers, handler);
2073 /* Only set the handle_channels function, the Channel property on the main
2074 * handler will always report all dispatched channels even if they came from
2075 * a different Handler */
2076 empathy_handler_set_handle_channels_func (handler,
2077 empathy_dispatcher_handle_channels, self);
2083 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2084 EmpathyHandler *handler)
2086 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2089 h = g_list_find (priv->handlers, handler);
2090 g_return_if_fail (h != NULL);
2092 priv->handlers = g_list_delete_link (priv->handlers, h);
2094 g_object_unref (handler);