1 /* * Copyright (C) 2007-2008 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>
24 #include <glib/gi18n-lib.h>
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/proxy-subclass.h>
32 #include <libmissioncontrol/mission-control.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include <extensions/extensions.h>
37 #include "empathy-dispatcher.h"
38 #include "empathy-utils.h"
39 #include "empathy-tube-handler.h"
40 #include "empathy-account-manager.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-tp-file.h"
43 #include "empathy-chatroom-manager.h"
44 #include "empathy-utils.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
51 EmpathyAccountManager *account_manager;
53 /* connection to connection data mapping */
54 GHashTable *connections;
55 /* accounts to connection mapping */
59 } EmpathyDispatcherPriv;
61 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
70 static guint signals[LAST_SIGNAL];
71 static EmpathyDispatcher *dispatcher = NULL;
74 EmpathyDispatcher *dispatcher;
75 EmpathyDispatchOperation *operation;
76 TpConnection *connection;
80 EmpathyContact *contact;
82 /* Properties to pass to the channel when requesting it */
84 EmpathyDispatcherRequestCb *cb;
86 gpointer *request_data;
87 } DispatcherRequestData;
91 /* Channel type specific wrapper object */
92 GObject *channel_wrapper;
97 /* ObjectPath => DispatchData.. */
98 GHashTable *dispatched_channels;
99 /* ObjectPath -> EmpathyDispatchOperations */
100 GHashTable *dispatching_channels;
101 /* ObjectPath -> EmpathyDispatchOperations */
102 GHashTable *outstanding_channels;
103 /* List of DispatcherRequestData */
104 GList *outstanding_requests;
107 static DispatchData *
108 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
110 DispatchData *d = g_slice_new0 (DispatchData);
111 d->channel = g_object_ref (channel);
112 d->channel_wrapper = g_object_ref (channel_wrapper);
118 free_dispatch_data (DispatchData *data)
120 g_object_unref (data->channel);
121 g_object_unref (data->channel_wrapper);
123 g_slice_free (DispatchData, data);
127 static DispatcherRequestData *
128 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
129 TpConnection *connection, const gchar *channel_type, guint handle_type,
130 guint handle, GHashTable *request,
131 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
133 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
135 result->dispatcher = dispatcher;
136 result->connection = connection;
138 result->channel_type = g_strdup (channel_type);
139 result->handle_type = handle_type;
140 result->handle = handle;
141 result->request = request;
144 result->contact = g_object_ref (contact);
147 result->user_data = user_data;
153 free_dispatcher_request_data (DispatcherRequestData *r)
155 g_free (r->channel_type);
157 if (r->contact != NULL)
158 g_object_unref (r->contact);
160 if (r->request != NULL)
161 g_hash_table_unref (r->request);
163 g_slice_free (DispatcherRequestData, r);
166 static ConnectionData *
167 new_connection_data (McAccount *account)
169 ConnectionData *cd = g_slice_new0 (ConnectionData);
170 cd->account = g_object_ref (account);
172 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
173 g_free, (GDestroyNotify) free_dispatch_data);
175 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
176 g_free, g_object_unref);
178 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
185 free_connection_data (ConnectionData *cd)
188 g_object_unref (cd->account);
189 g_hash_table_destroy (cd->dispatched_channels);
190 g_hash_table_destroy (cd->dispatching_channels);
192 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
194 free_dispatcher_request_data (l->data);
199 dispatcher_connection_invalidated_cb (TpConnection *connection,
200 guint domain, gint code, gchar *message,
201 EmpathyDispatcher *dispatcher)
203 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
206 DEBUG ("Error: %s", message);
207 cd = g_hash_table_lookup (priv->connections, connection);
209 g_hash_table_remove (priv->accounts, cd->account);
210 g_hash_table_remove (priv->connections, connection);
214 dispatcher_operation_can_start (EmpathyDispatcher *self,
215 EmpathyDispatchOperation *operation, ConnectionData *cd)
218 const gchar *channel_type =
219 empathy_dispatch_operation_get_channel_type (operation);
221 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
223 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
225 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
235 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
236 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
240 l = cd->outstanding_requests;
243 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
248 if (d->operation == operation)
253 d->cb (NULL, error, d->user_data);
255 d->cb (operation, NULL, d->user_data);
258 cd->outstanding_requests = g_list_delete_link
259 (cd->outstanding_requests, lt);
261 free_dispatcher_request_data (d);
267 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
268 gchar *message, EmpathyDispatcher *dispatcher)
270 /* Channel went away... */
271 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
272 TpConnection *connection;
273 EmpathyDispatchOperation *operation;
275 const gchar *object_path;
277 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
279 cd = g_hash_table_lookup (priv->connections, connection);
280 /* Connection itself invalidated? */
284 object_path = tp_proxy_get_object_path (proxy);
286 DEBUG ("Channel %s invalidated", object_path);
288 g_hash_table_remove (cd->dispatched_channels, object_path);
289 g_hash_table_remove (cd->dispatching_channels, object_path);
291 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
292 if (operation != NULL)
294 GError error = { domain, code, message };
295 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
296 g_hash_table_remove (cd->outstanding_channels, object_path);
297 g_object_unref (operation);
302 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
303 EmpathyDispatcher *dispatcher)
305 g_assert (empathy_dispatch_operation_is_incoming (operation));
306 DEBUG ("Send of for dispatching: %s",
307 empathy_dispatch_operation_get_object_path (operation));
308 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
312 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
313 EmpathyDispatcher *dispatcher)
315 /* Our job is done, remove the dispatch operation and mark the channel as
317 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
318 TpConnection *connection;
320 const gchar *object_path;
322 connection = empathy_dispatch_operation_get_tp_connection (operation);
323 cd = g_hash_table_lookup (priv->connections, connection);
324 g_assert (cd != NULL);
325 g_object_unref (G_OBJECT (connection));
327 object_path = empathy_dispatch_operation_get_object_path (operation);
329 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
332 d = new_dispatch_data (
333 empathy_dispatch_operation_get_channel (operation),
334 empathy_dispatch_operation_get_channel_wrapper (operation));
335 g_hash_table_insert (cd->dispatched_channels,
336 g_strdup (object_path), d);
338 g_hash_table_remove (cd->dispatching_channels, object_path);
340 DEBUG ("Channel claimed: %s", object_path);
344 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
345 EmpathyDispatcher *dispatcher)
347 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
348 TpConnection *connection;
350 EmpathyDispatchOperationState status;
352 g_signal_connect (operation, "approved",
353 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
355 g_signal_connect (operation, "claimed",
356 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
358 /* Signal the observers */
359 DEBUG ("Send to observers: %s",
360 empathy_dispatch_operation_get_object_path (operation));
361 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
363 empathy_dispatch_operation_start (operation);
365 /* Signal potential requestors */
366 connection = empathy_dispatch_operation_get_tp_connection (operation);
367 cd = g_hash_table_lookup (priv->connections, connection);
368 g_assert (cd != NULL);
369 g_object_unref (G_OBJECT (connection));
371 g_object_ref (operation);
373 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
374 status = empathy_dispatch_operation_get_status (operation);
375 g_object_unref (operation);
377 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
380 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
382 DEBUG ("Send to approvers: %s",
383 empathy_dispatch_operation_get_object_path (operation));
384 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
388 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
389 DEBUG ("Send of for dispatching: %s",
390 empathy_dispatch_operation_get_object_path (operation));
391 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
397 dispatcher_start_dispatching (EmpathyDispatcher *self,
398 EmpathyDispatchOperation *operation, ConnectionData *cd)
400 const gchar *object_path =
401 empathy_dispatch_operation_get_object_path (operation);
403 DEBUG ("Dispatching process started for %s", object_path);
405 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
407 g_assert (g_hash_table_lookup (cd->outstanding_channels,
408 object_path) == NULL);
410 g_hash_table_insert (cd->dispatching_channels,
411 g_strdup (object_path), operation);
413 switch (empathy_dispatch_operation_get_status (operation))
415 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
416 g_signal_connect (operation, "ready",
417 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
419 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
420 dispatch_operation_ready_cb (operation, dispatcher);
423 g_assert_not_reached();
427 else if (empathy_dispatch_operation_get_status (operation) >=
428 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
430 /* Already dispatching and the operation is pending, thus the observers
431 * have seen it (if applicable), so we can flush the request right away.
433 dispatch_operation_flush_requests (self, operation, NULL, cd);
439 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
445 g_hash_table_iter_init (&iter, cd->outstanding_channels);
446 while (g_hash_table_iter_next (&iter, NULL, &value))
448 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
450 if (dispatcher_operation_can_start (self, operation, cd))
452 dispatcher_start_dispatching (dispatcher, operation, cd);
453 g_hash_table_iter_remove (&iter);
460 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
461 TpConnection *connection,
462 const gchar *object_path, const gchar *channel_type,
463 guint handle_type, guint handle, GHashTable *properties,
466 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
469 EmpathyDispatchOperation *operation;
470 EmpathyContact *contact = NULL;
472 /* Channel types we never want to dispatch because they're either deprecated
473 * or can't sensibly be dispatch (e.g. channels that should always be
475 const char *blacklist[] = {
476 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
477 TP_IFACE_CHANNEL_TYPE_TUBES,
478 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
482 cd = g_hash_table_lookup (priv->connections, connection);
484 /* Don't bother with channels we have already dispatched or are dispatching
485 * currently. This can happen when NewChannel(s) is fired after
486 * RequestChannel/CreateChannel/EnsureChannel */
487 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
490 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
493 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
495 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
498 /* Only pick up non-requested text channels. For all other it doesn't make
499 * sense to handle it if we didn't request it. The same goes for channels we
500 * discovered by the Channels property or ListChannels */
501 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
503 DEBUG ("Ignoring incoming channel of type %s on %s",
504 channel_type, object_path);
508 for (i = 0 ; blacklist[i] != NULL; i++)
510 if (!tp_strdiff (channel_type, blacklist[i]))
512 DEBUG ("Ignoring blacklisted channel type %s on %s",
513 channel_type, object_path);
518 DEBUG ("New channel of type %s on %s", channel_type, object_path);
520 if (properties == NULL)
521 channel = tp_channel_new (connection, object_path, channel_type,
522 handle_type, handle, NULL);
524 channel = tp_channel_new_from_properties (connection, object_path,
527 g_signal_connect (channel, "invalidated",
528 G_CALLBACK (dispatcher_channel_invalidated_cb),
531 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
533 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
534 contact = empathy_contact_factory_get_from_handle (factory,
535 cd->account, handle);
536 g_object_unref (factory);
539 operation = empathy_dispatch_operation_new (connection, channel, contact,
542 g_object_unref (channel);
546 /* Request could either be by us or by a remote party. If there are no
547 * outstanding requests for this channel type we can assume it's remote.
548 * Otherwise we wait untill they are all satisfied */
549 if (dispatcher_operation_can_start (dispatcher, operation, cd))
550 dispatcher_start_dispatching (dispatcher, operation, cd);
552 g_hash_table_insert (cd->outstanding_channels,
553 g_strdup (object_path), operation);
557 dispatcher_start_dispatching (dispatcher, operation, cd);
562 dispatcher_connection_new_channel_cb (TpConnection *connection,
563 const gchar *object_path, const gchar *channel_type,
564 guint handle_type, guint handle,
565 gboolean suppress_handler, gpointer user_data,
568 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
570 /* Empathy heavily abuses surpress handler (don't try this at home), if
571 * surpress handler is true then it is an outgoing channel, which is
572 * requested either by us or some other party (like the megaphone applet).
573 * Otherwise it's an incoming channel */
574 dispatcher_connection_new_channel (dispatcher, connection,
575 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
579 dispatcher_connection_new_channel_with_properties (
580 EmpathyDispatcher *dispatcher, TpConnection *connection,
581 const gchar *object_path, GHashTable *properties)
583 const gchar *channel_type;
590 channel_type = tp_asv_get_string (properties,
591 TP_IFACE_CHANNEL ".ChannelType");
592 if (channel_type == NULL)
594 g_message ("%s had an invalid ChannelType property", object_path);
598 handle_type = tp_asv_get_uint32 (properties,
599 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
602 g_message ("%s had an invalid TargetHandleType property", object_path);
606 handle = tp_asv_get_uint32 (properties,
607 TP_IFACE_CHANNEL ".TargetHandle", &valid);
610 g_message ("%s had an invalid TargetHandle property", object_path);
614 /* We assume there is no channel dispather, so we're the only one dispatching
615 * it. Which means that a requested channel it is outgoing one */
616 requested = tp_asv_get_boolean (properties,
617 TP_IFACE_CHANNEL ".Requested", &valid);
620 g_message ("%s had an invalid Requested property", object_path);
624 dispatcher_connection_new_channel (dispatcher, connection,
625 object_path, channel_type, handle_type, handle, properties, !requested);
630 dispatcher_connection_new_channels_cb (
631 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
634 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
637 for (i = 0; i < channels->len ; i++)
639 GValueArray *arr = g_ptr_array_index (channels, i);
640 const gchar *object_path;
641 GHashTable *properties;
643 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
644 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
646 dispatcher_connection_new_channel_with_properties (dispatcher,
647 connection, object_path, properties);
652 dispatcher_connection_got_channels_property (TpProxy *proxy,
653 const GValue *channels_prop, const GError *error, gpointer user_data,
659 DEBUG ("Error: %s", error->message);
663 channels = g_value_get_boxed (channels_prop);
664 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
665 channels, NULL, object);
669 dispatcher_connection_list_channels_cb (TpConnection *connection,
670 const GPtrArray *channels,
678 DEBUG ("Error: %s", error->message);
682 for (i = 0; i < channels->len; i++) {
685 values = g_ptr_array_index (channels, i);
686 /* We don't have any extra info, so assume already existing channels are
688 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
690 g_value_get_boxed (g_value_array_get_nth (values, 0)),
691 g_value_get_string (g_value_array_get_nth (values, 1)),
692 g_value_get_uint (g_value_array_get_nth (values, 2)),
693 g_value_get_uint (g_value_array_get_nth (values, 3)),
699 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
700 const GPtrArray *capabilities,
706 DEBUG ("Error: %s", error->message);
711 dispatcher_connection_ready_cb (TpConnection *connection,
712 const GError *error, gpointer dispatcher)
714 GPtrArray *capabilities;
717 const gchar *remove = NULL;
721 dispatcher_connection_invalidated_cb (connection, error->domain,
722 error->code, error->message, dispatcher);
726 g_signal_connect (connection, "invalidated",
727 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
730 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
731 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
733 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
734 dispatcher_connection_new_channels_cb,
735 NULL, NULL, G_OBJECT (dispatcher), NULL);
737 tp_cli_dbus_properties_call_get (connection, -1,
738 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
739 dispatcher_connection_got_channels_property,
740 NULL, NULL, dispatcher);
744 tp_cli_connection_connect_to_new_channel (connection,
745 dispatcher_connection_new_channel_cb,
746 NULL, NULL, G_OBJECT (dispatcher), NULL);
748 tp_cli_connection_call_list_channels (connection, -1,
749 dispatcher_connection_list_channels_cb, NULL, NULL,
750 G_OBJECT (dispatcher));
754 /* Advertise VoIP capabilities */
755 /* FIXME: Capabilities is leaked */
756 capabilities = g_ptr_array_sized_new (1);
757 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
758 G_TYPE_UINT, G_TYPE_INVALID);
759 g_value_init (&cap, cap_type);
760 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
761 dbus_g_type_struct_set (&cap,
762 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
763 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
764 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
765 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
766 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
767 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
769 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
770 connection, -1, capabilities, &remove,
771 dispatcher_connection_advertise_capabilities_cb,
772 NULL, NULL, G_OBJECT (dispatcher));
776 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
778 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
779 TpConnection *connection;
781 connection = g_hash_table_lookup (priv->accounts, account);
782 if (connection != NULL)
785 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
786 if (connection == NULL)
789 g_hash_table_insert (priv->connections, g_object_ref (connection),
790 new_connection_data (account));
792 g_hash_table_insert (priv->accounts, g_object_ref (account),
793 g_object_ref (connection));
795 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
798 g_object_unref (connection);
802 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
803 McAccount *account, TpConnectionStatusReason reason,
804 TpConnectionStatus status, TpConnectionStatus previous,
805 EmpathyDispatcher *dispatcher)
807 dispatcher_update_account (dispatcher, account);
812 dispatcher_constructor (GType type, guint n_construct_params,
813 GObjectConstructParam *construct_params)
817 if (dispatcher == NULL)
819 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
820 (type, n_construct_params, construct_params);
822 dispatcher = EMPATHY_DISPATCHER (retval);
823 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
827 retval = g_object_ref (dispatcher);
834 dispatcher_finalize (GObject *object)
836 EmpathyDispatcherPriv *priv = GET_PRIV (object);
838 g_signal_handlers_disconnect_by_func (priv->account_manager,
839 dispatcher_account_connection_cb, object);
841 g_object_unref (priv->account_manager);
842 g_object_unref (priv->mc);
844 g_hash_table_destroy (priv->accounts);
845 g_hash_table_destroy (priv->connections);
849 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
851 GObjectClass *object_class = G_OBJECT_CLASS (klass);
853 object_class->finalize = dispatcher_finalize;
854 object_class->constructor = dispatcher_constructor;
857 g_signal_new ("observe",
858 G_TYPE_FROM_CLASS (klass),
862 g_cclosure_marshal_VOID__OBJECT,
864 1, EMPATHY_TYPE_DISPATCH_OPERATION);
867 g_signal_new ("approve",
868 G_TYPE_FROM_CLASS (klass),
872 g_cclosure_marshal_VOID__OBJECT,
874 1, EMPATHY_TYPE_DISPATCH_OPERATION);
877 g_signal_new ("dispatch",
878 G_TYPE_FROM_CLASS (klass),
882 g_cclosure_marshal_VOID__OBJECT,
884 1, EMPATHY_TYPE_DISPATCH_OPERATION);
886 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
891 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
894 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
895 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
897 dispatcher->priv = priv;
898 priv->mc = empathy_mission_control_new ();
899 priv->account_manager = empathy_account_manager_dup_singleton ();
901 g_signal_connect (priv->account_manager,
902 "account-connection-changed",
903 G_CALLBACK (dispatcher_account_connection_cb),
906 priv->accounts = g_hash_table_new_full (empathy_account_hash,
907 empathy_account_equal, g_object_unref, g_object_unref);
909 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
910 g_object_unref, (GDestroyNotify) free_connection_data);
912 accounts = mc_accounts_list_by_enabled (TRUE);
914 for (l = accounts; l; l = l->next) {
915 dispatcher_update_account (dispatcher, l->data);
916 g_object_unref (l->data);
918 g_list_free (accounts);
922 empathy_dispatcher_dup_singleton (void)
924 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
928 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
929 DispatcherRequestData *request_data, const GError *error)
931 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
932 ConnectionData *conn_data;
934 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
935 if (request_data->cb != NULL)
936 request_data->cb (NULL, error, request_data->user_data);
938 conn_data->outstanding_requests =
939 g_list_remove (conn_data->outstanding_requests, request_data);
940 free_dispatcher_request_data (request_data);
944 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
945 DispatcherRequestData *request_data, const gchar *object_path,
946 GHashTable *properties, const GError *error)
948 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
949 EmpathyDispatchOperation *operation = NULL;
950 ConnectionData *conn_data;
952 conn_data = g_hash_table_lookup (priv->connections,
953 request_data->connection);
957 DEBUG ("Channel request failed: %s", error->message);
959 dispatcher_request_failed (dispatcher, request_data, error);
964 operation = g_hash_table_lookup (conn_data->outstanding_channels,
967 if (operation != NULL)
968 g_hash_table_remove (conn_data->outstanding_channels, object_path);
970 operation = g_hash_table_lookup (conn_data->dispatching_channels,
973 if (operation == NULL)
975 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
980 operation = empathy_dispatch_operation_new_with_wrapper (
981 request_data->connection,
982 data->channel, request_data->contact, FALSE,
983 data->channel_wrapper);
989 if (properties != NULL)
990 channel = tp_channel_new_from_properties (request_data->connection,
991 object_path, properties, NULL);
993 channel = tp_channel_new (request_data->connection, object_path,
994 request_data->channel_type, request_data->handle_type,
995 request_data->handle, NULL);
997 g_signal_connect (channel, "invalidated",
998 G_CALLBACK (dispatcher_channel_invalidated_cb),
999 request_data->dispatcher);
1001 operation = empathy_dispatch_operation_new (request_data->connection,
1002 channel, request_data->contact, FALSE);
1003 g_object_unref (channel);
1008 /* Already existed set potential extra information */
1009 g_object_set (G_OBJECT (operation),
1010 "contact", request_data->contact,
1014 request_data->operation = operation;
1016 /* (pre)-approve this right away as we requested it */
1017 empathy_dispatch_operation_approve (operation);
1019 dispatcher_start_dispatching (request_data->dispatcher, operation,
1022 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1027 dispatcher_request_channel_cb (TpConnection *connection,
1028 const gchar *object_path, const GError *error,
1029 gpointer user_data, GObject *weak_object)
1031 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1032 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1034 dispatcher_connection_new_requested_channel (dispatcher,
1035 request_data, object_path, NULL, error);
1039 dispatcher_request_channel (DispatcherRequestData *request_data)
1041 tp_cli_connection_call_request_channel (request_data->connection, -1,
1042 request_data->channel_type,
1043 request_data->handle_type,
1044 request_data->handle,
1045 TRUE, dispatcher_request_channel_cb,
1046 request_data, NULL, G_OBJECT (request_data->dispatcher));
1050 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1051 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1053 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1054 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1056 TpConnection *connection;
1058 DispatcherRequestData *request_data;
1060 account = empathy_contact_get_account (contact);
1061 connection = g_hash_table_lookup (priv->accounts, account);
1063 g_assert (connection != NULL);
1064 cd = g_hash_table_lookup (priv->connections, connection);
1065 request_data = new_dispatcher_request_data (dispatcher, connection,
1066 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1067 contact, callback, user_data);
1069 cd->outstanding_requests = g_list_prepend
1070 (cd->outstanding_requests, request_data);
1072 dispatcher_request_channel (request_data);
1074 g_object_unref (dispatcher);
1079 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1080 const GError *error, gpointer user_data, GObject *object)
1082 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1084 request_data->handle = empathy_contact_get_handle (contact);
1086 dispatcher_request_channel (request_data);
1090 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1091 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1093 EmpathyDispatcher *dispatcher;
1094 EmpathyDispatcherPriv *priv;
1096 TpConnection *connection;
1097 ConnectionData *connection_data;
1098 DispatcherRequestData *request_data;
1100 dispatcher = empathy_dispatcher_dup_singleton();
1101 priv = GET_PRIV (dispatcher);
1103 account = empathy_contact_get_account (contact);
1104 connection = g_hash_table_lookup (priv->accounts, account);
1105 connection_data = g_hash_table_lookup (priv->connections, connection);
1107 /* The contact handle might not be known yet */
1108 request_data = new_dispatcher_request_data (dispatcher, connection,
1109 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1110 contact, callback, user_data);
1112 connection_data->outstanding_requests = g_list_prepend
1113 (connection_data->outstanding_requests, request_data);
1115 empathy_contact_call_when_ready (contact,
1116 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1117 request_data, NULL, G_OBJECT (dispatcher));
1119 g_object_unref (dispatcher);
1123 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1124 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1126 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1127 EmpathyContactFactory *factory;
1128 EmpathyContact *contact;
1130 factory = empathy_contact_factory_dup_singleton ();
1131 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1133 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1135 g_object_unref (contact);
1136 g_object_unref (factory);
1137 g_object_unref (dispatcher);
1142 dispatcher_request_handles_cb (TpConnection *connection,
1143 const GArray *handles, const GError *error, gpointer user_data,
1146 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1150 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1151 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1154 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1156 if (request_data->cb)
1157 request_data->cb (NULL, error, request_data->user_data);
1159 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1162 free_dispatcher_request_data (request_data);
1164 dispatcher_flush_outstanding_operations (dispatcher, cd);
1168 request_data->handle = g_array_index (handles, guint, 0);
1169 dispatcher_request_channel (request_data);
1173 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1174 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1176 EmpathyDispatcher *dispatcher;
1177 EmpathyDispatcherPriv *priv;
1178 DispatcherRequestData *request_data;
1179 TpConnection *connection;
1180 ConnectionData *connection_data;
1181 const gchar *names[] = { roomname, NULL };
1183 dispatcher = empathy_dispatcher_dup_singleton();
1184 priv = GET_PRIV (dispatcher);
1186 connection = g_hash_table_lookup (priv->accounts, account);
1187 connection_data = g_hash_table_lookup (priv->connections, connection);
1190 /* Don't know the room handle yet */
1191 request_data = new_dispatcher_request_data (dispatcher, connection,
1192 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1193 NULL, callback, user_data);
1195 connection_data->outstanding_requests = g_list_prepend
1196 (connection_data->outstanding_requests, request_data);
1198 tp_cli_connection_call_request_handles (connection, -1,
1199 TP_HANDLE_TYPE_ROOM, names,
1200 dispatcher_request_handles_cb, request_data, NULL,
1201 G_OBJECT (dispatcher));
1203 g_object_unref (dispatcher);
1207 dispatcher_create_channel_cb (TpConnection *connect,
1208 const gchar *object_path, GHashTable *properties, const GError *error,
1209 gpointer user_data, GObject *weak_object)
1211 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1212 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1214 dispatcher_connection_new_requested_channel (dispatcher,
1215 request_data, object_path, properties, error);
1220 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1221 const GError *error, gpointer user_data, GObject *object)
1223 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1224 GValue *target_handle;
1226 g_assert (request_data->request);
1230 dispatcher_request_failed (request_data->dispatcher,
1231 request_data, error);
1235 request_data->handle = empathy_contact_get_handle (contact);
1237 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1238 g_value_set_uint (target_handle, request_data->handle);
1239 g_hash_table_insert (request_data->request,
1240 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1242 tp_cli_connection_interface_requests_call_create_channel (
1243 request_data->connection, -1,
1244 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1245 G_OBJECT (request_data->dispatcher));
1249 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1250 const GError *error,
1253 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1257 dispatcher_request_failed (request_data->dispatcher,
1258 request_data, error);
1262 empathy_contact_call_when_ready (request_data->contact,
1263 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1264 request_data, NULL, G_OBJECT (request_data->dispatcher));
1268 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1269 const gchar *filename, guint64 size, guint64 date,
1270 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1273 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1274 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1275 McAccount *account = empathy_contact_get_account (contact);
1276 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1277 ConnectionData *connection_data =
1278 g_hash_table_lookup (priv->connections, connection);
1279 DispatcherRequestData *request_data;
1281 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1282 (GDestroyNotify) tp_g_value_slice_free);
1284 /* org.freedesktop.Telepathy.Channel.ChannelType */
1285 value = tp_g_value_slice_new (G_TYPE_STRING);
1286 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1287 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1289 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1290 value = tp_g_value_slice_new (G_TYPE_UINT);
1291 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1292 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1294 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1295 value = tp_g_value_slice_new (G_TYPE_STRING);
1296 g_value_set_string (value, content_type);
1297 g_hash_table_insert (request,
1298 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1300 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1301 value = tp_g_value_slice_new (G_TYPE_STRING);
1302 g_value_set_string (value, filename);
1303 g_hash_table_insert (request,
1304 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1306 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1307 value = tp_g_value_slice_new (G_TYPE_UINT64);
1308 g_value_set_uint64 (value, size);
1309 g_hash_table_insert (request,
1310 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1312 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1313 value = tp_g_value_slice_new (G_TYPE_UINT64);
1314 g_value_set_uint64 (value, date);
1315 g_hash_table_insert (request,
1316 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1319 /* The contact handle might not be known yet */
1320 request_data = new_dispatcher_request_data (dispatcher, connection,
1321 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1322 contact, callback, user_data);
1323 connection_data->outstanding_requests = g_list_prepend
1324 (connection_data->outstanding_requests, request_data);
1326 tp_connection_call_when_ready (connection,
1327 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1329 g_object_unref (dispatcher);