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