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-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,
103 GInputStream *stream;
104 GError *error /* comment to make the style checker happy */;
109 EmpathyFTHandler *handler;
113 EmpathyFTHandlerReadyCallback callback;
115 EmpathyFTHandler *handler;
120 gboolean dispose_run;
123 EmpathyTpFile *tpfile;
124 GCancellable *cancellable;
127 /* request for the new transfer */
130 /* transfer properties */
131 EmpathyContact *contact;
136 guint64 transferred_bytes;
139 TpFileHashType content_hash_type;
141 gint64 user_action_time;
145 guint remaining_time;
146 gint64 last_update_time;
148 gboolean is_completed;
149 } EmpathyFTHandlerPriv;
151 static guint signals[LAST_SIGNAL] = { 0 };
153 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
154 GCancellable *cancellable, gpointer user_data);
156 /* GObject implementations */
158 do_get_property (GObject *object,
163 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
168 g_value_set_object (value, priv->contact);
170 case PROP_CONTENT_TYPE:
171 g_value_set_string (value, priv->content_type);
173 case PROP_DESCRIPTION:
174 g_value_set_string (value, priv->description);
177 g_value_set_string (value, priv->filename);
179 case PROP_MODIFICATION_TIME:
180 g_value_set_uint64 (value, priv->mtime);
182 case PROP_TOTAL_BYTES:
183 g_value_set_uint64 (value, priv->total_bytes);
185 case PROP_TRANSFERRED_BYTES:
186 g_value_set_uint64 (value, priv->transferred_bytes);
189 g_value_set_object (value, priv->gfile);
192 g_value_set_object (value, priv->tpfile);
194 case PROP_USER_ACTION_TIME:
195 g_value_set_int64 (value, priv->user_action_time);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
203 do_set_property (GObject *object,
208 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
213 priv->contact = g_value_dup_object (value);
215 case PROP_CONTENT_TYPE:
216 priv->content_type = g_value_dup_string (value);
218 case PROP_DESCRIPTION:
219 priv->description = g_value_dup_string (value);
222 priv->filename = g_value_dup_string (value);
224 case PROP_MODIFICATION_TIME:
225 priv->mtime = g_value_get_uint64 (value);
227 case PROP_TOTAL_BYTES:
228 priv->total_bytes = g_value_get_uint64 (value);
230 case PROP_TRANSFERRED_BYTES:
231 priv->transferred_bytes = g_value_get_uint64 (value);
234 priv->gfile = g_value_dup_object (value);
237 priv->tpfile = g_value_dup_object (value);
239 case PROP_USER_ACTION_TIME:
240 priv->user_action_time = g_value_get_int64 (value);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
248 do_dispose (GObject *object)
250 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
252 if (priv->dispose_run)
255 priv->dispose_run = TRUE;
257 if (priv->contact != NULL) {
258 g_object_unref (priv->contact);
259 priv->contact = NULL;
262 if (priv->gfile != NULL) {
263 g_object_unref (priv->gfile);
267 if (priv->tpfile != NULL) {
268 empathy_tp_file_close (priv->tpfile);
269 g_object_unref (priv->tpfile);
273 if (priv->cancellable != NULL) {
274 g_object_unref (priv->cancellable);
275 priv->cancellable = NULL;
278 if (priv->request != NULL)
280 g_hash_table_unref (priv->request);
281 priv->request = NULL;
284 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
288 do_finalize (GObject *object)
290 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
292 DEBUG ("%p", object);
294 g_free (priv->content_type);
295 priv->content_type = NULL;
297 g_free (priv->filename);
298 priv->filename = NULL;
300 g_free (priv->description);
301 priv->description = NULL;
303 g_free (priv->content_hash);
304 priv->content_hash = NULL;
306 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
310 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
312 GObjectClass *object_class = G_OBJECT_CLASS (klass);
313 GParamSpec *param_spec;
315 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
317 object_class->get_property = do_get_property;
318 object_class->set_property = do_set_property;
319 object_class->dispose = do_dispose;
320 object_class->finalize = do_finalize;
325 * EmpathyFTHandler:contact:
327 * The remote #EmpathyContact for the transfer
329 param_spec = g_param_spec_object ("contact",
330 "contact", "The remote contact",
331 EMPATHY_TYPE_CONTACT,
332 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
333 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
336 * EmpathyFTHandler:content-type:
338 * The content type of the file being transferred
340 param_spec = g_param_spec_string ("content-type",
341 "content-type", "The content type of the file", NULL,
342 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
343 g_object_class_install_property (object_class,
344 PROP_CONTENT_TYPE, param_spec);
347 * EmpathyFTHandler:description:
349 * The description of the file being transferred
351 param_spec = g_param_spec_string ("description",
352 "description", "The description of the file", NULL,
353 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
354 g_object_class_install_property (object_class,
355 PROP_DESCRIPTION, param_spec);
358 * EmpathyFTHandler:filename:
360 * The name of the file being transferred
362 param_spec = g_param_spec_string ("filename",
363 "filename", "The name of the file", NULL,
364 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
365 g_object_class_install_property (object_class,
366 PROP_FILENAME, param_spec);
369 * EmpathyFTHandler:modification-time:
371 * The modification time of the file being transferred
373 param_spec = g_param_spec_uint64 ("modification-time",
374 "modification-time", "The mtime of the file", 0,
375 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
376 g_object_class_install_property (object_class,
377 PROP_MODIFICATION_TIME, param_spec);
380 * EmpathyFTHandler:total-bytes:
382 * The size (in bytes) of the file being transferred
384 param_spec = g_param_spec_uint64 ("total-bytes",
385 "total-bytes", "The size of the file", 0,
386 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
387 g_object_class_install_property (object_class,
388 PROP_TOTAL_BYTES, param_spec);
391 * EmpathyFTHandler:transferred-bytes:
393 * The number of the bytes already transferred
395 param_spec = g_param_spec_uint64 ("transferred-bytes",
396 "transferred-bytes", "The number of bytes already transferred", 0,
397 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
398 g_object_class_install_property (object_class,
399 PROP_TRANSFERRED_BYTES, param_spec);
402 * EmpathyFTHandler:gfile:
404 * The #GFile object where the transfer actually happens
406 param_spec = g_param_spec_object ("gfile",
407 "gfile", "The GFile we're handling",
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
410 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
413 * EmpathyFTHandler:tp-file:
415 * The underlying #EmpathyTpFile managing the transfer
417 param_spec = g_param_spec_object ("tp-file",
418 "tp-file", "The file's channel wrapper",
419 EMPATHY_TYPE_TP_FILE,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
421 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
423 param_spec = g_param_spec_int64 ("user-action-time", "user action time",
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
427 g_object_class_install_property (object_class, PROP_USER_ACTION_TIME,
433 * EmpathyFTHandler::transfer-started
434 * @handler: the object which has received the signal
435 * @tp_file: the #EmpathyTpFile for which the transfer has started
437 * This signal is emitted when the actual transfer starts.
439 signals[TRANSFER_STARTED] =
440 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
441 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
442 g_cclosure_marshal_generic,
444 1, EMPATHY_TYPE_TP_FILE);
447 * EmpathyFTHandler::transfer-done
448 * @handler: the object which has received the signal
449 * @tp_file: the #EmpathyTpFile for which the transfer has started
451 * This signal will be emitted when the actual transfer is completed
454 signals[TRANSFER_DONE] =
455 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
456 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
457 g_cclosure_marshal_generic,
459 1, EMPATHY_TYPE_TP_FILE);
462 * EmpathyFTHandler::transfer-error
463 * @handler: the object which has received the signal
466 * This signal can be emitted anytime between the call to
467 * empathy_ft_handler_start_transfer() and the last expected signal
468 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
469 * signal coming from the handler, meaning that no other operation will
470 * take place after this signal.
472 signals[TRANSFER_ERROR] =
473 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
474 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
475 g_cclosure_marshal_generic,
480 * EmpathyFTHandler::transfer-progress
481 * @handler: the object which has received the signal
482 * @current_bytes: the bytes currently transferred
483 * @total_bytes: the total bytes of the handler
484 * @remaining_time: the number of seconds remaining for the transfer
486 * @speed: the current speed of the transfer (in KB/s)
488 * This signal is emitted to notify clients of the progress of the
491 signals[TRANSFER_PROGRESS] =
492 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
493 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
494 g_cclosure_marshal_generic,
496 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
499 * EmpathyFTHandler::hashing-started
500 * @handler: the object which has received the signal
502 * This signal is emitted when the hashing operation of the handler
503 * is started. Note that this might happen or not, depending on the CM
504 * and remote contact capabilities. Clients shoud use
505 * empathy_ft_handler_get_use_hash() before calling
506 * empathy_ft_handler_start_transfer() to know whether they should connect
509 signals[HASHING_STARTED] =
510 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
511 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
512 g_cclosure_marshal_generic,
516 * EmpathyFTHandler::hashing-progress
517 * @handler: the object which has received the signal
518 * @current_bytes: the bytes currently hashed
519 * @total_bytes: the total bytes of the handler
521 * This signal is emitted to notify clients of the progress of the
524 signals[HASHING_PROGRESS] =
525 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
526 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
527 g_cclosure_marshal_generic,
529 2, G_TYPE_UINT64, G_TYPE_UINT64);
532 * EmpathyFTHandler::hashing-done
533 * @handler: the object which has received the signal
535 * This signal is emitted when the hashing operation of the handler
538 signals[HASHING_DONE] =
539 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
540 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
541 g_cclosure_marshal_generic,
546 empathy_ft_handler_init (EmpathyFTHandler *self)
548 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
549 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
552 priv->cancellable = g_cancellable_new ();
555 /* private functions */
558 hash_data_free (HashingData *data)
560 g_free (data->buffer);
562 if (data->stream != NULL)
563 g_object_unref (data->stream);
565 if (data->checksum != NULL)
566 g_checksum_free (data->checksum);
568 if (data->error != NULL)
569 g_error_free (data->error);
571 if (data->handler != NULL)
572 g_object_unref (data->handler);
574 g_slice_free (HashingData, data);
578 tp_file_hash_to_g_checksum (TpFileHashType type)
580 GChecksumType retval;
584 case TP_FILE_HASH_TYPE_MD5:
585 retval = G_CHECKSUM_MD5;
587 case TP_FILE_HASH_TYPE_SHA1:
588 retval = G_CHECKSUM_SHA1;
590 case TP_FILE_HASH_TYPE_SHA256:
591 retval = G_CHECKSUM_SHA256;
593 case TP_FILE_HASH_TYPE_NONE:
595 g_assert_not_reached ();
603 check_hash_incoming (EmpathyFTHandler *handler)
605 HashingData *hash_data;
606 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
608 if (!EMP_STR_EMPTY (priv->content_hash))
610 hash_data = g_slice_new0 (HashingData);
611 hash_data->total_bytes = priv->total_bytes;
612 hash_data->handler = g_object_ref (handler);
613 hash_data->checksum = g_checksum_new
614 (tp_file_hash_to_g_checksum (priv->content_hash_type));
616 g_signal_emit (handler, signals[HASHING_STARTED], 0);
618 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
619 G_PRIORITY_DEFAULT, priv->cancellable);
624 emit_error_signal (EmpathyFTHandler *handler,
627 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
629 DEBUG ("Error in transfer: %s\n", error->message);
631 if (!g_cancellable_is_cancelled (priv->cancellable))
632 g_cancellable_cancel (priv->cancellable);
634 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
638 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
642 EmpathyFTHandler *handler = user_data;
643 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
645 DEBUG ("Transfer operation callback, error %p", error);
649 emit_error_signal (handler, error);
653 priv->is_completed = TRUE;
654 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
656 empathy_tp_file_close (tp_file);
658 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
660 check_hash_incoming (handler);
666 update_remaining_time_and_speed (EmpathyFTHandler *handler,
667 guint64 transferred_bytes)
669 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
670 gint64 elapsed_time, current_time;
671 guint64 transferred, last_transferred_bytes;
675 last_transferred_bytes = priv->transferred_bytes;
676 priv->transferred_bytes = transferred_bytes;
678 current_time = empathy_time_get_current ();
679 elapsed_time = current_time - priv->last_update_time;
681 if (elapsed_time >= 1)
683 transferred = transferred_bytes - last_transferred_bytes;
684 speed = (gdouble) transferred / (gdouble) elapsed_time;
685 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
687 priv->remaining_time = remaining_time;
688 priv->last_update_time = current_time;
693 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
694 guint64 transferred_bytes,
697 EmpathyFTHandler *handler = user_data;
698 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
700 if (empathy_ft_handler_is_cancelled (handler))
703 if (transferred_bytes == 0)
705 priv->last_update_time = empathy_time_get_current ();
706 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
709 if (priv->transferred_bytes != transferred_bytes)
711 update_remaining_time_and_speed (handler, transferred_bytes);
713 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
714 transferred_bytes, priv->total_bytes, priv->remaining_time,
720 ft_handler_create_channel_cb (GObject *source,
721 GAsyncResult *result,
724 EmpathyFTHandler *handler = user_data;
725 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
726 GError *error = NULL;
729 DEBUG ("Dispatcher create channel CB");
731 channel = tp_account_channel_request_create_and_handle_channel_finish (
732 TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
735 DEBUG ("Failed to request FT channel: %s", error->message);
737 g_cancellable_set_error_if_cancelled (priv->cancellable, &error);
741 emit_error_signal (handler, error);
743 g_clear_object (&channel);
744 g_error_free (error);
748 priv->tpfile = EMPATHY_TP_FILE (channel);
750 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
751 ft_transfer_progress_callback, handler,
752 ft_transfer_operation_callback, handler);
756 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
759 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
760 TpAccountChannelRequest *req;
762 DEBUG ("Pushing request to the dispatcher");
764 account = empathy_contact_get_account (priv->contact);
766 req = tp_account_channel_request_new (account, priv->request,
767 priv->user_action_time);
769 tp_account_channel_request_create_and_handle_channel_async (req, NULL,
770 ft_handler_create_channel_cb, handler);
772 g_object_unref (req);
776 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
778 guint contact_handle;
779 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
782 contact_handle = empathy_contact_get_handle (priv->contact);
783 uri = g_file_get_uri (priv->gfile);
785 priv->request = tp_asv_new (
786 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
787 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
788 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
789 TP_HANDLE_TYPE_CONTACT,
790 TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
792 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_TYPE, G_TYPE_STRING,
794 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_FILENAME, G_TYPE_STRING,
796 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_SIZE, G_TYPE_UINT64,
798 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_DATE, G_TYPE_UINT64,
800 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_URI, G_TYPE_STRING, uri,
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;
814 DEBUG ("Closing stream after hashing.");
816 priv = GET_PRIV (handler);
818 if (hash_data->error != NULL)
820 error = hash_data->error;
821 hash_data->error = NULL;
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 _("File transfer completed, but the file was corrupted"));
843 DEBUG ("Hash verification matched, received %s, calculated %s",
845 g_checksum_get_string (hash_data->checksum));
850 /* set the checksum in the request...
851 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
853 tp_asv_set_string (priv->request,
854 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH,
855 g_checksum_get_string (hash_data->checksum));
862 emit_error_signal (handler, error);
863 g_clear_error (&error);
867 g_signal_emit (handler, signals[HASHING_DONE], 0);
869 if (!empathy_ft_handler_is_incoming (handler))
870 /* the request is complete now, push it to the dispatcher */
871 ft_handler_push_to_dispatcher (handler);
874 hash_data_free (hash_data);
880 emit_hashing_progress (gpointer user_data)
882 HashingData *hash_data = user_data;
884 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
885 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
891 do_hash_job (GIOSchedulerJob *job,
892 GCancellable *cancellable,
895 HashingData *hash_data = user_data;
897 GError *error = NULL;
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 check_hashing (CallbacksData *data)
1101 EmpathyFTHandler *handler = data->handler;
1102 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1103 GError *myerr = NULL;
1104 TpCapabilities *caps;
1108 conn = empathy_contact_get_connection (priv->contact);
1110 caps = tp_connection_get_capabilities (conn);
1113 data->callback (handler, NULL, data->user_data);
1117 classes = tp_capabilities_get_channel_classes (caps);
1119 /* set whether we support hash and the type of it */
1120 if (!set_content_hash_type_from_classes (handler, classes))
1122 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1123 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1124 _("File transfer not supported by remote contact"));
1126 if (!g_cancellable_is_cancelled (priv->cancellable))
1127 g_cancellable_cancel (priv->cancellable);
1129 data->callback (handler, myerr, data->user_data);
1130 g_clear_error (&myerr);
1134 /* get back to the caller now */
1135 data->callback (handler, NULL, data->user_data);
1139 callbacks_data_free (data);
1143 ft_handler_complete_request (EmpathyFTHandler *handler)
1145 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1147 /* populate the request table with all the known properties */
1148 ft_handler_populate_outgoing_request (handler);
1151 /* start hashing the file */
1152 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1153 priv->cancellable, ft_handler_read_async_cb, handler);
1155 /* push directly the handler to the dispatcher */
1156 ft_handler_push_to_dispatcher (handler);
1160 ft_handler_gfile_ready_cb (GObject *source,
1162 CallbacksData *cb_data)
1165 GError *error = NULL;
1167 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1169 DEBUG ("Got GFileInfo.");
1171 info = g_file_query_info_finish (priv->gfile, res, &error);
1176 if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1178 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1179 EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1180 _("The selected file is not a regular file"));
1184 priv->total_bytes = g_file_info_get_size (info);
1185 if (priv->total_bytes == 0)
1187 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1188 EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1189 _("The selected file is empty"));
1193 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1194 priv->filename = g_strdup (g_file_info_get_display_name (info));
1195 g_file_info_get_modification_time (info, &mtime);
1196 priv->mtime = mtime.tv_sec;
1197 priv->transferred_bytes = 0;
1198 priv->description = NULL;
1200 g_object_unref (info);
1205 if (!g_cancellable_is_cancelled (priv->cancellable))
1206 g_cancellable_cancel (priv->cancellable);
1208 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1209 g_error_free (error);
1211 callbacks_data_free (cb_data);
1215 /* see if FT/hashing are allowed */
1216 check_hashing (cb_data);
1221 contact_factory_contact_cb (TpConnection *connection,
1222 EmpathyContact *contact,
1223 const GError *error,
1225 GObject *weak_object)
1227 CallbacksData *cb_data = user_data;
1228 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1229 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1233 if (!g_cancellable_is_cancelled (priv->cancellable))
1234 g_cancellable_cancel (priv->cancellable);
1236 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1237 callbacks_data_free (cb_data);
1241 priv->contact = g_object_ref (contact);
1243 cb_data->callback (handler, NULL, cb_data->user_data);
1247 channel_get_all_properties_cb (TpProxy *proxy,
1248 GHashTable *properties,
1249 const GError *error,
1251 GObject *weak_object)
1253 CallbacksData *cb_data = user_data;
1254 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1255 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1260 if (!g_cancellable_is_cancelled (priv->cancellable))
1261 g_cancellable_cancel (priv->cancellable);
1263 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1265 callbacks_data_free (cb_data);
1269 priv->content_hash = g_value_dup_string (
1270 g_hash_table_lookup (properties, "ContentHash"));
1272 priv->content_hash_type = g_value_get_uint (
1273 g_hash_table_lookup (properties, "ContentHashType"));
1275 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1276 empathy_tp_contact_factory_get_from_handle (
1277 tp_channel_borrow_connection (TP_CHANNEL (proxy)), c_handle,
1278 contact_factory_contact_cb, cb_data, callbacks_data_free,
1279 G_OBJECT (handler));
1282 /* public methods */
1285 * empathy_ft_handler_new_outgoing:
1286 * @contact: the #EmpathyContact to send @source to
1287 * @source: the #GFile to send
1288 * @callback: callback to be called when the handler has been created
1289 * @user_data: user data to be passed to @callback
1291 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1294 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1297 EmpathyFTHandlerReadyCallback callback,
1300 EmpathyFTHandler *handler;
1301 CallbacksData *data;
1302 EmpathyFTHandlerPriv *priv;
1304 DEBUG ("New handler outgoing");
1306 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1307 g_return_if_fail (G_IS_FILE (source));
1309 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1312 "user-action-time", action_time,
1315 priv = GET_PRIV (handler);
1317 data = g_slice_new0 (CallbacksData);
1318 data->callback = callback;
1319 data->user_data = user_data;
1320 data->handler = g_object_ref (handler);
1322 /* start collecting info about the file */
1323 g_file_query_info_async (priv->gfile,
1324 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1325 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1326 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1327 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1328 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1329 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1330 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1334 * empathy_ft_handler_new_incoming:
1335 * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1336 * @callback: callback to be called when the handler has been created
1337 * @user_data: user data to be passed to @callback
1339 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1340 * Note that for the handler to be useful, you will have to set a destination
1341 * file with empathy_ft_handler_incoming_set_destination() after the handler
1345 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1346 EmpathyFTHandlerReadyCallback callback,
1349 EmpathyFTHandler *handler;
1350 CallbacksData *data;
1351 TpFileTransferChannel *ft_chan = (TpFileTransferChannel *) tp_file;
1352 EmpathyFTHandlerPriv *priv;
1354 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1356 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1357 "tp-file", tp_file, NULL);
1359 priv = GET_PRIV (handler);
1361 data = g_slice_new0 (CallbacksData);
1362 data->callback = callback;
1363 data->user_data = user_data;
1364 data->handler = g_object_ref (handler);
1366 priv->total_bytes = tp_file_transfer_channel_get_size (ft_chan);
1368 priv->transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (
1371 priv->filename = g_strdup (tp_file_transfer_channel_get_filename (ft_chan));
1373 priv->content_type = g_strdup (tp_file_transfer_channel_get_mime_type (
1376 priv->description = g_strdup (tp_file_transfer_channel_get_description (
1379 tp_cli_dbus_properties_call_get_all (tp_file,
1380 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1381 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1385 * empathy_ft_handler_start_transfer:
1386 * @handler: an #EmpathyFTHandler
1388 * Starts the transfer machinery. After this call, the transfer and hashing
1389 * signals will be emitted by the handler.
1392 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1394 EmpathyFTHandlerPriv *priv;
1396 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1398 priv = GET_PRIV (handler);
1400 if (priv->tpfile == NULL)
1402 ft_handler_complete_request (handler);
1406 /* TODO: add support for resume. */
1407 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1408 ft_transfer_progress_callback, handler,
1409 ft_transfer_operation_callback, handler);
1414 * empathy_ft_handler_cancel_transfer:
1415 * @handler: an #EmpathyFTHandler
1417 * Cancels an ongoing handler operation. Note that this doesn't destroy
1418 * the object, which will keep all the properties, altough it won't be able
1419 * to do any more I/O.
1422 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1424 EmpathyFTHandlerPriv *priv;
1426 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1428 priv = GET_PRIV (handler);
1430 /* if we don't have an EmpathyTpFile, we are hashing, so
1431 * we can just cancel the GCancellable to stop it.
1433 if (priv->tpfile == NULL)
1434 g_cancellable_cancel (priv->cancellable);
1436 empathy_tp_file_cancel (priv->tpfile);
1440 * empathy_ft_handler_incoming_set_destination:
1441 * @handler: an #EmpathyFTHandler
1442 * @destination: the #GFile where the transfer should be saved
1444 * Sets the destination of the incoming handler to be @destination.
1445 * Note that calling this method is mandatory before starting the transfer
1446 * for incoming handlers.
1449 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1452 EmpathyFTHandlerPriv *priv;
1454 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1455 g_return_if_fail (G_IS_FILE (destination));
1457 priv = GET_PRIV (handler);
1459 g_object_set (handler, "gfile", destination, NULL);
1461 /* check if hash is supported. if it isn't, set use_hash to FALSE
1462 * anyway, so that clients won't be expecting us to checksum.
1464 if (EMP_STR_EMPTY (priv->content_hash) ||
1465 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1466 priv->use_hash = FALSE;
1468 priv->use_hash = TRUE;
1472 * empathy_ft_handler_get_filename:
1473 * @handler: an #EmpathyFTHandler
1475 * Returns the name of the file being transferred.
1477 * Return value: the name of the file being transferred
1480 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1482 EmpathyFTHandlerPriv *priv;
1484 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1486 priv = GET_PRIV (handler);
1488 return priv->filename;
1492 * empathy_ft_handler_get_content_type:
1493 * @handler: an #EmpathyFTHandler
1495 * Returns the content type of the file being transferred.
1497 * Return value: the content type of the file being transferred
1500 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1502 EmpathyFTHandlerPriv *priv;
1504 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1506 priv = GET_PRIV (handler);
1508 return priv->content_type;
1512 * empathy_ft_handler_get_contact:
1513 * @handler: an #EmpathyFTHandler
1515 * Returns the remote #EmpathyContact at the other side of the transfer.
1517 * Return value: the remote #EmpathyContact for @handler
1520 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1522 EmpathyFTHandlerPriv *priv;
1524 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1526 priv = GET_PRIV (handler);
1528 return priv->contact;
1532 * empathy_ft_handler_get_gfile:
1533 * @handler: an #EmpathyFTHandler
1535 * Returns the #GFile where the transfer is being read/saved.
1537 * Return value: the #GFile where the transfer is being read/saved
1540 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1542 EmpathyFTHandlerPriv *priv;
1544 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1546 priv = GET_PRIV (handler);
1552 * empathy_ft_handler_get_use_hash:
1553 * @handler: an #EmpathyFTHandler
1555 * Returns whether @handler has checksumming enabled. This can depend on
1556 * the CM and the remote contact capabilities.
1558 * Return value: %TRUE if the handler has checksumming enabled,
1562 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1564 EmpathyFTHandlerPriv *priv;
1566 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1568 priv = GET_PRIV (handler);
1570 return priv->use_hash;
1574 * empathy_ft_handler_is_incoming:
1575 * @handler: an #EmpathyFTHandler
1577 * Returns whether @handler is incoming or outgoing.
1579 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1582 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1584 EmpathyFTHandlerPriv *priv;
1586 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1588 priv = GET_PRIV (handler);
1590 if (priv->tpfile == NULL)
1593 return !tp_channel_get_requested ((TpChannel *) priv->tpfile);
1597 * empathy_ft_handler_get_transferred_bytes:
1598 * @handler: an #EmpathyFTHandler
1600 * Returns the number of bytes already transferred by the handler.
1602 * Return value: the number of bytes already transferred by the handler.
1605 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1607 EmpathyFTHandlerPriv *priv;
1609 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1611 priv = GET_PRIV (handler);
1613 return priv->transferred_bytes;
1617 * empathy_ft_handler_get_total_bytes:
1618 * @handler: an #EmpathyFTHandler
1620 * Returns the total size of the file being transferred by the handler.
1622 * Return value: a number of bytes indicating the total size of the file being
1623 * transferred by the handler.
1626 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1628 EmpathyFTHandlerPriv *priv;
1630 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1632 priv = GET_PRIV (handler);
1634 return priv->total_bytes;
1638 * empathy_ft_handler_is_completed:
1639 * @handler: an #EmpathyFTHandler
1641 * Returns whether the transfer for @handler has been completed succesfully.
1643 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1647 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1649 EmpathyFTHandlerPriv *priv;
1651 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1653 priv = GET_PRIV (handler);
1655 return priv->is_completed;
1659 * empathy_ft_handler_is_cancelled:
1660 * @handler: an #EmpathyFTHandler
1662 * Returns whether the transfer for @handler has been cancelled or has stopped
1665 * Return value: %TRUE if the transfer for @handler has been cancelled
1666 * or has stopped due to an error, %FALSE otherwise.
1669 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1671 EmpathyFTHandlerPriv *priv;
1673 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1675 priv = GET_PRIV (handler);
1677 return g_cancellable_is_cancelled (priv->cancellable);