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>
31 #include <telepathy-glib/gtypes.h>
33 #include <libmissioncontrol/mission-control.h>
34 #include <libmissioncontrol/mc-account.h>
36 #include <extensions/extensions.h>
38 #include "empathy-dispatcher.h"
39 #include "empathy-utils.h"
40 #include "empathy-tube-handler.h"
41 #include "empathy-account-manager.h"
42 #include "empathy-contact-factory.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)
53 EmpathyAccountManager *account_manager;
55 /* connection to connection data mapping */
56 GHashTable *connections;
57 /* accounts to connection mapping */
62 /* channels which the dispatcher is listening "invalidated" */
64 } EmpathyDispatcherPriv;
66 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
76 static guint signals[LAST_SIGNAL];
77 static EmpathyDispatcher *dispatcher = NULL;
81 EmpathyDispatcher *dispatcher;
82 EmpathyDispatchOperation *operation;
83 TpConnection *connection;
87 EmpathyContact *contact;
89 /* Properties to pass to the channel when requesting it */
91 EmpathyDispatcherRequestCb *cb;
93 gpointer *request_data;
94 } DispatcherRequestData;
99 /* Channel type specific wrapper object */
100 GObject *channel_wrapper;
106 /* ObjectPath => DispatchData.. */
107 GHashTable *dispatched_channels;
108 /* ObjectPath -> EmpathyDispatchOperations */
109 GHashTable *dispatching_channels;
110 /* ObjectPath -> EmpathyDispatchOperations */
111 GHashTable *outstanding_channels;
112 /* List of DispatcherRequestData */
113 GList *outstanding_requests;
114 /* List of requestable channel classes */
115 GPtrArray *requestable_channels;
118 static DispatchData *
119 new_dispatch_data (TpChannel *channel,
120 GObject *channel_wrapper)
122 DispatchData *d = g_slice_new0 (DispatchData);
123 d->channel = g_object_ref (channel);
124 d->channel_wrapper = g_object_ref (channel_wrapper);
130 free_dispatch_data (DispatchData *data)
132 g_object_unref (data->channel);
133 g_object_unref (data->channel_wrapper);
135 g_slice_free (DispatchData, data);
138 static DispatcherRequestData *
139 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
140 TpConnection *connection,
141 const gchar *channel_type,
145 EmpathyContact *contact,
146 EmpathyDispatcherRequestCb *cb,
149 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
151 result->dispatcher = g_object_ref (dispatcher);
152 result->connection = connection;
154 result->channel_type = g_strdup (channel_type);
155 result->handle_type = handle_type;
156 result->handle = handle;
157 result->request = request;
160 result->contact = g_object_ref (contact);
163 result->user_data = user_data;
169 free_dispatcher_request_data (DispatcherRequestData *r)
171 g_free (r->channel_type);
173 if (r->dispatcher != NULL)
174 g_object_unref (r->dispatcher);
176 if (r->contact != NULL)
177 g_object_unref (r->contact);
179 if (r->request != NULL)
180 g_hash_table_unref (r->request);
182 g_slice_free (DispatcherRequestData, r);
185 static ConnectionData *
186 new_connection_data (McAccount *account)
188 ConnectionData *cd = g_slice_new0 (ConnectionData);
189 cd->account = g_object_ref (account);
191 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
192 g_free, (GDestroyNotify) free_dispatch_data);
194 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
195 g_free, g_object_unref);
197 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
204 free_connection_data (ConnectionData *cd)
207 g_object_unref (cd->account);
208 g_hash_table_destroy (cd->dispatched_channels);
209 g_hash_table_destroy (cd->dispatching_channels);
212 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
214 free_dispatcher_request_data (l->data);
217 if (cd->requestable_channels != NULL)
219 for (i = 0 ; i < cd->requestable_channels->len ; i++)
221 g_ptr_array_index (cd->requestable_channels, i));
222 g_ptr_array_free (cd->requestable_channels, TRUE);
227 dispatcher_connection_invalidated_cb (TpConnection *connection,
231 EmpathyDispatcher *dispatcher)
233 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
236 DEBUG ("Error: %s", message);
237 cd = g_hash_table_lookup (priv->connections, connection);
239 g_hash_table_remove (priv->accounts, cd->account);
240 g_hash_table_remove (priv->connections, connection);
244 dispatcher_operation_can_start (EmpathyDispatcher *self,
245 EmpathyDispatchOperation *operation,
249 const gchar *channel_type =
250 empathy_dispatch_operation_get_channel_type (operation);
252 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
254 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
256 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
266 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
267 EmpathyDispatchOperation *operation,
273 l = cd->outstanding_requests;
276 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
281 if (d->operation == operation)
286 d->cb (NULL, error, d->user_data);
288 d->cb (operation, NULL, d->user_data);
291 cd->outstanding_requests = g_list_delete_link
292 (cd->outstanding_requests, lt);
294 free_dispatcher_request_data (d);
300 dispatcher_channel_invalidated_cb (TpProxy *proxy,
304 EmpathyDispatcher *dispatcher)
306 /* Channel went away... */
307 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
308 TpConnection *connection;
309 EmpathyDispatchOperation *operation;
311 const gchar *object_path;
313 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
315 cd = g_hash_table_lookup (priv->connections, connection);
316 /* Connection itself invalidated? */
320 object_path = tp_proxy_get_object_path (proxy);
322 DEBUG ("Channel %s invalidated", object_path);
324 g_hash_table_remove (cd->dispatched_channels, object_path);
325 g_hash_table_remove (cd->dispatching_channels, object_path);
327 priv->channels = g_list_remove (priv->channels, proxy);
329 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
330 if (operation != NULL)
332 GError error = { domain, code, message };
333 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
334 g_hash_table_remove (cd->outstanding_channels, object_path);
335 g_object_unref (operation);
340 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
341 EmpathyDispatcher *dispatcher)
343 g_assert (empathy_dispatch_operation_is_incoming (operation));
344 DEBUG ("Send of for dispatching: %s",
345 empathy_dispatch_operation_get_object_path (operation));
346 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
350 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
351 EmpathyDispatcher *dispatcher)
353 /* Our job is done, remove the dispatch operation and mark the channel as
355 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
356 TpConnection *connection;
358 const gchar *object_path;
360 connection = empathy_dispatch_operation_get_tp_connection (operation);
361 cd = g_hash_table_lookup (priv->connections, connection);
362 g_assert (cd != NULL);
363 g_object_unref (G_OBJECT (connection));
365 object_path = empathy_dispatch_operation_get_object_path (operation);
367 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
370 d = new_dispatch_data (
371 empathy_dispatch_operation_get_channel (operation),
372 empathy_dispatch_operation_get_channel_wrapper (operation));
373 g_hash_table_insert (cd->dispatched_channels,
374 g_strdup (object_path), d);
376 g_hash_table_remove (cd->dispatching_channels, object_path);
378 DEBUG ("Channel claimed: %s", object_path);
382 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
383 EmpathyDispatcher *dispatcher)
385 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
386 TpConnection *connection;
388 EmpathyDispatchOperationState status;
390 g_signal_connect (operation, "approved",
391 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
393 g_signal_connect (operation, "claimed",
394 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
396 /* Signal the observers */
397 DEBUG ("Send to observers: %s",
398 empathy_dispatch_operation_get_object_path (operation));
399 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
401 empathy_dispatch_operation_start (operation);
403 /* Signal potential requestors */
404 connection = empathy_dispatch_operation_get_tp_connection (operation);
405 cd = g_hash_table_lookup (priv->connections, connection);
406 g_assert (cd != NULL);
407 g_object_unref (G_OBJECT (connection));
409 g_object_ref (operation);
410 g_object_ref (dispatcher);
412 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
413 status = empathy_dispatch_operation_get_status (operation);
414 g_object_unref (operation);
416 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
419 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
421 DEBUG ("Send to approvers: %s",
422 empathy_dispatch_operation_get_object_path (operation));
423 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
427 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
428 DEBUG ("Send of for dispatching: %s",
429 empathy_dispatch_operation_get_object_path (operation));
430 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
433 g_object_unref (dispatcher);
437 dispatcher_start_dispatching (EmpathyDispatcher *self,
438 EmpathyDispatchOperation *operation,
441 const gchar *object_path =
442 empathy_dispatch_operation_get_object_path (operation);
444 DEBUG ("Dispatching process started for %s", object_path);
446 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
448 g_assert (g_hash_table_lookup (cd->outstanding_channels,
449 object_path) == NULL);
451 g_hash_table_insert (cd->dispatching_channels,
452 g_strdup (object_path), operation);
454 switch (empathy_dispatch_operation_get_status (operation))
456 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
457 g_signal_connect (operation, "ready",
458 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
460 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
461 dispatch_operation_ready_cb (operation, dispatcher);
464 g_assert_not_reached();
468 else if (empathy_dispatch_operation_get_status (operation) >=
469 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
471 /* Already dispatching and the operation is pending, thus the observers
472 * have seen it (if applicable), so we can flush the request right away.
474 dispatch_operation_flush_requests (self, operation, NULL, cd);
479 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
485 g_hash_table_iter_init (&iter, cd->outstanding_channels);
486 while (g_hash_table_iter_next (&iter, NULL, &value))
488 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
490 if (dispatcher_operation_can_start (self, operation, cd))
492 dispatcher_start_dispatching (dispatcher, operation, cd);
493 g_hash_table_iter_remove (&iter);
499 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
500 TpConnection *connection,
501 const gchar *object_path,
502 const gchar *channel_type,
505 GHashTable *properties,
508 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
511 EmpathyDispatchOperation *operation;
512 EmpathyContact *contact = NULL;
514 /* Channel types we never want to dispatch because they're either deprecated
515 * or can't sensibly be dispatch (e.g. channels that should always be
517 const char *blacklist[] = {
518 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
519 TP_IFACE_CHANNEL_TYPE_TUBES,
520 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
524 cd = g_hash_table_lookup (priv->connections, connection);
526 /* Don't bother with channels we have already dispatched or are dispatching
527 * currently. This can happen when NewChannel(s) is fired after
528 * RequestChannel/CreateChannel/EnsureChannel */
529 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
532 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
535 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
537 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
540 /* Only pick up non-requested text and file channels. For all other it
541 * doesn't make sense to handle it if we didn't request it. The same goes
542 * for channels we discovered by the Channels property or ListChannels */
543 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
544 && tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
546 DEBUG ("Ignoring incoming channel of type %s on %s",
547 channel_type, object_path);
551 for (i = 0 ; blacklist[i] != NULL; i++)
553 if (!tp_strdiff (channel_type, blacklist[i]))
555 DEBUG ("Ignoring blacklisted channel type %s on %s",
556 channel_type, object_path);
561 DEBUG ("New channel of type %s on %s", channel_type, object_path);
563 if (properties == NULL)
564 channel = tp_channel_new (connection, object_path, channel_type,
565 handle_type, handle, NULL);
567 channel = tp_channel_new_from_properties (connection, object_path,
570 g_signal_connect (channel, "invalidated",
571 G_CALLBACK (dispatcher_channel_invalidated_cb),
574 priv->channels = g_list_prepend (priv->channels, channel);
576 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
578 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
579 contact = empathy_contact_factory_get_from_handle (factory,
580 cd->account, handle);
581 g_object_unref (factory);
584 operation = empathy_dispatch_operation_new (connection, channel, contact,
587 g_object_unref (channel);
591 /* Request could either be by us or by a remote party. If there are no
592 * outstanding requests for this channel type we can assume it's remote.
593 * Otherwise we wait untill they are all satisfied */
594 if (dispatcher_operation_can_start (dispatcher, operation, cd))
595 dispatcher_start_dispatching (dispatcher, operation, cd);
597 g_hash_table_insert (cd->outstanding_channels,
598 g_strdup (object_path), operation);
602 dispatcher_start_dispatching (dispatcher, operation, cd);
607 dispatcher_connection_new_channel_cb (TpConnection *connection,
608 const gchar *object_path,
609 const gchar *channel_type,
612 gboolean suppress_handler,
616 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
618 /* Empathy heavily abuses surpress handler (don't try this at home), if
619 * surpress handler is true then it is an outgoing channel, which is
620 * requested either by us or some other party (like the megaphone applet).
621 * Otherwise it's an incoming channel */
622 dispatcher_connection_new_channel (dispatcher, connection,
623 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
627 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
628 TpConnection *connection,
629 const gchar *object_path,
630 GHashTable *properties)
632 const gchar *channel_type;
639 channel_type = tp_asv_get_string (properties,
640 TP_IFACE_CHANNEL ".ChannelType");
641 if (channel_type == NULL)
643 g_message ("%s had an invalid ChannelType property", object_path);
647 handle_type = tp_asv_get_uint32 (properties,
648 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
651 g_message ("%s had an invalid TargetHandleType property", object_path);
655 handle = tp_asv_get_uint32 (properties,
656 TP_IFACE_CHANNEL ".TargetHandle", &valid);
659 g_message ("%s had an invalid TargetHandle property", object_path);
663 /* We assume there is no channel dispather, so we're the only one dispatching
664 * it. Which means that a requested channel it is outgoing one */
665 requested = tp_asv_get_boolean (properties,
666 TP_IFACE_CHANNEL ".Requested", &valid);
669 g_message ("%s had an invalid Requested property", object_path);
673 dispatcher_connection_new_channel (dispatcher, connection,
674 object_path, channel_type, handle_type, handle, properties, !requested);
678 dispatcher_connection_new_channels_cb (TpConnection *connection,
679 const GPtrArray *channels,
683 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
686 for (i = 0; i < channels->len ; i++)
688 GValueArray *arr = g_ptr_array_index (channels, i);
689 const gchar *object_path;
690 GHashTable *properties;
692 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
693 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
695 dispatcher_connection_new_channel_with_properties (dispatcher,
696 connection, object_path, properties);
701 dispatcher_connection_got_all (TpProxy *proxy,
702 GHashTable *properties,
707 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
708 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
710 GPtrArray *requestable_channels;
713 DEBUG ("Error: %s", error->message);
717 channels = tp_asv_get_boxed (properties, "Channels",
718 TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
720 if (channels == NULL)
721 DEBUG ("No Channels property !?! on connection");
723 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
724 channels, NULL, object);
726 requestable_channels = tp_asv_get_boxed (properties,
727 "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
729 if (requestable_channels == NULL)
730 DEBUG ("No RequestableChannelClasses property !?! on connection");
735 cd = g_hash_table_lookup (priv->connections, proxy);
736 g_assert (cd != NULL);
738 cd->requestable_channels = g_boxed_copy (
739 TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
744 dispatcher_connection_list_channels_cb (TpConnection *connection,
745 const GPtrArray *channels,
754 DEBUG ("Error: %s", error->message);
758 for (i = 0; i < channels->len; i++)
762 values = g_ptr_array_index (channels, i);
763 /* We don't have any extra info, so assume already existing channels are
765 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
767 g_value_get_boxed (g_value_array_get_nth (values, 0)),
768 g_value_get_string (g_value_array_get_nth (values, 1)),
769 g_value_get_uint (g_value_array_get_nth (values, 2)),
770 g_value_get_uint (g_value_array_get_nth (values, 3)),
776 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
777 const GPtrArray *capabilities,
783 DEBUG ("Error: %s", error->message);
787 dispatcher_connection_ready_cb (TpConnection *connection,
791 GPtrArray *capabilities;
794 const gchar *remove = NULL;
798 dispatcher_connection_invalidated_cb (connection, error->domain,
799 error->code, error->message, dispatcher);
803 g_signal_connect (connection, "invalidated",
804 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
806 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
807 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
809 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
810 dispatcher_connection_new_channels_cb,
811 NULL, NULL, G_OBJECT (dispatcher), NULL);
813 tp_cli_dbus_properties_call_get_all (connection, -1,
814 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
815 dispatcher_connection_got_all,
816 NULL, NULL, dispatcher);
820 tp_cli_connection_connect_to_new_channel (connection,
821 dispatcher_connection_new_channel_cb,
822 NULL, NULL, G_OBJECT (dispatcher), NULL);
824 tp_cli_connection_call_list_channels (connection, -1,
825 dispatcher_connection_list_channels_cb, NULL, NULL,
826 G_OBJECT (dispatcher));
830 /* Advertise VoIP capabilities */
831 /* FIXME: Capabilities is leaked */
832 capabilities = g_ptr_array_sized_new (1);
833 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
834 G_TYPE_UINT, G_TYPE_INVALID);
835 g_value_init (&cap, cap_type);
836 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
837 dbus_g_type_struct_set (&cap,
838 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
839 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
840 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
841 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
842 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
843 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
845 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
846 connection, -1, capabilities, &remove,
847 dispatcher_connection_advertise_capabilities_cb,
848 NULL, NULL, G_OBJECT (dispatcher));
852 dispatcher_update_account (EmpathyDispatcher *dispatcher,
855 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
856 TpConnection *connection;
858 connection = g_hash_table_lookup (priv->accounts, account);
859 if (connection != NULL)
862 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
863 if (connection == NULL)
866 g_hash_table_insert (priv->connections, g_object_ref (connection),
867 new_connection_data (account));
869 g_hash_table_insert (priv->accounts, g_object_ref (account),
870 g_object_ref (connection));
872 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
875 g_object_unref (connection);
879 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
881 TpConnectionStatusReason reason,
882 TpConnectionStatus status,
883 TpConnectionStatus previous,
884 EmpathyDispatcher *dispatcher)
886 dispatcher_update_account (dispatcher, account);
890 dispatcher_constructor (GType type,
891 guint n_construct_params,
892 GObjectConstructParam *construct_params)
896 if (dispatcher == NULL)
898 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
899 (type, n_construct_params, construct_params);
901 dispatcher = EMPATHY_DISPATCHER (retval);
902 g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
906 retval = g_object_ref (dispatcher);
913 dispatcher_finalize (GObject *object)
915 EmpathyDispatcherPriv *priv = GET_PRIV (object);
920 g_signal_handlers_disconnect_by_func (priv->account_manager,
921 dispatcher_account_connection_cb, object);
923 for (l = priv->channels; l; l = l->next)
925 g_signal_handlers_disconnect_by_func (l->data,
926 dispatcher_channel_invalidated_cb, object);
929 g_list_free (priv->channels);
931 g_hash_table_iter_init (&iter, priv->connections);
932 while (g_hash_table_iter_next (&iter, &connection, NULL))
934 g_signal_handlers_disconnect_by_func (connection,
935 dispatcher_connection_invalidated_cb, object);
938 g_object_unref (priv->account_manager);
939 g_object_unref (priv->mc);
941 g_hash_table_destroy (priv->accounts);
942 g_hash_table_destroy (priv->connections);
946 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
948 GObjectClass *object_class = G_OBJECT_CLASS (klass);
950 object_class->finalize = dispatcher_finalize;
951 object_class->constructor = dispatcher_constructor;
954 g_signal_new ("observe",
955 G_TYPE_FROM_CLASS (klass),
959 g_cclosure_marshal_VOID__OBJECT,
961 1, EMPATHY_TYPE_DISPATCH_OPERATION);
964 g_signal_new ("approve",
965 G_TYPE_FROM_CLASS (klass),
969 g_cclosure_marshal_VOID__OBJECT,
971 1, EMPATHY_TYPE_DISPATCH_OPERATION);
974 g_signal_new ("dispatch",
975 G_TYPE_FROM_CLASS (klass),
979 g_cclosure_marshal_VOID__OBJECT,
981 1, EMPATHY_TYPE_DISPATCH_OPERATION);
983 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
988 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
991 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
992 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
994 dispatcher->priv = priv;
995 priv->mc = empathy_mission_control_dup_singleton ();
996 priv->account_manager = empathy_account_manager_dup_singleton ();
998 g_signal_connect (priv->account_manager,
999 "account-connection-changed",
1000 G_CALLBACK (dispatcher_account_connection_cb),
1003 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1004 empathy_account_equal, g_object_unref, g_object_unref);
1006 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1007 g_object_unref, (GDestroyNotify) free_connection_data);
1009 priv->channels = NULL;
1011 accounts = mc_accounts_list_by_enabled (TRUE);
1013 for (l = accounts; l; l = l->next)
1015 dispatcher_update_account (dispatcher, l->data);
1016 g_object_unref (l->data);
1018 g_list_free (accounts);
1022 empathy_dispatcher_dup_singleton (void)
1024 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1028 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1029 DispatcherRequestData *request_data,
1030 const GError *error)
1032 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1033 ConnectionData *conn_data;
1035 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1036 if (request_data->cb != NULL)
1037 request_data->cb (NULL, error, request_data->user_data);
1039 conn_data->outstanding_requests =
1040 g_list_remove (conn_data->outstanding_requests, request_data);
1041 free_dispatcher_request_data (request_data);
1045 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1046 DispatcherRequestData *request_data,
1047 const gchar *object_path,
1048 GHashTable *properties,
1049 const GError *error)
1051 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1052 EmpathyDispatchOperation *operation = NULL;
1053 ConnectionData *conn_data;
1055 conn_data = g_hash_table_lookup (priv->connections,
1056 request_data->connection);
1060 DEBUG ("Channel request failed: %s", error->message);
1062 dispatcher_request_failed (dispatcher, request_data, error);
1067 operation = g_hash_table_lookup (conn_data->outstanding_channels,
1070 if (operation != NULL)
1071 g_hash_table_remove (conn_data->outstanding_channels, object_path);
1073 operation = g_hash_table_lookup (conn_data->dispatching_channels,
1076 if (operation == NULL)
1078 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1083 operation = empathy_dispatch_operation_new_with_wrapper (
1084 request_data->connection,
1085 data->channel, request_data->contact, FALSE,
1086 data->channel_wrapper);
1092 if (properties != NULL)
1093 channel = tp_channel_new_from_properties (request_data->connection,
1094 object_path, properties, NULL);
1096 channel = tp_channel_new (request_data->connection, object_path,
1097 request_data->channel_type, request_data->handle_type,
1098 request_data->handle, NULL);
1100 g_signal_connect (channel, "invalidated",
1101 G_CALLBACK (dispatcher_channel_invalidated_cb),
1102 request_data->dispatcher);
1104 priv->channels = g_list_prepend (priv->channels, channel);
1106 operation = empathy_dispatch_operation_new (request_data->connection,
1107 channel, request_data->contact, FALSE);
1108 g_object_unref (channel);
1113 /* Already existed set potential extra information */
1114 g_object_set (G_OBJECT (operation),
1115 "contact", request_data->contact,
1119 request_data->operation = operation;
1121 /* (pre)-approve this right away as we requested it */
1122 empathy_dispatch_operation_approve (operation);
1124 dispatcher_start_dispatching (request_data->dispatcher, operation,
1127 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1132 dispatcher_request_channel_cb (TpConnection *connection,
1133 const gchar *object_path,
1134 const GError *error,
1136 GObject *weak_object)
1138 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1139 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1141 dispatcher_connection_new_requested_channel (dispatcher,
1142 request_data, object_path, NULL, error);
1146 dispatcher_request_channel (DispatcherRequestData *request_data)
1148 tp_cli_connection_call_request_channel (request_data->connection, -1,
1149 request_data->channel_type,
1150 request_data->handle_type,
1151 request_data->handle,
1152 TRUE, dispatcher_request_channel_cb,
1153 request_data, NULL, G_OBJECT (request_data->dispatcher));
1157 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1158 EmpathyDispatcherRequestCb *callback,
1161 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1162 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1164 TpConnection *connection;
1166 DispatcherRequestData *request_data;
1168 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1170 account = empathy_contact_get_account (contact);
1171 connection = g_hash_table_lookup (priv->accounts, account);
1173 g_assert (connection != NULL);
1174 cd = g_hash_table_lookup (priv->connections, connection);
1175 request_data = new_dispatcher_request_data (dispatcher, connection,
1176 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1177 contact, callback, user_data);
1179 cd->outstanding_requests = g_list_prepend
1180 (cd->outstanding_requests, request_data);
1182 dispatcher_request_channel (request_data);
1184 g_object_unref (dispatcher);
1188 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1189 const GError *error,
1193 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1195 request_data->handle = empathy_contact_get_handle (contact);
1197 dispatcher_request_channel (request_data);
1201 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1202 EmpathyDispatcherRequestCb *callback,
1205 EmpathyDispatcher *dispatcher;
1206 EmpathyDispatcherPriv *priv;
1208 TpConnection *connection;
1209 ConnectionData *connection_data;
1210 DispatcherRequestData *request_data;
1212 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1214 dispatcher = empathy_dispatcher_dup_singleton();
1215 priv = GET_PRIV (dispatcher);
1217 account = empathy_contact_get_account (contact);
1218 connection = g_hash_table_lookup (priv->accounts, account);
1219 connection_data = g_hash_table_lookup (priv->connections, connection);
1221 /* The contact handle might not be known yet */
1222 request_data = new_dispatcher_request_data (dispatcher, connection,
1223 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1224 contact, callback, user_data);
1226 connection_data->outstanding_requests = g_list_prepend
1227 (connection_data->outstanding_requests, request_data);
1229 empathy_contact_call_when_ready (contact,
1230 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1231 request_data, NULL, G_OBJECT (dispatcher));
1233 g_object_unref (dispatcher);
1237 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1238 const gchar *contact_id,
1239 EmpathyDispatcherRequestCb *callback,
1242 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1243 EmpathyContactFactory *factory;
1244 EmpathyContact *contact;
1246 g_return_if_fail (MC_IS_ACCOUNT (account));
1247 g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1249 factory = empathy_contact_factory_dup_singleton ();
1250 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1252 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1254 g_object_unref (contact);
1255 g_object_unref (factory);
1256 g_object_unref (dispatcher);
1260 dispatcher_request_handles_cb (TpConnection *connection,
1261 const GArray *handles,
1262 const GError *error,
1266 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1270 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1271 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1274 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1276 if (request_data->cb)
1277 request_data->cb (NULL, error, request_data->user_data);
1279 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1282 free_dispatcher_request_data (request_data);
1284 dispatcher_flush_outstanding_operations (dispatcher, cd);
1288 request_data->handle = g_array_index (handles, guint, 0);
1289 dispatcher_request_channel (request_data);
1293 empathy_dispatcher_join_muc (McAccount *account,
1294 const gchar *roomname,
1295 EmpathyDispatcherRequestCb *callback,
1298 EmpathyDispatcher *dispatcher;
1299 EmpathyDispatcherPriv *priv;
1300 DispatcherRequestData *request_data;
1301 TpConnection *connection;
1302 ConnectionData *connection_data;
1303 const gchar *names[] = { roomname, NULL };
1305 g_return_if_fail (MC_IS_ACCOUNT (account));
1306 g_return_if_fail (!EMP_STR_EMPTY (roomname));
1308 dispatcher = empathy_dispatcher_dup_singleton();
1309 priv = GET_PRIV (dispatcher);
1311 connection = g_hash_table_lookup (priv->accounts, account);
1312 connection_data = g_hash_table_lookup (priv->connections, connection);
1315 /* Don't know the room handle yet */
1316 request_data = new_dispatcher_request_data (dispatcher, connection,
1317 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1318 NULL, callback, user_data);
1320 connection_data->outstanding_requests = g_list_prepend
1321 (connection_data->outstanding_requests, request_data);
1323 tp_cli_connection_call_request_handles (connection, -1,
1324 TP_HANDLE_TYPE_ROOM, names,
1325 dispatcher_request_handles_cb, request_data, NULL,
1326 G_OBJECT (dispatcher));
1328 g_object_unref (dispatcher);
1332 dispatcher_create_channel_cb (TpConnection *connect,
1333 const gchar *object_path,
1334 GHashTable *properties,
1335 const GError *error,
1337 GObject *weak_object)
1339 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1340 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1342 dispatcher_connection_new_requested_channel (dispatcher,
1343 request_data, object_path, properties, error);
1347 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1349 GHashTable *request,
1350 EmpathyDispatcherRequestCb *callback,
1353 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1354 ConnectionData *connection_data;
1355 DispatcherRequestData *request_data;
1356 const gchar *channel_type;
1360 TpConnection *connection;
1362 g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1363 g_return_if_fail (MC_IS_ACCOUNT (account));
1364 g_return_if_fail (request != NULL);
1366 connection = g_hash_table_lookup (priv->accounts, account);
1367 g_assert (connection != NULL);
1369 connection_data = g_hash_table_lookup (priv->connections, connection);
1370 g_assert (connection_data != NULL);
1372 channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1374 handle_type = tp_asv_get_uint32 (request,
1375 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1377 handle_type = TP_UNKNOWN_HANDLE_TYPE;
1379 handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1381 request_data = new_dispatcher_request_data (dispatcher, connection,
1382 channel_type, handle_type, handle, request,
1383 NULL, callback, user_data);
1385 connection_data->outstanding_requests = g_list_prepend
1386 (connection_data->outstanding_requests, request_data);
1388 tp_cli_connection_interface_requests_call_create_channel (
1389 request_data->connection, -1,
1390 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1391 G_OBJECT (request_data->dispatcher));
1395 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1396 const GError *error,
1400 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1401 GValue *target_handle;
1403 g_assert (request_data->request);
1407 dispatcher_request_failed (request_data->dispatcher,
1408 request_data, error);
1412 request_data->handle = empathy_contact_get_handle (contact);
1414 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1415 g_value_set_uint (target_handle, request_data->handle);
1416 g_hash_table_insert (request_data->request,
1417 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1419 tp_cli_connection_interface_requests_call_create_channel (
1420 request_data->connection, -1,
1421 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1422 G_OBJECT (request_data->dispatcher));
1426 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1427 const GError *error,
1430 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1434 dispatcher_request_failed (request_data->dispatcher,
1435 request_data, error);
1439 empathy_contact_call_when_ready (request_data->contact,
1440 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1441 request_data, NULL, G_OBJECT (request_data->dispatcher));
1445 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1446 const gchar *filename,
1449 const gchar *content_type,
1450 EmpathyDispatcherRequestCb *callback,
1453 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1454 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1455 McAccount *account = empathy_contact_get_account (contact);
1456 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1457 ConnectionData *connection_data =
1458 g_hash_table_lookup (priv->connections, connection);
1459 DispatcherRequestData *request_data;
1461 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1462 (GDestroyNotify) tp_g_value_slice_free);
1464 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1465 g_return_if_fail (!EMP_STR_EMPTY (filename));
1466 g_return_if_fail (!EMP_STR_EMPTY (content_type));
1468 /* org.freedesktop.Telepathy.Channel.ChannelType */
1469 value = tp_g_value_slice_new (G_TYPE_STRING);
1470 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1471 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1473 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1474 value = tp_g_value_slice_new (G_TYPE_UINT);
1475 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1476 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1478 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1479 value = tp_g_value_slice_new (G_TYPE_STRING);
1480 g_value_set_string (value, content_type);
1481 g_hash_table_insert (request,
1482 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1484 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1485 value = tp_g_value_slice_new (G_TYPE_STRING);
1486 g_value_set_string (value, filename);
1487 g_hash_table_insert (request,
1488 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1490 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1491 value = tp_g_value_slice_new (G_TYPE_UINT64);
1492 g_value_set_uint64 (value, size);
1493 g_hash_table_insert (request,
1494 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1496 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1497 value = tp_g_value_slice_new (G_TYPE_UINT64);
1498 g_value_set_uint64 (value, date);
1499 g_hash_table_insert (request,
1500 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1503 /* The contact handle might not be known yet */
1504 request_data = new_dispatcher_request_data (dispatcher, connection,
1505 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1506 contact, callback, user_data);
1507 connection_data->outstanding_requests = g_list_prepend
1508 (connection_data->outstanding_requests, request_data);
1510 tp_connection_call_when_ready (connection,
1511 dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1513 g_object_unref (dispatcher);
1517 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1519 const gchar *channel_type,
1522 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1524 TpConnection *connection;
1528 g_return_val_if_fail (channel_type != NULL, NULL);
1529 g_return_val_if_fail (handle_type != 0, NULL);
1531 connection = g_hash_table_lookup (priv->accounts, account);
1533 if (connection == NULL)
1536 cd = g_hash_table_lookup (priv->connections, connection);
1542 classes = cd->requestable_channels;
1543 if (classes == NULL)
1546 for (i = 0; i < classes->len; i++)
1552 const gchar *c_type;
1556 class = g_ptr_array_index (classes, i);
1557 fixed = g_value_array_get_nth (class, 0);
1559 fprops = g_value_get_boxed (fixed);
1560 c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1562 if (tp_strdiff (channel_type, c_type))
1565 h_type = tp_asv_get_uint32 (fprops,
1566 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1568 if (!valid || handle_type != h_type)
1571 allowed = g_value_array_get_nth (class, 1);
1573 return g_value_get_boxed (allowed);