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;
105 EmpFileTransferDirection direction;
106 EmpFileTransferState state;
107 EmpFileTransferStateChangeReason state_change_reason;
109 guint64 transferred_bytes;
111 gchar *unix_socket_path;
115 GCancellable *cancellable;
127 PROP_TRANSFERRED_BYTES,
132 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
135 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
139 object_class->finalize = tp_file_finalize;
140 object_class->constructor = tp_file_constructor;
141 object_class->get_property = tp_file_get_property;
142 object_class->set_property = tp_file_set_property;
144 /* Construct-only properties */
145 g_object_class_install_property (object_class,
147 g_param_spec_object ("account",
149 "The account associated with the channel",
152 G_PARAM_CONSTRUCT_ONLY));
154 g_object_class_install_property (object_class,
156 g_param_spec_object ("channel",
158 "The file transfer channel",
161 G_PARAM_CONSTRUCT_ONLY));
163 g_object_class_install_property (object_class,
165 g_param_spec_uint ("state",
166 "state of the transfer",
167 "The file transfer state",
174 g_object_class_install_property (object_class,
176 g_param_spec_uint ("direction",
177 "direction of the transfer",
178 "The file transfer direction",
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,
213 g_param_spec_string ("content-md5",
214 "file transfer md5sum",
215 "The md5 sum of the transfer's contents",
219 g_object_class_install_property (object_class,
220 PROP_TRANSFERRED_BYTES,
221 g_param_spec_uint64 ("transferred-bytes",
223 "The number of bytes transferred",
229 g_object_class_install_property (object_class,
231 g_param_spec_object ("in-stream",
232 "transfer input stream",
233 "The input stream for file transfer",
237 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
241 empathy_tp_file_init (EmpathyTpFile *tp_file)
246 tp_file_finalize (GObject *object)
248 EmpathyTpFilePriv *priv;
249 EmpathyTpFile *tp_file;
251 tp_file = EMPATHY_TP_FILE (object);
252 priv = GET_PRIV (tp_file);
256 DEBUG ("Closing channel..");
257 g_signal_handlers_disconnect_by_func (priv->channel,
258 tp_file_destroy_cb, object);
259 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
260 if (G_IS_OBJECT (priv->channel))
261 g_object_unref (priv->channel);
266 g_object_unref (priv->factory);
270 g_object_unref (priv->account);
274 g_object_unref (priv->mc);
278 g_free (priv->filename);
279 g_free (priv->unix_socket_path);
280 g_free (priv->description);
281 g_free (priv->content_md5);
282 g_free (priv->content_type);
285 g_object_unref (priv->in_stream);
287 if (priv->out_stream)
288 g_object_unref (priv->out_stream);
291 g_object_unref (priv->contact);
293 if (priv->cancellable)
294 g_object_unref (priv->cancellable);
296 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
300 tp_file_constructor (GType type,
302 GObjectConstructParam *props)
305 EmpathyTpFilePriv *priv;
306 GError *error = NULL;
307 GHashTable *properties;
310 tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
313 priv = GET_PRIV (tp_file);
315 priv->factory = empathy_contact_factory_new ();
316 priv->mc = empathy_mission_control_new ();
318 tp_cli_channel_connect_to_closed (priv->channel,
319 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
323 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
324 TP_PROXY (priv->channel),
325 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
326 tp_file_state_changed_cb,
330 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
331 TP_PROXY (priv->channel),
332 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
333 tp_file_transferred_bytes_changed_cb,
338 handle = tp_channel_get_handle (priv->channel, NULL);
339 priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
343 if (!tp_cli_dbus_properties_run_get_all (priv->channel,
344 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
346 DEBUG ("Failed to get properties: %s",
347 error ? error->message : "No error given");
348 g_clear_error (&error);
352 priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
354 priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
356 /* Invalid reason, so empathy_file_get_state_change_reason() can give
357 * a warning if called for a not closed file transfer. */
358 priv->state_change_reason = -1;
360 priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
361 properties, "TransferredBytes"));
363 priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
366 priv->content_md5 = g_value_dup_string (g_hash_table_lookup (properties,
369 priv->description = g_value_dup_string (g_hash_table_lookup (properties,
372 priv->unix_socket_path = g_value_dup_string (g_hash_table_lookup (properties,
375 priv->direction = g_value_get_uint (g_hash_table_lookup (properties,
378 g_hash_table_destroy (properties);
384 tp_file_get_property (GObject *object,
389 EmpathyTpFilePriv *priv;
390 EmpathyTpFile *tp_file;
392 priv = GET_PRIV (object);
393 tp_file = EMPATHY_TP_FILE (object);
398 g_value_set_object (value, priv->account);
401 g_value_set_object (value, priv->channel);
404 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
410 tp_file_channel_set_dbus_property (gpointer proxy,
411 const gchar *property,
414 DEBUG ("Setting %s property", property);
415 tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
417 EMP_IFACE_CHANNEL_TYPE_FILE,
426 tp_file_set_property (GObject *object,
431 EmpathyTpFilePriv *priv;
433 priv = GET_PRIV (object);
438 priv->account = g_object_ref (g_value_get_object (value));
441 priv->channel = g_object_ref (g_value_get_object (value));
444 priv->state = g_value_get_uint (value);
447 priv->direction = g_value_get_uint (value);
450 g_free (priv->filename);
451 priv->filename = g_value_dup_string (value);
452 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
455 priv->size = g_value_get_uint64 (value);
456 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
458 case PROP_CONTENT_TYPE:
459 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
460 g_free (priv->content_type);
461 priv->content_type = g_value_dup_string (value);
463 case PROP_CONTENT_MD5:
464 tp_file_channel_set_dbus_property (priv->channel, "ContentMD5", value);
465 g_free (priv->content_md5);
466 priv->content_md5 = g_value_dup_string (value);
470 g_object_unref (priv->in_stream);
471 priv->in_stream = g_object_ref (g_value_get_object (value));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
480 * empathy_tp_file_new:
481 * @account: the #McAccount for the channel
482 * @channel: a Telepathy channel
484 * Creates a new #EmpathyTpFile wrapping @channel.
486 * Returns: a new #EmpathyTpFile
489 empathy_tp_file_new (McAccount *account,
492 return g_object_new (EMPATHY_TYPE_TP_FILE,
499 * empathy_tp_file_get_id:
500 * @tp_file: an #EmpathyTpFile
502 * Returns the ID of @tp_file.
507 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
509 EmpathyTpFilePriv *priv;
511 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
513 priv = GET_PRIV (tp_file);
519 * empathy_tp_file_get_channel
520 * @tp_file: an #EmpathyTpFile
522 * Returns the Telepathy file transfer channel
524 * Returns: the #TpChannel
527 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
529 EmpathyTpFilePriv *priv;
531 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
533 priv = GET_PRIV (tp_file);
535 return priv->channel;
539 tp_file_destroy_cb (TpChannel *file_channel,
540 EmpathyTpFile *tp_file)
542 EmpathyTpFilePriv *priv;
544 priv = GET_PRIV (tp_file);
546 DEBUG ("Channel Closed or CM crashed");
548 g_object_unref (priv->channel);
549 priv->channel = NULL;
553 tp_file_closed_cb (TpChannel *file_channel,
554 EmpathyTpFile *tp_file,
555 GObject *weak_object)
557 EmpathyTpFilePriv *priv;
559 priv = GET_PRIV (tp_file);
561 /* The channel is closed, do just like if the proxy was destroyed */
562 g_signal_handlers_disconnect_by_func (priv->channel,
565 tp_file_destroy_cb (file_channel, tp_file);
573 g_get_current_time (&tv);
574 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
578 _get_local_socket (EmpathyTpFile *tp_file)
582 struct sockaddr_un addr;
583 EmpathyTpFilePriv *priv;
586 priv = GET_PRIV (tp_file);
588 /* TODO: This could probably be a little nicer. */
589 tp_cli_dbus_properties_run_get (priv->channel,
591 EMP_IFACE_CHANNEL_TYPE_FILE,
597 if (priv->unix_socket_path)
598 g_free (priv->unix_socket_path);
600 priv->unix_socket_path = g_value_dup_string (socket_path);
601 g_value_unset (socket_path);
603 if (G_STR_EMPTY (priv->unix_socket_path))
606 fd = socket (PF_UNIX, SOCK_STREAM, 0);
610 memset (&addr, 0, sizeof (addr));
611 addr.sun_family = AF_UNIX;
612 path_len = strlen (priv->unix_socket_path);
613 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
615 if (connect (fd, (struct sockaddr*) &addr,
626 * empathy_tp_file_accept:
627 * @tp_file: an #EmpathyTpFile
629 * Accepts a file transfer that's in the "local pending" state (i.e.
630 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
633 empathy_tp_file_accept (EmpathyTpFile *tp_file)
635 EmpathyTpFilePriv *priv;
637 GValue nothing = { 0 };
638 GError *error = NULL;
640 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
642 priv = GET_PRIV (tp_file);
644 g_return_if_fail (priv->out_stream != NULL);
646 DEBUG ("Accepting file: filename=%s", priv->filename);
648 g_value_init (¬hing, G_TYPE_STRING);
649 g_value_set_string (¬hing, "");
651 if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
652 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
653 ¬hing, &address, &error, NULL))
655 DEBUG ("Accept error: %s",
656 error ? error->message : "No message given");
657 g_clear_error (&error);
660 if (priv->unix_socket_path)
661 g_free (priv->unix_socket_path);
663 priv->unix_socket_path = g_value_dup_string (address);
664 g_value_unset (address);
666 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
670 receive_tp_file (EmpathyTpFile *tp_file)
672 EmpathyTpFilePriv *priv;
673 GInputStream *socket_stream;
676 priv = GET_PRIV (tp_file);
678 socket_fd = _get_local_socket (tp_file);
683 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
685 priv->cancellable = g_cancellable_new ();
687 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
689 g_object_unref (socket_stream);
694 send_tp_file (EmpathyTpFile *tp_file)
697 GOutputStream *socket_stream;
698 EmpathyTpFilePriv *priv;
700 priv = GET_PRIV (tp_file);
702 DEBUG ("Sending file content: filename=%s",
705 g_return_if_fail (priv->in_stream);
707 socket_fd = _get_local_socket (tp_file);
710 DEBUG ("failed to get local socket fd");
713 DEBUG ("got local socket fd");
714 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
716 priv->cancellable = g_cancellable_new ();
718 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
720 g_object_unref (socket_stream);
724 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
725 EmpFileTransferState state,
726 EmpFileTransferStateChangeReason reason,
727 EmpathyTpFile *tp_file)
729 EmpathyTpFilePriv *priv;
731 priv = GET_PRIV (tp_file);
733 DEBUG ("File transfer state changed: filename=%s, "
734 "old state=%u, state=%u, reason=%u",
735 priv->filename, priv->state, state, reason);
737 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
738 priv->start_time = get_time_msec ();
740 DEBUG ("state = %u, direction = %u, in_stream = %s, out_stream = %s",
741 state, priv->direction,
742 priv->in_stream ? "present" : "not present",
743 priv->out_stream ? "present" : "not present");
745 if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
746 priv->direction == EMP_FILE_TRANSFER_DIRECTION_OUTGOING &&
748 send_tp_file (tp_file);
749 else if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
750 priv->direction == EMP_FILE_TRANSFER_DIRECTION_INCOMING &&
752 receive_tp_file (tp_file);
755 priv->state_change_reason = reason;
757 g_object_notify (G_OBJECT (tp_file), "state");
761 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
763 EmpathyTpFile *tp_file,
764 GObject *weak_object)
766 EmpathyTpFilePriv *priv;
768 priv = GET_PRIV (tp_file);
770 if (priv->transferred_bytes == count)
773 priv->transferred_bytes = count;
775 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
779 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
781 EmpathyTpFilePriv *priv;
783 priv = GET_PRIV (tp_file);
785 return priv->contact;
789 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
791 EmpathyTpFilePriv *priv;
793 priv = GET_PRIV (tp_file);
795 return priv->in_stream;
799 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
801 EmpathyTpFilePriv *priv;
803 priv = GET_PRIV (tp_file);
805 return priv->out_stream;
809 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
811 EmpathyTpFilePriv *priv;
813 priv = GET_PRIV (tp_file);
815 return priv->filename;
818 EmpFileTransferDirection
819 empathy_tp_file_get_direction (EmpathyTpFile *tp_file)
821 EmpathyTpFilePriv *priv;
823 priv = GET_PRIV (tp_file);
825 return priv->direction;
829 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
831 EmpathyTpFilePriv *priv;
833 priv = GET_PRIV (tp_file);
838 EmpFileTransferStateChangeReason
839 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
841 EmpathyTpFilePriv *priv;
843 priv = GET_PRIV (tp_file);
845 g_return_val_if_fail (priv->state_change_reason >= 0,
846 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
848 return priv->state_change_reason;
852 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
854 EmpathyTpFilePriv *priv;
856 priv = GET_PRIV (tp_file);
862 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
864 EmpathyTpFilePriv *priv;
866 priv = GET_PRIV (tp_file);
868 return priv->transferred_bytes;
872 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
874 EmpathyTpFilePriv *priv;
875 gint64 curr_time, elapsed_time;
876 gdouble time_per_byte;
877 gdouble remaining_time;
879 priv = GET_PRIV (tp_file);
881 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
884 if (priv->transferred_bytes == priv->size)
887 curr_time = get_time_msec ();
888 elapsed_time = curr_time - priv->start_time;
889 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
890 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
892 return (gint) (remaining_time + 0.5);
896 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
898 EmpathyTpFilePriv *priv;
900 priv = GET_PRIV (tp_file);
902 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
904 g_cancellable_cancel (priv->cancellable);
908 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
909 GInputStream *in_stream)
911 EmpathyTpFilePriv *priv;
913 priv = GET_PRIV (tp_file);
915 if (priv->in_stream == in_stream)
918 if (priv->direction == EMP_FILE_TRANSFER_DIRECTION_INCOMING)
919 g_warning ("Setting an input stream for incoming file "
920 "transfers is useless");
923 g_object_unref (priv->in_stream);
926 g_object_ref (in_stream);
928 priv->in_stream = in_stream;
930 g_object_notify (G_OBJECT (tp_file), "in-stream");
934 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
935 GOutputStream *out_stream)
937 EmpathyTpFilePriv *priv;
939 priv = GET_PRIV (tp_file);
941 if (priv->out_stream == out_stream)
944 if (priv->direction == EMP_FILE_TRANSFER_DIRECTION_OUTGOING)
945 g_warning ("Setting an output stream for outgoing file "
946 "transfers is useless");
948 if (priv->out_stream)
949 g_object_unref (priv->out_stream);
952 g_object_ref (out_stream);
954 priv->out_stream = out_stream;
958 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
959 const gchar *filename)
961 EmpathyTpFilePriv *priv;
963 priv = GET_PRIV (tp_file);
965 g_return_if_fail (filename != NULL);
967 if (priv->filename && strcmp (filename, priv->filename) == 0)
970 g_free (priv->filename);
971 priv->filename = g_strdup (filename);
973 g_object_notify (G_OBJECT (tp_file), "filename");
976 /* Functions to copy the content of a GInputStream to a GOutputStream */
979 #define BUFFER_SIZE 4096
984 GCancellable *cancellable;
985 char *buff[N_BUFFERS]; /* the temporary buffers */
986 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
987 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
988 gint curr_read; /* index of the buffer used for reading */
989 gint curr_write; /* index of the buffer used for writing */
990 gboolean is_reading; /* we are reading */
991 gboolean is_writing; /* we are writing */
992 guint n_closed; /* number of streams that have been closed */
995 static void schedule_next (CopyData *copy);
998 free_copy_data_if_closed (CopyData *copy)
1002 /* Free the data only if both the input and output streams have
1005 if (copy->n_closed < 2)
1008 if (copy->in != NULL)
1009 g_object_unref (copy->in);
1011 if (copy->out != NULL)
1012 g_object_unref (copy->out);
1014 for (i = 0; i < N_BUFFERS; i++)
1015 g_free (copy->buff[i]);
1017 g_object_unref (copy->cancellable);
1022 io_error (CopyData *copy,
1025 g_cancellable_cancel (copy->cancellable);
1028 g_warning ("I/O error");
1029 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1030 ; /* Ignore cancellations */
1032 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1034 if (copy->in != NULL)
1035 g_input_stream_close (copy->in, NULL, NULL);
1037 if (copy->out != NULL)
1038 g_output_stream_close (copy->out, NULL, NULL);
1040 free_copy_data_if_closed (copy);
1044 close_done (GObject *source_object,
1048 CopyData *copy = user_data;
1050 g_object_unref (source_object);
1051 free_copy_data_if_closed (copy);
1055 write_done_cb (GObject *source_object,
1059 CopyData *copy = user_data;
1061 GError *error = NULL;
1063 count_write = g_output_stream_write_finish (copy->out, res, &error);
1065 if (count_write <= 0)
1067 io_error (copy, error);
1068 g_error_free (error);
1072 copy->is_full[copy->curr_write] = FALSE;
1073 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1074 copy->is_writing = FALSE;
1076 schedule_next (copy);
1080 read_done_cb (GObject *source_object,
1084 CopyData *copy = user_data;
1086 GError *error = NULL;
1088 count_read = g_input_stream_read_finish (copy->in, res, &error);
1090 if (count_read == 0)
1092 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1096 else if (count_read < 0)
1098 io_error (copy, error);
1099 g_error_free (error);
1103 copy->count[copy->curr_read] = count_read;
1104 copy->is_full[copy->curr_read] = TRUE;
1105 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1106 copy->is_reading = FALSE;
1108 schedule_next (copy);
1112 schedule_next (CopyData *copy)
1114 if (copy->in != NULL &&
1115 !copy->is_reading &&
1116 !copy->is_full[copy->curr_read])
1118 /* We are not reading and the current buffer is empty, so
1119 * start an async read. */
1120 copy->is_reading = TRUE;
1121 g_input_stream_read_async (copy->in,
1122 copy->buff[copy->curr_read],
1123 BUFFER_SIZE, 0, copy->cancellable,
1124 read_done_cb, copy);
1127 if (!copy->is_writing &&
1128 copy->is_full[copy->curr_write])
1130 if (copy->count[copy->curr_write] == 0)
1132 /* The last read on the buffer read 0 bytes, this
1133 * means that we got an EOF, so we can close
1134 * the output channel. */
1135 g_output_stream_close_async (copy->out, 0,
1142 /* We are not writing and the current buffer contains
1143 * data, so start an async write. */
1144 copy->is_writing = TRUE;
1145 g_output_stream_write_async (copy->out,
1146 copy->buff[copy->curr_write],
1147 copy->count[copy->curr_write],
1148 0, copy->cancellable,
1149 write_done_cb, copy);
1155 copy_stream (GInputStream *in,
1157 GCancellable *cancellable)
1162 g_return_if_fail (in != NULL);
1163 g_return_if_fail (out != NULL);
1165 copy = g_new0 (CopyData, 1);
1166 copy->in = g_object_ref (in);
1167 copy->out = g_object_ref (out);
1169 if (cancellable != NULL)
1170 copy->cancellable = g_object_ref (cancellable);
1172 copy->cancellable = g_cancellable_new ();
1174 for (i = 0; i < N_BUFFERS; i++)
1175 copy->buff[i] = g_malloc (BUFFER_SIZE);
1177 schedule_next (copy);