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-tp-contact-factory.h"
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-time.h"
33 #include "empathy-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_FT
36 #include "empathy-debug.h"
39 * SECTION:empathy-ft-handler
40 * @title: EmpathyFTHandler
41 * @short_description: an object representing a File Transfer
42 * @include: libempathy/empathy-ft-handler
44 * #EmpathyFTHandler is the object which represents a File Transfer with all
46 * The creation of an #EmpathyFTHandler is done with
47 * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
48 * even though clients should not need to call them directly, as
49 * #EmpathyFTFactory does it for them. Remember that for the file transfer
50 * to work with an incoming handler,
51 * empathy_ft_handler_incoming_set_destination() should be called after
52 * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
54 * It's important to note that, as the creation of the handlers is async, once
55 * an handler is created, it already has all the interesting properties set,
56 * like filename, total bytes, content type and so on, making it useful
57 * to be displayed in an UI.
58 * The transfer API works like a state machine; it has three signals,
59 * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
60 * emitted in the relevant phases.
61 * In addition, if the handler is created with checksumming enabled,
62 * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
63 * will be emitted before or after the transfer, depending on the direction
64 * (respectively outgoing and incoming) of the handler.
65 * At any time between the call to empathy_ft_handler_start_transfer() and
66 * the last signal, a ::transfer-error can be emitted, indicating that an
67 * error has happened in the operation. The message of the error is localized
71 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
73 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
75 #define BUFFER_SIZE 4096
102 EmpathyFTHandler *handler;
106 EmpathyFTHandlerReadyCallback callback;
108 EmpathyFTHandler *handler;
113 gboolean dispose_run;
116 EmpathyTpFile *tpfile;
117 GCancellable *cancellable;
120 /* request for the new transfer */
123 /* transfer properties */
124 EmpathyContact *contact;
129 guint64 transferred_bytes;
132 TpFileHashType content_hash_type;
133 TpFileTransferState current_state;
137 guint remaining_time;
138 time_t last_update_time;
140 gboolean is_completed;
141 } EmpathyFTHandlerPriv;
143 static guint signals[LAST_SIGNAL] = { 0 };
145 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
146 GCancellable *cancellable, gpointer user_data);
148 /* GObject implementations */
150 do_get_property (GObject *object,
155 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
160 g_value_set_object (value, priv->contact);
163 g_value_set_object (value, priv->gfile);
166 g_value_set_object (value, priv->tpfile);
169 g_value_set_boolean (value, priv->use_hash);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
177 do_set_property (GObject *object,
182 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
187 priv->contact = g_value_dup_object (value);
190 priv->gfile = g_value_dup_object (value);
193 priv->tpfile = g_value_dup_object (value);
196 priv->use_hash = g_value_get_boolean (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204 do_dispose (GObject *object)
206 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
208 if (priv->dispose_run)
211 priv->dispose_run = TRUE;
213 if (priv->contact != NULL) {
214 g_object_unref (priv->contact);
215 priv->contact = NULL;
218 if (priv->gfile != NULL) {
219 g_object_unref (priv->gfile);
223 if (priv->tpfile != NULL) {
224 empathy_tp_file_close (priv->tpfile);
225 g_object_unref (priv->tpfile);
229 if (priv->cancellable != NULL) {
230 g_object_unref (priv->cancellable);
231 priv->cancellable = NULL;
234 if (priv->request != NULL)
236 g_hash_table_unref (priv->request);
237 priv->request = NULL;
240 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
244 do_finalize (GObject *object)
246 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
248 DEBUG ("%p", object);
250 g_free (priv->content_type);
251 priv->content_type = NULL;
253 g_free (priv->filename);
254 priv->filename = NULL;
256 g_free (priv->description);
257 priv->description = NULL;
259 g_free (priv->content_hash);
260 priv->content_hash = NULL;
262 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
266 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
268 GObjectClass *object_class = G_OBJECT_CLASS (klass);
269 GParamSpec *param_spec;
271 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
273 object_class->get_property = do_get_property;
274 object_class->set_property = do_set_property;
275 object_class->dispose = do_dispose;
276 object_class->finalize = do_finalize;
281 * EmpathyFTHandler:contact:
283 * The remote #EmpathyContact for the transfer
285 param_spec = g_param_spec_object ("contact",
286 "contact", "The remote contact",
287 EMPATHY_TYPE_CONTACT,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
289 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
292 * EmpathyFTHandler:gfile:
294 * The #GFile object where the transfer actually happens
296 param_spec = g_param_spec_object ("gfile",
297 "gfile", "The GFile we're handling",
299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
300 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
303 * EmpathyFTHandler:tp-file:
305 * The underlying #EmpathyTpFile managing the transfer
307 param_spec = g_param_spec_object ("tp-file",
308 "tp-file", "The file's channel wrapper",
309 EMPATHY_TYPE_TP_FILE,
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
311 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
314 * EmpathyFTHandler:use-hash:
316 * %TRUE if checksumming is enabled for the handler, %FALSE otherwise
318 param_spec = g_param_spec_boolean ("use-hash",
319 "use-hash", "Whether we should use checksum when sending or receiving",
320 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
321 g_object_class_install_property (object_class, PROP_USE_HASH, param_spec);
326 * EmpathyFTHandler::transfer-started
327 * @handler: the object which has received the signal
328 * @tp_file: the #EmpathyTpFile for which the transfer has started
330 * This signal is emitted when the actual transfer starts.
332 signals[TRANSFER_STARTED] =
333 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
334 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
335 g_cclosure_marshal_VOID__OBJECT,
337 1, EMPATHY_TYPE_TP_FILE);
340 * EmpathyFTHandler::transfer-done
341 * @handler: the object which has received the signal
342 * @tp_file: the #EmpathyTpFile for which the transfer has started
344 * This signal will be emitted when the actual transfer is completed
347 signals[TRANSFER_DONE] =
348 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
349 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
350 g_cclosure_marshal_VOID__OBJECT,
352 1, EMPATHY_TYPE_TP_FILE);
355 * EmpathyFTHandler::transfer-error
356 * @handler: the object which has received the signal
359 * This signal can be emitted anytime between the call to
360 * empathy_ft_handler_start_transfer() and the last expected signal
361 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
362 * signal coming from the handler, meaning that no other operation will
363 * take place after this signal.
365 signals[TRANSFER_ERROR] =
366 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
367 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
368 g_cclosure_marshal_VOID__POINTER,
373 * EmpathyFTHandler::transfer-progress
374 * @handler: the object which has received the signal
375 * @current_bytes: the bytes currently transferred
376 * @total_bytes: the total bytes of the handler
377 * @remaining_time: the number of seconds remaining for the transfer
379 * @speed: the current speed of the transfer (in KB/s)
381 * This signal is emitted to notify clients of the progress of the
384 signals[TRANSFER_PROGRESS] =
385 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
386 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
387 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
389 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
392 * EmpathyFTHandler::hashing-started
393 * @handler: the object which has received the signal
395 * This signal is emitted when the hashing operation of the handler
396 * is started. Note that this only happens if the handler is created
397 * with checksum enabled and, even if the option is set, is not
398 * guaranteed to happen for incoming handlers, as the CM might not
399 * support sending/receiving the file hash.
401 signals[HASHING_STARTED] =
402 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
403 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
404 g_cclosure_marshal_VOID__VOID,
408 * EmpathyFTHandler::hashing-progress
409 * @handler: the object which has received the signal
410 * @current_bytes: the bytes currently hashed
411 * @total_bytes: the total bytes of the handler
413 * This signal is emitted to notify clients of the progress of the
416 signals[HASHING_PROGRESS] =
417 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
418 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
419 _empathy_marshal_VOID__UINT64_UINT64,
421 2, G_TYPE_UINT64, G_TYPE_UINT64);
424 * EmpathyFTHandler::hashing-done
425 * @handler: the object which has received the signal
427 * This signal is emitted when the hashing operation of the handler
430 signals[HASHING_DONE] =
431 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
432 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
433 g_cclosure_marshal_VOID__VOID,
438 empathy_ft_handler_init (EmpathyFTHandler *self)
440 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
441 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
444 priv->cancellable = g_cancellable_new ();
447 /* private functions */
450 hash_data_free (HashingData *data)
452 if (data->buffer != NULL)
454 g_free (data->buffer);
458 if (data->stream != NULL)
460 g_object_unref (data->stream);
464 if (data->checksum != NULL)
466 g_checksum_free (data->checksum);
467 data->checksum = NULL;
470 if (data->error != NULL)
472 g_error_free (data->error);
475 if (data->handler != NULL)
477 g_object_unref (data->handler);
478 data->handler = NULL;
481 g_slice_free (HashingData, data);
485 tp_file_hash_to_g_checksum (TpFileHashType type)
487 GChecksumType retval;
491 case TP_FILE_HASH_TYPE_MD5:
492 retval = G_CHECKSUM_MD5;
494 case TP_FILE_HASH_TYPE_SHA1:
495 retval = G_CHECKSUM_SHA1;
497 case TP_FILE_HASH_TYPE_SHA256:
498 retval = G_CHECKSUM_SHA256;
501 g_assert_not_reached ();
509 check_hash_incoming (EmpathyFTHandler *handler)
511 HashingData *hash_data;
512 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
514 if (!EMP_STR_EMPTY (priv->content_hash))
516 hash_data = g_slice_new0 (HashingData);
517 hash_data->total_bytes = priv->total_bytes;
518 hash_data->handler = g_object_ref (handler);
519 hash_data->checksum = g_checksum_new
520 (tp_file_hash_to_g_checksum (priv->content_hash_type));
522 g_signal_emit (handler, signals[HASHING_STARTED], 0);
524 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
525 G_PRIORITY_DEFAULT, priv->cancellable);
530 emit_error_signal (EmpathyFTHandler *handler,
533 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
535 if (!g_cancellable_is_cancelled (priv->cancellable))
536 g_cancellable_cancel (priv->cancellable);
538 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
542 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
546 EmpathyFTHandler *handler = user_data;
547 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
549 DEBUG ("Transfer operation callback, error %p", error);
553 emit_error_signal (handler, error);
557 priv->is_completed = TRUE;
558 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
560 empathy_tp_file_close (tp_file);
562 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
564 check_hash_incoming (handler);
570 update_remaining_time_and_speed (EmpathyFTHandler *handler,
571 guint64 transferred_bytes)
573 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
574 time_t elapsed_time, current_time;
575 guint64 transferred, last_transferred_bytes;
579 last_transferred_bytes = priv->transferred_bytes;
580 priv->transferred_bytes = transferred_bytes;
582 current_time = empathy_time_get_current ();
583 elapsed_time = current_time - priv->last_update_time;
585 if (elapsed_time >= 1)
587 transferred = transferred_bytes - last_transferred_bytes;
588 speed = (gdouble) transferred / (gdouble) elapsed_time;
589 remaining_time = (priv->total_bytes - transferred) / speed;
591 priv->remaining_time = remaining_time;
592 priv->last_update_time = current_time;
597 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
598 guint64 transferred_bytes,
601 EmpathyFTHandler *handler = user_data;
602 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
604 if (empathy_ft_handler_is_cancelled (handler))
607 if (transferred_bytes == 0)
609 priv->last_update_time = empathy_time_get_current ();
610 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
613 if (priv->transferred_bytes != transferred_bytes)
615 update_remaining_time_and_speed (handler, transferred_bytes);
617 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
618 transferred_bytes, priv->total_bytes, priv->remaining_time,
624 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
628 EmpathyFTHandler *handler = user_data;
629 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
630 GError *my_error = (GError *) error;
632 DEBUG ("Dispatcher create channel CB");
634 if (my_error == NULL)
636 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
639 if (my_error != NULL)
641 emit_error_signal (handler, my_error);
643 if (my_error != error)
644 g_clear_error (&my_error);
649 priv->tpfile = g_object_ref
650 (empathy_dispatch_operation_get_channel_wrapper (operation));
652 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
653 ft_transfer_progress_callback, handler,
654 ft_transfer_operation_callback, handler);
656 empathy_dispatch_operation_claim (operation);
660 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
662 EmpathyDispatcher *dispatcher;
663 TpConnection *connection;
664 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
666 DEBUG ("Pushing request to the dispatcher");
668 dispatcher = empathy_dispatcher_dup_singleton ();
669 connection = empathy_contact_get_connection (priv->contact);
671 /* I want to own a reference to the request, and destroy it later */
672 empathy_dispatcher_create_channel (dispatcher, connection,
673 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
675 g_object_unref (dispatcher);
679 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
681 EmpathyDispatcher *dispatcher;
682 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
683 TpConnection *connection;
687 dispatcher = empathy_dispatcher_dup_singleton ();
688 connection = empathy_contact_get_connection (priv->contact);
690 allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
691 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
693 if (!tp_strv_contains ((const gchar * const *) allowed,
694 TP_IFACE_CHANNEL ".TargetHandle"))
697 g_object_unref (dispatcher);
703 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
705 guint contact_handle;
708 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
710 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
711 NULL, (GDestroyNotify) tp_g_value_slice_free);
713 contact_handle = empathy_contact_get_handle (priv->contact);
715 /* org.freedesktop.Telepathy.Channel.ChannelType */
716 value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
717 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
719 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
720 value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT);
721 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
723 /* org.freedesktop.Telepathy.Channel.TargetHandle */
724 value = tp_g_value_slice_new_uint (contact_handle);
725 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
727 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
728 value = tp_g_value_slice_new_string (priv->content_type);
729 g_hash_table_insert (request,
730 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
732 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
733 value = tp_g_value_slice_new_string (priv->filename);
734 g_hash_table_insert (request,
735 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
737 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
738 value = tp_g_value_slice_new_uint64 (priv->total_bytes);
739 g_hash_table_insert (request,
740 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
742 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
743 value = tp_g_value_slice_new_uint64 ((guint64) priv->mtime);
744 g_hash_table_insert (request,
745 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
749 hash_job_done (gpointer user_data)
751 HashingData *hash_data = user_data;
752 EmpathyFTHandler *handler = hash_data->handler;
753 EmpathyFTHandlerPriv *priv;
754 GError *error = NULL;
757 DEBUG ("Closing stream after hashing.");
759 priv = GET_PRIV (handler);
761 if (hash_data->error != NULL)
763 error = hash_data->error;
767 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
769 if (empathy_ft_handler_is_incoming (handler))
771 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
774 DEBUG ("Hash mismatch when checking incoming handler: "
775 "received %s, calculated %s", priv->content_hash,
776 g_checksum_get_string (hash_data->checksum));
778 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
779 EMPATHY_FT_ERROR_HASH_MISMATCH,
780 _("The hash of the received file and the "
781 "sent one do not match"));
786 DEBUG ("Hash verification matched, received %s, calculated %s",
788 g_checksum_get_string (hash_data->checksum));
793 /* set the checksum in the request...
794 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
796 value = tp_g_value_slice_new_string
797 (g_checksum_get_string (hash_data->checksum));
798 g_hash_table_insert (priv->request,
799 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
806 emit_error_signal (handler, error);
807 g_clear_error (&error);
811 g_signal_emit (handler, signals[HASHING_DONE], 0);
813 if (!empathy_ft_handler_is_incoming (handler))
814 /* the request is complete now, push it to the dispatcher */
815 ft_handler_push_to_dispatcher (handler);
818 hash_data_free (hash_data);
824 emit_hashing_progress (gpointer user_data)
826 HashingData *hash_data = user_data;
828 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
829 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
835 do_hash_job (GIOSchedulerJob *job,
836 GCancellable *cancellable,
839 HashingData *hash_data = user_data;
841 EmpathyFTHandlerPriv *priv;
842 GError *error = NULL;
844 priv = GET_PRIV (hash_data->handler);
847 if (hash_data->buffer == NULL)
848 hash_data->buffer = g_slice_alloc0 (BUFFER_SIZE);
850 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
851 BUFFER_SIZE, cancellable, &error);
855 hash_data->total_read += bytes_read;
857 /* we now have the chunk */
860 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
861 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
864 g_slice_free (guchar, hash_data->buffer);
865 hash_data->buffer = NULL;
871 g_input_stream_close (hash_data->stream, cancellable, &error);
876 hash_data->error = error;
878 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
885 do_hash_job_incoming (GIOSchedulerJob *job,
886 GCancellable *cancellable,
889 HashingData *hash_data = user_data;
890 EmpathyFTHandler *handler = hash_data->handler;
891 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
892 GError *error = NULL;
894 DEBUG ("checking integrity for incoming handler");
896 /* need to get the stream first */
898 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
902 hash_data->error = error;
903 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
908 return do_hash_job (job, cancellable, user_data);
912 ft_handler_read_async_cb (GObject *source,
916 GFileInputStream *stream;
917 GError *error = NULL;
918 HashingData *hash_data;
920 EmpathyFTHandler *handler = user_data;
921 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
923 DEBUG ("GFile read async CB.");
925 stream = g_file_read_finish (priv->gfile, res, &error);
928 emit_error_signal (handler, error);
929 g_clear_error (&error);
934 hash_data = g_slice_new0 (HashingData);
935 hash_data->stream = G_INPUT_STREAM (stream);
936 hash_data->total_bytes = priv->total_bytes;
937 hash_data->handler = g_object_ref (handler);
938 /* FIXME: should look at the CM capabilities before setting the
941 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
943 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
944 value = tp_g_value_slice_new_uint (TP_FILE_HASH_TYPE_MD5);
945 g_hash_table_insert (priv->request,
946 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
948 g_signal_emit (handler, signals[HASHING_STARTED], 0);
950 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
951 G_PRIORITY_DEFAULT, priv->cancellable);
955 ft_handler_complete_request (EmpathyFTHandler *handler)
957 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
958 GError *myerr = NULL;
960 /* check if FT is allowed before firing up the I/O machinery */
961 if (!ft_handler_check_if_allowed (handler))
963 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
964 EMPATHY_FT_ERROR_NOT_SUPPORTED,
965 _("File transfer not supported by remote contact"));
967 emit_error_signal (handler, myerr);
968 g_clear_error (&myerr);
973 /* populate the request table with all the known properties */
974 ft_handler_populate_outgoing_request (handler);
977 /* start hashing the file */
978 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
979 priv->cancellable, ft_handler_read_async_cb, handler);
981 /* push directly the handler to the dispatcher */
982 ft_handler_push_to_dispatcher (handler);
986 callbacks_data_free (gpointer user_data)
988 CallbacksData *data = user_data;
990 if (data->handler != NULL)
991 g_object_unref (data->handler);
993 g_slice_free (CallbacksData, data);
997 ft_handler_gfile_ready_cb (GObject *source,
999 CallbacksData *cb_data)
1002 GError *error = NULL;
1004 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1006 DEBUG ("Got GFileInfo.");
1008 info = g_file_query_info_finish (priv->gfile, res, &error);
1013 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1014 priv->filename = g_strdup (g_file_info_get_display_name (info));
1015 priv->total_bytes = g_file_info_get_size (info);
1016 g_file_info_get_modification_time (info, &mtime);
1017 priv->mtime = mtime.tv_sec;
1018 priv->transferred_bytes = 0;
1019 priv->description = NULL;
1021 g_object_unref (info);
1026 cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
1030 if (!g_cancellable_is_cancelled (priv->cancellable))
1031 g_cancellable_cancel (priv->cancellable);
1033 cb_data->callback (NULL, error, cb_data->user_data);
1034 g_error_free (error);
1035 g_object_unref (cb_data->handler);
1038 callbacks_data_free (cb_data);
1042 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
1043 EmpathyContact *contact,
1044 const GError *error,
1046 GObject *weak_object)
1048 CallbacksData *cb_data = user_data;
1049 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1050 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1054 if (!g_cancellable_is_cancelled (priv->cancellable))
1055 g_cancellable_cancel (priv->cancellable);
1057 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
1058 g_object_unref (handler);
1062 priv->contact = contact;
1064 cb_data->callback (handler, NULL, cb_data->user_data);
1068 channel_get_all_properties_cb (TpProxy *proxy,
1069 GHashTable *properties,
1070 const GError *error,
1072 GObject *weak_object)
1074 CallbacksData *cb_data = user_data;
1075 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1076 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1077 EmpathyTpContactFactory *c_factory;
1082 if (!g_cancellable_is_cancelled (priv->cancellable))
1083 g_cancellable_cancel (priv->cancellable);
1085 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
1086 g_object_unref (handler);
1090 priv->total_bytes = g_value_get_uint64 (
1091 g_hash_table_lookup (properties, "Size"));
1093 priv->transferred_bytes = g_value_get_uint64 (
1094 g_hash_table_lookup (properties, "TransferredBytes"));
1096 priv->filename = g_value_dup_string (
1097 g_hash_table_lookup (properties, "Filename"));
1099 priv->content_hash = g_value_dup_string (
1100 g_hash_table_lookup (properties, "ContentHash"));
1102 priv->content_hash_type = g_value_get_uint (
1103 g_hash_table_lookup (properties, "ContentHashType"));
1105 priv->content_type = g_value_dup_string (
1106 g_hash_table_lookup (properties, "ContentType"));
1108 priv->description = g_value_dup_string (
1109 g_hash_table_lookup (properties, "Description"));
1111 c_factory = empathy_tp_contact_factory_dup_singleton
1112 (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
1113 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1114 empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
1115 contact_factory_contact_cb, cb_data, callbacks_data_free,
1116 G_OBJECT (handler));
1118 g_object_unref (c_factory);
1121 /* public methods */
1124 * empathy_ft_handler_new_outgoing:
1125 * @contact: the #EmpathyContact to send @source to
1126 * @source: the #GFile to send
1127 * @use_hash: whether the handler should send a checksum of the file
1128 * @callback: callback to be called when the handler has been created
1129 * @user_data: user data to be passed to @callback
1131 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1134 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1137 EmpathyFTHandlerReadyCallback callback,
1140 EmpathyFTHandler *handler;
1141 CallbacksData *data;
1142 EmpathyFTHandlerPriv *priv;
1144 DEBUG ("New handler outgoing, use hash %s",
1145 use_hash ? "True" : "False");
1147 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1148 g_return_if_fail (G_IS_FILE (source));
1150 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1151 "contact", contact, "gfile", source, "use-hash", use_hash, NULL);
1153 priv = GET_PRIV (handler);
1155 data = g_slice_new0 (CallbacksData);
1156 data->callback = callback;
1157 data->user_data = user_data;
1158 data->handler = g_object_ref (handler);
1160 /* start collecting info about the file */
1161 g_file_query_info_async (priv->gfile,
1162 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1163 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1164 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1165 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1166 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1167 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1171 * empathy_ft_handler_new_incoming:
1172 * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1173 * @callback: callback to be called when the handler has been created
1174 * @user_data: user data to be passed to @callback
1176 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1177 * Note that for the handler to be useful, you will have to set a destination
1178 * file with empathy_ft_handler_incoming_set_destination() after the handler
1182 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1183 EmpathyFTHandlerReadyCallback callback,
1186 EmpathyFTHandler *handler;
1188 CallbacksData *data;
1190 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1192 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1193 "tp-file", tp_file, NULL);
1195 g_object_get (tp_file, "channel", &channel, NULL);
1197 data = g_slice_new0 (CallbacksData);
1198 data->callback = callback;
1199 data->user_data = user_data;
1200 data->handler = g_object_ref (handler);
1202 tp_cli_dbus_properties_call_get_all (channel,
1203 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1204 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1208 * empathy_ft_handler_start_transfer:
1209 * @handler: an #EmpathyFTHandler
1211 * Starts the transfer machinery. After this call, the transfer and hashing
1212 * signals will be emitted by the handler.
1215 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1217 EmpathyFTHandlerPriv *priv;
1219 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1221 priv = GET_PRIV (handler);
1223 if (priv->tpfile == NULL)
1225 ft_handler_complete_request (handler);
1229 /* TODO: add support for resume. */
1230 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1231 ft_transfer_progress_callback, handler,
1232 ft_transfer_operation_callback, handler);
1237 * empathy_ft_handler_cancel_transfer:
1238 * @handler: an #EmpathyFTHandler
1240 * Cancels an ongoing handler operation. Note that this doesn't destroy
1241 * the object, which will keep all the properties, altough it won't be able
1242 * to do any more I/O.
1245 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1247 EmpathyFTHandlerPriv *priv;
1249 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1251 priv = GET_PRIV (handler);
1253 /* if we don't have an EmpathyTpFile, we are hashing, so
1254 * we can just cancel the GCancellable to stop it.
1256 if (priv->tpfile == NULL)
1257 g_cancellable_cancel (priv->cancellable);
1259 empathy_tp_file_cancel (priv->tpfile);
1263 * empathy_ft_handler_incoming_set_destination:
1264 * @handler: an #EmpathyFTHandler
1265 * @destination: the #GFile where the transfer should be saved
1266 * @use_hash: whether the handler should, after the transfer, try to
1267 * validate it with checksum.
1269 * Sets the destination of the incoming handler to be @destination.
1270 * Note that calling this method is mandatory before starting the transfer
1271 * for incoming handlers.
1274 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1278 DEBUG ("Set incoming destination, use hash %s",
1279 use_hash ? "True" : "False");
1281 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1282 g_return_if_fail (G_IS_FILE (destination));
1284 g_object_set (handler, "gfile", destination,
1285 "use-hash", use_hash, NULL);
1289 * empathy_ft_handler_get_filename:
1290 * @handler: an #EmpathyFTHandler
1292 * Returns the name of the file being transferred.
1294 * Return value: the name of the file being transferred
1297 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1299 EmpathyFTHandlerPriv *priv;
1301 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1303 priv = GET_PRIV (handler);
1305 return priv->filename;
1309 * empathy_ft_handler_get_content_type:
1310 * @handler: an #EmpathyFTHandler
1312 * Returns the content type of the file being transferred.
1314 * Return value: the content type of the file being transferred
1317 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1319 EmpathyFTHandlerPriv *priv;
1321 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1323 priv = GET_PRIV (handler);
1325 return priv->content_type;
1329 * empathy_ft_handler_get_contact:
1330 * @handler: an #EmpathyFTHandler
1332 * Returns the remote #EmpathyContact at the other side of the transfer.
1334 * Return value: the remote #EmpathyContact for @handler
1337 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1339 EmpathyFTHandlerPriv *priv;
1341 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1343 priv = GET_PRIV (handler);
1345 return priv->contact;
1349 * empathy_ft_handler_get_gfile:
1350 * @handler: an #EmpathyFTHandler
1352 * Returns the #GFile where the transfer is being read/saved.
1354 * Return value: the #GFile where the transfer is being read/saved
1357 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1359 EmpathyFTHandlerPriv *priv;
1361 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1363 priv = GET_PRIV (handler);
1369 * empathy_ft_handler_get_use_hash:
1370 * @handler: an #EmpathyFTHandler
1372 * Returns whether @handler has checksumming enabled.
1374 * Return value: %TRUE if the handler has checksumming enabled,
1378 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1380 EmpathyFTHandlerPriv *priv;
1382 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1384 priv = GET_PRIV (handler);
1386 return priv->use_hash;
1390 * empathy_ft_handler_is_incoming:
1391 * @handler: an #EmpathyFTHandler
1393 * Returns whether @handler is incoming or outgoing.
1395 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1398 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1400 EmpathyFTHandlerPriv *priv;
1402 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1404 priv = GET_PRIV (handler);
1406 if (priv->tpfile == NULL)
1409 return empathy_tp_file_is_incoming (priv->tpfile);
1413 * empathy_ft_handler_get_transferred_bytes:
1414 * @handler: an #EmpathyFTHandler
1416 * Returns the number of bytes already transferred by the handler.
1418 * Return value: the number of bytes already transferred by the handler.
1421 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1423 EmpathyFTHandlerPriv *priv;
1425 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1427 priv = GET_PRIV (handler);
1429 return priv->transferred_bytes;
1433 * empathy_ft_handler_get_total_bytes:
1434 * @handler: an #EmpathyFTHandler
1436 * Returns the total size of the file being transferred by the handler.
1438 * Return value: a number of bytes indicating the total size of the file being
1439 * transferred by the handler.
1442 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1444 EmpathyFTHandlerPriv *priv;
1446 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1448 priv = GET_PRIV (handler);
1450 return priv->total_bytes;
1454 * empathy_ft_handler_is_completed:
1455 * @handler: an #EmpathyFTHandler
1457 * Returns whether the transfer for @handler has been completed succesfully.
1459 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1463 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1465 EmpathyFTHandlerPriv *priv;
1467 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1469 priv = GET_PRIV (handler);
1471 return priv->is_completed;
1475 * empathy_ft_handler_is_cancelled:
1476 * @handler: an #EmpathyFTHandler
1478 * Returns whether the transfer for @handler has been cancelled or has stopped
1481 * Return value: %TRUE if the transfer for @handler has been cancelled
1482 * or has stopped due to an error, %FALSE otherwise.
1485 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1487 EmpathyFTHandlerPriv *priv;
1489 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1491 priv = GET_PRIV (handler);
1493 return g_cancellable_is_cancelled (priv->cancellable);