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 EmpathyDispatcherTube public;
76 EmpathyContactFactory *factory;
84 EmpathyDispatcher *dispatcher;
85 EmpathyDispatchOperation *operation;
86 TpConnection *connection;
90 EmpathyContact *contact;
92 /* Properties to pass to the channel when requesting it */
94 EmpathyDispatcherRequestCb *cb;
96 gpointer *request_data;
97 } DispatcherRequestData;
101 /* Channel type specific wrapper object */
102 GObject *channel_wrapper;
107 /* ObjectPath => DispatchData.. */
108 GHashTable *dispatched_channels;
109 /* ObjectPath -> EmpathyDispatchOperations */
110 GHashTable *dispatching_channels;
111 /* ObjectPath -> EmpathyDispatchOperations */
112 GHashTable *outstanding_channels;
113 /* List of DispatcherRequestData */
114 GList *outstanding_requests;
117 static DispatchData *
118 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
120 DispatchData *d = g_slice_new0 (DispatchData);
121 d->channel = g_object_ref (channel);
122 d->channel_wrapper = g_object_ref (channel_wrapper);
128 free_dispatch_data (DispatchData *data)
130 g_object_unref (data->channel);
131 g_object_unref (data->channel_wrapper);
133 g_slice_free (DispatchData, data);
137 static DispatcherRequestData *
138 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
139 TpConnection *connection, const gchar *channel_type, guint handle_type,
140 guint handle, GHashTable *request,
141 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
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,
210 guint domain, gint code, gchar *message,
211 EmpathyDispatcher *dispatcher)
213 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
216 DEBUG ("Error: %s", message);
217 cd = g_hash_table_lookup (priv->connections, connection);
219 g_hash_table_remove (priv->accounts, cd->account);
220 g_hash_table_remove (priv->connections, connection);
224 dispatcher_operation_can_start (EmpathyDispatcher *self,
225 EmpathyDispatchOperation *operation, ConnectionData *cd)
228 const gchar *channel_type =
229 empathy_dispatch_operation_get_channel_type (operation);
231 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
233 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
235 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
245 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
246 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
250 l = cd->outstanding_requests;
253 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
258 if (d->operation == operation)
263 d->cb (NULL, error, d->user_data);
265 d->cb (operation, NULL, d->user_data);
268 cd->outstanding_requests = g_list_delete_link
269 (cd->outstanding_requests, lt);
271 free_dispatcher_request_data (d);
277 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
278 gchar *message, EmpathyDispatcher *dispatcher)
280 /* Channel went away... */
281 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
282 TpConnection *connection;
283 EmpathyDispatchOperation *operation;
285 const gchar *object_path;
287 g_object_get (G_OBJECT (proxy), "connection", &connection, NULL);
289 cd = g_hash_table_lookup (priv->connections, connection);
290 /* Connection itself invalidated? */
294 object_path = tp_proxy_get_object_path (proxy);
296 DEBUG ("Channel %s invalidated", object_path);
298 g_hash_table_remove (cd->dispatched_channels, object_path);
299 g_hash_table_remove (cd->dispatching_channels, object_path);
301 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
302 if (operation != NULL)
304 GError error = { domain, code, message };
305 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
306 g_hash_table_remove (cd->outstanding_channels, object_path);
307 g_object_unref (operation);
312 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
313 EmpathyDispatcher *dispatcher)
315 g_assert (empathy_dispatch_operation_is_incoming (operation));
316 DEBUG ("Send of for dispatching: %s",
317 empathy_dispatch_operation_get_object_path (operation));
318 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
322 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
323 EmpathyDispatcher *dispatcher)
325 /* Our job is done, remove the dispatch operation and mark the channel as
327 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
328 TpConnection *connection;
330 const gchar *object_path;
332 connection = empathy_dispatch_operation_get_tp_connection (operation);
333 cd = g_hash_table_lookup (priv->connections, connection);
334 g_assert (cd != NULL);
335 g_object_unref (G_OBJECT (connection));
337 object_path = empathy_dispatch_operation_get_object_path (operation);
339 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
342 d = new_dispatch_data (
343 empathy_dispatch_operation_get_channel (operation),
344 empathy_dispatch_operation_get_channel_wrapper (operation));
345 g_hash_table_insert (cd->dispatched_channels,
346 g_strdup (object_path), d);
348 g_hash_table_remove (cd->dispatching_channels, object_path);
350 DEBUG ("Channel claimed: %s", object_path);
354 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
355 EmpathyDispatcher *dispatcher)
357 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
358 TpConnection *connection;
360 EmpathyDispatchOperationState status;
362 g_signal_connect (operation, "approved",
363 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
365 g_signal_connect (operation, "claimed",
366 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
368 /* Signal the observers */
369 DEBUG ("Send to observers: %s",
370 empathy_dispatch_operation_get_object_path (operation));
371 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
373 empathy_dispatch_operation_start (operation);
375 /* Signal potential requestors */
376 connection = empathy_dispatch_operation_get_tp_connection (operation);
377 cd = g_hash_table_lookup (priv->connections, connection);
378 g_assert (cd != NULL);
379 g_object_unref (G_OBJECT (connection));
381 g_object_ref (operation);
383 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
384 status = empathy_dispatch_operation_get_status (operation);
385 g_object_unref (operation);
387 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
390 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
392 DEBUG ("Send to approvers: %s",
393 empathy_dispatch_operation_get_object_path (operation));
394 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
398 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
399 DEBUG ("Send of for dispatching: %s",
400 empathy_dispatch_operation_get_object_path (operation));
401 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
407 dispatcher_start_dispatching (EmpathyDispatcher *self,
408 EmpathyDispatchOperation *operation, ConnectionData *cd)
410 const gchar *object_path =
411 empathy_dispatch_operation_get_object_path (operation);
413 DEBUG ("Dispatching process started for %s", object_path);
415 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
417 g_assert (g_hash_table_lookup (cd->outstanding_channels,
418 object_path) == NULL);
420 g_hash_table_insert (cd->dispatching_channels,
421 g_strdup (object_path), operation);
423 switch (empathy_dispatch_operation_get_status (operation))
425 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
426 g_signal_connect (operation, "ready",
427 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
429 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
430 dispatch_operation_ready_cb (operation, dispatcher);
433 g_assert_not_reached();
437 else if (empathy_dispatch_operation_get_status (operation) >=
438 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
440 /* Already dispatching and the operation is pending, thus the observers
441 * have seen it (if applicable), so we can flush the request right away.
443 dispatch_operation_flush_requests (self, operation, NULL, cd);
449 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
455 g_hash_table_iter_init (&iter, cd->outstanding_channels);
456 while (g_hash_table_iter_next (&iter, NULL, &value))
458 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
460 if (dispatcher_operation_can_start (self, operation, cd))
462 dispatcher_start_dispatching (dispatcher, operation, cd);
463 g_hash_table_iter_remove (&iter);
470 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
471 TpConnection *connection,
472 const gchar *object_path, const gchar *channel_type,
473 guint handle_type, guint handle, GHashTable *properties,
476 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
479 EmpathyDispatchOperation *operation;
480 EmpathyContact *contact = NULL;
482 /* Channel types we never want to dispatch because they're either deprecated
483 * or can't sensibly be dispatch (e.g. channels that should always be
485 const char *blacklist[] = {
486 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
487 TP_IFACE_CHANNEL_TYPE_TUBES,
488 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
492 cd = g_hash_table_lookup (priv->connections, connection);
494 /* Don't bother with channels we have already dispatched or are dispatching
495 * currently. This can happen when NewChannel(s) is fired after
496 * RequestChannel/CreateChannel/EnsureChannel */
497 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
500 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
503 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
505 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
508 /* Only pick up non-requested text channels. For all other it doesn't make
509 * sense to handle it if we didn't request it. The same goes for channels we
510 * discovered by the Channels property or ListChannels */
511 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
513 DEBUG ("Ignoring incoming channel of type %s on %s",
514 channel_type, object_path);
518 for (i = 0 ; blacklist[i] != NULL; i++)
520 if (!tp_strdiff (channel_type, blacklist[i]))
522 DEBUG ("Ignoring blacklisted channel type %s on %s",
523 channel_type, object_path);
528 DEBUG ("New channel of type %s on %s", channel_type, object_path);
530 if (properties == NULL)
531 channel = tp_channel_new (connection, object_path, channel_type,
532 handle_type, handle, NULL);
534 channel = tp_channel_new_from_properties (connection, object_path,
537 g_signal_connect (channel, "invalidated",
538 G_CALLBACK (dispatcher_channel_invalidated_cb),
541 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
543 EmpathyContactFactory *factory = empathy_contact_factory_new ();
544 contact = empathy_contact_factory_get_from_handle (factory,
545 cd->account, handle);
546 g_object_unref (factory);
549 operation = empathy_dispatch_operation_new (connection, channel, contact,
552 g_object_unref (channel);
556 /* Request could either be by us or by a remote party. If there are no
557 * outstanding requests for this channel type we can assume it's remote.
558 * Otherwise we wait untill they are all satisfied */
559 if (dispatcher_operation_can_start (dispatcher, operation, cd))
560 dispatcher_start_dispatching (dispatcher, operation, cd);
562 g_hash_table_insert (cd->outstanding_channels,
563 g_strdup (object_path), operation);
567 dispatcher_start_dispatching (dispatcher, operation, cd);
572 dispatcher_connection_new_channel_cb (TpConnection *connection,
573 const gchar *object_path, const gchar *channel_type,
574 guint handle_type, guint handle,
575 gboolean suppress_handler, gpointer user_data,
578 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
580 /* Empathy heavily abuses surpress handler (don't try this at home), if
581 * surpress handler is true then it is an outgoing channel, which is
582 * requested either by us or some other party (like the megaphone applet).
583 * Otherwise it's an incoming channel */
584 dispatcher_connection_new_channel (dispatcher, connection,
585 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
589 dispatcher_connection_new_channel_with_properties (
590 EmpathyDispatcher *dispatcher, TpConnection *connection,
591 const gchar *object_path, GHashTable *properties)
593 const gchar *channel_type;
600 channel_type = tp_asv_get_string (properties,
601 TP_IFACE_CHANNEL ".ChannelType");
602 if (channel_type == NULL)
604 g_message ("%s had an invalid ChannelType property", object_path);
608 handle_type = tp_asv_get_uint32 (properties,
609 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
612 g_message ("%s had an invalid TargetHandleType property", object_path);
616 handle = tp_asv_get_uint32 (properties,
617 TP_IFACE_CHANNEL ".TargetHandle", &valid);
620 g_message ("%s had an invalid TargetHandle property", object_path);
624 /* We assume there is no channel dispather, so we're the only one dispatching
625 * it. Which means that a requested channel it is outgoing one */
626 requested = tp_asv_get_boolean (properties,
627 TP_IFACE_CHANNEL ".Requested", &valid);
630 g_message ("%s had an invalid Requested property", object_path);
634 dispatcher_connection_new_channel (dispatcher, connection,
635 object_path, channel_type, handle_type, handle, properties, !requested);
640 dispatcher_connection_new_channels_cb (
641 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
644 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
647 for (i = 0; i < channels->len ; i++)
649 GValueArray *arr = g_ptr_array_index (channels, i);
650 const gchar *object_path;
651 GHashTable *properties;
653 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
654 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
656 dispatcher_connection_new_channel_with_properties (dispatcher,
657 connection, object_path, properties);
662 dispatcher_connection_got_channels_property (TpProxy *proxy,
663 const GValue *channels_prop, const GError *error, gpointer user_data,
669 DEBUG ("Error: %s", error->message);
673 channels = g_value_get_boxed (channels_prop);
674 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
675 channels, NULL, object);
679 dispatcher_connection_list_channels_cb (TpConnection *connection,
680 const GPtrArray *channels,
688 DEBUG ("Error: %s", error->message);
692 for (i = 0; i < channels->len; i++) {
695 values = g_ptr_array_index (channels, i);
696 /* We don't have any extra info, so assume already existing channels are
698 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
700 g_value_get_boxed (g_value_array_get_nth (values, 0)),
701 g_value_get_string (g_value_array_get_nth (values, 1)),
702 g_value_get_uint (g_value_array_get_nth (values, 2)),
703 g_value_get_uint (g_value_array_get_nth (values, 3)),
709 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
710 const GPtrArray *capabilities,
716 DEBUG ("Error: %s", error->message);
721 dispatcher_connection_ready_cb (TpConnection *connection,
722 const GError *error, gpointer dispatcher)
724 GPtrArray *capabilities;
727 const gchar *remove = NULL;
731 dispatcher_connection_invalidated_cb (connection, error->domain,
732 error->code, error->message, dispatcher);
736 g_signal_connect (connection, "invalidated",
737 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
740 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
741 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
743 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
744 dispatcher_connection_new_channels_cb,
745 NULL, NULL, G_OBJECT (dispatcher), NULL);
747 tp_cli_dbus_properties_call_get (connection, -1,
748 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
749 dispatcher_connection_got_channels_property,
750 NULL, NULL, dispatcher);
754 tp_cli_connection_connect_to_new_channel (connection,
755 dispatcher_connection_new_channel_cb,
756 NULL, NULL, G_OBJECT (dispatcher), NULL);
758 tp_cli_connection_call_list_channels (connection, -1,
759 dispatcher_connection_list_channels_cb, NULL, NULL,
760 G_OBJECT (dispatcher));
764 /* Advertise VoIP capabilities */
765 capabilities = g_ptr_array_sized_new (1);
766 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
767 G_TYPE_UINT, G_TYPE_INVALID);
768 g_value_init (&cap, cap_type);
769 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
770 dbus_g_type_struct_set (&cap,
771 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
772 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
773 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
774 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
775 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
776 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
778 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
779 connection, -1, capabilities, &remove,
780 dispatcher_connection_advertise_capabilities_cb,
781 NULL, NULL, G_OBJECT (dispatcher));
785 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
787 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
788 TpConnection *connection;
790 connection = g_hash_table_lookup (priv->accounts, account);
791 if (connection != NULL)
794 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
795 if (connection == NULL)
798 g_hash_table_insert (priv->connections, g_object_ref (connection),
799 new_connection_data (account));
801 g_hash_table_insert (priv->accounts, g_object_ref (account),
802 g_object_ref (connection));
804 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
809 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
810 McAccount *account, TpConnectionStatusReason reason,
811 TpConnectionStatus status, TpConnectionStatus previous,
812 EmpathyDispatcher *dispatcher)
814 dispatcher_update_account (dispatcher, account);
819 dispatcher_constructor (GType type, guint n_construct_params,
820 GObjectConstructParam *construct_params)
824 if (dispatcher == NULL)
826 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
827 (type, n_construct_params, construct_params);
829 dispatcher = EMPATHY_DISPATCHER (retval);
830 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
834 retval = g_object_ref (dispatcher);
841 dispatcher_finalize (GObject *object)
843 EmpathyDispatcherPriv *priv = GET_PRIV (object);
845 g_signal_handlers_disconnect_by_func (priv->account_manager,
846 dispatcher_account_connection_cb, object);
848 g_object_unref (priv->account_manager);
849 g_object_unref (priv->mc);
851 g_hash_table_destroy (priv->accounts);
852 g_hash_table_destroy (priv->connections);
856 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
858 GObjectClass *object_class = G_OBJECT_CLASS (klass);
860 object_class->finalize = dispatcher_finalize;
861 object_class->constructor = dispatcher_constructor;
864 g_signal_new ("observe",
865 G_TYPE_FROM_CLASS (klass),
869 g_cclosure_marshal_VOID__OBJECT,
871 1, EMPATHY_TYPE_DISPATCH_OPERATION);
874 g_signal_new ("approve",
875 G_TYPE_FROM_CLASS (klass),
879 g_cclosure_marshal_VOID__OBJECT,
881 1, EMPATHY_TYPE_DISPATCH_OPERATION);
884 g_signal_new ("dispatch",
885 G_TYPE_FROM_CLASS (klass),
889 g_cclosure_marshal_VOID__OBJECT,
891 1, EMPATHY_TYPE_DISPATCH_OPERATION);
893 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
898 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
901 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
902 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
904 dispatcher->priv = priv;
905 priv->mc = empathy_mission_control_new ();
906 priv->account_manager = empathy_account_manager_dup_singleton ();
908 g_signal_connect (priv->account_manager,
909 "account-connection-changed",
910 G_CALLBACK (dispatcher_account_connection_cb),
913 priv->accounts = g_hash_table_new_full (empathy_account_hash,
914 empathy_account_equal, g_object_unref, g_object_unref);
916 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
917 g_object_unref, (GDestroyNotify) free_connection_data);
919 accounts = mc_accounts_list_by_enabled (TRUE);
921 for (l = accounts; l; l = l->next) {
922 dispatcher_update_account (dispatcher, l->data);
923 g_object_unref (l->data);
925 g_list_free (accounts);
929 empathy_dispatcher_dup_singleton (void)
931 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
935 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
936 DispatcherRequestData *request_data, const GError *error)
938 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
939 ConnectionData *conn_data;
941 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
942 if (request_data->cb != NULL)
943 request_data->cb (NULL, error, request_data->user_data);
945 conn_data->outstanding_requests =
946 g_list_remove (conn_data->outstanding_requests, request_data);
947 free_dispatcher_request_data (request_data);
951 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
952 DispatcherRequestData *request_data, const gchar *object_path,
953 GHashTable *properties, const GError *error)
955 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
956 EmpathyDispatchOperation *operation = NULL;
957 ConnectionData *conn_data;
959 conn_data = g_hash_table_lookup (priv->connections,
960 request_data->connection);
964 DEBUG ("Channel request failed: %s", error->message);
966 dispatcher_request_failed (dispatcher, request_data, error);
971 operation = g_hash_table_lookup (conn_data->outstanding_channels,
974 if (operation != NULL)
975 g_hash_table_remove (conn_data->outstanding_channels, object_path);
977 operation = g_hash_table_lookup (conn_data->dispatching_channels,
980 if (operation == NULL)
982 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
987 operation = empathy_dispatch_operation_new_with_wrapper (
988 request_data->connection,
989 data->channel, request_data->contact, FALSE,
990 data->channel_wrapper);
996 if (properties != NULL)
997 channel = tp_channel_new_from_properties (request_data->connection,
998 object_path, properties, NULL);
1000 channel = tp_channel_new (request_data->connection, object_path,
1001 request_data->channel_type, request_data->handle_type,
1002 request_data->handle, NULL);
1004 g_signal_connect (channel, "invalidated",
1005 G_CALLBACK (dispatcher_channel_invalidated_cb),
1006 request_data->dispatcher);
1008 operation = empathy_dispatch_operation_new (request_data->connection,
1009 channel, request_data->contact, FALSE);
1010 g_object_unref (channel);
1015 /* Already existed set potential extra information */
1016 g_object_set (G_OBJECT (operation),
1017 "contact", request_data->contact,
1021 request_data->operation = operation;
1023 /* (pre)-approve this right away as we requested it */
1024 empathy_dispatch_operation_approve (operation);
1026 dispatcher_start_dispatching (request_data->dispatcher, operation,
1029 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1034 dispatcher_request_channel_cb (TpConnection *connection,
1035 const gchar *object_path, const GError *error,
1036 gpointer user_data, GObject *weak_object)
1038 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1039 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1041 dispatcher_connection_new_requested_channel (dispatcher,
1042 request_data, object_path, NULL, error);
1046 dispatcher_request_channel (DispatcherRequestData *request_data)
1048 tp_cli_connection_call_request_channel (request_data->connection, -1,
1049 request_data->channel_type,
1050 request_data->handle_type,
1051 request_data->handle,
1052 TRUE, dispatcher_request_channel_cb,
1053 request_data, NULL, G_OBJECT (request_data->dispatcher));
1057 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1058 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1060 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1061 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1063 TpConnection *connection;
1065 DispatcherRequestData *request_data;
1067 account = empathy_contact_get_account (contact);
1068 connection = g_hash_table_lookup (priv->accounts, account);
1070 g_assert (connection != NULL);
1071 cd = g_hash_table_lookup (priv->connections, connection);
1072 request_data = new_dispatcher_request_data (dispatcher, connection,
1073 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1074 contact, callback, user_data);
1076 cd->outstanding_requests = g_list_prepend
1077 (cd->outstanding_requests, request_data);
1079 dispatcher_request_channel (request_data);
1081 g_object_unref (dispatcher);
1086 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1087 const GError *error, gpointer user_data, GObject *object)
1089 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1091 request_data->handle = empathy_contact_get_handle (contact);
1093 dispatcher_request_channel (request_data);
1097 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1098 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1100 EmpathyDispatcher *dispatcher;
1101 EmpathyDispatcherPriv *priv;
1103 TpConnection *connection;
1104 ConnectionData *connection_data;
1105 DispatcherRequestData *request_data;
1107 dispatcher = empathy_dispatcher_dup_singleton();
1108 priv = GET_PRIV (dispatcher);
1110 account = empathy_contact_get_account (contact);
1111 connection = g_hash_table_lookup (priv->accounts, account);
1112 connection_data = g_hash_table_lookup (priv->connections, connection);
1114 /* The contact handle might not be known yet */
1115 request_data = new_dispatcher_request_data (dispatcher, connection,
1116 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1117 contact, callback, user_data);
1119 connection_data->outstanding_requests = g_list_prepend
1120 (connection_data->outstanding_requests, request_data);
1122 empathy_contact_call_when_ready (contact,
1123 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1124 request_data, NULL, G_OBJECT (dispatcher));
1126 g_object_unref (dispatcher);
1130 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1131 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1133 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1134 EmpathyContactFactory *factory;
1135 EmpathyContact *contact;
1137 factory = empathy_contact_factory_dup_singleton ();
1138 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1140 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1142 g_object_unref (contact);
1143 g_object_unref (factory);
1144 g_object_unref (dispatcher);
1149 dispatcher_request_handles_cb (TpConnection *connection,
1150 const GArray *handles, const GError *error, gpointer user_data,
1153 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1157 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1158 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1161 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1163 if (request_data->cb)
1164 request_data->cb (NULL, error, request_data->user_data);
1166 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1169 free_dispatcher_request_data (request_data);
1171 dispatcher_flush_outstanding_operations (dispatcher, cd);
1175 request_data->handle = g_array_index (handles, guint, 0);
1176 dispatcher_request_channel (request_data);
1180 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1181 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1183 EmpathyDispatcher *dispatcher;
1184 EmpathyDispatcherPriv *priv;
1185 DispatcherRequestData *request_data;
1186 TpConnection *connection;
1187 ConnectionData *connection_data;
1188 const gchar *names[] = { roomname, NULL };
1190 dispatcher = empathy_dispatcher_dup_singleton();
1191 priv = GET_PRIV (dispatcher);
1193 connection = g_hash_table_lookup (priv->accounts, account);
1194 connection_data = g_hash_table_lookup (priv->connections, connection);
1197 /* Don't know the room handle yet */
1198 request_data = new_dispatcher_request_data (dispatcher, connection,
1199 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1200 NULL, callback, user_data);
1202 connection_data->outstanding_requests = g_list_prepend
1203 (connection_data->outstanding_requests, request_data);
1205 tp_cli_connection_call_request_handles (connection, -1,
1206 TP_HANDLE_TYPE_ROOM, names,
1207 dispatcher_request_handles_cb, request_data, NULL,
1208 G_OBJECT (dispatcher));
1210 g_object_unref (dispatcher);
1214 dispatcher_create_channel_cb (TpConnection *connect,
1215 const gchar *object_path, GHashTable *properties, const GError *error,
1216 gpointer user_data, GObject *weak_object)
1218 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1219 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1221 dispatcher_connection_new_requested_channel (dispatcher,
1222 request_data, object_path, properties, error);
1227 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1228 const GError *error, gpointer user_data, GObject *object)
1230 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1231 GValue *target_handle;
1233 g_assert (request_data->request);
1237 dispatcher_request_failed (request_data->dispatcher,
1238 request_data, error);
1242 request_data->handle = empathy_contact_get_handle (contact);
1244 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1245 g_value_set_uint (target_handle, request_data->handle);
1246 g_hash_table_insert (request_data->request,
1247 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1249 tp_cli_connection_interface_requests_call_create_channel (
1250 request_data->connection, -1,
1251 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1252 G_OBJECT (request_data->dispatcher));
1256 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1257 const gchar *filename, guint64 size, guint64 date,
1258 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1261 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1262 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1263 McAccount *account = empathy_contact_get_account (contact);
1264 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1265 ConnectionData *connection_data =
1266 g_hash_table_lookup (priv->connections, connection);
1267 DispatcherRequestData *request_data;
1269 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1270 (GDestroyNotify) tp_g_value_slice_free);
1272 /* org.freedesktop.Telepathy.Channel.ChannelType */
1273 value = tp_g_value_slice_new (G_TYPE_STRING);
1274 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1275 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1277 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1278 value = tp_g_value_slice_new (G_TYPE_UINT);
1279 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1280 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1282 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1283 value = tp_g_value_slice_new (G_TYPE_STRING);
1284 g_value_set_string (value, content_type);
1285 g_hash_table_insert (request,
1286 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1288 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1289 value = tp_g_value_slice_new (G_TYPE_STRING);
1290 g_value_set_string (value, filename);
1291 g_hash_table_insert (request,
1292 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1294 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1295 value = tp_g_value_slice_new (G_TYPE_UINT64);
1296 g_value_set_uint64 (value, size);
1297 g_hash_table_insert (request,
1298 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1300 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1301 value = tp_g_value_slice_new (G_TYPE_UINT64);
1302 g_value_set_uint64 (value, date);
1303 g_hash_table_insert (request,
1304 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1307 /* The contact handle might not be known yet */
1308 request_data = new_dispatcher_request_data (dispatcher, connection,
1309 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1310 contact, callback, user_data);
1311 connection_data->outstanding_requests = g_list_prepend
1312 (connection_data->outstanding_requests, request_data);
1314 empathy_contact_call_when_ready (contact,
1315 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1316 request_data, NULL, G_OBJECT (dispatcher));
1318 g_object_unref (dispatcher);