]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-handler.c
Document EmpathyFTHandler
[empathy.git] / libempathy / empathy-ft-handler.c
1 /*
2  * empathy-ft-handler.c - Source for EmpathyFTHandler
3  * Copyright (C) 2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21  
22 /* empathy-ft-handler.c */
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <telepathy-glib/util.h>
27
28 #include "empathy-ft-handler.h"
29 #include "empathy-tp-contact-factory.h"
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-time.h"
33 #include "empathy-utils.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_FT
36 #include "empathy-debug.h"
37
38 /**
39  * SECTION:empathy-ft-handler
40  * @title: EmpathyFTHandler
41  * @short_description: an object representing a File Transfer
42  * @include: libempathy/empathy-ft-handler
43  *
44  * #EmpathyFTHandler is the object which represents a File Transfer with all
45  * its properties.
46  * The creation of an #EmpathyFTHandler is done with
47  * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
48  * even though clients should not need to call them directly, as
49  * #EmpathyFTFactory does it for them. Remember that for the file transfer
50  * to work with an incoming handler,
51  * empathy_ft_handler_incoming_set_destination() should be called after
52  * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
53  * automatically.
54  * It's important to note that, as the creation of the handlers is async, once
55  * an handler is created, it already has all the interesting properties set,
56  * like filename, total bytes, content type and so on, making it useful
57  * to be displayed in an UI.
58  * The transfer API works like a state machine; it has three signals,
59  * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
60  * emitted in the relevant phases.
61  * In addition, if the handler is created with checksumming enabled,
62  * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
63  * will be emitted before or after the transfer, depending on the direction
64  * (respectively outgoing and incoming) of the handler.
65  * At any time between the call to empathy_ft_handler_start_transfer() and
66  * the last signal, a ::transfer-error can be emitted, indicating that an 
67  * error has happened in the operation. The message of the error is localized
68  * to use in an UI.
69  */
70
71 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
72
73 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
74
75 #define BUFFER_SIZE 4096
76
77 enum {
78   PROP_TP_FILE = 1,
79   PROP_G_FILE,
80   PROP_CONTACT,
81   PROP_USE_HASH
82 };
83
84 enum {
85   HASHING_STARTED,
86   HASHING_PROGRESS,
87   HASHING_DONE,
88   TRANSFER_STARTED,
89   TRANSFER_PROGRESS,
90   TRANSFER_DONE,
91   TRANSFER_ERROR,
92   LAST_SIGNAL
93 };
94
95 typedef struct {
96   GInputStream *stream;
97   GError *error;
98   guchar *buffer;
99   GChecksum *checksum;
100   gssize total_read;
101   guint64 total_bytes;
102   EmpathyFTHandler *handler;
103 } HashingData;
104
105 typedef struct {
106   EmpathyFTHandlerReadyCallback callback;
107   gpointer user_data;
108   EmpathyFTHandler *handler;
109 } CallbacksData;
110
111 /* private data */
112 typedef struct {
113   gboolean dispose_run;
114
115   GFile *gfile;
116   EmpathyTpFile *tpfile;
117   GCancellable *cancellable;
118   gboolean use_hash;
119
120   /* request for the new transfer */
121   GHashTable *request;
122
123   /* transfer properties */
124   EmpathyContact *contact;
125   gchar *content_type;
126   gchar *filename;
127   gchar *description;
128   guint64 total_bytes;
129   guint64 transferred_bytes;
130   guint64 mtime;
131   gchar *content_hash;
132   TpFileHashType content_hash_type;
133   TpFileTransferState current_state;
134
135   /* time and speed */
136   gdouble speed;
137   guint remaining_time;
138   time_t last_update_time;
139
140   gboolean is_completed;
141 } EmpathyFTHandlerPriv;
142
143 static guint signals[LAST_SIGNAL] = { 0 };
144
145 static gboolean do_hash_job_incoming (GIOSchedulerJob *job,
146     GCancellable *cancellable, gpointer user_data);
147
148 /* GObject implementations */
149 static void
150 do_get_property (GObject *object,
151     guint property_id,
152     GValue *value,
153     GParamSpec *pspec)
154 {
155   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
156
157   switch (property_id)
158     {
159       case PROP_CONTACT:
160         g_value_set_object (value, priv->contact);
161         break;
162       case PROP_G_FILE:
163         g_value_set_object (value, priv->gfile);
164         break;
165       case PROP_TP_FILE:
166         g_value_set_object (value, priv->tpfile);
167         break;
168       case PROP_USE_HASH:
169         g_value_set_boolean (value, priv->use_hash);
170         break;
171       default:
172         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
173     }
174 }
175
176 static void
177 do_set_property (GObject *object,
178     guint property_id, 
179     const GValue *value,
180     GParamSpec *pspec)
181 {
182   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
183
184   switch (property_id)
185     {
186       case PROP_CONTACT:
187         priv->contact = g_value_dup_object (value);
188         break;
189       case PROP_G_FILE:
190         priv->gfile = g_value_dup_object (value);
191         break;
192       case PROP_TP_FILE:
193         priv->tpfile = g_value_dup_object (value);
194         break;
195       case PROP_USE_HASH:
196         priv->use_hash = g_value_get_boolean (value);
197         break;
198       default:
199         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
200     }
201 }
202
203 static void
204 do_dispose (GObject *object)
205 {
206   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
207
208   if (priv->dispose_run)
209     return;
210
211   priv->dispose_run = TRUE;
212
213   if (priv->contact != NULL) {
214     g_object_unref (priv->contact);
215     priv->contact = NULL;
216   }
217
218   if (priv->gfile != NULL) {
219     g_object_unref (priv->gfile);
220     priv->gfile = NULL;
221   }
222
223   if (priv->tpfile != NULL) {
224     empathy_tp_file_close (priv->tpfile);
225     g_object_unref (priv->tpfile);
226     priv->tpfile = NULL;
227   }
228
229   if (priv->cancellable != NULL) {
230     g_object_unref (priv->cancellable);
231     priv->cancellable = NULL;
232   }
233
234   if (priv->request != NULL)
235     {
236       g_hash_table_unref (priv->request);
237       priv->request = NULL;
238     }
239   
240   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
241 }
242
243 static void
244 do_finalize (GObject *object)
245 {
246   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
247
248   DEBUG ("%p", object);
249
250   g_free (priv->content_type);
251   priv->content_type = NULL;
252
253   g_free (priv->filename);
254   priv->filename = NULL;
255
256   g_free (priv->description);
257   priv->description = NULL;
258
259   g_free (priv->content_hash);
260   priv->content_hash = NULL;
261
262   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
263 }
264
265 static void
266 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
267 {
268   GObjectClass *object_class = G_OBJECT_CLASS (klass);
269   GParamSpec *param_spec;
270
271   g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
272
273   object_class->get_property = do_get_property;
274   object_class->set_property = do_set_property;
275   object_class->dispose = do_dispose;
276   object_class->finalize = do_finalize;
277
278   /* properties */
279
280   /**
281    * EmpathyFTHandler:contact:
282    *
283    * The remote #EmpathyContact for the transfer
284    */
285   param_spec = g_param_spec_object ("contact",
286     "contact", "The remote contact",
287     EMPATHY_TYPE_CONTACT,
288     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
289   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
290
291   /**
292    * EmpathyFTHandler:gfile:
293    *
294    * The #GFile object where the transfer actually happens
295    */
296   param_spec = g_param_spec_object ("gfile",
297     "gfile", "The GFile we're handling",
298     G_TYPE_FILE,
299     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
300   g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
301
302   /**
303    * EmpathyFTHandler:tp-file:
304    *
305    * The underlying #EmpathyTpFile managing the transfer
306    */
307   param_spec = g_param_spec_object ("tp-file",
308     "tp-file", "The file's channel wrapper",
309     EMPATHY_TYPE_TP_FILE,
310     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
311   g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
312
313   /**
314    * EmpathyFTHandler:use-hash:
315    *
316    * %TRUE if checksumming is enabled for the handler, %FALSE otherwise
317    */
318   param_spec = g_param_spec_boolean ("use-hash",
319     "use-hash", "Whether we should use checksum when sending or receiving",
320     FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
321   g_object_class_install_property (object_class, PROP_USE_HASH, param_spec);
322
323   /* signals */
324
325   /**
326    * EmpathyFTHandler::transfer-started
327    * @handler: the object which has received the signal
328    * @tp_file: the #EmpathyTpFile for which the transfer has started
329    *
330    * This signal is emitted when the actual transfer starts.
331    */
332   signals[TRANSFER_STARTED] =
333     g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
334         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
335         g_cclosure_marshal_VOID__OBJECT,
336         G_TYPE_NONE,
337         1, EMPATHY_TYPE_TP_FILE);
338
339   /**
340    * EmpathyFTHandler::transfer-done
341    * @handler: the object which has received the signal
342    * @tp_file: the #EmpathyTpFile for which the transfer has started
343    *
344    * This signal will be emitted when the actual transfer is completed
345    * successfully.
346    */
347   signals[TRANSFER_DONE] =
348     g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
349         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
350         g_cclosure_marshal_VOID__OBJECT,
351         G_TYPE_NONE,
352         1, EMPATHY_TYPE_TP_FILE);
353
354   /**
355    * EmpathyFTHandler::transfer-error
356    * @handler: the object which has received the signal
357    * @error: a #GError
358    *
359    * This signal can be emitted anytime between the call to
360    * empathy_ft_handler_start_transfer() and the last expected signal
361    * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
362    * signal coming from the handler, meaning that no other operation will
363    * take place after this signal.
364    */
365   signals[TRANSFER_ERROR] =
366     g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
367         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
368         g_cclosure_marshal_VOID__POINTER,
369         G_TYPE_NONE,
370         1, G_TYPE_POINTER);
371
372   /**
373    * EmpathyFTHandler::transfer-progress
374    * @handler: the object which has received the signal
375    * @current_bytes: the bytes currently transferred
376    * @total_bytes: the total bytes of the handler
377    * @remaining_time: the number of seconds remaining for the transfer
378    * to be completed
379    * @speed: the current speed of the transfer (in KB/s)
380    *
381    * This signal is emitted to notify clients of the progress of the 
382    * transfer.
383    */
384   signals[TRANSFER_PROGRESS] =
385     g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
386         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
387         _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
388         G_TYPE_NONE,
389         4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
390
391   /**
392    * EmpathyFTHandler::hashing-started
393    * @handler: the object which has received the signal
394    *
395    * This signal is emitted when the hashing operation of the handler
396    * is started. Note that this only happens if the handler is created
397    * with checksum enabled and, even if the option is set, is not
398    * guaranteed to happen for incoming handlers, as the CM might not
399    * support sending/receiving the file hash.
400    */
401   signals[HASHING_STARTED] =
402     g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
403         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
404         g_cclosure_marshal_VOID__VOID,
405         G_TYPE_NONE, 0);
406
407   /**
408    * EmpathyFTHandler::hashing-progress
409    * @handler: the object which has received the signal
410    * @current_bytes: the bytes currently hashed
411    * @total_bytes: the total bytes of the handler
412    *
413    * This signal is emitted to notify clients of the progress of the 
414    * hashing operation. 
415    */
416   signals[HASHING_PROGRESS] =
417     g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
418         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
419         _empathy_marshal_VOID__UINT64_UINT64,
420         G_TYPE_NONE,
421         2, G_TYPE_UINT64, G_TYPE_UINT64);
422
423   /**
424    * EmpathyFTHandler::hashing-done
425    * @handler: the object which has received the signal
426    *
427    * This signal is emitted when the hashing operation of the handler
428    * is completed.
429    */
430   signals[HASHING_DONE] =
431     g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
432         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
433         g_cclosure_marshal_VOID__VOID,
434         G_TYPE_NONE, 0);
435 }
436
437 static void
438 empathy_ft_handler_init (EmpathyFTHandler *self)
439 {
440   EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
441     EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
442
443   self->priv = priv;
444   priv->cancellable = g_cancellable_new ();
445 }
446
447 /* private functions */
448
449 static void
450 hash_data_free (HashingData *data)
451 {
452   if (data->buffer != NULL)
453     {
454       g_free (data->buffer);
455       data->buffer = NULL;
456     }
457
458   if (data->stream != NULL)
459     {
460       g_object_unref (data->stream);
461       data->stream = NULL;
462     }
463
464   if (data->checksum != NULL)
465     {
466       g_checksum_free (data->checksum);
467       data->checksum = NULL;
468     }
469
470   if (data->error != NULL)
471     {
472       g_error_free (data->error);
473       data->error = NULL;
474     }
475   if (data->handler != NULL)
476     {
477       g_object_unref (data->handler);
478       data->handler = NULL;
479     }
480
481   g_slice_free (HashingData, data);
482 }
483
484 static GChecksumType
485 tp_file_hash_to_g_checksum (TpFileHashType type)
486 {
487   GChecksumType retval;
488
489   switch (type)
490     {
491       case TP_FILE_HASH_TYPE_MD5:
492         retval = G_CHECKSUM_MD5;
493         break;
494       case TP_FILE_HASH_TYPE_SHA1:
495         retval = G_CHECKSUM_SHA1;
496         break;
497       case TP_FILE_HASH_TYPE_SHA256:
498         retval = G_CHECKSUM_SHA256;
499         break;
500       default:
501         g_assert_not_reached ();
502         break;
503     }
504
505   return retval;
506 }
507
508 static void
509 check_hash_incoming (EmpathyFTHandler *handler)
510 {
511   HashingData *hash_data;
512   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
513
514   if (!EMP_STR_EMPTY (priv->content_hash))
515     {
516       hash_data = g_slice_new0 (HashingData);
517       hash_data->total_bytes = priv->total_bytes;
518       hash_data->handler = g_object_ref (handler);
519       hash_data->checksum = g_checksum_new
520         (tp_file_hash_to_g_checksum (priv->content_hash_type));
521
522       g_signal_emit (handler, signals[HASHING_STARTED], 0);
523
524       g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL,
525                                G_PRIORITY_DEFAULT, priv->cancellable);
526     }
527 }
528
529 static void
530 emit_error_signal (EmpathyFTHandler *handler,
531     const GError *error)
532 {
533   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
534
535   if (!g_cancellable_is_cancelled (priv->cancellable))
536     g_cancellable_cancel (priv->cancellable);
537
538   g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
539 }
540
541 static void
542 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
543     const GError *error,
544     gpointer user_data)
545 {
546   EmpathyFTHandler *handler = user_data;
547   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
548
549   DEBUG ("Transfer operation callback, error %p", error);
550
551   if (error != NULL)
552     {
553       emit_error_signal (handler, error);
554     }
555   else 
556     {
557       priv->is_completed = TRUE;
558       g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
559
560       empathy_tp_file_close (tp_file);
561
562       if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
563         {
564           check_hash_incoming (handler);
565         }
566     }
567 }
568
569 static void
570 update_remaining_time_and_speed (EmpathyFTHandler *handler,
571     guint64 transferred_bytes)
572 {
573   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
574   time_t elapsed_time, current_time;
575   guint64 transferred, last_transferred_bytes;
576   gdouble speed;
577   gint remaining_time;
578
579   last_transferred_bytes = priv->transferred_bytes;
580   priv->transferred_bytes = transferred_bytes;
581
582   current_time = empathy_time_get_current ();
583   elapsed_time = current_time - priv->last_update_time;
584
585   if (elapsed_time >= 1)
586     {
587       transferred = transferred_bytes - last_transferred_bytes;
588       speed = (gdouble) transferred / (gdouble) elapsed_time;
589       remaining_time = (priv->total_bytes - transferred) / speed;
590       priv->speed = speed;
591       priv->remaining_time = remaining_time;
592       priv->last_update_time = current_time;
593     }
594 }
595
596 static void
597 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
598     guint64 transferred_bytes,
599     gpointer user_data)
600 {
601   EmpathyFTHandler *handler = user_data;
602   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
603
604   if (empathy_ft_handler_is_cancelled (handler))
605     return;
606
607   if (transferred_bytes == 0)
608     {
609       priv->last_update_time = empathy_time_get_current ();
610       g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
611     }
612
613   if (priv->transferred_bytes != transferred_bytes)
614     {
615       update_remaining_time_and_speed (handler, transferred_bytes);
616
617       g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
618           transferred_bytes, priv->total_bytes, priv->remaining_time,
619           priv->speed);
620     }
621 }
622
623 static void
624 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
625     const GError *error,
626     gpointer user_data)
627 {
628   EmpathyFTHandler *handler = user_data;
629   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
630   GError *my_error = (GError *) error;
631
632   DEBUG ("Dispatcher create channel CB");
633
634   if (my_error == NULL)
635     {
636       g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
637     }
638
639   if (my_error != NULL)
640     {
641       emit_error_signal (handler, my_error);
642
643       if (my_error != error)
644         g_clear_error (&my_error);
645
646       return;
647     }
648
649   priv->tpfile = g_object_ref
650       (empathy_dispatch_operation_get_channel_wrapper (operation));
651
652   empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
653       ft_transfer_progress_callback, handler,
654       ft_transfer_operation_callback, handler);
655
656   empathy_dispatch_operation_claim (operation);
657 }
658
659 static void
660 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
661 {
662   EmpathyDispatcher *dispatcher;
663   TpConnection *connection;
664   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
665
666   DEBUG ("Pushing request to the dispatcher");
667
668   dispatcher = empathy_dispatcher_dup_singleton ();
669   connection = empathy_contact_get_connection (priv->contact);
670
671   /* I want to own a reference to the request, and destroy it later */
672   empathy_dispatcher_create_channel (dispatcher, connection,
673       g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
674
675   g_object_unref (dispatcher);
676 }
677
678 static gboolean
679 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
680 {
681   EmpathyDispatcher *dispatcher;
682   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
683   TpConnection *connection;
684   GStrv allowed;
685   gboolean res = TRUE;
686
687   dispatcher = empathy_dispatcher_dup_singleton ();
688   connection = empathy_contact_get_connection (priv->contact);
689
690   allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
691       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
692
693   if (!tp_strv_contains ((const gchar * const *) allowed,
694       TP_IFACE_CHANNEL ".TargetHandle"))
695     res = FALSE;
696
697   g_object_unref (dispatcher);
698
699   return res;
700 }
701
702 static void
703 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
704 {
705   guint contact_handle;
706   GHashTable *request;
707   GValue *value;
708   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
709
710   request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
711             NULL, (GDestroyNotify) tp_g_value_slice_free);
712
713   contact_handle = empathy_contact_get_handle (priv->contact);
714
715   /* org.freedesktop.Telepathy.Channel.ChannelType */
716   value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
717   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
718
719   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
720   value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT);
721   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
722
723   /* org.freedesktop.Telepathy.Channel.TargetHandle */
724   value = tp_g_value_slice_new_uint (contact_handle);
725   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
726
727   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
728   value = tp_g_value_slice_new_string (priv->content_type);
729   g_hash_table_insert (request,
730       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
731
732   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
733   value = tp_g_value_slice_new_string (priv->filename);
734   g_hash_table_insert (request,
735       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
736
737   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
738   value = tp_g_value_slice_new_uint64 (priv->total_bytes);
739   g_hash_table_insert (request,
740       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
741
742   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
743   value = tp_g_value_slice_new_uint64 ((guint64) priv->mtime);
744   g_hash_table_insert (request,
745       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
746 }
747
748 static gboolean
749 hash_job_done (gpointer user_data)
750 {
751   HashingData *hash_data = user_data;
752   EmpathyFTHandler *handler = hash_data->handler;
753   EmpathyFTHandlerPriv *priv;
754   GError *error = NULL;
755   GValue *value;
756
757   DEBUG ("Closing stream after hashing.");
758
759   priv = GET_PRIV (handler);
760
761   if (hash_data->error != NULL)
762     {
763       error = hash_data->error;
764       goto cleanup;
765     }
766
767   DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum));
768
769   if (empathy_ft_handler_is_incoming (handler))
770     {
771       if (g_strcmp0 (g_checksum_get_string (hash_data->checksum),
772                      priv->content_hash))
773         {
774           DEBUG ("Hash mismatch when checking incoming handler: "
775                  "received %s, calculated %s", priv->content_hash,
776                  g_checksum_get_string (hash_data->checksum));
777
778           error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
779               EMPATHY_FT_ERROR_HASH_MISMATCH,
780               _("The hash of the received file and the "
781                 "sent one do not match"));
782           goto cleanup;
783         }
784       else
785         {
786           DEBUG ("Hash verification matched, received %s, calculated %s",
787                  priv->content_hash,
788                  g_checksum_get_string (hash_data->checksum));
789         }
790     }
791   else
792     {
793       /* set the checksum in the request...
794        * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
795        */
796       value = tp_g_value_slice_new_string
797           (g_checksum_get_string (hash_data->checksum));
798       g_hash_table_insert (priv->request,
799           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);      
800     }
801
802 cleanup:
803
804   if (error != NULL)
805     {
806       emit_error_signal (handler, error);
807       g_clear_error (&error);
808     }
809   else
810     {
811       g_signal_emit (handler, signals[HASHING_DONE], 0);
812
813       if (!empathy_ft_handler_is_incoming (handler))
814         /* the request is complete now, push it to the dispatcher */
815         ft_handler_push_to_dispatcher (handler);
816     }
817
818   hash_data_free (hash_data);
819
820   return FALSE;
821 }
822
823 static gboolean
824 emit_hashing_progress (gpointer user_data)
825 {
826   HashingData *hash_data = user_data;
827
828   g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
829       (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
830
831   return FALSE;
832 }
833
834 static gboolean
835 do_hash_job (GIOSchedulerJob *job,
836     GCancellable *cancellable,
837     gpointer user_data)
838 {
839   HashingData *hash_data = user_data;
840   gssize bytes_read;
841   EmpathyFTHandlerPriv *priv;
842   GError *error = NULL;
843
844   priv = GET_PRIV (hash_data->handler);
845
846 again:
847   if (hash_data->buffer == NULL)
848     hash_data->buffer = g_slice_alloc0 (BUFFER_SIZE);
849
850   bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
851                                     BUFFER_SIZE, cancellable, &error);
852   if (error != NULL)
853     goto out;
854
855   hash_data->total_read += bytes_read;
856
857   /* we now have the chunk */
858   if (bytes_read > 0)
859     {
860       g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
861       g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
862           hash_data, NULL);
863
864       g_slice_free (guchar, hash_data->buffer);
865       hash_data->buffer = NULL;
866
867       goto again;
868     }
869   else
870   {
871     g_input_stream_close (hash_data->stream, cancellable, &error);
872   }
873
874 out:
875   if (error != NULL)
876     hash_data->error = error;
877
878   g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
879       hash_data, NULL);
880
881   return FALSE;
882 }
883
884 static gboolean
885 do_hash_job_incoming (GIOSchedulerJob *job,
886     GCancellable *cancellable,
887     gpointer user_data)
888 {
889   HashingData *hash_data = user_data;
890   EmpathyFTHandler *handler = hash_data->handler;
891   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
892   GError *error = NULL;
893
894   DEBUG ("checking integrity for incoming handler");
895
896   /* need to get the stream first */
897   hash_data->stream =
898     G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
899
900   if (error != NULL)
901     {
902       hash_data->error = error;
903       g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
904           hash_data, NULL);
905       return FALSE;
906     }
907
908   return do_hash_job (job, cancellable, user_data);
909 }
910
911 static void
912 ft_handler_read_async_cb (GObject *source,
913     GAsyncResult *res,
914     gpointer user_data)
915 {
916   GFileInputStream *stream;
917   GError *error = NULL;
918   HashingData *hash_data;
919   GValue *value;
920   EmpathyFTHandler *handler = user_data;
921   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
922
923   DEBUG ("GFile read async CB.");
924
925   stream = g_file_read_finish (priv->gfile, res, &error);
926   if (error != NULL)
927     {
928       emit_error_signal (handler, error);
929       g_clear_error (&error);
930
931       return;
932     }
933
934   hash_data = g_slice_new0 (HashingData);
935   hash_data->stream = G_INPUT_STREAM (stream);
936   hash_data->total_bytes = priv->total_bytes;
937   hash_data->handler = g_object_ref (handler);
938   /* FIXME: should look at the CM capabilities before setting the
939    * checksum type?
940    */
941   hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
942
943   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
944   value = tp_g_value_slice_new_uint (TP_FILE_HASH_TYPE_MD5);
945   g_hash_table_insert (priv->request,
946       TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
947
948   g_signal_emit (handler, signals[HASHING_STARTED], 0);
949
950   g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
951       G_PRIORITY_DEFAULT, priv->cancellable);
952 }
953
954 static void
955 ft_handler_complete_request (EmpathyFTHandler *handler)
956
957   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
958   GError *myerr = NULL;
959
960   /* check if FT is allowed before firing up the I/O machinery */
961   if (!ft_handler_check_if_allowed (handler))
962     {
963       g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
964           EMPATHY_FT_ERROR_NOT_SUPPORTED,
965           _("File transfer not supported by remote contact"));
966
967       emit_error_signal (handler, myerr);
968       g_clear_error (&myerr);
969
970       return;
971     }
972
973   /* populate the request table with all the known properties */
974   ft_handler_populate_outgoing_request (handler);
975
976   if (priv->use_hash)
977     /* start hashing the file */
978     g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
979         priv->cancellable, ft_handler_read_async_cb, handler);
980   else
981     /* push directly the handler to the dispatcher */
982     ft_handler_push_to_dispatcher (handler);
983 }
984
985 static void
986 callbacks_data_free (gpointer user_data)
987 {
988   CallbacksData *data = user_data;
989
990   if (data->handler != NULL)
991     g_object_unref (data->handler);
992
993   g_slice_free (CallbacksData, data);
994 }
995
996 static void
997 ft_handler_gfile_ready_cb (GObject *source,
998     GAsyncResult *res,
999     CallbacksData *cb_data)
1000 {
1001   GFileInfo *info;
1002   GError *error = NULL;
1003   GTimeVal mtime;
1004   EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1005
1006   DEBUG ("Got GFileInfo.");
1007
1008   info = g_file_query_info_finish (priv->gfile, res, &error);
1009
1010   if (error != NULL)
1011     goto out;
1012
1013   priv->content_type = g_strdup (g_file_info_get_content_type (info));
1014   priv->filename = g_strdup (g_file_info_get_display_name (info));
1015   priv->total_bytes = g_file_info_get_size (info);
1016   g_file_info_get_modification_time (info, &mtime);
1017   priv->mtime = mtime.tv_sec;
1018   priv->transferred_bytes = 0;
1019   priv->description = NULL;
1020
1021   g_object_unref (info);
1022
1023 out:
1024   if (error == NULL)
1025     {
1026       cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
1027     }
1028   else
1029     {
1030       if (!g_cancellable_is_cancelled (priv->cancellable))
1031         g_cancellable_cancel (priv->cancellable);
1032
1033       cb_data->callback (NULL, error, cb_data->user_data);
1034       g_error_free (error);
1035       g_object_unref (cb_data->handler);
1036     }
1037
1038   callbacks_data_free (cb_data);
1039 }
1040
1041 static void 
1042 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
1043     EmpathyContact *contact,
1044     const GError *error,
1045     gpointer user_data,
1046     GObject *weak_object)
1047 {
1048   CallbacksData *cb_data = user_data;
1049   EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1050   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1051
1052   if (error != NULL)
1053     {
1054       if (!g_cancellable_is_cancelled (priv->cancellable))
1055         g_cancellable_cancel (priv->cancellable);
1056
1057       cb_data->callback (NULL, (GError *) error, cb_data->user_data);
1058       g_object_unref (handler);
1059       return;
1060     }
1061
1062   priv->contact = contact;
1063
1064   cb_data->callback (handler, NULL, cb_data->user_data);
1065 }
1066
1067 static void
1068 channel_get_all_properties_cb (TpProxy *proxy,
1069     GHashTable *properties,
1070     const GError *error,
1071     gpointer user_data,
1072     GObject *weak_object)
1073 {
1074   CallbacksData *cb_data = user_data;
1075   EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1076   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1077   EmpathyTpContactFactory *c_factory;
1078   TpHandle c_handle;
1079
1080   if (error != NULL)
1081     {
1082       if (!g_cancellable_is_cancelled (priv->cancellable))
1083         g_cancellable_cancel (priv->cancellable);
1084
1085       cb_data->callback (NULL, (GError *) error, cb_data->user_data);
1086       g_object_unref (handler);
1087       return;
1088     }
1089
1090   priv->total_bytes = g_value_get_uint64 (
1091       g_hash_table_lookup (properties, "Size"));
1092
1093   priv->transferred_bytes = g_value_get_uint64 (
1094       g_hash_table_lookup (properties, "TransferredBytes"));
1095
1096   priv->filename = g_value_dup_string (
1097       g_hash_table_lookup (properties, "Filename"));
1098
1099   priv->content_hash = g_value_dup_string (
1100       g_hash_table_lookup (properties, "ContentHash"));
1101
1102   priv->content_hash_type = g_value_get_uint (
1103       g_hash_table_lookup (properties, "ContentHashType"));
1104
1105   priv->content_type = g_value_dup_string (
1106       g_hash_table_lookup (properties, "ContentType"));
1107
1108   priv->description = g_value_dup_string (
1109       g_hash_table_lookup (properties, "Description"));
1110
1111   c_factory = empathy_tp_contact_factory_dup_singleton
1112       (tp_channel_borrow_connection (TP_CHANNEL (proxy)));
1113   c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
1114   empathy_tp_contact_factory_get_from_handle (c_factory, c_handle,
1115       contact_factory_contact_cb, cb_data, callbacks_data_free,
1116       G_OBJECT (handler));
1117
1118   g_object_unref (c_factory);
1119 }
1120
1121 /* public methods */
1122
1123 /**
1124  * empathy_ft_handler_new_outgoing:
1125  * @contact: the #EmpathyContact to send @source to
1126  * @source: the #GFile to send
1127  * @use_hash: whether the handler should send a checksum of the file
1128  * @callback: callback to be called when the handler has been created
1129  * @user_data: user data to be passed to @callback
1130  *
1131  * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1132  */
1133 void
1134 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1135     GFile *source,
1136     gboolean use_hash,
1137     EmpathyFTHandlerReadyCallback callback,
1138     gpointer user_data)
1139 {
1140   EmpathyFTHandler *handler;
1141   CallbacksData *data;
1142   EmpathyFTHandlerPriv *priv;
1143
1144   DEBUG ("New handler outgoing, use hash %s",
1145          use_hash ? "True" : "False");
1146
1147   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1148   g_return_if_fail (G_IS_FILE (source));
1149
1150   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1151       "contact", contact, "gfile", source, "use-hash", use_hash, NULL);
1152
1153   priv = GET_PRIV (handler);
1154
1155   data = g_slice_new0 (CallbacksData);
1156   data->callback = callback;
1157   data->user_data = user_data;
1158   data->handler = g_object_ref (handler);
1159
1160   /* start collecting info about the file */
1161   g_file_query_info_async (priv->gfile,
1162       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1163       G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1164       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1165       G_FILE_ATTRIBUTE_TIME_MODIFIED,
1166       G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1167       NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1168 }
1169
1170 /**
1171  * empathy_ft_handler_new_incoming:
1172  * @tp_file: the #EmpathyTpFile wrapping the incoming channel
1173  * @callback: callback to be called when the handler has been created
1174  * @user_data: user data to be passed to @callback
1175  *
1176  * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1177  * Note that for the handler to be useful, you will have to set a destination
1178  * file with empathy_ft_handler_incoming_set_destination() after the handler
1179  * is ready.
1180  */
1181 void
1182 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
1183     EmpathyFTHandlerReadyCallback callback,
1184     gpointer user_data)
1185 {
1186   EmpathyFTHandler *handler;
1187   TpChannel *channel;
1188   CallbacksData *data;
1189
1190   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1191
1192   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1193       "tp-file", tp_file, NULL);
1194
1195   g_object_get (tp_file, "channel", &channel, NULL);
1196
1197   data = g_slice_new0 (CallbacksData);
1198   data->callback = callback;
1199   data->user_data = user_data;
1200   data->handler = g_object_ref (handler);
1201
1202   tp_cli_dbus_properties_call_get_all (channel,
1203       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1204       channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1205 }
1206
1207 /**
1208  * empathy_ft_handler_start_transfer:
1209  * @handler: an #EmpathyFTHandler
1210  *
1211  * Starts the transfer machinery. After this call, the transfer and hashing
1212  * signals will be emitted by the handler.
1213  */
1214 void
1215 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1216 {
1217   EmpathyFTHandlerPriv *priv;
1218
1219   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1220
1221   priv = GET_PRIV (handler);
1222
1223   if (priv->tpfile == NULL)
1224     {
1225       ft_handler_complete_request (handler);
1226     }
1227   else
1228     {
1229       /* TODO: add support for resume. */
1230       empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
1231           ft_transfer_progress_callback, handler,
1232           ft_transfer_operation_callback, handler);
1233     }
1234 }
1235
1236 /**
1237  * empathy_ft_handler_cancel_transfer:
1238  * @handler: an #EmpathyFTHandler
1239  *
1240  * Cancels an ongoing handler operation. Note that this doesn't destroy
1241  * the object, which will keep all the properties, altough it won't be able
1242  * to do any more I/O.
1243  */
1244 void
1245 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1246 {
1247   EmpathyFTHandlerPriv *priv;
1248
1249   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1250
1251   priv = GET_PRIV (handler);
1252
1253   /* if we don't have an EmpathyTpFile, we are hashing, so
1254    * we can just cancel the GCancellable to stop it.
1255    */
1256   if (priv->tpfile == NULL)
1257     g_cancellable_cancel (priv->cancellable);
1258   else
1259     empathy_tp_file_cancel (priv->tpfile);
1260 }
1261
1262 /**
1263  * empathy_ft_handler_incoming_set_destination:
1264  * @handler: an #EmpathyFTHandler
1265  * @destination: the #GFile where the transfer should be saved
1266  * @use_hash: whether the handler should, after the transfer, try to
1267  * validate it with checksum.
1268  *
1269  * Sets the destination of the incoming handler to be @destination.
1270  * Note that calling this method is mandatory before starting the transfer
1271  * for incoming handlers.
1272  */
1273 void
1274 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1275     GFile *destination,
1276     gboolean use_hash)
1277 {
1278   DEBUG ("Set incoming destination, use hash %s",
1279          use_hash ? "True" : "False");
1280
1281   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1282   g_return_if_fail (G_IS_FILE (destination));
1283
1284   g_object_set (handler, "gfile", destination,
1285       "use-hash", use_hash, NULL);
1286 }
1287
1288 /**
1289  * empathy_ft_handler_get_filename:
1290  * @handler: an #EmpathyFTHandler
1291  *
1292  * Returns the name of the file being transferred.
1293  *
1294  * Return value: the name of the file being transferred
1295  */
1296 const char *
1297 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1298 {
1299   EmpathyFTHandlerPriv *priv;
1300
1301   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1302
1303   priv = GET_PRIV (handler);
1304
1305   return priv->filename;
1306 }
1307
1308 /**
1309  * empathy_ft_handler_get_content_type:
1310  * @handler: an #EmpathyFTHandler
1311  *
1312  * Returns the content type of the file being transferred.
1313  *
1314  * Return value: the content type of the file being transferred
1315  */
1316 const char *
1317 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1318 {
1319   EmpathyFTHandlerPriv *priv;
1320
1321   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1322
1323   priv = GET_PRIV (handler);
1324
1325   return priv->content_type;
1326 }
1327
1328 /**
1329  * empathy_ft_handler_get_contact:
1330  * @handler: an #EmpathyFTHandler
1331  *
1332  * Returns the remote #EmpathyContact at the other side of the transfer.
1333  *
1334  * Return value: the remote #EmpathyContact for @handler
1335  */
1336 EmpathyContact *
1337 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1338 {
1339   EmpathyFTHandlerPriv *priv;
1340
1341   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1342
1343   priv = GET_PRIV (handler);
1344
1345   return priv->contact;
1346 }
1347
1348 /**
1349  * empathy_ft_handler_get_gfile:
1350  * @handler: an #EmpathyFTHandler
1351  *
1352  * Returns the #GFile where the transfer is being read/saved.
1353  *
1354  * Return value: the #GFile where the transfer is being read/saved
1355  */
1356 GFile *
1357 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1358 {
1359   EmpathyFTHandlerPriv *priv;
1360
1361   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1362
1363   priv = GET_PRIV (handler);
1364
1365   return priv->gfile;
1366 }
1367
1368 /**
1369  * empathy_ft_handler_get_use_hash:
1370  * @handler: an #EmpathyFTHandler
1371  *
1372  * Returns whether @handler has checksumming enabled.
1373  *
1374  * Return value: %TRUE if the handler has checksumming enabled,
1375  * %FALSE otherwise.
1376  */
1377 gboolean
1378 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1379 {
1380   EmpathyFTHandlerPriv *priv;
1381
1382   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1383
1384   priv = GET_PRIV (handler);
1385
1386   return priv->use_hash;
1387 }
1388
1389 /**
1390  * empathy_ft_handler_is_incoming:
1391  * @handler: an #EmpathyFTHandler
1392  *
1393  * Returns whether @handler is incoming or outgoing.
1394  *
1395  * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1396  */
1397 gboolean
1398 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1399 {
1400   EmpathyFTHandlerPriv *priv;
1401
1402   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1403
1404   priv = GET_PRIV (handler);
1405
1406   if (priv->tpfile == NULL)
1407     return FALSE;
1408
1409   return empathy_tp_file_is_incoming (priv->tpfile);  
1410 }
1411
1412 /**
1413  * empathy_ft_handler_get_transferred_bytes:
1414  * @handler: an #EmpathyFTHandler
1415  *
1416  * Returns the number of bytes already transferred by the handler.
1417  *
1418  * Return value: the number of bytes already transferred by the handler.
1419  */
1420 guint64
1421 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1422 {
1423   EmpathyFTHandlerPriv *priv;
1424
1425   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1426
1427   priv = GET_PRIV (handler);
1428
1429   return priv->transferred_bytes;
1430 }
1431
1432 /**
1433  * empathy_ft_handler_get_total_bytes:
1434  * @handler: an #EmpathyFTHandler
1435  *
1436  * Returns the total size of the file being transferred by the handler.
1437  *
1438  * Return value: a number of bytes indicating the total size of the file being
1439  * transferred by the handler.
1440  */
1441 guint64
1442 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1443 {
1444   EmpathyFTHandlerPriv *priv;
1445
1446   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1447
1448   priv = GET_PRIV (handler);
1449
1450   return priv->total_bytes;
1451 }
1452
1453 /**
1454  * empathy_ft_handler_is_completed:
1455  * @handler: an #EmpathyFTHandler
1456  *
1457  * Returns whether the transfer for @handler has been completed succesfully.
1458  *
1459  * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1460  * otherwise
1461  */
1462 gboolean
1463 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1464 {
1465   EmpathyFTHandlerPriv *priv;
1466
1467   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1468
1469   priv = GET_PRIV (handler);
1470
1471   return priv->is_completed;
1472 }
1473
1474 /**
1475  * empathy_ft_handler_is_cancelled:
1476  * @handler: an #EmpathyFTHandler
1477  *
1478  * Returns whether the transfer for @handler has been cancelled or has stopped
1479  * due to an error.
1480  *
1481  * Return value: %TRUE if the transfer for @handler has been cancelled 
1482  * or has stopped due to an error, %FALSE otherwise.
1483  */
1484 gboolean
1485 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1486 {
1487   EmpathyFTHandlerPriv *priv;
1488
1489   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1490
1491   priv = GET_PRIV (handler);
1492
1493   return g_cancellable_is_cancelled (priv->cancellable);
1494 }