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