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);
339 DEBUG ("Error: %s", message);
340 g_hash_table_remove (priv->connections, connection);
344 dispatch_operation_flush_requests (EmpathyDispatcher *self,
345 EmpathyDispatchOperation *operation,
351 l = cd->outstanding_requests;
354 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
359 if (d->operation == operation)
364 d->cb (NULL, error, d->user_data);
366 d->cb (operation, NULL, d->user_data);
369 cd->outstanding_requests = g_list_delete_link
370 (cd->outstanding_requests, lt);
372 free_dispatcher_request_data (d);
378 dispatcher_channel_invalidated_cb (TpProxy *proxy,
382 EmpathyDispatcher *self)
384 /* Channel went away... */
385 EmpathyDispatcherPriv *priv = GET_PRIV (self);
386 TpConnection *connection;
388 const gchar *object_path;
390 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
392 cd = g_hash_table_lookup (priv->connections, connection);
393 /* Connection itself invalidated? */
397 object_path = tp_proxy_get_object_path (proxy);
399 DEBUG ("Channel %s invalidated", object_path);
401 g_hash_table_remove (cd->dispatched_channels, object_path);
402 g_hash_table_remove (cd->dispatching_channels, object_path);
404 priv->channels = g_list_remove (priv->channels, proxy);
408 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
409 EmpathyDispatcher *self)
411 g_assert (empathy_dispatch_operation_is_incoming (operation));
412 DEBUG ("Send of for dispatching: %s",
413 empathy_dispatch_operation_get_object_path (operation));
414 g_signal_emit (self, signals[DISPATCH], 0, operation);
418 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
419 EmpathyDispatcher *self)
421 /* Our job is done, remove the dispatch operation and mark the channel as
423 EmpathyDispatcherPriv *priv = GET_PRIV (self);
424 TpConnection *connection;
426 const gchar *object_path;
428 connection = empathy_dispatch_operation_get_tp_connection (operation);
429 cd = g_hash_table_lookup (priv->connections, connection);
430 g_assert (cd != NULL);
432 object_path = empathy_dispatch_operation_get_object_path (operation);
434 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
437 d = new_dispatch_data (
438 empathy_dispatch_operation_get_channel (operation),
439 empathy_dispatch_operation_get_channel_wrapper (operation));
440 g_hash_table_insert (cd->dispatched_channels,
441 g_strdup (object_path), d);
443 g_hash_table_remove (cd->dispatching_channels, object_path);
445 DEBUG ("Channel claimed: %s", object_path);
449 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
450 EmpathyDispatcher *self)
452 EmpathyDispatcherPriv *priv = GET_PRIV (self);
453 TpConnection *connection;
455 EmpathyDispatchOperationState status;
457 g_signal_connect (operation, "approved",
458 G_CALLBACK (dispatch_operation_approved_cb), self);
460 g_signal_connect (operation, "claimed",
461 G_CALLBACK (dispatch_operation_claimed_cb), self);
463 /* Signal the observers */
464 DEBUG ("Send to observers: %s",
465 empathy_dispatch_operation_get_object_path (operation));
466 g_signal_emit (self, signals[OBSERVE], 0, operation);
468 empathy_dispatch_operation_start (operation);
470 /* Signal potential requestors */
471 connection = empathy_dispatch_operation_get_tp_connection (operation);
472 cd = g_hash_table_lookup (priv->connections, connection);
473 g_assert (cd != NULL);
475 g_object_ref (operation);
478 dispatch_operation_flush_requests (self, operation, NULL, cd);
479 status = empathy_dispatch_operation_get_status (operation);
480 g_object_unref (operation);
482 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
485 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
487 DEBUG ("Send to approvers: %s",
488 empathy_dispatch_operation_get_object_path (operation));
489 g_signal_emit (self, signals[APPROVE], 0, operation);
493 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
494 DEBUG ("Send of for dispatching: %s",
495 empathy_dispatch_operation_get_object_path (operation));
496 g_signal_emit (self, signals[DISPATCH], 0, operation);
499 g_object_unref (self);
503 dispatcher_start_dispatching (EmpathyDispatcher *self,
504 EmpathyDispatchOperation *operation,
507 const gchar *object_path =
508 empathy_dispatch_operation_get_object_path (operation);
510 DEBUG ("Dispatching process started for %s", object_path);
512 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
514 g_hash_table_insert (cd->dispatching_channels,
515 g_strdup (object_path), operation);
517 switch (empathy_dispatch_operation_get_status (operation))
519 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
520 g_signal_connect (operation, "ready",
521 G_CALLBACK (dispatch_operation_ready_cb), self);
523 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
524 dispatch_operation_ready_cb (operation, self);
527 g_assert_not_reached ();
531 else if (empathy_dispatch_operation_get_status (operation) >=
532 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
534 /* Already dispatching and the operation is pending, thus the observers
535 * have seen it (if applicable), so we can flush the request right away.
537 dispatch_operation_flush_requests (self, operation, NULL, cd);
542 dispatcher_connection_new_channel (EmpathyDispatcher *self,
543 TpConnection *connection,
544 const gchar *object_path,
545 const gchar *channel_type,
548 GHashTable *properties,
550 const GPtrArray *requests_satisfied)
552 EmpathyDispatcherPriv *priv = GET_PRIV (self);
555 EmpathyDispatchOperation *operation;
557 /* Channel types we never want to dispatch because they're either deprecated
558 * or can't sensibly be dispatch (e.g. channels that should always be
560 const char *blacklist[] = {
561 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
562 TP_IFACE_CHANNEL_TYPE_TUBES,
563 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
567 dispatcher_init_connection_if_needed (self, connection);
569 cd = g_hash_table_lookup (priv->connections, connection);
571 for (i = 0 ; blacklist[i] != NULL; i++)
573 if (!tp_strdiff (channel_type, blacklist[i]))
575 DEBUG ("Ignoring blacklisted channel type %s on %s",
576 channel_type, object_path);
581 DEBUG ("%s channel of type %s on %s", incoming ? "incoming" : "outgoing",
582 channel_type, object_path);
584 if ((operation = g_hash_table_lookup (cd->dispatching_channels,
585 object_path)) != NULL)
587 /* This operation was already being dispatched, assume we got the channel
588 * again because something asked for it and approve it right away */
589 empathy_dispatch_operation_approve (operation);
593 if (properties == NULL)
594 channel = tp_channel_new (connection, object_path, channel_type,
595 handle_type, handle, NULL);
597 channel = tp_channel_new_from_properties (connection, object_path,
600 g_signal_connect (channel, "invalidated",
601 G_CALLBACK (dispatcher_channel_invalidated_cb),
604 priv->channels = g_list_prepend (priv->channels, channel);
606 operation = empathy_dispatch_operation_new (connection, channel, NULL,
609 g_object_unref (channel);
611 if (!incoming && requests_satisfied != NULL)
614 gboolean found = FALSE;
616 l = cd->outstanding_requests;
620 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
625 if (d->request == NULL)
628 if (d->operation != NULL)
631 path = tp_proxy_get_object_path (d->channel_request);
632 for (n = 0; n < requests_satisfied->len ; n++)
634 const gchar *p = g_ptr_array_index (requests_satisfied, n);
635 if (!tp_strdiff (p, path))
637 DEBUG ("Channel satified request %s"
638 "(already dispatched: %d)", p, found);
641 d->operation = operation;
647 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
649 dispatcher_request_failed (dispatcher, d, &err);
656 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
657 empathy_dispatch_operation_approve (operation);
659 dispatcher_start_dispatching (dispatcher, operation, cd);
663 dispatcher_connection_new_channel_with_properties (
664 EmpathyDispatcher *self,
665 TpConnection *connection,
666 const gchar *object_path,
667 GHashTable *properties,
668 const GPtrArray *requests_satisfied)
670 const gchar *channel_type;
676 channel_type = tp_asv_get_string (properties,
677 TP_IFACE_CHANNEL ".ChannelType");
678 if (channel_type == NULL)
680 g_message ("%s had an invalid ChannelType property", object_path);
684 handle_type = tp_asv_get_uint32 (properties,
685 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
688 g_message ("%s had an invalid TargetHandleType property", object_path);
692 handle = tp_asv_get_uint32 (properties,
693 TP_IFACE_CHANNEL ".TargetHandle", &valid);
696 g_message ("%s had an invalid TargetHandle property", object_path);
700 /* We assume there is no channel dispather, so we're the only one dispatching
701 * it. Which means that a requested channel it is outgoing one */
702 requested = tp_asv_get_boolean (properties,
703 TP_IFACE_CHANNEL ".Requested", &valid);
706 g_message ("%s had an invalid Requested property", object_path);
710 dispatcher_connection_new_channel (self, connection,
711 object_path, channel_type, handle_type, handle, properties, !requested,
716 dispatcher_connection_got_all (TpProxy *proxy,
717 GHashTable *properties,
722 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
723 EmpathyDispatcherPriv *priv = GET_PRIV (self);
724 GPtrArray *requestable_channels;
727 DEBUG ("Error: %s", error->message);
731 requestable_channels = tp_asv_get_boxed (properties,
732 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
734 if (requestable_channels == NULL)
735 DEBUG ("No RequestableChannelClasses property !?! on connection");
740 FindChannelRequest *request;
743 cd = g_hash_table_lookup (priv->connections, proxy);
744 g_assert (cd != NULL);
746 cd->requestable_channels = g_boxed_copy (
747 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
749 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
752 for (l = requests; l != NULL; l = l->next)
756 retval = empathy_dispatcher_find_channel_classes (self,
757 TP_CONNECTION (proxy), request->channel_type,
758 request->handle_type, request->properties);
759 request->callback (retval, request->user_data);
761 free_find_channel_request (request);
762 g_list_free (retval);
765 g_list_free (requests);
767 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
772 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
773 const GPtrArray *capabilities,
779 DEBUG ("Error: %s", error->message);
783 connection_ready_cb (TpConnection *connection,
787 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
788 GPtrArray *capabilities;
791 const gchar *remove_ = NULL;
795 DEBUG ("Error: %s", error->message);
799 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
800 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
802 tp_cli_dbus_properties_call_get_all (connection, -1,
803 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
804 dispatcher_connection_got_all,
805 NULL, NULL, G_OBJECT (self));
808 /* Advertise VoIP capabilities */
809 capabilities = g_ptr_array_sized_new (1);
810 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
811 G_TYPE_UINT, G_TYPE_INVALID);
812 g_value_init (&cap, cap_type);
813 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
814 dbus_g_type_struct_set (&cap,
815 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
816 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
817 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
818 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
819 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
820 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
822 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
824 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
825 connection, -1, capabilities, &remove_,
826 dispatcher_connection_advertise_capabilities_cb,
827 NULL, NULL, G_OBJECT (self));
829 g_value_unset (&cap);
830 g_ptr_array_free (capabilities, TRUE);
832 g_object_unref (self);
836 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
837 TpConnection *connection)
839 EmpathyDispatcherPriv *priv = GET_PRIV (self);
841 if (g_hash_table_lookup (priv->connections, connection) != NULL)
844 g_hash_table_insert (priv->connections, g_object_ref (connection),
845 new_connection_data ());
847 g_signal_connect (connection, "invalidated",
848 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
850 /* Ensure to keep the self object alive while the call_when_ready is
853 tp_connection_call_when_ready (connection, connection_ready_cb, self);
857 dispatcher_status_changed_cb (TpAccount *account,
861 gchar *dbus_error_name,
863 EmpathyDispatcher *self)
865 TpConnection *conn = tp_account_get_connection (account);
868 dispatcher_init_connection_if_needed (self, conn);
872 remove_idle_handlers (gpointer key,
878 source_id = GPOINTER_TO_UINT (value);
879 g_source_remove (source_id);
883 dispatcher_constructor (GType type,
884 guint n_construct_params,
885 GObjectConstructParam *construct_params)
888 EmpathyDispatcherPriv *priv;
890 if (dispatcher != NULL)
891 return g_object_ref (dispatcher);
893 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
894 (type, n_construct_params, construct_params);
896 dispatcher = EMPATHY_DISPATCHER (retval);
897 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
899 priv = GET_PRIV (dispatcher);
901 if (priv->handler == NULL)
902 priv->handler = empathy_handler_new (NULL, NULL, NULL);
904 empathy_handler_set_handle_channels_func (priv->handler,
905 empathy_dispatcher_handle_channels,
908 empathy_handler_set_channels_func (priv->handler,
909 empathy_dispatcher_get_channels,
916 dispatcher_dispose (GObject *object)
918 EmpathyDispatcherPriv *priv = GET_PRIV (object);
923 if (priv->dispose_has_run)
926 priv->dispose_has_run = TRUE;
928 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
929 g_object_unref (G_OBJECT (l->data));
931 g_list_free (priv->handlers);
932 priv->handlers = NULL;
934 if (priv->handler != NULL)
935 g_object_unref (priv->handler);
936 priv->handler = NULL;
938 g_hash_table_iter_init (&iter, priv->connections);
939 while (g_hash_table_iter_next (&iter, &connection, NULL))
941 g_signal_handlers_disconnect_by_func (connection,
942 dispatcher_connection_invalidated_cb, object);
945 g_hash_table_destroy (priv->connections);
946 priv->connections = NULL;
948 if (priv->channel_dispatcher != NULL)
949 g_object_unref (priv->channel_dispatcher);
950 priv->channel_dispatcher = NULL;
952 if (priv->dbus != NULL)
953 g_object_unref (priv->dbus);
956 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
960 dispatcher_finalize (GObject *object)
962 EmpathyDispatcherPriv *priv = GET_PRIV (object);
967 gpointer account, id;
969 if (priv->request_channel_class_async_ids != NULL)
971 g_hash_table_foreach (priv->request_channel_class_async_ids,
972 remove_idle_handlers, NULL);
973 g_hash_table_destroy (priv->request_channel_class_async_ids);
976 for (l = priv->channels; l; l = l->next)
978 g_signal_handlers_disconnect_by_func (l->data,
979 dispatcher_channel_invalidated_cb, object);
982 g_list_free (priv->channels);
984 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
985 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
987 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
991 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
992 while (g_hash_table_iter_next (&iter, &account, &id))
994 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
996 g_hash_table_destroy (priv->status_changed_handlers);
998 g_object_unref (priv->account_manager);
1000 g_hash_table_destroy (priv->outstanding_classes_requests);
1004 dispatcher_set_property (GObject *object,
1006 const GValue *value,
1009 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1010 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1012 switch (property_id)
1015 priv->handler = g_value_dup_object (value);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1024 dispatcher_get_property (GObject *object,
1029 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1030 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1032 switch (property_id)
1035 g_value_set_object (value, priv->handler);
1038 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1044 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1046 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1047 GParamSpec *param_spec;
1049 object_class->dispose = dispatcher_dispose;
1050 object_class->finalize = dispatcher_finalize;
1051 object_class->constructor = dispatcher_constructor;
1053 object_class->get_property = dispatcher_get_property;
1054 object_class->set_property = dispatcher_set_property;
1056 param_spec = g_param_spec_object ("handler", "handler",
1057 "The main Telepathy Client Hander object",
1058 EMPATHY_TYPE_HANDLER,
1059 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1060 g_object_class_install_property (object_class,
1061 PROP_HANDLER, param_spec);
1064 g_signal_new ("observe",
1065 G_TYPE_FROM_CLASS (klass),
1069 g_cclosure_marshal_VOID__OBJECT,
1071 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1074 g_signal_new ("approve",
1075 G_TYPE_FROM_CLASS (klass),
1079 g_cclosure_marshal_VOID__OBJECT,
1081 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1084 g_signal_new ("dispatch",
1085 G_TYPE_FROM_CLASS (klass),
1089 g_cclosure_marshal_VOID__OBJECT,
1091 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1094 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1098 connect_account (EmpathyDispatcher *self,
1101 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1102 TpConnection *conn = tp_account_get_connection (account);
1105 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1112 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1114 id = g_signal_connect (account, "status-changed",
1115 G_CALLBACK (dispatcher_status_changed_cb), self);
1117 g_hash_table_insert (priv->status_changed_handlers, account,
1118 GUINT_TO_POINTER (id));
1122 account_manager_prepared_cb (GObject *source_object,
1123 GAsyncResult *result,
1126 GList *accounts, *l;
1127 EmpathyDispatcher *self = user_data;
1128 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1129 GError *error = NULL;
1131 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1133 DEBUG ("Failed to prepare account manager: %s", error->message);
1134 g_error_free (error);
1138 accounts = tp_account_manager_get_valid_accounts (account_manager);
1139 for (l = accounts; l; l = l->next)
1141 TpAccount *a = l->data;
1143 connect_account (self, a);
1145 g_list_free (accounts);
1149 account_prepare_cb (GObject *source_object,
1150 GAsyncResult *result,
1153 EmpathyDispatcher *self = user_data;
1154 TpAccount *account = TP_ACCOUNT (source_object);
1155 GError *error = NULL;
1157 if (!tp_account_prepare_finish (account, result, &error))
1159 DEBUG ("Failed to prepare account: %s", error->message);
1160 g_error_free (error);
1164 connect_account (self, account);
1168 account_validity_changed_cb (TpAccountManager *manager,
1176 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1180 empathy_dispatcher_init (EmpathyDispatcher *self)
1182 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1183 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1186 priv->account_manager = tp_account_manager_dup ();
1188 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1189 g_object_unref, (GDestroyNotify) free_connection_data);
1191 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1192 g_direct_equal, g_object_unref, NULL);
1194 priv->channels = NULL;
1196 tp_account_manager_prepare_async (priv->account_manager, NULL,
1197 account_manager_prepared_cb, self);
1199 empathy_signal_connect_weak (priv->account_manager,
1200 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1203 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1205 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1208 priv->dbus = tp_dbus_daemon_dup (NULL);
1209 priv->channel_dispatcher = tp_channel_dispatcher_new (priv->dbus);
1213 empathy_dispatcher_new (const gchar *name,
1217 EmpathyHandler *handler;
1218 EmpathyDispatcher *ret;
1220 g_assert (dispatcher == NULL);
1221 handler = empathy_handler_new (name, filters, capabilities);
1223 ret = EMPATHY_DISPATCHER (
1224 g_object_new (EMPATHY_TYPE_DISPATCHER,
1227 g_object_unref (handler);
1233 empathy_dispatcher_dup_singleton (void)
1235 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1239 dispatcher_request_failed (EmpathyDispatcher *self,
1240 DispatcherRequestData *request_data,
1241 const GError *error)
1243 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1244 ConnectionData *conn_data;
1246 conn_data = g_hash_table_lookup (priv->connections,
1247 request_data->connection);
1248 if (request_data->cb != NULL)
1249 request_data->cb (NULL, error, request_data->user_data);
1251 if (conn_data != NULL)
1253 conn_data->outstanding_requests =
1254 g_list_remove (conn_data->outstanding_requests, request_data);
1256 /* else Connection has been invalidated */
1258 free_dispatcher_request_data (request_data);
1262 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1263 DispatcherRequestData *request_data,
1264 const gchar *object_path,
1265 GHashTable *properties,
1266 const GError *error)
1268 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1269 EmpathyDispatchOperation *operation = NULL;
1270 ConnectionData *conn_data;
1272 /* The DispatcherRequestData owns a ref on the self object. As the request
1273 * data could be destroyed (when calling dispatcher_request_failed for
1274 * example) we keep a ref on self to be sure it stays alive while we are
1275 * executing this function. */
1276 g_object_ref (self);
1278 conn_data = g_hash_table_lookup (priv->connections,
1279 request_data->connection);
1283 DEBUG ("Channel request failed: %s", error->message);
1285 dispatcher_request_failed (self, request_data, error);
1290 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1293 if (operation == NULL)
1295 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1300 operation = empathy_dispatch_operation_new_with_wrapper (
1301 request_data->connection,
1302 data->channel, request_data->contact, FALSE,
1303 data->channel_wrapper);
1309 if (properties != NULL)
1310 channel = tp_channel_new_from_properties (request_data->connection,
1311 object_path, properties, NULL);
1313 channel = tp_channel_new (request_data->connection, object_path,
1314 request_data->channel_type, request_data->handle_type,
1315 request_data->handle, NULL);
1317 g_signal_connect (channel, "invalidated",
1318 G_CALLBACK (dispatcher_channel_invalidated_cb),
1319 request_data->dispatcher);
1321 priv->channels = g_list_prepend (priv->channels, channel);
1323 operation = empathy_dispatch_operation_new (request_data->connection,
1324 channel, request_data->contact, FALSE);
1325 g_object_unref (channel);
1330 /* Already existed set potential extra information */
1331 g_object_set (G_OBJECT (operation),
1332 "contact", request_data->contact,
1336 request_data->operation = operation;
1338 /* (pre)-approve this right away as we requested it
1339 * This might cause the channel to be claimed, in which case the operation
1340 * will disappear. So ref it, and check the status before starting the
1343 g_object_ref (operation);
1344 empathy_dispatch_operation_approve (operation);
1346 if (empathy_dispatch_operation_get_status (operation) <
1347 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1348 dispatcher_start_dispatching (request_data->dispatcher, operation,
1351 g_object_unref (operation);
1355 dispatcher_request_channel_cb (TpConnection *connection,
1356 const gchar *object_path,
1357 const GError *error,
1359 GObject *weak_object)
1361 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1362 EmpathyDispatcher *self =
1363 EMPATHY_DISPATCHER (request_data->dispatcher);
1365 request_data->pending_call = NULL;
1367 dispatcher_connection_new_requested_channel (self,
1368 request_data, object_path, NULL, error);
1372 dispatcher_request_channel (DispatcherRequestData *request_data)
1374 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1375 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1377 /* Extend the request_data to be a valid request */
1378 g_assert (request_data->request == NULL);
1379 request_data->request = tp_asv_new (
1380 TP_IFACE_CHANNEL ".ChannelType",
1381 G_TYPE_STRING, request_data->channel_type,
1382 TP_IFACE_CHANNEL ".TargetHandleType",
1383 G_TYPE_UINT, request_data->handle_type,
1386 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1387 tp_asv_set_uint32 (request_data->request,
1388 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1390 empathy_dispatcher_call_create_or_ensure_channel (
1391 request_data->dispatcher, request_data);
1395 TpProxyPendingCall *call = tp_cli_connection_call_request_channel (
1396 request_data->connection, -1,
1397 request_data->channel_type,
1398 request_data->handle_type,
1399 request_data->handle,
1400 TRUE, dispatcher_request_channel_cb,
1401 request_data, NULL, NULL);
1404 request_data->pending_call = call;
1409 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1410 EmpathyDispatcherRequestCb *callback,
1413 EmpathyDispatcher *self;
1414 EmpathyDispatcherPriv *priv;
1415 TpConnection *connection;
1416 ConnectionData *connection_data;
1417 DispatcherRequestData *request_data;
1419 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1421 self = empathy_dispatcher_dup_singleton ();
1422 priv = GET_PRIV (self);
1424 connection = empathy_contact_get_connection (contact);
1425 connection_data = g_hash_table_lookup (priv->connections, connection);
1427 /* The contact handle might not be known yet */
1428 request_data = new_dispatcher_request_data (self, connection,
1429 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1430 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1431 request_data->should_ensure = TRUE;
1433 connection_data->outstanding_requests = g_list_prepend
1434 (connection_data->outstanding_requests, request_data);
1436 dispatcher_request_channel (request_data);
1438 g_object_unref (self);
1443 EmpathyDispatcher *dispatcher;
1444 EmpathyDispatcherRequestCb *callback;
1446 } ChatWithContactIdData;
1449 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1450 EmpathyContact *contact,
1451 const GError *error,
1453 GObject *weak_object)
1455 ChatWithContactIdData *data = user_data;
1459 /* FIXME: Should call data->callback with the error */
1460 DEBUG ("Error: %s", error->message);
1464 empathy_dispatcher_chat_with_contact (contact, data->callback,
1468 g_object_unref (data->dispatcher);
1469 g_slice_free (ChatWithContactIdData, data);
1473 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1474 const gchar *contact_id,
1475 EmpathyDispatcherRequestCb *callback,
1478 EmpathyDispatcher *self;
1479 EmpathyTpContactFactory *factory;
1480 ChatWithContactIdData *data;
1482 g_return_if_fail (TP_IS_CONNECTION (connection));
1483 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1485 self = empathy_dispatcher_dup_singleton ();
1486 factory = empathy_tp_contact_factory_dup_singleton (connection);
1487 data = g_slice_new0 (ChatWithContactIdData);
1488 data->dispatcher = self;
1489 data->callback = callback;
1490 data->user_data = user_data;
1491 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1492 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1494 g_object_unref (factory);
1498 dispatcher_request_handles_cb (TpConnection *connection,
1499 const GArray *handles,
1500 const GError *error,
1504 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1506 request_data->pending_call = NULL;
1510 EmpathyDispatcher *self = request_data->dispatcher;
1511 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1514 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1516 if (request_data->cb)
1517 request_data->cb (NULL, error, request_data->user_data);
1519 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1522 free_dispatcher_request_data (request_data);
1526 request_data->handle = g_array_index (handles, guint, 0);
1527 dispatcher_request_channel (request_data);
1531 empathy_dispatcher_join_muc (TpConnection *connection,
1532 const gchar *roomname,
1533 EmpathyDispatcherRequestCb *callback,
1536 EmpathyDispatcher *self;
1537 EmpathyDispatcherPriv *priv;
1538 DispatcherRequestData *request_data;
1539 ConnectionData *connection_data;
1540 const gchar *names[] = { roomname, NULL };
1541 TpProxyPendingCall *call;
1543 g_return_if_fail (TP_IS_CONNECTION (connection));
1544 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1546 self = empathy_dispatcher_dup_singleton ();
1547 priv = GET_PRIV (self);
1549 connection_data = g_hash_table_lookup (priv->connections, connection);
1550 g_assert (connection_data != NULL);
1552 /* Don't know the room handle yet */
1553 request_data = new_dispatcher_request_data (self, connection,
1554 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1555 NULL, callback, user_data);
1556 request_data->should_ensure = TRUE;
1558 connection_data->outstanding_requests = g_list_prepend
1559 (connection_data->outstanding_requests, request_data);
1561 call = tp_cli_connection_call_request_handles (
1563 TP_HANDLE_TYPE_ROOM, names,
1564 dispatcher_request_handles_cb, request_data, NULL, NULL);
1567 request_data->pending_call = call;
1569 g_object_unref (self);
1573 dispatcher_channel_request_failed_cb (TpChannelRequest *request,
1575 const gchar *message,
1577 GObject *weak_object)
1579 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1580 EmpathyDispatcher *self =
1581 EMPATHY_DISPATCHER (request_data->dispatcher);
1584 request_data->pending_call = NULL;
1586 DEBUG ("Request failed: %s - %s %s",
1587 tp_proxy_get_object_path (request),
1590 tp_proxy_dbus_error_to_gerror (TP_PROXY (request),
1591 error, message, &err);
1593 dispatcher_request_failed (self, request_data, err);
1599 dispatcher_channel_request_succeeded_cb (TpChannelRequest *request,
1601 GObject *weak_object)
1603 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1604 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1605 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1606 ConnectionData *conn_data;
1608 conn_data = g_hash_table_lookup (priv->connections,
1609 request_data->connection);
1611 DEBUG ("Request succeeded: %s", tp_proxy_get_object_path (request));
1613 /* When success gets called the internal request should have been satisfied,
1614 * if it's still in outstanding_requests and doesn't have an operation
1615 * assigned to it, the channel got handled by someone else.. */
1617 if (g_list_find (conn_data->outstanding_requests, request_data) == NULL)
1620 if (request_data->operation == NULL)
1622 GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS, "Not yours!" };
1623 dispatcher_request_failed (self, request_data, &err);
1628 dispatcher_channel_request_proceed_cb (TpChannelRequest *request,
1629 const GError *error,
1631 GObject *weak_object)
1633 EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1634 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1636 request_data->pending_call = NULL;
1639 dispatcher_request_failed (self, request_data, error);
1643 dispatcher_create_channel_cb (TpChannelDispatcher *proxy,
1644 const gchar *request_path,
1645 const GError *error,
1647 GObject *weak_object)
1649 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1650 EmpathyDispatcher *self = EMPATHY_DISPATCHER (request_data->dispatcher);
1651 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1652 TpChannelRequest *request;
1654 TpProxyPendingCall *call;
1656 request_data->pending_call = NULL;
1660 dispatcher_request_failed (self, request_data, error);
1664 request = tp_channel_request_new (priv->dbus, request_path, NULL, NULL);
1665 request_data->channel_request = request;
1667 if (tp_cli_channel_request_connect_to_failed (request,
1668 dispatcher_channel_request_failed_cb, request_data,
1669 NULL, G_OBJECT (self), &err) == NULL)
1671 dispatcher_request_failed (self, request_data, err);
1676 if (tp_cli_channel_request_connect_to_succeeded (request,
1677 dispatcher_channel_request_succeeded_cb, request_data,
1678 NULL, G_OBJECT (self), &err) == NULL)
1680 dispatcher_request_failed (self, request_data, err);
1685 call = tp_cli_channel_request_call_proceed (request,
1686 -1, dispatcher_channel_request_proceed_cb,
1687 request_data, NULL, G_OBJECT (self));
1690 request_data->pending_call = call;
1694 empathy_dispatcher_call_create_or_ensure_channel (
1695 EmpathyDispatcher *self,
1696 DispatcherRequestData *request_data)
1698 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1700 const gchar *handler = "";
1701 TpProxyPendingCall *call;
1703 account = empathy_get_account_for_connection (request_data->connection);
1705 g_assert (account != NULL);
1707 if (request_data->cb)
1708 handler = empathy_handler_get_busname (priv->handler);
1710 if (request_data->should_ensure)
1712 call = tp_cli_channel_dispatcher_call_ensure_channel (
1713 priv->channel_dispatcher,
1714 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1715 request_data->request, 0, handler,
1716 dispatcher_create_channel_cb, request_data, NULL, NULL);
1720 call = tp_cli_channel_dispatcher_call_create_channel (
1721 priv->channel_dispatcher,
1722 -1, tp_proxy_get_object_path (TP_PROXY (account)),
1723 request_data->request, 0, handler,
1724 dispatcher_create_channel_cb, request_data, NULL,
1725 G_OBJECT (dispatcher));
1729 request_data->pending_call = call;
1733 * empathy_dispatcher_create_channel:
1734 * @self: the EmpathyDispatcher
1735 * @connection: the Connection to dispatch on
1736 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1737 * @callback: a callback for when the channel arrives (or NULL)
1738 * @user_data: optional user data (or NULL)
1740 * When calling this function, #EmpathyDispatcher takes ownership of your
1741 * reference to @request. DO NOT unref or destroy @request. When the request is
1742 * done, @request will be unreferenced. Take another reference if you want to
1746 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1747 TpConnection *connection,
1748 GHashTable *request,
1749 EmpathyDispatcherRequestCb *callback,
1752 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1753 ConnectionData *connection_data;
1754 DispatcherRequestData *request_data;
1755 const gchar *channel_type;
1760 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1761 g_return_if_fail (TP_IS_CONNECTION (connection));
1762 g_return_if_fail (request != NULL);
1764 connection_data = g_hash_table_lookup (priv->connections, connection);
1765 g_assert (connection_data != NULL);
1767 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1769 handle_type = tp_asv_get_uint32 (request,
1770 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1772 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1774 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1776 request_data = new_dispatcher_request_data (self, connection,
1777 channel_type, handle_type, handle, request,
1778 NULL, callback, user_data);
1780 connection_data->outstanding_requests = g_list_prepend
1781 (connection_data->outstanding_requests, request_data);
1783 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1787 channel_class_matches (GValueArray *class,
1788 const char *channel_type,
1790 GArray *fixed_properties)
1798 v = g_value_array_get_nth (class, 0);
1800 /* if the class doesn't match channel type discard it. */
1801 fprops = g_value_get_boxed (v);
1802 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1804 if (tp_strdiff (channel_type, c_type))
1807 /* we have the right channel type, see if the handle type matches */
1808 h_type = tp_asv_get_uint32 (fprops,
1809 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1811 if (!valid || (handle_type != h_type && handle_type != TP_UNKNOWN_HANDLE_TYPE))
1814 if (fixed_properties != NULL)
1816 gpointer h_key, h_val;
1818 GHashTableIter iter;
1821 g_hash_table_iter_init (&iter, fprops);
1823 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1825 /* discard ChannelType and TargetHandleType, as we already
1828 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1830 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1835 for (idx = 0; idx < fixed_properties->len; idx++)
1837 /* if |key| doesn't exist in |fixed_properties|, discard
1842 g_array_index (fixed_properties, char *, idx)))
1845 /* exit the for() loop */
1856 /* if no fixed_properties are specified, discard the classes
1857 * with some fixed properties other than the two we already
1860 if (g_hash_table_size (fprops) > 2)
1868 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1869 TpConnection *connection,
1870 const gchar *channel_type,
1872 GArray *fixed_properties)
1874 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1877 GList *matching_classes;
1881 g_return_val_if_fail (channel_type != NULL, NULL);
1883 cd = g_hash_table_lookup (priv->connections, connection);
1888 classes = cd->requestable_channels;
1889 if (classes == NULL)
1892 matching_classes = NULL;
1894 for (i = 0; i < classes->len; i++)
1896 class = g_ptr_array_index (classes, i);
1898 if (!channel_class_matches
1899 (class, channel_type, handle_type, fixed_properties))
1902 matching_classes = g_list_prepend (matching_classes, class);
1905 return matching_classes;
1909 find_channel_class_idle_cb (gpointer user_data)
1913 FindChannelRequest *request = user_data;
1915 gboolean is_ready = TRUE;
1916 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1918 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1920 cd = g_hash_table_lookup (priv->connections, request->connection);
1924 else if (cd->requestable_channels == NULL)
1929 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1930 request->connection, request->channel_type, request->handle_type,
1931 request->properties);
1933 request->callback (retval, request->user_data);
1934 free_find_channel_request (request);
1935 g_list_free (retval);
1940 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1941 request->connection);
1942 requests = g_list_prepend (requests, request);
1944 g_hash_table_insert (priv->outstanding_classes_requests,
1945 request->connection, requests);
1951 setup_varargs (va_list var_args,
1952 const char *channel_namespace,
1953 const char *first_property_name)
1959 if (first_property_name == NULL)
1962 name = first_property_name;
1963 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1965 while (name != NULL)
1967 name_full = g_strdup (name);
1968 properties = g_array_append_val (properties, name_full);
1969 name = va_arg (var_args, char *);
1976 * empathy_dispatcher_find_requestable_channel_classes:
1977 * @dispatcher: an #EmpathyDispatcher
1978 * @connection: a #TpConnection
1979 * @channel_type: a string identifying the type of the channel to lookup
1980 * @handle_type: the handle type for the channel, or %TP_UNKNOWN_HANDLE_TYPE
1981 * if you don't care about the channel's target handle type
1982 * @first_property_name: %NULL, or the name of the first fixed property,
1983 * followed optionally by more names, followed by %NULL.
1985 * Returns all the channel classes that a client can request for the connection
1986 * @connection, of the type identified by @channel_type, @handle_type and the
1987 * fixed properties list.
1988 * The classes which are compatible with a fixed properties list (i.e. those
1989 * that will be returned by this function) are intended as those that do not
1990 * contain any fixed property other than those in the list; note that this
1991 * doesn't guarantee that all the classes compatible with the list will contain
1992 * all the requested fixed properties, so the clients will have to filter
1993 * the returned list themselves.
1994 * If @first_property_name is %NULL, only the classes with no other fixed
1995 * properties than ChannelType and TargetHandleType will be returned.
1996 * Note that this function may return %NULL without performing any lookup if
1997 * @connection is not ready. To ensure that @connection is always ready,
1998 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
2000 * Return value: a #GList of #GValueArray objects, where the first element in
2001 * the array is a #GHashTable of the fixed properties, and the second is
2002 * a #GStrv of the allowed properties for the class. The list should be free'd
2003 * with g_list_free() when done, but the objects inside the list are owned
2004 * by the #EmpathyDispatcher and must not be modified.
2007 empathy_dispatcher_find_requestable_channel_classes
2008 (EmpathyDispatcher *self,
2009 TpConnection *connection,
2010 const gchar *channel_type,
2012 const char *first_property_name,
2017 EmpathyDispatcherPriv *priv;
2022 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
2023 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
2024 g_return_val_if_fail (channel_type != NULL, NULL);
2026 priv = GET_PRIV (self);
2028 va_start (var_args, first_property_name);
2030 properties = setup_varargs (var_args, channel_type, first_property_name);
2034 retval = empathy_dispatcher_find_channel_classes (self, connection,
2035 channel_type, handle_type, properties);
2037 if (properties != NULL)
2039 /* free the properties array */
2040 for (idx = 0; idx < properties->len ; idx++)
2042 str = g_array_index (properties, char *, idx);
2046 g_array_free (properties, TRUE);
2053 * empathy_dispatcher_find_requestable_channel_classes_async:
2054 * @dispatcher: an #EmpathyDispatcher
2055 * @connection: a #TpConnection
2056 * @channel_type: a string identifying the type of the channel to lookup
2057 * @handle_type: the handle type for the channel
2058 * @callback: the callback to call when @connection is ready
2059 * @user_data: the user data to pass to @callback
2060 * @first_property_name: %NULL, or the name of the first fixed property,
2061 * followed optionally by more names, followed by %NULL.
2063 * Please see the documentation of
2064 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2065 * description of this function.
2068 empathy_dispatcher_find_requestable_channel_classes_async
2069 (EmpathyDispatcher *self,
2070 TpConnection *connection,
2071 const gchar *channel_type,
2073 EmpathyDispatcherFindChannelClassCb callback,
2075 const char *first_property_name,
2080 FindChannelRequest *request;
2081 EmpathyDispatcherPriv *priv;
2084 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2085 g_return_if_fail (TP_IS_CONNECTION (connection));
2086 g_return_if_fail (channel_type != NULL);
2087 g_return_if_fail (handle_type != 0);
2089 priv = GET_PRIV (self);
2091 va_start (var_args, first_property_name);
2093 properties = setup_varargs (var_args, channel_type, first_property_name);
2097 /* append another request for this connection */
2098 request = g_slice_new0 (FindChannelRequest);
2099 request->dispatcher = g_object_ref (self);
2100 request->channel_type = g_strdup (channel_type);
2101 request->handle_type = handle_type;
2102 request->connection = connection;
2103 request->callback = callback;
2104 request->user_data = user_data;
2105 request->properties = properties;
2107 source_id = g_idle_add (find_channel_class_idle_cb, request);
2109 g_hash_table_insert (priv->request_channel_class_async_ids,
2110 request, GUINT_TO_POINTER (source_id));
2114 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2117 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2118 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2120 return priv->channels;
2124 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2125 const gchar *account_path,
2126 const gchar *connection_path,
2127 const GPtrArray *channels,
2128 const GPtrArray *requests_satisfied,
2130 GHashTable *handler_info,
2134 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2135 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2138 TpConnection *connection;
2140 /* FIXME: should probably find out whether the account manager is prepared
2141 * before ensuring. See bug #600111. */
2142 account = tp_account_manager_ensure_account (priv->account_manager,
2144 g_assert (account != NULL);
2146 connection = tp_account_ensure_connection (account, connection_path);
2147 if (connection == NULL)
2149 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2150 "Invalid connection argument");
2154 for (i = 0; i < channels->len ; i++)
2156 GValueArray *arr = g_ptr_array_index (channels, i);
2157 const gchar *object_path;
2158 GHashTable *properties;
2160 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2161 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2163 dispatcher_connection_new_channel_with_properties (self,
2164 connection, object_path, properties, requests_satisfied);
2172 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2177 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2178 EmpathyHandler *handler;
2180 handler = empathy_handler_new (name, filters, capabilities);
2181 priv->handlers = g_list_prepend (priv->handlers, handler);
2183 /* Only set the handle_channels function, the Channel property on the main
2184 * handler will always report all dispatched channels even if they came from
2185 * a different Handler */
2186 empathy_handler_set_handle_channels_func (handler,
2187 empathy_dispatcher_handle_channels, self);
2193 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2194 EmpathyHandler *handler)
2196 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2199 h = g_list_find (priv->handlers, handler);
2200 g_return_if_fail (h != NULL);
2202 priv->handlers = g_list_delete_link (priv->handlers, h);
2204 g_object_unref (handler);