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