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;
132 EmpathyContact *contact;
134 /* Properties to pass to the channel when requesting it */
136 EmpathyDispatcherRequestCb *cb;
138 gpointer *request_data;
139 } DispatcherRequestData;
144 /* Channel type specific wrapper object */
145 GObject *channel_wrapper;
150 /* ObjectPath => DispatchData.. */
151 GHashTable *dispatched_channels;
152 /* ObjectPath -> EmpathyDispatchOperations */
153 GHashTable *dispatching_channels;
155 /* ObjectPath -> EmpathyDispatchOperations
157 * This holds channels which were announced with NewChannel while we have an
158 * outstanding channel request for a channel of this type. On the Requests
159 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
160 * return before NewChannels is emitted, but there was no guarantee of the
161 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
162 * held in limbo here until we know whether they were requested.
164 GHashTable *outstanding_channels;
165 /* List of DispatcherRequestData */
166 GList *outstanding_requests;
167 /* List of requestable channel classes */
168 GPtrArray *requestable_channels;
173 EmpathyDispatcher *dispatcher;
174 TpConnection *connection;
178 EmpathyDispatcherFindChannelClassCb *callback;
180 } FindChannelRequest;
183 empathy_dispatcher_call_create_channel (EmpathyDispatcher *dispatcher,
184 DispatcherRequestData *request_data);
186 static DispatchData *
187 new_dispatch_data (TpChannel *channel,
188 GObject *channel_wrapper)
190 DispatchData *d = g_slice_new0 (DispatchData);
191 d->channel = g_object_ref (channel);
192 if (channel_wrapper != NULL)
193 d->channel_wrapper = g_object_ref (channel_wrapper);
199 free_dispatch_data (DispatchData *data)
201 g_object_unref (data->channel);
202 if (data->channel_wrapper != NULL)
203 g_object_unref (data->channel_wrapper);
205 g_slice_free (DispatchData, data);
208 static DispatcherRequestData *
209 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
210 TpConnection *connection,
211 const gchar *channel_type,
215 EmpathyContact *contact,
216 EmpathyDispatcherRequestCb *cb,
219 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
221 result->dispatcher = g_object_ref (dispatcher);
222 result->connection = connection;
224 result->channel_type = g_strdup (channel_type);
225 result->handle_type = handle_type;
226 result->handle = handle;
227 result->request = request;
230 result->contact = g_object_ref (contact);
233 result->user_data = user_data;
239 free_dispatcher_request_data (DispatcherRequestData *r)
241 g_free (r->channel_type);
243 if (r->dispatcher != NULL)
244 g_object_unref (r->dispatcher);
246 if (r->contact != NULL)
247 g_object_unref (r->contact);
249 if (r->request != NULL)
250 g_hash_table_unref (r->request);
252 g_slice_free (DispatcherRequestData, r);
255 static ConnectionData *
256 new_connection_data (void)
258 ConnectionData *cd = g_slice_new0 (ConnectionData);
260 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
261 g_free, (GDestroyNotify) free_dispatch_data);
263 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
264 g_free, g_object_unref);
266 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
273 free_connection_data (ConnectionData *cd)
277 g_hash_table_destroy (cd->dispatched_channels);
278 g_hash_table_destroy (cd->dispatching_channels);
281 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
283 free_dispatcher_request_data (l->data);
286 if (cd->requestable_channels != NULL)
288 for (i = 0 ; i < cd->requestable_channels->len ; i++)
290 g_ptr_array_index (cd->requestable_channels, i));
291 g_ptr_array_free (cd->requestable_channels, TRUE);
296 free_find_channel_request (FindChannelRequest *r)
301 g_object_unref (r->dispatcher);
302 g_free (r->channel_type);
304 if (r->properties != NULL)
306 for (idx = 0; idx < r->properties->len ; idx++)
308 str = g_array_index (r->properties, char *, idx);
312 g_array_free (r->properties, TRUE);
315 g_slice_free (FindChannelRequest, r);
319 dispatcher_connection_invalidated_cb (TpConnection *connection,
323 EmpathyDispatcher *dispatcher)
325 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
327 DEBUG ("Error: %s", message);
328 g_hash_table_remove (priv->connections, connection);
332 dispatcher_operation_can_start (EmpathyDispatcher *self,
333 EmpathyDispatchOperation *operation,
337 const gchar *channel_type =
338 empathy_dispatch_operation_get_channel_type (operation);
340 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
342 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
344 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
354 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
355 EmpathyDispatchOperation *operation,
361 l = cd->outstanding_requests;
364 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
369 if (d->operation == operation)
374 d->cb (NULL, error, d->user_data);
376 d->cb (operation, NULL, d->user_data);
379 cd->outstanding_requests = g_list_delete_link
380 (cd->outstanding_requests, lt);
382 free_dispatcher_request_data (d);
388 dispatcher_channel_invalidated_cb (TpProxy *proxy,
392 EmpathyDispatcher *dispatcher)
394 /* Channel went away... */
395 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
396 TpConnection *connection;
397 EmpathyDispatchOperation *operation;
399 const gchar *object_path;
401 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
403 cd = g_hash_table_lookup (priv->connections, connection);
404 /* Connection itself invalidated? */
408 object_path = tp_proxy_get_object_path (proxy);
410 DEBUG ("Channel %s invalidated", object_path);
412 g_hash_table_remove (cd->dispatched_channels, object_path);
413 g_hash_table_remove (cd->dispatching_channels, object_path);
415 priv->channels = g_list_remove (priv->channels, proxy);
417 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
418 if (operation != NULL)
420 GError error = { domain, code, message };
421 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
422 g_hash_table_remove (cd->outstanding_channels, object_path);
423 g_object_unref (operation);
428 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
429 EmpathyDispatcher *dispatcher)
431 g_assert (empathy_dispatch_operation_is_incoming (operation));
432 DEBUG ("Send of for dispatching: %s",
433 empathy_dispatch_operation_get_object_path (operation));
434 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
438 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
439 EmpathyDispatcher *dispatcher)
441 /* Our job is done, remove the dispatch operation and mark the channel as
443 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
444 TpConnection *connection;
446 const gchar *object_path;
448 connection = empathy_dispatch_operation_get_tp_connection (operation);
449 cd = g_hash_table_lookup (priv->connections, connection);
450 g_assert (cd != NULL);
452 object_path = empathy_dispatch_operation_get_object_path (operation);
454 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
457 d = new_dispatch_data (
458 empathy_dispatch_operation_get_channel (operation),
459 empathy_dispatch_operation_get_channel_wrapper (operation));
460 g_hash_table_insert (cd->dispatched_channels,
461 g_strdup (object_path), d);
463 g_hash_table_remove (cd->dispatching_channels, object_path);
465 DEBUG ("Channel claimed: %s", object_path);
469 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
470 EmpathyDispatcher *dispatcher)
472 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
473 TpConnection *connection;
475 EmpathyDispatchOperationState status;
477 g_signal_connect (operation, "approved",
478 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
480 g_signal_connect (operation, "claimed",
481 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
483 /* Signal the observers */
484 DEBUG ("Send to observers: %s",
485 empathy_dispatch_operation_get_object_path (operation));
486 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
488 empathy_dispatch_operation_start (operation);
490 /* Signal potential requestors */
491 connection = empathy_dispatch_operation_get_tp_connection (operation);
492 cd = g_hash_table_lookup (priv->connections, connection);
493 g_assert (cd != NULL);
495 g_object_ref (operation);
496 g_object_ref (dispatcher);
498 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
499 status = empathy_dispatch_operation_get_status (operation);
500 g_object_unref (operation);
502 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
505 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
507 DEBUG ("Send to approvers: %s",
508 empathy_dispatch_operation_get_object_path (operation));
509 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
513 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
514 DEBUG ("Send of for dispatching: %s",
515 empathy_dispatch_operation_get_object_path (operation));
516 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
519 g_object_unref (dispatcher);
523 dispatcher_start_dispatching (EmpathyDispatcher *self,
524 EmpathyDispatchOperation *operation,
527 const gchar *object_path =
528 empathy_dispatch_operation_get_object_path (operation);
530 DEBUG ("Dispatching process started for %s", object_path);
532 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
534 g_assert (g_hash_table_lookup (cd->outstanding_channels,
535 object_path) == NULL);
537 g_hash_table_insert (cd->dispatching_channels,
538 g_strdup (object_path), operation);
540 switch (empathy_dispatch_operation_get_status (operation))
542 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
543 g_signal_connect (operation, "ready",
544 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
546 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
547 dispatch_operation_ready_cb (operation, dispatcher);
550 g_assert_not_reached ();
554 else if (empathy_dispatch_operation_get_status (operation) >=
555 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
557 /* Already dispatching and the operation is pending, thus the observers
558 * have seen it (if applicable), so we can flush the request right away.
560 dispatch_operation_flush_requests (self, operation, NULL, cd);
565 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
571 g_hash_table_iter_init (&iter, cd->outstanding_channels);
572 while (g_hash_table_iter_next (&iter, NULL, &value))
574 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
576 if (dispatcher_operation_can_start (self, operation, cd))
578 g_hash_table_iter_remove (&iter);
579 dispatcher_start_dispatching (dispatcher, operation, cd);
585 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
586 TpConnection *connection,
587 const gchar *object_path,
588 const gchar *channel_type,
591 GHashTable *properties,
594 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
597 EmpathyDispatchOperation *operation;
599 /* Channel types we never want to dispatch because they're either deprecated
600 * or can't sensibly be dispatch (e.g. channels that should always be
602 const char *blacklist[] = {
603 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
604 TP_IFACE_CHANNEL_TYPE_TUBES,
605 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
609 dispatcher_init_connection_if_needed (dispatcher, connection);
611 cd = g_hash_table_lookup (priv->connections, connection);
613 /* Don't bother with channels we have already dispatched or are dispatching
614 * currently. This can happen when NewChannel(s) is fired after
615 * RequestChannel/CreateChannel/EnsureChannel */
616 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
619 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
622 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
624 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
627 /* Only pick up non-requested text and file channels. For all other it
628 * doesn't make sense to handle it if we didn't request it. The same goes
629 * for channels we discovered by the Channels property or ListChannels */
630 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
631 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
633 DEBUG ("Ignoring incoming channel of type %s on %s",
634 channel_type, object_path);
638 for (i = 0 ; blacklist[i] != NULL; i++)
640 if (!tp_strdiff (channel_type, blacklist[i]))
642 DEBUG ("Ignoring blacklisted channel type %s on %s",
643 channel_type, object_path);
648 DEBUG ("New channel of type %s on %s", channel_type, object_path);
650 if (properties == NULL)
651 channel = tp_channel_new (connection, object_path, channel_type,
652 handle_type, handle, NULL);
654 channel = tp_channel_new_from_properties (connection, object_path,
657 g_signal_connect (channel, "invalidated",
658 G_CALLBACK (dispatcher_channel_invalidated_cb),
661 priv->channels = g_list_prepend (priv->channels, channel);
663 operation = empathy_dispatch_operation_new (connection, channel, NULL,
666 g_object_unref (channel);
670 /* Request could either be by us or by a remote party. If there are no
671 * outstanding requests for this channel type we can assume it's remote.
672 * Otherwise we wait untill they are all satisfied */
673 if (dispatcher_operation_can_start (dispatcher, operation, cd))
674 dispatcher_start_dispatching (dispatcher, operation, cd);
676 g_hash_table_insert (cd->outstanding_channels,
677 g_strdup (object_path), operation);
681 dispatcher_start_dispatching (dispatcher, operation, cd);
686 dispatcher_connection_new_channel_with_properties (
687 EmpathyDispatcher *dispatcher,
688 TpConnection *connection,
689 const gchar *object_path,
690 GHashTable *properties)
692 const gchar *channel_type;
699 channel_type = tp_asv_get_string (properties,
700 TP_IFACE_CHANNEL ".ChannelType");
701 if (channel_type == NULL)
703 g_message ("%s had an invalid ChannelType property", object_path);
707 handle_type = tp_asv_get_uint32 (properties,
708 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
711 g_message ("%s had an invalid TargetHandleType property", object_path);
715 handle = tp_asv_get_uint32 (properties,
716 TP_IFACE_CHANNEL ".TargetHandle", &valid);
719 g_message ("%s had an invalid TargetHandle property", object_path);
723 /* We assume there is no channel dispather, so we're the only one dispatching
724 * it. Which means that a requested channel it is outgoing one */
725 requested = tp_asv_get_boolean (properties,
726 TP_IFACE_CHANNEL ".Requested", &valid);
729 g_message ("%s had an invalid Requested property", object_path);
733 dispatcher_connection_new_channel (dispatcher, connection,
734 object_path, channel_type, handle_type, handle, properties, !requested);
738 dispatcher_connection_got_all (TpProxy *proxy,
739 GHashTable *properties,
744 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
745 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
746 GPtrArray *requestable_channels;
749 DEBUG ("Error: %s", error->message);
753 requestable_channels = tp_asv_get_boxed (properties,
754 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
756 if (requestable_channels == NULL)
757 DEBUG ("No RequestableChannelClasses property !?! on connection");
762 FindChannelRequest *request;
765 cd = g_hash_table_lookup (priv->connections, proxy);
766 g_assert (cd != NULL);
768 cd->requestable_channels = g_boxed_copy (
769 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
771 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
774 for (l = requests; l != NULL; l = l->next)
778 retval = empathy_dispatcher_find_channel_classes (dispatcher,
779 TP_CONNECTION (proxy), request->channel_type,
780 request->handle_type, request->properties);
781 request->callback (retval, request->user_data);
783 free_find_channel_request (request);
784 g_list_free (retval);
787 g_list_free (requests);
789 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
794 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
795 const GPtrArray *capabilities,
801 DEBUG ("Error: %s", error->message);
805 dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
806 TpConnection *connection)
808 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
809 GPtrArray *capabilities;
812 const gchar *remove = NULL;
814 if (g_hash_table_lookup (priv->connections, connection) != NULL)
817 g_hash_table_insert (priv->connections, g_object_ref (connection),
818 new_connection_data ());
820 g_signal_connect (connection, "invalidated",
821 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
823 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
824 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
826 tp_cli_dbus_properties_call_get_all (connection, -1,
827 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
828 dispatcher_connection_got_all,
829 NULL, NULL, G_OBJECT (dispatcher));
832 /* Advertise VoIP capabilities */
833 capabilities = g_ptr_array_sized_new (1);
834 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
835 G_TYPE_UINT, G_TYPE_INVALID);
836 g_value_init (&cap, cap_type);
837 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
838 dbus_g_type_struct_set (&cap,
839 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
840 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
841 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
842 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
843 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
844 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
846 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
847 connection, -1, capabilities, &remove,
848 dispatcher_connection_advertise_capabilities_cb,
849 NULL, NULL, G_OBJECT (dispatcher));
851 g_value_unset (&cap);
852 g_ptr_array_free (capabilities, TRUE);
856 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
857 TpConnection *connection,
858 EmpathyDispatcher *dispatcher)
860 dispatcher_init_connection_if_needed (dispatcher, connection);
864 remove_idle_handlers (gpointer key,
870 source_id = GPOINTER_TO_UINT (value);
871 g_source_remove (source_id);
875 dispatcher_constructor (GType type,
876 guint n_construct_params,
877 GObjectConstructParam *construct_params)
880 EmpathyDispatcherPriv *priv;
882 if (dispatcher != NULL)
883 return g_object_ref (dispatcher);
885 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
886 (type, n_construct_params, construct_params);
888 dispatcher = EMPATHY_DISPATCHER (retval);
889 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
891 priv = GET_PRIV (dispatcher);
893 if (priv->handler == NULL)
894 priv->handler = empathy_handler_new (NULL, NULL, NULL);
896 empathy_handler_set_handle_channels_func (priv->handler,
897 empathy_dispatcher_handle_channels,
900 empathy_handler_set_channels_func (priv->handler,
901 empathy_dispatcher_get_channels,
908 dispatcher_dispose (GObject *object)
910 EmpathyDispatcherPriv *priv = GET_PRIV (object);
913 if (priv->dispose_has_run)
916 priv->dispose_has_run = TRUE;
918 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
919 g_object_unref (G_OBJECT (l->data));
921 g_list_free (priv->handlers);
922 priv->handlers = NULL;
924 if (priv->handler != NULL)
925 g_object_unref (priv->handler);
926 priv->handler = NULL;
928 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
932 dispatcher_finalize (GObject *object)
934 EmpathyDispatcherPriv *priv = GET_PRIV (object);
940 if (priv->request_channel_class_async_ids != NULL)
942 g_hash_table_foreach (priv->request_channel_class_async_ids,
943 remove_idle_handlers, NULL);
944 g_hash_table_destroy (priv->request_channel_class_async_ids);
947 g_signal_handlers_disconnect_by_func (priv->account_manager,
948 dispatcher_new_connection_cb, object);
950 for (l = priv->channels; l; l = l->next)
952 g_signal_handlers_disconnect_by_func (l->data,
953 dispatcher_channel_invalidated_cb, object);
956 g_list_free (priv->channels);
958 g_hash_table_iter_init (&iter, priv->connections);
959 while (g_hash_table_iter_next (&iter, &connection, NULL))
961 g_signal_handlers_disconnect_by_func (connection,
962 dispatcher_connection_invalidated_cb, object);
965 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
966 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
968 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
972 g_object_unref (priv->account_manager);
974 g_hash_table_destroy (priv->connections);
975 g_hash_table_destroy (priv->outstanding_classes_requests);
979 dispatcher_set_property (GObject *object,
984 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
985 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
990 priv->handler = g_value_dup_object (value);
993 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
999 dispatcher_get_property (GObject *object,
1004 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1005 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1007 switch (property_id)
1010 g_value_set_object (value, priv->handler);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1019 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1021 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1022 GParamSpec *param_spec;
1024 object_class->dispose = dispatcher_dispose;
1025 object_class->finalize = dispatcher_finalize;
1026 object_class->constructor = dispatcher_constructor;
1028 object_class->get_property = dispatcher_get_property;
1029 object_class->set_property = dispatcher_set_property;
1031 param_spec = g_param_spec_object ("handler", "handler",
1032 "The main Telepathy Client Hander object",
1033 EMPATHY_TYPE_HANDLER,
1034 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1035 g_object_class_install_property (object_class,
1036 PROP_HANDLER, param_spec);
1039 g_signal_new ("observe",
1040 G_TYPE_FROM_CLASS (klass),
1044 g_cclosure_marshal_VOID__OBJECT,
1046 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1049 g_signal_new ("approve",
1050 G_TYPE_FROM_CLASS (klass),
1054 g_cclosure_marshal_VOID__OBJECT,
1056 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1059 g_signal_new ("dispatch",
1060 G_TYPE_FROM_CLASS (klass),
1064 g_cclosure_marshal_VOID__OBJECT,
1066 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1069 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1073 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1075 GList *connections, *l;
1076 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1077 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1079 dispatcher->priv = priv;
1080 priv->account_manager = empathy_account_manager_dup_singleton ();
1082 g_signal_connect (priv->account_manager, "new-connection",
1083 G_CALLBACK (dispatcher_new_connection_cb),
1086 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1087 g_object_unref, (GDestroyNotify) free_connection_data);
1089 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1090 g_direct_equal, g_object_unref, NULL);
1092 priv->channels = NULL;
1094 connections = empathy_account_manager_dup_connections (priv->account_manager);
1095 for (l = connections; l; l = l->next)
1097 dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
1098 g_object_unref (l->data);
1100 g_list_free (connections);
1102 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1107 empathy_dispatcher_new (const gchar *name,
1111 g_assert (dispatcher == NULL);
1112 EmpathyHandler *handler;
1113 EmpathyDispatcher *ret;
1115 handler = empathy_handler_new (name, filters, capabilities);
1117 ret = EMPATHY_DISPATCHER (
1118 g_object_new (EMPATHY_TYPE_DISPATCHER,
1121 g_object_unref (handler);
1127 empathy_dispatcher_dup_singleton (void)
1129 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1133 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1134 DispatcherRequestData *request_data,
1135 const GError *error)
1137 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1138 ConnectionData *conn_data;
1140 conn_data = g_hash_table_lookup (priv->connections,
1141 request_data->connection);
1142 if (request_data->cb != NULL)
1143 request_data->cb (NULL, error, request_data->user_data);
1145 conn_data->outstanding_requests =
1146 g_list_remove (conn_data->outstanding_requests, request_data);
1147 free_dispatcher_request_data (request_data);
1151 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1152 DispatcherRequestData *request_data,
1153 const gchar *object_path,
1154 GHashTable *properties,
1155 const GError *error)
1157 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1158 EmpathyDispatchOperation *operation = NULL;
1159 ConnectionData *conn_data;
1161 conn_data = g_hash_table_lookup (priv->connections,
1162 request_data->connection);
1166 DEBUG ("Channel request failed: %s", error->message);
1168 dispatcher_request_failed (dispatcher, request_data, error);
1173 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1176 if (operation != NULL)
1177 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1179 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1182 if (operation == NULL)
1184 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1189 operation = empathy_dispatch_operation_new_with_wrapper (
1190 request_data->connection,
1191 data->channel, request_data->contact, FALSE,
1192 data->channel_wrapper);
1198 if (properties != NULL)
1199 channel = tp_channel_new_from_properties (request_data->connection,
1200 object_path, properties, NULL);
1202 channel = tp_channel_new (request_data->connection, object_path,
1203 request_data->channel_type, request_data->handle_type,
1204 request_data->handle, NULL);
1206 g_signal_connect (channel, "invalidated",
1207 G_CALLBACK (dispatcher_channel_invalidated_cb),
1208 request_data->dispatcher);
1210 priv->channels = g_list_prepend (priv->channels, channel);
1212 operation = empathy_dispatch_operation_new (request_data->connection,
1213 channel, request_data->contact, FALSE);
1214 g_object_unref (channel);
1219 /* Already existed set potential extra information */
1220 g_object_set (G_OBJECT (operation),
1221 "contact", request_data->contact,
1225 request_data->operation = operation;
1227 /* (pre)-approve this right away as we requested it
1228 * This might cause the channel to be claimed, in which case the operation
1229 * will disappear. So ref it, and check the status before starting the
1232 g_object_ref (operation);
1233 empathy_dispatch_operation_approve (operation);
1235 if (empathy_dispatch_operation_get_status (operation) <
1236 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1237 dispatcher_start_dispatching (request_data->dispatcher, operation,
1240 g_object_unref (operation);
1243 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1248 dispatcher_request_channel_cb (TpConnection *connection,
1249 const gchar *object_path,
1250 const GError *error,
1252 GObject *weak_object)
1254 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1255 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1257 dispatcher_connection_new_requested_channel (dispatcher,
1258 request_data, object_path, NULL, error);
1262 dispatcher_request_channel (DispatcherRequestData *request_data)
1264 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1265 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1267 /* Extend the request_data to be a valid request */
1268 g_assert (request_data->request == NULL);
1269 request_data->request = tp_asv_new (
1270 TP_IFACE_CHANNEL ".ChannelType",
1271 G_TYPE_STRING, request_data->channel_type,
1272 TP_IFACE_CHANNEL ".TargetHandleType",
1273 G_TYPE_UINT, request_data->handle_type,
1276 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1277 tp_asv_set_uint32 (request_data->request,
1278 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1280 empathy_dispatcher_call_create_channel (request_data->dispatcher,
1285 tp_cli_connection_call_request_channel (request_data->connection, -1,
1286 request_data->channel_type,
1287 request_data->handle_type,
1288 request_data->handle,
1289 TRUE, dispatcher_request_channel_cb,
1290 request_data, NULL, G_OBJECT (request_data->dispatcher));
1295 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1296 EmpathyDispatcherRequestCb *callback,
1299 EmpathyDispatcher *dispatcher;
1300 EmpathyDispatcherPriv *priv;
1301 TpConnection *connection;
1302 ConnectionData *connection_data;
1303 DispatcherRequestData *request_data;
1305 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1307 dispatcher = empathy_dispatcher_dup_singleton ();
1308 priv = GET_PRIV (dispatcher);
1310 connection = empathy_contact_get_connection (contact);
1311 connection_data = g_hash_table_lookup (priv->connections, connection);
1313 /* The contact handle might not be known yet */
1314 request_data = new_dispatcher_request_data (dispatcher, connection,
1315 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1316 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1318 connection_data->outstanding_requests = g_list_prepend
1319 (connection_data->outstanding_requests, request_data);
1321 dispatcher_request_channel (request_data);
1323 g_object_unref (dispatcher);
1328 EmpathyDispatcher *dispatcher;
1329 EmpathyDispatcherRequestCb *callback;
1331 } ChatWithContactIdData;
1334 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1335 EmpathyContact *contact,
1336 const GError *error,
1338 GObject *weak_object)
1340 ChatWithContactIdData *data = user_data;
1344 /* FIXME: Should call data->callback with the error */
1345 DEBUG ("Error: %s", error->message);
1349 empathy_dispatcher_chat_with_contact (contact, data->callback,
1353 g_object_unref (data->dispatcher);
1354 g_slice_free (ChatWithContactIdData, data);
1358 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1359 const gchar *contact_id,
1360 EmpathyDispatcherRequestCb *callback,
1363 EmpathyDispatcher *dispatcher;
1364 EmpathyTpContactFactory *factory;
1365 ChatWithContactIdData *data;
1367 g_return_if_fail (TP_IS_CONNECTION (connection));
1368 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1370 dispatcher = empathy_dispatcher_dup_singleton ();
1371 factory = empathy_tp_contact_factory_dup_singleton (connection);
1372 data = g_slice_new0 (ChatWithContactIdData);
1373 data->dispatcher = dispatcher;
1374 data->callback = callback;
1375 data->user_data = user_data;
1376 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1377 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1379 g_object_unref (factory);
1383 dispatcher_request_handles_cb (TpConnection *connection,
1384 const GArray *handles,
1385 const GError *error,
1389 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1393 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1394 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1397 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1399 if (request_data->cb)
1400 request_data->cb (NULL, error, request_data->user_data);
1402 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1405 free_dispatcher_request_data (request_data);
1407 dispatcher_flush_outstanding_operations (dispatcher, cd);
1411 request_data->handle = g_array_index (handles, guint, 0);
1412 dispatcher_request_channel (request_data);
1416 empathy_dispatcher_join_muc (TpConnection *connection,
1417 const gchar *roomname,
1418 EmpathyDispatcherRequestCb *callback,
1421 EmpathyDispatcher *dispatcher;
1422 EmpathyDispatcherPriv *priv;
1423 DispatcherRequestData *request_data;
1424 ConnectionData *connection_data;
1425 const gchar *names[] = { roomname, NULL };
1427 g_return_if_fail (TP_IS_CONNECTION (connection));
1428 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1430 dispatcher = empathy_dispatcher_dup_singleton ();
1431 priv = GET_PRIV (dispatcher);
1433 connection_data = g_hash_table_lookup (priv->connections, connection);
1435 /* Don't know the room handle yet */
1436 request_data = new_dispatcher_request_data (dispatcher, connection,
1437 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1438 NULL, callback, user_data);
1440 connection_data->outstanding_requests = g_list_prepend
1441 (connection_data->outstanding_requests, request_data);
1443 tp_cli_connection_call_request_handles (connection, -1,
1444 TP_HANDLE_TYPE_ROOM, names,
1445 dispatcher_request_handles_cb, request_data, NULL,
1446 G_OBJECT (dispatcher));
1448 g_object_unref (dispatcher);
1452 dispatcher_create_channel_cb (TpConnection *connect,
1453 const gchar *object_path,
1454 GHashTable *properties,
1455 const GError *error,
1457 GObject *weak_object)
1459 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1460 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1462 dispatcher_connection_new_requested_channel (dispatcher,
1463 request_data, object_path, properties, error);
1467 empathy_dispatcher_call_create_channel (EmpathyDispatcher *dispatcher,
1468 DispatcherRequestData *request_data)
1470 tp_cli_connection_interface_requests_call_create_channel (
1471 request_data->connection, -1,
1472 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1473 G_OBJECT (request_data->dispatcher));
1477 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1478 TpConnection *connection,
1479 GHashTable *request,
1480 EmpathyDispatcherRequestCb *callback,
1483 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1484 ConnectionData *connection_data;
1485 DispatcherRequestData *request_data;
1486 const gchar *channel_type;
1491 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1492 g_return_if_fail (TP_IS_CONNECTION (connection));
1493 g_return_if_fail (request != NULL);
1495 connection_data = g_hash_table_lookup (priv->connections, connection);
1496 g_assert (connection_data != NULL);
1498 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1500 handle_type = tp_asv_get_uint32 (request,
1501 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1503 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1505 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1507 request_data = new_dispatcher_request_data (dispatcher, connection,
1508 channel_type, handle_type, handle, request,
1509 NULL, callback, user_data);
1511 connection_data->outstanding_requests = g_list_prepend
1512 (connection_data->outstanding_requests, request_data);
1514 empathy_dispatcher_call_create_channel (dispatcher, request_data);
1518 channel_class_matches (GValueArray *class,
1519 const char *channel_type,
1521 GArray *fixed_properties)
1529 v = g_value_array_get_nth (class, 0);
1531 /* if the class doesn't match channel type discard it. */
1532 fprops = g_value_get_boxed (v);
1533 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1535 if (tp_strdiff (channel_type, c_type))
1538 /* we have the right channel type, see if the handle type matches */
1539 h_type = tp_asv_get_uint32 (fprops,
1540 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1542 if (!valid || handle_type != h_type)
1545 if (fixed_properties != NULL)
1547 gpointer h_key, h_val;
1549 GHashTableIter iter;
1552 g_hash_table_iter_init (&iter, fprops);
1554 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1556 /* discard ChannelType and TargetHandleType, as we already
1559 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1561 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1566 for (idx = 0; idx < fixed_properties->len; idx++)
1568 /* if |key| doesn't exist in |fixed_properties|, discard
1573 g_array_index (fixed_properties, char *, idx)))
1576 /* exit the for() loop */
1587 /* if no fixed_properties are specified, discard the classes
1588 * with some fixed properties other than the two we already
1591 if (g_hash_table_size (fprops) > 2)
1599 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher,
1600 TpConnection *connection,
1601 const gchar *channel_type,
1603 GArray *fixed_properties)
1605 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1608 GList *matching_classes;
1612 g_return_val_if_fail (channel_type != NULL, NULL);
1613 g_return_val_if_fail (handle_type != 0, NULL);
1615 cd = g_hash_table_lookup (priv->connections, connection);
1620 classes = cd->requestable_channels;
1621 if (classes == NULL)
1624 matching_classes = NULL;
1626 for (i = 0; i < classes->len; i++)
1628 class = g_ptr_array_index (classes, i);
1630 if (!channel_class_matches
1631 (class, channel_type, handle_type, fixed_properties))
1634 matching_classes = g_list_prepend (matching_classes, class);
1637 return matching_classes;
1641 find_channel_class_idle_cb (gpointer user_data)
1645 FindChannelRequest *request = user_data;
1647 gboolean is_ready = TRUE;
1648 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1650 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1652 cd = g_hash_table_lookup (priv->connections, request->connection);
1656 else if (cd->requestable_channels == NULL)
1661 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1662 request->connection, request->channel_type, request->handle_type,
1663 request->properties);
1665 request->callback (retval, request->user_data);
1666 free_find_channel_request (request);
1667 g_list_free (retval);
1672 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1673 request->connection);
1674 requests = g_list_prepend (requests, request);
1676 g_hash_table_insert (priv->outstanding_classes_requests,
1677 request->connection, requests);
1683 setup_varargs (va_list var_args,
1684 const char *channel_namespace,
1685 const char *first_property_name)
1691 if (first_property_name == NULL)
1694 name = first_property_name;
1695 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1697 while (name != NULL)
1699 name_full = g_strdup (name);
1700 properties = g_array_append_val (properties, name_full);
1701 name = va_arg (var_args, char *);
1708 * empathy_dispatcher_find_requestable_channel_classes:
1709 * @dispatcher: an #EmpathyDispatcher
1710 * @connection: a #TpConnection
1711 * @channel_type: a string identifying the type of the channel to lookup
1712 * @handle_type: the handle type for the channel
1713 * @first_property_name: %NULL, or the name of the first fixed property,
1714 * followed optionally by more names, followed by %NULL.
1716 * Returns all the channel classes that a client can request for the connection
1717 * @connection, of the type identified by @channel_type, @handle_type and the
1718 * fixed properties list.
1719 * The classes which are compatible with a fixed properties list (i.e. those
1720 * that will be returned by this function) are intended as those that do not
1721 * contain any fixed property other than those in the list; note that this
1722 * doesn't guarantee that all the classes compatible with the list will contain
1723 * all the requested fixed properties, so the clients will have to filter
1724 * the returned list themselves.
1725 * If @first_property_name is %NULL, only the classes with no other fixed
1726 * properties than ChannelType and TargetHandleType will be returned.
1727 * Note that this function may return %NULL without performing any lookup if
1728 * @connection is not ready. To ensure that @connection is always ready,
1729 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1731 * Return value: a #GList of #GValueArray objects, where the first element in
1732 * the array is a #GHashTable of the fixed properties, and the second is
1733 * a #GStrv of the allowed properties for the class. The list should be free'd
1734 * with g_list_free() when done, but the objects inside the list are owned
1735 * by the #EmpathyDispatcher and must not be modified.
1738 empathy_dispatcher_find_requestable_channel_classes
1739 (EmpathyDispatcher *dispatcher,
1740 TpConnection *connection,
1741 const gchar *channel_type,
1743 const char *first_property_name,
1748 EmpathyDispatcherPriv *priv;
1753 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL);
1754 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1755 g_return_val_if_fail (channel_type != NULL, NULL);
1756 g_return_val_if_fail (handle_type != 0, NULL);
1758 priv = GET_PRIV (dispatcher);
1760 va_start (var_args, first_property_name);
1762 properties = setup_varargs (var_args, channel_type, first_property_name);
1766 retval = empathy_dispatcher_find_channel_classes (dispatcher, connection,
1767 channel_type, handle_type, properties);
1769 if (properties != NULL)
1771 /* free the properties array */
1772 for (idx = 0; idx < properties->len ; idx++)
1774 str = g_array_index (properties, char *, idx);
1778 g_array_free (properties, TRUE);
1785 * empathy_dispatcher_find_requestable_channel_classes_async:
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 * @callback: the callback to call when @connection is ready
1791 * @user_data: the user data to pass to @callback
1792 * @first_property_name: %NULL, or the name of the first fixed property,
1793 * followed optionally by more names, followed by %NULL.
1795 * Please see the documentation of
1796 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1797 * description of this function.
1800 empathy_dispatcher_find_requestable_channel_classes_async
1801 (EmpathyDispatcher *dispatcher,
1802 TpConnection *connection,
1803 const gchar *channel_type,
1805 EmpathyDispatcherFindChannelClassCb callback,
1807 const char *first_property_name,
1812 FindChannelRequest *request;
1813 EmpathyDispatcherPriv *priv;
1816 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1817 g_return_if_fail (TP_IS_CONNECTION (connection));
1818 g_return_if_fail (channel_type != NULL);
1819 g_return_if_fail (handle_type != 0);
1821 priv = GET_PRIV (dispatcher);
1823 va_start (var_args, first_property_name);
1825 properties = setup_varargs (var_args, channel_type, first_property_name);
1829 /* append another request for this connection */
1830 request = g_slice_new0 (FindChannelRequest);
1831 request->dispatcher = g_object_ref (dispatcher);
1832 request->channel_type = g_strdup (channel_type);
1833 request->handle_type = handle_type;
1834 request->connection = connection;
1835 request->callback = callback;
1836 request->user_data = user_data;
1837 request->properties = properties;
1839 source_id = g_idle_add (find_channel_class_idle_cb, request);
1841 g_hash_table_insert (priv->request_channel_class_async_ids,
1842 request, GUINT_TO_POINTER (source_id));
1846 empathy_dispatcher_get_channels (EmpathyHandler *handler,
1849 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1850 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1852 return priv->channels;
1856 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
1857 const gchar *account_path,
1858 const gchar *connection_path,
1859 const GPtrArray *channels,
1860 const GPtrArray *requests_satisfied,
1862 GHashTable *handler_info,
1866 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1867 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1869 EmpathyAccount *account;
1870 TpConnection *connection;
1872 account = empathy_account_manager_ensure_account (priv->account_manager,
1874 g_assert (account != NULL);
1876 connection = empathy_account_get_connection_for_path (account,
1878 if (connection == NULL)
1880 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1881 "Invalid connection argument");
1885 for (i = 0; i < channels->len ; i++)
1887 GValueArray *arr = g_ptr_array_index (channels, i);
1888 const gchar *object_path;
1889 GHashTable *properties;
1891 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
1892 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
1894 dispatcher_connection_new_channel_with_properties (dispatcher,
1895 connection, object_path, properties);
1903 empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
1908 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1909 EmpathyHandler *handler;
1911 handler = empathy_handler_new (name, filters, capabilities);
1912 priv->handlers = g_list_prepend (priv->handlers, handler);
1914 /* Only set the handle_channels function, the Channel property on the main
1915 * handler will always report all dispatched channels even if they came from
1916 * a different Handler */
1917 empathy_handler_set_handle_channels_func (handler,
1918 empathy_dispatcher_handle_channels,
1925 empathy_dispatcher_remove_handler (EmpathyDispatcher *dispatcher,
1926 EmpathyHandler *handler)
1928 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1931 h = g_list_find (priv->handlers, handler);
1932 g_return_if_fail (h != NULL);
1934 priv->handlers = g_list_delete_link (priv->handlers, h);
1936 g_object_unref (handler);