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, TP_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 capabilities = g_ptr_array_sized_new (1);
834 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
835 G_TYPE_UINT, G_TYPE_INVALID);
836 g_value_init (&cap, cap_type);
837 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
838 dbus_g_type_struct_set (&cap,
839 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
840 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
841 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
842 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
843 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
844 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
846 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
847 connection, -1, capabilities, &remove,
848 dispatcher_connection_advertise_capabilities_cb,
849 NULL, NULL, G_OBJECT (dispatcher));
851 g_value_unset (&cap);
852 g_ptr_array_free (capabilities, TRUE);
856 dispatcher_update_account (EmpathyDispatcher *dispatcher,
859 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
860 TpConnection *connection;
862 connection = g_hash_table_lookup (priv->accounts, account);
863 if (connection != NULL)
866 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
867 if (connection == NULL)
870 g_hash_table_insert (priv->connections, g_object_ref (connection),
871 new_connection_data (account));
873 g_hash_table_insert (priv->accounts, g_object_ref (account),
874 g_object_ref (connection));
876 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
879 g_object_unref (connection);
883 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
885 TpConnectionStatusReason reason,
886 TpConnectionStatus status,
887 TpConnectionStatus previous,
888 EmpathyDispatcher *dispatcher)
890 dispatcher_update_account (dispatcher, account);
894 dispatcher_constructor (GType type,
895 guint n_construct_params,
896 GObjectConstructParam *construct_params)
900 if (dispatcher == NULL)
902 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
903 (type, n_construct_params, construct_params);
905 dispatcher = EMPATHY_DISPATCHER (retval);
906 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
910 retval = g_object_ref (dispatcher);
917 dispatcher_finalize (GObject *object)
919 EmpathyDispatcherPriv *priv = GET_PRIV (object);
924 g_signal_handlers_disconnect_by_func (priv->account_manager,
925 dispatcher_account_connection_cb, object);
927 for (l = priv->channels; l; l = l->next)
929 g_signal_handlers_disconnect_by_func (l->data,
930 dispatcher_channel_invalidated_cb, object);
933 g_list_free (priv->channels);
935 g_hash_table_iter_init (&iter, priv->connections);
936 while (g_hash_table_iter_next (&iter, &connection, NULL))
938 g_signal_handlers_disconnect_by_func (connection,
939 dispatcher_connection_invalidated_cb, object);
942 g_object_unref (priv->account_manager);
943 g_object_unref (priv->mc);
945 g_hash_table_destroy (priv->accounts);
946 g_hash_table_destroy (priv->connections);
950 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
952 GObjectClass *object_class = G_OBJECT_CLASS (klass);
954 object_class->finalize = dispatcher_finalize;
955 object_class->constructor = dispatcher_constructor;
958 g_signal_new ("observe",
959 G_TYPE_FROM_CLASS (klass),
963 g_cclosure_marshal_VOID__OBJECT,
965 1, EMPATHY_TYPE_DISPATCH_OPERATION);
968 g_signal_new ("approve",
969 G_TYPE_FROM_CLASS (klass),
973 g_cclosure_marshal_VOID__OBJECT,
975 1, EMPATHY_TYPE_DISPATCH_OPERATION);
978 g_signal_new ("dispatch",
979 G_TYPE_FROM_CLASS (klass),
983 g_cclosure_marshal_VOID__OBJECT,
985 1, EMPATHY_TYPE_DISPATCH_OPERATION);
987 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
992 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
995 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
996 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
998 dispatcher->priv = priv;
999 priv->mc = empathy_mission_control_dup_singleton ();
1000 priv->account_manager = empathy_account_manager_dup_singleton ();
1002 g_signal_connect (priv->account_manager,
1003 "account-connection-changed",
1004 G_CALLBACK (dispatcher_account_connection_cb),
1007 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1008 empathy_account_equal, g_object_unref, g_object_unref);
1010 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1011 g_object_unref, (GDestroyNotify) free_connection_data);
1013 priv->channels = NULL;
1015 accounts = mc_accounts_list_by_enabled (TRUE);
1017 for (l = accounts; l; l = l->next)
1019 dispatcher_update_account (dispatcher, l->data);
1020 g_object_unref (l->data);
1022 g_list_free (accounts);
1026 empathy_dispatcher_dup_singleton (void)
1028 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1032 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1033 DispatcherRequestData *request_data,
1034 const GError *error)
1036 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1037 ConnectionData *conn_data;
1039 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1040 if (request_data->cb != NULL)
1041 request_data->cb (NULL, error, request_data->user_data);
1043 conn_data->outstanding_requests =
1044 g_list_remove (conn_data->outstanding_requests, request_data);
1045 free_dispatcher_request_data (request_data);
1049 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1050 DispatcherRequestData *request_data,
1051 const gchar *object_path,
1052 GHashTable *properties,
1053 const GError *error)
1055 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1056 EmpathyDispatchOperation *operation = NULL;
1057 ConnectionData *conn_data;
1059 conn_data = g_hash_table_lookup (priv->connections,
1060 request_data->connection);
1064 DEBUG ("Channel request failed: %s", error->message);
1066 dispatcher_request_failed (dispatcher, request_data, error);
1071 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1074 if (operation != NULL)
1075 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1077 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1080 if (operation == NULL)
1082 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1087 operation = empathy_dispatch_operation_new_with_wrapper (
1088 request_data->connection,
1089 data->channel, request_data->contact, FALSE,
1090 data->channel_wrapper);
1096 if (properties != NULL)
1097 channel = tp_channel_new_from_properties (request_data->connection,
1098 object_path, properties, NULL);
1100 channel = tp_channel_new (request_data->connection, object_path,
1101 request_data->channel_type, request_data->handle_type,
1102 request_data->handle, NULL);
1104 g_signal_connect (channel, "invalidated",
1105 G_CALLBACK (dispatcher_channel_invalidated_cb),
1106 request_data->dispatcher);
1108 priv->channels = g_list_prepend (priv->channels, channel);
1110 operation = empathy_dispatch_operation_new (request_data->connection,
1111 channel, request_data->contact, FALSE);
1112 g_object_unref (channel);
1117 /* Already existed set potential extra information */
1118 g_object_set (G_OBJECT (operation),
1119 "contact", request_data->contact,
1123 request_data->operation = operation;
1125 /* (pre)-approve this right away as we requested it
1126 * This might cause the channel to be claimed, in which case the operation
1127 * will disappear. So ref it, and check the status before starting the
1130 g_object_ref (operation);
1131 empathy_dispatch_operation_approve (operation);
1133 if (empathy_dispatch_operation_get_status (operation) <
1134 EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1135 dispatcher_start_dispatching (request_data->dispatcher, operation,
1138 g_object_unref (operation);
1141 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1146 dispatcher_request_channel_cb (TpConnection *connection,
1147 const gchar *object_path,
1148 const GError *error,
1150 GObject *weak_object)
1152 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1153 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1155 dispatcher_connection_new_requested_channel (dispatcher,
1156 request_data, object_path, NULL, error);
1160 dispatcher_request_channel (DispatcherRequestData *request_data)
1162 tp_cli_connection_call_request_channel (request_data->connection, -1,
1163 request_data->channel_type,
1164 request_data->handle_type,
1165 request_data->handle,
1166 TRUE, dispatcher_request_channel_cb,
1167 request_data, NULL, G_OBJECT (request_data->dispatcher));
1171 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1172 EmpathyDispatcherRequestCb *callback,
1175 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1176 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1178 TpConnection *connection;
1180 DispatcherRequestData *request_data;
1182 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1184 account = empathy_contact_get_account (contact);
1185 connection = g_hash_table_lookup (priv->accounts, account);
1187 g_assert (connection != NULL);
1188 cd = g_hash_table_lookup (priv->connections, connection);
1189 request_data = new_dispatcher_request_data (dispatcher, connection,
1190 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1191 contact, callback, user_data);
1193 cd->outstanding_requests = g_list_prepend
1194 (cd->outstanding_requests, request_data);
1196 dispatcher_request_channel (request_data);
1198 g_object_unref (dispatcher);
1202 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1203 const GError *error,
1207 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1209 request_data->handle = empathy_contact_get_handle (contact);
1211 dispatcher_request_channel (request_data);
1215 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1216 EmpathyDispatcherRequestCb *callback,
1219 EmpathyDispatcher *dispatcher;
1220 EmpathyDispatcherPriv *priv;
1222 TpConnection *connection;
1223 ConnectionData *connection_data;
1224 DispatcherRequestData *request_data;
1226 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1228 dispatcher = empathy_dispatcher_dup_singleton();
1229 priv = GET_PRIV (dispatcher);
1231 account = empathy_contact_get_account (contact);
1232 connection = g_hash_table_lookup (priv->accounts, account);
1233 connection_data = g_hash_table_lookup (priv->connections, connection);
1235 /* The contact handle might not be known yet */
1236 request_data = new_dispatcher_request_data (dispatcher, connection,
1237 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1238 contact, callback, user_data);
1240 connection_data->outstanding_requests = g_list_prepend
1241 (connection_data->outstanding_requests, request_data);
1243 empathy_contact_call_when_ready (contact,
1244 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1245 request_data, NULL, G_OBJECT (dispatcher));
1247 g_object_unref (dispatcher);
1251 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1252 const gchar *contact_id,
1253 EmpathyDispatcherRequestCb *callback,
1256 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1257 EmpathyContactFactory *factory;
1258 EmpathyContact *contact;
1260 g_return_if_fail (MC_IS_ACCOUNT (account));
1261 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1263 factory = empathy_contact_factory_dup_singleton ();
1264 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1266 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1268 g_object_unref (contact);
1269 g_object_unref (factory);
1270 g_object_unref (dispatcher);
1274 dispatcher_request_handles_cb (TpConnection *connection,
1275 const GArray *handles,
1276 const GError *error,
1280 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1284 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1285 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1288 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1290 if (request_data->cb)
1291 request_data->cb (NULL, error, request_data->user_data);
1293 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1296 free_dispatcher_request_data (request_data);
1298 dispatcher_flush_outstanding_operations (dispatcher, cd);
1302 request_data->handle = g_array_index (handles, guint, 0);
1303 dispatcher_request_channel (request_data);
1307 empathy_dispatcher_join_muc (McAccount *account,
1308 const gchar *roomname,
1309 EmpathyDispatcherRequestCb *callback,
1312 EmpathyDispatcher *dispatcher;
1313 EmpathyDispatcherPriv *priv;
1314 DispatcherRequestData *request_data;
1315 TpConnection *connection;
1316 ConnectionData *connection_data;
1317 const gchar *names[] = { roomname, NULL };
1319 g_return_if_fail (MC_IS_ACCOUNT (account));
1320 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1322 dispatcher = empathy_dispatcher_dup_singleton();
1323 priv = GET_PRIV (dispatcher);
1325 connection = g_hash_table_lookup (priv->accounts, account);
1326 connection_data = g_hash_table_lookup (priv->connections, connection);
1329 /* Don't know the room handle yet */
1330 request_data = new_dispatcher_request_data (dispatcher, connection,
1331 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1332 NULL, callback, user_data);
1334 connection_data->outstanding_requests = g_list_prepend
1335 (connection_data->outstanding_requests, request_data);
1337 tp_cli_connection_call_request_handles (connection, -1,
1338 TP_HANDLE_TYPE_ROOM, names,
1339 dispatcher_request_handles_cb, request_data, NULL,
1340 G_OBJECT (dispatcher));
1342 g_object_unref (dispatcher);
1346 dispatcher_create_channel_cb (TpConnection *connect,
1347 const gchar *object_path,
1348 GHashTable *properties,
1349 const GError *error,
1351 GObject *weak_object)
1353 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1354 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1356 dispatcher_connection_new_requested_channel (dispatcher,
1357 request_data, object_path, properties, error);
1361 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1363 GHashTable *request,
1364 EmpathyDispatcherRequestCb *callback,
1367 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1368 ConnectionData *connection_data;
1369 DispatcherRequestData *request_data;
1370 const gchar *channel_type;
1374 TpConnection *connection;
1376 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1377 g_return_if_fail (MC_IS_ACCOUNT (account));
1378 g_return_if_fail (request != NULL);
1380 connection = g_hash_table_lookup (priv->accounts, account);
1381 g_assert (connection != NULL);
1383 connection_data = g_hash_table_lookup (priv->connections, connection);
1384 g_assert (connection_data != NULL);
1386 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1388 handle_type = tp_asv_get_uint32 (request,
1389 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1391 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1393 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1395 request_data = new_dispatcher_request_data (dispatcher, connection,
1396 channel_type, handle_type, handle, request,
1397 NULL, callback, user_data);
1399 connection_data->outstanding_requests = g_list_prepend
1400 (connection_data->outstanding_requests, request_data);
1402 tp_cli_connection_interface_requests_call_create_channel (
1403 request_data->connection, -1,
1404 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1405 G_OBJECT (request_data->dispatcher));
1409 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1410 const GError *error,
1414 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1415 GValue *target_handle;
1417 g_assert (request_data->request);
1421 dispatcher_request_failed (request_data->dispatcher,
1422 request_data, error);
1426 request_data->handle = empathy_contact_get_handle (contact);
1428 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1429 g_value_set_uint (target_handle, request_data->handle);
1430 g_hash_table_insert (request_data->request,
1431 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1433 tp_cli_connection_interface_requests_call_create_channel (
1434 request_data->connection, -1,
1435 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1436 G_OBJECT (request_data->dispatcher));
1440 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1441 const GError *error,
1444 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1448 dispatcher_request_failed (request_data->dispatcher,
1449 request_data, error);
1453 empathy_contact_call_when_ready (request_data->contact,
1454 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1455 request_data, NULL, G_OBJECT (request_data->dispatcher));
1459 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1460 const gchar *filename,
1463 const gchar *content_type,
1464 EmpathyDispatcherRequestCb *callback,
1467 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1468 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1469 McAccount *account = empathy_contact_get_account (contact);
1470 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1471 ConnectionData *connection_data =
1472 g_hash_table_lookup (priv->connections, connection);
1473 DispatcherRequestData *request_data;
1475 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1476 (GDestroyNotify) tp_g_value_slice_free);
1478 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1479 g_return_if_fail (!EMP_STR_EMPTY (filename));
1480 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1482 /* org.freedesktop.Telepathy.Channel.ChannelType */
1483 value = tp_g_value_slice_new (G_TYPE_STRING);
1484 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1485 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1487 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1488 value = tp_g_value_slice_new (G_TYPE_UINT);
1489 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1490 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1492 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1493 value = tp_g_value_slice_new (G_TYPE_STRING);
1494 g_value_set_string (value, content_type);
1495 g_hash_table_insert (request,
1496 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1498 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1499 value = tp_g_value_slice_new (G_TYPE_STRING);
1500 g_value_set_string (value, filename);
1501 g_hash_table_insert (request,
1502 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1504 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1505 value = tp_g_value_slice_new (G_TYPE_UINT64);
1506 g_value_set_uint64 (value, size);
1507 g_hash_table_insert (request,
1508 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1510 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1511 value = tp_g_value_slice_new (G_TYPE_UINT64);
1512 g_value_set_uint64 (value, date);
1513 g_hash_table_insert (request,
1514 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1517 /* The contact handle might not be known yet */
1518 request_data = new_dispatcher_request_data (dispatcher, connection,
1519 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1520 contact, callback, user_data);
1521 connection_data->outstanding_requests = g_list_prepend
1522 (connection_data->outstanding_requests, request_data);
1524 tp_connection_call_when_ready (connection,
1525 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1527 g_object_unref (dispatcher);
1531 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1533 const gchar *channel_type,
1536 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1538 TpConnection *connection;
1542 g_return_val_if_fail (channel_type != NULL, NULL);
1543 g_return_val_if_fail (handle_type != 0, NULL);
1545 connection = g_hash_table_lookup (priv->accounts, account);
1547 if (connection == NULL)
1550 cd = g_hash_table_lookup (priv->connections, connection);
1556 classes = cd->requestable_channels;
1557 if (classes == NULL)
1560 for (i = 0; i < classes->len; i++)
1566 const gchar *c_type;
1570 class = g_ptr_array_index (classes, i);
1571 fixed = g_value_array_get_nth (class, 0);
1573 fprops = g_value_get_boxed (fixed);
1574 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1576 if (tp_strdiff (channel_type, c_type))
1579 h_type = tp_asv_get_uint32 (fprops,
1580 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1582 if (!valid || handle_type != h_type)
1585 allowed = g_value_array_get_nth (class, 1);
1587 return g_value_get_boxed (allowed);