]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
React to Tp remote errors
[empathy.git] / libempathy / empathy-tp-file.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
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  * Authors: Marco Barisione <marco@barisione.org>
20  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
21  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include <glib/gi18n-lib.h>
34
35 #include <gio/gio.h>
36 #include <gio/gunixinputstream.h>
37 #include <gio/gunixoutputstream.h>
38
39 #include <telepathy-glib/proxy-subclass.h>
40 #include <telepathy-glib/util.h>
41
42 #include "empathy-tp-file.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_FT
48 #include "empathy-debug.h"
49
50 /**
51  * SECTION:empathy-tp-file
52  * @title: EmpathyTpFile
53  * @short_description: Object which represents a Telepathy file channel
54  * @include: libempathy/empathy-tp-file.h
55  *
56  * #EmpathyTpFile is an object which represents a Telepathy file channel.
57  */
58
59 /**
60  * EmpathyTpFile:
61  * @parent: parent object
62  *
63  * Object which represents a Telepathy file channel.
64  */
65
66 /**
67  * EMPATHY_TP_FILE_UNKNOWN_SIZE:
68  *
69  * Value used for the "size" or "estimated-size" properties when the size of
70  * the transferred file is unknown.
71  */
72
73 /* EmpathyTpFile object */
74
75 typedef struct {
76   TpChannel *channel;
77   gboolean ready;
78
79   GInputStream *in_stream;
80   GOutputStream *out_stream;
81
82   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
83   TpFileTransferState state;
84   TpFileTransferStateChangeReason state_change_reason;
85
86   /* transfer properties */
87   gboolean incoming;
88   time_t start_time;
89   GArray *unix_socket_path;
90   guint64 offset;
91
92   /* GCancellable we're passed when offering/accepting the transfer */
93   GCancellable *cancellable;
94
95   /* callbacks for the operation */
96   EmpathyTpFileProgressCallback progress_callback;
97   gpointer progress_user_data;
98   EmpathyTpFileOperationCallback op_callback;
99   gpointer op_user_data;
100
101   gboolean dispose_run;
102 } EmpathyTpFilePriv;
103
104 enum {
105   PROP_0,
106   PROP_CHANNEL,
107   PROP_INCOMING
108 };
109
110 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
111
112 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
113
114 /* private functions */
115
116 static void
117 tp_file_get_state_cb (TpProxy *proxy,
118                       const GValue *value,
119                       const GError *error,
120                       gpointer user_data,
121                       GObject *weak_object)
122 {
123   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
124
125   if (error)
126     {
127       /* set a default value for the state */
128       priv->state = TP_FILE_TRANSFER_STATE_NONE;
129       return;
130     }
131
132   priv->state = g_value_get_uint (value);
133 }
134
135 static void
136 tp_file_invalidated_cb (TpProxy       *proxy,
137                         guint          domain,
138                         gint           code,
139                         gchar         *message,
140                         EmpathyTpFile *tp_file)
141 {
142   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
143
144   DEBUG ("Channel invalidated: %s", message);
145
146   if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
147       priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
148     {
149       /* The channel is not in a finished state, an error occured */
150       priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
151       priv->state_change_reason =
152           TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
153     }
154 }
155
156 static void
157 ft_operation_close_clean (EmpathyTpFile *tp_file)
158 {
159   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
160
161   DEBUG ("FT operation close clean");
162
163   if (priv->op_callback)
164     priv->op_callback (tp_file, NULL, priv->op_user_data);
165 }
166
167 static void
168 ft_operation_close_with_error (EmpathyTpFile *tp_file,
169                                GError *error)
170 {
171   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
172
173   DEBUG ("FT operation close with error %s", error->message);
174
175   /* close the channel if it's not cancelled already */
176   if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
177     empathy_tp_file_cancel (tp_file);
178
179   if (priv->op_callback)
180     priv->op_callback (tp_file, error, priv->op_user_data);
181 }
182
183 static void
184 splice_stream_ready_cb (GObject *source,
185                         GAsyncResult *res,
186                         gpointer user_data)
187 {
188   EmpathyTpFile *tp_file;
189   GError *error = NULL;
190
191   tp_file = user_data;
192
193   DEBUG ("Splice stream ready cb");
194
195   g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
196
197   if (error != NULL)
198     {
199       ft_operation_close_with_error (tp_file, error);
200       g_clear_error (&error);
201       return;
202     }
203 }
204
205 static void
206 tp_file_start_transfer (EmpathyTpFile *tp_file)
207 {
208   gint fd;
209   struct sockaddr_un addr;
210   GError *error = NULL;
211   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
212
213   fd = socket (PF_UNIX, SOCK_STREAM, 0);
214   if (fd < 0)
215     {
216       int code = errno;
217
218       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
219           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
220
221       DEBUG ("Failed to create socket, closing channel");
222
223       ft_operation_close_with_error (tp_file, error);
224       g_clear_error (&error);
225
226       return;
227     }
228
229   memset (&addr, 0, sizeof (addr));
230   addr.sun_family = AF_UNIX;
231   strncpy (addr.sun_path, priv->unix_socket_path->data,
232       priv->unix_socket_path->len);
233
234   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
235     {
236       int code = errno;
237
238       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
239           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
240
241       DEBUG ("Failed to connect socket, closing channel");
242
243       ft_operation_close_with_error (tp_file, error);
244       close (fd);
245       g_clear_error (&error);
246
247       return;
248     }
249
250   DEBUG ("Start the transfer");
251
252   priv->start_time = empathy_time_get_current ();
253
254   /* notify we're starting a transfer */
255   if (priv->progress_callback)
256     priv->progress_callback (tp_file, 0, priv->progress_user_data);
257
258   if (priv->incoming)
259     {
260       GInputStream *socket_stream;
261
262       socket_stream = g_unix_input_stream_new (fd, TRUE);
263
264       g_output_stream_splice_async (priv->out_stream, socket_stream,
265           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
266           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
267           G_PRIORITY_DEFAULT, priv->cancellable,
268           splice_stream_ready_cb, tp_file);
269
270       g_object_unref (socket_stream);
271     }
272   else
273     {
274       GOutputStream *socket_stream;
275
276       socket_stream = g_unix_output_stream_new (fd, TRUE);
277
278       g_output_stream_splice_async (socket_stream, priv->in_stream,
279           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
280           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
281           G_PRIORITY_DEFAULT, priv->cancellable,
282           splice_stream_ready_cb, tp_file);
283
284       g_object_unref (socket_stream);
285     }
286 }
287
288 static GError *
289 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
290 {
291   const char *string;
292   GError *retval;
293
294   string = NULL;
295
296   switch (reason)
297     {
298       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
299         string = _("No reason was specified");
300         break;
301       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
302         string = _("The change in state was requested");
303         break;
304       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
305         string = _("You canceled the file transfer");
306         break;
307       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
308         string = _("The other participant canceled the file transfer");
309         break;
310       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
311         string = _("Error while trying to transfer the file");
312         break;
313       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
314         string = _("The other participant is unable to transfer the file");
315         break;
316       default:
317         string = _("Unknown reason");
318         break;
319     }
320
321   retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
322       EMPATHY_FT_ERROR_TP_ERROR, string);
323
324   return retval;
325 }
326
327 static void
328 tp_file_state_changed_cb (TpChannel *proxy,
329                           guint state,
330                           guint reason,
331                           gpointer user_data,
332                           GObject *weak_object)
333 {
334   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
335   GError *error;
336
337   if (state == priv->state)
338     return;
339
340   DEBUG ("File transfer state changed:\n"
341       "old state = %u, state = %u, reason = %u\n"
342       "\tincoming = %s, in_stream = %s, out_stream = %s",
343       priv->state, state, reason,
344       priv->incoming ? "yes" : "no",
345       priv->in_stream ? "present" : "not present",
346       priv->out_stream ? "present" : "not present");
347
348   priv->state = state;
349   priv->state_change_reason = reason;
350
351   /* If the channel is open AND we have the socket path, we can start the
352    * transfer. The socket path could be NULL if we are not doing the actual
353    * data transfer but are just an observer for the channel.
354    */
355   if (state == TP_FILE_TRANSFER_STATE_OPEN &&
356       priv->unix_socket_path != NULL)
357     tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
358
359   if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
360     ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
361
362   if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
363     {
364       error = error_from_state_change_reason (priv->state_change_reason);
365       ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
366     }
367 }
368
369 static void
370 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
371                                       guint64 count,
372                                       gpointer user_data,
373                                       GObject *weak_object)
374 {
375   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
376
377   /* don't notify for 0 bytes count */
378   if (count == 0)
379     return;
380
381   /* notify clients */
382   if (priv->progress_callback)
383     priv->progress_callback (EMPATHY_TP_FILE (weak_object),
384         count, priv->progress_user_data);
385 }
386
387 static void
388 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
389                                         const GValue *address,
390                                         const GError *error,
391                                         gpointer user_data,
392                                         GObject *weak_object)
393 {
394   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
395   GError *myerr = NULL;
396   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
397
398   g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
399
400   if (error)
401     {
402       if (myerr)
403         {
404           /* if we were both cancelled and failed when calling the method,
405           * report the method error.
406           */
407           g_clear_error (&myerr);
408           myerr = g_error_copy (error);
409         }
410     }
411
412   if (myerr)
413     {
414       DEBUG ("Error: %s", error->message);
415       ft_operation_close_with_error (tp_file, myerr);
416       g_clear_error (&myerr);
417       return;
418     }
419
420   if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
421     {
422       priv->unix_socket_path = g_value_dup_boxed (address);
423     }
424   else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
425     {
426       /* Old bugged version of telepathy-salut used to store the address
427        * as a 's' instead of an 'ay' */
428       const gchar *path;
429
430       path = g_value_get_string (address);
431       priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
432                                                   strlen (path));
433       g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
434     }
435
436   DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
437
438   /* if the channel is already open, start the transfer now, otherwise,
439    * wait for the state change signal.
440    */
441   if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
442     tp_file_start_transfer (tp_file);
443 }
444
445 static void
446 file_read_async_cb (GObject *source,
447                     GAsyncResult *res,
448                     gpointer user_data)
449 {
450   GValue nothing = { 0 };
451   EmpathyTpFile *tp_file = user_data;
452   EmpathyTpFilePriv *priv;
453   GFileInputStream *in_stream;
454   GError *error = NULL;
455
456   priv = GET_PRIV (tp_file);
457
458   in_stream = g_file_read_finish (G_FILE (source), res, &error);
459
460   if (error != NULL)
461     {
462       ft_operation_close_with_error (tp_file, error);
463       g_clear_error (&error);
464       return;
465     }
466
467   priv->in_stream = G_INPUT_STREAM (in_stream);
468
469   g_value_init (&nothing, G_TYPE_STRING);
470   g_value_set_static_string (&nothing, "");
471
472   tp_cli_channel_type_file_transfer_call_provide_file (
473       priv->channel, -1,
474       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
475       &nothing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
476 }
477
478 static void
479 file_replace_async_cb (GObject *source,
480                        GAsyncResult *res,
481                        gpointer user_data)
482 {
483   GValue nothing = { 0 };
484   EmpathyTpFile *tp_file = user_data;
485   EmpathyTpFilePriv *priv;
486   GError *error = NULL;
487   GFileOutputStream *out_stream;
488
489   priv = GET_PRIV (tp_file);
490
491   out_stream = g_file_replace_finish (G_FILE (source), res, &error);
492
493   if (error != NULL)
494     {
495       ft_operation_close_with_error (tp_file, error);
496       g_clear_error (&error);
497
498       return;
499     }
500
501   priv->out_stream = G_OUTPUT_STREAM (out_stream);
502
503   g_value_init (&nothing, G_TYPE_STRING);
504   g_value_set_static_string (&nothing, "");
505
506   tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
507       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
508       &nothing, priv->offset,
509       ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
510 }
511
512 static void
513 close_channel_internal (EmpathyTpFile *tp_file,
514                         gboolean cancel)
515 {
516   EmpathyTpFilePriv *priv;
517
518   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
519   
520   priv = GET_PRIV (tp_file);
521
522   DEBUG ("Closing channel..");
523   tp_cli_channel_call_close (priv->channel, -1,
524     NULL, NULL, NULL, NULL);
525
526   if (priv->cancellable != NULL &&
527       !g_cancellable_is_cancelled (priv->cancellable) && cancel)
528     g_cancellable_cancel (priv->cancellable);
529 }
530
531 /* GObject methods */
532
533 static void
534 empathy_tp_file_init (EmpathyTpFile *tp_file)
535 {
536   EmpathyTpFilePriv *priv;
537
538   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
539       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
540
541   tp_file->priv = priv;
542 }
543
544 static void
545 do_dispose (GObject *object)
546 {
547   EmpathyTpFilePriv *priv = GET_PRIV (object);
548
549   if (priv->dispose_run)
550     return;
551
552   priv->dispose_run = TRUE;
553
554   if (priv->channel)
555     {
556       g_signal_handlers_disconnect_by_func (priv->channel,
557           tp_file_invalidated_cb, object);
558       g_object_unref (priv->channel);
559       priv->channel = NULL;
560     }
561
562   if (priv->in_stream)
563     g_object_unref (priv->in_stream);
564
565   if (priv->out_stream)
566     g_object_unref (priv->out_stream);
567
568   if (priv->cancellable)
569     g_object_unref (priv->cancellable);
570
571   G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
572 }
573
574 static void
575 do_finalize (GObject *object)
576 {
577   EmpathyTpFilePriv *priv = GET_PRIV (object);
578
579   DEBUG ("%p", object);
580
581   if (priv->unix_socket_path != NULL)
582     {
583       g_array_free (priv->unix_socket_path, TRUE);
584       priv->unix_socket_path = NULL;
585     }
586
587   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
588 }
589
590 static void
591 do_get_property (GObject *object,
592                  guint param_id,
593                  GValue *value,
594                  GParamSpec *pspec)
595 {
596   EmpathyTpFilePriv *priv = GET_PRIV (object);
597
598   switch (param_id)
599     {
600       case PROP_CHANNEL:
601         g_value_set_object (value, priv->channel);
602         break;
603       case PROP_INCOMING:
604         g_value_set_boolean (value, priv->incoming);
605         break;
606       default:
607         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
608         break;
609     };
610 }
611
612 static void
613 do_set_property (GObject *object,
614                  guint param_id,
615                  const GValue *value,
616                  GParamSpec *pspec)
617 {
618   EmpathyTpFilePriv *priv = GET_PRIV (object);
619   switch (param_id)
620     {
621       case PROP_CHANNEL:
622         priv->channel = g_object_ref (g_value_get_object (value));
623         break;
624       case PROP_INCOMING:
625         priv->incoming = g_value_get_boolean (value);
626         break;
627       default:
628         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
629         break;
630     };
631 }
632
633 static GObject *
634 do_constructor (GType type,
635                 guint n_props,
636                 GObjectConstructParam *props)
637 {
638   GObject *file_obj;
639   EmpathyTpFile *tp_file;
640   EmpathyTpFilePriv *priv;
641
642   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
643       n_props, props);
644   
645   tp_file = EMPATHY_TP_FILE (file_obj);
646   priv = GET_PRIV (tp_file);
647
648   g_signal_connect (priv->channel, "invalidated",
649     G_CALLBACK (tp_file_invalidated_cb), tp_file);
650
651   tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
652       priv->channel, tp_file_state_changed_cb, NULL, NULL,
653       G_OBJECT (tp_file), NULL);
654
655   tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
656       priv->channel, tp_file_transferred_bytes_changed_cb,
657       NULL, NULL, G_OBJECT (tp_file), NULL);
658
659   tp_cli_dbus_properties_call_get (priv->channel,
660       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
661       NULL, NULL, file_obj);
662
663   priv->state_change_reason =
664       TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
665
666   return file_obj;
667 }
668
669 static void
670 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
671 {
672   GObjectClass *object_class = G_OBJECT_CLASS (klass);
673
674   object_class->finalize = do_finalize;
675   object_class->dispose = do_dispose;
676   object_class->constructor = do_constructor;
677   object_class->get_property = do_get_property;
678   object_class->set_property = do_set_property;
679
680   /* Construct-only properties */
681   g_object_class_install_property (object_class,
682       PROP_CHANNEL,
683       g_param_spec_object ("channel",
684           "telepathy channel",
685           "The file transfer channel",
686           TP_TYPE_CHANNEL,
687           G_PARAM_READWRITE |
688           G_PARAM_CONSTRUCT_ONLY));
689
690   g_object_class_install_property (object_class,
691       PROP_INCOMING,
692       g_param_spec_boolean ("incoming",
693           "direction of transfer",
694           "The direction of the file being transferred",
695           FALSE,
696           G_PARAM_READWRITE |
697           G_PARAM_CONSTRUCT_ONLY));
698
699   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
700 }
701
702 /* public methods */
703
704 /**
705  * empathy_tp_file_new:
706  * @channel: a #TpChannel
707  *
708  * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
709  * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
710  * should be unrefed with g_object_unref() when finished with.
711  *
712  * Return value: a new #EmpathyTpFile
713  */
714 EmpathyTpFile *
715 empathy_tp_file_new (TpChannel *channel, gboolean incoming)
716 {
717   EmpathyTpFile *tp_file;
718
719   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
720
721   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
722       "channel", channel, "incoming", incoming,
723       NULL);
724
725   return tp_file;
726 }
727
728 void
729 empathy_tp_file_accept (EmpathyTpFile *tp_file,
730                         guint64 offset,
731                         GFile *gfile,
732                         GCancellable *cancellable,
733                         EmpathyTpFileProgressCallback progress_callback,
734                         gpointer progress_user_data,
735                         EmpathyTpFileOperationCallback op_callback,
736                         gpointer op_user_data)
737 {
738   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
739
740   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
741   g_return_if_fail (G_IS_FILE (gfile));
742   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
743
744   priv->cancellable = g_object_ref (cancellable);
745   priv->progress_callback = progress_callback;
746   priv->progress_user_data = progress_user_data;
747   priv->op_callback = op_callback;
748   priv->op_user_data = op_user_data;
749   priv->offset = offset;
750
751   g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
752       G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
753 }
754
755 void
756 empathy_tp_file_offer (EmpathyTpFile *tp_file,
757                        GFile *gfile,
758                        GCancellable *cancellable,
759                        EmpathyTpFileProgressCallback progress_callback,
760                        gpointer progress_user_data,
761                        EmpathyTpFileOperationCallback op_callback,
762                        gpointer op_user_data)
763 {
764   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
765
766   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
767   g_return_if_fail (G_IS_FILE (gfile));
768   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
769
770   priv->cancellable = g_object_ref (cancellable);
771   priv->progress_callback = progress_callback;
772   priv->progress_user_data = progress_user_data;
773   priv->op_callback = op_callback;
774   priv->op_user_data = op_user_data;
775
776   g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
777       file_read_async_cb, tp_file);
778 }
779
780 /**
781  * empathy_tp_file_is_incoming:
782  * @tp_file: an #EmpathyTpFile
783  *
784  * Returns whether @tp_file is incoming.
785  *
786  * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
787  */
788 gboolean
789 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
790 {
791   EmpathyTpFilePriv *priv;
792
793   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
794   
795   priv = GET_PRIV (tp_file);
796
797   return priv->incoming;
798 }
799
800 void
801 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
802 {
803   close_channel_internal (tp_file, TRUE);
804 }
805
806 /**
807  * empathy_tp_file_is_ready:
808  * @tp_file: an #EmpathyTpFile
809  *
810  * Returns whether the file channel @tp_file is ready for use.
811  *
812  * @tp_file is classed as ready if its state is no longer
813  * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
814  * contact have been fully received.
815  *
816  * Return value: %TRUE if @tp_file is ready for use
817  */
818 gboolean
819 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
820 {
821   close_channel_internal (tp_file, FALSE);
822 }