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/interfaces.h>
41 #include <extensions/extensions.h>
43 #include "empathy-dispatcher.h"
44 #include "empathy-utils.h"
45 #include "empathy-tp-contact-factory.h"
46 #include "empathy-chatroom-manager.h"
47 #include "empathy-utils.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
50 #include <libempathy/empathy-debug.h>
52 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
55 gboolean dispose_has_run;
57 TpAccountManager *account_manager;
58 /* connection to connection data mapping */
59 GHashTable *connections;
60 GHashTable *outstanding_classes_requests;
62 GHashTable *request_channel_class_async_ids;
63 /* reffed (TpAccount *) => gulong
64 * Signal handler ID of the "status-changed" signal */
65 GHashTable *status_changed_handlers;
66 } EmpathyDispatcherPriv;
68 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
70 static EmpathyDispatcher *dispatcher = NULL;
72 static void dispatcher_init_connection_if_needed (
73 EmpathyDispatcher *dispatcher,
74 TpConnection *connection);
76 static GList * empathy_dispatcher_find_channel_classes
77 (EmpathyDispatcher *dispatcher, TpConnection *connection,
78 const gchar *channel_type, guint handle_type, GArray *fixed_properties);
83 /* Channel type specific wrapper object */
84 GObject *channel_wrapper;
89 /* List of requestable channel classes */
90 GPtrArray *requestable_channels;
95 EmpathyDispatcher *dispatcher;
96 TpConnection *connection;
100 EmpathyDispatcherFindChannelClassCb *callback;
102 } FindChannelRequest;
104 static ConnectionData *
105 new_connection_data (void)
107 return g_slice_new0 (ConnectionData);
111 free_connection_data (ConnectionData *cd)
115 if (cd->requestable_channels != NULL)
117 for (i = 0 ; i < cd->requestable_channels->len ; i++)
119 g_ptr_array_index (cd->requestable_channels, i));
120 g_ptr_array_free (cd->requestable_channels, TRUE);
125 free_find_channel_request (FindChannelRequest *r)
130 g_object_unref (r->dispatcher);
131 g_free (r->channel_type);
133 if (r->properties != NULL)
135 for (idx = 0; idx < r->properties->len ; idx++)
137 str = g_array_index (r->properties, char *, idx);
141 g_array_free (r->properties, TRUE);
144 g_slice_free (FindChannelRequest, r);
148 dispatcher_connection_invalidated_cb (TpConnection *connection,
152 EmpathyDispatcher *self)
154 EmpathyDispatcherPriv *priv = GET_PRIV (self);
156 DEBUG ("Error: %s", message);
158 g_hash_table_remove (priv->connections, connection);
162 got_connection_rcc (EmpathyDispatcher *self,
163 TpConnection *connection)
165 EmpathyDispatcherPriv *priv = GET_PRIV (self);
166 TpCapabilities *caps;
169 FindChannelRequest *request;
172 caps = tp_connection_get_capabilities (connection);
173 g_assert (caps != NULL);
175 cd = g_hash_table_lookup (priv->connections, connection);
176 g_assert (cd != NULL);
178 cd->requestable_channels = g_boxed_copy (
179 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST,
180 tp_capabilities_get_channel_classes (caps));
182 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
185 for (l = requests; l != NULL; l = l->next)
189 retval = empathy_dispatcher_find_channel_classes (self,
190 connection, request->channel_type,
191 request->handle_type, request->properties);
192 request->callback (retval, request->user_data);
194 free_find_channel_request (request);
195 g_list_free (retval);
198 g_list_free (requests);
200 g_hash_table_remove (priv->outstanding_classes_requests, connection);
204 connection_prepare_cb (GObject *source,
205 GAsyncResult *result,
208 EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
209 GError *error = NULL;
210 TpConnection *connection = (TpConnection *) source;
212 if (!tp_proxy_prepare_finish (source, result, &error))
214 DEBUG ("Error: %s", error->message);
216 g_error_free (error);
220 got_connection_rcc (self, connection);
223 g_object_unref (self);
227 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
228 TpConnection *connection)
230 EmpathyDispatcherPriv *priv = GET_PRIV (self);
231 GQuark features[] = { TP_CONNECTION_FEATURE_CORE,
232 TP_CONNECTION_FEATURE_CAPABILITIES,
235 if (g_hash_table_lookup (priv->connections, connection) != NULL)
238 g_hash_table_insert (priv->connections, g_object_ref (connection),
239 new_connection_data ());
241 g_signal_connect (connection, "invalidated",
242 G_CALLBACK (dispatcher_connection_invalidated_cb), self);
244 /* Ensure to keep the self object alive while preparing the connection */
247 tp_proxy_prepare_async (connection, features, connection_prepare_cb, self);
251 dispatcher_status_changed_cb (TpAccount *account,
255 gchar *dbus_error_name,
257 EmpathyDispatcher *self)
259 TpConnection *conn = tp_account_get_connection (account);
262 dispatcher_init_connection_if_needed (self, conn);
266 remove_idle_handlers (gpointer key,
272 source_id = GPOINTER_TO_UINT (value);
273 g_source_remove (source_id);
277 dispatcher_constructor (GType type,
278 guint n_construct_params,
279 GObjectConstructParam *construct_params)
282 EmpathyDispatcherPriv *priv;
284 if (dispatcher != NULL)
285 return g_object_ref (dispatcher);
287 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
288 (type, n_construct_params, construct_params);
290 dispatcher = EMPATHY_DISPATCHER (retval);
291 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
293 priv = GET_PRIV (dispatcher);
299 dispatcher_dispose (GObject *object)
301 EmpathyDispatcherPriv *priv = GET_PRIV (object);
305 if (priv->dispose_has_run)
308 priv->dispose_has_run = TRUE;
310 g_hash_table_iter_init (&iter, priv->connections);
311 while (g_hash_table_iter_next (&iter, &connection, NULL))
313 g_signal_handlers_disconnect_by_func (connection,
314 dispatcher_connection_invalidated_cb, object);
317 g_hash_table_destroy (priv->connections);
318 priv->connections = NULL;
320 G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
324 dispatcher_finalize (GObject *object)
326 EmpathyDispatcherPriv *priv = GET_PRIV (object);
330 gpointer account, id;
332 if (priv->request_channel_class_async_ids != NULL)
334 g_hash_table_foreach (priv->request_channel_class_async_ids,
335 remove_idle_handlers, NULL);
336 g_hash_table_destroy (priv->request_channel_class_async_ids);
339 g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
340 while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
342 g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
346 g_hash_table_iter_init (&iter, priv->status_changed_handlers);
347 while (g_hash_table_iter_next (&iter, &account, &id))
349 g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
351 g_hash_table_destroy (priv->status_changed_handlers);
353 g_object_unref (priv->account_manager);
355 g_hash_table_destroy (priv->outstanding_classes_requests);
359 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
361 GObjectClass *object_class = G_OBJECT_CLASS (klass);
363 object_class->dispose = dispatcher_dispose;
364 object_class->finalize = dispatcher_finalize;
365 object_class->constructor = dispatcher_constructor;
367 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
371 connect_account (EmpathyDispatcher *self,
374 EmpathyDispatcherPriv *priv = GET_PRIV (self);
375 TpConnection *conn = tp_account_get_connection (account);
378 id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
385 dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
387 id = g_signal_connect (account, "status-changed",
388 G_CALLBACK (dispatcher_status_changed_cb), self);
390 g_hash_table_insert (priv->status_changed_handlers, g_object_ref (account),
391 GUINT_TO_POINTER (id));
395 account_manager_prepared_cb (GObject *source_object,
396 GAsyncResult *result,
400 EmpathyDispatcher *self = user_data;
401 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
402 GError *error = NULL;
404 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
406 DEBUG ("Failed to prepare account manager: %s", error->message);
407 g_error_free (error);
411 accounts = tp_account_manager_get_valid_accounts (account_manager);
412 for (l = accounts; l; l = l->next)
414 TpAccount *a = l->data;
416 connect_account (self, a);
419 g_list_foreach (accounts, (GFunc) g_object_ref, NULL);
420 g_list_free (accounts);
424 account_prepare_cb (GObject *source_object,
425 GAsyncResult *result,
428 EmpathyDispatcher *self = user_data;
429 TpAccount *account = TP_ACCOUNT (source_object);
430 GError *error = NULL;
432 if (!tp_account_prepare_finish (account, result, &error))
434 DEBUG ("Failed to prepare account: %s", error->message);
435 g_error_free (error);
439 connect_account (self, account);
443 account_validity_changed_cb (TpAccountManager *manager,
451 tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
455 empathy_dispatcher_init (EmpathyDispatcher *self)
457 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
458 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
461 priv->account_manager = tp_account_manager_dup ();
463 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
464 g_object_unref, (GDestroyNotify) free_connection_data);
466 priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
467 g_direct_equal, g_object_unref, NULL);
469 tp_account_manager_prepare_async (priv->account_manager, NULL,
470 account_manager_prepared_cb, self);
472 tp_g_signal_connect_object (priv->account_manager,
473 "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
476 priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
478 priv->status_changed_handlers = g_hash_table_new_full (NULL, NULL,
479 (GDestroyNotify) g_object_unref, NULL);
483 empathy_dispatcher_dup_singleton (void)
485 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
489 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
492 empathy_dispatcher_chat_with_contact_id (
493 empathy_contact_get_account (contact), empathy_contact_get_id (contact),
498 ensure_text_channel_cb (GObject *source,
499 GAsyncResult *result,
502 GError *error = NULL;
504 if (!tp_account_channel_request_ensure_channel_finish (
505 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
507 DEBUG ("Failed to ensure text channel: %s", error->message);
508 g_error_free (error);
513 empathy_dispatcher_chat_with_contact_id (TpAccount *account,
514 const gchar *contact_id,
518 TpAccountChannelRequest *req;
520 request = tp_asv_new (
521 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
522 TP_IFACE_CHANNEL_TYPE_TEXT,
523 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
524 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
527 req = tp_account_channel_request_new (account, request, timestamp);
529 tp_account_channel_request_ensure_channel_async (req, NULL, NULL,
530 ensure_text_channel_cb, NULL);
532 g_hash_table_unref (request);
533 g_object_unref (req);
537 empathy_dispatcher_join_muc (TpAccount *account,
538 const gchar *room_name,
542 TpAccountChannelRequest *req;
544 request = tp_asv_new (
545 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
546 TP_IFACE_CHANNEL_TYPE_TEXT,
547 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
548 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, room_name,
551 req = tp_account_channel_request_new (account, request, timestamp);
553 tp_account_channel_request_ensure_channel_async (req, NULL, NULL,
554 ensure_text_channel_cb, NULL);
556 g_hash_table_unref (request);
557 g_object_unref (req);
561 channel_class_matches (GValueArray *class,
562 const char *channel_type,
564 GArray *fixed_properties)
572 v = g_value_array_get_nth (class, 0);
574 /* if the class doesn't match channel type discard it. */
575 fprops = g_value_get_boxed (v);
576 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
578 if (tp_strdiff (channel_type, c_type))
581 /* we have the right channel type, see if the handle type matches */
582 h_type = tp_asv_get_uint32 (fprops,
583 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
585 if (!valid || (handle_type != h_type && handle_type != TP_UNKNOWN_HANDLE_TYPE))
588 if (fixed_properties != NULL)
590 gpointer h_key, h_val;
595 g_hash_table_iter_init (&iter, fprops);
597 while (g_hash_table_iter_next (&iter, &h_key, &h_val))
599 /* discard ChannelType and TargetHandleType, as we already
602 if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
604 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
609 for (idx = 0; idx < fixed_properties->len; idx++)
611 /* if |key| doesn't exist in |fixed_properties|, discard
616 g_array_index (fixed_properties, char *, idx)))
619 /* exit the for() loop */
630 /* if no fixed_properties are specified, discard the classes
631 * with some fixed properties other than the two we already
634 if (g_hash_table_size (fprops) > 2)
642 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
643 TpConnection *connection,
644 const gchar *channel_type,
646 GArray *fixed_properties)
648 EmpathyDispatcherPriv *priv = GET_PRIV (self);
651 GList *matching_classes;
655 g_return_val_if_fail (channel_type != NULL, NULL);
657 cd = g_hash_table_lookup (priv->connections, connection);
662 classes = cd->requestable_channels;
666 matching_classes = NULL;
668 for (i = 0; i < classes->len; i++)
670 class = g_ptr_array_index (classes, i);
672 if (!channel_class_matches
673 (class, channel_type, handle_type, fixed_properties))
676 matching_classes = g_list_prepend (matching_classes, class);
679 return matching_classes;
683 find_channel_class_idle_cb (gpointer user_data)
687 FindChannelRequest *request = user_data;
689 gboolean is_ready = TRUE;
690 EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
692 g_hash_table_remove (priv->request_channel_class_async_ids, request);
694 cd = g_hash_table_lookup (priv->connections, request->connection);
698 else if (cd->requestable_channels == NULL)
703 retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
704 request->connection, request->channel_type, request->handle_type,
705 request->properties);
707 request->callback (retval, request->user_data);
708 free_find_channel_request (request);
709 g_list_free (retval);
714 requests = g_hash_table_lookup (priv->outstanding_classes_requests,
715 request->connection);
716 requests = g_list_prepend (requests, request);
718 g_hash_table_insert (priv->outstanding_classes_requests,
719 request->connection, requests);
725 setup_varargs (va_list var_args,
726 const char *channel_namespace,
727 const char *first_property_name)
733 if (first_property_name == NULL)
736 name = first_property_name;
737 properties = g_array_new (TRUE, TRUE, sizeof (char *));
741 name_full = g_strdup (name);
742 properties = g_array_append_val (properties, name_full);
743 name = va_arg (var_args, char *);
750 * empathy_dispatcher_find_requestable_channel_classes:
751 * @dispatcher: an #EmpathyDispatcher
752 * @connection: a #TpConnection
753 * @channel_type: a string identifying the type of the channel to lookup
754 * @handle_type: the handle type for the channel, or %TP_UNKNOWN_HANDLE_TYPE
755 * if you don't care about the channel's target handle type
756 * @first_property_name: %NULL, or the name of the first fixed property,
757 * followed optionally by more names, followed by %NULL.
759 * Returns all the channel classes that a client can request for the connection
760 * @connection, of the type identified by @channel_type, @handle_type and the
761 * fixed properties list.
762 * The classes which are compatible with a fixed properties list (i.e. those
763 * that will be returned by this function) are intended as those that do not
764 * contain any fixed property other than those in the list; note that this
765 * doesn't guarantee that all the classes compatible with the list will contain
766 * all the requested fixed properties, so the clients will have to filter
767 * the returned list themselves.
768 * If @first_property_name is %NULL, only the classes with no other fixed
769 * properties than ChannelType and TargetHandleType will be returned.
770 * Note that this function may return %NULL without performing any lookup if
771 * @connection is not ready. To ensure that @connection is always ready,
772 * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
774 * Return value: a #GList of #GValueArray objects, where the first element in
775 * the array is a #GHashTable of the fixed properties, and the second is
776 * a #GStrv of the allowed properties for the class. The list should be free'd
777 * with g_list_free() when done, but the objects inside the list are owned
778 * by the #EmpathyDispatcher and must not be modified.
781 empathy_dispatcher_find_requestable_channel_classes
782 (EmpathyDispatcher *self,
783 TpConnection *connection,
784 const gchar *channel_type,
786 const char *first_property_name,
791 EmpathyDispatcherPriv *priv;
796 g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
797 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
798 g_return_val_if_fail (channel_type != NULL, NULL);
800 priv = GET_PRIV (self);
802 va_start (var_args, first_property_name);
804 properties = setup_varargs (var_args, channel_type, first_property_name);
808 retval = empathy_dispatcher_find_channel_classes (self, connection,
809 channel_type, handle_type, properties);
811 if (properties != NULL)
813 /* free the properties array */
814 for (idx = 0; idx < properties->len ; idx++)
816 str = g_array_index (properties, char *, idx);
820 g_array_free (properties, TRUE);
827 * empathy_dispatcher_find_requestable_channel_classes_async:
828 * @dispatcher: an #EmpathyDispatcher
829 * @connection: a #TpConnection
830 * @channel_type: a string identifying the type of the channel to lookup
831 * @handle_type: the handle type for the channel
832 * @callback: the callback to call when @connection is ready
833 * @user_data: the user data to pass to @callback
834 * @first_property_name: %NULL, or the name of the first fixed property,
835 * followed optionally by more names, followed by %NULL.
837 * Please see the documentation of
838 * empathy_dispatcher_find_requestable_channel_classes() for a detailed
839 * description of this function.
842 empathy_dispatcher_find_requestable_channel_classes_async
843 (EmpathyDispatcher *self,
844 TpConnection *connection,
845 const gchar *channel_type,
847 EmpathyDispatcherFindChannelClassCb callback,
849 const char *first_property_name,
854 FindChannelRequest *request;
855 EmpathyDispatcherPriv *priv;
858 g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
859 g_return_if_fail (TP_IS_CONNECTION (connection));
860 g_return_if_fail (channel_type != NULL);
861 g_return_if_fail (handle_type != 0);
863 priv = GET_PRIV (self);
865 va_start (var_args, first_property_name);
867 properties = setup_varargs (var_args, channel_type, first_property_name);
871 /* append another request for this connection */
872 request = g_slice_new0 (FindChannelRequest);
873 request->dispatcher = g_object_ref (self);
874 request->channel_type = g_strdup (channel_type);
875 request->handle_type = handle_type;
876 request->connection = connection;
877 request->callback = callback;
878 request->user_data = user_data;
879 request->properties = properties;
881 source_id = g_idle_add (find_channel_class_idle_cb, request);
883 g_hash_table_insert (priv->request_channel_class_async_ids,
884 request, GUINT_TO_POINTER (source_id));