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>
27 #include <telepathy-glib/dbus.h>
29 #include "empathy-ft-handler.h"
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.h"
33 #include "empathy-time.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_FT
37 #include "empathy-debug.h"
40 * SECTION:empathy-ft-handler
41 * @title: EmpathyFTHandler
42 * @short_description: an object representing a File Transfer
43 * @include: libempathy/empathy-ft-handler
45 * #EmpathyFTHandler is the object which represents a File Transfer with all
47 * The creation of an #EmpathyFTHandler is done with
48 * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
49 * even though clients should not need to call them directly, as
50 * #EmpathyFTFactory does it for them. Remember that for the file transfer
51 * to work with an incoming handler,
52 * empathy_ft_handler_incoming_set_destination() should be called after
53 * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
55 * It's important to note that, as the creation of the handlers is async, once
56 * an handler is created, it already has all the interesting properties set,
57 * like filename, total bytes, content type and so on, making it useful
58 * to be displayed in an UI.
59 * The transfer API works like a state machine; it has three signals,
60 * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
61 * emitted in the relevant phases.
62 * In addition, if the handler is created with checksumming enabled,
63 * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
64 * will be emitted before or after the transfer, depending on the direction
65 * (respectively outgoing and incoming) of the handler.
66 * At any time between the call to empathy_ft_handler_start_transfer() and
67 * the last signal, a ::transfer-error can be emitted, indicating that an
68 * error has happened in the operation. The message of the error is localized
72 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
74 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
76 #define BUFFER_SIZE 4096
85 PROP_MODIFICATION_TIME,
87 PROP_TRANSFERRED_BYTES
102 GInputStream *stream;
103 GError *error /* comment to make the style checker happy */;
108 EmpathyFTHandler *handler;
112 EmpathyFTHandlerReadyCallback callback;
114 EmpathyFTHandler *handler;
119 gboolean dispose_run;
122 EmpathyTpFile *tpfile;
123 GCancellable *cancellable;
126 EmpathyDispatcher *dispatcher;
128 /* request for the new transfer */
131 /* transfer properties */
132 EmpathyContact *contact;
137 guint64 transferred_bytes;
140 TpFileHashType content_hash_type;
144 guint remaining_time;
145 time_t last_update_time;
147 gboolean is_completed;
148 } EmpathyFTHandlerPriv;
150 static guint signals[LAST_SIGNAL] = { 0 };
152 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
153 GCancellable *cancellable, gpointer user_data);
155 /* GObject implementations */
157 do_get_property (GObject *object,
162 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
167 g_value_set_object (value, priv->contact);
169 case PROP_CONTENT_TYPE:
170 g_value_set_string (value, priv->content_type);
172 case PROP_DESCRIPTION:
173 g_value_set_string (value, priv->description);
176 g_value_set_string (value, priv->filename);
178 case PROP_MODIFICATION_TIME:
179 g_value_set_uint64 (value, priv->mtime);
181 case PROP_TOTAL_BYTES:
182 g_value_set_uint64 (value, priv->total_bytes);
184 case PROP_TRANSFERRED_BYTES:
185 g_value_set_uint64 (value, priv->transferred_bytes);
188 g_value_set_object (value, priv->gfile);
191 g_value_set_object (value, priv->tpfile);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
199 do_set_property (GObject *object,
204 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
209 priv->contact = g_value_dup_object (value);
211 case PROP_CONTENT_TYPE:
212 priv->content_type = g_value_dup_string (value);
214 case PROP_DESCRIPTION:
215 priv->description = g_value_dup_string (value);
218 priv->filename = g_value_dup_string (value);
220 case PROP_MODIFICATION_TIME:
221 priv->mtime = g_value_get_uint64 (value);
223 case PROP_TOTAL_BYTES:
224 priv->total_bytes = g_value_get_uint64 (value);
226 case PROP_TRANSFERRED_BYTES:
227 priv->transferred_bytes = g_value_get_uint64 (value);
230 priv->gfile = g_value_dup_object (value);
233 priv->tpfile = g_value_dup_object (value);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
241 do_dispose (GObject *object)
243 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
245 if (priv->dispose_run)
248 priv->dispose_run = TRUE;
250 if (priv->contact != NULL) {
251 g_object_unref (priv->contact);
252 priv->contact = NULL;
255 if (priv->gfile != NULL) {
256 g_object_unref (priv->gfile);
260 if (priv->tpfile != NULL) {
261 empathy_tp_file_close (priv->tpfile);
262 g_object_unref (priv->tpfile);
266 if (priv->cancellable != NULL) {
267 g_object_unref (priv->cancellable);
268 priv->cancellable = NULL;
271 if (priv->request != NULL)
273 g_hash_table_unref (priv->request);
274 priv->request = NULL;
277 if (priv->dispatcher != NULL)
279 g_object_unref (priv->dispatcher);
280 priv->dispatcher = NULL;
283 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
287 do_finalize (GObject *object)
289 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
291 DEBUG ("%p", object);
293 g_free (priv->content_type);
294 priv->content_type = NULL;
296 g_free (priv->filename);
297 priv->filename = NULL;
299 g_free (priv->description);
300 priv->description = NULL;
302 g_free (priv->content_hash);
303 priv->content_hash = NULL;
305 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
309 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
311 GObjectClass *object_class = G_OBJECT_CLASS (klass);
312 GParamSpec *param_spec;
314 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
316 object_class->get_property = do_get_property;
317 object_class->set_property = do_set_property;
318 object_class->dispose = do_dispose;
319 object_class->finalize = do_finalize;
324 * EmpathyFTHandler:contact:
326 * The remote #EmpathyContact for the transfer
328 param_spec = g_param_spec_object ("contact",
329 "contact", "The remote contact",
330 EMPATHY_TYPE_CONTACT,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
332 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
335 * EmpathyFTHandler:content-type:
337 * The content type of the file being transferred
339 param_spec = g_param_spec_string ("content-type",
340 "content-type", "The content type of the file", NULL,
341 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
342 g_object_class_install_property (object_class,
343 PROP_CONTENT_TYPE, param_spec);
346 * EmpathyFTHandler:description:
348 * The description of the file being transferred
350 param_spec = g_param_spec_string ("description",
351 "description", "The description of the file", NULL,
352 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
353 g_object_class_install_property (object_class,
354 PROP_DESCRIPTION, param_spec);
357 * EmpathyFTHandler:filename:
359 * The name of the file being transferred
361 param_spec = g_param_spec_string ("filename",
362 "filename", "The name of the file", NULL,
363 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
364 g_object_class_install_property (object_class,
365 PROP_FILENAME, param_spec);
368 * EmpathyFTHandler:modification-time:
370 * The modification time of the file being transferred
372 param_spec = g_param_spec_uint64 ("modification-time",
373 "modification-time", "The mtime of the file", 0,
374 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
375 g_object_class_install_property (object_class,
376 PROP_MODIFICATION_TIME, param_spec);
379 * EmpathyFTHandler:total-bytes:
381 * The size (in bytes) of the file being transferred
383 param_spec = g_param_spec_uint64 ("total-bytes",
384 "total-bytes", "The size of the file", 0,
385 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
386 g_object_class_install_property (object_class,
387 PROP_TOTAL_BYTES, param_spec);
390 * EmpathyFTHandler:transferred-bytes:
392 * The number of the bytes already transferred
394 param_spec = g_param_spec_uint64 ("transferred-bytes",
395 "transferred-bytes", "The number of bytes already transferred", 0,
396 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
397 g_object_class_install_property (object_class,
398 PROP_TRANSFERRED_BYTES, param_spec);
401 * EmpathyFTHandler:gfile:
403 * The #GFile object where the transfer actually happens
405 param_spec = g_param_spec_object ("gfile",
406 "gfile", "The GFile we're handling",
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
409 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
412 * EmpathyFTHandler:tp-file:
414 * The underlying #EmpathyTpFile managing the transfer
416 param_spec = g_param_spec_object ("tp-file",
417 "tp-file", "The file's channel wrapper",
418 EMPATHY_TYPE_TP_FILE,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
420 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
425 * EmpathyFTHandler::transfer-started
426 * @handler: the object which has received the signal
427 * @tp_file: the #EmpathyTpFile for which the transfer has started
429 * This signal is emitted when the actual transfer starts.
431 signals[TRANSFER_STARTED] =
432 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
433 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
434 g_cclosure_marshal_VOID__OBJECT,
436 1, EMPATHY_TYPE_TP_FILE);
439 * EmpathyFTHandler::transfer-done
440 * @handler: the object which has received the signal
441 * @tp_file: the #EmpathyTpFile for which the transfer has started
443 * This signal will be emitted when the actual transfer is completed
446 signals[TRANSFER_DONE] =
447 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
448 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
449 g_cclosure_marshal_VOID__OBJECT,
451 1, EMPATHY_TYPE_TP_FILE);
454 * EmpathyFTHandler::transfer-error
455 * @handler: the object which has received the signal
458 * This signal can be emitted anytime between the call to
459 * empathy_ft_handler_start_transfer() and the last expected signal
460 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
461 * signal coming from the handler, meaning that no other operation will
462 * take place after this signal.
464 signals[TRANSFER_ERROR] =
465 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
466 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
467 g_cclosure_marshal_VOID__POINTER,
472 * EmpathyFTHandler::transfer-progress
473 * @handler: the object which has received the signal
474 * @current_bytes: the bytes currently transferred
475 * @total_bytes: the total bytes of the handler
476 * @remaining_time: the number of seconds remaining for the transfer
478 * @speed: the current speed of the transfer (in KB/s)
480 * This signal is emitted to notify clients of the progress of the
483 signals[TRANSFER_PROGRESS] =
484 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
485 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
486 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
488 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
491 * EmpathyFTHandler::hashing-started
492 * @handler: the object which has received the signal
494 * This signal is emitted when the hashing operation of the handler
495 * is started. Note that this might happen or not, depending on the CM
496 * and remote contact capabilities. Clients shoud use
497 * empathy_ft_handler_get_use_hash() before calling
498 * empathy_ft_handler_start_transfer() to know whether they should connect
501 signals[HASHING_STARTED] =
502 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
503 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
504 g_cclosure_marshal_VOID__VOID,
508 * EmpathyFTHandler::hashing-progress
509 * @handler: the object which has received the signal
510 * @current_bytes: the bytes currently hashed
511 * @total_bytes: the total bytes of the handler
513 * This signal is emitted to notify clients of the progress of the
516 signals[HASHING_PROGRESS] =
517 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
518 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
519 _empathy_marshal_VOID__UINT64_UINT64,
521 2, G_TYPE_UINT64, G_TYPE_UINT64);
524 * EmpathyFTHandler::hashing-done
525 * @handler: the object which has received the signal
527 * This signal is emitted when the hashing operation of the handler
530 signals[HASHING_DONE] =
531 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
532 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
533 g_cclosure_marshal_VOID__VOID,
538 empathy_ft_handler_init (EmpathyFTHandler *self)
540 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
541 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
544 priv->cancellable = g_cancellable_new ();
545 priv->dispatcher = empathy_dispatcher_dup_singleton ();
548 /* private functions */
551 hash_data_free (HashingData *data)
553 g_free (data->buffer);
555 if (data->stream != NULL)
556 g_object_unref (data->stream);
558 if (data->checksum != NULL)
559 g_checksum_free (data->checksum);
561 if (data->error != NULL)
562 g_error_free (data->error);
564 if (data->handler != NULL)
565 g_object_unref (data->handler);
567 g_slice_free (HashingData, data);
571 tp_file_hash_to_g_checksum (TpFileHashType type)
573 GChecksumType retval;
577 case TP_FILE_HASH_TYPE_MD5:
578 retval = G_CHECKSUM_MD5;
580 case TP_FILE_HASH_TYPE_SHA1:
581 retval = G_CHECKSUM_SHA1;
583 case TP_FILE_HASH_TYPE_SHA256:
584 retval = G_CHECKSUM_SHA256;
587 g_assert_not_reached ();
595 check_hash_incoming (EmpathyFTHandler *handler)
597 HashingData *hash_data;
598 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
600 if (!EMP_STR_EMPTY (priv->content_hash))
602 hash_data = g_slice_new0 (HashingData);
603 hash_data->total_bytes = priv->total_bytes;
604 hash_data->handler = g_object_ref (handler);
605 hash_data->checksum = g_checksum_new
606 (tp_file_hash_to_g_checksum (priv->content_hash_type));
608 g_signal_emit (handler, signals[HASHING_STARTED], 0);
610 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
611 G_PRIORITY_DEFAULT, priv->cancellable);
616 emit_error_signal (EmpathyFTHandler *handler,
619 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
621 if (!g_cancellable_is_cancelled (priv->cancellable))
622 g_cancellable_cancel (priv->cancellable);
624 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
628 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
632 EmpathyFTHandler *handler = user_data;
633 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
635 DEBUG ("Transfer operation callback, error %p", error);
639 emit_error_signal (handler, error);
643 priv->is_completed = TRUE;
644 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
646 empathy_tp_file_close (tp_file);
648 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
650 check_hash_incoming (handler);
656 update_remaining_time_and_speed (EmpathyFTHandler *handler,
657 guint64 transferred_bytes)
659 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
660 time_t elapsed_time, current_time;
661 guint64 transferred, last_transferred_bytes;
665 last_transferred_bytes = priv->transferred_bytes;
666 priv->transferred_bytes = transferred_bytes;
668 current_time = empathy_time_get_current ();
669 elapsed_time = current_time - priv->last_update_time;
671 if (elapsed_time >= 1)
673 transferred = transferred_bytes - last_transferred_bytes;
674 speed = (gdouble) transferred / (gdouble) elapsed_time;
675 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
677 priv->remaining_time = remaining_time;
678 priv->last_update_time = current_time;
683 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
684 guint64 transferred_bytes,
687 EmpathyFTHandler *handler = user_data;
688 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
690 if (empathy_ft_handler_is_cancelled (handler))
693 if (transferred_bytes == 0)
695 priv->last_update_time = empathy_time_get_current ();
696 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
699 if (priv->transferred_bytes != transferred_bytes)
701 update_remaining_time_and_speed (handler, transferred_bytes);
703 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
704 transferred_bytes, priv->total_bytes, priv->remaining_time,
710 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
714 EmpathyFTHandler *handler = user_data;
715 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
716 GError *my_error = (GError *) error;
718 DEBUG ("Dispatcher create channel CB");
720 if (my_error == NULL)
722 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
725 if (my_error != NULL)
727 emit_error_signal (handler, my_error);
729 if (my_error != error)
730 g_clear_error (&my_error);
735 priv->tpfile = g_object_ref
736 (empathy_dispatch_operation_get_channel_wrapper (operation));
738 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
739 ft_transfer_progress_callback, handler,
740 ft_transfer_operation_callback, handler);
742 empathy_dispatch_operation_claim (operation);
746 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
748 TpConnection *connection;
749 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
751 DEBUG ("Pushing request to the dispatcher");
753 connection = empathy_contact_get_connection (priv->contact);
755 /* I want to own a reference to the request, and destroy it later */
756 empathy_dispatcher_create_channel (priv->dispatcher, connection,
757 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
761 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
763 guint contact_handle;
766 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
768 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
769 NULL, (GDestroyNotify) tp_g_value_slice_free);
771 contact_handle = empathy_contact_get_handle (priv->contact);
773 /* org.freedesktop.Telepathy.Channel.ChannelType */
774 value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
775 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
777 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
778 value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT);
779 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
781 /* org.freedesktop.Telepathy.Channel.TargetHandle */
782 value = tp_g_value_slice_new_uint (contact_handle);
783 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
785 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
786 value = tp_g_value_slice_new_string (priv->content_type);
787 g_hash_table_insert (request,
788 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
790 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
791 value = tp_g_value_slice_new_string (priv->filename);
792 g_hash_table_insert (request,
793 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
795 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
796 value = tp_g_value_slice_new_uint64 (priv->total_bytes);
797 g_hash_table_insert (request,
798 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
800 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
801 value = tp_g_value_slice_new_uint64 ((guint64) priv->mtime);
802 g_hash_table_insert (request,
803 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
807 hash_job_done (gpointer user_data)
809 HashingData *hash_data = user_data;
810 EmpathyFTHandler *handler = hash_data->handler;
811 EmpathyFTHandlerPriv *priv;
812 GError *error = NULL;
815 DEBUG ("Closing stream after hashing.");
817 priv = GET_PRIV (handler);
819 if (hash_data->error != NULL)
821 error = hash_data->error;
825 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
827 if (empathy_ft_handler_is_incoming (handler))
829 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
832 DEBUG ("Hash mismatch when checking incoming handler: "
833 "received %s, calculated %s", priv->content_hash,
834 g_checksum_get_string (hash_data->checksum));
836 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
837 EMPATHY_FT_ERROR_HASH_MISMATCH,
838 _("The hash of the received file and the "
839 "sent one do not match"));
844 DEBUG ("Hash verification matched, received %s, calculated %s",
846 g_checksum_get_string (hash_data->checksum));
851 /* set the checksum in the request...
852 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
854 value = tp_g_value_slice_new_string
855 (g_checksum_get_string (hash_data->checksum));
856 g_hash_table_insert (priv->request,
857 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
864 emit_error_signal (handler, error);
865 g_clear_error (&error);
869 g_signal_emit (handler, signals[HASHING_DONE], 0);
871 if (!empathy_ft_handler_is_incoming (handler))
872 /* the request is complete now, push it to the dispatcher */
873 ft_handler_push_to_dispatcher (handler);
876 hash_data_free (hash_data);
882 emit_hashing_progress (gpointer user_data)
884 HashingData *hash_data = user_data;
886 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
887 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
893 do_hash_job (GIOSchedulerJob *job,
894 GCancellable *cancellable,
897 HashingData *hash_data = user_data;
899 EmpathyFTHandlerPriv *priv;
900 GError *error = NULL;
902 priv = GET_PRIV (hash_data->handler);
905 if (hash_data->buffer == NULL)
906 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
908 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
909 BUFFER_SIZE, cancellable, &error);
913 hash_data->total_read += bytes_read;
915 /* we now have the chunk */
918 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
919 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
922 g_free (hash_data->buffer);
923 hash_data->buffer = NULL;
929 g_input_stream_close (hash_data->stream, cancellable, &error);
934 hash_data->error = error;
936 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
943 do_hash_job_incoming (GIOSchedulerJob *job,
944 GCancellable *cancellable,
947 HashingData *hash_data = user_data;
948 EmpathyFTHandler *handler = hash_data->handler;
949 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
950 GError *error = NULL;
952 DEBUG ("checking integrity for incoming handler");
954 /* need to get the stream first */
956 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
960 hash_data->error = error;
961 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
966 return do_hash_job (job, cancellable, user_data);
970 ft_handler_read_async_cb (GObject *source,
974 GFileInputStream *stream;
975 GError *error = NULL;
976 HashingData *hash_data;
978 EmpathyFTHandler *handler = user_data;
979 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
981 DEBUG ("GFile read async CB.");
983 stream = g_file_read_finish (priv->gfile, res, &error);
986 emit_error_signal (handler, error);
987 g_clear_error (&error);
992 hash_data = g_slice_new0 (HashingData);
993 hash_data->stream = G_INPUT_STREAM (stream);
994 hash_data->total_bytes = priv->total_bytes;
995 hash_data->handler = g_object_ref (handler);
996 /* FIXME: MD5 is the only ContentHashType supported right now */
997 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
999 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
1000 value = tp_g_value_slice_new_uint (TP_FILE_HASH_TYPE_MD5);
1001 g_hash_table_insert (priv->request,
1002 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
1004 g_signal_emit (handler, signals[HASHING_STARTED], 0);
1006 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1007 G_PRIORITY_DEFAULT, priv->cancellable);
1011 callbacks_data_free (gpointer user_data)
1013 CallbacksData *data = user_data;
1015 if (data->handler != NULL)
1016 g_object_unref (data->handler);
1018 g_slice_free (CallbacksData, data);
1022 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1028 GArray *possible_values;
1032 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1034 possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1036 for (l = classes; l != NULL; l = l->next)
1039 v = g_value_array_get_nth (class, 0);
1040 fprops = g_value_get_boxed (v);
1042 value = tp_asv_get_uint32
1043 (fprops, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType",
1047 g_array_append_val (possible_values, value);
1050 if (possible_values->len == 0)
1052 /* there are no channel classes with hash support, disable it. */
1053 priv->use_hash = FALSE;
1054 priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1059 priv->use_hash = TRUE;
1061 if (possible_values->len == 1)
1063 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1067 /* order the array and pick the first non zero, so that MD5
1068 * is the preferred value.
1070 g_array_sort (possible_values, empathy_uint_compare);
1072 if (g_array_index (possible_values, guint, 0) == 0)
1073 priv->content_hash_type = g_array_index (possible_values, guint, 1);
1075 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1079 g_array_free (possible_values, TRUE);
1081 DEBUG ("Hash enabled %s; setting content hash type as %u",
1082 priv->use_hash ? "True" : "False", priv->content_hash_type);
1086 find_ft_channel_classes_cb (GList *channel_classes,
1089 CallbacksData *data = user_data;
1090 EmpathyFTHandler *handler = data->handler;
1091 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1092 GError *myerr = NULL;
1094 if (channel_classes == NULL)
1096 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1097 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1098 _("File transfer not supported by remote contact"));
1100 if (!g_cancellable_is_cancelled (priv->cancellable))
1101 g_cancellable_cancel (priv->cancellable);
1103 data->callback (handler, myerr, data->user_data);
1104 g_clear_error (&myerr);
1108 /* set whether we support hash and the type of it */
1109 set_content_hash_type_from_classes (handler, channel_classes);
1111 /* get back to the caller now */
1112 data->callback (handler, NULL, data->user_data);
1115 callbacks_data_free (data);
1119 ft_handler_complete_request (EmpathyFTHandler *handler)
1121 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1123 /* populate the request table with all the known properties */
1124 ft_handler_populate_outgoing_request (handler);
1127 /* start hashing the file */
1128 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1129 priv->cancellable, ft_handler_read_async_cb, handler);
1131 /* push directly the handler to the dispatcher */
1132 ft_handler_push_to_dispatcher (handler);
1136 ft_handler_gfile_ready_cb (GObject *source,
1138 CallbacksData *cb_data)
1141 GError *error = NULL;
1143 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1145 DEBUG ("Got GFileInfo.");
1147 info = g_file_query_info_finish (priv->gfile, res, &error);
1152 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1153 priv->filename = g_strdup (g_file_info_get_display_name (info));
1154 priv->total_bytes = g_file_info_get_size (info);
1155 g_file_info_get_modification_time (info, &mtime);
1156 priv->mtime = mtime.tv_sec;
1157 priv->transferred_bytes = 0;
1158 priv->description = NULL;
1160 g_object_unref (info);
1165 if (!g_cancellable_is_cancelled (priv->cancellable))
1166 g_cancellable_cancel (priv->cancellable);
1168 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1169 g_error_free (error);
1171 callbacks_data_free (cb_data);
1175 /* see if FT/hashing are allowed */
1176 empathy_dispatcher_find_requestable_channel_classes_async
1177 (priv->dispatcher, empathy_contact_get_connection (priv->contact),
1178 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
1179 find_ft_channel_classes_cb, cb_data,
1180 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", NULL);
1185 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
1186 EmpathyContact *contact,
1187 const GError *error,
1189 GObject *weak_object)
1191 CallbacksData *cb_data = user_data;
1192 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1193 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1197 if (!g_cancellable_is_cancelled (priv->cancellable))
1198 g_cancellable_cancel (priv->cancellable);
1200 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1201 callbacks_data_free (cb_data);
1205 priv->contact = contact;
1207 cb_data->callback (handler, NULL, cb_data->user_data);
1211 channel_get_all_properties_cb (TpProxy *proxy,
1212 GHashTable *properties,
1213 const GError *error,
1215 GObject *weak_object)
1217 CallbacksData *cb_data = user_data;
1218 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1219 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1220 EmpathyTpContactFactory *c_factory;
1225 if (!g_cancellable_is_cancelled (priv->cancellable))
1226 g_cancellable_cancel (priv->cancellable);
1228 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1230 callbacks_data_free (cb_data);
1234 priv->total_bytes = g_value_get_uint64 (
1235 g_hash_table_lookup (properties, "Size"));
1237 priv->transferred_bytes = g_value_get_uint64 (
1238 g_hash_table_lookup (properties, "TransferredBytes"));
1240 priv->filename = g_value_dup_string (
1241 g_hash_table_lookup (properties, "Filename"));
1243 priv->content_hash = g_value_dup_string (
1244 g_hash_table_lookup (properties, "ContentHash"));
1246 priv->content_hash_type = g_value_get_uint (
1247 g_hash_table_lookup (properties, "ContentHashType"));
1249 priv->content_type = g_value_dup_string (
1250 g_hash_table_lookup (properties, "ContentType"));
1252 priv->description = g_value_dup_string (
1253 g_hash_table_lookup (properties, "Description"));
1255 c_factory = empathy_tp_contact_factory_dup_singleton
1256 (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
1257 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1258 empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
1259 contact_factory_contact_cb, cb_data, callbacks_data_free,
1260 G_OBJECT (handler));
1262 g_object_unref (c_factory);
1265 /* public methods */
1268 * empathy_ft_handler_new_outgoing:
1269 * @contact: the #EmpathyContact to send @source to
1270 * @source: the #GFile to send
1271 * @callback: callback to be called when the handler has been created
1272 * @user_data: user data to be passed to @callback
1274 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1277 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1279 EmpathyFTHandlerReadyCallback callback,
1282 EmpathyFTHandler *handler;
1283 CallbacksData *data;
1284 EmpathyFTHandlerPriv *priv;
1286 DEBUG ("New handler outgoing");
1288 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1289 g_return_if_fail (G_IS_FILE (source));
1291 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1292 "contact", contact, "gfile", source, NULL);
1294 priv = GET_PRIV (handler);
1296 data = g_slice_new0 (CallbacksData);
1297 data->callback = callback;
1298 data->user_data = user_data;
1299 data->handler = g_object_ref (handler);
1301 /* start collecting info about the file */
1302 g_file_query_info_async (priv->gfile,
1303 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1304 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1305 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1306 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1307 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1308 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1312 * empathy_ft_handler_new_incoming:
1313 * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1314 * @callback: callback to be called when the handler has been created
1315 * @user_data: user data to be passed to @callback
1317 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1318 * Note that for the handler to be useful, you will have to set a destination
1319 * file with empathy_ft_handler_incoming_set_destination() after the handler
1323 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1324 EmpathyFTHandlerReadyCallback callback,
1327 EmpathyFTHandler *handler;
1329 CallbacksData *data;
1331 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1333 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1334 "tp-file", tp_file, NULL);
1336 g_object_get (tp_file, "channel", &channel, NULL);
1338 data = g_slice_new0 (CallbacksData);
1339 data->callback = callback;
1340 data->user_data = user_data;
1341 data->handler = g_object_ref (handler);
1343 tp_cli_dbus_properties_call_get_all (channel,
1344 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1345 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1349 * empathy_ft_handler_start_transfer:
1350 * @handler: an #EmpathyFTHandler
1352 * Starts the transfer machinery. After this call, the transfer and hashing
1353 * signals will be emitted by the handler.
1356 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1358 EmpathyFTHandlerPriv *priv;
1360 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1362 priv = GET_PRIV (handler);
1364 if (priv->tpfile == NULL)
1366 ft_handler_complete_request (handler);
1370 /* TODO: add support for resume. */
1371 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1372 ft_transfer_progress_callback, handler,
1373 ft_transfer_operation_callback, handler);
1378 * empathy_ft_handler_cancel_transfer:
1379 * @handler: an #EmpathyFTHandler
1381 * Cancels an ongoing handler operation. Note that this doesn't destroy
1382 * the object, which will keep all the properties, altough it won't be able
1383 * to do any more I/O.
1386 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1388 EmpathyFTHandlerPriv *priv;
1390 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1392 priv = GET_PRIV (handler);
1394 /* if we don't have an EmpathyTpFile, we are hashing, so
1395 * we can just cancel the GCancellable to stop it.
1397 if (priv->tpfile == NULL)
1398 g_cancellable_cancel (priv->cancellable);
1400 empathy_tp_file_cancel (priv->tpfile);
1404 * empathy_ft_handler_incoming_set_destination:
1405 * @handler: an #EmpathyFTHandler
1406 * @destination: the #GFile where the transfer should be saved
1408 * Sets the destination of the incoming handler to be @destination.
1409 * Note that calling this method is mandatory before starting the transfer
1410 * for incoming handlers.
1413 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1416 EmpathyFTHandlerPriv *priv;
1418 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1419 g_return_if_fail (G_IS_FILE (destination));
1421 priv = GET_PRIV (handler);
1423 g_object_set (handler, "gfile", destination, NULL);
1425 /* check if hash is supported. if it isn't, set use_hash to FALSE
1426 * anyway, so that clients won't be expecting us to checksum.
1428 if (EMP_STR_EMPTY (priv->content_hash) ||
1429 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1430 priv->use_hash = FALSE;
1432 priv->use_hash = TRUE;
1436 * empathy_ft_handler_get_filename:
1437 * @handler: an #EmpathyFTHandler
1439 * Returns the name of the file being transferred.
1441 * Return value: the name of the file being transferred
1444 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1446 EmpathyFTHandlerPriv *priv;
1448 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1450 priv = GET_PRIV (handler);
1452 return priv->filename;
1456 * empathy_ft_handler_get_content_type:
1457 * @handler: an #EmpathyFTHandler
1459 * Returns the content type of the file being transferred.
1461 * Return value: the content type of the file being transferred
1464 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1466 EmpathyFTHandlerPriv *priv;
1468 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1470 priv = GET_PRIV (handler);
1472 return priv->content_type;
1476 * empathy_ft_handler_get_contact:
1477 * @handler: an #EmpathyFTHandler
1479 * Returns the remote #EmpathyContact at the other side of the transfer.
1481 * Return value: the remote #EmpathyContact for @handler
1484 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1486 EmpathyFTHandlerPriv *priv;
1488 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1490 priv = GET_PRIV (handler);
1492 return priv->contact;
1496 * empathy_ft_handler_get_gfile:
1497 * @handler: an #EmpathyFTHandler
1499 * Returns the #GFile where the transfer is being read/saved.
1501 * Return value: the #GFile where the transfer is being read/saved
1504 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1506 EmpathyFTHandlerPriv *priv;
1508 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1510 priv = GET_PRIV (handler);
1516 * empathy_ft_handler_get_use_hash:
1517 * @handler: an #EmpathyFTHandler
1519 * Returns whether @handler has checksumming enabled. This can depend on
1520 * the CM and the remote contact capabilities.
1522 * Return value: %TRUE if the handler has checksumming enabled,
1526 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1528 EmpathyFTHandlerPriv *priv;
1530 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1532 priv = GET_PRIV (handler);
1534 return priv->use_hash;
1538 * empathy_ft_handler_is_incoming:
1539 * @handler: an #EmpathyFTHandler
1541 * Returns whether @handler is incoming or outgoing.
1543 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1546 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1548 EmpathyFTHandlerPriv *priv;
1550 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1552 priv = GET_PRIV (handler);
1554 if (priv->tpfile == NULL)
1557 return empathy_tp_file_is_incoming (priv->tpfile);
1561 * empathy_ft_handler_get_transferred_bytes:
1562 * @handler: an #EmpathyFTHandler
1564 * Returns the number of bytes already transferred by the handler.
1566 * Return value: the number of bytes already transferred by the handler.
1569 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1571 EmpathyFTHandlerPriv *priv;
1573 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1575 priv = GET_PRIV (handler);
1577 return priv->transferred_bytes;
1581 * empathy_ft_handler_get_total_bytes:
1582 * @handler: an #EmpathyFTHandler
1584 * Returns the total size of the file being transferred by the handler.
1586 * Return value: a number of bytes indicating the total size of the file being
1587 * transferred by the handler.
1590 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1592 EmpathyFTHandlerPriv *priv;
1594 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1596 priv = GET_PRIV (handler);
1598 return priv->total_bytes;
1602 * empathy_ft_handler_is_completed:
1603 * @handler: an #EmpathyFTHandler
1605 * Returns whether the transfer for @handler has been completed succesfully.
1607 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1611 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1613 EmpathyFTHandlerPriv *priv;
1615 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1617 priv = GET_PRIV (handler);
1619 return priv->is_completed;
1623 * empathy_ft_handler_is_cancelled:
1624 * @handler: an #EmpathyFTHandler
1626 * Returns whether the transfer for @handler has been cancelled or has stopped
1629 * Return value: %TRUE if the transfer for @handler has been cancelled
1630 * or has stopped due to an error, %FALSE otherwise.
1633 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1635 EmpathyFTHandlerPriv *priv;
1637 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1639 priv = GET_PRIV (handler);
1641 return g_cancellable_is_cancelled (priv->cancellable);