2 * empathy-ft-handler.c - Source for EmpathyFTHandler
3 * Copyright (C) 2009 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 * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 /* empathy-ft-handler.c */
25 #include <glib/gi18n.h>
26 #include <telepathy-glib/util.h>
28 #include "empathy-ft-handler.h"
29 #include "empathy-contact-factory.h"
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_FT
35 #include "empathy-debug.h"
37 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
41 #define BUFFER_SIZE 4096
62 gboolean done_reading;
68 EmpathyFTHandler *handler;
72 EmpathyFTHandlerReadyCallback callback;
74 EmpathyFTHandler *handler;
81 EmpathyTpFile *tpfile;
82 GCancellable *cancellable;
84 /* request for the new transfer */
87 /* transfer properties */
88 EmpathyContact *contact;
93 guint64 transferred_bytes;
96 TpFileHashType content_hash_type;
97 TpFileTransferState current_state;
99 gboolean is_completed;
100 } EmpathyFTHandlerPriv;
102 static guint signals[LAST_SIGNAL] = { 0 };
105 static void schedule_hash_chunk (HashingData *hash_data);
107 /* GObject implementations */
109 do_get_property (GObject *object,
114 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
119 g_value_set_object (value, priv->contact);
122 g_value_set_object (value, priv->gfile);
125 g_value_set_object (value, priv->tpfile);
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 do_set_property (GObject *object,
138 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
143 priv->contact = g_value_dup_object (value);
146 priv->gfile = g_value_dup_object (value);
149 priv->tpfile = g_value_dup_object (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
157 do_dispose (GObject *object)
159 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
161 if (priv->dispose_run)
164 priv->dispose_run = TRUE;
167 g_object_unref (priv->contact);
168 priv->contact = NULL;
172 g_object_unref (priv->gfile);
177 empathy_tp_file_close (priv->tpfile);
178 g_object_unref (priv->tpfile);
182 if (priv->cancellable) {
183 g_object_unref (priv->cancellable);
184 priv->cancellable = NULL;
187 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
191 do_finalize (GObject *object)
193 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
195 DEBUG ("%p", object);
197 g_free (priv->content_type);
198 priv->content_type = NULL;
200 g_free (priv->filename);
201 priv->filename = NULL;
203 g_free (priv->description);
204 priv->description = NULL;
206 g_free (priv->content_hash);
207 priv->content_hash = NULL;
209 if (priv->request != NULL)
211 g_hash_table_destroy (priv->request);
212 priv->request = NULL;
215 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
219 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
222 GParamSpec *param_spec;
224 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
226 object_class->get_property = do_get_property;
227 object_class->set_property = do_set_property;
228 object_class->dispose = do_dispose;
229 object_class->finalize = do_finalize;
232 param_spec = g_param_spec_object ("contact",
233 "contact", "The remote contact",
234 EMPATHY_TYPE_CONTACT,
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
236 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
238 param_spec = g_param_spec_object ("gfile",
239 "gfile", "The GFile we're handling",
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
242 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
244 param_spec = g_param_spec_object ("tp-file",
245 "tp-file", "The file's channel wrapper",
246 EMPATHY_TYPE_TP_FILE,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
248 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
251 signals[TRANSFER_STARTED] =
252 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
254 g_cclosure_marshal_VOID__OBJECT,
256 1, EMPATHY_TYPE_TP_FILE);
258 signals[TRANSFER_DONE] =
259 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
261 g_cclosure_marshal_VOID__OBJECT,
263 1, EMPATHY_TYPE_TP_FILE);
265 signals[TRANSFER_ERROR] =
266 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
268 g_cclosure_marshal_VOID__POINTER,
272 signals[TRANSFER_PROGRESS] =
273 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
274 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
275 _empathy_marshal_VOID__UINT64_UINT64,
277 2, G_TYPE_UINT64, G_TYPE_UINT64);
279 signals[HASHING_STARTED] =
280 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
281 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
282 g_cclosure_marshal_VOID__VOID,
285 signals[HASHING_PROGRESS] =
286 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
288 _empathy_marshal_VOID__UINT64_UINT64,
290 2, G_TYPE_UINT64, G_TYPE_UINT64);
292 signals[HASHING_DONE] =
293 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
294 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
295 g_cclosure_marshal_VOID__VOID,
300 empathy_ft_handler_init (EmpathyFTHandler *self)
302 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
303 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
306 priv->cancellable = g_cancellable_new ();
309 /* private functions */
312 hash_data_free (HashingData *data)
314 if (data->buffer != NULL)
316 g_free (data->buffer);
320 if (data->stream != NULL)
322 g_object_unref (data->stream);
326 if (data->checksum != NULL)
328 g_checksum_free (data->checksum);
329 data->checksum = NULL;
332 if (data->error != NULL)
334 g_error_free (data->error);
337 if (data->handler != NULL)
339 g_object_unref (data->handler);
340 data->handler = NULL;
343 g_slice_free (HashingData, data);
347 emit_error_signal (EmpathyFTHandler *handler,
350 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
352 if (!g_cancellable_is_cancelled (priv->cancellable))
353 g_cancellable_cancel (priv->cancellable);
355 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
359 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
363 EmpathyFTHandler *handler = user_data;
364 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
366 DEBUG ("Transfer operation callback, error %p", error);
370 emit_error_signal (handler, error);
374 priv->is_completed = TRUE;
375 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
377 empathy_tp_file_close (tp_file);
382 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
383 guint64 transferred_bytes,
386 EmpathyFTHandler *handler = user_data;
387 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
389 if (transferred_bytes == 0) {
390 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
394 if (priv->transferred_bytes != transferred_bytes)
396 priv->transferred_bytes = transferred_bytes;
397 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
398 transferred_bytes, priv->total_bytes);
403 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
407 EmpathyFTHandler *handler = user_data;
408 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
409 GError *my_error = (GError *) error;
411 DEBUG ("Dispatcher create channel CB");
413 /* we can destroy now the request */
414 g_hash_table_destroy (priv->request);
415 priv->request = NULL;
417 if (my_error == NULL)
419 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
422 if (my_error != NULL)
424 emit_error_signal (handler, my_error);
426 if (my_error != error)
427 g_clear_error (&my_error);
432 priv->tpfile = g_object_ref
433 (empathy_dispatch_operation_get_channel_wrapper (operation));
435 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
436 ft_transfer_progress_callback, handler,
437 ft_transfer_operation_callback, handler);
439 empathy_dispatch_operation_claim (operation);
443 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
445 EmpathyDispatcher *dispatcher;
447 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
449 DEBUG ("Pushing request to the dispatcher");
451 dispatcher = empathy_dispatcher_dup_singleton ();
452 account = empathy_contact_get_account (priv->contact);
454 /* I want to own a reference to the request, and destroy it later */
455 empathy_dispatcher_create_channel (dispatcher, account,
456 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
458 g_object_unref (dispatcher);
462 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
464 EmpathyDispatcher *dispatcher;
465 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
470 dispatcher = empathy_dispatcher_dup_singleton ();
471 account = empathy_contact_get_account (priv->contact);
473 allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
474 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
476 if (!tp_strv_contains ((const gchar * const *) allowed,
477 TP_IFACE_CHANNEL ".TargetHandle"))
480 g_object_unref (dispatcher);
486 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
488 guint contact_handle;
491 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
493 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
494 NULL, (GDestroyNotify) tp_g_value_slice_free);
496 contact_handle = empathy_contact_get_handle (priv->contact);
498 /* org.freedesktop.Telepathy.Channel.ChannelType */
499 value = tp_g_value_slice_new (G_TYPE_STRING);
500 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
501 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
503 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
504 value = tp_g_value_slice_new (G_TYPE_UINT);
505 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
506 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
508 /* org.freedesktop.Telepathy.Channel.TargetHandle */
509 value = tp_g_value_slice_new (G_TYPE_UINT);
510 g_value_set_uint (value, contact_handle);
511 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
513 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
514 value = tp_g_value_slice_new (G_TYPE_STRING);
515 g_value_set_string (value, priv->content_type);
516 g_hash_table_insert (request,
517 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
519 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
520 value = tp_g_value_slice_new (G_TYPE_STRING);
521 g_value_set_string (value, priv->filename);
522 g_hash_table_insert (request,
523 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
525 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
526 value = tp_g_value_slice_new (G_TYPE_UINT64);
527 g_value_set_uint64 (value, (guint64) priv->total_bytes);
528 g_hash_table_insert (request,
529 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
531 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
532 value = tp_g_value_slice_new (G_TYPE_UINT64);
533 g_value_set_uint64 (value, (guint64) priv->mtime);
534 g_hash_table_insert (request,
535 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
539 hash_job_async_close_stream_cb (GObject *source,
543 HashingData *hash_data = user_data;
544 EmpathyFTHandler *handler = hash_data->handler;
545 EmpathyFTHandlerPriv *priv;
546 GError *error = NULL;
549 DEBUG ("Closing stream after hashing.");
551 priv = GET_PRIV (handler);
553 /* if we're here we for sure have done reading, check if we stopped due
556 g_input_stream_close_finish (hash_data->stream, res, &error);
559 if (hash_data->error != NULL)
561 /* if we already stopped due to an error, probably we're completely
562 * hosed for some reason. just return the first read error
565 g_clear_error (&error);
566 error = hash_data->error;
572 if (hash_data->error != NULL)
574 error = hash_data->error;
578 /* set the checksum in the request */
580 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
582 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash */
583 value = tp_g_value_slice_new (G_TYPE_STRING);
584 g_value_set_string (value, g_checksum_get_string (hash_data->checksum));
585 g_hash_table_insert (priv->request,
586 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
592 emit_error_signal (handler, error);
596 g_signal_emit (handler, signals[HASHING_DONE], 0);
598 /* the request is complete now, push it to the dispatcher */
599 ft_handler_push_to_dispatcher (handler);
602 hash_data_free (hash_data);
606 hash_job_async_read_cb (GObject *source,
610 HashingData *hash_data = user_data;
612 GError *error = NULL;
614 bytes_read = g_input_stream_read_finish (hash_data->stream, res, &error);
617 hash_data->error = error;
618 hash_data->done_reading = TRUE;
622 hash_data->total_read += bytes_read;
624 /* we now have the chunk */
627 hash_data->done_reading = TRUE;
632 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
633 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
634 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
638 g_free (hash_data->buffer);
639 hash_data->buffer = NULL;
641 schedule_hash_chunk (hash_data);
645 schedule_hash_chunk (HashingData *hash_data)
647 EmpathyFTHandlerPriv *priv;
649 priv = GET_PRIV (hash_data->handler);
651 if (hash_data->done_reading)
653 g_input_stream_close_async (hash_data->stream, G_PRIORITY_DEFAULT,
654 priv->cancellable, hash_job_async_close_stream_cb, hash_data);
658 if (hash_data->buffer == NULL)
659 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
661 g_input_stream_read_async (hash_data->stream, hash_data->buffer,
662 BUFFER_SIZE, G_PRIORITY_DEFAULT, priv->cancellable,
663 hash_job_async_read_cb, hash_data);
668 ft_handler_read_async_cb (GObject *source,
672 GFileInputStream *stream;
673 GError *error = NULL;
674 HashingData *hash_data;
676 EmpathyFTHandler *handler = user_data;
677 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
679 DEBUG ("GFile read async CB.");
681 stream = g_file_read_finish (priv->gfile, res, &error);
684 emit_error_signal (handler, error);
685 g_clear_error (&error);
690 hash_data = g_slice_new0 (HashingData);
691 hash_data->stream = G_INPUT_STREAM (stream);
692 hash_data->done_reading = FALSE;
693 hash_data->total_bytes = priv->total_bytes;
694 hash_data->handler = g_object_ref (handler);
695 /* FIXME: should look at the CM capabilities before setting the
698 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
700 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
701 value = tp_g_value_slice_new (G_TYPE_UINT);
702 g_value_set_uint (value, TP_FILE_HASH_TYPE_MD5);
703 g_hash_table_insert (priv->request,
704 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
706 g_signal_emit (handler, signals[HASHING_STARTED], 0);
708 schedule_hash_chunk (hash_data);
712 ft_handler_complete_request (EmpathyFTHandler *handler)
714 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
715 GError *myerr = NULL;
717 /* check if FT is allowed before firing up the I/O machinery */
718 if (!ft_handler_check_if_allowed (handler))
720 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
721 EMPATHY_FT_ERROR_NOT_SUPPORTED,
722 _("File transfer not supported by remote contact"));
724 emit_error_signal (handler, myerr);
729 /* populate the request table with all the known properties */
730 ft_handler_populate_outgoing_request (handler);
732 /* now start hashing the file */
733 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
734 priv->cancellable, ft_handler_read_async_cb, handler);
738 callbacks_data_free (gpointer user_data)
740 CallbacksData *data = user_data;
743 g_object_unref (data->handler);
745 g_slice_free (CallbacksData, data);
749 ft_handler_gfile_ready_cb (GObject *source,
751 CallbacksData *cb_data)
754 GError *error = NULL;
756 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
758 DEBUG ("Got GFileInfo.");
760 info = g_file_query_info_finish (priv->gfile, res, &error);
765 priv->content_type = g_strdup (g_file_info_get_content_type (info));
766 priv->filename = g_strdup (g_file_info_get_display_name (info));
767 priv->total_bytes = g_file_info_get_size (info);
768 g_file_info_get_modification_time (info, &mtime);
769 priv->mtime = mtime.tv_sec;
770 priv->transferred_bytes = 0;
771 priv->description = NULL;
773 g_object_unref (info);
778 cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
782 cb_data->callback (NULL, error, cb_data->user_data);
783 g_error_free (error);
784 g_object_unref (cb_data->handler);
787 callbacks_data_free (cb_data);
791 ft_handler_contact_ready_cb (EmpathyContact *contact,
794 GObject *weak_object)
796 CallbacksData *cb_data = user_data;
797 EmpathyFTHandlerPriv *priv = GET_PRIV (weak_object);
799 g_assert (priv->contact != NULL);
800 g_assert (priv->gfile != NULL);
802 DEBUG ("Contact is ready.");
804 /* start collecting info about the file */
805 g_file_query_info_async (priv->gfile,
806 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
807 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
808 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
809 G_FILE_ATTRIBUTE_TIME_MODIFIED,
810 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
811 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb,
816 channel_get_all_properties_cb (TpProxy *proxy,
817 GHashTable *properties,
820 GObject *weak_object)
822 CallbacksData *cb_data = user_data;
823 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
824 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
825 EmpathyContactFactory *c_factory;
831 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
832 g_object_unref (handler);
836 priv->total_bytes = g_value_get_uint64 (
837 g_hash_table_lookup (properties, "Size"));
839 priv->transferred_bytes = g_value_get_uint64 (
840 g_hash_table_lookup (properties, "TransferredBytes"));
842 priv->filename = g_value_dup_string (
843 g_hash_table_lookup (properties, "Filename"));
845 priv->content_hash = g_value_dup_string (
846 g_hash_table_lookup (properties, "ContentHash"));
848 priv->content_hash_type = g_value_get_uint (
849 g_hash_table_lookup (properties, "ContentHashType"));
851 priv->content_type = g_value_dup_string (
852 g_hash_table_lookup (properties, "ContentType"));
854 priv->description = g_value_dup_string (
855 g_hash_table_lookup (properties, "Description"));
857 g_hash_table_destroy (properties);
859 c_factory = empathy_contact_factory_dup_singleton ();
860 account = empathy_channel_get_account (TP_CHANNEL (proxy));
861 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
862 priv->contact = empathy_contact_factory_get_from_handle
863 (c_factory, account, c_handle);
865 g_object_unref (c_factory);
866 g_object_unref (account);
868 cb_data->callback (handler, NULL, cb_data->user_data);
874 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
876 EmpathyFTHandlerReadyCallback callback,
879 EmpathyFTHandler *handler;
881 EmpathyFTHandlerPriv *priv;
883 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
884 g_return_if_fail (G_IS_FILE (source));
886 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
887 "contact", contact, "gfile", source, NULL);
889 priv = GET_PRIV (handler);
891 data = g_slice_new0 (CallbacksData);
892 data->callback = callback;
893 data->user_data = user_data;
894 data->handler = g_object_ref (handler);
896 empathy_contact_call_when_ready (priv->contact,
897 EMPATHY_CONTACT_READY_HANDLE,
898 ft_handler_contact_ready_cb, data, NULL, G_OBJECT (handler));
902 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
903 EmpathyFTHandlerReadyCallback callback,
906 EmpathyFTHandler *handler;
910 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
912 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
913 "tp-file", tp_file, NULL);
915 g_object_get (tp_file, "channel", &channel, NULL);
917 data = g_slice_new0 (CallbacksData);
918 data->callback = callback;
919 data->user_data = user_data;
920 data->handler = g_object_ref (handler);
922 tp_cli_dbus_properties_call_get_all (channel,
923 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
924 channel_get_all_properties_cb, data, callbacks_data_free, G_OBJECT (handler));
928 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
930 EmpathyFTHandlerPriv *priv;
932 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
934 priv = GET_PRIV (handler);
936 if (priv->tpfile == NULL)
938 ft_handler_complete_request (handler);
942 /* TODO: add support for resume. */
943 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
944 ft_transfer_progress_callback, handler,
945 ft_transfer_operation_callback, handler);
950 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
952 EmpathyFTHandlerPriv *priv;
954 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
956 priv = GET_PRIV (handler);
958 /* if we don't have an EmpathyTpFile, we are hashing, so
959 * we can just cancel the GCancellable to stop it.
961 if (priv->tpfile == NULL)
962 g_cancellable_cancel (priv->cancellable);
964 empathy_tp_file_cancel (priv->tpfile);
968 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
971 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
972 g_return_if_fail (G_IS_FILE (destination));
974 g_object_set (handler, "gfile", destination, NULL);
978 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
980 EmpathyFTHandlerPriv *priv;
982 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
984 priv = GET_PRIV (handler);
986 return priv->filename;
990 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
992 EmpathyFTHandlerPriv *priv;
994 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
996 priv = GET_PRIV (handler);
998 return priv->content_type;
1002 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1004 EmpathyFTHandlerPriv *priv;
1006 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1008 priv = GET_PRIV (handler);
1010 return priv->contact;
1014 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1016 EmpathyFTHandlerPriv *priv;
1018 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1020 priv = GET_PRIV (handler);
1026 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1028 EmpathyFTHandlerPriv *priv;
1030 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1032 priv = GET_PRIV (handler);
1034 if (priv->tpfile == NULL)
1037 return empathy_tp_file_is_incoming (priv->tpfile);
1041 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1043 EmpathyFTHandlerPriv *priv;
1045 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1047 priv = GET_PRIV (handler);
1049 return priv->transferred_bytes;
1053 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1055 EmpathyFTHandlerPriv *priv;
1057 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1059 priv = GET_PRIV (handler);
1061 return priv->total_bytes;
1065 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1067 EmpathyFTHandlerPriv *priv;
1069 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1071 priv = GET_PRIV (handler);
1073 return priv->is_completed;
1077 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1079 EmpathyFTHandlerPriv *priv;
1081 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1083 priv = GET_PRIV (handler);
1085 return g_cancellable_is_cancelled (priv->cancellable);