]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-handler.c
Emit the transfer_started signal as soon as we start an incoming transfer.
[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 <extensions/extensions.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <telepathy-glib/util.h>
28
29 #include "empathy-ft-handler.h"
30 #include "empathy-contact-factory.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.h"
33 #include "empathy-utils.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_FT
36 #include "empathy-debug.h"
37
38 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
41
42 #define BUFFER_SIZE 4096
43
44 enum {
45   PROP_TP_FILE = 1,
46   PROP_G_FILE,
47   PROP_CONTACT
48 };
49
50 enum {
51   HASHING_STARTED,
52   HASHING_PROGRESS,
53   HASHING_DONE,
54   TRANSFER_STARTED,
55   TRANSFER_PROGRESS,
56   TRANSFER_DONE,
57   TRANSFER_ERROR,
58   LAST_SIGNAL
59 };
60
61 typedef struct {
62   GInputStream *stream;
63   gboolean done_reading;
64   GError *error;
65   guchar *buffer;
66   GChecksum *checksum;
67   gssize total_read;
68   guint64 total_bytes;
69   EmpathyFTHandler *handler;
70 } HashingData;
71
72 typedef struct {
73   EmpathyFTHandlerReadyCallback callback;
74   gpointer user_data;
75   EmpathyFTHandler *handler;
76 } CallbacksData;
77
78 /* private data */
79 typedef struct {
80   gboolean dispose_run;
81   GFile *gfile;
82   EmpathyTpFile *tpfile;
83   GCancellable *cancellable;
84
85   /* request for the new transfer */
86   GHashTable *request;
87
88   /* transfer properties */
89   EmpathyContact *contact;
90   gchar *content_type;
91   gchar *filename;
92   gchar *description;
93   guint64 total_bytes;
94   guint64 transferred_bytes;
95   guint64 mtime;
96   gchar *content_hash;
97   EmpFileHashType content_hash_type;
98 } EmpathyFTHandlerPriv;
99
100 static guint signals[LAST_SIGNAL] = { 0 };
101
102 /* prototypes */
103 static void schedule_hash_chunk (HashingData *hash_data);
104
105 /* GObject implementations */
106 static void
107 do_get_property (GObject *object,
108                  guint property_id,
109                  GValue *value,
110                  GParamSpec *pspec)
111 {
112   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
113
114   switch (property_id)
115     {
116       case PROP_CONTACT:
117         g_value_set_object (value, priv->contact);
118         break;
119       case PROP_G_FILE:
120         g_value_set_object (value, priv->gfile);
121         break;
122       case PROP_TP_FILE:
123         g_value_set_object (value, priv->tpfile);
124         break;
125       default:
126         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127     }
128 }
129
130 static void
131 do_set_property (GObject *object,
132                  guint property_id, 
133                  const GValue *value,
134                  GParamSpec *pspec)
135 {
136   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
137
138   switch (property_id)
139     {
140       case PROP_CONTACT:
141         priv->contact = g_value_dup_object (value);
142         break;
143       case PROP_G_FILE:
144         priv->gfile = g_value_dup_object (value);
145         break;
146       case PROP_TP_FILE:
147         priv->tpfile = g_value_dup_object (value);
148         break;
149       default:
150         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
151     }
152 }
153
154 static void
155 do_dispose (GObject *object)
156 {
157   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
158
159   if (priv->dispose_run)
160     return;
161
162   priv->dispose_run = TRUE;
163
164   if (priv->contact) {
165     g_object_unref (priv->contact);
166     priv->contact = NULL;
167   }
168
169   if (priv->gfile) {
170     g_object_unref (priv->gfile);
171     priv->gfile = NULL;
172   }
173
174   if (priv->tpfile) {
175     empathy_tp_file_close (priv->tpfile);
176     g_object_unref (priv->tpfile);
177     priv->tpfile = NULL;
178   }
179
180   if (priv->cancellable) {
181     g_object_unref (priv->cancellable);
182     priv->cancellable = NULL;
183   }
184   
185   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
186 }
187
188 static void
189 do_finalize (GObject *object)
190 {
191   EmpathyFTHandlerPriv *priv = GET_PRIV (object);
192
193   g_free (priv->content_type);
194   priv->content_type = NULL;
195
196   g_free (priv->filename);
197   priv->filename = NULL;
198
199   g_free (priv->description);
200   priv->description = NULL;
201
202   g_free (priv->content_hash);
203   priv->content_hash = NULL;
204
205   if (priv->request != NULL)
206     {
207       g_hash_table_destroy (priv->request);
208       priv->request = NULL;
209     }
210
211   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
212 }
213
214 static void
215 empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
216 {
217   GObjectClass *object_class = G_OBJECT_CLASS (klass);
218   GParamSpec *param_spec;
219
220   g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv));
221
222   object_class->get_property = do_get_property;
223   object_class->set_property = do_set_property;
224   object_class->dispose = do_dispose;
225   object_class->finalize = do_finalize;
226
227   /* properties */
228   param_spec = g_param_spec_object ("contact",
229     "contact", "The remote contact",
230     EMPATHY_TYPE_CONTACT,
231     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
232   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
233
234   param_spec = g_param_spec_object ("gfile",
235     "gfile", "The GFile we're handling",
236     G_TYPE_FILE,
237     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
238   g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
239
240   param_spec = g_param_spec_object ("tp-file",
241     "tp-file", "The file's channel wrapper",
242     EMPATHY_TYPE_TP_FILE,
243     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
244   g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
245
246   /* signals */
247   signals[TRANSFER_STARTED] =
248     g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
249         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
250         g_cclosure_marshal_VOID__OBJECT,
251         G_TYPE_NONE,
252         1, EMPATHY_TYPE_TP_FILE);
253
254   signals[TRANSFER_DONE] =
255     g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
256         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
257         g_cclosure_marshal_VOID__OBJECT,
258         G_TYPE_NONE,
259         1, EMPATHY_TYPE_TP_FILE);
260
261   signals[TRANSFER_ERROR] =
262     g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
263         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
264         g_cclosure_marshal_VOID__POINTER,
265         G_TYPE_NONE,
266         1, G_TYPE_POINTER);
267
268   signals[TRANSFER_PROGRESS] =
269     g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
270         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
271         _empathy_marshal_VOID__UINT64_UINT64,
272         G_TYPE_NONE,
273         2, G_TYPE_UINT64, G_TYPE_UINT64);
274
275   signals[HASHING_STARTED] =
276     g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
277         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
278         g_cclosure_marshal_VOID__VOID,
279         G_TYPE_NONE, 0);
280
281   signals[HASHING_PROGRESS] =
282     g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
283         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
284         _empathy_marshal_VOID__UINT64_UINT64,
285         G_TYPE_NONE,
286         2, G_TYPE_UINT64, G_TYPE_UINT64);
287
288   signals[HASHING_DONE] =
289     g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
290         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
291         g_cclosure_marshal_VOID__VOID,
292         G_TYPE_NONE, 0);
293 }
294
295 static void
296 empathy_ft_handler_init (EmpathyFTHandler *self)
297 {
298   EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
299     EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv);
300
301   self->priv = priv;
302 }
303
304 /* private functions */
305
306 static void
307 hash_data_free (HashingData *data)
308 {
309   if (data->buffer != NULL)
310     {
311       g_free (data->buffer);
312       data->buffer = NULL;
313     }
314
315   if (data->stream != NULL)
316     {
317       g_object_unref (data->stream);
318       data->stream = NULL;
319     }
320
321   if (data->checksum != NULL)
322     {
323       g_checksum_free (data->checksum);
324       data->checksum = NULL;
325     }
326
327   if (data->error != NULL)
328     {
329       g_error_free (data->error);
330       data->error = NULL;
331     }
332   if (data->handler != NULL)
333     {
334       g_object_unref (data->handler);
335       data->handler = NULL;
336     }
337
338   g_slice_free (HashingData, data);
339 }
340
341 static void
342 ft_transfer_operation_callback (EmpathyTpFile *tp_file,
343                                 const GError *error,
344                                 gpointer user_data)
345 {
346   EmpathyFTHandler *handler = user_data;
347
348   if (error != NULL)
349     g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
350   else
351     g_signal_emit (handler, signals[TRANSFER_DONE], 0);
352 }
353
354 static void
355 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
356                                guint64 transferred_bytes,
357                                gpointer user_data)
358 {
359   EmpathyFTHandler *handler = user_data;
360   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
361
362   if (priv->transferred_bytes != transferred_bytes)
363     {
364       priv->transferred_bytes = transferred_bytes;
365       g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
366           transferred_bytes, priv->total_bytes);
367     }
368 }
369
370 static void
371 ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
372                               const GError *error,
373                               gpointer user_data)
374 {
375   EmpathyFTHandler *handler = user_data;
376   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
377
378   DEBUG ("FT: dispatcher create channel CB");
379
380   if (error != NULL)
381     {
382       /* TODO: error handling */
383       return;
384     }
385
386   priv->tpfile = g_object_ref
387       (empathy_dispatch_operation_get_channel_wrapper (operation));
388   empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
389       ft_transfer_progress_callback, handler,
390       ft_transfer_operation_callback, handler);
391
392   empathy_dispatch_operation_claim (operation);
393 }
394
395 static void
396 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
397 {
398   EmpathyDispatcher *dispatcher;
399   McAccount *account;
400   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
401
402   DEBUG ("FT: pushing request to the dispatcher");
403
404   dispatcher = empathy_dispatcher_dup_singleton ();
405   account = empathy_contact_get_account (priv->contact);
406
407   empathy_dispatcher_create_channel (dispatcher, account, priv->request,
408       ft_handler_create_channel_cb, handler);
409
410   g_object_unref (dispatcher);
411 }
412
413 static gboolean
414 ft_handler_check_if_allowed (EmpathyFTHandler *handler)
415 {
416   EmpathyDispatcher *dispatcher;
417   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
418   McAccount *account;
419   GStrv allowed;
420   gboolean res = TRUE;
421
422   dispatcher = empathy_dispatcher_dup_singleton ();
423   account = empathy_contact_get_account (priv->contact);
424
425   allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
426       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
427
428   if (!tp_strv_contains ((const gchar * const *) allowed,
429       TP_IFACE_CHANNEL ".TargetHandle"))
430     res = FALSE;
431
432   g_object_unref (dispatcher);
433
434   return res;
435 }
436
437 static void
438 ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
439 {
440   guint contact_handle;
441   GHashTable *request;
442   GValue *value;
443   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
444
445   request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal,
446             NULL, (GDestroyNotify) tp_g_value_slice_free);
447
448   contact_handle = empathy_contact_get_handle (priv->contact);
449
450   /* org.freedesktop.Telepathy.Channel.ChannelType */
451   value = tp_g_value_slice_new (G_TYPE_STRING);
452   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
453   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
454
455   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
456   value = tp_g_value_slice_new (G_TYPE_UINT);
457   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
458   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
459
460   /* org.freedesktop.Telepathy.Channel.TargetHandle */
461   value = tp_g_value_slice_new (G_TYPE_UINT);
462   g_value_set_uint (value, contact_handle);
463   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
464
465   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
466   value = tp_g_value_slice_new (G_TYPE_STRING);
467   g_value_set_string (value, priv->content_type);
468   g_hash_table_insert (request,
469       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
470
471   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
472   value = tp_g_value_slice_new (G_TYPE_STRING);
473   g_value_set_string (value, priv->filename);
474   g_hash_table_insert (request,
475       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
476
477   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
478   value = tp_g_value_slice_new (G_TYPE_UINT64);
479   g_value_set_uint64 (value, (guint64) priv->total_bytes);
480   g_hash_table_insert (request,
481       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
482
483   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
484   value = tp_g_value_slice_new (G_TYPE_UINT64);
485   g_value_set_uint64 (value, (guint64) priv->mtime);
486   g_hash_table_insert (request,
487       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
488 }
489
490 static void
491 hash_job_async_close_stream_cb (GObject *source,
492                                 GAsyncResult *res,
493                                 gpointer user_data)
494 {
495   HashingData *hash_data = user_data;
496   EmpathyFTHandler *handler = hash_data->handler;
497   EmpathyFTHandlerPriv *priv;
498   GError *error = NULL;
499   GValue *value;
500
501   DEBUG ("FT: closing stream after hashing.");
502
503   priv = GET_PRIV (handler);
504
505   /* if we're here we for sure have done reading, check if we stopped due
506    * to an error.
507    */
508   g_input_stream_close_finish (hash_data->stream, res, &error);
509   if (error != NULL)
510     {
511       if (hash_data->error != NULL)
512         {
513           /* if we already stopped due to an error, probably we're completely
514            * hosed for some reason. just return the first read error
515            * to the user.
516            */
517           g_clear_error (&error);
518           error = hash_data->error;
519         }
520
521       goto cleanup;
522     }
523
524   if (hash_data->error != NULL)
525     {
526       error = hash_data->error;
527       goto cleanup;
528     }
529
530   /* set the checksum in the request */
531
532   DEBUG ("FT: got file hash %s", g_checksum_get_string (hash_data->checksum));
533
534   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash */
535   value = tp_g_value_slice_new (G_TYPE_STRING);
536   g_value_set_string (value, g_checksum_get_string (hash_data->checksum));
537   g_hash_table_insert (priv->request,
538       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
539
540 cleanup:
541   hash_data_free (hash_data);
542
543   if (error != NULL)
544     {
545       g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
546       g_clear_error (&error);
547     }
548   else
549     {
550       g_signal_emit (handler, signals[HASHING_DONE], 0);
551
552       /* the request is complete now, push it to the dispatcher */
553       ft_handler_push_to_dispatcher (handler);
554     }
555 }
556
557 static void
558 hash_job_async_read_cb (GObject *source,
559                         GAsyncResult *res,
560                         gpointer user_data)
561 {
562   HashingData *hash_data = user_data;
563   gssize bytes_read;
564   GError *error = NULL;
565
566   DEBUG ("FT: reading a chunk for hashing.");
567
568   bytes_read = g_input_stream_read_finish (hash_data->stream, res, &error);
569   if (error != NULL)
570     {
571       hash_data->error = error;
572       hash_data->done_reading = TRUE;
573       goto out;
574     }
575
576   hash_data->total_read += bytes_read;
577
578   /* we now have the chunk */
579   if (bytes_read == 0)
580     {
581       hash_data->done_reading = TRUE;
582       goto out;
583     }
584   else
585     {
586       g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read);
587       g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0,
588           (guint64) hash_data->total_read, (guint64) hash_data->total_bytes);
589     }
590
591 out:
592   g_free (hash_data->buffer);
593   hash_data->buffer = NULL;
594
595   schedule_hash_chunk (hash_data);
596 }
597
598 static void
599 schedule_hash_chunk (HashingData *hash_data)
600 {
601   EmpathyFTHandlerPriv *priv;
602
603   priv = GET_PRIV (hash_data->handler);
604
605   if (hash_data->done_reading)
606     {
607       g_input_stream_close_async (hash_data->stream, G_PRIORITY_DEFAULT,
608           priv->cancellable, hash_job_async_close_stream_cb, hash_data);
609     }
610   else
611     {
612       if (hash_data->buffer == NULL)
613         hash_data->buffer = g_malloc0 (BUFFER_SIZE);
614
615       g_input_stream_read_async (hash_data->stream, hash_data->buffer,
616           BUFFER_SIZE, G_PRIORITY_DEFAULT, priv->cancellable,
617           hash_job_async_read_cb, hash_data);
618     }
619 }
620
621 static void
622 ft_handler_read_async_cb (GObject *source,
623                           GAsyncResult *res,
624                           gpointer user_data)
625 {
626   GFileInputStream *stream;
627   GError *error = NULL;
628   HashingData *hash_data;
629   GValue *value;
630   EmpathyFTHandler *handler = user_data;
631   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
632
633   DEBUG ("FT: GFile read async CB.");
634
635   stream = g_file_read_finish (priv->gfile, res, &error);
636   if (error != NULL)
637     {
638       g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error);
639       g_clear_error (&error);
640
641       return;
642     }
643
644   hash_data = g_slice_new0 (HashingData);
645   hash_data->stream = G_INPUT_STREAM (stream);
646   hash_data->done_reading = FALSE;
647   hash_data->total_bytes = priv->total_bytes;
648   hash_data->handler = g_object_ref (handler);
649   /* FIXME: should look at the CM capabilities before setting the
650    * checksum type?
651    */
652   hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
653
654   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
655   value = tp_g_value_slice_new (G_TYPE_UINT);
656   g_value_set_uint (value, EMP_FILE_HASH_TYPE_MD5);
657   g_hash_table_insert (priv->request,
658       EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value);
659
660   g_signal_emit (handler, signals[HASHING_STARTED], 0);
661
662   schedule_hash_chunk (hash_data);
663 }
664
665 static void
666 ft_handler_complete_request (EmpathyFTHandler *handler)
667
668   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
669   GError *myerr = NULL;
670
671   /* check if FT is allowed before firing up the I/O machinery */
672   if (!ft_handler_check_if_allowed (handler))
673     {
674       g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
675           EMPATHY_FT_ERROR_NOT_SUPPORTED,
676           _("File transfer not supported by remote contact"));
677
678       g_signal_emit (handler, signals[TRANSFER_ERROR], 0, myerr);
679       g_clear_error (&myerr);
680
681       return;
682     }
683
684   /* populate the request table with all the known properties */
685   ft_handler_populate_outgoing_request (handler);
686
687   /* now start hashing the file */
688   g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT,
689       priv->cancellable, ft_handler_read_async_cb, handler);
690 }
691
692 static void
693 callbacks_data_free (gpointer user_data)
694 {
695   CallbacksData *data = user_data;
696
697   if (data->handler)
698     g_object_unref (data->handler);
699
700   g_slice_free (CallbacksData, data);
701 }
702
703 static void
704 ft_handler_gfile_ready_cb (GObject *source,
705                            GAsyncResult *res,
706                            CallbacksData *cb_data)
707 {
708   GFileInfo *info;
709   GError *error = NULL;
710   GTimeVal mtime;
711   EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler);
712
713   DEBUG ("FT: got GFileInfo.");
714
715   info = g_file_query_info_finish (priv->gfile, res, &error);
716
717   if (error != NULL)
718     goto out;
719
720   priv->content_type = g_strdup (g_file_info_get_content_type (info));
721   priv->filename = g_strdup (g_file_info_get_display_name (info));
722   priv->total_bytes = g_file_info_get_size (info);
723   g_file_info_get_modification_time (info, &mtime);
724   priv->mtime = mtime.tv_sec;
725   priv->transferred_bytes = 0;
726   priv->description = NULL;
727
728   g_object_unref (info);
729
730 out:
731   if (error == NULL)
732     {
733       cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
734     }
735   else
736     {
737       cb_data->callback (NULL, error, cb_data->user_data);
738       g_error_free (error);
739       g_object_unref (cb_data->handler);
740     }
741
742   callbacks_data_free (cb_data);
743 }
744
745 static void
746 ft_handler_contact_ready_cb (EmpathyContact *contact,
747                              const GError *error,
748                              gpointer user_data,
749                              GObject *weak_object)  
750 {
751   CallbacksData *cb_data = user_data;
752   EmpathyFTHandlerPriv *priv = GET_PRIV (weak_object);
753
754   g_assert (priv->contact != NULL);
755   g_assert (priv->gfile != NULL);
756
757   DEBUG ("FT: contact is ready.");
758
759   /* start collecting info about the file */
760   g_file_query_info_async (priv->gfile,
761       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
762       G_FILE_ATTRIBUTE_STANDARD_SIZE ","
763       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
764       G_FILE_ATTRIBUTE_TIME_MODIFIED,
765       G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
766       NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb,
767       cb_data);
768 }
769
770 static void
771 channel_get_all_properties_cb (TpProxy *proxy,
772                                GHashTable *properties,
773                                const GError *error,
774                                gpointer user_data,
775                                GObject *weak_object)
776 {
777   CallbacksData *cb_data = user_data;
778   EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
779   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
780   EmpathyContactFactory *c_factory;
781   guint c_handle;
782   McAccount *account;
783
784   if (error != NULL)
785     {
786       cb_data->callback (NULL, (GError *) error, cb_data->user_data);
787       g_object_unref (handler);
788       return;
789     }
790
791   priv->total_bytes = g_value_get_uint64 (
792       g_hash_table_lookup (properties, "Size"));
793
794   priv->transferred_bytes = g_value_get_uint64 (
795       g_hash_table_lookup (properties, "TransferredBytes"));
796
797   priv->filename = g_value_dup_string (
798       g_hash_table_lookup (properties, "Filename"));
799
800   priv->content_hash = g_value_dup_string (
801       g_hash_table_lookup (properties, "ContentHash"));
802
803   priv->content_hash_type = g_value_get_uint (
804       g_hash_table_lookup (properties, "ContentHashType"));
805
806   priv->content_type = g_value_dup_string (
807       g_hash_table_lookup (properties, "ContentType"));
808
809   priv->description = g_value_dup_string (
810       g_hash_table_lookup (properties, "Description"));
811
812   g_hash_table_destroy (properties);
813
814   c_factory = empathy_contact_factory_dup_singleton ();
815   account = empathy_channel_get_account (TP_CHANNEL (proxy));
816   c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
817   priv->contact = empathy_contact_factory_get_from_handle
818       (c_factory, account,c_handle);
819
820   g_object_unref (c_factory);
821   g_object_unref (account);
822
823   cb_data->callback (handler, NULL, cb_data->user_data);
824 }
825
826 /* public methods */
827
828 void
829 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
830                                  GFile *source,
831                                  EmpathyFTHandlerReadyCallback callback,
832                                  gpointer user_data)
833 {
834   EmpathyFTHandler *handler;
835   CallbacksData *data;
836   EmpathyFTHandlerPriv *priv;
837
838   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
839   g_return_if_fail (G_IS_FILE (source));
840
841   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
842       "contact", contact, "gfile", source, NULL);
843
844   priv = GET_PRIV (handler);
845
846   data = g_slice_new0 (CallbacksData);
847   data->callback = callback;
848   data->user_data = user_data;
849   data->handler = g_object_ref (handler);
850
851   empathy_contact_call_when_ready (priv->contact,
852       EMPATHY_CONTACT_READY_HANDLE,
853       ft_handler_contact_ready_cb, data, NULL, G_OBJECT (handler));
854 }
855
856 void
857 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
858                                  GFile *destination,
859                                  EmpathyFTHandlerReadyCallback callback,
860                                  gpointer user_data)
861 {
862   EmpathyFTHandler *handler;
863   TpChannel *channel;
864   CallbacksData *data;
865
866   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
867   g_return_if_fail (G_IS_FILE (destination));
868
869   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
870       "tp-file", tp_file, "gfile", destination, NULL);
871
872   g_object_get (tp_file, "channel", &channel, NULL);
873
874   data = g_slice_new0 (CallbacksData);
875   data->callback = callback;
876   data->user_data = user_data;
877   data->handler = g_object_ref (handler);
878
879   tp_cli_dbus_properties_call_get_all (channel,
880       -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
881       channel_get_all_properties_cb, data, callbacks_data_free, G_OBJECT (handler));
882 }
883
884 void
885 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler,
886                                    GCancellable *cancellable)
887 {
888   EmpathyFTHandlerPriv *priv;
889
890   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
891
892   priv = GET_PRIV (handler);
893   priv->cancellable = g_object_ref (cancellable);
894
895   if (priv->tpfile == NULL)
896     {
897       ft_handler_complete_request (handler);
898     }
899   else
900     {
901       /* emit the start signal now, so that we can catch errors in the
902        * op callback.
903        */
904       g_signal_emit (handler, signals[TRANSFER_STARTED], 0, priv->tpfile);
905
906       /* TODO: add support for resume. */
907       empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable,
908           ft_transfer_progress_callback, handler,
909           ft_transfer_operation_callback, handler);
910     }
911 }