1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n.h>
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/proxy-subclass.h>
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <extensions/extensions.h>
39 #include "empathy-dispatcher.h"
40 #include "empathy-utils.h"
41 #include "empathy-tube-handler.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-tp-group.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 GHashTable *connections;
56 EmpathyChatroomManager *chatroom_mgr;
57 } EmpathyDispatcherPriv;
59 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
68 static guint signals[LAST_SIGNAL];
69 static EmpathyDispatcher *dispatcher = NULL;
72 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
75 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
79 EmpathyDispatcherTube public;
80 EmpathyContactFactory *factory;
88 empathy_dispatcher_tube_get_type (void)
90 static GType type_id = 0;
93 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
94 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
95 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
101 EmpathyDispatcherTube *
102 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
104 DispatcherTube *tube = (DispatcherTube*) data;
106 g_return_val_if_fail (tube != NULL, NULL);
114 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
116 DispatcherTube *tube = (DispatcherTube*) data;
118 g_return_if_fail (tube != NULL);
120 if (--tube->ref_count == 0) {
121 if (!tube->handled) {
122 DEBUG ("Tube can't be handled, closing");
123 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
129 g_free (tube->bus_name);
130 g_free (tube->object_path);
131 g_object_unref (tube->factory);
132 g_object_unref (tube->public.channel);
133 g_object_unref (tube->public.initiator);
134 g_slice_free (DispatcherTube, tube);
139 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
144 DispatcherTube *tube = user_data;
147 DEBUG ("Error: %s", error->message);
149 tube->handled = TRUE;
154 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
155 EmpathyDispatcherTube *user_data)
157 DispatcherTube *tube = (DispatcherTube*) user_data;
159 if (tube->public.activatable) {
166 /* Create the proxy for the tube handler */
167 thandler = g_object_new (TP_TYPE_PROXY,
168 "dbus-connection", tp_get_bus (),
169 "bus-name", tube->bus_name,
170 "object-path", tube->object_path,
172 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
174 /* Give the tube to the handler */
175 g_object_get (tube->public.channel,
176 "connection", &connection,
177 "object-path", &object_path,
178 "handle_type", &handle_type,
182 DEBUG ("Dispatching tube");
183 emp_cli_tube_handler_call_handle_tube (thandler, -1,
184 connection->bus_name,
185 connection->object_path,
186 object_path, handle_type,
187 handle, tube->public.id,
188 dispatcher_tubes_handle_tube_cb,
189 empathy_dispatcher_tube_ref (user_data),
190 (GDestroyNotify) empathy_dispatcher_tube_unref,
191 G_OBJECT (dispatcher));
193 g_object_unref (thandler);
194 g_object_unref (connection);
195 g_free (object_path);
200 dispatcher_tubes_new_tube_cb (TpChannel *channel,
204 const gchar *service,
205 GHashTable *parameters,
210 static TpDBusDaemon *daemon = NULL;
211 DispatcherTube *tube;
215 gboolean running = FALSE;
216 GError *error = NULL;
218 /* Increase tube count */
219 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
220 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
221 DEBUG ("Increased tube count for channel %p: %d", channel, number);
223 /* We dispatch only local pending tubes */
224 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
229 daemon = tp_dbus_daemon_new (tp_get_bus ());
232 account = empathy_channel_get_account (channel);
233 tube = g_slice_new (DispatcherTube);
235 tube->handled = FALSE;
236 tube->factory = empathy_contact_factory_new ();
237 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
238 tube->object_path = empathy_tube_handler_build_object_path (type, service);
239 tube->public.activatable = FALSE;
240 tube->public.id = id;
241 tube->public.channel = g_object_ref (channel);
242 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
245 g_object_unref (account);
247 /* Check if that bus-name has an owner, if it has one that means the
248 * app is already running and we can directly give the channel. */
249 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
250 &running, NULL, NULL);
252 DEBUG ("Tube handler running");
253 tube->public.activatable = TRUE;
254 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
255 (EmpathyDispatcherTube*) tube);
256 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
260 /* Check if that bus-name is activatable, if not that means the
261 * application needed to handle this tube isn't installed. */
262 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
265 DEBUG ("Error listing activatable names: %s", error->message);
266 g_clear_error (&error);
270 for (name = names; *name; name++) {
271 if (!tp_strdiff (*name, tube->bus_name)) {
272 tube->public.activatable = TRUE;
279 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
280 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
284 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
285 const GPtrArray *tubes,
293 DEBUG ("Error: %s", error->message);
297 for (i = 0; i < tubes->len; i++) {
300 values = g_ptr_array_index (tubes, i);
301 dispatcher_tubes_new_tube_cb (channel,
302 g_value_get_uint (g_value_array_get_nth (values, 0)),
303 g_value_get_uint (g_value_array_get_nth (values, 1)),
304 g_value_get_uint (g_value_array_get_nth (values, 2)),
305 g_value_get_string (g_value_array_get_nth (values, 3)),
306 g_value_get_boxed (g_value_array_get_nth (values, 4)),
307 g_value_get_uint (g_value_array_get_nth (values, 5)),
308 user_data, dispatcher);
313 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
317 EmpathyDispatcher *dispatcher)
319 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
321 DEBUG ("%s", message);
323 priv->tubes = g_slist_remove (priv->tubes, proxy);
324 g_object_unref (proxy);
328 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
335 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
337 DEBUG ("No more tube, closing channel");
338 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
340 else if (number > 1) {
341 DEBUG ("Decrease tube count: %d", number);
342 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
347 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
350 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
354 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
355 g_signal_connect (channel, "invalidated",
356 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
359 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
360 dispatcher_tubes_tube_closed_cb,
362 G_OBJECT (dispatcher), NULL);
363 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
364 dispatcher_tubes_new_tube_cb,
366 G_OBJECT (dispatcher), NULL);
367 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
368 dispatcher_tubes_list_tubes_cb,
370 G_OBJECT (dispatcher));
374 dispatcher_connection_invalidated_cb (TpConnection *connection,
378 EmpathyDispatcher *dispatcher)
380 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
384 DEBUG ("Error: %s", message);
386 g_hash_table_iter_init (&iter, priv->connections);
387 while (g_hash_table_iter_next (&iter, &key, &value)) {
388 if (value == connection) {
389 g_hash_table_remove (priv->connections, key);
397 EmpathyDispatcher *self;
398 EmpathyChatroom *chatroom;
399 } dispatcher_connection_invalidated_cb_ctx;
401 static dispatcher_connection_invalidated_cb_ctx *
402 dispatcher_connection_invalidated_cb_ctx_new (EmpathyDispatcher *dispatcher,
403 EmpathyChatroom *chatroom)
405 dispatcher_connection_invalidated_cb_ctx *ctx;
407 ctx = g_slice_new (dispatcher_connection_invalidated_cb_ctx);
409 ctx->self = g_object_ref (dispatcher);
410 ctx->chatroom = g_object_ref (chatroom);
416 dispatcher_connection_invalidated_cb_ctx_free (
417 dispatcher_connection_invalidated_cb_ctx *ctx)
419 g_object_unref (ctx->self);
420 g_object_unref (ctx->chatroom);
422 g_slice_free (dispatcher_connection_invalidated_cb_ctx, ctx);
425 static void dispatcher_chatroom_invalidated_cb (
430 dispatcher_connection_invalidated_cb_ctx *ctx)
432 EmpathyDispatcherPriv *priv = GET_PRIV (ctx->self);
435 g_object_get (ctx->chatroom, "favorite", &favorite, NULL);
439 /* Chatroom is in favorites so don't remove it from the manager */
440 g_object_set (ctx->chatroom, "tp-channel", NULL, NULL);
444 empathy_chatroom_manager_remove (priv->chatroom_mgr, ctx->chatroom);
449 dispatcher_connection_new_channel_cb (TpConnection *connection,
450 const gchar *object_path,
451 const gchar *channel_type,
454 gboolean suppress_handler,
458 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
459 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
461 gpointer had_channels;
463 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
464 if (had_channels == NULL) {
465 /* ListChannels didn't return yet, return to avoid duplicate
470 channel = tp_channel_new (connection, object_path, channel_type,
471 handle_type, handle, NULL);
472 tp_channel_run_until_ready (channel, NULL, NULL);
474 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
475 dispatcher_tubes_handle_channel (dispatcher, channel);
478 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
479 handle_type == TP_HANDLE_TYPE_ROOM)
481 /* Add the chatroom to the chatroom manager */
482 EmpathyChatroom *chatroom;
487 dispatcher_connection_invalidated_cb_ctx *ctx;
489 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
490 g_array_append_val (handles, handle);
492 tp_cli_connection_run_inspect_handles (connection, -1,
493 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
495 mc = empathy_mission_control_new ();
496 account = mission_control_get_account_for_tpconnection (mc, connection,
499 chatroom = empathy_chatroom_manager_find (priv->chatroom_mgr, account,
501 if (chatroom == NULL)
503 chatroom = empathy_chatroom_new (account);
504 empathy_chatroom_set_name (chatroom, room_ids[0]);
505 empathy_chatroom_manager_add (priv->chatroom_mgr, chatroom);
509 g_object_ref (chatroom);
512 g_object_set (chatroom, "tp-channel", channel, NULL);
514 ctx = dispatcher_connection_invalidated_cb_ctx_new (dispatcher, chatroom);
516 g_signal_connect_data (channel, "invalidated",
517 G_CALLBACK (dispatcher_chatroom_invalidated_cb), ctx,
518 (GClosureNotify) dispatcher_connection_invalidated_cb_ctx_free, 0);
520 g_free (room_ids[0]);
522 g_array_free (handles, TRUE);
524 g_object_unref (account);
525 g_object_unref (chatroom);
528 if (suppress_handler) {
529 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
531 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
534 g_object_unref (channel);
538 dispatcher_connection_list_channels_cb (TpConnection *connection,
539 const GPtrArray *channels,
547 DEBUG ("Error: %s", error->message);
551 g_object_set_data (G_OBJECT (connection), "had-channels",
552 GUINT_TO_POINTER (1));
554 for (i = 0; i < channels->len; i++) {
557 values = g_ptr_array_index (channels, i);
558 dispatcher_connection_new_channel_cb (connection,
559 g_value_get_boxed (g_value_array_get_nth (values, 0)),
560 g_value_get_string (g_value_array_get_nth (values, 1)),
561 g_value_get_uint (g_value_array_get_nth (values, 2)),
562 g_value_get_uint (g_value_array_get_nth (values, 3)),
563 FALSE, user_data, dispatcher);
568 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
569 const GPtrArray *capabilities,
575 DEBUG ("Error: %s", error->message);
580 dispatcher_connection_ready_cb (TpConnection *connection,
584 GPtrArray *capabilities;
587 const gchar *remove = NULL;
590 dispatcher_connection_invalidated_cb (connection,
598 g_signal_connect (connection, "invalidated",
599 G_CALLBACK (dispatcher_connection_invalidated_cb),
601 tp_cli_connection_connect_to_new_channel (connection,
602 dispatcher_connection_new_channel_cb,
604 G_OBJECT (dispatcher), NULL);
605 tp_cli_connection_call_list_channels (connection, -1,
606 dispatcher_connection_list_channels_cb,
608 G_OBJECT (dispatcher));
610 /* Advertise VoIP capabilities */
611 capabilities = g_ptr_array_sized_new (1);
612 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
613 G_TYPE_UINT, G_TYPE_INVALID);
614 g_value_init (&cap, cap_type);
615 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
616 dbus_g_type_struct_set (&cap,
617 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
618 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
619 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
620 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
621 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
623 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
625 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
627 capabilities, &remove,
628 dispatcher_connection_advertise_capabilities_cb,
629 NULL, NULL, G_OBJECT (dispatcher));
630 /* FIXME: Is that leaked? */
634 dispatcher_update_account (EmpathyDispatcher *dispatcher,
637 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
638 TpConnection *connection;
640 connection = g_hash_table_lookup (priv->connections, account);
645 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
650 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
651 tp_connection_call_when_ready (connection,
652 dispatcher_connection_ready_cb,
657 dispatcher_status_changed_cb (MissionControl *mc,
658 TpConnectionStatus status,
660 TpConnectionStatusReason reason,
661 const gchar *unique_name,
662 EmpathyDispatcher *dispatcher)
666 account = mc_account_lookup (unique_name);
667 dispatcher_update_account (dispatcher, account);
668 g_object_unref (account);
672 dispatcher_finalize (GObject *object)
674 EmpathyDispatcherPriv *priv = GET_PRIV (object);
677 empathy_disconnect_account_status_changed (priv->token);
678 g_object_unref (priv->mc);
680 for (l = priv->tubes; l; l = l->next) {
681 g_signal_handlers_disconnect_by_func (l->data,
682 dispatcher_tubes_channel_invalidated_cb,
684 g_object_unref (l->data);
686 g_slist_free (priv->tubes);
688 g_hash_table_destroy (priv->connections);
690 g_object_unref (priv->chatroom_mgr);
694 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
696 GObjectClass *object_class = G_OBJECT_CLASS (klass);
698 object_class->finalize = dispatcher_finalize;
700 signals[DISPATCH_CHANNEL] =
701 g_signal_new ("dispatch-channel",
702 G_TYPE_FROM_CLASS (klass),
706 g_cclosure_marshal_VOID__OBJECT,
709 signals[FILTER_CHANNEL] =
710 g_signal_new ("filter-channel",
711 G_TYPE_FROM_CLASS (klass),
715 g_cclosure_marshal_VOID__OBJECT,
718 signals[FILTER_TUBE] =
719 g_signal_new ("filter-tube",
720 G_TYPE_FROM_CLASS (klass),
724 g_cclosure_marshal_VOID__BOXED,
726 1, EMPATHY_TYPE_DISPATCHER_TUBE);
728 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
732 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
735 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
736 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
738 dispatcher->priv = priv;
739 priv->mc = empathy_mission_control_new ();
740 priv->token = empathy_connect_to_account_status_changed (priv->mc,
741 G_CALLBACK (dispatcher_status_changed_cb),
744 priv->connections = g_hash_table_new_full (empathy_account_hash,
745 empathy_account_equal,
748 accounts = mc_accounts_list_by_enabled (TRUE);
749 for (l = accounts; l; l = l->next) {
750 dispatcher_update_account (dispatcher, l->data);
751 g_object_unref (l->data);
753 g_list_free (accounts);
755 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
759 empathy_dispatcher_new (void)
762 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
763 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
765 g_object_ref (dispatcher);
772 const gchar *channel_type;
775 } DispatcherRequestData;
778 dispatcher_request_channel_cb (TpConnection *connection,
779 const gchar *object_path,
782 GObject *weak_object)
784 DispatcherRequestData *data = (DispatcherRequestData*) user_data;
787 DEBUG ("Error: %s", error->message);
794 channel = tp_channel_new (connection, object_path,
799 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
804 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
808 TpConnection *connection;
811 EmpathyContactFactory *factory;
812 EmpathyTpGroup *group;
813 EmpathyContact *self_contact;
814 GError *error = NULL;
816 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
818 mc = empathy_mission_control_new ();
819 account = empathy_contact_get_account (contact);
820 connection = mission_control_get_tpconnection (mc, account, NULL);
821 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
824 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
825 * will be catched in EmpathyFilter */
826 if (!tp_cli_connection_run_request_channel (connection, -1,
827 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
834 DEBUG ("Couldn't request channel: %s",
835 error ? error->message : "No error given");
836 g_clear_error (&error);
837 g_object_unref (connection);
841 channel = tp_channel_new (connection,
842 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
843 TP_HANDLE_TYPE_NONE, 0, NULL);
845 group = empathy_tp_group_new (channel);
846 empathy_run_until_ready (group);
848 factory = empathy_contact_factory_new ();
849 self_contact = empathy_contact_factory_get_user (factory, account);
850 empathy_contact_run_until_ready (self_contact,
851 EMPATHY_CONTACT_READY_HANDLE,
854 empathy_tp_group_add_member (group, contact, "");
855 empathy_tp_group_add_member (group, self_contact, "");
857 g_object_unref (factory);
858 g_object_unref (self_contact);
859 g_object_unref (group);
860 g_object_unref (connection);
861 g_object_unref (channel);
862 g_free (object_path);
866 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
868 EmpathyContactFactory *factory;
869 EmpathyContact *contact;
871 factory = empathy_contact_factory_new ();
872 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
873 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
875 empathy_dispatcher_call_with_contact (contact);
877 g_object_unref (contact);
878 g_object_unref (factory);
882 empathy_dispatcher_chat_with_contact (EmpathyContact *contact)
886 TpConnection *connection;
887 DispatcherRequestData *data;
889 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
891 mc = empathy_mission_control_new ();
892 account = empathy_contact_get_account (contact);
893 connection = mission_control_get_tpconnection (mc, account, NULL);
894 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
897 /* We abuse of suppress_handler, TRUE means OUTGOING. */
898 data = g_new (DispatcherRequestData, 1);
899 data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
900 data->handle_type = TP_HANDLE_TYPE_CONTACT;
901 data->handle = empathy_contact_get_handle (contact);
902 tp_cli_connection_call_request_channel (connection, -1,
907 dispatcher_request_channel_cb,
910 g_object_unref (connection);
914 empathy_dispatcher_chat_with_contact_id (McAccount *account,
915 const gchar *contact_id)
917 EmpathyContactFactory *factory;
918 EmpathyContact *contact;
920 factory = empathy_contact_factory_new ();
921 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
922 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
924 empathy_dispatcher_chat_with_contact (contact);
926 g_object_unref (contact);
927 g_object_unref (factory);
933 EmpathyContact *contact;
934 } FileChannelRequest;
937 file_channel_request_cb (TpConnection *connection,
938 const gchar *object_path,
941 GObject *weak_object)
943 GValue value = { 0 };
948 EmpathyTpFile *tp_file;
949 FileChannelRequest *request = (FileChannelRequest *) user_data;
952 DEBUG ("Couldn't request channel: %s", error->message);
956 info = g_file_query_info (request->gfile,
957 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
958 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
960 size = info ? g_file_info_get_size (info) : EMPATHY_TP_FILE_UNKNOWN_SIZE;
961 filename = g_file_get_basename (request->gfile);
962 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
964 DEBUG ("Sending %s from a stream to %s (size %llu, content-type %s)",
965 filename, empathy_contact_get_name (request->contact), size,
966 g_file_info_get_content_type (info));
968 channel = tp_channel_new (connection,
970 EMP_IFACE_CHANNEL_TYPE_FILE,
971 TP_HANDLE_TYPE_CONTACT,
975 /* FIXME: this should go in CreateChannel in the new requests API */
976 g_value_init (&value, G_TYPE_STRING);
977 g_value_set_string (&value, g_filename_display_basename (filename));
978 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
979 EMP_IFACE_CHANNEL_TYPE_FILE, "Filename",
980 &value, NULL, NULL, NULL, NULL);
981 g_value_reset (&value);
983 g_value_set_string (&value, g_file_info_get_content_type (info));
984 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
985 EMP_IFACE_CHANNEL_TYPE_FILE, "ContentType",
986 &value, NULL, NULL, NULL, NULL);
988 g_value_unset (&value);
990 g_value_init (&value, G_TYPE_UINT64);
991 g_value_set_uint64 (&value, size);
992 tp_cli_dbus_properties_call_set (TP_PROXY (channel), -1,
993 EMP_IFACE_CHANNEL_TYPE_FILE, "Size",
994 &value, NULL, NULL, NULL, NULL);
995 g_value_unset (&value);
997 tp_file = empathy_tp_file_new (channel);
1000 empathy_tp_file_set_gfile (tp_file, request->gfile, NULL);
1003 empathy_tp_file_offer (tp_file);
1005 g_object_unref (request->gfile);
1006 g_slice_free (FileChannelRequest, request);
1007 g_object_unref (channel);
1012 empathy_dispatcher_send_file (EmpathyContact *contact,
1017 TpConnection *connection;
1019 FileChannelRequest *request;
1021 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1022 g_return_if_fail (G_IS_FILE (gfile));
1024 mc = empathy_mission_control_new ();
1025 account = empathy_contact_get_account (contact);
1026 connection = mission_control_get_tpconnection (mc, account, NULL);
1027 handle = empathy_contact_get_handle (contact);
1029 request = g_slice_new0 (FileChannelRequest);
1030 request->gfile = g_object_ref (gfile);
1031 request->handle = handle;
1032 request->contact = contact;
1034 tp_cli_connection_call_request_channel (connection, -1,
1035 EMP_IFACE_CHANNEL_TYPE_FILE,
1036 TP_HANDLE_TYPE_CONTACT,
1039 file_channel_request_cb,
1044 g_object_unref (mc);
1045 g_object_unref (connection);