2 * empathy-ft-handler.c - Source for EmpathyFTHandler
3 * Copyright (C) 2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 /* empathy-ft-handler.c */
25 #include <glib/gi18n.h>
26 #include <telepathy-glib/util.h>
28 #include "empathy-ft-handler.h"
29 #include "empathy-tp-contact-factory.h"
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-time.h"
33 #include "empathy-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_FT
36 #include "empathy-debug.h"
38 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
42 #define BUFFER_SIZE 4096
69 EmpathyFTHandler *handler;
73 EmpathyFTHandlerReadyCallback callback;
75 EmpathyFTHandler *handler;
83 EmpathyTpFile *tpfile;
84 GCancellable *cancellable;
87 /* request for the new transfer */
90 /* transfer properties */
91 EmpathyContact *contact;
96 guint64 transferred_bytes;
99 TpFileHashType content_hash_type;
100 TpFileTransferState current_state;
104 guint remaining_time;
105 time_t last_update_time;
107 gboolean is_completed;
108 } EmpathyFTHandlerPriv;
110 static guint signals[LAST_SIGNAL] = { 0 };
112 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
113 GCancellable *cancellable, gpointer user_data);
115 /* GObject implementations */
117 do_get_property (GObject *object,
122 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
127 g_value_set_object (value, priv->contact);
130 g_value_set_object (value, priv->gfile);
133 g_value_set_object (value, priv->tpfile);
136 g_value_set_boolean (value, priv->use_hash);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144 do_set_property (GObject *object,
149 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
154 priv->contact = g_value_dup_object (value);
157 priv->gfile = g_value_dup_object (value);
160 priv->tpfile = g_value_dup_object (value);
163 priv->use_hash = g_value_get_boolean (value);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
171 do_dispose (GObject *object)
173 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
175 if (priv->dispose_run)
178 priv->dispose_run = TRUE;
181 g_object_unref (priv->contact);
182 priv->contact = NULL;
186 g_object_unref (priv->gfile);
191 empathy_tp_file_close (priv->tpfile);
192 g_object_unref (priv->tpfile);
196 if (priv->cancellable) {
197 g_object_unref (priv->cancellable);
198 priv->cancellable = NULL;
201 if (priv->request != NULL)
203 g_hash_table_unref (priv->request);
204 priv->request = NULL;
207 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
211 do_finalize (GObject *object)
213 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
215 DEBUG ("%p", object);
217 g_free (priv->content_type);
218 priv->content_type = NULL;
220 g_free (priv->filename);
221 priv->filename = NULL;
223 g_free (priv->description);
224 priv->description = NULL;
226 g_free (priv->content_hash);
227 priv->content_hash = NULL;
229 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
233 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
235 GObjectClass *object_class = G_OBJECT_CLASS (klass);
236 GParamSpec *param_spec;
238 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
240 object_class->get_property = do_get_property;
241 object_class->set_property = do_set_property;
242 object_class->dispose = do_dispose;
243 object_class->finalize = do_finalize;
246 param_spec = g_param_spec_object ("contact",
247 "contact", "The remote contact",
248 EMPATHY_TYPE_CONTACT,
249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
250 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
252 param_spec = g_param_spec_object ("gfile",
253 "gfile", "The GFile we're handling",
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
256 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
258 param_spec = g_param_spec_object ("tp-file",
259 "tp-file", "The file's channel wrapper",
260 EMPATHY_TYPE_TP_FILE,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
262 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
264 param_spec = g_param_spec_boolean ("use-hash",
265 "use-hash", "Whether we should use checksum when sending or receiving",
266 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
267 g_object_class_install_property (object_class, PROP_USE_HASH, param_spec);
270 signals[TRANSFER_STARTED] =
271 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
272 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
273 g_cclosure_marshal_VOID__OBJECT,
275 1, EMPATHY_TYPE_TP_FILE);
277 signals[TRANSFER_DONE] =
278 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
279 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
280 g_cclosure_marshal_VOID__OBJECT,
282 1, EMPATHY_TYPE_TP_FILE);
284 signals[TRANSFER_ERROR] =
285 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
286 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
287 g_cclosure_marshal_VOID__POINTER,
291 signals[TRANSFER_PROGRESS] =
292 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
293 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
294 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
296 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
298 signals[HASHING_STARTED] =
299 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
300 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
301 g_cclosure_marshal_VOID__VOID,
304 signals[HASHING_PROGRESS] =
305 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
306 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
307 _empathy_marshal_VOID__UINT64_UINT64,
309 2, G_TYPE_UINT64, G_TYPE_UINT64);
311 signals[HASHING_DONE] =
312 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
313 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
314 g_cclosure_marshal_VOID__VOID,
319 empathy_ft_handler_init (EmpathyFTHandler *self)
321 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
322 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
325 priv->cancellable = g_cancellable_new ();
328 /* private functions */
331 hash_data_free (HashingData *data)
333 if (data->buffer != NULL)
335 g_free (data->buffer);
339 if (data->stream != NULL)
341 g_object_unref (data->stream);
345 if (data->checksum != NULL)
347 g_checksum_free (data->checksum);
348 data->checksum = NULL;
351 if (data->error != NULL)
353 g_error_free (data->error);
356 if (data->handler != NULL)
358 g_object_unref (data->handler);
359 data->handler = NULL;
362 g_slice_free (HashingData, data);
366 tp_file_hash_to_g_checksum (TpFileHashType type)
368 GChecksumType retval;
372 case TP_FILE_HASH_TYPE_MD5:
373 retval = G_CHECKSUM_MD5;
375 case TP_FILE_HASH_TYPE_SHA1:
376 retval = G_CHECKSUM_SHA1;
378 case TP_FILE_HASH_TYPE_SHA256:
379 retval = G_CHECKSUM_SHA256;
382 g_assert_not_reached ();
390 check_hash_incoming (EmpathyFTHandler *handler)
392 HashingData *hash_data;
393 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
395 if (!EMP_STR_EMPTY (priv->content_hash))
397 hash_data = g_slice_new0 (HashingData);
398 hash_data->total_bytes = priv->total_bytes;
399 hash_data->handler = g_object_ref (handler);
400 hash_data->checksum = g_checksum_new
401 (tp_file_hash_to_g_checksum (priv->content_hash_type));
403 g_signal_emit (handler, signals[HASHING_STARTED], 0);
405 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
406 G_PRIORITY_DEFAULT, priv->cancellable);
411 emit_error_signal (EmpathyFTHandler *handler,
414 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
416 if (!g_cancellable_is_cancelled (priv->cancellable))
417 g_cancellable_cancel (priv->cancellable);
419 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
423 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
427 EmpathyFTHandler *handler = user_data;
428 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
430 DEBUG ("Transfer operation callback, error %p", error);
434 emit_error_signal (handler, error);
438 priv->is_completed = TRUE;
439 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
441 empathy_tp_file_close (tp_file);
443 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
445 check_hash_incoming (handler);
451 update_remaining_time_and_speed (EmpathyFTHandler *handler,
452 guint64 transferred_bytes)
454 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
455 time_t elapsed_time, current_time;
456 guint64 transferred, last_transferred_bytes;
460 last_transferred_bytes = priv->transferred_bytes;
461 priv->transferred_bytes = transferred_bytes;
463 current_time = empathy_time_get_current ();
464 elapsed_time = current_time - priv->last_update_time;
466 if (elapsed_time >= 1)
468 transferred = transferred_bytes - last_transferred_bytes;
469 speed = (gdouble) transferred / (gdouble) elapsed_time;
470 remaining_time = (priv->total_bytes - transferred) / speed;
472 priv->remaining_time = remaining_time;
473 priv->last_update_time = current_time;
478 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
479 guint64 transferred_bytes,
482 EmpathyFTHandler *handler = user_data;
483 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
485 if (empathy_ft_handler_is_cancelled (handler))
488 if (transferred_bytes == 0)
490 priv->last_update_time = empathy_time_get_current ();
491 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
494 if (priv->transferred_bytes != transferred_bytes)
496 update_remaining_time_and_speed (handler, transferred_bytes);
498 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
499 transferred_bytes, priv->total_bytes, priv->remaining_time,
505 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
509 EmpathyFTHandler *handler = user_data;
510 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
511 GError *my_error = (GError *) error;
513 DEBUG ("Dispatcher create channel CB");
515 if (my_error == NULL)
517 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
520 if (my_error != NULL)
522 emit_error_signal (handler, my_error);
524 if (my_error != error)
525 g_clear_error (&my_error);
530 priv->tpfile = g_object_ref
531 (empathy_dispatch_operation_get_channel_wrapper (operation));
533 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
534 ft_transfer_progress_callback, handler,
535 ft_transfer_operation_callback, handler);
537 empathy_dispatch_operation_claim (operation);
541 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
543 EmpathyDispatcher *dispatcher;
544 TpConnection *connection;
545 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
547 DEBUG ("Pushing request to the dispatcher");
549 dispatcher = empathy_dispatcher_dup_singleton ();
550 connection = empathy_contact_get_connection (priv->contact);
552 /* I want to own a reference to the request, and destroy it later */
553 empathy_dispatcher_create_channel (dispatcher, connection,
554 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
556 g_object_unref (dispatcher);
560 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
562 EmpathyDispatcher *dispatcher;
563 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
564 TpConnection *connection;
568 dispatcher = empathy_dispatcher_dup_singleton ();
569 connection = empathy_contact_get_connection (priv->contact);
571 allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
572 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
574 if (!tp_strv_contains ((const gchar * const *) allowed,
575 TP_IFACE_CHANNEL ".TargetHandle"))
578 g_object_unref (dispatcher);
584 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
586 guint contact_handle;
589 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
591 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
592 NULL, (GDestroyNotify) tp_g_value_slice_free);
594 contact_handle = empathy_contact_get_handle (priv->contact);
596 /* org.freedesktop.Telepathy.Channel.ChannelType */
597 value = tp_g_value_slice_new (G_TYPE_STRING);
598 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
599 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
601 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
602 value = tp_g_value_slice_new (G_TYPE_UINT);
603 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
604 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
606 /* org.freedesktop.Telepathy.Channel.TargetHandle */
607 value = tp_g_value_slice_new (G_TYPE_UINT);
608 g_value_set_uint (value, contact_handle);
609 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
611 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
612 value = tp_g_value_slice_new (G_TYPE_STRING);
613 g_value_set_string (value, priv->content_type);
614 g_hash_table_insert (request,
615 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
617 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
618 value = tp_g_value_slice_new (G_TYPE_STRING);
619 g_value_set_string (value, priv->filename);
620 g_hash_table_insert (request,
621 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
623 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
624 value = tp_g_value_slice_new (G_TYPE_UINT64);
625 g_value_set_uint64 (value, (guint64) priv->total_bytes);
626 g_hash_table_insert (request,
627 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
629 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
630 value = tp_g_value_slice_new (G_TYPE_UINT64);
631 g_value_set_uint64 (value, (guint64) priv->mtime);
632 g_hash_table_insert (request,
633 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
637 hash_job_done (gpointer user_data)
639 HashingData *hash_data = user_data;
640 EmpathyFTHandler *handler = hash_data->handler;
641 EmpathyFTHandlerPriv *priv;
642 GError *error = NULL;
645 DEBUG ("Closing stream after hashing.");
647 priv = GET_PRIV (handler);
649 if (hash_data->error != NULL)
651 error = hash_data->error;
655 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
657 if (empathy_ft_handler_is_incoming (handler))
659 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
662 DEBUG ("Hash mismatch when checking incoming handler: "
663 "received %s, calculated %s", priv->content_hash,
664 g_checksum_get_string (hash_data->checksum));
666 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
667 EMPATHY_FT_ERROR_HASH_MISMATCH,
668 _("The hash of the received file and the "
669 "sent one do not match"));
674 DEBUG ("Hash verification matched, received %s, calculated %s",
676 g_checksum_get_string (hash_data->checksum));
681 /* set the checksum in the request...
682 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
684 value = tp_g_value_slice_new (G_TYPE_STRING);
685 g_value_set_string (value, g_checksum_get_string (hash_data->checksum));
686 g_hash_table_insert (priv->request,
687 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
694 emit_error_signal (handler, error);
695 g_clear_error (&error);
699 g_signal_emit (handler, signals[HASHING_DONE], 0);
701 if (!empathy_ft_handler_is_incoming (handler))
702 /* the request is complete now, push it to the dispatcher */
703 ft_handler_push_to_dispatcher (handler);
706 hash_data_free (hash_data);
712 emit_hashing_progress (gpointer user_data)
714 HashingData *hash_data = user_data;
716 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
717 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
723 do_hash_job (GIOSchedulerJob *job,
724 GCancellable *cancellable,
727 HashingData *hash_data = user_data;
729 EmpathyFTHandlerPriv *priv;
730 GError *error = NULL;
732 priv = GET_PRIV (hash_data->handler);
735 if (hash_data->buffer == NULL)
736 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
738 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
739 BUFFER_SIZE, cancellable, &error);
743 hash_data->total_read += bytes_read;
745 /* we now have the chunk */
748 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
749 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
752 g_free (hash_data->buffer);
753 hash_data->buffer = NULL;
759 g_input_stream_close (hash_data->stream, cancellable, &error);
764 hash_data->error = error;
766 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
773 do_hash_job_incoming (GIOSchedulerJob *job,
774 GCancellable *cancellable,
777 HashingData *hash_data = user_data;
778 EmpathyFTHandler *handler = hash_data->handler;
779 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
780 GError *error = NULL;
782 DEBUG ("checking integrity for incoming handler");
784 /* need to get the stream first */
786 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
790 hash_data->error = error;
791 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
796 return do_hash_job (job, cancellable, user_data);
800 ft_handler_read_async_cb (GObject *source,
804 GFileInputStream *stream;
805 GError *error = NULL;
806 HashingData *hash_data;
808 EmpathyFTHandler *handler = user_data;
809 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
811 DEBUG ("GFile read async CB.");
813 stream = g_file_read_finish (priv->gfile, res, &error);
816 emit_error_signal (handler, error);
817 g_clear_error (&error);
822 hash_data = g_slice_new0 (HashingData);
823 hash_data->stream = G_INPUT_STREAM (stream);
824 hash_data->total_bytes = priv->total_bytes;
825 hash_data->handler = g_object_ref (handler);
826 /* FIXME: should look at the CM capabilities before setting the
829 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
831 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
832 value = tp_g_value_slice_new (G_TYPE_UINT);
833 g_value_set_uint (value, TP_FILE_HASH_TYPE_MD5);
834 g_hash_table_insert (priv->request,
835 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
837 g_signal_emit (handler, signals[HASHING_STARTED], 0);
839 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
840 G_PRIORITY_DEFAULT, priv->cancellable);
844 ft_handler_complete_request (EmpathyFTHandler *handler)
846 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
847 GError *myerr = NULL;
849 /* check if FT is allowed before firing up the I/O machinery */
850 if (!ft_handler_check_if_allowed (handler))
852 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
853 EMPATHY_FT_ERROR_NOT_SUPPORTED,
854 _("File transfer not supported by remote contact"));
856 emit_error_signal (handler, myerr);
857 g_clear_error (&myerr);
862 /* populate the request table with all the known properties */
863 ft_handler_populate_outgoing_request (handler);
866 /* start hashing the file */
867 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
868 priv->cancellable, ft_handler_read_async_cb, handler);
870 /* push directly the handler to the dispatcher */
871 ft_handler_push_to_dispatcher (handler);
875 callbacks_data_free (gpointer user_data)
877 CallbacksData *data = user_data;
880 g_object_unref (data->handler);
882 g_slice_free (CallbacksData, data);
886 ft_handler_gfile_ready_cb (GObject *source,
888 CallbacksData *cb_data)
891 GError *error = NULL;
893 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
895 DEBUG ("Got GFileInfo.");
897 info = g_file_query_info_finish (priv->gfile, res, &error);
902 priv->content_type = g_strdup (g_file_info_get_content_type (info));
903 priv->filename = g_strdup (g_file_info_get_display_name (info));
904 priv->total_bytes = g_file_info_get_size (info);
905 g_file_info_get_modification_time (info, &mtime);
906 priv->mtime = mtime.tv_sec;
907 priv->transferred_bytes = 0;
908 priv->description = NULL;
910 g_object_unref (info);
915 cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
919 if (!g_cancellable_is_cancelled (priv->cancellable))
920 g_cancellable_cancel (priv->cancellable);
922 cb_data->callback (NULL, error, cb_data->user_data);
923 g_error_free (error);
924 g_object_unref (cb_data->handler);
927 callbacks_data_free (cb_data);
931 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
932 EmpathyContact *contact,
935 GObject *weak_object)
937 CallbacksData *cb_data = user_data;
938 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
939 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
943 if (!g_cancellable_is_cancelled (priv->cancellable))
944 g_cancellable_cancel (priv->cancellable);
946 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
947 g_object_unref (handler);
951 priv->contact = contact;
953 cb_data->callback (handler, NULL, cb_data->user_data);
957 channel_get_all_properties_cb (TpProxy *proxy,
958 GHashTable *properties,
961 GObject *weak_object)
963 CallbacksData *cb_data = user_data;
964 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
965 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
966 EmpathyTpContactFactory *c_factory;
971 if (!g_cancellable_is_cancelled (priv->cancellable))
972 g_cancellable_cancel (priv->cancellable);
974 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
975 g_object_unref (handler);
979 priv->total_bytes = g_value_get_uint64 (
980 g_hash_table_lookup (properties, "Size"));
982 priv->transferred_bytes = g_value_get_uint64 (
983 g_hash_table_lookup (properties, "TransferredBytes"));
985 priv->filename = g_value_dup_string (
986 g_hash_table_lookup (properties, "Filename"));
988 priv->content_hash = g_value_dup_string (
989 g_hash_table_lookup (properties, "ContentHash"));
991 priv->content_hash_type = g_value_get_uint (
992 g_hash_table_lookup (properties, "ContentHashType"));
994 priv->content_type = g_value_dup_string (
995 g_hash_table_lookup (properties, "ContentType"));
997 priv->description = g_value_dup_string (
998 g_hash_table_lookup (properties, "Description"));
1000 c_factory = empathy_tp_contact_factory_dup_singleton
1001 (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
1002 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1003 empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
1004 contact_factory_contact_cb, cb_data, callbacks_data_free,
1005 G_OBJECT (handler));
1008 /* public methods */
1011 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1014 EmpathyFTHandlerReadyCallback callback,
1017 EmpathyFTHandler *handler;
1018 CallbacksData *data;
1019 EmpathyFTHandlerPriv *priv;
1021 DEBUG ("New handler outgoing, use hash %s",
1022 use_hash ? "True" : "False");
1024 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1025 g_return_if_fail (G_IS_FILE (source));
1027 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1028 "contact", contact, "gfile", source, "use-hash", use_hash, NULL);
1030 priv = GET_PRIV (handler);
1032 data = g_slice_new0 (CallbacksData);
1033 data->callback = callback;
1034 data->user_data = user_data;
1035 data->handler = g_object_ref (handler);
1037 /* start collecting info about the file */
1038 g_file_query_info_async (priv->gfile,
1039 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1040 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1041 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1042 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1043 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1044 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1048 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1049 EmpathyFTHandlerReadyCallback callback,
1052 EmpathyFTHandler *handler;
1054 CallbacksData *data;
1056 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1058 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1059 "tp-file", tp_file, NULL);
1061 g_object_get (tp_file, "channel", &channel, NULL);
1063 data = g_slice_new0 (CallbacksData);
1064 data->callback = callback;
1065 data->user_data = user_data;
1066 data->handler = g_object_ref (handler);
1068 tp_cli_dbus_properties_call_get_all (channel,
1069 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1070 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1074 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1076 EmpathyFTHandlerPriv *priv;
1078 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1080 priv = GET_PRIV (handler);
1082 if (priv->tpfile == NULL)
1084 ft_handler_complete_request (handler);
1088 /* TODO: add support for resume. */
1089 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1090 ft_transfer_progress_callback, handler,
1091 ft_transfer_operation_callback, handler);
1096 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1098 EmpathyFTHandlerPriv *priv;
1100 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1102 priv = GET_PRIV (handler);
1104 /* if we don't have an EmpathyTpFile, we are hashing, so
1105 * we can just cancel the GCancellable to stop it.
1107 if (priv->tpfile == NULL)
1108 g_cancellable_cancel (priv->cancellable);
1110 empathy_tp_file_cancel (priv->tpfile);
1114 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1118 DEBUG ("Set incoming destination, use hash %s",
1119 use_hash ? "True" : "False");
1121 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1122 g_return_if_fail (G_IS_FILE (destination));
1124 g_object_set (handler, "gfile", destination,
1125 "use-hash", use_hash, NULL);
1129 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1131 EmpathyFTHandlerPriv *priv;
1133 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1135 priv = GET_PRIV (handler);
1137 return priv->filename;
1141 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1143 EmpathyFTHandlerPriv *priv;
1145 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1147 priv = GET_PRIV (handler);
1149 return priv->content_type;
1153 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1155 EmpathyFTHandlerPriv *priv;
1157 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1159 priv = GET_PRIV (handler);
1161 return priv->contact;
1165 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1167 EmpathyFTHandlerPriv *priv;
1169 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1171 priv = GET_PRIV (handler);
1177 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1179 EmpathyFTHandlerPriv *priv;
1181 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1183 priv = GET_PRIV (handler);
1185 return priv->use_hash;
1189 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1191 EmpathyFTHandlerPriv *priv;
1193 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1195 priv = GET_PRIV (handler);
1197 if (priv->tpfile == NULL)
1200 return empathy_tp_file_is_incoming (priv->tpfile);
1204 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1206 EmpathyFTHandlerPriv *priv;
1208 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1210 priv = GET_PRIV (handler);
1212 return priv->transferred_bytes;
1216 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1218 EmpathyFTHandlerPriv *priv;
1220 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1222 priv = GET_PRIV (handler);
1224 return priv->total_bytes;
1228 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1230 EmpathyFTHandlerPriv *priv;
1232 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1234 priv = GET_PRIV (handler);
1236 return priv->is_completed;
1240 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1242 EmpathyFTHandlerPriv *priv;
1244 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1246 priv = GET_PRIV (handler);
1248 return g_cancellable_is_cancelled (priv->cancellable);