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/util.h>
35 #include <telepathy-glib/dbus.h>
36 #include <telepathy-glib/proxy-subclass.h>
37 #include <telepathy-glib/gtypes.h>
38 #include <telepathy-glib/defs.h>
39 #include <telepathy-glib/svc-client.h>
40 #include <telepathy-glib/svc-generic.h>
41 #include <telepathy-glib/interfaces.h>
43 #include <extensions/extensions.h>
45 #include "empathy-dispatcher.h"
46 #include "empathy-handler.h"
47 #include "empathy-utils.h"
48 #include "empathy-tp-contact-factory.h"
49 #include "empathy-chatroom-manager.h"
50 #include "empathy-utils.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
53 #include <libempathy/empathy-debug.h>
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
58 gboolean dispose_has_run;
60 TpAccountManager *account_manager;
61 /* connection to connection data mapping */
62 GHashTable *connections;
63 GHashTable *outstanding_classes_requests;
66 /* channels which the dispatcher is listening "invalidated" */
71 EmpathyHandler *handler;
76 GHashTable *request_channel_class_async_ids;
77 /* (TpAccount *) => gulong
78 * Signal handler ID of the "status-changed" signal */
79 GHashTable *status_changed_handlers;
80 } EmpathyDispatcherPriv;
83 empathy_dispatcher_get_channels (EmpathyHandler *handler,
87 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
88 const gchar *account_path,
89 const gchar *connection_path,
90 const GPtrArray *channels,
91 const GPtrArray *requests_satisfied,
93 GHashTable *handler_info,
97 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
113 static guint signals[LAST_SIGNAL];
114 static EmpathyDispatcher *dispatcher = NULL;
116 static void dispatcher_init_connection_if_needed (
117 EmpathyDispatcher *dispatcher,
118 TpConnection *connection);
120 static GList * empathy_dispatcher_find_channel_classes
121 (EmpathyDispatcher *dispatcher, TpConnection *connection,
122 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
127 EmpathyDispatcher *dispatcher;
128 EmpathyDispatchOperation *operation;
129 TpConnection *connection;
130 gboolean should_ensure;
134 EmpathyContact *contact;
135 TpProxyPendingCall *pending_call;
137 /* Properties to pass to the channel when requesting it */
139 EmpathyDispatcherRequestCb *cb;
141 gpointer *request_data;
142 } DispatcherRequestData;
147 /* Channel type specific wrapper object */
148 GObject *channel_wrapper;
153 /* ObjectPath => DispatchData.. */
154 GHashTable *dispatched_channels;
155 /* ObjectPath -> EmpathyDispatchOperations */
156 GHashTable *dispatching_channels;
158 /* ObjectPath -> EmpathyDispatchOperations
160 * This holds channels which were announced with NewChannel while we have an
161 * outstanding channel request for a channel of this type. On the Requests
162 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
163 * return before NewChannels is emitted, but there was no guarantee of the
164 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
165 * held in limbo here until we know whether they were requested.
167 GHashTable *outstanding_channels;
168 /* List of DispatcherRequestData */
169 GList *outstanding_requests;
170 /* List of requestable channel classes */
171 GPtrArray *requestable_channels;
176 EmpathyDispatcher *dispatcher;
177 TpConnection *connection;
181 EmpathyDispatcherFindChannelClassCb *callback;
183 } FindChannelRequest;
186 empathy_dispatcher_call_create_or_ensure_channel (
187 EmpathyDispatcher *dispatcher,
188 DispatcherRequestData *request_data);
190 static DispatchData *
191 new_dispatch_data (TpChannel *channel,
192 GObject *channel_wrapper)
194 DispatchData *d = g_slice_new0 (DispatchData);
195 d->channel = g_object_ref (channel);
196 if (channel_wrapper != NULL)
197 d->channel_wrapper = g_object_ref (channel_wrapper);
203 free_dispatch_data (DispatchData *data)
205 g_object_unref (data->channel);
206 if (data->channel_wrapper != NULL)
207 g_object_unref (data->channel_wrapper);
209 g_slice_free (DispatchData, data);
212 static DispatcherRequestData *
213 new_dispatcher_request_data (EmpathyDispatcher *self,
214 TpConnection *connection,
215 const gchar *channel_type,
219 EmpathyContact *contact,
220 EmpathyDispatcherRequestCb *cb,
223 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
225 result->dispatcher = g_object_ref (self);
226 result->connection = connection;
228 result->should_ensure = FALSE;
230 result->channel_type = g_strdup (channel_type);
231 result->handle_type = handle_type;
232 result->handle = handle;
233 result->request = request;
236 result->contact = g_object_ref (contact);
239 result->user_data = user_data;
245 free_dispatcher_request_data (DispatcherRequestData *r)
247 g_free (r->channel_type);
249 if (r->dispatcher != NULL)
250 g_object_unref (r->dispatcher);
252 if (r->contact != NULL)
253 g_object_unref (r->contact);
255 if (r->request != NULL)
256 g_hash_table_unref (r->request);
258 if (r->pending_call != NULL)
259 tp_proxy_pending_call_cancel (r->pending_call);
261 g_slice_free (DispatcherRequestData, r);
264 static ConnectionData *
265 new_connection_data (void)
267 ConnectionData *cd = g_slice_new0 (ConnectionData);
269 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
270 g_free, (GDestroyNotify) free_dispatch_data);
272 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
273 g_free, g_object_unref);
275 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
282 free_connection_data (ConnectionData *cd)
287 g_hash_table_destroy (cd->dispatched_channels);
288 g_hash_table_destroy (cd->dispatching_channels);
290 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
292 free_dispatcher_request_data (l->data);
295 if (cd->requestable_channels != NULL)
297 for (i = 0 ; i < cd->requestable_channels->len ; i++)
299 g_ptr_array_index (cd->requestable_channels, i));
300 g_ptr_array_free (cd->requestable_channels, TRUE);
305 free_find_channel_request (FindChannelRequest *r)
310 g_object_unref (r->dispatcher);
311 g_free (r->channel_type);
313 if (r->properties != NULL)
315 for (idx = 0; idx < r->properties->len ; idx++)
317 str = g_array_index (r->properties, char *, idx);
321 g_array_free (r->properties, TRUE);
324 g_slice_free (FindChannelRequest, r);
328 dispatcher_connection_invalidated_cb (TpConnection *connection,
332 EmpathyDispatcher *self)
334 EmpathyDispatcherPriv *priv = GET_PRIV (self);
336 DEBUG ("Error: %s", message);
337 g_hash_table_remove (priv->connections, connection);
341 dispatcher_operation_can_start (EmpathyDispatcher *self,
342 EmpathyDispatchOperation *operation,
346 const gchar *channel_type =
347 empathy_dispatch_operation_get_channel_type (operation);
349 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
351 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
353 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
363 dispatch_operation_flush_requests (EmpathyDispatcher *self,
364 EmpathyDispatchOperation *operation,
370 l = cd->outstanding_requests;
373 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
378 if (d->operation == operation)
383 d->cb (NULL, error, d->user_data);
385 d->cb (operation, NULL, d->user_data);
388 cd->outstanding_requests = g_list_delete_link
389 (cd->outstanding_requests, lt);
391 free_dispatcher_request_data (d);
397 dispatcher_channel_invalidated_cb (TpProxy *proxy,
401 EmpathyDispatcher *self)
403 /* Channel went away... */
404 EmpathyDispatcherPriv *priv = GET_PRIV (self);
405 TpConnection *connection;
406 EmpathyDispatchOperation *operation;
408 const gchar *object_path;
410 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
412 cd = g_hash_table_lookup (priv->connections, connection);
413 /* Connection itself invalidated? */
417 object_path = tp_proxy_get_object_path (proxy);
419 DEBUG ("Channel %s invalidated", object_path);
421 g_hash_table_remove (cd->dispatched_channels, object_path);
422 g_hash_table_remove (cd->dispatching_channels, object_path);
424 priv->channels = g_list_remove (priv->channels, proxy);
426 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
427 if (operation != NULL)
429 GError error = { domain, code, message };
430 dispatch_operation_flush_requests (self, operation, &error, cd);
431 g_hash_table_remove (cd->outstanding_channels, object_path);
432 g_object_unref (operation);
437 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
438 EmpathyDispatcher *self)
440 g_assert (empathy_dispatch_operation_is_incoming (operation));
441 DEBUG ("Send of for dispatching: %s",
442 empathy_dispatch_operation_get_object_path (operation));
443 g_signal_emit (self, signals[DISPATCH], 0, operation);
447 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
448 EmpathyDispatcher *self)
450 /* Our job is done, remove the dispatch operation and mark the channel as
452 EmpathyDispatcherPriv *priv = GET_PRIV (self);
453 TpConnection *connection;
455 const gchar *object_path;
457 connection = empathy_dispatch_operation_get_tp_connection (operation);
458 cd = g_hash_table_lookup (priv->connections, connection);
459 g_assert (cd != NULL);
461 object_path = empathy_dispatch_operation_get_object_path (operation);
463 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
466 d = new_dispatch_data (
467 empathy_dispatch_operation_get_channel (operation),
468 empathy_dispatch_operation_get_channel_wrapper (operation));
469 g_hash_table_insert (cd->dispatched_channels,
470 g_strdup (object_path), d);
472 g_hash_table_remove (cd->dispatching_channels, object_path);
474 DEBUG ("Channel claimed: %s", object_path);
478 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
479 EmpathyDispatcher *self)
481 EmpathyDispatcherPriv *priv = GET_PRIV (self);
482 TpConnection *connection;
484 EmpathyDispatchOperationState status;
486 g_signal_connect (operation, "approved",
487 G_CALLBACK (dispatch_operation_approved_cb), self);
489 g_signal_connect (operation, "claimed",
490 G_CALLBACK (dispatch_operation_claimed_cb), self);
492 /* Signal the observers */
493 DEBUG ("Send to observers: %s",
494 empathy_dispatch_operation_get_object_path (operation));
495 g_signal_emit (self, signals[OBSERVE], 0, operation);
497 empathy_dispatch_operation_start (operation);
499 /* Signal potential requestors */
500 connection = empathy_dispatch_operation_get_tp_connection (operation);
501 cd = g_hash_table_lookup (priv->connections, connection);
502 g_assert (cd != NULL);
504 g_object_ref (operation);
507 dispatch_operation_flush_requests (self, operation, NULL, cd);
508 status = empathy_dispatch_operation_get_status (operation);
509 g_object_unref (operation);
511 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
514 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
516 DEBUG ("Send to approvers: %s",
517 empathy_dispatch_operation_get_object_path (operation));
518 g_signal_emit (self, signals[APPROVE], 0, operation);
522 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
523 DEBUG ("Send of for dispatching: %s",
524 empathy_dispatch_operation_get_object_path (operation));
525 g_signal_emit (self, signals[DISPATCH], 0, operation);
528 g_object_unref (self);
532 dispatcher_start_dispatching (EmpathyDispatcher *self,
533 EmpathyDispatchOperation *operation,
536 const gchar *object_path =
537 empathy_dispatch_operation_get_object_path (operation);
539 DEBUG ("Dispatching process started for %s", object_path);
541 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
543 g_assert (g_hash_table_lookup (cd->outstanding_channels,
544 object_path) == NULL);
546 g_hash_table_insert (cd->dispatching_channels,
547 g_strdup (object_path), operation);
549 switch (empathy_dispatch_operation_get_status (operation))
551 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
552 g_signal_connect (operation, "ready",
553 G_CALLBACK (dispatch_operation_ready_cb), self);
555 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
556 dispatch_operation_ready_cb (operation, self);
559 g_assert_not_reached ();
563 else if (empathy_dispatch_operation_get_status (operation) >=
564 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
566 /* Already dispatching and the operation is pending, thus the observers
567 * have seen it (if applicable), so we can flush the request right away.
569 dispatch_operation_flush_requests (self, operation, NULL, cd);
574 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
580 g_hash_table_iter_init (&iter, cd->outstanding_channels);
581 while (g_hash_table_iter_next (&iter, NULL, &value))
583 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
585 if (dispatcher_operation_can_start (self, operation, cd))
587 g_hash_table_iter_remove (&iter);
588 dispatcher_start_dispatching (self, operation, cd);
594 dispatcher_connection_new_channel (EmpathyDispatcher *self,
595 TpConnection *connection,
596 const gchar *object_path,
597 const gchar *channel_type,
600 GHashTable *properties,
603 EmpathyDispatcherPriv *priv = GET_PRIV (self);
606 EmpathyDispatchOperation *operation;
608 /* Channel types we never want to dispatch because they're either deprecated
609 * or can't sensibly be dispatch (e.g. channels that should always be
611 const char *blacklist[] = {
612 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
613 TP_IFACE_CHANNEL_TYPE_TUBES,
614 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
618 dispatcher_init_connection_if_needed (self, connection);
620 cd = g_hash_table_lookup (priv->connections, connection);
622 /* Don't bother with channels we have already dispatched or are dispatching
623 * currently. This can happen when NewChannel(s) is fired after
624 * RequestChannel/CreateChannel/EnsureChannel */
625 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
628 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
631 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
633 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
636 /* Only pick up non-requested text and file channels. For all other it
637 * doesn't make sense to handle it if we didn't request it. The same goes
638 * for channels we discovered by the Channels property or ListChannels */
639 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
640 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
642 DEBUG ("Ignoring incoming channel of type %s on %s",
643 channel_type, object_path);
647 for (i = 0 ; blacklist[i] != NULL; i++)
649 if (!tp_strdiff (channel_type, blacklist[i]))
651 DEBUG ("Ignoring blacklisted channel type %s on %s",
652 channel_type, object_path);
657 DEBUG ("New channel of type %s on %s", channel_type, object_path);
659 if (properties == NULL)
660 channel = tp_channel_new (connection, object_path, channel_type,
661 handle_type, handle, NULL);
663 channel = tp_channel_new_from_properties (connection, object_path,
666 g_signal_connect (channel, "invalidated",
667 G_CALLBACK (dispatcher_channel_invalidated_cb),
670 priv->channels = g_list_prepend (priv->channels, channel);
672 operation = empathy_dispatch_operation_new (connection, channel, NULL,
675 g_object_unref (channel);
679 /* Request could either be by us or by a remote party. If there are no
680 * outstanding requests for this channel type we can assume it's remote.
681 * Otherwise we wait untill they are all satisfied */
682 if (dispatcher_operation_can_start (self, operation, cd))
683 dispatcher_start_dispatching (self, operation, cd);
685 g_hash_table_insert (cd->outstanding_channels,
686 g_strdup (object_path), operation);
690 dispatcher_start_dispatching (self, operation, cd);
695 dispatcher_connection_new_channel_with_properties (
696 EmpathyDispatcher *self,
697 TpConnection *connection,
698 const gchar *object_path,
699 GHashTable *properties)
701 const gchar *channel_type;
708 channel_type = tp_asv_get_string (properties,
709 TP_IFACE_CHANNEL ".ChannelType");
710 if (channel_type == NULL)
712 g_message ("%s had an invalid ChannelType property", object_path);
716 handle_type = tp_asv_get_uint32 (properties,
717 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
720 g_message ("%s had an invalid TargetHandleType property", object_path);
724 handle = tp_asv_get_uint32 (properties,
725 TP_IFACE_CHANNEL ".TargetHandle", &valid);
728 g_message ("%s had an invalid TargetHandle property", object_path);
732 /* We assume there is no channel dispather, so we're the only one dispatching
733 * it. Which means that a requested channel it is outgoing one */
734 requested = tp_asv_get_boolean (properties,
735 TP_IFACE_CHANNEL ".Requested", &valid);
738 g_message ("%s had an invalid Requested property", object_path);
742 dispatcher_connection_new_channel (self, connection,
743 object_path, channel_type, handle_type, handle, properties, !requested);
747 dispatcher_connection_got_all (TpProxy *proxy,
748 GHashTable *properties,
753 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
754 EmpathyDispatcherPriv *priv = GET_PRIV (self);
755 GPtrArray *requestable_channels;
756 GPtrArray *existing_channels;
759 DEBUG ("Error: %s", error->message);
763 requestable_channels = tp_asv_get_boxed (properties,
764 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
766 if (requestable_channels == NULL)
767 DEBUG ("No RequestableChannelClasses property !?! on connection");
772 FindChannelRequest *request;
775 cd = g_hash_table_lookup (priv->connections, proxy);
776 g_assert (cd != NULL);
778 cd->requestable_channels = g_boxed_copy (
779 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
781 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
784 for (l = requests; l != NULL; l = l->next)
788 retval = empathy_dispatcher_find_channel_classes (self,
789 TP_CONNECTION (proxy), request->channel_type,
790 request->handle_type, request->properties);
791 request->callback (retval, request->user_data);
793 free_find_channel_request (request);
794 g_list_free (retval);
797 g_list_free (requests);
799 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
802 existing_channels = tp_asv_get_boxed (properties,
803 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
805 if (existing_channels != NULL)
809 for (idx = 0; idx < existing_channels->len; idx++)
811 GValueArray *values = g_ptr_array_index (existing_channels, idx);
812 const gchar *object_path;
815 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
816 props = g_value_get_boxed (g_value_array_get_nth (values, 1));
818 if (tp_strdiff (tp_asv_get_string (props,
819 TP_IFACE_CHANNEL ".ChannelType"),
820 TP_IFACE_CHANNEL_TYPE_TEXT))
823 dispatcher_connection_new_channel_with_properties (self,
824 TP_CONNECTION (proxy), object_path, props);
830 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
831 const GPtrArray *capabilities,
837 DEBUG ("Error: %s", error->message);
841 connection_ready_cb (TpConnection *connection,
845 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
846 GPtrArray *capabilities;
849 const gchar *remove_ = NULL;
853 DEBUG ("Error: %s", error->message);
857 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
858 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
860 tp_cli_dbus_properties_call_get_all (connection, -1,
861 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
862 dispatcher_connection_got_all,
863 NULL, NULL, G_OBJECT (self));
866 /* Advertise VoIP capabilities */
867 capabilities = g_ptr_array_sized_new (1);
868 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
869 G_TYPE_UINT, G_TYPE_INVALID);
870 g_value_init (&cap, cap_type);
871 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
872 dbus_g_type_struct_set (&cap,
873 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
874 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
875 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
876 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
877 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
878 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
880 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
882 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
883 connection, -1, capabilities, &remove_,
884 dispatcher_connection_advertise_capabilities_cb,
885 NULL, NULL, G_OBJECT (self));
887 g_value_unset (&cap);
888 g_ptr_array_free (capabilities, TRUE);
890 g_object_unref (self);
894 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
895 TpConnection *connection)
897 EmpathyDispatcherPriv *priv = GET_PRIV (self);
899 if (g_hash_table_lookup (priv->connections, connection) != NULL)
902 g_hash_table_insert (priv->connections, g_object_ref (connection),
903 new_connection_data ());
905 g_signal_connect (connection, "invalidated",
906 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
908 /* Ensure to keep the self object alive while the call_when_ready is
911 tp_connection_call_when_ready (connection, connection_ready_cb, self);
915 dispatcher_status_changed_cb (TpAccount *account,
919 gchar *dbus_error_name,
921 EmpathyDispatcher *self)
923 TpConnection *conn = tp_account_get_connection (account);
926 dispatcher_init_connection_if_needed (self, conn);
930 remove_idle_handlers (gpointer key,
936 source_id = GPOINTER_TO_UINT (value);
937 g_source_remove (source_id);
941 dispatcher_constructor (GType type,
942 guint n_construct_params,
943 GObjectConstructParam *construct_params)
946 EmpathyDispatcherPriv *priv;
948 if (dispatcher != NULL)
949 return g_object_ref (dispatcher);
951 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
952 (type, n_construct_params, construct_params);
954 dispatcher = EMPATHY_DISPATCHER (retval);
955 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
957 priv = GET_PRIV (dispatcher);
959 if (priv->handler == NULL)
960 priv->handler = empathy_handler_new (NULL, NULL, NULL);
962 empathy_handler_set_handle_channels_func (priv->handler,
963 empathy_dispatcher_handle_channels,
966 empathy_handler_set_channels_func (priv->handler,
967 empathy_dispatcher_get_channels,
974 dispatcher_dispose (GObject *object)
976 EmpathyDispatcherPriv *priv = GET_PRIV (object);
981 if (priv->dispose_has_run)
984 priv->dispose_has_run = TRUE;
986 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
987 g_object_unref (G_OBJECT (l->data));
989 g_list_free (priv->handlers);
990 priv->handlers = NULL;
992 if (priv->handler != NULL)
993 g_object_unref (priv->handler);
994 priv->handler = NULL;
996 g_hash_table_iter_init (&iter, priv->connections);
997 while (g_hash_table_iter_next (&iter, &connection, NULL))
999 g_signal_handlers_disconnect_by_func (connection,
1000 dispatcher_connection_invalidated_cb, object);
1003 g_hash_table_destroy (priv->connections);
1004 priv->connections = NULL;
1006 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
1010 dispatcher_finalize (GObject *object)
1012 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1014 GHashTableIter iter;
1015 gpointer connection;
1017 gpointer account, id;
1019 if (priv->request_channel_class_async_ids != NULL)
1021 g_hash_table_foreach (priv->request_channel_class_async_ids,
1022 remove_idle_handlers, NULL);
1023 g_hash_table_destroy (priv->request_channel_class_async_ids);
1026 for (l = priv->channels; l; l = l->next)
1028 g_signal_handlers_disconnect_by_func (l->data,
1029 dispatcher_channel_invalidated_cb, object);
1032 g_list_free (priv->channels);
1034 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1035 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1037 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1041 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1042 while (g_hash_table_iter_next (&iter, &account, &id))
1044 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1046 g_hash_table_destroy (priv->status_changed_handlers);
1048 g_object_unref (priv->account_manager);
1050 g_hash_table_destroy (priv->outstanding_classes_requests);
1054 dispatcher_set_property (GObject *object,
1056 const GValue *value,
1059 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1060 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1062 switch (property_id)
1065 priv->handler = g_value_dup_object (value);
1068 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1074 dispatcher_get_property (GObject *object,
1079 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1080 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1082 switch (property_id)
1085 g_value_set_object (value, priv->handler);
1088 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1094 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1096 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1097 GParamSpec *param_spec;
1099 object_class->dispose = dispatcher_dispose;
1100 object_class->finalize = dispatcher_finalize;
1101 object_class->constructor = dispatcher_constructor;
1103 object_class->get_property = dispatcher_get_property;
1104 object_class->set_property = dispatcher_set_property;
1106 param_spec = g_param_spec_object ("handler", "handler",
1107 "The main Telepathy Client Hander object",
1108 EMPATHY_TYPE_HANDLER,
1109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1110 g_object_class_install_property (object_class,
1111 PROP_HANDLER, param_spec);
1114 g_signal_new ("observe",
1115 G_TYPE_FROM_CLASS (klass),
1119 g_cclosure_marshal_VOID__OBJECT,
1121 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1124 g_signal_new ("approve",
1125 G_TYPE_FROM_CLASS (klass),
1129 g_cclosure_marshal_VOID__OBJECT,
1131 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1134 g_signal_new ("dispatch",
1135 G_TYPE_FROM_CLASS (klass),
1139 g_cclosure_marshal_VOID__OBJECT,
1141 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1144 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1148 connect_account (EmpathyDispatcher *self,
1151 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1152 TpConnection *conn = tp_account_get_connection (account);
1155 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1162 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1164 id = g_signal_connect (account, "status-changed",
1165 G_CALLBACK (dispatcher_status_changed_cb), self);
1167 g_hash_table_insert (priv->status_changed_handlers, account,
1168 GUINT_TO_POINTER (id));
1172 account_manager_prepared_cb (GObject *source_object,
1173 GAsyncResult *result,
1176 GList *accounts, *l;
1177 EmpathyDispatcher *self = user_data;
1178 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1179 GError *error = NULL;
1181 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1183 DEBUG ("Failed to prepare account manager: %s", error->message);
1184 g_error_free (error);
1188 accounts = tp_account_manager_get_valid_accounts (account_manager);
1189 for (l = accounts; l; l = l->next)
1191 TpAccount *a = l->data;
1193 connect_account (self, a);
1195 g_list_free (accounts);
1199 account_prepare_cb (GObject *source_object,
1200 GAsyncResult *result,
1203 EmpathyDispatcher *self = user_data;
1204 TpAccount *account = TP_ACCOUNT (source_object);
1205 GError *error = NULL;
1207 if (!tp_account_prepare_finish (account, result, &error))
1209 DEBUG ("Failed to prepare account: %s", error->message);
1210 g_error_free (error);
1214 connect_account (self, account);
1218 account_validity_changed_cb (TpAccountManager *manager,
1226 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1230 empathy_dispatcher_init (EmpathyDispatcher *self)
1232 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1233 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1236 priv->account_manager = tp_account_manager_dup ();
1238 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1239 g_object_unref, (GDestroyNotify) free_connection_data);
1241 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1242 g_direct_equal, g_object_unref, NULL);
1244 priv->channels = NULL;
1246 tp_account_manager_prepare_async (priv->account_manager, NULL,
1247 account_manager_prepared_cb, self);
1249 empathy_signal_connect_weak (priv->account_manager,
1250 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1253 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1255 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1260 empathy_dispatcher_new (const gchar *name,
1264 EmpathyHandler *handler;
1265 EmpathyDispatcher *ret;
1267 g_assert (dispatcher == NULL);
1268 handler = empathy_handler_new (name, filters, capabilities);
1270 ret = EMPATHY_DISPATCHER (
1271 g_object_new (EMPATHY_TYPE_DISPATCHER,
1274 g_object_unref (handler);
1280 empathy_dispatcher_dup_singleton (void)
1282 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1286 dispatcher_request_failed (EmpathyDispatcher *self,
1287 DispatcherRequestData *request_data,
1288 const GError *error)
1290 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1291 ConnectionData *conn_data;
1293 conn_data = g_hash_table_lookup (priv->connections,
1294 request_data->connection);
1295 if (request_data->cb != NULL)
1296 request_data->cb (NULL, error, request_data->user_data);
1298 if (conn_data != NULL)
1300 conn_data->outstanding_requests =
1301 g_list_remove (conn_data->outstanding_requests, request_data);
1303 /* else Connection has been invalidated */
1305 free_dispatcher_request_data (request_data);
1309 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1310 DispatcherRequestData *request_data,
1311 const gchar *object_path,
1312 GHashTable *properties,
1313 const GError *error)
1315 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1316 EmpathyDispatchOperation *operation = NULL;
1317 ConnectionData *conn_data;
1319 /* The DispatcherRequestData owns a ref on the self object. As the request
1320 * data could be destroyed (when calling dispatcher_request_failed for
1321 * example) we keep a ref on self to be sure it stays alive while we are
1322 * executing this function. */
1323 g_object_ref (self);
1325 conn_data = g_hash_table_lookup (priv->connections,
1326 request_data->connection);
1330 DEBUG ("Channel request failed: %s", error->message);
1332 dispatcher_request_failed (self, request_data, error);
1337 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1340 if (operation != NULL)
1341 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1343 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1346 if (operation == NULL)
1348 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1353 operation = empathy_dispatch_operation_new_with_wrapper (
1354 request_data->connection,
1355 data->channel, request_data->contact, FALSE,
1356 data->channel_wrapper);
1362 if (properties != NULL)
1363 channel = tp_channel_new_from_properties (request_data->connection,
1364 object_path, properties, NULL);
1366 channel = tp_channel_new (request_data->connection, object_path,
1367 request_data->channel_type, request_data->handle_type,
1368 request_data->handle, NULL);
1370 g_signal_connect (channel, "invalidated",
1371 G_CALLBACK (dispatcher_channel_invalidated_cb),
1372 request_data->dispatcher);
1374 priv->channels = g_list_prepend (priv->channels, channel);
1376 operation = empathy_dispatch_operation_new (request_data->connection,
1377 channel, request_data->contact, FALSE);
1378 g_object_unref (channel);
1383 /* Already existed set potential extra information */
1384 g_object_set (G_OBJECT (operation),
1385 "contact", request_data->contact,
1389 request_data->operation = operation;
1391 /* (pre)-approve this right away as we requested it
1392 * This might cause the channel to be claimed, in which case the operation
1393 * will disappear. So ref it, and check the status before starting the
1396 g_object_ref (operation);
1397 empathy_dispatch_operation_approve (operation);
1399 if (empathy_dispatch_operation_get_status (operation) <
1400 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1401 dispatcher_start_dispatching (request_data->dispatcher, operation,
1404 g_object_unref (operation);
1407 dispatcher_flush_outstanding_operations (self, conn_data);
1408 g_object_unref (self);
1412 dispatcher_request_channel_cb (TpConnection *connection,
1413 const gchar *object_path,
1414 const GError *error,
1416 GObject *weak_object)
1418 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1419 EmpathyDispatcher *self =
1420 EMPATHY_DISPATCHER (request_data->dispatcher);
1422 request_data->pending_call = NULL;
1424 dispatcher_connection_new_requested_channel (self,
1425 request_data, object_path, NULL, error);
1429 dispatcher_request_channel (DispatcherRequestData *request_data)
1431 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1432 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1434 /* Extend the request_data to be a valid request */
1435 g_assert (request_data->request == NULL);
1436 request_data->request = tp_asv_new (
1437 TP_IFACE_CHANNEL ".ChannelType",
1438 G_TYPE_STRING, request_data->channel_type,
1439 TP_IFACE_CHANNEL ".TargetHandleType",
1440 G_TYPE_UINT, request_data->handle_type,
1443 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1444 tp_asv_set_uint32 (request_data->request,
1445 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1447 empathy_dispatcher_call_create_or_ensure_channel (
1448 request_data->dispatcher, request_data);
1452 request_data->pending_call = tp_cli_connection_call_request_channel (
1453 request_data->connection, -1,
1454 request_data->channel_type,
1455 request_data->handle_type,
1456 request_data->handle,
1457 TRUE, dispatcher_request_channel_cb,
1458 request_data, NULL, NULL);
1463 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1464 EmpathyDispatcherRequestCb *callback,
1467 EmpathyDispatcher *self;
1468 EmpathyDispatcherPriv *priv;
1469 TpConnection *connection;
1470 ConnectionData *connection_data;
1471 DispatcherRequestData *request_data;
1473 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1475 self = empathy_dispatcher_dup_singleton ();
1476 priv = GET_PRIV (self);
1478 connection = empathy_contact_get_connection (contact);
1479 connection_data = g_hash_table_lookup (priv->connections, connection);
1481 /* The contact handle might not be known yet */
1482 request_data = new_dispatcher_request_data (self, connection,
1483 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1484 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1485 request_data->should_ensure = TRUE;
1487 connection_data->outstanding_requests = g_list_prepend
1488 (connection_data->outstanding_requests, request_data);
1490 dispatcher_request_channel (request_data);
1492 g_object_unref (self);
1497 EmpathyDispatcher *dispatcher;
1498 EmpathyDispatcherRequestCb *callback;
1500 } ChatWithContactIdData;
1503 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1504 EmpathyContact *contact,
1505 const GError *error,
1507 GObject *weak_object)
1509 ChatWithContactIdData *data = user_data;
1513 /* FIXME: Should call data->callback with the error */
1514 DEBUG ("Error: %s", error->message);
1518 empathy_dispatcher_chat_with_contact (contact, data->callback,
1522 g_object_unref (data->dispatcher);
1523 g_slice_free (ChatWithContactIdData, data);
1527 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1528 const gchar *contact_id,
1529 EmpathyDispatcherRequestCb *callback,
1532 EmpathyDispatcher *self;
1533 EmpathyTpContactFactory *factory;
1534 ChatWithContactIdData *data;
1536 g_return_if_fail (TP_IS_CONNECTION (connection));
1537 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1539 self = empathy_dispatcher_dup_singleton ();
1540 factory = empathy_tp_contact_factory_dup_singleton (connection);
1541 data = g_slice_new0 (ChatWithContactIdData);
1542 data->dispatcher = self;
1543 data->callback = callback;
1544 data->user_data = user_data;
1545 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1546 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1548 g_object_unref (factory);
1552 dispatcher_request_handles_cb (TpConnection *connection,
1553 const GArray *handles,
1554 const GError *error,
1558 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1560 request_data->pending_call = NULL;
1564 EmpathyDispatcher *self = request_data->dispatcher;
1565 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1568 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1570 if (request_data->cb)
1571 request_data->cb (NULL, error, request_data->user_data);
1573 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1576 free_dispatcher_request_data (request_data);
1578 dispatcher_flush_outstanding_operations (self, cd);
1582 request_data->handle = g_array_index (handles, guint, 0);
1583 dispatcher_request_channel (request_data);
1587 empathy_dispatcher_join_muc (TpConnection *connection,
1588 const gchar *roomname,
1589 EmpathyDispatcherRequestCb *callback,
1592 EmpathyDispatcher *self;
1593 EmpathyDispatcherPriv *priv;
1594 DispatcherRequestData *request_data;
1595 ConnectionData *connection_data;
1596 const gchar *names[] = { roomname, NULL };
1598 g_return_if_fail (TP_IS_CONNECTION (connection));
1599 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1601 self = empathy_dispatcher_dup_singleton ();
1602 priv = GET_PRIV (self);
1604 connection_data = g_hash_table_lookup (priv->connections, connection);
1605 g_assert (connection_data != NULL);
1607 /* Don't know the room handle yet */
1608 request_data = new_dispatcher_request_data (self, connection,
1609 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1610 NULL, callback, user_data);
1611 request_data->should_ensure = TRUE;
1613 connection_data->outstanding_requests = g_list_prepend
1614 (connection_data->outstanding_requests, request_data);
1616 request_data->pending_call = tp_cli_connection_call_request_handles (
1618 TP_HANDLE_TYPE_ROOM, names,
1619 dispatcher_request_handles_cb, request_data, NULL, NULL);
1621 g_object_unref (self);
1625 dispatcher_create_channel_cb (TpConnection *connect,
1626 const gchar *object_path,
1627 GHashTable *properties,
1628 const GError *error,
1630 GObject *weak_object)
1632 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1633 EmpathyDispatcher *self =
1634 EMPATHY_DISPATCHER (request_data->dispatcher);
1636 request_data->pending_call = NULL;
1638 dispatcher_connection_new_requested_channel (self,
1639 request_data, object_path, properties, error);
1643 dispatcher_ensure_channel_cb (TpConnection *connect,
1645 const gchar *object_path,
1646 GHashTable *properties,
1647 const GError *error,
1649 GObject *weak_object)
1651 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1652 EmpathyDispatcher *self =
1653 EMPATHY_DISPATCHER (request_data->dispatcher);
1655 request_data->pending_call = NULL;
1657 dispatcher_connection_new_requested_channel (self,
1658 request_data, object_path, properties, error);
1662 empathy_dispatcher_call_create_or_ensure_channel (
1663 EmpathyDispatcher *self,
1664 DispatcherRequestData *request_data)
1666 if (request_data->should_ensure)
1668 request_data->pending_call =
1669 tp_cli_connection_interface_requests_call_ensure_channel (
1670 request_data->connection, -1,
1671 request_data->request, dispatcher_ensure_channel_cb,
1672 request_data, NULL, NULL);
1676 request_data->pending_call =
1677 tp_cli_connection_interface_requests_call_create_channel (
1678 request_data->connection, -1,
1679 request_data->request, dispatcher_create_channel_cb,
1680 request_data, NULL, NULL);
1685 * empathy_dispatcher_create_channel:
1686 * @self: the EmpathyDispatcher
1687 * @connection: the Connection to dispatch on
1688 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1689 * @callback: a callback for when the channel arrives (or NULL)
1690 * @user_data: optional user data (or NULL)
1692 * When calling this function, #EmpathyDispatcher takes ownership of your
1693 * reference to @request. DO NOT unref or destroy @request. When the request is
1694 * done, @request will be unreferenced. Take another reference if you want to
1698 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1699 TpConnection *connection,
1700 GHashTable *request,
1701 EmpathyDispatcherRequestCb *callback,
1704 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1705 ConnectionData *connection_data;
1706 DispatcherRequestData *request_data;
1707 const gchar *channel_type;
1712 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1713 g_return_if_fail (TP_IS_CONNECTION (connection));
1714 g_return_if_fail (request != NULL);
1716 connection_data = g_hash_table_lookup (priv->connections, connection);
1717 g_assert (connection_data != NULL);
1719 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1721 handle_type = tp_asv_get_uint32 (request,
1722 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1724 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1726 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1728 request_data = new_dispatcher_request_data (self, connection,
1729 channel_type, handle_type, handle, request,
1730 NULL, callback, user_data);
1732 connection_data->outstanding_requests = g_list_prepend
1733 (connection_data->outstanding_requests, request_data);
1735 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1739 channel_class_matches (GValueArray *class,
1740 const char *channel_type,
1742 GArray *fixed_properties)
1750 v = g_value_array_get_nth (class, 0);
1752 /* if the class doesn't match channel type discard it. */
1753 fprops = g_value_get_boxed (v);
1754 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1756 if (tp_strdiff (channel_type, c_type))
1759 /* we have the right channel type, see if the handle type matches */
1760 h_type = tp_asv_get_uint32 (fprops,
1761 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1763 if (!valid || handle_type != h_type)
1766 if (fixed_properties != NULL)
1768 gpointer h_key, h_val;
1770 GHashTableIter iter;
1773 g_hash_table_iter_init (&iter, fprops);
1775 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1777 /* discard ChannelType and TargetHandleType, as we already
1780 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1782 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1787 for (idx = 0; idx < fixed_properties->len; idx++)
1789 /* if |key| doesn't exist in |fixed_properties|, discard
1794 g_array_index (fixed_properties, char *, idx)))
1797 /* exit the for() loop */
1808 /* if no fixed_properties are specified, discard the classes
1809 * with some fixed properties other than the two we already
1812 if (g_hash_table_size (fprops) > 2)
1820 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1821 TpConnection *connection,
1822 const gchar *channel_type,
1824 GArray *fixed_properties)
1826 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1829 GList *matching_classes;
1833 g_return_val_if_fail (channel_type != NULL, NULL);
1834 g_return_val_if_fail (handle_type != 0, NULL);
1836 cd = g_hash_table_lookup (priv->connections, connection);
1841 classes = cd->requestable_channels;
1842 if (classes == NULL)
1845 matching_classes = NULL;
1847 for (i = 0; i < classes->len; i++)
1849 class = g_ptr_array_index (classes, i);
1851 if (!channel_class_matches
1852 (class, channel_type, handle_type, fixed_properties))
1855 matching_classes = g_list_prepend (matching_classes, class);
1858 return matching_classes;
1862 find_channel_class_idle_cb (gpointer user_data)
1866 FindChannelRequest *request = user_data;
1868 gboolean is_ready = TRUE;
1869 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1871 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1873 cd = g_hash_table_lookup (priv->connections, request->connection);
1877 else if (cd->requestable_channels == NULL)
1882 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1883 request->connection, request->channel_type, request->handle_type,
1884 request->properties);
1886 request->callback (retval, request->user_data);
1887 free_find_channel_request (request);
1888 g_list_free (retval);
1893 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1894 request->connection);
1895 requests = g_list_prepend (requests, request);
1897 g_hash_table_insert (priv->outstanding_classes_requests,
1898 request->connection, requests);
1904 setup_varargs (va_list var_args,
1905 const char *channel_namespace,
1906 const char *first_property_name)
1912 if (first_property_name == NULL)
1915 name = first_property_name;
1916 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1918 while (name != NULL)
1920 name_full = g_strdup (name);
1921 properties = g_array_append_val (properties, name_full);
1922 name = va_arg (var_args, char *);
1929 * empathy_dispatcher_find_requestable_channel_classes:
1930 * @dispatcher: an #EmpathyDispatcher
1931 * @connection: a #TpConnection
1932 * @channel_type: a string identifying the type of the channel to lookup
1933 * @handle_type: the handle type for the channel
1934 * @first_property_name: %NULL, or the name of the first fixed property,
1935 * followed optionally by more names, followed by %NULL.
1937 * Returns all the channel classes that a client can request for the connection
1938 * @connection, of the type identified by @channel_type, @handle_type and the
1939 * fixed properties list.
1940 * The classes which are compatible with a fixed properties list (i.e. those
1941 * that will be returned by this function) are intended as those that do not
1942 * contain any fixed property other than those in the list; note that this
1943 * doesn't guarantee that all the classes compatible with the list will contain
1944 * all the requested fixed properties, so the clients will have to filter
1945 * the returned list themselves.
1946 * If @first_property_name is %NULL, only the classes with no other fixed
1947 * properties than ChannelType and TargetHandleType will be returned.
1948 * Note that this function may return %NULL without performing any lookup if
1949 * @connection is not ready. To ensure that @connection is always ready,
1950 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1952 * Return value: a #GList of #GValueArray objects, where the first element in
1953 * the array is a #GHashTable of the fixed properties, and the second is
1954 * a #GStrv of the allowed properties for the class. The list should be free'd
1955 * with g_list_free() when done, but the objects inside the list are owned
1956 * by the #EmpathyDispatcher and must not be modified.
1959 empathy_dispatcher_find_requestable_channel_classes
1960 (EmpathyDispatcher *self,
1961 TpConnection *connection,
1962 const gchar *channel_type,
1964 const char *first_property_name,
1969 EmpathyDispatcherPriv *priv;
1974 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1975 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1976 g_return_val_if_fail (channel_type != NULL, NULL);
1977 g_return_val_if_fail (handle_type != 0, NULL);
1979 priv = GET_PRIV (self);
1981 va_start (var_args, first_property_name);
1983 properties = setup_varargs (var_args, channel_type, first_property_name);
1987 retval = empathy_dispatcher_find_channel_classes (self, connection,
1988 channel_type, handle_type, properties);
1990 if (properties != NULL)
1992 /* free the properties array */
1993 for (idx = 0; idx < properties->len ; idx++)
1995 str = g_array_index (properties, char *, idx);
1999 g_array_free (properties, TRUE);
2006 * empathy_dispatcher_find_requestable_channel_classes_async:
2007 * @dispatcher: an #EmpathyDispatcher
2008 * @connection: a #TpConnection
2009 * @channel_type: a string identifying the type of the channel to lookup
2010 * @handle_type: the handle type for the channel
2011 * @callback: the callback to call when @connection is ready
2012 * @user_data: the user data to pass to @callback
2013 * @first_property_name: %NULL, or the name of the first fixed property,
2014 * followed optionally by more names, followed by %NULL.
2016 * Please see the documentation of
2017 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2018 * description of this function.
2021 empathy_dispatcher_find_requestable_channel_classes_async
2022 (EmpathyDispatcher *self,
2023 TpConnection *connection,
2024 const gchar *channel_type,
2026 EmpathyDispatcherFindChannelClassCb callback,
2028 const char *first_property_name,
2033 FindChannelRequest *request;
2034 EmpathyDispatcherPriv *priv;
2037 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2038 g_return_if_fail (TP_IS_CONNECTION (connection));
2039 g_return_if_fail (channel_type != NULL);
2040 g_return_if_fail (handle_type != 0);
2042 priv = GET_PRIV (self);
2044 va_start (var_args, first_property_name);
2046 properties = setup_varargs (var_args, channel_type, first_property_name);
2050 /* append another request for this connection */
2051 request = g_slice_new0 (FindChannelRequest);
2052 request->dispatcher = g_object_ref (self);
2053 request->channel_type = g_strdup (channel_type);
2054 request->handle_type = handle_type;
2055 request->connection = connection;
2056 request->callback = callback;
2057 request->user_data = user_data;
2058 request->properties = properties;
2060 source_id = g_idle_add (find_channel_class_idle_cb, request);
2062 g_hash_table_insert (priv->request_channel_class_async_ids,
2063 request, GUINT_TO_POINTER (source_id));
2067 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2070 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2071 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2073 return priv->channels;
2077 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2078 const gchar *account_path,
2079 const gchar *connection_path,
2080 const GPtrArray *channels,
2081 const GPtrArray *requests_satisfied,
2083 GHashTable *handler_info,
2087 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2088 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2091 TpConnection *connection;
2093 /* FIXME: should probably find out whether the account manager is prepared
2094 * before ensuring. See bug #600111. */
2095 account = tp_account_manager_ensure_account (priv->account_manager,
2097 g_assert (account != NULL);
2099 connection = tp_account_ensure_connection (account, connection_path);
2100 if (connection == NULL)
2102 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2103 "Invalid connection argument");
2107 for (i = 0; i < channels->len ; i++)
2109 GValueArray *arr = g_ptr_array_index (channels, i);
2110 const gchar *object_path;
2111 GHashTable *properties;
2113 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2114 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2116 dispatcher_connection_new_channel_with_properties (self,
2117 connection, object_path, properties);
2125 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2130 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2131 EmpathyHandler *handler;
2133 handler = empathy_handler_new (name, filters, capabilities);
2134 priv->handlers = g_list_prepend (priv->handlers, handler);
2136 /* Only set the handle_channels function, the Channel property on the main
2137 * handler will always report all dispatched channels even if they came from
2138 * a different Handler */
2139 empathy_handler_set_handle_channels_func (handler,
2140 empathy_dispatcher_handle_channels, self);
2146 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2147 EmpathyHandler *handler)
2149 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2152 h = g_list_find (priv->handlers, handler);
2153 g_return_if_fail (h != NULL);
2155 priv->handlers = g_list_delete_link (priv->handlers, h);
2157 g_object_unref (handler);