]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-handler.c
sort contacts by most recent event
[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_file_transfer_hash (priv->request,
911           TP_FILE_HASH_TYPE_MD5, g_checksum_get_string (hash_data->checksum));
912     }
913
914 cleanup:
915
916   if (error != NULL)
917     {
918       emit_error_signal (handler, error);
919       g_clear_error (&error);
920     }
921   else
922     {
923       g_signal_emit (handler, signals[HASHING_DONE], 0);
924
925       if (!empathy_ft_handler_is_incoming (handler))
926         /* the request is complete now, push it to the dispatcher */
927         ft_handler_push_to_dispatcher (handler);
928     }
929
930   hash_data_free (hash_data);
931
932   return FALSE;
933 }
934
935 static gboolean
936 emit_hashing_progress (gpointer user_data)
937 {
938   HashingData *hash_data = user_data;
939
940   g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
941       (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
942
943   return FALSE;
944 }
945
946 static gboolean
947 do_hash_job (GIOSchedulerJob *job,
948     GCancellable *cancellable,
949     gpointer user_data)
950 {
951   HashingData *hash_data = user_data;
952   gssize bytes_read;
953   GError *error = NULL;
954
955 again:
956   if (hash_data->buffer == NULL)
957     hash_data->buffer = g_malloc0 (BUFFER_SIZE);
958
959   bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer,
960                                     BUFFER_SIZE, cancellable, &error);
961   if (error != NULL)
962     goto out;
963
964   hash_data->total_read += bytes_read;
965
966   /* we now have the chunk */
967   if (bytes_read > 0)
968     {
969       g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
970       g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress,
971           hash_data, NULL);
972
973       g_free (hash_data->buffer);
974       hash_data->buffer = NULL;
975
976       goto again;
977     }
978   else
979   {
980     g_input_stream_close (hash_data->stream, cancellable, &error);
981   }
982
983 out:
984   if (error != NULL)
985     hash_data->error = error;
986
987   g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
988       hash_data, NULL);
989
990   return FALSE;
991 }
992
993 static gboolean
994 do_hash_job_incoming (GIOSchedulerJob *job,
995     GCancellable *cancellable,
996     gpointer user_data)
997 {
998   HashingData *hash_data = user_data;
999   EmpathyFTHandler *handler = hash_data->handler;
1000   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1001   GError *error = NULL;
1002
1003   DEBUG ("checking integrity for incoming handler");
1004
1005   /* need to get the stream first */
1006   hash_data->stream =
1007     G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
1008
1009   if (error != NULL)
1010     {
1011       hash_data->error = error;
1012       g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
1013           hash_data, NULL);
1014       return FALSE;
1015     }
1016
1017   return do_hash_job (job, cancellable, user_data);
1018 }
1019
1020 static void
1021 ft_handler_read_async_cb (GObject *source,
1022     GAsyncResult *res,
1023     gpointer user_data)
1024 {
1025   GFileInputStream *stream;
1026   GError *error = NULL;
1027   HashingData *hash_data;
1028   EmpathyFTHandler *handler = user_data;
1029   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1030
1031   DEBUG ("GFile read async CB.");
1032
1033   stream = g_file_read_finish (priv->gfile, res, &error);
1034   if (error != NULL)
1035     {
1036       emit_error_signal (handler, error);
1037       g_clear_error (&error);
1038
1039       return;
1040     }
1041
1042   hash_data = g_slice_new0 (HashingData);
1043   hash_data->stream = G_INPUT_STREAM (stream);
1044   hash_data->total_bytes = priv->total_bytes;
1045   hash_data->handler = g_object_ref (handler);
1046   /* FIXME: MD5 is the only ContentHashType supported right now */
1047   hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
1048
1049   g_signal_emit (handler, signals[HASHING_STARTED], 0);
1050
1051   g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
1052       G_PRIORITY_DEFAULT, priv->cancellable);
1053 }
1054
1055 static void
1056 callbacks_data_free (gpointer user_data)
1057 {
1058   CallbacksData *data = user_data;
1059
1060   if (data->handler != NULL)
1061     g_object_unref (data->handler);
1062
1063   g_slice_free (CallbacksData, data);
1064 }
1065
1066 static gboolean
1067 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
1068     GPtrArray *classes)
1069 {
1070   GArray *possible_values;
1071   guint value;
1072   gboolean valid;
1073   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1074   gboolean support_ft = FALSE;
1075   guint i;
1076
1077   possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
1078
1079   for (i = 0; i < classes->len; i++)
1080     {
1081       GHashTable *fixed;
1082       GStrv allowed;
1083       const gchar *chan_type;
1084
1085       tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
1086           &fixed, &allowed);
1087
1088       chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
1089
1090       if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1091         continue;
1092
1093       if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
1094           TP_HANDLE_TYPE_CONTACT)
1095         continue;
1096
1097       support_ft = TRUE;
1098
1099       value = tp_asv_get_uint32
1100         (fixed, TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
1101          &valid);
1102
1103       if (valid)
1104         g_array_append_val (possible_values, value);
1105     }
1106
1107   if (!support_ft)
1108     {
1109       g_array_unref (possible_values);
1110       return FALSE;
1111     }
1112
1113   if (possible_values->len == 0)
1114     {
1115       /* there are no channel classes with hash support, disable it. */
1116       priv->use_hash = FALSE;
1117       priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
1118
1119       goto out;
1120     }
1121
1122   priv->use_hash = TRUE;
1123
1124   if (possible_values->len == 1)
1125     {
1126       priv->content_hash_type = g_array_index (possible_values, guint, 0);
1127     }
1128   else
1129     {
1130       /* order the array and pick the first non zero, so that MD5
1131        * is the preferred value.
1132        */
1133       g_array_sort (possible_values, empathy_uint_compare);
1134
1135       if (g_array_index (possible_values, guint, 0) == 0)
1136         priv->content_hash_type = g_array_index (possible_values, guint, 1);
1137       else
1138         priv->content_hash_type = g_array_index (possible_values, guint, 0);
1139     }
1140
1141 out:
1142   g_array_unref (possible_values);
1143
1144   DEBUG ("Hash enabled %s; setting content hash type as %u",
1145          priv->use_hash ? "True" : "False", priv->content_hash_type);
1146
1147   return TRUE;
1148 }
1149
1150 static void
1151 check_hashing (CallbacksData *data)
1152 {
1153   EmpathyFTHandler *handler = data->handler;
1154   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1155   GError *myerr = NULL;
1156   TpCapabilities *caps;
1157   GPtrArray *classes;
1158   TpConnection *conn;
1159
1160   conn = empathy_contact_get_connection (priv->contact);
1161
1162   caps = tp_connection_get_capabilities (conn);
1163   if (caps == NULL)
1164     {
1165       data->callback (handler, NULL, data->user_data);
1166       goto out;
1167     }
1168
1169   classes = tp_capabilities_get_channel_classes (caps);
1170
1171   /* set whether we support hash and the type of it */
1172   if (!set_content_hash_type_from_classes (handler, classes))
1173     {
1174       g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
1175           EMPATHY_FT_ERROR_NOT_SUPPORTED,
1176           _("File transfer not supported by remote contact"));
1177
1178       if (!g_cancellable_is_cancelled (priv->cancellable))
1179         g_cancellable_cancel (priv->cancellable);
1180
1181       data->callback (handler, myerr, data->user_data);
1182       g_clear_error (&myerr);
1183     }
1184   else
1185     {
1186       /* get back to the caller now */
1187       data->callback (handler, NULL, data->user_data);
1188     }
1189
1190 out:
1191   callbacks_data_free (data);
1192 }
1193
1194 static void
1195 ft_handler_complete_request (EmpathyFTHandler *handler)
1196 {
1197   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1198
1199   /* populate the request table with all the known properties */
1200   ft_handler_populate_outgoing_request (handler);
1201
1202   if (priv->use_hash)
1203     /* start hashing the file */
1204     g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
1205         priv->cancellable, ft_handler_read_async_cb, handler);
1206   else
1207     /* push directly the handler to the dispatcher */
1208     ft_handler_push_to_dispatcher (handler);
1209 }
1210
1211 static void
1212 ft_handler_gfile_ready_cb (GObject *source,
1213     GAsyncResult *res,
1214     CallbacksData *cb_data)
1215 {
1216   GFileInfo *info;
1217   GError *error = NULL;
1218   GTimeVal mtime;
1219   EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
1220
1221   DEBUG ("Got GFileInfo.");
1222
1223   info = g_file_query_info_finish (priv->gfile, res, &error);
1224
1225   if (error != NULL)
1226     goto out;
1227
1228   if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
1229     {
1230       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1231           EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
1232           _("The selected file is not a regular file"));
1233       goto out;
1234     }
1235
1236   priv->total_bytes = g_file_info_get_size (info);
1237   if (priv->total_bytes == 0)
1238     {
1239       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
1240           EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
1241           _("The selected file is empty"));
1242       goto out;
1243     }
1244
1245   priv->content_type = g_strdup (g_file_info_get_content_type (info));
1246   priv->filename = g_strdup (g_file_info_get_display_name (info));
1247   g_file_info_get_modification_time (info, &mtime);
1248   priv->mtime = mtime.tv_sec;
1249   priv->transferred_bytes = 0;
1250   priv->description = NULL;
1251
1252   g_object_unref (info);
1253
1254 out:
1255   if (error != NULL)
1256     {
1257       if (!g_cancellable_is_cancelled (priv->cancellable))
1258         g_cancellable_cancel (priv->cancellable);
1259
1260       cb_data->callback (cb_data->handler, error, cb_data->user_data);
1261       g_error_free (error);
1262
1263       callbacks_data_free (cb_data);
1264     }
1265   else
1266     {
1267       /* see if FT/hashing are allowed */
1268       check_hashing (cb_data);
1269     }
1270 }
1271
1272 static void
1273 channel_get_all_properties_cb (TpProxy *proxy,
1274     GHashTable *properties,
1275     const GError *error,
1276     gpointer user_data,
1277     GObject *weak_object)
1278 {
1279   CallbacksData *cb_data = user_data;
1280   EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
1281   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
1282   TpContact *contact;
1283
1284   if (error != NULL)
1285     {
1286       if (!g_cancellable_is_cancelled (priv->cancellable))
1287         g_cancellable_cancel (priv->cancellable);
1288
1289       cb_data->callback (handler, (GError *) error, cb_data->user_data);
1290
1291       callbacks_data_free (cb_data);
1292       return;
1293     }
1294
1295   priv->content_hash = g_value_dup_string (
1296       g_hash_table_lookup (properties, "ContentHash"));
1297
1298   priv->content_hash_type = g_value_get_uint (
1299       g_hash_table_lookup (properties, "ContentHashType"));
1300
1301   contact = tp_channel_get_target_contact (TP_CHANNEL (proxy));
1302   priv->contact = empathy_contact_dup_from_tp_contact (contact);
1303
1304   cb_data->callback (handler, NULL, cb_data->user_data);
1305 }
1306
1307 /* public methods */
1308
1309 /**
1310  * empathy_ft_handler_new_outgoing:
1311  * @contact: the #EmpathyContact to send @source to
1312  * @source: the #GFile to send
1313  * @callback: callback to be called when the handler has been created
1314  * @user_data: user data to be passed to @callback
1315  *
1316  * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
1317  */
1318 void
1319 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
1320     GFile *source,
1321     gint64 action_time,
1322     EmpathyFTHandlerReadyCallback callback,
1323     gpointer user_data)
1324 {
1325   EmpathyFTHandler *handler;
1326   CallbacksData *data;
1327   EmpathyFTHandlerPriv *priv;
1328
1329   DEBUG ("New handler outgoing");
1330
1331   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1332   g_return_if_fail (G_IS_FILE (source));
1333
1334   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1335       "contact", contact,
1336       "gfile", source,
1337       "user-action-time", action_time,
1338       NULL);
1339
1340   priv = GET_PRIV (handler);
1341
1342   data = g_slice_new0 (CallbacksData);
1343   data->callback = callback;
1344   data->user_data = user_data;
1345   data->handler = g_object_ref (handler);
1346
1347   /* start collecting info about the file */
1348   g_file_query_info_async (priv->gfile,
1349       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
1350       G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1351       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1352       G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1353       G_FILE_ATTRIBUTE_TIME_MODIFIED,
1354       G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
1355       NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
1356 }
1357
1358 /**
1359  * empathy_ft_handler_new_incoming:
1360  * @channel: the #TpFileTransferChannel proxy to the incoming channel
1361  * @callback: callback to be called when the handler has been created
1362  * @user_data: user data to be passed to @callback
1363  *
1364  * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
1365  * Note that for the handler to be useful, you will have to set a destination
1366  * file with empathy_ft_handler_incoming_set_destination() after the handler
1367  * is ready.
1368  */
1369 void
1370 empathy_ft_handler_new_incoming (TpFileTransferChannel *channel,
1371     EmpathyFTHandlerReadyCallback callback,
1372     gpointer user_data)
1373 {
1374   EmpathyFTHandler *handler;
1375   CallbacksData *data;
1376   EmpathyFTHandlerPriv *priv;
1377
1378   g_return_if_fail (TP_IS_FILE_TRANSFER_CHANNEL (channel));
1379
1380   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
1381       "channel", channel, NULL);
1382
1383   priv = GET_PRIV (handler);
1384
1385   data = g_slice_new0 (CallbacksData);
1386   data->callback = callback;
1387   data->user_data = user_data;
1388   data->handler = g_object_ref (handler);
1389
1390   priv->total_bytes = tp_file_transfer_channel_get_size (channel);
1391
1392   priv->transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (
1393       channel);
1394
1395   priv->filename = g_strdup (tp_file_transfer_channel_get_filename (channel));
1396
1397   priv->content_type = g_strdup (tp_file_transfer_channel_get_mime_type (
1398       channel));
1399
1400   priv->description = g_strdup (tp_file_transfer_channel_get_description (
1401       channel));
1402
1403   tp_cli_dbus_properties_call_get_all (channel,
1404       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
1405       channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
1406 }
1407
1408 /**
1409  * empathy_ft_handler_start_transfer:
1410  * @handler: an #EmpathyFTHandler
1411  *
1412  * Starts the transfer machinery. After this call, the transfer and hashing
1413  * signals will be emitted by the handler.
1414  */
1415 void
1416 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
1417 {
1418   EmpathyFTHandlerPriv *priv;
1419
1420   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1421
1422   priv = GET_PRIV (handler);
1423
1424   if (priv->channel == NULL)
1425     {
1426       ft_handler_complete_request (handler);
1427     }
1428   else
1429     {
1430       /* TODO: add support for resume. */
1431       tp_file_transfer_channel_accept_file_async (priv->channel,
1432           priv->gfile, 0, ft_transfer_accept_cb, handler);
1433
1434       tp_g_signal_connect_object (priv->channel, "notify::state",
1435           G_CALLBACK (ft_transfer_state_cb), handler, 0);
1436       tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
1437           G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
1438     }
1439 }
1440
1441 /**
1442  * empathy_ft_handler_cancel_transfer:
1443  * @handler: an #EmpathyFTHandler
1444  *
1445  * Cancels an ongoing handler operation. Note that this doesn't destroy
1446  * the object, which will keep all the properties, altough it won't be able
1447  * to do any more I/O.
1448  */
1449 void
1450 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
1451 {
1452   EmpathyFTHandlerPriv *priv;
1453
1454   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1455
1456   priv = GET_PRIV (handler);
1457
1458   /* if we don't have a channel, we are hashing, so
1459    * we can just cancel the GCancellable to stop it.
1460    */
1461   if (priv->channel == NULL)
1462     g_cancellable_cancel (priv->cancellable);
1463   else
1464     tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, NULL);
1465 }
1466
1467 /**
1468  * empathy_ft_handler_incoming_set_destination:
1469  * @handler: an #EmpathyFTHandler
1470  * @destination: the #GFile where the transfer should be saved
1471  *
1472  * Sets the destination of the incoming handler to be @destination.
1473  * Note that calling this method is mandatory before starting the transfer
1474  * for incoming handlers.
1475  */
1476 void
1477 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
1478     GFile *destination)
1479 {
1480   EmpathyFTHandlerPriv *priv;
1481
1482   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1483   g_return_if_fail (G_IS_FILE (destination));
1484
1485   priv = GET_PRIV (handler);
1486
1487   g_object_set (handler, "gfile", destination, NULL);
1488
1489   /* check if hash is supported. if it isn't, set use_hash to FALSE
1490    * anyway, so that clients won't be expecting us to checksum.
1491    */
1492   if (TPAW_STR_EMPTY (priv->content_hash) ||
1493       priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
1494     priv->use_hash = FALSE;
1495   else
1496     priv->use_hash = TRUE;
1497 }
1498
1499 /**
1500  * empathy_ft_handler_get_filename:
1501  * @handler: an #EmpathyFTHandler
1502  *
1503  * Returns the name of the file being transferred.
1504  *
1505  * Return value: the name of the file being transferred
1506  */
1507 const char *
1508 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
1509 {
1510   EmpathyFTHandlerPriv *priv;
1511
1512   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1513
1514   priv = GET_PRIV (handler);
1515
1516   return priv->filename;
1517 }
1518
1519 /**
1520  * empathy_ft_handler_get_content_type:
1521  * @handler: an #EmpathyFTHandler
1522  *
1523  * Returns the content type of the file being transferred.
1524  *
1525  * Return value: the content type of the file being transferred
1526  */
1527 const char *
1528 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
1529 {
1530   EmpathyFTHandlerPriv *priv;
1531
1532   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1533
1534   priv = GET_PRIV (handler);
1535
1536   return priv->content_type;
1537 }
1538
1539 /**
1540  * empathy_ft_handler_get_contact:
1541  * @handler: an #EmpathyFTHandler
1542  *
1543  * Returns the remote #EmpathyContact at the other side of the transfer.
1544  *
1545  * Return value: the remote #EmpathyContact for @handler
1546  */
1547 EmpathyContact *
1548 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
1549 {
1550   EmpathyFTHandlerPriv *priv;
1551
1552   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1553
1554   priv = GET_PRIV (handler);
1555
1556   return priv->contact;
1557 }
1558
1559 /**
1560  * empathy_ft_handler_get_gfile:
1561  * @handler: an #EmpathyFTHandler
1562  *
1563  * Returns the #GFile where the transfer is being read/saved.
1564  *
1565  * Return value: the #GFile where the transfer is being read/saved
1566  */
1567 GFile *
1568 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
1569 {
1570   EmpathyFTHandlerPriv *priv;
1571
1572   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL);
1573
1574   priv = GET_PRIV (handler);
1575
1576   return priv->gfile;
1577 }
1578
1579 /**
1580  * empathy_ft_handler_get_use_hash:
1581  * @handler: an #EmpathyFTHandler
1582  *
1583  * Returns whether @handler has checksumming enabled. This can depend on
1584  * the CM and the remote contact capabilities.
1585  *
1586  * Return value: %TRUE if the handler has checksumming enabled,
1587  * %FALSE otherwise.
1588  */
1589 gboolean
1590 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
1591 {
1592   EmpathyFTHandlerPriv *priv;
1593
1594   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1595
1596   priv = GET_PRIV (handler);
1597
1598   return priv->use_hash;
1599 }
1600
1601 /**
1602  * empathy_ft_handler_is_incoming:
1603  * @handler: an #EmpathyFTHandler
1604  *
1605  * Returns whether @handler is incoming or outgoing.
1606  *
1607  * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
1608  */
1609 gboolean
1610 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
1611 {
1612   EmpathyFTHandlerPriv *priv;
1613
1614   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1615
1616   priv = GET_PRIV (handler);
1617
1618   if (priv->channel == NULL)
1619     return FALSE;
1620
1621   return !tp_channel_get_requested ((TpChannel *) priv->channel);
1622 }
1623
1624 /**
1625  * empathy_ft_handler_get_transferred_bytes:
1626  * @handler: an #EmpathyFTHandler
1627  *
1628  * Returns the number of bytes already transferred by the handler.
1629  *
1630  * Return value: the number of bytes already transferred by the handler.
1631  */
1632 guint64
1633 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
1634 {
1635   EmpathyFTHandlerPriv *priv;
1636
1637   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1638
1639   priv = GET_PRIV (handler);
1640
1641   return priv->transferred_bytes;
1642 }
1643
1644 /**
1645  * empathy_ft_handler_get_total_bytes:
1646  * @handler: an #EmpathyFTHandler
1647  *
1648  * Returns the total size of the file being transferred by the handler.
1649  *
1650  * Return value: a number of bytes indicating the total size of the file being
1651  * transferred by the handler.
1652  */
1653 guint64
1654 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
1655 {
1656   EmpathyFTHandlerPriv *priv;
1657
1658   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0);
1659
1660   priv = GET_PRIV (handler);
1661
1662   return priv->total_bytes;
1663 }
1664
1665 /**
1666  * empathy_ft_handler_is_completed:
1667  * @handler: an #EmpathyFTHandler
1668  *
1669  * Returns whether the transfer for @handler has been completed succesfully.
1670  *
1671  * Return value: %TRUE if the handler has been transferred correctly, %FALSE
1672  * otherwise
1673  */
1674 gboolean
1675 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
1676 {
1677   EmpathyFTHandlerPriv *priv;
1678
1679   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1680
1681   priv = GET_PRIV (handler);
1682
1683   return priv->is_completed;
1684 }
1685
1686 /**
1687  * empathy_ft_handler_is_cancelled:
1688  * @handler: an #EmpathyFTHandler
1689  *
1690  * Returns whether the transfer for @handler has been cancelled or has stopped
1691  * due to an error.
1692  *
1693  * Return value: %TRUE if the transfer for @handler has been cancelled
1694  * or has stopped due to an error, %FALSE otherwise.
1695  */
1696 gboolean
1697 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
1698 {
1699   EmpathyFTHandlerPriv *priv;
1700
1701   g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE);
1702
1703   priv = GET_PRIV (handler);
1704
1705   return g_cancellable_is_cancelled (priv->cancellable);
1706 }