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 */
71 GInputStream *in_stream;
72 GOutputStream *out_stream;
74 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
75 TpFileTransferState state;
76 TpFileTransferStateChangeReason state_change_reason;
77 TpSocketAddressType socket_address_type;
78 TpSocketAccessControl socket_access_control;
80 /* transfer properties */
83 GArray *socket_address;
87 /* GCancellable we're passed when offering/accepting the transfer */
88 GCancellable *cancellable;
90 /* callbacks for the operation */
91 EmpathyTpFileProgressCallback progress_callback;
92 gpointer progress_user_data;
93 EmpathyTpFileOperationCallback op_callback;
94 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 EmpathyTpFilePriv *priv;
246 GError *error = NULL;
249 priv = GET_PRIV (tp_file);
251 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
253 DEBUG ("Splice stream ready cb, error %p", error);
255 if (error != NULL && !priv->is_closing)
257 ft_operation_close_with_error (tp_file, error);
258 g_clear_error (&error);
264 tp_file_start_transfer (EmpathyTpFile *tp_file)
266 gint fd, domain, res = 0;
267 GError *error = NULL;
268 struct sockaddr *my_addr = NULL;
270 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
272 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
276 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
282 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
283 EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
285 DEBUG ("Socket not supported, closing channel");
287 ft_operation_close_with_error (tp_file, error);
288 g_clear_error (&error);
293 fd = socket (domain, SOCK_STREAM, 0);
299 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
300 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
302 DEBUG ("Failed to create socket, closing channel");
304 ft_operation_close_with_error (tp_file, error);
305 g_clear_error (&error);
310 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
312 struct sockaddr_un addr;
314 memset (&addr, 0, sizeof (addr));
315 addr.sun_family = domain;
316 strncpy (addr.sun_path, priv->socket_address->data,
317 priv->socket_address->len);
319 my_addr = (struct sockaddr *) &addr;
320 my_size = sizeof (addr);
322 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
324 struct sockaddr_in addr;
326 memset (&addr, 0, sizeof (addr));
327 addr.sin_family = domain;
328 inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr);
329 addr.sin_port = htons (priv->port);
331 my_addr = (struct sockaddr *) &addr;
332 my_size = sizeof (addr);
335 res = connect (fd, my_addr, my_size);
341 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
342 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
344 DEBUG ("Failed to connect socket, closing channel");
346 ft_operation_close_with_error (tp_file, error);
348 g_clear_error (&error);
353 DEBUG ("Start the transfer");
355 priv->start_time = empathy_time_get_current ();
357 /* notify we're starting a transfer */
358 if (priv->progress_callback != NULL)
359 priv->progress_callback (tp_file, 0, priv->progress_user_data);
363 GInputStream *socket_stream;
365 socket_stream = g_unix_input_stream_new (fd, TRUE);
367 g_output_stream_splice_async (priv->out_stream, socket_stream,
368 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
369 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
370 G_PRIORITY_DEFAULT, priv->cancellable,
371 splice_stream_ready_cb, tp_file);
373 g_object_unref (socket_stream);
377 GOutputStream *socket_stream;
379 socket_stream = g_unix_output_stream_new (fd, TRUE);
381 g_output_stream_splice_async (socket_stream, priv->in_stream,
382 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
383 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
384 G_PRIORITY_DEFAULT, priv->cancellable,
385 splice_stream_ready_cb, tp_file);
387 g_object_unref (socket_stream);
392 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
395 GError *retval = NULL;
401 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
402 string = _("No reason was specified");
404 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
405 string = _("The change in state was requested");
407 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
408 string = _("You canceled the file transfer");
410 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
411 string = _("The other participant canceled the file transfer");
413 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
414 string = _("Error while trying to transfer the file");
416 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
417 string = _("The other participant is unable to transfer the file");
420 string = _("Unknown reason");
424 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
425 EMPATHY_FT_ERROR_TP_ERROR, string);
431 tp_file_state_changed_cb (TpChannel *proxy,
435 GObject *weak_object)
437 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
438 GError *error = NULL;
440 if (state == priv->state)
443 DEBUG ("File transfer state changed:\n"
444 "old state = %u, state = %u, reason = %u\n"
445 "\tincoming = %s, in_stream = %s, out_stream = %s",
446 priv->state, state, reason,
447 priv->incoming ? "yes" : "no",
448 priv->in_stream ? "present" : "not present",
449 priv->out_stream ? "present" : "not present");
452 priv->state_change_reason = reason;
454 /* If the channel is open AND we have the socket path, we can start the
455 * transfer. The socket path could be NULL if we are not doing the actual
456 * data transfer but are just an observer for the channel.
458 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
459 priv->socket_address != NULL)
460 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
462 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
463 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
465 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
467 error = error_from_state_change_reason (priv->state_change_reason);
468 ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
469 g_clear_error (&error);
474 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
477 GObject *weak_object)
479 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
481 /* don't notify for 0 bytes count */
486 if (priv->progress_callback != NULL)
487 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
488 count, priv->progress_user_data);
492 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
493 const GValue *address,
496 GObject *weak_object)
498 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
499 GError *myerr = NULL;
500 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
502 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
508 /* if we were both cancelled and failed when calling the method,
509 * report the method error.
511 g_clear_error (&myerr);
514 myerr = g_error_copy (error);
519 DEBUG ("Error: %s", myerr->message);
520 ft_operation_close_with_error (tp_file, myerr);
521 g_clear_error (&myerr);
525 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
527 priv->socket_address = g_value_dup_boxed (address);
529 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
531 /* Old bugged version of telepathy-salut used to store the address
532 * as a 's' instead of an 'ay' */
535 path = g_value_get_string (address);
536 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
538 g_array_insert_vals (priv->socket_address, 0, path, strlen (path));
540 else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)
542 GValueArray *val_array;
546 val_array = g_value_get_boxed (address);
549 v = g_value_array_get_nth (val_array, 0);
550 addr = g_value_get_string (v);
551 priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
553 g_array_insert_vals (priv->socket_address, 0, addr, strlen (addr));
556 v = g_value_array_get_nth (val_array, 1);
557 priv->port = g_value_get_uint (v);
560 DEBUG ("Got socket address: %s, port (not zero if IPV4): %d",
561 priv->socket_address->data, priv->port);
563 /* if the channel is already open, start the transfer now, otherwise,
564 * wait for the state change signal.
566 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
567 tp_file_start_transfer (tp_file);
571 initialize_empty_ac_variant (TpSocketAccessControl ac,
574 /* TODO: we will add more types here once we support PORT access control. */
575 if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
577 g_value_init (val, G_TYPE_STRING);
578 g_value_set_static_string (val, "");
583 file_read_async_cb (GObject *source,
587 GValue nothing = { 0 };
588 EmpathyTpFile *tp_file = user_data;
589 EmpathyTpFilePriv *priv;
590 GFileInputStream *in_stream;
591 GError *error = NULL;
593 priv = GET_PRIV (tp_file);
595 in_stream = g_file_read_finish (G_FILE (source), res, &error);
597 if (error != NULL && !priv->is_closing)
599 ft_operation_close_with_error (tp_file, error);
600 g_clear_error (&error);
604 priv->in_stream = G_INPUT_STREAM (in_stream);
606 /* we don't impose specific interface/port requirements even
607 * if we're not using UNIX sockets.
609 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
611 tp_cli_channel_type_file_transfer_call_provide_file (
613 priv->socket_address_type, priv->socket_access_control,
614 ¬hing, ft_operation_provide_or_accept_file_cb,
615 NULL, NULL, G_OBJECT (tp_file));
619 file_replace_async_cb (GObject *source,
623 GValue nothing = { 0 };
624 EmpathyTpFile *tp_file = user_data;
625 EmpathyTpFilePriv *priv;
626 GError *error = NULL;
627 GFileOutputStream *out_stream;
629 priv = GET_PRIV (tp_file);
631 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
635 ft_operation_close_with_error (tp_file, error);
636 g_clear_error (&error);
641 priv->out_stream = G_OUTPUT_STREAM (out_stream);
643 /* we don't impose specific interface/port requirements even
644 * if we're not using UNIX sockets.
646 initialize_empty_ac_variant (priv->socket_access_control, ¬hing);
648 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
649 -1, priv->socket_address_type, priv->socket_access_control,
650 ¬hing, priv->offset,
651 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
655 channel_closed_cb (TpChannel *proxy,
658 GObject *weak_object)
660 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
661 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
662 gboolean cancel = GPOINTER_TO_INT (user_data);
664 DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
666 if (priv->cancellable != NULL &&
667 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
668 g_cancellable_cancel (priv->cancellable);
672 close_channel_internal (EmpathyTpFile *tp_file,
675 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
677 DEBUG ("Closing channel, should cancel %s", cancel ?
680 priv->is_closing = TRUE;
682 tp_cli_channel_call_close (priv->channel, -1,
683 channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
686 /* GObject methods */
689 empathy_tp_file_init (EmpathyTpFile *tp_file)
691 EmpathyTpFilePriv *priv;
693 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
694 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
696 tp_file->priv = priv;
700 do_dispose (GObject *object)
702 EmpathyTpFilePriv *priv = GET_PRIV (object);
704 if (priv->dispose_run)
707 priv->dispose_run = TRUE;
709 if (priv->channel != NULL)
711 g_signal_handlers_disconnect_by_func (priv->channel,
712 tp_file_invalidated_cb, object);
713 g_object_unref (priv->channel);
714 priv->channel = NULL;
717 if (priv->in_stream != NULL)
718 g_object_unref (priv->in_stream);
720 if (priv->out_stream != NULL)
721 g_object_unref (priv->out_stream);
723 if (priv->cancellable != NULL)
724 g_object_unref (priv->cancellable);
726 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
730 do_finalize (GObject *object)
732 EmpathyTpFilePriv *priv = GET_PRIV (object);
734 DEBUG ("%p", object);
736 if (priv->socket_address != NULL)
738 g_array_free (priv->socket_address, TRUE);
739 priv->socket_address = NULL;
742 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
746 do_get_property (GObject *object,
751 EmpathyTpFilePriv *priv = GET_PRIV (object);
756 g_value_set_object (value, priv->channel);
759 g_value_set_boolean (value, priv->incoming);
762 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
768 do_set_property (GObject *object,
773 EmpathyTpFilePriv *priv = GET_PRIV (object);
777 priv->channel = g_object_ref (g_value_get_object (value));
780 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
786 do_constructed (GObject *object)
788 EmpathyTpFile *tp_file;
789 EmpathyTpFilePriv *priv;
791 tp_file = EMPATHY_TP_FILE (object);
792 priv = GET_PRIV (tp_file);
794 g_signal_connect (priv->channel, "invalidated",
795 G_CALLBACK (tp_file_invalidated_cb), tp_file);
797 priv->incoming = !tp_channel_get_requested (priv->channel);
799 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
800 priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
802 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
803 priv->channel, tp_file_transferred_bytes_changed_cb,
804 NULL, NULL, object, NULL);
806 tp_cli_dbus_properties_call_get (priv->channel,
807 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
810 tp_cli_dbus_properties_call_get (priv->channel,
811 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
812 tp_file_get_available_socket_types_cb, NULL, NULL, object);
814 priv->state_change_reason =
815 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
819 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
821 GObjectClass *object_class = G_OBJECT_CLASS (klass);
823 object_class->finalize = do_finalize;
824 object_class->dispose = do_dispose;
825 object_class->constructed = do_constructed;
826 object_class->get_property = do_get_property;
827 object_class->set_property = do_set_property;
829 /* Construct-only properties */
832 * EmpathyTpFile:channel:
834 * The #TpChannel requested for the file transfer.
836 g_object_class_install_property (object_class,
838 g_param_spec_object ("channel",
840 "The file transfer channel",
843 G_PARAM_CONSTRUCT_ONLY));
846 * EmpathyTpFile:incoming:
848 * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
850 g_object_class_install_property (object_class,
852 g_param_spec_boolean ("incoming",
853 "direction of transfer",
854 "The direction of the file being transferred",
858 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
864 * empathy_tp_file_new:
865 * @channel: a #TpChannel
866 * @incoming: whether the file transfer is incoming or not
868 * Creates a new #EmpathyTpFile wrapping @channel.
869 * The returned #EmpathyTpFile should be unrefed
870 * with g_object_unref() when finished with.
872 * Return value: a new #EmpathyTpFile
875 empathy_tp_file_new (TpChannel *channel)
877 EmpathyTpFile *tp_file;
879 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
881 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
889 * empathy_tp_file_accept:
890 * @tp_file: an incoming #EmpathyTpFile
891 * @offset: the offset of @gfile where we should start writing
892 * @gfile: the destination #GFile for the transfer
893 * @cancellable: a #GCancellable
894 * @progress_callback: function to callback with progress information
895 * @progress_user_data: user_data to pass to @progress_callback
896 * @op_callback: function to callback when the transfer ends
897 * @op_user_data: user_data to pass to @op_callback
899 * Accepts an incoming file transfer, saving the result into @gfile.
900 * The callback @op_callback will be called both when the transfer is
901 * successful and in case of an error. Note that cancelling @cancellable,
902 * closes the socket of the file operation in progress, but doesn't
903 * guarantee that the transfer channel will be closed as well. Thus,
904 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
905 * actually cancel an ongoing #EmpathyTpFile.
908 empathy_tp_file_accept (EmpathyTpFile *tp_file,
911 GCancellable *cancellable,
912 EmpathyTpFileProgressCallback progress_callback,
913 gpointer progress_user_data,
914 EmpathyTpFileOperationCallback op_callback,
915 gpointer op_user_data)
917 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
919 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
920 g_return_if_fail (G_IS_FILE (gfile));
921 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
923 priv->cancellable = g_object_ref (cancellable);
924 priv->progress_callback = progress_callback;
925 priv->progress_user_data = progress_user_data;
926 priv->op_callback = op_callback;
927 priv->op_user_data = op_user_data;
928 priv->offset = offset;
930 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
931 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
936 * empathy_tp_file_offer:
937 * @tp_file: an outgoing #EmpathyTpFile
938 * @gfile: the source #GFile for the transfer
939 * @cancellable: a #GCancellable
940 * @progress_callback: function to callback with progress information
941 * @progress_user_data: user_data to pass to @progress_callback
942 * @op_callback: function to callback when the transfer ends
943 * @op_user_data: user_data to pass to @op_callback
945 * Offers an outgoing file transfer, reading data from @gfile.
946 * The callback @op_callback will be called both when the transfer is
947 * successful and in case of an error. Note that cancelling @cancellable,
948 * closes the socket of the file operation in progress, but doesn't
949 * guarantee that the transfer channel will be closed as well. Thus,
950 * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
951 * actually cancel an ongoing #EmpathyTpFile.
954 empathy_tp_file_offer (EmpathyTpFile *tp_file,
956 GCancellable *cancellable,
957 EmpathyTpFileProgressCallback progress_callback,
958 gpointer progress_user_data,
959 EmpathyTpFileOperationCallback op_callback,
960 gpointer op_user_data)
962 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
964 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
965 g_return_if_fail (G_IS_FILE (gfile));
966 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
968 priv->cancellable = g_object_ref (cancellable);
969 priv->progress_callback = progress_callback;
970 priv->progress_user_data = progress_user_data;
971 priv->op_callback = op_callback;
972 priv->op_user_data = op_user_data;
974 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
975 file_read_async_cb, tp_file);
979 * empathy_tp_file_is_incoming:
980 * @tp_file: an #EmpathyTpFile
982 * Returns whether @tp_file is incoming.
984 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
987 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
989 EmpathyTpFilePriv *priv;
991 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
993 priv = GET_PRIV (tp_file);
995 return priv->incoming;
999 * empathy_tp_file_cancel:
1000 * @tp_file: an #EmpathyTpFile
1002 * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
1003 * cancelling any I/O operation and closing the socket.
1006 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1008 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1010 close_channel_internal (tp_file, TRUE);
1014 * empathy_tp_file_close:
1015 * @tp_file: an #EmpathyTpFile
1017 * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
1018 * method after the transfer has ended.
1021 empathy_tp_file_close (EmpathyTpFile *tp_file)
1023 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1025 close_channel_internal (tp_file, FALSE);