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 */
61 /* channels which the dispatcher is listening "invalidated" */
63 } EmpathyDispatcherPriv;
65 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
75 static guint signals[LAST_SIGNAL];
76 static EmpathyDispatcher *dispatcher = NULL;
80 EmpathyDispatcher *dispatcher;
81 EmpathyDispatchOperation *operation;
82 TpConnection *connection;
86 EmpathyContact *contact;
88 /* Properties to pass to the channel when requesting it */
90 EmpathyDispatcherRequestCb *cb;
92 gpointer *request_data;
93 } DispatcherRequestData;
98 /* Channel type specific wrapper object */
99 GObject *channel_wrapper;
105 /* ObjectPath => DispatchData.. */
106 GHashTable *dispatched_channels;
107 /* ObjectPath -> EmpathyDispatchOperations */
108 GHashTable *dispatching_channels;
109 /* ObjectPath -> EmpathyDispatchOperations */
110 GHashTable *outstanding_channels;
111 /* List of DispatcherRequestData */
112 GList *outstanding_requests;
115 static DispatchData *
116 new_dispatch_data (TpChannel *channel,
117 GObject *channel_wrapper)
119 DispatchData *d = g_slice_new0 (DispatchData);
120 d->channel = g_object_ref (channel);
121 d->channel_wrapper = g_object_ref (channel_wrapper);
127 free_dispatch_data (DispatchData *data)
129 g_object_unref (data->channel);
130 g_object_unref (data->channel_wrapper);
132 g_slice_free (DispatchData, data);
135 static DispatcherRequestData *
136 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
137 TpConnection *connection,
138 const gchar *channel_type,
142 EmpathyContact *contact,
143 EmpathyDispatcherRequestCb *cb,
146 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
148 result->dispatcher = g_object_ref (dispatcher);
149 result->connection = connection;
151 result->channel_type = g_strdup (channel_type);
152 result->handle_type = handle_type;
153 result->handle = handle;
154 result->request = request;
157 result->contact = g_object_ref (contact);
160 result->user_data = user_data;
166 free_dispatcher_request_data (DispatcherRequestData *r)
168 g_free (r->channel_type);
170 if (r->dispatcher != NULL)
171 g_object_unref (r->dispatcher);
173 if (r->contact != NULL)
174 g_object_unref (r->contact);
176 if (r->request != NULL)
177 g_hash_table_unref (r->request);
179 g_slice_free (DispatcherRequestData, r);
182 static ConnectionData *
183 new_connection_data (McAccount *account)
185 ConnectionData *cd = g_slice_new0 (ConnectionData);
186 cd->account = g_object_ref (account);
188 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
189 g_free, (GDestroyNotify) free_dispatch_data);
191 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
192 g_free, g_object_unref);
194 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
201 free_connection_data (ConnectionData *cd)
204 g_object_unref (cd->account);
205 g_hash_table_destroy (cd->dispatched_channels);
206 g_hash_table_destroy (cd->dispatching_channels);
208 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
210 free_dispatcher_request_data (l->data);
215 dispatcher_connection_invalidated_cb (TpConnection *connection,
219 EmpathyDispatcher *dispatcher)
221 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
224 DEBUG ("Error: %s", message);
225 cd = g_hash_table_lookup (priv->connections, connection);
227 g_hash_table_remove (priv->accounts, cd->account);
228 g_hash_table_remove (priv->connections, connection);
232 dispatcher_operation_can_start (EmpathyDispatcher *self,
233 EmpathyDispatchOperation *operation,
237 const gchar *channel_type =
238 empathy_dispatch_operation_get_channel_type (operation);
240 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
242 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
244 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
254 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
255 EmpathyDispatchOperation *operation,
261 l = cd->outstanding_requests;
264 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
269 if (d->operation == operation)
274 d->cb (NULL, error, d->user_data);
276 d->cb (operation, NULL, d->user_data);
279 cd->outstanding_requests = g_list_delete_link
280 (cd->outstanding_requests, lt);
282 free_dispatcher_request_data (d);
288 dispatcher_channel_invalidated_cb (TpProxy *proxy,
292 EmpathyDispatcher *dispatcher)
294 /* Channel went away... */
295 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
296 TpConnection *connection;
297 EmpathyDispatchOperation *operation;
299 const gchar *object_path;
301 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
303 cd = g_hash_table_lookup (priv->connections, connection);
304 /* Connection itself invalidated? */
308 object_path = tp_proxy_get_object_path (proxy);
310 DEBUG ("Channel %s invalidated", object_path);
312 g_hash_table_remove (cd->dispatched_channels, object_path);
313 g_hash_table_remove (cd->dispatching_channels, object_path);
315 priv->channels = g_list_remove (priv->channels, proxy);
317 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
318 if (operation != NULL)
320 GError error = { domain, code, message };
321 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
322 g_hash_table_remove (cd->outstanding_channels, object_path);
323 g_object_unref (operation);
328 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
329 EmpathyDispatcher *dispatcher)
331 g_assert (empathy_dispatch_operation_is_incoming (operation));
332 DEBUG ("Send of for dispatching: %s",
333 empathy_dispatch_operation_get_object_path (operation));
334 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
338 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
339 EmpathyDispatcher *dispatcher)
341 /* Our job is done, remove the dispatch operation and mark the channel as
343 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
344 TpConnection *connection;
346 const gchar *object_path;
348 connection = empathy_dispatch_operation_get_tp_connection (operation);
349 cd = g_hash_table_lookup (priv->connections, connection);
350 g_assert (cd != NULL);
351 g_object_unref (G_OBJECT (connection));
353 object_path = empathy_dispatch_operation_get_object_path (operation);
355 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
358 d = new_dispatch_data (
359 empathy_dispatch_operation_get_channel (operation),
360 empathy_dispatch_operation_get_channel_wrapper (operation));
361 g_hash_table_insert (cd->dispatched_channels,
362 g_strdup (object_path), d);
364 g_hash_table_remove (cd->dispatching_channels, object_path);
366 DEBUG ("Channel claimed: %s", object_path);
370 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
371 EmpathyDispatcher *dispatcher)
373 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
374 TpConnection *connection;
376 EmpathyDispatchOperationState status;
378 g_signal_connect (operation, "approved",
379 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
381 g_signal_connect (operation, "claimed",
382 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
384 /* Signal the observers */
385 DEBUG ("Send to observers: %s",
386 empathy_dispatch_operation_get_object_path (operation));
387 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
389 empathy_dispatch_operation_start (operation);
391 /* Signal potential requestors */
392 connection = empathy_dispatch_operation_get_tp_connection (operation);
393 cd = g_hash_table_lookup (priv->connections, connection);
394 g_assert (cd != NULL);
395 g_object_unref (G_OBJECT (connection));
397 g_object_ref (operation);
398 g_object_ref (dispatcher);
400 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
401 status = empathy_dispatch_operation_get_status (operation);
402 g_object_unref (operation);
404 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
407 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
409 DEBUG ("Send to approvers: %s",
410 empathy_dispatch_operation_get_object_path (operation));
411 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
415 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
416 DEBUG ("Send of for dispatching: %s",
417 empathy_dispatch_operation_get_object_path (operation));
418 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
421 g_object_unref (dispatcher);
425 dispatcher_start_dispatching (EmpathyDispatcher *self,
426 EmpathyDispatchOperation *operation,
429 const gchar *object_path =
430 empathy_dispatch_operation_get_object_path (operation);
432 DEBUG ("Dispatching process started for %s", object_path);
434 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
436 g_assert (g_hash_table_lookup (cd->outstanding_channels,
437 object_path) == NULL);
439 g_hash_table_insert (cd->dispatching_channels,
440 g_strdup (object_path), operation);
442 switch (empathy_dispatch_operation_get_status (operation))
444 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
445 g_signal_connect (operation, "ready",
446 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
448 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
449 dispatch_operation_ready_cb (operation, dispatcher);
452 g_assert_not_reached();
456 else if (empathy_dispatch_operation_get_status (operation) >=
457 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
459 /* Already dispatching and the operation is pending, thus the observers
460 * have seen it (if applicable), so we can flush the request right away.
462 dispatch_operation_flush_requests (self, operation, NULL, cd);
467 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
473 g_hash_table_iter_init (&iter, cd->outstanding_channels);
474 while (g_hash_table_iter_next (&iter, NULL, &value))
476 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
478 if (dispatcher_operation_can_start (self, operation, cd))
480 dispatcher_start_dispatching (dispatcher, operation, cd);
481 g_hash_table_iter_remove (&iter);
487 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
488 TpConnection *connection,
489 const gchar *object_path,
490 const gchar *channel_type,
493 GHashTable *properties,
496 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
499 EmpathyDispatchOperation *operation;
500 EmpathyContact *contact = NULL;
502 /* Channel types we never want to dispatch because they're either deprecated
503 * or can't sensibly be dispatch (e.g. channels that should always be
505 const char *blacklist[] = {
506 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
507 TP_IFACE_CHANNEL_TYPE_TUBES,
508 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
512 cd = g_hash_table_lookup (priv->connections, connection);
514 /* Don't bother with channels we have already dispatched or are dispatching
515 * currently. This can happen when NewChannel(s) is fired after
516 * RequestChannel/CreateChannel/EnsureChannel */
517 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
520 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
523 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
525 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
528 /* Only pick up non-requested text and file channels. For all other it
529 * doesn't make sense to handle it if we didn't request it. The same goes
530 * for channels we discovered by the Channels property or ListChannels */
531 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
532 && tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
534 DEBUG ("Ignoring incoming channel of type %s on %s",
535 channel_type, object_path);
539 for (i = 0 ; blacklist[i] != NULL; i++)
541 if (!tp_strdiff (channel_type, blacklist[i]))
543 DEBUG ("Ignoring blacklisted channel type %s on %s",
544 channel_type, object_path);
549 DEBUG ("New channel of type %s on %s", channel_type, object_path);
551 if (properties == NULL)
552 channel = tp_channel_new (connection, object_path, channel_type,
553 handle_type, handle, NULL);
555 channel = tp_channel_new_from_properties (connection, object_path,
558 g_signal_connect (channel, "invalidated",
559 G_CALLBACK (dispatcher_channel_invalidated_cb),
562 priv->channels = g_list_append (priv->channels, channel);
564 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
566 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
567 contact = empathy_contact_factory_get_from_handle (factory,
568 cd->account, handle);
569 g_object_unref (factory);
572 operation = empathy_dispatch_operation_new (connection, channel, contact,
575 g_object_unref (channel);
579 /* Request could either be by us or by a remote party. If there are no
580 * outstanding requests for this channel type we can assume it's remote.
581 * Otherwise we wait untill they are all satisfied */
582 if (dispatcher_operation_can_start (dispatcher, operation, cd))
583 dispatcher_start_dispatching (dispatcher, operation, cd);
585 g_hash_table_insert (cd->outstanding_channels,
586 g_strdup (object_path), operation);
590 dispatcher_start_dispatching (dispatcher, operation, cd);
595 dispatcher_connection_new_channel_cb (TpConnection *connection,
596 const gchar *object_path,
597 const gchar *channel_type,
600 gboolean suppress_handler,
604 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
606 /* Empathy heavily abuses surpress handler (don't try this at home), if
607 * surpress handler is true then it is an outgoing channel, which is
608 * requested either by us or some other party (like the megaphone applet).
609 * Otherwise it's an incoming channel */
610 dispatcher_connection_new_channel (dispatcher, connection,
611 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
615 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
616 TpConnection *connection,
617 const gchar *object_path,
618 GHashTable *properties)
620 const gchar *channel_type;
627 channel_type = tp_asv_get_string (properties,
628 TP_IFACE_CHANNEL ".ChannelType");
629 if (channel_type == NULL)
631 g_message ("%s had an invalid ChannelType property", object_path);
635 handle_type = tp_asv_get_uint32 (properties,
636 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
639 g_message ("%s had an invalid TargetHandleType property", object_path);
643 handle = tp_asv_get_uint32 (properties,
644 TP_IFACE_CHANNEL ".TargetHandle", &valid);
647 g_message ("%s had an invalid TargetHandle property", object_path);
651 /* We assume there is no channel dispather, so we're the only one dispatching
652 * it. Which means that a requested channel it is outgoing one */
653 requested = tp_asv_get_boolean (properties,
654 TP_IFACE_CHANNEL ".Requested", &valid);
657 g_message ("%s had an invalid Requested property", object_path);
661 dispatcher_connection_new_channel (dispatcher, connection,
662 object_path, channel_type, handle_type, handle, properties, !requested);
666 dispatcher_connection_new_channels_cb (TpConnection *connection,
667 const GPtrArray *channels,
671 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
674 for (i = 0; i < channels->len ; i++)
676 GValueArray *arr = g_ptr_array_index (channels, i);
677 const gchar *object_path;
678 GHashTable *properties;
680 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
681 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
683 dispatcher_connection_new_channel_with_properties (dispatcher,
684 connection, object_path, properties);
689 dispatcher_connection_got_channels_property (TpProxy *proxy,
690 const GValue *channels_prop,
698 DEBUG ("Error: %s", error->message);
702 channels = g_value_get_boxed (channels_prop);
703 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
704 channels, NULL, object);
708 dispatcher_connection_list_channels_cb (TpConnection *connection,
709 const GPtrArray *channels,
718 DEBUG ("Error: %s", error->message);
722 for (i = 0; i < channels->len; i++)
726 values = g_ptr_array_index (channels, i);
727 /* We don't have any extra info, so assume already existing channels are
729 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
731 g_value_get_boxed (g_value_array_get_nth (values, 0)),
732 g_value_get_string (g_value_array_get_nth (values, 1)),
733 g_value_get_uint (g_value_array_get_nth (values, 2)),
734 g_value_get_uint (g_value_array_get_nth (values, 3)),
740 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
741 const GPtrArray *capabilities,
747 DEBUG ("Error: %s", error->message);
751 dispatcher_connection_ready_cb (TpConnection *connection,
755 GPtrArray *capabilities;
758 const gchar *remove = NULL;
762 dispatcher_connection_invalidated_cb (connection, error->domain,
763 error->code, error->message, dispatcher);
767 g_signal_connect (connection, "invalidated",
768 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
770 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
771 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
773 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
774 dispatcher_connection_new_channels_cb,
775 NULL, NULL, G_OBJECT (dispatcher), NULL);
777 tp_cli_dbus_properties_call_get (connection, -1,
778 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
779 dispatcher_connection_got_channels_property,
780 NULL, NULL, dispatcher);
784 tp_cli_connection_connect_to_new_channel (connection,
785 dispatcher_connection_new_channel_cb,
786 NULL, NULL, G_OBJECT (dispatcher), NULL);
788 tp_cli_connection_call_list_channels (connection, -1,
789 dispatcher_connection_list_channels_cb, NULL, NULL,
790 G_OBJECT (dispatcher));
794 /* Advertise VoIP capabilities */
795 /* FIXME: Capabilities is leaked */
796 capabilities = g_ptr_array_sized_new (1);
797 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
798 G_TYPE_UINT, G_TYPE_INVALID);
799 g_value_init (&cap, cap_type);
800 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
801 dbus_g_type_struct_set (&cap,
802 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
803 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
804 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
805 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
806 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
807 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
809 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
810 connection, -1, capabilities, &remove,
811 dispatcher_connection_advertise_capabilities_cb,
812 NULL, NULL, G_OBJECT (dispatcher));
816 dispatcher_update_account (EmpathyDispatcher *dispatcher,
819 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
820 TpConnection *connection;
822 connection = g_hash_table_lookup (priv->accounts, account);
823 if (connection != NULL)
826 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
827 if (connection == NULL)
830 g_hash_table_insert (priv->connections, g_object_ref (connection),
831 new_connection_data (account));
833 g_hash_table_insert (priv->accounts, g_object_ref (account),
834 g_object_ref (connection));
836 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
839 g_object_unref (connection);
843 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
845 TpConnectionStatusReason reason,
846 TpConnectionStatus status,
847 TpConnectionStatus previous,
848 EmpathyDispatcher *dispatcher)
850 dispatcher_update_account (dispatcher, account);
854 dispatcher_constructor (GType type,
855 guint n_construct_params,
856 GObjectConstructParam *construct_params)
860 if (dispatcher == NULL)
862 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
863 (type, n_construct_params, construct_params);
865 dispatcher = EMPATHY_DISPATCHER (retval);
866 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
870 retval = g_object_ref (dispatcher);
877 dispatcher_finalize (GObject *object)
879 EmpathyDispatcherPriv *priv = GET_PRIV (object);
884 g_signal_handlers_disconnect_by_func (priv->account_manager,
885 dispatcher_account_connection_cb, object);
887 for (l = priv->channels; l; l = l->next)
889 g_signal_handlers_disconnect_by_func (l->data,
890 dispatcher_channel_invalidated_cb, object);
893 g_list_free (priv->channels);
895 g_hash_table_iter_init (&iter, priv->connections);
896 while (g_hash_table_iter_next (&iter, &connection, NULL))
898 g_signal_handlers_disconnect_by_func (connection,
899 dispatcher_connection_invalidated_cb, object);
902 g_object_unref (priv->account_manager);
903 g_object_unref (priv->mc);
905 g_hash_table_destroy (priv->accounts);
906 g_hash_table_destroy (priv->connections);
910 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
912 GObjectClass *object_class = G_OBJECT_CLASS (klass);
914 object_class->finalize = dispatcher_finalize;
915 object_class->constructor = dispatcher_constructor;
918 g_signal_new ("observe",
919 G_TYPE_FROM_CLASS (klass),
923 g_cclosure_marshal_VOID__OBJECT,
925 1, EMPATHY_TYPE_DISPATCH_OPERATION);
928 g_signal_new ("approve",
929 G_TYPE_FROM_CLASS (klass),
933 g_cclosure_marshal_VOID__OBJECT,
935 1, EMPATHY_TYPE_DISPATCH_OPERATION);
938 g_signal_new ("dispatch",
939 G_TYPE_FROM_CLASS (klass),
943 g_cclosure_marshal_VOID__OBJECT,
945 1, EMPATHY_TYPE_DISPATCH_OPERATION);
947 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
952 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
955 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
956 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
958 dispatcher->priv = priv;
959 priv->mc = empathy_mission_control_dup_singleton ();
960 priv->account_manager = empathy_account_manager_dup_singleton ();
962 g_signal_connect (priv->account_manager,
963 "account-connection-changed",
964 G_CALLBACK (dispatcher_account_connection_cb),
967 priv->accounts = g_hash_table_new_full (empathy_account_hash,
968 empathy_account_equal, g_object_unref, g_object_unref);
970 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
971 g_object_unref, (GDestroyNotify) free_connection_data);
973 priv->channels = NULL;
975 accounts = mc_accounts_list_by_enabled (TRUE);
977 for (l = accounts; l; l = l->next)
979 dispatcher_update_account (dispatcher, l->data);
980 g_object_unref (l->data);
982 g_list_free (accounts);
986 empathy_dispatcher_dup_singleton (void)
988 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
992 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
993 DispatcherRequestData *request_data,
996 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
997 ConnectionData *conn_data;
999 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1000 if (request_data->cb != NULL)
1001 request_data->cb (NULL, error, request_data->user_data);
1003 conn_data->outstanding_requests =
1004 g_list_remove (conn_data->outstanding_requests, request_data);
1005 free_dispatcher_request_data (request_data);
1009 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1010 DispatcherRequestData *request_data,
1011 const gchar *object_path,
1012 GHashTable *properties,
1013 const GError *error)
1015 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1016 EmpathyDispatchOperation *operation = NULL;
1017 ConnectionData *conn_data;
1019 conn_data = g_hash_table_lookup (priv->connections,
1020 request_data->connection);
1024 DEBUG ("Channel request failed: %s", error->message);
1026 dispatcher_request_failed (dispatcher, request_data, error);
1031 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1034 if (operation != NULL)
1035 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1037 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1040 if (operation == NULL)
1042 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1047 operation = empathy_dispatch_operation_new_with_wrapper (
1048 request_data->connection,
1049 data->channel, request_data->contact, FALSE,
1050 data->channel_wrapper);
1056 if (properties != NULL)
1057 channel = tp_channel_new_from_properties (request_data->connection,
1058 object_path, properties, NULL);
1060 channel = tp_channel_new (request_data->connection, object_path,
1061 request_data->channel_type, request_data->handle_type,
1062 request_data->handle, NULL);
1064 g_signal_connect (channel, "invalidated",
1065 G_CALLBACK (dispatcher_channel_invalidated_cb),
1066 request_data->dispatcher);
1068 priv->channels = g_list_append (priv->channels, channel);
1070 operation = empathy_dispatch_operation_new (request_data->connection,
1071 channel, request_data->contact, FALSE);
1072 g_object_unref (channel);
1077 /* Already existed set potential extra information */
1078 g_object_set (G_OBJECT (operation),
1079 "contact", request_data->contact,
1083 request_data->operation = operation;
1085 /* (pre)-approve this right away as we requested it */
1086 empathy_dispatch_operation_approve (operation);
1088 dispatcher_start_dispatching (request_data->dispatcher, operation,
1091 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1096 dispatcher_request_channel_cb (TpConnection *connection,
1097 const gchar *object_path,
1098 const GError *error,
1100 GObject *weak_object)
1102 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1103 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1105 dispatcher_connection_new_requested_channel (dispatcher,
1106 request_data, object_path, NULL, error);
1110 dispatcher_request_channel (DispatcherRequestData *request_data)
1112 tp_cli_connection_call_request_channel (request_data->connection, -1,
1113 request_data->channel_type,
1114 request_data->handle_type,
1115 request_data->handle,
1116 TRUE, dispatcher_request_channel_cb,
1117 request_data, NULL, G_OBJECT (request_data->dispatcher));
1121 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1122 EmpathyDispatcherRequestCb *callback,
1125 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1126 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1128 TpConnection *connection;
1130 DispatcherRequestData *request_data;
1132 account = empathy_contact_get_account (contact);
1133 connection = g_hash_table_lookup (priv->accounts, account);
1135 g_assert (connection != NULL);
1136 cd = g_hash_table_lookup (priv->connections, connection);
1137 request_data = new_dispatcher_request_data (dispatcher, connection,
1138 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1139 contact, callback, user_data);
1141 cd->outstanding_requests = g_list_prepend
1142 (cd->outstanding_requests, request_data);
1144 dispatcher_request_channel (request_data);
1146 g_object_unref (dispatcher);
1150 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1151 const GError *error,
1155 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1157 request_data->handle = empathy_contact_get_handle (contact);
1159 dispatcher_request_channel (request_data);
1163 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1164 EmpathyDispatcherRequestCb *callback,
1167 EmpathyDispatcher *dispatcher;
1168 EmpathyDispatcherPriv *priv;
1170 TpConnection *connection;
1171 ConnectionData *connection_data;
1172 DispatcherRequestData *request_data;
1174 dispatcher = empathy_dispatcher_dup_singleton();
1175 priv = GET_PRIV (dispatcher);
1177 account = empathy_contact_get_account (contact);
1178 connection = g_hash_table_lookup (priv->accounts, account);
1179 connection_data = g_hash_table_lookup (priv->connections, connection);
1181 /* The contact handle might not be known yet */
1182 request_data = new_dispatcher_request_data (dispatcher, connection,
1183 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1184 contact, callback, user_data);
1186 connection_data->outstanding_requests = g_list_prepend
1187 (connection_data->outstanding_requests, request_data);
1189 empathy_contact_call_when_ready (contact,
1190 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1191 request_data, NULL, G_OBJECT (dispatcher));
1193 g_object_unref (dispatcher);
1197 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1198 const gchar *contact_id,
1199 EmpathyDispatcherRequestCb *callback,
1202 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1203 EmpathyContactFactory *factory;
1204 EmpathyContact *contact;
1206 factory = empathy_contact_factory_dup_singleton ();
1207 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1209 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1211 g_object_unref (contact);
1212 g_object_unref (factory);
1213 g_object_unref (dispatcher);
1217 dispatcher_request_handles_cb (TpConnection *connection,
1218 const GArray *handles,
1219 const GError *error,
1223 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1227 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1228 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1231 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1233 if (request_data->cb)
1234 request_data->cb (NULL, error, request_data->user_data);
1236 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1239 free_dispatcher_request_data (request_data);
1241 dispatcher_flush_outstanding_operations (dispatcher, cd);
1245 request_data->handle = g_array_index (handles, guint, 0);
1246 dispatcher_request_channel (request_data);
1250 empathy_dispatcher_join_muc (McAccount *account,
1251 const gchar *roomname,
1252 EmpathyDispatcherRequestCb *callback,
1255 EmpathyDispatcher *dispatcher;
1256 EmpathyDispatcherPriv *priv;
1257 DispatcherRequestData *request_data;
1258 TpConnection *connection;
1259 ConnectionData *connection_data;
1260 const gchar *names[] = { roomname, NULL };
1262 dispatcher = empathy_dispatcher_dup_singleton();
1263 priv = GET_PRIV (dispatcher);
1265 connection = g_hash_table_lookup (priv->accounts, account);
1266 connection_data = g_hash_table_lookup (priv->connections, connection);
1269 /* Don't know the room handle yet */
1270 request_data = new_dispatcher_request_data (dispatcher, connection,
1271 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1272 NULL, callback, user_data);
1274 connection_data->outstanding_requests = g_list_prepend
1275 (connection_data->outstanding_requests, request_data);
1277 tp_cli_connection_call_request_handles (connection, -1,
1278 TP_HANDLE_TYPE_ROOM, names,
1279 dispatcher_request_handles_cb, request_data, NULL,
1280 G_OBJECT (dispatcher));
1282 g_object_unref (dispatcher);
1286 dispatcher_create_channel_cb (TpConnection *connect,
1287 const gchar *object_path,
1288 GHashTable *properties,
1289 const GError *error,
1291 GObject *weak_object)
1293 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1294 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1296 dispatcher_connection_new_requested_channel (dispatcher,
1297 request_data, object_path, properties, error);
1301 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1302 const GError *error,
1306 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1307 GValue *target_handle;
1309 g_assert (request_data->request);
1313 dispatcher_request_failed (request_data->dispatcher,
1314 request_data, error);
1318 request_data->handle = empathy_contact_get_handle (contact);
1320 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1321 g_value_set_uint (target_handle, request_data->handle);
1322 g_hash_table_insert (request_data->request,
1323 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1325 tp_cli_connection_interface_requests_call_create_channel (
1326 request_data->connection, -1,
1327 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1328 G_OBJECT (request_data->dispatcher));
1332 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1333 const GError *error,
1336 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1340 dispatcher_request_failed (request_data->dispatcher,
1341 request_data, error);
1345 empathy_contact_call_when_ready (request_data->contact,
1346 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1347 request_data, NULL, G_OBJECT (request_data->dispatcher));
1351 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1352 const gchar *filename,
1355 const gchar *content_type,
1356 EmpathyDispatcherRequestCb *callback,
1359 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1360 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1361 McAccount *account = empathy_contact_get_account (contact);
1362 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1363 ConnectionData *connection_data =
1364 g_hash_table_lookup (priv->connections, connection);
1365 DispatcherRequestData *request_data;
1367 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1368 (GDestroyNotify) tp_g_value_slice_free);
1370 /* org.freedesktop.Telepathy.Channel.ChannelType */
1371 value = tp_g_value_slice_new (G_TYPE_STRING);
1372 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1373 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1375 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1376 value = tp_g_value_slice_new (G_TYPE_UINT);
1377 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1378 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1380 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1381 value = tp_g_value_slice_new (G_TYPE_STRING);
1382 g_value_set_string (value, content_type);
1383 g_hash_table_insert (request,
1384 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1386 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1387 value = tp_g_value_slice_new (G_TYPE_STRING);
1388 g_value_set_string (value, filename);
1389 g_hash_table_insert (request,
1390 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1392 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1393 value = tp_g_value_slice_new (G_TYPE_UINT64);
1394 g_value_set_uint64 (value, size);
1395 g_hash_table_insert (request,
1396 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1398 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1399 value = tp_g_value_slice_new (G_TYPE_UINT64);
1400 g_value_set_uint64 (value, date);
1401 g_hash_table_insert (request,
1402 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1405 /* The contact handle might not be known yet */
1406 request_data = new_dispatcher_request_data (dispatcher, connection,
1407 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1408 contact, callback, user_data);
1409 connection_data->outstanding_requests = g_list_prepend
1410 (connection_data->outstanding_requests, request_data);
1412 tp_connection_call_when_ready (connection,
1413 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1415 g_object_unref (dispatcher);