]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Use != NULL for pointer checks.
[empathy.git] / libempathy / empathy-tp-file.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Marco Barisione <marco@barisione.org>
20  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
21  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include <glib/gi18n-lib.h>
34
35 #include <gio/gio.h>
36 #include <gio/gunixinputstream.h>
37 #include <gio/gunixoutputstream.h>
38
39 #include <telepathy-glib/proxy-subclass.h>
40 #include <telepathy-glib/util.h>
41
42 #include "empathy-tp-file.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_FT
48 #include "empathy-debug.h"
49
50 /**
51  * SECTION:empathy-tp-file
52  * @title: EmpathyTpFile
53  * @short_description: Object which represents a Telepathy file channel
54  * @include: libempathy/empathy-tp-file.h
55  *
56  * #EmpathyTpFile is an object which represents a Telepathy file channel.
57  */
58
59 /**
60  * EmpathyTpFile:
61  * @parent: parent object
62  *
63  * Object which represents a Telepathy file channel.
64  */
65
66 /**
67  * EMPATHY_TP_FILE_UNKNOWN_SIZE:
68  *
69  * Value used for the "size" or "estimated-size" properties when the size of
70  * the transferred file is unknown.
71  */
72
73 /* EmpathyTpFile object */
74
75 typedef struct {
76   TpChannel *channel;
77   gboolean ready;
78
79   GInputStream *in_stream;
80   GOutputStream *out_stream;
81
82   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
83   TpFileTransferState state;
84   TpFileTransferStateChangeReason state_change_reason;
85
86   /* transfer properties */
87   gboolean incoming;
88   time_t start_time;
89   GArray *unix_socket_path;
90   guint64 offset;
91
92   /* GCancellable we're passed when offering/accepting the transfer */
93   GCancellable *cancellable;
94
95   /* callbacks for the operation */
96   EmpathyTpFileProgressCallback progress_callback;
97   gpointer progress_user_data;
98   EmpathyTpFileOperationCallback op_callback;
99   gpointer op_user_data;
100
101   gboolean is_closed;
102
103   gboolean dispose_run;
104 } EmpathyTpFilePriv;
105
106 enum {
107   PROP_0,
108   PROP_CHANNEL,
109   PROP_INCOMING
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 != NULL)
128     {
129       /* set a default value for the state */
130       priv->state = TP_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 != TP_FILE_TRANSFER_STATE_COMPLETED &&
149       priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
150     {
151       /* The channel is not in a finished state, an error occured */
152       priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
153       priv->state_change_reason =
154           TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
155     }
156 }
157
158 static void
159 ft_operation_close_clean (EmpathyTpFile *tp_file)
160 {
161   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
162
163   DEBUG ("FT operation close clean");
164
165   if (priv->is_closed)
166     return;
167
168   priv->is_closed = TRUE;
169
170   if (priv->op_callback != NULL)
171     priv->op_callback (tp_file, NULL, priv->op_user_data);
172 }
173
174 static void
175 ft_operation_close_with_error (EmpathyTpFile *tp_file,
176     GError *error)
177 {
178   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
179
180   DEBUG ("FT operation close with error %s", error->message);
181
182   if (priv->is_closed)
183     return;
184
185   priv->is_closed = TRUE;
186
187   /* close the channel if it's not cancelled already */
188   if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
189     empathy_tp_file_cancel (tp_file);
190
191   if (priv->op_callback != NULL)
192     priv->op_callback (tp_file, error, priv->op_user_data);
193 }
194
195 static void
196 splice_stream_ready_cb (GObject *source,
197     GAsyncResult *res,
198     gpointer user_data)
199 {
200   EmpathyTpFile *tp_file;
201   GError *error = NULL;
202
203   tp_file = user_data;
204
205   DEBUG ("Splice stream ready cb");
206
207   g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
208
209   if (error != NULL)
210     {
211       ft_operation_close_with_error (tp_file, error);
212       g_clear_error (&error);
213       return;
214     }
215 }
216
217 static void
218 tp_file_start_transfer (EmpathyTpFile *tp_file)
219 {
220   gint fd;
221   struct sockaddr_un addr;
222   GError *error = NULL;
223   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
224
225   fd = socket (PF_UNIX, SOCK_STREAM, 0);
226   if (fd < 0)
227     {
228       int code = errno;
229
230       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
231           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
232
233       DEBUG ("Failed to create socket, closing channel");
234
235       ft_operation_close_with_error (tp_file, error);
236       g_clear_error (&error);
237
238       return;
239     }
240
241   memset (&addr, 0, sizeof (addr));
242   addr.sun_family = AF_UNIX;
243   strncpy (addr.sun_path, priv->unix_socket_path->data,
244       priv->unix_socket_path->len);
245
246   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
247     {
248       int code = errno;
249
250       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
251           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
252
253       DEBUG ("Failed to connect socket, closing channel");
254
255       ft_operation_close_with_error (tp_file, error);
256       close (fd);
257       g_clear_error (&error);
258
259       return;
260     }
261
262   DEBUG ("Start the transfer");
263
264   priv->start_time = empathy_time_get_current ();
265
266   /* notify we're starting a transfer */
267   if (priv->progress_callback != NULL)
268     priv->progress_callback (tp_file, 0, priv->progress_user_data);
269
270   if (priv->incoming)
271     {
272       GInputStream *socket_stream;
273
274       socket_stream = g_unix_input_stream_new (fd, TRUE);
275
276       g_output_stream_splice_async (priv->out_stream, socket_stream,
277           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
278           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
279           G_PRIORITY_DEFAULT, priv->cancellable,
280           splice_stream_ready_cb, tp_file);
281
282       g_object_unref (socket_stream);
283     }
284   else
285     {
286       GOutputStream *socket_stream;
287
288       socket_stream = g_unix_output_stream_new (fd, TRUE);
289
290       g_output_stream_splice_async (socket_stream, priv->in_stream,
291           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
292           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
293           G_PRIORITY_DEFAULT, priv->cancellable,
294           splice_stream_ready_cb, tp_file);
295
296       g_object_unref (socket_stream);
297     }
298 }
299
300 static GError *
301 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
302 {
303   const char *string;
304   GError *retval;
305
306   string = NULL;
307
308   switch (reason)
309     {
310       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
311         string = _("No reason was specified");
312         break;
313       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
314         string = _("The change in state was requested");
315         break;
316       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
317         string = _("You canceled the file transfer");
318         break;
319       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
320         string = _("The other participant canceled the file transfer");
321         break;
322       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
323         string = _("Error while trying to transfer the file");
324         break;
325       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
326         string = _("The other participant is unable to transfer the file");
327         break;
328       default:
329         string = _("Unknown reason");
330         break;
331     }
332
333   retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
334       EMPATHY_FT_ERROR_TP_ERROR, string);
335
336   return retval;
337 }
338
339 static void
340 tp_file_state_changed_cb (TpChannel *proxy,
341     guint state,
342     guint reason,
343     gpointer user_data,
344     GObject *weak_object)
345 {
346   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
347   GError *error;
348
349   if (state == priv->state)
350     return;
351
352   DEBUG ("File transfer state changed:\n"
353       "old state = %u, state = %u, reason = %u\n"
354       "\tincoming = %s, in_stream = %s, out_stream = %s",
355       priv->state, state, reason,
356       priv->incoming ? "yes" : "no",
357       priv->in_stream ? "present" : "not present",
358       priv->out_stream ? "present" : "not present");
359
360   priv->state = state;
361   priv->state_change_reason = reason;
362
363   /* If the channel is open AND we have the socket path, we can start the
364    * transfer. The socket path could be NULL if we are not doing the actual
365    * data transfer but are just an observer for the channel.
366    */
367   if (state == TP_FILE_TRANSFER_STATE_OPEN &&
368       priv->unix_socket_path != NULL)
369     tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
370
371   if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
372     ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
373
374   if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
375     {
376       error = error_from_state_change_reason (priv->state_change_reason);
377       ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
378       g_clear_error (&error);
379     }
380 }
381
382 static void
383 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
384     guint64 count,
385     gpointer user_data,
386     GObject *weak_object)
387 {
388   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
389
390   /* don't notify for 0 bytes count */
391   if (count == 0)
392     return;
393
394   /* notify clients */
395   if (priv->progress_callback != NULL)
396     priv->progress_callback (EMPATHY_TP_FILE (weak_object),
397         count, priv->progress_user_data);
398 }
399
400 static void
401 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
402     const GValue *address,
403     const GError *error,
404     gpointer user_data,
405     GObject *weak_object)
406 {
407   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
408   GError *myerr = NULL;
409   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
410
411   g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
412
413   if (error != NULL)
414     {
415       if (myerr != NULL)
416         {
417           /* if we were both cancelled and failed when calling the method,
418           * report the method error.
419           */
420           g_clear_error (&myerr);
421           myerr = g_error_copy (error);
422         }
423     }
424
425   if (myerr != NULL)
426     {
427       DEBUG ("Error: %s", error->message);
428       ft_operation_close_with_error (tp_file, myerr);
429       g_clear_error (&myerr);
430       return;
431     }
432
433   if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
434     {
435       priv->unix_socket_path = g_value_dup_boxed (address);
436     }
437   else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
438     {
439       /* Old bugged version of telepathy-salut used to store the address
440        * as a 's' instead of an 'ay' */
441       const gchar *path;
442
443       path = g_value_get_string (address);
444       priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
445                                                   strlen (path));
446       g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
447     }
448
449   DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
450
451   /* if the channel is already open, start the transfer now, otherwise,
452    * wait for the state change signal.
453    */
454   if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
455     tp_file_start_transfer (tp_file);
456 }
457
458 static void
459 file_read_async_cb (GObject *source,
460     GAsyncResult *res,
461     gpointer user_data)
462 {
463   GValue nothing = { 0 };
464   EmpathyTpFile *tp_file = user_data;
465   EmpathyTpFilePriv *priv;
466   GFileInputStream *in_stream;
467   GError *error = NULL;
468
469   priv = GET_PRIV (tp_file);
470
471   in_stream = g_file_read_finish (G_FILE (source), res, &error);
472
473   if (error != NULL)
474     {
475       ft_operation_close_with_error (tp_file, error);
476       g_clear_error (&error);
477       return;
478     }
479
480   priv->in_stream = G_INPUT_STREAM (in_stream);
481
482   g_value_init (&nothing, G_TYPE_STRING);
483   g_value_set_static_string (&nothing, "");
484
485   tp_cli_channel_type_file_transfer_call_provide_file (
486       priv->channel, -1,
487       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
488       &nothing, ft_operation_provide_or_accept_file_cb,
489       NULL, NULL, G_OBJECT (tp_file));
490 }
491
492 static void
493 file_replace_async_cb (GObject *source,
494     GAsyncResult *res,
495     gpointer user_data)
496 {
497   GValue nothing = { 0 };
498   EmpathyTpFile *tp_file = user_data;
499   EmpathyTpFilePriv *priv;
500   GError *error = NULL;
501   GFileOutputStream *out_stream;
502
503   priv = GET_PRIV (tp_file);
504
505   out_stream = g_file_replace_finish (G_FILE (source), res, &error);
506
507   if (error != NULL)
508     {
509       ft_operation_close_with_error (tp_file, error);
510       g_clear_error (&error);
511
512       return;
513     }
514
515   priv->out_stream = G_OUTPUT_STREAM (out_stream);
516
517   g_value_init (&nothing, G_TYPE_STRING);
518   g_value_set_static_string (&nothing, "");
519
520   tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
521       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
522       &nothing, priv->offset,
523       ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
524 }
525
526 static void
527 channel_closed_cb (TpChannel *proxy,
528     const GError *error,
529     gpointer user_data,
530     GObject *weak_object)
531 {
532   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
533   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
534   gboolean *cancel = user_data;
535
536   DEBUG ("Channel is closed");
537
538   if (priv->cancellable != NULL &&
539       !g_cancellable_is_cancelled (priv->cancellable) && *cancel)
540     g_cancellable_cancel (priv->cancellable);  
541 }
542
543 static void
544 close_channel_internal (EmpathyTpFile *tp_file,
545     gboolean cancel)
546 {
547   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
548
549   DEBUG ("Closing channel..");
550
551   tp_cli_channel_call_close (priv->channel, -1,
552     channel_closed_cb, &cancel, NULL, G_OBJECT (tp_file));
553 }
554
555 /* GObject methods */
556
557 static void
558 empathy_tp_file_init (EmpathyTpFile *tp_file)
559 {
560   EmpathyTpFilePriv *priv;
561
562   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
563       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
564
565   tp_file->priv = priv;
566 }
567
568 static void
569 do_dispose (GObject *object)
570 {
571   EmpathyTpFilePriv *priv = GET_PRIV (object);
572
573   if (priv->dispose_run)
574     return;
575
576   priv->dispose_run = TRUE;
577
578   if (priv->channel != NULL)
579     {
580       g_signal_handlers_disconnect_by_func (priv->channel,
581           tp_file_invalidated_cb, object);
582       g_object_unref (priv->channel);
583       priv->channel = NULL;
584     }
585
586   if (priv->in_stream != NULL)
587     g_object_unref (priv->in_stream);
588
589   if (priv->out_stream != NULL)
590     g_object_unref (priv->out_stream);
591
592   if (priv->cancellable != NULL)
593     g_object_unref (priv->cancellable);
594
595   G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
596 }
597
598 static void
599 do_finalize (GObject *object)
600 {
601   EmpathyTpFilePriv *priv = GET_PRIV (object);
602
603   DEBUG ("%p", object);
604
605   if (priv->unix_socket_path != NULL)
606     {
607       g_array_free (priv->unix_socket_path, TRUE);
608       priv->unix_socket_path = NULL;
609     }
610
611   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
612 }
613
614 static void
615 do_get_property (GObject *object,
616     guint param_id,
617     GValue *value,
618     GParamSpec *pspec)
619 {
620   EmpathyTpFilePriv *priv = GET_PRIV (object);
621
622   switch (param_id)
623     {
624       case PROP_CHANNEL:
625         g_value_set_object (value, priv->channel);
626         break;
627       case PROP_INCOMING:
628         g_value_set_boolean (value, priv->incoming);
629         break;
630       default:
631         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
632         break;
633     };
634 }
635
636 static void
637 do_set_property (GObject *object,
638     guint param_id,
639     const GValue *value,
640     GParamSpec *pspec)
641 {
642   EmpathyTpFilePriv *priv = GET_PRIV (object);
643   switch (param_id)
644     {
645       case PROP_CHANNEL:
646         priv->channel = g_object_ref (g_value_get_object (value));
647         break;
648       case PROP_INCOMING:
649         priv->incoming = g_value_get_boolean (value);
650         break;
651       default:
652         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
653         break;
654     };
655 }
656
657 static GObject *
658 do_constructor (GType type,
659     guint n_props,
660     GObjectConstructParam *props)
661 {
662   GObject *file_obj;
663   EmpathyTpFile *tp_file;
664   EmpathyTpFilePriv *priv;
665
666   file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
667       n_props, props);
668   
669   tp_file = EMPATHY_TP_FILE (file_obj);
670   priv = GET_PRIV (tp_file);
671
672   g_signal_connect (priv->channel, "invalidated",
673     G_CALLBACK (tp_file_invalidated_cb), tp_file);
674
675   tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
676       priv->channel, tp_file_state_changed_cb, NULL, NULL,
677       G_OBJECT (tp_file), NULL);
678
679   tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
680       priv->channel, tp_file_transferred_bytes_changed_cb,
681       NULL, NULL, G_OBJECT (tp_file), NULL);
682
683   tp_cli_dbus_properties_call_get (priv->channel,
684       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
685       NULL, NULL, file_obj);
686
687   priv->state_change_reason =
688       TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
689
690   return file_obj;
691 }
692
693 static void
694 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
695 {
696   GObjectClass *object_class = G_OBJECT_CLASS (klass);
697
698   object_class->finalize = do_finalize;
699   object_class->dispose = do_dispose;
700   object_class->constructor = do_constructor;
701   object_class->get_property = do_get_property;
702   object_class->set_property = do_set_property;
703
704   /* Construct-only properties */
705   g_object_class_install_property (object_class,
706       PROP_CHANNEL,
707       g_param_spec_object ("channel",
708           "telepathy channel",
709           "The file transfer channel",
710           TP_TYPE_CHANNEL,
711           G_PARAM_READWRITE |
712           G_PARAM_CONSTRUCT_ONLY));
713
714   g_object_class_install_property (object_class,
715       PROP_INCOMING,
716       g_param_spec_boolean ("incoming",
717           "direction of transfer",
718           "The direction of the file being transferred",
719           FALSE,
720           G_PARAM_READWRITE |
721           G_PARAM_CONSTRUCT_ONLY));
722
723   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
724 }
725
726 /* public methods */
727
728 /**
729  * empathy_tp_file_new:
730  * @channel: a #TpChannel
731  *
732  * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
733  * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
734  * should be unrefed with g_object_unref() when finished with.
735  *
736  * Return value: a new #EmpathyTpFile
737  */
738 EmpathyTpFile *
739 empathy_tp_file_new (TpChannel *channel,
740     gboolean incoming)
741 {
742   EmpathyTpFile *tp_file;
743
744   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
745
746   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
747       "channel", channel, "incoming", incoming,
748       NULL);
749
750   return tp_file;
751 }
752
753 void
754 empathy_tp_file_accept (EmpathyTpFile *tp_file,
755     guint64 offset,
756     GFile *gfile,
757     GCancellable *cancellable,
758     EmpathyTpFileProgressCallback progress_callback,
759     gpointer progress_user_data,
760     EmpathyTpFileOperationCallback op_callback,
761     gpointer op_user_data)
762 {
763   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
764
765   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
766   g_return_if_fail (G_IS_FILE (gfile));
767   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
768
769   priv->cancellable = g_object_ref (cancellable);
770   priv->progress_callback = progress_callback;
771   priv->progress_user_data = progress_user_data;
772   priv->op_callback = op_callback;
773   priv->op_user_data = op_user_data;
774   priv->offset = offset;
775
776   g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
777       G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
778 }
779
780 void
781 empathy_tp_file_offer (EmpathyTpFile *tp_file,
782     GFile *gfile,
783     GCancellable *cancellable,
784     EmpathyTpFileProgressCallback progress_callback,
785     gpointer progress_user_data,
786     EmpathyTpFileOperationCallback op_callback,
787     gpointer op_user_data)
788 {
789   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
790
791   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
792   g_return_if_fail (G_IS_FILE (gfile));
793   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
794
795   priv->cancellable = g_object_ref (cancellable);
796   priv->progress_callback = progress_callback;
797   priv->progress_user_data = progress_user_data;
798   priv->op_callback = op_callback;
799   priv->op_user_data = op_user_data;
800
801   g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
802       file_read_async_cb, tp_file);
803 }
804
805 /**
806  * empathy_tp_file_is_incoming:
807  * @tp_file: an #EmpathyTpFile
808  *
809  * Returns whether @tp_file is incoming.
810  *
811  * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
812  */
813 gboolean
814 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
815 {
816   EmpathyTpFilePriv *priv;
817
818   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
819   
820   priv = GET_PRIV (tp_file);
821
822   return priv->incoming;
823 }
824
825 void
826 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
827 {
828   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
829
830   close_channel_internal (tp_file, TRUE);
831 }
832
833 void
834 empathy_tp_file_close (EmpathyTpFile *tp_file)
835 {
836   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
837
838   close_channel_internal (tp_file, FALSE);
839 }