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