1 /* * Copyright (C) 2007-2009 Collabora Ltd.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * Authors: Xavier Claessens <xclaesse@gmail.com>
18 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 #define DISPATCHER_BUS_NAME TP_CLIENT_BUS_NAME_BASE "Empathy"
23 #define DISPATCHER_OBJECT_PATH TP_CLIENT_OBJECT_PATH_BASE "Empathy"
29 #include <glib/gi18n-lib.h>
31 #include <telepathy-glib/enums.h>
32 #include <telepathy-glib/connection.h>
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/dbus.h>
35 #include <telepathy-glib/proxy-subclass.h>
36 #include <telepathy-glib/gtypes.h>
37 #include <telepathy-glib/defs.h>
38 #include <telepathy-glib/svc-client.h>
39 #include <telepathy-glib/svc-generic.h>
40 #include <telepathy-glib/interfaces.h>
42 #include <extensions/extensions.h>
44 #include "empathy-dispatcher.h"
45 #include "empathy-handler.h"
46 #include "empathy-utils.h"
47 #include "empathy-tube-handler.h"
48 #include "empathy-account-manager.h"
49 #include "empathy-tp-contact-factory.h"
50 #include "empathy-chatroom-manager.h"
51 #include "empathy-utils.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
59 gboolean dispose_has_run;
61 EmpathyAccountManager *account_manager;
62 /* connection to connection data mapping */
63 GHashTable *connections;
64 GHashTable *outstanding_classes_requests;
68 /* channels which the dispatcher is listening "invalidated" */
73 EmpathyHandler *handler;
78 GHashTable *request_channel_class_async_ids;
79 } EmpathyDispatcherPriv;
82 empathy_dispatcher_get_channels (EmpathyHandler *handler,
86 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
87 const gchar *account_path,
88 const gchar *connection_path,
89 const GPtrArray *channels,
90 const GPtrArray *requests_satisfied,
92 GHashTable *handler_info,
96 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
112 static guint signals[LAST_SIGNAL];
113 static EmpathyDispatcher *dispatcher = NULL;
115 static void dispatcher_init_connection_if_needed (
116 EmpathyDispatcher *dispatcher,
117 TpConnection *connection);
119 static GList * empathy_dispatcher_find_channel_classes
120 (EmpathyDispatcher *dispatcher, TpConnection *connection,
121 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
126 EmpathyDispatcher *dispatcher;
127 EmpathyDispatchOperation *operation;
128 TpConnection *connection;
129 gboolean should_ensure;
133 EmpathyContact *contact;
135 /* Properties to pass to the channel when requesting it */
137 EmpathyDispatcherRequestCb *cb;
139 gpointer *request_data;
140 } DispatcherRequestData;
145 /* Channel type specific wrapper object */
146 GObject *channel_wrapper;
151 /* ObjectPath => DispatchData.. */
152 GHashTable *dispatched_channels;
153 /* ObjectPath -> EmpathyDispatchOperations */
154 GHashTable *dispatching_channels;
156 /* ObjectPath -> EmpathyDispatchOperations
158 * This holds channels which were announced with NewChannel while we have an
159 * outstanding channel request for a channel of this type. On the Requests
160 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
161 * return before NewChannels is emitted, but there was no guarantee of the
162 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
163 * held in limbo here until we know whether they were requested.
165 GHashTable *outstanding_channels;
166 /* List of DispatcherRequestData */
167 GList *outstanding_requests;
168 /* List of requestable channel classes */
169 GPtrArray *requestable_channels;
174 EmpathyDispatcher *dispatcher;
175 TpConnection *connection;
179 EmpathyDispatcherFindChannelClassCb *callback;
181 } FindChannelRequest;
184 empathy_dispatcher_call_create_or_ensure_channel (
185 EmpathyDispatcher *dispatcher,
186 DispatcherRequestData *request_data);
188 static DispatchData *
189 new_dispatch_data (TpChannel *channel,
190 GObject *channel_wrapper)
192 DispatchData *d = g_slice_new0 (DispatchData);
193 d->channel = g_object_ref (channel);
194 if (channel_wrapper != NULL)
195 d->channel_wrapper = g_object_ref (channel_wrapper);
201 free_dispatch_data (DispatchData *data)
203 g_object_unref (data->channel);
204 if (data->channel_wrapper != NULL)
205 g_object_unref (data->channel_wrapper);
207 g_slice_free (DispatchData, data);
210 static DispatcherRequestData *
211 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
212 TpConnection *connection,
213 const gchar *channel_type,
217 EmpathyContact *contact,
218 EmpathyDispatcherRequestCb *cb,
221 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
223 result->dispatcher = g_object_ref (dispatcher);
224 result->connection = connection;
226 result->should_ensure = FALSE;
228 result->channel_type = g_strdup (channel_type);
229 result->handle_type = handle_type;
230 result->handle = handle;
231 result->request = request;
234 result->contact = g_object_ref (contact);
237 result->user_data = user_data;
243 free_dispatcher_request_data (DispatcherRequestData *r)
245 g_free (r->channel_type);
247 if (r->dispatcher != NULL)
248 g_object_unref (r->dispatcher);
250 if (r->contact != NULL)
251 g_object_unref (r->contact);
253 if (r->request != NULL)
254 g_hash_table_unref (r->request);
256 g_slice_free (DispatcherRequestData, r);
259 static ConnectionData *
260 new_connection_data (void)
262 ConnectionData *cd = g_slice_new0 (ConnectionData);
264 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
265 g_free, (GDestroyNotify) free_dispatch_data);
267 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
268 g_free, g_object_unref);
270 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
277 free_connection_data (ConnectionData *cd)
281 g_hash_table_destroy (cd->dispatched_channels);
282 g_hash_table_destroy (cd->dispatching_channels);
285 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
287 free_dispatcher_request_data (l->data);
290 if (cd->requestable_channels != NULL)
292 for (i = 0 ; i < cd->requestable_channels->len ; i++)
294 g_ptr_array_index (cd->requestable_channels, i));
295 g_ptr_array_free (cd->requestable_channels, TRUE);
300 free_find_channel_request (FindChannelRequest *r)
305 g_object_unref (r->dispatcher);
306 g_free (r->channel_type);
308 if (r->properties != NULL)
310 for (idx = 0; idx < r->properties->len ; idx++)
312 str = g_array_index (r->properties, char *, idx);
316 g_array_free (r->properties, TRUE);
319 g_slice_free (FindChannelRequest, r);
323 dispatcher_connection_invalidated_cb (TpConnection *connection,
327 EmpathyDispatcher *dispatcher)
329 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
331 DEBUG ("Error: %s", message);
332 g_hash_table_remove (priv->connections, connection);
336 dispatcher_operation_can_start (EmpathyDispatcher *self,
337 EmpathyDispatchOperation *operation,
341 const gchar *channel_type =
342 empathy_dispatch_operation_get_channel_type (operation);
344 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
346 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
348 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
358 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
359 EmpathyDispatchOperation *operation,
365 l = cd->outstanding_requests;
368 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
373 if (d->operation == operation)
378 d->cb (NULL, error, d->user_data);
380 d->cb (operation, NULL, d->user_data);
383 cd->outstanding_requests = g_list_delete_link
384 (cd->outstanding_requests, lt);
386 free_dispatcher_request_data (d);
392 dispatcher_channel_invalidated_cb (TpProxy *proxy,
396 EmpathyDispatcher *dispatcher)
398 /* Channel went away... */
399 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
400 TpConnection *connection;
401 EmpathyDispatchOperation *operation;
403 const gchar *object_path;
405 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
407 cd = g_hash_table_lookup (priv->connections, connection);
408 /* Connection itself invalidated? */
412 object_path = tp_proxy_get_object_path (proxy);
414 DEBUG ("Channel %s invalidated", object_path);
416 g_hash_table_remove (cd->dispatched_channels, object_path);
417 g_hash_table_remove (cd->dispatching_channels, object_path);
419 priv->channels = g_list_remove (priv->channels, proxy);
421 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
422 if (operation != NULL)
424 GError error = { domain, code, message };
425 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
426 g_hash_table_remove (cd->outstanding_channels, object_path);
427 g_object_unref (operation);
432 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
433 EmpathyDispatcher *dispatcher)
435 g_assert (empathy_dispatch_operation_is_incoming (operation));
436 DEBUG ("Send of for dispatching: %s",
437 empathy_dispatch_operation_get_object_path (operation));
438 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
442 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
443 EmpathyDispatcher *dispatcher)
445 /* Our job is done, remove the dispatch operation and mark the channel as
447 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
448 TpConnection *connection;
450 const gchar *object_path;
452 connection = empathy_dispatch_operation_get_tp_connection (operation);
453 cd = g_hash_table_lookup (priv->connections, connection);
454 g_assert (cd != NULL);
456 object_path = empathy_dispatch_operation_get_object_path (operation);
458 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
461 d = new_dispatch_data (
462 empathy_dispatch_operation_get_channel (operation),
463 empathy_dispatch_operation_get_channel_wrapper (operation));
464 g_hash_table_insert (cd->dispatched_channels,
465 g_strdup (object_path), d);
467 g_hash_table_remove (cd->dispatching_channels, object_path);
469 DEBUG ("Channel claimed: %s", object_path);
473 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
474 EmpathyDispatcher *dispatcher)
476 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
477 TpConnection *connection;
479 EmpathyDispatchOperationState status;
481 g_signal_connect (operation, "approved",
482 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
484 g_signal_connect (operation, "claimed",
485 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
487 /* Signal the observers */
488 DEBUG ("Send to observers: %s",
489 empathy_dispatch_operation_get_object_path (operation));
490 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
492 empathy_dispatch_operation_start (operation);
494 /* Signal potential requestors */
495 connection = empathy_dispatch_operation_get_tp_connection (operation);
496 cd = g_hash_table_lookup (priv->connections, connection);
497 g_assert (cd != NULL);
499 g_object_ref (operation);
500 g_object_ref (dispatcher);
502 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
503 status = empathy_dispatch_operation_get_status (operation);
504 g_object_unref (operation);
506 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
509 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
511 DEBUG ("Send to approvers: %s",
512 empathy_dispatch_operation_get_object_path (operation));
513 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
517 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
518 DEBUG ("Send of for dispatching: %s",
519 empathy_dispatch_operation_get_object_path (operation));
520 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
523 g_object_unref (dispatcher);
527 dispatcher_start_dispatching (EmpathyDispatcher *self,
528 EmpathyDispatchOperation *operation,
531 const gchar *object_path =
532 empathy_dispatch_operation_get_object_path (operation);
534 DEBUG ("Dispatching process started for %s", object_path);
536 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
538 g_assert (g_hash_table_lookup (cd->outstanding_channels,
539 object_path) == NULL);
541 g_hash_table_insert (cd->dispatching_channels,
542 g_strdup (object_path), operation);
544 switch (empathy_dispatch_operation_get_status (operation))
546 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
547 g_signal_connect (operation, "ready",
548 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
550 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
551 dispatch_operation_ready_cb (operation, dispatcher);
554 g_assert_not_reached ();
558 else if (empathy_dispatch_operation_get_status (operation) >=
559 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
561 /* Already dispatching and the operation is pending, thus the observers
562 * have seen it (if applicable), so we can flush the request right away.
564 dispatch_operation_flush_requests (self, operation, NULL, cd);
569 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
575 g_hash_table_iter_init (&iter, cd->outstanding_channels);
576 while (g_hash_table_iter_next (&iter, NULL, &value))
578 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
580 if (dispatcher_operation_can_start (self, operation, cd))
582 g_hash_table_iter_remove (&iter);
583 dispatcher_start_dispatching (dispatcher, operation, cd);
589 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
590 TpConnection *connection,
591 const gchar *object_path,
592 const gchar *channel_type,
595 GHashTable *properties,
598 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
601 EmpathyDispatchOperation *operation;
603 /* Channel types we never want to dispatch because they're either deprecated
604 * or can't sensibly be dispatch (e.g. channels that should always be
606 const char *blacklist[] = {
607 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
608 TP_IFACE_CHANNEL_TYPE_TUBES,
609 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
613 dispatcher_init_connection_if_needed (dispatcher, connection);
615 cd = g_hash_table_lookup (priv->connections, connection);
617 /* Don't bother with channels we have already dispatched or are dispatching
618 * currently. This can happen when NewChannel(s) is fired after
619 * RequestChannel/CreateChannel/EnsureChannel */
620 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
623 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
626 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
628 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
631 /* Only pick up non-requested text and file channels. For all other it
632 * doesn't make sense to handle it if we didn't request it. The same goes
633 * for channels we discovered by the Channels property or ListChannels */
634 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
635 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
637 DEBUG ("Ignoring incoming channel of type %s on %s",
638 channel_type, object_path);
642 for (i = 0 ; blacklist[i] != NULL; i++)
644 if (!tp_strdiff (channel_type, blacklist[i]))
646 DEBUG ("Ignoring blacklisted channel type %s on %s",
647 channel_type, object_path);
652 DEBUG ("New channel of type %s on %s", channel_type, object_path);
654 if (properties == NULL)
655 channel = tp_channel_new (connection, object_path, channel_type,
656 handle_type, handle, NULL);
658 channel = tp_channel_new_from_properties (connection, object_path,
661 g_signal_connect (channel, "invalidated",
662 G_CALLBACK (dispatcher_channel_invalidated_cb),
665 priv->channels = g_list_prepend (priv->channels, channel);
667 operation = empathy_dispatch_operation_new (connection, channel, NULL,
670 g_object_unref (channel);
674 /* Request could either be by us or by a remote party. If there are no
675 * outstanding requests for this channel type we can assume it's remote.
676 * Otherwise we wait untill they are all satisfied */
677 if (dispatcher_operation_can_start (dispatcher, operation, cd))
678 dispatcher_start_dispatching (dispatcher, operation, cd);
680 g_hash_table_insert (cd->outstanding_channels,
681 g_strdup (object_path), operation);
685 dispatcher_start_dispatching (dispatcher, operation, cd);
690 dispatcher_connection_new_channel_with_properties (
691 EmpathyDispatcher *dispatcher,
692 TpConnection *connection,
693 const gchar *object_path,
694 GHashTable *properties)
696 const gchar *channel_type;
703 channel_type = tp_asv_get_string (properties,
704 TP_IFACE_CHANNEL ".ChannelType");
705 if (channel_type == NULL)
707 g_message ("%s had an invalid ChannelType property", object_path);
711 handle_type = tp_asv_get_uint32 (properties,
712 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
715 g_message ("%s had an invalid TargetHandleType property", object_path);
719 handle = tp_asv_get_uint32 (properties,
720 TP_IFACE_CHANNEL ".TargetHandle", &valid);
723 g_message ("%s had an invalid TargetHandle property", object_path);
727 /* We assume there is no channel dispather, so we're the only one dispatching
728 * it. Which means that a requested channel it is outgoing one */
729 requested = tp_asv_get_boolean (properties,
730 TP_IFACE_CHANNEL ".Requested", &valid);
733 g_message ("%s had an invalid Requested property", object_path);
737 dispatcher_connection_new_channel (dispatcher, connection,
738 object_path, channel_type, handle_type, handle, properties, !requested);
742 dispatcher_connection_got_all (TpProxy *proxy,
743 GHashTable *properties,
748 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
749 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
750 GPtrArray *requestable_channels;
751 GPtrArray *existing_channels;
754 DEBUG ("Error: %s", error->message);
758 requestable_channels = tp_asv_get_boxed (properties,
759 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
761 if (requestable_channels == NULL)
762 DEBUG ("No RequestableChannelClasses property !?! on connection");
767 FindChannelRequest *request;
770 cd = g_hash_table_lookup (priv->connections, proxy);
771 g_assert (cd != NULL);
773 cd->requestable_channels = g_boxed_copy (
774 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
776 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
779 for (l = requests; l != NULL; l = l->next)
783 retval = empathy_dispatcher_find_channel_classes (dispatcher,
784 TP_CONNECTION (proxy), request->channel_type,
785 request->handle_type, request->properties);
786 request->callback (retval, request->user_data);
788 free_find_channel_request (request);
789 g_list_free (retval);
792 g_list_free (requests);
794 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
797 existing_channels = tp_asv_get_boxed (properties,
798 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
800 if (existing_channels != NULL)
804 for (idx = 0; idx < existing_channels->len; idx++)
806 GValueArray *values = g_ptr_array_index (existing_channels, idx);
807 const gchar *object_path;
808 GHashTable *properties;
810 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
811 properties = g_value_get_boxed (g_value_array_get_nth (values, 1));
813 if (tp_strdiff (tp_asv_get_string (properties,
814 TP_IFACE_CHANNEL ".ChannelType"),
815 TP_IFACE_CHANNEL_TYPE_TEXT))
818 dispatcher_connection_new_channel_with_properties (dispatcher,
819 TP_CONNECTION (proxy), object_path, properties);
825 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
826 const GPtrArray *capabilities,
832 DEBUG ("Error: %s", error->message);
836 dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
837 TpConnection *connection)
839 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
840 GPtrArray *capabilities;
843 const gchar *remove = NULL;
845 if (g_hash_table_lookup (priv->connections, connection) != NULL)
848 g_hash_table_insert (priv->connections, g_object_ref (connection),
849 new_connection_data ());
851 g_signal_connect (connection, "invalidated",
852 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
854 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
855 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
857 tp_cli_dbus_properties_call_get_all (connection, -1,
858 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
859 dispatcher_connection_got_all,
860 NULL, NULL, G_OBJECT (dispatcher));
863 /* Advertise VoIP capabilities */
864 capabilities = g_ptr_array_sized_new (1);
865 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
866 G_TYPE_UINT, G_TYPE_INVALID);
867 g_value_init (&cap, cap_type);
868 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
869 dbus_g_type_struct_set (&cap,
870 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
871 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
872 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
873 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
874 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
875 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
877 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
878 connection, -1, capabilities, &remove,
879 dispatcher_connection_advertise_capabilities_cb,
880 NULL, NULL, G_OBJECT (dispatcher));
882 g_value_unset (&cap);
883 g_ptr_array_free (capabilities, TRUE);
887 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
888 TpConnection *connection,
889 EmpathyDispatcher *dispatcher)
891 dispatcher_init_connection_if_needed (dispatcher, connection);
895 remove_idle_handlers (gpointer key,
901 source_id = GPOINTER_TO_UINT (value);
902 g_source_remove (source_id);
906 dispatcher_constructor (GType type,
907 guint n_construct_params,
908 GObjectConstructParam *construct_params)
911 EmpathyDispatcherPriv *priv;
913 if (dispatcher != NULL)
914 return g_object_ref (dispatcher);
916 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
917 (type, n_construct_params, construct_params);
919 dispatcher = EMPATHY_DISPATCHER (retval);
920 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
922 priv = GET_PRIV (dispatcher);
924 if (priv->handler == NULL)
925 priv->handler = empathy_handler_new (NULL, NULL, NULL);
927 empathy_handler_set_handle_channels_func (priv->handler,
928 empathy_dispatcher_handle_channels,
931 empathy_handler_set_channels_func (priv->handler,
932 empathy_dispatcher_get_channels,
939 dispatcher_dispose (GObject *object)
941 EmpathyDispatcherPriv *priv = GET_PRIV (object);
944 if (priv->dispose_has_run)
947 priv->dispose_has_run = TRUE;
949 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
950 g_object_unref (G_OBJECT (l->data));
952 g_list_free (priv->handlers);
953 priv->handlers = NULL;
955 if (priv->handler != NULL)
956 g_object_unref (priv->handler);
957 priv->handler = NULL;
959 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
963 dispatcher_finalize (GObject *object)
965 EmpathyDispatcherPriv *priv = GET_PRIV (object);
971 if (priv->request_channel_class_async_ids != NULL)
973 g_hash_table_foreach (priv->request_channel_class_async_ids,
974 remove_idle_handlers, NULL);
975 g_hash_table_destroy (priv->request_channel_class_async_ids);
978 g_signal_handlers_disconnect_by_func (priv->account_manager,
979 dispatcher_new_connection_cb, object);
981 for (l = priv->channels; l; l = l->next)
983 g_signal_handlers_disconnect_by_func (l->data,
984 dispatcher_channel_invalidated_cb, object);
987 g_list_free (priv->channels);
989 g_hash_table_iter_init (&iter, priv->connections);
990 while (g_hash_table_iter_next (&iter, &connection, NULL))
992 g_signal_handlers_disconnect_by_func (connection,
993 dispatcher_connection_invalidated_cb, object);
996 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
997 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
999 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1003 g_object_unref (priv->account_manager);
1005 g_hash_table_destroy (priv->connections);
1006 g_hash_table_destroy (priv->outstanding_classes_requests);
1010 dispatcher_set_property (GObject *object,
1012 const GValue *value,
1015 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1016 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1018 switch (property_id)
1021 priv->handler = g_value_dup_object (value);
1024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1030 dispatcher_get_property (GObject *object,
1035 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1036 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1038 switch (property_id)
1041 g_value_set_object (value, priv->handler);
1044 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1050 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1052 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1053 GParamSpec *param_spec;
1055 object_class->dispose = dispatcher_dispose;
1056 object_class->finalize = dispatcher_finalize;
1057 object_class->constructor = dispatcher_constructor;
1059 object_class->get_property = dispatcher_get_property;
1060 object_class->set_property = dispatcher_set_property;
1062 param_spec = g_param_spec_object ("handler", "handler",
1063 "The main Telepathy Client Hander object",
1064 EMPATHY_TYPE_HANDLER,
1065 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1066 g_object_class_install_property (object_class,
1067 PROP_HANDLER, param_spec);
1070 g_signal_new ("observe",
1071 G_TYPE_FROM_CLASS (klass),
1075 g_cclosure_marshal_VOID__OBJECT,
1077 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1080 g_signal_new ("approve",
1081 G_TYPE_FROM_CLASS (klass),
1085 g_cclosure_marshal_VOID__OBJECT,
1087 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1090 g_signal_new ("dispatch",
1091 G_TYPE_FROM_CLASS (klass),
1095 g_cclosure_marshal_VOID__OBJECT,
1097 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1100 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1104 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1106 GList *connections, *l;
1107 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1108 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1110 dispatcher->priv = priv;
1111 priv->account_manager = empathy_account_manager_dup_singleton ();
1113 g_signal_connect (priv->account_manager, "new-connection",
1114 G_CALLBACK (dispatcher_new_connection_cb),
1117 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1118 g_object_unref, (GDestroyNotify) free_connection_data);
1120 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1121 g_direct_equal, g_object_unref, NULL);
1123 priv->channels = NULL;
1125 connections = empathy_account_manager_dup_connections (
1126 priv->account_manager);
1127 for (l = connections; l; l = l->next)
1129 dispatcher_new_connection_cb (priv->account_manager, l->data,
1131 g_object_unref (l->data);
1133 g_list_free (connections);
1135 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1140 empathy_dispatcher_new (const gchar *name,
1144 g_assert (dispatcher == NULL);
1145 EmpathyHandler *handler;
1146 EmpathyDispatcher *ret;
1148 handler = empathy_handler_new (name, filters, capabilities);
1150 ret = EMPATHY_DISPATCHER (
1151 g_object_new (EMPATHY_TYPE_DISPATCHER,
1154 g_object_unref (handler);
1160 empathy_dispatcher_dup_singleton (void)
1162 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1166 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1167 DispatcherRequestData *request_data,
1168 const GError *error)
1170 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1171 ConnectionData *conn_data;
1173 conn_data = g_hash_table_lookup (priv->connections,
1174 request_data->connection);
1175 if (request_data->cb != NULL)
1176 request_data->cb (NULL, error, request_data->user_data);
1178 if (conn_data != NULL)
1180 conn_data->outstanding_requests =
1181 g_list_remove (conn_data->outstanding_requests, request_data);
1183 /* else Connection has been invalidated */
1185 free_dispatcher_request_data (request_data);
1189 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1190 DispatcherRequestData *request_data,
1191 const gchar *object_path,
1192 GHashTable *properties,
1193 const GError *error)
1195 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1196 EmpathyDispatchOperation *operation = NULL;
1197 ConnectionData *conn_data;
1199 conn_data = g_hash_table_lookup (priv->connections,
1200 request_data->connection);
1204 DEBUG ("Channel request failed: %s", error->message);
1206 dispatcher_request_failed (dispatcher, request_data, error);
1211 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1214 if (operation != NULL)
1215 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1217 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1220 if (operation == NULL)
1222 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1227 operation = empathy_dispatch_operation_new_with_wrapper (
1228 request_data->connection,
1229 data->channel, request_data->contact, FALSE,
1230 data->channel_wrapper);
1236 if (properties != NULL)
1237 channel = tp_channel_new_from_properties (request_data->connection,
1238 object_path, properties, NULL);
1240 channel = tp_channel_new (request_data->connection, object_path,
1241 request_data->channel_type, request_data->handle_type,
1242 request_data->handle, NULL);
1244 g_signal_connect (channel, "invalidated",
1245 G_CALLBACK (dispatcher_channel_invalidated_cb),
1246 request_data->dispatcher);
1248 priv->channels = g_list_prepend (priv->channels, channel);
1250 operation = empathy_dispatch_operation_new (request_data->connection,
1251 channel, request_data->contact, FALSE);
1252 g_object_unref (channel);
1257 /* Already existed set potential extra information */
1258 g_object_set (G_OBJECT (operation),
1259 "contact", request_data->contact,
1263 request_data->operation = operation;
1265 /* (pre)-approve this right away as we requested it
1266 * This might cause the channel to be claimed, in which case the operation
1267 * will disappear. So ref it, and check the status before starting the
1270 g_object_ref (operation);
1271 empathy_dispatch_operation_approve (operation);
1273 if (empathy_dispatch_operation_get_status (operation) <
1274 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1275 dispatcher_start_dispatching (request_data->dispatcher, operation,
1278 g_object_unref (operation);
1281 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1286 dispatcher_request_channel_cb (TpConnection *connection,
1287 const gchar *object_path,
1288 const GError *error,
1290 GObject *weak_object)
1292 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1293 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1295 dispatcher_connection_new_requested_channel (dispatcher,
1296 request_data, object_path, NULL, error);
1300 dispatcher_request_channel (DispatcherRequestData *request_data)
1302 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1303 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1305 /* Extend the request_data to be a valid request */
1306 g_assert (request_data->request == NULL);
1307 request_data->request = tp_asv_new (
1308 TP_IFACE_CHANNEL ".ChannelType",
1309 G_TYPE_STRING, request_data->channel_type,
1310 TP_IFACE_CHANNEL ".TargetHandleType",
1311 G_TYPE_UINT, request_data->handle_type,
1314 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1315 tp_asv_set_uint32 (request_data->request,
1316 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1318 empathy_dispatcher_call_create_or_ensure_channel (
1319 request_data->dispatcher, request_data);
1323 tp_cli_connection_call_request_channel (request_data->connection, -1,
1324 request_data->channel_type,
1325 request_data->handle_type,
1326 request_data->handle,
1327 TRUE, dispatcher_request_channel_cb,
1328 request_data, NULL, G_OBJECT (request_data->dispatcher));
1333 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1334 EmpathyDispatcherRequestCb *callback,
1337 EmpathyDispatcher *dispatcher;
1338 EmpathyDispatcherPriv *priv;
1339 TpConnection *connection;
1340 ConnectionData *connection_data;
1341 DispatcherRequestData *request_data;
1343 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1345 dispatcher = empathy_dispatcher_dup_singleton ();
1346 priv = GET_PRIV (dispatcher);
1348 connection = empathy_contact_get_connection (contact);
1349 connection_data = g_hash_table_lookup (priv->connections, connection);
1351 /* The contact handle might not be known yet */
1352 request_data = new_dispatcher_request_data (dispatcher, connection,
1353 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1354 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1355 request_data->should_ensure = TRUE;
1357 connection_data->outstanding_requests = g_list_prepend
1358 (connection_data->outstanding_requests, request_data);
1360 dispatcher_request_channel (request_data);
1362 g_object_unref (dispatcher);
1367 EmpathyDispatcher *dispatcher;
1368 EmpathyDispatcherRequestCb *callback;
1370 } ChatWithContactIdData;
1373 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1374 EmpathyContact *contact,
1375 const GError *error,
1377 GObject *weak_object)
1379 ChatWithContactIdData *data = user_data;
1383 /* FIXME: Should call data->callback with the error */
1384 DEBUG ("Error: %s", error->message);
1388 empathy_dispatcher_chat_with_contact (contact, data->callback,
1392 g_object_unref (data->dispatcher);
1393 g_slice_free (ChatWithContactIdData, data);
1397 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1398 const gchar *contact_id,
1399 EmpathyDispatcherRequestCb *callback,
1402 EmpathyDispatcher *dispatcher;
1403 EmpathyTpContactFactory *factory;
1404 ChatWithContactIdData *data;
1406 g_return_if_fail (TP_IS_CONNECTION (connection));
1407 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1409 dispatcher = empathy_dispatcher_dup_singleton ();
1410 factory = empathy_tp_contact_factory_dup_singleton (connection);
1411 data = g_slice_new0 (ChatWithContactIdData);
1412 data->dispatcher = dispatcher;
1413 data->callback = callback;
1414 data->user_data = user_data;
1415 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1416 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1418 g_object_unref (factory);
1422 dispatcher_request_handles_cb (TpConnection *connection,
1423 const GArray *handles,
1424 const GError *error,
1428 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1432 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1433 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1436 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1438 if (request_data->cb)
1439 request_data->cb (NULL, error, request_data->user_data);
1441 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1444 free_dispatcher_request_data (request_data);
1446 dispatcher_flush_outstanding_operations (dispatcher, cd);
1450 request_data->handle = g_array_index (handles, guint, 0);
1451 dispatcher_request_channel (request_data);
1455 empathy_dispatcher_join_muc (TpConnection *connection,
1456 const gchar *roomname,
1457 EmpathyDispatcherRequestCb *callback,
1460 EmpathyDispatcher *dispatcher;
1461 EmpathyDispatcherPriv *priv;
1462 DispatcherRequestData *request_data;
1463 ConnectionData *connection_data;
1464 const gchar *names[] = { roomname, NULL };
1466 g_return_if_fail (TP_IS_CONNECTION (connection));
1467 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1469 dispatcher = empathy_dispatcher_dup_singleton ();
1470 priv = GET_PRIV (dispatcher);
1472 connection_data = g_hash_table_lookup (priv->connections, connection);
1474 /* Don't know the room handle yet */
1475 request_data = new_dispatcher_request_data (dispatcher, connection,
1476 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1477 NULL, callback, user_data);
1479 connection_data->outstanding_requests = g_list_prepend
1480 (connection_data->outstanding_requests, request_data);
1482 tp_cli_connection_call_request_handles (connection, -1,
1483 TP_HANDLE_TYPE_ROOM, names,
1484 dispatcher_request_handles_cb, request_data, NULL,
1485 G_OBJECT (dispatcher));
1487 g_object_unref (dispatcher);
1491 dispatcher_create_channel_cb (TpConnection *connect,
1492 const gchar *object_path,
1493 GHashTable *properties,
1494 const GError *error,
1496 GObject *weak_object)
1498 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1499 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1501 dispatcher_connection_new_requested_channel (dispatcher,
1502 request_data, object_path, properties, error);
1506 dispatcher_ensure_channel_cb (TpConnection *connect,
1508 const gchar *object_path,
1509 GHashTable *properties,
1510 const GError *error,
1512 GObject *weak_object)
1514 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1515 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1517 dispatcher_connection_new_requested_channel (dispatcher,
1518 request_data, object_path, properties, error);
1522 empathy_dispatcher_call_create_or_ensure_channel (
1523 EmpathyDispatcher *dispatcher,
1524 DispatcherRequestData *request_data)
1526 if (request_data->should_ensure)
1528 tp_cli_connection_interface_requests_call_ensure_channel (
1529 request_data->connection, -1,
1530 request_data->request, dispatcher_ensure_channel_cb,
1531 request_data, NULL, G_OBJECT (request_data->dispatcher));
1535 tp_cli_connection_interface_requests_call_create_channel (
1536 request_data->connection, -1,
1537 request_data->request, dispatcher_create_channel_cb,
1538 request_data, NULL, G_OBJECT (request_data->dispatcher));
1543 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1544 TpConnection *connection,
1545 GHashTable *request,
1546 EmpathyDispatcherRequestCb *callback,
1549 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1550 ConnectionData *connection_data;
1551 DispatcherRequestData *request_data;
1552 const gchar *channel_type;
1557 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1558 g_return_if_fail (TP_IS_CONNECTION (connection));
1559 g_return_if_fail (request != NULL);
1561 connection_data = g_hash_table_lookup (priv->connections, connection);
1562 g_assert (connection_data != NULL);
1564 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1566 handle_type = tp_asv_get_uint32 (request,
1567 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1569 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1571 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1573 request_data = new_dispatcher_request_data (dispatcher, connection,
1574 channel_type, handle_type, handle, request,
1575 NULL, callback, user_data);
1577 connection_data->outstanding_requests = g_list_prepend
1578 (connection_data->outstanding_requests, request_data);
1580 empathy_dispatcher_call_create_or_ensure_channel (dispatcher, request_data);
1584 channel_class_matches (GValueArray *class,
1585 const char *channel_type,
1587 GArray *fixed_properties)
1595 v = g_value_array_get_nth (class, 0);
1597 /* if the class doesn't match channel type discard it. */
1598 fprops = g_value_get_boxed (v);
1599 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1601 if (tp_strdiff (channel_type, c_type))
1604 /* we have the right channel type, see if the handle type matches */
1605 h_type = tp_asv_get_uint32 (fprops,
1606 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1608 if (!valid || handle_type != h_type)
1611 if (fixed_properties != NULL)
1613 gpointer h_key, h_val;
1615 GHashTableIter iter;
1618 g_hash_table_iter_init (&iter, fprops);
1620 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1622 /* discard ChannelType and TargetHandleType, as we already
1625 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1627 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1632 for (idx = 0; idx < fixed_properties->len; idx++)
1634 /* if |key| doesn't exist in |fixed_properties|, discard
1639 g_array_index (fixed_properties, char *, idx)))
1642 /* exit the for() loop */
1653 /* if no fixed_properties are specified, discard the classes
1654 * with some fixed properties other than the two we already
1657 if (g_hash_table_size (fprops) > 2)
1665 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher,
1666 TpConnection *connection,
1667 const gchar *channel_type,
1669 GArray *fixed_properties)
1671 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1674 GList *matching_classes;
1678 g_return_val_if_fail (channel_type != NULL, NULL);
1679 g_return_val_if_fail (handle_type != 0, NULL);
1681 cd = g_hash_table_lookup (priv->connections, connection);
1686 classes = cd->requestable_channels;
1687 if (classes == NULL)
1690 matching_classes = NULL;
1692 for (i = 0; i < classes->len; i++)
1694 class = g_ptr_array_index (classes, i);
1696 if (!channel_class_matches
1697 (class, channel_type, handle_type, fixed_properties))
1700 matching_classes = g_list_prepend (matching_classes, class);
1703 return matching_classes;
1707 find_channel_class_idle_cb (gpointer user_data)
1711 FindChannelRequest *request = user_data;
1713 gboolean is_ready = TRUE;
1714 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1716 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1718 cd = g_hash_table_lookup (priv->connections, request->connection);
1722 else if (cd->requestable_channels == NULL)
1727 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1728 request->connection, request->channel_type, request->handle_type,
1729 request->properties);
1731 request->callback (retval, request->user_data);
1732 free_find_channel_request (request);
1733 g_list_free (retval);
1738 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1739 request->connection);
1740 requests = g_list_prepend (requests, request);
1742 g_hash_table_insert (priv->outstanding_classes_requests,
1743 request->connection, requests);
1749 setup_varargs (va_list var_args,
1750 const char *channel_namespace,
1751 const char *first_property_name)
1757 if (first_property_name == NULL)
1760 name = first_property_name;
1761 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1763 while (name != NULL)
1765 name_full = g_strdup (name);
1766 properties = g_array_append_val (properties, name_full);
1767 name = va_arg (var_args, char *);
1774 * empathy_dispatcher_find_requestable_channel_classes:
1775 * @dispatcher: an #EmpathyDispatcher
1776 * @connection: a #TpConnection
1777 * @channel_type: a string identifying the type of the channel to lookup
1778 * @handle_type: the handle type for the channel
1779 * @first_property_name: %NULL, or the name of the first fixed property,
1780 * followed optionally by more names, followed by %NULL.
1782 * Returns all the channel classes that a client can request for the connection
1783 * @connection, of the type identified by @channel_type, @handle_type and the
1784 * fixed properties list.
1785 * The classes which are compatible with a fixed properties list (i.e. those
1786 * that will be returned by this function) are intended as those that do not
1787 * contain any fixed property other than those in the list; note that this
1788 * doesn't guarantee that all the classes compatible with the list will contain
1789 * all the requested fixed properties, so the clients will have to filter
1790 * the returned list themselves.
1791 * If @first_property_name is %NULL, only the classes with no other fixed
1792 * properties than ChannelType and TargetHandleType will be returned.
1793 * Note that this function may return %NULL without performing any lookup if
1794 * @connection is not ready. To ensure that @connection is always ready,
1795 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1797 * Return value: a #GList of #GValueArray objects, where the first element in
1798 * the array is a #GHashTable of the fixed properties, and the second is
1799 * a #GStrv of the allowed properties for the class. The list should be free'd
1800 * with g_list_free() when done, but the objects inside the list are owned
1801 * by the #EmpathyDispatcher and must not be modified.
1804 empathy_dispatcher_find_requestable_channel_classes
1805 (EmpathyDispatcher *dispatcher,
1806 TpConnection *connection,
1807 const gchar *channel_type,
1809 const char *first_property_name,
1814 EmpathyDispatcherPriv *priv;
1819 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL);
1820 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1821 g_return_val_if_fail (channel_type != NULL, NULL);
1822 g_return_val_if_fail (handle_type != 0, NULL);
1824 priv = GET_PRIV (dispatcher);
1826 va_start (var_args, first_property_name);
1828 properties = setup_varargs (var_args, channel_type, first_property_name);
1832 retval = empathy_dispatcher_find_channel_classes (dispatcher, connection,
1833 channel_type, handle_type, properties);
1835 if (properties != NULL)
1837 /* free the properties array */
1838 for (idx = 0; idx < properties->len ; idx++)
1840 str = g_array_index (properties, char *, idx);
1844 g_array_free (properties, TRUE);
1851 * empathy_dispatcher_find_requestable_channel_classes_async:
1852 * @dispatcher: an #EmpathyDispatcher
1853 * @connection: a #TpConnection
1854 * @channel_type: a string identifying the type of the channel to lookup
1855 * @handle_type: the handle type for the channel
1856 * @callback: the callback to call when @connection is ready
1857 * @user_data: the user data to pass to @callback
1858 * @first_property_name: %NULL, or the name of the first fixed property,
1859 * followed optionally by more names, followed by %NULL.
1861 * Please see the documentation of
1862 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1863 * description of this function.
1866 empathy_dispatcher_find_requestable_channel_classes_async
1867 (EmpathyDispatcher *dispatcher,
1868 TpConnection *connection,
1869 const gchar *channel_type,
1871 EmpathyDispatcherFindChannelClassCb callback,
1873 const char *first_property_name,
1878 FindChannelRequest *request;
1879 EmpathyDispatcherPriv *priv;
1882 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1883 g_return_if_fail (TP_IS_CONNECTION (connection));
1884 g_return_if_fail (channel_type != NULL);
1885 g_return_if_fail (handle_type != 0);
1887 priv = GET_PRIV (dispatcher);
1889 va_start (var_args, first_property_name);
1891 properties = setup_varargs (var_args, channel_type, first_property_name);
1895 /* append another request for this connection */
1896 request = g_slice_new0 (FindChannelRequest);
1897 request->dispatcher = g_object_ref (dispatcher);
1898 request->channel_type = g_strdup (channel_type);
1899 request->handle_type = handle_type;
1900 request->connection = connection;
1901 request->callback = callback;
1902 request->user_data = user_data;
1903 request->properties = properties;
1905 source_id = g_idle_add (find_channel_class_idle_cb, request);
1907 g_hash_table_insert (priv->request_channel_class_async_ids,
1908 request, GUINT_TO_POINTER (source_id));
1912 empathy_dispatcher_get_channels (EmpathyHandler *handler,
1915 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1916 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1918 return priv->channels;
1922 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
1923 const gchar *account_path,
1924 const gchar *connection_path,
1925 const GPtrArray *channels,
1926 const GPtrArray *requests_satisfied,
1928 GHashTable *handler_info,
1932 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1933 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1935 EmpathyAccount *account;
1936 TpConnection *connection;
1938 account = empathy_account_manager_ensure_account (priv->account_manager,
1940 g_assert (account != NULL);
1942 connection = empathy_account_get_connection_for_path (account,
1944 if (connection == NULL)
1946 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1947 "Invalid connection argument");
1951 for (i = 0; i < channels->len ; i++)
1953 GValueArray *arr = g_ptr_array_index (channels, i);
1954 const gchar *object_path;
1955 GHashTable *properties;
1957 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
1958 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
1960 dispatcher_connection_new_channel_with_properties (dispatcher,
1961 connection, object_path, properties);
1969 empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
1974 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1975 EmpathyHandler *handler;
1977 handler = empathy_handler_new (name, filters, capabilities);
1978 priv->handlers = g_list_prepend (priv->handlers, handler);
1980 /* Only set the handle_channels function, the Channel property on the main
1981 * handler will always report all dispatched channels even if they came from
1982 * a different Handler */
1983 empathy_handler_set_handle_channels_func (handler,
1984 empathy_dispatcher_handle_channels,
1991 empathy_dispatcher_remove_handler (EmpathyDispatcher *dispatcher,
1992 EmpathyHandler *handler)
1994 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1997 h = g_list_find (priv->handlers, handler);
1998 g_return_if_fail (h != NULL);
2000 priv->handlers = g_list_delete_link (priv->handlers, h);
2002 g_object_unref (handler);