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/account-channel-request.h>
27 #include <telepathy-glib/util.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/interfaces.h>
31 #include "empathy-ft-handler.h"
32 #include "empathy-tp-contact-factory.h"
33 #include "empathy-dispatcher.h"
34 #include "empathy-marshal.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_FT
39 #include "empathy-debug.h"
42 * SECTION:empathy-ft-handler
43 * @title: EmpathyFTHandler
44 * @short_description: an object representing a File Transfer
45 * @include: libempathy/empathy-ft-handler
47 * #EmpathyFTHandler is the object which represents a File Transfer with all
49 * The creation of an #EmpathyFTHandler is done with
50 * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
51 * even though clients should not need to call them directly, as
52 * #EmpathyFTFactory does it for them. Remember that for the file transfer
53 * to work with an incoming handler,
54 * empathy_ft_handler_incoming_set_destination() should be called after
55 * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
57 * It's important to note that, as the creation of the handlers is async, once
58 * an handler is created, it already has all the interesting properties set,
59 * like filename, total bytes, content type and so on, making it useful
60 * to be displayed in an UI.
61 * The transfer API works like a state machine; it has three signals,
62 * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
63 * emitted in the relevant phases.
64 * In addition, if the handler is created with checksumming enabled,
65 * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
66 * will be emitted before or after the transfer, depending on the direction
67 * (respectively outgoing and incoming) of the handler.
68 * At any time between the call to empathy_ft_handler_start_transfer() and
69 * the last signal, a ::transfer-error can be emitted, indicating that an
70 * error has happened in the operation. The message of the error is localized
74 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
76 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
78 #define BUFFER_SIZE 4096
87 PROP_MODIFICATION_TIME,
89 PROP_TRANSFERRED_BYTES
104 GInputStream *stream;
105 GError *error /* comment to make the style checker happy */;
110 EmpathyFTHandler *handler;
114 EmpathyFTHandlerReadyCallback callback;
116 EmpathyFTHandler *handler;
121 gboolean dispose_run;
124 EmpathyTpFile *tpfile;
125 GCancellable *cancellable;
128 EmpathyDispatcher *dispatcher;
130 /* request for the new transfer */
133 /* transfer properties */
134 EmpathyContact *contact;
139 guint64 transferred_bytes;
142 TpFileHashType content_hash_type;
146 guint remaining_time;
147 time_t last_update_time;
149 gboolean is_completed;
150 } EmpathyFTHandlerPriv;
152 static guint signals[LAST_SIGNAL] = { 0 };
154 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
155 GCancellable *cancellable, gpointer user_data);
157 /* GObject implementations */
159 do_get_property (GObject *object,
164 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
169 g_value_set_object (value, priv->contact);
171 case PROP_CONTENT_TYPE:
172 g_value_set_string (value, priv->content_type);
174 case PROP_DESCRIPTION:
175 g_value_set_string (value, priv->description);
178 g_value_set_string (value, priv->filename);
180 case PROP_MODIFICATION_TIME:
181 g_value_set_uint64 (value, priv->mtime);
183 case PROP_TOTAL_BYTES:
184 g_value_set_uint64 (value, priv->total_bytes);
186 case PROP_TRANSFERRED_BYTES:
187 g_value_set_uint64 (value, priv->transferred_bytes);
190 g_value_set_object (value, priv->gfile);
193 g_value_set_object (value, priv->tpfile);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
201 do_set_property (GObject *object,
206 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
211 priv->contact = g_value_dup_object (value);
213 case PROP_CONTENT_TYPE:
214 priv->content_type = g_value_dup_string (value);
216 case PROP_DESCRIPTION:
217 priv->description = g_value_dup_string (value);
220 priv->filename = g_value_dup_string (value);
222 case PROP_MODIFICATION_TIME:
223 priv->mtime = g_value_get_uint64 (value);
225 case PROP_TOTAL_BYTES:
226 priv->total_bytes = g_value_get_uint64 (value);
228 case PROP_TRANSFERRED_BYTES:
229 priv->transferred_bytes = g_value_get_uint64 (value);
232 priv->gfile = g_value_dup_object (value);
235 priv->tpfile = g_value_dup_object (value);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
243 do_dispose (GObject *object)
245 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
247 if (priv->dispose_run)
250 priv->dispose_run = TRUE;
252 if (priv->contact != NULL) {
253 g_object_unref (priv->contact);
254 priv->contact = NULL;
257 if (priv->gfile != NULL) {
258 g_object_unref (priv->gfile);
262 if (priv->tpfile != NULL) {
263 empathy_tp_file_close (priv->tpfile);
264 g_object_unref (priv->tpfile);
268 if (priv->cancellable != NULL) {
269 g_object_unref (priv->cancellable);
270 priv->cancellable = NULL;
273 if (priv->request != NULL)
275 g_hash_table_unref (priv->request);
276 priv->request = NULL;
279 if (priv->dispatcher != NULL)
281 g_object_unref (priv->dispatcher);
282 priv->dispatcher = NULL;
285 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
289 do_finalize (GObject *object)
291 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
293 DEBUG ("%p", object);
295 g_free (priv->content_type);
296 priv->content_type = NULL;
298 g_free (priv->filename);
299 priv->filename = NULL;
301 g_free (priv->description);
302 priv->description = NULL;
304 g_free (priv->content_hash);
305 priv->content_hash = NULL;
307 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
311 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
313 GObjectClass *object_class = G_OBJECT_CLASS (klass);
314 GParamSpec *param_spec;
316 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
318 object_class->get_property = do_get_property;
319 object_class->set_property = do_set_property;
320 object_class->dispose = do_dispose;
321 object_class->finalize = do_finalize;
326 * EmpathyFTHandler:contact:
328 * The remote #EmpathyContact for the transfer
330 param_spec = g_param_spec_object ("contact",
331 "contact", "The remote contact",
332 EMPATHY_TYPE_CONTACT,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
334 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
337 * EmpathyFTHandler:content-type:
339 * The content type of the file being transferred
341 param_spec = g_param_spec_string ("content-type",
342 "content-type", "The content type of the file", NULL,
343 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
344 g_object_class_install_property (object_class,
345 PROP_CONTENT_TYPE, param_spec);
348 * EmpathyFTHandler:description:
350 * The description of the file being transferred
352 param_spec = g_param_spec_string ("description",
353 "description", "The description of the file", NULL,
354 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
355 g_object_class_install_property (object_class,
356 PROP_DESCRIPTION, param_spec);
359 * EmpathyFTHandler:filename:
361 * The name of the file being transferred
363 param_spec = g_param_spec_string ("filename",
364 "filename", "The name of the file", NULL,
365 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
366 g_object_class_install_property (object_class,
367 PROP_FILENAME, param_spec);
370 * EmpathyFTHandler:modification-time:
372 * The modification time of the file being transferred
374 param_spec = g_param_spec_uint64 ("modification-time",
375 "modification-time", "The mtime of the file", 0,
376 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
377 g_object_class_install_property (object_class,
378 PROP_MODIFICATION_TIME, param_spec);
381 * EmpathyFTHandler:total-bytes:
383 * The size (in bytes) of the file being transferred
385 param_spec = g_param_spec_uint64 ("total-bytes",
386 "total-bytes", "The size of the file", 0,
387 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
388 g_object_class_install_property (object_class,
389 PROP_TOTAL_BYTES, param_spec);
392 * EmpathyFTHandler:transferred-bytes:
394 * The number of the bytes already transferred
396 param_spec = g_param_spec_uint64 ("transferred-bytes",
397 "transferred-bytes", "The number of bytes already transferred", 0,
398 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
399 g_object_class_install_property (object_class,
400 PROP_TRANSFERRED_BYTES, param_spec);
403 * EmpathyFTHandler:gfile:
405 * The #GFile object where the transfer actually happens
407 param_spec = g_param_spec_object ("gfile",
408 "gfile", "The GFile we're handling",
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
411 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
414 * EmpathyFTHandler:tp-file:
416 * The underlying #EmpathyTpFile managing the transfer
418 param_spec = g_param_spec_object ("tp-file",
419 "tp-file", "The file's channel wrapper",
420 EMPATHY_TYPE_TP_FILE,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
422 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
427 * EmpathyFTHandler::transfer-started
428 * @handler: the object which has received the signal
429 * @tp_file: the #EmpathyTpFile for which the transfer has started
431 * This signal is emitted when the actual transfer starts.
433 signals[TRANSFER_STARTED] =
434 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
435 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
436 g_cclosure_marshal_VOID__OBJECT,
438 1, EMPATHY_TYPE_TP_FILE);
441 * EmpathyFTHandler::transfer-done
442 * @handler: the object which has received the signal
443 * @tp_file: the #EmpathyTpFile for which the transfer has started
445 * This signal will be emitted when the actual transfer is completed
448 signals[TRANSFER_DONE] =
449 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
450 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
451 g_cclosure_marshal_VOID__OBJECT,
453 1, EMPATHY_TYPE_TP_FILE);
456 * EmpathyFTHandler::transfer-error
457 * @handler: the object which has received the signal
460 * This signal can be emitted anytime between the call to
461 * empathy_ft_handler_start_transfer() and the last expected signal
462 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
463 * signal coming from the handler, meaning that no other operation will
464 * take place after this signal.
466 signals[TRANSFER_ERROR] =
467 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
468 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
469 g_cclosure_marshal_VOID__POINTER,
474 * EmpathyFTHandler::transfer-progress
475 * @handler: the object which has received the signal
476 * @current_bytes: the bytes currently transferred
477 * @total_bytes: the total bytes of the handler
478 * @remaining_time: the number of seconds remaining for the transfer
480 * @speed: the current speed of the transfer (in KB/s)
482 * This signal is emitted to notify clients of the progress of the
485 signals[TRANSFER_PROGRESS] =
486 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
487 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
488 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
490 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
493 * EmpathyFTHandler::hashing-started
494 * @handler: the object which has received the signal
496 * This signal is emitted when the hashing operation of the handler
497 * is started. Note that this might happen or not, depending on the CM
498 * and remote contact capabilities. Clients shoud use
499 * empathy_ft_handler_get_use_hash() before calling
500 * empathy_ft_handler_start_transfer() to know whether they should connect
503 signals[HASHING_STARTED] =
504 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
505 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
506 g_cclosure_marshal_VOID__VOID,
510 * EmpathyFTHandler::hashing-progress
511 * @handler: the object which has received the signal
512 * @current_bytes: the bytes currently hashed
513 * @total_bytes: the total bytes of the handler
515 * This signal is emitted to notify clients of the progress of the
518 signals[HASHING_PROGRESS] =
519 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
520 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
521 _empathy_marshal_VOID__UINT64_UINT64,
523 2, G_TYPE_UINT64, G_TYPE_UINT64);
526 * EmpathyFTHandler::hashing-done
527 * @handler: the object which has received the signal
529 * This signal is emitted when the hashing operation of the handler
532 signals[HASHING_DONE] =
533 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
534 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
535 g_cclosure_marshal_VOID__VOID,
540 empathy_ft_handler_init (EmpathyFTHandler *self)
542 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
543 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
546 priv->cancellable = g_cancellable_new ();
547 priv->dispatcher = empathy_dispatcher_dup_singleton ();
550 /* private functions */
553 hash_data_free (HashingData *data)
555 g_free (data->buffer);
557 if (data->stream != NULL)
558 g_object_unref (data->stream);
560 if (data->checksum != NULL)
561 g_checksum_free (data->checksum);
563 if (data->error != NULL)
564 g_error_free (data->error);
566 if (data->handler != NULL)
567 g_object_unref (data->handler);
569 g_slice_free (HashingData, data);
573 tp_file_hash_to_g_checksum (TpFileHashType type)
575 GChecksumType retval;
579 case TP_FILE_HASH_TYPE_MD5:
580 retval = G_CHECKSUM_MD5;
582 case TP_FILE_HASH_TYPE_SHA1:
583 retval = G_CHECKSUM_SHA1;
585 case TP_FILE_HASH_TYPE_SHA256:
586 retval = G_CHECKSUM_SHA256;
588 case TP_FILE_HASH_TYPE_NONE:
590 g_assert_not_reached ();
598 check_hash_incoming (EmpathyFTHandler *handler)
600 HashingData *hash_data;
601 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
603 if (!EMP_STR_EMPTY (priv->content_hash))
605 hash_data = g_slice_new0 (HashingData);
606 hash_data->total_bytes = priv->total_bytes;
607 hash_data->handler = g_object_ref (handler);
608 hash_data->checksum = g_checksum_new
609 (tp_file_hash_to_g_checksum (priv->content_hash_type));
611 g_signal_emit (handler, signals[HASHING_STARTED], 0);
613 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
614 G_PRIORITY_DEFAULT, priv->cancellable);
619 emit_error_signal (EmpathyFTHandler *handler,
622 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
624 DEBUG ("Error in transfer: %s\n", error->message);
626 if (!g_cancellable_is_cancelled (priv->cancellable))
627 g_cancellable_cancel (priv->cancellable);
629 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
633 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
637 EmpathyFTHandler *handler = user_data;
638 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
640 DEBUG ("Transfer operation callback, error %p", error);
644 emit_error_signal (handler, error);
648 priv->is_completed = TRUE;
649 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
651 empathy_tp_file_close (tp_file);
653 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
655 check_hash_incoming (handler);
661 update_remaining_time_and_speed (EmpathyFTHandler *handler,
662 guint64 transferred_bytes)
664 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
665 time_t elapsed_time, current_time;
666 guint64 transferred, last_transferred_bytes;
670 last_transferred_bytes = priv->transferred_bytes;
671 priv->transferred_bytes = transferred_bytes;
673 current_time = empathy_time_get_current ();
674 elapsed_time = current_time - priv->last_update_time;
676 if (elapsed_time >= 1)
678 transferred = transferred_bytes - last_transferred_bytes;
679 speed = (gdouble) transferred / (gdouble) elapsed_time;
680 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
682 priv->remaining_time = remaining_time;
683 priv->last_update_time = current_time;
688 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
689 guint64 transferred_bytes,
692 EmpathyFTHandler *handler = user_data;
693 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
695 if (empathy_ft_handler_is_cancelled (handler))
698 if (transferred_bytes == 0)
700 priv->last_update_time = empathy_time_get_current ();
701 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
704 if (priv->transferred_bytes != transferred_bytes)
706 update_remaining_time_and_speed (handler, transferred_bytes);
708 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
709 transferred_bytes, priv->total_bytes, priv->remaining_time,
715 ft_handler_create_channel_cb (GObject *source,
716 GAsyncResult *result,
719 EmpathyFTHandler *handler = user_data;
720 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
721 GError *error = NULL;
722 TpChannel *channel = NULL;
724 DEBUG ("Dispatcher create channel CB");
726 channel = tp_account_channel_request_create_and_handle_channel_finish (
727 TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
730 DEBUG ("Failed to request FT channel: %s", error->message);
732 g_cancellable_set_error_if_cancelled (priv->cancellable, &error);
736 emit_error_signal (handler, error);
738 g_error_free (error);
742 priv->tpfile = empathy_tp_file_new (channel);
744 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
745 ft_transfer_progress_callback, handler,
746 ft_transfer_operation_callback, handler);
749 tp_clear_object (&channel);
753 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
756 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
757 TpAccountChannelRequest *req;
759 DEBUG ("Pushing request to the dispatcher");
761 account = empathy_contact_get_account (priv->contact);
763 req = tp_account_channel_request_new (account, priv->request,
764 TP_USER_ACTION_TIME_NOT_USER_ACTION);
766 tp_account_channel_request_create_and_handle_channel_async (req, NULL,
767 ft_handler_create_channel_cb, handler);
769 g_object_unref (req);
773 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
775 guint contact_handle;
776 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
779 contact_handle = empathy_contact_get_handle (priv->contact);
780 uri = g_file_get_uri (priv->gfile);
782 priv->request = tp_asv_new (
783 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
784 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
785 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
786 TP_HANDLE_TYPE_CONTACT,
787 TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
789 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_TYPE, G_TYPE_STRING,
791 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_FILENAME, G_TYPE_STRING,
793 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_SIZE, G_TYPE_UINT64,
795 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_DATE, G_TYPE_UINT64,
797 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_URI, G_TYPE_STRING, uri,
804 hash_job_done (gpointer user_data)
806 HashingData *hash_data = user_data;
807 EmpathyFTHandler *handler = hash_data->handler;
808 EmpathyFTHandlerPriv *priv;
809 GError *error = NULL;
811 DEBUG ("Closing stream after hashing.");
813 priv = GET_PRIV (handler);
815 if (hash_data->error != NULL)
817 error = hash_data->error;
818 hash_data->error = NULL;
822 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
824 if (empathy_ft_handler_is_incoming (handler))
826 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
829 DEBUG ("Hash mismatch when checking incoming handler: "
830 "received %s, calculated %s", priv->content_hash,
831 g_checksum_get_string (hash_data->checksum));
833 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
834 EMPATHY_FT_ERROR_HASH_MISMATCH,
835 _("File transfer completed, but the file was corrupted"));
840 DEBUG ("Hash verification matched, received %s, calculated %s",
842 g_checksum_get_string (hash_data->checksum));
847 /* set the checksum in the request...
848 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
850 tp_asv_set_string (priv->request,
851 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH,
852 g_checksum_get_string (hash_data->checksum));
859 emit_error_signal (handler, error);
860 g_clear_error (&error);
864 g_signal_emit (handler, signals[HASHING_DONE], 0);
866 if (!empathy_ft_handler_is_incoming (handler))
867 /* the request is complete now, push it to the dispatcher */
868 ft_handler_push_to_dispatcher (handler);
871 hash_data_free (hash_data);
877 emit_hashing_progress (gpointer user_data)
879 HashingData *hash_data = user_data;
881 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
882 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
888 do_hash_job (GIOSchedulerJob *job,
889 GCancellable *cancellable,
892 HashingData *hash_data = user_data;
894 EmpathyFTHandlerPriv *priv;
895 GError *error = NULL;
897 priv = GET_PRIV (hash_data->handler);
900 if (hash_data->buffer == NULL)
901 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
903 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
904 BUFFER_SIZE, cancellable, &error);
908 hash_data->total_read += bytes_read;
910 /* we now have the chunk */
913 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
914 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
917 g_free (hash_data->buffer);
918 hash_data->buffer = NULL;
924 g_input_stream_close (hash_data->stream, cancellable, &error);
929 hash_data->error = error;
931 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
938 do_hash_job_incoming (GIOSchedulerJob *job,
939 GCancellable *cancellable,
942 HashingData *hash_data = user_data;
943 EmpathyFTHandler *handler = hash_data->handler;
944 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
945 GError *error = NULL;
947 DEBUG ("checking integrity for incoming handler");
949 /* need to get the stream first */
951 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
955 hash_data->error = error;
956 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
961 return do_hash_job (job, cancellable, user_data);
965 ft_handler_read_async_cb (GObject *source,
969 GFileInputStream *stream;
970 GError *error = NULL;
971 HashingData *hash_data;
972 EmpathyFTHandler *handler = user_data;
973 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
975 DEBUG ("GFile read async CB.");
977 stream = g_file_read_finish (priv->gfile, res, &error);
980 emit_error_signal (handler, error);
981 g_clear_error (&error);
986 hash_data = g_slice_new0 (HashingData);
987 hash_data->stream = G_INPUT_STREAM (stream);
988 hash_data->total_bytes = priv->total_bytes;
989 hash_data->handler = g_object_ref (handler);
990 /* FIXME: MD5 is the only ContentHashType supported right now */
991 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
993 tp_asv_set_uint32 (priv->request,
994 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
995 TP_FILE_HASH_TYPE_MD5);
997 g_signal_emit (handler, signals[HASHING_STARTED], 0);
999 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1000 G_PRIORITY_DEFAULT, priv->cancellable);
1004 callbacks_data_free (gpointer user_data)
1006 CallbacksData *data = user_data;
1008 if (data->handler != NULL)
1009 g_object_unref (data->handler);
1011 g_slice_free (CallbacksData, data);
1015 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1018 GArray *possible_values;
1021 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1022 gboolean support_ft = FALSE;
1025 possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1027 for (i = 0; i < classes->len; i++)
1031 const gchar *chan_type;
1033 tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
1036 chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
1038 if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1041 if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
1042 TP_HANDLE_TYPE_CONTACT)
1047 value = tp_asv_get_uint32
1048 (fixed, TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
1052 g_array_append_val (possible_values, value);
1057 g_array_free (possible_values, TRUE);
1061 if (possible_values->len == 0)
1063 /* there are no channel classes with hash support, disable it. */
1064 priv->use_hash = FALSE;
1065 priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1070 priv->use_hash = TRUE;
1072 if (possible_values->len == 1)
1074 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1078 /* order the array and pick the first non zero, so that MD5
1079 * is the preferred value.
1081 g_array_sort (possible_values, empathy_uint_compare);
1083 if (g_array_index (possible_values, guint, 0) == 0)
1084 priv->content_hash_type = g_array_index (possible_values, guint, 1);
1086 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1090 g_array_free (possible_values, TRUE);
1092 DEBUG ("Hash enabled %s; setting content hash type as %u",
1093 priv->use_hash ? "True" : "False", priv->content_hash_type);
1099 conn_prepared_cb (GObject *conn,
1100 GAsyncResult *result,
1103 CallbacksData *data = user_data;
1104 EmpathyFTHandler *handler = data->handler;
1105 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1106 GError *myerr = NULL;
1107 TpCapabilities *caps;
1110 if (!tp_proxy_prepare_finish (conn, result, &myerr))
1112 DEBUG ("Failed to prepare connection: %s", myerr->message);
1114 data->callback (handler, myerr, data->user_data);
1118 caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
1119 classes = tp_capabilities_get_channel_classes (caps);
1121 /* set whether we support hash and the type of it */
1122 if (!set_content_hash_type_from_classes (handler, classes))
1124 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1125 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1126 _("File transfer not supported by remote contact"));
1128 if (!g_cancellable_is_cancelled (priv->cancellable))
1129 g_cancellable_cancel (priv->cancellable);
1131 data->callback (handler, myerr, data->user_data);
1132 g_clear_error (&myerr);
1136 /* get back to the caller now */
1137 data->callback (handler, NULL, data->user_data);
1141 callbacks_data_free (data);
1145 ft_handler_complete_request (EmpathyFTHandler *handler)
1147 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1149 /* populate the request table with all the known properties */
1150 ft_handler_populate_outgoing_request (handler);
1153 /* start hashing the file */
1154 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1155 priv->cancellable, ft_handler_read_async_cb, handler);
1157 /* push directly the handler to the dispatcher */
1158 ft_handler_push_to_dispatcher (handler);
1162 ft_handler_gfile_ready_cb (GObject *source,
1164 CallbacksData *cb_data)
1167 GError *error = NULL;
1169 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1171 DEBUG ("Got GFileInfo.");
1173 info = g_file_query_info_finish (priv->gfile, res, &error);
1178 if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1180 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1181 EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1182 _("The selected file is not a regular file"));
1186 priv->total_bytes = g_file_info_get_size (info);
1187 if (priv->total_bytes == 0)
1189 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1190 EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1191 _("The selected file is empty"));
1195 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1196 priv->filename = g_strdup (g_file_info_get_display_name (info));
1197 g_file_info_get_modification_time (info, &mtime);
1198 priv->mtime = mtime.tv_sec;
1199 priv->transferred_bytes = 0;
1200 priv->description = NULL;
1202 g_object_unref (info);
1207 if (!g_cancellable_is_cancelled (priv->cancellable))
1208 g_cancellable_cancel (priv->cancellable);
1210 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1211 g_error_free (error);
1213 callbacks_data_free (cb_data);
1217 /* see if FT/hashing are allowed */
1218 TpConnection *connection;
1219 GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
1221 connection = empathy_contact_get_connection (priv->contact);
1222 g_assert (connection != NULL);
1224 tp_proxy_prepare_async (connection, features,
1225 conn_prepared_cb, cb_data);
1230 contact_factory_contact_cb (TpConnection *connection,
1231 EmpathyContact *contact,
1232 const GError *error,
1234 GObject *weak_object)
1236 CallbacksData *cb_data = user_data;
1237 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1238 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1242 if (!g_cancellable_is_cancelled (priv->cancellable))
1243 g_cancellable_cancel (priv->cancellable);
1245 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1246 callbacks_data_free (cb_data);
1250 priv->contact = g_object_ref (contact);
1252 cb_data->callback (handler, NULL, cb_data->user_data);
1256 channel_get_all_properties_cb (TpProxy *proxy,
1257 GHashTable *properties,
1258 const GError *error,
1260 GObject *weak_object)
1262 CallbacksData *cb_data = user_data;
1263 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1264 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1269 if (!g_cancellable_is_cancelled (priv->cancellable))
1270 g_cancellable_cancel (priv->cancellable);
1272 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1274 callbacks_data_free (cb_data);
1278 priv->total_bytes = g_value_get_uint64 (
1279 g_hash_table_lookup (properties, "Size"));
1281 priv->transferred_bytes = g_value_get_uint64 (
1282 g_hash_table_lookup (properties, "TransferredBytes"));
1284 priv->filename = g_value_dup_string (
1285 g_hash_table_lookup (properties, "Filename"));
1287 priv->content_hash = g_value_dup_string (
1288 g_hash_table_lookup (properties, "ContentHash"));
1290 priv->content_hash_type = g_value_get_uint (
1291 g_hash_table_lookup (properties, "ContentHashType"));
1293 priv->content_type = g_value_dup_string (
1294 g_hash_table_lookup (properties, "ContentType"));
1296 priv->description = g_value_dup_string (
1297 g_hash_table_lookup (properties, "Description"));
1299 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1300 empathy_tp_contact_factory_get_from_handle (
1301 tp_channel_borrow_connection (TP_CHANNEL (proxy)), c_handle,
1302 contact_factory_contact_cb, cb_data, callbacks_data_free,
1303 G_OBJECT (handler));
1306 /* public methods */
1309 * empathy_ft_handler_new_outgoing:
1310 * @contact: the #EmpathyContact to send @source to
1311 * @source: the #GFile to send
1312 * @callback: callback to be called when the handler has been created
1313 * @user_data: user data to be passed to @callback
1315 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1318 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1320 EmpathyFTHandlerReadyCallback callback,
1323 EmpathyFTHandler *handler;
1324 CallbacksData *data;
1325 EmpathyFTHandlerPriv *priv;
1327 DEBUG ("New handler outgoing");
1329 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1330 g_return_if_fail (G_IS_FILE (source));
1332 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1333 "contact", contact, "gfile", source, NULL);
1335 priv = GET_PRIV (handler);
1337 data = g_slice_new0 (CallbacksData);
1338 data->callback = callback;
1339 data->user_data = user_data;
1340 data->handler = g_object_ref (handler);
1342 /* start collecting info about the file */
1343 g_file_query_info_async (priv->gfile,
1344 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1345 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1346 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1347 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1348 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1349 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1350 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1354 * empathy_ft_handler_new_incoming:
1355 * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1356 * @callback: callback to be called when the handler has been created
1357 * @user_data: user data to be passed to @callback
1359 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1360 * Note that for the handler to be useful, you will have to set a destination
1361 * file with empathy_ft_handler_incoming_set_destination() after the handler
1365 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1366 EmpathyFTHandlerReadyCallback callback,
1369 EmpathyFTHandler *handler;
1371 CallbacksData *data;
1373 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1375 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1376 "tp-file", tp_file, NULL);
1378 g_object_get (tp_file, "channel", &channel, NULL);
1380 data = g_slice_new0 (CallbacksData);
1381 data->callback = callback;
1382 data->user_data = user_data;
1383 data->handler = g_object_ref (handler);
1385 tp_cli_dbus_properties_call_get_all (channel,
1386 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1387 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1391 * empathy_ft_handler_start_transfer:
1392 * @handler: an #EmpathyFTHandler
1394 * Starts the transfer machinery. After this call, the transfer and hashing
1395 * signals will be emitted by the handler.
1398 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1400 EmpathyFTHandlerPriv *priv;
1402 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1404 priv = GET_PRIV (handler);
1406 if (priv->tpfile == NULL)
1408 ft_handler_complete_request (handler);
1412 /* TODO: add support for resume. */
1413 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1414 ft_transfer_progress_callback, handler,
1415 ft_transfer_operation_callback, handler);
1420 * empathy_ft_handler_cancel_transfer:
1421 * @handler: an #EmpathyFTHandler
1423 * Cancels an ongoing handler operation. Note that this doesn't destroy
1424 * the object, which will keep all the properties, altough it won't be able
1425 * to do any more I/O.
1428 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1430 EmpathyFTHandlerPriv *priv;
1432 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1434 priv = GET_PRIV (handler);
1436 /* if we don't have an EmpathyTpFile, we are hashing, so
1437 * we can just cancel the GCancellable to stop it.
1439 if (priv->tpfile == NULL)
1440 g_cancellable_cancel (priv->cancellable);
1442 empathy_tp_file_cancel (priv->tpfile);
1446 * empathy_ft_handler_incoming_set_destination:
1447 * @handler: an #EmpathyFTHandler
1448 * @destination: the #GFile where the transfer should be saved
1450 * Sets the destination of the incoming handler to be @destination.
1451 * Note that calling this method is mandatory before starting the transfer
1452 * for incoming handlers.
1455 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1458 EmpathyFTHandlerPriv *priv;
1460 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1461 g_return_if_fail (G_IS_FILE (destination));
1463 priv = GET_PRIV (handler);
1465 g_object_set (handler, "gfile", destination, NULL);
1467 /* check if hash is supported. if it isn't, set use_hash to FALSE
1468 * anyway, so that clients won't be expecting us to checksum.
1470 if (EMP_STR_EMPTY (priv->content_hash) ||
1471 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1472 priv->use_hash = FALSE;
1474 priv->use_hash = TRUE;
1478 * empathy_ft_handler_get_filename:
1479 * @handler: an #EmpathyFTHandler
1481 * Returns the name of the file being transferred.
1483 * Return value: the name of the file being transferred
1486 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1488 EmpathyFTHandlerPriv *priv;
1490 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1492 priv = GET_PRIV (handler);
1494 return priv->filename;
1498 * empathy_ft_handler_get_content_type:
1499 * @handler: an #EmpathyFTHandler
1501 * Returns the content type of the file being transferred.
1503 * Return value: the content type of the file being transferred
1506 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1508 EmpathyFTHandlerPriv *priv;
1510 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1512 priv = GET_PRIV (handler);
1514 return priv->content_type;
1518 * empathy_ft_handler_get_contact:
1519 * @handler: an #EmpathyFTHandler
1521 * Returns the remote #EmpathyContact at the other side of the transfer.
1523 * Return value: the remote #EmpathyContact for @handler
1526 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1528 EmpathyFTHandlerPriv *priv;
1530 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1532 priv = GET_PRIV (handler);
1534 return priv->contact;
1538 * empathy_ft_handler_get_gfile:
1539 * @handler: an #EmpathyFTHandler
1541 * Returns the #GFile where the transfer is being read/saved.
1543 * Return value: the #GFile where the transfer is being read/saved
1546 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1548 EmpathyFTHandlerPriv *priv;
1550 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1552 priv = GET_PRIV (handler);
1558 * empathy_ft_handler_get_use_hash:
1559 * @handler: an #EmpathyFTHandler
1561 * Returns whether @handler has checksumming enabled. This can depend on
1562 * the CM and the remote contact capabilities.
1564 * Return value: %TRUE if the handler has checksumming enabled,
1568 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1570 EmpathyFTHandlerPriv *priv;
1572 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1574 priv = GET_PRIV (handler);
1576 return priv->use_hash;
1580 * empathy_ft_handler_is_incoming:
1581 * @handler: an #EmpathyFTHandler
1583 * Returns whether @handler is incoming or outgoing.
1585 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1588 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1590 EmpathyFTHandlerPriv *priv;
1592 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1594 priv = GET_PRIV (handler);
1596 if (priv->tpfile == NULL)
1599 return empathy_tp_file_is_incoming (priv->tpfile);
1603 * empathy_ft_handler_get_transferred_bytes:
1604 * @handler: an #EmpathyFTHandler
1606 * Returns the number of bytes already transferred by the handler.
1608 * Return value: the number of bytes already transferred by the handler.
1611 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1613 EmpathyFTHandlerPriv *priv;
1615 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1617 priv = GET_PRIV (handler);
1619 return priv->transferred_bytes;
1623 * empathy_ft_handler_get_total_bytes:
1624 * @handler: an #EmpathyFTHandler
1626 * Returns the total size of the file being transferred by the handler.
1628 * Return value: a number of bytes indicating the total size of the file being
1629 * transferred by the handler.
1632 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1634 EmpathyFTHandlerPriv *priv;
1636 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1638 priv = GET_PRIV (handler);
1640 return priv->total_bytes;
1644 * empathy_ft_handler_is_completed:
1645 * @handler: an #EmpathyFTHandler
1647 * Returns whether the transfer for @handler has been completed succesfully.
1649 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1653 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1655 EmpathyFTHandlerPriv *priv;
1657 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1659 priv = GET_PRIV (handler);
1661 return priv->is_completed;
1665 * empathy_ft_handler_is_cancelled:
1666 * @handler: an #EmpathyFTHandler
1668 * Returns whether the transfer for @handler has been cancelled or has stopped
1671 * Return value: %TRUE if the transfer for @handler has been cancelled
1672 * or has stopped due to an error, %FALSE otherwise.
1675 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1677 EmpathyFTHandlerPriv *priv;
1679 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1681 priv = GET_PRIV (handler);
1683 return g_cancellable_is_cancelled (priv->cancellable);