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