1 /* * Copyright (C) 2007-2008 Collabora Ltd.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <glib/gi18n-lib.h>
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/proxy-subclass.h>
32 #include <libmissioncontrol/mission-control.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include <extensions/extensions.h>
37 #include "empathy-dispatcher.h"
38 #include "empathy-utils.h"
39 #include "empathy-tube-handler.h"
40 #include "empathy-account-manager.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-tp-file.h"
43 #include "empathy-chatroom-manager.h"
44 #include "empathy-utils.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
52 EmpathyAccountManager *account_manager;
54 /* connection to connection data mapping */
55 GHashTable *connections;
56 /* accounts to connection mapping */
60 } EmpathyDispatcherPriv;
62 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
72 static guint signals[LAST_SIGNAL];
73 static EmpathyDispatcher *dispatcher = NULL;
77 EmpathyDispatcher *dispatcher;
78 EmpathyDispatchOperation *operation;
79 TpConnection *connection;
83 EmpathyContact *contact;
85 /* Properties to pass to the channel when requesting it */
87 EmpathyDispatcherRequestCb *cb;
89 gpointer *request_data;
90 } DispatcherRequestData;
95 /* Channel type specific wrapper object */
96 GObject *channel_wrapper;
102 /* ObjectPath => DispatchData.. */
103 GHashTable *dispatched_channels;
104 /* ObjectPath -> EmpathyDispatchOperations */
105 GHashTable *dispatching_channels;
106 /* ObjectPath -> EmpathyDispatchOperations */
107 GHashTable *outstanding_channels;
108 /* List of DispatcherRequestData */
109 GList *outstanding_requests;
112 static DispatchData *
113 new_dispatch_data (TpChannel *channel,
114 GObject *channel_wrapper)
116 DispatchData *d = g_slice_new0 (DispatchData);
117 d->channel = g_object_ref (channel);
118 d->channel_wrapper = g_object_ref (channel_wrapper);
124 free_dispatch_data (DispatchData *data)
126 g_object_unref (data->channel);
127 g_object_unref (data->channel_wrapper);
129 g_slice_free (DispatchData, data);
132 static DispatcherRequestData *
133 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
134 TpConnection *connection,
135 const gchar *channel_type,
139 EmpathyContact *contact,
140 EmpathyDispatcherRequestCb *cb,
143 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
145 result->dispatcher = dispatcher;
146 result->connection = connection;
148 result->channel_type = g_strdup (channel_type);
149 result->handle_type = handle_type;
150 result->handle = handle;
151 result->request = request;
154 result->contact = g_object_ref (contact);
157 result->user_data = user_data;
163 free_dispatcher_request_data (DispatcherRequestData *r)
165 g_free (r->channel_type);
167 if (r->contact != NULL)
168 g_object_unref (r->contact);
170 if (r->request != NULL)
171 g_hash_table_unref (r->request);
173 g_slice_free (DispatcherRequestData, r);
176 static ConnectionData *
177 new_connection_data (McAccount *account)
179 ConnectionData *cd = g_slice_new0 (ConnectionData);
180 cd->account = g_object_ref (account);
182 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
183 g_free, (GDestroyNotify) free_dispatch_data);
185 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
186 g_free, g_object_unref);
188 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
195 free_connection_data (ConnectionData *cd)
198 g_object_unref (cd->account);
199 g_hash_table_destroy (cd->dispatched_channels);
200 g_hash_table_destroy (cd->dispatching_channels);
202 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
204 free_dispatcher_request_data (l->data);
209 dispatcher_connection_invalidated_cb (TpConnection *connection,
213 EmpathyDispatcher *dispatcher)
215 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
218 DEBUG ("Error: %s", message);
219 cd = g_hash_table_lookup (priv->connections, connection);
221 g_hash_table_remove (priv->accounts, cd->account);
222 g_hash_table_remove (priv->connections, connection);
226 dispatcher_operation_can_start (EmpathyDispatcher *self,
227 EmpathyDispatchOperation *operation,
231 const gchar *channel_type =
232 empathy_dispatch_operation_get_channel_type (operation);
234 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
236 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
238 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
248 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
249 EmpathyDispatchOperation *operation,
255 l = cd->outstanding_requests;
258 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
263 if (d->operation == operation)
268 d->cb (NULL, error, d->user_data);
270 d->cb (operation, NULL, d->user_data);
273 cd->outstanding_requests = g_list_delete_link
274 (cd->outstanding_requests, lt);
276 free_dispatcher_request_data (d);
282 dispatcher_channel_invalidated_cb (TpProxy *proxy,
286 EmpathyDispatcher *dispatcher)
288 /* Channel went away... */
289 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
290 TpConnection *connection;
291 EmpathyDispatchOperation *operation;
293 const gchar *object_path;
295 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
297 cd = g_hash_table_lookup (priv->connections, connection);
298 /* Connection itself invalidated? */
302 object_path = tp_proxy_get_object_path (proxy);
304 DEBUG ("Channel %s invalidated", object_path);
306 g_hash_table_remove (cd->dispatched_channels, object_path);
307 g_hash_table_remove (cd->dispatching_channels, object_path);
309 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
310 if (operation != NULL)
312 GError error = { domain, code, message };
313 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
314 g_hash_table_remove (cd->outstanding_channels, object_path);
315 g_object_unref (operation);
320 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
321 EmpathyDispatcher *dispatcher)
323 g_assert (empathy_dispatch_operation_is_incoming (operation));
324 DEBUG ("Send of for dispatching: %s",
325 empathy_dispatch_operation_get_object_path (operation));
326 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
330 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
331 EmpathyDispatcher *dispatcher)
333 /* Our job is done, remove the dispatch operation and mark the channel as
335 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
336 TpConnection *connection;
338 const gchar *object_path;
340 connection = empathy_dispatch_operation_get_tp_connection (operation);
341 cd = g_hash_table_lookup (priv->connections, connection);
342 g_assert (cd != NULL);
343 g_object_unref (G_OBJECT (connection));
345 object_path = empathy_dispatch_operation_get_object_path (operation);
347 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
350 d = new_dispatch_data (
351 empathy_dispatch_operation_get_channel (operation),
352 empathy_dispatch_operation_get_channel_wrapper (operation));
353 g_hash_table_insert (cd->dispatched_channels,
354 g_strdup (object_path), d);
356 g_hash_table_remove (cd->dispatching_channels, object_path);
358 DEBUG ("Channel claimed: %s", object_path);
362 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
363 EmpathyDispatcher *dispatcher)
365 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
366 TpConnection *connection;
368 EmpathyDispatchOperationState status;
370 g_signal_connect (operation, "approved",
371 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
373 g_signal_connect (operation, "claimed",
374 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
376 /* Signal the observers */
377 DEBUG ("Send to observers: %s",
378 empathy_dispatch_operation_get_object_path (operation));
379 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
381 empathy_dispatch_operation_start (operation);
383 /* Signal potential requestors */
384 connection = empathy_dispatch_operation_get_tp_connection (operation);
385 cd = g_hash_table_lookup (priv->connections, connection);
386 g_assert (cd != NULL);
387 g_object_unref (G_OBJECT (connection));
389 g_object_ref (operation);
391 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
392 status = empathy_dispatch_operation_get_status (operation);
393 g_object_unref (operation);
395 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
398 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
400 DEBUG ("Send to approvers: %s",
401 empathy_dispatch_operation_get_object_path (operation));
402 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
406 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
407 DEBUG ("Send of for dispatching: %s",
408 empathy_dispatch_operation_get_object_path (operation));
409 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
415 dispatcher_start_dispatching (EmpathyDispatcher *self,
416 EmpathyDispatchOperation *operation,
419 const gchar *object_path =
420 empathy_dispatch_operation_get_object_path (operation);
422 DEBUG ("Dispatching process started for %s", object_path);
424 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
426 g_assert (g_hash_table_lookup (cd->outstanding_channels,
427 object_path) == NULL);
429 g_hash_table_insert (cd->dispatching_channels,
430 g_strdup (object_path), operation);
432 switch (empathy_dispatch_operation_get_status (operation))
434 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
435 g_signal_connect (operation, "ready",
436 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
438 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
439 dispatch_operation_ready_cb (operation, dispatcher);
442 g_assert_not_reached();
446 else if (empathy_dispatch_operation_get_status (operation) >=
447 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
449 /* Already dispatching and the operation is pending, thus the observers
450 * have seen it (if applicable), so we can flush the request right away.
452 dispatch_operation_flush_requests (self, operation, NULL, cd);
457 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
463 g_hash_table_iter_init (&iter, cd->outstanding_channels);
464 while (g_hash_table_iter_next (&iter, NULL, &value))
466 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
468 if (dispatcher_operation_can_start (self, operation, cd))
470 dispatcher_start_dispatching (dispatcher, operation, cd);
471 g_hash_table_iter_remove (&iter);
477 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
478 TpConnection *connection,
479 const gchar *object_path,
480 const gchar *channel_type,
483 GHashTable *properties,
486 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
489 EmpathyDispatchOperation *operation;
490 EmpathyContact *contact = NULL;
492 /* Channel types we never want to dispatch because they're either deprecated
493 * or can't sensibly be dispatch (e.g. channels that should always be
495 const char *blacklist[] = {
496 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
497 TP_IFACE_CHANNEL_TYPE_TUBES,
498 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
502 cd = g_hash_table_lookup (priv->connections, connection);
504 /* Don't bother with channels we have already dispatched or are dispatching
505 * currently. This can happen when NewChannel(s) is fired after
506 * RequestChannel/CreateChannel/EnsureChannel */
507 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
510 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
513 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
515 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
518 /* Only pick up non-requested text and file channels. For all other it
519 * doesn't make sense to handle it if we didn't request it. The same goes
520 * for channels we discovered by the Channels property or ListChannels */
521 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
522 && tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
524 DEBUG ("Ignoring incoming channel of type %s on %s",
525 channel_type, object_path);
529 for (i = 0 ; blacklist[i] != NULL; i++)
531 if (!tp_strdiff (channel_type, blacklist[i]))
533 DEBUG ("Ignoring blacklisted channel type %s on %s",
534 channel_type, object_path);
539 DEBUG ("New channel of type %s on %s", channel_type, object_path);
541 if (properties == NULL)
542 channel = tp_channel_new (connection, object_path, channel_type,
543 handle_type, handle, NULL);
545 channel = tp_channel_new_from_properties (connection, object_path,
548 g_signal_connect (channel, "invalidated",
549 G_CALLBACK (dispatcher_channel_invalidated_cb),
552 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
554 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
555 contact = empathy_contact_factory_get_from_handle (factory,
556 cd->account, handle);
557 g_object_unref (factory);
560 operation = empathy_dispatch_operation_new (connection, channel, contact,
563 g_object_unref (channel);
567 /* Request could either be by us or by a remote party. If there are no
568 * outstanding requests for this channel type we can assume it's remote.
569 * Otherwise we wait untill they are all satisfied */
570 if (dispatcher_operation_can_start (dispatcher, operation, cd))
571 dispatcher_start_dispatching (dispatcher, operation, cd);
573 g_hash_table_insert (cd->outstanding_channels,
574 g_strdup (object_path), operation);
578 dispatcher_start_dispatching (dispatcher, operation, cd);
583 dispatcher_connection_new_channel_cb (TpConnection *connection,
584 const gchar *object_path,
585 const gchar *channel_type,
588 gboolean suppress_handler,
592 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
594 /* Empathy heavily abuses surpress handler (don't try this at home), if
595 * surpress handler is true then it is an outgoing channel, which is
596 * requested either by us or some other party (like the megaphone applet).
597 * Otherwise it's an incoming channel */
598 dispatcher_connection_new_channel (dispatcher, connection,
599 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
603 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
604 TpConnection *connection,
605 const gchar *object_path,
606 GHashTable *properties)
608 const gchar *channel_type;
615 channel_type = tp_asv_get_string (properties,
616 TP_IFACE_CHANNEL ".ChannelType");
617 if (channel_type == NULL)
619 g_message ("%s had an invalid ChannelType property", object_path);
623 handle_type = tp_asv_get_uint32 (properties,
624 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
627 g_message ("%s had an invalid TargetHandleType property", object_path);
631 handle = tp_asv_get_uint32 (properties,
632 TP_IFACE_CHANNEL ".TargetHandle", &valid);
635 g_message ("%s had an invalid TargetHandle property", object_path);
639 /* We assume there is no channel dispather, so we're the only one dispatching
640 * it. Which means that a requested channel it is outgoing one */
641 requested = tp_asv_get_boolean (properties,
642 TP_IFACE_CHANNEL ".Requested", &valid);
645 g_message ("%s had an invalid Requested property", object_path);
649 dispatcher_connection_new_channel (dispatcher, connection,
650 object_path, channel_type, handle_type, handle, properties, !requested);
654 dispatcher_connection_new_channels_cb (TpConnection *connection,
655 const GPtrArray *channels,
659 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
662 for (i = 0; i < channels->len ; i++)
664 GValueArray *arr = g_ptr_array_index (channels, i);
665 const gchar *object_path;
666 GHashTable *properties;
668 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
669 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
671 dispatcher_connection_new_channel_with_properties (dispatcher,
672 connection, object_path, properties);
677 dispatcher_connection_got_channels_property (TpProxy *proxy,
678 const GValue *channels_prop,
686 DEBUG ("Error: %s", error->message);
690 channels = g_value_get_boxed (channels_prop);
691 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
692 channels, NULL, object);
696 dispatcher_connection_list_channels_cb (TpConnection *connection,
697 const GPtrArray *channels,
706 DEBUG ("Error: %s", error->message);
710 for (i = 0; i < channels->len; i++)
714 values = g_ptr_array_index (channels, i);
715 /* We don't have any extra info, so assume already existing channels are
717 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
719 g_value_get_boxed (g_value_array_get_nth (values, 0)),
720 g_value_get_string (g_value_array_get_nth (values, 1)),
721 g_value_get_uint (g_value_array_get_nth (values, 2)),
722 g_value_get_uint (g_value_array_get_nth (values, 3)),
728 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
729 const GPtrArray *capabilities,
735 DEBUG ("Error: %s", error->message);
739 dispatcher_connection_ready_cb (TpConnection *connection,
743 GPtrArray *capabilities;
746 const gchar *remove = NULL;
750 dispatcher_connection_invalidated_cb (connection, error->domain,
751 error->code, error->message, dispatcher);
755 g_signal_connect (connection, "invalidated",
756 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
759 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
760 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
762 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
763 dispatcher_connection_new_channels_cb,
764 NULL, NULL, G_OBJECT (dispatcher), NULL);
766 tp_cli_dbus_properties_call_get (connection, -1,
767 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
768 dispatcher_connection_got_channels_property,
769 NULL, NULL, dispatcher);
773 tp_cli_connection_connect_to_new_channel (connection,
774 dispatcher_connection_new_channel_cb,
775 NULL, NULL, G_OBJECT (dispatcher), NULL);
777 tp_cli_connection_call_list_channels (connection, -1,
778 dispatcher_connection_list_channels_cb, NULL, NULL,
779 G_OBJECT (dispatcher));
783 /* Advertise VoIP capabilities */
784 /* FIXME: Capabilities is leaked */
785 capabilities = g_ptr_array_sized_new (1);
786 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
787 G_TYPE_UINT, G_TYPE_INVALID);
788 g_value_init (&cap, cap_type);
789 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
790 dbus_g_type_struct_set (&cap,
791 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
792 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
793 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
794 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
795 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
796 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
798 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
799 connection, -1, capabilities, &remove,
800 dispatcher_connection_advertise_capabilities_cb,
801 NULL, NULL, G_OBJECT (dispatcher));
805 dispatcher_update_account (EmpathyDispatcher *dispatcher,
808 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
809 TpConnection *connection;
811 connection = g_hash_table_lookup (priv->accounts, account);
812 if (connection != NULL)
815 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
816 if (connection == NULL)
819 g_hash_table_insert (priv->connections, g_object_ref (connection),
820 new_connection_data (account));
822 g_hash_table_insert (priv->accounts, g_object_ref (account),
823 g_object_ref (connection));
825 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
828 g_object_unref (connection);
832 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
834 TpConnectionStatusReason reason,
835 TpConnectionStatus status,
836 TpConnectionStatus previous,
837 EmpathyDispatcher *dispatcher)
839 dispatcher_update_account (dispatcher, account);
843 dispatcher_constructor (GType type,
844 guint n_construct_params,
845 GObjectConstructParam *construct_params)
849 if (dispatcher == NULL)
851 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
852 (type, n_construct_params, construct_params);
854 dispatcher = EMPATHY_DISPATCHER (retval);
855 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
859 retval = g_object_ref (dispatcher);
866 dispatcher_finalize (GObject *object)
868 EmpathyDispatcherPriv *priv = GET_PRIV (object);
870 g_signal_handlers_disconnect_by_func (priv->account_manager,
871 dispatcher_account_connection_cb, object);
873 g_object_unref (priv->account_manager);
874 g_object_unref (priv->mc);
876 g_hash_table_destroy (priv->accounts);
877 g_hash_table_destroy (priv->connections);
881 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
883 GObjectClass *object_class = G_OBJECT_CLASS (klass);
885 object_class->finalize = dispatcher_finalize;
886 object_class->constructor = dispatcher_constructor;
889 g_signal_new ("observe",
890 G_TYPE_FROM_CLASS (klass),
894 g_cclosure_marshal_VOID__OBJECT,
896 1, EMPATHY_TYPE_DISPATCH_OPERATION);
899 g_signal_new ("approve",
900 G_TYPE_FROM_CLASS (klass),
904 g_cclosure_marshal_VOID__OBJECT,
906 1, EMPATHY_TYPE_DISPATCH_OPERATION);
909 g_signal_new ("dispatch",
910 G_TYPE_FROM_CLASS (klass),
914 g_cclosure_marshal_VOID__OBJECT,
916 1, EMPATHY_TYPE_DISPATCH_OPERATION);
918 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
923 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
926 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
927 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
929 dispatcher->priv = priv;
930 priv->mc = empathy_mission_control_dup_singleton ();
931 priv->account_manager = empathy_account_manager_dup_singleton ();
933 g_signal_connect (priv->account_manager,
934 "account-connection-changed",
935 G_CALLBACK (dispatcher_account_connection_cb),
938 priv->accounts = g_hash_table_new_full (empathy_account_hash,
939 empathy_account_equal, g_object_unref, g_object_unref);
941 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
942 g_object_unref, (GDestroyNotify) free_connection_data);
944 accounts = mc_accounts_list_by_enabled (TRUE);
946 for (l = accounts; l; l = l->next)
948 dispatcher_update_account (dispatcher, l->data);
949 g_object_unref (l->data);
951 g_list_free (accounts);
955 empathy_dispatcher_dup_singleton (void)
957 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
961 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
962 DispatcherRequestData *request_data,
965 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
966 ConnectionData *conn_data;
968 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
969 if (request_data->cb != NULL)
970 request_data->cb (NULL, error, request_data->user_data);
972 conn_data->outstanding_requests =
973 g_list_remove (conn_data->outstanding_requests, request_data);
974 free_dispatcher_request_data (request_data);
978 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
979 DispatcherRequestData *request_data,
980 const gchar *object_path,
981 GHashTable *properties,
984 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
985 EmpathyDispatchOperation *operation = NULL;
986 ConnectionData *conn_data;
988 conn_data = g_hash_table_lookup (priv->connections,
989 request_data->connection);
993 DEBUG ("Channel request failed: %s", error->message);
995 dispatcher_request_failed (dispatcher, request_data, error);
1000 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1003 if (operation != NULL)
1004 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1006 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1009 if (operation == NULL)
1011 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1016 operation = empathy_dispatch_operation_new_with_wrapper (
1017 request_data->connection,
1018 data->channel, request_data->contact, FALSE,
1019 data->channel_wrapper);
1025 if (properties != NULL)
1026 channel = tp_channel_new_from_properties (request_data->connection,
1027 object_path, properties, NULL);
1029 channel = tp_channel_new (request_data->connection, object_path,
1030 request_data->channel_type, request_data->handle_type,
1031 request_data->handle, NULL);
1033 g_signal_connect (channel, "invalidated",
1034 G_CALLBACK (dispatcher_channel_invalidated_cb),
1035 request_data->dispatcher);
1037 operation = empathy_dispatch_operation_new (request_data->connection,
1038 channel, request_data->contact, FALSE);
1039 g_object_unref (channel);
1044 /* Already existed set potential extra information */
1045 g_object_set (G_OBJECT (operation),
1046 "contact", request_data->contact,
1050 request_data->operation = operation;
1052 /* (pre)-approve this right away as we requested it */
1053 empathy_dispatch_operation_approve (operation);
1055 dispatcher_start_dispatching (request_data->dispatcher, operation,
1058 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1063 dispatcher_request_channel_cb (TpConnection *connection,
1064 const gchar *object_path,
1065 const GError *error,
1067 GObject *weak_object)
1069 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1070 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1072 dispatcher_connection_new_requested_channel (dispatcher,
1073 request_data, object_path, NULL, error);
1077 dispatcher_request_channel (DispatcherRequestData *request_data)
1079 tp_cli_connection_call_request_channel (request_data->connection, -1,
1080 request_data->channel_type,
1081 request_data->handle_type,
1082 request_data->handle,
1083 TRUE, dispatcher_request_channel_cb,
1084 request_data, NULL, G_OBJECT (request_data->dispatcher));
1088 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1089 EmpathyDispatcherRequestCb *callback,
1092 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1093 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1095 TpConnection *connection;
1097 DispatcherRequestData *request_data;
1099 account = empathy_contact_get_account (contact);
1100 connection = g_hash_table_lookup (priv->accounts, account);
1102 g_assert (connection != NULL);
1103 cd = g_hash_table_lookup (priv->connections, connection);
1104 request_data = new_dispatcher_request_data (dispatcher, connection,
1105 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1106 contact, callback, user_data);
1108 cd->outstanding_requests = g_list_prepend
1109 (cd->outstanding_requests, request_data);
1111 dispatcher_request_channel (request_data);
1113 g_object_unref (dispatcher);
1117 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1118 const GError *error,
1122 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1124 request_data->handle = empathy_contact_get_handle (contact);
1126 dispatcher_request_channel (request_data);
1130 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1131 EmpathyDispatcherRequestCb *callback,
1134 EmpathyDispatcher *dispatcher;
1135 EmpathyDispatcherPriv *priv;
1137 TpConnection *connection;
1138 ConnectionData *connection_data;
1139 DispatcherRequestData *request_data;
1141 dispatcher = empathy_dispatcher_dup_singleton();
1142 priv = GET_PRIV (dispatcher);
1144 account = empathy_contact_get_account (contact);
1145 connection = g_hash_table_lookup (priv->accounts, account);
1146 connection_data = g_hash_table_lookup (priv->connections, connection);
1148 /* The contact handle might not be known yet */
1149 request_data = new_dispatcher_request_data (dispatcher, connection,
1150 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1151 contact, callback, user_data);
1153 connection_data->outstanding_requests = g_list_prepend
1154 (connection_data->outstanding_requests, request_data);
1156 empathy_contact_call_when_ready (contact,
1157 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1158 request_data, NULL, G_OBJECT (dispatcher));
1160 g_object_unref (dispatcher);
1164 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1165 const gchar *contact_id,
1166 EmpathyDispatcherRequestCb *callback,
1169 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1170 EmpathyContactFactory *factory;
1171 EmpathyContact *contact;
1173 factory = empathy_contact_factory_dup_singleton ();
1174 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1176 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1178 g_object_unref (contact);
1179 g_object_unref (factory);
1180 g_object_unref (dispatcher);
1184 dispatcher_request_handles_cb (TpConnection *connection,
1185 const GArray *handles,
1186 const GError *error,
1190 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1194 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1195 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1198 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1200 if (request_data->cb)
1201 request_data->cb (NULL, error, request_data->user_data);
1203 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1206 free_dispatcher_request_data (request_data);
1208 dispatcher_flush_outstanding_operations (dispatcher, cd);
1212 request_data->handle = g_array_index (handles, guint, 0);
1213 dispatcher_request_channel (request_data);
1217 empathy_dispatcher_join_muc (McAccount *account,
1218 const gchar *roomname,
1219 EmpathyDispatcherRequestCb *callback,
1222 EmpathyDispatcher *dispatcher;
1223 EmpathyDispatcherPriv *priv;
1224 DispatcherRequestData *request_data;
1225 TpConnection *connection;
1226 ConnectionData *connection_data;
1227 const gchar *names[] = { roomname, NULL };
1229 dispatcher = empathy_dispatcher_dup_singleton();
1230 priv = GET_PRIV (dispatcher);
1232 connection = g_hash_table_lookup (priv->accounts, account);
1233 connection_data = g_hash_table_lookup (priv->connections, connection);
1236 /* Don't know the room handle yet */
1237 request_data = new_dispatcher_request_data (dispatcher, connection,
1238 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1239 NULL, callback, user_data);
1241 connection_data->outstanding_requests = g_list_prepend
1242 (connection_data->outstanding_requests, request_data);
1244 tp_cli_connection_call_request_handles (connection, -1,
1245 TP_HANDLE_TYPE_ROOM, names,
1246 dispatcher_request_handles_cb, request_data, NULL,
1247 G_OBJECT (dispatcher));
1249 g_object_unref (dispatcher);
1253 dispatcher_create_channel_cb (TpConnection *connect,
1254 const gchar *object_path,
1255 GHashTable *properties,
1256 const GError *error,
1258 GObject *weak_object)
1260 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1261 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1263 dispatcher_connection_new_requested_channel (dispatcher,
1264 request_data, object_path, properties, error);
1268 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1269 const GError *error,
1273 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1274 GValue *target_handle;
1276 g_assert (request_data->request);
1280 dispatcher_request_failed (request_data->dispatcher,
1281 request_data, error);
1285 request_data->handle = empathy_contact_get_handle (contact);
1287 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1288 g_value_set_uint (target_handle, request_data->handle);
1289 g_hash_table_insert (request_data->request,
1290 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1292 tp_cli_connection_interface_requests_call_create_channel (
1293 request_data->connection, -1,
1294 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1295 G_OBJECT (request_data->dispatcher));
1299 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1300 const GError *error,
1303 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1307 dispatcher_request_failed (request_data->dispatcher,
1308 request_data, error);
1312 empathy_contact_call_when_ready (request_data->contact,
1313 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1314 request_data, NULL, G_OBJECT (request_data->dispatcher));
1318 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1319 const gchar *filename,
1322 const gchar *content_type,
1323 EmpathyDispatcherRequestCb *callback,
1326 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1327 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1328 McAccount *account = empathy_contact_get_account (contact);
1329 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1330 ConnectionData *connection_data =
1331 g_hash_table_lookup (priv->connections, connection);
1332 DispatcherRequestData *request_data;
1334 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1335 (GDestroyNotify) tp_g_value_slice_free);
1337 /* org.freedesktop.Telepathy.Channel.ChannelType */
1338 value = tp_g_value_slice_new (G_TYPE_STRING);
1339 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1340 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1342 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1343 value = tp_g_value_slice_new (G_TYPE_UINT);
1344 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1345 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1347 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1348 value = tp_g_value_slice_new (G_TYPE_STRING);
1349 g_value_set_string (value, content_type);
1350 g_hash_table_insert (request,
1351 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1353 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1354 value = tp_g_value_slice_new (G_TYPE_STRING);
1355 g_value_set_string (value, filename);
1356 g_hash_table_insert (request,
1357 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1359 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1360 value = tp_g_value_slice_new (G_TYPE_UINT64);
1361 g_value_set_uint64 (value, size);
1362 g_hash_table_insert (request,
1363 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1365 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1366 value = tp_g_value_slice_new (G_TYPE_UINT64);
1367 g_value_set_uint64 (value, date);
1368 g_hash_table_insert (request,
1369 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1372 /* The contact handle might not be known yet */
1373 request_data = new_dispatcher_request_data (dispatcher, connection,
1374 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1375 contact, callback, user_data);
1376 connection_data->outstanding_requests = g_list_prepend
1377 (connection_data->outstanding_requests, request_data);
1379 tp_connection_call_when_ready (connection,
1380 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1382 g_object_unref (dispatcher);