]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Modify EmpathyFTHandler to adapt to the changes in EmpathyTpFile API.
[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     priv->progress_callback (EMPATHY_TP_FILE (weak_object),
413         priv->transferred_bytes, priv->size, priv->progress_user_data);
414 }
415
416 static void
417 splice_stream_ready_cb (GObject *source,
418                         GAsyncResult *res,
419                         gpointer user_data)
420 {
421   EmpathyTpFile *tp_file;
422   GError *error = NULL;
423
424   tp_file = user_data;
425
426   g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
427
428   if (error != NULL)
429     {
430       ft_operation_close_with_error (tp_file, error);
431       g_clear_error (&error);
432       return;
433     }
434
435   ft_operation_close_clean (tp_file);
436 }
437
438 static void
439 tp_file_start_transfer (EmpathyTpFile *tp_file)
440 {
441   gint fd;
442   size_t path_len;
443   struct sockaddr_un addr;
444   GError *error = NULL;
445   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
446
447   fd = socket (PF_UNIX, SOCK_STREAM, 0);
448   if (fd < 0)
449     {
450       int code = errno;
451
452       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
453           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
454
455       DEBUG ("Failed to create socket, closing channel");
456
457       ft_operation_close_with_error (tp_file, error);
458       g_clear_error (&error);
459
460       return;
461     }
462
463   memset (&addr, 0, sizeof (addr));
464   addr.sun_family = AF_UNIX;
465   path_len = strlen (priv->unix_socket_path);
466   strncpy (addr.sun_path, priv->unix_socket_path, path_len);
467
468   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
469     {
470       int code = errno;
471
472       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
473           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
474
475       DEBUG ("Failed to connect socket, closing channel");
476
477       ft_operation_close_with_error (tp_file, error);
478       close (fd);
479       g_clear_error (&error);
480
481       return;
482     }
483
484   DEBUG ("Start the transfer");
485
486   priv->start_time = empathy_time_get_current ();
487
488   /* notify we're starting a transfer */
489   if (priv->progress_callback)
490     priv->progress_callback (tp_file, 0, priv->size,
491         priv->progress_callback_user_data);
492
493   if (priv->incoming)
494     {
495       GInputStream *socket_stream;
496
497       socket_stream = g_unix_input_stream_new (fd, TRUE);
498
499       g_output_stream_splice_async (priv->out_stream, socket_stream,
500           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
501           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
502           G_PRIORITY_DEFAULT, priv->cancellable,
503           splice_stream_ready_cb, tp_file);
504
505       g_object_unref (socket_stream);
506     }
507   else
508     {
509       GOutputStream *socket_stream;
510
511       socket_stream = g_unix_output_stream_new (fd, TRUE);
512
513       g_output_stream_splice_async (socket_stream, priv->in_stream,
514           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
515           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
516           G_PRIORITY_DEFAULT, priv->cancellable,
517           splice_stream_ready_cb, tp_file);
518
519       g_object_unref (socket_stream);
520     }
521 }
522
523 static void
524 ft_operation_provide_or_accept_file_cb (TpProxy *proxy,
525                                         const GValue *address,
526                                         const GError *error,
527                                         gpointer user_data,
528                                         GObject *weak_object)
529 {
530   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
531   GError *myerr = NULL;
532   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
533
534   g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
535
536   if (error)
537     {
538       if (myerr)
539         {
540           /* if we were both cancelled and failed when calling the method,
541           * report the method error.
542           */
543           g_clear_error (&myerr);
544           myerr = g_error_copy (error);
545         }
546     }
547
548   if (myerr)
549     {
550       DEBUG ("Error: %s", error->message);
551       ft_operation_close_with_error (tp_file, myerr);
552       g_clear_error (&myerr);
553       return;
554     }
555
556   priv->unix_socket_path = g_value_dup_string (address);
557
558   DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
559
560   if (priv->state == EMP_FILE_TRANSFER_STATE_OPEN)
561     tp_file_start_transfer (tp_file);
562 }
563
564 static void
565 file_read_async_cb (GObject *source,
566                     GAsyncResult *res,
567                     gpointer user_data)
568 {
569   GValue nothing = { 0 };
570   EmpathyTpFile *tp_file = user_data;
571   EmpathyTpFilePriv *priv;
572   GFileInputStream *in_stream;
573   GError *error = NULL;
574
575   priv = GET_PRIV (tp_file);
576
577   in_stream = g_file_read_finish (G_FILE (source), res, &error);
578
579   if (error != NULL)
580     {
581       ft_operation_close_with_error (tp_file, error);
582       g_clear_error (&error);
583       return;
584     }
585
586   priv->in_stream = G_INPUT_STREAM (in_stream);
587
588   g_value_init (&nothing, G_TYPE_STRING);
589   g_value_set_static_string (&nothing, "");
590
591   emp_cli_channel_type_file_transfer_call_provide_file (
592       TP_PROXY (tp_file->priv->channel), -1,
593       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
594       &nothing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
595 }
596
597 static void
598 file_replace_async_cb (GObject *source,
599                        GAsyncResult *res,
600                        gpointer user_data)
601 {
602   GValue nothing = { 0 };
603   EmpathyTpFile *tp_file = user_data;
604   EmpathyTpFilePriv *priv;
605   GError *error = NULL;
606   GFileOutputStream *out_stream;
607
608   priv = GET_PRIV (tp_file);
609
610   out_stream = g_file_replace_finish (G_FILE (source), res, &error);
611
612   if (error != NULL)
613     {
614       ft_operation_close_with_error (tp_file, error);
615       g_clear_error (&error);
616
617       return;
618     }
619
620   priv->out_stream = out_stream;
621
622   priv->filename = g_file_get_basename (gfile);
623   g_object_notify (G_OBJECT (tp_file), "filename");
624
625   DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
626
627   g_value_init (&nothing, G_TYPE_STRING);
628   g_value_set_static_string (&nothing, "");
629
630   emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY (priv->channel),
631       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
632       &nothing, offset, ft_operation_provide_or_accept_file_cb, NULL, NULL,
633       G_OBJECT (tp_file));
634 }
635
636
637 /* public methods */
638
639 /**
640  * empathy_tp_file_new:
641  * @channel: a #TpChannel
642  *
643  * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
644  * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
645  * should be unrefed with g_object_unref() when finished with.
646  *
647  * Return value: a new #EmpathyTpFile
648  */
649 EmpathyTpFile *
650 empathy_tp_file_new (TpChannel *channel)
651 {
652   EmpathyTpFile *tp_file;
653
654   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
655
656   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
657       "channel", channel,
658       NULL);
659
660   return tp_file;
661 }
662
663 void
664 empathy_tp_file_accept (EmpathyTpFile *tp_file,
665                         guint64 offset,
666                         GFile *gfile,
667                         GCancellable *cancellable,
668                         EmpathyTpFileProgressCallback progress_callback,
669                         gpointer progress_user_data,
670                         EmpathyTpFileOperationCallback op_callback,
671                         gpointer op_user_data)
672 {
673   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
674
675   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
676   g_return_if_fail (G_IS_FILE (gfile));
677   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
678
679   priv->cancellable = g_object_ref (cancellable);
680   priv->progress_callback = progress_callback;
681   priv->progress_user_data = progress_user_data;
682   priv->op_callback = op_callback;
683   priv->op_user_data = op_user_data;
684   priv->incoming = TRUE;
685
686   g_file_replace_async (file, G_PRIORITY_DEFAULT, cancellable,
687       file_replace_async_cb, tp_file);
688 }
689
690 void
691 empathy_tp_file_offer (EmpathyTpFile *tp_file,
692                        GFile *gfile,
693                        GCancellable *cancellable,
694                        EmpathyTpFileProgressCallback progress_callback,
695                        gpointer progress_user_data,
696                        EmpathyTpFileOperationCallback op_callback,
697                        gpointer op_user_data)
698 {
699   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
700
701   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
702   g_return_if_fail (G_IS_FILE (gfile));
703   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
704
705   priv->cancellable = g_object_ref (cancellable);
706   priv->progress_callback = progress_callback;
707   priv->progress_user_data = progress_user_data;
708   priv->op_callback = op_callback;
709   priv->op_user_data = op_user_data;
710   priv->incoming = FALSE;
711
712   g_file_read_async (file, G_PRIORITY_DEFAULT, cancellable,
713       file_read_async_cb, tp_file);
714 }
715
716 /**
717  * empathy_tp_file_is_incoming:
718  * @tp_file: an #EmpathyTpFile
719  *
720  * Returns whether @tp_file is incoming.
721  *
722  * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
723  */
724 gboolean
725 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
726 {
727   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
728   return tp_file->priv->incoming;
729 }
730
731 /**
732  * empathy_tp_file_get_state:
733  * @tp_file: an #EmpathyTpFile
734  * @reason: return location for state change reason, or %NULL
735  *
736  * Gets the current state of @tp_file. If @reason is not %NULL, then
737  * it is set to the reason of the last state change.
738  *
739  * Return value: a #TpFileTransferState
740  */
741 TpFileTransferState
742 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
743                            TpFileTransferStateChangeReason *reason)
744 {
745   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
746
747   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
748       TP_FILE_TRANSFER_STATE_NONE);
749
750   if (reason != NULL)
751     *reason = priv->state_change_reason;
752
753   return priv->state;
754 }
755
756 /**
757  * empathy_tp_file_cancel:
758  * @tp_file: an #EmpathyTpFile
759  *
760  * Cancels the file transfer, @tp_file.
761  */
762 void
763 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
764 {
765   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
766
767   DEBUG ("Closing channel..");
768   tp_cli_channel_call_close (tp_file->priv->channel, -1,
769     NULL, NULL, NULL, NULL);
770
771   if (tp_file->priv->cancellable != NULL)
772     g_cancellable_cancel (tp_file->priv->cancellable);
773 }
774
775 /**
776  * empathy_tp_file_is_ready:
777  * @tp_file: an #EmpathyTpFile
778  *
779  * Returns whether the file channel @tp_file is ready for use.
780  *
781  * @tp_file is classed as ready if its state is no longer
782  * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
783  * contact have been fully received.
784  *
785  * Return value: %TRUE if @tp_file is ready for use
786  */
787 gboolean
788 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
789 {
790   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
791
792   return tp_file->priv->ready;
793 }
794