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