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));
812 dispatcher_connection_new_channel_with_properties (dispatcher,
813 TP_CONNECTION (proxy), object_path, properties);
819 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
820 const GPtrArray *capabilities,
826 DEBUG ("Error: %s", error->message);
830 dispatcher_init_connection_if_needed (EmpathyDispatcher *dispatcher,
831 TpConnection *connection)
833 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
834 GPtrArray *capabilities;
837 const gchar *remove = NULL;
839 if (g_hash_table_lookup (priv->connections, connection) != NULL)
842 g_hash_table_insert (priv->connections, g_object_ref (connection),
843 new_connection_data ());
845 g_signal_connect (connection, "invalidated",
846 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
848 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
849 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
851 tp_cli_dbus_properties_call_get_all (connection, -1,
852 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
853 dispatcher_connection_got_all,
854 NULL, NULL, G_OBJECT (dispatcher));
857 /* Advertise VoIP capabilities */
858 capabilities = g_ptr_array_sized_new (1);
859 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
860 G_TYPE_UINT, G_TYPE_INVALID);
861 g_value_init (&cap, cap_type);
862 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
863 dbus_g_type_struct_set (&cap,
864 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
865 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
866 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
867 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
868 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
869 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
871 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
872 connection, -1, capabilities, &remove,
873 dispatcher_connection_advertise_capabilities_cb,
874 NULL, NULL, G_OBJECT (dispatcher));
876 g_value_unset (&cap);
877 g_ptr_array_free (capabilities, TRUE);
881 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
882 TpConnection *connection,
883 EmpathyDispatcher *dispatcher)
885 dispatcher_init_connection_if_needed (dispatcher, connection);
889 remove_idle_handlers (gpointer key,
895 source_id = GPOINTER_TO_UINT (value);
896 g_source_remove (source_id);
900 dispatcher_constructor (GType type,
901 guint n_construct_params,
902 GObjectConstructParam *construct_params)
905 EmpathyDispatcherPriv *priv;
907 if (dispatcher != NULL)
908 return g_object_ref (dispatcher);
910 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
911 (type, n_construct_params, construct_params);
913 dispatcher = EMPATHY_DISPATCHER (retval);
914 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
916 priv = GET_PRIV (dispatcher);
918 if (priv->handler == NULL)
919 priv->handler = empathy_handler_new (NULL, NULL, NULL);
921 empathy_handler_set_handle_channels_func (priv->handler,
922 empathy_dispatcher_handle_channels,
925 empathy_handler_set_channels_func (priv->handler,
926 empathy_dispatcher_get_channels,
933 dispatcher_dispose (GObject *object)
935 EmpathyDispatcherPriv *priv = GET_PRIV (object);
938 if (priv->dispose_has_run)
941 priv->dispose_has_run = TRUE;
943 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
944 g_object_unref (G_OBJECT (l->data));
946 g_list_free (priv->handlers);
947 priv->handlers = NULL;
949 if (priv->handler != NULL)
950 g_object_unref (priv->handler);
951 priv->handler = NULL;
953 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
957 dispatcher_finalize (GObject *object)
959 EmpathyDispatcherPriv *priv = GET_PRIV (object);
965 if (priv->request_channel_class_async_ids != NULL)
967 g_hash_table_foreach (priv->request_channel_class_async_ids,
968 remove_idle_handlers, NULL);
969 g_hash_table_destroy (priv->request_channel_class_async_ids);
972 g_signal_handlers_disconnect_by_func (priv->account_manager,
973 dispatcher_new_connection_cb, object);
975 for (l = priv->channels; l; l = l->next)
977 g_signal_handlers_disconnect_by_func (l->data,
978 dispatcher_channel_invalidated_cb, object);
981 g_list_free (priv->channels);
983 g_hash_table_iter_init (&iter, priv->connections);
984 while (g_hash_table_iter_next (&iter, &connection, NULL))
986 g_signal_handlers_disconnect_by_func (connection,
987 dispatcher_connection_invalidated_cb, object);
990 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
991 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
993 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
997 g_object_unref (priv->account_manager);
999 g_hash_table_destroy (priv->connections);
1000 g_hash_table_destroy (priv->outstanding_classes_requests);
1004 dispatcher_set_property (GObject *object,
1006 const GValue *value,
1009 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1010 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1012 switch (property_id)
1015 priv->handler = g_value_dup_object (value);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1024 dispatcher_get_property (GObject *object,
1029 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1030 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1032 switch (property_id)
1035 g_value_set_object (value, priv->handler);
1038 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1044 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1046 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1047 GParamSpec *param_spec;
1049 object_class->dispose = dispatcher_dispose;
1050 object_class->finalize = dispatcher_finalize;
1051 object_class->constructor = dispatcher_constructor;
1053 object_class->get_property = dispatcher_get_property;
1054 object_class->set_property = dispatcher_set_property;
1056 param_spec = g_param_spec_object ("handler", "handler",
1057 "The main Telepathy Client Hander object",
1058 EMPATHY_TYPE_HANDLER,
1059 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1060 g_object_class_install_property (object_class,
1061 PROP_HANDLER, param_spec);
1064 g_signal_new ("observe",
1065 G_TYPE_FROM_CLASS (klass),
1069 g_cclosure_marshal_VOID__OBJECT,
1071 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1074 g_signal_new ("approve",
1075 G_TYPE_FROM_CLASS (klass),
1079 g_cclosure_marshal_VOID__OBJECT,
1081 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1084 g_signal_new ("dispatch",
1085 G_TYPE_FROM_CLASS (klass),
1089 g_cclosure_marshal_VOID__OBJECT,
1091 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1094 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1098 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
1100 GList *connections, *l;
1101 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
1102 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1104 dispatcher->priv = priv;
1105 priv->account_manager = empathy_account_manager_dup_singleton ();
1107 g_signal_connect (priv->account_manager, "new-connection",
1108 G_CALLBACK (dispatcher_new_connection_cb),
1111 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1112 g_object_unref, (GDestroyNotify) free_connection_data);
1114 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1115 g_direct_equal, g_object_unref, NULL);
1117 priv->channels = NULL;
1119 connections = empathy_account_manager_dup_connections (
1120 priv->account_manager);
1121 for (l = connections; l; l = l->next)
1123 dispatcher_new_connection_cb (priv->account_manager, l->data,
1125 g_object_unref (l->data);
1127 g_list_free (connections);
1129 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1134 empathy_dispatcher_new (const gchar *name,
1138 g_assert (dispatcher == NULL);
1139 EmpathyHandler *handler;
1140 EmpathyDispatcher *ret;
1142 handler = empathy_handler_new (name, filters, capabilities);
1144 ret = EMPATHY_DISPATCHER (
1145 g_object_new (EMPATHY_TYPE_DISPATCHER,
1148 g_object_unref (handler);
1154 empathy_dispatcher_dup_singleton (void)
1156 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1160 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1161 DispatcherRequestData *request_data,
1162 const GError *error)
1164 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1165 ConnectionData *conn_data;
1167 conn_data = g_hash_table_lookup (priv->connections,
1168 request_data->connection);
1169 if (request_data->cb != NULL)
1170 request_data->cb (NULL, error, request_data->user_data);
1172 if (conn_data != NULL)
1174 conn_data->outstanding_requests =
1175 g_list_remove (conn_data->outstanding_requests, request_data);
1177 /* else Connection has been invalidated */
1179 free_dispatcher_request_data (request_data);
1183 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1184 DispatcherRequestData *request_data,
1185 const gchar *object_path,
1186 GHashTable *properties,
1187 const GError *error)
1189 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1190 EmpathyDispatchOperation *operation = NULL;
1191 ConnectionData *conn_data;
1193 conn_data = g_hash_table_lookup (priv->connections,
1194 request_data->connection);
1198 DEBUG ("Channel request failed: %s", error->message);
1200 dispatcher_request_failed (dispatcher, request_data, error);
1205 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1208 if (operation != NULL)
1209 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1211 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1214 if (operation == NULL)
1216 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1221 operation = empathy_dispatch_operation_new_with_wrapper (
1222 request_data->connection,
1223 data->channel, request_data->contact, FALSE,
1224 data->channel_wrapper);
1230 if (properties != NULL)
1231 channel = tp_channel_new_from_properties (request_data->connection,
1232 object_path, properties, NULL);
1234 channel = tp_channel_new (request_data->connection, object_path,
1235 request_data->channel_type, request_data->handle_type,
1236 request_data->handle, NULL);
1238 g_signal_connect (channel, "invalidated",
1239 G_CALLBACK (dispatcher_channel_invalidated_cb),
1240 request_data->dispatcher);
1242 priv->channels = g_list_prepend (priv->channels, channel);
1244 operation = empathy_dispatch_operation_new (request_data->connection,
1245 channel, request_data->contact, FALSE);
1246 g_object_unref (channel);
1251 /* Already existed set potential extra information */
1252 g_object_set (G_OBJECT (operation),
1253 "contact", request_data->contact,
1257 request_data->operation = operation;
1259 /* (pre)-approve this right away as we requested it
1260 * This might cause the channel to be claimed, in which case the operation
1261 * will disappear. So ref it, and check the status before starting the
1264 g_object_ref (operation);
1265 empathy_dispatch_operation_approve (operation);
1267 if (empathy_dispatch_operation_get_status (operation) <
1268 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1269 dispatcher_start_dispatching (request_data->dispatcher, operation,
1272 g_object_unref (operation);
1275 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1280 dispatcher_request_channel_cb (TpConnection *connection,
1281 const gchar *object_path,
1282 const GError *error,
1284 GObject *weak_object)
1286 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1287 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1289 dispatcher_connection_new_requested_channel (dispatcher,
1290 request_data, object_path, NULL, error);
1294 dispatcher_request_channel (DispatcherRequestData *request_data)
1296 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1297 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1299 /* Extend the request_data to be a valid request */
1300 g_assert (request_data->request == NULL);
1301 request_data->request = tp_asv_new (
1302 TP_IFACE_CHANNEL ".ChannelType",
1303 G_TYPE_STRING, request_data->channel_type,
1304 TP_IFACE_CHANNEL ".TargetHandleType",
1305 G_TYPE_UINT, request_data->handle_type,
1308 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1309 tp_asv_set_uint32 (request_data->request,
1310 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1312 empathy_dispatcher_call_create_or_ensure_channel (
1313 request_data->dispatcher, request_data);
1317 tp_cli_connection_call_request_channel (request_data->connection, -1,
1318 request_data->channel_type,
1319 request_data->handle_type,
1320 request_data->handle,
1321 TRUE, dispatcher_request_channel_cb,
1322 request_data, NULL, G_OBJECT (request_data->dispatcher));
1327 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1328 EmpathyDispatcherRequestCb *callback,
1331 EmpathyDispatcher *dispatcher;
1332 EmpathyDispatcherPriv *priv;
1333 TpConnection *connection;
1334 ConnectionData *connection_data;
1335 DispatcherRequestData *request_data;
1337 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1339 dispatcher = empathy_dispatcher_dup_singleton ();
1340 priv = GET_PRIV (dispatcher);
1342 connection = empathy_contact_get_connection (contact);
1343 connection_data = g_hash_table_lookup (priv->connections, connection);
1345 /* The contact handle might not be known yet */
1346 request_data = new_dispatcher_request_data (dispatcher, connection,
1347 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1348 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1349 request_data->should_ensure = TRUE;
1351 connection_data->outstanding_requests = g_list_prepend
1352 (connection_data->outstanding_requests, request_data);
1354 dispatcher_request_channel (request_data);
1356 g_object_unref (dispatcher);
1361 EmpathyDispatcher *dispatcher;
1362 EmpathyDispatcherRequestCb *callback;
1364 } ChatWithContactIdData;
1367 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1368 EmpathyContact *contact,
1369 const GError *error,
1371 GObject *weak_object)
1373 ChatWithContactIdData *data = user_data;
1377 /* FIXME: Should call data->callback with the error */
1378 DEBUG ("Error: %s", error->message);
1382 empathy_dispatcher_chat_with_contact (contact, data->callback,
1386 g_object_unref (data->dispatcher);
1387 g_slice_free (ChatWithContactIdData, data);
1391 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1392 const gchar *contact_id,
1393 EmpathyDispatcherRequestCb *callback,
1396 EmpathyDispatcher *dispatcher;
1397 EmpathyTpContactFactory *factory;
1398 ChatWithContactIdData *data;
1400 g_return_if_fail (TP_IS_CONNECTION (connection));
1401 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1403 dispatcher = empathy_dispatcher_dup_singleton ();
1404 factory = empathy_tp_contact_factory_dup_singleton (connection);
1405 data = g_slice_new0 (ChatWithContactIdData);
1406 data->dispatcher = dispatcher;
1407 data->callback = callback;
1408 data->user_data = user_data;
1409 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1410 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1412 g_object_unref (factory);
1416 dispatcher_request_handles_cb (TpConnection *connection,
1417 const GArray *handles,
1418 const GError *error,
1422 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1426 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1427 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1430 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1432 if (request_data->cb)
1433 request_data->cb (NULL, error, request_data->user_data);
1435 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1438 free_dispatcher_request_data (request_data);
1440 dispatcher_flush_outstanding_operations (dispatcher, cd);
1444 request_data->handle = g_array_index (handles, guint, 0);
1445 dispatcher_request_channel (request_data);
1449 empathy_dispatcher_join_muc (TpConnection *connection,
1450 const gchar *roomname,
1451 EmpathyDispatcherRequestCb *callback,
1454 EmpathyDispatcher *dispatcher;
1455 EmpathyDispatcherPriv *priv;
1456 DispatcherRequestData *request_data;
1457 ConnectionData *connection_data;
1458 const gchar *names[] = { roomname, NULL };
1460 g_return_if_fail (TP_IS_CONNECTION (connection));
1461 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1463 dispatcher = empathy_dispatcher_dup_singleton ();
1464 priv = GET_PRIV (dispatcher);
1466 connection_data = g_hash_table_lookup (priv->connections, connection);
1468 /* Don't know the room handle yet */
1469 request_data = new_dispatcher_request_data (dispatcher, connection,
1470 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1471 NULL, callback, user_data);
1473 connection_data->outstanding_requests = g_list_prepend
1474 (connection_data->outstanding_requests, request_data);
1476 tp_cli_connection_call_request_handles (connection, -1,
1477 TP_HANDLE_TYPE_ROOM, names,
1478 dispatcher_request_handles_cb, request_data, NULL,
1479 G_OBJECT (dispatcher));
1481 g_object_unref (dispatcher);
1485 dispatcher_create_channel_cb (TpConnection *connect,
1486 const gchar *object_path,
1487 GHashTable *properties,
1488 const GError *error,
1490 GObject *weak_object)
1492 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1493 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1495 dispatcher_connection_new_requested_channel (dispatcher,
1496 request_data, object_path, properties, error);
1500 dispatcher_ensure_channel_cb (TpConnection *connect,
1502 const gchar *object_path,
1503 GHashTable *properties,
1504 const GError *error,
1506 GObject *weak_object)
1508 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1509 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1511 dispatcher_connection_new_requested_channel (dispatcher,
1512 request_data, object_path, properties, error);
1516 empathy_dispatcher_call_create_or_ensure_channel (
1517 EmpathyDispatcher *dispatcher,
1518 DispatcherRequestData *request_data)
1520 if (request_data->should_ensure)
1522 tp_cli_connection_interface_requests_call_ensure_channel (
1523 request_data->connection, -1,
1524 request_data->request, dispatcher_ensure_channel_cb,
1525 request_data, NULL, G_OBJECT (request_data->dispatcher));
1529 tp_cli_connection_interface_requests_call_create_channel (
1530 request_data->connection, -1,
1531 request_data->request, dispatcher_create_channel_cb,
1532 request_data, NULL, G_OBJECT (request_data->dispatcher));
1537 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1538 TpConnection *connection,
1539 GHashTable *request,
1540 EmpathyDispatcherRequestCb *callback,
1543 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1544 ConnectionData *connection_data;
1545 DispatcherRequestData *request_data;
1546 const gchar *channel_type;
1551 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1552 g_return_if_fail (TP_IS_CONNECTION (connection));
1553 g_return_if_fail (request != NULL);
1555 connection_data = g_hash_table_lookup (priv->connections, connection);
1556 g_assert (connection_data != NULL);
1558 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1560 handle_type = tp_asv_get_uint32 (request,
1561 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1563 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1565 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1567 request_data = new_dispatcher_request_data (dispatcher, connection,
1568 channel_type, handle_type, handle, request,
1569 NULL, callback, user_data);
1571 connection_data->outstanding_requests = g_list_prepend
1572 (connection_data->outstanding_requests, request_data);
1574 empathy_dispatcher_call_create_or_ensure_channel (dispatcher, request_data);
1578 channel_class_matches (GValueArray *class,
1579 const char *channel_type,
1581 GArray *fixed_properties)
1589 v = g_value_array_get_nth (class, 0);
1591 /* if the class doesn't match channel type discard it. */
1592 fprops = g_value_get_boxed (v);
1593 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1595 if (tp_strdiff (channel_type, c_type))
1598 /* we have the right channel type, see if the handle type matches */
1599 h_type = tp_asv_get_uint32 (fprops,
1600 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1602 if (!valid || handle_type != h_type)
1605 if (fixed_properties != NULL)
1607 gpointer h_key, h_val;
1609 GHashTableIter iter;
1612 g_hash_table_iter_init (&iter, fprops);
1614 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1616 /* discard ChannelType and TargetHandleType, as we already
1619 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1621 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1626 for (idx = 0; idx < fixed_properties->len; idx++)
1628 /* if |key| doesn't exist in |fixed_properties|, discard
1633 g_array_index (fixed_properties, char *, idx)))
1636 /* exit the for() loop */
1647 /* if no fixed_properties are specified, discard the classes
1648 * with some fixed properties other than the two we already
1651 if (g_hash_table_size (fprops) > 2)
1659 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher,
1660 TpConnection *connection,
1661 const gchar *channel_type,
1663 GArray *fixed_properties)
1665 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1668 GList *matching_classes;
1672 g_return_val_if_fail (channel_type != NULL, NULL);
1673 g_return_val_if_fail (handle_type != 0, NULL);
1675 cd = g_hash_table_lookup (priv->connections, connection);
1680 classes = cd->requestable_channels;
1681 if (classes == NULL)
1684 matching_classes = NULL;
1686 for (i = 0; i < classes->len; i++)
1688 class = g_ptr_array_index (classes, i);
1690 if (!channel_class_matches
1691 (class, channel_type, handle_type, fixed_properties))
1694 matching_classes = g_list_prepend (matching_classes, class);
1697 return matching_classes;
1701 find_channel_class_idle_cb (gpointer user_data)
1705 FindChannelRequest *request = user_data;
1707 gboolean is_ready = TRUE;
1708 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1710 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1712 cd = g_hash_table_lookup (priv->connections, request->connection);
1716 else if (cd->requestable_channels == NULL)
1721 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1722 request->connection, request->channel_type, request->handle_type,
1723 request->properties);
1725 request->callback (retval, request->user_data);
1726 free_find_channel_request (request);
1727 g_list_free (retval);
1732 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1733 request->connection);
1734 requests = g_list_prepend (requests, request);
1736 g_hash_table_insert (priv->outstanding_classes_requests,
1737 request->connection, requests);
1743 setup_varargs (va_list var_args,
1744 const char *channel_namespace,
1745 const char *first_property_name)
1751 if (first_property_name == NULL)
1754 name = first_property_name;
1755 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1757 while (name != NULL)
1759 name_full = g_strdup (name);
1760 properties = g_array_append_val (properties, name_full);
1761 name = va_arg (var_args, char *);
1768 * empathy_dispatcher_find_requestable_channel_classes:
1769 * @dispatcher: an #EmpathyDispatcher
1770 * @connection: a #TpConnection
1771 * @channel_type: a string identifying the type of the channel to lookup
1772 * @handle_type: the handle type for the channel
1773 * @first_property_name: %NULL, or the name of the first fixed property,
1774 * followed optionally by more names, followed by %NULL.
1776 * Returns all the channel classes that a client can request for the connection
1777 * @connection, of the type identified by @channel_type, @handle_type and the
1778 * fixed properties list.
1779 * The classes which are compatible with a fixed properties list (i.e. those
1780 * that will be returned by this function) are intended as those that do not
1781 * contain any fixed property other than those in the list; note that this
1782 * doesn't guarantee that all the classes compatible with the list will contain
1783 * all the requested fixed properties, so the clients will have to filter
1784 * the returned list themselves.
1785 * If @first_property_name is %NULL, only the classes with no other fixed
1786 * properties than ChannelType and TargetHandleType will be returned.
1787 * Note that this function may return %NULL without performing any lookup if
1788 * @connection is not ready. To ensure that @connection is always ready,
1789 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1791 * Return value: a #GList of #GValueArray objects, where the first element in
1792 * the array is a #GHashTable of the fixed properties, and the second is
1793 * a #GStrv of the allowed properties for the class. The list should be free'd
1794 * with g_list_free() when done, but the objects inside the list are owned
1795 * by the #EmpathyDispatcher and must not be modified.
1798 empathy_dispatcher_find_requestable_channel_classes
1799 (EmpathyDispatcher *dispatcher,
1800 TpConnection *connection,
1801 const gchar *channel_type,
1803 const char *first_property_name,
1808 EmpathyDispatcherPriv *priv;
1813 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL);
1814 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1815 g_return_val_if_fail (channel_type != NULL, NULL);
1816 g_return_val_if_fail (handle_type != 0, NULL);
1818 priv = GET_PRIV (dispatcher);
1820 va_start (var_args, first_property_name);
1822 properties = setup_varargs (var_args, channel_type, first_property_name);
1826 retval = empathy_dispatcher_find_channel_classes (dispatcher, connection,
1827 channel_type, handle_type, properties);
1829 if (properties != NULL)
1831 /* free the properties array */
1832 for (idx = 0; idx < properties->len ; idx++)
1834 str = g_array_index (properties, char *, idx);
1838 g_array_free (properties, TRUE);
1845 * empathy_dispatcher_find_requestable_channel_classes_async:
1846 * @dispatcher: an #EmpathyDispatcher
1847 * @connection: a #TpConnection
1848 * @channel_type: a string identifying the type of the channel to lookup
1849 * @handle_type: the handle type for the channel
1850 * @callback: the callback to call when @connection is ready
1851 * @user_data: the user data to pass to @callback
1852 * @first_property_name: %NULL, or the name of the first fixed property,
1853 * followed optionally by more names, followed by %NULL.
1855 * Please see the documentation of
1856 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1857 * description of this function.
1860 empathy_dispatcher_find_requestable_channel_classes_async
1861 (EmpathyDispatcher *dispatcher,
1862 TpConnection *connection,
1863 const gchar *channel_type,
1865 EmpathyDispatcherFindChannelClassCb callback,
1867 const char *first_property_name,
1872 FindChannelRequest *request;
1873 EmpathyDispatcherPriv *priv;
1876 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1877 g_return_if_fail (TP_IS_CONNECTION (connection));
1878 g_return_if_fail (channel_type != NULL);
1879 g_return_if_fail (handle_type != 0);
1881 priv = GET_PRIV (dispatcher);
1883 va_start (var_args, first_property_name);
1885 properties = setup_varargs (var_args, channel_type, first_property_name);
1889 /* append another request for this connection */
1890 request = g_slice_new0 (FindChannelRequest);
1891 request->dispatcher = g_object_ref (dispatcher);
1892 request->channel_type = g_strdup (channel_type);
1893 request->handle_type = handle_type;
1894 request->connection = connection;
1895 request->callback = callback;
1896 request->user_data = user_data;
1897 request->properties = properties;
1899 source_id = g_idle_add (find_channel_class_idle_cb, request);
1901 g_hash_table_insert (priv->request_channel_class_async_ids,
1902 request, GUINT_TO_POINTER (source_id));
1906 empathy_dispatcher_get_channels (EmpathyHandler *handler,
1909 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1910 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1912 return priv->channels;
1916 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
1917 const gchar *account_path,
1918 const gchar *connection_path,
1919 const GPtrArray *channels,
1920 const GPtrArray *requests_satisfied,
1922 GHashTable *handler_info,
1926 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (user_data);
1927 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1929 EmpathyAccount *account;
1930 TpConnection *connection;
1932 account = empathy_account_manager_ensure_account (priv->account_manager,
1934 g_assert (account != NULL);
1936 connection = empathy_account_get_connection_for_path (account,
1938 if (connection == NULL)
1940 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1941 "Invalid connection argument");
1945 for (i = 0; i < channels->len ; i++)
1947 GValueArray *arr = g_ptr_array_index (channels, i);
1948 const gchar *object_path;
1949 GHashTable *properties;
1951 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
1952 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
1954 dispatcher_connection_new_channel_with_properties (dispatcher,
1955 connection, object_path, properties);
1963 empathy_dispatcher_add_handler (EmpathyDispatcher *dispatcher,
1968 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1969 EmpathyHandler *handler;
1971 handler = empathy_handler_new (name, filters, capabilities);
1972 priv->handlers = g_list_prepend (priv->handlers, handler);
1974 /* Only set the handle_channels function, the Channel property on the main
1975 * handler will always report all dispatched channels even if they came from
1976 * a different Handler */
1977 empathy_handler_set_handle_channels_func (handler,
1978 empathy_dispatcher_handle_channels,
1985 empathy_dispatcher_remove_handler (EmpathyDispatcher *dispatcher,
1986 EmpathyHandler *handler)
1988 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1991 h = g_list_find (priv->handlers, handler);
1992 g_return_if_fail (h != NULL);
1994 priv->handlers = g_list_delete_link (priv->handlers, h);
1996 g_object_unref (handler);