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 account_prepare_cb (GObject *source_object,
1202 GAsyncResult *result,
1205 EmpathyDispatcher *self = user_data;
1206 TpAccount *account = TP_ACCOUNT (source_object);
1207 GError *error = NULL;
1209 if (!tp_account_prepare_finish (account, result, &error))
1211 DEBUG ("Failed to prepare account: %s", error->message);
1212 g_error_free (error);
1216 connect_account (self, account);
1220 account_validity_changed_cb (TpAccountManager *manager,
1228 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1232 empathy_dispatcher_init (EmpathyDispatcher *self)
1234 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1235 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1238 priv->account_manager = tp_account_manager_dup ();
1240 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1241 g_object_unref, (GDestroyNotify) free_connection_data);
1243 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1244 g_direct_equal, g_object_unref, NULL);
1246 priv->channels = NULL;
1248 tp_account_manager_prepare_async (priv->account_manager, NULL,
1249 account_manager_prepared_cb, self);
1251 empathy_signal_connect_weak (priv->account_manager,
1252 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1255 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1257 priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1262 empathy_dispatcher_new (const gchar *name,
1266 EmpathyHandler *handler;
1267 EmpathyDispatcher *ret;
1269 g_assert (dispatcher == NULL);
1270 handler = empathy_handler_new (name, filters, capabilities);
1272 ret = EMPATHY_DISPATCHER (
1273 g_object_new (EMPATHY_TYPE_DISPATCHER,
1276 g_object_unref (handler);
1282 empathy_dispatcher_dup_singleton (void)
1284 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1288 dispatcher_request_failed (EmpathyDispatcher *self,
1289 DispatcherRequestData *request_data,
1290 const GError *error)
1292 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1293 ConnectionData *conn_data;
1295 conn_data = g_hash_table_lookup (priv->connections,
1296 request_data->connection);
1297 if (request_data->cb != NULL)
1298 request_data->cb (NULL, error, request_data->user_data);
1300 if (conn_data != NULL)
1302 conn_data->outstanding_requests =
1303 g_list_remove (conn_data->outstanding_requests, request_data);
1305 /* else Connection has been invalidated */
1307 free_dispatcher_request_data (request_data);
1311 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1312 DispatcherRequestData *request_data,
1313 const gchar *object_path,
1314 GHashTable *properties,
1315 const GError *error)
1317 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1318 EmpathyDispatchOperation *operation = NULL;
1319 ConnectionData *conn_data;
1321 conn_data = g_hash_table_lookup (priv->connections,
1322 request_data->connection);
1326 DEBUG ("Channel request failed: %s", error->message);
1328 dispatcher_request_failed (self, request_data, error);
1333 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1336 if (operation != NULL)
1337 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1339 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1342 if (operation == NULL)
1344 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1349 operation = empathy_dispatch_operation_new_with_wrapper (
1350 request_data->connection,
1351 data->channel, request_data->contact, FALSE,
1352 data->channel_wrapper);
1358 if (properties != NULL)
1359 channel = tp_channel_new_from_properties (request_data->connection,
1360 object_path, properties, NULL);
1362 channel = tp_channel_new (request_data->connection, object_path,
1363 request_data->channel_type, request_data->handle_type,
1364 request_data->handle, NULL);
1366 g_signal_connect (channel, "invalidated",
1367 G_CALLBACK (dispatcher_channel_invalidated_cb),
1368 request_data->dispatcher);
1370 priv->channels = g_list_prepend (priv->channels, channel);
1372 operation = empathy_dispatch_operation_new (request_data->connection,
1373 channel, request_data->contact, FALSE);
1374 g_object_unref (channel);
1379 /* Already existed set potential extra information */
1380 g_object_set (G_OBJECT (operation),
1381 "contact", request_data->contact,
1385 request_data->operation = operation;
1387 /* (pre)-approve this right away as we requested it
1388 * This might cause the channel to be claimed, in which case the operation
1389 * will disappear. So ref it, and check the status before starting the
1392 g_object_ref (operation);
1393 empathy_dispatch_operation_approve (operation);
1395 if (empathy_dispatch_operation_get_status (operation) <
1396 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1397 dispatcher_start_dispatching (request_data->dispatcher, operation,
1400 g_object_unref (operation);
1403 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1408 dispatcher_request_channel_cb (TpConnection *connection,
1409 const gchar *object_path,
1410 const GError *error,
1412 GObject *weak_object)
1414 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1415 EmpathyDispatcher *self =
1416 EMPATHY_DISPATCHER (request_data->dispatcher);
1418 request_data->pending_call = NULL;
1420 dispatcher_connection_new_requested_channel (self,
1421 request_data, object_path, NULL, error);
1425 dispatcher_request_channel (DispatcherRequestData *request_data)
1427 if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1428 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1430 /* Extend the request_data to be a valid request */
1431 g_assert (request_data->request == NULL);
1432 request_data->request = tp_asv_new (
1433 TP_IFACE_CHANNEL ".ChannelType",
1434 G_TYPE_STRING, request_data->channel_type,
1435 TP_IFACE_CHANNEL ".TargetHandleType",
1436 G_TYPE_UINT, request_data->handle_type,
1439 if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1440 tp_asv_set_uint32 (request_data->request,
1441 TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1443 empathy_dispatcher_call_create_or_ensure_channel (
1444 request_data->dispatcher, request_data);
1448 request_data->pending_call = tp_cli_connection_call_request_channel (
1449 request_data->connection, -1,
1450 request_data->channel_type,
1451 request_data->handle_type,
1452 request_data->handle,
1453 TRUE, dispatcher_request_channel_cb,
1454 request_data, NULL, NULL);
1459 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1460 EmpathyDispatcherRequestCb *callback,
1463 EmpathyDispatcher *self;
1464 EmpathyDispatcherPriv *priv;
1465 TpConnection *connection;
1466 ConnectionData *connection_data;
1467 DispatcherRequestData *request_data;
1469 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1471 self = empathy_dispatcher_dup_singleton ();
1472 priv = GET_PRIV (self);
1474 connection = empathy_contact_get_connection (contact);
1475 connection_data = g_hash_table_lookup (priv->connections, connection);
1477 /* The contact handle might not be known yet */
1478 request_data = new_dispatcher_request_data (self, connection,
1479 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1480 empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1481 request_data->should_ensure = TRUE;
1483 connection_data->outstanding_requests = g_list_prepend
1484 (connection_data->outstanding_requests, request_data);
1486 dispatcher_request_channel (request_data);
1488 g_object_unref (self);
1493 EmpathyDispatcher *dispatcher;
1494 EmpathyDispatcherRequestCb *callback;
1496 } ChatWithContactIdData;
1499 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1500 EmpathyContact *contact,
1501 const GError *error,
1503 GObject *weak_object)
1505 ChatWithContactIdData *data = user_data;
1509 /* FIXME: Should call data->callback with the error */
1510 DEBUG ("Error: %s", error->message);
1514 empathy_dispatcher_chat_with_contact (contact, data->callback,
1518 g_object_unref (data->dispatcher);
1519 g_slice_free (ChatWithContactIdData, data);
1523 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1524 const gchar *contact_id,
1525 EmpathyDispatcherRequestCb *callback,
1528 EmpathyDispatcher *self;
1529 EmpathyTpContactFactory *factory;
1530 ChatWithContactIdData *data;
1532 g_return_if_fail (TP_IS_CONNECTION (connection));
1533 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1535 self = empathy_dispatcher_dup_singleton ();
1536 factory = empathy_tp_contact_factory_dup_singleton (connection);
1537 data = g_slice_new0 (ChatWithContactIdData);
1538 data->dispatcher = self;
1539 data->callback = callback;
1540 data->user_data = user_data;
1541 empathy_tp_contact_factory_get_from_id (factory, contact_id,
1542 dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1544 g_object_unref (factory);
1548 dispatcher_request_handles_cb (TpConnection *connection,
1549 const GArray *handles,
1550 const GError *error,
1554 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1556 request_data->pending_call = NULL;
1560 EmpathyDispatcher *self = request_data->dispatcher;
1561 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1564 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1566 if (request_data->cb)
1567 request_data->cb (NULL, error, request_data->user_data);
1569 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1572 free_dispatcher_request_data (request_data);
1574 dispatcher_flush_outstanding_operations (self, cd);
1578 request_data->handle = g_array_index (handles, guint, 0);
1579 dispatcher_request_channel (request_data);
1583 empathy_dispatcher_join_muc (TpConnection *connection,
1584 const gchar *roomname,
1585 EmpathyDispatcherRequestCb *callback,
1588 EmpathyDispatcher *self;
1589 EmpathyDispatcherPriv *priv;
1590 DispatcherRequestData *request_data;
1591 ConnectionData *connection_data;
1592 const gchar *names[] = { roomname, NULL };
1594 g_return_if_fail (TP_IS_CONNECTION (connection));
1595 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1597 self = empathy_dispatcher_dup_singleton ();
1598 priv = GET_PRIV (self);
1600 connection_data = g_hash_table_lookup (priv->connections, connection);
1601 g_assert (connection_data != NULL);
1603 /* Don't know the room handle yet */
1604 request_data = new_dispatcher_request_data (self, connection,
1605 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1606 NULL, callback, user_data);
1607 request_data->should_ensure = TRUE;
1609 connection_data->outstanding_requests = g_list_prepend
1610 (connection_data->outstanding_requests, request_data);
1612 request_data->pending_call = tp_cli_connection_call_request_handles (
1614 TP_HANDLE_TYPE_ROOM, names,
1615 dispatcher_request_handles_cb, request_data, NULL, NULL);
1617 g_object_unref (self);
1621 dispatcher_create_channel_cb (TpConnection *connect,
1622 const gchar *object_path,
1623 GHashTable *properties,
1624 const GError *error,
1626 GObject *weak_object)
1628 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1629 EmpathyDispatcher *self =
1630 EMPATHY_DISPATCHER (request_data->dispatcher);
1632 request_data->pending_call = NULL;
1634 dispatcher_connection_new_requested_channel (self,
1635 request_data, object_path, properties, error);
1639 dispatcher_ensure_channel_cb (TpConnection *connect,
1641 const gchar *object_path,
1642 GHashTable *properties,
1643 const GError *error,
1645 GObject *weak_object)
1647 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1648 EmpathyDispatcher *self =
1649 EMPATHY_DISPATCHER (request_data->dispatcher);
1651 request_data->pending_call = NULL;
1653 dispatcher_connection_new_requested_channel (self,
1654 request_data, object_path, properties, error);
1658 empathy_dispatcher_call_create_or_ensure_channel (
1659 EmpathyDispatcher *self,
1660 DispatcherRequestData *request_data)
1662 if (request_data->should_ensure)
1664 request_data->pending_call =
1665 tp_cli_connection_interface_requests_call_ensure_channel (
1666 request_data->connection, -1,
1667 request_data->request, dispatcher_ensure_channel_cb,
1668 request_data, NULL, NULL);
1672 request_data->pending_call =
1673 tp_cli_connection_interface_requests_call_create_channel (
1674 request_data->connection, -1,
1675 request_data->request, dispatcher_create_channel_cb,
1676 request_data, NULL, NULL);
1681 * empathy_dispatcher_create_channel:
1682 * @self: the EmpathyDispatcher
1683 * @connection: the Connection to dispatch on
1684 * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1685 * @callback: a callback for when the channel arrives (or NULL)
1686 * @user_data: optional user data (or NULL)
1688 * When calling this function, #EmpathyDispatcher takes ownership of your
1689 * reference to @request. DO NOT unref or destroy @request. When the request is
1690 * done, @request will be unreferenced. Take another reference if you want to
1694 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1695 TpConnection *connection,
1696 GHashTable *request,
1697 EmpathyDispatcherRequestCb *callback,
1700 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1701 ConnectionData *connection_data;
1702 DispatcherRequestData *request_data;
1703 const gchar *channel_type;
1708 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1709 g_return_if_fail (TP_IS_CONNECTION (connection));
1710 g_return_if_fail (request != NULL);
1712 connection_data = g_hash_table_lookup (priv->connections, connection);
1713 g_assert (connection_data != NULL);
1715 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1717 handle_type = tp_asv_get_uint32 (request,
1718 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1720 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1722 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1724 request_data = new_dispatcher_request_data (self, connection,
1725 channel_type, handle_type, handle, request,
1726 NULL, callback, user_data);
1728 connection_data->outstanding_requests = g_list_prepend
1729 (connection_data->outstanding_requests, request_data);
1731 empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1735 channel_class_matches (GValueArray *class,
1736 const char *channel_type,
1738 GArray *fixed_properties)
1746 v = g_value_array_get_nth (class, 0);
1748 /* if the class doesn't match channel type discard it. */
1749 fprops = g_value_get_boxed (v);
1750 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1752 if (tp_strdiff (channel_type, c_type))
1755 /* we have the right channel type, see if the handle type matches */
1756 h_type = tp_asv_get_uint32 (fprops,
1757 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1759 if (!valid || handle_type != h_type)
1762 if (fixed_properties != NULL)
1764 gpointer h_key, h_val;
1766 GHashTableIter iter;
1769 g_hash_table_iter_init (&iter, fprops);
1771 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1773 /* discard ChannelType and TargetHandleType, as we already
1776 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1778 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1783 for (idx = 0; idx < fixed_properties->len; idx++)
1785 /* if |key| doesn't exist in |fixed_properties|, discard
1790 g_array_index (fixed_properties, char *, idx)))
1793 /* exit the for() loop */
1804 /* if no fixed_properties are specified, discard the classes
1805 * with some fixed properties other than the two we already
1808 if (g_hash_table_size (fprops) > 2)
1816 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1817 TpConnection *connection,
1818 const gchar *channel_type,
1820 GArray *fixed_properties)
1822 EmpathyDispatcherPriv *priv = GET_PRIV (self);
1825 GList *matching_classes;
1829 g_return_val_if_fail (channel_type != NULL, NULL);
1830 g_return_val_if_fail (handle_type != 0, NULL);
1832 cd = g_hash_table_lookup (priv->connections, connection);
1837 classes = cd->requestable_channels;
1838 if (classes == NULL)
1841 matching_classes = NULL;
1843 for (i = 0; i < classes->len; i++)
1845 class = g_ptr_array_index (classes, i);
1847 if (!channel_class_matches
1848 (class, channel_type, handle_type, fixed_properties))
1851 matching_classes = g_list_prepend (matching_classes, class);
1854 return matching_classes;
1858 find_channel_class_idle_cb (gpointer user_data)
1862 FindChannelRequest *request = user_data;
1864 gboolean is_ready = TRUE;
1865 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1867 g_hash_table_remove (priv->request_channel_class_async_ids, request);
1869 cd = g_hash_table_lookup (priv->connections, request->connection);
1873 else if (cd->requestable_channels == NULL)
1878 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1879 request->connection, request->channel_type, request->handle_type,
1880 request->properties);
1882 request->callback (retval, request->user_data);
1883 free_find_channel_request (request);
1884 g_list_free (retval);
1889 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1890 request->connection);
1891 requests = g_list_prepend (requests, request);
1893 g_hash_table_insert (priv->outstanding_classes_requests,
1894 request->connection, requests);
1900 setup_varargs (va_list var_args,
1901 const char *channel_namespace,
1902 const char *first_property_name)
1908 if (first_property_name == NULL)
1911 name = first_property_name;
1912 properties = g_array_new (TRUE, TRUE, sizeof (char *));
1914 while (name != NULL)
1916 name_full = g_strdup (name);
1917 properties = g_array_append_val (properties, name_full);
1918 name = va_arg (var_args, char *);
1925 * empathy_dispatcher_find_requestable_channel_classes:
1926 * @dispatcher: an #EmpathyDispatcher
1927 * @connection: a #TpConnection
1928 * @channel_type: a string identifying the type of the channel to lookup
1929 * @handle_type: the handle type for the channel
1930 * @first_property_name: %NULL, or the name of the first fixed property,
1931 * followed optionally by more names, followed by %NULL.
1933 * Returns all the channel classes that a client can request for the connection
1934 * @connection, of the type identified by @channel_type, @handle_type and the
1935 * fixed properties list.
1936 * The classes which are compatible with a fixed properties list (i.e. those
1937 * that will be returned by this function) are intended as those that do not
1938 * contain any fixed property other than those in the list; note that this
1939 * doesn't guarantee that all the classes compatible with the list will contain
1940 * all the requested fixed properties, so the clients will have to filter
1941 * the returned list themselves.
1942 * If @first_property_name is %NULL, only the classes with no other fixed
1943 * properties than ChannelType and TargetHandleType will be returned.
1944 * Note that this function may return %NULL without performing any lookup if
1945 * @connection is not ready. To ensure that @connection is always ready,
1946 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1948 * Return value: a #GList of #GValueArray objects, where the first element in
1949 * the array is a #GHashTable of the fixed properties, and the second is
1950 * a #GStrv of the allowed properties for the class. The list should be free'd
1951 * with g_list_free() when done, but the objects inside the list are owned
1952 * by the #EmpathyDispatcher and must not be modified.
1955 empathy_dispatcher_find_requestable_channel_classes
1956 (EmpathyDispatcher *self,
1957 TpConnection *connection,
1958 const gchar *channel_type,
1960 const char *first_property_name,
1965 EmpathyDispatcherPriv *priv;
1970 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1971 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1972 g_return_val_if_fail (channel_type != NULL, NULL);
1973 g_return_val_if_fail (handle_type != 0, NULL);
1975 priv = GET_PRIV (self);
1977 va_start (var_args, first_property_name);
1979 properties = setup_varargs (var_args, channel_type, first_property_name);
1983 retval = empathy_dispatcher_find_channel_classes (self, connection,
1984 channel_type, handle_type, properties);
1986 if (properties != NULL)
1988 /* free the properties array */
1989 for (idx = 0; idx < properties->len ; idx++)
1991 str = g_array_index (properties, char *, idx);
1995 g_array_free (properties, TRUE);
2002 * empathy_dispatcher_find_requestable_channel_classes_async:
2003 * @dispatcher: an #EmpathyDispatcher
2004 * @connection: a #TpConnection
2005 * @channel_type: a string identifying the type of the channel to lookup
2006 * @handle_type: the handle type for the channel
2007 * @callback: the callback to call when @connection is ready
2008 * @user_data: the user data to pass to @callback
2009 * @first_property_name: %NULL, or the name of the first fixed property,
2010 * followed optionally by more names, followed by %NULL.
2012 * Please see the documentation of
2013 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2014 * description of this function.
2017 empathy_dispatcher_find_requestable_channel_classes_async
2018 (EmpathyDispatcher *self,
2019 TpConnection *connection,
2020 const gchar *channel_type,
2022 EmpathyDispatcherFindChannelClassCb callback,
2024 const char *first_property_name,
2029 FindChannelRequest *request;
2030 EmpathyDispatcherPriv *priv;
2033 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2034 g_return_if_fail (TP_IS_CONNECTION (connection));
2035 g_return_if_fail (channel_type != NULL);
2036 g_return_if_fail (handle_type != 0);
2038 priv = GET_PRIV (self);
2040 va_start (var_args, first_property_name);
2042 properties = setup_varargs (var_args, channel_type, first_property_name);
2046 /* append another request for this connection */
2047 request = g_slice_new0 (FindChannelRequest);
2048 request->dispatcher = g_object_ref (self);
2049 request->channel_type = g_strdup (channel_type);
2050 request->handle_type = handle_type;
2051 request->connection = connection;
2052 request->callback = callback;
2053 request->user_data = user_data;
2054 request->properties = properties;
2056 source_id = g_idle_add (find_channel_class_idle_cb, request);
2058 g_hash_table_insert (priv->request_channel_class_async_ids,
2059 request, GUINT_TO_POINTER (source_id));
2063 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2066 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2067 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2069 return priv->channels;
2073 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2074 const gchar *account_path,
2075 const gchar *connection_path,
2076 const GPtrArray *channels,
2077 const GPtrArray *requests_satisfied,
2079 GHashTable *handler_info,
2083 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2084 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2087 TpConnection *connection;
2089 /* FIXME: should probably find out whether the account manager is prepared
2090 * before ensuring. See bug #600111. */
2091 account = tp_account_manager_ensure_account (priv->account_manager,
2093 g_assert (account != NULL);
2095 connection = tp_account_ensure_connection (account, connection_path);
2096 if (connection == NULL)
2098 g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2099 "Invalid connection argument");
2103 for (i = 0; i < channels->len ; i++)
2105 GValueArray *arr = g_ptr_array_index (channels, i);
2106 const gchar *object_path;
2107 GHashTable *properties;
2109 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2110 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2112 dispatcher_connection_new_channel_with_properties (self,
2113 connection, object_path, properties);
2121 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2126 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2127 EmpathyHandler *handler;
2129 handler = empathy_handler_new (name, filters, capabilities);
2130 priv->handlers = g_list_prepend (priv->handlers, handler);
2132 /* Only set the handle_channels function, the Channel property on the main
2133 * handler will always report all dispatched channels even if they came from
2134 * a different Handler */
2135 empathy_handler_set_handle_channels_func (handler,
2136 empathy_dispatcher_handle_channels, self);
2142 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2143 EmpathyHandler *handler)
2145 EmpathyDispatcherPriv *priv = GET_PRIV (self);
2148 h = g_list_find (priv->handlers, handler);
2149 g_return_if_fail (h != NULL);
2151 priv->handlers = g_list_delete_link (priv->handlers, h);
2153 g_object_unref (handler);