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-group.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)
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);
71 static guint signals[LAST_SIGNAL];
72 static EmpathyDispatcher *dispatcher = NULL;
75 EmpathyDispatcher *dispatcher;
76 EmpathyDispatchOperation *operation;
77 TpConnection *connection;
81 EmpathyContact *contact;
83 /* Properties to pass to the channel when requesting it */
85 EmpathyDispatcherRequestCb *cb;
87 gpointer *request_data;
88 } DispatcherRequestData;
92 /* Channel type specific wrapper object */
93 GObject *channel_wrapper;
98 /* ObjectPath => DispatchData.. */
99 GHashTable *dispatched_channels;
100 /* ObjectPath -> EmpathyDispatchOperations */
101 GHashTable *dispatching_channels;
102 /* ObjectPath -> EmpathyDispatchOperations */
103 GHashTable *outstanding_channels;
104 /* List of DispatcherRequestData */
105 GList *outstanding_requests;
108 static DispatchData *
109 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
111 DispatchData *d = g_slice_new0 (DispatchData);
112 d->channel = g_object_ref (channel);
113 d->channel_wrapper = g_object_ref (channel_wrapper);
119 free_dispatch_data (DispatchData *data)
121 g_object_unref (data->channel);
122 g_object_unref (data->channel_wrapper);
124 g_slice_free (DispatchData, data);
128 static DispatcherRequestData *
129 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
130 TpConnection *connection, const gchar *channel_type, guint handle_type,
131 guint handle, GHashTable *request,
132 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
134 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
136 result->dispatcher = dispatcher;
137 result->connection = connection;
139 result->channel_type = g_strdup (channel_type);
140 result->handle_type = handle_type;
141 result->handle = handle;
142 result->request = request;
145 result->contact = g_object_ref (contact);
148 result->user_data = user_data;
154 free_dispatcher_request_data (DispatcherRequestData *r)
156 g_free (r->channel_type);
158 if (r->contact != NULL)
159 g_object_unref (r->contact);
161 if (r->request != NULL)
162 g_hash_table_unref (r->request);
164 g_slice_free (DispatcherRequestData, r);
167 static ConnectionData *
168 new_connection_data (McAccount *account)
170 ConnectionData *cd = g_slice_new0 (ConnectionData);
171 cd->account = g_object_ref (account);
173 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
174 g_free, (GDestroyNotify) free_dispatch_data);
176 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
177 g_free, g_object_unref);
179 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
186 free_connection_data (ConnectionData *cd)
189 g_object_unref (cd->account);
190 g_hash_table_destroy (cd->dispatched_channels);
191 g_hash_table_destroy (cd->dispatching_channels);
193 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
195 free_dispatcher_request_data (l->data);
200 dispatcher_connection_invalidated_cb (TpConnection *connection,
201 guint domain, gint code, gchar *message,
202 EmpathyDispatcher *dispatcher)
204 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
207 DEBUG ("Error: %s", message);
208 cd = g_hash_table_lookup (priv->connections, connection);
210 g_hash_table_remove (priv->accounts, cd->account);
211 g_hash_table_remove (priv->connections, connection);
215 dispatcher_operation_can_start (EmpathyDispatcher *self,
216 EmpathyDispatchOperation *operation, ConnectionData *cd)
219 const gchar *channel_type =
220 empathy_dispatch_operation_get_channel_type (operation);
222 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
224 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
226 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
236 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
237 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
241 l = cd->outstanding_requests;
244 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
249 if (d->operation == operation)
254 d->cb (NULL, error, d->user_data);
256 d->cb (operation, NULL, d->user_data);
259 cd->outstanding_requests = g_list_delete_link
260 (cd->outstanding_requests, lt);
262 free_dispatcher_request_data (d);
268 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
269 gchar *message, EmpathyDispatcher *dispatcher)
271 /* Channel went away... */
272 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
273 TpConnection *connection;
274 EmpathyDispatchOperation *operation;
276 const gchar *object_path;
278 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
280 cd = g_hash_table_lookup (priv->connections, connection);
281 /* Connection itself invalidated? */
285 object_path = tp_proxy_get_object_path (proxy);
287 DEBUG ("Channel %s invalidated", object_path);
289 g_hash_table_remove (cd->dispatched_channels, object_path);
290 g_hash_table_remove (cd->dispatching_channels, object_path);
292 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
293 if (operation != NULL)
295 GError error = { domain, code, message };
296 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
297 g_hash_table_remove (cd->outstanding_channels, object_path);
298 g_object_unref (operation);
303 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
304 EmpathyDispatcher *dispatcher)
306 g_assert (empathy_dispatch_operation_is_incoming (operation));
307 DEBUG ("Send of for dispatching: %s",
308 empathy_dispatch_operation_get_object_path (operation));
309 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
313 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
314 EmpathyDispatcher *dispatcher)
316 /* Our job is done, remove the dispatch operation and mark the channel as
318 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319 TpConnection *connection;
321 const gchar *object_path;
323 connection = empathy_dispatch_operation_get_tp_connection (operation);
324 cd = g_hash_table_lookup (priv->connections, connection);
325 g_assert (cd != NULL);
326 g_object_unref (G_OBJECT (connection));
328 object_path = empathy_dispatch_operation_get_object_path (operation);
330 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
333 d = new_dispatch_data (
334 empathy_dispatch_operation_get_channel (operation),
335 empathy_dispatch_operation_get_channel_wrapper (operation));
336 g_hash_table_insert (cd->dispatched_channels,
337 g_strdup (object_path), d);
339 g_hash_table_remove (cd->dispatching_channels, object_path);
341 DEBUG ("Channel claimed: %s", object_path);
345 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
346 EmpathyDispatcher *dispatcher)
348 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
349 TpConnection *connection;
351 EmpathyDispatchOperationState status;
353 g_signal_connect (operation, "approved",
354 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
356 g_signal_connect (operation, "claimed",
357 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
359 /* Signal the observers */
360 DEBUG ("Send to observers: %s",
361 empathy_dispatch_operation_get_object_path (operation));
362 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
364 empathy_dispatch_operation_start (operation);
366 /* Signal potential requestors */
367 connection = empathy_dispatch_operation_get_tp_connection (operation);
368 cd = g_hash_table_lookup (priv->connections, connection);
369 g_assert (cd != NULL);
370 g_object_unref (G_OBJECT (connection));
372 g_object_ref (operation);
374 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
375 status = empathy_dispatch_operation_get_status (operation);
376 g_object_unref (operation);
378 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
381 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
383 DEBUG ("Send to approvers: %s",
384 empathy_dispatch_operation_get_object_path (operation));
385 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
389 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
390 DEBUG ("Send of for dispatching: %s",
391 empathy_dispatch_operation_get_object_path (operation));
392 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
398 dispatcher_start_dispatching (EmpathyDispatcher *self,
399 EmpathyDispatchOperation *operation, ConnectionData *cd)
401 const gchar *object_path =
402 empathy_dispatch_operation_get_object_path (operation);
404 DEBUG ("Dispatching process started for %s", object_path);
406 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
408 g_assert (g_hash_table_lookup (cd->outstanding_channels,
409 object_path) == NULL);
411 g_hash_table_insert (cd->dispatching_channels,
412 g_strdup (object_path), operation);
414 switch (empathy_dispatch_operation_get_status (operation))
416 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
417 g_signal_connect (operation, "ready",
418 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
420 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
421 dispatch_operation_ready_cb (operation, dispatcher);
424 g_assert_not_reached();
428 else if (empathy_dispatch_operation_get_status (operation) >=
429 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
431 /* Already dispatching and the operation is pending, thus the observers
432 * have seen it (if applicable), so we can flush the request right away.
434 dispatch_operation_flush_requests (self, operation, NULL, cd);
440 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
446 g_hash_table_iter_init (&iter, cd->outstanding_channels);
447 while (g_hash_table_iter_next (&iter, NULL, &value))
449 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
451 if (dispatcher_operation_can_start (self, operation, cd))
453 dispatcher_start_dispatching (dispatcher, operation, cd);
454 g_hash_table_iter_remove (&iter);
461 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
462 TpConnection *connection,
463 const gchar *object_path, const gchar *channel_type,
464 guint handle_type, guint handle, GHashTable *properties,
467 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
470 EmpathyDispatchOperation *operation;
471 EmpathyContact *contact = NULL;
473 /* Channel types we never want to dispatch because they're either deprecated
474 * or can't sensibly be dispatch (e.g. channels that should always be
476 const char *blacklist[] = {
477 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
478 TP_IFACE_CHANNEL_TYPE_TUBES,
479 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
483 cd = g_hash_table_lookup (priv->connections, connection);
485 /* Don't bother with channels we have already dispatched or are dispatching
486 * currently. This can happen when NewChannel(s) is fired after
487 * RequestChannel/CreateChannel/EnsureChannel */
488 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
491 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
494 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
496 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
499 /* Only pick up non-requested text channels. For all other it doesn't make
500 * sense to handle it if we didn't request it. The same goes for channels we
501 * discovered by the Channels property or ListChannels */
502 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
504 DEBUG ("Ignoring incoming channel of type %s on %s",
505 channel_type, object_path);
509 for (i = 0 ; blacklist[i] != NULL; i++)
511 if (!tp_strdiff (channel_type, blacklist[i]))
513 DEBUG ("Ignoring blacklisted channel type %s on %s",
514 channel_type, object_path);
519 DEBUG ("New channel of type %s on %s", channel_type, object_path);
521 if (properties == NULL)
522 channel = tp_channel_new (connection, object_path, channel_type,
523 handle_type, handle, NULL);
525 channel = tp_channel_new_from_properties (connection, object_path,
528 g_signal_connect (channel, "invalidated",
529 G_CALLBACK (dispatcher_channel_invalidated_cb),
532 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
534 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
535 contact = empathy_contact_factory_get_from_handle (factory,
536 cd->account, handle);
537 g_object_unref (factory);
540 operation = empathy_dispatch_operation_new (connection, channel, contact,
543 g_object_unref (channel);
547 /* Request could either be by us or by a remote party. If there are no
548 * outstanding requests for this channel type we can assume it's remote.
549 * Otherwise we wait untill they are all satisfied */
550 if (dispatcher_operation_can_start (dispatcher, operation, cd))
551 dispatcher_start_dispatching (dispatcher, operation, cd);
553 g_hash_table_insert (cd->outstanding_channels,
554 g_strdup (object_path), operation);
558 dispatcher_start_dispatching (dispatcher, operation, cd);
563 dispatcher_connection_new_channel_cb (TpConnection *connection,
564 const gchar *object_path, const gchar *channel_type,
565 guint handle_type, guint handle,
566 gboolean suppress_handler, gpointer user_data,
569 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
571 /* Empathy heavily abuses surpress handler (don't try this at home), if
572 * surpress handler is true then it is an outgoing channel, which is
573 * requested either by us or some other party (like the megaphone applet).
574 * Otherwise it's an incoming channel */
575 dispatcher_connection_new_channel (dispatcher, connection,
576 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
580 dispatcher_connection_new_channel_with_properties (
581 EmpathyDispatcher *dispatcher, TpConnection *connection,
582 const gchar *object_path, GHashTable *properties)
584 const gchar *channel_type;
591 channel_type = tp_asv_get_string (properties,
592 TP_IFACE_CHANNEL ".ChannelType");
593 if (channel_type == NULL)
595 g_message ("%s had an invalid ChannelType property", object_path);
599 handle_type = tp_asv_get_uint32 (properties,
600 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
603 g_message ("%s had an invalid TargetHandleType property", object_path);
607 handle = tp_asv_get_uint32 (properties,
608 TP_IFACE_CHANNEL ".TargetHandle", &valid);
611 g_message ("%s had an invalid TargetHandle property", object_path);
615 /* We assume there is no channel dispather, so we're the only one dispatching
616 * it. Which means that a requested channel it is outgoing one */
617 requested = tp_asv_get_boolean (properties,
618 TP_IFACE_CHANNEL ".Requested", &valid);
621 g_message ("%s had an invalid Requested property", object_path);
625 dispatcher_connection_new_channel (dispatcher, connection,
626 object_path, channel_type, handle_type, handle, properties, !requested);
631 dispatcher_connection_new_channels_cb (
632 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
635 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
638 for (i = 0; i < channels->len ; i++)
640 GValueArray *arr = g_ptr_array_index (channels, i);
641 const gchar *object_path;
642 GHashTable *properties;
644 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
645 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
647 dispatcher_connection_new_channel_with_properties (dispatcher,
648 connection, object_path, properties);
653 dispatcher_connection_got_channels_property (TpProxy *proxy,
654 const GValue *channels_prop, const GError *error, gpointer user_data,
660 DEBUG ("Error: %s", error->message);
664 channels = g_value_get_boxed (channels_prop);
665 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
666 channels, NULL, object);
670 dispatcher_connection_list_channels_cb (TpConnection *connection,
671 const GPtrArray *channels,
679 DEBUG ("Error: %s", error->message);
683 for (i = 0; i < channels->len; i++) {
686 values = g_ptr_array_index (channels, i);
687 /* We don't have any extra info, so assume already existing channels are
689 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
691 g_value_get_boxed (g_value_array_get_nth (values, 0)),
692 g_value_get_string (g_value_array_get_nth (values, 1)),
693 g_value_get_uint (g_value_array_get_nth (values, 2)),
694 g_value_get_uint (g_value_array_get_nth (values, 3)),
700 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
701 const GPtrArray *capabilities,
707 DEBUG ("Error: %s", error->message);
712 dispatcher_connection_ready_cb (TpConnection *connection,
713 const GError *error, gpointer dispatcher)
715 GPtrArray *capabilities;
718 const gchar *remove = NULL;
722 dispatcher_connection_invalidated_cb (connection, error->domain,
723 error->code, error->message, dispatcher);
727 g_signal_connect (connection, "invalidated",
728 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
731 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
732 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
734 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
735 dispatcher_connection_new_channels_cb,
736 NULL, NULL, G_OBJECT (dispatcher), NULL);
738 tp_cli_dbus_properties_call_get (connection, -1,
739 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
740 dispatcher_connection_got_channels_property,
741 NULL, NULL, dispatcher);
745 tp_cli_connection_connect_to_new_channel (connection,
746 dispatcher_connection_new_channel_cb,
747 NULL, NULL, G_OBJECT (dispatcher), NULL);
749 tp_cli_connection_call_list_channels (connection, -1,
750 dispatcher_connection_list_channels_cb, NULL, NULL,
751 G_OBJECT (dispatcher));
755 /* Advertise VoIP capabilities */
756 /* FIXME: Capabilities is leaked */
757 capabilities = g_ptr_array_sized_new (1);
758 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
759 G_TYPE_UINT, G_TYPE_INVALID);
760 g_value_init (&cap, cap_type);
761 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
762 dbus_g_type_struct_set (&cap,
763 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
764 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
765 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
766 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
767 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
768 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
770 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
771 connection, -1, capabilities, &remove,
772 dispatcher_connection_advertise_capabilities_cb,
773 NULL, NULL, G_OBJECT (dispatcher));
777 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
779 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
780 TpConnection *connection;
782 connection = g_hash_table_lookup (priv->accounts, account);
783 if (connection != NULL)
786 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
787 if (connection == NULL)
790 g_hash_table_insert (priv->connections, g_object_ref (connection),
791 new_connection_data (account));
793 g_hash_table_insert (priv->accounts, g_object_ref (account),
794 g_object_ref (connection));
796 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
799 g_object_unref (connection);
803 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
804 McAccount *account, TpConnectionStatusReason reason,
805 TpConnectionStatus status, TpConnectionStatus previous,
806 EmpathyDispatcher *dispatcher)
808 dispatcher_update_account (dispatcher, account);
813 dispatcher_constructor (GType type, guint n_construct_params,
814 GObjectConstructParam *construct_params)
818 if (dispatcher == NULL)
820 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
821 (type, n_construct_params, construct_params);
823 dispatcher = EMPATHY_DISPATCHER (retval);
824 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
828 retval = g_object_ref (dispatcher);
835 dispatcher_finalize (GObject *object)
837 EmpathyDispatcherPriv *priv = GET_PRIV (object);
839 g_signal_handlers_disconnect_by_func (priv->account_manager,
840 dispatcher_account_connection_cb, object);
842 g_object_unref (priv->account_manager);
843 g_object_unref (priv->mc);
845 g_hash_table_destroy (priv->accounts);
846 g_hash_table_destroy (priv->connections);
850 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
852 GObjectClass *object_class = G_OBJECT_CLASS (klass);
854 object_class->finalize = dispatcher_finalize;
855 object_class->constructor = dispatcher_constructor;
858 g_signal_new ("observe",
859 G_TYPE_FROM_CLASS (klass),
863 g_cclosure_marshal_VOID__OBJECT,
865 1, EMPATHY_TYPE_DISPATCH_OPERATION);
868 g_signal_new ("approve",
869 G_TYPE_FROM_CLASS (klass),
873 g_cclosure_marshal_VOID__OBJECT,
875 1, EMPATHY_TYPE_DISPATCH_OPERATION);
878 g_signal_new ("dispatch",
879 G_TYPE_FROM_CLASS (klass),
883 g_cclosure_marshal_VOID__OBJECT,
885 1, EMPATHY_TYPE_DISPATCH_OPERATION);
887 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
892 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
895 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
896 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
898 dispatcher->priv = priv;
899 priv->mc = empathy_mission_control_new ();
900 priv->account_manager = empathy_account_manager_dup_singleton ();
902 g_signal_connect (priv->account_manager,
903 "account-connection-changed",
904 G_CALLBACK (dispatcher_account_connection_cb),
907 priv->accounts = g_hash_table_new_full (empathy_account_hash,
908 empathy_account_equal, g_object_unref, g_object_unref);
910 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
911 g_object_unref, (GDestroyNotify) free_connection_data);
913 accounts = mc_accounts_list_by_enabled (TRUE);
915 for (l = accounts; l; l = l->next) {
916 dispatcher_update_account (dispatcher, l->data);
917 g_object_unref (l->data);
919 g_list_free (accounts);
923 empathy_dispatcher_dup_singleton (void)
925 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
929 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
930 DispatcherRequestData *request_data, const GError *error)
932 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
933 ConnectionData *conn_data;
935 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
936 if (request_data->cb != NULL)
937 request_data->cb (NULL, error, request_data->user_data);
939 conn_data->outstanding_requests =
940 g_list_remove (conn_data->outstanding_requests, request_data);
941 free_dispatcher_request_data (request_data);
945 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
946 DispatcherRequestData *request_data, const gchar *object_path,
947 GHashTable *properties, const GError *error)
949 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
950 EmpathyDispatchOperation *operation = NULL;
951 ConnectionData *conn_data;
953 conn_data = g_hash_table_lookup (priv->connections,
954 request_data->connection);
958 DEBUG ("Channel request failed: %s", error->message);
960 dispatcher_request_failed (dispatcher, request_data, error);
965 operation = g_hash_table_lookup (conn_data->outstanding_channels,
968 if (operation != NULL)
969 g_hash_table_remove (conn_data->outstanding_channels, object_path);
971 operation = g_hash_table_lookup (conn_data->dispatching_channels,
974 if (operation == NULL)
976 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
981 operation = empathy_dispatch_operation_new_with_wrapper (
982 request_data->connection,
983 data->channel, request_data->contact, FALSE,
984 data->channel_wrapper);
990 if (properties != NULL)
991 channel = tp_channel_new_from_properties (request_data->connection,
992 object_path, properties, NULL);
994 channel = tp_channel_new (request_data->connection, object_path,
995 request_data->channel_type, request_data->handle_type,
996 request_data->handle, NULL);
998 g_signal_connect (channel, "invalidated",
999 G_CALLBACK (dispatcher_channel_invalidated_cb),
1000 request_data->dispatcher);
1002 operation = empathy_dispatch_operation_new (request_data->connection,
1003 channel, request_data->contact, FALSE);
1004 g_object_unref (channel);
1009 /* Already existed set potential extra information */
1010 g_object_set (G_OBJECT (operation),
1011 "contact", request_data->contact,
1015 request_data->operation = operation;
1017 /* (pre)-approve this right away as we requested it */
1018 empathy_dispatch_operation_approve (operation);
1020 dispatcher_start_dispatching (request_data->dispatcher, operation,
1023 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1028 dispatcher_request_channel_cb (TpConnection *connection,
1029 const gchar *object_path, const GError *error,
1030 gpointer user_data, GObject *weak_object)
1032 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1033 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1035 dispatcher_connection_new_requested_channel (dispatcher,
1036 request_data, object_path, NULL, error);
1040 dispatcher_request_channel (DispatcherRequestData *request_data)
1042 tp_cli_connection_call_request_channel (request_data->connection, -1,
1043 request_data->channel_type,
1044 request_data->handle_type,
1045 request_data->handle,
1046 TRUE, dispatcher_request_channel_cb,
1047 request_data, NULL, G_OBJECT (request_data->dispatcher));
1051 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1052 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1054 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1055 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1057 TpConnection *connection;
1059 DispatcherRequestData *request_data;
1061 account = empathy_contact_get_account (contact);
1062 connection = g_hash_table_lookup (priv->accounts, account);
1064 g_assert (connection != NULL);
1065 cd = g_hash_table_lookup (priv->connections, connection);
1066 request_data = new_dispatcher_request_data (dispatcher, connection,
1067 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1068 contact, callback, user_data);
1070 cd->outstanding_requests = g_list_prepend
1071 (cd->outstanding_requests, request_data);
1073 dispatcher_request_channel (request_data);
1075 g_object_unref (dispatcher);
1080 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1081 const GError *error, gpointer user_data, GObject *object)
1083 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1085 request_data->handle = empathy_contact_get_handle (contact);
1087 dispatcher_request_channel (request_data);
1091 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1092 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1094 EmpathyDispatcher *dispatcher;
1095 EmpathyDispatcherPriv *priv;
1097 TpConnection *connection;
1098 ConnectionData *connection_data;
1099 DispatcherRequestData *request_data;
1101 dispatcher = empathy_dispatcher_dup_singleton();
1102 priv = GET_PRIV (dispatcher);
1104 account = empathy_contact_get_account (contact);
1105 connection = g_hash_table_lookup (priv->accounts, account);
1106 connection_data = g_hash_table_lookup (priv->connections, connection);
1108 /* The contact handle might not be known yet */
1109 request_data = new_dispatcher_request_data (dispatcher, connection,
1110 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1111 contact, callback, user_data);
1113 connection_data->outstanding_requests = g_list_prepend
1114 (connection_data->outstanding_requests, request_data);
1116 empathy_contact_call_when_ready (contact,
1117 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1118 request_data, NULL, G_OBJECT (dispatcher));
1120 g_object_unref (dispatcher);
1124 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1125 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1127 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1128 EmpathyContactFactory *factory;
1129 EmpathyContact *contact;
1131 factory = empathy_contact_factory_dup_singleton ();
1132 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1134 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1136 g_object_unref (contact);
1137 g_object_unref (factory);
1138 g_object_unref (dispatcher);
1143 dispatcher_request_handles_cb (TpConnection *connection,
1144 const GArray *handles, const GError *error, gpointer user_data,
1147 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1151 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1152 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1155 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1157 if (request_data->cb)
1158 request_data->cb (NULL, error, request_data->user_data);
1160 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1163 free_dispatcher_request_data (request_data);
1165 dispatcher_flush_outstanding_operations (dispatcher, cd);
1169 request_data->handle = g_array_index (handles, guint, 0);
1170 dispatcher_request_channel (request_data);
1174 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1175 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1177 EmpathyDispatcher *dispatcher;
1178 EmpathyDispatcherPriv *priv;
1179 DispatcherRequestData *request_data;
1180 TpConnection *connection;
1181 ConnectionData *connection_data;
1182 const gchar *names[] = { roomname, NULL };
1184 dispatcher = empathy_dispatcher_dup_singleton();
1185 priv = GET_PRIV (dispatcher);
1187 connection = g_hash_table_lookup (priv->accounts, account);
1188 connection_data = g_hash_table_lookup (priv->connections, connection);
1191 /* Don't know the room handle yet */
1192 request_data = new_dispatcher_request_data (dispatcher, connection,
1193 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1194 NULL, callback, user_data);
1196 connection_data->outstanding_requests = g_list_prepend
1197 (connection_data->outstanding_requests, request_data);
1199 tp_cli_connection_call_request_handles (connection, -1,
1200 TP_HANDLE_TYPE_ROOM, names,
1201 dispatcher_request_handles_cb, request_data, NULL,
1202 G_OBJECT (dispatcher));
1204 g_object_unref (dispatcher);
1208 dispatcher_create_channel_cb (TpConnection *connect,
1209 const gchar *object_path, GHashTable *properties, const GError *error,
1210 gpointer user_data, GObject *weak_object)
1212 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1213 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1215 dispatcher_connection_new_requested_channel (dispatcher,
1216 request_data, object_path, properties, error);
1221 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1222 const GError *error, gpointer user_data, GObject *object)
1224 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1225 GValue *target_handle;
1227 g_assert (request_data->request);
1231 dispatcher_request_failed (request_data->dispatcher,
1232 request_data, error);
1236 request_data->handle = empathy_contact_get_handle (contact);
1238 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1239 g_value_set_uint (target_handle, request_data->handle);
1240 g_hash_table_insert (request_data->request,
1241 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1243 tp_cli_connection_interface_requests_call_create_channel (
1244 request_data->connection, -1,
1245 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1246 G_OBJECT (request_data->dispatcher));
1250 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1251 const gchar *filename, guint64 size, guint64 date,
1252 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1255 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1256 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1257 McAccount *account = empathy_contact_get_account (contact);
1258 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1259 ConnectionData *connection_data =
1260 g_hash_table_lookup (priv->connections, connection);
1261 DispatcherRequestData *request_data;
1263 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1264 (GDestroyNotify) tp_g_value_slice_free);
1266 /* org.freedesktop.Telepathy.Channel.ChannelType */
1267 value = tp_g_value_slice_new (G_TYPE_STRING);
1268 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1269 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1271 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1272 value = tp_g_value_slice_new (G_TYPE_UINT);
1273 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1274 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1276 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1277 value = tp_g_value_slice_new (G_TYPE_STRING);
1278 g_value_set_string (value, content_type);
1279 g_hash_table_insert (request,
1280 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1282 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1283 value = tp_g_value_slice_new (G_TYPE_STRING);
1284 g_value_set_string (value, filename);
1285 g_hash_table_insert (request,
1286 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1288 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1289 value = tp_g_value_slice_new (G_TYPE_UINT64);
1290 g_value_set_uint64 (value, size);
1291 g_hash_table_insert (request,
1292 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1294 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1295 value = tp_g_value_slice_new (G_TYPE_UINT64);
1296 g_value_set_uint64 (value, date);
1297 g_hash_table_insert (request,
1298 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1301 /* The contact handle might not be known yet */
1302 request_data = new_dispatcher_request_data (dispatcher, connection,
1303 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1304 contact, callback, user_data);
1305 connection_data->outstanding_requests = g_list_prepend
1306 (connection_data->outstanding_requests, request_data);
1308 empathy_contact_call_when_ready (contact,
1309 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1310 request_data, NULL, G_OBJECT (dispatcher));
1312 g_object_unref (dispatcher);