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