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 d->channel_wrapper = g_object_ref (channel_wrapper);
130 free_dispatch_data (DispatchData *data)
132 g_object_unref (data->channel);
133 g_object_unref (data->channel_wrapper);
135 g_slice_free (DispatchData, data);
138 static DispatcherRequestData *
139 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
140 TpConnection *connection,
141 const gchar *channel_type,
145 EmpathyContact *contact,
146 EmpathyDispatcherRequestCb *cb,
149 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
151 result->dispatcher = g_object_ref (dispatcher);
152 result->connection = connection;
154 result->channel_type = g_strdup (channel_type);
155 result->handle_type = handle_type;
156 result->handle = handle;
157 result->request = request;
160 result->contact = g_object_ref (contact);
163 result->user_data = user_data;
169 free_dispatcher_request_data (DispatcherRequestData *r)
171 g_free (r->channel_type);
173 if (r->dispatcher != NULL)
174 g_object_unref (r->dispatcher);
176 if (r->contact != NULL)
177 g_object_unref (r->contact);
179 if (r->request != NULL)
180 g_hash_table_unref (r->request);
182 g_slice_free (DispatcherRequestData, r);
185 static ConnectionData *
186 new_connection_data (McAccount *account)
188 ConnectionData *cd = g_slice_new0 (ConnectionData);
189 cd->account = g_object_ref (account);
191 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
192 g_free, (GDestroyNotify) free_dispatch_data);
194 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
195 g_free, g_object_unref);
197 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
204 free_connection_data (ConnectionData *cd)
207 g_object_unref (cd->account);
208 g_hash_table_destroy (cd->dispatched_channels);
209 g_hash_table_destroy (cd->dispatching_channels);
212 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
214 free_dispatcher_request_data (l->data);
217 if (cd->requestable_channels != NULL)
219 for (i = 0 ; i < cd->requestable_channels->len ; i++)
221 g_ptr_array_index (cd->requestable_channels, i));
222 g_ptr_array_free (cd->requestable_channels, TRUE);
227 dispatcher_connection_invalidated_cb (TpConnection *connection,
231 EmpathyDispatcher *dispatcher)
233 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
236 DEBUG ("Error: %s", message);
237 cd = g_hash_table_lookup (priv->connections, connection);
239 g_hash_table_remove (priv->accounts, cd->account);
240 g_hash_table_remove (priv->connections, connection);
244 dispatcher_operation_can_start (EmpathyDispatcher *self,
245 EmpathyDispatchOperation *operation,
249 const gchar *channel_type =
250 empathy_dispatch_operation_get_channel_type (operation);
252 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
254 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
256 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
266 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
267 EmpathyDispatchOperation *operation,
273 l = cd->outstanding_requests;
276 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
281 if (d->operation == operation)
286 d->cb (NULL, error, d->user_data);
288 d->cb (operation, NULL, d->user_data);
291 cd->outstanding_requests = g_list_delete_link
292 (cd->outstanding_requests, lt);
294 free_dispatcher_request_data (d);
300 dispatcher_channel_invalidated_cb (TpProxy *proxy,
304 EmpathyDispatcher *dispatcher)
306 /* Channel went away... */
307 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
308 TpConnection *connection;
309 EmpathyDispatchOperation *operation;
311 const gchar *object_path;
313 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
315 cd = g_hash_table_lookup (priv->connections, connection);
316 /* Connection itself invalidated? */
320 object_path = tp_proxy_get_object_path (proxy);
322 DEBUG ("Channel %s invalidated", object_path);
324 g_hash_table_remove (cd->dispatched_channels, object_path);
325 g_hash_table_remove (cd->dispatching_channels, object_path);
327 priv->channels = g_list_remove (priv->channels, proxy);
329 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
330 if (operation != NULL)
332 GError error = { domain, code, message };
333 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
334 g_hash_table_remove (cd->outstanding_channels, object_path);
335 g_object_unref (operation);
340 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
341 EmpathyDispatcher *dispatcher)
343 g_assert (empathy_dispatch_operation_is_incoming (operation));
344 DEBUG ("Send of for dispatching: %s",
345 empathy_dispatch_operation_get_object_path (operation));
346 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
350 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
351 EmpathyDispatcher *dispatcher)
353 /* Our job is done, remove the dispatch operation and mark the channel as
355 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
356 TpConnection *connection;
358 const gchar *object_path;
360 connection = empathy_dispatch_operation_get_tp_connection (operation);
361 cd = g_hash_table_lookup (priv->connections, connection);
362 g_assert (cd != NULL);
363 g_object_unref (G_OBJECT (connection));
365 object_path = empathy_dispatch_operation_get_object_path (operation);
367 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
370 d = new_dispatch_data (
371 empathy_dispatch_operation_get_channel (operation),
372 empathy_dispatch_operation_get_channel_wrapper (operation));
373 g_hash_table_insert (cd->dispatched_channels,
374 g_strdup (object_path), d);
376 g_hash_table_remove (cd->dispatching_channels, object_path);
378 DEBUG ("Channel claimed: %s", object_path);
382 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
383 EmpathyDispatcher *dispatcher)
385 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
386 TpConnection *connection;
388 EmpathyDispatchOperationState status;
390 g_signal_connect (operation, "approved",
391 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
393 g_signal_connect (operation, "claimed",
394 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
396 /* Signal the observers */
397 DEBUG ("Send to observers: %s",
398 empathy_dispatch_operation_get_object_path (operation));
399 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
401 empathy_dispatch_operation_start (operation);
403 /* Signal potential requestors */
404 connection = empathy_dispatch_operation_get_tp_connection (operation);
405 cd = g_hash_table_lookup (priv->connections, connection);
406 g_assert (cd != NULL);
407 g_object_unref (G_OBJECT (connection));
409 g_object_ref (operation);
410 g_object_ref (dispatcher);
412 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
413 status = empathy_dispatch_operation_get_status (operation);
414 g_object_unref (operation);
416 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
419 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
421 DEBUG ("Send to approvers: %s",
422 empathy_dispatch_operation_get_object_path (operation));
423 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
427 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
428 DEBUG ("Send of for dispatching: %s",
429 empathy_dispatch_operation_get_object_path (operation));
430 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
433 g_object_unref (dispatcher);
437 dispatcher_start_dispatching (EmpathyDispatcher *self,
438 EmpathyDispatchOperation *operation,
441 const gchar *object_path =
442 empathy_dispatch_operation_get_object_path (operation);
444 DEBUG ("Dispatching process started for %s", object_path);
446 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
448 g_assert (g_hash_table_lookup (cd->outstanding_channels,
449 object_path) == NULL);
451 g_hash_table_insert (cd->dispatching_channels,
452 g_strdup (object_path), operation);
454 switch (empathy_dispatch_operation_get_status (operation))
456 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
457 g_signal_connect (operation, "ready",
458 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
460 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
461 dispatch_operation_ready_cb (operation, dispatcher);
464 g_assert_not_reached();
468 else if (empathy_dispatch_operation_get_status (operation) >=
469 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
471 /* Already dispatching and the operation is pending, thus the observers
472 * have seen it (if applicable), so we can flush the request right away.
474 dispatch_operation_flush_requests (self, operation, NULL, cd);
479 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
485 g_hash_table_iter_init (&iter, cd->outstanding_channels);
486 while (g_hash_table_iter_next (&iter, NULL, &value))
488 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
490 if (dispatcher_operation_can_start (self, operation, cd))
492 dispatcher_start_dispatching (dispatcher, operation, cd);
493 g_hash_table_iter_remove (&iter);
499 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
500 TpConnection *connection,
501 const gchar *object_path,
502 const gchar *channel_type,
505 GHashTable *properties,
508 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
511 EmpathyDispatchOperation *operation;
512 EmpathyContact *contact = NULL;
514 /* Channel types we never want to dispatch because they're either deprecated
515 * or can't sensibly be dispatch (e.g. channels that should always be
517 const char *blacklist[] = {
518 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
519 TP_IFACE_CHANNEL_TYPE_TUBES,
520 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
524 cd = g_hash_table_lookup (priv->connections, connection);
526 /* Don't bother with channels we have already dispatched or are dispatching
527 * currently. This can happen when NewChannel(s) is fired after
528 * RequestChannel/CreateChannel/EnsureChannel */
529 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
532 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
535 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
537 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
540 /* Only pick up non-requested text and file channels. For all other it
541 * doesn't make sense to handle it if we didn't request it. The same goes
542 * for channels we discovered by the Channels property or ListChannels */
543 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
544 && tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
546 DEBUG ("Ignoring incoming channel of type %s on %s",
547 channel_type, object_path);
551 for (i = 0 ; blacklist[i] != NULL; i++)
553 if (!tp_strdiff (channel_type, blacklist[i]))
555 DEBUG ("Ignoring blacklisted channel type %s on %s",
556 channel_type, object_path);
561 DEBUG ("New channel of type %s on %s", channel_type, object_path);
563 if (properties == NULL)
564 channel = tp_channel_new (connection, object_path, channel_type,
565 handle_type, handle, NULL);
567 channel = tp_channel_new_from_properties (connection, object_path,
570 g_signal_connect (channel, "invalidated",
571 G_CALLBACK (dispatcher_channel_invalidated_cb),
574 priv->channels = g_list_prepend (priv->channels, channel);
576 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
578 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
579 contact = empathy_contact_factory_get_from_handle (factory,
580 cd->account, handle);
581 g_object_unref (factory);
584 operation = empathy_dispatch_operation_new (connection, channel, contact,
587 g_object_unref (channel);
591 /* Request could either be by us or by a remote party. If there are no
592 * outstanding requests for this channel type we can assume it's remote.
593 * Otherwise we wait untill they are all satisfied */
594 if (dispatcher_operation_can_start (dispatcher, operation, cd))
595 dispatcher_start_dispatching (dispatcher, operation, cd);
597 g_hash_table_insert (cd->outstanding_channels,
598 g_strdup (object_path), operation);
602 dispatcher_start_dispatching (dispatcher, operation, cd);
607 dispatcher_connection_new_channel_cb (TpConnection *connection,
608 const gchar *object_path,
609 const gchar *channel_type,
612 gboolean suppress_handler,
616 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
618 /* Empathy heavily abuses surpress handler (don't try this at home), if
619 * surpress handler is true then it is an outgoing channel, which is
620 * requested either by us or some other party (like the megaphone applet).
621 * Otherwise it's an incoming channel */
622 dispatcher_connection_new_channel (dispatcher, connection,
623 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
627 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
628 TpConnection *connection,
629 const gchar *object_path,
630 GHashTable *properties)
632 const gchar *channel_type;
639 channel_type = tp_asv_get_string (properties,
640 TP_IFACE_CHANNEL ".ChannelType");
641 if (channel_type == NULL)
643 g_message ("%s had an invalid ChannelType property", object_path);
647 handle_type = tp_asv_get_uint32 (properties,
648 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
651 g_message ("%s had an invalid TargetHandleType property", object_path);
655 handle = tp_asv_get_uint32 (properties,
656 TP_IFACE_CHANNEL ".TargetHandle", &valid);
659 g_message ("%s had an invalid TargetHandle property", object_path);
663 /* We assume there is no channel dispather, so we're the only one dispatching
664 * it. Which means that a requested channel it is outgoing one */
665 requested = tp_asv_get_boolean (properties,
666 TP_IFACE_CHANNEL ".Requested", &valid);
669 g_message ("%s had an invalid Requested property", object_path);
673 dispatcher_connection_new_channel (dispatcher, connection,
674 object_path, channel_type, handle_type, handle, properties, !requested);
678 dispatcher_connection_new_channels_cb (TpConnection *connection,
679 const GPtrArray *channels,
683 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
686 for (i = 0; i < channels->len ; i++)
688 GValueArray *arr = g_ptr_array_index (channels, i);
689 const gchar *object_path;
690 GHashTable *properties;
692 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
693 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
695 dispatcher_connection_new_channel_with_properties (dispatcher,
696 connection, object_path, properties);
701 dispatcher_connection_got_all (TpProxy *proxy, GHashTable *properties,
702 const GError *error, gpointer user_data, GObject *object)
704 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
705 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
707 GPtrArray *requestable_channels;
710 DEBUG ("Error: %s", error->message);
714 channels = tp_asv_get_boxed (properties, "Channels",
715 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
717 if (channels == NULL)
718 DEBUG ("No Channels property !?! on connection");
720 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
721 channels, NULL, object);
723 requestable_channels = tp_asv_get_boxed (properties,
724 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
726 if (requestable_channels == NULL)
727 DEBUG ("No RequestableChannelClasses property !?! on connection");
732 cd = g_hash_table_lookup (priv->connections, proxy);
733 g_assert (cd != NULL);
735 cd->requestable_channels = g_boxed_copy (
736 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
741 dispatcher_connection_list_channels_cb (TpConnection *connection,
742 const GPtrArray *channels,
751 DEBUG ("Error: %s", error->message);
755 for (i = 0; i < channels->len; i++)
759 values = g_ptr_array_index (channels, i);
760 /* We don't have any extra info, so assume already existing channels are
762 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
764 g_value_get_boxed (g_value_array_get_nth (values, 0)),
765 g_value_get_string (g_value_array_get_nth (values, 1)),
766 g_value_get_uint (g_value_array_get_nth (values, 2)),
767 g_value_get_uint (g_value_array_get_nth (values, 3)),
773 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
774 const GPtrArray *capabilities,
780 DEBUG ("Error: %s", error->message);
784 dispatcher_connection_ready_cb (TpConnection *connection,
788 GPtrArray *capabilities;
791 const gchar *remove = NULL;
795 dispatcher_connection_invalidated_cb (connection, error->domain,
796 error->code, error->message, dispatcher);
800 g_signal_connect (connection, "invalidated",
801 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
803 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
804 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
806 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
807 dispatcher_connection_new_channels_cb,
808 NULL, NULL, G_OBJECT (dispatcher), NULL);
810 tp_cli_dbus_properties_call_get_all (connection, -1,
811 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
812 dispatcher_connection_got_all,
813 NULL, NULL, dispatcher);
817 tp_cli_connection_connect_to_new_channel (connection,
818 dispatcher_connection_new_channel_cb,
819 NULL, NULL, G_OBJECT (dispatcher), NULL);
821 tp_cli_connection_call_list_channels (connection, -1,
822 dispatcher_connection_list_channels_cb, NULL, NULL,
823 G_OBJECT (dispatcher));
827 /* Advertise VoIP capabilities */
828 /* FIXME: Capabilities is leaked */
829 capabilities = g_ptr_array_sized_new (1);
830 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
831 G_TYPE_UINT, G_TYPE_INVALID);
832 g_value_init (&cap, cap_type);
833 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
834 dbus_g_type_struct_set (&cap,
835 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
836 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
837 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
838 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
839 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
840 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
842 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
843 connection, -1, capabilities, &remove,
844 dispatcher_connection_advertise_capabilities_cb,
845 NULL, NULL, G_OBJECT (dispatcher));
849 dispatcher_update_account (EmpathyDispatcher *dispatcher,
852 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
853 TpConnection *connection;
855 connection = g_hash_table_lookup (priv->accounts, account);
856 if (connection != NULL)
859 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
860 if (connection == NULL)
863 g_hash_table_insert (priv->connections, g_object_ref (connection),
864 new_connection_data (account));
866 g_hash_table_insert (priv->accounts, g_object_ref (account),
867 g_object_ref (connection));
869 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
872 g_object_unref (connection);
876 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
878 TpConnectionStatusReason reason,
879 TpConnectionStatus status,
880 TpConnectionStatus previous,
881 EmpathyDispatcher *dispatcher)
883 dispatcher_update_account (dispatcher, account);
887 dispatcher_constructor (GType type,
888 guint n_construct_params,
889 GObjectConstructParam *construct_params)
893 if (dispatcher == NULL)
895 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
896 (type, n_construct_params, construct_params);
898 dispatcher = EMPATHY_DISPATCHER (retval);
899 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
903 retval = g_object_ref (dispatcher);
910 dispatcher_finalize (GObject *object)
912 EmpathyDispatcherPriv *priv = GET_PRIV (object);
917 g_signal_handlers_disconnect_by_func (priv->account_manager,
918 dispatcher_account_connection_cb, object);
920 for (l = priv->channels; l; l = l->next)
922 g_signal_handlers_disconnect_by_func (l->data,
923 dispatcher_channel_invalidated_cb, object);
926 g_list_free (priv->channels);
928 g_hash_table_iter_init (&iter, priv->connections);
929 while (g_hash_table_iter_next (&iter, &connection, NULL))
931 g_signal_handlers_disconnect_by_func (connection,
932 dispatcher_connection_invalidated_cb, object);
935 g_object_unref (priv->account_manager);
936 g_object_unref (priv->mc);
938 g_hash_table_destroy (priv->accounts);
939 g_hash_table_destroy (priv->connections);
943 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
945 GObjectClass *object_class = G_OBJECT_CLASS (klass);
947 object_class->finalize = dispatcher_finalize;
948 object_class->constructor = dispatcher_constructor;
951 g_signal_new ("observe",
952 G_TYPE_FROM_CLASS (klass),
956 g_cclosure_marshal_VOID__OBJECT,
958 1, EMPATHY_TYPE_DISPATCH_OPERATION);
961 g_signal_new ("approve",
962 G_TYPE_FROM_CLASS (klass),
966 g_cclosure_marshal_VOID__OBJECT,
968 1, EMPATHY_TYPE_DISPATCH_OPERATION);
971 g_signal_new ("dispatch",
972 G_TYPE_FROM_CLASS (klass),
976 g_cclosure_marshal_VOID__OBJECT,
978 1, EMPATHY_TYPE_DISPATCH_OPERATION);
980 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
985 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
988 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
989 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
991 dispatcher->priv = priv;
992 priv->mc = empathy_mission_control_dup_singleton ();
993 priv->account_manager = empathy_account_manager_dup_singleton ();
995 g_signal_connect (priv->account_manager,
996 "account-connection-changed",
997 G_CALLBACK (dispatcher_account_connection_cb),
1000 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1001 empathy_account_equal, g_object_unref, g_object_unref);
1003 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1004 g_object_unref, (GDestroyNotify) free_connection_data);
1006 priv->channels = NULL;
1008 accounts = mc_accounts_list_by_enabled (TRUE);
1010 for (l = accounts; l; l = l->next)
1012 dispatcher_update_account (dispatcher, l->data);
1013 g_object_unref (l->data);
1015 g_list_free (accounts);
1019 empathy_dispatcher_dup_singleton (void)
1021 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1025 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1026 DispatcherRequestData *request_data,
1027 const GError *error)
1029 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1030 ConnectionData *conn_data;
1032 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1033 if (request_data->cb != NULL)
1034 request_data->cb (NULL, error, request_data->user_data);
1036 conn_data->outstanding_requests =
1037 g_list_remove (conn_data->outstanding_requests, request_data);
1038 free_dispatcher_request_data (request_data);
1042 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1043 DispatcherRequestData *request_data,
1044 const gchar *object_path,
1045 GHashTable *properties,
1046 const GError *error)
1048 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1049 EmpathyDispatchOperation *operation = NULL;
1050 ConnectionData *conn_data;
1052 conn_data = g_hash_table_lookup (priv->connections,
1053 request_data->connection);
1057 DEBUG ("Channel request failed: %s", error->message);
1059 dispatcher_request_failed (dispatcher, request_data, error);
1064 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1067 if (operation != NULL)
1068 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1070 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1073 if (operation == NULL)
1075 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1080 operation = empathy_dispatch_operation_new_with_wrapper (
1081 request_data->connection,
1082 data->channel, request_data->contact, FALSE,
1083 data->channel_wrapper);
1089 if (properties != NULL)
1090 channel = tp_channel_new_from_properties (request_data->connection,
1091 object_path, properties, NULL);
1093 channel = tp_channel_new (request_data->connection, object_path,
1094 request_data->channel_type, request_data->handle_type,
1095 request_data->handle, NULL);
1097 g_signal_connect (channel, "invalidated",
1098 G_CALLBACK (dispatcher_channel_invalidated_cb),
1099 request_data->dispatcher);
1101 priv->channels = g_list_prepend (priv->channels, channel);
1103 operation = empathy_dispatch_operation_new (request_data->connection,
1104 channel, request_data->contact, FALSE);
1105 g_object_unref (channel);
1110 /* Already existed set potential extra information */
1111 g_object_set (G_OBJECT (operation),
1112 "contact", request_data->contact,
1116 request_data->operation = operation;
1118 /* (pre)-approve this right away as we requested it */
1119 empathy_dispatch_operation_approve (operation);
1121 dispatcher_start_dispatching (request_data->dispatcher, operation,
1124 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1129 dispatcher_request_channel_cb (TpConnection *connection,
1130 const gchar *object_path,
1131 const GError *error,
1133 GObject *weak_object)
1135 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1136 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1138 dispatcher_connection_new_requested_channel (dispatcher,
1139 request_data, object_path, NULL, error);
1143 dispatcher_request_channel (DispatcherRequestData *request_data)
1145 tp_cli_connection_call_request_channel (request_data->connection, -1,
1146 request_data->channel_type,
1147 request_data->handle_type,
1148 request_data->handle,
1149 TRUE, dispatcher_request_channel_cb,
1150 request_data, NULL, G_OBJECT (request_data->dispatcher));
1154 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1155 EmpathyDispatcherRequestCb *callback,
1158 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1159 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1161 TpConnection *connection;
1163 DispatcherRequestData *request_data;
1165 account = empathy_contact_get_account (contact);
1166 connection = g_hash_table_lookup (priv->accounts, account);
1168 g_assert (connection != NULL);
1169 cd = g_hash_table_lookup (priv->connections, connection);
1170 request_data = new_dispatcher_request_data (dispatcher, connection,
1171 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1172 contact, callback, user_data);
1174 cd->outstanding_requests = g_list_prepend
1175 (cd->outstanding_requests, request_data);
1177 dispatcher_request_channel (request_data);
1179 g_object_unref (dispatcher);
1183 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1184 const GError *error,
1188 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1190 request_data->handle = empathy_contact_get_handle (contact);
1192 dispatcher_request_channel (request_data);
1196 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1197 EmpathyDispatcherRequestCb *callback,
1200 EmpathyDispatcher *dispatcher;
1201 EmpathyDispatcherPriv *priv;
1203 TpConnection *connection;
1204 ConnectionData *connection_data;
1205 DispatcherRequestData *request_data;
1207 dispatcher = empathy_dispatcher_dup_singleton();
1208 priv = GET_PRIV (dispatcher);
1210 account = empathy_contact_get_account (contact);
1211 connection = g_hash_table_lookup (priv->accounts, account);
1212 connection_data = g_hash_table_lookup (priv->connections, connection);
1214 /* The contact handle might not be known yet */
1215 request_data = new_dispatcher_request_data (dispatcher, connection,
1216 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1217 contact, callback, user_data);
1219 connection_data->outstanding_requests = g_list_prepend
1220 (connection_data->outstanding_requests, request_data);
1222 empathy_contact_call_when_ready (contact,
1223 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1224 request_data, NULL, G_OBJECT (dispatcher));
1226 g_object_unref (dispatcher);
1230 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1231 const gchar *contact_id,
1232 EmpathyDispatcherRequestCb *callback,
1235 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1236 EmpathyContactFactory *factory;
1237 EmpathyContact *contact;
1239 factory = empathy_contact_factory_dup_singleton ();
1240 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1242 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1244 g_object_unref (contact);
1245 g_object_unref (factory);
1246 g_object_unref (dispatcher);
1250 dispatcher_request_handles_cb (TpConnection *connection,
1251 const GArray *handles,
1252 const GError *error,
1256 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1260 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1261 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1264 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1266 if (request_data->cb)
1267 request_data->cb (NULL, error, request_data->user_data);
1269 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1272 free_dispatcher_request_data (request_data);
1274 dispatcher_flush_outstanding_operations (dispatcher, cd);
1278 request_data->handle = g_array_index (handles, guint, 0);
1279 dispatcher_request_channel (request_data);
1283 empathy_dispatcher_join_muc (McAccount *account,
1284 const gchar *roomname,
1285 EmpathyDispatcherRequestCb *callback,
1288 EmpathyDispatcher *dispatcher;
1289 EmpathyDispatcherPriv *priv;
1290 DispatcherRequestData *request_data;
1291 TpConnection *connection;
1292 ConnectionData *connection_data;
1293 const gchar *names[] = { roomname, NULL };
1295 dispatcher = empathy_dispatcher_dup_singleton();
1296 priv = GET_PRIV (dispatcher);
1298 connection = g_hash_table_lookup (priv->accounts, account);
1299 connection_data = g_hash_table_lookup (priv->connections, connection);
1302 /* Don't know the room handle yet */
1303 request_data = new_dispatcher_request_data (dispatcher, connection,
1304 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1305 NULL, callback, user_data);
1307 connection_data->outstanding_requests = g_list_prepend
1308 (connection_data->outstanding_requests, request_data);
1310 tp_cli_connection_call_request_handles (connection, -1,
1311 TP_HANDLE_TYPE_ROOM, names,
1312 dispatcher_request_handles_cb, request_data, NULL,
1313 G_OBJECT (dispatcher));
1315 g_object_unref (dispatcher);
1319 dispatcher_create_channel_cb (TpConnection *connect,
1320 const gchar *object_path,
1321 GHashTable *properties,
1322 const GError *error,
1324 GObject *weak_object)
1326 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1327 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1329 dispatcher_connection_new_requested_channel (dispatcher,
1330 request_data, object_path, properties, error);
1334 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1335 McAccount *account, GHashTable *request,
1336 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1338 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1339 ConnectionData *connection_data;
1340 DispatcherRequestData *request_data;
1341 const gchar *channel_type;
1345 TpConnection *connection;
1347 connection = g_hash_table_lookup (priv->accounts, account);
1348 g_assert (connection != NULL);
1350 connection_data = g_hash_table_lookup (priv->connections, connection);
1351 g_assert (connection_data != NULL);
1353 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1355 handle_type = tp_asv_get_uint32 (request,
1356 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1358 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1360 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1362 request_data = new_dispatcher_request_data (dispatcher, connection,
1363 channel_type, handle_type, handle, request,
1364 NULL, callback, user_data);
1366 connection_data->outstanding_requests = g_list_prepend
1367 (connection_data->outstanding_requests, request_data);
1369 tp_cli_connection_interface_requests_call_create_channel (
1370 request_data->connection, -1,
1371 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1372 G_OBJECT (request_data->dispatcher));
1376 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1377 const GError *error,
1381 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1382 GValue *target_handle;
1384 g_assert (request_data->request);
1388 dispatcher_request_failed (request_data->dispatcher,
1389 request_data, error);
1393 request_data->handle = empathy_contact_get_handle (contact);
1395 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1396 g_value_set_uint (target_handle, request_data->handle);
1397 g_hash_table_insert (request_data->request,
1398 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1400 tp_cli_connection_interface_requests_call_create_channel (
1401 request_data->connection, -1,
1402 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1403 G_OBJECT (request_data->dispatcher));
1407 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1408 const GError *error,
1411 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1415 dispatcher_request_failed (request_data->dispatcher,
1416 request_data, error);
1420 empathy_contact_call_when_ready (request_data->contact,
1421 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1422 request_data, NULL, G_OBJECT (request_data->dispatcher));
1426 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1427 const gchar *filename,
1430 const gchar *content_type,
1431 EmpathyDispatcherRequestCb *callback,
1434 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1435 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1436 McAccount *account = empathy_contact_get_account (contact);
1437 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1438 ConnectionData *connection_data =
1439 g_hash_table_lookup (priv->connections, connection);
1440 DispatcherRequestData *request_data;
1442 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1443 (GDestroyNotify) tp_g_value_slice_free);
1445 /* org.freedesktop.Telepathy.Channel.ChannelType */
1446 value = tp_g_value_slice_new (G_TYPE_STRING);
1447 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1448 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1450 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1451 value = tp_g_value_slice_new (G_TYPE_UINT);
1452 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1453 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1455 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1456 value = tp_g_value_slice_new (G_TYPE_STRING);
1457 g_value_set_string (value, content_type);
1458 g_hash_table_insert (request,
1459 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1461 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1462 value = tp_g_value_slice_new (G_TYPE_STRING);
1463 g_value_set_string (value, filename);
1464 g_hash_table_insert (request,
1465 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1467 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1468 value = tp_g_value_slice_new (G_TYPE_UINT64);
1469 g_value_set_uint64 (value, size);
1470 g_hash_table_insert (request,
1471 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1473 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1474 value = tp_g_value_slice_new (G_TYPE_UINT64);
1475 g_value_set_uint64 (value, date);
1476 g_hash_table_insert (request,
1477 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1480 /* The contact handle might not be known yet */
1481 request_data = new_dispatcher_request_data (dispatcher, connection,
1482 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1483 contact, callback, user_data);
1484 connection_data->outstanding_requests = g_list_prepend
1485 (connection_data->outstanding_requests, request_data);
1487 tp_connection_call_when_ready (connection,
1488 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1490 g_object_unref (dispatcher);
1494 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1495 McAccount *account, const gchar *channel_type, guint handle_type)
1497 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1499 TpConnection *connection;
1503 g_return_val_if_fail (channel_type != NULL, NULL);
1504 g_return_val_if_fail (handle_type != 0, NULL);
1506 connection = g_hash_table_lookup (priv->accounts, account);
1508 if (connection == NULL)
1511 cd = g_hash_table_lookup (priv->connections, connection);
1517 classes = cd->requestable_channels;
1518 if (classes == NULL)
1521 for (i = 0; i < classes->len; i++)
1527 const gchar *c_type;
1531 class = g_ptr_array_index (classes, i);
1532 fixed = g_value_array_get_nth (class, 0);
1534 fprops = g_value_get_boxed (fixed);
1535 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1537 if (tp_strdiff (channel_type, c_type))
1540 h_type = tp_asv_get_uint32 (fprops,
1541 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1543 if (!valid || handle_type != h_type)
1546 allowed = g_value_array_get_nth (class, 1);
1548 return g_value_get_boxed (allowed);