Updated Hungarian translation
[empathy-mirror.git] / libempathy / empathy-ft-handler.c
blobfe1583eacbc1f3dcb67223e5307c19db1643e915
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 GHashTable *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 if (priv->request != NULL)
278 g_hash_table_unref (priv->request);
279 priv->request = NULL;
282 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
285 static void
286 do_finalize (GObject *object)
288 EmpathyFTHandlerPriv *priv = GET_PRIV (object);
290 DEBUG ("%p", object);
292 g_free (priv->content_type);
293 priv->content_type = NULL;
295 g_free (priv->filename);
296 priv->filename = NULL;
298 g_free (priv->description);
299 priv->description = NULL;
301 g_free (priv->content_hash);
302 priv->content_hash = NULL;
304 G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
307 static void
308 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
310 GObjectClass *object_class = G_OBJECT_CLASS (klass);
311 GParamSpec *param_spec;
313 g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
315 object_class->get_property = do_get_property;
316 object_class->set_property = do_set_property;
317 object_class->dispose = do_dispose;
318 object_class->finalize = do_finalize;
320 /* properties */
323 * EmpathyFTHandler:contact:
325 * The remote #EmpathyContact for the transfer
327 param_spec = g_param_spec_object ("contact",
328 "contact", "The remote contact",
329 EMPATHY_TYPE_CONTACT,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
331 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
334 * EmpathyFTHandler:content-type:
336 * The content type of the file being transferred
338 param_spec = g_param_spec_string ("content-type",
339 "content-type", "The content type of the file", NULL,
340 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
341 g_object_class_install_property (object_class,
342 PROP_CONTENT_TYPE, param_spec);
345 * EmpathyFTHandler:description:
347 * The description of the file being transferred
349 param_spec = g_param_spec_string ("description",
350 "description", "The description of the file", NULL,
351 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
352 g_object_class_install_property (object_class,
353 PROP_DESCRIPTION, param_spec);
356 * EmpathyFTHandler:filename:
358 * The name of the file being transferred
360 param_spec = g_param_spec_string ("filename",
361 "filename", "The name of the file", NULL,
362 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
363 g_object_class_install_property (object_class,
364 PROP_FILENAME, param_spec);
367 * EmpathyFTHandler:modification-time:
369 * The modification time of the file being transferred
371 param_spec = g_param_spec_uint64 ("modification-time",
372 "modification-time", "The mtime of the file", 0,
373 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
374 g_object_class_install_property (object_class,
375 PROP_MODIFICATION_TIME, param_spec);
378 * EmpathyFTHandler:total-bytes:
380 * The size (in bytes) of the file being transferred
382 param_spec = g_param_spec_uint64 ("total-bytes",
383 "total-bytes", "The size of the file", 0,
384 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
385 g_object_class_install_property (object_class,
386 PROP_TOTAL_BYTES, param_spec);
389 * EmpathyFTHandler:transferred-bytes:
391 * The number of the bytes already transferred
393 param_spec = g_param_spec_uint64 ("transferred-bytes",
394 "transferred-bytes", "The number of bytes already transferred", 0,
395 G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
396 g_object_class_install_property (object_class,
397 PROP_TRANSFERRED_BYTES, param_spec);
400 * EmpathyFTHandler:gfile:
402 * The #GFile object where the transfer actually happens
404 param_spec = g_param_spec_object ("gfile",
405 "gfile", "The GFile we're handling",
406 G_TYPE_FILE,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
408 g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
411 * EmpathyFTHandler:channel:
413 * The underlying #TpFileTransferChannel managing the transfer
415 param_spec = g_param_spec_object ("channel",
416 "channel", "The file transfer channel",
417 TP_TYPE_FILE_TRANSFER_CHANNEL,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
419 g_object_class_install_property (object_class, PROP_CHANNEL, param_spec);
421 param_spec = g_param_spec_int64 ("user-action-time", "user action time",
422 "User action time",
423 0, G_MAXINT64, 0,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
425 g_object_class_install_property (object_class, PROP_USER_ACTION_TIME,
426 param_spec);
428 /* signals */
431 * EmpathyFTHandler::transfer-started
432 * @handler: the object which has received the signal
433 * @channel: the #TpFileTransferChannel for which the transfer has started
435 * This signal is emitted when the actual transfer starts.
437 signals[TRANSFER_STARTED] =
438 g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
439 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
440 g_cclosure_marshal_generic,
441 G_TYPE_NONE,
442 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
445 * EmpathyFTHandler::transfer-done
446 * @handler: the object which has received the signal
447 * @channel: the #TpFileTransferChannel for which the transfer has started
449 * This signal will be emitted when the actual transfer is completed
450 * successfully.
452 signals[TRANSFER_DONE] =
453 g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
454 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
455 g_cclosure_marshal_generic,
456 G_TYPE_NONE,
457 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
460 * EmpathyFTHandler::transfer-error
461 * @handler: the object which has received the signal
462 * @error: a #GError
464 * This signal can be emitted anytime between the call to
465 * empathy_ft_handler_start_transfer() and the last expected signal
466 * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
467 * signal coming from the handler, meaning that no other operation will
468 * take place after this signal.
470 signals[TRANSFER_ERROR] =
471 g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
472 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
473 g_cclosure_marshal_generic,
474 G_TYPE_NONE,
475 1, G_TYPE_POINTER);
478 * EmpathyFTHandler::transfer-progress
479 * @handler: the object which has received the signal
480 * @current_bytes: the bytes currently transferred
481 * @total_bytes: the total bytes of the handler
482 * @remaining_time: the number of seconds remaining for the transfer
483 * to be completed
484 * @speed: the current speed of the transfer (in KB/s)
486 * This signal is emitted to notify clients of the progress of the
487 * transfer.
489 signals[TRANSFER_PROGRESS] =
490 g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
491 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
492 g_cclosure_marshal_generic,
493 G_TYPE_NONE,
494 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
497 * EmpathyFTHandler::hashing-started
498 * @handler: the object which has received the signal
500 * This signal is emitted when the hashing operation of the handler
501 * is started. Note that this might happen or not, depending on the CM
502 * and remote contact capabilities. Clients shoud use
503 * empathy_ft_handler_get_use_hash() before calling
504 * empathy_ft_handler_start_transfer() to know whether they should connect
505 * to this signal.
507 signals[HASHING_STARTED] =
508 g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
509 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
510 g_cclosure_marshal_generic,
511 G_TYPE_NONE, 0);
514 * EmpathyFTHandler::hashing-progress
515 * @handler: the object which has received the signal
516 * @current_bytes: the bytes currently hashed
517 * @total_bytes: the total bytes of the handler
519 * This signal is emitted to notify clients of the progress of the
520 * hashing operation.
522 signals[HASHING_PROGRESS] =
523 g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
524 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
525 g_cclosure_marshal_generic,
526 G_TYPE_NONE,
527 2, G_TYPE_UINT64, G_TYPE_UINT64);
530 * EmpathyFTHandler::hashing-done
531 * @handler: the object which has received the signal
533 * This signal is emitted when the hashing operation of the handler
534 * is completed.
536 signals[HASHING_DONE] =
537 g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
538 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
539 g_cclosure_marshal_generic,
540 G_TYPE_NONE, 0);
543 static void
544 empathy_ft_handler_init (EmpathyFTHandler *self)
546 EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
547 EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
549 self->priv = priv;
550 priv->cancellable = g_cancellable_new ();
553 /* private functions */
555 static void
556 hash_data_free (HashingData *data)
558 g_free (data->buffer);
560 if (data->stream != NULL)
561 g_object_unref (data->stream);
563 if (data->checksum != NULL)
564 g_checksum_free (data->checksum);
566 if (data->error != NULL)
567 g_error_free (data->error);
569 if (data->handler != NULL)
570 g_object_unref (data->handler);
572 g_slice_free (HashingData, data);
575 static GChecksumType
576 tp_file_hash_to_g_checksum (TpFileHashType type)
578 GChecksumType retval;
580 switch (type)
582 case TP_FILE_HASH_TYPE_MD5:
583 retval = G_CHECKSUM_MD5;
584 break;
585 case TP_FILE_HASH_TYPE_SHA1:
586 retval = G_CHECKSUM_SHA1;
587 break;
588 case TP_FILE_HASH_TYPE_SHA256:
589 retval = G_CHECKSUM_SHA256;
590 break;
591 case TP_FILE_HASH_TYPE_NONE:
592 default:
593 g_assert_not_reached ();
594 break;
597 return retval;
600 static void
601 check_hash_incoming (EmpathyFTHandler *handler)
603 HashingData *hash_data;
604 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
606 if (!TPAW_STR_EMPTY (priv->content_hash))
608 hash_data = g_slice_new0 (HashingData);
609 hash_data->total_bytes = priv->total_bytes;
610 hash_data->handler = g_object_ref (handler);
611 hash_data->checksum = g_checksum_new
612 (tp_file_hash_to_g_checksum (priv->content_hash_type));
614 g_signal_emit (handler, signals[HASHING_STARTED], 0);
616 g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
617 G_PRIORITY_DEFAULT, priv->cancellable);
621 static void
622 emit_error_signal (EmpathyFTHandler *handler,
623 const GError *error)
625 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
627 DEBUG ("Error in transfer: %s\n", error->message);
629 if (!g_cancellable_is_cancelled (priv->cancellable))
630 g_cancellable_cancel (priv->cancellable);
632 g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
635 static void
636 update_remaining_time_and_speed (EmpathyFTHandler *handler,
637 guint64 transferred_bytes)
639 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
640 gint64 elapsed_time, current_time;
641 guint64 transferred, last_transferred_bytes;
642 gdouble speed;
643 gint remaining_time;
645 last_transferred_bytes = priv->transferred_bytes;
646 priv->transferred_bytes = transferred_bytes;
648 current_time = tpaw_time_get_current ();
649 elapsed_time = current_time - priv->last_update_time;
651 if (elapsed_time >= 1)
653 transferred = transferred_bytes - last_transferred_bytes;
654 speed = (gdouble) transferred / (gdouble) elapsed_time;
655 remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
656 priv->speed = speed;
657 priv->remaining_time = remaining_time;
658 priv->last_update_time = current_time;
662 static void
663 ft_transfer_transferred_bytes_cb (TpFileTransferChannel *channel,
664 GParamSpec *pspec,
665 EmpathyFTHandler *handler)
667 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
668 guint64 bytes;
670 if (empathy_ft_handler_is_cancelled (handler))
671 return;
673 bytes = tp_file_transfer_channel_get_transferred_bytes (channel);
675 if (priv->transferred_bytes == 0)
677 priv->last_update_time = tpaw_time_get_current ();
678 g_signal_emit (handler, signals[TRANSFER_STARTED], 0, channel);
681 if (priv->transferred_bytes != bytes)
683 update_remaining_time_and_speed (handler, bytes);
685 g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
686 bytes, priv->total_bytes, priv->remaining_time,
687 priv->speed);
691 static void
692 ft_transfer_provide_cb (GObject *source,
693 GAsyncResult *result,
694 gpointer user_data)
696 TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
697 EmpathyFTHandler *handler = user_data;
698 GError *error = NULL;
700 if (!tp_file_transfer_channel_provide_file_finish (channel, result, &error))
702 emit_error_signal (handler, error);
703 g_clear_error (&error);
707 static void
708 ft_transfer_accept_cb (GObject *source,
709 GAsyncResult *result,
710 gpointer user_data)
712 TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
713 EmpathyFTHandler *handler = user_data;
714 GError *error = NULL;
716 if (!tp_file_transfer_channel_accept_file_finish (channel, result, &error))
718 emit_error_signal (handler, error);
719 g_clear_error (&error);
723 static GError *
724 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
726 const char *string;
727 GError *retval = NULL;
729 string = NULL;
731 switch (reason)
733 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
734 string = _("No reason was specified");
735 break;
736 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
737 string = _("The change in state was requested");
738 break;
739 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
740 string = _("You canceled the file transfer");
741 break;
742 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
743 string = _("The other participant canceled the file transfer");
744 break;
745 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
746 string = _("Error while trying to transfer the file");
747 break;
748 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
749 string = _("The other participant is unable to transfer the file");
750 break;
751 default:
752 string = _("Unknown reason");
753 break;
756 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
757 EMPATHY_FT_ERROR_TP_ERROR, string);
759 return retval;
762 static void
763 ft_transfer_state_cb (TpFileTransferChannel *channel,
764 GParamSpec *pspec,
765 EmpathyFTHandler *handler)
767 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
768 TpFileTransferStateChangeReason reason;
769 TpFileTransferState state = tp_file_transfer_channel_get_state (
770 channel, &reason);
772 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
774 priv->is_completed = TRUE;
775 g_signal_emit (handler, signals[TRANSFER_DONE], 0, channel);
777 tp_channel_close_async (TP_CHANNEL (channel), NULL, NULL);
779 if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
781 check_hash_incoming (handler);
784 else if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
786 GError *error = error_from_state_change_reason (reason);
787 emit_error_signal (handler, error);
788 g_clear_error (&error);
792 static void
793 ft_handler_create_channel_cb (GObject *source,
794 GAsyncResult *result,
795 gpointer user_data)
797 EmpathyFTHandler *handler = user_data;
798 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
799 GError *error = NULL;
800 TpChannel *channel;
802 DEBUG ("Dispatcher create channel CB");
804 channel = tp_account_channel_request_create_and_handle_channel_finish (
805 TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
807 if (channel == NULL)
808 DEBUG ("Failed to request FT channel: %s", error->message);
809 else
810 g_cancellable_set_error_if_cancelled (priv->cancellable, &error);
812 if (error != NULL)
814 emit_error_signal (handler, error);
816 g_clear_object (&channel);
817 g_error_free (error);
818 return;
821 priv->channel = TP_FILE_TRANSFER_CHANNEL (channel);
823 tp_g_signal_connect_object (priv->channel, "notify::state",
824 G_CALLBACK (ft_transfer_state_cb), handler, 0);
825 tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
826 G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
828 tp_file_transfer_channel_provide_file_async (priv->channel, priv->gfile,
829 ft_transfer_provide_cb, handler);
832 static void
833 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
835 TpAccount *account;
836 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
837 TpAccountChannelRequest *req;
839 DEBUG ("Pushing request to the dispatcher");
841 account = empathy_contact_get_account (priv->contact);
843 req = tp_account_channel_request_new (account, priv->request,
844 priv->user_action_time);
846 tp_account_channel_request_create_and_handle_channel_async (req, NULL,
847 ft_handler_create_channel_cb, handler);
849 g_object_unref (req);
852 static void
853 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
855 guint contact_handle;
856 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
857 gchar *uri;
859 contact_handle = empathy_contact_get_handle (priv->contact);
860 uri = g_file_get_uri (priv->gfile);
862 priv->request = tp_asv_new (
863 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
864 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
865 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
866 TP_HANDLE_TYPE_CONTACT,
867 TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
868 contact_handle,
869 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_TYPE, G_TYPE_STRING,
870 priv->content_type,
871 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_FILENAME, G_TYPE_STRING,
872 priv->filename,
873 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_SIZE, G_TYPE_UINT64,
874 priv->total_bytes,
875 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_DATE, G_TYPE_UINT64,
876 priv->mtime,
877 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_URI, G_TYPE_STRING, uri,
878 NULL);
880 g_free (uri);
883 static gboolean
884 hash_job_done (gpointer user_data)
886 HashingData *hash_data = user_data;
887 EmpathyFTHandler *handler = hash_data->handler;
888 EmpathyFTHandlerPriv *priv;
889 GError *error = NULL;
891 DEBUG ("Closing stream after hashing.");
893 priv = GET_PRIV (handler);
895 if (hash_data->error != NULL)
897 error = hash_data->error;
898 hash_data->error = NULL;
899 goto cleanup;
902 DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
904 if (empathy_ft_handler_is_incoming (handler))
906 if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
907 priv->content_hash))
909 DEBUG ("Hash mismatch when checking incoming handler: "
910 "received %s, calculated %s", priv->content_hash,
911 g_checksum_get_string (hash_data->checksum));
913 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
914 EMPATHY_FT_ERROR_HASH_MISMATCH,
915 _("File transfer completed, but the file was corrupted"));
916 goto cleanup;
918 else
920 DEBUG ("Hash verification matched, received %s, calculated %s",
921 priv->content_hash,
922 g_checksum_get_string (hash_data->checksum));
925 else
927 /* set the checksum in the request...
928 * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
930 tp_asv_set_string (priv->request,
931 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH,
932 g_checksum_get_string (hash_data->checksum));
935 cleanup:
937 if (error != NULL)
939 emit_error_signal (handler, error);
940 g_clear_error (&error);
942 else
944 g_signal_emit (handler, signals[HASHING_DONE], 0);
946 if (!empathy_ft_handler_is_incoming (handler))
947 /* the request is complete now, push it to the dispatcher */
948 ft_handler_push_to_dispatcher (handler);
951 hash_data_free (hash_data);
953 return FALSE;
956 static gboolean
957 emit_hashing_progress (gpointer user_data)
959 HashingData *hash_data = user_data;
961 g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
962 (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
964 return FALSE;
967 static gboolean
968 do_hash_job (GIOSchedulerJob *job,
969 GCancellable *cancellable,
970 gpointer user_data)
972 HashingData *hash_data = user_data;
973 gssize bytes_read;
974 GError *error = NULL;
976 again:
977 if (hash_data->buffer == NULL)
978 hash_data->buffer = g_malloc0 (BUFFER_SIZE);
980 bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
981 BUFFER_SIZE, cancellable, &error);
982 if (error != NULL)
983 goto out;
985 hash_data->total_read += bytes_read;
987 /* we now have the chunk */
988 if (bytes_read > 0)
990 g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
991 g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
992 hash_data, NULL);
994 g_free (hash_data->buffer);
995 hash_data->buffer = NULL;
997 goto again;
999 else
1001 g_input_stream_close (hash_data->stream, cancellable, &error);
1004 out:
1005 if (error != NULL)
1006 hash_data->error = error;
1008 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
1009 hash_data, NULL);
1011 return FALSE;
1014 static gboolean
1015 do_hash_job_incoming (GIOSchedulerJob *job,
1016 GCancellable *cancellable,
1017 gpointer user_data)
1019 HashingData *hash_data = user_data;
1020 EmpathyFTHandler *handler = hash_data->handler;
1021 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1022 GError *error = NULL;
1024 DEBUG ("checking integrity for incoming handler");
1026 /* need to get the stream first */
1027 hash_data->stream =
1028 G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
1030 if (error != NULL)
1032 hash_data->error = error;
1033 g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
1034 hash_data, NULL);
1035 return FALSE;
1038 return do_hash_job (job, cancellable, user_data);
1041 static void
1042 ft_handler_read_async_cb (GObject *source,
1043 GAsyncResult *res,
1044 gpointer user_data)
1046 GFileInputStream *stream;
1047 GError *error = NULL;
1048 HashingData *hash_data;
1049 EmpathyFTHandler *handler = user_data;
1050 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1052 DEBUG ("GFile read async CB.");
1054 stream = g_file_read_finish (priv->gfile, res, &error);
1055 if (error != NULL)
1057 emit_error_signal (handler, error);
1058 g_clear_error (&error);
1060 return;
1063 hash_data = g_slice_new0 (HashingData);
1064 hash_data->stream = G_INPUT_STREAM (stream);
1065 hash_data->total_bytes = priv->total_bytes;
1066 hash_data->handler = g_object_ref (handler);
1067 /* FIXME: MD5 is the only ContentHashType supported right now */
1068 hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
1070 tp_asv_set_uint32 (priv->request,
1071 TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
1072 TP_FILE_HASH_TYPE_MD5);
1074 g_signal_emit (handler, signals[HASHING_STARTED], 0);
1076 g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1077 G_PRIORITY_DEFAULT, priv->cancellable);
1080 static void
1081 callbacks_data_free (gpointer user_data)
1083 CallbacksData *data = user_data;
1085 if (data->handler != NULL)
1086 g_object_unref (data->handler);
1088 g_slice_free (CallbacksData, data);
1091 static gboolean
1092 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1093 GPtrArray *classes)
1095 GArray *possible_values;
1096 guint value;
1097 gboolean valid;
1098 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1099 gboolean support_ft = FALSE;
1100 guint i;
1102 possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1104 for (i = 0; i < classes->len; i++)
1106 GHashTable *fixed;
1107 GStrv allowed;
1108 const gchar *chan_type;
1110 tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
1111 &fixed, &allowed);
1113 chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
1115 if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1116 continue;
1118 if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
1119 TP_HANDLE_TYPE_CONTACT)
1120 continue;
1122 support_ft = TRUE;
1124 value = tp_asv_get_uint32
1125 (fixed, TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
1126 &valid);
1128 if (valid)
1129 g_array_append_val (possible_values, value);
1132 if (!support_ft)
1134 g_array_unref (possible_values);
1135 return FALSE;
1138 if (possible_values->len == 0)
1140 /* there are no channel classes with hash support, disable it. */
1141 priv->use_hash = FALSE;
1142 priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1144 goto out;
1147 priv->use_hash = TRUE;
1149 if (possible_values->len == 1)
1151 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1153 else
1155 /* order the array and pick the first non zero, so that MD5
1156 * is the preferred value.
1158 g_array_sort (possible_values, empathy_uint_compare);
1160 if (g_array_index (possible_values, guint, 0) == 0)
1161 priv->content_hash_type = g_array_index (possible_values, guint, 1);
1162 else
1163 priv->content_hash_type = g_array_index (possible_values, guint, 0);
1166 out:
1167 g_array_unref (possible_values);
1169 DEBUG ("Hash enabled %s; setting content hash type as %u",
1170 priv->use_hash ? "True" : "False", priv->content_hash_type);
1172 return TRUE;
1175 static void
1176 check_hashing (CallbacksData *data)
1178 EmpathyFTHandler *handler = data->handler;
1179 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1180 GError *myerr = NULL;
1181 TpCapabilities *caps;
1182 GPtrArray *classes;
1183 TpConnection *conn;
1185 conn = empathy_contact_get_connection (priv->contact);
1187 caps = tp_connection_get_capabilities (conn);
1188 if (caps == NULL)
1190 data->callback (handler, NULL, data->user_data);
1191 goto out;
1194 classes = tp_capabilities_get_channel_classes (caps);
1196 /* set whether we support hash and the type of it */
1197 if (!set_content_hash_type_from_classes (handler, classes))
1199 g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1200 EMPATHY_FT_ERROR_NOT_SUPPORTED,
1201 _("File transfer not supported by remote contact"));
1203 if (!g_cancellable_is_cancelled (priv->cancellable))
1204 g_cancellable_cancel (priv->cancellable);
1206 data->callback (handler, myerr, data->user_data);
1207 g_clear_error (&myerr);
1209 else
1211 /* get back to the caller now */
1212 data->callback (handler, NULL, data->user_data);
1215 out:
1216 callbacks_data_free (data);
1219 static void
1220 ft_handler_complete_request (EmpathyFTHandler *handler)
1222 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1224 /* populate the request table with all the known properties */
1225 ft_handler_populate_outgoing_request (handler);
1227 if (priv->use_hash)
1228 /* start hashing the file */
1229 g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1230 priv->cancellable, ft_handler_read_async_cb, handler);
1231 else
1232 /* push directly the handler to the dispatcher */
1233 ft_handler_push_to_dispatcher (handler);
1236 static void
1237 ft_handler_gfile_ready_cb (GObject *source,
1238 GAsyncResult *res,
1239 CallbacksData *cb_data)
1241 GFileInfo *info;
1242 GError *error = NULL;
1243 GTimeVal mtime;
1244 EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1246 DEBUG ("Got GFileInfo.");
1248 info = g_file_query_info_finish (priv->gfile, res, &error);
1250 if (error != NULL)
1251 goto out;
1253 if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1255 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1256 EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1257 _("The selected file is not a regular file"));
1258 goto out;
1261 priv->total_bytes = g_file_info_get_size (info);
1262 if (priv->total_bytes == 0)
1264 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1265 EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1266 _("The selected file is empty"));
1267 goto out;
1270 priv->content_type = g_strdup (g_file_info_get_content_type (info));
1271 priv->filename = g_strdup (g_file_info_get_display_name (info));
1272 g_file_info_get_modification_time (info, &mtime);
1273 priv->mtime = mtime.tv_sec;
1274 priv->transferred_bytes = 0;
1275 priv->description = NULL;
1277 g_object_unref (info);
1279 out:
1280 if (error != NULL)
1282 if (!g_cancellable_is_cancelled (priv->cancellable))
1283 g_cancellable_cancel (priv->cancellable);
1285 cb_data->callback (cb_data->handler, error, cb_data->user_data);
1286 g_error_free (error);
1288 callbacks_data_free (cb_data);
1290 else
1292 /* see if FT/hashing are allowed */
1293 check_hashing (cb_data);
1297 static void
1298 channel_get_all_properties_cb (TpProxy *proxy,
1299 GHashTable *properties,
1300 const GError *error,
1301 gpointer user_data,
1302 GObject *weak_object)
1304 CallbacksData *cb_data = user_data;
1305 EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1306 EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1307 TpContact *contact;
1309 if (error != NULL)
1311 if (!g_cancellable_is_cancelled (priv->cancellable))
1312 g_cancellable_cancel (priv->cancellable);
1314 cb_data->callback (handler, (GError *) error, cb_data->user_data);
1316 callbacks_data_free (cb_data);
1317 return;
1320 priv->content_hash = g_value_dup_string (
1321 g_hash_table_lookup (properties, "ContentHash"));
1323 priv->content_hash_type = g_value_get_uint (
1324 g_hash_table_lookup (properties, "ContentHashType"));
1326 contact = tp_channel_get_target_contact (TP_CHANNEL (proxy));
1327 priv->contact = empathy_contact_dup_from_tp_contact (contact);
1329 cb_data->callback (handler, NULL, cb_data->user_data);
1332 /* public methods */
1335 * empathy_ft_handler_new_outgoing:
1336 * @contact: the #EmpathyContact to send @source to
1337 * @source: the #GFile to send
1338 * @callback: callback to be called when the handler has been created
1339 * @user_data: user data to be passed to @callback
1341 * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1343 void
1344 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1345 GFile *source,
1346 gint64 action_time,
1347 EmpathyFTHandlerReadyCallback callback,
1348 gpointer user_data)
1350 EmpathyFTHandler *handler;
1351 CallbacksData *data;
1352 EmpathyFTHandlerPriv *priv;
1354 DEBUG ("New handler outgoing");
1356 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1357 g_return_if_fail (G_IS_FILE (source));
1359 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1360 "contact", contact,
1361 "gfile", source,
1362 "user-action-time", action_time,
1363 NULL);
1365 priv = GET_PRIV (handler);
1367 data = g_slice_new0 (CallbacksData);
1368 data->callback = callback;
1369 data->user_data = user_data;
1370 data->handler = g_object_ref (handler);
1372 /* start collecting info about the file */
1373 g_file_query_info_async (priv->gfile,
1374 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1375 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1376 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1377 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1378 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1379 G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1380 NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1384 * empathy_ft_handler_new_incoming:
1385 * @channel: the #TpFileTransferChannel proxy to the incoming channel
1386 * @callback: callback to be called when the handler has been created
1387 * @user_data: user data to be passed to @callback
1389 * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1390 * Note that for the handler to be useful, you will have to set a destination
1391 * file with empathy_ft_handler_incoming_set_destination() after the handler
1392 * is ready.
1394 void
1395 empathy_ft_handler_new_incoming (TpFileTransferChannel *channel,
1396 EmpathyFTHandlerReadyCallback callback,
1397 gpointer user_data)
1399 EmpathyFTHandler *handler;
1400 CallbacksData *data;
1401 EmpathyFTHandlerPriv *priv;
1403 g_return_if_fail (TP_IS_FILE_TRANSFER_CHANNEL (channel));
1405 handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1406 "channel", channel, NULL);
1408 priv = GET_PRIV (handler);
1410 data = g_slice_new0 (CallbacksData);
1411 data->callback = callback;
1412 data->user_data = user_data;
1413 data->handler = g_object_ref (handler);
1415 priv->total_bytes = tp_file_transfer_channel_get_size (channel);
1417 priv->transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (
1418 channel);
1420 priv->filename = g_strdup (tp_file_transfer_channel_get_filename (channel));
1422 priv->content_type = g_strdup (tp_file_transfer_channel_get_mime_type (
1423 channel));
1425 priv->description = g_strdup (tp_file_transfer_channel_get_description (
1426 channel));
1428 tp_cli_dbus_properties_call_get_all (channel,
1429 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1430 channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1434 * empathy_ft_handler_start_transfer:
1435 * @handler: an #EmpathyFTHandler
1437 * Starts the transfer machinery. After this call, the transfer and hashing
1438 * signals will be emitted by the handler.
1440 void
1441 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1443 EmpathyFTHandlerPriv *priv;
1445 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1447 priv = GET_PRIV (handler);
1449 if (priv->channel == NULL)
1451 ft_handler_complete_request (handler);
1453 else
1455 /* TODO: add support for resume. */
1456 tp_file_transfer_channel_accept_file_async (priv->channel,
1457 priv->gfile, 0, ft_transfer_accept_cb, handler);
1459 tp_g_signal_connect_object (priv->channel, "notify::state",
1460 G_CALLBACK (ft_transfer_state_cb), handler, 0);
1461 tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
1462 G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
1467 * empathy_ft_handler_cancel_transfer:
1468 * @handler: an #EmpathyFTHandler
1470 * Cancels an ongoing handler operation. Note that this doesn't destroy
1471 * the object, which will keep all the properties, altough it won't be able
1472 * to do any more I/O.
1474 void
1475 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1477 EmpathyFTHandlerPriv *priv;
1479 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1481 priv = GET_PRIV (handler);
1483 /* if we don't have a channel, we are hashing, so
1484 * we can just cancel the GCancellable to stop it.
1486 if (priv->channel == NULL)
1487 g_cancellable_cancel (priv->cancellable);
1488 else
1489 tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, NULL);
1493 * empathy_ft_handler_incoming_set_destination:
1494 * @handler: an #EmpathyFTHandler
1495 * @destination: the #GFile where the transfer should be saved
1497 * Sets the destination of the incoming handler to be @destination.
1498 * Note that calling this method is mandatory before starting the transfer
1499 * for incoming handlers.
1501 void
1502 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1503 GFile *destination)
1505 EmpathyFTHandlerPriv *priv;
1507 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1508 g_return_if_fail (G_IS_FILE (destination));
1510 priv = GET_PRIV (handler);
1512 g_object_set (handler, "gfile", destination, NULL);
1514 /* check if hash is supported. if it isn't, set use_hash to FALSE
1515 * anyway, so that clients won't be expecting us to checksum.
1517 if (TPAW_STR_EMPTY (priv->content_hash) ||
1518 priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1519 priv->use_hash = FALSE;
1520 else
1521 priv->use_hash = TRUE;
1525 * empathy_ft_handler_get_filename:
1526 * @handler: an #EmpathyFTHandler
1528 * Returns the name of the file being transferred.
1530 * Return value: the name of the file being transferred
1532 const char *
1533 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1535 EmpathyFTHandlerPriv *priv;
1537 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1539 priv = GET_PRIV (handler);
1541 return priv->filename;
1545 * empathy_ft_handler_get_content_type:
1546 * @handler: an #EmpathyFTHandler
1548 * Returns the content type of the file being transferred.
1550 * Return value: the content type of the file being transferred
1552 const char *
1553 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1555 EmpathyFTHandlerPriv *priv;
1557 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1559 priv = GET_PRIV (handler);
1561 return priv->content_type;
1565 * empathy_ft_handler_get_contact:
1566 * @handler: an #EmpathyFTHandler
1568 * Returns the remote #EmpathyContact at the other side of the transfer.
1570 * Return value: the remote #EmpathyContact for @handler
1572 EmpathyContact *
1573 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1575 EmpathyFTHandlerPriv *priv;
1577 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1579 priv = GET_PRIV (handler);
1581 return priv->contact;
1585 * empathy_ft_handler_get_gfile:
1586 * @handler: an #EmpathyFTHandler
1588 * Returns the #GFile where the transfer is being read/saved.
1590 * Return value: the #GFile where the transfer is being read/saved
1592 GFile *
1593 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1595 EmpathyFTHandlerPriv *priv;
1597 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1599 priv = GET_PRIV (handler);
1601 return priv->gfile;
1605 * empathy_ft_handler_get_use_hash:
1606 * @handler: an #EmpathyFTHandler
1608 * Returns whether @handler has checksumming enabled. This can depend on
1609 * the CM and the remote contact capabilities.
1611 * Return value: %TRUE if the handler has checksumming enabled,
1612 * %FALSE otherwise.
1614 gboolean
1615 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1617 EmpathyFTHandlerPriv *priv;
1619 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1621 priv = GET_PRIV (handler);
1623 return priv->use_hash;
1627 * empathy_ft_handler_is_incoming:
1628 * @handler: an #EmpathyFTHandler
1630 * Returns whether @handler is incoming or outgoing.
1632 * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1634 gboolean
1635 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1637 EmpathyFTHandlerPriv *priv;
1639 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1641 priv = GET_PRIV (handler);
1643 if (priv->channel == NULL)
1644 return FALSE;
1646 return !tp_channel_get_requested ((TpChannel *) priv->channel);
1650 * empathy_ft_handler_get_transferred_bytes:
1651 * @handler: an #EmpathyFTHandler
1653 * Returns the number of bytes already transferred by the handler.
1655 * Return value: the number of bytes already transferred by the handler.
1657 guint64
1658 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1660 EmpathyFTHandlerPriv *priv;
1662 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1664 priv = GET_PRIV (handler);
1666 return priv->transferred_bytes;
1670 * empathy_ft_handler_get_total_bytes:
1671 * @handler: an #EmpathyFTHandler
1673 * Returns the total size of the file being transferred by the handler.
1675 * Return value: a number of bytes indicating the total size of the file being
1676 * transferred by the handler.
1678 guint64
1679 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1681 EmpathyFTHandlerPriv *priv;
1683 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1685 priv = GET_PRIV (handler);
1687 return priv->total_bytes;
1691 * empathy_ft_handler_is_completed:
1692 * @handler: an #EmpathyFTHandler
1694 * Returns whether the transfer for @handler has been completed succesfully.
1696 * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1697 * otherwise
1699 gboolean
1700 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1702 EmpathyFTHandlerPriv *priv;
1704 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1706 priv = GET_PRIV (handler);
1708 return priv->is_completed;
1712 * empathy_ft_handler_is_cancelled:
1713 * @handler: an #EmpathyFTHandler
1715 * Returns whether the transfer for @handler has been cancelled or has stopped
1716 * due to an error.
1718 * Return value: %TRUE if the transfer for @handler has been cancelled
1719 * or has stopped due to an error, %FALSE otherwise.
1721 gboolean
1722 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1724 EmpathyFTHandlerPriv *priv;
1726 g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1728 priv = GET_PRIV (handler);
1730 return g_cancellable_is_cancelled (priv->cancellable);