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>
28 #include <telepathy-glib/interfaces.h>
30 #include "empathy-ft-handler.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-dispatcher.h"
33 #include "empathy-marshal.h"
34 #include "empathy-time.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_FT
38 #include "empathy-debug.h"
41 * SECTION:empathy-ft-handler
42 * @title: EmpathyFTHandler
43 * @short_description: an object representing a File Transfer
44 * @include: libempathy/empathy-ft-handler
46 * #EmpathyFTHandler is the object which represents a File Transfer with all
48 * The creation of an #EmpathyFTHandler is done with
49 * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
50 * even though clients should not need to call them directly, as
51 * #EmpathyFTFactory does it for them. Remember that for the file transfer
52 * to work with an incoming handler,
53 * empathy_ft_handler_incoming_set_destination() should be called after
54 * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
56 * It's important to note that, as the creation of the handlers is async, once
57 * an handler is created, it already has all the interesting properties set,
58 * like filename, total bytes, content type and so on, making it useful
59 * to be displayed in an UI.
60 * The transfer API works like a state machine; it has three signals,
61 * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
62 * emitted in the relevant phases.
63 * In addition, if the handler is created with checksumming enabled,
64 * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
65 * will be emitted before or after the transfer, depending on the direction
66 * (respectively outgoing and incoming) of the handler.
67 * At any time between the call to empathy_ft_handler_start_transfer() and
68 * the last signal, a ::transfer-error can be emitted, indicating that an
69 * error has happened in the operation. The message of the error is localized
73 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
75 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
77 #define BUFFER_SIZE 4096
86 PROP_MODIFICATION_TIME,
88 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 EmpathyDispatcher *dispatcher;
129 /* request for the new transfer */
132 /* transfer properties */
133 EmpathyContact *contact;
138 guint64 transferred_bytes;
141 TpFileHashType content_hash_type;
145 guint remaining_time;
146 time_t 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);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
200 do_set_property (GObject *object,
205 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
210 priv->contact = g_value_dup_object (value);
212 case PROP_CONTENT_TYPE:
213 priv->content_type = g_value_dup_string (value);
215 case PROP_DESCRIPTION:
216 priv->description = g_value_dup_string (value);
219 priv->filename = g_value_dup_string (value);
221 case PROP_MODIFICATION_TIME:
222 priv->mtime = g_value_get_uint64 (value);
224 case PROP_TOTAL_BYTES:
225 priv->total_bytes = g_value_get_uint64 (value);
227 case PROP_TRANSFERRED_BYTES:
228 priv->transferred_bytes = g_value_get_uint64 (value);
231 priv->gfile = g_value_dup_object (value);
234 priv->tpfile = g_value_dup_object (value);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
242 do_dispose (GObject *object)
244 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
246 if (priv->dispose_run)
249 priv->dispose_run = TRUE;
251 if (priv->contact != NULL) {
252 g_object_unref (priv->contact);
253 priv->contact = NULL;
256 if (priv->gfile != NULL) {
257 g_object_unref (priv->gfile);
261 if (priv->tpfile != NULL) {
262 empathy_tp_file_close (priv->tpfile);
263 g_object_unref (priv->tpfile);
267 if (priv->cancellable != NULL) {
268 g_object_unref (priv->cancellable);
269 priv->cancellable = NULL;
272 if (priv->request != NULL)
274 g_hash_table_unref (priv->request);
275 priv->request = NULL;
278 if (priv->dispatcher != NULL)
280 g_object_unref (priv->dispatcher);
281 priv->dispatcher = 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);
426 * EmpathyFTHandler::transfer-started
427 * @handler: the object which has received the signal
428 * @tp_file: the #EmpathyTpFile for which the transfer has started
430 * This signal is emitted when the actual transfer starts.
432 signals[TRANSFER_STARTED] =
433 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
434 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
435 g_cclosure_marshal_VOID__OBJECT,
437 1, EMPATHY_TYPE_TP_FILE);
440 * EmpathyFTHandler::transfer-done
441 * @handler: the object which has received the signal
442 * @tp_file: the #EmpathyTpFile for which the transfer has started
444 * This signal will be emitted when the actual transfer is completed
447 signals[TRANSFER_DONE] =
448 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
449 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
450 g_cclosure_marshal_VOID__OBJECT,
452 1, EMPATHY_TYPE_TP_FILE);
455 * EmpathyFTHandler::transfer-error
456 * @handler: the object which has received the signal
459 * This signal can be emitted anytime between the call to
460 * empathy_ft_handler_start_transfer() and the last expected signal
461 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
462 * signal coming from the handler, meaning that no other operation will
463 * take place after this signal.
465 signals[TRANSFER_ERROR] =
466 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
467 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
468 g_cclosure_marshal_VOID__POINTER,
473 * EmpathyFTHandler::transfer-progress
474 * @handler: the object which has received the signal
475 * @current_bytes: the bytes currently transferred
476 * @total_bytes: the total bytes of the handler
477 * @remaining_time: the number of seconds remaining for the transfer
479 * @speed: the current speed of the transfer (in KB/s)
481 * This signal is emitted to notify clients of the progress of the
484 signals[TRANSFER_PROGRESS] =
485 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
486 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
487 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
489 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
492 * EmpathyFTHandler::hashing-started
493 * @handler: the object which has received the signal
495 * This signal is emitted when the hashing operation of the handler
496 * is started. Note that this might happen or not, depending on the CM
497 * and remote contact capabilities. Clients shoud use
498 * empathy_ft_handler_get_use_hash() before calling
499 * empathy_ft_handler_start_transfer() to know whether they should connect
502 signals[HASHING_STARTED] =
503 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
504 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
505 g_cclosure_marshal_VOID__VOID,
509 * EmpathyFTHandler::hashing-progress
510 * @handler: the object which has received the signal
511 * @current_bytes: the bytes currently hashed
512 * @total_bytes: the total bytes of the handler
514 * This signal is emitted to notify clients of the progress of the
517 signals[HASHING_PROGRESS] =
518 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
519 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
520 _empathy_marshal_VOID__UINT64_UINT64,
522 2, G_TYPE_UINT64, G_TYPE_UINT64);
525 * EmpathyFTHandler::hashing-done
526 * @handler: the object which has received the signal
528 * This signal is emitted when the hashing operation of the handler
531 signals[HASHING_DONE] =
532 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
533 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
534 g_cclosure_marshal_VOID__VOID,
539 empathy_ft_handler_init (EmpathyFTHandler *self)
541 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
542 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
545 priv->cancellable = g_cancellable_new ();
546 priv->dispatcher = empathy_dispatcher_dup_singleton ();
549 /* private functions */
552 hash_data_free (HashingData *data)
554 g_free (data->buffer);
556 if (data->stream != NULL)
557 g_object_unref (data->stream);
559 if (data->checksum != NULL)
560 g_checksum_free (data->checksum);
562 if (data->error != NULL)
563 g_error_free (data->error);
565 if (data->handler != NULL)
566 g_object_unref (data->handler);
568 g_slice_free (HashingData, data);
572 tp_file_hash_to_g_checksum (TpFileHashType type)
574 GChecksumType retval;
578 case TP_FILE_HASH_TYPE_MD5:
579 retval = G_CHECKSUM_MD5;
581 case TP_FILE_HASH_TYPE_SHA1:
582 retval = G_CHECKSUM_SHA1;
584 case TP_FILE_HASH_TYPE_SHA256:
585 retval = G_CHECKSUM_SHA256;
588 g_assert_not_reached ();
596 check_hash_incoming (EmpathyFTHandler *handler)
598 HashingData *hash_data;
599 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
601 if (!EMP_STR_EMPTY (priv->content_hash))
603 hash_data = g_slice_new0 (HashingData);
604 hash_data->total_bytes = priv->total_bytes;
605 hash_data->handler = g_object_ref (handler);
606 hash_data->checksum = g_checksum_new
607 (tp_file_hash_to_g_checksum (priv->content_hash_type));
609 g_signal_emit (handler, signals[HASHING_STARTED], 0);
611 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
612 G_PRIORITY_DEFAULT, priv->cancellable);
617 emit_error_signal (EmpathyFTHandler *handler,
620 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
622 DEBUG ("Error in transfer: %s\n", error->message);
624 if (!g_cancellable_is_cancelled (priv->cancellable))
625 g_cancellable_cancel (priv->cancellable);
627 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
631 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
635 EmpathyFTHandler *handler = user_data;
636 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
638 DEBUG ("Transfer operation callback, error %p", error);
642 emit_error_signal (handler, error);
646 priv->is_completed = TRUE;
647 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
649 empathy_tp_file_close (tp_file);
651 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
653 check_hash_incoming (handler);
659 update_remaining_time_and_speed (EmpathyFTHandler *handler,
660 guint64 transferred_bytes)
662 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
663 time_t elapsed_time, current_time;
664 guint64 transferred, last_transferred_bytes;
668 last_transferred_bytes = priv->transferred_bytes;
669 priv->transferred_bytes = transferred_bytes;
671 current_time = empathy_time_get_current ();
672 elapsed_time = current_time - priv->last_update_time;
674 if (elapsed_time >= 1)
676 transferred = transferred_bytes - last_transferred_bytes;
677 speed = (gdouble) transferred / (gdouble) elapsed_time;
678 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
680 priv->remaining_time = remaining_time;
681 priv->last_update_time = current_time;
686 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
687 guint64 transferred_bytes,
690 EmpathyFTHandler *handler = user_data;
691 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
693 if (empathy_ft_handler_is_cancelled (handler))
696 if (transferred_bytes == 0)
698 priv->last_update_time = empathy_time_get_current ();
699 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
702 if (priv->transferred_bytes != transferred_bytes)
704 update_remaining_time_and_speed (handler, transferred_bytes);
706 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
707 transferred_bytes, priv->total_bytes, priv->remaining_time,
713 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
717 EmpathyFTHandler *handler = user_data;
718 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
719 GError *my_error = (GError *) error;
721 DEBUG ("Dispatcher create channel CB");
723 if (my_error == NULL)
725 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
728 if (my_error != NULL)
730 emit_error_signal (handler, my_error);
732 if (my_error != error)
733 g_clear_error (&my_error);
738 priv->tpfile = g_object_ref
739 (empathy_dispatch_operation_get_channel_wrapper (operation));
741 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
742 ft_transfer_progress_callback, handler,
743 ft_transfer_operation_callback, handler);
745 empathy_dispatch_operation_claim (operation);
749 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
751 TpConnection *connection;
752 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
754 DEBUG ("Pushing request to the dispatcher");
756 connection = empathy_contact_get_connection (priv->contact);
758 /* I want to own a reference to the request, and destroy it later */
759 empathy_dispatcher_create_channel (priv->dispatcher, connection,
760 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
764 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
766 guint contact_handle;
769 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
771 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
772 NULL, (GDestroyNotify) tp_g_value_slice_free);
774 contact_handle = empathy_contact_get_handle (priv->contact);
776 /* org.freedesktop.Telepathy.Channel.ChannelType */
777 value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
778 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
780 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
781 value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT);
782 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
784 /* org.freedesktop.Telepathy.Channel.TargetHandle */
785 value = tp_g_value_slice_new_uint (contact_handle);
786 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
788 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
789 value = tp_g_value_slice_new_string (priv->content_type);
790 g_hash_table_insert (request,
791 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
793 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
794 value = tp_g_value_slice_new_string (priv->filename);
795 g_hash_table_insert (request,
796 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
798 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
799 value = tp_g_value_slice_new_uint64 (priv->total_bytes);
800 g_hash_table_insert (request,
801 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
803 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
804 value = tp_g_value_slice_new_uint64 ((guint64) priv->mtime);
805 g_hash_table_insert (request,
806 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
810 hash_job_done (gpointer user_data)
812 HashingData *hash_data = user_data;
813 EmpathyFTHandler *handler = hash_data->handler;
814 EmpathyFTHandlerPriv *priv;
815 GError *error = NULL;
818 DEBUG ("Closing stream after hashing.");
820 priv = GET_PRIV (handler);
822 if (hash_data->error != NULL)
824 error = hash_data->error;
825 hash_data->error = NULL;
829 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
831 if (empathy_ft_handler_is_incoming (handler))
833 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
836 DEBUG ("Hash mismatch when checking incoming handler: "
837 "received %s, calculated %s", priv->content_hash,
838 g_checksum_get_string (hash_data->checksum));
840 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
841 EMPATHY_FT_ERROR_HASH_MISMATCH,
842 _("The hash of the received file and the "
843 "sent one do not match"));
848 DEBUG ("Hash verification matched, received %s, calculated %s",
850 g_checksum_get_string (hash_data->checksum));
855 /* set the checksum in the request...
856 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
858 value = tp_g_value_slice_new_string
859 (g_checksum_get_string (hash_data->checksum));
860 g_hash_table_insert (priv->request,
861 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
868 emit_error_signal (handler, error);
869 g_clear_error (&error);
873 g_signal_emit (handler, signals[HASHING_DONE], 0);
875 if (!empathy_ft_handler_is_incoming (handler))
876 /* the request is complete now, push it to the dispatcher */
877 ft_handler_push_to_dispatcher (handler);
880 hash_data_free (hash_data);
886 emit_hashing_progress (gpointer user_data)
888 HashingData *hash_data = user_data;
890 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
891 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
897 do_hash_job (GIOSchedulerJob *job,
898 GCancellable *cancellable,
901 HashingData *hash_data = user_data;
903 EmpathyFTHandlerPriv *priv;
904 GError *error = NULL;
906 priv = GET_PRIV (hash_data->handler);
909 if (hash_data->buffer == NULL)
910 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
912 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
913 BUFFER_SIZE, cancellable, &error);
917 hash_data->total_read += bytes_read;
919 /* we now have the chunk */
922 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
923 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
926 g_free (hash_data->buffer);
927 hash_data->buffer = NULL;
933 g_input_stream_close (hash_data->stream, cancellable, &error);
938 hash_data->error = error;
940 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
947 do_hash_job_incoming (GIOSchedulerJob *job,
948 GCancellable *cancellable,
951 HashingData *hash_data = user_data;
952 EmpathyFTHandler *handler = hash_data->handler;
953 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
954 GError *error = NULL;
956 DEBUG ("checking integrity for incoming handler");
958 /* need to get the stream first */
960 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
964 hash_data->error = error;
965 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
970 return do_hash_job (job, cancellable, user_data);
974 ft_handler_read_async_cb (GObject *source,
978 GFileInputStream *stream;
979 GError *error = NULL;
980 HashingData *hash_data;
982 EmpathyFTHandler *handler = user_data;
983 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
985 DEBUG ("GFile read async CB.");
987 stream = g_file_read_finish (priv->gfile, res, &error);
990 emit_error_signal (handler, error);
991 g_clear_error (&error);
996 hash_data = g_slice_new0 (HashingData);
997 hash_data->stream = G_INPUT_STREAM (stream);
998 hash_data->total_bytes = priv->total_bytes;
999 hash_data->handler = g_object_ref (handler);
1000 /* FIXME: MD5 is the only ContentHashType supported right now */
1001 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
1003 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
1004 value = tp_g_value_slice_new_uint (TP_FILE_HASH_TYPE_MD5);
1005 g_hash_table_insert (priv->request,
1006 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
1008 g_signal_emit (handler, signals[HASHING_STARTED], 0);
1010 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1011 G_PRIORITY_DEFAULT, priv->cancellable);
1015 callbacks_data_free (gpointer user_data)
1017 CallbacksData *data = user_data;
1019 if (data->handler != NULL)
1020 g_object_unref (data->handler);
1022 g_slice_free (CallbacksData, data);
1026 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1032 GArray *possible_values;
1036 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1038 possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1040 for (l = classes; l != NULL; l = l->next)
1043 v = g_value_array_get_nth (class, 0);
1044 fprops = g_value_get_boxed (v);
1046 value = tp_asv_get_uint32
1047 (fprops, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType",
1051 g_array_append_val (possible_values, value);
1054 if (possible_values->len == 0)
1056 /* there are no channel classes with hash support, disable it. */
1057 priv->use_hash = FALSE;
1058 priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1063 priv->use_hash = TRUE;
1065 if (possible_values->len == 1)
1067 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1071 /* order the array and pick the first non zero, so that MD5
1072 * is the preferred value.
1074 g_array_sort (possible_values, empathy_uint_compare);
1076 if (g_array_index (possible_values, guint, 0) == 0)
1077 priv->content_hash_type = g_array_index (possible_values, guint, 1);
1079 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1083 g_array_free (possible_values, TRUE);
1085 DEBUG ("Hash enabled %s; setting content hash type as %u",
1086 priv->use_hash ? "True" : "False", priv->content_hash_type);
1090 find_ft_channel_classes_cb (GList *channel_classes,
1093 CallbacksData *data = user_data;
1094 EmpathyFTHandler *handler = data->handler;
1095 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1096 GError *myerr = NULL;
1098 if (channel_classes == NULL)
1100 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1101 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1102 _("File transfer not supported by remote contact"));
1104 if (!g_cancellable_is_cancelled (priv->cancellable))
1105 g_cancellable_cancel (priv->cancellable);
1107 data->callback (handler, myerr, data->user_data);
1108 g_clear_error (&myerr);
1112 /* set whether we support hash and the type of it */
1113 set_content_hash_type_from_classes (handler, channel_classes);
1115 /* get back to the caller now */
1116 data->callback (handler, NULL, data->user_data);
1119 callbacks_data_free (data);
1123 ft_handler_complete_request (EmpathyFTHandler *handler)
1125 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1127 /* populate the request table with all the known properties */
1128 ft_handler_populate_outgoing_request (handler);
1131 /* start hashing the file */
1132 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1133 priv->cancellable, ft_handler_read_async_cb, handler);
1135 /* push directly the handler to the dispatcher */
1136 ft_handler_push_to_dispatcher (handler);
1140 ft_handler_gfile_ready_cb (GObject *source,
1142 CallbacksData *cb_data)
1145 GError *error = NULL;
1147 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1149 DEBUG ("Got GFileInfo.");
1151 info = g_file_query_info_finish (priv->gfile, res, &error);
1156 if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1158 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1159 EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1160 _("The selected file is not a regular file"));
1164 priv->total_bytes = g_file_info_get_size (info);
1165 if (priv->total_bytes == 0)
1167 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1168 EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1169 _("The selected file is empty"));
1173 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1174 priv->filename = g_strdup (g_file_info_get_display_name (info));
1175 g_file_info_get_modification_time (info, &mtime);
1176 priv->mtime = mtime.tv_sec;
1177 priv->transferred_bytes = 0;
1178 priv->description = NULL;
1180 g_object_unref (info);
1185 if (!g_cancellable_is_cancelled (priv->cancellable))
1186 g_cancellable_cancel (priv->cancellable);
1188 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1189 g_error_free (error);
1191 callbacks_data_free (cb_data);
1195 /* see if FT/hashing are allowed */
1196 empathy_dispatcher_find_requestable_channel_classes_async
1197 (priv->dispatcher, empathy_contact_get_connection (priv->contact),
1198 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
1199 find_ft_channel_classes_cb, cb_data,
1200 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", NULL);
1205 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
1206 EmpathyContact *contact,
1207 const GError *error,
1209 GObject *weak_object)
1211 CallbacksData *cb_data = user_data;
1212 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1213 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1217 if (!g_cancellable_is_cancelled (priv->cancellable))
1218 g_cancellable_cancel (priv->cancellable);
1220 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1221 callbacks_data_free (cb_data);
1225 priv->contact = g_object_ref (contact);
1227 cb_data->callback (handler, NULL, cb_data->user_data);
1231 channel_get_all_properties_cb (TpProxy *proxy,
1232 GHashTable *properties,
1233 const GError *error,
1235 GObject *weak_object)
1237 CallbacksData *cb_data = user_data;
1238 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1239 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1240 EmpathyTpContactFactory *c_factory;
1245 if (!g_cancellable_is_cancelled (priv->cancellable))
1246 g_cancellable_cancel (priv->cancellable);
1248 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1250 callbacks_data_free (cb_data);
1254 priv->total_bytes = g_value_get_uint64 (
1255 g_hash_table_lookup (properties, "Size"));
1257 priv->transferred_bytes = g_value_get_uint64 (
1258 g_hash_table_lookup (properties, "TransferredBytes"));
1260 priv->filename = g_value_dup_string (
1261 g_hash_table_lookup (properties, "Filename"));
1263 priv->content_hash = g_value_dup_string (
1264 g_hash_table_lookup (properties, "ContentHash"));
1266 priv->content_hash_type = g_value_get_uint (
1267 g_hash_table_lookup (properties, "ContentHashType"));
1269 priv->content_type = g_value_dup_string (
1270 g_hash_table_lookup (properties, "ContentType"));
1272 priv->description = g_value_dup_string (
1273 g_hash_table_lookup (properties, "Description"));
1275 c_factory = empathy_tp_contact_factory_dup_singleton
1276 (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
1277 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1278 empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
1279 contact_factory_contact_cb, cb_data, callbacks_data_free,
1280 G_OBJECT (handler));
1282 g_object_unref (c_factory);
1285 /* public methods */
1288 * empathy_ft_handler_new_outgoing:
1289 * @contact: the #EmpathyContact to send @source to
1290 * @source: the #GFile to send
1291 * @callback: callback to be called when the handler has been created
1292 * @user_data: user data to be passed to @callback
1294 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1297 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1299 EmpathyFTHandlerReadyCallback callback,
1302 EmpathyFTHandler *handler;
1303 CallbacksData *data;
1304 EmpathyFTHandlerPriv *priv;
1306 DEBUG ("New handler outgoing");
1308 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1309 g_return_if_fail (G_IS_FILE (source));
1311 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1312 "contact", contact, "gfile", source, NULL);
1314 priv = GET_PRIV (handler);
1316 data = g_slice_new0 (CallbacksData);
1317 data->callback = callback;
1318 data->user_data = user_data;
1319 data->handler = g_object_ref (handler);
1321 /* start collecting info about the file */
1322 g_file_query_info_async (priv->gfile,
1323 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1324 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1325 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1326 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1327 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1328 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1329 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1333 * empathy_ft_handler_new_incoming:
1334 * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1335 * @callback: callback to be called when the handler has been created
1336 * @user_data: user data to be passed to @callback
1338 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1339 * Note that for the handler to be useful, you will have to set a destination
1340 * file with empathy_ft_handler_incoming_set_destination() after the handler
1344 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1345 EmpathyFTHandlerReadyCallback callback,
1348 EmpathyFTHandler *handler;
1350 CallbacksData *data;
1352 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1354 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1355 "tp-file", tp_file, NULL);
1357 g_object_get (tp_file, "channel", &channel, NULL);
1359 data = g_slice_new0 (CallbacksData);
1360 data->callback = callback;
1361 data->user_data = user_data;
1362 data->handler = g_object_ref (handler);
1364 tp_cli_dbus_properties_call_get_all (channel,
1365 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1366 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1370 * empathy_ft_handler_start_transfer:
1371 * @handler: an #EmpathyFTHandler
1373 * Starts the transfer machinery. After this call, the transfer and hashing
1374 * signals will be emitted by the handler.
1377 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1379 EmpathyFTHandlerPriv *priv;
1381 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1383 priv = GET_PRIV (handler);
1385 if (priv->tpfile == NULL)
1387 ft_handler_complete_request (handler);
1391 /* TODO: add support for resume. */
1392 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1393 ft_transfer_progress_callback, handler,
1394 ft_transfer_operation_callback, handler);
1399 * empathy_ft_handler_cancel_transfer:
1400 * @handler: an #EmpathyFTHandler
1402 * Cancels an ongoing handler operation. Note that this doesn't destroy
1403 * the object, which will keep all the properties, altough it won't be able
1404 * to do any more I/O.
1407 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1409 EmpathyFTHandlerPriv *priv;
1411 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1413 priv = GET_PRIV (handler);
1415 /* if we don't have an EmpathyTpFile, we are hashing, so
1416 * we can just cancel the GCancellable to stop it.
1418 if (priv->tpfile == NULL)
1419 g_cancellable_cancel (priv->cancellable);
1421 empathy_tp_file_cancel (priv->tpfile);
1425 * empathy_ft_handler_incoming_set_destination:
1426 * @handler: an #EmpathyFTHandler
1427 * @destination: the #GFile where the transfer should be saved
1429 * Sets the destination of the incoming handler to be @destination.
1430 * Note that calling this method is mandatory before starting the transfer
1431 * for incoming handlers.
1434 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1437 EmpathyFTHandlerPriv *priv;
1439 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1440 g_return_if_fail (G_IS_FILE (destination));
1442 priv = GET_PRIV (handler);
1444 g_object_set (handler, "gfile", destination, NULL);
1446 /* check if hash is supported. if it isn't, set use_hash to FALSE
1447 * anyway, so that clients won't be expecting us to checksum.
1449 if (EMP_STR_EMPTY (priv->content_hash) ||
1450 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1451 priv->use_hash = FALSE;
1453 priv->use_hash = TRUE;
1457 * empathy_ft_handler_get_filename:
1458 * @handler: an #EmpathyFTHandler
1460 * Returns the name of the file being transferred.
1462 * Return value: the name of the file being transferred
1465 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1467 EmpathyFTHandlerPriv *priv;
1469 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1471 priv = GET_PRIV (handler);
1473 return priv->filename;
1477 * empathy_ft_handler_get_content_type:
1478 * @handler: an #EmpathyFTHandler
1480 * Returns the content type of the file being transferred.
1482 * Return value: the content type of the file being transferred
1485 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1487 EmpathyFTHandlerPriv *priv;
1489 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1491 priv = GET_PRIV (handler);
1493 return priv->content_type;
1497 * empathy_ft_handler_get_contact:
1498 * @handler: an #EmpathyFTHandler
1500 * Returns the remote #EmpathyContact at the other side of the transfer.
1502 * Return value: the remote #EmpathyContact for @handler
1505 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1507 EmpathyFTHandlerPriv *priv;
1509 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1511 priv = GET_PRIV (handler);
1513 return priv->contact;
1517 * empathy_ft_handler_get_gfile:
1518 * @handler: an #EmpathyFTHandler
1520 * Returns the #GFile where the transfer is being read/saved.
1522 * Return value: the #GFile where the transfer is being read/saved
1525 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1527 EmpathyFTHandlerPriv *priv;
1529 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1531 priv = GET_PRIV (handler);
1537 * empathy_ft_handler_get_use_hash:
1538 * @handler: an #EmpathyFTHandler
1540 * Returns whether @handler has checksumming enabled. This can depend on
1541 * the CM and the remote contact capabilities.
1543 * Return value: %TRUE if the handler has checksumming enabled,
1547 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1549 EmpathyFTHandlerPriv *priv;
1551 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1553 priv = GET_PRIV (handler);
1555 return priv->use_hash;
1559 * empathy_ft_handler_is_incoming:
1560 * @handler: an #EmpathyFTHandler
1562 * Returns whether @handler is incoming or outgoing.
1564 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1567 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1569 EmpathyFTHandlerPriv *priv;
1571 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1573 priv = GET_PRIV (handler);
1575 if (priv->tpfile == NULL)
1578 return empathy_tp_file_is_incoming (priv->tpfile);
1582 * empathy_ft_handler_get_transferred_bytes:
1583 * @handler: an #EmpathyFTHandler
1585 * Returns the number of bytes already transferred by the handler.
1587 * Return value: the number of bytes already transferred by the handler.
1590 empathy_ft_handler_get_transferred_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->transferred_bytes;
1602 * empathy_ft_handler_get_total_bytes:
1603 * @handler: an #EmpathyFTHandler
1605 * Returns the total size of the file being transferred by the handler.
1607 * Return value: a number of bytes indicating the total size of the file being
1608 * transferred by the handler.
1611 empathy_ft_handler_get_total_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->total_bytes;
1623 * empathy_ft_handler_is_completed:
1624 * @handler: an #EmpathyFTHandler
1626 * Returns whether the transfer for @handler has been completed succesfully.
1628 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1632 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1634 EmpathyFTHandlerPriv *priv;
1636 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1638 priv = GET_PRIV (handler);
1640 return priv->is_completed;
1644 * empathy_ft_handler_is_cancelled:
1645 * @handler: an #EmpathyFTHandler
1647 * Returns whether the transfer for @handler has been cancelled or has stopped
1650 * Return value: %TRUE if the transfer for @handler has been cancelled
1651 * or has stopped due to an error, %FALSE otherwise.
1654 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1656 EmpathyFTHandlerPriv *priv;
1658 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1660 priv = GET_PRIV (handler);
1662 return g_cancellable_is_cancelled (priv->cancellable);