1 /* * Copyright (C) 2007-2008 Collabora Ltd.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include <glib/gi18n-lib.h>
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/proxy-subclass.h>
32 #include <libmissioncontrol/mission-control.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include <extensions/extensions.h>
37 #include "empathy-dispatcher.h"
38 #include "empathy-utils.h"
39 #include "empathy-tube-handler.h"
40 #include "empathy-account-manager.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-tp-group.h"
43 #include "empathy-tp-file.h"
44 #include "empathy-chatroom-manager.h"
45 #include "empathy-utils.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
48 #include <libempathy/empathy-debug.h>
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
52 EmpathyAccountManager *account_manager;
54 /* connection to connection data mapping */
55 GHashTable *connections;
56 /* accounts to connection mapping */
60 } EmpathyDispatcherPriv;
62 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
71 static guint signals[LAST_SIGNAL];
72 static EmpathyDispatcher *dispatcher = NULL;
75 EmpathyDispatcher *dispatcher;
76 EmpathyDispatchOperation *operation;
77 TpConnection *connection;
81 EmpathyContact *contact;
83 /* Properties to pass to the channel when requesting it */
85 EmpathyDispatcherRequestCb *cb;
87 gpointer *request_data;
88 } DispatcherRequestData;
92 /* Channel type specific wrapper object */
93 GObject *channel_wrapper;
98 /* ObjectPath => DispatchData.. */
99 GHashTable *dispatched_channels;
100 /* ObjectPath -> EmpathyDispatchOperations */
101 GHashTable *dispatching_channels;
102 /* ObjectPath -> EmpathyDispatchOperations */
103 GHashTable *outstanding_channels;
104 /* List of DispatcherRequestData */
105 GList *outstanding_requests;
108 static DispatchData *
109 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
111 DispatchData *d = g_slice_new0 (DispatchData);
112 d->channel = g_object_ref (channel);
113 d->channel_wrapper = g_object_ref (channel_wrapper);
119 free_dispatch_data (DispatchData *data)
121 g_object_unref (data->channel);
122 g_object_unref (data->channel_wrapper);
124 g_slice_free (DispatchData, data);
128 static DispatcherRequestData *
129 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
130 TpConnection *connection, const gchar *channel_type, guint handle_type,
131 guint handle, GHashTable *request,
132 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
134 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
136 result->dispatcher = dispatcher;
137 result->connection = connection;
139 result->channel_type = g_strdup (channel_type);
140 result->handle_type = handle_type;
141 result->handle = handle;
142 result->request = request;
145 result->contact = g_object_ref (contact);
148 result->user_data = user_data;
154 free_dispatcher_request_data (DispatcherRequestData *r)
156 g_free (r->channel_type);
158 if (r->contact != NULL)
159 g_object_unref (r->contact);
161 if (r->request != NULL)
162 g_hash_table_unref (r->request);
164 g_slice_free (DispatcherRequestData, r);
167 static ConnectionData *
168 new_connection_data (McAccount *account)
170 ConnectionData *cd = g_slice_new0 (ConnectionData);
171 cd->account = g_object_ref (account);
173 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
174 g_free, (GDestroyNotify) free_dispatch_data);
176 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
177 g_free, g_object_unref);
179 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
186 free_connection_data (ConnectionData *cd)
189 g_object_unref (cd->account);
190 g_hash_table_destroy (cd->dispatched_channels);
191 g_hash_table_destroy (cd->dispatching_channels);
193 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
195 free_dispatcher_request_data (l->data);
200 dispatcher_connection_invalidated_cb (TpConnection *connection,
201 guint domain, gint code, gchar *message,
202 EmpathyDispatcher *dispatcher)
204 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
207 DEBUG ("Error: %s", message);
208 cd = g_hash_table_lookup (priv->connections, connection);
210 g_hash_table_remove (priv->accounts, cd->account);
211 g_hash_table_remove (priv->connections, connection);
215 dispatcher_operation_can_start (EmpathyDispatcher *self,
216 EmpathyDispatchOperation *operation, ConnectionData *cd)
219 const gchar *channel_type =
220 empathy_dispatch_operation_get_channel_type (operation);
222 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
224 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
226 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
236 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
237 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
241 l = cd->outstanding_requests;
244 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
249 if (d->operation == operation)
254 d->cb (NULL, error, d->user_data);
256 d->cb (operation, NULL, d->user_data);
259 cd->outstanding_requests = g_list_delete_link
260 (cd->outstanding_requests, lt);
262 free_dispatcher_request_data (d);
268 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
269 gchar *message, EmpathyDispatcher *dispatcher)
271 /* Channel went away... */
272 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
273 TpConnection *connection;
274 EmpathyDispatchOperation *operation;
276 const gchar *object_path;
278 connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
280 cd = g_hash_table_lookup (priv->connections, connection);
281 /* Connection itself invalidated? */
285 object_path = tp_proxy_get_object_path (proxy);
287 DEBUG ("Channel %s invalidated", object_path);
289 g_hash_table_remove (cd->dispatched_channels, object_path);
290 g_hash_table_remove (cd->dispatching_channels, object_path);
292 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
293 if (operation != NULL)
295 GError error = { domain, code, message };
296 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
297 g_hash_table_remove (cd->outstanding_channels, object_path);
298 g_object_unref (operation);
303 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
304 EmpathyDispatcher *dispatcher)
306 g_assert (empathy_dispatch_operation_is_incoming (operation));
307 DEBUG ("Send of for dispatching: %s",
308 empathy_dispatch_operation_get_object_path (operation));
309 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
313 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
314 EmpathyDispatcher *dispatcher)
316 /* Our job is done, remove the dispatch operation and mark the channel as
318 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319 TpConnection *connection;
321 const gchar *object_path;
323 connection = empathy_dispatch_operation_get_tp_connection (operation);
324 cd = g_hash_table_lookup (priv->connections, connection);
325 g_assert (cd != NULL);
326 g_object_unref (G_OBJECT (connection));
328 object_path = empathy_dispatch_operation_get_object_path (operation);
330 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
333 d = new_dispatch_data (
334 empathy_dispatch_operation_get_channel (operation),
335 empathy_dispatch_operation_get_channel_wrapper (operation));
336 g_hash_table_insert (cd->dispatched_channels,
337 g_strdup (object_path), d);
339 g_hash_table_remove (cd->dispatching_channels, object_path);
341 DEBUG ("Channel claimed: %s", object_path);
345 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
346 EmpathyDispatcher *dispatcher)
348 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
349 TpConnection *connection;
351 EmpathyDispatchOperationState status;
353 g_signal_connect (operation, "approved",
354 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
356 g_signal_connect (operation, "claimed",
357 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
359 /* Signal the observers */
360 DEBUG ("Send to observers: %s",
361 empathy_dispatch_operation_get_object_path (operation));
362 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
364 empathy_dispatch_operation_start (operation);
366 /* Signal potential requestors */
367 connection = empathy_dispatch_operation_get_tp_connection (operation);
368 cd = g_hash_table_lookup (priv->connections, connection);
369 g_assert (cd != NULL);
370 g_object_unref (G_OBJECT (connection));
372 g_object_ref (operation);
374 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
375 status = empathy_dispatch_operation_get_status (operation);
376 g_object_unref (operation);
378 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
381 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
383 DEBUG ("Send to approvers: %s",
384 empathy_dispatch_operation_get_object_path (operation));
385 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
389 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
390 DEBUG ("Send of for dispatching: %s",
391 empathy_dispatch_operation_get_object_path (operation));
392 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
398 dispatcher_start_dispatching (EmpathyDispatcher *self,
399 EmpathyDispatchOperation *operation, ConnectionData *cd)
401 const gchar *object_path =
402 empathy_dispatch_operation_get_object_path (operation);
404 DEBUG ("Dispatching process started for %s", object_path);
406 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
408 g_assert (g_hash_table_lookup (cd->outstanding_channels,
409 object_path) == NULL);
411 g_hash_table_insert (cd->dispatching_channels,
412 g_strdup (object_path), operation);
414 switch (empathy_dispatch_operation_get_status (operation))
416 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
417 g_signal_connect (operation, "ready",
418 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
420 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
421 dispatch_operation_ready_cb (operation, dispatcher);
424 g_assert_not_reached();
428 else if (empathy_dispatch_operation_get_status (operation) >=
429 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
431 /* Already dispatching and the operation is pending, thus the observers
432 * have seen it (if applicable), so we can flush the request right away.
434 dispatch_operation_flush_requests (self, operation, NULL, cd);
440 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
446 g_hash_table_iter_init (&iter, cd->outstanding_channels);
447 while (g_hash_table_iter_next (&iter, NULL, &value))
449 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
451 if (dispatcher_operation_can_start (self, operation, cd))
453 dispatcher_start_dispatching (dispatcher, operation, cd);
454 g_hash_table_iter_remove (&iter);
461 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
462 TpConnection *connection,
463 const gchar *object_path, const gchar *channel_type,
464 guint handle_type, guint handle, GHashTable *properties,
467 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
470 EmpathyDispatchOperation *operation;
471 EmpathyContact *contact = NULL;
473 /* Channel types we never want to dispatch because they're either deprecated
474 * or can't sensibly be dispatch (e.g. channels that should always be
476 const char *blacklist[] = {
477 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
478 TP_IFACE_CHANNEL_TYPE_TUBES,
479 TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
483 cd = g_hash_table_lookup (priv->connections, connection);
485 /* Don't bother with channels we have already dispatched or are dispatching
486 * currently. This can happen when NewChannel(s) is fired after
487 * RequestChannel/CreateChannel/EnsureChannel */
488 if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
491 if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
494 /* Should never occur, but just in case a CM fires spurious NewChannel(s)
496 if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
499 /* Only pick up non-requested text channels. For all other it doesn't make
500 * sense to handle it if we didn't request it. The same goes for channels we
501 * discovered by the Channels property or ListChannels */
502 if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
504 DEBUG ("Ignoring incoming channel of type %s on %s",
505 channel_type, object_path);
509 for (i = 0 ; blacklist[i] != NULL; i++)
511 if (!tp_strdiff (channel_type, blacklist[i]))
513 DEBUG ("Ignoring blacklisted channel type %s on %s",
514 channel_type, object_path);
519 DEBUG ("New channel of type %s on %s", channel_type, object_path);
521 if (properties == NULL)
522 channel = tp_channel_new (connection, object_path, channel_type,
523 handle_type, handle, NULL);
525 channel = tp_channel_new_from_properties (connection, object_path,
528 g_signal_connect (channel, "invalidated",
529 G_CALLBACK (dispatcher_channel_invalidated_cb),
532 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
534 EmpathyContactFactory *factory = empathy_contact_factory_dup_singleton ();
535 contact = empathy_contact_factory_get_from_handle (factory,
536 cd->account, handle);
537 g_object_unref (factory);
540 operation = empathy_dispatch_operation_new (connection, channel, contact,
543 g_object_unref (channel);
547 /* Request could either be by us or by a remote party. If there are no
548 * outstanding requests for this channel type we can assume it's remote.
549 * Otherwise we wait untill they are all satisfied */
550 if (dispatcher_operation_can_start (dispatcher, operation, cd))
551 dispatcher_start_dispatching (dispatcher, operation, cd);
553 g_hash_table_insert (cd->outstanding_channels,
554 g_strdup (object_path), operation);
558 dispatcher_start_dispatching (dispatcher, operation, cd);
563 dispatcher_connection_new_channel_cb (TpConnection *connection,
564 const gchar *object_path, const gchar *channel_type,
565 guint handle_type, guint handle,
566 gboolean suppress_handler, gpointer user_data,
569 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
571 /* Empathy heavily abuses surpress handler (don't try this at home), if
572 * surpress handler is true then it is an outgoing channel, which is
573 * requested either by us or some other party (like the megaphone applet).
574 * Otherwise it's an incoming channel */
575 dispatcher_connection_new_channel (dispatcher, connection,
576 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
580 dispatcher_connection_new_channel_with_properties (
581 EmpathyDispatcher *dispatcher, TpConnection *connection,
582 const gchar *object_path, GHashTable *properties)
584 const gchar *channel_type;
591 channel_type = tp_asv_get_string (properties,
592 TP_IFACE_CHANNEL ".ChannelType");
593 if (channel_type == NULL)
595 g_message ("%s had an invalid ChannelType property", object_path);
599 handle_type = tp_asv_get_uint32 (properties,
600 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
603 g_message ("%s had an invalid TargetHandleType property", object_path);
607 handle = tp_asv_get_uint32 (properties,
608 TP_IFACE_CHANNEL ".TargetHandle", &valid);
611 g_message ("%s had an invalid TargetHandle property", object_path);
615 /* We assume there is no channel dispather, so we're the only one dispatching
616 * it. Which means that a requested channel it is outgoing one */
617 requested = tp_asv_get_boolean (properties,
618 TP_IFACE_CHANNEL ".Requested", &valid);
621 g_message ("%s had an invalid Requested property", object_path);
625 dispatcher_connection_new_channel (dispatcher, connection,
626 object_path, channel_type, handle_type, handle, properties, !requested);
631 dispatcher_connection_new_channels_cb (
632 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
635 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
638 for (i = 0; i < channels->len ; i++)
640 GValueArray *arr = g_ptr_array_index (channels, i);
641 const gchar *object_path;
642 GHashTable *properties;
644 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
645 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
647 dispatcher_connection_new_channel_with_properties (dispatcher,
648 connection, object_path, properties);
653 dispatcher_connection_got_channels_property (TpProxy *proxy,
654 const GValue *channels_prop, const GError *error, gpointer user_data,
660 DEBUG ("Error: %s", error->message);
664 channels = g_value_get_boxed (channels_prop);
665 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
666 channels, NULL, object);
670 dispatcher_connection_list_channels_cb (TpConnection *connection,
671 const GPtrArray *channels,
679 DEBUG ("Error: %s", error->message);
683 for (i = 0; i < channels->len; i++) {
686 values = g_ptr_array_index (channels, i);
687 /* We don't have any extra info, so assume already existing channels are
689 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
691 g_value_get_boxed (g_value_array_get_nth (values, 0)),
692 g_value_get_string (g_value_array_get_nth (values, 1)),
693 g_value_get_uint (g_value_array_get_nth (values, 2)),
694 g_value_get_uint (g_value_array_get_nth (values, 3)),
700 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
701 const GPtrArray *capabilities,
707 DEBUG ("Error: %s", error->message);
712 dispatcher_connection_ready_cb (TpConnection *connection,
713 const GError *error, gpointer dispatcher)
715 GPtrArray *capabilities;
718 const gchar *remove = NULL;
722 dispatcher_connection_invalidated_cb (connection, error->domain,
723 error->code, error->message, dispatcher);
727 g_signal_connect (connection, "invalidated",
728 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
731 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
732 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
734 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
735 dispatcher_connection_new_channels_cb,
736 NULL, NULL, G_OBJECT (dispatcher), NULL);
738 tp_cli_dbus_properties_call_get (connection, -1,
739 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
740 dispatcher_connection_got_channels_property,
741 NULL, NULL, dispatcher);
745 tp_cli_connection_connect_to_new_channel (connection,
746 dispatcher_connection_new_channel_cb,
747 NULL, NULL, G_OBJECT (dispatcher), NULL);
749 tp_cli_connection_call_list_channels (connection, -1,
750 dispatcher_connection_list_channels_cb, NULL, NULL,
751 G_OBJECT (dispatcher));
755 /* Advertise VoIP capabilities */
756 /* FIXME: Capabilities is leaked */
757 capabilities = g_ptr_array_sized_new (1);
758 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
759 G_TYPE_UINT, G_TYPE_INVALID);
760 g_value_init (&cap, cap_type);
761 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
762 dbus_g_type_struct_set (&cap,
763 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
764 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
765 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
766 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
767 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
768 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
770 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
771 connection, -1, capabilities, &remove,
772 dispatcher_connection_advertise_capabilities_cb,
773 NULL, NULL, G_OBJECT (dispatcher));
777 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
779 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
780 TpConnection *connection;
782 connection = g_hash_table_lookup (priv->accounts, account);
783 if (connection != NULL)
786 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
787 if (connection == NULL)
790 g_hash_table_insert (priv->connections, g_object_ref (connection),
791 new_connection_data (account));
793 g_hash_table_insert (priv->accounts, g_object_ref (account),
794 g_object_ref (connection));
796 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
801 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
802 McAccount *account, TpConnectionStatusReason reason,
803 TpConnectionStatus status, TpConnectionStatus previous,
804 EmpathyDispatcher *dispatcher)
806 dispatcher_update_account (dispatcher, account);
811 dispatcher_constructor (GType type, guint n_construct_params,
812 GObjectConstructParam *construct_params)
816 if (dispatcher == NULL)
818 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
819 (type, n_construct_params, construct_params);
821 dispatcher = EMPATHY_DISPATCHER (retval);
822 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
826 retval = g_object_ref (dispatcher);
833 dispatcher_finalize (GObject *object)
835 EmpathyDispatcherPriv *priv = GET_PRIV (object);
837 g_signal_handlers_disconnect_by_func (priv->account_manager,
838 dispatcher_account_connection_cb, object);
840 g_object_unref (priv->account_manager);
841 g_object_unref (priv->mc);
843 g_hash_table_destroy (priv->accounts);
844 g_hash_table_destroy (priv->connections);
848 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
850 GObjectClass *object_class = G_OBJECT_CLASS (klass);
852 object_class->finalize = dispatcher_finalize;
853 object_class->constructor = dispatcher_constructor;
856 g_signal_new ("observe",
857 G_TYPE_FROM_CLASS (klass),
861 g_cclosure_marshal_VOID__OBJECT,
863 1, EMPATHY_TYPE_DISPATCH_OPERATION);
866 g_signal_new ("approve",
867 G_TYPE_FROM_CLASS (klass),
871 g_cclosure_marshal_VOID__OBJECT,
873 1, EMPATHY_TYPE_DISPATCH_OPERATION);
876 g_signal_new ("dispatch",
877 G_TYPE_FROM_CLASS (klass),
881 g_cclosure_marshal_VOID__OBJECT,
883 1, EMPATHY_TYPE_DISPATCH_OPERATION);
885 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
890 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
893 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
894 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
896 dispatcher->priv = priv;
897 priv->mc = empathy_mission_control_new ();
898 priv->account_manager = empathy_account_manager_dup_singleton ();
900 g_signal_connect (priv->account_manager,
901 "account-connection-changed",
902 G_CALLBACK (dispatcher_account_connection_cb),
905 priv->accounts = g_hash_table_new_full (empathy_account_hash,
906 empathy_account_equal, g_object_unref, g_object_unref);
908 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
909 g_object_unref, (GDestroyNotify) free_connection_data);
911 accounts = mc_accounts_list_by_enabled (TRUE);
913 for (l = accounts; l; l = l->next) {
914 dispatcher_update_account (dispatcher, l->data);
915 g_object_unref (l->data);
917 g_list_free (accounts);
921 empathy_dispatcher_dup_singleton (void)
923 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
927 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
928 DispatcherRequestData *request_data, const GError *error)
930 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
931 ConnectionData *conn_data;
933 conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
934 if (request_data->cb != NULL)
935 request_data->cb (NULL, error, request_data->user_data);
937 conn_data->outstanding_requests =
938 g_list_remove (conn_data->outstanding_requests, request_data);
939 free_dispatcher_request_data (request_data);
943 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
944 DispatcherRequestData *request_data, const gchar *object_path,
945 GHashTable *properties, const GError *error)
947 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
948 EmpathyDispatchOperation *operation = NULL;
949 ConnectionData *conn_data;
951 conn_data = g_hash_table_lookup (priv->connections,
952 request_data->connection);
956 DEBUG ("Channel request failed: %s", error->message);
958 dispatcher_request_failed (dispatcher, request_data, error);
963 operation = g_hash_table_lookup (conn_data->outstanding_channels,
966 if (operation != NULL)
967 g_hash_table_remove (conn_data->outstanding_channels, object_path);
969 operation = g_hash_table_lookup (conn_data->dispatching_channels,
972 if (operation == NULL)
974 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
979 operation = empathy_dispatch_operation_new_with_wrapper (
980 request_data->connection,
981 data->channel, request_data->contact, FALSE,
982 data->channel_wrapper);
988 if (properties != NULL)
989 channel = tp_channel_new_from_properties (request_data->connection,
990 object_path, properties, NULL);
992 channel = tp_channel_new (request_data->connection, object_path,
993 request_data->channel_type, request_data->handle_type,
994 request_data->handle, NULL);
996 g_signal_connect (channel, "invalidated",
997 G_CALLBACK (dispatcher_channel_invalidated_cb),
998 request_data->dispatcher);
1000 operation = empathy_dispatch_operation_new (request_data->connection,
1001 channel, request_data->contact, FALSE);
1002 g_object_unref (channel);
1007 /* Already existed set potential extra information */
1008 g_object_set (G_OBJECT (operation),
1009 "contact", request_data->contact,
1013 request_data->operation = operation;
1015 /* (pre)-approve this right away as we requested it */
1016 empathy_dispatch_operation_approve (operation);
1018 dispatcher_start_dispatching (request_data->dispatcher, operation,
1021 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1026 dispatcher_request_channel_cb (TpConnection *connection,
1027 const gchar *object_path, const GError *error,
1028 gpointer user_data, GObject *weak_object)
1030 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1031 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1033 dispatcher_connection_new_requested_channel (dispatcher,
1034 request_data, object_path, NULL, error);
1038 dispatcher_request_channel (DispatcherRequestData *request_data)
1040 tp_cli_connection_call_request_channel (request_data->connection, -1,
1041 request_data->channel_type,
1042 request_data->handle_type,
1043 request_data->handle,
1044 TRUE, dispatcher_request_channel_cb,
1045 request_data, NULL, G_OBJECT (request_data->dispatcher));
1049 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1050 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1052 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1053 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1055 TpConnection *connection;
1057 DispatcherRequestData *request_data;
1059 account = empathy_contact_get_account (contact);
1060 connection = g_hash_table_lookup (priv->accounts, account);
1062 g_assert (connection != NULL);
1063 cd = g_hash_table_lookup (priv->connections, connection);
1064 request_data = new_dispatcher_request_data (dispatcher, connection,
1065 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1066 contact, callback, user_data);
1068 cd->outstanding_requests = g_list_prepend
1069 (cd->outstanding_requests, request_data);
1071 dispatcher_request_channel (request_data);
1073 g_object_unref (dispatcher);
1078 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1079 const GError *error, gpointer user_data, GObject *object)
1081 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1083 request_data->handle = empathy_contact_get_handle (contact);
1085 dispatcher_request_channel (request_data);
1089 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1090 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1092 EmpathyDispatcher *dispatcher;
1093 EmpathyDispatcherPriv *priv;
1095 TpConnection *connection;
1096 ConnectionData *connection_data;
1097 DispatcherRequestData *request_data;
1099 dispatcher = empathy_dispatcher_dup_singleton();
1100 priv = GET_PRIV (dispatcher);
1102 account = empathy_contact_get_account (contact);
1103 connection = g_hash_table_lookup (priv->accounts, account);
1104 connection_data = g_hash_table_lookup (priv->connections, connection);
1106 /* The contact handle might not be known yet */
1107 request_data = new_dispatcher_request_data (dispatcher, connection,
1108 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1109 contact, callback, user_data);
1111 connection_data->outstanding_requests = g_list_prepend
1112 (connection_data->outstanding_requests, request_data);
1114 empathy_contact_call_when_ready (contact,
1115 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1116 request_data, NULL, G_OBJECT (dispatcher));
1118 g_object_unref (dispatcher);
1122 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1123 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1125 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1126 EmpathyContactFactory *factory;
1127 EmpathyContact *contact;
1129 factory = empathy_contact_factory_dup_singleton ();
1130 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1132 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1134 g_object_unref (contact);
1135 g_object_unref (factory);
1136 g_object_unref (dispatcher);
1141 dispatcher_request_handles_cb (TpConnection *connection,
1142 const GArray *handles, const GError *error, gpointer user_data,
1145 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1149 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1150 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1153 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1155 if (request_data->cb)
1156 request_data->cb (NULL, error, request_data->user_data);
1158 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1161 free_dispatcher_request_data (request_data);
1163 dispatcher_flush_outstanding_operations (dispatcher, cd);
1167 request_data->handle = g_array_index (handles, guint, 0);
1168 dispatcher_request_channel (request_data);
1172 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1173 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1175 EmpathyDispatcher *dispatcher;
1176 EmpathyDispatcherPriv *priv;
1177 DispatcherRequestData *request_data;
1178 TpConnection *connection;
1179 ConnectionData *connection_data;
1180 const gchar *names[] = { roomname, NULL };
1182 dispatcher = empathy_dispatcher_dup_singleton();
1183 priv = GET_PRIV (dispatcher);
1185 connection = g_hash_table_lookup (priv->accounts, account);
1186 connection_data = g_hash_table_lookup (priv->connections, connection);
1189 /* Don't know the room handle yet */
1190 request_data = new_dispatcher_request_data (dispatcher, connection,
1191 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1192 NULL, callback, user_data);
1194 connection_data->outstanding_requests = g_list_prepend
1195 (connection_data->outstanding_requests, request_data);
1197 tp_cli_connection_call_request_handles (connection, -1,
1198 TP_HANDLE_TYPE_ROOM, names,
1199 dispatcher_request_handles_cb, request_data, NULL,
1200 G_OBJECT (dispatcher));
1202 g_object_unref (dispatcher);
1206 dispatcher_create_channel_cb (TpConnection *connect,
1207 const gchar *object_path, GHashTable *properties, const GError *error,
1208 gpointer user_data, GObject *weak_object)
1210 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1211 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1213 dispatcher_connection_new_requested_channel (dispatcher,
1214 request_data, object_path, properties, error);
1219 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1220 const GError *error, gpointer user_data, GObject *object)
1222 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1223 GValue *target_handle;
1225 g_assert (request_data->request);
1229 dispatcher_request_failed (request_data->dispatcher,
1230 request_data, error);
1234 request_data->handle = empathy_contact_get_handle (contact);
1236 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1237 g_value_set_uint (target_handle, request_data->handle);
1238 g_hash_table_insert (request_data->request,
1239 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1241 tp_cli_connection_interface_requests_call_create_channel (
1242 request_data->connection, -1,
1243 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1244 G_OBJECT (request_data->dispatcher));
1248 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1249 const gchar *filename, guint64 size, guint64 date,
1250 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1253 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1254 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1255 McAccount *account = empathy_contact_get_account (contact);
1256 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1257 ConnectionData *connection_data =
1258 g_hash_table_lookup (priv->connections, connection);
1259 DispatcherRequestData *request_data;
1261 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1262 (GDestroyNotify) tp_g_value_slice_free);
1264 /* org.freedesktop.Telepathy.Channel.ChannelType */
1265 value = tp_g_value_slice_new (G_TYPE_STRING);
1266 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1267 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1269 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1270 value = tp_g_value_slice_new (G_TYPE_UINT);
1271 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1272 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1274 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1275 value = tp_g_value_slice_new (G_TYPE_STRING);
1276 g_value_set_string (value, content_type);
1277 g_hash_table_insert (request,
1278 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1280 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1281 value = tp_g_value_slice_new (G_TYPE_STRING);
1282 g_value_set_string (value, filename);
1283 g_hash_table_insert (request,
1284 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1286 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1287 value = tp_g_value_slice_new (G_TYPE_UINT64);
1288 g_value_set_uint64 (value, size);
1289 g_hash_table_insert (request,
1290 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1292 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1293 value = tp_g_value_slice_new (G_TYPE_UINT64);
1294 g_value_set_uint64 (value, date);
1295 g_hash_table_insert (request,
1296 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1299 /* The contact handle might not be known yet */
1300 request_data = new_dispatcher_request_data (dispatcher, connection,
1301 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1302 contact, callback, user_data);
1303 connection_data->outstanding_requests = g_list_prepend
1304 (connection_data->outstanding_requests, request_data);
1306 empathy_contact_call_when_ready (contact,
1307 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1308 request_data, NULL, G_OBJECT (dispatcher));
1310 g_object_unref (dispatcher);