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);
791 dispatcher_constructor (GType type, guint n_construct_params,
792 GObjectConstructParam *construct_params)
796 if (dispatcher == NULL)
798 retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
799 (type, n_construct_params, construct_params);
801 dispatcher = EMPATHY_DISPATCHER (retval);
802 g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
806 retval = g_object_ref (dispatcher);
813 dispatcher_finalize (GObject *object)
815 EmpathyDispatcherPriv *priv = GET_PRIV (object);
817 g_signal_handlers_disconnect_by_func (priv->account_manager,
818 dispatcher_account_connection_cb, object);
820 g_object_unref (priv->account_manager);
821 g_object_unref (priv->mc);
823 g_hash_table_destroy (priv->connections);
825 g_object_unref (priv->chatroom_mgr);
829 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
831 GObjectClass *object_class = G_OBJECT_CLASS (klass);
833 object_class->finalize = dispatcher_finalize;
834 object_class->constructor = dispatcher_constructor;
837 g_signal_new ("observe",
838 G_TYPE_FROM_CLASS (klass),
842 g_cclosure_marshal_VOID__OBJECT,
844 1, EMPATHY_TYPE_DISPATCH_OPERATION);
847 g_signal_new ("approve",
848 G_TYPE_FROM_CLASS (klass),
852 g_cclosure_marshal_VOID__OBJECT,
854 1, EMPATHY_TYPE_DISPATCH_OPERATION);
857 g_signal_new ("dispatch",
858 G_TYPE_FROM_CLASS (klass),
862 g_cclosure_marshal_VOID__OBJECT,
864 1, EMPATHY_TYPE_DISPATCH_OPERATION);
866 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
871 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
874 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
875 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
877 dispatcher->priv = priv;
878 priv->mc = empathy_mission_control_new ();
879 priv->account_manager = empathy_account_manager_dup_singleton ();
881 g_signal_connect (priv->account_manager,
882 "account-connection-changed",
883 G_CALLBACK (dispatcher_account_connection_cb),
886 priv->accounts = g_hash_table_new_full (empathy_account_hash,
887 empathy_account_equal, g_object_unref, g_object_unref);
889 priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
890 g_object_unref, (GDestroyNotify) free_connection_data);
892 accounts = mc_accounts_list_by_enabled (TRUE);
894 for (l = accounts; l; l = l->next) {
895 dispatcher_update_account (dispatcher, l->data);
896 g_object_unref (l->data);
898 g_list_free (accounts);
900 /* FIXME thisshould probably be created by another object.. */
901 priv->chatroom_mgr = empathy_chatroom_manager_dup_singleton (NULL);
902 empathy_chatroom_manager_observe (priv->chatroom_mgr, dispatcher);
906 empathy_dispatcher_dup_singleton (void)
908 return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
912 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
913 DispatcherRequestData *request_data, const gchar *object_path,
914 GHashTable *properties, const GError *error)
916 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
917 EmpathyDispatchOperation *operation = NULL;
918 ConnectionData *conn_data;
920 conn_data = g_hash_table_lookup (priv->connections,
921 request_data->connection);
925 DEBUG ("Channel request failed: %s", error->message);
927 if (request_data->cb != NULL)
928 request_data->cb (NULL, error, request_data->user_data);
930 conn_data->outstanding_requests =
931 g_list_remove (conn_data->outstanding_requests, request_data);
932 free_dispatcher_request_data (request_data);
937 operation = g_hash_table_lookup (conn_data->outstanding_channels,
940 if (operation != NULL)
941 g_hash_table_remove (conn_data->outstanding_channels, object_path);
943 operation = g_hash_table_lookup (conn_data->dispatching_channels,
946 if (operation == NULL)
948 DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
953 operation = empathy_dispatch_operation_new_with_wrapper (
954 request_data->connection,
955 data->channel, request_data->contact, FALSE,
956 data->channel_wrapper);
962 if (properties != NULL)
963 channel = tp_channel_new_from_properties (request_data->connection,
964 object_path, properties, NULL);
966 channel = tp_channel_new (request_data->connection, object_path,
967 request_data->channel_type, request_data->handle_type,
968 request_data->handle, NULL);
970 g_signal_connect (channel, "invalidated",
971 G_CALLBACK (dispatcher_channel_invalidated_cb),
972 request_data->dispatcher);
974 operation = empathy_dispatch_operation_new (request_data->connection,
975 channel, request_data->contact, FALSE);
976 g_object_unref (channel);
981 /* Already existed set potential extra information */
982 g_object_set (G_OBJECT (operation),
983 "contact", request_data->contact,
987 request_data->operation = operation;
989 /* (pre)-approve this right away as we requested it */
990 empathy_dispatch_operation_approve (operation);
992 dispatcher_start_dispatching (request_data->dispatcher, operation,
995 dispatcher_flush_outstanding_operations (request_data->dispatcher,
1000 dispatcher_request_channel_cb (TpConnection *connection,
1001 const gchar *object_path, const GError *error,
1002 gpointer user_data, GObject *weak_object)
1004 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1005 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1007 dispatcher_connection_new_requested_channel (dispatcher,
1008 request_data, object_path, NULL, error);
1012 dispatcher_request_channel (DispatcherRequestData *request_data)
1014 tp_cli_connection_call_request_channel (request_data->connection, -1,
1015 request_data->channel_type,
1016 request_data->handle_type,
1017 request_data->handle,
1018 TRUE, dispatcher_request_channel_cb,
1019 request_data, NULL, G_OBJECT (request_data->dispatcher));
1023 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1024 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1026 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1027 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1029 TpConnection *connection;
1031 DispatcherRequestData *request_data;
1033 account = empathy_contact_get_account (contact);
1034 connection = g_hash_table_lookup (priv->accounts, account);
1036 g_assert (connection != NULL);
1037 cd = g_hash_table_lookup (priv->connections, connection);
1038 request_data = new_dispatcher_request_data (dispatcher, connection,
1039 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1040 contact, callback, user_data);
1042 cd->outstanding_requests = g_list_prepend
1043 (cd->outstanding_requests, request_data);
1045 dispatcher_request_channel (request_data);
1047 g_object_unref (dispatcher);
1052 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1054 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1056 request_data->handle = empathy_contact_get_handle (contact);
1058 dispatcher_request_channel (request_data);
1062 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1063 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1065 EmpathyDispatcher *dispatcher;
1066 EmpathyDispatcherPriv *priv;
1068 TpConnection *connection;
1069 ConnectionData *connection_data;
1070 DispatcherRequestData *request_data;
1072 dispatcher = empathy_dispatcher_dup_singleton();
1073 priv = GET_PRIV (dispatcher);
1075 account = empathy_contact_get_account (contact);
1076 connection = g_hash_table_lookup (priv->accounts, account);
1077 connection_data = g_hash_table_lookup (priv->connections, connection);
1079 /* The contact handle might not be known yet */
1080 request_data = new_dispatcher_request_data (dispatcher, connection,
1081 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1082 contact, callback, user_data);
1084 connection_data->outstanding_requests = g_list_prepend
1085 (connection_data->outstanding_requests, request_data);
1087 empathy_contact_call_when_ready (contact,
1088 EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1091 g_object_unref (dispatcher);
1095 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1096 *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1098 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1099 EmpathyContactFactory *factory;
1100 EmpathyContact *contact;
1102 factory = empathy_contact_factory_dup_singleton ();
1103 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1105 empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1107 g_object_unref (contact);
1108 g_object_unref (factory);
1109 g_object_unref (dispatcher);
1114 dispatcher_request_handles_cb (TpConnection *connection,
1115 const GArray *handles, const GError *error, gpointer user_data,
1118 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1122 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1123 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1126 cd = g_hash_table_lookup (priv->connections, request_data->connection);
1128 if (request_data->cb)
1129 request_data->cb (NULL, error, request_data->user_data);
1131 cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1134 free_dispatcher_request_data (request_data);
1136 dispatcher_flush_outstanding_operations (dispatcher, cd);
1140 request_data->handle = g_array_index (handles, guint, 0);
1141 dispatcher_request_channel (request_data);
1145 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1146 EmpathyDispatcherRequestCb *callback, gpointer user_data)
1148 EmpathyDispatcher *dispatcher;
1149 EmpathyDispatcherPriv *priv;
1150 DispatcherRequestData *request_data;
1151 TpConnection *connection;
1152 ConnectionData *connection_data;
1153 const gchar *names[] = { roomname, NULL };
1155 dispatcher = empathy_dispatcher_dup_singleton();
1156 priv = GET_PRIV (dispatcher);
1158 connection = g_hash_table_lookup (priv->accounts, account);
1159 connection_data = g_hash_table_lookup (priv->connections, connection);
1162 /* Don't know the room handle yet */
1163 request_data = new_dispatcher_request_data (dispatcher, connection,
1164 TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1165 NULL, callback, user_data);
1167 connection_data->outstanding_requests = g_list_prepend
1168 (connection_data->outstanding_requests, request_data);
1170 tp_cli_connection_call_request_handles (connection, -1,
1171 TP_HANDLE_TYPE_ROOM, names,
1172 dispatcher_request_handles_cb, request_data, NULL,
1173 G_OBJECT (dispatcher));
1175 g_object_unref (dispatcher);
1179 dispatcher_create_channel_cb (TpConnection *connect,
1180 const gchar *object_path, GHashTable *properties, const GError *error,
1181 gpointer user_data, GObject *weak_object)
1183 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1184 DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1186 dispatcher_connection_new_requested_channel (dispatcher,
1187 request_data, object_path, properties, error);
1192 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1195 DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1196 GValue *target_handle;
1198 g_assert (request_data->request);
1200 request_data->handle = empathy_contact_get_handle (contact);
1202 target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1203 g_value_set_uint (target_handle, request_data->handle);
1204 g_hash_table_insert (request_data->request,
1205 TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1207 tp_cli_connection_interface_requests_call_create_channel (
1208 request_data->connection, -1,
1209 request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1210 G_OBJECT (request_data->dispatcher));
1214 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1215 const gchar *filename, guint64 size, guint64 date,
1216 const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1219 EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1220 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1221 McAccount *account = empathy_contact_get_account (contact);
1222 TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1223 ConnectionData *connection_data =
1224 g_hash_table_lookup (priv->connections, connection);
1225 DispatcherRequestData *request_data;
1227 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1228 (GDestroyNotify) tp_g_value_slice_free);
1230 /* org.freedesktop.Telepathy.Channel.ChannelType */
1231 value = tp_g_value_slice_new (G_TYPE_STRING);
1232 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1233 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1235 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1236 value = tp_g_value_slice_new (G_TYPE_UINT);
1237 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1238 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1240 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1241 value = tp_g_value_slice_new (G_TYPE_STRING);
1242 g_value_set_string (value, content_type);
1243 g_hash_table_insert (request,
1244 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1246 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1247 value = tp_g_value_slice_new (G_TYPE_STRING);
1248 g_value_set_string (value, filename);
1249 g_hash_table_insert (request,
1250 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1252 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1253 value = tp_g_value_slice_new (G_TYPE_UINT64);
1254 g_value_set_uint64 (value, size);
1255 g_hash_table_insert (request,
1256 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1258 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1259 value = tp_g_value_slice_new (G_TYPE_UINT64);
1260 g_value_set_uint64 (value, date);
1261 g_hash_table_insert (request,
1262 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1265 /* The contact handle might not be known yet */
1266 request_data = new_dispatcher_request_data (dispatcher, connection,
1267 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1268 contact, callback, user_data);
1269 connection_data->outstanding_requests = g_list_prepend
1270 (connection_data->outstanding_requests, request_data);
1272 empathy_contact_call_when_ready (contact,
1273 EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1276 g_object_unref (dispatcher);