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