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 = channel;
122 d->channel_wrapper = 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 cd = g_hash_table_lookup (priv->connections, connection);
484 /* Don't bother with channels we have already dispatched or are dispatching
485 * currently. This can happen when NewChannel(s) is fired after
486 * RequestChannel/CreateChannel/EnsureChannel */
487 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
490 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
493 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
495 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
498 DEBUG ("New channel of type %s on %s",
499 channel_type, object_path);
501 if (properties == NULL)
502 channel = tp_channel_new (connection, object_path, channel_type,
503 handle_type, handle, NULL);
505 channel = tp_channel_new_from_properties (connection, object_path,
508 g_signal_connect (channel, "invalidated",
509 G_CALLBACK (dispatcher_channel_invalidated_cb),
512 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
514 EmpathyContactFactory *factory = empathy_contact_factory_new ();
515 contact = empathy_contact_factory_get_from_handle (factory,
516 cd->account, handle);
517 g_object_unref (factory);
520 operation = empathy_dispatch_operation_new (connection, channel, contact,
523 g_object_unref (channel);
527 /* Request could either be by us or by a remote party. If there are no
528 * outstanding requests for this channel type we can assume it's remote.
529 * Otherwise we wait untill they are all satisfied */
530 if (dispatcher_operation_can_start (dispatcher, operation, cd))
531 dispatcher_start_dispatching (dispatcher, operation, cd);
533 g_hash_table_insert (cd->outstanding_channels,
534 g_strdup (object_path), operation);
538 dispatcher_start_dispatching (dispatcher, operation, cd);
543 dispatcher_connection_new_channel_cb (TpConnection *connection,
544 const gchar *object_path, const gchar *channel_type,
545 guint handle_type, guint handle,
546 gboolean suppress_handler, gpointer user_data,
549 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
551 /* Empathy heavily abuses surpress handler (don't try this at home), if
552 * surpress handler is true then it is an outgoing channel, which is
553 * requested either by us or some other party (like the megaphone applet).
554 * Otherwise it's an incoming channel */
555 dispatcher_connection_new_channel (dispatcher, connection,
556 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
560 dispatcher_connection_new_channel_with_properties (
561 EmpathyDispatcher *dispatcher, TpConnection *connection,
562 const gchar *object_path, GHashTable *properties)
564 const gchar *channel_type;
571 channel_type = tp_asv_get_string (properties,
572 TP_IFACE_CHANNEL ".ChannelType");
573 if (channel_type == NULL)
575 g_message ("%s had an invalid ChannelType property", object_path);
579 handle_type = tp_asv_get_uint32 (properties,
580 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
583 g_message ("%s had an invalid TargetHandleType property", object_path);
587 handle = tp_asv_get_uint32 (properties,
588 TP_IFACE_CHANNEL ".TargetHandle", &valid);
591 g_message ("%s had an invalid TargetHandle property", object_path);
595 /* We assume there is no channel dispather, so we're the only one dispatching
596 * it. Which means that a requested channel it is outgoing one */
597 requested = tp_asv_get_boolean (properties,
598 TP_IFACE_CHANNEL ".Requested", &valid);
601 g_message ("%s had an invalid Requested property", object_path);
605 dispatcher_connection_new_channel (dispatcher, connection,
606 object_path, channel_type, handle_type, handle, properties, !requested);
611 dispatcher_connection_new_channels_cb (
612 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
615 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
618 for (i = 0; i < channels->len ; i++)
620 GValueArray *arr = g_ptr_array_index (channels, i);
621 const gchar *object_path;
622 GHashTable *properties;
624 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
625 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
627 dispatcher_connection_new_channel_with_properties (dispatcher,
628 connection, object_path, properties);
633 dispatcher_connection_got_channels_property (TpProxy *proxy,
634 const GValue *channels_prop, const GError *error, gpointer user_data,
640 DEBUG ("Error: %s", error->message);
644 channels = g_value_get_boxed (channels_prop);
645 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
646 channels, NULL, object);
650 dispatcher_connection_list_channels_cb (TpConnection *connection,
651 const GPtrArray *channels,
659 DEBUG ("Error: %s", error->message);
663 for (i = 0; i < channels->len; i++) {
666 values = g_ptr_array_index (channels, i);
667 /* We don't have any extra info, so assume already existing channels are
669 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
671 g_value_get_boxed (g_value_array_get_nth (values, 0)),
672 g_value_get_string (g_value_array_get_nth (values, 1)),
673 g_value_get_uint (g_value_array_get_nth (values, 2)),
674 g_value_get_uint (g_value_array_get_nth (values, 3)),
680 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
681 const GPtrArray *capabilities,
687 DEBUG ("Error: %s", error->message);
692 dispatcher_connection_ready_cb (TpConnection *connection,
693 const GError *error, gpointer dispatcher)
695 GPtrArray *capabilities;
698 const gchar *remove = NULL;
702 dispatcher_connection_invalidated_cb (connection, error->domain,
703 error->code, error->message, dispatcher);
707 g_signal_connect (connection, "invalidated",
708 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
711 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
712 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
714 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
715 dispatcher_connection_new_channels_cb,
716 NULL, NULL, G_OBJECT (dispatcher), NULL);
718 tp_cli_dbus_properties_call_get (connection, -1,
719 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
720 dispatcher_connection_got_channels_property,
721 NULL, NULL, dispatcher);
725 tp_cli_connection_connect_to_new_channel (connection,
726 dispatcher_connection_new_channel_cb,
727 NULL, NULL, G_OBJECT (dispatcher), NULL);
729 tp_cli_connection_call_list_channels (connection, -1,
730 dispatcher_connection_list_channels_cb, NULL, NULL,
731 G_OBJECT (dispatcher));
735 /* Advertise VoIP capabilities */
736 capabilities = g_ptr_array_sized_new (1);
737 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
738 G_TYPE_UINT, G_TYPE_INVALID);
739 g_value_init (&cap, cap_type);
740 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
741 dbus_g_type_struct_set (&cap,
742 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
743 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
744 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
745 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
746 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
747 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
749 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
750 connection, -1, capabilities, &remove,
751 dispatcher_connection_advertise_capabilities_cb,
752 NULL, NULL, G_OBJECT (dispatcher));
756 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
758 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
759 TpConnection *connection;
761 connection = g_hash_table_lookup (priv->accounts, account);
762 if (connection != NULL)
765 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
766 if (connection == NULL)
769 g_hash_table_insert (priv->connections, g_object_ref (connection),
770 new_connection_data (account));
772 g_hash_table_insert (priv->accounts, g_object_ref (account),
773 g_object_ref (connection));
775 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
780 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
781 McAccount *account, TpConnectionStatusReason reason,
782 TpConnectionStatus status, TpConnectionStatus previous,
783 EmpathyDispatcher *dispatcher)
785 dispatcher_update_account (dispatcher, account);
790 dispatcher_constructor (GType type, guint n_construct_params,
791 GObjectConstructParam *construct_params)
795 if (dispatcher == NULL)
797 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
798 (type, n_construct_params, construct_params);
800 dispatcher = EMPATHY_DISPATCHER (retval);
801 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
805 retval = g_object_ref (dispatcher);
812 dispatcher_finalize (GObject *object)
814 EmpathyDispatcherPriv *priv = GET_PRIV (object);
816 g_signal_handlers_disconnect_by_func (priv->account_manager,
817 dispatcher_account_connection_cb, object);
819 g_object_unref (priv->account_manager);
820 g_object_unref (priv->mc);
822 g_hash_table_destroy (priv->accounts);
823 g_hash_table_destroy (priv->connections);
827 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
829 GObjectClass *object_class = G_OBJECT_CLASS (klass);
831 object_class->finalize = dispatcher_finalize;
832 object_class->constructor = dispatcher_constructor;
835 g_signal_new ("observe",
836 G_TYPE_FROM_CLASS (klass),
840 g_cclosure_marshal_VOID__OBJECT,
842 1, EMPATHY_TYPE_DISPATCH_OPERATION);
845 g_signal_new ("approve",
846 G_TYPE_FROM_CLASS (klass),
850 g_cclosure_marshal_VOID__OBJECT,
852 1, EMPATHY_TYPE_DISPATCH_OPERATION);
855 g_signal_new ("dispatch",
856 G_TYPE_FROM_CLASS (klass),
860 g_cclosure_marshal_VOID__OBJECT,
862 1, EMPATHY_TYPE_DISPATCH_OPERATION);
864 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
869 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
872 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
873 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
875 dispatcher->priv = priv;
876 priv->mc = empathy_mission_control_new ();
877 priv->account_manager = empathy_account_manager_dup_singleton ();
879 g_signal_connect (priv->account_manager,
880 "account-connection-changed",
881 G_CALLBACK (dispatcher_account_connection_cb),
884 priv->accounts = g_hash_table_new_full (empathy_account_hash,
885 empathy_account_equal, g_object_unref, g_object_unref);
887 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
888 g_object_unref, (GDestroyNotify) free_connection_data);
890 accounts = mc_accounts_list_by_enabled (TRUE);
892 for (l = accounts; l; l = l->next) {
893 dispatcher_update_account (dispatcher, l->data);
894 g_object_unref (l->data);
896 g_list_free (accounts);
900 empathy_dispatcher_dup_singleton (void)
902 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
906 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
907 DispatcherRequestData *request_data, const gchar *object_path,
908 GHashTable *properties, const GError *error)
910 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
911 EmpathyDispatchOperation *operation = NULL;
912 ConnectionData *conn_data;
914 conn_data = g_hash_table_lookup (priv->connections,
915 request_data->connection);
919 DEBUG ("Channel request failed: %s", error->message);
921 if (request_data->cb != NULL)
922 request_data->cb (NULL, error, request_data->user_data);
924 conn_data->outstanding_requests =
925 g_list_remove (conn_data->outstanding_requests, request_data);
926 free_dispatcher_request_data (request_data);
931 operation = g_hash_table_lookup (conn_data->outstanding_channels,
934 if (operation != NULL)
935 g_hash_table_remove (conn_data->outstanding_channels, object_path);
937 operation = g_hash_table_lookup (conn_data->dispatching_channels,
940 if (operation == NULL)
942 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
947 operation = empathy_dispatch_operation_new_with_wrapper (
948 request_data->connection,
949 data->channel, request_data->contact, FALSE,
950 data->channel_wrapper);
956 if (properties != NULL)
957 channel = tp_channel_new_from_properties (request_data->connection,
958 object_path, properties, NULL);
960 channel = tp_channel_new (request_data->connection, object_path,
961 request_data->channel_type, request_data->handle_type,
962 request_data->handle, NULL);
964 g_signal_connect (channel, "invalidated",
965 G_CALLBACK (dispatcher_channel_invalidated_cb),
966 request_data->dispatcher);
968 operation = empathy_dispatch_operation_new (request_data->connection,
969 channel, request_data->contact, FALSE);
970 g_object_unref (channel);
975 /* Already existed set potential extra information */
976 g_object_set (G_OBJECT (operation),
977 "contact", request_data->contact,
981 request_data->operation = operation;
983 /* (pre)-approve this right away as we requested it */
984 empathy_dispatch_operation_approve (operation);
986 dispatcher_start_dispatching (request_data->dispatcher, operation,
989 dispatcher_flush_outstanding_operations (request_data->dispatcher,
994 dispatcher_request_channel_cb (TpConnection *connection,
995 const gchar *object_path, const GError *error,
996 gpointer user_data, GObject *weak_object)
998 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
999 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1001 dispatcher_connection_new_requested_channel (dispatcher,
1002 request_data, object_path, NULL, error);
1006 dispatcher_request_channel (DispatcherRequestData *request_data)
1008 tp_cli_connection_call_request_channel (request_data->connection, -1,
1009 request_data->channel_type,
1010 request_data->handle_type,
1011 request_data->handle,
1012 TRUE, dispatcher_request_channel_cb,
1013 request_data, NULL, G_OBJECT (request_data->dispatcher));
1017 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1018 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1020 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1021 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1023 TpConnection *connection;
1025 DispatcherRequestData *request_data;
1027 account = empathy_contact_get_account (contact);
1028 connection = g_hash_table_lookup (priv->accounts, account);
1030 g_assert (connection != NULL);
1031 cd = g_hash_table_lookup (priv->connections, connection);
1032 request_data = new_dispatcher_request_data (dispatcher, connection,
1033 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1034 contact, callback, user_data);
1036 cd->outstanding_requests = g_list_prepend
1037 (cd->outstanding_requests, request_data);
1039 dispatcher_request_channel (request_data);
1041 g_object_unref (dispatcher);
1046 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1048 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1050 request_data->handle = empathy_contact_get_handle (contact);
1052 dispatcher_request_channel (request_data);
1056 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1057 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1059 EmpathyDispatcher *dispatcher;
1060 EmpathyDispatcherPriv *priv;
1062 TpConnection *connection;
1063 ConnectionData *connection_data;
1064 DispatcherRequestData *request_data;
1066 dispatcher = empathy_dispatcher_dup_singleton();
1067 priv = GET_PRIV (dispatcher);
1069 account = empathy_contact_get_account (contact);
1070 connection = g_hash_table_lookup (priv->accounts, account);
1071 connection_data = g_hash_table_lookup (priv->connections, connection);
1073 /* The contact handle might not be known yet */
1074 request_data = new_dispatcher_request_data (dispatcher, connection,
1075 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1076 contact, callback, user_data);
1078 connection_data->outstanding_requests = g_list_prepend
1079 (connection_data->outstanding_requests, request_data);
1081 empathy_contact_call_when_ready (contact,
1082 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1085 g_object_unref (dispatcher);
1089 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1090 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1092 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1093 EmpathyContactFactory *factory;
1094 EmpathyContact *contact;
1096 factory = empathy_contact_factory_dup_singleton ();
1097 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1099 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1101 g_object_unref (contact);
1102 g_object_unref (factory);
1103 g_object_unref (dispatcher);
1108 dispatcher_request_handles_cb (TpConnection *connection,
1109 const GArray *handles, const GError *error, gpointer user_data,
1112 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1116 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1117 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1120 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1122 if (request_data->cb)
1123 request_data->cb (NULL, error, request_data->user_data);
1125 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1128 free_dispatcher_request_data (request_data);
1130 dispatcher_flush_outstanding_operations (dispatcher, cd);
1134 request_data->handle = g_array_index (handles, guint, 0);
1135 dispatcher_request_channel (request_data);
1139 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1140 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1142 EmpathyDispatcher *dispatcher;
1143 EmpathyDispatcherPriv *priv;
1144 DispatcherRequestData *request_data;
1145 TpConnection *connection;
1146 ConnectionData *connection_data;
1147 const gchar *names[] = { roomname, NULL };
1149 dispatcher = empathy_dispatcher_dup_singleton();
1150 priv = GET_PRIV (dispatcher);
1152 connection = g_hash_table_lookup (priv->accounts, account);
1153 connection_data = g_hash_table_lookup (priv->connections, connection);
1156 /* Don't know the room handle yet */
1157 request_data = new_dispatcher_request_data (dispatcher, connection,
1158 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1159 NULL, callback, user_data);
1161 connection_data->outstanding_requests = g_list_prepend
1162 (connection_data->outstanding_requests, request_data);
1164 tp_cli_connection_call_request_handles (connection, -1,
1165 TP_HANDLE_TYPE_ROOM, names,
1166 dispatcher_request_handles_cb, request_data, NULL,
1167 G_OBJECT (dispatcher));
1169 g_object_unref (dispatcher);
1173 dispatcher_create_channel_cb (TpConnection *connect,
1174 const gchar *object_path, GHashTable *properties, const GError *error,
1175 gpointer user_data, GObject *weak_object)
1177 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1178 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1180 dispatcher_connection_new_requested_channel (dispatcher,
1181 request_data, object_path, properties, error);
1186 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1189 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1190 GValue *target_handle;
1192 g_assert (request_data->request);
1194 request_data->handle = empathy_contact_get_handle (contact);
1196 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1197 g_value_set_uint (target_handle, request_data->handle);
1198 g_hash_table_insert (request_data->request,
1199 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1201 tp_cli_connection_interface_requests_call_create_channel (
1202 request_data->connection, -1,
1203 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1204 G_OBJECT (request_data->dispatcher));
1208 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1209 const gchar *filename, guint64 size, guint64 date,
1210 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1213 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1214 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1215 McAccount *account = empathy_contact_get_account (contact);
1216 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1217 ConnectionData *connection_data =
1218 g_hash_table_lookup (priv->connections, connection);
1219 DispatcherRequestData *request_data;
1221 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1222 (GDestroyNotify) tp_g_value_slice_free);
1224 /* org.freedesktop.Telepathy.Channel.ChannelType */
1225 value = tp_g_value_slice_new (G_TYPE_STRING);
1226 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1227 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1229 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1230 value = tp_g_value_slice_new (G_TYPE_UINT);
1231 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1232 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1234 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1235 value = tp_g_value_slice_new (G_TYPE_STRING);
1236 g_value_set_string (value, content_type);
1237 g_hash_table_insert (request,
1238 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1240 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1241 value = tp_g_value_slice_new (G_TYPE_STRING);
1242 g_value_set_string (value, filename);
1243 g_hash_table_insert (request,
1244 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1246 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1247 value = tp_g_value_slice_new (G_TYPE_UINT64);
1248 g_value_set_uint64 (value, size);
1249 g_hash_table_insert (request,
1250 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1252 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1253 value = tp_g_value_slice_new (G_TYPE_UINT64);
1254 g_value_set_uint64 (value, date);
1255 g_hash_table_insert (request,
1256 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1259 /* The contact handle might not be known yet */
1260 request_data = new_dispatcher_request_data (dispatcher, connection,
1261 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1262 contact, callback, user_data);
1263 connection_data->outstanding_requests = g_list_prepend
1264 (connection_data->outstanding_requests, request_data);
1266 empathy_contact_call_when_ready (contact,
1267 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1270 g_object_unref (dispatcher);