1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
4 * @author: Xavier Claessens <xclaesse@gmail.com>
5 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
27 #include <sys/socket.h>
30 #include <glib/gi18n.h>
33 #include <gio/gunixinputstream.h>
34 #include <gio/gunixoutputstream.h>
36 #include <libtelepathy/tp-conn.h>
37 #include <libtelepathy/tp-helpers.h>
38 #include <libtelepathy/tp-props-iface.h>
40 #include <telepathy-glib/proxy-subclass.h>
42 #include "empathy-tp-file.h"
43 #include "empathy-contact-factory.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 * @short_description: File channel
54 * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
55 * @include: libempthy/empathy-tp-file.h
57 * The #EmpathyTpFile object represents a Telepathy file channel.
61 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
63 * Value used for the "size" or "estimated-size" properties when the size of
64 * the transferred file is unknown.
67 static void empathy_tp_file_class_init (EmpathyTpFileClass *klass);
68 static void empathy_tp_file_init (EmpathyTpFile *tp_file);
69 static void tp_file_finalize (GObject *object);
70 static GObject *tp_file_constructor (GType type, guint n_props,
71 GObjectConstructParam *props);
72 static void tp_file_get_property (GObject *object, guint param_id,
73 GValue *value, GParamSpec *pspec);
74 static void tp_file_set_property (GObject *object, guint param_id,
75 const GValue *value, GParamSpec *pspec);
76 static void tp_file_destroy_cb (TpChannel *file_chan, EmpathyTpFile *tp_file);
77 static void tp_file_closed_cb (TpChannel *file_chan, EmpathyTpFile *tp_file,
78 GObject *weak_object);
79 static void tp_file_state_changed_cb (DBusGProxy *file_iface, guint state,
80 guint reason, EmpathyTpFile *tp_file);
81 static void tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
82 guint64 count, EmpathyTpFile *tp_file, GObject *weak_object);
83 static void copy_stream (GInputStream *in, GOutputStream *out,
84 GCancellable *cancellable);
86 /* EmpathyTpFile object */
88 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
89 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv))
91 typedef struct _EmpathyTpFilePriv EmpathyTpFilePriv;
93 struct _EmpathyTpFilePriv {
94 EmpathyContactFactory *factory;
100 EmpathyTpFile *cached_empathy_file;
101 EmpathyContact *contact;
102 GInputStream *in_stream;
103 GOutputStream *out_stream;
106 EmpFileTransferState state;
107 EmpFileTransferStateChangeReason state_change_reason;
109 guint64 transferred_bytes;
111 gchar *unix_socket_path;
113 EmpFileHashType content_hash_type;
116 GCancellable *cancellable;
128 PROP_TRANSFERRED_BYTES,
129 PROP_CONTENT_HASH_TYPE,
134 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
137 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
141 object_class->finalize = tp_file_finalize;
142 object_class->constructor = tp_file_constructor;
143 object_class->get_property = tp_file_get_property;
144 object_class->set_property = tp_file_set_property;
146 /* Construct-only properties */
147 g_object_class_install_property (object_class,
149 g_param_spec_object ("account",
151 "The account associated with the channel",
154 G_PARAM_CONSTRUCT_ONLY));
156 g_object_class_install_property (object_class,
158 g_param_spec_object ("channel",
160 "The file transfer channel",
163 G_PARAM_CONSTRUCT_ONLY));
165 g_object_class_install_property (object_class,
167 g_param_spec_uint ("state",
168 "state of the transfer",
169 "The file transfer state",
176 g_object_class_install_property (object_class,
178 g_param_spec_boolean ("incoming",
180 "Whether the transfer is incoming",
185 g_object_class_install_property (object_class,
187 g_param_spec_string ("filename",
188 "name of the transfer",
189 "The file transfer filename",
193 g_object_class_install_property (object_class,
195 g_param_spec_uint64 ("size",
197 "The file transfer size",
203 g_object_class_install_property (object_class,
205 g_param_spec_string ("content-type",
206 "file transfer content-type",
207 "The file transfer content-type",
211 g_object_class_install_property (object_class,
212 PROP_CONTENT_HASH_TYPE,
213 g_param_spec_uint ("content-hash-type",
214 "file transfer hash type",
215 "The type of the file transfer hash",
221 g_object_class_install_property (object_class,
223 g_param_spec_string ("content-hash",
224 "file transfer hash",
225 "The hash of the transfer's contents",
229 g_object_class_install_property (object_class,
230 PROP_TRANSFERRED_BYTES,
231 g_param_spec_uint64 ("transferred-bytes",
233 "The number of bytes transferred",
239 g_object_class_install_property (object_class,
241 g_param_spec_object ("in-stream",
242 "transfer input stream",
243 "The input stream for file transfer",
247 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
251 empathy_tp_file_init (EmpathyTpFile *tp_file)
256 tp_file_finalize (GObject *object)
258 EmpathyTpFilePriv *priv;
259 EmpathyTpFile *tp_file;
261 tp_file = EMPATHY_TP_FILE (object);
262 priv = GET_PRIV (tp_file);
266 DEBUG ("Closing channel..");
267 g_signal_handlers_disconnect_by_func (priv->channel,
268 tp_file_destroy_cb, object);
269 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
270 if (G_IS_OBJECT (priv->channel))
271 g_object_unref (priv->channel);
276 g_object_unref (priv->factory);
280 g_object_unref (priv->account);
284 g_object_unref (priv->mc);
288 g_free (priv->filename);
289 g_free (priv->unix_socket_path);
290 g_free (priv->description);
291 g_free (priv->content_hash);
292 g_free (priv->content_type);
295 g_object_unref (priv->in_stream);
297 if (priv->out_stream)
298 g_object_unref (priv->out_stream);
301 g_object_unref (priv->contact);
303 if (priv->cancellable)
304 g_object_unref (priv->cancellable);
306 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
310 tp_file_constructor (GType type,
312 GObjectConstructParam *props)
315 EmpathyTpFilePriv *priv;
316 GError *error = NULL;
317 GHashTable *properties;
320 tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
323 priv = GET_PRIV (tp_file);
325 priv->factory = empathy_contact_factory_new ();
326 priv->mc = empathy_mission_control_new ();
328 tp_cli_channel_connect_to_closed (priv->channel,
329 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
333 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
334 TP_PROXY (priv->channel),
335 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
336 tp_file_state_changed_cb,
340 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
341 TP_PROXY (priv->channel),
342 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
343 tp_file_transferred_bytes_changed_cb,
348 handle = tp_channel_get_handle (priv->channel, NULL);
349 priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
353 if (!tp_cli_dbus_properties_run_get_all (priv->channel,
354 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
356 DEBUG ("Failed to get properties: %s",
357 error ? error->message : "No error given");
358 g_clear_error (&error);
362 priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
364 priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
366 /* Invalid reason, so empathy_file_get_state_change_reason() can give
367 * a warning if called for a not closed file transfer. */
368 priv->state_change_reason = -1;
370 priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
371 properties, "TransferredBytes"));
373 priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
376 priv->content_hash = g_value_dup_string (g_hash_table_lookup (properties,
379 priv->description = g_value_dup_string (g_hash_table_lookup (properties,
382 priv->unix_socket_path = g_value_dup_string (g_hash_table_lookup (properties,
385 if (priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
386 priv->incoming = TRUE;
388 g_hash_table_destroy (properties);
394 tp_file_get_property (GObject *object,
399 EmpathyTpFilePriv *priv;
400 EmpathyTpFile *tp_file;
402 priv = GET_PRIV (object);
403 tp_file = EMPATHY_TP_FILE (object);
408 g_value_set_object (value, priv->account);
411 g_value_set_object (value, priv->channel);
414 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
420 tp_file_channel_set_dbus_property (gpointer proxy,
421 const gchar *property,
424 DEBUG ("Setting %s property", property);
425 tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
427 EMP_IFACE_CHANNEL_TYPE_FILE,
436 tp_file_set_property (GObject *object,
441 EmpathyTpFilePriv *priv;
443 priv = GET_PRIV (object);
448 priv->account = g_object_ref (g_value_get_object (value));
451 priv->channel = g_object_ref (g_value_get_object (value));
454 priv->state = g_value_get_uint (value);
457 priv->incoming = g_value_get_boolean (value);
460 g_free (priv->filename);
461 priv->filename = g_value_dup_string (value);
462 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
465 priv->size = g_value_get_uint64 (value);
466 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
468 case PROP_CONTENT_TYPE:
469 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
470 g_free (priv->content_type);
471 priv->content_type = g_value_dup_string (value);
473 case PROP_CONTENT_HASH:
474 tp_file_channel_set_dbus_property (priv->channel, "ContentHash", value);
475 g_free (priv->content_hash);
476 priv->content_hash = g_value_dup_string (value);
480 g_object_unref (priv->in_stream);
481 priv->in_stream = g_object_ref (g_value_get_object (value));
484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
490 * empathy_tp_file_new:
491 * @account: the #McAccount for the channel
492 * @channel: a Telepathy channel
494 * Creates a new #EmpathyTpFile wrapping @channel.
496 * Returns: a new #EmpathyTpFile
499 empathy_tp_file_new (McAccount *account,
502 return g_object_new (EMPATHY_TYPE_TP_FILE,
509 * empathy_tp_file_get_id:
510 * @tp_file: an #EmpathyTpFile
512 * Returns the ID of @tp_file.
517 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
519 EmpathyTpFilePriv *priv;
521 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
523 priv = GET_PRIV (tp_file);
529 * empathy_tp_file_get_channel
530 * @tp_file: an #EmpathyTpFile
532 * Returns the Telepathy file transfer channel
534 * Returns: the #TpChannel
537 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
539 EmpathyTpFilePriv *priv;
541 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
543 priv = GET_PRIV (tp_file);
545 return priv->channel;
549 tp_file_destroy_cb (TpChannel *file_channel,
550 EmpathyTpFile *tp_file)
552 EmpathyTpFilePriv *priv;
554 priv = GET_PRIV (tp_file);
556 DEBUG ("Channel Closed or CM crashed");
558 g_object_unref (priv->channel);
559 priv->channel = NULL;
563 tp_file_closed_cb (TpChannel *file_channel,
564 EmpathyTpFile *tp_file,
565 GObject *weak_object)
567 EmpathyTpFilePriv *priv;
569 priv = GET_PRIV (tp_file);
571 /* The channel is closed, do just like if the proxy was destroyed */
572 g_signal_handlers_disconnect_by_func (priv->channel,
575 tp_file_destroy_cb (file_channel, tp_file);
583 g_get_current_time (&tv);
584 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
588 _get_local_socket (EmpathyTpFile *tp_file)
592 struct sockaddr_un addr;
593 EmpathyTpFilePriv *priv;
596 priv = GET_PRIV (tp_file);
598 /* TODO: This could probably be a little nicer. */
599 tp_cli_dbus_properties_run_get (priv->channel,
601 EMP_IFACE_CHANNEL_TYPE_FILE,
607 if (priv->unix_socket_path)
608 g_free (priv->unix_socket_path);
610 priv->unix_socket_path = g_value_dup_string (socket_path);
611 g_value_unset (socket_path);
613 if (G_STR_EMPTY (priv->unix_socket_path))
616 fd = socket (PF_UNIX, SOCK_STREAM, 0);
620 memset (&addr, 0, sizeof (addr));
621 addr.sun_family = AF_UNIX;
622 path_len = strlen (priv->unix_socket_path);
623 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
625 if (connect (fd, (struct sockaddr*) &addr,
636 * empathy_tp_file_accept:
637 * @tp_file: an #EmpathyTpFile
639 * Accepts a file transfer that's in the "local pending" state (i.e.
640 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
643 empathy_tp_file_accept (EmpathyTpFile *tp_file)
645 EmpathyTpFilePriv *priv;
647 GValue nothing = { 0 };
648 GError *error = NULL;
650 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
652 priv = GET_PRIV (tp_file);
654 g_return_if_fail (priv->out_stream != NULL);
656 DEBUG ("Accepting file: filename=%s", priv->filename);
658 g_value_init (¬hing, G_TYPE_STRING);
659 g_value_set_string (¬hing, "");
661 if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
662 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
663 ¬hing, &address, &error, NULL))
665 DEBUG ("Accept error: %s",
666 error ? error->message : "No message given");
667 g_clear_error (&error);
670 if (priv->unix_socket_path)
671 g_free (priv->unix_socket_path);
673 priv->unix_socket_path = g_value_dup_string (address);
674 g_value_unset (address);
676 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
680 receive_tp_file (EmpathyTpFile *tp_file)
682 EmpathyTpFilePriv *priv;
683 GInputStream *socket_stream;
686 priv = GET_PRIV (tp_file);
688 socket_fd = _get_local_socket (tp_file);
693 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
695 priv->cancellable = g_cancellable_new ();
697 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
699 g_object_unref (socket_stream);
704 send_tp_file (EmpathyTpFile *tp_file)
707 GOutputStream *socket_stream;
708 EmpathyTpFilePriv *priv;
710 priv = GET_PRIV (tp_file);
712 DEBUG ("Sending file content: filename=%s",
715 g_return_if_fail (priv->in_stream);
717 socket_fd = _get_local_socket (tp_file);
720 DEBUG ("failed to get local socket fd");
723 DEBUG ("got local socket fd");
724 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
726 priv->cancellable = g_cancellable_new ();
728 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
730 g_object_unref (socket_stream);
734 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
735 EmpFileTransferState state,
736 EmpFileTransferStateChangeReason reason,
737 EmpathyTpFile *tp_file)
739 EmpathyTpFilePriv *priv;
741 priv = GET_PRIV (tp_file);
743 DEBUG ("File transfer state changed: filename=%s, "
744 "old state=%u, state=%u, reason=%u",
745 priv->filename, priv->state, state, reason);
747 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
748 priv->start_time = get_time_msec ();
750 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
751 state, priv->incoming ? "yes" : "no",
752 priv->in_stream ? "present" : "not present",
753 priv->out_stream ? "present" : "not present");
755 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
757 send_tp_file (tp_file);
758 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
760 receive_tp_file (tp_file);
763 priv->state_change_reason = reason;
765 g_object_notify (G_OBJECT (tp_file), "state");
769 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
771 EmpathyTpFile *tp_file,
772 GObject *weak_object)
774 EmpathyTpFilePriv *priv;
776 priv = GET_PRIV (tp_file);
778 if (priv->transferred_bytes == count)
781 priv->transferred_bytes = count;
783 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
787 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
789 EmpathyTpFilePriv *priv;
791 priv = GET_PRIV (tp_file);
793 return priv->contact;
797 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
799 EmpathyTpFilePriv *priv;
801 priv = GET_PRIV (tp_file);
803 return priv->in_stream;
807 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
809 EmpathyTpFilePriv *priv;
811 priv = GET_PRIV (tp_file);
813 return priv->out_stream;
817 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
819 EmpathyTpFilePriv *priv;
821 priv = GET_PRIV (tp_file);
823 return priv->filename;
827 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
829 EmpathyTpFilePriv *priv;
831 priv = GET_PRIV (tp_file);
833 return priv->incoming;
837 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
839 EmpathyTpFilePriv *priv;
841 priv = GET_PRIV (tp_file);
846 EmpFileTransferStateChangeReason
847 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
849 EmpathyTpFilePriv *priv;
851 priv = GET_PRIV (tp_file);
853 g_return_val_if_fail (priv->state_change_reason >= 0,
854 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
856 return priv->state_change_reason;
860 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
862 EmpathyTpFilePriv *priv;
864 priv = GET_PRIV (tp_file);
870 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
872 EmpathyTpFilePriv *priv;
874 priv = GET_PRIV (tp_file);
876 return priv->transferred_bytes;
880 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
882 EmpathyTpFilePriv *priv;
883 gint64 curr_time, elapsed_time;
884 gdouble time_per_byte;
885 gdouble remaining_time;
887 priv = GET_PRIV (tp_file);
889 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
892 if (priv->transferred_bytes == priv->size)
895 curr_time = get_time_msec ();
896 elapsed_time = curr_time - priv->start_time;
897 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
898 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
900 return (gint) (remaining_time + 0.5);
904 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
906 EmpathyTpFilePriv *priv;
908 priv = GET_PRIV (tp_file);
910 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
912 g_cancellable_cancel (priv->cancellable);
916 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
917 GInputStream *in_stream)
919 EmpathyTpFilePriv *priv;
921 priv = GET_PRIV (tp_file);
923 if (priv->in_stream == in_stream)
927 g_warning ("Setting an input stream for incoming file "
928 "transfers is useless");
931 g_object_unref (priv->in_stream);
934 g_object_ref (in_stream);
936 priv->in_stream = in_stream;
938 g_object_notify (G_OBJECT (tp_file), "in-stream");
942 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
943 GOutputStream *out_stream)
945 EmpathyTpFilePriv *priv;
947 priv = GET_PRIV (tp_file);
949 if (priv->out_stream == out_stream)
953 g_warning ("Setting an output stream for outgoing file "
954 "transfers is useless");
956 if (priv->out_stream)
957 g_object_unref (priv->out_stream);
960 g_object_ref (out_stream);
962 priv->out_stream = out_stream;
966 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
967 const gchar *filename)
969 EmpathyTpFilePriv *priv;
971 priv = GET_PRIV (tp_file);
973 g_return_if_fail (filename != NULL);
975 if (priv->filename && strcmp (filename, priv->filename) == 0)
978 g_free (priv->filename);
979 priv->filename = g_strdup (filename);
981 g_object_notify (G_OBJECT (tp_file), "filename");
984 /* Functions to copy the content of a GInputStream to a GOutputStream */
987 #define BUFFER_SIZE 4096
992 GCancellable *cancellable;
993 char *buff[N_BUFFERS]; /* the temporary buffers */
994 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
995 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
996 gint curr_read; /* index of the buffer used for reading */
997 gint curr_write; /* index of the buffer used for writing */
998 gboolean is_reading; /* we are reading */
999 gboolean is_writing; /* we are writing */
1000 guint n_closed; /* number of streams that have been closed */
1003 static void schedule_next (CopyData *copy);
1006 free_copy_data_if_closed (CopyData *copy)
1010 /* Free the data only if both the input and output streams have
1013 if (copy->n_closed < 2)
1016 if (copy->in != NULL)
1017 g_object_unref (copy->in);
1019 if (copy->out != NULL)
1020 g_object_unref (copy->out);
1022 for (i = 0; i < N_BUFFERS; i++)
1023 g_free (copy->buff[i]);
1025 g_object_unref (copy->cancellable);
1030 io_error (CopyData *copy,
1033 g_cancellable_cancel (copy->cancellable);
1036 g_warning ("I/O error");
1037 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1038 ; /* Ignore cancellations */
1040 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1042 if (copy->in != NULL)
1043 g_input_stream_close (copy->in, NULL, NULL);
1045 if (copy->out != NULL)
1046 g_output_stream_close (copy->out, NULL, NULL);
1048 free_copy_data_if_closed (copy);
1052 close_done (GObject *source_object,
1056 CopyData *copy = user_data;
1058 g_object_unref (source_object);
1059 free_copy_data_if_closed (copy);
1063 write_done_cb (GObject *source_object,
1067 CopyData *copy = user_data;
1069 GError *error = NULL;
1071 count_write = g_output_stream_write_finish (copy->out, res, &error);
1073 if (count_write <= 0)
1075 io_error (copy, error);
1076 g_error_free (error);
1080 copy->is_full[copy->curr_write] = FALSE;
1081 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1082 copy->is_writing = FALSE;
1084 schedule_next (copy);
1088 read_done_cb (GObject *source_object,
1092 CopyData *copy = user_data;
1094 GError *error = NULL;
1096 count_read = g_input_stream_read_finish (copy->in, res, &error);
1098 if (count_read == 0)
1100 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1104 else if (count_read < 0)
1106 io_error (copy, error);
1107 g_error_free (error);
1111 copy->count[copy->curr_read] = count_read;
1112 copy->is_full[copy->curr_read] = TRUE;
1113 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1114 copy->is_reading = FALSE;
1116 schedule_next (copy);
1120 schedule_next (CopyData *copy)
1122 if (copy->in != NULL &&
1123 !copy->is_reading &&
1124 !copy->is_full[copy->curr_read])
1126 /* We are not reading and the current buffer is empty, so
1127 * start an async read. */
1128 copy->is_reading = TRUE;
1129 g_input_stream_read_async (copy->in,
1130 copy->buff[copy->curr_read],
1131 BUFFER_SIZE, 0, copy->cancellable,
1132 read_done_cb, copy);
1135 if (!copy->is_writing &&
1136 copy->is_full[copy->curr_write])
1138 if (copy->count[copy->curr_write] == 0)
1140 /* The last read on the buffer read 0 bytes, this
1141 * means that we got an EOF, so we can close
1142 * the output channel. */
1143 g_output_stream_close_async (copy->out, 0,
1150 /* We are not writing and the current buffer contains
1151 * data, so start an async write. */
1152 copy->is_writing = TRUE;
1153 g_output_stream_write_async (copy->out,
1154 copy->buff[copy->curr_write],
1155 copy->count[copy->curr_write],
1156 0, copy->cancellable,
1157 write_done_cb, copy);
1163 copy_stream (GInputStream *in,
1165 GCancellable *cancellable)
1170 g_return_if_fail (in != NULL);
1171 g_return_if_fail (out != NULL);
1173 copy = g_new0 (CopyData, 1);
1174 copy->in = g_object_ref (in);
1175 copy->out = g_object_ref (out);
1177 if (cancellable != NULL)
1178 copy->cancellable = g_object_ref (cancellable);
1180 copy->cancellable = g_cancellable_new ();
1182 for (i = 0; i < N_BUFFERS; i++)
1183 copy->buff[i] = g_malloc (BUFFER_SIZE);
1185 schedule_next (copy);