1 /* * Copyright (C) 2007-2009 Collabora Ltd.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * Authors: Xavier Claessens <xclaesse@gmail.com>
18 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 #define DISPATCHER_BUS_NAME TP_CLIENT_BUS_NAME_BASE "Empathy"
23 #define DISPATCHER_OBJECT_PATH TP_CLIENT_OBJECT_PATH_BASE "Empathy"
29 #include <glib/gi18n-lib.h>
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/enums.h>
33 #include <telepathy-glib/connection.h>
34 #include <telepathy-glib/channel-dispatcher.h>
35 #include <telepathy-glib/channel-request.h>
36 #include <telepathy-glib/util.h>
37 #include <telepathy-glib/dbus.h>
38 #include <telepathy-glib/proxy-subclass.h>
39 #include <telepathy-glib/gtypes.h>
40 #include <telepathy-glib/defs.h>
41 #include <telepathy-glib/svc-client.h>
42 #include <telepathy-glib/svc-generic.h>
43 #include <telepathy-glib/interfaces.h>
45 #include <extensions/extensions.h>
47 #include "empathy-dispatcher.h"
48 #include "empathy-handler.h"
49 #include "empathy-utils.h"
50 #include "empathy-tp-contact-factory.h"
51 #include "empathy-chatroom-manager.h"
52 #include "empathy-utils.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
55 #include <libempathy/empathy-debug.h>
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
60 gboolean dispose_has_run;
62 TpAccountManager *account_manager;
63 /* connection to connection data mapping */
64 GHashTable *connections;
65 GHashTable *outstanding_classes_requests;
68 /* channels which the dispatcher is listening "invalidated" */
73 EmpathyHandler *handler;
78 GHashTable *request_channel_class_async_ids;
79 /* reffed (TpAccount *) => gulong
80 * Signal handler ID of the "status-changed" signal */
81 GHashTable *status_changed_handlers;
83 TpChannelDispatcher *channel_dispatcher;
85 } EmpathyDispatcherPriv;
88 empathy_dispatcher_get_channels (EmpathyHandler *handler,
92 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
93 const gchar *account_path,
94 const gchar *connection_path,
95 const GPtrArray *channels,
96 const GPtrArray *requests_satisfied,
98 GHashTable *handler_info,
102 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
117 static guint signals[LAST_SIGNAL];
118 static EmpathyDispatcher *dispatcher = NULL;
120 static void dispatcher_init_connection_if_needed (
121 EmpathyDispatcher *dispatcher,
122 TpConnection *connection);
124 static GList * empathy_dispatcher_find_channel_classes
125 (EmpathyDispatcher *dispatcher, TpConnection *connection,
126 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
131 EmpathyDispatcher *dispatcher;
132 EmpathyDispatchOperation *operation;
133 TpConnection *connection;
134 gboolean should_ensure;
138 EmpathyContact *contact;
139 TpProxyPendingCall *pending_call;
141 /* Properties to pass to the channel when requesting it */
144 EmpathyDispatcherRequestCb *cb;
146 gpointer *request_data;
148 TpChannelRequest *channel_request;
149 } DispatcherRequestData;
154 /* Channel type specific wrapper object */
155 GObject *channel_wrapper;
160 /* ObjectPath => DispatchData.. */
161 GHashTable *dispatched_channels;
162 /* ObjectPath -> EmpathyDispatchOperations */
163 GHashTable *dispatching_channels;
165 /* List of DispatcherRequestData */
166 GList *outstanding_requests;
167 /* List of requestable channel classes */
168 GPtrArray *requestable_channels;
173 EmpathyDispatcher *dispatcher;
174 TpConnection *connection;
178 EmpathyDispatcherFindChannelClassCb *callback;
180 } FindChannelRequest;
183 empathy_dispatcher_call_create_or_ensure_channel (
184 EmpathyDispatcher *dispatcher,
185 DispatcherRequestData *request_data);
188 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
189 DispatcherRequestData *request_data,
190 const GError *error);
192 static DispatchData *
193 new_dispatch_data (TpChannel *channel,
194 GObject *channel_wrapper)
196 DispatchData *d = g_slice_new0 (DispatchData);
197 d->channel = g_object_ref (channel);
198 if (channel_wrapper != NULL)
199 d->channel_wrapper = g_object_ref (channel_wrapper);
205 free_dispatch_data (DispatchData *data)
207 g_object_unref (data->channel);
208 if (data->channel_wrapper != NULL)
209 g_object_unref (data->channel_wrapper);
211 g_slice_free (DispatchData, data);
214 static DispatcherRequestData *
215 new_dispatcher_request_data (EmpathyDispatcher *self,
216 TpConnection *connection,
217 const gchar *channel_type,
222 EmpathyContact *contact,
223 EmpathyDispatcherRequestCb *cb,
226 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
228 result->dispatcher = g_object_ref (self);
229 result->connection = connection;
231 result->should_ensure = FALSE;
233 result->channel_type = g_strdup (channel_type);
234 result->handle_type = handle_type;
235 result->handle = handle;
236 result->request = request;
237 result->timestamp = timestamp;
240 result->contact = g_object_ref (contact);
243 result->user_data = user_data;
249 free_dispatcher_request_data (DispatcherRequestData *r)
251 g_free (r->channel_type);
253 if (r->dispatcher != NULL)
254 g_object_unref (r->dispatcher);
256 if (r->contact != NULL)
257 g_object_unref (r->contact);
259 if (r->request != NULL)
260 g_hash_table_unref (r->request);
263 if (r->pending_call != NULL)
264 tp_proxy_pending_call_cancel (r->pending_call);
266 if (r->channel_request != NULL)
267 g_object_unref (r->channel_request);
269 g_slice_free (DispatcherRequestData, r);
272 static ConnectionData *
273 new_connection_data (void)
275 ConnectionData *cd = g_slice_new0 (ConnectionData);
277 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
278 g_free, (GDestroyNotify) free_dispatch_data);
280 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
281 g_free, g_object_unref);
287 free_connection_data (ConnectionData *cd)
292 g_hash_table_destroy (cd->dispatched_channels);
293 g_hash_table_destroy (cd->dispatching_channels);
295 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
297 free_dispatcher_request_data (l->data);
300 if (cd->requestable_channels != NULL)
302 for (i = 0 ; i < cd->requestable_channels->len ; i++)
304 g_ptr_array_index (cd->requestable_channels, i));
305 g_ptr_array_free (cd->requestable_channels, TRUE);
310 free_find_channel_request (FindChannelRequest *r)
315 g_object_unref (r->dispatcher);
316 g_free (r->channel_type);
318 if (r->properties != NULL)
320 for (idx = 0; idx < r->properties->len ; idx++)
322 str = g_array_index (r->properties, char *, idx);
326 g_array_free (r->properties, TRUE);
329 g_slice_free (FindChannelRequest, r);
333 dispatcher_connection_invalidated_cb (TpConnection *connection,
337 EmpathyDispatcher *self)
339 EmpathyDispatcherPriv *priv = GET_PRIV (self);
340 ConnectionData *connection_data;
342 DEBUG ("Error: %s", message);
344 /* Terminate pending requests, if any */
345 connection_data = g_hash_table_lookup (priv->connections, connection);
346 if (connection_data != NULL)
349 GError *error = NULL;
351 error = g_error_new_literal (domain, code, message);
353 for (l = connection_data->outstanding_requests; l != NULL;
356 DispatcherRequestData *request_data = l->data;
358 if (request_data->cb != NULL)
359 request_data->cb (NULL, error, request_data->user_data);
362 g_error_free (error);
365 g_hash_table_remove (priv->connections, connection);
369 dispatch_operation_flush_requests (EmpathyDispatcher *self,
370 EmpathyDispatchOperation *operation,
376 l = cd->outstanding_requests;
379 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
384 if (d->operation == operation)
389 d->cb (NULL, error, d->user_data);
391 d->cb (operation, NULL, d->user_data);
394 cd->outstanding_requests = g_list_delete_link
395 (cd->outstanding_requests, lt);
397 free_dispatcher_request_data (d);
403 dispatcher_channel_invalidated_cb (TpProxy *proxy,
407 EmpathyDispatcher *self)
409 /* Channel went away... */
410 EmpathyDispatcherPriv *priv = GET_PRIV (self);
411 TpConnection *connection;
413 const gchar *object_path;
415 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
417 priv->channels = g_list_remove (priv->channels, proxy);
419 cd = g_hash_table_lookup (priv->connections, connection);
420 /* Connection itself invalidated? */
424 object_path = tp_proxy_get_object_path (proxy);
426 DEBUG ("Channel %s invalidated", object_path);
428 g_hash_table_remove (cd->dispatched_channels, object_path);
429 g_hash_table_remove (cd->dispatching_channels, object_path);
433 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
434 EmpathyDispatcher *self)
436 /* Our job is done, remove the dispatch operation and mark the channel as
438 EmpathyDispatcherPriv *priv = GET_PRIV (self);
439 TpConnection *connection;
441 const gchar *object_path;
443 connection = empathy_dispatch_operation_get_tp_connection (operation);
444 cd = g_hash_table_lookup (priv->connections, connection);
445 g_assert (cd != NULL);
447 object_path = empathy_dispatch_operation_get_object_path (operation);
449 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
452 d = new_dispatch_data (
453 empathy_dispatch_operation_get_channel (operation),
454 empathy_dispatch_operation_get_channel_wrapper (operation));
455 g_hash_table_insert (cd->dispatched_channels,
456 g_strdup (object_path), d);
458 g_hash_table_remove (cd->dispatching_channels, object_path);
460 DEBUG ("Channel claimed: %s", object_path);
464 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
465 EmpathyDispatcher *self)
467 EmpathyDispatcherPriv *priv = GET_PRIV (self);
468 TpConnection *connection;
470 EmpathyDispatchOperationState status;
472 g_signal_connect (operation, "claimed",
473 G_CALLBACK (dispatch_operation_claimed_cb), self);
475 /* Signal the observers */
476 DEBUG ("Send to observers: %s",
477 empathy_dispatch_operation_get_object_path (operation));
478 g_signal_emit (self, signals[OBSERVE], 0, operation);
480 empathy_dispatch_operation_start (operation);
482 /* Signal potential requestors */
483 connection = empathy_dispatch_operation_get_tp_connection (operation);
484 cd = g_hash_table_lookup (priv->connections, connection);
485 g_assert (cd != NULL);
487 g_object_ref (operation);
490 dispatch_operation_flush_requests (self, operation, NULL, cd);
491 status = empathy_dispatch_operation_get_status (operation);
492 g_object_unref (operation);
494 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
497 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
498 DEBUG ("Send of for dispatching: %s",
499 empathy_dispatch_operation_get_object_path (operation));
500 g_signal_emit (self, signals[DISPATCH], 0, operation);
502 g_object_unref (self);
506 dispatcher_start_dispatching (EmpathyDispatcher *self,
507 EmpathyDispatchOperation *operation,
510 const gchar *object_path =
511 empathy_dispatch_operation_get_object_path (operation);
513 DEBUG ("Dispatching process started for %s", object_path);
515 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
517 g_hash_table_insert (cd->dispatching_channels,
518 g_strdup (object_path), operation);
520 switch (empathy_dispatch_operation_get_status (operation))
522 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
523 g_signal_connect (operation, "ready",
524 G_CALLBACK (dispatch_operation_ready_cb), self);
526 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
527 dispatch_operation_ready_cb (operation, self);
529 case EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING:
530 case EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED:
531 case EMPATHY_DISPATCHER_OPERATION_STATE_INVALIDATED:
533 g_assert_not_reached ();
537 else if (empathy_dispatch_operation_get_status (operation) >=
538 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
540 /* Already dispatching and the operation is pending, thus the observers
541 * have seen it (if applicable), so we can flush the request right away.
543 dispatch_operation_flush_requests (self, operation, NULL, cd);
548 dispatcher_connection_new_channel (EmpathyDispatcher *self,
549 TpConnection *connection,
550 const gchar *object_path,
551 const gchar *channel_type,
554 GHashTable *properties,
556 const GPtrArray *requests_satisfied,
559 EmpathyDispatcherPriv *priv = GET_PRIV (self);
562 EmpathyDispatchOperation *operation;
564 /* Channel types we never want to dispatch because they're either deprecated
565 * or can't sensibly be dispatch (e.g. channels that should always be
567 const char *blacklist[] = {
568 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
569 TP_IFACE_CHANNEL_TYPE_TUBES,
570 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
574 dispatcher_init_connection_if_needed (self, connection);
576 cd = g_hash_table_lookup (priv->connections, connection);
578 for (i = 0 ; blacklist[i] != NULL; i++)
580 if (!tp_strdiff (channel_type, blacklist[i]))
582 DEBUG ("Ignoring blacklisted channel type %s on %s",
583 channel_type, object_path);
588 DEBUG ("%s channel of type %s on %s", incoming ? "incoming" : "outgoing",
589 channel_type, object_path);
591 if (properties == NULL)
592 channel = tp_channel_new (connection, object_path, channel_type,
593 handle_type, handle, NULL);
595 channel = tp_channel_new_from_properties (connection, object_path,
598 g_signal_connect (channel, "invalidated",
599 G_CALLBACK (dispatcher_channel_invalidated_cb),
602 priv->channels = g_list_prepend (priv->channels, channel);
604 operation = empathy_dispatch_operation_new (connection, channel, NULL,
605 incoming, timestamp);
607 g_object_unref (channel);
609 if (!incoming && requests_satisfied != NULL)
612 gboolean found = FALSE;
614 l = cd->outstanding_requests;
618 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
623 if (d->request == NULL)
626 if (d->operation != NULL)
629 path = tp_proxy_get_object_path (d->channel_request);
630 for (n = 0; n < requests_satisfied->len ; n++)
632 const gchar *p = g_ptr_array_index (requests_satisfied, n);
633 if (!tp_strdiff (p, path))
635 DEBUG ("Channel satified request %s"
636 "(already dispatched: %d)", p, found);
639 d->operation = operation;
645 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
647 dispatcher_request_failed (dispatcher, d, &err);
654 dispatcher_start_dispatching (dispatcher, operation, cd);
658 dispatcher_connection_new_channel_with_properties (
659 EmpathyDispatcher *self,
660 TpConnection *connection,
661 const gchar *object_path,
662 GHashTable *properties,
663 const GPtrArray *requests_satisfied,
666 const gchar *channel_type;
672 channel_type = tp_asv_get_string (properties,
673 TP_IFACE_CHANNEL ".ChannelType");
674 if (channel_type == NULL)
676 g_message ("%s had an invalid ChannelType property", object_path);
680 handle_type = tp_asv_get_uint32 (properties,
681 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
684 g_message ("%s had an invalid TargetHandleType property", object_path);
688 handle = tp_asv_get_uint32 (properties,
689 TP_IFACE_CHANNEL ".TargetHandle", &valid);
692 g_message ("%s had an invalid TargetHandle property", object_path);
696 /* We assume there is no channel dispather, so we're the only one dispatching
697 * it. Which means that a requested channel it is outgoing one */
698 requested = tp_asv_get_boolean (properties,
699 TP_IFACE_CHANNEL ".Requested", &valid);
702 g_message ("%s had an invalid Requested property", object_path);
706 dispatcher_connection_new_channel (self, connection,
707 object_path, channel_type, handle_type, handle, properties, !requested,
708 requests_satisfied, timestamp);
712 dispatcher_connection_got_all (TpProxy *proxy,
713 GHashTable *properties,
718 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
719 EmpathyDispatcherPriv *priv = GET_PRIV (self);
720 GPtrArray *requestable_channels;
723 DEBUG ("Error: %s", error->message);
727 requestable_channels = tp_asv_get_boxed (properties,
728 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
730 if (requestable_channels == NULL)
731 DEBUG ("No RequestableChannelClasses property !?! on connection");
736 FindChannelRequest *request;
739 cd = g_hash_table_lookup (priv->connections, proxy);
740 g_assert (cd != NULL);
742 cd->requestable_channels = g_boxed_copy (
743 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
745 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
748 for (l = requests; l != NULL; l = l->next)
752 retval = empathy_dispatcher_find_channel_classes (self,
753 TP_CONNECTION (proxy), request->channel_type,
754 request->handle_type, request->properties);
755 request->callback (retval, request->user_data);
757 free_find_channel_request (request);
758 g_list_free (retval);
761 g_list_free (requests);
763 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
768 connection_ready_cb (TpConnection *connection,
772 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
776 DEBUG ("Error: %s", error->message);
780 tp_cli_dbus_properties_call_get_all (connection, -1,
781 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, dispatcher_connection_got_all,
782 NULL, NULL, G_OBJECT (self));
785 g_object_unref (self);
789 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
790 TpConnection *connection)
792 EmpathyDispatcherPriv *priv = GET_PRIV (self);
794 if (g_hash_table_lookup (priv->connections, connection) != NULL)
797 g_hash_table_insert (priv->connections, g_object_ref (connection),
798 new_connection_data ());
800 g_signal_connect (connection, "invalidated",
801 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
803 /* Ensure to keep the self object alive while the call_when_ready is
806 tp_connection_call_when_ready (connection, connection_ready_cb, self);
810 dispatcher_status_changed_cb (TpAccount *account,
814 gchar *dbus_error_name,
816 EmpathyDispatcher *self)
818 TpConnection *conn = tp_account_get_connection (account);
821 dispatcher_init_connection_if_needed (self, conn);
825 remove_idle_handlers (gpointer key,
831 source_id = GPOINTER_TO_UINT (value);
832 g_source_remove (source_id);
836 dispatcher_constructor (GType type,
837 guint n_construct_params,
838 GObjectConstructParam *construct_params)
841 EmpathyDispatcherPriv *priv;
843 if (dispatcher != NULL)
844 return g_object_ref (dispatcher);
846 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
847 (type, n_construct_params, construct_params);
849 dispatcher = EMPATHY_DISPATCHER (retval);
850 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
852 priv = GET_PRIV (dispatcher);
854 if (priv->handler == NULL)
855 priv->handler = empathy_handler_new (NULL, NULL, NULL);
857 empathy_handler_set_handle_channels_func (priv->handler,
858 empathy_dispatcher_handle_channels,
861 empathy_handler_set_channels_func (priv->handler,
862 empathy_dispatcher_get_channels,
869 dispatcher_dispose (GObject *object)
871 EmpathyDispatcherPriv *priv = GET_PRIV (object);
876 if (priv->dispose_has_run)
879 priv->dispose_has_run = TRUE;
881 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
882 g_object_unref (G_OBJECT (l->data));
884 g_list_free (priv->handlers);
885 priv->handlers = NULL;
887 if (priv->handler != NULL)
888 g_object_unref (priv->handler);
889 priv->handler = NULL;
891 g_hash_table_iter_init (&iter, priv->connections);
892 while (g_hash_table_iter_next (&iter, &connection, NULL))
894 g_signal_handlers_disconnect_by_func (connection,
895 dispatcher_connection_invalidated_cb, object);
898 g_hash_table_destroy (priv->connections);
899 priv->connections = NULL;
901 if (priv->channel_dispatcher != NULL)
902 g_object_unref (priv->channel_dispatcher);
903 priv->channel_dispatcher = NULL;
905 if (priv->dbus != NULL)
906 g_object_unref (priv->dbus);
909 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
913 dispatcher_finalize (GObject *object)
915 EmpathyDispatcherPriv *priv = GET_PRIV (object);
920 gpointer account, id;
922 if (priv->request_channel_class_async_ids != NULL)
924 g_hash_table_foreach (priv->request_channel_class_async_ids,
925 remove_idle_handlers, NULL);
926 g_hash_table_destroy (priv->request_channel_class_async_ids);
929 for (l = priv->channels; l; l = l->next)
931 g_signal_handlers_disconnect_by_func (l->data,
932 dispatcher_channel_invalidated_cb, object);
935 g_list_free (priv->channels);
937 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
938 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
940 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
944 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
945 while (g_hash_table_iter_next (&iter, &account, &id))
947 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
949 g_hash_table_destroy (priv->status_changed_handlers);
951 g_object_unref (priv->account_manager);
953 g_hash_table_destroy (priv->outstanding_classes_requests);
957 dispatcher_set_property (GObject *object,
962 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
963 EmpathyDispatcherPriv *priv = GET_PRIV (self);
968 priv->handler = g_value_dup_object (value);
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
977 dispatcher_get_property (GObject *object,
982 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
983 EmpathyDispatcherPriv *priv = GET_PRIV (self);
988 g_value_set_object (value, priv->handler);
991 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
997 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
999 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1000 GParamSpec *param_spec;
1002 object_class->dispose = dispatcher_dispose;
1003 object_class->finalize = dispatcher_finalize;
1004 object_class->constructor = dispatcher_constructor;
1006 object_class->get_property = dispatcher_get_property;
1007 object_class->set_property = dispatcher_set_property;
1009 param_spec = g_param_spec_object ("handler", "handler",
1010 "The main Telepathy Client Hander object",
1011 EMPATHY_TYPE_HANDLER,
1012 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1013 g_object_class_install_property (object_class,
1014 PROP_HANDLER, param_spec);
1017 g_signal_new ("observe",
1018 G_TYPE_FROM_CLASS (klass),
1022 g_cclosure_marshal_VOID__OBJECT,
1024 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1027 g_signal_new ("dispatch",
1028 G_TYPE_FROM_CLASS (klass),
1032 g_cclosure_marshal_VOID__OBJECT,
1034 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1037 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1041 connect_account (EmpathyDispatcher *self,
1044 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1045 TpConnection *conn = tp_account_get_connection (account);
1048 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1055 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1057 id = g_signal_connect (account, "status-changed",
1058 G_CALLBACK (dispatcher_status_changed_cb), self);
1060 g_hash_table_insert (priv->status_changed_handlers, g_object_ref (account),
1061 GUINT_TO_POINTER (id));
1065 account_manager_prepared_cb (GObject *source_object,
1066 GAsyncResult *result,
1069 GList *accounts, *l;
1070 EmpathyDispatcher *self = user_data;
1071 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1072 GError *error = NULL;
1074 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1076 DEBUG ("Failed to prepare account manager: %s", error->message);
1077 g_error_free (error);
1081 accounts = tp_account_manager_get_valid_accounts (account_manager);
1082 for (l = accounts; l; l = l->next)
1084 TpAccount *a = l->data;
1086 connect_account (self, a);
1089 g_list_foreach (accounts, (GFunc) g_object_ref, NULL);
1090 g_list_free (accounts);
1094 account_prepare_cb (GObject *source_object,
1095 GAsyncResult *result,
1098 EmpathyDispatcher *self = user_data;
1099 TpAccount *account = TP_ACCOUNT (source_object);
1100 GError *error = NULL;
1102 if (!tp_account_prepare_finish (account, result, &error))
1104 DEBUG ("Failed to prepare account: %s", error->message);
1105 g_error_free (error);
1109 connect_account (self, account);
1113 account_validity_changed_cb (TpAccountManager *manager,
1121 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1125 empathy_dispatcher_init (EmpathyDispatcher *self)
1127 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1128 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1131 priv->account_manager = tp_account_manager_dup ();
1133 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1134 g_object_unref, (GDestroyNotify) free_connection_data);
1136 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1137 g_direct_equal, g_object_unref, NULL);
1139 priv->channels = NULL;
1141 tp_account_manager_prepare_async (priv->account_manager, NULL,
1142 account_manager_prepared_cb, self);
1144 tp_g_signal_connect_object (priv->account_manager,
1145 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1148 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1150 priv->status_changed_handlers = g_hash_table_new_full (NULL, NULL,
1151 (GDestroyNotify) g_object_unref, NULL);
1153 priv->dbus = tp_dbus_daemon_dup (NULL);
1154 priv->channel_dispatcher = tp_channel_dispatcher_new (priv->dbus);
1158 empathy_dispatcher_new (const gchar *name,
1162 EmpathyHandler *handler;
1163 EmpathyDispatcher *ret;
1165 g_assert (dispatcher == NULL);
1166 handler = empathy_handler_new (name, filters, capabilities);
1168 ret = EMPATHY_DISPATCHER (
1169 g_object_new (EMPATHY_TYPE_DISPATCHER,
1172 g_object_unref (handler);
1178 empathy_dispatcher_dup_singleton (void)
1180 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1184 dispatcher_request_failed (EmpathyDispatcher *self,
1185 DispatcherRequestData *request_data,
1186 const GError *error)
1188 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1189 ConnectionData *conn_data;
1191 conn_data = g_hash_table_lookup (priv->connections,
1192 request_data->connection);
1193 if (request_data->cb != NULL)
1194 request_data->cb (NULL, error, request_data->user_data);
1196 if (conn_data != NULL)
1198 conn_data->outstanding_requests =
1199 g_list_remove (conn_data->outstanding_requests, request_data);
1201 /* else Connection has been invalidated */
1203 free_dispatcher_request_data (request_data);
1207 dispatcher_request_channel (DispatcherRequestData *request_data)
1209 /* Extend the request_data to be a valid request */
1210 g_assert (request_data->request == NULL);
1211 request_data->request = tp_asv_new (
1212 TP_IFACE_CHANNEL ".ChannelType",
1213 G_TYPE_STRING, request_data->channel_type,
1214 TP_IFACE_CHANNEL ".TargetHandleType",
1215 G_TYPE_UINT, request_data->handle_type,
1218 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1219 tp_asv_set_uint32 (request_data->request, TP_IFACE_CHANNEL ".TargetHandle",
1220 request_data->handle);
1222 empathy_dispatcher_call_create_or_ensure_channel (request_data->dispatcher,
1227 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1229 EmpathyDispatcherRequestCb *callback,
1232 EmpathyDispatcher *self;
1233 EmpathyDispatcherPriv *priv;
1234 TpConnection *connection;
1235 ConnectionData *connection_data;
1236 DispatcherRequestData *request_data;
1238 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1240 self = empathy_dispatcher_dup_singleton ();
1241 priv = GET_PRIV (self);
1243 connection = empathy_contact_get_connection (contact);
1244 connection_data = g_hash_table_lookup (priv->connections, connection);
1245 if (connection_data == NULL)
1247 /* Connection has been invalidated */
1248 if (callback != NULL)
1250 GError error = { TP_DBUS_ERRORS, TP_DBUS_ERROR_PROXY_UNREFERENCED,
1251 "Connection has been invalidated" };
1252 callback (NULL, &error, user_data);
1257 /* The contact handle might not be known yet */
1258 request_data = new_dispatcher_request_data (self, connection,
1259 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1260 empathy_contact_get_handle (contact), NULL, timestamp, contact,
1261 callback, user_data);
1262 request_data->should_ensure = TRUE;
1264 connection_data->outstanding_requests = g_list_prepend
1265 (connection_data->outstanding_requests, request_data);
1267 dispatcher_request_channel (request_data);
1270 g_object_unref (self);
1275 EmpathyDispatcher *dispatcher;
1276 EmpathyDispatcherRequestCb *callback;
1279 } ChatWithContactIdData;
1282 dispatcher_chat_with_contact_id_cb (TpConnection *connection,
1283 EmpathyContact *contact,
1284 const GError *error,
1286 GObject *weak_object)
1288 ChatWithContactIdData *data = user_data;
1292 DEBUG ("Error: %s", error->message);
1294 if (data->callback != NULL)
1296 data->callback (NULL, error, data->user_data);
1301 empathy_dispatcher_chat_with_contact (contact, data->timestamp,
1302 data->callback, data->user_data);
1305 g_object_unref (data->dispatcher);
1306 g_slice_free (ChatWithContactIdData, data);
1310 empathy_dispatcher_chat_with_contact_id (TpAccount *account,
1311 const gchar *contact_id,
1314 EmpathyDispatcher *self;
1315 ChatWithContactIdData *data;
1316 TpConnection *connection;
1318 g_return_if_fail (TP_IS_ACCOUNT (account));
1319 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1321 connection = tp_account_get_connection (account);
1322 if (connection == NULL)
1325 self = empathy_dispatcher_dup_singleton ();
1326 data = g_slice_new0 (ChatWithContactIdData);
1327 data->dispatcher = self;
1328 data->callback = NULL;
1329 data->user_data = NULL;
1330 data->timestamp = timestamp;
1331 empathy_tp_contact_factory_get_from_id (connection, contact_id,
1332 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1336 dispatcher_request_handles_cb (TpConnection *connection,
1337 const GArray *handles,
1338 const GError *error,
1342 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1344 request_data->pending_call = NULL;
1348 EmpathyDispatcher *self = request_data->dispatcher;
1349 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1352 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1354 if (request_data->cb)
1355 request_data->cb (NULL, error, request_data->user_data);
1357 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1360 free_dispatcher_request_data (request_data);
1364 request_data->handle = g_array_index (handles, guint, 0);
1365 dispatcher_request_channel (request_data);
1369 empathy_dispatcher_join_muc (TpConnection *connection,
1370 const gchar *roomname,
1373 EmpathyDispatcher *self;
1374 EmpathyDispatcherPriv *priv;
1375 DispatcherRequestData *request_data;
1376 ConnectionData *connection_data;
1377 const gchar *names[] = { roomname, NULL };
1378 TpProxyPendingCall *call;
1380 g_return_if_fail (TP_IS_CONNECTION (connection));
1381 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1383 self = empathy_dispatcher_dup_singleton ();
1384 priv = GET_PRIV (self);
1386 connection_data = g_hash_table_lookup (priv->connections, connection);
1387 g_assert (connection_data != NULL);
1389 /* Don't know the room handle yet */
1390 request_data = new_dispatcher_request_data (self, connection,
1391 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL, timestamp,
1393 request_data->should_ensure = TRUE;
1395 connection_data->outstanding_requests = g_list_prepend
1396 (connection_data->outstanding_requests, request_data);
1398 call = tp_cli_connection_call_request_handles (
1400 TP_HANDLE_TYPE_ROOM, names,
1401 dispatcher_request_handles_cb, request_data, NULL, NULL);
1404 request_data->pending_call = call;
1406 g_object_unref (self);
1410 dispatcher_channel_request_failed_cb (TpChannelRequest *request,
1412 const gchar *message,
1414 GObject *weak_object)
1416 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1417 EmpathyDispatcher *self =
1418 EMPATHY_DISPATCHER (request_data->dispatcher);
1421 request_data->pending_call = NULL;
1423 DEBUG ("Request failed: %s - %s %s",
1424 tp_proxy_get_object_path (request),
1427 tp_proxy_dbus_error_to_gerror (TP_PROXY (request),
1428 error, message, &err);
1430 dispatcher_request_failed (self, request_data, err);
1436 dispatcher_channel_request_succeeded_cb (TpChannelRequest *request,
1438 GObject *weak_object)
1440 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1441 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1442 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1443 ConnectionData *conn_data;
1445 conn_data = g_hash_table_lookup (priv->connections,
1446 request_data->connection);
1448 DEBUG ("Request succeeded: %s", tp_proxy_get_object_path (request));
1450 /* When success gets called the internal request should have been satisfied,
1451 * if it's still in outstanding_requests and doesn't have an operation
1452 * assigned to it, the channel got handled by someone else.. */
1454 if (g_list_find (conn_data->outstanding_requests, request_data) == NULL)
1457 if (request_data->operation == NULL)
1459 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS, "Not yours!" };
1460 dispatcher_request_failed (self, request_data, &err);
1465 dispatcher_channel_request_proceed_cb (TpChannelRequest *request,
1466 const GError *error,
1468 GObject *weak_object)
1470 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1471 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1473 request_data->pending_call = NULL;
1476 dispatcher_request_failed (self, request_data, error);
1480 dispatcher_create_channel_cb (TpChannelDispatcher *proxy,
1481 const gchar *request_path,
1482 const GError *error,
1484 GObject *weak_object)
1486 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1487 EmpathyDispatcher *self = EMPATHY_DISPATCHER (request_data->dispatcher);
1488 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1489 TpChannelRequest *request;
1491 TpProxyPendingCall *call;
1493 request_data->pending_call = NULL;
1497 dispatcher_request_failed (self, request_data, error);
1501 request = tp_channel_request_new (priv->dbus, request_path, NULL, NULL);
1502 request_data->channel_request = request;
1504 if (tp_cli_channel_request_connect_to_failed (request,
1505 dispatcher_channel_request_failed_cb, request_data,
1506 NULL, G_OBJECT (self), &err) == NULL)
1508 dispatcher_request_failed (self, request_data, err);
1513 if (tp_cli_channel_request_connect_to_succeeded (request,
1514 dispatcher_channel_request_succeeded_cb, request_data,
1515 NULL, G_OBJECT (self), &err) == NULL)
1517 dispatcher_request_failed (self, request_data, err);
1522 call = tp_cli_channel_request_call_proceed (request,
1523 -1, dispatcher_channel_request_proceed_cb,
1524 request_data, NULL, G_OBJECT (self));
1527 request_data->pending_call = call;
1531 empathy_dispatcher_call_create_or_ensure_channel (
1532 EmpathyDispatcher *self,
1533 DispatcherRequestData *request_data)
1535 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1537 const gchar *handler = "";
1538 TpProxyPendingCall *call;
1540 account = empathy_get_account_for_connection (request_data->connection);
1542 g_assert (account != NULL);
1544 if (request_data->cb)
1545 handler = empathy_handler_get_busname (priv->handler);
1547 if (request_data->should_ensure)
1549 call = tp_cli_channel_dispatcher_call_ensure_channel (
1550 priv->channel_dispatcher,
1551 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1552 request_data->request, request_data->timestamp, handler,
1553 dispatcher_create_channel_cb, request_data, NULL, NULL);
1557 call = tp_cli_channel_dispatcher_call_create_channel (
1558 priv->channel_dispatcher,
1559 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1560 request_data->request, request_data->timestamp, handler,
1561 dispatcher_create_channel_cb, request_data, NULL,
1562 G_OBJECT (dispatcher));
1566 request_data->pending_call = call;
1570 * empathy_dispatcher_create_channel:
1571 * @self: the EmpathyDispatcher
1572 * @connection: the Connection to dispatch on
1573 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1574 * @callback: a callback for when the channel arrives (or NULL)
1575 * @user_data: optional user data (or NULL)
1577 * When calling this function, #EmpathyDispatcher takes ownership of your
1578 * reference to @request. DO NOT unref or destroy @request. When the request is
1579 * done, @request will be unreferenced. Take another reference if you want to
1583 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1584 TpConnection *connection,
1585 GHashTable *request,
1587 EmpathyDispatcherRequestCb *callback,
1590 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1591 ConnectionData *connection_data;
1592 DispatcherRequestData *request_data;
1593 const gchar *channel_type;
1598 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1599 g_return_if_fail (TP_IS_CONNECTION (connection));
1600 g_return_if_fail (request != NULL);
1602 connection_data = g_hash_table_lookup (priv->connections, connection);
1603 g_assert (connection_data != NULL);
1605 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1607 handle_type = tp_asv_get_uint32 (request,
1608 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1610 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1612 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1614 request_data = new_dispatcher_request_data (self, connection,
1615 channel_type, handle_type, handle, request, timestamp,
1616 NULL, callback, user_data);
1618 connection_data->outstanding_requests = g_list_prepend
1619 (connection_data->outstanding_requests, request_data);
1621 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1625 channel_class_matches (GValueArray *class,
1626 const char *channel_type,
1628 GArray *fixed_properties)
1636 v = g_value_array_get_nth (class, 0);
1638 /* if the class doesn't match channel type discard it. */
1639 fprops = g_value_get_boxed (v);
1640 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1642 if (tp_strdiff (channel_type, c_type))
1645 /* we have the right channel type, see if the handle type matches */
1646 h_type = tp_asv_get_uint32 (fprops,
1647 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1649 if (!valid || (handle_type != h_type && handle_type != TP_UNKNOWN_HANDLE_TYPE))
1652 if (fixed_properties != NULL)
1654 gpointer h_key, h_val;
1656 GHashTableIter iter;
1659 g_hash_table_iter_init (&iter, fprops);
1661 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1663 /* discard ChannelType and TargetHandleType, as we already
1666 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1668 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1673 for (idx = 0; idx < fixed_properties->len; idx++)
1675 /* if |key| doesn't exist in |fixed_properties|, discard
1680 g_array_index (fixed_properties, char *, idx)))
1683 /* exit the for() loop */
1694 /* if no fixed_properties are specified, discard the classes
1695 * with some fixed properties other than the two we already
1698 if (g_hash_table_size (fprops) > 2)
1706 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1707 TpConnection *connection,
1708 const gchar *channel_type,
1710 GArray *fixed_properties)
1712 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1715 GList *matching_classes;
1719 g_return_val_if_fail (channel_type != NULL, NULL);
1721 cd = g_hash_table_lookup (priv->connections, connection);
1726 classes = cd->requestable_channels;
1727 if (classes == NULL)
1730 matching_classes = NULL;
1732 for (i = 0; i < classes->len; i++)
1734 class = g_ptr_array_index (classes, i);
1736 if (!channel_class_matches
1737 (class, channel_type, handle_type, fixed_properties))
1740 matching_classes = g_list_prepend (matching_classes, class);
1743 return matching_classes;
1747 find_channel_class_idle_cb (gpointer user_data)
1751 FindChannelRequest *request = user_data;
1753 gboolean is_ready = TRUE;
1754 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1756 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1758 cd = g_hash_table_lookup (priv->connections, request->connection);
1762 else if (cd->requestable_channels == NULL)
1767 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1768 request->connection, request->channel_type, request->handle_type,
1769 request->properties);
1771 request->callback (retval, request->user_data);
1772 free_find_channel_request (request);
1773 g_list_free (retval);
1778 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1779 request->connection);
1780 requests = g_list_prepend (requests, request);
1782 g_hash_table_insert (priv->outstanding_classes_requests,
1783 request->connection, requests);
1789 setup_varargs (va_list var_args,
1790 const char *channel_namespace,
1791 const char *first_property_name)
1797 if (first_property_name == NULL)
1800 name = first_property_name;
1801 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1803 while (name != NULL)
1805 name_full = g_strdup (name);
1806 properties = g_array_append_val (properties, name_full);
1807 name = va_arg (var_args, char *);
1814 * empathy_dispatcher_find_requestable_channel_classes:
1815 * @dispatcher: an #EmpathyDispatcher
1816 * @connection: a #TpConnection
1817 * @channel_type: a string identifying the type of the channel to lookup
1818 * @handle_type: the handle type for the channel, or %TP_UNKNOWN_HANDLE_TYPE
1819 * if you don't care about the channel's target handle type
1820 * @first_property_name: %NULL, or the name of the first fixed property,
1821 * followed optionally by more names, followed by %NULL.
1823 * Returns all the channel classes that a client can request for the connection
1824 * @connection, of the type identified by @channel_type, @handle_type and the
1825 * fixed properties list.
1826 * The classes which are compatible with a fixed properties list (i.e. those
1827 * that will be returned by this function) are intended as those that do not
1828 * contain any fixed property other than those in the list; note that this
1829 * doesn't guarantee that all the classes compatible with the list will contain
1830 * all the requested fixed properties, so the clients will have to filter
1831 * the returned list themselves.
1832 * If @first_property_name is %NULL, only the classes with no other fixed
1833 * properties than ChannelType and TargetHandleType will be returned.
1834 * Note that this function may return %NULL without performing any lookup if
1835 * @connection is not ready. To ensure that @connection is always ready,
1836 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1838 * Return value: a #GList of #GValueArray objects, where the first element in
1839 * the array is a #GHashTable of the fixed properties, and the second is
1840 * a #GStrv of the allowed properties for the class. The list should be free'd
1841 * with g_list_free() when done, but the objects inside the list are owned
1842 * by the #EmpathyDispatcher and must not be modified.
1845 empathy_dispatcher_find_requestable_channel_classes
1846 (EmpathyDispatcher *self,
1847 TpConnection *connection,
1848 const gchar *channel_type,
1850 const char *first_property_name,
1855 EmpathyDispatcherPriv *priv;
1860 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1861 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1862 g_return_val_if_fail (channel_type != NULL, NULL);
1864 priv = GET_PRIV (self);
1866 va_start (var_args, first_property_name);
1868 properties = setup_varargs (var_args, channel_type, first_property_name);
1872 retval = empathy_dispatcher_find_channel_classes (self, connection,
1873 channel_type, handle_type, properties);
1875 if (properties != NULL)
1877 /* free the properties array */
1878 for (idx = 0; idx < properties->len ; idx++)
1880 str = g_array_index (properties, char *, idx);
1884 g_array_free (properties, TRUE);
1891 * empathy_dispatcher_find_requestable_channel_classes_async:
1892 * @dispatcher: an #EmpathyDispatcher
1893 * @connection: a #TpConnection
1894 * @channel_type: a string identifying the type of the channel to lookup
1895 * @handle_type: the handle type for the channel
1896 * @callback: the callback to call when @connection is ready
1897 * @user_data: the user data to pass to @callback
1898 * @first_property_name: %NULL, or the name of the first fixed property,
1899 * followed optionally by more names, followed by %NULL.
1901 * Please see the documentation of
1902 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1903 * description of this function.
1906 empathy_dispatcher_find_requestable_channel_classes_async
1907 (EmpathyDispatcher *self,
1908 TpConnection *connection,
1909 const gchar *channel_type,
1911 EmpathyDispatcherFindChannelClassCb callback,
1913 const char *first_property_name,
1918 FindChannelRequest *request;
1919 EmpathyDispatcherPriv *priv;
1922 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1923 g_return_if_fail (TP_IS_CONNECTION (connection));
1924 g_return_if_fail (channel_type != NULL);
1925 g_return_if_fail (handle_type != 0);
1927 priv = GET_PRIV (self);
1929 va_start (var_args, first_property_name);
1931 properties = setup_varargs (var_args, channel_type, first_property_name);
1935 /* append another request for this connection */
1936 request = g_slice_new0 (FindChannelRequest);
1937 request->dispatcher = g_object_ref (self);
1938 request->channel_type = g_strdup (channel_type);
1939 request->handle_type = handle_type;
1940 request->connection = connection;
1941 request->callback = callback;
1942 request->user_data = user_data;
1943 request->properties = properties;
1945 source_id = g_idle_add (find_channel_class_idle_cb, request);
1947 g_hash_table_insert (priv->request_channel_class_async_ids,
1948 request, GUINT_TO_POINTER (source_id));
1952 empathy_dispatcher_get_channels (EmpathyHandler *handler,
1955 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
1956 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1958 return priv->channels;
1962 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
1963 const gchar *account_path,
1964 const gchar *connection_path,
1965 const GPtrArray *channels,
1966 const GPtrArray *requests_satisfied,
1968 GHashTable *handler_info,
1972 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
1973 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1976 TpConnection *connection;
1978 /* FIXME: should probably find out whether the account manager is prepared
1979 * before ensuring. See bug #600111. */
1980 account = tp_account_manager_ensure_account (priv->account_manager,
1982 g_assert (account != NULL);
1984 connection = tp_account_ensure_connection (account, connection_path);
1985 if (connection == NULL)
1987 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
1988 "Invalid connection argument");
1992 for (i = 0; i < channels->len ; i++)
1994 GValueArray *arr = g_ptr_array_index (channels, i);
1995 const gchar *object_path;
1996 GHashTable *properties;
1998 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
1999 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2001 dispatcher_connection_new_channel_with_properties (self,
2002 connection, object_path, properties, requests_satisfied, timestamp);
2010 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2015 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2016 EmpathyHandler *handler;
2018 handler = empathy_handler_new (name, filters, capabilities);
2019 priv->handlers = g_list_prepend (priv->handlers, handler);
2021 /* Only set the handle_channels function, the Channel property on the main
2022 * handler will always report all dispatched channels even if they came from
2023 * a different Handler */
2024 empathy_handler_set_handle_channels_func (handler,
2025 empathy_dispatcher_handle_channels, self);
2031 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2032 EmpathyHandler *handler)
2034 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2037 h = g_list_find (priv->handlers, handler);
2038 g_return_if_fail (h != NULL);
2040 priv->handlers = g_list_delete_link (priv->handlers, h);
2042 g_object_unref (handler);