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