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>
31 #include <telepathy-glib/gtypes.h>
33 #include <libmissioncontrol/mission-control.h>
34 #include <libmissioncontrol/mc-account.h>
36 #include <extensions/extensions.h>
38 #include "empathy-dispatcher.h"
39 #include "empathy-utils.h"
40 #include "empathy-tube-handler.h"
41 #include "empathy-account-manager.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-tp-file.h"
44 #include "empathy-chatroom-manager.h"
45 #include "empathy-utils.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
48 #include <libempathy/empathy-debug.h>
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
53 EmpathyAccountManager *account_manager;
55 /* connection to connection data mapping */
56 GHashTable *connections;
57 /* accounts to connection mapping */
62 /* channels which the dispatcher is listening "invalidated" */
64 } EmpathyDispatcherPriv;
66 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
76 static guint signals[LAST_SIGNAL];
77 static EmpathyDispatcher *dispatcher = NULL;
81 EmpathyDispatcher *dispatcher;
82 EmpathyDispatchOperation *operation;
83 TpConnection *connection;
87 EmpathyContact *contact;
89 /* Properties to pass to the channel when requesting it */
91 EmpathyDispatcherRequestCb *cb;
93 gpointer *request_data;
94 } DispatcherRequestData;
99 /* Channel type specific wrapper object */
100 GObject *channel_wrapper;
106 /* ObjectPath => DispatchData.. */
107 GHashTable *dispatched_channels;
108 /* ObjectPath -> EmpathyDispatchOperations */
109 GHashTable *dispatching_channels;
110 /* ObjectPath -> EmpathyDispatchOperations */
111 GHashTable *outstanding_channels;
112 /* List of DispatcherRequestData */
113 GList *outstanding_requests;
114 /* List of requestable channel classes */
115 GPtrArray *requestable_channels;
118 static DispatchData *
119 new_dispatch_data (TpChannel *channel,
120 GObject *channel_wrapper)
122 DispatchData *d = g_slice_new0 (DispatchData);
123 d->channel = g_object_ref (channel);
124 if (channel_wrapper != NULL)
125 d->channel_wrapper = g_object_ref (channel_wrapper);
131 free_dispatch_data (DispatchData *data)
133 g_object_unref (data->channel);
134 if (data->channel_wrapper != NULL)
135 g_object_unref (data->channel_wrapper);
137 g_slice_free (DispatchData, data);
140 static DispatcherRequestData *
141 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
142 TpConnection *connection,
143 const gchar *channel_type,
147 EmpathyContact *contact,
148 EmpathyDispatcherRequestCb *cb,
151 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
153 result->dispatcher = g_object_ref (dispatcher);
154 result->connection = connection;
156 result->channel_type = g_strdup (channel_type);
157 result->handle_type = handle_type;
158 result->handle = handle;
159 result->request = request;
162 result->contact = g_object_ref (contact);
165 result->user_data = user_data;
171 free_dispatcher_request_data (DispatcherRequestData *r)
173 g_free (r->channel_type);
175 if (r->dispatcher != NULL)
176 g_object_unref (r->dispatcher);
178 if (r->contact != NULL)
179 g_object_unref (r->contact);
181 if (r->request != NULL)
182 g_hash_table_unref (r->request);
184 g_slice_free (DispatcherRequestData, r);
187 static ConnectionData *
188 new_connection_data (McAccount *account)
190 ConnectionData *cd = g_slice_new0 (ConnectionData);
191 cd->account = g_object_ref (account);
193 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
194 g_free, (GDestroyNotify) free_dispatch_data);
196 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
197 g_free, g_object_unref);
199 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
206 free_connection_data (ConnectionData *cd)
209 g_object_unref (cd->account);
210 g_hash_table_destroy (cd->dispatched_channels);
211 g_hash_table_destroy (cd->dispatching_channels);
214 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
216 free_dispatcher_request_data (l->data);
219 if (cd->requestable_channels != NULL)
221 for (i = 0 ; i < cd->requestable_channels->len ; i++)
223 g_ptr_array_index (cd->requestable_channels, i));
224 g_ptr_array_free (cd->requestable_channels, TRUE);
229 dispatcher_connection_invalidated_cb (TpConnection *connection,
233 EmpathyDispatcher *dispatcher)
235 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
238 DEBUG ("Error: %s", message);
239 cd = g_hash_table_lookup (priv->connections, connection);
241 g_hash_table_remove (priv->accounts, cd->account);
242 g_hash_table_remove (priv->connections, connection);
246 dispatcher_operation_can_start (EmpathyDispatcher *self,
247 EmpathyDispatchOperation *operation,
251 const gchar *channel_type =
252 empathy_dispatch_operation_get_channel_type (operation);
254 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
256 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
258 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
268 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
269 EmpathyDispatchOperation *operation,
275 l = cd->outstanding_requests;
278 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
283 if (d->operation == operation)
288 d->cb (NULL, error, d->user_data);
290 d->cb (operation, NULL, d->user_data);
293 cd->outstanding_requests = g_list_delete_link
294 (cd->outstanding_requests, lt);
296 free_dispatcher_request_data (d);
302 dispatcher_channel_invalidated_cb (TpProxy *proxy,
306 EmpathyDispatcher *dispatcher)
308 /* Channel went away... */
309 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
310 TpConnection *connection;
311 EmpathyDispatchOperation *operation;
313 const gchar *object_path;
315 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
317 cd = g_hash_table_lookup (priv->connections, connection);
318 /* Connection itself invalidated? */
322 object_path = tp_proxy_get_object_path (proxy);
324 DEBUG ("Channel %s invalidated", object_path);
326 g_hash_table_remove (cd->dispatched_channels, object_path);
327 g_hash_table_remove (cd->dispatching_channels, object_path);
329 priv->channels = g_list_remove (priv->channels, proxy);
331 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
332 if (operation != NULL)
334 GError error = { domain, code, message };
335 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
336 g_hash_table_remove (cd->outstanding_channels, object_path);
337 g_object_unref (operation);
342 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
343 EmpathyDispatcher *dispatcher)
345 g_assert (empathy_dispatch_operation_is_incoming (operation));
346 DEBUG ("Send of for dispatching: %s",
347 empathy_dispatch_operation_get_object_path (operation));
348 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
352 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
353 EmpathyDispatcher *dispatcher)
355 /* Our job is done, remove the dispatch operation and mark the channel as
357 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
358 TpConnection *connection;
360 const gchar *object_path;
362 connection = empathy_dispatch_operation_get_tp_connection (operation);
363 cd = g_hash_table_lookup (priv->connections, connection);
364 g_assert (cd != NULL);
365 g_object_unref (G_OBJECT (connection));
367 object_path = empathy_dispatch_operation_get_object_path (operation);
369 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
372 d = new_dispatch_data (
373 empathy_dispatch_operation_get_channel (operation),
374 empathy_dispatch_operation_get_channel_wrapper (operation));
375 g_hash_table_insert (cd->dispatched_channels,
376 g_strdup (object_path), d);
378 g_hash_table_remove (cd->dispatching_channels, object_path);
380 DEBUG ("Channel claimed: %s", object_path);
384 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
385 EmpathyDispatcher *dispatcher)
387 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
388 TpConnection *connection;
390 EmpathyDispatchOperationState status;
392 g_signal_connect (operation, "approved",
393 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
395 g_signal_connect (operation, "claimed",
396 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
398 /* Signal the observers */
399 DEBUG ("Send to observers: %s",
400 empathy_dispatch_operation_get_object_path (operation));
401 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
403 empathy_dispatch_operation_start (operation);
405 /* Signal potential requestors */
406 connection = empathy_dispatch_operation_get_tp_connection (operation);
407 cd = g_hash_table_lookup (priv->connections, connection);
408 g_assert (cd != NULL);
409 g_object_unref (G_OBJECT (connection));
411 g_object_ref (operation);
412 g_object_ref (dispatcher);
414 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
415 status = empathy_dispatch_operation_get_status (operation);
416 g_object_unref (operation);
418 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
421 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
423 DEBUG ("Send to approvers: %s",
424 empathy_dispatch_operation_get_object_path (operation));
425 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
429 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
430 DEBUG ("Send of for dispatching: %s",
431 empathy_dispatch_operation_get_object_path (operation));
432 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
435 g_object_unref (dispatcher);
439 dispatcher_start_dispatching (EmpathyDispatcher *self,
440 EmpathyDispatchOperation *operation,
443 const gchar *object_path =
444 empathy_dispatch_operation_get_object_path (operation);
446 DEBUG ("Dispatching process started for %s", object_path);
448 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
450 g_assert (g_hash_table_lookup (cd->outstanding_channels,
451 object_path) == NULL);
453 g_hash_table_insert (cd->dispatching_channels,
454 g_strdup (object_path), operation);
456 switch (empathy_dispatch_operation_get_status (operation))
458 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
459 g_signal_connect (operation, "ready",
460 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
462 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
463 dispatch_operation_ready_cb (operation, dispatcher);
466 g_assert_not_reached();
470 else if (empathy_dispatch_operation_get_status (operation) >=
471 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
473 /* Already dispatching and the operation is pending, thus the observers
474 * have seen it (if applicable), so we can flush the request right away.
476 dispatch_operation_flush_requests (self, operation, NULL, cd);
481 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
487 g_hash_table_iter_init (&iter, cd->outstanding_channels);
488 while (g_hash_table_iter_next (&iter, NULL, &value))
490 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
492 if (dispatcher_operation_can_start (self, operation, cd))
494 dispatcher_start_dispatching (dispatcher, operation, cd);
495 g_hash_table_iter_remove (&iter);
501 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
502 TpConnection *connection,
503 const gchar *object_path,
504 const gchar *channel_type,
507 GHashTable *properties,
510 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
513 EmpathyDispatchOperation *operation;
514 EmpathyContact *contact = NULL;
516 /* Channel types we never want to dispatch because they're either deprecated
517 * or can't sensibly be dispatch (e.g. channels that should always be
519 const char *blacklist[] = {
520 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
521 TP_IFACE_CHANNEL_TYPE_TUBES,
522 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
526 cd = g_hash_table_lookup (priv->connections, connection);
528 /* Don't bother with channels we have already dispatched or are dispatching
529 * currently. This can happen when NewChannel(s) is fired after
530 * RequestChannel/CreateChannel/EnsureChannel */
531 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
534 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
537 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
539 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
542 /* Only pick up non-requested text and file channels. For all other it
543 * doesn't make sense to handle it if we didn't request it. The same goes
544 * for channels we discovered by the Channels property or ListChannels */
545 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
546 && tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
548 DEBUG ("Ignoring incoming channel of type %s on %s",
549 channel_type, object_path);
553 for (i = 0 ; blacklist[i] != NULL; i++)
555 if (!tp_strdiff (channel_type, blacklist[i]))
557 DEBUG ("Ignoring blacklisted channel type %s on %s",
558 channel_type, object_path);
563 DEBUG ("New channel of type %s on %s", channel_type, object_path);
565 if (properties == NULL)
566 channel = tp_channel_new (connection, object_path, channel_type,
567 handle_type, handle, NULL);
569 channel = tp_channel_new_from_properties (connection, object_path,
572 g_signal_connect (channel, "invalidated",
573 G_CALLBACK (dispatcher_channel_invalidated_cb),
576 priv->channels = g_list_prepend (priv->channels, channel);
578 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
580 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
581 contact = empathy_contact_factory_get_from_handle (factory,
582 cd->account, handle);
583 g_object_unref (factory);
586 operation = empathy_dispatch_operation_new (connection, channel, contact,
589 g_object_unref (channel);
593 /* Request could either be by us or by a remote party. If there are no
594 * outstanding requests for this channel type we can assume it's remote.
595 * Otherwise we wait untill they are all satisfied */
596 if (dispatcher_operation_can_start (dispatcher, operation, cd))
597 dispatcher_start_dispatching (dispatcher, operation, cd);
599 g_hash_table_insert (cd->outstanding_channels,
600 g_strdup (object_path), operation);
604 dispatcher_start_dispatching (dispatcher, operation, cd);
609 dispatcher_connection_new_channel_cb (TpConnection *connection,
610 const gchar *object_path,
611 const gchar *channel_type,
614 gboolean suppress_handler,
618 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
620 /* Empathy heavily abuses surpress handler (don't try this at home), if
621 * surpress handler is true then it is an outgoing channel, which is
622 * requested either by us or some other party (like the megaphone applet).
623 * Otherwise it's an incoming channel */
624 dispatcher_connection_new_channel (dispatcher, connection,
625 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
629 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
630 TpConnection *connection,
631 const gchar *object_path,
632 GHashTable *properties)
634 const gchar *channel_type;
641 channel_type = tp_asv_get_string (properties,
642 TP_IFACE_CHANNEL ".ChannelType");
643 if (channel_type == NULL)
645 g_message ("%s had an invalid ChannelType property", object_path);
649 handle_type = tp_asv_get_uint32 (properties,
650 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
653 g_message ("%s had an invalid TargetHandleType property", object_path);
657 handle = tp_asv_get_uint32 (properties,
658 TP_IFACE_CHANNEL ".TargetHandle", &valid);
661 g_message ("%s had an invalid TargetHandle property", object_path);
665 /* We assume there is no channel dispather, so we're the only one dispatching
666 * it. Which means that a requested channel it is outgoing one */
667 requested = tp_asv_get_boolean (properties,
668 TP_IFACE_CHANNEL ".Requested", &valid);
671 g_message ("%s had an invalid Requested property", object_path);
675 dispatcher_connection_new_channel (dispatcher, connection,
676 object_path, channel_type, handle_type, handle, properties, !requested);
680 dispatcher_connection_new_channels_cb (TpConnection *connection,
681 const GPtrArray *channels,
685 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
688 for (i = 0; i < channels->len ; i++)
690 GValueArray *arr = g_ptr_array_index (channels, i);
691 const gchar *object_path;
692 GHashTable *properties;
694 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
695 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
697 dispatcher_connection_new_channel_with_properties (dispatcher,
698 connection, object_path, properties);
703 dispatcher_connection_got_all (TpProxy *proxy,
704 GHashTable *properties,
709 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
710 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
712 GPtrArray *requestable_channels;
715 DEBUG ("Error: %s", error->message);
719 channels = tp_asv_get_boxed (properties, "Channels",
720 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
722 if (channels == NULL)
723 DEBUG ("No Channels property !?! on connection");
725 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
726 channels, NULL, object);
728 requestable_channels = tp_asv_get_boxed (properties,
729 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
731 if (requestable_channels == NULL)
732 DEBUG ("No RequestableChannelClasses property !?! on connection");
737 cd = g_hash_table_lookup (priv->connections, proxy);
738 g_assert (cd != NULL);
740 cd->requestable_channels = g_boxed_copy (
741 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
746 dispatcher_connection_list_channels_cb (TpConnection *connection,
747 const GPtrArray *channels,
756 DEBUG ("Error: %s", error->message);
760 for (i = 0; i < channels->len; i++)
764 values = g_ptr_array_index (channels, i);
765 /* We don't have any extra info, so assume already existing channels are
767 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
769 g_value_get_boxed (g_value_array_get_nth (values, 0)),
770 g_value_get_string (g_value_array_get_nth (values, 1)),
771 g_value_get_uint (g_value_array_get_nth (values, 2)),
772 g_value_get_uint (g_value_array_get_nth (values, 3)),
778 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
779 const GPtrArray *capabilities,
785 DEBUG ("Error: %s", error->message);
789 dispatcher_connection_ready_cb (TpConnection *connection,
793 GPtrArray *capabilities;
796 const gchar *remove = NULL;
800 dispatcher_connection_invalidated_cb (connection, error->domain,
801 error->code, error->message, dispatcher);
805 g_signal_connect (connection, "invalidated",
806 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
808 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
809 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
811 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
812 dispatcher_connection_new_channels_cb,
813 NULL, NULL, G_OBJECT (dispatcher), NULL);
815 tp_cli_dbus_properties_call_get_all (connection, -1,
816 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
817 dispatcher_connection_got_all,
818 NULL, NULL, dispatcher);
822 tp_cli_connection_connect_to_new_channel (connection,
823 dispatcher_connection_new_channel_cb,
824 NULL, NULL, G_OBJECT (dispatcher), NULL);
826 tp_cli_connection_call_list_channels (connection, -1,
827 dispatcher_connection_list_channels_cb, NULL, NULL,
828 G_OBJECT (dispatcher));
832 /* Advertise VoIP capabilities */
833 /* FIXME: Capabilities is leaked */
834 capabilities = g_ptr_array_sized_new (1);
835 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
836 G_TYPE_UINT, G_TYPE_INVALID);
837 g_value_init (&cap, cap_type);
838 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
839 dbus_g_type_struct_set (&cap,
840 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
841 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
842 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
843 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
844 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
845 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
847 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
848 connection, -1, capabilities, &remove,
849 dispatcher_connection_advertise_capabilities_cb,
850 NULL, NULL, G_OBJECT (dispatcher));
854 dispatcher_update_account (EmpathyDispatcher *dispatcher,
857 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
858 TpConnection *connection;
860 connection = g_hash_table_lookup (priv->accounts, account);
861 if (connection != NULL)
864 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
865 if (connection == NULL)
868 g_hash_table_insert (priv->connections, g_object_ref (connection),
869 new_connection_data (account));
871 g_hash_table_insert (priv->accounts, g_object_ref (account),
872 g_object_ref (connection));
874 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
877 g_object_unref (connection);
881 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
883 TpConnectionStatusReason reason,
884 TpConnectionStatus status,
885 TpConnectionStatus previous,
886 EmpathyDispatcher *dispatcher)
888 dispatcher_update_account (dispatcher, account);
892 dispatcher_constructor (GType type,
893 guint n_construct_params,
894 GObjectConstructParam *construct_params)
898 if (dispatcher == NULL)
900 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
901 (type, n_construct_params, construct_params);
903 dispatcher = EMPATHY_DISPATCHER (retval);
904 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
908 retval = g_object_ref (dispatcher);
915 dispatcher_finalize (GObject *object)
917 EmpathyDispatcherPriv *priv = GET_PRIV (object);
922 g_signal_handlers_disconnect_by_func (priv->account_manager,
923 dispatcher_account_connection_cb, object);
925 for (l = priv->channels; l; l = l->next)
927 g_signal_handlers_disconnect_by_func (l->data,
928 dispatcher_channel_invalidated_cb, object);
931 g_list_free (priv->channels);
933 g_hash_table_iter_init (&iter, priv->connections);
934 while (g_hash_table_iter_next (&iter, &connection, NULL))
936 g_signal_handlers_disconnect_by_func (connection,
937 dispatcher_connection_invalidated_cb, object);
940 g_object_unref (priv->account_manager);
941 g_object_unref (priv->mc);
943 g_hash_table_destroy (priv->accounts);
944 g_hash_table_destroy (priv->connections);
948 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
950 GObjectClass *object_class = G_OBJECT_CLASS (klass);
952 object_class->finalize = dispatcher_finalize;
953 object_class->constructor = dispatcher_constructor;
956 g_signal_new ("observe",
957 G_TYPE_FROM_CLASS (klass),
961 g_cclosure_marshal_VOID__OBJECT,
963 1, EMPATHY_TYPE_DISPATCH_OPERATION);
966 g_signal_new ("approve",
967 G_TYPE_FROM_CLASS (klass),
971 g_cclosure_marshal_VOID__OBJECT,
973 1, EMPATHY_TYPE_DISPATCH_OPERATION);
976 g_signal_new ("dispatch",
977 G_TYPE_FROM_CLASS (klass),
981 g_cclosure_marshal_VOID__OBJECT,
983 1, EMPATHY_TYPE_DISPATCH_OPERATION);
985 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
990 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
993 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
994 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
996 dispatcher->priv = priv;
997 priv->mc = empathy_mission_control_dup_singleton ();
998 priv->account_manager = empathy_account_manager_dup_singleton ();
1000 g_signal_connect (priv->account_manager,
1001 "account-connection-changed",
1002 G_CALLBACK (dispatcher_account_connection_cb),
1005 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1006 empathy_account_equal, g_object_unref, g_object_unref);
1008 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1009 g_object_unref, (GDestroyNotify) free_connection_data);
1011 priv->channels = NULL;
1013 accounts = mc_accounts_list_by_enabled (TRUE);
1015 for (l = accounts; l; l = l->next)
1017 dispatcher_update_account (dispatcher, l->data);
1018 g_object_unref (l->data);
1020 g_list_free (accounts);
1024 empathy_dispatcher_dup_singleton (void)
1026 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1030 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1031 DispatcherRequestData *request_data,
1032 const GError *error)
1034 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1035 ConnectionData *conn_data;
1037 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1038 if (request_data->cb != NULL)
1039 request_data->cb (NULL, error, request_data->user_data);
1041 conn_data->outstanding_requests =
1042 g_list_remove (conn_data->outstanding_requests, request_data);
1043 free_dispatcher_request_data (request_data);
1047 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1048 DispatcherRequestData *request_data,
1049 const gchar *object_path,
1050 GHashTable *properties,
1051 const GError *error)
1053 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1054 EmpathyDispatchOperation *operation = NULL;
1055 ConnectionData *conn_data;
1057 conn_data = g_hash_table_lookup (priv->connections,
1058 request_data->connection);
1062 DEBUG ("Channel request failed: %s", error->message);
1064 dispatcher_request_failed (dispatcher, request_data, error);
1069 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1072 if (operation != NULL)
1073 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1075 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1078 if (operation == NULL)
1080 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1085 operation = empathy_dispatch_operation_new_with_wrapper (
1086 request_data->connection,
1087 data->channel, request_data->contact, FALSE,
1088 data->channel_wrapper);
1094 if (properties != NULL)
1095 channel = tp_channel_new_from_properties (request_data->connection,
1096 object_path, properties, NULL);
1098 channel = tp_channel_new (request_data->connection, object_path,
1099 request_data->channel_type, request_data->handle_type,
1100 request_data->handle, NULL);
1102 g_signal_connect (channel, "invalidated",
1103 G_CALLBACK (dispatcher_channel_invalidated_cb),
1104 request_data->dispatcher);
1106 priv->channels = g_list_prepend (priv->channels, channel);
1108 operation = empathy_dispatch_operation_new (request_data->connection,
1109 channel, request_data->contact, FALSE);
1110 g_object_unref (channel);
1115 /* Already existed set potential extra information */
1116 g_object_set (G_OBJECT (operation),
1117 "contact", request_data->contact,
1121 request_data->operation = operation;
1123 /* (pre)-approve this right away as we requested it */
1124 empathy_dispatch_operation_approve (operation);
1126 dispatcher_start_dispatching (request_data->dispatcher, operation,
1129 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1134 dispatcher_request_channel_cb (TpConnection *connection,
1135 const gchar *object_path,
1136 const GError *error,
1138 GObject *weak_object)
1140 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1141 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1143 dispatcher_connection_new_requested_channel (dispatcher,
1144 request_data, object_path, NULL, error);
1148 dispatcher_request_channel (DispatcherRequestData *request_data)
1150 tp_cli_connection_call_request_channel (request_data->connection, -1,
1151 request_data->channel_type,
1152 request_data->handle_type,
1153 request_data->handle,
1154 TRUE, dispatcher_request_channel_cb,
1155 request_data, NULL, G_OBJECT (request_data->dispatcher));
1159 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1160 EmpathyDispatcherRequestCb *callback,
1163 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1164 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1166 TpConnection *connection;
1168 DispatcherRequestData *request_data;
1170 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1172 account = empathy_contact_get_account (contact);
1173 connection = g_hash_table_lookup (priv->accounts, account);
1175 g_assert (connection != NULL);
1176 cd = g_hash_table_lookup (priv->connections, connection);
1177 request_data = new_dispatcher_request_data (dispatcher, connection,
1178 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1179 contact, callback, user_data);
1181 cd->outstanding_requests = g_list_prepend
1182 (cd->outstanding_requests, request_data);
1184 dispatcher_request_channel (request_data);
1186 g_object_unref (dispatcher);
1190 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1191 const GError *error,
1195 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1197 request_data->handle = empathy_contact_get_handle (contact);
1199 dispatcher_request_channel (request_data);
1203 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1204 EmpathyDispatcherRequestCb *callback,
1207 EmpathyDispatcher *dispatcher;
1208 EmpathyDispatcherPriv *priv;
1210 TpConnection *connection;
1211 ConnectionData *connection_data;
1212 DispatcherRequestData *request_data;
1214 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1216 dispatcher = empathy_dispatcher_dup_singleton();
1217 priv = GET_PRIV (dispatcher);
1219 account = empathy_contact_get_account (contact);
1220 connection = g_hash_table_lookup (priv->accounts, account);
1221 connection_data = g_hash_table_lookup (priv->connections, connection);
1223 /* The contact handle might not be known yet */
1224 request_data = new_dispatcher_request_data (dispatcher, connection,
1225 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1226 contact, callback, user_data);
1228 connection_data->outstanding_requests = g_list_prepend
1229 (connection_data->outstanding_requests, request_data);
1231 empathy_contact_call_when_ready (contact,
1232 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1233 request_data, NULL, G_OBJECT (dispatcher));
1235 g_object_unref (dispatcher);
1239 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1240 const gchar *contact_id,
1241 EmpathyDispatcherRequestCb *callback,
1244 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1245 EmpathyContactFactory *factory;
1246 EmpathyContact *contact;
1248 g_return_if_fail (MC_IS_ACCOUNT (account));
1249 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1251 factory = empathy_contact_factory_dup_singleton ();
1252 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1254 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1256 g_object_unref (contact);
1257 g_object_unref (factory);
1258 g_object_unref (dispatcher);
1262 dispatcher_request_handles_cb (TpConnection *connection,
1263 const GArray *handles,
1264 const GError *error,
1268 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1272 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1273 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1276 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1278 if (request_data->cb)
1279 request_data->cb (NULL, error, request_data->user_data);
1281 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1284 free_dispatcher_request_data (request_data);
1286 dispatcher_flush_outstanding_operations (dispatcher, cd);
1290 request_data->handle = g_array_index (handles, guint, 0);
1291 dispatcher_request_channel (request_data);
1295 empathy_dispatcher_join_muc (McAccount *account,
1296 const gchar *roomname,
1297 EmpathyDispatcherRequestCb *callback,
1300 EmpathyDispatcher *dispatcher;
1301 EmpathyDispatcherPriv *priv;
1302 DispatcherRequestData *request_data;
1303 TpConnection *connection;
1304 ConnectionData *connection_data;
1305 const gchar *names[] = { roomname, NULL };
1307 g_return_if_fail (MC_IS_ACCOUNT (account));
1308 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1310 dispatcher = empathy_dispatcher_dup_singleton();
1311 priv = GET_PRIV (dispatcher);
1313 connection = g_hash_table_lookup (priv->accounts, account);
1314 connection_data = g_hash_table_lookup (priv->connections, connection);
1317 /* Don't know the room handle yet */
1318 request_data = new_dispatcher_request_data (dispatcher, connection,
1319 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1320 NULL, callback, user_data);
1322 connection_data->outstanding_requests = g_list_prepend
1323 (connection_data->outstanding_requests, request_data);
1325 tp_cli_connection_call_request_handles (connection, -1,
1326 TP_HANDLE_TYPE_ROOM, names,
1327 dispatcher_request_handles_cb, request_data, NULL,
1328 G_OBJECT (dispatcher));
1330 g_object_unref (dispatcher);
1334 dispatcher_create_channel_cb (TpConnection *connect,
1335 const gchar *object_path,
1336 GHashTable *properties,
1337 const GError *error,
1339 GObject *weak_object)
1341 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1342 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1344 dispatcher_connection_new_requested_channel (dispatcher,
1345 request_data, object_path, properties, error);
1349 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1351 GHashTable *request,
1352 EmpathyDispatcherRequestCb *callback,
1355 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1356 ConnectionData *connection_data;
1357 DispatcherRequestData *request_data;
1358 const gchar *channel_type;
1362 TpConnection *connection;
1364 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1365 g_return_if_fail (MC_IS_ACCOUNT (account));
1366 g_return_if_fail (request != NULL);
1368 connection = g_hash_table_lookup (priv->accounts, account);
1369 g_assert (connection != NULL);
1371 connection_data = g_hash_table_lookup (priv->connections, connection);
1372 g_assert (connection_data != NULL);
1374 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1376 handle_type = tp_asv_get_uint32 (request,
1377 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1379 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1381 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1383 request_data = new_dispatcher_request_data (dispatcher, connection,
1384 channel_type, handle_type, handle, request,
1385 NULL, callback, user_data);
1387 connection_data->outstanding_requests = g_list_prepend
1388 (connection_data->outstanding_requests, request_data);
1390 tp_cli_connection_interface_requests_call_create_channel (
1391 request_data->connection, -1,
1392 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1393 G_OBJECT (request_data->dispatcher));
1397 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1398 const GError *error,
1402 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1403 GValue *target_handle;
1405 g_assert (request_data->request);
1409 dispatcher_request_failed (request_data->dispatcher,
1410 request_data, error);
1414 request_data->handle = empathy_contact_get_handle (contact);
1416 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1417 g_value_set_uint (target_handle, request_data->handle);
1418 g_hash_table_insert (request_data->request,
1419 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1421 tp_cli_connection_interface_requests_call_create_channel (
1422 request_data->connection, -1,
1423 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1424 G_OBJECT (request_data->dispatcher));
1428 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1429 const GError *error,
1432 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1436 dispatcher_request_failed (request_data->dispatcher,
1437 request_data, error);
1441 empathy_contact_call_when_ready (request_data->contact,
1442 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1443 request_data, NULL, G_OBJECT (request_data->dispatcher));
1447 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1448 const gchar *filename,
1451 const gchar *content_type,
1452 EmpathyDispatcherRequestCb *callback,
1455 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1456 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1457 McAccount *account = empathy_contact_get_account (contact);
1458 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1459 ConnectionData *connection_data =
1460 g_hash_table_lookup (priv->connections, connection);
1461 DispatcherRequestData *request_data;
1463 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1464 (GDestroyNotify) tp_g_value_slice_free);
1466 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1467 g_return_if_fail (!EMP_STR_EMPTY (filename));
1468 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1470 /* org.freedesktop.Telepathy.Channel.ChannelType */
1471 value = tp_g_value_slice_new (G_TYPE_STRING);
1472 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1473 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1475 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1476 value = tp_g_value_slice_new (G_TYPE_UINT);
1477 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1478 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1480 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1481 value = tp_g_value_slice_new (G_TYPE_STRING);
1482 g_value_set_string (value, content_type);
1483 g_hash_table_insert (request,
1484 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1486 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1487 value = tp_g_value_slice_new (G_TYPE_STRING);
1488 g_value_set_string (value, filename);
1489 g_hash_table_insert (request,
1490 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1492 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1493 value = tp_g_value_slice_new (G_TYPE_UINT64);
1494 g_value_set_uint64 (value, size);
1495 g_hash_table_insert (request,
1496 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1498 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1499 value = tp_g_value_slice_new (G_TYPE_UINT64);
1500 g_value_set_uint64 (value, date);
1501 g_hash_table_insert (request,
1502 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1505 /* The contact handle might not be known yet */
1506 request_data = new_dispatcher_request_data (dispatcher, connection,
1507 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1508 contact, callback, user_data);
1509 connection_data->outstanding_requests = g_list_prepend
1510 (connection_data->outstanding_requests, request_data);
1512 tp_connection_call_when_ready (connection,
1513 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1515 g_object_unref (dispatcher);
1519 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1521 const gchar *channel_type,
1524 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1526 TpConnection *connection;
1530 g_return_val_if_fail (channel_type != NULL, NULL);
1531 g_return_val_if_fail (handle_type != 0, NULL);
1533 connection = g_hash_table_lookup (priv->accounts, account);
1535 if (connection == NULL)
1538 cd = g_hash_table_lookup (priv->connections, connection);
1544 classes = cd->requestable_channels;
1545 if (classes == NULL)
1548 for (i = 0; i < classes->len; i++)
1554 const gchar *c_type;
1558 class = g_ptr_array_index (classes, i);
1559 fixed = g_value_array_get_nth (class, 0);
1561 fprops = g_value_get_boxed (fixed);
1562 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1564 if (tp_strdiff (channel_type, c_type))
1567 h_type = tp_asv_get_uint32 (fprops,
1568 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1570 if (!valid || handle_type != h_type)
1573 allowed = g_value_array_get_nth (class, 1);
1575 return g_value_get_boxed (allowed);