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