Update Slovak translation
[empathy-mirror.git] / libempathy / empathy-ft-handler.c
blob367e9460562fbc7504e31d47a83209470ed65147
1 /*
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 "config.h"
25 #include "empathy-ft-handler.h"
27 #include <glib/gi18n-lib.h>
28 #include <tp-account-widgets/tpaw-time.h>
29 #include <tp-account-widgets/tpaw-utils.h>
30 #include <telepathy-glib/telepathy-glib-dbus.h>
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_FT
35 #include "empathy-debug.h"
37 /**
38 * SECTION:empathy-ft-handler
39 * @title: EmpathyFTHandler
40 * @short_description: an object representing a File Transfer
41 * @include: libempathy/empathy-ft-handler
43 * #EmpathyFTHandler is the object which represents a File Transfer with all
44 * its properties.
45 * The creation of an #EmpathyFTHandler is done with
46 * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
47 * even though clients should not need to call them directly, as
48 * #EmpathyFTFactory does it for them. Remember that for the file transfer
49 * to work with an incoming handler,
50 * empathy_ft_handler_incoming_set_destination() should be called after
51 * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
52 * automatically.
53 * It's important to note that, as the creation of the handlers is async, once
54 * an handler is created, it already has all the interesting properties set,
55 * like filename, total bytes, content type and so on, making it useful
56 * to be displayed in an UI.
57 * The transfer API works like a state machine; it has three signals,
58 * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
59 * emitted in the relevant phases.
60 * In addition, if the handler is created with checksumming enabled,
61 * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
62 * will be emitted before or after the transfer, depending on the direction
63 * (respectively outgoing and incoming) of the handler.
64 * At any time between the call to empathy_ft_handler_start_transfer() and
65 * the last signal, a ::transfer-error can be emitted, indicating that an
66 * error has happened in the operation. The message of the error is localized
67 * to use in an UI.
70 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
72 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
74 #define BUFFER_SIZE 4096
76 enum {
77 PROP_CHANNEL = 1,
78 PROP_G_FILE,
79 PROP_CONTACT,
80 PROP_CONTENT_TYPE,
81 PROP_DESCRIPTION,
82 PROP_FILENAME,
83 PROP_MODIFICATION_TIME,
84 PROP_TOTAL_BYTES,
85 PROP_TRANSFERRED_BYTES,
86 PROP_USER_ACTION_TIME
89 enum {
90 HASHING_STARTED,
91 HASHING_PROGRESS,
92 HASHING_DONE,
93 TRANSFER_STARTED,
94 TRANSFER_PROGRESS,
95 TRANSFER_DONE,
96 TRANSFER_ERROR,
97 LAST_SIGNAL
100 typedef struct {
101 GInputStream *stream;
102 GError *error /* comment to make the style checker happy */;
103 guchar *buffer;
104 GChecksum *checksum;
105 gssize total_read;
106 guint64 total_bytes;
107 EmpathyFTHandler *handler;
108 } HashingData;
110 typedef struct {
111 EmpathyFTHandlerReadyCallback callback;
112 gpointer user_data;
113 EmpathyFTHandler *handler;
114 } CallbacksData;
116 /* private data */
117 typedef struct {
118 gboolean dispose_run;
120 GFile *gfile;
121 TpFileTransferChannel *channel;
122 GCancellable *cancellable;
123 gboolean use_hash;
125 /* request for the new transfer */
126 TpAccountChannelRequest *request;
128 /* transfer properties */
129 EmpathyContact *contact;
130 gchar *content_type;
131 gchar *filename;
132 gchar *description;
133 guint64 total_bytes;
134 guint64 transferred_bytes;
135 guint64 mtime;
136 gchar *content_hash;
137 TpFileHashType content_hash_type;
139 gint64 user_action_time;
141 /* time and speed */
142 gdouble speed;
143 guint remaining_time;
144 gint64 last_update_time;
146 gboolean is_completed;
147 } EmpathyFTHandlerPriv;
149 static guint signals[LAST_SIGNAL] = { 0 };
151 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
152 GCancellable *cancellable, gpointer user_data);
154 /* GObject implementations */
155 static void
156 do_get_property (GObject *object,
157 guint property_id,
158 GValue *value,
159 GParamSpec *pspec)
161 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
163 switch (property_id)
165 case PROP_CONTACT:
166 g_value_set_object (value, priv->contact);
167 break;
168 case PROP_CONTENT_TYPE:
169 g_value_set_string (value, priv->content_type);
170 break;
171 case PROP_DESCRIPTION:
172 g_value_set_string (value, priv->description);
173 break;
174 case PROP_FILENAME:
175 g_value_set_string (value, priv->filename);
176 break;
177 case PROP_MODIFICATION_TIME:
178 g_value_set_uint64 (value, priv->mtime);
179 break;
180 case PROP_TOTAL_BYTES:
181 g_value_set_uint64 (value, priv->total_bytes);
182 break;
183 case PROP_TRANSFERRED_BYTES:
184 g_value_set_uint64 (value, priv->transferred_bytes);
185 break;
186 case PROP_G_FILE:
187 g_value_set_object (value, priv->gfile);
188 break;
189 case PROP_CHANNEL:
190 g_value_set_object (value, priv->channel);
191 break;
192 case PROP_USER_ACTION_TIME:
193 g_value_set_int64 (value, priv->user_action_time);
194 break;
195 default:
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
200 static void
201 do_set_property (GObject *object,
202 guint property_id,
203 const GValue *value,
204 GParamSpec *pspec)
206 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
208 switch (property_id)
210 case PROP_CONTACT:
211 priv->contact = g_value_dup_object (value);
212 break;
213 case PROP_CONTENT_TYPE:
214 priv->content_type = g_value_dup_string (value);
215 break;
216 case PROP_DESCRIPTION:
217 priv->description = g_value_dup_string (value);
218 break;
219 case PROP_FILENAME:
220 priv->filename = g_value_dup_string (value);
221 break;
222 case PROP_MODIFICATION_TIME:
223 priv->mtime = g_value_get_uint64 (value);
224 break;
225 case PROP_TOTAL_BYTES:
226 priv->total_bytes = g_value_get_uint64 (value);
227 break;
228 case PROP_TRANSFERRED_BYTES:
229 priv->transferred_bytes = g_value_get_uint64 (value);
230 break;
231 case PROP_G_FILE:
232 priv->gfile = g_value_dup_object (value);
233 break;
234 case PROP_CHANNEL:
235 priv->channel = g_value_dup_object (value);
236 break;
237 case PROP_USER_ACTION_TIME:
238 priv->user_action_time = g_value_get_int64 (value);
239 break;
240 default:
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
245 static void
246 do_dispose (GObject *object)
248 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
250 if (priv->dispose_run)
251 return;
253 priv->dispose_run = TRUE;
255 if (priv->contact != NULL) {
256 g_object_unref (priv->contact);
257 priv->contact = NULL;
260 if (priv->gfile != NULL) {
261 g_object_unref (priv->gfile);
262 priv->gfile = NULL;
265 if (priv->channel != NULL) {
266 tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, NULL);
267 g_object_unref (priv->channel);
268 priv->channel = NULL;
271 if (priv->cancellable != NULL) {
272 g_object_unref (priv->cancellable);
273 priv->cancellable = NULL;
276 g_clear_object (&priv->request);
278 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
281 static void
282 do_finalize (GObject *object)
284 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
286 DEBUG ("%p", object);
288 g_free (priv->content_type);
289 priv->content_type = NULL;
291 g_free (priv->filename);
292 priv->filename = NULL;
294 g_free (priv->description);
295 priv->description = NULL;
297 g_free (priv->content_hash);
298 priv->content_hash = NULL;
300 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
303 static void
304 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
306 GObjectClass *object_class = G_OBJECT_CLASS (klass);
307 GParamSpec *param_spec;
309 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
311 object_class->get_property = do_get_property;
312 object_class->set_property = do_set_property;
313 object_class->dispose = do_dispose;
314 object_class->finalize = do_finalize;
316 /* properties */
319 * EmpathyFTHandler:contact:
321 * The remote #EmpathyContact for the transfer
323 param_spec = g_param_spec_object ("contact",
324 "contact", "The remote contact",
325 EMPATHY_TYPE_CONTACT,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
327 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
330 * EmpathyFTHandler:content-type:
332 * The content type of the file being transferred
334 param_spec = g_param_spec_string ("content-type",
335 "content-type", "The content type of the file", NULL,
336 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
337 g_object_class_install_property (object_class,
338 PROP_CONTENT_TYPE, param_spec);
341 * EmpathyFTHandler:description:
343 * The description of the file being transferred
345 param_spec = g_param_spec_string ("description",
346 "description", "The description of the file", NULL,
347 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
348 g_object_class_install_property (object_class,
349 PROP_DESCRIPTION, param_spec);
352 * EmpathyFTHandler:filename:
354 * The name of the file being transferred
356 param_spec = g_param_spec_string ("filename",
357 "filename", "The name of the file", NULL,
358 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
359 g_object_class_install_property (object_class,
360 PROP_FILENAME, param_spec);
363 * EmpathyFTHandler:modification-time:
365 * The modification time of the file being transferred
367 param_spec = g_param_spec_uint64 ("modification-time",
368 "modification-time", "The mtime of the file", 0,
369 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
370 g_object_class_install_property (object_class,
371 PROP_MODIFICATION_TIME, param_spec);
374 * EmpathyFTHandler:total-bytes:
376 * The size (in bytes) of the file being transferred
378 param_spec = g_param_spec_uint64 ("total-bytes",
379 "total-bytes", "The size of the file", 0,
380 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
381 g_object_class_install_property (object_class,
382 PROP_TOTAL_BYTES, param_spec);
385 * EmpathyFTHandler:transferred-bytes:
387 * The number of the bytes already transferred
389 param_spec = g_param_spec_uint64 ("transferred-bytes",
390 "transferred-bytes", "The number of bytes already transferred", 0,
391 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
392 g_object_class_install_property (object_class,
393 PROP_TRANSFERRED_BYTES, param_spec);
396 * EmpathyFTHandler:gfile:
398 * The #GFile object where the transfer actually happens
400 param_spec = g_param_spec_object ("gfile",
401 "gfile", "The GFile we're handling",
402 G_TYPE_FILE,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
404 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
407 * EmpathyFTHandler:channel:
409 * The underlying #TpFileTransferChannel managing the transfer
411 param_spec = g_param_spec_object ("channel",
412 "channel", "The file transfer channel",
413 TP_TYPE_FILE_TRANSFER_CHANNEL,
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
415 g_object_class_install_property (object_class, PROP_CHANNEL, param_spec);
417 param_spec = g_param_spec_int64 ("user-action-time", "user action time",
418 "User action time",
419 0, G_MAXINT64, 0,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
421 g_object_class_install_property (object_class, PROP_USER_ACTION_TIME,
422 param_spec);
424 /* signals */
427 * EmpathyFTHandler::transfer-started
428 * @handler: the object which has received the signal
429 * @channel: the #TpFileTransferChannel for which the transfer has started
431 * This signal is emitted when the actual transfer starts.
433 signals[TRANSFER_STARTED] =
434 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
435 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
436 g_cclosure_marshal_generic,
437 G_TYPE_NONE,
438 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
441 * EmpathyFTHandler::transfer-done
442 * @handler: the object which has received the signal
443 * @channel: the #TpFileTransferChannel for which the transfer has started
445 * This signal will be emitted when the actual transfer is completed
446 * successfully.
448 signals[TRANSFER_DONE] =
449 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
450 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
451 g_cclosure_marshal_generic,
452 G_TYPE_NONE,
453 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
456 * EmpathyFTHandler::transfer-error
457 * @handler: the object which has received the signal
458 * @error: a #GError
460 * This signal can be emitted anytime between the call to
461 * empathy_ft_handler_start_transfer() and the last expected signal
462 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
463 * signal coming from the handler, meaning that no other operation will
464 * take place after this signal.
466 signals[TRANSFER_ERROR] =
467 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
468 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
469 g_cclosure_marshal_generic,
470 G_TYPE_NONE,
471 1, G_TYPE_POINTER);
474 * EmpathyFTHandler::transfer-progress
475 * @handler: the object which has received the signal
476 * @current_bytes: the bytes currently transferred
477 * @total_bytes: the total bytes of the handler
478 * @remaining_time: the number of seconds remaining for the transfer
479 * to be completed
480 * @speed: the current speed of the transfer (in KB/s)
482 * This signal is emitted to notify clients of the progress of the
483 * transfer.
485 signals[TRANSFER_PROGRESS] =
486 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
487 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
488 g_cclosure_marshal_generic,
489 G_TYPE_NONE,
490 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
493 * EmpathyFTHandler::hashing-started
494 * @handler: the object which has received the signal
496 * This signal is emitted when the hashing operation of the handler
497 * is started. Note that this might happen or not, depending on the CM
498 * and remote contact capabilities. Clients shoud use
499 * empathy_ft_handler_get_use_hash() before calling
500 * empathy_ft_handler_start_transfer() to know whether they should connect
501 * to this signal.
503 signals[HASHING_STARTED] =
504 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
505 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
506 g_cclosure_marshal_generic,
507 G_TYPE_NONE, 0);
510 * EmpathyFTHandler::hashing-progress
511 * @handler: the object which has received the signal
512 * @current_bytes: the bytes currently hashed
513 * @total_bytes: the total bytes of the handler
515 * This signal is emitted to notify clients of the progress of the
516 * hashing operation.
518 signals[HASHING_PROGRESS] =
519 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
520 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
521 g_cclosure_marshal_generic,
522 G_TYPE_NONE,
523 2, G_TYPE_UINT64, G_TYPE_UINT64);
526 * EmpathyFTHandler::hashing-done
527 * @handler: the object which has received the signal
529 * This signal is emitted when the hashing operation of the handler
530 * is completed.
532 signals[HASHING_DONE] =
533 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
534 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
535 g_cclosure_marshal_generic,
536 G_TYPE_NONE, 0);
539 static void
540 empathy_ft_handler_init (EmpathyFTHandler *self)
542 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
543 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
545 self->priv = priv;
546 priv->cancellable = g_cancellable_new ();
549 /* private functions */
551 static void
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);
571 static GChecksumType
572 tp_file_hash_to_g_checksum (TpFileHashType type)
574 GChecksumType retval;
576 switch (type)
578 case TP_FILE_HASH_TYPE_MD5:
579 retval = G_CHECKSUM_MD5;
580 break;
581 case TP_FILE_HASH_TYPE_SHA1:
582 retval = G_CHECKSUM_SHA1;
583 break;
584 case TP_FILE_HASH_TYPE_SHA256:
585 retval = G_CHECKSUM_SHA256;
586 break;
587 case TP_FILE_HASH_TYPE_NONE:
588 default:
589 g_assert_not_reached ();
590 break;
593 return retval;
596 static void
597 check_hash_incoming (EmpathyFTHandler *handler)
599 HashingData *hash_data;
600 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
602 if (!TPAW_STR_EMPTY (priv->content_hash))
604 hash_data = g_slice_new0 (HashingData);
605 hash_data->total_bytes = priv->total_bytes;
606 hash_data->handler = g_object_ref (handler);
607 hash_data->checksum = g_checksum_new
608 (tp_file_hash_to_g_checksum (priv->content_hash_type));
610 g_signal_emit (handler, signals[HASHING_STARTED], 0);
612 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
613 G_PRIORITY_DEFAULT, priv->cancellable);
617 static void
618 emit_error_signal (EmpathyFTHandler *handler,
619 const GError *error)
621 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
623 DEBUG ("Error in transfer: %s\n", error->message);
625 if (!g_cancellable_is_cancelled (priv->cancellable))
626 g_cancellable_cancel (priv->cancellable);
628 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
631 static void
632 update_remaining_time_and_speed (EmpathyFTHandler *handler,
633 guint64 transferred_bytes)
635 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
636 gint64 elapsed_time, current_time;
637 guint64 transferred, last_transferred_bytes;
638 gdouble speed;
639 gint remaining_time;
641 last_transferred_bytes = priv->transferred_bytes;
642 priv->transferred_bytes = transferred_bytes;
644 current_time = tpaw_time_get_current ();
645 elapsed_time = current_time - priv->last_update_time;
647 if (elapsed_time >= 1)
649 transferred = transferred_bytes - last_transferred_bytes;
650 speed = (gdouble) transferred / (gdouble) elapsed_time;
651 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
652 priv->speed = speed;
653 priv->remaining_time = remaining_time;
654 priv->last_update_time = current_time;
658 static void
659 ft_transfer_transferred_bytes_cb (TpFileTransferChannel *channel,
660 GParamSpec *pspec,
661 EmpathyFTHandler *handler)
663 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
664 guint64 bytes;
666 if (empathy_ft_handler_is_cancelled (handler))
667 return;
669 bytes = tp_file_transfer_channel_get_transferred_bytes (channel);
671 if (priv->transferred_bytes == 0)
673 priv->last_update_time = tpaw_time_get_current ();
674 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, channel);
677 if (priv->transferred_bytes != bytes)
679 update_remaining_time_and_speed (handler, bytes);
681 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
682 bytes, priv->total_bytes, priv->remaining_time,
683 priv->speed);
687 static void
688 ft_transfer_provide_cb (GObject *source,
689 GAsyncResult *result,
690 gpointer user_data)
692 TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
693 EmpathyFTHandler *handler = user_data;
694 GError *error = NULL;
696 if (!tp_file_transfer_channel_provide_file_finish (channel, result, &error))
698 emit_error_signal (handler, error);
699 g_clear_error (&error);
703 static void
704 ft_transfer_accept_cb (GObject *source,
705 GAsyncResult *result,
706 gpointer user_data)
708 TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
709 EmpathyFTHandler *handler = user_data;
710 GError *error = NULL;
712 if (!tp_file_transfer_channel_accept_file_finish (channel, result, &error))
714 emit_error_signal (handler, error);
715 g_clear_error (&error);
719 static GError *
720 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
722 const char *string;
723 GError *retval = NULL;
725 string = NULL;
727 switch (reason)
729 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
730 string = _("No reason was specified");
731 break;
732 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
733 string = _("The change in state was requested");
734 break;
735 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
736 string = _("You canceled the file transfer");
737 break;
738 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
739 string = _("The other participant canceled the file transfer");
740 break;
741 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
742 string = _("Error while trying to transfer the file");
743 break;
744 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
745 string = _("The other participant is unable to transfer the file");
746 break;
747 default:
748 string = _("Unknown reason");
749 break;
752 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
753 EMPATHY_FT_ERROR_TP_ERROR, string);
755 return retval;
758 static void
759 ft_transfer_state_cb (TpFileTransferChannel *channel,
760 GParamSpec *pspec,
761 EmpathyFTHandler *handler)
763 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
764 TpFileTransferStateChangeReason reason;
765 TpFileTransferState state = tp_file_transfer_channel_get_state (
766 channel, &reason);
768 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
770 priv->is_completed = TRUE;
771 g_signal_emit (handler, signals[TRANSFER_DONE], 0, channel);
773 tp_channel_close_async (TP_CHANNEL (channel), NULL, NULL);
775 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
777 check_hash_incoming (handler);
780 else if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
782 GError *error = error_from_state_change_reason (reason);
783 emit_error_signal (handler, error);
784 g_clear_error (&error);
788 static void
789 ft_handler_create_channel_cb (GObject *source,
790 GAsyncResult *result,
791 gpointer user_data)
793 EmpathyFTHandler *handler = user_data;
794 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
795 GError *error = NULL;
796 TpChannel *channel;
798 DEBUG ("Dispatcher create channel CB");
800 channel = tp_account_channel_request_create_and_handle_channel_finish (
801 TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
803 if (channel == NULL)
804 DEBUG ("Failed to request FT channel: %s", error->message);
805 else
806 g_cancellable_set_error_if_cancelled (priv->cancellable, &error);
808 if (error != NULL)
810 emit_error_signal (handler, error);
812 g_clear_object (&channel);
813 g_error_free (error);
814 return;
817 priv->channel = TP_FILE_TRANSFER_CHANNEL (channel);
819 tp_g_signal_connect_object (priv->channel, "notify::state",
820 G_CALLBACK (ft_transfer_state_cb), handler, 0);
821 tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
822 G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
824 tp_file_transfer_channel_provide_file_async (priv->channel, priv->gfile,
825 ft_transfer_provide_cb, handler);
828 static void
829 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
831 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
833 DEBUG ("Pushing request to the dispatcher");
835 tp_account_channel_request_create_and_handle_channel_async (priv->request,
836 NULL, ft_handler_create_channel_cb, handler);
839 static void
840 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
842 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
843 gchar *uri;
844 TpAccount *account;
846 uri = g_file_get_uri (priv->gfile);
847 account = empathy_contact_get_account (priv->contact);
849 priv->request = tp_account_channel_request_new_file_transfer (account,
850 priv->filename, priv->content_type, priv->total_bytes,
851 priv->user_action_time);
853 tp_account_channel_request_set_target_contact (priv->request,
854 empathy_contact_get_tp_contact (priv->contact));
856 tp_account_channel_request_set_file_transfer_timestamp (priv->request,
857 priv->mtime);
858 tp_account_channel_request_set_file_transfer_uri (priv->request, uri);
860 g_free (uri);
863 static gboolean
864 hash_job_done (gpointer user_data)
866 HashingData *hash_data = user_data;
867 EmpathyFTHandler *handler = hash_data->handler;
868 EmpathyFTHandlerPriv *priv;
869 GError *error = NULL;
871 DEBUG ("Closing stream after hashing.");
873 priv = GET_PRIV (handler);
875 if (hash_data->error != NULL)
877 error = hash_data->error;
878 hash_data->error = NULL;
879 goto cleanup;
882 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
884 if (empathy_ft_handler_is_incoming (handler))
886 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
887 priv->content_hash))
889 DEBUG ("Hash mismatch when checking incoming handler: "
890 "received %s, calculated %s", priv->content_hash,
891 g_checksum_get_string (hash_data->checksum));
893 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
894 EMPATHY_FT_ERROR_HASH_MISMATCH,
895 _("File transfer completed, but the file was corrupted"));
896 goto cleanup;
898 else
900 DEBUG ("Hash verification matched, received %s, calculated %s",
901 priv->content_hash,
902 g_checksum_get_string (hash_data->checksum));
905 else
907 /* set the checksum in the request...
908 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
910 tp_account_channel_request_set_file_transfer_hash (priv->request,
911 TP_FILE_HASH_TYPE_MD5, g_checksum_get_string (hash_data->checksum));
914 cleanup:
916 if (error != NULL)
918 emit_error_signal (handler, error);
919 g_clear_error (&error);
921 else
923 g_signal_emit (handler, signals[HASHING_DONE], 0);
925 if (!empathy_ft_handler_is_incoming (handler))
926 /* the request is complete now, push it to the dispatcher */
927 ft_handler_push_to_dispatcher (handler);
930 hash_data_free (hash_data);
932 return FALSE;
935 static gboolean
936 emit_hashing_progress (gpointer user_data)
938 HashingData *hash_data = user_data;
940 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
941 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
943 return FALSE;
946 static gboolean
947 do_hash_job (GIOSchedulerJob *job,
948 GCancellable *cancellable,
949 gpointer user_data)
951 HashingData *hash_data = user_data;
952 gssize bytes_read;
953 GError *error = NULL;
955 again:
956 if (hash_data->buffer == NULL)
957 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
959 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
960 BUFFER_SIZE, cancellable, &error);
961 if (error != NULL)
962 goto out;
964 hash_data->total_read += bytes_read;
966 /* we now have the chunk */
967 if (bytes_read > 0)
969 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
970 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
971 hash_data, NULL);
973 g_free (hash_data->buffer);
974 hash_data->buffer = NULL;
976 goto again;
978 else
980 g_input_stream_close (hash_data->stream, cancellable, &error);
983 out:
984 if (error != NULL)
985 hash_data->error = error;
987 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
988 hash_data, NULL);
990 return FALSE;
993 static gboolean
994 do_hash_job_incoming (GIOSchedulerJob *job,
995 GCancellable *cancellable,
996 gpointer user_data)
998 HashingData *hash_data = user_data;
999 EmpathyFTHandler *handler = hash_data->handler;
1000 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1001 GError *error = NULL;
1003 DEBUG ("checking integrity for incoming handler");
1005 /* need to get the stream first */
1006 hash_data->stream =
1007 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
1009 if (error != NULL)
1011 hash_data->error = error;
1012 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
1013 hash_data, NULL);
1014 return FALSE;
1017 return do_hash_job (job, cancellable, user_data);
1020 static void
1021 ft_handler_read_async_cb (GObject *source,
1022 GAsyncResult *res,
1023 gpointer user_data)
1025 GFileInputStream *stream;
1026 GError *error = NULL;
1027 HashingData *hash_data;
1028 EmpathyFTHandler *handler = user_data;
1029 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1031 DEBUG ("GFile read async CB.");
1033 stream = g_file_read_finish (priv->gfile, res, &error);
1034 if (error != NULL)
1036 emit_error_signal (handler, error);
1037 g_clear_error (&error);
1039 return;
1042 hash_data = g_slice_new0 (HashingData);
1043 hash_data->stream = G_INPUT_STREAM (stream);
1044 hash_data->total_bytes = priv->total_bytes;
1045 hash_data->handler = g_object_ref (handler);
1046 /* FIXME: MD5 is the only ContentHashType supported right now */
1047 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
1049 g_signal_emit (handler, signals[HASHING_STARTED], 0);
1051 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1052 G_PRIORITY_DEFAULT, priv->cancellable);
1055 static void
1056 callbacks_data_free (gpointer user_data)
1058 CallbacksData *data = user_data;
1060 if (data->handler != NULL)
1061 g_object_unref (data->handler);
1063 g_slice_free (CallbacksData, data);
1066 static gboolean
1067 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1068 GPtrArray *classes)
1070 GArray *possible_values;
1071 guint value;
1072 gboolean valid;
1073 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1074 gboolean support_ft = FALSE;
1075 guint i;
1077 possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1079 for (i = 0; i < classes->len; i++)
1081 GHashTable *fixed;
1082 GStrv allowed;
1083 const gchar *chan_type;
1085 tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
1086 &fixed, &allowed);
1088 chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
1090 if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1091 continue;
1093 if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
1094 TP_HANDLE_TYPE_CONTACT)
1095 continue;
1097 support_ft = TRUE;
1099 value = tp_asv_get_uint32
1100 (fixed, TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
1101 &valid);
1103 if (valid)
1104 g_array_append_val (possible_values, value);
1107 if (!support_ft)
1109 g_array_unref (possible_values);
1110 return FALSE;
1113 if (possible_values->len == 0)
1115 /* there are no channel classes with hash support, disable it. */
1116 priv->use_hash = FALSE;
1117 priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1119 goto out;
1122 priv->use_hash = TRUE;
1124 if (possible_values->len == 1)
1126 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1128 else
1130 /* order the array and pick the first non zero, so that MD5
1131 * is the preferred value.
1133 g_array_sort (possible_values, empathy_uint_compare);
1135 if (g_array_index (possible_values, guint, 0) == 0)
1136 priv->content_hash_type = g_array_index (possible_values, guint, 1);
1137 else
1138 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1141 out:
1142 g_array_unref (possible_values);
1144 DEBUG ("Hash enabled %s; setting content hash type as %u",
1145 priv->use_hash ? "True" : "False", priv->content_hash_type);
1147 return TRUE;
1150 static void
1151 check_hashing (CallbacksData *data)
1153 EmpathyFTHandler *handler = data->handler;
1154 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1155 GError *myerr = NULL;
1156 TpCapabilities *caps;
1157 GPtrArray *classes;
1158 TpConnection *conn;
1160 conn = empathy_contact_get_connection (priv->contact);
1162 caps = tp_connection_get_capabilities (conn);
1163 if (caps == NULL)
1165 data->callback (handler, NULL, data->user_data);
1166 goto out;
1169 classes = tp_capabilities_get_channel_classes (caps);
1171 /* set whether we support hash and the type of it */
1172 if (!set_content_hash_type_from_classes (handler, classes))
1174 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1175 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1176 _("File transfer not supported by remote contact"));
1178 if (!g_cancellable_is_cancelled (priv->cancellable))
1179 g_cancellable_cancel (priv->cancellable);
1181 data->callback (handler, myerr, data->user_data);
1182 g_clear_error (&myerr);
1184 else
1186 /* get back to the caller now */
1187 data->callback (handler, NULL, data->user_data);
1190 out:
1191 callbacks_data_free (data);
1194 static void
1195 ft_handler_complete_request (EmpathyFTHandler *handler)
1197 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1199 /* populate the request table with all the known properties */
1200 ft_handler_populate_outgoing_request (handler);
1202 if (priv->use_hash)
1203 /* start hashing the file */
1204 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1205 priv->cancellable, ft_handler_read_async_cb, handler);
1206 else
1207 /* push directly the handler to the dispatcher */
1208 ft_handler_push_to_dispatcher (handler);
1211 static void
1212 ft_handler_gfile_ready_cb (GObject *source,
1213 GAsyncResult *res,
1214 CallbacksData *cb_data)
1216 GFileInfo *info;
1217 GError *error = NULL;
1218 GTimeVal mtime;
1219 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1221 DEBUG ("Got GFileInfo.");
1223 info = g_file_query_info_finish (priv->gfile, res, &error);
1225 if (error != NULL)
1226 goto out;
1228 if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1230 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1231 EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1232 _("The selected file is not a regular file"));
1233 goto out;
1236 priv->total_bytes = g_file_info_get_size (info);
1237 if (priv->total_bytes == 0)
1239 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1240 EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1241 _("The selected file is empty"));
1242 goto out;
1245 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1246 priv->filename = g_strdup (g_file_info_get_display_name (info));
1247 g_file_info_get_modification_time (info, &mtime);
1248 priv->mtime = mtime.tv_sec;
1249 priv->transferred_bytes = 0;
1250 priv->description = NULL;
1252 g_object_unref (info);
1254 out:
1255 if (error != NULL)
1257 if (!g_cancellable_is_cancelled (priv->cancellable))
1258 g_cancellable_cancel (priv->cancellable);
1260 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1261 g_error_free (error);
1263 callbacks_data_free (cb_data);
1265 else
1267 /* see if FT/hashing are allowed */
1268 check_hashing (cb_data);
1272 static void
1273 channel_get_all_properties_cb (TpProxy *proxy,
1274 GHashTable *properties,
1275 const GError *error,
1276 gpointer user_data,
1277 GObject *weak_object)
1279 CallbacksData *cb_data = user_data;
1280 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1281 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1282 TpContact *contact;
1284 if (error != NULL)
1286 if (!g_cancellable_is_cancelled (priv->cancellable))
1287 g_cancellable_cancel (priv->cancellable);
1289 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1291 callbacks_data_free (cb_data);
1292 return;
1295 priv->content_hash = g_value_dup_string (
1296 g_hash_table_lookup (properties, "ContentHash"));
1298 priv->content_hash_type = g_value_get_uint (
1299 g_hash_table_lookup (properties, "ContentHashType"));
1301 contact = tp_channel_get_target_contact (TP_CHANNEL (proxy));
1302 priv->contact = empathy_contact_dup_from_tp_contact (contact);
1304 cb_data->callback (handler, NULL, cb_data->user_data);
1307 /* public methods */
1310 * empathy_ft_handler_new_outgoing:
1311 * @contact: the #EmpathyContact to send @source to
1312 * @source: the #GFile to send
1313 * @callback: callback to be called when the handler has been created
1314 * @user_data: user data to be passed to @callback
1316 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1318 void
1319 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1320 GFile *source,
1321 gint64 action_time,
1322 EmpathyFTHandlerReadyCallback callback,
1323 gpointer user_data)
1325 EmpathyFTHandler *handler;
1326 CallbacksData *data;
1327 EmpathyFTHandlerPriv *priv;
1329 DEBUG ("New handler outgoing");
1331 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1332 g_return_if_fail (G_IS_FILE (source));
1334 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1335 "contact", contact,
1336 "gfile", source,
1337 "user-action-time", action_time,
1338 NULL);
1340 priv = GET_PRIV (handler);
1342 data = g_slice_new0 (CallbacksData);
1343 data->callback = callback;
1344 data->user_data = user_data;
1345 data->handler = g_object_ref (handler);
1347 /* start collecting info about the file */
1348 g_file_query_info_async (priv->gfile,
1349 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1350 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1351 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1352 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1353 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1354 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1355 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1359 * empathy_ft_handler_new_incoming:
1360 * @channel: the #TpFileTransferChannel proxy to the incoming channel
1361 * @callback: callback to be called when the handler has been created
1362 * @user_data: user data to be passed to @callback
1364 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1365 * Note that for the handler to be useful, you will have to set a destination
1366 * file with empathy_ft_handler_incoming_set_destination() after the handler
1367 * is ready.
1369 void
1370 empathy_ft_handler_new_incoming (TpFileTransferChannel *channel,
1371 EmpathyFTHandlerReadyCallback callback,
1372 gpointer user_data)
1374 EmpathyFTHandler *handler;
1375 CallbacksData *data;
1376 EmpathyFTHandlerPriv *priv;
1378 g_return_if_fail (TP_IS_FILE_TRANSFER_CHANNEL (channel));
1380 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1381 "channel", channel, NULL);
1383 priv = GET_PRIV (handler);
1385 data = g_slice_new0 (CallbacksData);
1386 data->callback = callback;
1387 data->user_data = user_data;
1388 data->handler = g_object_ref (handler);
1390 priv->total_bytes = tp_file_transfer_channel_get_size (channel);
1392 priv->transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (
1393 channel);
1395 priv->filename = g_strdup (tp_file_transfer_channel_get_filename (channel));
1397 priv->content_type = g_strdup (tp_file_transfer_channel_get_mime_type (
1398 channel));
1400 priv->description = g_strdup (tp_file_transfer_channel_get_description (
1401 channel));
1403 tp_cli_dbus_properties_call_get_all (channel,
1404 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1405 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1409 * empathy_ft_handler_start_transfer:
1410 * @handler: an #EmpathyFTHandler
1412 * Starts the transfer machinery. After this call, the transfer and hashing
1413 * signals will be emitted by the handler.
1415 void
1416 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1418 EmpathyFTHandlerPriv *priv;
1420 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1422 priv = GET_PRIV (handler);
1424 if (priv->channel == NULL)
1426 ft_handler_complete_request (handler);
1428 else
1430 /* TODO: add support for resume. */
1431 tp_file_transfer_channel_accept_file_async (priv->channel,
1432 priv->gfile, 0, ft_transfer_accept_cb, handler);
1434 tp_g_signal_connect_object (priv->channel, "notify::state",
1435 G_CALLBACK (ft_transfer_state_cb), handler, 0);
1436 tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
1437 G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
1442 * empathy_ft_handler_cancel_transfer:
1443 * @handler: an #EmpathyFTHandler
1445 * Cancels an ongoing handler operation. Note that this doesn't destroy
1446 * the object, which will keep all the properties, altough it won't be able
1447 * to do any more I/O.
1449 void
1450 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1452 EmpathyFTHandlerPriv *priv;
1454 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1456 priv = GET_PRIV (handler);
1458 /* if we don't have a channel, we are hashing, so
1459 * we can just cancel the GCancellable to stop it.
1461 if (priv->channel == NULL)
1462 g_cancellable_cancel (priv->cancellable);
1463 else
1464 tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, NULL);
1468 * empathy_ft_handler_incoming_set_destination:
1469 * @handler: an #EmpathyFTHandler
1470 * @destination: the #GFile where the transfer should be saved
1472 * Sets the destination of the incoming handler to be @destination.
1473 * Note that calling this method is mandatory before starting the transfer
1474 * for incoming handlers.
1476 void
1477 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1478 GFile *destination)
1480 EmpathyFTHandlerPriv *priv;
1482 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1483 g_return_if_fail (G_IS_FILE (destination));
1485 priv = GET_PRIV (handler);
1487 g_object_set (handler, "gfile", destination, NULL);
1489 /* check if hash is supported. if it isn't, set use_hash to FALSE
1490 * anyway, so that clients won't be expecting us to checksum.
1492 if (TPAW_STR_EMPTY (priv->content_hash) ||
1493 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1494 priv->use_hash = FALSE;
1495 else
1496 priv->use_hash = TRUE;
1500 * empathy_ft_handler_get_filename:
1501 * @handler: an #EmpathyFTHandler
1503 * Returns the name of the file being transferred.
1505 * Return value: the name of the file being transferred
1507 const char *
1508 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1510 EmpathyFTHandlerPriv *priv;
1512 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1514 priv = GET_PRIV (handler);
1516 return priv->filename;
1520 * empathy_ft_handler_get_content_type:
1521 * @handler: an #EmpathyFTHandler
1523 * Returns the content type of the file being transferred.
1525 * Return value: the content type of the file being transferred
1527 const char *
1528 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1530 EmpathyFTHandlerPriv *priv;
1532 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1534 priv = GET_PRIV (handler);
1536 return priv->content_type;
1540 * empathy_ft_handler_get_contact:
1541 * @handler: an #EmpathyFTHandler
1543 * Returns the remote #EmpathyContact at the other side of the transfer.
1545 * Return value: the remote #EmpathyContact for @handler
1547 EmpathyContact *
1548 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1550 EmpathyFTHandlerPriv *priv;
1552 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1554 priv = GET_PRIV (handler);
1556 return priv->contact;
1560 * empathy_ft_handler_get_gfile:
1561 * @handler: an #EmpathyFTHandler
1563 * Returns the #GFile where the transfer is being read/saved.
1565 * Return value: the #GFile where the transfer is being read/saved
1567 GFile *
1568 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1570 EmpathyFTHandlerPriv *priv;
1572 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1574 priv = GET_PRIV (handler);
1576 return priv->gfile;
1580 * empathy_ft_handler_get_use_hash:
1581 * @handler: an #EmpathyFTHandler
1583 * Returns whether @handler has checksumming enabled. This can depend on
1584 * the CM and the remote contact capabilities.
1586 * Return value: %TRUE if the handler has checksumming enabled,
1587 * %FALSE otherwise.
1589 gboolean
1590 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1592 EmpathyFTHandlerPriv *priv;
1594 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1596 priv = GET_PRIV (handler);
1598 return priv->use_hash;
1602 * empathy_ft_handler_is_incoming:
1603 * @handler: an #EmpathyFTHandler
1605 * Returns whether @handler is incoming or outgoing.
1607 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1609 gboolean
1610 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1612 EmpathyFTHandlerPriv *priv;
1614 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1616 priv = GET_PRIV (handler);
1618 if (priv->channel == NULL)
1619 return FALSE;
1621 return !tp_channel_get_requested ((TpChannel *) priv->channel);
1625 * empathy_ft_handler_get_transferred_bytes:
1626 * @handler: an #EmpathyFTHandler
1628 * Returns the number of bytes already transferred by the handler.
1630 * Return value: the number of bytes already transferred by the handler.
1632 guint64
1633 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1635 EmpathyFTHandlerPriv *priv;
1637 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1639 priv = GET_PRIV (handler);
1641 return priv->transferred_bytes;
1645 * empathy_ft_handler_get_total_bytes:
1646 * @handler: an #EmpathyFTHandler
1648 * Returns the total size of the file being transferred by the handler.
1650 * Return value: a number of bytes indicating the total size of the file being
1651 * transferred by the handler.
1653 guint64
1654 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1656 EmpathyFTHandlerPriv *priv;
1658 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1660 priv = GET_PRIV (handler);
1662 return priv->total_bytes;
1666 * empathy_ft_handler_is_completed:
1667 * @handler: an #EmpathyFTHandler
1669 * Returns whether the transfer for @handler has been completed succesfully.
1671 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1672 * otherwise
1674 gboolean
1675 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1677 EmpathyFTHandlerPriv *priv;
1679 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1681 priv = GET_PRIV (handler);
1683 return priv->is_completed;
1687 * empathy_ft_handler_is_cancelled:
1688 * @handler: an #EmpathyFTHandler
1690 * Returns whether the transfer for @handler has been cancelled or has stopped
1691 * due to an error.
1693 * Return value: %TRUE if the transfer for @handler has been cancelled
1694 * or has stopped due to an error, %FALSE otherwise.
1696 gboolean
1697 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1699 EmpathyFTHandlerPriv *priv;
1701 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1703 priv = GET_PRIV (handler);
1705 return g_cancellable_is_cancelled (priv->cancellable);