2 * Copyright (C) 2007-2009 Collabora Ltd.
3 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
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.
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.
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
19 * Authors: Marco Barisione <marco@barisione.org>
20 * Jonny Lamb <jonny.lamb@collabora.co.uk>
21 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
29 #include <arpa/inet.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <glib/gi18n-lib.h>
38 #include <gio/gunixinputstream.h>
39 #include <gio/gunixoutputstream.h>
41 #include <telepathy-glib/gtypes.h>
42 #include <telepathy-glib/proxy-subclass.h>
43 #include <telepathy-glib/util.h>
44 #include <telepathy-glib/interfaces.h>
46 #include "empathy-tp-file.h"
47 #include "empathy-marshal.h"
48 #include "empathy-time.h"
49 #include "empathy-utils.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_FT
52 #include "empathy-debug.h"
55 * SECTION:empathy-tp-file
56 * @title: EmpathyTpFile
57 * @short_description: Object which represents a Telepathy file channel
58 * @include: libempathy/empathy-tp-file.h
60 * #EmpathyTpFile is an object which represents a Telepathy file channel.
61 * Usually, clients do not need to deal with #EmpathyTpFile objects directly,
62 * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for
63 * transferring files using libempathy.
66 /* EmpathyTpFile object */
72 GInputStream *in_stream;
73 GOutputStream *out_stream;
75 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
76 TpFileTransferState state;
77 TpFileTransferStateChangeReason state_change_reason;
78 TpSocketAddressType socket_address_type;
79 TpSocketAccessControl socket_access_control;
81 /* transfer properties */
84 GArray *socket_address;
88 /* GCancellable we're passed when offering/accepting the transfer */
89 GCancellable *cancellable;
91 /* callbacks for the operation */
92 EmpathyTpFileProgressCallback progress_callback;
93 gpointer progress_user_data;
94 EmpathyTpFileOperationCallback op_callback;
95 gpointer op_user_data;
108 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
110 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
112 /* private functions */
115 tp_file_get_state_cb (TpProxy *proxy,
119 GObject *weak_object)
121 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
125 /* set a default value for the state */
126 priv->state = TP_FILE_TRANSFER_STATE_NONE;
130 priv->state = g_value_get_uint (value);
134 tp_file_get_available_socket_types_cb (TpProxy *proxy,
138 GObject *weak_object)
140 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
141 GHashTable *socket_types;
142 GArray *access_controls;
145 !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
147 /* set a default value */
148 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
149 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
153 socket_types = g_value_get_boxed (value);
155 /* here UNIX is preferred to IPV4 */
156 if ((access_controls = g_hash_table_lookup (socket_types,
157 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL)
159 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
160 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
164 if ((access_controls = g_hash_table_lookup (socket_types,
165 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
167 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
169 /* TODO: we should prefer PORT over LOCALHOST when the CM will
173 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
177 DEBUG ("Socket address type: %u, access control %u",
178 priv->socket_address_type, priv->socket_access_control);
182 tp_file_invalidated_cb (TpProxy *proxy,
186 EmpathyTpFile *tp_file)
188 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
190 DEBUG ("Channel invalidated: %s", message);
192 if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
193 priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
195 /* The channel is not in a finished state, an error occured */
196 priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
197 priv->state_change_reason =
198 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
203 ft_operation_close_clean (EmpathyTpFile *tp_file)
205 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
210 DEBUG ("FT operation close clean");
212 priv->is_closed = TRUE;
214 if (priv->op_callback != NULL)
215 priv->op_callback (tp_file, NULL, priv->op_user_data);
219 ft_operation_close_with_error (EmpathyTpFile *tp_file,
222 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
227 DEBUG ("FT operation close with error %s", error->message);
229 priv->is_closed = TRUE;
231 /* close the channel if it's not cancelled already */
232 if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
233 empathy_tp_file_cancel (tp_file);
235 if (priv->op_callback != NULL)
236 priv->op_callback (tp_file, error, priv->op_user_data);
240 splice_stream_ready_cb (GObject *source,
244 EmpathyTpFile *tp_file;
245 GError *error = NULL;
249 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
251 DEBUG ("Splice stream ready cb, error %p", error);
255 ft_operation_close_with_error (tp_file, error);
256 g_clear_error (&error);
262 tp_file_start_transfer (EmpathyTpFile *tp_file)
264 gint fd, domain, res = 0;
265 GError *error = NULL;
266 struct sockaddr *my_addr = NULL;
268 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
270 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
274 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
280 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
281 EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
283 DEBUG ("Socket not supported, closing channel");
285 ft_operation_close_with_error (tp_file, error);
286 g_clear_error (&error);
291 fd = socket (domain, SOCK_STREAM, 0);
297 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
298 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
300 DEBUG ("Failed to create socket, closing channel");
302 ft_operation_close_with_error (tp_file, error);
303 g_clear_error (&error);
308 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
310 struct sockaddr_un addr;
312 memset (&addr, 0, sizeof (addr));
313 addr.sun_family = domain;
314 strncpy (addr.sun_path, priv->socket_address->data,
315 priv->socket_address->len);
317 my_addr = (struct sockaddr *) &addr;
318 my_size = sizeof (addr);
320 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
322 struct sockaddr_in addr;
324 memset (&addr, 0, sizeof (addr));
325 addr.sin_family = domain;
326 inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr);
327 addr.sin_port = htons (priv->port);
329 my_addr = (struct sockaddr *) &addr;
330 my_size = sizeof (addr);
333 res = connect (fd, my_addr, my_size);
339 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
340 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
342 DEBUG ("Failed to connect socket, closing channel");
344 ft_operation_close_with_error (tp_file, error);
346 g_clear_error (&error);
351 DEBUG ("Start the transfer");
353 priv->start_time = empathy_time_get_current ();
355 /* notify we're starting a transfer */
356 if (priv->progress_callback != NULL)
357 priv->progress_callback (tp_file, 0, priv->progress_user_data);
361 GInputStream *socket_stream;
363 socket_stream = g_unix_input_stream_new (fd, TRUE);
365 g_output_stream_splice_async (priv->out_stream, socket_stream,
366 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
367 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
368 G_PRIORITY_DEFAULT, priv->cancellable,
369 splice_stream_ready_cb, tp_file);
371 g_object_unref (socket_stream);
375 GOutputStream *socket_stream;
377 socket_stream = g_unix_output_stream_new (fd, TRUE);
379 g_output_stream_splice_async (socket_stream, priv->in_stream,
380 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
381 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
382 G_PRIORITY_DEFAULT, priv->cancellable,
383 splice_stream_ready_cb, tp_file);
385 g_object_unref (socket_stream);
390 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
393 GError *retval = NULL;
399 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
400 string = _("No reason was specified");
402 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
403 string = _("The change in state was requested");
405 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
406 string = _("You canceled the file transfer");
408 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
409 string = _("The other participant canceled the file transfer");
411 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
412 string = _("Error while trying to transfer the file");
414 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
415 string = _("The other participant is unable to transfer the file");
418 string = _("Unknown reason");
422 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
423 EMPATHY_FT_ERROR_TP_ERROR, string);
429 tp_file_state_changed_cb (TpChannel *proxy,
433 GObject *weak_object)
435 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
436 GError *error = NULL;
438 if (state == priv->state)
441 DEBUG ("File transfer state changed:\n"
442 "old state = %u, state = %u, reason = %u\n"
443 "\tincoming = %s, in_stream = %s, out_stream = %s",
444 priv->state, state, reason,
445 priv->incoming ? "yes" : "no",
446 priv->in_stream ? "present" : "not present",
447 priv->out_stream ? "present" : "not present");
450 priv->state_change_reason = reason;
452 /* If the channel is open AND we have the socket path, we can start the
453 * transfer. The socket path could be NULL if we are not doing the actual
454 * data transfer but are just an observer for the channel.
456 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
457 priv->socket_address != NULL)
458 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
460 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
461 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
463 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
465 error = error_from_state_change_reason (priv->state_change_reason);
466 ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
467 g_clear_error (&error);
472 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
475 GObject *weak_object)
477 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
479 /* don't notify for 0 bytes count */
484 if (priv->progress_callback != NULL)
485 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
486 count, priv->progress_user_data);
490 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
491 const GValue *address,
494 GObject *weak_object)
496 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
497 GError *myerr = NULL;
498 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
500 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
506 /* if we were both cancelled and failed when calling the method,
507 * report the method error.
509 g_clear_error (&myerr);
512 myerr = g_error_copy (error);
517 DEBUG ("Error: %s", myerr->message);
518 ft_operation_close_with_error (tp_file, myerr);
519 g_clear_error (&myerr);
523 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
525 priv->socket_address = g_value_dup_boxed (address);
527 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
529 /* Old bugged version of telepathy-salut used to store the address
530 * as a 's' instead of an 'ay' */
533 path = g_value_get_string (address);
534 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
536 g_array_insert_vals (priv->socket_address, 0, path, strlen (path));
538 else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)
540 GValueArray *val_array;
544 val_array = g_value_get_boxed (address);
547 v = g_value_array_get_nth (val_array, 0);
548 addr = g_value_get_string (v);
549 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
551 g_array_insert_vals (priv->socket_address, 0, addr, strlen (addr));
554 v = g_value_array_get_nth (val_array, 1);
555 priv->port = g_value_get_uint (v);
558 DEBUG ("Got socket address: %s, port (not zero if IPV4): %d",
559 priv->socket_address->data, priv->port);
561 /* if the channel is already open, start the transfer now, otherwise,
562 * wait for the state change signal.
564 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
565 tp_file_start_transfer (tp_file);
569 initialize_empty_ac_variant (TpSocketAccessControl ac,
572 /* TODO: we will add more types here once we support PORT access control. */
573 if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
575 g_value_init (val, G_TYPE_STRING);
576 g_value_set_static_string (val, "");
581 file_read_async_cb (GObject *source,
585 GValue nothing = { 0 };
586 EmpathyTpFile *tp_file = user_data;
587 EmpathyTpFilePriv *priv;
588 GFileInputStream *in_stream;
589 GError *error = NULL;
591 priv = GET_PRIV (tp_file);
593 in_stream = g_file_read_finish (G_FILE (source), res, &error);
597 ft_operation_close_with_error (tp_file, error);
598 g_clear_error (&error);
602 priv->in_stream = G_INPUT_STREAM (in_stream);
604 /* we don't impose specific interface/port requirements even
605 * if we're not using UNIX sockets.
607 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
609 tp_cli_channel_type_file_transfer_call_provide_file (
611 priv->socket_address_type, priv->socket_access_control,
612 ¬hing, ft_operation_provide_or_accept_file_cb,
613 NULL, NULL, G_OBJECT (tp_file));
617 file_replace_async_cb (GObject *source,
621 GValue nothing = { 0 };
622 EmpathyTpFile *tp_file = user_data;
623 EmpathyTpFilePriv *priv;
624 GError *error = NULL;
625 GFileOutputStream *out_stream;
627 priv = GET_PRIV (tp_file);
629 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
633 ft_operation_close_with_error (tp_file, error);
634 g_clear_error (&error);
639 priv->out_stream = G_OUTPUT_STREAM (out_stream);
641 /* we don't impose specific interface/port requirements even
642 * if we're not using UNIX sockets.
644 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
646 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
647 -1, priv->socket_address_type, priv->socket_access_control,
648 ¬hing, priv->offset,
649 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
653 channel_closed_cb (TpChannel *proxy,
656 GObject *weak_object)
658 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
659 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
660 gboolean cancel = GPOINTER_TO_INT (user_data);
662 DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
664 if (priv->cancellable != NULL &&
665 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
666 g_cancellable_cancel (priv->cancellable);
670 close_channel_internal (EmpathyTpFile *tp_file,
673 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
675 DEBUG ("Closing channel, should cancel %s", cancel ?
678 tp_cli_channel_call_close (priv->channel, -1,
679 channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
682 /* GObject methods */
685 empathy_tp_file_init (EmpathyTpFile *tp_file)
687 EmpathyTpFilePriv *priv;
689 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
690 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
692 tp_file->priv = priv;
696 do_dispose (GObject *object)
698 EmpathyTpFilePriv *priv = GET_PRIV (object);
700 if (priv->dispose_run)
703 priv->dispose_run = TRUE;
705 if (priv->channel != NULL)
707 g_signal_handlers_disconnect_by_func (priv->channel,
708 tp_file_invalidated_cb, object);
709 g_object_unref (priv->channel);
710 priv->channel = NULL;
713 if (priv->in_stream != NULL)
714 g_object_unref (priv->in_stream);
716 if (priv->out_stream != NULL)
717 g_object_unref (priv->out_stream);
719 if (priv->cancellable != NULL)
720 g_object_unref (priv->cancellable);
722 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
726 do_finalize (GObject *object)
728 EmpathyTpFilePriv *priv = GET_PRIV (object);
730 DEBUG ("%p", object);
732 if (priv->socket_address != NULL)
734 g_array_free (priv->socket_address, TRUE);
735 priv->socket_address = NULL;
738 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
742 do_get_property (GObject *object,
747 EmpathyTpFilePriv *priv = GET_PRIV (object);
752 g_value_set_object (value, priv->channel);
755 g_value_set_boolean (value, priv->incoming);
758 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
764 do_set_property (GObject *object,
769 EmpathyTpFilePriv *priv = GET_PRIV (object);
773 priv->channel = g_object_ref (g_value_get_object (value));
776 priv->incoming = g_value_get_boolean (value);
779 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
785 do_constructed (GObject *object)
787 EmpathyTpFile *tp_file;
788 EmpathyTpFilePriv *priv;
790 tp_file = EMPATHY_TP_FILE (object);
791 priv = GET_PRIV (tp_file);
793 g_signal_connect (priv->channel, "invalidated",
794 G_CALLBACK (tp_file_invalidated_cb), tp_file);
796 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
797 priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
799 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
800 priv->channel, tp_file_transferred_bytes_changed_cb,
801 NULL, NULL, object, NULL);
803 tp_cli_dbus_properties_call_get (priv->channel,
804 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
807 tp_cli_dbus_properties_call_get (priv->channel,
808 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
809 tp_file_get_available_socket_types_cb, NULL, NULL, object);
811 priv->state_change_reason =
812 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
816 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
818 GObjectClass *object_class = G_OBJECT_CLASS (klass);
820 object_class->finalize = do_finalize;
821 object_class->dispose = do_dispose;
822 object_class->constructed = do_constructed;
823 object_class->get_property = do_get_property;
824 object_class->set_property = do_set_property;
826 /* Construct-only properties */
829 * EmpathyTpFile:channel:
831 * The #TpChannel requested for the file transfer.
833 g_object_class_install_property (object_class,
835 g_param_spec_object ("channel",
837 "The file transfer channel",
840 G_PARAM_CONSTRUCT_ONLY));
843 * EmpathyTpFile:incoming:
845 * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
847 g_object_class_install_property (object_class,
849 g_param_spec_boolean ("incoming",
850 "direction of transfer",
851 "The direction of the file being transferred",
854 G_PARAM_CONSTRUCT_ONLY));
856 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
862 * empathy_tp_file_new:
863 * @channel: a #TpChannel
864 * @incoming: whether the file transfer is incoming or not
866 * Creates a new #EmpathyTpFile wrapping @channel, with the direction
867 * specified by @incoming. The returned #EmpathyTpFile should be unrefed
868 * with g_object_unref() when finished with.
870 * Return value: a new #EmpathyTpFile
873 empathy_tp_file_new (TpChannel *channel,
876 EmpathyTpFile *tp_file;
878 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
880 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
881 "channel", channel, "incoming", incoming,
888 * empathy_tp_file_accept:
889 * @tp_file: an incoming #EmpathyTpFile
890 * @offset: the offset of @gfile where we should start writing
891 * @gfile: the destination #GFile for the transfer
892 * @cancellable: a #GCancellable
893 * @progress_callback: function to callback with progress information
894 * @progress_user_data: user_data to pass to @progress_callback
895 * @op_callback: function to callback when the transfer ends
896 * @op_user_data: user_data to pass to @op_callback
898 * Accepts an incoming file transfer, saving the result into @gfile.
899 * The callback @op_callback will be called both when the transfer is
900 * successful and in case of an error. Note that cancelling @cancellable,
901 * closes the socket of the file operation in progress, but doesn't
902 * guarantee that the transfer channel will be closed as well. Thus,
903 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
904 * actually cancel an ongoing #EmpathyTpFile.
907 empathy_tp_file_accept (EmpathyTpFile *tp_file,
910 GCancellable *cancellable,
911 EmpathyTpFileProgressCallback progress_callback,
912 gpointer progress_user_data,
913 EmpathyTpFileOperationCallback op_callback,
914 gpointer op_user_data)
916 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
918 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
919 g_return_if_fail (G_IS_FILE (gfile));
920 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
922 priv->cancellable = g_object_ref (cancellable);
923 priv->progress_callback = progress_callback;
924 priv->progress_user_data = progress_user_data;
925 priv->op_callback = op_callback;
926 priv->op_user_data = op_user_data;
927 priv->offset = offset;
929 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
930 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
935 * empathy_tp_file_offer:
936 * @tp_file: an outgoing #EmpathyTpFile
937 * @gfile: the source #GFile for the transfer
938 * @cancellable: a #GCancellable
939 * @progress_callback: function to callback with progress information
940 * @progress_user_data: user_data to pass to @progress_callback
941 * @op_callback: function to callback when the transfer ends
942 * @op_user_data: user_data to pass to @op_callback
944 * Offers an outgoing file transfer, reading data from @gfile.
945 * The callback @op_callback will be called both when the transfer is
946 * successful and in case of an error. Note that cancelling @cancellable,
947 * closes the socket of the file operation in progress, but doesn't
948 * guarantee that the transfer channel will be closed as well. Thus,
949 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
950 * actually cancel an ongoing #EmpathyTpFile.
953 empathy_tp_file_offer (EmpathyTpFile *tp_file,
955 GCancellable *cancellable,
956 EmpathyTpFileProgressCallback progress_callback,
957 gpointer progress_user_data,
958 EmpathyTpFileOperationCallback op_callback,
959 gpointer op_user_data)
961 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
963 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
964 g_return_if_fail (G_IS_FILE (gfile));
965 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
967 priv->cancellable = g_object_ref (cancellable);
968 priv->progress_callback = progress_callback;
969 priv->progress_user_data = progress_user_data;
970 priv->op_callback = op_callback;
971 priv->op_user_data = op_user_data;
973 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
974 file_read_async_cb, tp_file);
978 * empathy_tp_file_is_incoming:
979 * @tp_file: an #EmpathyTpFile
981 * Returns whether @tp_file is incoming.
983 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
986 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
988 EmpathyTpFilePriv *priv;
990 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
992 priv = GET_PRIV (tp_file);
994 return priv->incoming;
998 * empathy_tp_file_cancel:
999 * @tp_file: an #EmpathyTpFile
1001 * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
1002 * cancelling any I/O operation and closing the socket.
1005 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1007 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1009 close_channel_internal (tp_file, TRUE);
1013 * empathy_tp_file_close:
1014 * @tp_file: an #EmpathyTpFile
1016 * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
1017 * method after the transfer has ended.
1020 empathy_tp_file_close (EmpathyTpFile *tp_file)
1022 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1024 close_channel_internal (tp_file, FALSE);