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>
34 #include <glib/gi18n-lib.h>
37 #include <gio/gunixinputstream.h>
38 #include <gio/gunixoutputstream.h>
40 #include <telepathy-glib/gtypes.h>
41 #include <telepathy-glib/proxy-subclass.h>
42 #include <telepathy-glib/util.h>
44 #include "empathy-tp-file.h"
45 #include "empathy-marshal.h"
46 #include "empathy-time.h"
47 #include "empathy-utils.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_FT
50 #include "empathy-debug.h"
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
58 * #EmpathyTpFile is an object which represents a Telepathy file channel.
59 * Usually, clients do not need to deal with #EmpathyTpFile objects directly,
60 * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for
61 * transferring files using libempathy.
64 /* EmpathyTpFile object */
70 GInputStream *in_stream;
71 GOutputStream *out_stream;
73 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
74 TpFileTransferState state;
75 TpFileTransferStateChangeReason state_change_reason;
76 TpSocketAddressType socket_address_type;
77 TpSocketAccessControl socket_access_control;
79 /* transfer properties */
82 GArray *socket_address;
86 /* GCancellable we're passed when offering/accepting the transfer */
87 GCancellable *cancellable;
89 /* callbacks for the operation */
90 EmpathyTpFileProgressCallback progress_callback;
91 gpointer progress_user_data;
92 EmpathyTpFileOperationCallback op_callback;
93 gpointer op_user_data;
106 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
108 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
110 /* private functions */
113 tp_file_get_state_cb (TpProxy *proxy,
117 GObject *weak_object)
119 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
123 /* set a default value for the state */
124 priv->state = TP_FILE_TRANSFER_STATE_NONE;
128 priv->state = g_value_get_uint (value);
132 tp_file_get_available_socket_types_cb (TpProxy *proxy,
136 GObject *weak_object)
138 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
139 GHashTable *socket_types;
140 GArray *access_controls;
143 !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
145 /* set a default value */
146 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
147 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
151 socket_types = g_value_get_boxed (value);
153 /* here UNIX is preferred to IPV4 */
154 if ((access_controls = g_hash_table_lookup (socket_types,
155 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL)
157 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
158 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
162 if ((access_controls = g_hash_table_lookup (socket_types,
163 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
165 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
167 /* TODO: we should prefer PORT over LOCALHOST when the CM will
171 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
175 DEBUG ("Socket address type: %u, access control %u",
176 priv->socket_address_type, priv->socket_access_control);
180 tp_file_invalidated_cb (TpProxy *proxy,
184 EmpathyTpFile *tp_file)
186 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
188 DEBUG ("Channel invalidated: %s", message);
190 if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
191 priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
193 /* The channel is not in a finished state, an error occured */
194 priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
195 priv->state_change_reason =
196 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
201 ft_operation_close_clean (EmpathyTpFile *tp_file)
203 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
208 DEBUG ("FT operation close clean");
210 priv->is_closed = TRUE;
212 if (priv->op_callback != NULL)
213 priv->op_callback (tp_file, NULL, priv->op_user_data);
217 ft_operation_close_with_error (EmpathyTpFile *tp_file,
220 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
225 DEBUG ("FT operation close with error %s", error->message);
227 priv->is_closed = TRUE;
229 /* close the channel if it's not cancelled already */
230 if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
231 empathy_tp_file_cancel (tp_file);
233 if (priv->op_callback != NULL)
234 priv->op_callback (tp_file, error, priv->op_user_data);
238 splice_stream_ready_cb (GObject *source,
242 EmpathyTpFile *tp_file;
243 GError *error = NULL;
247 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
249 DEBUG ("Splice stream ready cb, error %p", error);
253 ft_operation_close_with_error (tp_file, error);
254 g_clear_error (&error);
260 tp_file_start_transfer (EmpathyTpFile *tp_file)
262 gint fd, domain, res = 0;
263 GError *error = NULL;
264 struct sockaddr *my_addr = NULL;
266 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
268 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
272 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
278 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
279 EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
281 DEBUG ("Socket not supported, closing channel");
283 ft_operation_close_with_error (tp_file, error);
284 g_clear_error (&error);
289 fd = socket (domain, SOCK_STREAM, 0);
295 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
296 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
298 DEBUG ("Failed to create socket, closing channel");
300 ft_operation_close_with_error (tp_file, error);
301 g_clear_error (&error);
306 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
308 struct sockaddr_un addr;
310 memset (&addr, 0, sizeof (addr));
311 addr.sun_family = domain;
312 strncpy (addr.sun_path, priv->socket_address->data,
313 priv->socket_address->len);
315 my_addr = (struct sockaddr *) &addr;
316 my_size = sizeof (addr);
318 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
320 struct sockaddr_in addr;
322 memset (&addr, 0, sizeof (addr));
323 addr.sin_family = domain;
324 inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr);
325 addr.sin_port = htons (priv->port);
327 my_addr = (struct sockaddr *) &addr;
328 my_size = sizeof (addr);
331 res = connect (fd, my_addr, my_size);
337 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
338 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
340 DEBUG ("Failed to connect socket, closing channel");
342 ft_operation_close_with_error (tp_file, error);
344 g_clear_error (&error);
349 DEBUG ("Start the transfer");
351 priv->start_time = empathy_time_get_current ();
353 /* notify we're starting a transfer */
354 if (priv->progress_callback != NULL)
355 priv->progress_callback (tp_file, 0, priv->progress_user_data);
359 GInputStream *socket_stream;
361 socket_stream = g_unix_input_stream_new (fd, TRUE);
363 g_output_stream_splice_async (priv->out_stream, socket_stream,
364 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
365 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
366 G_PRIORITY_DEFAULT, priv->cancellable,
367 splice_stream_ready_cb, tp_file);
369 g_object_unref (socket_stream);
373 GOutputStream *socket_stream;
375 socket_stream = g_unix_output_stream_new (fd, TRUE);
377 g_output_stream_splice_async (socket_stream, priv->in_stream,
378 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
379 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
380 G_PRIORITY_DEFAULT, priv->cancellable,
381 splice_stream_ready_cb, tp_file);
383 g_object_unref (socket_stream);
388 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
391 GError *retval = NULL;
397 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
398 string = _("No reason was specified");
400 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
401 string = _("The change in state was requested");
403 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
404 string = _("You canceled the file transfer");
406 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
407 string = _("The other participant canceled the file transfer");
409 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
410 string = _("Error while trying to transfer the file");
412 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
413 string = _("The other participant is unable to transfer the file");
416 string = _("Unknown reason");
420 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
421 EMPATHY_FT_ERROR_TP_ERROR, string);
427 tp_file_state_changed_cb (TpChannel *proxy,
431 GObject *weak_object)
433 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
434 GError *error = NULL;
436 if (state == priv->state)
439 DEBUG ("File transfer state changed:\n"
440 "old state = %u, state = %u, reason = %u\n"
441 "\tincoming = %s, in_stream = %s, out_stream = %s",
442 priv->state, state, reason,
443 priv->incoming ? "yes" : "no",
444 priv->in_stream ? "present" : "not present",
445 priv->out_stream ? "present" : "not present");
448 priv->state_change_reason = reason;
450 /* If the channel is open AND we have the socket path, we can start the
451 * transfer. The socket path could be NULL if we are not doing the actual
452 * data transfer but are just an observer for the channel.
454 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
455 priv->socket_address != NULL)
456 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
458 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
459 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
461 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
463 error = error_from_state_change_reason (priv->state_change_reason);
464 ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
465 g_clear_error (&error);
470 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
473 GObject *weak_object)
475 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
477 /* don't notify for 0 bytes count */
482 if (priv->progress_callback != NULL)
483 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
484 count, priv->progress_user_data);
488 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
489 const GValue *address,
492 GObject *weak_object)
494 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
495 GError *myerr = NULL;
496 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
498 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
504 /* if we were both cancelled and failed when calling the method,
505 * report the method error.
507 g_clear_error (&myerr);
510 myerr = g_error_copy (error);
515 DEBUG ("Error: %s", myerr->message);
516 ft_operation_close_with_error (tp_file, myerr);
517 g_clear_error (&myerr);
521 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
523 priv->socket_address = g_value_dup_boxed (address);
525 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
527 /* Old bugged version of telepathy-salut used to store the address
528 * as a 's' instead of an 'ay' */
531 path = g_value_get_string (address);
532 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
534 g_array_insert_vals (priv->socket_address, 0, path, strlen (path));
536 else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)
538 GValueArray *val_array;
542 val_array = g_value_get_boxed (address);
545 v = g_value_array_get_nth (val_array, 0);
546 addr = g_value_get_string (v);
547 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
549 g_array_insert_vals (priv->socket_address, 0, addr, strlen (addr));
552 v = g_value_array_get_nth (val_array, 1);
553 priv->port = g_value_get_uint (v);
556 DEBUG ("Got socket address: %s, port (not zero if IPV4): %d",
557 priv->socket_address->data, priv->port);
559 /* if the channel is already open, start the transfer now, otherwise,
560 * wait for the state change signal.
562 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
563 tp_file_start_transfer (tp_file);
567 initialize_empty_ac_variant (TpSocketAccessControl ac,
570 /* TODO: we will add more types here once we support PORT access control. */
571 if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
573 g_value_init (val, G_TYPE_STRING);
574 g_value_set_static_string (val, "");
579 file_read_async_cb (GObject *source,
583 GValue nothing = { 0 };
584 EmpathyTpFile *tp_file = user_data;
585 EmpathyTpFilePriv *priv;
586 GFileInputStream *in_stream;
587 GError *error = NULL;
589 priv = GET_PRIV (tp_file);
591 in_stream = g_file_read_finish (G_FILE (source), res, &error);
595 ft_operation_close_with_error (tp_file, error);
596 g_clear_error (&error);
600 priv->in_stream = G_INPUT_STREAM (in_stream);
602 /* we don't impose specific interface/port requirements even
603 * if we're not using UNIX sockets.
605 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
607 tp_cli_channel_type_file_transfer_call_provide_file (
609 priv->socket_address_type, priv->socket_access_control,
610 ¬hing, ft_operation_provide_or_accept_file_cb,
611 NULL, NULL, G_OBJECT (tp_file));
615 file_replace_async_cb (GObject *source,
619 GValue nothing = { 0 };
620 EmpathyTpFile *tp_file = user_data;
621 EmpathyTpFilePriv *priv;
622 GError *error = NULL;
623 GFileOutputStream *out_stream;
625 priv = GET_PRIV (tp_file);
627 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
631 ft_operation_close_with_error (tp_file, error);
632 g_clear_error (&error);
637 priv->out_stream = G_OUTPUT_STREAM (out_stream);
639 /* we don't impose specific interface/port requirements even
640 * if we're not using UNIX sockets.
642 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
644 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
645 -1, priv->socket_address_type, priv->socket_access_control,
646 ¬hing, priv->offset,
647 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
651 channel_closed_cb (TpChannel *proxy,
654 GObject *weak_object)
656 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
657 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
658 gboolean cancel = GPOINTER_TO_INT (user_data);
660 DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
662 if (priv->cancellable != NULL &&
663 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
664 g_cancellable_cancel (priv->cancellable);
668 close_channel_internal (EmpathyTpFile *tp_file,
671 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
673 DEBUG ("Closing channel, should cancel %s", cancel ?
676 tp_cli_channel_call_close (priv->channel, -1,
677 channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
680 /* GObject methods */
683 empathy_tp_file_init (EmpathyTpFile *tp_file)
685 EmpathyTpFilePriv *priv;
687 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
688 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
690 tp_file->priv = priv;
694 do_dispose (GObject *object)
696 EmpathyTpFilePriv *priv = GET_PRIV (object);
698 if (priv->dispose_run)
701 priv->dispose_run = TRUE;
703 if (priv->channel != NULL)
705 g_signal_handlers_disconnect_by_func (priv->channel,
706 tp_file_invalidated_cb, object);
707 g_object_unref (priv->channel);
708 priv->channel = NULL;
711 if (priv->in_stream != NULL)
712 g_object_unref (priv->in_stream);
714 if (priv->out_stream != NULL)
715 g_object_unref (priv->out_stream);
717 if (priv->cancellable != NULL)
718 g_object_unref (priv->cancellable);
720 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
724 do_finalize (GObject *object)
726 EmpathyTpFilePriv *priv = GET_PRIV (object);
728 DEBUG ("%p", object);
730 if (priv->socket_address != NULL)
732 g_array_free (priv->socket_address, TRUE);
733 priv->socket_address = NULL;
736 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
740 do_get_property (GObject *object,
745 EmpathyTpFilePriv *priv = GET_PRIV (object);
750 g_value_set_object (value, priv->channel);
753 g_value_set_boolean (value, priv->incoming);
756 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
762 do_set_property (GObject *object,
767 EmpathyTpFilePriv *priv = GET_PRIV (object);
771 priv->channel = g_object_ref (g_value_get_object (value));
774 priv->incoming = g_value_get_boolean (value);
777 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
783 do_constructed (GObject *object)
785 EmpathyTpFile *tp_file;
786 EmpathyTpFilePriv *priv;
788 tp_file = EMPATHY_TP_FILE (object);
789 priv = GET_PRIV (tp_file);
791 g_signal_connect (priv->channel, "invalidated",
792 G_CALLBACK (tp_file_invalidated_cb), tp_file);
794 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
795 priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
797 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
798 priv->channel, tp_file_transferred_bytes_changed_cb,
799 NULL, NULL, object, NULL);
801 tp_cli_dbus_properties_call_get (priv->channel,
802 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
805 tp_cli_dbus_properties_call_get (priv->channel,
806 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
807 tp_file_get_available_socket_types_cb, NULL, NULL, object);
809 priv->state_change_reason =
810 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
814 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
816 GObjectClass *object_class = G_OBJECT_CLASS (klass);
818 object_class->finalize = do_finalize;
819 object_class->dispose = do_dispose;
820 object_class->constructed = do_constructed;
821 object_class->get_property = do_get_property;
822 object_class->set_property = do_set_property;
824 /* Construct-only properties */
827 * EmpathyTpFile:channel:
829 * The #TpChannel requested for the file transfer.
831 g_object_class_install_property (object_class,
833 g_param_spec_object ("channel",
835 "The file transfer channel",
838 G_PARAM_CONSTRUCT_ONLY));
841 * EmpathyTpFile:incoming:
843 * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
845 g_object_class_install_property (object_class,
847 g_param_spec_boolean ("incoming",
848 "direction of transfer",
849 "The direction of the file being transferred",
852 G_PARAM_CONSTRUCT_ONLY));
854 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
860 * empathy_tp_file_new:
861 * @channel: a #TpChannel
862 * @incoming: whether the file transfer is incoming or not
864 * Creates a new #EmpathyTpFile wrapping @channel, with the direction
865 * specified by @incoming. The returned #EmpathyTpFile should be unrefed
866 * with g_object_unref() when finished with.
868 * Return value: a new #EmpathyTpFile
871 empathy_tp_file_new (TpChannel *channel,
874 EmpathyTpFile *tp_file;
876 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
878 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
879 "channel", channel, "incoming", incoming,
886 * empathy_tp_file_accept:
887 * @tp_file: an incoming #EmpathyTpFile
888 * @offset: the offset of @gfile where we should start writing
889 * @gfile: the destination #GFile for the transfer
890 * @cancellable: a #GCancellable
891 * @progress_callback: function to callback with progress information
892 * @progress_user_data: user_data to pass to @progress_callback
893 * @op_callback: function to callback when the transfer ends
894 * @op_user_data: user_data to pass to @op_callback
896 * Accepts an incoming file transfer, saving the result into @gfile.
897 * The callback @op_callback will be called both when the transfer is
898 * successful and in case of an error. Note that cancelling @cancellable,
899 * closes the socket of the file operation in progress, but doesn't
900 * guarantee that the transfer channel will be closed as well. Thus,
901 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
902 * actually cancel an ongoing #EmpathyTpFile.
905 empathy_tp_file_accept (EmpathyTpFile *tp_file,
908 GCancellable *cancellable,
909 EmpathyTpFileProgressCallback progress_callback,
910 gpointer progress_user_data,
911 EmpathyTpFileOperationCallback op_callback,
912 gpointer op_user_data)
914 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
916 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
917 g_return_if_fail (G_IS_FILE (gfile));
918 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
920 priv->cancellable = g_object_ref (cancellable);
921 priv->progress_callback = progress_callback;
922 priv->progress_user_data = progress_user_data;
923 priv->op_callback = op_callback;
924 priv->op_user_data = op_user_data;
925 priv->offset = offset;
927 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
928 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
933 * empathy_tp_file_offer:
934 * @tp_file: an outgoing #EmpathyTpFile
935 * @gfile: the source #GFile for the transfer
936 * @cancellable: a #GCancellable
937 * @progress_callback: function to callback with progress information
938 * @progress_user_data: user_data to pass to @progress_callback
939 * @op_callback: function to callback when the transfer ends
940 * @op_user_data: user_data to pass to @op_callback
942 * Offers an outgoing file transfer, reading data from @gfile.
943 * The callback @op_callback will be called both when the transfer is
944 * successful and in case of an error. Note that cancelling @cancellable,
945 * closes the socket of the file operation in progress, but doesn't
946 * guarantee that the transfer channel will be closed as well. Thus,
947 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
948 * actually cancel an ongoing #EmpathyTpFile.
951 empathy_tp_file_offer (EmpathyTpFile *tp_file,
953 GCancellable *cancellable,
954 EmpathyTpFileProgressCallback progress_callback,
955 gpointer progress_user_data,
956 EmpathyTpFileOperationCallback op_callback,
957 gpointer op_user_data)
959 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
961 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
962 g_return_if_fail (G_IS_FILE (gfile));
963 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
965 priv->cancellable = g_object_ref (cancellable);
966 priv->progress_callback = progress_callback;
967 priv->progress_user_data = progress_user_data;
968 priv->op_callback = op_callback;
969 priv->op_user_data = op_user_data;
971 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
972 file_read_async_cb, tp_file);
976 * empathy_tp_file_is_incoming:
977 * @tp_file: an #EmpathyTpFile
979 * Returns whether @tp_file is incoming.
981 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
984 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
986 EmpathyTpFilePriv *priv;
988 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
990 priv = GET_PRIV (tp_file);
992 return priv->incoming;
996 * empathy_tp_file_cancel:
997 * @tp_file: an #EmpathyTpFile
999 * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
1000 * cancelling any I/O operation and closing the socket.
1003 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1005 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1007 close_channel_internal (tp_file, TRUE);
1011 * empathy_tp_file_close:
1012 * @tp_file: an #EmpathyTpFile
1014 * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
1015 * method after the transfer has ended.
1018 empathy_tp_file_close (EmpathyTpFile *tp_file)
1020 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1022 close_channel_internal (tp_file, FALSE);