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