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 */
24 #include <extensions/extensions.h>
26 #include <glib/gi18n.h>
27 #include <telepathy-glib/util.h>
29 #include "empathy-ft-handler.h"
30 #include "empathy-contact-factory.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.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 EmpFileHashType content_hash_type;
98 EmpFileTransferState current_state;
100 gboolean is_completed;
101 gboolean is_cancelled;
102 } EmpathyFTHandlerPriv;
104 static guint signals[LAST_SIGNAL] = { 0 };
107 static void schedule_hash_chunk (HashingData *hash_data);
109 /* GObject implementations */
111 do_get_property (GObject *object,
116 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
121 g_value_set_object (value, priv->contact);
124 g_value_set_object (value, priv->gfile);
127 g_value_set_object (value, priv->tpfile);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
135 do_set_property (GObject *object,
140 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
145 priv->contact = g_value_dup_object (value);
148 priv->gfile = g_value_dup_object (value);
151 priv->tpfile = g_value_dup_object (value);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 do_dispose (GObject *object)
161 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
163 if (priv->dispose_run)
166 priv->dispose_run = TRUE;
169 g_object_unref (priv->contact);
170 priv->contact = NULL;
174 g_object_unref (priv->gfile);
179 empathy_tp_file_close (priv->tpfile);
180 g_object_unref (priv->tpfile);
184 if (priv->cancellable) {
185 g_object_unref (priv->cancellable);
186 priv->cancellable = NULL;
189 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
193 do_finalize (GObject *object)
195 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
197 g_free (priv->content_type);
198 priv->content_type = NULL;
200 g_free (priv->filename);
201 priv->filename = NULL;
203 g_free (priv->description);
204 priv->description = NULL;
206 g_free (priv->content_hash);
207 priv->content_hash = NULL;
209 if (priv->request != NULL)
211 g_hash_table_destroy (priv->request);
212 priv->request = NULL;
215 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
219 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
222 GParamSpec *param_spec;
224 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
226 object_class->get_property = do_get_property;
227 object_class->set_property = do_set_property;
228 object_class->dispose = do_dispose;
229 object_class->finalize = do_finalize;
232 param_spec = g_param_spec_object ("contact",
233 "contact", "The remote contact",
234 EMPATHY_TYPE_CONTACT,
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
236 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
238 param_spec = g_param_spec_object ("gfile",
239 "gfile", "The GFile we're handling",
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
242 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
244 param_spec = g_param_spec_object ("tp-file",
245 "tp-file", "The file's channel wrapper",
246 EMPATHY_TYPE_TP_FILE,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
248 g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
251 signals[TRANSFER_STARTED] =
252 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
254 g_cclosure_marshal_VOID__OBJECT,
256 1, EMPATHY_TYPE_TP_FILE);
258 signals[TRANSFER_DONE] =
259 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
261 g_cclosure_marshal_VOID__OBJECT,
263 1, EMPATHY_TYPE_TP_FILE);
265 signals[TRANSFER_ERROR] =
266 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
268 g_cclosure_marshal_VOID__POINTER,
272 signals[TRANSFER_PROGRESS] =
273 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
274 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
275 _empathy_marshal_VOID__UINT64_UINT64,
277 2, G_TYPE_UINT64, G_TYPE_UINT64);
279 signals[HASHING_STARTED] =
280 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
281 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
282 g_cclosure_marshal_VOID__VOID,
285 signals[HASHING_PROGRESS] =
286 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
288 _empathy_marshal_VOID__UINT64_UINT64,
290 2, G_TYPE_UINT64, G_TYPE_UINT64);
292 signals[HASHING_DONE] =
293 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
294 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
295 g_cclosure_marshal_VOID__VOID,
300 empathy_ft_handler_init (EmpathyFTHandler *self)
302 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
303 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
308 /* private functions */
311 hash_data_free (HashingData *data)
313 if (data->buffer != NULL)
315 g_free (data->buffer);
319 if (data->stream != NULL)
321 g_object_unref (data->stream);
325 if (data->checksum != NULL)
327 g_checksum_free (data->checksum);
328 data->checksum = NULL;
331 if (data->error != NULL)
333 g_error_free (data->error);
336 if (data->handler != NULL)
338 g_object_unref (data->handler);
339 data->handler = NULL;
342 g_slice_free (HashingData, data);
346 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
350 EmpathyFTHandler *handler = user_data;
351 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
353 DEBUG ("Transfer operation callback, error %p", error);
357 priv->is_cancelled = TRUE;
358 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
362 priv->is_completed = TRUE;
363 g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
368 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
369 guint64 transferred_bytes,
372 EmpathyFTHandler *handler = user_data;
373 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
375 if (priv->transferred_bytes != transferred_bytes)
377 priv->transferred_bytes = transferred_bytes;
378 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
379 transferred_bytes, priv->total_bytes);
384 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
388 EmpathyFTHandler *handler = user_data;
389 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
390 GError *my_error = NULL;
392 DEBUG ("Dispatcher create channel CB");
396 priv->is_cancelled = TRUE;
397 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
401 g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
403 if (my_error != NULL)
405 priv->is_cancelled = TRUE;
406 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, my_error);
407 g_clear_error (&my_error);
412 priv->tpfile = g_object_ref
413 (empathy_dispatch_operation_get_channel_wrapper (operation));
415 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, priv->tpfile);
417 empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
418 ft_transfer_progress_callback, handler,
419 ft_transfer_operation_callback, handler);
421 empathy_dispatch_operation_claim (operation);
425 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
427 EmpathyDispatcher *dispatcher;
429 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
431 DEBUG ("Pushing request to the dispatcher");
433 dispatcher = empathy_dispatcher_dup_singleton ();
434 account = empathy_contact_get_account (priv->contact);
436 empathy_dispatcher_create_channel (dispatcher, account, priv->request,
437 ft_handler_create_channel_cb, handler);
439 g_object_unref (dispatcher);
443 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
445 EmpathyDispatcher *dispatcher;
446 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
451 dispatcher = empathy_dispatcher_dup_singleton ();
452 account = empathy_contact_get_account (priv->contact);
454 allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
455 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
457 if (!tp_strv_contains ((const gchar * const *) allowed,
458 TP_IFACE_CHANNEL ".TargetHandle"))
461 g_object_unref (dispatcher);
467 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
469 guint contact_handle;
472 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
474 request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
475 NULL, (GDestroyNotify) tp_g_value_slice_free);
477 contact_handle = empathy_contact_get_handle (priv->contact);
479 /* org.freedesktop.Telepathy.Channel.ChannelType */
480 value = tp_g_value_slice_new (G_TYPE_STRING);
481 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
482 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
484 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
485 value = tp_g_value_slice_new (G_TYPE_UINT);
486 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
487 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
489 /* org.freedesktop.Telepathy.Channel.TargetHandle */
490 value = tp_g_value_slice_new (G_TYPE_UINT);
491 g_value_set_uint (value, contact_handle);
492 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
494 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
495 value = tp_g_value_slice_new (G_TYPE_STRING);
496 g_value_set_string (value, priv->content_type);
497 g_hash_table_insert (request,
498 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
500 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
501 value = tp_g_value_slice_new (G_TYPE_STRING);
502 g_value_set_string (value, priv->filename);
503 g_hash_table_insert (request,
504 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
506 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
507 value = tp_g_value_slice_new (G_TYPE_UINT64);
508 g_value_set_uint64 (value, (guint64) priv->total_bytes);
509 g_hash_table_insert (request,
510 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
512 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
513 value = tp_g_value_slice_new (G_TYPE_UINT64);
514 g_value_set_uint64 (value, (guint64) priv->mtime);
515 g_hash_table_insert (request,
516 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
520 hash_job_async_close_stream_cb (GObject *source,
524 HashingData *hash_data = user_data;
525 EmpathyFTHandler *handler = hash_data->handler;
526 EmpathyFTHandlerPriv *priv;
527 GError *error = NULL;
530 DEBUG ("Closing stream after hashing.");
532 priv = GET_PRIV (handler);
534 /* if we're here we for sure have done reading, check if we stopped due
537 g_input_stream_close_finish (hash_data->stream, res, &error);
540 if (hash_data->error != NULL)
542 /* if we already stopped due to an error, probably we're completely
543 * hosed for some reason. just return the first read error
546 g_clear_error (&error);
547 error = hash_data->error;
553 if (hash_data->error != NULL)
555 error = hash_data->error;
559 /* set the checksum in the request */
561 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
563 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash */
564 value = tp_g_value_slice_new (G_TYPE_STRING);
565 g_value_set_string (value, g_checksum_get_string (hash_data->checksum));
566 g_hash_table_insert (priv->request,
567 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
570 hash_data_free (hash_data);
574 priv->is_cancelled = TRUE;
575 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
576 g_clear_error (&error);
580 g_signal_emit (handler, signals[HASHING_DONE], 0);
582 /* the request is complete now, push it to the dispatcher */
583 ft_handler_push_to_dispatcher (handler);
588 hash_job_async_read_cb (GObject *source,
592 HashingData *hash_data = user_data;
594 GError *error = NULL;
596 DEBUG ("Reading a chunk for hashing.");
598 bytes_read = g_input_stream_read_finish (hash_data->stream, res, &error);
601 hash_data->error = error;
602 hash_data->done_reading = TRUE;
606 hash_data->total_read += bytes_read;
608 /* we now have the chunk */
611 hash_data->done_reading = TRUE;
616 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
617 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
618 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
622 g_free (hash_data->buffer);
623 hash_data->buffer = NULL;
625 schedule_hash_chunk (hash_data);
629 schedule_hash_chunk (HashingData *hash_data)
631 EmpathyFTHandlerPriv *priv;
633 priv = GET_PRIV (hash_data->handler);
635 if (hash_data->done_reading)
637 g_input_stream_close_async (hash_data->stream, G_PRIORITY_DEFAULT,
638 priv->cancellable, hash_job_async_close_stream_cb, hash_data);
642 if (hash_data->buffer == NULL)
643 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
645 g_input_stream_read_async (hash_data->stream, hash_data->buffer,
646 BUFFER_SIZE, G_PRIORITY_DEFAULT, priv->cancellable,
647 hash_job_async_read_cb, hash_data);
652 ft_handler_read_async_cb (GObject *source,
656 GFileInputStream *stream;
657 GError *error = NULL;
658 HashingData *hash_data;
660 EmpathyFTHandler *handler = user_data;
661 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
663 DEBUG ("GFile read async CB.");
665 stream = g_file_read_finish (priv->gfile, res, &error);
668 priv->is_cancelled = TRUE;
669 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
670 g_clear_error (&error);
675 hash_data = g_slice_new0 (HashingData);
676 hash_data->stream = G_INPUT_STREAM (stream);
677 hash_data->done_reading = FALSE;
678 hash_data->total_bytes = priv->total_bytes;
679 hash_data->handler = g_object_ref (handler);
680 /* FIXME: should look at the CM capabilities before setting the
683 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
685 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
686 value = tp_g_value_slice_new (G_TYPE_UINT);
687 g_value_set_uint (value, EMP_FILE_HASH_TYPE_MD5);
688 g_hash_table_insert (priv->request,
689 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
691 g_signal_emit (handler, signals[HASHING_STARTED], 0);
693 schedule_hash_chunk (hash_data);
697 ft_handler_complete_request (EmpathyFTHandler *handler)
699 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
700 GError *myerr = NULL;
702 /* check if FT is allowed before firing up the I/O machinery */
703 if (!ft_handler_check_if_allowed (handler))
705 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
706 EMPATHY_FT_ERROR_NOT_SUPPORTED,
707 _("File transfer not supported by remote contact"));
709 priv->is_cancelled = TRUE;
710 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, myerr);
711 g_clear_error (&myerr);
716 /* populate the request table with all the known properties */
717 ft_handler_populate_outgoing_request (handler);
719 /* now start hashing the file */
720 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
721 priv->cancellable, ft_handler_read_async_cb, handler);
725 callbacks_data_free (gpointer user_data)
727 CallbacksData *data = user_data;
730 g_object_unref (data->handler);
732 g_slice_free (CallbacksData, data);
736 ft_handler_gfile_ready_cb (GObject *source,
738 CallbacksData *cb_data)
741 GError *error = NULL;
743 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
745 DEBUG ("Got GFileInfo.");
747 info = g_file_query_info_finish (priv->gfile, res, &error);
752 priv->content_type = g_strdup (g_file_info_get_content_type (info));
753 priv->filename = g_strdup (g_file_info_get_display_name (info));
754 priv->total_bytes = g_file_info_get_size (info);
755 g_file_info_get_modification_time (info, &mtime);
756 priv->mtime = mtime.tv_sec;
757 priv->transferred_bytes = 0;
758 priv->description = NULL;
760 g_object_unref (info);
765 cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
769 cb_data->callback (NULL, error, cb_data->user_data);
770 g_error_free (error);
771 g_object_unref (cb_data->handler);
774 callbacks_data_free (cb_data);
778 ft_handler_contact_ready_cb (EmpathyContact *contact,
781 GObject *weak_object)
783 CallbacksData *cb_data = user_data;
784 EmpathyFTHandlerPriv *priv = GET_PRIV (weak_object);
786 g_assert (priv->contact != NULL);
787 g_assert (priv->gfile != NULL);
789 DEBUG ("Contact is ready.");
791 /* start collecting info about the file */
792 g_file_query_info_async (priv->gfile,
793 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
794 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
795 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
796 G_FILE_ATTRIBUTE_TIME_MODIFIED,
797 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
798 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb,
803 channel_get_all_properties_cb (TpProxy *proxy,
804 GHashTable *properties,
807 GObject *weak_object)
809 CallbacksData *cb_data = user_data;
810 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
811 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
812 EmpathyContactFactory *c_factory;
818 cb_data->callback (NULL, (GError *) error, cb_data->user_data);
819 g_object_unref (handler);
823 priv->total_bytes = g_value_get_uint64 (
824 g_hash_table_lookup (properties, "Size"));
826 priv->transferred_bytes = g_value_get_uint64 (
827 g_hash_table_lookup (properties, "TransferredBytes"));
829 priv->filename = g_value_dup_string (
830 g_hash_table_lookup (properties, "Filename"));
832 priv->content_hash = g_value_dup_string (
833 g_hash_table_lookup (properties, "ContentHash"));
835 priv->content_hash_type = g_value_get_uint (
836 g_hash_table_lookup (properties, "ContentHashType"));
838 priv->content_type = g_value_dup_string (
839 g_hash_table_lookup (properties, "ContentType"));
841 priv->description = g_value_dup_string (
842 g_hash_table_lookup (properties, "Description"));
844 g_hash_table_destroy (properties);
846 c_factory = empathy_contact_factory_dup_singleton ();
847 account = empathy_channel_get_account (TP_CHANNEL (proxy));
848 c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
849 priv->contact = empathy_contact_factory_get_from_handle
850 (c_factory, account, c_handle);
852 g_object_unref (c_factory);
853 g_object_unref (account);
855 cb_data->callback (handler, NULL, cb_data->user_data);
861 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
863 EmpathyFTHandlerReadyCallback callback,
866 EmpathyFTHandler *handler;
868 EmpathyFTHandlerPriv *priv;
870 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
871 g_return_if_fail (G_IS_FILE (source));
873 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
874 "contact", contact, "gfile", source, NULL);
876 priv = GET_PRIV (handler);
878 data = g_slice_new0 (CallbacksData);
879 data->callback = callback;
880 data->user_data = user_data;
881 data->handler = g_object_ref (handler);
883 empathy_contact_call_when_ready (priv->contact,
884 EMPATHY_CONTACT_READY_HANDLE,
885 ft_handler_contact_ready_cb, data, NULL, G_OBJECT (handler));
889 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
890 EmpathyFTHandlerReadyCallback callback,
893 EmpathyFTHandler *handler;
897 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
899 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
900 "tp-file", tp_file, NULL);
902 g_object_get (tp_file, "channel", &channel, NULL);
904 data = g_slice_new0 (CallbacksData);
905 data->callback = callback;
906 data->user_data = user_data;
907 data->handler = g_object_ref (handler);
909 tp_cli_dbus_properties_call_get_all (channel,
910 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
911 channel_get_all_properties_cb, data, callbacks_data_free, G_OBJECT (handler));
915 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler,
916 GCancellable *cancellable)
918 EmpathyFTHandlerPriv *priv;
920 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
922 priv = GET_PRIV (handler);
923 priv->cancellable = g_object_ref (cancellable);
925 if (priv->tpfile == NULL)
927 ft_handler_complete_request (handler);
931 /* emit the start signal now, so that we can catch errors in the
934 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, priv->tpfile);
936 /* TODO: add support for resume. */
937 empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
938 ft_transfer_progress_callback, handler,
939 ft_transfer_operation_callback, handler);
944 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
947 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
948 g_return_if_fail (G_IS_FILE (destination));
950 g_object_set (handler, "gfile", destination, NULL);
954 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
956 EmpathyFTHandlerPriv *priv;
958 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
960 priv = GET_PRIV (handler);
962 return priv->filename;
966 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
968 EmpathyFTHandlerPriv *priv;
970 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
972 priv = GET_PRIV (handler);
974 return priv->content_type;
978 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
980 EmpathyFTHandlerPriv *priv;
982 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
984 priv = GET_PRIV (handler);
986 return priv->contact;
990 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
992 EmpathyFTHandlerPriv *priv;
994 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
996 priv = GET_PRIV (handler);
1001 EmpFileTransferState
1002 empathy_ft_handler_get_state (EmpathyFTHandler *handler,
1003 char **state_string)
1005 EmpathyFTHandlerPriv *priv;
1007 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), -1);
1009 priv = GET_PRIV (handler);
1011 return priv->current_state;
1015 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1017 EmpathyFTHandlerPriv *priv;
1019 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1021 priv = GET_PRIV (handler);
1023 if (priv->tpfile == NULL)
1026 return empathy_tp_file_is_incoming (priv->tpfile);
1030 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1032 EmpathyFTHandlerPriv *priv;
1034 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1036 priv = GET_PRIV (handler);
1038 return priv->transferred_bytes;
1042 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1044 EmpathyFTHandlerPriv *priv;
1046 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1048 priv = GET_PRIV (handler);
1050 return priv->total_bytes;
1054 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1056 EmpathyFTHandlerPriv *priv;
1058 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1060 priv = GET_PRIV (handler);
1062 return priv->is_completed;
1066 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1068 EmpathyFTHandlerPriv *priv;
1070 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1072 priv = GET_PRIV (handler);
1074 return priv->is_cancelled;