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 /* (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);
118 static guint signals[LAST_SIGNAL];
119 static EmpathyDispatcher *dispatcher = NULL;
121 static void dispatcher_init_connection_if_needed (
122 EmpathyDispatcher *dispatcher,
123 TpConnection *connection);
125 static GList * empathy_dispatcher_find_channel_classes
126 (EmpathyDispatcher *dispatcher, TpConnection *connection,
127 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
132 EmpathyDispatcher *dispatcher;
133 EmpathyDispatchOperation *operation;
134 TpConnection *connection;
135 gboolean should_ensure;
139 EmpathyContact *contact;
140 TpProxyPendingCall *pending_call;
142 /* 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,
221 EmpathyContact *contact,
222 EmpathyDispatcherRequestCb *cb,
225 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
227 result->dispatcher = g_object_ref (self);
228 result->connection = connection;
230 result->should_ensure = FALSE;
232 result->channel_type = g_strdup (channel_type);
233 result->handle_type = handle_type;
234 result->handle = handle;
235 result->request = request;
238 result->contact = g_object_ref (contact);
241 result->user_data = user_data;
247 free_dispatcher_request_data (DispatcherRequestData *r)
249 g_free (r->channel_type);
251 if (r->dispatcher != NULL)
252 g_object_unref (r->dispatcher);
254 if (r->contact != NULL)
255 g_object_unref (r->contact);
257 if (r->request != NULL)
258 g_hash_table_unref (r->request);
261 if (r->pending_call != NULL)
262 tp_proxy_pending_call_cancel (r->pending_call);
264 if (r->channel_request != NULL)
265 g_object_unref (r->channel_request);
267 g_slice_free (DispatcherRequestData, r);
270 static ConnectionData *
271 new_connection_data (void)
273 ConnectionData *cd = g_slice_new0 (ConnectionData);
275 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
276 g_free, (GDestroyNotify) free_dispatch_data);
278 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
279 g_free, g_object_unref);
285 free_connection_data (ConnectionData *cd)
290 g_hash_table_destroy (cd->dispatched_channels);
291 g_hash_table_destroy (cd->dispatching_channels);
293 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
295 free_dispatcher_request_data (l->data);
298 if (cd->requestable_channels != NULL)
300 for (i = 0 ; i < cd->requestable_channels->len ; i++)
302 g_ptr_array_index (cd->requestable_channels, i));
303 g_ptr_array_free (cd->requestable_channels, TRUE);
308 free_find_channel_request (FindChannelRequest *r)
313 g_object_unref (r->dispatcher);
314 g_free (r->channel_type);
316 if (r->properties != NULL)
318 for (idx = 0; idx < r->properties->len ; idx++)
320 str = g_array_index (r->properties, char *, idx);
324 g_array_free (r->properties, TRUE);
327 g_slice_free (FindChannelRequest, r);
331 dispatcher_connection_invalidated_cb (TpConnection *connection,
335 EmpathyDispatcher *self)
337 EmpathyDispatcherPriv *priv = GET_PRIV (self);
338 ConnectionData *connection_data;
340 DEBUG ("Error: %s", message);
342 /* Terminate pending requests, if any */
343 connection_data = g_hash_table_lookup (priv->connections, connection);
344 if (connection_data != NULL)
349 error = g_error_new_literal (domain, code, message);
351 for (l = connection_data->outstanding_requests; l != NULL;
354 DispatcherRequestData *request_data = l->data;
356 if (request_data->cb != NULL)
357 request_data->cb (NULL, error, request_data->user_data);
360 g_error_free (error);
363 g_hash_table_remove (priv->connections, connection);
367 dispatch_operation_flush_requests (EmpathyDispatcher *self,
368 EmpathyDispatchOperation *operation,
374 l = cd->outstanding_requests;
377 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
382 if (d->operation == operation)
387 d->cb (NULL, error, d->user_data);
389 d->cb (operation, NULL, d->user_data);
392 cd->outstanding_requests = g_list_delete_link
393 (cd->outstanding_requests, lt);
395 free_dispatcher_request_data (d);
401 dispatcher_channel_invalidated_cb (TpProxy *proxy,
405 EmpathyDispatcher *self)
407 /* Channel went away... */
408 EmpathyDispatcherPriv *priv = GET_PRIV (self);
409 TpConnection *connection;
411 const gchar *object_path;
413 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
415 cd = g_hash_table_lookup (priv->connections, connection);
416 /* Connection itself invalidated? */
420 object_path = tp_proxy_get_object_path (proxy);
422 DEBUG ("Channel %s invalidated", object_path);
424 g_hash_table_remove (cd->dispatched_channels, object_path);
425 g_hash_table_remove (cd->dispatching_channels, object_path);
427 priv->channels = g_list_remove (priv->channels, proxy);
431 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
432 EmpathyDispatcher *self)
434 g_assert (empathy_dispatch_operation_is_incoming (operation));
435 DEBUG ("Send of for dispatching: %s",
436 empathy_dispatch_operation_get_object_path (operation));
437 g_signal_emit (self, signals[DISPATCH], 0, operation);
441 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
442 EmpathyDispatcher *self)
444 /* Our job is done, remove the dispatch operation and mark the channel as
446 EmpathyDispatcherPriv *priv = GET_PRIV (self);
447 TpConnection *connection;
449 const gchar *object_path;
451 connection = empathy_dispatch_operation_get_tp_connection (operation);
452 cd = g_hash_table_lookup (priv->connections, connection);
453 g_assert (cd != NULL);
455 object_path = empathy_dispatch_operation_get_object_path (operation);
457 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
460 d = new_dispatch_data (
461 empathy_dispatch_operation_get_channel (operation),
462 empathy_dispatch_operation_get_channel_wrapper (operation));
463 g_hash_table_insert (cd->dispatched_channels,
464 g_strdup (object_path), d);
466 g_hash_table_remove (cd->dispatching_channels, object_path);
468 DEBUG ("Channel claimed: %s", object_path);
472 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
473 EmpathyDispatcher *self)
475 EmpathyDispatcherPriv *priv = GET_PRIV (self);
476 TpConnection *connection;
478 EmpathyDispatchOperationState status;
480 g_signal_connect (operation, "approved",
481 G_CALLBACK (dispatch_operation_approved_cb), self);
483 g_signal_connect (operation, "claimed",
484 G_CALLBACK (dispatch_operation_claimed_cb), self);
486 /* Signal the observers */
487 DEBUG ("Send to observers: %s",
488 empathy_dispatch_operation_get_object_path (operation));
489 g_signal_emit (self, signals[OBSERVE], 0, operation);
491 empathy_dispatch_operation_start (operation);
493 /* Signal potential requestors */
494 connection = empathy_dispatch_operation_get_tp_connection (operation);
495 cd = g_hash_table_lookup (priv->connections, connection);
496 g_assert (cd != NULL);
498 g_object_ref (operation);
501 dispatch_operation_flush_requests (self, operation, NULL, cd);
502 status = empathy_dispatch_operation_get_status (operation);
503 g_object_unref (operation);
505 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
508 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
510 DEBUG ("Send to approvers: %s",
511 empathy_dispatch_operation_get_object_path (operation));
512 g_signal_emit (self, signals[APPROVE], 0, operation);
516 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
517 DEBUG ("Send of for dispatching: %s",
518 empathy_dispatch_operation_get_object_path (operation));
519 g_signal_emit (self, signals[DISPATCH], 0, operation);
522 g_object_unref (self);
526 dispatcher_start_dispatching (EmpathyDispatcher *self,
527 EmpathyDispatchOperation *operation,
530 const gchar *object_path =
531 empathy_dispatch_operation_get_object_path (operation);
533 DEBUG ("Dispatching process started for %s", object_path);
535 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
537 g_hash_table_insert (cd->dispatching_channels,
538 g_strdup (object_path), operation);
540 switch (empathy_dispatch_operation_get_status (operation))
542 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
543 g_signal_connect (operation, "ready",
544 G_CALLBACK (dispatch_operation_ready_cb), self);
546 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
547 dispatch_operation_ready_cb (operation, self);
550 g_assert_not_reached ();
554 else if (empathy_dispatch_operation_get_status (operation) >=
555 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
557 /* Already dispatching and the operation is pending, thus the observers
558 * have seen it (if applicable), so we can flush the request right away.
560 dispatch_operation_flush_requests (self, operation, NULL, cd);
565 dispatcher_connection_new_channel (EmpathyDispatcher *self,
566 TpConnection *connection,
567 const gchar *object_path,
568 const gchar *channel_type,
571 GHashTable *properties,
573 const GPtrArray *requests_satisfied)
575 EmpathyDispatcherPriv *priv = GET_PRIV (self);
578 EmpathyDispatchOperation *operation;
580 /* Channel types we never want to dispatch because they're either deprecated
581 * or can't sensibly be dispatch (e.g. channels that should always be
583 const char *blacklist[] = {
584 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
585 TP_IFACE_CHANNEL_TYPE_TUBES,
586 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
590 dispatcher_init_connection_if_needed (self, connection);
592 cd = g_hash_table_lookup (priv->connections, connection);
594 for (i = 0 ; blacklist[i] != NULL; i++)
596 if (!tp_strdiff (channel_type, blacklist[i]))
598 DEBUG ("Ignoring blacklisted channel type %s on %s",
599 channel_type, object_path);
604 DEBUG ("%s channel of type %s on %s", incoming ? "incoming" : "outgoing",
605 channel_type, object_path);
607 if ((operation = g_hash_table_lookup (cd->dispatching_channels,
608 object_path)) != NULL)
610 /* This operation was already being dispatched, assume we got the channel
611 * again because something asked for it and approve it right away */
612 empathy_dispatch_operation_approve (operation);
616 if (properties == NULL)
617 channel = tp_channel_new (connection, object_path, channel_type,
618 handle_type, handle, NULL);
620 channel = tp_channel_new_from_properties (connection, object_path,
623 g_signal_connect (channel, "invalidated",
624 G_CALLBACK (dispatcher_channel_invalidated_cb),
627 priv->channels = g_list_prepend (priv->channels, channel);
629 operation = empathy_dispatch_operation_new (connection, channel, NULL,
632 g_object_unref (channel);
634 if (!incoming && requests_satisfied != NULL)
637 gboolean found = FALSE;
639 l = cd->outstanding_requests;
643 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
648 if (d->request == NULL)
651 if (d->operation != NULL)
654 path = tp_proxy_get_object_path (d->channel_request);
655 for (n = 0; n < requests_satisfied->len ; n++)
657 const gchar *p = g_ptr_array_index (requests_satisfied, n);
658 if (!tp_strdiff (p, path))
660 DEBUG ("Channel satified request %s"
661 "(already dispatched: %d)", p, found);
664 d->operation = operation;
670 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
672 dispatcher_request_failed (dispatcher, d, &err);
679 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
680 empathy_dispatch_operation_approve (operation);
682 dispatcher_start_dispatching (dispatcher, operation, cd);
686 dispatcher_connection_new_channel_with_properties (
687 EmpathyDispatcher *self,
688 TpConnection *connection,
689 const gchar *object_path,
690 GHashTable *properties,
691 const GPtrArray *requests_satisfied)
693 const gchar *channel_type;
699 channel_type = tp_asv_get_string (properties,
700 TP_IFACE_CHANNEL ".ChannelType");
701 if (channel_type == NULL)
703 g_message ("%s had an invalid ChannelType property", object_path);
707 handle_type = tp_asv_get_uint32 (properties,
708 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
711 g_message ("%s had an invalid TargetHandleType property", object_path);
715 handle = tp_asv_get_uint32 (properties,
716 TP_IFACE_CHANNEL ".TargetHandle", &valid);
719 g_message ("%s had an invalid TargetHandle property", object_path);
723 /* We assume there is no channel dispather, so we're the only one dispatching
724 * it. Which means that a requested channel it is outgoing one */
725 requested = tp_asv_get_boolean (properties,
726 TP_IFACE_CHANNEL ".Requested", &valid);
729 g_message ("%s had an invalid Requested property", object_path);
733 dispatcher_connection_new_channel (self, connection,
734 object_path, channel_type, handle_type, handle, properties, !requested,
739 dispatcher_connection_got_all (TpProxy *proxy,
740 GHashTable *properties,
745 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
746 EmpathyDispatcherPriv *priv = GET_PRIV (self);
747 GPtrArray *requestable_channels;
750 DEBUG ("Error: %s", error->message);
754 requestable_channels = tp_asv_get_boxed (properties,
755 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
757 if (requestable_channels == NULL)
758 DEBUG ("No RequestableChannelClasses property !?! on connection");
763 FindChannelRequest *request;
766 cd = g_hash_table_lookup (priv->connections, proxy);
767 g_assert (cd != NULL);
769 cd->requestable_channels = g_boxed_copy (
770 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
772 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
775 for (l = requests; l != NULL; l = l->next)
779 retval = empathy_dispatcher_find_channel_classes (self,
780 TP_CONNECTION (proxy), request->channel_type,
781 request->handle_type, request->properties);
782 request->callback (retval, request->user_data);
784 free_find_channel_request (request);
785 g_list_free (retval);
788 g_list_free (requests);
790 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
795 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
796 const GPtrArray *capabilities,
802 DEBUG ("Error: %s", error->message);
806 connection_ready_cb (TpConnection *connection,
810 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
811 GPtrArray *capabilities;
814 const gchar *remove_ = NULL;
818 DEBUG ("Error: %s", error->message);
822 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
823 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
825 tp_cli_dbus_properties_call_get_all (connection, -1,
826 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
827 dispatcher_connection_got_all,
828 NULL, NULL, G_OBJECT (self));
831 /* Advertise VoIP capabilities */
832 capabilities = g_ptr_array_sized_new (1);
833 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
834 G_TYPE_UINT, G_TYPE_INVALID);
835 g_value_init (&cap, cap_type);
836 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
837 dbus_g_type_struct_set (&cap,
838 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
839 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
840 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
841 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
842 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
843 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
845 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
847 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
848 connection, -1, capabilities, &remove_,
849 dispatcher_connection_advertise_capabilities_cb,
850 NULL, NULL, G_OBJECT (self));
852 g_value_unset (&cap);
853 g_ptr_array_free (capabilities, TRUE);
855 g_object_unref (self);
859 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
860 TpConnection *connection)
862 EmpathyDispatcherPriv *priv = GET_PRIV (self);
864 if (g_hash_table_lookup (priv->connections, connection) != NULL)
867 g_hash_table_insert (priv->connections, g_object_ref (connection),
868 new_connection_data ());
870 g_signal_connect (connection, "invalidated",
871 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
873 /* Ensure to keep the self object alive while the call_when_ready is
876 tp_connection_call_when_ready (connection, connection_ready_cb, self);
880 dispatcher_status_changed_cb (TpAccount *account,
884 gchar *dbus_error_name,
886 EmpathyDispatcher *self)
888 TpConnection *conn = tp_account_get_connection (account);
891 dispatcher_init_connection_if_needed (self, conn);
895 remove_idle_handlers (gpointer key,
901 source_id = GPOINTER_TO_UINT (value);
902 g_source_remove (source_id);
906 dispatcher_constructor (GType type,
907 guint n_construct_params,
908 GObjectConstructParam *construct_params)
911 EmpathyDispatcherPriv *priv;
913 if (dispatcher != NULL)
914 return g_object_ref (dispatcher);
916 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
917 (type, n_construct_params, construct_params);
919 dispatcher = EMPATHY_DISPATCHER (retval);
920 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
922 priv = GET_PRIV (dispatcher);
924 if (priv->handler == NULL)
925 priv->handler = empathy_handler_new (NULL, NULL, NULL);
927 empathy_handler_set_handle_channels_func (priv->handler,
928 empathy_dispatcher_handle_channels,
931 empathy_handler_set_channels_func (priv->handler,
932 empathy_dispatcher_get_channels,
939 dispatcher_dispose (GObject *object)
941 EmpathyDispatcherPriv *priv = GET_PRIV (object);
946 if (priv->dispose_has_run)
949 priv->dispose_has_run = TRUE;
951 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
952 g_object_unref (G_OBJECT (l->data));
954 g_list_free (priv->handlers);
955 priv->handlers = NULL;
957 if (priv->handler != NULL)
958 g_object_unref (priv->handler);
959 priv->handler = NULL;
961 g_hash_table_iter_init (&iter, priv->connections);
962 while (g_hash_table_iter_next (&iter, &connection, NULL))
964 g_signal_handlers_disconnect_by_func (connection,
965 dispatcher_connection_invalidated_cb, object);
968 g_hash_table_destroy (priv->connections);
969 priv->connections = NULL;
971 if (priv->channel_dispatcher != NULL)
972 g_object_unref (priv->channel_dispatcher);
973 priv->channel_dispatcher = NULL;
975 if (priv->dbus != NULL)
976 g_object_unref (priv->dbus);
979 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
983 dispatcher_finalize (GObject *object)
985 EmpathyDispatcherPriv *priv = GET_PRIV (object);
990 gpointer account, id;
992 if (priv->request_channel_class_async_ids != NULL)
994 g_hash_table_foreach (priv->request_channel_class_async_ids,
995 remove_idle_handlers, NULL);
996 g_hash_table_destroy (priv->request_channel_class_async_ids);
999 for (l = priv->channels; l; l = l->next)
1001 g_signal_handlers_disconnect_by_func (l->data,
1002 dispatcher_channel_invalidated_cb, object);
1005 g_list_free (priv->channels);
1007 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1008 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1010 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1014 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1015 while (g_hash_table_iter_next (&iter, &account, &id))
1017 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1019 g_hash_table_destroy (priv->status_changed_handlers);
1021 g_object_unref (priv->account_manager);
1023 g_hash_table_destroy (priv->outstanding_classes_requests);
1027 dispatcher_set_property (GObject *object,
1029 const GValue *value,
1032 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1033 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1035 switch (property_id)
1038 priv->handler = g_value_dup_object (value);
1041 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1047 dispatcher_get_property (GObject *object,
1052 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1053 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1055 switch (property_id)
1058 g_value_set_object (value, priv->handler);
1061 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1067 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1069 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1070 GParamSpec *param_spec;
1072 object_class->dispose = dispatcher_dispose;
1073 object_class->finalize = dispatcher_finalize;
1074 object_class->constructor = dispatcher_constructor;
1076 object_class->get_property = dispatcher_get_property;
1077 object_class->set_property = dispatcher_set_property;
1079 param_spec = g_param_spec_object ("handler", "handler",
1080 "The main Telepathy Client Hander object",
1081 EMPATHY_TYPE_HANDLER,
1082 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1083 g_object_class_install_property (object_class,
1084 PROP_HANDLER, param_spec);
1087 g_signal_new ("observe",
1088 G_TYPE_FROM_CLASS (klass),
1092 g_cclosure_marshal_VOID__OBJECT,
1094 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1097 g_signal_new ("approve",
1098 G_TYPE_FROM_CLASS (klass),
1102 g_cclosure_marshal_VOID__OBJECT,
1104 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1107 g_signal_new ("dispatch",
1108 G_TYPE_FROM_CLASS (klass),
1112 g_cclosure_marshal_VOID__OBJECT,
1114 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1117 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1121 connect_account (EmpathyDispatcher *self,
1124 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1125 TpConnection *conn = tp_account_get_connection (account);
1128 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1135 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1137 id = g_signal_connect (account, "status-changed",
1138 G_CALLBACK (dispatcher_status_changed_cb), self);
1140 g_hash_table_insert (priv->status_changed_handlers, account,
1141 GUINT_TO_POINTER (id));
1145 account_manager_prepared_cb (GObject *source_object,
1146 GAsyncResult *result,
1149 GList *accounts, *l;
1150 EmpathyDispatcher *self = user_data;
1151 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1152 GError *error = NULL;
1154 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1156 DEBUG ("Failed to prepare account manager: %s", error->message);
1157 g_error_free (error);
1161 accounts = tp_account_manager_get_valid_accounts (account_manager);
1162 for (l = accounts; l; l = l->next)
1164 TpAccount *a = l->data;
1166 connect_account (self, a);
1168 g_list_free (accounts);
1172 account_prepare_cb (GObject *source_object,
1173 GAsyncResult *result,
1176 EmpathyDispatcher *self = user_data;
1177 TpAccount *account = TP_ACCOUNT (source_object);
1178 GError *error = NULL;
1180 if (!tp_account_prepare_finish (account, result, &error))
1182 DEBUG ("Failed to prepare account: %s", error->message);
1183 g_error_free (error);
1187 connect_account (self, account);
1191 account_validity_changed_cb (TpAccountManager *manager,
1199 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1203 empathy_dispatcher_init (EmpathyDispatcher *self)
1205 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1206 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1209 priv->account_manager = tp_account_manager_dup ();
1211 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1212 g_object_unref, (GDestroyNotify) free_connection_data);
1214 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1215 g_direct_equal, g_object_unref, NULL);
1217 priv->channels = NULL;
1219 tp_account_manager_prepare_async (priv->account_manager, NULL,
1220 account_manager_prepared_cb, self);
1222 tp_g_signal_connect_object (priv->account_manager,
1223 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1226 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1228 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1231 priv->dbus = tp_dbus_daemon_dup (NULL);
1232 priv->channel_dispatcher = tp_channel_dispatcher_new (priv->dbus);
1236 empathy_dispatcher_new (const gchar *name,
1240 EmpathyHandler *handler;
1241 EmpathyDispatcher *ret;
1243 g_assert (dispatcher == NULL);
1244 handler = empathy_handler_new (name, filters, capabilities);
1246 ret = EMPATHY_DISPATCHER (
1247 g_object_new (EMPATHY_TYPE_DISPATCHER,
1250 g_object_unref (handler);
1256 empathy_dispatcher_dup_singleton (void)
1258 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1262 dispatcher_request_failed (EmpathyDispatcher *self,
1263 DispatcherRequestData *request_data,
1264 const GError *error)
1266 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1267 ConnectionData *conn_data;
1269 conn_data = g_hash_table_lookup (priv->connections,
1270 request_data->connection);
1271 if (request_data->cb != NULL)
1272 request_data->cb (NULL, error, request_data->user_data);
1274 if (conn_data != NULL)
1276 conn_data->outstanding_requests =
1277 g_list_remove (conn_data->outstanding_requests, request_data);
1279 /* else Connection has been invalidated */
1281 free_dispatcher_request_data (request_data);
1285 dispatcher_request_channel (DispatcherRequestData *request_data)
1287 /* Extend the request_data to be a valid request */
1288 g_assert (request_data->request == NULL);
1289 request_data->request = tp_asv_new (
1290 TP_IFACE_CHANNEL ".ChannelType",
1291 G_TYPE_STRING, request_data->channel_type,
1292 TP_IFACE_CHANNEL ".TargetHandleType",
1293 G_TYPE_UINT, request_data->handle_type,
1296 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1297 tp_asv_set_uint32 (request_data->request, TP_IFACE_CHANNEL ".TargetHandle",
1298 request_data->handle);
1300 empathy_dispatcher_call_create_or_ensure_channel (request_data->dispatcher,
1305 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1306 EmpathyDispatcherRequestCb *callback,
1309 EmpathyDispatcher *self;
1310 EmpathyDispatcherPriv *priv;
1311 TpConnection *connection;
1312 ConnectionData *connection_data;
1313 DispatcherRequestData *request_data;
1315 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1317 self = empathy_dispatcher_dup_singleton ();
1318 priv = GET_PRIV (self);
1320 connection = empathy_contact_get_connection (contact);
1321 connection_data = g_hash_table_lookup (priv->connections, connection);
1322 if (connection_data == NULL)
1324 /* Connection has been invalidated */
1325 if (callback != NULL)
1327 GError error = { TP_DBUS_ERRORS, TP_DBUS_ERROR_PROXY_UNREFERENCED,
1328 "Connection has been invalidated" };
1329 callback (NULL, &error, user_data);
1334 /* The contact handle might not be known yet */
1335 request_data = new_dispatcher_request_data (self, connection,
1336 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1337 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1338 request_data->should_ensure = TRUE;
1340 connection_data->outstanding_requests = g_list_prepend
1341 (connection_data->outstanding_requests, request_data);
1343 dispatcher_request_channel (request_data);
1346 g_object_unref (self);
1351 EmpathyDispatcher *dispatcher;
1352 EmpathyDispatcherRequestCb *callback;
1354 } ChatWithContactIdData;
1357 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1358 EmpathyContact *contact,
1359 const GError *error,
1361 GObject *weak_object)
1363 ChatWithContactIdData *data = user_data;
1367 DEBUG ("Error: %s", error->message);
1369 if (data->callback != NULL)
1371 data->callback (NULL, error, data->user_data);
1376 empathy_dispatcher_chat_with_contact (contact, data->callback,
1380 g_object_unref (data->dispatcher);
1381 g_slice_free (ChatWithContactIdData, data);
1385 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1386 const gchar *contact_id,
1387 EmpathyDispatcherRequestCb *callback,
1390 EmpathyDispatcher *self;
1391 EmpathyTpContactFactory *factory;
1392 ChatWithContactIdData *data;
1394 g_return_if_fail (TP_IS_CONNECTION (connection));
1395 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1397 self = empathy_dispatcher_dup_singleton ();
1398 factory = empathy_tp_contact_factory_dup_singleton (connection);
1399 data = g_slice_new0 (ChatWithContactIdData);
1400 data->dispatcher = self;
1401 data->callback = callback;
1402 data->user_data = user_data;
1403 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1404 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1406 g_object_unref (factory);
1410 dispatcher_request_handles_cb (TpConnection *connection,
1411 const GArray *handles,
1412 const GError *error,
1416 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1418 request_data->pending_call = NULL;
1422 EmpathyDispatcher *self = request_data->dispatcher;
1423 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1426 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1428 if (request_data->cb)
1429 request_data->cb (NULL, error, request_data->user_data);
1431 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1434 free_dispatcher_request_data (request_data);
1438 request_data->handle = g_array_index (handles, guint, 0);
1439 dispatcher_request_channel (request_data);
1443 empathy_dispatcher_join_muc (TpConnection *connection,
1444 const gchar *roomname,
1445 EmpathyDispatcherRequestCb *callback,
1448 EmpathyDispatcher *self;
1449 EmpathyDispatcherPriv *priv;
1450 DispatcherRequestData *request_data;
1451 ConnectionData *connection_data;
1452 const gchar *names[] = { roomname, NULL };
1453 TpProxyPendingCall *call;
1455 g_return_if_fail (TP_IS_CONNECTION (connection));
1456 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1458 self = empathy_dispatcher_dup_singleton ();
1459 priv = GET_PRIV (self);
1461 connection_data = g_hash_table_lookup (priv->connections, connection);
1462 g_assert (connection_data != NULL);
1464 /* Don't know the room handle yet */
1465 request_data = new_dispatcher_request_data (self, connection,
1466 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1467 NULL, callback, user_data);
1468 request_data->should_ensure = TRUE;
1470 connection_data->outstanding_requests = g_list_prepend
1471 (connection_data->outstanding_requests, request_data);
1473 call = tp_cli_connection_call_request_handles (
1475 TP_HANDLE_TYPE_ROOM, names,
1476 dispatcher_request_handles_cb, request_data, NULL, NULL);
1479 request_data->pending_call = call;
1481 g_object_unref (self);
1485 dispatcher_channel_request_failed_cb (TpChannelRequest *request,
1487 const gchar *message,
1489 GObject *weak_object)
1491 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1492 EmpathyDispatcher *self =
1493 EMPATHY_DISPATCHER (request_data->dispatcher);
1496 request_data->pending_call = NULL;
1498 DEBUG ("Request failed: %s - %s %s",
1499 tp_proxy_get_object_path (request),
1502 tp_proxy_dbus_error_to_gerror (TP_PROXY (request),
1503 error, message, &err);
1505 dispatcher_request_failed (self, request_data, err);
1511 dispatcher_channel_request_succeeded_cb (TpChannelRequest *request,
1513 GObject *weak_object)
1515 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1516 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1517 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1518 ConnectionData *conn_data;
1520 conn_data = g_hash_table_lookup (priv->connections,
1521 request_data->connection);
1523 DEBUG ("Request succeeded: %s", tp_proxy_get_object_path (request));
1525 /* When success gets called the internal request should have been satisfied,
1526 * if it's still in outstanding_requests and doesn't have an operation
1527 * assigned to it, the channel got handled by someone else.. */
1529 if (g_list_find (conn_data->outstanding_requests, request_data) == NULL)
1532 if (request_data->operation == NULL)
1534 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS, "Not yours!" };
1535 dispatcher_request_failed (self, request_data, &err);
1540 dispatcher_channel_request_proceed_cb (TpChannelRequest *request,
1541 const GError *error,
1543 GObject *weak_object)
1545 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1546 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1548 request_data->pending_call = NULL;
1551 dispatcher_request_failed (self, request_data, error);
1555 dispatcher_create_channel_cb (TpChannelDispatcher *proxy,
1556 const gchar *request_path,
1557 const GError *error,
1559 GObject *weak_object)
1561 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1562 EmpathyDispatcher *self = EMPATHY_DISPATCHER (request_data->dispatcher);
1563 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1564 TpChannelRequest *request;
1566 TpProxyPendingCall *call;
1568 request_data->pending_call = NULL;
1572 dispatcher_request_failed (self, request_data, error);
1576 request = tp_channel_request_new (priv->dbus, request_path, NULL, NULL);
1577 request_data->channel_request = request;
1579 if (tp_cli_channel_request_connect_to_failed (request,
1580 dispatcher_channel_request_failed_cb, request_data,
1581 NULL, G_OBJECT (self), &err) == NULL)
1583 dispatcher_request_failed (self, request_data, err);
1588 if (tp_cli_channel_request_connect_to_succeeded (request,
1589 dispatcher_channel_request_succeeded_cb, request_data,
1590 NULL, G_OBJECT (self), &err) == NULL)
1592 dispatcher_request_failed (self, request_data, err);
1597 call = tp_cli_channel_request_call_proceed (request,
1598 -1, dispatcher_channel_request_proceed_cb,
1599 request_data, NULL, G_OBJECT (self));
1602 request_data->pending_call = call;
1606 empathy_dispatcher_call_create_or_ensure_channel (
1607 EmpathyDispatcher *self,
1608 DispatcherRequestData *request_data)
1610 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1612 const gchar *handler = "";
1613 TpProxyPendingCall *call;
1615 account = empathy_get_account_for_connection (request_data->connection);
1617 g_assert (account != NULL);
1619 if (request_data->cb)
1620 handler = empathy_handler_get_busname (priv->handler);
1622 if (request_data->should_ensure)
1624 call = tp_cli_channel_dispatcher_call_ensure_channel (
1625 priv->channel_dispatcher,
1626 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1627 request_data->request, 0, handler,
1628 dispatcher_create_channel_cb, request_data, NULL, NULL);
1632 call = tp_cli_channel_dispatcher_call_create_channel (
1633 priv->channel_dispatcher,
1634 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1635 request_data->request, 0, handler,
1636 dispatcher_create_channel_cb, request_data, NULL,
1637 G_OBJECT (dispatcher));
1641 request_data->pending_call = call;
1645 * empathy_dispatcher_create_channel:
1646 * @self: the EmpathyDispatcher
1647 * @connection: the Connection to dispatch on
1648 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1649 * @callback: a callback for when the channel arrives (or NULL)
1650 * @user_data: optional user data (or NULL)
1652 * When calling this function, #EmpathyDispatcher takes ownership of your
1653 * reference to @request. DO NOT unref or destroy @request. When the request is
1654 * done, @request will be unreferenced. Take another reference if you want to
1658 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1659 TpConnection *connection,
1660 GHashTable *request,
1661 EmpathyDispatcherRequestCb *callback,
1664 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1665 ConnectionData *connection_data;
1666 DispatcherRequestData *request_data;
1667 const gchar *channel_type;
1672 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1673 g_return_if_fail (TP_IS_CONNECTION (connection));
1674 g_return_if_fail (request != NULL);
1676 connection_data = g_hash_table_lookup (priv->connections, connection);
1677 g_assert (connection_data != NULL);
1679 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1681 handle_type = tp_asv_get_uint32 (request,
1682 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1684 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1686 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1688 request_data = new_dispatcher_request_data (self, connection,
1689 channel_type, handle_type, handle, request,
1690 NULL, callback, user_data);
1692 connection_data->outstanding_requests = g_list_prepend
1693 (connection_data->outstanding_requests, request_data);
1695 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1699 channel_class_matches (GValueArray *class,
1700 const char *channel_type,
1702 GArray *fixed_properties)
1710 v = g_value_array_get_nth (class, 0);
1712 /* if the class doesn't match channel type discard it. */
1713 fprops = g_value_get_boxed (v);
1714 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1716 if (tp_strdiff (channel_type, c_type))
1719 /* we have the right channel type, see if the handle type matches */
1720 h_type = tp_asv_get_uint32 (fprops,
1721 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1723 if (!valid || (handle_type != h_type && handle_type != TP_UNKNOWN_HANDLE_TYPE))
1726 if (fixed_properties != NULL)
1728 gpointer h_key, h_val;
1730 GHashTableIter iter;
1733 g_hash_table_iter_init (&iter, fprops);
1735 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1737 /* discard ChannelType and TargetHandleType, as we already
1740 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1742 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1747 for (idx = 0; idx < fixed_properties->len; idx++)
1749 /* if |key| doesn't exist in |fixed_properties|, discard
1754 g_array_index (fixed_properties, char *, idx)))
1757 /* exit the for() loop */
1768 /* if no fixed_properties are specified, discard the classes
1769 * with some fixed properties other than the two we already
1772 if (g_hash_table_size (fprops) > 2)
1780 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1781 TpConnection *connection,
1782 const gchar *channel_type,
1784 GArray *fixed_properties)
1786 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1789 GList *matching_classes;
1793 g_return_val_if_fail (channel_type != NULL, NULL);
1795 cd = g_hash_table_lookup (priv->connections, connection);
1800 classes = cd->requestable_channels;
1801 if (classes == NULL)
1804 matching_classes = NULL;
1806 for (i = 0; i < classes->len; i++)
1808 class = g_ptr_array_index (classes, i);
1810 if (!channel_class_matches
1811 (class, channel_type, handle_type, fixed_properties))
1814 matching_classes = g_list_prepend (matching_classes, class);
1817 return matching_classes;
1821 find_channel_class_idle_cb (gpointer user_data)
1825 FindChannelRequest *request = user_data;
1827 gboolean is_ready = TRUE;
1828 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1830 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1832 cd = g_hash_table_lookup (priv->connections, request->connection);
1836 else if (cd->requestable_channels == NULL)
1841 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1842 request->connection, request->channel_type, request->handle_type,
1843 request->properties);
1845 request->callback (retval, request->user_data);
1846 free_find_channel_request (request);
1847 g_list_free (retval);
1852 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1853 request->connection);
1854 requests = g_list_prepend (requests, request);
1856 g_hash_table_insert (priv->outstanding_classes_requests,
1857 request->connection, requests);
1863 setup_varargs (va_list var_args,
1864 const char *channel_namespace,
1865 const char *first_property_name)
1871 if (first_property_name == NULL)
1874 name = first_property_name;
1875 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1877 while (name != NULL)
1879 name_full = g_strdup (name);
1880 properties = g_array_append_val (properties, name_full);
1881 name = va_arg (var_args, char *);
1888 * empathy_dispatcher_find_requestable_channel_classes:
1889 * @dispatcher: an #EmpathyDispatcher
1890 * @connection: a #TpConnection
1891 * @channel_type: a string identifying the type of the channel to lookup
1892 * @handle_type: the handle type for the channel, or %TP_UNKNOWN_HANDLE_TYPE
1893 * if you don't care about the channel's target handle type
1894 * @first_property_name: %NULL, or the name of the first fixed property,
1895 * followed optionally by more names, followed by %NULL.
1897 * Returns all the channel classes that a client can request for the connection
1898 * @connection, of the type identified by @channel_type, @handle_type and the
1899 * fixed properties list.
1900 * The classes which are compatible with a fixed properties list (i.e. those
1901 * that will be returned by this function) are intended as those that do not
1902 * contain any fixed property other than those in the list; note that this
1903 * doesn't guarantee that all the classes compatible with the list will contain
1904 * all the requested fixed properties, so the clients will have to filter
1905 * the returned list themselves.
1906 * If @first_property_name is %NULL, only the classes with no other fixed
1907 * properties than ChannelType and TargetHandleType will be returned.
1908 * Note that this function may return %NULL without performing any lookup if
1909 * @connection is not ready. To ensure that @connection is always ready,
1910 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1912 * Return value: a #GList of #GValueArray objects, where the first element in
1913 * the array is a #GHashTable of the fixed properties, and the second is
1914 * a #GStrv of the allowed properties for the class. The list should be free'd
1915 * with g_list_free() when done, but the objects inside the list are owned
1916 * by the #EmpathyDispatcher and must not be modified.
1919 empathy_dispatcher_find_requestable_channel_classes
1920 (EmpathyDispatcher *self,
1921 TpConnection *connection,
1922 const gchar *channel_type,
1924 const char *first_property_name,
1929 EmpathyDispatcherPriv *priv;
1934 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1935 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1936 g_return_val_if_fail (channel_type != NULL, NULL);
1938 priv = GET_PRIV (self);
1940 va_start (var_args, first_property_name);
1942 properties = setup_varargs (var_args, channel_type, first_property_name);
1946 retval = empathy_dispatcher_find_channel_classes (self, connection,
1947 channel_type, handle_type, properties);
1949 if (properties != NULL)
1951 /* free the properties array */
1952 for (idx = 0; idx < properties->len ; idx++)
1954 str = g_array_index (properties, char *, idx);
1958 g_array_free (properties, TRUE);
1965 * empathy_dispatcher_find_requestable_channel_classes_async:
1966 * @dispatcher: an #EmpathyDispatcher
1967 * @connection: a #TpConnection
1968 * @channel_type: a string identifying the type of the channel to lookup
1969 * @handle_type: the handle type for the channel
1970 * @callback: the callback to call when @connection is ready
1971 * @user_data: the user data to pass to @callback
1972 * @first_property_name: %NULL, or the name of the first fixed property,
1973 * followed optionally by more names, followed by %NULL.
1975 * Please see the documentation of
1976 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1977 * description of this function.
1980 empathy_dispatcher_find_requestable_channel_classes_async
1981 (EmpathyDispatcher *self,
1982 TpConnection *connection,
1983 const gchar *channel_type,
1985 EmpathyDispatcherFindChannelClassCb callback,
1987 const char *first_property_name,
1992 FindChannelRequest *request;
1993 EmpathyDispatcherPriv *priv;
1996 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1997 g_return_if_fail (TP_IS_CONNECTION (connection));
1998 g_return_if_fail (channel_type != NULL);
1999 g_return_if_fail (handle_type != 0);
2001 priv = GET_PRIV (self);
2003 va_start (var_args, first_property_name);
2005 properties = setup_varargs (var_args, channel_type, first_property_name);
2009 /* append another request for this connection */
2010 request = g_slice_new0 (FindChannelRequest);
2011 request->dispatcher = g_object_ref (self);
2012 request->channel_type = g_strdup (channel_type);
2013 request->handle_type = handle_type;
2014 request->connection = connection;
2015 request->callback = callback;
2016 request->user_data = user_data;
2017 request->properties = properties;
2019 source_id = g_idle_add (find_channel_class_idle_cb, request);
2021 g_hash_table_insert (priv->request_channel_class_async_ids,
2022 request, GUINT_TO_POINTER (source_id));
2026 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2029 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2030 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2032 return priv->channels;
2036 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2037 const gchar *account_path,
2038 const gchar *connection_path,
2039 const GPtrArray *channels,
2040 const GPtrArray *requests_satisfied,
2042 GHashTable *handler_info,
2046 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2047 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2050 TpConnection *connection;
2052 /* FIXME: should probably find out whether the account manager is prepared
2053 * before ensuring. See bug #600111. */
2054 account = tp_account_manager_ensure_account (priv->account_manager,
2056 g_assert (account != NULL);
2058 connection = tp_account_ensure_connection (account, connection_path);
2059 if (connection == NULL)
2061 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2062 "Invalid connection argument");
2066 for (i = 0; i < channels->len ; i++)
2068 GValueArray *arr = g_ptr_array_index (channels, i);
2069 const gchar *object_path;
2070 GHashTable *properties;
2072 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2073 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2075 dispatcher_connection_new_channel_with_properties (self,
2076 connection, object_path, properties, requests_satisfied);
2084 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2089 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2090 EmpathyHandler *handler;
2092 handler = empathy_handler_new (name, filters, capabilities);
2093 priv->handlers = g_list_prepend (priv->handlers, handler);
2095 /* Only set the handle_channels function, the Channel property on the main
2096 * handler will always report all dispatched channels even if they came from
2097 * a different Handler */
2098 empathy_handler_set_handle_channels_func (handler,
2099 empathy_dispatcher_handle_channels, self);
2105 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2106 EmpathyHandler *handler)
2108 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2111 h = g_list_find (priv->handlers, handler);
2112 g_return_if_fail (h != NULL);
2114 priv->handlers = g_list_delete_link (priv->handlers, h);
2116 g_object_unref (handler);