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 channels. For all other it doesn't make
519 * sense to handle it if we didn't request it. The same goes for channels we
520 * discovered by the Channels property or ListChannels */
521 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
523 DEBUG ("Ignoring incoming channel of type %s on %s",
524 channel_type, object_path);
528 for (i = 0 ; blacklist[i] != NULL; i++)
530 if (!tp_strdiff (channel_type, blacklist[i]))
532 DEBUG ("Ignoring blacklisted channel type %s on %s",
533 channel_type, object_path);
538 DEBUG ("New channel of type %s on %s", channel_type, object_path);
540 if (properties == NULL)
541 channel = tp_channel_new (connection, object_path, channel_type,
542 handle_type, handle, NULL);
544 channel = tp_channel_new_from_properties (connection, object_path,
547 g_signal_connect (channel, "invalidated",
548 G_CALLBACK (dispatcher_channel_invalidated_cb),
551 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
553 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
554 contact = empathy_contact_factory_get_from_handle (factory,
555 cd->account, handle);
556 g_object_unref (factory);
559 operation = empathy_dispatch_operation_new (connection, channel, contact,
562 g_object_unref (channel);
566 /* Request could either be by us or by a remote party. If there are no
567 * outstanding requests for this channel type we can assume it's remote.
568 * Otherwise we wait untill they are all satisfied */
569 if (dispatcher_operation_can_start (dispatcher, operation, cd))
570 dispatcher_start_dispatching (dispatcher, operation, cd);
572 g_hash_table_insert (cd->outstanding_channels,
573 g_strdup (object_path), operation);
577 dispatcher_start_dispatching (dispatcher, operation, cd);
582 dispatcher_connection_new_channel_cb (TpConnection *connection,
583 const gchar *object_path,
584 const gchar *channel_type,
587 gboolean suppress_handler,
591 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
593 /* Empathy heavily abuses surpress handler (don't try this at home), if
594 * surpress handler is true then it is an outgoing channel, which is
595 * requested either by us or some other party (like the megaphone applet).
596 * Otherwise it's an incoming channel */
597 dispatcher_connection_new_channel (dispatcher, connection,
598 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
602 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
603 TpConnection *connection,
604 const gchar *object_path,
605 GHashTable *properties)
607 const gchar *channel_type;
614 channel_type = tp_asv_get_string (properties,
615 TP_IFACE_CHANNEL ".ChannelType");
616 if (channel_type == NULL)
618 g_message ("%s had an invalid ChannelType property", object_path);
622 handle_type = tp_asv_get_uint32 (properties,
623 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
626 g_message ("%s had an invalid TargetHandleType property", object_path);
630 handle = tp_asv_get_uint32 (properties,
631 TP_IFACE_CHANNEL ".TargetHandle", &valid);
634 g_message ("%s had an invalid TargetHandle property", object_path);
638 /* We assume there is no channel dispather, so we're the only one dispatching
639 * it. Which means that a requested channel it is outgoing one */
640 requested = tp_asv_get_boolean (properties,
641 TP_IFACE_CHANNEL ".Requested", &valid);
644 g_message ("%s had an invalid Requested property", object_path);
648 dispatcher_connection_new_channel (dispatcher, connection,
649 object_path, channel_type, handle_type, handle, properties, !requested);
653 dispatcher_connection_new_channels_cb (TpConnection *connection,
654 const GPtrArray *channels,
658 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
661 for (i = 0; i < channels->len ; i++)
663 GValueArray *arr = g_ptr_array_index (channels, i);
664 const gchar *object_path;
665 GHashTable *properties;
667 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
668 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
670 dispatcher_connection_new_channel_with_properties (dispatcher,
671 connection, object_path, properties);
676 dispatcher_connection_got_channels_property (TpProxy *proxy,
677 const GValue *channels_prop,
685 DEBUG ("Error: %s", error->message);
689 channels = g_value_get_boxed (channels_prop);
690 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
691 channels, NULL, object);
695 dispatcher_connection_list_channels_cb (TpConnection *connection,
696 const GPtrArray *channels,
705 DEBUG ("Error: %s", error->message);
709 for (i = 0; i < channels->len; i++)
713 values = g_ptr_array_index (channels, i);
714 /* We don't have any extra info, so assume already existing channels are
716 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
718 g_value_get_boxed (g_value_array_get_nth (values, 0)),
719 g_value_get_string (g_value_array_get_nth (values, 1)),
720 g_value_get_uint (g_value_array_get_nth (values, 2)),
721 g_value_get_uint (g_value_array_get_nth (values, 3)),
727 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
728 const GPtrArray *capabilities,
734 DEBUG ("Error: %s", error->message);
738 dispatcher_connection_ready_cb (TpConnection *connection,
742 GPtrArray *capabilities;
745 const gchar *remove = NULL;
749 dispatcher_connection_invalidated_cb (connection, error->domain,
750 error->code, error->message, dispatcher);
754 g_signal_connect (connection, "invalidated",
755 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
758 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
759 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
761 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
762 dispatcher_connection_new_channels_cb,
763 NULL, NULL, G_OBJECT (dispatcher), NULL);
765 tp_cli_dbus_properties_call_get (connection, -1,
766 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
767 dispatcher_connection_got_channels_property,
768 NULL, NULL, dispatcher);
772 tp_cli_connection_connect_to_new_channel (connection,
773 dispatcher_connection_new_channel_cb,
774 NULL, NULL, G_OBJECT (dispatcher), NULL);
776 tp_cli_connection_call_list_channels (connection, -1,
777 dispatcher_connection_list_channels_cb, NULL, NULL,
778 G_OBJECT (dispatcher));
782 /* Advertise VoIP capabilities */
783 /* FIXME: Capabilities is leaked */
784 capabilities = g_ptr_array_sized_new (1);
785 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
786 G_TYPE_UINT, G_TYPE_INVALID);
787 g_value_init (&cap, cap_type);
788 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
789 dbus_g_type_struct_set (&cap,
790 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
791 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
792 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
793 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
794 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
795 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
797 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
798 connection, -1, capabilities, &remove,
799 dispatcher_connection_advertise_capabilities_cb,
800 NULL, NULL, G_OBJECT (dispatcher));
804 dispatcher_update_account (EmpathyDispatcher *dispatcher,
807 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
808 TpConnection *connection;
810 connection = g_hash_table_lookup (priv->accounts, account);
811 if (connection != NULL)
814 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
815 if (connection == NULL)
818 g_hash_table_insert (priv->connections, g_object_ref (connection),
819 new_connection_data (account));
821 g_hash_table_insert (priv->accounts, g_object_ref (account),
822 g_object_ref (connection));
824 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
827 g_object_unref (connection);
831 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
833 TpConnectionStatusReason reason,
834 TpConnectionStatus status,
835 TpConnectionStatus previous,
836 EmpathyDispatcher *dispatcher)
838 dispatcher_update_account (dispatcher, account);
842 dispatcher_constructor (GType type,
843 guint n_construct_params,
844 GObjectConstructParam *construct_params)
848 if (dispatcher == NULL)
850 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
851 (type, n_construct_params, construct_params);
853 dispatcher = EMPATHY_DISPATCHER (retval);
854 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
858 retval = g_object_ref (dispatcher);
865 dispatcher_finalize (GObject *object)
867 EmpathyDispatcherPriv *priv = GET_PRIV (object);
869 g_signal_handlers_disconnect_by_func (priv->account_manager,
870 dispatcher_account_connection_cb, object);
872 g_object_unref (priv->account_manager);
873 g_object_unref (priv->mc);
875 g_hash_table_destroy (priv->accounts);
876 g_hash_table_destroy (priv->connections);
880 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
882 GObjectClass *object_class = G_OBJECT_CLASS (klass);
884 object_class->finalize = dispatcher_finalize;
885 object_class->constructor = dispatcher_constructor;
888 g_signal_new ("observe",
889 G_TYPE_FROM_CLASS (klass),
893 g_cclosure_marshal_VOID__OBJECT,
895 1, EMPATHY_TYPE_DISPATCH_OPERATION);
898 g_signal_new ("approve",
899 G_TYPE_FROM_CLASS (klass),
903 g_cclosure_marshal_VOID__OBJECT,
905 1, EMPATHY_TYPE_DISPATCH_OPERATION);
908 g_signal_new ("dispatch",
909 G_TYPE_FROM_CLASS (klass),
913 g_cclosure_marshal_VOID__OBJECT,
915 1, EMPATHY_TYPE_DISPATCH_OPERATION);
917 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
922 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
925 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
926 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
928 dispatcher->priv = priv;
929 priv->mc = empathy_mission_control_new ();
930 priv->account_manager = empathy_account_manager_dup_singleton ();
932 g_signal_connect (priv->account_manager,
933 "account-connection-changed",
934 G_CALLBACK (dispatcher_account_connection_cb),
937 priv->accounts = g_hash_table_new_full (empathy_account_hash,
938 empathy_account_equal, g_object_unref, g_object_unref);
940 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
941 g_object_unref, (GDestroyNotify) free_connection_data);
943 accounts = mc_accounts_list_by_enabled (TRUE);
945 for (l = accounts; l; l = l->next)
947 dispatcher_update_account (dispatcher, l->data);
948 g_object_unref (l->data);
950 g_list_free (accounts);
954 empathy_dispatcher_dup_singleton (void)
956 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
960 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
961 DispatcherRequestData *request_data,
964 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
965 ConnectionData *conn_data;
967 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
968 if (request_data->cb != NULL)
969 request_data->cb (NULL, error, request_data->user_data);
971 conn_data->outstanding_requests =
972 g_list_remove (conn_data->outstanding_requests, request_data);
973 free_dispatcher_request_data (request_data);
977 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
978 DispatcherRequestData *request_data,
979 const gchar *object_path,
980 GHashTable *properties,
983 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
984 EmpathyDispatchOperation *operation = NULL;
985 ConnectionData *conn_data;
987 conn_data = g_hash_table_lookup (priv->connections,
988 request_data->connection);
992 DEBUG ("Channel request failed: %s", error->message);
994 dispatcher_request_failed (dispatcher, request_data, error);
999 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1002 if (operation != NULL)
1003 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1005 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1008 if (operation == NULL)
1010 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1015 operation = empathy_dispatch_operation_new_with_wrapper (
1016 request_data->connection,
1017 data->channel, request_data->contact, FALSE,
1018 data->channel_wrapper);
1024 if (properties != NULL)
1025 channel = tp_channel_new_from_properties (request_data->connection,
1026 object_path, properties, NULL);
1028 channel = tp_channel_new (request_data->connection, object_path,
1029 request_data->channel_type, request_data->handle_type,
1030 request_data->handle, NULL);
1032 g_signal_connect (channel, "invalidated",
1033 G_CALLBACK (dispatcher_channel_invalidated_cb),
1034 request_data->dispatcher);
1036 operation = empathy_dispatch_operation_new (request_data->connection,
1037 channel, request_data->contact, FALSE);
1038 g_object_unref (channel);
1043 /* Already existed set potential extra information */
1044 g_object_set (G_OBJECT (operation),
1045 "contact", request_data->contact,
1049 request_data->operation = operation;
1051 /* (pre)-approve this right away as we requested it */
1052 empathy_dispatch_operation_approve (operation);
1054 dispatcher_start_dispatching (request_data->dispatcher, operation,
1057 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1062 dispatcher_request_channel_cb (TpConnection *connection,
1063 const gchar *object_path,
1064 const GError *error,
1066 GObject *weak_object)
1068 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1069 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1071 dispatcher_connection_new_requested_channel (dispatcher,
1072 request_data, object_path, NULL, error);
1076 dispatcher_request_channel (DispatcherRequestData *request_data)
1078 tp_cli_connection_call_request_channel (request_data->connection, -1,
1079 request_data->channel_type,
1080 request_data->handle_type,
1081 request_data->handle,
1082 TRUE, dispatcher_request_channel_cb,
1083 request_data, NULL, G_OBJECT (request_data->dispatcher));
1087 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1088 EmpathyDispatcherRequestCb *callback,
1091 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1092 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1094 TpConnection *connection;
1096 DispatcherRequestData *request_data;
1098 account = empathy_contact_get_account (contact);
1099 connection = g_hash_table_lookup (priv->accounts, account);
1101 g_assert (connection != NULL);
1102 cd = g_hash_table_lookup (priv->connections, connection);
1103 request_data = new_dispatcher_request_data (dispatcher, connection,
1104 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1105 contact, callback, user_data);
1107 cd->outstanding_requests = g_list_prepend
1108 (cd->outstanding_requests, request_data);
1110 dispatcher_request_channel (request_data);
1112 g_object_unref (dispatcher);
1116 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1117 const GError *error,
1121 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1123 request_data->handle = empathy_contact_get_handle (contact);
1125 dispatcher_request_channel (request_data);
1129 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1130 EmpathyDispatcherRequestCb *callback,
1133 EmpathyDispatcher *dispatcher;
1134 EmpathyDispatcherPriv *priv;
1136 TpConnection *connection;
1137 ConnectionData *connection_data;
1138 DispatcherRequestData *request_data;
1140 dispatcher = empathy_dispatcher_dup_singleton();
1141 priv = GET_PRIV (dispatcher);
1143 account = empathy_contact_get_account (contact);
1144 connection = g_hash_table_lookup (priv->accounts, account);
1145 connection_data = g_hash_table_lookup (priv->connections, connection);
1147 /* The contact handle might not be known yet */
1148 request_data = new_dispatcher_request_data (dispatcher, connection,
1149 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1150 contact, callback, user_data);
1152 connection_data->outstanding_requests = g_list_prepend
1153 (connection_data->outstanding_requests, request_data);
1155 empathy_contact_call_when_ready (contact,
1156 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1157 request_data, NULL, G_OBJECT (dispatcher));
1159 g_object_unref (dispatcher);
1163 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1164 const gchar *contact_id,
1165 EmpathyDispatcherRequestCb *callback,
1168 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1169 EmpathyContactFactory *factory;
1170 EmpathyContact *contact;
1172 factory = empathy_contact_factory_dup_singleton ();
1173 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1175 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1177 g_object_unref (contact);
1178 g_object_unref (factory);
1179 g_object_unref (dispatcher);
1183 dispatcher_request_handles_cb (TpConnection *connection,
1184 const GArray *handles,
1185 const GError *error,
1189 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1193 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1194 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1197 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1199 if (request_data->cb)
1200 request_data->cb (NULL, error, request_data->user_data);
1202 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1205 free_dispatcher_request_data (request_data);
1207 dispatcher_flush_outstanding_operations (dispatcher, cd);
1211 request_data->handle = g_array_index (handles, guint, 0);
1212 dispatcher_request_channel (request_data);
1216 empathy_dispatcher_join_muc (McAccount *account,
1217 const gchar *roomname,
1218 EmpathyDispatcherRequestCb *callback,
1221 EmpathyDispatcher *dispatcher;
1222 EmpathyDispatcherPriv *priv;
1223 DispatcherRequestData *request_data;
1224 TpConnection *connection;
1225 ConnectionData *connection_data;
1226 const gchar *names[] = { roomname, NULL };
1228 dispatcher = empathy_dispatcher_dup_singleton();
1229 priv = GET_PRIV (dispatcher);
1231 connection = g_hash_table_lookup (priv->accounts, account);
1232 connection_data = g_hash_table_lookup (priv->connections, connection);
1235 /* Don't know the room handle yet */
1236 request_data = new_dispatcher_request_data (dispatcher, connection,
1237 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1238 NULL, callback, user_data);
1240 connection_data->outstanding_requests = g_list_prepend
1241 (connection_data->outstanding_requests, request_data);
1243 tp_cli_connection_call_request_handles (connection, -1,
1244 TP_HANDLE_TYPE_ROOM, names,
1245 dispatcher_request_handles_cb, request_data, NULL,
1246 G_OBJECT (dispatcher));
1248 g_object_unref (dispatcher);
1252 dispatcher_create_channel_cb (TpConnection *connect,
1253 const gchar *object_path,
1254 GHashTable *properties,
1255 const GError *error,
1257 GObject *weak_object)
1259 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1260 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1262 dispatcher_connection_new_requested_channel (dispatcher,
1263 request_data, object_path, properties, error);
1267 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1268 const GError *error,
1272 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1273 GValue *target_handle;
1275 g_assert (request_data->request);
1279 dispatcher_request_failed (request_data->dispatcher,
1280 request_data, error);
1284 request_data->handle = empathy_contact_get_handle (contact);
1286 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1287 g_value_set_uint (target_handle, request_data->handle);
1288 g_hash_table_insert (request_data->request,
1289 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1291 tp_cli_connection_interface_requests_call_create_channel (
1292 request_data->connection, -1,
1293 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1294 G_OBJECT (request_data->dispatcher));
1298 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1299 const GError *error,
1302 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1306 dispatcher_request_failed (request_data->dispatcher,
1307 request_data, error);
1311 empathy_contact_call_when_ready (request_data->contact,
1312 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1313 request_data, NULL, G_OBJECT (request_data->dispatcher));
1317 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1318 const gchar *filename,
1321 const gchar *content_type,
1322 EmpathyDispatcherRequestCb *callback,
1325 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1326 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1327 McAccount *account = empathy_contact_get_account (contact);
1328 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1329 ConnectionData *connection_data =
1330 g_hash_table_lookup (priv->connections, connection);
1331 DispatcherRequestData *request_data;
1333 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1334 (GDestroyNotify) tp_g_value_slice_free);
1336 /* org.freedesktop.Telepathy.Channel.ChannelType */
1337 value = tp_g_value_slice_new (G_TYPE_STRING);
1338 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1339 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1341 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1342 value = tp_g_value_slice_new (G_TYPE_UINT);
1343 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1344 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1346 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1347 value = tp_g_value_slice_new (G_TYPE_STRING);
1348 g_value_set_string (value, content_type);
1349 g_hash_table_insert (request,
1350 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1352 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1353 value = tp_g_value_slice_new (G_TYPE_STRING);
1354 g_value_set_string (value, filename);
1355 g_hash_table_insert (request,
1356 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1358 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1359 value = tp_g_value_slice_new (G_TYPE_UINT64);
1360 g_value_set_uint64 (value, size);
1361 g_hash_table_insert (request,
1362 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1364 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1365 value = tp_g_value_slice_new (G_TYPE_UINT64);
1366 g_value_set_uint64 (value, date);
1367 g_hash_table_insert (request,
1368 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1371 /* The contact handle might not be known yet */
1372 request_data = new_dispatcher_request_data (dispatcher, connection,
1373 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1374 contact, callback, user_data);
1375 connection_data->outstanding_requests = g_list_prepend
1376 (connection_data->outstanding_requests, request_data);
1378 tp_connection_call_when_ready (connection,
1379 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1381 g_object_unref (dispatcher);