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
63 gboolean done_reading;
69 EmpathyFTHandler *handler;
73 EmpathyFTHandlerReadyCallback callback;
75 EmpathyFTHandler *handler;
82 EmpathyTpFile *tpfile;
83 GCancellable *cancellable;
85 /* request for the new transfer */
88 /* transfer properties */
89 EmpathyContact *contact;
94 guint64 transferred_bytes;
97 TpFileHashType content_hash_type;
98 TpFileTransferState current_state;
102 guint remaining_time;
103 time_t last_update_time;
105 gboolean is_completed;
106 } EmpathyFTHandlerPriv;
108 static guint signals[LAST_SIGNAL] = { 0 };
110 /* GObject implementations */
112 do_get_property (GObject *object,
117 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
122 g_value_set_object (value, priv->contact);
125 g_value_set_object (value, priv->gfile);
128 g_value_set_object (value, priv->tpfile);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
136 do_set_property (GObject *object,
141 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
146 priv->contact = g_value_dup_object (value);
149 priv->gfile = g_value_dup_object (value);
152 priv->tpfile = g_value_dup_object (value);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
160 do_dispose (GObject *object)
162 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
164 if (priv->dispose_run)
167 priv->dispose_run = TRUE;
170 g_object_unref (priv->contact);
171 priv->contact = NULL;
175 g_object_unref (priv->gfile);
180 empathy_tp_file_close (priv->tpfile);
181 g_object_unref (priv->tpfile);
185 if (priv->cancellable) {
186 g_object_unref (priv->cancellable);
187 priv->cancellable = NULL;
190 if (priv->request != NULL)
192 g_hash_table_unref (priv->request);
193 priv->request = NULL;
196 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
200 do_finalize (GObject *object)
202 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
204 DEBUG ("%p", object);
206 g_free (priv->content_type);
207 priv->content_type = NULL;
209 g_free (priv->filename);
210 priv->filename = NULL;
212 g_free (priv->description);
213 priv->description = NULL;
215 g_free (priv->content_hash);
216 priv->content_hash = NULL;
218 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
222 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
224 GObjectClass *object_class = G_OBJECT_CLASS (klass);
225 GParamSpec *param_spec;
227 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
229 object_class->get_property = do_get_property;
230 object_class->set_property = do_set_property;
231 object_class->dispose = do_dispose;
232 object_class->finalize = do_finalize;
235 param_spec = g_param_spec_object ("contact",
236 "contact", "The remote contact",
237 EMPATHY_TYPE_CONTACT,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
239 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
241 param_spec = g_param_spec_object ("gfile",
242 "gfile", "The GFile we're handling",
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
245 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
247 param_spec = g_param_spec_object ("tp-file",
248 "tp-file", "The file's channel wrapper",
249 EMPATHY_TYPE_TP_FILE,
250 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
251 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
254 signals[TRANSFER_STARTED] =
255 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
256 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
257 g_cclosure_marshal_VOID__OBJECT,
259 1, EMPATHY_TYPE_TP_FILE);
261 signals[TRANSFER_DONE] =
262 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
263 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
264 g_cclosure_marshal_VOID__OBJECT,
266 1, EMPATHY_TYPE_TP_FILE);
268 signals[TRANSFER_ERROR] =
269 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
271 g_cclosure_marshal_VOID__POINTER,
275 signals[TRANSFER_PROGRESS] =
276 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
277 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
278 _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
280 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
282 signals[HASHING_STARTED] =
283 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
284 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
285 g_cclosure_marshal_VOID__VOID,
288 signals[HASHING_PROGRESS] =
289 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
291 _empathy_marshal_VOID__UINT64_UINT64,
293 2, G_TYPE_UINT64, G_TYPE_UINT64);
295 signals[HASHING_DONE] =
296 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
297 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
298 g_cclosure_marshal_VOID__VOID,
303 empathy_ft_handler_init (EmpathyFTHandler *self)
305 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
306 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
309 priv->cancellable = g_cancellable_new ();
312 /* private functions */
315 hash_data_free (HashingData *data)
317 if (data->buffer != NULL)
319 g_free (data->buffer);
323 if (data->stream != NULL)
325 g_object_unref (data->stream);
329 if (data->checksum != NULL)
331 g_checksum_free (data->checksum);
332 data->checksum = NULL;
335 if (data->error != NULL)
337 g_error_free (data->error);
340 if (data->handler != NULL)
342 g_object_unref (data->handler);
343 data->handler = NULL;
346 g_slice_free (HashingData, data);
350 emit_error_signal (EmpathyFTHandler *handler,
353 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
355 if (!g_cancellable_is_cancelled (priv->cancellable))
356 g_cancellable_cancel (priv->cancellable);
358 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
362 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
366 EmpathyFTHandler *handler = user_data;
367 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
369 DEBUG ("Transfer operation callback, error %p", error);
373 emit_error_signal (handler, error);
377 priv->is_completed = TRUE;
378 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
380 empathy_tp_file_close (tp_file);
385 update_remaining_time_and_speed (EmpathyFTHandler *handler,
386 guint64 transferred_bytes)
388 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
389 time_t elapsed_time, current_time;
390 guint64 transferred, last_transferred_bytes;
394 last_transferred_bytes = priv->transferred_bytes;
395 priv->transferred_bytes = transferred_bytes;
397 current_time = empathy_time_get_current ();
398 elapsed_time = current_time - priv->last_update_time;
400 if (elapsed_time >= 1)
402 transferred = transferred_bytes - last_transferred_bytes;
403 speed = (gdouble) transferred / (gdouble) elapsed_time;
404 remaining_time = (priv->total_bytes - transferred) / speed;
406 priv->remaining_time = remaining_time;
407 priv->last_update_time = current_time;
412 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
413 guint64 transferred_bytes,
416 EmpathyFTHandler *handler = user_data;
417 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
419 if (empathy_ft_handler_is_cancelled (handler))
422 if (transferred_bytes == 0)
424 priv->last_update_time = empathy_time_get_current ();
425 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
428 if (priv->transferred_bytes != transferred_bytes)
430 update_remaining_time_and_speed (handler, transferred_bytes);
432 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
433 transferred_bytes, priv->total_bytes, priv->remaining_time,
439 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
443 EmpathyFTHandler *handler = user_data;
444 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
445 GError *my_error = (GError *) error;
447 DEBUG ("Dispatcher create channel CB");
449 if (my_error == NULL)
451 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
454 if (my_error != NULL)
456 emit_error_signal (handler, my_error);
458 if (my_error != error)
459 g_clear_error (&my_error);
464 priv->tpfile = g_object_ref
465 (empathy_dispatch_operation_get_channel_wrapper (operation));
467 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
468 ft_transfer_progress_callback, handler,
469 ft_transfer_operation_callback, handler);
471 empathy_dispatch_operation_claim (operation);
475 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
477 EmpathyDispatcher *dispatcher;
478 TpConnection *connection;
479 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
481 DEBUG ("Pushing request to the dispatcher");
483 dispatcher = empathy_dispatcher_dup_singleton ();
484 connection = empathy_contact_get_connection (priv->contact);
486 /* I want to own a reference to the request, and destroy it later */
487 empathy_dispatcher_create_channel (dispatcher, connection,
488 g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
490 g_object_unref (dispatcher);
494 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
496 EmpathyDispatcher *dispatcher;
497 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
498 TpConnection *connection;
502 dispatcher = empathy_dispatcher_dup_singleton ();
503 connection = empathy_contact_get_connection (priv->contact);
505 allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
506 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
508 if (!tp_strv_contains ((const gchar * const *) allowed,
509 TP_IFACE_CHANNEL ".TargetHandle"))
512 g_object_unref (dispatcher);
518 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
520 guint contact_handle;
523 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
525 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
526 NULL, (GDestroyNotify) tp_g_value_slice_free);
528 contact_handle = empathy_contact_get_handle (priv->contact);
530 /* org.freedesktop.Telepathy.Channel.ChannelType */
531 value = tp_g_value_slice_new (G_TYPE_STRING);
532 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
533 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
535 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
536 value = tp_g_value_slice_new (G_TYPE_UINT);
537 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
538 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
540 /* org.freedesktop.Telepathy.Channel.TargetHandle */
541 value = tp_g_value_slice_new (G_TYPE_UINT);
542 g_value_set_uint (value, contact_handle);
543 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
545 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
546 value = tp_g_value_slice_new (G_TYPE_STRING);
547 g_value_set_string (value, priv->content_type);
548 g_hash_table_insert (request,
549 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
551 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
552 value = tp_g_value_slice_new (G_TYPE_STRING);
553 g_value_set_string (value, priv->filename);
554 g_hash_table_insert (request,
555 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
557 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
558 value = tp_g_value_slice_new (G_TYPE_UINT64);
559 g_value_set_uint64 (value, (guint64) priv->total_bytes);
560 g_hash_table_insert (request,
561 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
563 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
564 value = tp_g_value_slice_new (G_TYPE_UINT64);
565 g_value_set_uint64 (value, (guint64) priv->mtime);
566 g_hash_table_insert (request,
567 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
571 hash_job_done (gpointer user_data)
573 HashingData *hash_data = user_data;
574 EmpathyFTHandler *handler = hash_data->handler;
575 EmpathyFTHandlerPriv *priv;
576 GError *error = NULL;
579 DEBUG ("Closing stream after hashing.");
581 priv = GET_PRIV (handler);
583 if (hash_data->error != NULL)
585 error = hash_data->error;
589 /* set the checksum in the request */
591 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
593 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash */
594 value = tp_g_value_slice_new (G_TYPE_STRING);
595 g_value_set_string (value, g_checksum_get_string (hash_data->checksum));
596 g_hash_table_insert (priv->request,
597 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
603 emit_error_signal (handler, error);
607 g_signal_emit (handler, signals[HASHING_DONE], 0);
609 /* the request is complete now, push it to the dispatcher */
610 ft_handler_push_to_dispatcher (handler);
613 hash_data_free (hash_data);
619 emit_hashing_progress (gpointer user_data)
621 HashingData *hash_data = user_data;
623 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
624 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
630 do_hash_job (GIOSchedulerJob *job,
631 GCancellable *cancellable,
634 HashingData *hash_data = user_data;
636 EmpathyFTHandlerPriv *priv;
637 GError *error = NULL;
639 priv = GET_PRIV (hash_data->handler);
642 if (hash_data->buffer == NULL)
643 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
645 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
646 BUFFER_SIZE, cancellable, &error);
650 hash_data->total_read += bytes_read;
652 /* we now have the chunk */
655 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
656 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
659 g_free (hash_data->buffer);
660 hash_data->buffer = NULL;
666 g_input_stream_close (hash_data->stream, cancellable, &error);
671 hash_data->error = error;
673 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
680 ft_handler_read_async_cb (GObject *source,
684 GFileInputStream *stream;
685 GError *error = NULL;
686 HashingData *hash_data;
688 EmpathyFTHandler *handler = user_data;
689 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
691 DEBUG ("GFile read async CB.");
693 stream = g_file_read_finish (priv->gfile, res, &error);
696 emit_error_signal (handler, error);
697 g_clear_error (&error);
702 hash_data = g_slice_new0 (HashingData);
703 hash_data->stream = G_INPUT_STREAM (stream);
704 hash_data->done_reading = FALSE;
705 hash_data->total_bytes = priv->total_bytes;
706 hash_data->handler = g_object_ref (handler);
707 /* FIXME: should look at the CM capabilities before setting the
710 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
712 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
713 value = tp_g_value_slice_new (G_TYPE_UINT);
714 g_value_set_uint (value, TP_FILE_HASH_TYPE_MD5);
715 g_hash_table_insert (priv->request,
716 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
718 g_signal_emit (handler, signals[HASHING_STARTED], 0);
720 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
721 G_PRIORITY_DEFAULT, priv->cancellable);
725 ft_handler_complete_request (EmpathyFTHandler *handler)
727 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
728 GError *myerr = NULL;
730 /* check if FT is allowed before firing up the I/O machinery */
731 if (!ft_handler_check_if_allowed (handler))
733 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
734 EMPATHY_FT_ERROR_NOT_SUPPORTED,
735 _("File transfer not supported by remote contact"));
737 emit_error_signal (handler, myerr);
742 /* populate the request table with all the known properties */
743 ft_handler_populate_outgoing_request (handler);
745 /* now start hashing the file */
746 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
747 priv->cancellable, ft_handler_read_async_cb, handler);
751 callbacks_data_free (gpointer user_data)
753 CallbacksData *data = user_data;
756 g_object_unref (data->handler);
758 g_slice_free (CallbacksData, data);
762 ft_handler_gfile_ready_cb (GObject *source,
764 CallbacksData *cb_data)
767 GError *error = NULL;
769 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
771 DEBUG ("Got GFileInfo.");
773 info = g_file_query_info_finish (priv->gfile, res, &error);
778 priv->content_type = g_strdup (g_file_info_get_content_type (info));
779 priv->filename = g_strdup (g_file_info_get_display_name (info));
780 priv->total_bytes = g_file_info_get_size (info);
781 g_file_info_get_modification_time (info, &mtime);
782 priv->mtime = mtime.tv_sec;
783 priv->transferred_bytes = 0;
784 priv->description = NULL;
786 g_object_unref (info);
791 cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
795 cb_data->callback (NULL, error, cb_data->user_data);
796 g_error_free (error);
797 g_object_unref (cb_data->handler);
800 callbacks_data_free (cb_data);
804 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
805 EmpathyContact *contact,
808 GObject *weak_object)
810 CallbacksData *cb_data = user_data;
811 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
812 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
816 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
817 g_object_unref (handler);
821 priv->contact = contact;
823 cb_data->callback (handler, NULL, cb_data->user_data);
827 channel_get_all_properties_cb (TpProxy *proxy,
828 GHashTable *properties,
831 GObject *weak_object)
833 CallbacksData *cb_data = user_data;
834 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
835 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
836 EmpathyTpContactFactory *c_factory;
841 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
842 g_object_unref (handler);
846 priv->total_bytes = g_value_get_uint64 (
847 g_hash_table_lookup (properties, "Size"));
849 priv->transferred_bytes = g_value_get_uint64 (
850 g_hash_table_lookup (properties, "TransferredBytes"));
852 priv->filename = g_value_dup_string (
853 g_hash_table_lookup (properties, "Filename"));
855 priv->content_hash = g_value_dup_string (
856 g_hash_table_lookup (properties, "ContentHash"));
858 priv->content_hash_type = g_value_get_uint (
859 g_hash_table_lookup (properties, "ContentHashType"));
861 priv->content_type = g_value_dup_string (
862 g_hash_table_lookup (properties, "ContentType"));
864 priv->description = g_value_dup_string (
865 g_hash_table_lookup (properties, "Description"));
867 g_hash_table_destroy (properties);
869 c_factory = empathy_tp_contact_factory_dup_singleton
870 (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
871 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
872 empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
873 contact_factory_contact_cb, cb_data, callbacks_data_free,
880 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
882 EmpathyFTHandlerReadyCallback callback,
885 EmpathyFTHandler *handler;
887 EmpathyFTHandlerPriv *priv;
889 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
890 g_return_if_fail (G_IS_FILE (source));
892 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
893 "contact", contact, "gfile", source, NULL);
895 priv = GET_PRIV (handler);
897 data = g_slice_new0 (CallbacksData);
898 data->callback = callback;
899 data->user_data = user_data;
900 data->handler = g_object_ref (handler);
902 /* start collecting info about the file */
903 g_file_query_info_async (priv->gfile,
904 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
905 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
906 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
907 G_FILE_ATTRIBUTE_TIME_MODIFIED,
908 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
909 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
913 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
914 EmpathyFTHandlerReadyCallback callback,
917 EmpathyFTHandler *handler;
921 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
923 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
924 "tp-file", tp_file, NULL);
926 g_object_get (tp_file, "channel", &channel, NULL);
928 data = g_slice_new0 (CallbacksData);
929 data->callback = callback;
930 data->user_data = user_data;
931 data->handler = g_object_ref (handler);
933 tp_cli_dbus_properties_call_get_all (channel,
934 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
935 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
939 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
941 EmpathyFTHandlerPriv *priv;
943 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
945 priv = GET_PRIV (handler);
947 if (priv->tpfile == NULL)
949 ft_handler_complete_request (handler);
953 /* TODO: add support for resume. */
954 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
955 ft_transfer_progress_callback, handler,
956 ft_transfer_operation_callback, handler);
961 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
963 EmpathyFTHandlerPriv *priv;
965 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
967 priv = GET_PRIV (handler);
969 /* if we don't have an EmpathyTpFile, we are hashing, so
970 * we can just cancel the GCancellable to stop it.
972 if (priv->tpfile == NULL)
973 g_cancellable_cancel (priv->cancellable);
975 empathy_tp_file_cancel (priv->tpfile);
979 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
982 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
983 g_return_if_fail (G_IS_FILE (destination));
985 g_object_set (handler, "gfile", destination, NULL);
989 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
991 EmpathyFTHandlerPriv *priv;
993 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
995 priv = GET_PRIV (handler);
997 return priv->filename;
1001 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1003 EmpathyFTHandlerPriv *priv;
1005 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1007 priv = GET_PRIV (handler);
1009 return priv->content_type;
1013 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1015 EmpathyFTHandlerPriv *priv;
1017 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1019 priv = GET_PRIV (handler);
1021 return priv->contact;
1025 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1027 EmpathyFTHandlerPriv *priv;
1029 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1031 priv = GET_PRIV (handler);
1037 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1039 EmpathyFTHandlerPriv *priv;
1041 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1043 priv = GET_PRIV (handler);
1045 if (priv->tpfile == NULL)
1048 return empathy_tp_file_is_incoming (priv->tpfile);
1052 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1054 EmpathyFTHandlerPriv *priv;
1056 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1058 priv = GET_PRIV (handler);
1060 return priv->transferred_bytes;
1064 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1066 EmpathyFTHandlerPriv *priv;
1068 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1070 priv = GET_PRIV (handler);
1072 return priv->total_bytes;
1076 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1078 EmpathyFTHandlerPriv *priv;
1080 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1082 priv = GET_PRIV (handler);
1084 return priv->is_completed;
1088 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1090 EmpathyFTHandlerPriv *priv;
1092 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1094 priv = GET_PRIV (handler);
1096 return g_cancellable_is_cancelled (priv->cancellable);