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 EmpathyChatroomManager *chatroom_mgr;
61 } EmpathyDispatcherPriv;
63 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
72 static guint signals[LAST_SIGNAL];
73 static EmpathyDispatcher *dispatcher = NULL;
76 EmpathyDispatcherTube public;
77 EmpathyContactFactory *factory;
85 EmpathyDispatcher *dispatcher;
86 EmpathyDispatchOperation *operation;
87 TpConnection *connection;
91 EmpathyContact *contact;
93 /* Properties to pass to the channel when requesting it */
95 EmpathyDispatcherRequestCb *cb;
97 gpointer *request_data;
98 } DispatcherRequestData;
102 /* Channel type specific wrapper object */
103 GObject *channel_wrapper;
108 /* ObjectPath => DispatchData.. */
109 GHashTable *dispatched_channels;
110 /* ObjectPath -> EmpathyDispatchOperations */
111 GHashTable *dispatching_channels;
112 /* ObjectPath -> EmpathyDispatchOperations */
113 GHashTable *outstanding_channels;
114 /* List of DispatcherRequestData */
115 GList *outstanding_requests;
118 static DispatchData *
119 new_dispatch_data (TpChannel *channel, GObject *channel_wrapper)
121 DispatchData *d = g_slice_new0 (DispatchData);
122 d->channel = channel;
123 d->channel_wrapper = channel_wrapper;
129 free_dispatch_data (DispatchData *data)
131 g_object_unref (data->channel);
132 g_object_unref (data->channel_wrapper);
134 g_slice_free (DispatchData, data);
138 static DispatcherRequestData *
139 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
140 TpConnection *connection, const gchar *channel_type, guint handle_type,
141 guint handle, GHashTable *request,
142 EmpathyContact *contact, EmpathyDispatcherRequestCb *cb, gpointer user_data)
144 DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
146 result->dispatcher = dispatcher;
147 result->connection = connection;
149 result->channel_type = g_strdup (channel_type);
150 result->handle_type = handle_type;
151 result->handle = handle;
152 result->request = request;
155 result->contact = g_object_ref (contact);
158 result->user_data = user_data;
164 free_dispatcher_request_data (DispatcherRequestData *r)
166 g_free (r->channel_type);
168 if (r->contact != NULL)
169 g_object_unref (r->contact);
171 if (r->request != NULL)
172 g_hash_table_unref (r->request);
174 g_slice_free (DispatcherRequestData, r);
177 static ConnectionData *
178 new_connection_data (McAccount *account)
180 ConnectionData *cd = g_slice_new0 (ConnectionData);
181 cd->account = g_object_ref (account);
183 cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
184 g_free, (GDestroyNotify) free_dispatch_data);
186 cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
187 g_free, g_object_unref);
189 cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
196 free_connection_data (ConnectionData *cd)
199 g_object_unref (cd->account);
200 g_hash_table_destroy (cd->dispatched_channels);
201 g_hash_table_destroy (cd->dispatching_channels);
203 for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
205 free_dispatcher_request_data (l->data);
210 dispatcher_connection_invalidated_cb (TpConnection *connection,
211 guint domain, gint code, gchar *message,
212 EmpathyDispatcher *dispatcher)
214 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
217 DEBUG ("Error: %s", message);
218 cd = g_hash_table_lookup (priv->connections, connection);
220 g_hash_table_remove (priv->accounts, cd->account);
221 g_hash_table_remove (priv->connections, connection);
225 dispatcher_operation_can_start (EmpathyDispatcher *self,
226 EmpathyDispatchOperation *operation, ConnectionData *cd)
229 const gchar *channel_type =
230 empathy_dispatch_operation_get_channel_type (operation);
232 for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
234 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
236 if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
246 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
247 EmpathyDispatchOperation *operation, GError *error, ConnectionData *cd)
251 l = cd->outstanding_requests;
254 DispatcherRequestData *d = (DispatcherRequestData *) l->data;
259 if (d->operation == operation)
264 d->cb (NULL, error, d->user_data);
266 d->cb (operation, NULL, d->user_data);
269 cd->outstanding_requests = g_list_delete_link
270 (cd->outstanding_requests, lt);
272 free_dispatcher_request_data (d);
278 dispatcher_channel_invalidated_cb (TpProxy *proxy, guint domain, gint code,
279 gchar *message, EmpathyDispatcher *dispatcher)
281 /* Channel went away... */
282 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
283 TpConnection *connection;
284 EmpathyDispatchOperation *operation;
286 const gchar *object_path;
288 g_object_get (G_OBJECT (proxy), "connection", &connection, NULL);
290 cd = g_hash_table_lookup (priv->connections, connection);
291 /* Connection itself invalidated? */
295 object_path = tp_proxy_get_object_path (proxy);
297 DEBUG ("Channel %s invalidated", object_path);
299 g_hash_table_remove (cd->dispatched_channels, object_path);
300 g_hash_table_remove (cd->dispatching_channels, object_path);
302 operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
303 if (operation != NULL)
305 GError error = { domain, code, message };
306 dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
307 g_hash_table_remove (cd->outstanding_channels, object_path);
308 g_object_unref (operation);
313 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
314 EmpathyDispatcher *dispatcher)
316 g_assert (empathy_dispatch_operation_is_incoming (operation));
317 DEBUG ("Send of for dispatching: %s",
318 empathy_dispatch_operation_get_object_path (operation));
319 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
323 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
324 EmpathyDispatcher *dispatcher)
326 /* Our job is done, remove the dispatch operation and mark the channel as
328 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
329 TpConnection *connection;
331 const gchar *object_path;
333 connection = empathy_dispatch_operation_get_tp_connection (operation);
334 cd = g_hash_table_lookup (priv->connections, connection);
335 g_assert (cd != NULL);
336 g_object_unref (G_OBJECT (connection));
338 object_path = empathy_dispatch_operation_get_object_path (operation);
340 if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
343 d = new_dispatch_data (
344 empathy_dispatch_operation_get_channel (operation),
345 empathy_dispatch_operation_get_channel_wrapper (operation));
346 g_hash_table_insert (cd->dispatched_channels,
347 g_strdup (object_path), d);
349 g_hash_table_remove (cd->dispatching_channels, object_path);
351 DEBUG ("Channel claimed: %s", object_path);
355 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
356 EmpathyDispatcher *dispatcher)
358 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
359 TpConnection *connection;
361 EmpathyDispatchOperationState status;
363 g_signal_connect (operation, "approved",
364 G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
366 g_signal_connect (operation, "claimed",
367 G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
369 /* Signal the observers */
370 DEBUG ("Send to observers: %s",
371 empathy_dispatch_operation_get_object_path (operation));
372 g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
374 empathy_dispatch_operation_start (operation);
376 /* Signal potential requestors */
377 connection = empathy_dispatch_operation_get_tp_connection (operation);
378 cd = g_hash_table_lookup (priv->connections, connection);
379 g_assert (cd != NULL);
380 g_object_unref (G_OBJECT (connection));
382 g_object_ref (operation);
384 dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
385 status = empathy_dispatch_operation_get_status (operation);
386 g_object_unref (operation);
388 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
391 if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
393 DEBUG ("Send to approvers: %s",
394 empathy_dispatch_operation_get_object_path (operation));
395 g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
399 g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
400 DEBUG ("Send of for dispatching: %s",
401 empathy_dispatch_operation_get_object_path (operation));
402 g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
408 dispatcher_start_dispatching (EmpathyDispatcher *self,
409 EmpathyDispatchOperation *operation, ConnectionData *cd)
411 const gchar *object_path =
412 empathy_dispatch_operation_get_object_path (operation);
414 DEBUG ("Dispatching process started for %s", object_path);
416 if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
418 g_assert (g_hash_table_lookup (cd->outstanding_channels,
419 object_path) == NULL);
421 g_hash_table_insert (cd->dispatching_channels,
422 g_strdup (object_path), operation);
424 switch (empathy_dispatch_operation_get_status (operation))
426 case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
427 g_signal_connect (operation, "ready",
428 G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
430 case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
431 dispatch_operation_ready_cb (operation, dispatcher);
434 g_assert_not_reached();
438 else if (empathy_dispatch_operation_get_status (operation) >=
439 EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
441 /* Already dispatching and the operation is pending, thus the observers
442 * have seen it (if applicable), so we can flush the request right away.
444 dispatch_operation_flush_requests (self, operation, NULL, cd);
450 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
456 g_hash_table_iter_init (&iter, cd->outstanding_channels);
457 while (g_hash_table_iter_next (&iter, NULL, &value))
459 EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
461 if (dispatcher_operation_can_start (self, operation, cd))
463 dispatcher_start_dispatching (dispatcher, operation, cd);
464 g_hash_table_iter_remove (&iter);
471 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
472 TpConnection *connection,
473 const gchar *object_path, const gchar *channel_type,
474 guint handle_type, guint handle, GHashTable *properties,
477 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
480 EmpathyDispatchOperation *operation;
481 EmpathyContact *contact = NULL;
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 DEBUG ("New channel of type %s on %s",
500 channel_type, object_path);
502 if (properties == NULL)
503 channel = tp_channel_new (connection, object_path, channel_type,
504 handle_type, handle, NULL);
506 channel = tp_channel_new_from_properties (connection, object_path,
509 g_signal_connect (channel, "invalidated",
510 G_CALLBACK (dispatcher_channel_invalidated_cb),
513 if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
515 EmpathyContactFactory *factory = empathy_contact_factory_new ();
516 contact = empathy_contact_factory_get_from_handle (factory,
517 cd->account, handle);
518 g_object_unref (factory);
521 operation = empathy_dispatch_operation_new (connection, channel, contact,
524 g_object_unref (channel);
528 /* Request could either be by us or by a remote party. If there are no
529 * outstanding requests for this channel type we can assume it's remote.
530 * Otherwise we wait untill they are all satisfied */
531 if (dispatcher_operation_can_start (dispatcher, operation, cd))
532 dispatcher_start_dispatching (dispatcher, operation, cd);
534 g_hash_table_insert (cd->outstanding_channels,
535 g_strdup (object_path), operation);
539 dispatcher_start_dispatching (dispatcher, operation, cd);
544 dispatcher_connection_new_channel_cb (TpConnection *connection,
545 const gchar *object_path, const gchar *channel_type,
546 guint handle_type, guint handle,
547 gboolean suppress_handler, gpointer user_data,
550 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
552 /* Empathy heavily abuses surpress handler (don't try this at home), if
553 * surpress handler is true then it is an outgoing channel, which is
554 * requested either by us or some other party (like the megaphone applet).
555 * Otherwise it's an incoming channel */
556 dispatcher_connection_new_channel (dispatcher, connection,
557 object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
561 dispatcher_connection_new_channel_with_properties (
562 EmpathyDispatcher *dispatcher, TpConnection *connection,
563 const gchar *object_path, GHashTable *properties)
565 const gchar *channel_type;
572 channel_type = tp_asv_get_string (properties,
573 TP_IFACE_CHANNEL ".ChannelType");
574 if (channel_type == NULL)
576 g_message ("%s had an invalid ChannelType property", object_path);
580 handle_type = tp_asv_get_uint32 (properties,
581 TP_IFACE_CHANNEL ".TargetHandleType", &valid);
584 g_message ("%s had an invalid TargetHandleType property", object_path);
588 handle = tp_asv_get_uint32 (properties,
589 TP_IFACE_CHANNEL ".TargetHandle", &valid);
592 g_message ("%s had an invalid TargetHandle property", object_path);
596 /* We assume there is no channel dispather, so we're the only one dispatching
597 * it. Which means that a requested channel it is outgoing one */
598 requested = tp_asv_get_boolean (properties,
599 TP_IFACE_CHANNEL ".Requested", &valid);
602 g_message ("%s had an invalid Requested property", object_path);
606 dispatcher_connection_new_channel (dispatcher, connection,
607 object_path, channel_type, handle_type, handle, properties, !requested);
612 dispatcher_connection_new_channels_cb (
613 TpConnection *connection, const GPtrArray *channels, gpointer user_data,
616 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
619 for (i = 0; i < channels->len ; i++)
621 GValueArray *arr = g_ptr_array_index (channels, i);
622 const gchar *object_path;
623 GHashTable *properties;
625 object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
626 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
628 dispatcher_connection_new_channel_with_properties (dispatcher,
629 connection, object_path, properties);
634 dispatcher_connection_got_channels_property (TpProxy *proxy,
635 const GValue *channels_prop, const GError *error, gpointer user_data,
641 DEBUG ("Error: %s", error->message);
645 channels = g_value_get_boxed (channels_prop);
646 dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
647 channels, NULL, object);
651 dispatcher_connection_list_channels_cb (TpConnection *connection,
652 const GPtrArray *channels,
660 DEBUG ("Error: %s", error->message);
664 for (i = 0; i < channels->len; i++) {
667 values = g_ptr_array_index (channels, i);
668 /* We don't have any extra info, so assume already existing channels are
670 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
672 g_value_get_boxed (g_value_array_get_nth (values, 0)),
673 g_value_get_string (g_value_array_get_nth (values, 1)),
674 g_value_get_uint (g_value_array_get_nth (values, 2)),
675 g_value_get_uint (g_value_array_get_nth (values, 3)),
681 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
682 const GPtrArray *capabilities,
688 DEBUG ("Error: %s", error->message);
693 dispatcher_connection_ready_cb (TpConnection *connection,
694 const GError *error, gpointer dispatcher)
696 GPtrArray *capabilities;
699 const gchar *remove = NULL;
703 dispatcher_connection_invalidated_cb (connection, error->domain,
704 error->code, error->message, dispatcher);
708 g_signal_connect (connection, "invalidated",
709 G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
712 if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
713 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
715 tp_cli_connection_interface_requests_connect_to_new_channels (connection,
716 dispatcher_connection_new_channels_cb,
717 NULL, NULL, G_OBJECT (dispatcher), NULL);
719 tp_cli_dbus_properties_call_get (connection, -1,
720 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
721 dispatcher_connection_got_channels_property,
722 NULL, NULL, dispatcher);
726 tp_cli_connection_connect_to_new_channel (connection,
727 dispatcher_connection_new_channel_cb,
728 NULL, NULL, G_OBJECT (dispatcher), NULL);
730 tp_cli_connection_call_list_channels (connection, -1,
731 dispatcher_connection_list_channels_cb, NULL, NULL,
732 G_OBJECT (dispatcher));
736 /* Advertise VoIP capabilities */
737 capabilities = g_ptr_array_sized_new (1);
738 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
739 G_TYPE_UINT, G_TYPE_INVALID);
740 g_value_init (&cap, cap_type);
741 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
742 dbus_g_type_struct_set (&cap,
743 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
744 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
745 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
746 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
747 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
748 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
750 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
751 connection, -1, capabilities, &remove,
752 dispatcher_connection_advertise_capabilities_cb,
753 NULL, NULL, G_OBJECT (dispatcher));
757 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
759 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
760 TpConnection *connection;
762 connection = g_hash_table_lookup (priv->accounts, account);
763 if (connection != NULL)
766 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
767 if (connection == NULL)
770 g_hash_table_insert (priv->connections, g_object_ref (connection),
771 new_connection_data (account));
773 g_hash_table_insert (priv->accounts, g_object_ref (account),
774 g_object_ref (connection));
776 tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
781 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
782 McAccount *account, TpConnectionStatusReason reason,
783 TpConnectionStatus status, TpConnectionStatus previous,
784 EmpathyDispatcher *dispatcher)
786 dispatcher_update_account (dispatcher, account);
790 dispatcher_finalize (GObject *object)
792 EmpathyDispatcherPriv *priv = GET_PRIV (object);
794 g_signal_handlers_disconnect_by_func (priv->account_manager,
795 dispatcher_account_connection_cb, object);
797 g_object_unref (priv->account_manager);
798 g_object_unref (priv->mc);
800 g_hash_table_destroy (priv->connections);
802 g_object_unref (priv->chatroom_mgr);
806 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
808 GObjectClass *object_class = G_OBJECT_CLASS (klass);
810 object_class->finalize = dispatcher_finalize;
813 g_signal_new ("observe",
814 G_TYPE_FROM_CLASS (klass),
818 g_cclosure_marshal_VOID__OBJECT,
820 1, EMPATHY_TYPE_DISPATCH_OPERATION);
823 g_signal_new ("approve",
824 G_TYPE_FROM_CLASS (klass),
828 g_cclosure_marshal_VOID__OBJECT,
830 1, EMPATHY_TYPE_DISPATCH_OPERATION);
833 g_signal_new ("dispatch",
834 G_TYPE_FROM_CLASS (klass),
838 g_cclosure_marshal_VOID__OBJECT,
840 1, EMPATHY_TYPE_DISPATCH_OPERATION);
842 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
847 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
850 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
851 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
853 dispatcher->priv = priv;
854 priv->mc = empathy_mission_control_new ();
855 priv->account_manager = empathy_account_manager_dup_singleton ();
857 g_signal_connect (priv->account_manager,
858 "account-connection-changed",
859 G_CALLBACK (dispatcher_account_connection_cb),
862 priv->accounts = g_hash_table_new_full (empathy_account_hash,
863 empathy_account_equal, g_object_unref, g_object_unref);
865 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
866 g_object_unref, (GDestroyNotify) free_connection_data);
868 accounts = mc_accounts_list_by_enabled (TRUE);
870 for (l = accounts; l; l = l->next) {
871 dispatcher_update_account (dispatcher, l->data);
872 g_object_unref (l->data);
874 g_list_free (accounts);
876 /* FIXME thisshould probably be created by another object.. */
877 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
878 empathy_chatroom_manager_observe (priv->chatroom_mgr, dispatcher);
882 empathy_get_dispatcher (void)
885 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
886 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
888 g_object_ref (dispatcher);
895 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
896 DispatcherRequestData *request_data, const gchar *object_path,
897 GHashTable *properties, const GError *error)
899 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
900 EmpathyDispatchOperation *operation = NULL;
901 ConnectionData *conn_data;
903 conn_data = g_hash_table_lookup (priv->connections,
904 request_data->connection);
908 DEBUG ("Channel request failed: %s", error->message);
910 if (request_data->cb != NULL)
911 request_data->cb (NULL, error, request_data->user_data);
913 conn_data->outstanding_requests =
914 g_list_remove (conn_data->outstanding_requests, request_data);
915 free_dispatcher_request_data (request_data);
920 operation = g_hash_table_lookup (conn_data->outstanding_channels,
923 if (operation != NULL)
924 g_hash_table_remove (conn_data->outstanding_channels, object_path);
926 operation = g_hash_table_lookup (conn_data->dispatching_channels,
929 if (operation == NULL)
931 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
936 operation = empathy_dispatch_operation_new_with_wrapper (
937 request_data->connection,
938 data->channel, request_data->contact, FALSE,
939 data->channel_wrapper);
945 if (properties != NULL)
946 channel = tp_channel_new_from_properties (request_data->connection,
947 object_path, properties, NULL);
949 channel = tp_channel_new (request_data->connection, object_path,
950 request_data->channel_type, request_data->handle_type,
951 request_data->handle, NULL);
953 g_signal_connect (channel, "invalidated",
954 G_CALLBACK (dispatcher_channel_invalidated_cb),
955 request_data->dispatcher);
957 operation = empathy_dispatch_operation_new (request_data->connection,
958 channel, request_data->contact, FALSE);
959 g_object_unref (channel);
964 /* Already existed set potential extra information */
965 g_object_set (G_OBJECT (operation),
966 "contact", request_data->contact,
970 request_data->operation = operation;
972 /* (pre)-approve this right away as we requested it */
973 empathy_dispatch_operation_approve (operation);
975 dispatcher_start_dispatching (request_data->dispatcher, operation,
978 dispatcher_flush_outstanding_operations (request_data->dispatcher,
983 dispatcher_request_channel_cb (TpConnection *connection,
984 const gchar *object_path, const GError *error,
985 gpointer user_data, GObject *weak_object)
987 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
988 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
990 dispatcher_connection_new_requested_channel (dispatcher,
991 request_data, object_path, NULL, error);
995 dispatcher_request_channel (DispatcherRequestData *request_data)
997 tp_cli_connection_call_request_channel (request_data->connection, -1,
998 request_data->channel_type,
999 request_data->handle_type,
1000 request_data->handle,
1001 TRUE, dispatcher_request_channel_cb,
1002 request_data, NULL, G_OBJECT (request_data->dispatcher));
1006 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1007 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1009 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1010 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1012 TpConnection *connection;
1014 DispatcherRequestData *request_data;
1016 account = empathy_contact_get_account (contact);
1017 connection = g_hash_table_lookup (priv->accounts, account);
1019 g_assert (connection != NULL);
1020 cd = g_hash_table_lookup (priv->connections, connection);
1021 request_data = new_dispatcher_request_data (dispatcher, connection,
1022 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1023 contact, callback, user_data);
1025 cd->outstanding_requests = g_list_prepend
1026 (cd->outstanding_requests, request_data);
1028 dispatcher_request_channel (request_data);
1030 g_object_unref (dispatcher);
1035 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1037 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1039 request_data->handle = empathy_contact_get_handle (contact);
1041 dispatcher_request_channel (request_data);
1045 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1046 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1048 EmpathyDispatcher *dispatcher;
1049 EmpathyDispatcherPriv *priv;
1051 TpConnection *connection;
1052 ConnectionData *connection_data;
1053 DispatcherRequestData *request_data;
1055 dispatcher = empathy_get_dispatcher();
1056 priv = GET_PRIV (dispatcher);
1058 account = empathy_contact_get_account (contact);
1059 connection = g_hash_table_lookup (priv->accounts, account);
1060 connection_data = g_hash_table_lookup (priv->connections, connection);
1062 /* The contact handle might not be known yet */
1063 request_data = new_dispatcher_request_data (dispatcher, connection,
1064 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1065 contact, callback, user_data);
1067 connection_data->outstanding_requests = g_list_prepend
1068 (connection_data->outstanding_requests, request_data);
1070 empathy_contact_call_when_ready (contact,
1071 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1074 g_object_unref (dispatcher);
1078 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1079 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1081 EmpathyDispatcher *dispatcher = empathy_get_dispatcher ();
1082 EmpathyContactFactory *factory;
1083 EmpathyContact *contact;
1085 factory = empathy_contact_factory_dup_singleton ();
1086 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1088 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1090 g_object_unref (contact);
1091 g_object_unref (factory);
1092 g_object_unref (dispatcher);
1097 dispatcher_request_handles_cb (TpConnection *connection,
1098 const GArray *handles, const GError *error, gpointer user_data,
1101 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1105 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1106 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1109 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1111 if (request_data->cb)
1112 request_data->cb (NULL, error, request_data->user_data);
1114 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1117 free_dispatcher_request_data (request_data);
1119 dispatcher_flush_outstanding_operations (dispatcher, cd);
1123 request_data->handle = g_array_index (handles, guint, 0);
1124 dispatcher_request_channel (request_data);
1128 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1129 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1131 EmpathyDispatcher *dispatcher;
1132 EmpathyDispatcherPriv *priv;
1133 DispatcherRequestData *request_data;
1134 TpConnection *connection;
1135 ConnectionData *connection_data;
1136 const gchar *names[] = { roomname, NULL };
1138 dispatcher = empathy_get_dispatcher();
1139 priv = GET_PRIV (dispatcher);
1141 connection = g_hash_table_lookup (priv->accounts, account);
1142 connection_data = g_hash_table_lookup (priv->connections, connection);
1145 /* Don't know the room handle yet */
1146 request_data = new_dispatcher_request_data (dispatcher, connection,
1147 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1148 NULL, callback, user_data);
1150 connection_data->outstanding_requests = g_list_prepend
1151 (connection_data->outstanding_requests, request_data);
1153 tp_cli_connection_call_request_handles (connection, -1,
1154 TP_HANDLE_TYPE_ROOM, names,
1155 dispatcher_request_handles_cb, request_data, NULL,
1156 G_OBJECT (dispatcher));
1158 g_object_unref (dispatcher);
1162 dispatcher_create_channel_cb (TpConnection *connect,
1163 const gchar *object_path, GHashTable *properties, const GError *error,
1164 gpointer user_data, GObject *weak_object)
1166 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1167 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1169 dispatcher_connection_new_requested_channel (dispatcher,
1170 request_data, object_path, properties, error);
1175 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1178 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1179 GValue *target_handle;
1181 g_assert (request_data->request);
1183 request_data->handle = empathy_contact_get_handle (contact);
1185 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1186 g_value_set_uint (target_handle, request_data->handle);
1187 g_hash_table_insert (request_data->request,
1188 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1190 tp_cli_connection_interface_requests_call_create_channel (
1191 request_data->connection, -1,
1192 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1193 G_OBJECT (request_data->dispatcher));
1197 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1198 const gchar *filename, guint64 size, guint64 date,
1199 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1202 EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1203 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1204 McAccount *account = empathy_contact_get_account (contact);
1205 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1206 ConnectionData *connection_data =
1207 g_hash_table_lookup (priv->connections, connection);
1208 DispatcherRequestData *request_data;
1210 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1211 (GDestroyNotify) tp_g_value_slice_free);
1213 /* org.freedesktop.Telepathy.Channel.ChannelType */
1214 value = tp_g_value_slice_new (G_TYPE_STRING);
1215 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1216 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1218 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1219 value = tp_g_value_slice_new (G_TYPE_UINT);
1220 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1221 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1223 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1224 value = tp_g_value_slice_new (G_TYPE_STRING);
1225 g_value_set_string (value, content_type);
1226 g_hash_table_insert (request,
1227 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1229 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1230 value = tp_g_value_slice_new (G_TYPE_STRING);
1231 g_value_set_string (value, filename);
1232 g_hash_table_insert (request,
1233 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1235 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1236 value = tp_g_value_slice_new (G_TYPE_UINT64);
1237 g_value_set_uint64 (value, size);
1238 g_hash_table_insert (request,
1239 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1241 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1242 value = tp_g_value_slice_new (G_TYPE_UINT64);
1243 g_value_set_uint64 (value, date);
1244 g_hash_table_insert (request,
1245 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1248 /* The contact handle might not be known yet */
1249 request_data = new_dispatcher_request_data (dispatcher, connection,
1250 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1251 contact, callback, user_data);
1252 connection_data->outstanding_requests = g_list_prepend
1253 (connection_data->outstanding_requests, request_data);
1255 empathy_contact_call_when_ready (contact,
1256 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1259 g_object_unref (dispatcher);