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-tp-file.h"
45 #include "empathy-chatroom-manager.h"
46 #include "empathy-utils.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
49 #include <libempathy/empathy-debug.h>
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
53 GHashTable *connections;
57 EmpathyChatroomManager *chatroom_mgr;
58 } EmpathyDispatcherPriv;
60 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
69 static guint signals[LAST_SIGNAL];
70 static EmpathyDispatcher *dispatcher = NULL;
73 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
76 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
80 EmpathyDispatcherTube public;
81 EmpathyContactFactory *factory;
89 empathy_dispatcher_tube_get_type (void)
91 static GType type_id = 0;
94 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
95 (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
96 (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
102 EmpathyDispatcherTube *
103 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
105 DispatcherTube *tube = (DispatcherTube*) data;
107 g_return_val_if_fail (tube != NULL, NULL);
115 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
117 DispatcherTube *tube = (DispatcherTube*) data;
119 g_return_if_fail (tube != NULL);
121 if (--tube->ref_count == 0) {
122 if (!tube->handled) {
123 DEBUG ("Tube can't be handled, closing");
124 tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
130 g_free (tube->bus_name);
131 g_free (tube->object_path);
132 g_object_unref (tube->factory);
133 g_object_unref (tube->public.channel);
134 g_object_unref (tube->public.initiator);
135 g_slice_free (DispatcherTube, tube);
140 dispatcher_tubes_handle_tube_cb (TpProxy *channel,
145 DispatcherTube *tube = user_data;
148 DEBUG ("Error: %s", error->message);
150 tube->handled = TRUE;
155 empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher,
156 EmpathyDispatcherTube *user_data)
158 DispatcherTube *tube = (DispatcherTube*) user_data;
160 if (tube->public.activatable) {
167 /* Create the proxy for the tube handler */
168 thandler = g_object_new (TP_TYPE_PROXY,
169 "dbus-connection", tp_get_bus (),
170 "bus-name", tube->bus_name,
171 "object-path", tube->object_path,
173 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
175 /* Give the tube to the handler */
176 g_object_get (tube->public.channel,
177 "connection", &connection,
178 "object-path", &object_path,
179 "handle_type", &handle_type,
183 DEBUG ("Dispatching tube");
184 emp_cli_tube_handler_call_handle_tube (thandler, -1,
185 connection->bus_name,
186 connection->object_path,
187 object_path, handle_type,
188 handle, tube->public.id,
189 dispatcher_tubes_handle_tube_cb,
190 empathy_dispatcher_tube_ref (user_data),
191 (GDestroyNotify) empathy_dispatcher_tube_unref,
192 G_OBJECT (dispatcher));
194 g_object_unref (thandler);
195 g_object_unref (connection);
196 g_free (object_path);
201 dispatcher_tubes_new_tube_cb (TpChannel *channel,
205 const gchar *service,
206 GHashTable *parameters,
211 static TpDBusDaemon *daemon = NULL;
212 DispatcherTube *tube;
216 gboolean running = FALSE;
217 GError *error = NULL;
219 /* Increase tube count */
220 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
221 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
222 DEBUG ("Increased tube count for channel %p: %d", channel, number);
224 /* We dispatch only local pending tubes */
225 if (state != TP_TUBE_STATE_LOCAL_PENDING) {
230 daemon = tp_dbus_daemon_new (tp_get_bus ());
233 account = empathy_channel_get_account (channel);
234 tube = g_slice_new (DispatcherTube);
236 tube->handled = FALSE;
237 tube->factory = empathy_contact_factory_new ();
238 tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
239 tube->object_path = empathy_tube_handler_build_object_path (type, service);
240 tube->public.activatable = FALSE;
241 tube->public.id = id;
242 tube->public.channel = g_object_ref (channel);
243 tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
246 g_object_unref (account);
248 /* Check if that bus-name has an owner, if it has one that means the
249 * app is already running and we can directly give the channel. */
250 tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
251 &running, NULL, NULL);
253 DEBUG ("Tube handler running");
254 tube->public.activatable = TRUE;
255 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
256 (EmpathyDispatcherTube*) tube);
257 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
261 /* Check if that bus-name is activatable, if not that means the
262 * application needed to handle this tube isn't installed. */
263 if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
266 DEBUG ("Error listing activatable names: %s", error->message);
267 g_clear_error (&error);
271 for (name = names; *name; name++) {
272 if (!tp_strdiff (*name, tube->bus_name)) {
273 tube->public.activatable = TRUE;
280 g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
281 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
285 dispatcher_tubes_list_tubes_cb (TpChannel *channel,
286 const GPtrArray *tubes,
294 DEBUG ("Error: %s", error->message);
298 for (i = 0; i < tubes->len; i++) {
301 values = g_ptr_array_index (tubes, i);
302 dispatcher_tubes_new_tube_cb (channel,
303 g_value_get_uint (g_value_array_get_nth (values, 0)),
304 g_value_get_uint (g_value_array_get_nth (values, 1)),
305 g_value_get_uint (g_value_array_get_nth (values, 2)),
306 g_value_get_string (g_value_array_get_nth (values, 3)),
307 g_value_get_boxed (g_value_array_get_nth (values, 4)),
308 g_value_get_uint (g_value_array_get_nth (values, 5)),
309 user_data, dispatcher);
314 dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy,
318 EmpathyDispatcher *dispatcher)
320 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
322 DEBUG ("%s", message);
324 priv->tubes = g_slist_remove (priv->tubes, proxy);
325 g_object_unref (proxy);
329 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
336 number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
338 DEBUG ("No more tube, closing channel");
339 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
341 else if (number > 1) {
342 DEBUG ("Decrease tube count: %d", number);
343 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
348 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
351 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
355 priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
356 g_signal_connect (channel, "invalidated",
357 G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
360 tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
361 dispatcher_tubes_tube_closed_cb,
363 G_OBJECT (dispatcher), NULL);
364 tp_cli_channel_type_tubes_connect_to_new_tube (channel,
365 dispatcher_tubes_new_tube_cb,
367 G_OBJECT (dispatcher), NULL);
368 tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
369 dispatcher_tubes_list_tubes_cb,
371 G_OBJECT (dispatcher));
375 dispatcher_connection_invalidated_cb (TpConnection *connection,
379 EmpathyDispatcher *dispatcher)
381 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
385 DEBUG ("Error: %s", message);
387 g_hash_table_iter_init (&iter, priv->connections);
388 while (g_hash_table_iter_next (&iter, &key, &value)) {
389 if (value == connection) {
390 g_hash_table_remove (priv->connections, key);
398 EmpathyDispatcher *self;
399 EmpathyChatroom *chatroom;
400 } dispatcher_connection_invalidated_cb_ctx;
402 static dispatcher_connection_invalidated_cb_ctx *
403 dispatcher_connection_invalidated_cb_ctx_new (EmpathyDispatcher *dispatcher,
404 EmpathyChatroom *chatroom)
406 dispatcher_connection_invalidated_cb_ctx *ctx;
408 ctx = g_slice_new (dispatcher_connection_invalidated_cb_ctx);
410 ctx->self = g_object_ref (dispatcher);
411 ctx->chatroom = g_object_ref (chatroom);
417 dispatcher_connection_invalidated_cb_ctx_free (
418 dispatcher_connection_invalidated_cb_ctx *ctx)
420 g_object_unref (ctx->self);
421 g_object_unref (ctx->chatroom);
423 g_slice_free (dispatcher_connection_invalidated_cb_ctx, ctx);
426 static void dispatcher_chatroom_invalidated_cb (
431 dispatcher_connection_invalidated_cb_ctx *ctx)
433 EmpathyDispatcherPriv *priv = GET_PRIV (ctx->self);
436 g_object_get (ctx->chatroom, "favorite", &favorite, NULL);
440 /* Chatroom is in favorites so don't remove it from the manager */
441 g_object_set (ctx->chatroom, "tp-channel", NULL, NULL);
445 empathy_chatroom_manager_remove (priv->chatroom_mgr, ctx->chatroom);
450 dispatcher_connection_new_channel_cb (TpConnection *connection,
451 const gchar *object_path,
452 const gchar *channel_type,
455 gboolean suppress_handler,
459 EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
460 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
462 gpointer had_channels;
464 had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
465 if (had_channels == NULL) {
466 /* ListChannels didn't return yet, return to avoid duplicate
471 channel = tp_channel_new (connection, object_path, channel_type,
472 handle_type, handle, NULL);
473 tp_channel_run_until_ready (channel, NULL, NULL);
475 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
476 dispatcher_tubes_handle_channel (dispatcher, channel);
479 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
480 handle_type == TP_HANDLE_TYPE_ROOM)
482 /* Add the chatroom to the chatroom manager */
483 EmpathyChatroom *chatroom;
488 dispatcher_connection_invalidated_cb_ctx *ctx;
490 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
491 g_array_append_val (handles, handle);
493 tp_cli_connection_run_inspect_handles (connection, -1,
494 TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
496 mc = empathy_mission_control_new ();
497 account = mission_control_get_account_for_tpconnection (mc, connection,
500 chatroom = empathy_chatroom_manager_find (priv->chatroom_mgr, account,
502 if (chatroom == NULL)
504 chatroom = empathy_chatroom_new (account);
505 empathy_chatroom_set_name (chatroom, room_ids[0]);
506 empathy_chatroom_manager_add (priv->chatroom_mgr, chatroom);
510 g_object_ref (chatroom);
513 g_object_set (chatroom, "tp-channel", channel, NULL);
515 ctx = dispatcher_connection_invalidated_cb_ctx_new (dispatcher, chatroom);
517 g_signal_connect_data (channel, "invalidated",
518 G_CALLBACK (dispatcher_chatroom_invalidated_cb), ctx,
519 (GClosureNotify) dispatcher_connection_invalidated_cb_ctx_free, 0);
521 g_free (room_ids[0]);
523 g_array_free (handles, TRUE);
525 g_object_unref (account);
526 g_object_unref (chatroom);
529 if (suppress_handler) {
530 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
532 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
535 g_object_unref (channel);
539 dispatcher_connection_list_channels_cb (TpConnection *connection,
540 const GPtrArray *channels,
548 DEBUG ("Error: %s", error->message);
552 g_object_set_data (G_OBJECT (connection), "had-channels",
553 GUINT_TO_POINTER (1));
555 for (i = 0; i < channels->len; i++) {
558 values = g_ptr_array_index (channels, i);
559 dispatcher_connection_new_channel_cb (connection,
560 g_value_get_boxed (g_value_array_get_nth (values, 0)),
561 g_value_get_string (g_value_array_get_nth (values, 1)),
562 g_value_get_uint (g_value_array_get_nth (values, 2)),
563 g_value_get_uint (g_value_array_get_nth (values, 3)),
564 FALSE, user_data, dispatcher);
569 dispatcher_connection_advertise_capabilities_cb (TpConnection *connection,
570 const GPtrArray *capabilities,
576 DEBUG ("Error: %s", error->message);
581 dispatcher_connection_ready_cb (TpConnection *connection,
585 GPtrArray *capabilities;
588 const gchar *remove = NULL;
591 dispatcher_connection_invalidated_cb (connection,
599 g_signal_connect (connection, "invalidated",
600 G_CALLBACK (dispatcher_connection_invalidated_cb),
602 tp_cli_connection_connect_to_new_channel (connection,
603 dispatcher_connection_new_channel_cb,
605 G_OBJECT (dispatcher), NULL);
606 tp_cli_connection_call_list_channels (connection, -1,
607 dispatcher_connection_list_channels_cb,
609 G_OBJECT (dispatcher));
611 /* Advertise VoIP capabilities */
612 capabilities = g_ptr_array_sized_new (1);
613 cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
614 G_TYPE_UINT, G_TYPE_INVALID);
615 g_value_init (&cap, cap_type);
616 g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
617 dbus_g_type_struct_set (&cap,
618 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
619 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
620 TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
621 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN |
622 TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
624 g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
626 tp_cli_connection_interface_capabilities_call_advertise_capabilities (
628 capabilities, &remove,
629 dispatcher_connection_advertise_capabilities_cb,
630 NULL, NULL, G_OBJECT (dispatcher));
631 /* FIXME: Is that leaked? */
635 dispatcher_update_account (EmpathyDispatcher *dispatcher,
638 EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
639 TpConnection *connection;
641 connection = g_hash_table_lookup (priv->connections, account);
646 connection = mission_control_get_tpconnection (priv->mc, account, NULL);
651 g_hash_table_insert (priv->connections, g_object_ref (account), connection);
652 tp_connection_call_when_ready (connection,
653 dispatcher_connection_ready_cb,
658 dispatcher_status_changed_cb (MissionControl *mc,
659 TpConnectionStatus status,
661 TpConnectionStatusReason reason,
662 const gchar *unique_name,
663 EmpathyDispatcher *dispatcher)
667 account = mc_account_lookup (unique_name);
668 dispatcher_update_account (dispatcher, account);
669 g_object_unref (account);
673 dispatcher_finalize (GObject *object)
675 EmpathyDispatcherPriv *priv = GET_PRIV (object);
678 empathy_disconnect_account_status_changed (priv->token);
679 g_object_unref (priv->mc);
681 for (l = priv->tubes; l; l = l->next) {
682 g_signal_handlers_disconnect_by_func (l->data,
683 dispatcher_tubes_channel_invalidated_cb,
685 g_object_unref (l->data);
687 g_slist_free (priv->tubes);
689 g_hash_table_destroy (priv->connections);
691 g_object_unref (priv->chatroom_mgr);
695 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
697 GObjectClass *object_class = G_OBJECT_CLASS (klass);
699 object_class->finalize = dispatcher_finalize;
701 signals[DISPATCH_CHANNEL] =
702 g_signal_new ("dispatch-channel",
703 G_TYPE_FROM_CLASS (klass),
707 g_cclosure_marshal_VOID__OBJECT,
710 signals[FILTER_CHANNEL] =
711 g_signal_new ("filter-channel",
712 G_TYPE_FROM_CLASS (klass),
716 g_cclosure_marshal_VOID__OBJECT,
719 signals[FILTER_TUBE] =
720 g_signal_new ("filter-tube",
721 G_TYPE_FROM_CLASS (klass),
725 g_cclosure_marshal_VOID__BOXED,
727 1, EMPATHY_TYPE_DISPATCHER_TUBE);
729 g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
733 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
736 EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
737 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
739 dispatcher->priv = priv;
740 priv->mc = empathy_mission_control_new ();
741 priv->token = empathy_connect_to_account_status_changed (priv->mc,
742 G_CALLBACK (dispatcher_status_changed_cb),
745 priv->connections = g_hash_table_new_full (empathy_account_hash,
746 empathy_account_equal,
749 accounts = mc_accounts_list_by_enabled (TRUE);
750 for (l = accounts; l; l = l->next) {
751 dispatcher_update_account (dispatcher, l->data);
752 g_object_unref (l->data);
754 g_list_free (accounts);
756 priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
760 empathy_dispatcher_new (void)
763 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
764 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
766 g_object_ref (dispatcher);
773 const gchar *channel_type;
776 } DispatcherRequestData;
779 dispatcher_request_channel_cb (TpConnection *connection,
780 const gchar *object_path,
783 GObject *weak_object)
785 DispatcherRequestData *data = (DispatcherRequestData*) user_data;
788 DEBUG ("Error: %s", error->message);
795 channel = tp_channel_new (connection, object_path,
800 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
805 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
809 TpConnection *connection;
812 EmpathyContactFactory *factory;
813 EmpathyTpGroup *group;
814 EmpathyContact *self_contact;
815 GError *error = NULL;
817 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
819 mc = empathy_mission_control_new ();
820 account = empathy_contact_get_account (contact);
821 connection = mission_control_get_tpconnection (mc, account, NULL);
822 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
825 /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
826 * will be catched in EmpathyFilter */
827 if (!tp_cli_connection_run_request_channel (connection, -1,
828 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
835 DEBUG ("Couldn't request channel: %s",
836 error ? error->message : "No error given");
837 g_clear_error (&error);
838 g_object_unref (connection);
842 channel = tp_channel_new (connection,
843 object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
844 TP_HANDLE_TYPE_NONE, 0, NULL);
846 group = empathy_tp_group_new (channel);
847 empathy_run_until_ready (group);
849 factory = empathy_contact_factory_new ();
850 self_contact = empathy_contact_factory_get_user (factory, account);
851 empathy_contact_run_until_ready (self_contact,
852 EMPATHY_CONTACT_READY_HANDLE,
855 empathy_tp_group_add_member (group, contact, "");
856 empathy_tp_group_add_member (group, self_contact, "");
858 g_object_unref (factory);
859 g_object_unref (self_contact);
860 g_object_unref (group);
861 g_object_unref (connection);
862 g_object_unref (channel);
863 g_free (object_path);
867 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
869 EmpathyContactFactory *factory;
870 EmpathyContact *contact;
872 factory = empathy_contact_factory_new ();
873 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
874 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
876 empathy_dispatcher_call_with_contact (contact);
878 g_object_unref (contact);
879 g_object_unref (factory);
883 empathy_dispatcher_chat_with_contact (EmpathyContact *contact)
887 TpConnection *connection;
888 DispatcherRequestData *data;
890 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
892 mc = empathy_mission_control_new ();
893 account = empathy_contact_get_account (contact);
894 connection = mission_control_get_tpconnection (mc, account, NULL);
895 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
898 /* We abuse of suppress_handler, TRUE means OUTGOING. */
899 data = g_new (DispatcherRequestData, 1);
900 data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
901 data->handle_type = TP_HANDLE_TYPE_CONTACT;
902 data->handle = empathy_contact_get_handle (contact);
903 tp_cli_connection_call_request_channel (connection, -1,
908 dispatcher_request_channel_cb,
911 g_object_unref (connection);
915 empathy_dispatcher_chat_with_contact_id (McAccount *account,
916 const gchar *contact_id)
918 EmpathyContactFactory *factory;
919 EmpathyContact *contact;
921 factory = empathy_contact_factory_new ();
922 contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
923 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
925 empathy_dispatcher_chat_with_contact (contact);
927 g_object_unref (contact);
928 g_object_unref (factory);
934 } FileChannelRequest;
937 file_channel_create_cb (TpConnection *connection,
938 const gchar *object_path,
939 GHashTable *properties,
942 GObject *weak_object)
945 EmpathyTpFile *tp_file;
946 FileChannelRequest *request = (FileChannelRequest *) user_data;
949 DEBUG ("Couldn't request channel: %s", error->message);
953 channel = tp_channel_new (connection,
955 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
956 TP_HANDLE_TYPE_CONTACT,
960 tp_file = empathy_tp_file_new (channel);
961 empathy_tp_file_offer (tp_file, request->gfile, NULL);
963 g_object_unref (request->gfile);
964 g_slice_free (FileChannelRequest, request);
965 g_object_unref (channel);
969 empathy_dispatcher_send_file (EmpathyContact *contact,
974 TpConnection *connection;
976 FileChannelRequest *request;
982 GError *error = NULL;
984 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
985 g_return_if_fail (G_IS_FILE (gfile));
987 info = g_file_query_info (gfile,
988 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
989 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
990 G_FILE_ATTRIBUTE_TIME_MODIFIED,
994 DEBUG ("Can't get info about the file: %s", error->message);
995 g_clear_error (&error);
999 mc = empathy_mission_control_new ();
1000 account = empathy_contact_get_account (contact);
1001 connection = mission_control_get_tpconnection (mc, account, NULL);
1002 handle = empathy_contact_get_handle (contact);
1004 request = g_slice_new0 (FileChannelRequest);
1005 request->gfile = g_object_ref (gfile);
1006 request->handle = handle;
1008 filename = g_file_get_basename (request->gfile);
1009 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
1011 DEBUG ("Sending %s from a stream to %s (size %llu, content-type %s)",
1012 filename, empathy_contact_get_name (contact),
1013 g_file_info_get_size (info),
1014 g_file_info_get_content_type (info));
1016 args = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1017 (GDestroyNotify) tp_g_value_slice_free);
1019 /* org.freedesktop.Telepathy.Channel.ChannelType */
1020 value = tp_g_value_slice_new (G_TYPE_STRING);
1021 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1022 g_hash_table_insert (args, TP_IFACE_CHANNEL ".ChannelType", value);
1024 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1025 value = tp_g_value_slice_new (G_TYPE_UINT);
1026 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1027 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandleType", value);
1029 /* org.freedesktop.Telepathy.Channel.TargetHandle */
1030 value = tp_g_value_slice_new (G_TYPE_UINT);
1031 g_value_set_uint (value, handle);
1032 g_hash_table_insert (args, TP_IFACE_CHANNEL ".TargetHandle", value);
1034 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1035 value = tp_g_value_slice_new (G_TYPE_STRING);
1036 g_value_set_string (value, g_file_info_get_content_type (info));
1037 g_hash_table_insert (args,
1038 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1040 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1041 value = tp_g_value_slice_new (G_TYPE_STRING);
1042 g_value_set_string (value, g_filename_display_basename (filename));
1043 g_hash_table_insert (args,
1044 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1046 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1047 value = tp_g_value_slice_new (G_TYPE_UINT64);
1048 g_value_set_uint64 (value, g_file_info_get_size (info));
1049 g_hash_table_insert (args,
1050 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1052 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1053 g_file_info_get_modification_time (info, &last_modif);
1054 value = tp_g_value_slice_new (G_TYPE_UINT64);
1055 g_value_set_uint64 (value, last_modif.tv_sec);
1056 g_hash_table_insert (args,
1057 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1059 /* FIXME: Description ? */
1060 /* FIXME: ContentHashType and ContentHash ? */
1062 tp_cli_connection_interface_requests_call_create_channel (connection, -1,
1063 args, file_channel_create_cb, request, NULL, NULL);
1065 g_hash_table_destroy (args);
1067 g_object_unref (mc);
1068 g_object_unref (connection);