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