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-tube-handler.h"
49 #include "empathy-tp-contact-factory.h"
50 #include "empathy-chatroom-manager.h"
51 #include "empathy-utils.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
59 gboolean dispose_has_run;
61 TpAccountManager *account_manager;
62 /* connection to connection data mapping */
63 GHashTable *connections;
64 GHashTable *outstanding_classes_requests;
68 /* channels which the dispatcher is listening "invalidated" */
73 EmpathyHandler *handler;
78 GHashTable *request_channel_class_async_ids;
79 /* (TpAccount *) => gulong
80 * Signal handler ID of the "status-changed" signal */
81 GHashTable *status_changed_handlers;
82 } EmpathyDispatcherPriv;
85 empathy_dispatcher_get_channels (EmpathyHandler *handler,
89 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
90 const gchar *account_path,
91 const gchar *connection_path,
92 const GPtrArray *channels,
93 const GPtrArray *requests_satisfied,
95 GHashTable *handler_info,
99 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
115 static guint signals[LAST_SIGNAL];
116 static EmpathyDispatcher *dispatcher = NULL;
118 static void dispatcher_init_connection_if_needed (
119 EmpathyDispatcher *dispatcher,
120 TpConnection *connection);
122 static GList * empathy_dispatcher_find_channel_classes
123 (EmpathyDispatcher *dispatcher, TpConnection *connection,
124 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
129 EmpathyDispatcher *dispatcher;
130 EmpathyDispatchOperation *operation;
131 TpConnection *connection;
132 gboolean should_ensure;
136 EmpathyContact *contact;
137 TpProxyPendingCall *pending_call;
139 /* Properties to pass to the channel when requesting it */
141 EmpathyDispatcherRequestCb *cb;
143 gpointer *request_data;
144 } DispatcherRequestData;
149 /* Channel type specific wrapper object */
150 GObject *channel_wrapper;
155 /* ObjectPath => DispatchData.. */
156 GHashTable *dispatched_channels;
157 /* ObjectPath -> EmpathyDispatchOperations */
158 GHashTable *dispatching_channels;
160 /* ObjectPath -> EmpathyDispatchOperations
162 * This holds channels which were announced with NewChannel while we have an
163 * outstanding channel request for a channel of this type. On the Requests
164 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
165 * return before NewChannels is emitted, but there was no guarantee of the
166 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
167 * held in limbo here until we know whether they were requested.
169 GHashTable *outstanding_channels;
170 /* List of DispatcherRequestData */
171 GList *outstanding_requests;
172 /* List of requestable channel classes */
173 GPtrArray *requestable_channels;
178 EmpathyDispatcher *dispatcher;
179 TpConnection *connection;
183 EmpathyDispatcherFindChannelClassCb *callback;
185 } FindChannelRequest;
188 empathy_dispatcher_call_create_or_ensure_channel (
189 EmpathyDispatcher *dispatcher,
190 DispatcherRequestData *request_data);
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);
260 if (r->pending_call != NULL)
261 tp_proxy_pending_call_cancel (r->pending_call);
263 g_slice_free (DispatcherRequestData, r);
266 static ConnectionData *
267 new_connection_data (void)
269 ConnectionData *cd = g_slice_new0 (ConnectionData);
271 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
272 g_free, (GDestroyNotify) free_dispatch_data);
274 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
275 g_free, g_object_unref);
277 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
284 free_connection_data (ConnectionData *cd)
289 g_hash_table_destroy (cd->dispatched_channels);
290 g_hash_table_destroy (cd->dispatching_channels);
292 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
294 free_dispatcher_request_data (l->data);
297 if (cd->requestable_channels != NULL)
299 for (i = 0 ; i < cd->requestable_channels->len ; i++)
301 g_ptr_array_index (cd->requestable_channels, i));
302 g_ptr_array_free (cd->requestable_channels, TRUE);
307 free_find_channel_request (FindChannelRequest *r)
312 g_object_unref (r->dispatcher);
313 g_free (r->channel_type);
315 if (r->properties != NULL)
317 for (idx = 0; idx < r->properties->len ; idx++)
319 str = g_array_index (r->properties, char *, idx);
323 g_array_free (r->properties, TRUE);
326 g_slice_free (FindChannelRequest, r);
330 dispatcher_connection_invalidated_cb (TpConnection *connection,
334 EmpathyDispatcher *self)
336 EmpathyDispatcherPriv *priv = GET_PRIV (self);
338 DEBUG ("Error: %s", message);
339 g_hash_table_remove (priv->connections, connection);
343 dispatcher_operation_can_start (EmpathyDispatcher *self,
344 EmpathyDispatchOperation *operation,
348 const gchar *channel_type =
349 empathy_dispatch_operation_get_channel_type (operation);
351 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
353 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
355 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
365 dispatch_operation_flush_requests (EmpathyDispatcher *self,
366 EmpathyDispatchOperation *operation,
372 l = cd->outstanding_requests;
375 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
380 if (d->operation == operation)
385 d->cb (NULL, error, d->user_data);
387 d->cb (operation, NULL, d->user_data);
390 cd->outstanding_requests = g_list_delete_link
391 (cd->outstanding_requests, lt);
393 free_dispatcher_request_data (d);
399 dispatcher_channel_invalidated_cb (TpProxy *proxy,
403 EmpathyDispatcher *self)
405 /* Channel went away... */
406 EmpathyDispatcherPriv *priv = GET_PRIV (self);
407 TpConnection *connection;
408 EmpathyDispatchOperation *operation;
410 const gchar *object_path;
412 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
414 cd = g_hash_table_lookup (priv->connections, connection);
415 /* Connection itself invalidated? */
419 object_path = tp_proxy_get_object_path (proxy);
421 DEBUG ("Channel %s invalidated", object_path);
423 g_hash_table_remove (cd->dispatched_channels, object_path);
424 g_hash_table_remove (cd->dispatching_channels, object_path);
426 priv->channels = g_list_remove (priv->channels, proxy);
428 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
429 if (operation != NULL)
431 GError error = { domain, code, message };
432 dispatch_operation_flush_requests (self, operation, &error, cd);
433 g_hash_table_remove (cd->outstanding_channels, object_path);
434 g_object_unref (operation);
439 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
440 EmpathyDispatcher *self)
442 g_assert (empathy_dispatch_operation_is_incoming (operation));
443 DEBUG ("Send of for dispatching: %s",
444 empathy_dispatch_operation_get_object_path (operation));
445 g_signal_emit (self, signals[DISPATCH], 0, operation);
449 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
450 EmpathyDispatcher *self)
452 /* Our job is done, remove the dispatch operation and mark the channel as
454 EmpathyDispatcherPriv *priv = GET_PRIV (self);
455 TpConnection *connection;
457 const gchar *object_path;
459 connection = empathy_dispatch_operation_get_tp_connection (operation);
460 cd = g_hash_table_lookup (priv->connections, connection);
461 g_assert (cd != NULL);
463 object_path = empathy_dispatch_operation_get_object_path (operation);
465 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
468 d = new_dispatch_data (
469 empathy_dispatch_operation_get_channel (operation),
470 empathy_dispatch_operation_get_channel_wrapper (operation));
471 g_hash_table_insert (cd->dispatched_channels,
472 g_strdup (object_path), d);
474 g_hash_table_remove (cd->dispatching_channels, object_path);
476 DEBUG ("Channel claimed: %s", object_path);
480 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
481 EmpathyDispatcher *self)
483 EmpathyDispatcherPriv *priv = GET_PRIV (self);
484 TpConnection *connection;
486 EmpathyDispatchOperationState status;
488 g_signal_connect (operation, "approved",
489 G_CALLBACK (dispatch_operation_approved_cb), self);
491 g_signal_connect (operation, "claimed",
492 G_CALLBACK (dispatch_operation_claimed_cb), self);
494 /* Signal the observers */
495 DEBUG ("Send to observers: %s",
496 empathy_dispatch_operation_get_object_path (operation));
497 g_signal_emit (self, signals[OBSERVE], 0, operation);
499 empathy_dispatch_operation_start (operation);
501 /* Signal potential requestors */
502 connection = empathy_dispatch_operation_get_tp_connection (operation);
503 cd = g_hash_table_lookup (priv->connections, connection);
504 g_assert (cd != NULL);
506 g_object_ref (operation);
509 dispatch_operation_flush_requests (self, operation, NULL, cd);
510 status = empathy_dispatch_operation_get_status (operation);
511 g_object_unref (operation);
513 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
516 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
518 DEBUG ("Send to approvers: %s",
519 empathy_dispatch_operation_get_object_path (operation));
520 g_signal_emit (self, signals[APPROVE], 0, operation);
524 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
525 DEBUG ("Send of for dispatching: %s",
526 empathy_dispatch_operation_get_object_path (operation));
527 g_signal_emit (self, signals[DISPATCH], 0, operation);
530 g_object_unref (self);
534 dispatcher_start_dispatching (EmpathyDispatcher *self,
535 EmpathyDispatchOperation *operation,
538 const gchar *object_path =
539 empathy_dispatch_operation_get_object_path (operation);
541 DEBUG ("Dispatching process started for %s", object_path);
543 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
545 g_assert (g_hash_table_lookup (cd->outstanding_channels,
546 object_path) == NULL);
548 g_hash_table_insert (cd->dispatching_channels,
549 g_strdup (object_path), operation);
551 switch (empathy_dispatch_operation_get_status (operation))
553 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
554 g_signal_connect (operation, "ready",
555 G_CALLBACK (dispatch_operation_ready_cb), self);
557 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
558 dispatch_operation_ready_cb (operation, self);
561 g_assert_not_reached ();
565 else if (empathy_dispatch_operation_get_status (operation) >=
566 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
568 /* Already dispatching and the operation is pending, thus the observers
569 * have seen it (if applicable), so we can flush the request right away.
571 dispatch_operation_flush_requests (self, operation, NULL, cd);
576 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
582 g_hash_table_iter_init (&iter, cd->outstanding_channels);
583 while (g_hash_table_iter_next (&iter, NULL, &value))
585 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
587 if (dispatcher_operation_can_start (self, operation, cd))
589 g_hash_table_iter_remove (&iter);
590 dispatcher_start_dispatching (self, operation, cd);
596 dispatcher_connection_new_channel (EmpathyDispatcher *self,
597 TpConnection *connection,
598 const gchar *object_path,
599 const gchar *channel_type,
602 GHashTable *properties,
605 EmpathyDispatcherPriv *priv = GET_PRIV (self);
608 EmpathyDispatchOperation *operation;
610 /* Channel types we never want to dispatch because they're either deprecated
611 * or can't sensibly be dispatch (e.g. channels that should always be
613 const char *blacklist[] = {
614 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
615 TP_IFACE_CHANNEL_TYPE_TUBES,
616 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
620 dispatcher_init_connection_if_needed (self, connection);
622 cd = g_hash_table_lookup (priv->connections, connection);
624 /* Don't bother with channels we have already dispatched or are dispatching
625 * currently. This can happen when NewChannel(s) is fired after
626 * RequestChannel/CreateChannel/EnsureChannel */
627 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
630 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
633 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
635 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
638 /* Only pick up non-requested text and file channels. For all other it
639 * doesn't make sense to handle it if we didn't request it. The same goes
640 * for channels we discovered by the Channels property or ListChannels */
641 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
642 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
644 DEBUG ("Ignoring incoming channel of type %s on %s",
645 channel_type, object_path);
649 for (i = 0 ; blacklist[i] != NULL; i++)
651 if (!tp_strdiff (channel_type, blacklist[i]))
653 DEBUG ("Ignoring blacklisted channel type %s on %s",
654 channel_type, object_path);
659 DEBUG ("New channel of type %s on %s", channel_type, object_path);
661 if (properties == NULL)
662 channel = tp_channel_new (connection, object_path, channel_type,
663 handle_type, handle, NULL);
665 channel = tp_channel_new_from_properties (connection, object_path,
668 g_signal_connect (channel, "invalidated",
669 G_CALLBACK (dispatcher_channel_invalidated_cb),
672 priv->channels = g_list_prepend (priv->channels, channel);
674 operation = empathy_dispatch_operation_new (connection, channel, NULL,
677 g_object_unref (channel);
681 /* Request could either be by us or by a remote party. If there are no
682 * outstanding requests for this channel type we can assume it's remote.
683 * Otherwise we wait untill they are all satisfied */
684 if (dispatcher_operation_can_start (self, operation, cd))
685 dispatcher_start_dispatching (self, operation, cd);
687 g_hash_table_insert (cd->outstanding_channels,
688 g_strdup (object_path), operation);
692 dispatcher_start_dispatching (self, operation, cd);
697 dispatcher_connection_new_channel_with_properties (
698 EmpathyDispatcher *self,
699 TpConnection *connection,
700 const gchar *object_path,
701 GHashTable *properties)
703 const gchar *channel_type;
710 channel_type = tp_asv_get_string (properties,
711 TP_IFACE_CHANNEL ".ChannelType");
712 if (channel_type == NULL)
714 g_message ("%s had an invalid ChannelType property", object_path);
718 handle_type = tp_asv_get_uint32 (properties,
719 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
722 g_message ("%s had an invalid TargetHandleType property", object_path);
726 handle = tp_asv_get_uint32 (properties,
727 TP_IFACE_CHANNEL ".TargetHandle", &valid);
730 g_message ("%s had an invalid TargetHandle property", object_path);
734 /* We assume there is no channel dispather, so we're the only one dispatching
735 * it. Which means that a requested channel it is outgoing one */
736 requested = tp_asv_get_boolean (properties,
737 TP_IFACE_CHANNEL ".Requested", &valid);
740 g_message ("%s had an invalid Requested property", object_path);
744 dispatcher_connection_new_channel (self, connection,
745 object_path, channel_type, handle_type, handle, properties, !requested);
749 dispatcher_connection_got_all (TpProxy *proxy,
750 GHashTable *properties,
755 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
756 EmpathyDispatcherPriv *priv = GET_PRIV (self);
757 GPtrArray *requestable_channels;
758 GPtrArray *existing_channels;
761 DEBUG ("Error: %s", error->message);
765 requestable_channels = tp_asv_get_boxed (properties,
766 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
768 if (requestable_channels == NULL)
769 DEBUG ("No RequestableChannelClasses property !?! on connection");
774 FindChannelRequest *request;
777 cd = g_hash_table_lookup (priv->connections, proxy);
778 g_assert (cd != NULL);
780 cd->requestable_channels = g_boxed_copy (
781 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
783 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
786 for (l = requests; l != NULL; l = l->next)
790 retval = empathy_dispatcher_find_channel_classes (self,
791 TP_CONNECTION (proxy), request->channel_type,
792 request->handle_type, request->properties);
793 request->callback (retval, request->user_data);
795 free_find_channel_request (request);
796 g_list_free (retval);
799 g_list_free (requests);
801 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
804 existing_channels = tp_asv_get_boxed (properties,
805 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
807 if (existing_channels != NULL)
811 for (idx = 0; idx < existing_channels->len; idx++)
813 GValueArray *values = g_ptr_array_index (existing_channels, idx);
814 const gchar *object_path;
817 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
818 props = g_value_get_boxed (g_value_array_get_nth (values, 1));
820 if (tp_strdiff (tp_asv_get_string (props,
821 TP_IFACE_CHANNEL ".ChannelType"),
822 TP_IFACE_CHANNEL_TYPE_TEXT))
825 dispatcher_connection_new_channel_with_properties (self,
826 TP_CONNECTION (proxy), object_path, props);
832 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
833 const GPtrArray *capabilities,
839 DEBUG ("Error: %s", error->message);
843 connection_ready_cb (TpConnection *connection,
847 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
848 GPtrArray *capabilities;
851 const gchar *remove_ = NULL;
855 DEBUG ("Error: %s", error->message);
859 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
860 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
862 tp_cli_dbus_properties_call_get_all (connection, -1,
863 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
864 dispatcher_connection_got_all,
865 NULL, NULL, G_OBJECT (self));
868 /* Advertise VoIP capabilities */
869 capabilities = g_ptr_array_sized_new (1);
870 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
871 G_TYPE_UINT, G_TYPE_INVALID);
872 g_value_init (&cap, cap_type);
873 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
874 dbus_g_type_struct_set (&cap,
875 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
876 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
877 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
878 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
879 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
880 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
882 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
884 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
885 connection, -1, capabilities, &remove_,
886 dispatcher_connection_advertise_capabilities_cb,
887 NULL, NULL, G_OBJECT (self));
889 g_value_unset (&cap);
890 g_ptr_array_free (capabilities, TRUE);
892 g_object_unref (self);
896 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
897 TpConnection *connection)
899 EmpathyDispatcherPriv *priv = GET_PRIV (self);
901 if (g_hash_table_lookup (priv->connections, connection) != NULL)
904 g_hash_table_insert (priv->connections, g_object_ref (connection),
905 new_connection_data ());
907 g_signal_connect (connection, "invalidated",
908 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
910 /* Ensure to keep the self object alive while the call_when_ready is
913 tp_connection_call_when_ready (connection, connection_ready_cb, self);
917 dispatcher_status_changed_cb (TpAccount *account,
921 gchar *dbus_error_name,
923 EmpathyDispatcher *self)
925 TpConnection *conn = tp_account_get_connection (account);
928 dispatcher_init_connection_if_needed (self, conn);
932 remove_idle_handlers (gpointer key,
938 source_id = GPOINTER_TO_UINT (value);
939 g_source_remove (source_id);
943 dispatcher_constructor (GType type,
944 guint n_construct_params,
945 GObjectConstructParam *construct_params)
948 EmpathyDispatcherPriv *priv;
950 if (dispatcher != NULL)
951 return g_object_ref (dispatcher);
953 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
954 (type, n_construct_params, construct_params);
956 dispatcher = EMPATHY_DISPATCHER (retval);
957 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
959 priv = GET_PRIV (dispatcher);
961 if (priv->handler == NULL)
962 priv->handler = empathy_handler_new (NULL, NULL, NULL);
964 empathy_handler_set_handle_channels_func (priv->handler,
965 empathy_dispatcher_handle_channels,
968 empathy_handler_set_channels_func (priv->handler,
969 empathy_dispatcher_get_channels,
976 dispatcher_dispose (GObject *object)
978 EmpathyDispatcherPriv *priv = GET_PRIV (object);
983 if (priv->dispose_has_run)
986 priv->dispose_has_run = TRUE;
988 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
989 g_object_unref (G_OBJECT (l->data));
991 g_list_free (priv->handlers);
992 priv->handlers = NULL;
994 if (priv->handler != NULL)
995 g_object_unref (priv->handler);
996 priv->handler = NULL;
998 g_hash_table_iter_init (&iter, priv->connections);
999 while (g_hash_table_iter_next (&iter, &connection, NULL))
1001 g_signal_handlers_disconnect_by_func (connection,
1002 dispatcher_connection_invalidated_cb, object);
1005 g_hash_table_destroy (priv->connections);
1006 priv->connections = NULL;
1008 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
1012 dispatcher_finalize (GObject *object)
1014 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1016 GHashTableIter iter;
1017 gpointer connection;
1019 gpointer account, id;
1021 if (priv->request_channel_class_async_ids != NULL)
1023 g_hash_table_foreach (priv->request_channel_class_async_ids,
1024 remove_idle_handlers, NULL);
1025 g_hash_table_destroy (priv->request_channel_class_async_ids);
1028 for (l = priv->channels; l; l = l->next)
1030 g_signal_handlers_disconnect_by_func (l->data,
1031 dispatcher_channel_invalidated_cb, object);
1034 g_list_free (priv->channels);
1036 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1037 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1039 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1043 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1044 while (g_hash_table_iter_next (&iter, &account, &id))
1046 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1048 g_hash_table_destroy (priv->status_changed_handlers);
1050 g_object_unref (priv->account_manager);
1052 g_hash_table_destroy (priv->outstanding_classes_requests);
1056 dispatcher_set_property (GObject *object,
1058 const GValue *value,
1061 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1062 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1064 switch (property_id)
1067 priv->handler = g_value_dup_object (value);
1070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1076 dispatcher_get_property (GObject *object,
1081 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1082 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1084 switch (property_id)
1087 g_value_set_object (value, priv->handler);
1090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1096 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1098 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1099 GParamSpec *param_spec;
1101 object_class->dispose = dispatcher_dispose;
1102 object_class->finalize = dispatcher_finalize;
1103 object_class->constructor = dispatcher_constructor;
1105 object_class->get_property = dispatcher_get_property;
1106 object_class->set_property = dispatcher_set_property;
1108 param_spec = g_param_spec_object ("handler", "handler",
1109 "The main Telepathy Client Hander object",
1110 EMPATHY_TYPE_HANDLER,
1111 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1112 g_object_class_install_property (object_class,
1113 PROP_HANDLER, param_spec);
1116 g_signal_new ("observe",
1117 G_TYPE_FROM_CLASS (klass),
1121 g_cclosure_marshal_VOID__OBJECT,
1123 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1126 g_signal_new ("approve",
1127 G_TYPE_FROM_CLASS (klass),
1131 g_cclosure_marshal_VOID__OBJECT,
1133 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1136 g_signal_new ("dispatch",
1137 G_TYPE_FROM_CLASS (klass),
1141 g_cclosure_marshal_VOID__OBJECT,
1143 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1146 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1150 connect_account (EmpathyDispatcher *self,
1153 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1154 TpConnection *conn = tp_account_get_connection (account);
1157 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1164 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1166 id = g_signal_connect (account, "status-changed",
1167 G_CALLBACK (dispatcher_status_changed_cb), self);
1169 g_hash_table_insert (priv->status_changed_handlers, account,
1170 GUINT_TO_POINTER (id));
1174 account_manager_prepared_cb (GObject *source_object,
1175 GAsyncResult *result,
1178 GList *accounts, *l;
1179 EmpathyDispatcher *self = user_data;
1180 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1181 GError *error = NULL;
1183 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1185 DEBUG ("Failed to prepare account manager: %s", error->message);
1186 g_error_free (error);
1190 accounts = tp_account_manager_get_valid_accounts (account_manager);
1191 for (l = accounts; l; l = l->next)
1193 TpAccount *a = l->data;
1195 connect_account (self, a);
1197 g_list_free (accounts);
1201 empathy_dispatcher_init (EmpathyDispatcher *self)
1203 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1204 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1207 priv->account_manager = tp_account_manager_dup ();
1209 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1210 g_object_unref, (GDestroyNotify) free_connection_data);
1212 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1213 g_direct_equal, g_object_unref, NULL);
1215 priv->channels = NULL;
1217 tp_account_manager_prepare_async (priv->account_manager, NULL,
1218 account_manager_prepared_cb, self);
1220 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1222 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1227 empathy_dispatcher_new (const gchar *name,
1231 EmpathyHandler *handler;
1232 EmpathyDispatcher *ret;
1234 g_assert (dispatcher == NULL);
1235 handler = empathy_handler_new (name, filters, capabilities);
1237 ret = EMPATHY_DISPATCHER (
1238 g_object_new (EMPATHY_TYPE_DISPATCHER,
1241 g_object_unref (handler);
1247 empathy_dispatcher_dup_singleton (void)
1249 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1253 dispatcher_request_failed (EmpathyDispatcher *self,
1254 DispatcherRequestData *request_data,
1255 const GError *error)
1257 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1258 ConnectionData *conn_data;
1260 conn_data = g_hash_table_lookup (priv->connections,
1261 request_data->connection);
1262 if (request_data->cb != NULL)
1263 request_data->cb (NULL, error, request_data->user_data);
1265 if (conn_data != NULL)
1267 conn_data->outstanding_requests =
1268 g_list_remove (conn_data->outstanding_requests, request_data);
1270 /* else Connection has been invalidated */
1272 free_dispatcher_request_data (request_data);
1276 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1277 DispatcherRequestData *request_data,
1278 const gchar *object_path,
1279 GHashTable *properties,
1280 const GError *error)
1282 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1283 EmpathyDispatchOperation *operation = NULL;
1284 ConnectionData *conn_data;
1286 conn_data = g_hash_table_lookup (priv->connections,
1287 request_data->connection);
1291 DEBUG ("Channel request failed: %s", error->message);
1293 dispatcher_request_failed (self, request_data, error);
1298 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1301 if (operation != NULL)
1302 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1304 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1307 if (operation == NULL)
1309 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1314 operation = empathy_dispatch_operation_new_with_wrapper (
1315 request_data->connection,
1316 data->channel, request_data->contact, FALSE,
1317 data->channel_wrapper);
1323 if (properties != NULL)
1324 channel = tp_channel_new_from_properties (request_data->connection,
1325 object_path, properties, NULL);
1327 channel = tp_channel_new (request_data->connection, object_path,
1328 request_data->channel_type, request_data->handle_type,
1329 request_data->handle, NULL);
1331 g_signal_connect (channel, "invalidated",
1332 G_CALLBACK (dispatcher_channel_invalidated_cb),
1333 request_data->dispatcher);
1335 priv->channels = g_list_prepend (priv->channels, channel);
1337 operation = empathy_dispatch_operation_new (request_data->connection,
1338 channel, request_data->contact, FALSE);
1339 g_object_unref (channel);
1344 /* Already existed set potential extra information */
1345 g_object_set (G_OBJECT (operation),
1346 "contact", request_data->contact,
1350 request_data->operation = operation;
1352 /* (pre)-approve this right away as we requested it
1353 * This might cause the channel to be claimed, in which case the operation
1354 * will disappear. So ref it, and check the status before starting the
1357 g_object_ref (operation);
1358 empathy_dispatch_operation_approve (operation);
1360 if (empathy_dispatch_operation_get_status (operation) <
1361 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1362 dispatcher_start_dispatching (request_data->dispatcher, operation,
1365 g_object_unref (operation);
1368 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1373 dispatcher_request_channel_cb (TpConnection *connection,
1374 const gchar *object_path,
1375 const GError *error,
1377 GObject *weak_object)
1379 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1380 EmpathyDispatcher *self =
1381 EMPATHY_DISPATCHER (request_data->dispatcher);
1383 request_data->pending_call = NULL;
1385 dispatcher_connection_new_requested_channel (self,
1386 request_data, object_path, NULL, error);
1390 dispatcher_request_channel (DispatcherRequestData *request_data)
1392 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1393 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1395 /* Extend the request_data to be a valid request */
1396 g_assert (request_data->request == NULL);
1397 request_data->request = tp_asv_new (
1398 TP_IFACE_CHANNEL ".ChannelType",
1399 G_TYPE_STRING, request_data->channel_type,
1400 TP_IFACE_CHANNEL ".TargetHandleType",
1401 G_TYPE_UINT, request_data->handle_type,
1404 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1405 tp_asv_set_uint32 (request_data->request,
1406 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1408 empathy_dispatcher_call_create_or_ensure_channel (
1409 request_data->dispatcher, request_data);
1413 request_data->pending_call = tp_cli_connection_call_request_channel (
1414 request_data->connection, -1,
1415 request_data->channel_type,
1416 request_data->handle_type,
1417 request_data->handle,
1418 TRUE, dispatcher_request_channel_cb,
1419 request_data, NULL, NULL);
1424 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1425 EmpathyDispatcherRequestCb *callback,
1428 EmpathyDispatcher *self;
1429 EmpathyDispatcherPriv *priv;
1430 TpConnection *connection;
1431 ConnectionData *connection_data;
1432 DispatcherRequestData *request_data;
1434 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1436 self = empathy_dispatcher_dup_singleton ();
1437 priv = GET_PRIV (self);
1439 connection = empathy_contact_get_connection (contact);
1440 connection_data = g_hash_table_lookup (priv->connections, connection);
1442 /* The contact handle might not be known yet */
1443 request_data = new_dispatcher_request_data (self, connection,
1444 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1445 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1446 request_data->should_ensure = TRUE;
1448 connection_data->outstanding_requests = g_list_prepend
1449 (connection_data->outstanding_requests, request_data);
1451 dispatcher_request_channel (request_data);
1453 g_object_unref (self);
1458 EmpathyDispatcher *dispatcher;
1459 EmpathyDispatcherRequestCb *callback;
1461 } ChatWithContactIdData;
1464 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1465 EmpathyContact *contact,
1466 const GError *error,
1468 GObject *weak_object)
1470 ChatWithContactIdData *data = user_data;
1474 /* FIXME: Should call data->callback with the error */
1475 DEBUG ("Error: %s", error->message);
1479 empathy_dispatcher_chat_with_contact (contact, data->callback,
1483 g_object_unref (data->dispatcher);
1484 g_slice_free (ChatWithContactIdData, data);
1488 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1489 const gchar *contact_id,
1490 EmpathyDispatcherRequestCb *callback,
1493 EmpathyDispatcher *self;
1494 EmpathyTpContactFactory *factory;
1495 ChatWithContactIdData *data;
1497 g_return_if_fail (TP_IS_CONNECTION (connection));
1498 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1500 self = empathy_dispatcher_dup_singleton ();
1501 factory = empathy_tp_contact_factory_dup_singleton (connection);
1502 data = g_slice_new0 (ChatWithContactIdData);
1503 data->dispatcher = self;
1504 data->callback = callback;
1505 data->user_data = user_data;
1506 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1507 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1509 g_object_unref (factory);
1513 dispatcher_request_handles_cb (TpConnection *connection,
1514 const GArray *handles,
1515 const GError *error,
1519 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1521 request_data->pending_call = NULL;
1525 EmpathyDispatcher *self = request_data->dispatcher;
1526 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1529 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1531 if (request_data->cb)
1532 request_data->cb (NULL, error, request_data->user_data);
1534 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1537 free_dispatcher_request_data (request_data);
1539 dispatcher_flush_outstanding_operations (self, cd);
1543 request_data->handle = g_array_index (handles, guint, 0);
1544 dispatcher_request_channel (request_data);
1548 empathy_dispatcher_join_muc (TpConnection *connection,
1549 const gchar *roomname,
1550 EmpathyDispatcherRequestCb *callback,
1553 EmpathyDispatcher *self;
1554 EmpathyDispatcherPriv *priv;
1555 DispatcherRequestData *request_data;
1556 ConnectionData *connection_data;
1557 const gchar *names[] = { roomname, NULL };
1559 g_return_if_fail (TP_IS_CONNECTION (connection));
1560 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1562 self = empathy_dispatcher_dup_singleton ();
1563 priv = GET_PRIV (self);
1565 connection_data = g_hash_table_lookup (priv->connections, connection);
1566 g_assert (connection_data != NULL);
1568 /* Don't know the room handle yet */
1569 request_data = new_dispatcher_request_data (self, connection,
1570 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1571 NULL, callback, user_data);
1572 request_data->should_ensure = TRUE;
1574 connection_data->outstanding_requests = g_list_prepend
1575 (connection_data->outstanding_requests, request_data);
1577 request_data->pending_call = tp_cli_connection_call_request_handles (
1579 TP_HANDLE_TYPE_ROOM, names,
1580 dispatcher_request_handles_cb, request_data, NULL, NULL);
1582 g_object_unref (self);
1586 dispatcher_create_channel_cb (TpConnection *connect,
1587 const gchar *object_path,
1588 GHashTable *properties,
1589 const GError *error,
1591 GObject *weak_object)
1593 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1594 EmpathyDispatcher *self =
1595 EMPATHY_DISPATCHER (request_data->dispatcher);
1597 request_data->pending_call = NULL;
1599 dispatcher_connection_new_requested_channel (self,
1600 request_data, object_path, properties, error);
1604 dispatcher_ensure_channel_cb (TpConnection *connect,
1606 const gchar *object_path,
1607 GHashTable *properties,
1608 const GError *error,
1610 GObject *weak_object)
1612 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1613 EmpathyDispatcher *self =
1614 EMPATHY_DISPATCHER (request_data->dispatcher);
1616 request_data->pending_call = NULL;
1618 dispatcher_connection_new_requested_channel (self,
1619 request_data, object_path, properties, error);
1623 empathy_dispatcher_call_create_or_ensure_channel (
1624 EmpathyDispatcher *self,
1625 DispatcherRequestData *request_data)
1627 if (request_data->should_ensure)
1629 request_data->pending_call =
1630 tp_cli_connection_interface_requests_call_ensure_channel (
1631 request_data->connection, -1,
1632 request_data->request, dispatcher_ensure_channel_cb,
1633 request_data, NULL, NULL);
1637 request_data->pending_call =
1638 tp_cli_connection_interface_requests_call_create_channel (
1639 request_data->connection, -1,
1640 request_data->request, dispatcher_create_channel_cb,
1641 request_data, NULL, NULL);
1646 * empathy_dispatcher_create_channel:
1647 * @self: the EmpathyDispatcher
1648 * @connection: the Connection to dispatch on
1649 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1650 * @callback: a callback for when the channel arrives (or NULL)
1651 * @user_data: optional user data (or NULL)
1653 * When calling this function, #EmpathyDispatcher takes ownership of your
1654 * reference to @request. DO NOT unref or destroy @request. When the request is
1655 * done, @request will be unreferenced. Take another reference if you want to
1659 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1660 TpConnection *connection,
1661 GHashTable *request,
1662 EmpathyDispatcherRequestCb *callback,
1665 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1666 ConnectionData *connection_data;
1667 DispatcherRequestData *request_data;
1668 const gchar *channel_type;
1673 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1674 g_return_if_fail (TP_IS_CONNECTION (connection));
1675 g_return_if_fail (request != NULL);
1677 connection_data = g_hash_table_lookup (priv->connections, connection);
1678 g_assert (connection_data != NULL);
1680 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1682 handle_type = tp_asv_get_uint32 (request,
1683 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1685 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1687 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1689 request_data = new_dispatcher_request_data (self, connection,
1690 channel_type, handle_type, handle, request,
1691 NULL, callback, user_data);
1693 connection_data->outstanding_requests = g_list_prepend
1694 (connection_data->outstanding_requests, request_data);
1696 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1700 channel_class_matches (GValueArray *class,
1701 const char *channel_type,
1703 GArray *fixed_properties)
1711 v = g_value_array_get_nth (class, 0);
1713 /* if the class doesn't match channel type discard it. */
1714 fprops = g_value_get_boxed (v);
1715 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1717 if (tp_strdiff (channel_type, c_type))
1720 /* we have the right channel type, see if the handle type matches */
1721 h_type = tp_asv_get_uint32 (fprops,
1722 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1724 if (!valid || handle_type != h_type)
1727 if (fixed_properties != NULL)
1729 gpointer h_key, h_val;
1731 GHashTableIter iter;
1734 g_hash_table_iter_init (&iter, fprops);
1736 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1738 /* discard ChannelType and TargetHandleType, as we already
1741 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1743 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1748 for (idx = 0; idx < fixed_properties->len; idx++)
1750 /* if |key| doesn't exist in |fixed_properties|, discard
1755 g_array_index (fixed_properties, char *, idx)))
1758 /* exit the for() loop */
1769 /* if no fixed_properties are specified, discard the classes
1770 * with some fixed properties other than the two we already
1773 if (g_hash_table_size (fprops) > 2)
1781 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1782 TpConnection *connection,
1783 const gchar *channel_type,
1785 GArray *fixed_properties)
1787 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1790 GList *matching_classes;
1794 g_return_val_if_fail (channel_type != NULL, NULL);
1795 g_return_val_if_fail (handle_type != 0, NULL);
1797 cd = g_hash_table_lookup (priv->connections, connection);
1802 classes = cd->requestable_channels;
1803 if (classes == NULL)
1806 matching_classes = NULL;
1808 for (i = 0; i < classes->len; i++)
1810 class = g_ptr_array_index (classes, i);
1812 if (!channel_class_matches
1813 (class, channel_type, handle_type, fixed_properties))
1816 matching_classes = g_list_prepend (matching_classes, class);
1819 return matching_classes;
1823 find_channel_class_idle_cb (gpointer user_data)
1827 FindChannelRequest *request = user_data;
1829 gboolean is_ready = TRUE;
1830 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1832 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1834 cd = g_hash_table_lookup (priv->connections, request->connection);
1838 else if (cd->requestable_channels == NULL)
1843 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1844 request->connection, request->channel_type, request->handle_type,
1845 request->properties);
1847 request->callback (retval, request->user_data);
1848 free_find_channel_request (request);
1849 g_list_free (retval);
1854 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1855 request->connection);
1856 requests = g_list_prepend (requests, request);
1858 g_hash_table_insert (priv->outstanding_classes_requests,
1859 request->connection, requests);
1865 setup_varargs (va_list var_args,
1866 const char *channel_namespace,
1867 const char *first_property_name)
1873 if (first_property_name == NULL)
1876 name = first_property_name;
1877 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1879 while (name != NULL)
1881 name_full = g_strdup (name);
1882 properties = g_array_append_val (properties, name_full);
1883 name = va_arg (var_args, char *);
1890 * empathy_dispatcher_find_requestable_channel_classes:
1891 * @dispatcher: an #EmpathyDispatcher
1892 * @connection: a #TpConnection
1893 * @channel_type: a string identifying the type of the channel to lookup
1894 * @handle_type: the handle type for the channel
1895 * @first_property_name: %NULL, or the name of the first fixed property,
1896 * followed optionally by more names, followed by %NULL.
1898 * Returns all the channel classes that a client can request for the connection
1899 * @connection, of the type identified by @channel_type, @handle_type and the
1900 * fixed properties list.
1901 * The classes which are compatible with a fixed properties list (i.e. those
1902 * that will be returned by this function) are intended as those that do not
1903 * contain any fixed property other than those in the list; note that this
1904 * doesn't guarantee that all the classes compatible with the list will contain
1905 * all the requested fixed properties, so the clients will have to filter
1906 * the returned list themselves.
1907 * If @first_property_name is %NULL, only the classes with no other fixed
1908 * properties than ChannelType and TargetHandleType will be returned.
1909 * Note that this function may return %NULL without performing any lookup if
1910 * @connection is not ready. To ensure that @connection is always ready,
1911 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1913 * Return value: a #GList of #GValueArray objects, where the first element in
1914 * the array is a #GHashTable of the fixed properties, and the second is
1915 * a #GStrv of the allowed properties for the class. The list should be free'd
1916 * with g_list_free() when done, but the objects inside the list are owned
1917 * by the #EmpathyDispatcher and must not be modified.
1920 empathy_dispatcher_find_requestable_channel_classes
1921 (EmpathyDispatcher *self,
1922 TpConnection *connection,
1923 const gchar *channel_type,
1925 const char *first_property_name,
1930 EmpathyDispatcherPriv *priv;
1935 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1936 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1937 g_return_val_if_fail (channel_type != NULL, NULL);
1938 g_return_val_if_fail (handle_type != 0, NULL);
1940 priv = GET_PRIV (self);
1942 va_start (var_args, first_property_name);
1944 properties = setup_varargs (var_args, channel_type, first_property_name);
1948 retval = empathy_dispatcher_find_channel_classes (self, connection,
1949 channel_type, handle_type, properties);
1951 if (properties != NULL)
1953 /* free the properties array */
1954 for (idx = 0; idx < properties->len ; idx++)
1956 str = g_array_index (properties, char *, idx);
1960 g_array_free (properties, TRUE);
1967 * empathy_dispatcher_find_requestable_channel_classes_async:
1968 * @dispatcher: an #EmpathyDispatcher
1969 * @connection: a #TpConnection
1970 * @channel_type: a string identifying the type of the channel to lookup
1971 * @handle_type: the handle type for the channel
1972 * @callback: the callback to call when @connection is ready
1973 * @user_data: the user data to pass to @callback
1974 * @first_property_name: %NULL, or the name of the first fixed property,
1975 * followed optionally by more names, followed by %NULL.
1977 * Please see the documentation of
1978 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
1979 * description of this function.
1982 empathy_dispatcher_find_requestable_channel_classes_async
1983 (EmpathyDispatcher *self,
1984 TpConnection *connection,
1985 const gchar *channel_type,
1987 EmpathyDispatcherFindChannelClassCb callback,
1989 const char *first_property_name,
1994 FindChannelRequest *request;
1995 EmpathyDispatcherPriv *priv;
1998 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1999 g_return_if_fail (TP_IS_CONNECTION (connection));
2000 g_return_if_fail (channel_type != NULL);
2001 g_return_if_fail (handle_type != 0);
2003 priv = GET_PRIV (self);
2005 va_start (var_args, first_property_name);
2007 properties = setup_varargs (var_args, channel_type, first_property_name);
2011 /* append another request for this connection */
2012 request = g_slice_new0 (FindChannelRequest);
2013 request->dispatcher = g_object_ref (self);
2014 request->channel_type = g_strdup (channel_type);
2015 request->handle_type = handle_type;
2016 request->connection = connection;
2017 request->callback = callback;
2018 request->user_data = user_data;
2019 request->properties = properties;
2021 source_id = g_idle_add (find_channel_class_idle_cb, request);
2023 g_hash_table_insert (priv->request_channel_class_async_ids,
2024 request, GUINT_TO_POINTER (source_id));
2028 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2031 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2032 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2034 return priv->channels;
2038 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2039 const gchar *account_path,
2040 const gchar *connection_path,
2041 const GPtrArray *channels,
2042 const GPtrArray *requests_satisfied,
2044 GHashTable *handler_info,
2048 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2049 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2052 TpConnection *connection;
2054 /* FIXME: should probably find out whether the account manager is prepared
2055 * before ensuring. See bug #600111. */
2056 account = tp_account_manager_ensure_account (priv->account_manager,
2058 g_assert (account != NULL);
2060 connection = tp_account_ensure_connection (account, connection_path);
2061 if (connection == NULL)
2063 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2064 "Invalid connection argument");
2068 for (i = 0; i < channels->len ; i++)
2070 GValueArray *arr = g_ptr_array_index (channels, i);
2071 const gchar *object_path;
2072 GHashTable *properties;
2074 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2075 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2077 dispatcher_connection_new_channel_with_properties (self,
2078 connection, object_path, properties);
2086 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2091 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2092 EmpathyHandler *handler;
2094 handler = empathy_handler_new (name, filters, capabilities);
2095 priv->handlers = g_list_prepend (priv->handlers, handler);
2097 /* Only set the handle_channels function, the Channel property on the main
2098 * handler will always report all dispatched channels even if they came from
2099 * a different Handler */
2100 empathy_handler_set_handle_channels_func (handler,
2101 empathy_dispatcher_handle_channels, self);
2107 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2108 EmpathyHandler *handler)
2110 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2113 h = g_list_find (priv->handlers, handler);
2114 g_return_if_fail (h != NULL);
2116 priv->handlers = g_list_delete_link (priv->handlers, h);
2118 g_object_unref (handler);