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 <sys/types.h>
30 #include <sys/socket.h>
33 #include <glib/gi18n-lib.h>
36 #include <gio/gunixinputstream.h>
37 #include <gio/gunixoutputstream.h>
39 #include <telepathy-glib/gtypes.h>
40 #include <telepathy-glib/proxy-subclass.h>
41 #include <telepathy-glib/util.h>
43 #include "empathy-tp-file.h"
44 #include "empathy-marshal.h"
45 #include "empathy-time.h"
46 #include "empathy-utils.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_FT
49 #include "empathy-debug.h"
52 * SECTION:empathy-tp-file
53 * @title: EmpathyTpFile
54 * @short_description: Object which represents a Telepathy file channel
55 * @include: libempathy/empathy-tp-file.h
57 * #EmpathyTpFile is an object which represents a Telepathy file channel.
58 * Usually, clients do not need to deal with #EmpathyTpFile objects directly,
59 * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for
60 * transferring files using libempathy.
63 /* EmpathyTpFile object */
69 GInputStream *in_stream;
70 GOutputStream *out_stream;
72 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
73 TpFileTransferState state;
74 TpFileTransferStateChangeReason state_change_reason;
75 TpSocketAddressType socket_address_type;
76 TpSocketAccessControl socket_access_control;
78 /* transfer properties */
81 GArray *unix_socket_path;
84 /* GCancellable we're passed when offering/accepting the transfer */
85 GCancellable *cancellable;
87 /* callbacks for the operation */
88 EmpathyTpFileProgressCallback progress_callback;
89 gpointer progress_user_data;
90 EmpathyTpFileOperationCallback op_callback;
91 gpointer op_user_data;
104 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
106 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
108 /* private functions */
111 tp_file_get_state_cb (TpProxy *proxy,
115 GObject *weak_object)
117 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
121 /* set a default value for the state */
122 priv->state = TP_FILE_TRANSFER_STATE_NONE;
126 priv->state = g_value_get_uint (value);
130 uint_compare (gconstpointer a, gconstpointer b)
132 const guint *uinta = a;
133 const guint *uintb = b;
135 if (*uinta == *uintb)
138 return (*uinta > *uintb) ? 1 : -1;
142 tp_file_get_available_socket_types_cb (TpProxy *proxy,
146 GObject *weak_object)
148 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
149 GHashTable *socket_types;
150 GArray *access_controls;
153 !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
155 /* set a default value */
156 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
157 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
161 socket_types = g_value_get_boxed (value);
163 /* here UNIX is preferred to IPV4 */
164 if ((access_controls = g_hash_table_lookup (socket_types,
165 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL)
167 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
168 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
172 if ((access_controls = g_hash_table_lookup (socket_types,
173 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
175 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
176 g_array_sort (access_controls, uint_compare);
178 /* here port is preferred over localhost */
179 if ((g_array_index (access_controls, guint, 0) ==
180 TP_SOCKET_ACCESS_CONTROL_LOCALHOST) &&
181 (g_array_index (access_controls, guint, 1) ==
182 TP_SOCKET_ACCESS_CONTROL_PORT))
183 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_PORT;
185 priv->socket_access_control =
186 g_array_index (access_controls, guint, 0);
190 DEBUG ("Socket address type: %d, access control %d",
191 priv->socket_address_type, priv->socket_access_control);
195 tp_file_invalidated_cb (TpProxy *proxy,
199 EmpathyTpFile *tp_file)
201 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
203 DEBUG ("Channel invalidated: %s", message);
205 if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
206 priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
208 /* The channel is not in a finished state, an error occured */
209 priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
210 priv->state_change_reason =
211 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
216 ft_operation_close_clean (EmpathyTpFile *tp_file)
218 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
223 DEBUG ("FT operation close clean");
225 priv->is_closed = TRUE;
227 if (priv->op_callback != NULL)
228 priv->op_callback (tp_file, NULL, priv->op_user_data);
232 ft_operation_close_with_error (EmpathyTpFile *tp_file,
235 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
240 DEBUG ("FT operation close with error %s", error->message);
242 priv->is_closed = TRUE;
244 /* close the channel if it's not cancelled already */
245 if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
246 empathy_tp_file_cancel (tp_file);
248 if (priv->op_callback != NULL)
249 priv->op_callback (tp_file, error, priv->op_user_data);
253 splice_stream_ready_cb (GObject *source,
257 EmpathyTpFile *tp_file;
258 GError *error = NULL;
262 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
264 DEBUG ("Splice stream ready cb, error %p", error);
268 ft_operation_close_with_error (tp_file, error);
269 g_clear_error (&error);
275 tp_file_start_transfer (EmpathyTpFile *tp_file)
278 struct sockaddr_un addr;
279 GError *error = NULL;
280 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
282 fd = socket (PF_UNIX, SOCK_STREAM, 0);
287 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
288 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
290 DEBUG ("Failed to create socket, closing channel");
292 ft_operation_close_with_error (tp_file, error);
293 g_clear_error (&error);
298 memset (&addr, 0, sizeof (addr));
299 addr.sun_family = AF_UNIX;
300 strncpy (addr.sun_path, priv->unix_socket_path->data,
301 priv->unix_socket_path->len);
303 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
307 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
308 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
310 DEBUG ("Failed to connect socket, closing channel");
312 ft_operation_close_with_error (tp_file, error);
314 g_clear_error (&error);
319 DEBUG ("Start the transfer");
321 priv->start_time = empathy_time_get_current ();
323 /* notify we're starting a transfer */
324 if (priv->progress_callback != NULL)
325 priv->progress_callback (tp_file, 0, priv->progress_user_data);
329 GInputStream *socket_stream;
331 socket_stream = g_unix_input_stream_new (fd, TRUE);
333 g_output_stream_splice_async (priv->out_stream, socket_stream,
334 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
335 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
336 G_PRIORITY_DEFAULT, priv->cancellable,
337 splice_stream_ready_cb, tp_file);
339 g_object_unref (socket_stream);
343 GOutputStream *socket_stream;
345 socket_stream = g_unix_output_stream_new (fd, TRUE);
347 g_output_stream_splice_async (socket_stream, priv->in_stream,
348 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
349 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
350 G_PRIORITY_DEFAULT, priv->cancellable,
351 splice_stream_ready_cb, tp_file);
353 g_object_unref (socket_stream);
358 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
361 GError *retval = NULL;
367 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
368 string = _("No reason was specified");
370 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
371 string = _("The change in state was requested");
373 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
374 string = _("You canceled the file transfer");
376 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
377 string = _("The other participant canceled the file transfer");
379 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
380 string = _("Error while trying to transfer the file");
382 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
383 string = _("The other participant is unable to transfer the file");
386 string = _("Unknown reason");
390 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
391 EMPATHY_FT_ERROR_TP_ERROR, string);
397 tp_file_state_changed_cb (TpChannel *proxy,
401 GObject *weak_object)
403 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
404 GError *error = NULL;
406 if (state == priv->state)
409 DEBUG ("File transfer state changed:\n"
410 "old state = %u, state = %u, reason = %u\n"
411 "\tincoming = %s, in_stream = %s, out_stream = %s",
412 priv->state, state, reason,
413 priv->incoming ? "yes" : "no",
414 priv->in_stream ? "present" : "not present",
415 priv->out_stream ? "present" : "not present");
418 priv->state_change_reason = reason;
420 /* If the channel is open AND we have the socket path, we can start the
421 * transfer. The socket path could be NULL if we are not doing the actual
422 * data transfer but are just an observer for the channel.
424 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
425 priv->unix_socket_path != NULL)
426 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
428 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
429 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
431 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
433 error = error_from_state_change_reason (priv->state_change_reason);
434 ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
435 g_clear_error (&error);
440 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
443 GObject *weak_object)
445 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
447 /* don't notify for 0 bytes count */
452 if (priv->progress_callback != NULL)
453 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
454 count, priv->progress_user_data);
458 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
459 const GValue *address,
462 GObject *weak_object)
464 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
465 GError *myerr = NULL;
466 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
468 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
474 /* if we were both cancelled and failed when calling the method,
475 * report the method error.
477 g_clear_error (&myerr);
478 myerr = g_error_copy (error);
484 DEBUG ("Error: %s", error->message);
485 ft_operation_close_with_error (tp_file, myerr);
486 g_clear_error (&myerr);
490 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
492 priv->unix_socket_path = g_value_dup_boxed (address);
494 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
496 /* Old bugged version of telepathy-salut used to store the address
497 * as a 's' instead of an 'ay' */
500 path = g_value_get_string (address);
501 priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
503 g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
506 DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
508 /* if the channel is already open, start the transfer now, otherwise,
509 * wait for the state change signal.
511 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
512 tp_file_start_transfer (tp_file);
516 file_read_async_cb (GObject *source,
520 GValue nothing = { 0 };
521 EmpathyTpFile *tp_file = user_data;
522 EmpathyTpFilePriv *priv;
523 GFileInputStream *in_stream;
524 GError *error = NULL;
526 priv = GET_PRIV (tp_file);
528 in_stream = g_file_read_finish (G_FILE (source), res, &error);
532 ft_operation_close_with_error (tp_file, error);
533 g_clear_error (&error);
537 priv->in_stream = G_INPUT_STREAM (in_stream);
539 g_value_init (¬hing, G_TYPE_STRING);
540 g_value_set_static_string (¬hing, "");
542 tp_cli_channel_type_file_transfer_call_provide_file (
544 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
545 ¬hing, ft_operation_provide_or_accept_file_cb,
546 NULL, NULL, G_OBJECT (tp_file));
550 file_replace_async_cb (GObject *source,
554 GValue nothing = { 0 };
555 EmpathyTpFile *tp_file = user_data;
556 EmpathyTpFilePriv *priv;
557 GError *error = NULL;
558 GFileOutputStream *out_stream;
560 priv = GET_PRIV (tp_file);
562 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
566 ft_operation_close_with_error (tp_file, error);
567 g_clear_error (&error);
572 priv->out_stream = G_OUTPUT_STREAM (out_stream);
574 g_value_init (¬hing, G_TYPE_STRING);
575 g_value_set_static_string (¬hing, "");
577 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
578 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
579 ¬hing, priv->offset,
580 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
584 channel_closed_cb (TpChannel *proxy,
587 GObject *weak_object)
589 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
590 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
591 gboolean cancel = GPOINTER_TO_INT (user_data);
593 DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
595 if (priv->cancellable != NULL &&
596 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
597 g_cancellable_cancel (priv->cancellable);
601 close_channel_internal (EmpathyTpFile *tp_file,
604 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
606 DEBUG ("Closing channel, should cancel %s", cancel ?
609 tp_cli_channel_call_close (priv->channel, -1,
610 channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
613 /* GObject methods */
616 empathy_tp_file_init (EmpathyTpFile *tp_file)
618 EmpathyTpFilePriv *priv;
620 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
621 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
623 tp_file->priv = priv;
627 do_dispose (GObject *object)
629 EmpathyTpFilePriv *priv = GET_PRIV (object);
631 if (priv->dispose_run)
634 priv->dispose_run = TRUE;
636 if (priv->channel != NULL)
638 g_signal_handlers_disconnect_by_func (priv->channel,
639 tp_file_invalidated_cb, object);
640 g_object_unref (priv->channel);
641 priv->channel = NULL;
644 if (priv->in_stream != NULL)
645 g_object_unref (priv->in_stream);
647 if (priv->out_stream != NULL)
648 g_object_unref (priv->out_stream);
650 if (priv->cancellable != NULL)
651 g_object_unref (priv->cancellable);
653 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
657 do_finalize (GObject *object)
659 EmpathyTpFilePriv *priv = GET_PRIV (object);
661 DEBUG ("%p", object);
663 if (priv->unix_socket_path != NULL)
665 g_array_free (priv->unix_socket_path, TRUE);
666 priv->unix_socket_path = NULL;
669 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
673 do_get_property (GObject *object,
678 EmpathyTpFilePriv *priv = GET_PRIV (object);
683 g_value_set_object (value, priv->channel);
686 g_value_set_boolean (value, priv->incoming);
689 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
695 do_set_property (GObject *object,
700 EmpathyTpFilePriv *priv = GET_PRIV (object);
704 priv->channel = g_object_ref (g_value_get_object (value));
707 priv->incoming = g_value_get_boolean (value);
710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
716 do_constructed (GObject *object)
718 EmpathyTpFile *tp_file;
719 EmpathyTpFilePriv *priv;
721 tp_file = EMPATHY_TP_FILE (object);
722 priv = GET_PRIV (tp_file);
724 g_signal_connect (priv->channel, "invalidated",
725 G_CALLBACK (tp_file_invalidated_cb), tp_file);
727 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
728 priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
730 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
731 priv->channel, tp_file_transferred_bytes_changed_cb,
732 NULL, NULL, object, NULL);
734 tp_cli_dbus_properties_call_get (priv->channel,
735 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
738 tp_cli_dbus_properties_call_get (priv->channel,
739 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
740 tp_file_get_available_socket_types_cb, NULL, NULL, file_obj);
742 priv->state_change_reason =
743 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
747 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
749 GObjectClass *object_class = G_OBJECT_CLASS (klass);
751 object_class->finalize = do_finalize;
752 object_class->dispose = do_dispose;
753 object_class->constructed = do_constructed;
754 object_class->get_property = do_get_property;
755 object_class->set_property = do_set_property;
757 /* Construct-only properties */
760 * EmpathyTpFile:channel:
762 * The #TpChannel requested for the file transfer.
764 g_object_class_install_property (object_class,
766 g_param_spec_object ("channel",
768 "The file transfer channel",
771 G_PARAM_CONSTRUCT_ONLY));
774 * EmpathyTpFile:incoming:
776 * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
778 g_object_class_install_property (object_class,
780 g_param_spec_boolean ("incoming",
781 "direction of transfer",
782 "The direction of the file being transferred",
785 G_PARAM_CONSTRUCT_ONLY));
787 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
793 * empathy_tp_file_new:
794 * @channel: a #TpChannel
795 * @incoming: whether the file transfer is incoming or not
797 * Creates a new #EmpathyTpFile wrapping @channel, with the direction
798 * specified by @incoming. The returned #EmpathyTpFile should be unrefed
799 * with g_object_unref() when finished with.
801 * Return value: a new #EmpathyTpFile
804 empathy_tp_file_new (TpChannel *channel,
807 EmpathyTpFile *tp_file;
809 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
811 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
812 "channel", channel, "incoming", incoming,
819 * empathy_tp_file_accept:
820 * @tp_file: an incoming #EmpathyTpFile
821 * @offset: the offset of @gfile where we should start writing
822 * @gfile: the destination #GFile for the transfer
823 * @cancellable: a #GCancellable
824 * @progress_callback: function to callback with progress information
825 * @progress_user_data: user_data to pass to @progress_callback
826 * @op_callback: function to callback when the transfer ends
827 * @op_user_data: user_data to pass to @op_callback
829 * Accepts an incoming file transfer, saving the result into @gfile.
830 * The callback @op_callback will be called both when the transfer is
831 * successful and in case of an error. Note that cancelling @cancellable,
832 * closes the socket of the file operation in progress, but doesn't
833 * guarantee that the transfer channel will be closed as well. Thus,
834 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
835 * actually cancel an ongoing #EmpathyTpFile.
838 empathy_tp_file_accept (EmpathyTpFile *tp_file,
841 GCancellable *cancellable,
842 EmpathyTpFileProgressCallback progress_callback,
843 gpointer progress_user_data,
844 EmpathyTpFileOperationCallback op_callback,
845 gpointer op_user_data)
847 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
849 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
850 g_return_if_fail (G_IS_FILE (gfile));
851 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
853 priv->cancellable = g_object_ref (cancellable);
854 priv->progress_callback = progress_callback;
855 priv->progress_user_data = progress_user_data;
856 priv->op_callback = op_callback;
857 priv->op_user_data = op_user_data;
858 priv->offset = offset;
860 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
861 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
866 * empathy_tp_file_offer:
867 * @tp_file: an outgoing #EmpathyTpFile
868 * @gfile: the source #GFile for the transfer
869 * @cancellable: a #GCancellable
870 * @progress_callback: function to callback with progress information
871 * @progress_user_data: user_data to pass to @progress_callback
872 * @op_callback: function to callback when the transfer ends
873 * @op_user_data: user_data to pass to @op_callback
875 * Offers an outgoing file transfer, reading data from @gfile.
876 * The callback @op_callback will be called both when the transfer is
877 * successful and in case of an error. Note that cancelling @cancellable,
878 * closes the socket of the file operation in progress, but doesn't
879 * guarantee that the transfer channel will be closed as well. Thus,
880 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
881 * actually cancel an ongoing #EmpathyTpFile.
884 empathy_tp_file_offer (EmpathyTpFile *tp_file,
886 GCancellable *cancellable,
887 EmpathyTpFileProgressCallback progress_callback,
888 gpointer progress_user_data,
889 EmpathyTpFileOperationCallback op_callback,
890 gpointer op_user_data)
892 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
894 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
895 g_return_if_fail (G_IS_FILE (gfile));
896 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
898 priv->cancellable = g_object_ref (cancellable);
899 priv->progress_callback = progress_callback;
900 priv->progress_user_data = progress_user_data;
901 priv->op_callback = op_callback;
902 priv->op_user_data = op_user_data;
904 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
905 file_read_async_cb, tp_file);
909 * empathy_tp_file_is_incoming:
910 * @tp_file: an #EmpathyTpFile
912 * Returns whether @tp_file is incoming.
914 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
917 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
919 EmpathyTpFilePriv *priv;
921 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
923 priv = GET_PRIV (tp_file);
925 return priv->incoming;
929 * empathy_tp_file_cancel:
930 * @tp_file: an #EmpathyTpFile
932 * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
933 * cancelling any I/O operation and closing the socket.
936 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
938 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
940 close_channel_internal (tp_file, TRUE);
944 * empathy_tp_file_close:
945 * @tp_file: an #EmpathyTpFile
947 * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
948 * method after the transfer has ended.
951 empathy_tp_file_close (EmpathyTpFile *tp_file)
953 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
955 close_channel_internal (tp_file, FALSE);