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