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;
67 /* channels which the dispatcher is listening "invalidated" */
72 EmpathyHandler *handler;
77 GHashTable *request_channel_class_async_ids;
78 /* (TpAccount *) => gulong
79 * Signal handler ID of the "status-changed" signal */
80 GHashTable *status_changed_handlers;
81 } EmpathyDispatcherPriv;
84 empathy_dispatcher_get_channels (EmpathyHandler *handler,
88 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
89 const gchar *account_path,
90 const gchar *connection_path,
91 const GPtrArray *channels,
92 const GPtrArray *requests_satisfied,
94 GHashTable *handler_info,
98 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
114 static guint signals[LAST_SIGNAL];
115 static EmpathyDispatcher *dispatcher = NULL;
117 static void dispatcher_init_connection_if_needed (
118 EmpathyDispatcher *dispatcher,
119 TpConnection *connection);
121 static GList * empathy_dispatcher_find_channel_classes
122 (EmpathyDispatcher *dispatcher, TpConnection *connection,
123 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
128 EmpathyDispatcher *dispatcher;
129 EmpathyDispatchOperation *operation;
130 TpConnection *connection;
131 gboolean should_ensure;
135 EmpathyContact *contact;
136 TpProxyPendingCall *pending_call;
138 /* Properties to pass to the channel when requesting it */
140 EmpathyDispatcherRequestCb *cb;
142 gpointer *request_data;
143 } DispatcherRequestData;
148 /* Channel type specific wrapper object */
149 GObject *channel_wrapper;
154 /* ObjectPath => DispatchData.. */
155 GHashTable *dispatched_channels;
156 /* ObjectPath -> EmpathyDispatchOperations */
157 GHashTable *dispatching_channels;
159 /* ObjectPath -> EmpathyDispatchOperations
161 * This holds channels which were announced with NewChannel while we have an
162 * outstanding channel request for a channel of this type. On the Requests
163 * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
164 * return before NewChannels is emitted, but there was no guarantee of the
165 * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
166 * held in limbo here until we know whether they were requested.
168 GHashTable *outstanding_channels;
169 /* List of DispatcherRequestData */
170 GList *outstanding_requests;
171 /* List of requestable channel classes */
172 GPtrArray *requestable_channels;
177 EmpathyDispatcher *dispatcher;
178 TpConnection *connection;
182 EmpathyDispatcherFindChannelClassCb *callback;
184 } FindChannelRequest;
187 empathy_dispatcher_call_create_or_ensure_channel (
188 EmpathyDispatcher *dispatcher,
189 DispatcherRequestData *request_data);
191 static DispatchData *
192 new_dispatch_data (TpChannel *channel,
193 GObject *channel_wrapper)
195 DispatchData *d = g_slice_new0 (DispatchData);
196 d->channel = g_object_ref (channel);
197 if (channel_wrapper != NULL)
198 d->channel_wrapper = g_object_ref (channel_wrapper);
204 free_dispatch_data (DispatchData *data)
206 g_object_unref (data->channel);
207 if (data->channel_wrapper != NULL)
208 g_object_unref (data->channel_wrapper);
210 g_slice_free (DispatchData, data);
213 static DispatcherRequestData *
214 new_dispatcher_request_data (EmpathyDispatcher *self,
215 TpConnection *connection,
216 const gchar *channel_type,
220 EmpathyContact *contact,
221 EmpathyDispatcherRequestCb *cb,
224 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
226 result->dispatcher = g_object_ref (self);
227 result->connection = connection;
229 result->should_ensure = FALSE;
231 result->channel_type = g_strdup (channel_type);
232 result->handle_type = handle_type;
233 result->handle = handle;
234 result->request = request;
237 result->contact = g_object_ref (contact);
240 result->user_data = user_data;
246 free_dispatcher_request_data (DispatcherRequestData *r)
248 g_free (r->channel_type);
250 if (r->dispatcher != NULL)
251 g_object_unref (r->dispatcher);
253 if (r->contact != NULL)
254 g_object_unref (r->contact);
256 if (r->request != NULL)
257 g_hash_table_unref (r->request);
259 if (r->pending_call != NULL)
260 tp_proxy_pending_call_cancel (r->pending_call);
262 g_slice_free (DispatcherRequestData, r);
265 static ConnectionData *
266 new_connection_data (void)
268 ConnectionData *cd = g_slice_new0 (ConnectionData);
270 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
271 g_free, (GDestroyNotify) free_dispatch_data);
273 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
274 g_free, g_object_unref);
276 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
283 free_connection_data (ConnectionData *cd)
288 g_hash_table_destroy (cd->dispatched_channels);
289 g_hash_table_destroy (cd->dispatching_channels);
291 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
293 free_dispatcher_request_data (l->data);
296 if (cd->requestable_channels != NULL)
298 for (i = 0 ; i < cd->requestable_channels->len ; i++)
300 g_ptr_array_index (cd->requestable_channels, i));
301 g_ptr_array_free (cd->requestable_channels, TRUE);
306 free_find_channel_request (FindChannelRequest *r)
311 g_object_unref (r->dispatcher);
312 g_free (r->channel_type);
314 if (r->properties != NULL)
316 for (idx = 0; idx < r->properties->len ; idx++)
318 str = g_array_index (r->properties, char *, idx);
322 g_array_free (r->properties, TRUE);
325 g_slice_free (FindChannelRequest, r);
329 dispatcher_connection_invalidated_cb (TpConnection *connection,
333 EmpathyDispatcher *self)
335 EmpathyDispatcherPriv *priv = GET_PRIV (self);
337 DEBUG ("Error: %s", message);
338 g_hash_table_remove (priv->connections, connection);
342 dispatcher_operation_can_start (EmpathyDispatcher *self,
343 EmpathyDispatchOperation *operation,
347 const gchar *channel_type =
348 empathy_dispatch_operation_get_channel_type (operation);
350 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
352 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
354 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
364 dispatch_operation_flush_requests (EmpathyDispatcher *self,
365 EmpathyDispatchOperation *operation,
371 l = cd->outstanding_requests;
374 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
379 if (d->operation == operation)
384 d->cb (NULL, error, d->user_data);
386 d->cb (operation, NULL, d->user_data);
389 cd->outstanding_requests = g_list_delete_link
390 (cd->outstanding_requests, lt);
392 free_dispatcher_request_data (d);
398 dispatcher_channel_invalidated_cb (TpProxy *proxy,
402 EmpathyDispatcher *self)
404 /* Channel went away... */
405 EmpathyDispatcherPriv *priv = GET_PRIV (self);
406 TpConnection *connection;
407 EmpathyDispatchOperation *operation;
409 const gchar *object_path;
411 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
413 cd = g_hash_table_lookup (priv->connections, connection);
414 /* Connection itself invalidated? */
418 object_path = tp_proxy_get_object_path (proxy);
420 DEBUG ("Channel %s invalidated", object_path);
422 g_hash_table_remove (cd->dispatched_channels, object_path);
423 g_hash_table_remove (cd->dispatching_channels, object_path);
425 priv->channels = g_list_remove (priv->channels, proxy);
427 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
428 if (operation != NULL)
430 GError error = { domain, code, message };
431 dispatch_operation_flush_requests (self, operation, &error, cd);
432 g_hash_table_remove (cd->outstanding_channels, object_path);
433 g_object_unref (operation);
438 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
439 EmpathyDispatcher *self)
441 g_assert (empathy_dispatch_operation_is_incoming (operation));
442 DEBUG ("Send of for dispatching: %s",
443 empathy_dispatch_operation_get_object_path (operation));
444 g_signal_emit (self, signals[DISPATCH], 0, operation);
448 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
449 EmpathyDispatcher *self)
451 /* Our job is done, remove the dispatch operation and mark the channel as
453 EmpathyDispatcherPriv *priv = GET_PRIV (self);
454 TpConnection *connection;
456 const gchar *object_path;
458 connection = empathy_dispatch_operation_get_tp_connection (operation);
459 cd = g_hash_table_lookup (priv->connections, connection);
460 g_assert (cd != NULL);
462 object_path = empathy_dispatch_operation_get_object_path (operation);
464 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
467 d = new_dispatch_data (
468 empathy_dispatch_operation_get_channel (operation),
469 empathy_dispatch_operation_get_channel_wrapper (operation));
470 g_hash_table_insert (cd->dispatched_channels,
471 g_strdup (object_path), d);
473 g_hash_table_remove (cd->dispatching_channels, object_path);
475 DEBUG ("Channel claimed: %s", object_path);
479 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
480 EmpathyDispatcher *self)
482 EmpathyDispatcherPriv *priv = GET_PRIV (self);
483 TpConnection *connection;
485 EmpathyDispatchOperationState status;
487 g_signal_connect (operation, "approved",
488 G_CALLBACK (dispatch_operation_approved_cb), self);
490 g_signal_connect (operation, "claimed",
491 G_CALLBACK (dispatch_operation_claimed_cb), self);
493 /* Signal the observers */
494 DEBUG ("Send to observers: %s",
495 empathy_dispatch_operation_get_object_path (operation));
496 g_signal_emit (self, signals[OBSERVE], 0, operation);
498 empathy_dispatch_operation_start (operation);
500 /* Signal potential requestors */
501 connection = empathy_dispatch_operation_get_tp_connection (operation);
502 cd = g_hash_table_lookup (priv->connections, connection);
503 g_assert (cd != NULL);
505 g_object_ref (operation);
508 dispatch_operation_flush_requests (self, operation, NULL, cd);
509 status = empathy_dispatch_operation_get_status (operation);
510 g_object_unref (operation);
512 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
515 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
517 DEBUG ("Send to approvers: %s",
518 empathy_dispatch_operation_get_object_path (operation));
519 g_signal_emit (self, signals[APPROVE], 0, operation);
523 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
524 DEBUG ("Send of for dispatching: %s",
525 empathy_dispatch_operation_get_object_path (operation));
526 g_signal_emit (self, signals[DISPATCH], 0, operation);
529 g_object_unref (self);
533 dispatcher_start_dispatching (EmpathyDispatcher *self,
534 EmpathyDispatchOperation *operation,
537 const gchar *object_path =
538 empathy_dispatch_operation_get_object_path (operation);
540 DEBUG ("Dispatching process started for %s", object_path);
542 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
544 g_assert (g_hash_table_lookup (cd->outstanding_channels,
545 object_path) == NULL);
547 g_hash_table_insert (cd->dispatching_channels,
548 g_strdup (object_path), operation);
550 switch (empathy_dispatch_operation_get_status (operation))
552 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
553 g_signal_connect (operation, "ready",
554 G_CALLBACK (dispatch_operation_ready_cb), self);
556 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
557 dispatch_operation_ready_cb (operation, self);
560 g_assert_not_reached ();
564 else if (empathy_dispatch_operation_get_status (operation) >=
565 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
567 /* Already dispatching and the operation is pending, thus the observers
568 * have seen it (if applicable), so we can flush the request right away.
570 dispatch_operation_flush_requests (self, operation, NULL, cd);
575 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
581 g_hash_table_iter_init (&iter, cd->outstanding_channels);
582 while (g_hash_table_iter_next (&iter, NULL, &value))
584 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
586 if (dispatcher_operation_can_start (self, operation, cd))
588 g_hash_table_iter_remove (&iter);
589 dispatcher_start_dispatching (self, operation, cd);
595 dispatcher_connection_new_channel (EmpathyDispatcher *self,
596 TpConnection *connection,
597 const gchar *object_path,
598 const gchar *channel_type,
601 GHashTable *properties,
604 EmpathyDispatcherPriv *priv = GET_PRIV (self);
607 EmpathyDispatchOperation *operation;
609 /* Channel types we never want to dispatch because they're either deprecated
610 * or can't sensibly be dispatch (e.g. channels that should always be
612 const char *blacklist[] = {
613 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
614 TP_IFACE_CHANNEL_TYPE_TUBES,
615 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
619 dispatcher_init_connection_if_needed (self, connection);
621 cd = g_hash_table_lookup (priv->connections, connection);
623 /* Don't bother with channels we have already dispatched or are dispatching
624 * currently. This can happen when NewChannel(s) is fired after
625 * RequestChannel/CreateChannel/EnsureChannel */
626 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
629 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
632 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
634 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
637 /* Only pick up non-requested text and file channels. For all other it
638 * doesn't make sense to handle it if we didn't request it. The same goes
639 * for channels we discovered by the Channels property or ListChannels */
640 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
641 && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
643 DEBUG ("Ignoring incoming channel of type %s on %s",
644 channel_type, object_path);
648 for (i = 0 ; blacklist[i] != NULL; i++)
650 if (!tp_strdiff (channel_type, blacklist[i]))
652 DEBUG ("Ignoring blacklisted channel type %s on %s",
653 channel_type, object_path);
658 DEBUG ("New channel of type %s on %s", channel_type, object_path);
660 if (properties == NULL)
661 channel = tp_channel_new (connection, object_path, channel_type,
662 handle_type, handle, NULL);
664 channel = tp_channel_new_from_properties (connection, object_path,
667 g_signal_connect (channel, "invalidated",
668 G_CALLBACK (dispatcher_channel_invalidated_cb),
671 priv->channels = g_list_prepend (priv->channels, channel);
673 operation = empathy_dispatch_operation_new (connection, channel, NULL,
676 g_object_unref (channel);
680 /* Request could either be by us or by a remote party. If there are no
681 * outstanding requests for this channel type we can assume it's remote.
682 * Otherwise we wait untill they are all satisfied */
683 if (dispatcher_operation_can_start (self, operation, cd))
684 dispatcher_start_dispatching (self, operation, cd);
686 g_hash_table_insert (cd->outstanding_channels,
687 g_strdup (object_path), operation);
691 dispatcher_start_dispatching (self, operation, cd);
696 dispatcher_connection_new_channel_with_properties (
697 EmpathyDispatcher *self,
698 TpConnection *connection,
699 const gchar *object_path,
700 GHashTable *properties)
702 const gchar *channel_type;
709 channel_type = tp_asv_get_string (properties,
710 TP_IFACE_CHANNEL ".ChannelType");
711 if (channel_type == NULL)
713 g_message ("%s had an invalid ChannelType property", object_path);
717 handle_type = tp_asv_get_uint32 (properties,
718 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
721 g_message ("%s had an invalid TargetHandleType property", object_path);
725 handle = tp_asv_get_uint32 (properties,
726 TP_IFACE_CHANNEL ".TargetHandle", &valid);
729 g_message ("%s had an invalid TargetHandle property", object_path);
733 /* We assume there is no channel dispather, so we're the only one dispatching
734 * it. Which means that a requested channel it is outgoing one */
735 requested = tp_asv_get_boolean (properties,
736 TP_IFACE_CHANNEL ".Requested", &valid);
739 g_message ("%s had an invalid Requested property", object_path);
743 dispatcher_connection_new_channel (self, connection,
744 object_path, channel_type, handle_type, handle, properties, !requested);
748 dispatcher_connection_got_all (TpProxy *proxy,
749 GHashTable *properties,
754 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
755 EmpathyDispatcherPriv *priv = GET_PRIV (self);
756 GPtrArray *requestable_channels;
757 GPtrArray *existing_channels;
760 DEBUG ("Error: %s", error->message);
764 requestable_channels = tp_asv_get_boxed (properties,
765 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
767 if (requestable_channels == NULL)
768 DEBUG ("No RequestableChannelClasses property !?! on connection");
773 FindChannelRequest *request;
776 cd = g_hash_table_lookup (priv->connections, proxy);
777 g_assert (cd != NULL);
779 cd->requestable_channels = g_boxed_copy (
780 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
782 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
785 for (l = requests; l != NULL; l = l->next)
789 retval = empathy_dispatcher_find_channel_classes (self,
790 TP_CONNECTION (proxy), request->channel_type,
791 request->handle_type, request->properties);
792 request->callback (retval, request->user_data);
794 free_find_channel_request (request);
795 g_list_free (retval);
798 g_list_free (requests);
800 g_hash_table_remove (priv->outstanding_classes_requests, proxy);
803 existing_channels = tp_asv_get_boxed (properties,
804 "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
806 if (existing_channels != NULL)
810 for (idx = 0; idx < existing_channels->len; idx++)
812 GValueArray *values = g_ptr_array_index (existing_channels, idx);
813 const gchar *object_path;
816 object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
817 props = g_value_get_boxed (g_value_array_get_nth (values, 1));
819 if (tp_strdiff (tp_asv_get_string (props,
820 TP_IFACE_CHANNEL ".ChannelType"),
821 TP_IFACE_CHANNEL_TYPE_TEXT))
824 dispatcher_connection_new_channel_with_properties (self,
825 TP_CONNECTION (proxy), object_path, props);
831 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
832 const GPtrArray *capabilities,
838 DEBUG ("Error: %s", error->message);
842 connection_ready_cb (TpConnection *connection,
846 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
847 GPtrArray *capabilities;
850 const gchar *remove_ = NULL;
854 DEBUG ("Error: %s", error->message);
858 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
859 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
861 tp_cli_dbus_properties_call_get_all (connection, -1,
862 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
863 dispatcher_connection_got_all,
864 NULL, NULL, G_OBJECT (self));
867 /* Advertise VoIP capabilities */
868 capabilities = g_ptr_array_sized_new (1);
869 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
870 G_TYPE_UINT, G_TYPE_INVALID);
871 g_value_init (&cap, cap_type);
872 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
873 dbus_g_type_struct_set (&cap,
874 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
875 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
876 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
877 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
878 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
879 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
881 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
883 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
884 connection, -1, capabilities, &remove_,
885 dispatcher_connection_advertise_capabilities_cb,
886 NULL, NULL, G_OBJECT (self));
888 g_value_unset (&cap);
889 g_ptr_array_free (capabilities, TRUE);
891 g_object_unref (self);
895 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
896 TpConnection *connection)
898 EmpathyDispatcherPriv *priv = GET_PRIV (self);
900 if (g_hash_table_lookup (priv->connections, connection) != NULL)
903 g_hash_table_insert (priv->connections, g_object_ref (connection),
904 new_connection_data ());
906 g_signal_connect (connection, "invalidated",
907 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
909 /* Ensure to keep the self object alive while the call_when_ready is
912 tp_connection_call_when_ready (connection, connection_ready_cb, self);
916 dispatcher_status_changed_cb (TpAccount *account,
920 gchar *dbus_error_name,
922 EmpathyDispatcher *self)
924 TpConnection *conn = tp_account_get_connection (account);
927 dispatcher_init_connection_if_needed (self, conn);
931 remove_idle_handlers (gpointer key,
937 source_id = GPOINTER_TO_UINT (value);
938 g_source_remove (source_id);
942 dispatcher_constructor (GType type,
943 guint n_construct_params,
944 GObjectConstructParam *construct_params)
947 EmpathyDispatcherPriv *priv;
949 if (dispatcher != NULL)
950 return g_object_ref (dispatcher);
952 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
953 (type, n_construct_params, construct_params);
955 dispatcher = EMPATHY_DISPATCHER (retval);
956 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
958 priv = GET_PRIV (dispatcher);
960 if (priv->handler == NULL)
961 priv->handler = empathy_handler_new (NULL, NULL, NULL);
963 empathy_handler_set_handle_channels_func (priv->handler,
964 empathy_dispatcher_handle_channels,
967 empathy_handler_set_channels_func (priv->handler,
968 empathy_dispatcher_get_channels,
975 dispatcher_dispose (GObject *object)
977 EmpathyDispatcherPriv *priv = GET_PRIV (object);
982 if (priv->dispose_has_run)
985 priv->dispose_has_run = TRUE;
987 for (l = priv->handlers ; l != NULL; l = g_list_next (l))
988 g_object_unref (G_OBJECT (l->data));
990 g_list_free (priv->handlers);
991 priv->handlers = NULL;
993 if (priv->handler != NULL)
994 g_object_unref (priv->handler);
995 priv->handler = NULL;
997 g_hash_table_iter_init (&iter, priv->connections);
998 while (g_hash_table_iter_next (&iter, &connection, NULL))
1000 g_signal_handlers_disconnect_by_func (connection,
1001 dispatcher_connection_invalidated_cb, object);
1004 g_hash_table_destroy (priv->connections);
1005 priv->connections = NULL;
1007 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
1011 dispatcher_finalize (GObject *object)
1013 EmpathyDispatcherPriv *priv = GET_PRIV (object);
1015 GHashTableIter iter;
1016 gpointer connection;
1018 gpointer account, id;
1020 if (priv->request_channel_class_async_ids != NULL)
1022 g_hash_table_foreach (priv->request_channel_class_async_ids,
1023 remove_idle_handlers, NULL);
1024 g_hash_table_destroy (priv->request_channel_class_async_ids);
1027 for (l = priv->channels; l; l = l->next)
1029 g_signal_handlers_disconnect_by_func (l->data,
1030 dispatcher_channel_invalidated_cb, object);
1033 g_list_free (priv->channels);
1035 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1036 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1038 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1042 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1043 while (g_hash_table_iter_next (&iter, &account, &id))
1045 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1047 g_hash_table_destroy (priv->status_changed_handlers);
1049 g_object_unref (priv->account_manager);
1051 g_hash_table_destroy (priv->outstanding_classes_requests);
1055 dispatcher_set_property (GObject *object,
1057 const GValue *value,
1060 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1061 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1063 switch (property_id)
1066 priv->handler = g_value_dup_object (value);
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1075 dispatcher_get_property (GObject *object,
1080 EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1081 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1083 switch (property_id)
1086 g_value_set_object (value, priv->handler);
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1095 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1097 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1098 GParamSpec *param_spec;
1100 object_class->dispose = dispatcher_dispose;
1101 object_class->finalize = dispatcher_finalize;
1102 object_class->constructor = dispatcher_constructor;
1104 object_class->get_property = dispatcher_get_property;
1105 object_class->set_property = dispatcher_set_property;
1107 param_spec = g_param_spec_object ("handler", "handler",
1108 "The main Telepathy Client Hander object",
1109 EMPATHY_TYPE_HANDLER,
1110 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1111 g_object_class_install_property (object_class,
1112 PROP_HANDLER, param_spec);
1115 g_signal_new ("observe",
1116 G_TYPE_FROM_CLASS (klass),
1120 g_cclosure_marshal_VOID__OBJECT,
1122 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1125 g_signal_new ("approve",
1126 G_TYPE_FROM_CLASS (klass),
1130 g_cclosure_marshal_VOID__OBJECT,
1132 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1135 g_signal_new ("dispatch",
1136 G_TYPE_FROM_CLASS (klass),
1140 g_cclosure_marshal_VOID__OBJECT,
1142 1, EMPATHY_TYPE_DISPATCH_OPERATION);
1145 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1149 connect_account (EmpathyDispatcher *self,
1152 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1153 TpConnection *conn = tp_account_get_connection (account);
1156 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1163 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1165 id = g_signal_connect (account, "status-changed",
1166 G_CALLBACK (dispatcher_status_changed_cb), self);
1168 g_hash_table_insert (priv->status_changed_handlers, account,
1169 GUINT_TO_POINTER (id));
1173 account_manager_prepared_cb (GObject *source_object,
1174 GAsyncResult *result,
1177 GList *accounts, *l;
1178 EmpathyDispatcher *self = user_data;
1179 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1180 GError *error = NULL;
1182 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1184 DEBUG ("Failed to prepare account manager: %s", error->message);
1185 g_error_free (error);
1189 accounts = tp_account_manager_get_valid_accounts (account_manager);
1190 for (l = accounts; l; l = l->next)
1192 TpAccount *a = l->data;
1194 connect_account (self, a);
1196 g_list_free (accounts);
1200 account_prepare_cb (GObject *source_object,
1201 GAsyncResult *result,
1204 EmpathyDispatcher *self = user_data;
1205 TpAccount *account = TP_ACCOUNT (source_object);
1206 GError *error = NULL;
1208 if (!tp_account_prepare_finish (account, result, &error))
1210 DEBUG ("Failed to prepare account: %s", error->message);
1211 g_error_free (error);
1215 connect_account (self, account);
1219 account_validity_changed_cb (TpAccountManager *manager,
1227 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1231 empathy_dispatcher_init (EmpathyDispatcher *self)
1233 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1234 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1237 priv->account_manager = tp_account_manager_dup ();
1239 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1240 g_object_unref, (GDestroyNotify) free_connection_data);
1242 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1243 g_direct_equal, g_object_unref, NULL);
1245 priv->channels = NULL;
1247 tp_account_manager_prepare_async (priv->account_manager, NULL,
1248 account_manager_prepared_cb, self);
1250 empathy_signal_connect_weak (priv->account_manager,
1251 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1254 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1256 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1261 empathy_dispatcher_new (const gchar *name,
1265 EmpathyHandler *handler;
1266 EmpathyDispatcher *ret;
1268 g_assert (dispatcher == NULL);
1269 handler = empathy_handler_new (name, filters, capabilities);
1271 ret = EMPATHY_DISPATCHER (
1272 g_object_new (EMPATHY_TYPE_DISPATCHER,
1275 g_object_unref (handler);
1281 empathy_dispatcher_dup_singleton (void)
1283 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1287 dispatcher_request_failed (EmpathyDispatcher *self,
1288 DispatcherRequestData *request_data,
1289 const GError *error)
1291 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1292 ConnectionData *conn_data;
1294 conn_data = g_hash_table_lookup (priv->connections,
1295 request_data->connection);
1296 if (request_data->cb != NULL)
1297 request_data->cb (NULL, error, request_data->user_data);
1299 if (conn_data != NULL)
1301 conn_data->outstanding_requests =
1302 g_list_remove (conn_data->outstanding_requests, request_data);
1304 /* else Connection has been invalidated */
1306 free_dispatcher_request_data (request_data);
1310 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1311 DispatcherRequestData *request_data,
1312 const gchar *object_path,
1313 GHashTable *properties,
1314 const GError *error)
1316 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1317 EmpathyDispatchOperation *operation = NULL;
1318 ConnectionData *conn_data;
1320 conn_data = g_hash_table_lookup (priv->connections,
1321 request_data->connection);
1325 DEBUG ("Channel request failed: %s", error->message);
1327 dispatcher_request_failed (self, request_data, error);
1332 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1335 if (operation != NULL)
1336 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1338 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1341 if (operation == NULL)
1343 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1348 operation = empathy_dispatch_operation_new_with_wrapper (
1349 request_data->connection,
1350 data->channel, request_data->contact, FALSE,
1351 data->channel_wrapper);
1357 if (properties != NULL)
1358 channel = tp_channel_new_from_properties (request_data->connection,
1359 object_path, properties, NULL);
1361 channel = tp_channel_new (request_data->connection, object_path,
1362 request_data->channel_type, request_data->handle_type,
1363 request_data->handle, NULL);
1365 g_signal_connect (channel, "invalidated",
1366 G_CALLBACK (dispatcher_channel_invalidated_cb),
1367 request_data->dispatcher);
1369 priv->channels = g_list_prepend (priv->channels, channel);
1371 operation = empathy_dispatch_operation_new (request_data->connection,
1372 channel, request_data->contact, FALSE);
1373 g_object_unref (channel);
1378 /* Already existed set potential extra information */
1379 g_object_set (G_OBJECT (operation),
1380 "contact", request_data->contact,
1384 request_data->operation = operation;
1386 /* (pre)-approve this right away as we requested it
1387 * This might cause the channel to be claimed, in which case the operation
1388 * will disappear. So ref it, and check the status before starting the
1391 g_object_ref (operation);
1392 empathy_dispatch_operation_approve (operation);
1394 if (empathy_dispatch_operation_get_status (operation) <
1395 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1396 dispatcher_start_dispatching (request_data->dispatcher, operation,
1399 g_object_unref (operation);
1402 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1407 dispatcher_request_channel_cb (TpConnection *connection,
1408 const gchar *object_path,
1409 const GError *error,
1411 GObject *weak_object)
1413 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1414 EmpathyDispatcher *self =
1415 EMPATHY_DISPATCHER (request_data->dispatcher);
1417 request_data->pending_call = NULL;
1419 dispatcher_connection_new_requested_channel (self,
1420 request_data, object_path, NULL, error);
1424 dispatcher_request_channel (DispatcherRequestData *request_data)
1426 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1427 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1429 /* Extend the request_data to be a valid request */
1430 g_assert (request_data->request == NULL);
1431 request_data->request = tp_asv_new (
1432 TP_IFACE_CHANNEL ".ChannelType",
1433 G_TYPE_STRING, request_data->channel_type,
1434 TP_IFACE_CHANNEL ".TargetHandleType",
1435 G_TYPE_UINT, request_data->handle_type,
1438 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1439 tp_asv_set_uint32 (request_data->request,
1440 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1442 empathy_dispatcher_call_create_or_ensure_channel (
1443 request_data->dispatcher, request_data);
1447 request_data->pending_call = tp_cli_connection_call_request_channel (
1448 request_data->connection, -1,
1449 request_data->channel_type,
1450 request_data->handle_type,
1451 request_data->handle,
1452 TRUE, dispatcher_request_channel_cb,
1453 request_data, NULL, NULL);
1458 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1459 EmpathyDispatcherRequestCb *callback,
1462 EmpathyDispatcher *self;
1463 EmpathyDispatcherPriv *priv;
1464 TpConnection *connection;
1465 ConnectionData *connection_data;
1466 DispatcherRequestData *request_data;
1468 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1470 self = empathy_dispatcher_dup_singleton ();
1471 priv = GET_PRIV (self);
1473 connection = empathy_contact_get_connection (contact);
1474 connection_data = g_hash_table_lookup (priv->connections, connection);
1476 /* The contact handle might not be known yet */
1477 request_data = new_dispatcher_request_data (self, connection,
1478 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1479 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1480 request_data->should_ensure = TRUE;
1482 connection_data->outstanding_requests = g_list_prepend
1483 (connection_data->outstanding_requests, request_data);
1485 dispatcher_request_channel (request_data);
1487 g_object_unref (self);
1492 EmpathyDispatcher *dispatcher;
1493 EmpathyDispatcherRequestCb *callback;
1495 } ChatWithContactIdData;
1498 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1499 EmpathyContact *contact,
1500 const GError *error,
1502 GObject *weak_object)
1504 ChatWithContactIdData *data = user_data;
1508 /* FIXME: Should call data->callback with the error */
1509 DEBUG ("Error: %s", error->message);
1513 empathy_dispatcher_chat_with_contact (contact, data->callback,
1517 g_object_unref (data->dispatcher);
1518 g_slice_free (ChatWithContactIdData, data);
1522 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1523 const gchar *contact_id,
1524 EmpathyDispatcherRequestCb *callback,
1527 EmpathyDispatcher *self;
1528 EmpathyTpContactFactory *factory;
1529 ChatWithContactIdData *data;
1531 g_return_if_fail (TP_IS_CONNECTION (connection));
1532 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1534 self = empathy_dispatcher_dup_singleton ();
1535 factory = empathy_tp_contact_factory_dup_singleton (connection);
1536 data = g_slice_new0 (ChatWithContactIdData);
1537 data->dispatcher = self;
1538 data->callback = callback;
1539 data->user_data = user_data;
1540 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1541 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1543 g_object_unref (factory);
1547 dispatcher_request_handles_cb (TpConnection *connection,
1548 const GArray *handles,
1549 const GError *error,
1553 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1555 request_data->pending_call = NULL;
1559 EmpathyDispatcher *self = request_data->dispatcher;
1560 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1563 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1565 if (request_data->cb)
1566 request_data->cb (NULL, error, request_data->user_data);
1568 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1571 free_dispatcher_request_data (request_data);
1573 dispatcher_flush_outstanding_operations (self, cd);
1577 request_data->handle = g_array_index (handles, guint, 0);
1578 dispatcher_request_channel (request_data);
1582 empathy_dispatcher_join_muc (TpConnection *connection,
1583 const gchar *roomname,
1584 EmpathyDispatcherRequestCb *callback,
1587 EmpathyDispatcher *self;
1588 EmpathyDispatcherPriv *priv;
1589 DispatcherRequestData *request_data;
1590 ConnectionData *connection_data;
1591 const gchar *names[] = { roomname, NULL };
1593 g_return_if_fail (TP_IS_CONNECTION (connection));
1594 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1596 self = empathy_dispatcher_dup_singleton ();
1597 priv = GET_PRIV (self);
1599 connection_data = g_hash_table_lookup (priv->connections, connection);
1600 g_assert (connection_data != NULL);
1602 /* Don't know the room handle yet */
1603 request_data = new_dispatcher_request_data (self, connection,
1604 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1605 NULL, callback, user_data);
1606 request_data->should_ensure = TRUE;
1608 connection_data->outstanding_requests = g_list_prepend
1609 (connection_data->outstanding_requests, request_data);
1611 request_data->pending_call = tp_cli_connection_call_request_handles (
1613 TP_HANDLE_TYPE_ROOM, names,
1614 dispatcher_request_handles_cb, request_data, NULL, NULL);
1616 g_object_unref (self);
1620 dispatcher_create_channel_cb (TpConnection *connect,
1621 const gchar *object_path,
1622 GHashTable *properties,
1623 const GError *error,
1625 GObject *weak_object)
1627 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1628 EmpathyDispatcher *self =
1629 EMPATHY_DISPATCHER (request_data->dispatcher);
1631 request_data->pending_call = NULL;
1633 dispatcher_connection_new_requested_channel (self,
1634 request_data, object_path, properties, error);
1638 dispatcher_ensure_channel_cb (TpConnection *connect,
1640 const gchar *object_path,
1641 GHashTable *properties,
1642 const GError *error,
1644 GObject *weak_object)
1646 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1647 EmpathyDispatcher *self =
1648 EMPATHY_DISPATCHER (request_data->dispatcher);
1650 request_data->pending_call = NULL;
1652 dispatcher_connection_new_requested_channel (self,
1653 request_data, object_path, properties, error);
1657 empathy_dispatcher_call_create_or_ensure_channel (
1658 EmpathyDispatcher *self,
1659 DispatcherRequestData *request_data)
1661 if (request_data->should_ensure)
1663 request_data->pending_call =
1664 tp_cli_connection_interface_requests_call_ensure_channel (
1665 request_data->connection, -1,
1666 request_data->request, dispatcher_ensure_channel_cb,
1667 request_data, NULL, NULL);
1671 request_data->pending_call =
1672 tp_cli_connection_interface_requests_call_create_channel (
1673 request_data->connection, -1,
1674 request_data->request, dispatcher_create_channel_cb,
1675 request_data, NULL, NULL);
1680 * empathy_dispatcher_create_channel:
1681 * @self: the EmpathyDispatcher
1682 * @connection: the Connection to dispatch on
1683 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1684 * @callback: a callback for when the channel arrives (or NULL)
1685 * @user_data: optional user data (or NULL)
1687 * When calling this function, #EmpathyDispatcher takes ownership of your
1688 * reference to @request. DO NOT unref or destroy @request. When the request is
1689 * done, @request will be unreferenced. Take another reference if you want to
1693 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1694 TpConnection *connection,
1695 GHashTable *request,
1696 EmpathyDispatcherRequestCb *callback,
1699 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1700 ConnectionData *connection_data;
1701 DispatcherRequestData *request_data;
1702 const gchar *channel_type;
1707 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1708 g_return_if_fail (TP_IS_CONNECTION (connection));
1709 g_return_if_fail (request != NULL);
1711 connection_data = g_hash_table_lookup (priv->connections, connection);
1712 g_assert (connection_data != NULL);
1714 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1716 handle_type = tp_asv_get_uint32 (request,
1717 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1719 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1721 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1723 request_data = new_dispatcher_request_data (self, connection,
1724 channel_type, handle_type, handle, request,
1725 NULL, callback, user_data);
1727 connection_data->outstanding_requests = g_list_prepend
1728 (connection_data->outstanding_requests, request_data);
1730 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1734 channel_class_matches (GValueArray *class,
1735 const char *channel_type,
1737 GArray *fixed_properties)
1745 v = g_value_array_get_nth (class, 0);
1747 /* if the class doesn't match channel type discard it. */
1748 fprops = g_value_get_boxed (v);
1749 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1751 if (tp_strdiff (channel_type, c_type))
1754 /* we have the right channel type, see if the handle type matches */
1755 h_type = tp_asv_get_uint32 (fprops,
1756 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1758 if (!valid || handle_type != h_type)
1761 if (fixed_properties != NULL)
1763 gpointer h_key, h_val;
1765 GHashTableIter iter;
1768 g_hash_table_iter_init (&iter, fprops);
1770 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1772 /* discard ChannelType and TargetHandleType, as we already
1775 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1777 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1782 for (idx = 0; idx < fixed_properties->len; idx++)
1784 /* if |key| doesn't exist in |fixed_properties|, discard
1789 g_array_index (fixed_properties, char *, idx)))
1792 /* exit the for() loop */
1803 /* if no fixed_properties are specified, discard the classes
1804 * with some fixed properties other than the two we already
1807 if (g_hash_table_size (fprops) > 2)
1815 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1816 TpConnection *connection,
1817 const gchar *channel_type,
1819 GArray *fixed_properties)
1821 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1824 GList *matching_classes;
1828 g_return_val_if_fail (channel_type != NULL, NULL);
1829 g_return_val_if_fail (handle_type != 0, NULL);
1831 cd = g_hash_table_lookup (priv->connections, connection);
1836 classes = cd->requestable_channels;
1837 if (classes == NULL)
1840 matching_classes = NULL;
1842 for (i = 0; i < classes->len; i++)
1844 class = g_ptr_array_index (classes, i);
1846 if (!channel_class_matches
1847 (class, channel_type, handle_type, fixed_properties))
1850 matching_classes = g_list_prepend (matching_classes, class);
1853 return matching_classes;
1857 find_channel_class_idle_cb (gpointer user_data)
1861 FindChannelRequest *request = user_data;
1863 gboolean is_ready = TRUE;
1864 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1866 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1868 cd = g_hash_table_lookup (priv->connections, request->connection);
1872 else if (cd->requestable_channels == NULL)
1877 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1878 request->connection, request->channel_type, request->handle_type,
1879 request->properties);
1881 request->callback (retval, request->user_data);
1882 free_find_channel_request (request);
1883 g_list_free (retval);
1888 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1889 request->connection);
1890 requests = g_list_prepend (requests, request);
1892 g_hash_table_insert (priv->outstanding_classes_requests,
1893 request->connection, requests);
1899 setup_varargs (va_list var_args,
1900 const char *channel_namespace,
1901 const char *first_property_name)
1907 if (first_property_name == NULL)
1910 name = first_property_name;
1911 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1913 while (name != NULL)
1915 name_full = g_strdup (name);
1916 properties = g_array_append_val (properties, name_full);
1917 name = va_arg (var_args, char *);
1924 * empathy_dispatcher_find_requestable_channel_classes:
1925 * @dispatcher: an #EmpathyDispatcher
1926 * @connection: a #TpConnection
1927 * @channel_type: a string identifying the type of the channel to lookup
1928 * @handle_type: the handle type for the channel
1929 * @first_property_name: %NULL, or the name of the first fixed property,
1930 * followed optionally by more names, followed by %NULL.
1932 * Returns all the channel classes that a client can request for the connection
1933 * @connection, of the type identified by @channel_type, @handle_type and the
1934 * fixed properties list.
1935 * The classes which are compatible with a fixed properties list (i.e. those
1936 * that will be returned by this function) are intended as those that do not
1937 * contain any fixed property other than those in the list; note that this
1938 * doesn't guarantee that all the classes compatible with the list will contain
1939 * all the requested fixed properties, so the clients will have to filter
1940 * the returned list themselves.
1941 * If @first_property_name is %NULL, only the classes with no other fixed
1942 * properties than ChannelType and TargetHandleType will be returned.
1943 * Note that this function may return %NULL without performing any lookup if
1944 * @connection is not ready. To ensure that @connection is always ready,
1945 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1947 * Return value: a #GList of #GValueArray objects, where the first element in
1948 * the array is a #GHashTable of the fixed properties, and the second is
1949 * a #GStrv of the allowed properties for the class. The list should be free'd
1950 * with g_list_free() when done, but the objects inside the list are owned
1951 * by the #EmpathyDispatcher and must not be modified.
1954 empathy_dispatcher_find_requestable_channel_classes
1955 (EmpathyDispatcher *self,
1956 TpConnection *connection,
1957 const gchar *channel_type,
1959 const char *first_property_name,
1964 EmpathyDispatcherPriv *priv;
1969 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1970 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1971 g_return_val_if_fail (channel_type != NULL, NULL);
1972 g_return_val_if_fail (handle_type != 0, NULL);
1974 priv = GET_PRIV (self);
1976 va_start (var_args, first_property_name);
1978 properties = setup_varargs (var_args, channel_type, first_property_name);
1982 retval = empathy_dispatcher_find_channel_classes (self, connection,
1983 channel_type, handle_type, properties);
1985 if (properties != NULL)
1987 /* free the properties array */
1988 for (idx = 0; idx < properties->len ; idx++)
1990 str = g_array_index (properties, char *, idx);
1994 g_array_free (properties, TRUE);
2001 * empathy_dispatcher_find_requestable_channel_classes_async:
2002 * @dispatcher: an #EmpathyDispatcher
2003 * @connection: a #TpConnection
2004 * @channel_type: a string identifying the type of the channel to lookup
2005 * @handle_type: the handle type for the channel
2006 * @callback: the callback to call when @connection is ready
2007 * @user_data: the user data to pass to @callback
2008 * @first_property_name: %NULL, or the name of the first fixed property,
2009 * followed optionally by more names, followed by %NULL.
2011 * Please see the documentation of
2012 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2013 * description of this function.
2016 empathy_dispatcher_find_requestable_channel_classes_async
2017 (EmpathyDispatcher *self,
2018 TpConnection *connection,
2019 const gchar *channel_type,
2021 EmpathyDispatcherFindChannelClassCb callback,
2023 const char *first_property_name,
2028 FindChannelRequest *request;
2029 EmpathyDispatcherPriv *priv;
2032 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2033 g_return_if_fail (TP_IS_CONNECTION (connection));
2034 g_return_if_fail (channel_type != NULL);
2035 g_return_if_fail (handle_type != 0);
2037 priv = GET_PRIV (self);
2039 va_start (var_args, first_property_name);
2041 properties = setup_varargs (var_args, channel_type, first_property_name);
2045 /* append another request for this connection */
2046 request = g_slice_new0 (FindChannelRequest);
2047 request->dispatcher = g_object_ref (self);
2048 request->channel_type = g_strdup (channel_type);
2049 request->handle_type = handle_type;
2050 request->connection = connection;
2051 request->callback = callback;
2052 request->user_data = user_data;
2053 request->properties = properties;
2055 source_id = g_idle_add (find_channel_class_idle_cb, request);
2057 g_hash_table_insert (priv->request_channel_class_async_ids,
2058 request, GUINT_TO_POINTER (source_id));
2062 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2065 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2066 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2068 return priv->channels;
2072 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2073 const gchar *account_path,
2074 const gchar *connection_path,
2075 const GPtrArray *channels,
2076 const GPtrArray *requests_satisfied,
2078 GHashTable *handler_info,
2082 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2083 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2086 TpConnection *connection;
2088 /* FIXME: should probably find out whether the account manager is prepared
2089 * before ensuring. See bug #600111. */
2090 account = tp_account_manager_ensure_account (priv->account_manager,
2092 g_assert (account != NULL);
2094 connection = tp_account_ensure_connection (account, connection_path);
2095 if (connection == NULL)
2097 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2098 "Invalid connection argument");
2102 for (i = 0; i < channels->len ; i++)
2104 GValueArray *arr = g_ptr_array_index (channels, i);
2105 const gchar *object_path;
2106 GHashTable *properties;
2108 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2109 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2111 dispatcher_connection_new_channel_with_properties (self,
2112 connection, object_path, properties);
2120 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2125 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2126 EmpathyHandler *handler;
2128 handler = empathy_handler_new (name, filters, capabilities);
2129 priv->handlers = g_list_prepend (priv->handlers, handler);
2131 /* Only set the handle_channels function, the Channel property on the main
2132 * handler will always report all dispatched channels even if they came from
2133 * a different Handler */
2134 empathy_handler_set_handle_channels_func (handler,
2135 empathy_dispatcher_handle_channels, self);
2141 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2142 EmpathyHandler *handler)
2144 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2147 h = g_list_find (priv->handlers, handler);
2148 g_return_if_fail (h != NULL);
2150 priv->handlers = g_list_delete_link (priv->handlers, h);
2152 g_object_unref (handler);