1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
4 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Marco Barisione <marco@barisione.org>
21 * Jonny Lamb <jonny.lamb@collabora.co.uk>
28 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <glib/gi18n.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
38 #include <libtelepathy/tp-conn.h>
39 #include <libtelepathy/tp-helpers.h>
40 #include <libtelepathy/tp-props-iface.h>
42 #include <telepathy-glib/proxy-subclass.h>
44 #include "empathy-tp-file.h"
45 #include "empathy-contact-factory.h"
46 #include "empathy-marshal.h"
47 #include "empathy-time.h"
48 #include "empathy-utils.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_FT
51 #include "empathy-debug.h"
54 * SECTION:empathy-tp-file
55 * @short_description: File channel
56 * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
57 * @include: libempthy/empathy-tp-file.h
59 * The #EmpathyTpFile object represents a Telepathy file channel.
63 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
65 * Value used for the "size" or "estimated-size" properties when the size of
66 * the transferred file is unknown.
69 static void empathy_tp_file_class_init (EmpathyTpFileClass *klass);
70 static void empathy_tp_file_init (EmpathyTpFile *tp_file);
71 static void tp_file_finalize (GObject *object);
72 static GObject *tp_file_constructor (GType type, guint n_props,
73 GObjectConstructParam *props);
74 static void tp_file_get_property (GObject *object, guint param_id,
75 GValue *value, GParamSpec *pspec);
76 static void tp_file_set_property (GObject *object, guint param_id,
77 const GValue *value, GParamSpec *pspec);
78 static void tp_file_destroy_cb (TpChannel *file_chan, EmpathyTpFile *tp_file);
79 static void tp_file_closed_cb (TpChannel *file_chan, EmpathyTpFile *tp_file,
80 GObject *weak_object);
81 static void tp_file_state_changed_cb (DBusGProxy *file_iface, guint state,
82 guint reason, EmpathyTpFile *tp_file);
83 static void tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
84 guint64 count, EmpathyTpFile *tp_file, GObject *weak_object);
85 static void copy_stream (GInputStream *in, GOutputStream *out,
86 GCancellable *cancellable);
88 /* EmpathyTpFile object */
90 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
91 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv))
93 typedef struct _EmpathyTpFilePriv EmpathyTpFilePriv;
95 struct _EmpathyTpFilePriv {
96 EmpathyContactFactory *factory;
102 EmpathyTpFile *cached_empathy_file;
103 EmpathyContact *contact;
104 GInputStream *in_stream;
105 GOutputStream *out_stream;
108 EmpFileTransferState state;
109 EmpFileTransferStateChangeReason state_change_reason;
111 guint64 transferred_bytes;
113 gchar *unix_socket_path;
115 EmpFileHashType content_hash_type;
118 GCancellable *cancellable;
130 PROP_TRANSFERRED_BYTES,
131 PROP_CONTENT_HASH_TYPE,
136 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
139 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
143 object_class->finalize = tp_file_finalize;
144 object_class->constructor = tp_file_constructor;
145 object_class->get_property = tp_file_get_property;
146 object_class->set_property = tp_file_set_property;
148 /* Construct-only properties */
149 g_object_class_install_property (object_class,
151 g_param_spec_object ("account",
153 "The account associated with the channel",
156 G_PARAM_CONSTRUCT_ONLY));
158 g_object_class_install_property (object_class,
160 g_param_spec_object ("channel",
162 "The file transfer channel",
165 G_PARAM_CONSTRUCT_ONLY));
167 g_object_class_install_property (object_class,
169 g_param_spec_uint ("state",
170 "state of the transfer",
171 "The file transfer state",
178 g_object_class_install_property (object_class,
180 g_param_spec_boolean ("incoming",
182 "Whether the transfer is incoming",
187 g_object_class_install_property (object_class,
189 g_param_spec_string ("filename",
190 "name of the transfer",
191 "The file transfer filename",
195 g_object_class_install_property (object_class,
197 g_param_spec_uint64 ("size",
199 "The file transfer size",
205 g_object_class_install_property (object_class,
207 g_param_spec_string ("content-type",
208 "file transfer content-type",
209 "The file transfer content-type",
213 g_object_class_install_property (object_class,
214 PROP_CONTENT_HASH_TYPE,
215 g_param_spec_uint ("content-hash-type",
216 "file transfer hash type",
217 "The type of the file transfer hash",
223 g_object_class_install_property (object_class,
225 g_param_spec_string ("content-hash",
226 "file transfer hash",
227 "The hash of the transfer's contents",
231 g_object_class_install_property (object_class,
232 PROP_TRANSFERRED_BYTES,
233 g_param_spec_uint64 ("transferred-bytes",
235 "The number of bytes transferred",
241 g_object_class_install_property (object_class,
243 g_param_spec_object ("in-stream",
244 "transfer input stream",
245 "The input stream for file transfer",
249 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
253 empathy_tp_file_init (EmpathyTpFile *tp_file)
258 tp_file_finalize (GObject *object)
260 EmpathyTpFilePriv *priv;
261 EmpathyTpFile *tp_file;
263 tp_file = EMPATHY_TP_FILE (object);
264 priv = GET_PRIV (tp_file);
268 DEBUG ("Closing channel..");
269 g_signal_handlers_disconnect_by_func (priv->channel,
270 tp_file_destroy_cb, object);
271 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
272 if (G_IS_OBJECT (priv->channel))
273 g_object_unref (priv->channel);
278 g_object_unref (priv->factory);
282 g_object_unref (priv->account);
286 g_object_unref (priv->mc);
290 g_free (priv->filename);
291 g_free (priv->unix_socket_path);
292 g_free (priv->description);
293 g_free (priv->content_hash);
294 g_free (priv->content_type);
297 g_object_unref (priv->in_stream);
299 if (priv->out_stream)
300 g_object_unref (priv->out_stream);
303 g_object_unref (priv->contact);
305 if (priv->cancellable)
306 g_object_unref (priv->cancellable);
308 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
312 tp_file_constructor (GType type,
314 GObjectConstructParam *props)
317 EmpathyTpFilePriv *priv;
318 GError *error = NULL;
319 GHashTable *properties;
322 tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
325 priv = GET_PRIV (tp_file);
327 priv->factory = empathy_contact_factory_new ();
328 priv->mc = empathy_mission_control_new ();
330 tp_cli_channel_connect_to_closed (priv->channel,
331 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
335 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
336 TP_PROXY (priv->channel),
337 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
338 tp_file_state_changed_cb,
342 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
343 TP_PROXY (priv->channel),
344 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
345 tp_file_transferred_bytes_changed_cb,
350 handle = tp_channel_get_handle (priv->channel, NULL);
351 priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
355 if (!tp_cli_dbus_properties_run_get_all (priv->channel,
356 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
358 DEBUG ("Failed to get properties: %s",
359 error ? error->message : "No error given");
360 g_clear_error (&error);
364 priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
366 priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
368 /* Invalid reason, so empathy_file_get_state_change_reason() can give
369 * a warning if called for a not closed file transfer. */
370 priv->state_change_reason = -1;
372 priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
373 properties, "TransferredBytes"));
375 priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
378 priv->content_hash = g_value_dup_string (g_hash_table_lookup (properties,
381 priv->description = g_value_dup_string (g_hash_table_lookup (properties,
384 if (priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
385 priv->incoming = TRUE;
387 g_hash_table_destroy (properties);
393 tp_file_get_property (GObject *object,
398 EmpathyTpFilePriv *priv;
399 EmpathyTpFile *tp_file;
401 priv = GET_PRIV (object);
402 tp_file = EMPATHY_TP_FILE (object);
407 g_value_set_object (value, priv->account);
410 g_value_set_object (value, priv->channel);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
419 tp_file_channel_set_dbus_property (gpointer proxy,
420 const gchar *property,
423 DEBUG ("Setting %s property", property);
424 tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
426 EMP_IFACE_CHANNEL_TYPE_FILE,
435 tp_file_set_property (GObject *object,
440 EmpathyTpFilePriv *priv;
442 priv = GET_PRIV (object);
447 priv->account = g_object_ref (g_value_get_object (value));
450 priv->channel = g_object_ref (g_value_get_object (value));
453 priv->state = g_value_get_uint (value);
456 priv->incoming = g_value_get_boolean (value);
459 g_free (priv->filename);
460 priv->filename = g_value_dup_string (value);
461 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
464 priv->size = g_value_get_uint64 (value);
465 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
467 case PROP_CONTENT_TYPE:
468 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
469 g_free (priv->content_type);
470 priv->content_type = g_value_dup_string (value);
472 case PROP_CONTENT_HASH:
473 tp_file_channel_set_dbus_property (priv->channel, "ContentHash", value);
474 g_free (priv->content_hash);
475 priv->content_hash = g_value_dup_string (value);
479 g_object_unref (priv->in_stream);
480 priv->in_stream = g_object_ref (g_value_get_object (value));
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
489 * empathy_tp_file_new:
490 * @account: the #McAccount for the channel
491 * @channel: a Telepathy channel
493 * Creates a new #EmpathyTpFile wrapping @channel.
495 * Returns: a new #EmpathyTpFile
498 empathy_tp_file_new (McAccount *account,
501 return g_object_new (EMPATHY_TYPE_TP_FILE,
508 * empathy_tp_file_get_id:
509 * @tp_file: an #EmpathyTpFile
511 * Returns the ID of @tp_file.
516 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
518 EmpathyTpFilePriv *priv;
520 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
522 priv = GET_PRIV (tp_file);
528 * empathy_tp_file_get_channel
529 * @tp_file: an #EmpathyTpFile
531 * Returns the Telepathy file transfer channel
533 * Returns: the #TpChannel
536 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
538 EmpathyTpFilePriv *priv;
540 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
542 priv = GET_PRIV (tp_file);
544 return priv->channel;
548 tp_file_destroy_cb (TpChannel *file_channel,
549 EmpathyTpFile *tp_file)
551 EmpathyTpFilePriv *priv;
553 priv = GET_PRIV (tp_file);
555 DEBUG ("Channel Closed or CM crashed");
557 g_object_unref (priv->channel);
558 priv->channel = NULL;
562 tp_file_closed_cb (TpChannel *file_channel,
563 EmpathyTpFile *tp_file,
564 GObject *weak_object)
566 EmpathyTpFilePriv *priv;
568 priv = GET_PRIV (tp_file);
570 /* The channel is closed, do just like if the proxy was destroyed */
571 g_signal_handlers_disconnect_by_func (priv->channel,
574 tp_file_destroy_cb (file_channel, tp_file);
582 g_get_current_time (&tv);
583 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
587 _get_local_socket (EmpathyTpFile *tp_file)
591 struct sockaddr_un addr;
592 EmpathyTpFilePriv *priv;
594 priv = GET_PRIV (tp_file);
596 if (G_STR_EMPTY (priv->unix_socket_path))
599 fd = socket (PF_UNIX, SOCK_STREAM, 0);
603 memset (&addr, 0, sizeof (addr));
604 addr.sun_family = AF_UNIX;
605 path_len = strlen (priv->unix_socket_path);
606 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
608 if (connect (fd, (struct sockaddr*) &addr,
619 * empathy_tp_file_accept:
620 * @tp_file: an #EmpathyTpFile
622 * Accepts a file transfer that's in the "local pending" state (i.e.
623 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
626 empathy_tp_file_accept (EmpathyTpFile *tp_file,
629 EmpathyTpFilePriv *priv;
631 GValue nothing = { 0 };
632 GError *error = NULL;
634 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
636 priv = GET_PRIV (tp_file);
638 g_return_if_fail (priv->out_stream != NULL);
640 DEBUG ("Accepting file: filename=%s", priv->filename);
642 g_value_init (¬hing, G_TYPE_STRING);
643 g_value_set_string (¬hing, "");
645 if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
646 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
647 ¬hing, offset, &address, &error, NULL))
649 DEBUG ("Accept error: %s",
650 error ? error->message : "No message given");
651 g_clear_error (&error);
655 if (priv->unix_socket_path)
656 g_free (priv->unix_socket_path);
658 priv->unix_socket_path = g_value_dup_string (address);
659 g_value_unset (address);
661 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
665 empathy_tp_file_offer (EmpathyTpFile *tp_file)
667 EmpathyTpFilePriv *priv;
669 GError *error = NULL;
670 GValue nothing = { 0 };
672 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
674 priv = GET_PRIV (tp_file);
676 g_value_init (¬hing, G_TYPE_STRING);
677 g_value_set_string (¬hing, "");
679 if (!emp_cli_channel_type_file_run_offer_file (TP_PROXY (priv->channel),
680 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
681 ¬hing, &address, &error, NULL))
683 DEBUG ("OfferFile error: %s",
684 error ? error->message : "No message given");
685 g_clear_error (&error);
689 if (priv->unix_socket_path)
690 g_free (priv->unix_socket_path);
692 priv->unix_socket_path = g_value_dup_string (address);
693 g_value_unset (address);
695 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
699 receive_tp_file (EmpathyTpFile *tp_file)
701 EmpathyTpFilePriv *priv;
702 GInputStream *socket_stream;
705 priv = GET_PRIV (tp_file);
707 socket_fd = _get_local_socket (tp_file);
712 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
714 priv->cancellable = g_cancellable_new ();
716 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
718 g_object_unref (socket_stream);
722 send_tp_file (EmpathyTpFile *tp_file)
725 GOutputStream *socket_stream;
726 EmpathyTpFilePriv *priv;
728 priv = GET_PRIV (tp_file);
730 DEBUG ("Sending file content: filename=%s",
733 g_return_if_fail (priv->in_stream);
735 socket_fd = _get_local_socket (tp_file);
738 DEBUG ("failed to get local socket fd");
741 DEBUG ("got local socket fd");
742 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
744 priv->cancellable = g_cancellable_new ();
746 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
748 g_object_unref (socket_stream);
752 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
753 EmpFileTransferState state,
754 EmpFileTransferStateChangeReason reason,
755 EmpathyTpFile *tp_file)
757 EmpathyTpFilePriv *priv;
759 priv = GET_PRIV (tp_file);
761 DEBUG ("File transfer state changed: filename=%s, "
762 "old state=%u, state=%u, reason=%u",
763 priv->filename, priv->state, state, reason);
765 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
766 priv->start_time = get_time_msec ();
768 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
769 state, priv->incoming ? "yes" : "no",
770 priv->in_stream ? "present" : "not present",
771 priv->out_stream ? "present" : "not present");
773 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
775 send_tp_file (tp_file);
776 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
778 receive_tp_file (tp_file);
781 priv->state_change_reason = reason;
783 g_object_notify (G_OBJECT (tp_file), "state");
787 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
789 EmpathyTpFile *tp_file,
790 GObject *weak_object)
792 EmpathyTpFilePriv *priv;
794 priv = GET_PRIV (tp_file);
796 if (priv->transferred_bytes == count)
799 priv->transferred_bytes = count;
801 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
805 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
807 EmpathyTpFilePriv *priv;
809 priv = GET_PRIV (tp_file);
811 return priv->contact;
815 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
817 EmpathyTpFilePriv *priv;
819 priv = GET_PRIV (tp_file);
821 return priv->in_stream;
825 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
827 EmpathyTpFilePriv *priv;
829 priv = GET_PRIV (tp_file);
831 return priv->out_stream;
835 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
837 EmpathyTpFilePriv *priv;
839 priv = GET_PRIV (tp_file);
841 return priv->filename;
845 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
847 EmpathyTpFilePriv *priv;
849 priv = GET_PRIV (tp_file);
851 return priv->incoming;
855 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
857 EmpathyTpFilePriv *priv;
859 priv = GET_PRIV (tp_file);
864 EmpFileTransferStateChangeReason
865 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
867 EmpathyTpFilePriv *priv;
869 priv = GET_PRIV (tp_file);
871 g_return_val_if_fail (priv->state_change_reason >= 0,
872 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
874 return priv->state_change_reason;
878 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
880 EmpathyTpFilePriv *priv;
882 priv = GET_PRIV (tp_file);
888 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
890 EmpathyTpFilePriv *priv;
892 priv = GET_PRIV (tp_file);
894 return priv->transferred_bytes;
898 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
900 EmpathyTpFilePriv *priv;
901 gint64 curr_time, elapsed_time;
902 gdouble time_per_byte;
903 gdouble remaining_time;
905 priv = GET_PRIV (tp_file);
907 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
910 if (priv->transferred_bytes == priv->size)
913 curr_time = get_time_msec ();
914 elapsed_time = curr_time - priv->start_time;
915 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
916 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
918 return (gint) (remaining_time + 0.5);
922 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
924 EmpathyTpFilePriv *priv;
926 priv = GET_PRIV (tp_file);
928 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
930 g_cancellable_cancel (priv->cancellable);
934 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
935 GInputStream *in_stream)
937 EmpathyTpFilePriv *priv;
939 priv = GET_PRIV (tp_file);
941 if (priv->in_stream == in_stream)
945 g_warning ("Setting an input stream for incoming file "
946 "transfers is useless");
949 g_object_unref (priv->in_stream);
952 g_object_ref (in_stream);
954 priv->in_stream = in_stream;
956 g_object_notify (G_OBJECT (tp_file), "in-stream");
960 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
961 GOutputStream *out_stream)
963 EmpathyTpFilePriv *priv;
965 priv = GET_PRIV (tp_file);
967 if (priv->out_stream == out_stream)
971 g_warning ("Setting an output stream for outgoing file "
972 "transfers is useless");
974 if (priv->out_stream)
975 g_object_unref (priv->out_stream);
978 g_object_ref (out_stream);
980 priv->out_stream = out_stream;
984 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
985 const gchar *filename)
987 EmpathyTpFilePriv *priv;
989 priv = GET_PRIV (tp_file);
991 g_return_if_fail (filename != NULL);
993 if (priv->filename && strcmp (filename, priv->filename) == 0)
996 g_free (priv->filename);
997 priv->filename = g_strdup (filename);
999 g_object_notify (G_OBJECT (tp_file), "filename");
1002 /* Functions to copy the content of a GInputStream to a GOutputStream */
1005 #define BUFFER_SIZE 4096
1010 GCancellable *cancellable;
1011 char *buff[N_BUFFERS]; /* the temporary buffers */
1012 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
1013 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
1014 gint curr_read; /* index of the buffer used for reading */
1015 gint curr_write; /* index of the buffer used for writing */
1016 gboolean is_reading; /* we are reading */
1017 gboolean is_writing; /* we are writing */
1018 guint n_closed; /* number of streams that have been closed */
1021 static void schedule_next (CopyData *copy);
1024 free_copy_data_if_closed (CopyData *copy)
1028 /* Free the data only if both the input and output streams have
1031 if (copy->n_closed < 2)
1034 if (copy->in != NULL)
1035 g_object_unref (copy->in);
1037 if (copy->out != NULL)
1038 g_object_unref (copy->out);
1040 for (i = 0; i < N_BUFFERS; i++)
1041 g_free (copy->buff[i]);
1043 g_object_unref (copy->cancellable);
1048 io_error (CopyData *copy,
1051 g_cancellable_cancel (copy->cancellable);
1054 g_warning ("I/O error");
1055 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1056 ; /* Ignore cancellations */
1058 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1060 if (copy->in != NULL)
1061 g_input_stream_close (copy->in, NULL, NULL);
1063 if (copy->out != NULL)
1064 g_output_stream_close (copy->out, NULL, NULL);
1066 free_copy_data_if_closed (copy);
1070 close_done (GObject *source_object,
1074 CopyData *copy = user_data;
1076 g_object_unref (source_object);
1077 free_copy_data_if_closed (copy);
1081 write_done_cb (GObject *source_object,
1085 CopyData *copy = user_data;
1087 GError *error = NULL;
1089 count_write = g_output_stream_write_finish (copy->out, res, &error);
1091 if (count_write <= 0)
1093 io_error (copy, error);
1094 g_error_free (error);
1098 copy->is_full[copy->curr_write] = FALSE;
1099 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1100 copy->is_writing = FALSE;
1102 schedule_next (copy);
1106 read_done_cb (GObject *source_object,
1110 CopyData *copy = user_data;
1112 GError *error = NULL;
1114 count_read = g_input_stream_read_finish (copy->in, res, &error);
1116 if (count_read == 0)
1118 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1122 else if (count_read < 0)
1124 io_error (copy, error);
1125 g_error_free (error);
1129 copy->count[copy->curr_read] = count_read;
1130 copy->is_full[copy->curr_read] = TRUE;
1131 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1132 copy->is_reading = FALSE;
1134 schedule_next (copy);
1138 schedule_next (CopyData *copy)
1140 if (copy->in != NULL &&
1141 !copy->is_reading &&
1142 !copy->is_full[copy->curr_read])
1144 /* We are not reading and the current buffer is empty, so
1145 * start an async read. */
1146 copy->is_reading = TRUE;
1147 g_input_stream_read_async (copy->in,
1148 copy->buff[copy->curr_read],
1149 BUFFER_SIZE, 0, copy->cancellable,
1150 read_done_cb, copy);
1153 if (!copy->is_writing &&
1154 copy->is_full[copy->curr_write])
1156 if (copy->count[copy->curr_write] == 0)
1158 /* The last read on the buffer read 0 bytes, this
1159 * means that we got an EOF, so we can close
1160 * the output channel. */
1161 g_output_stream_close_async (copy->out, 0,
1168 /* We are not writing and the current buffer contains
1169 * data, so start an async write. */
1170 copy->is_writing = TRUE;
1171 g_output_stream_write_async (copy->out,
1172 copy->buff[copy->curr_write],
1173 copy->count[copy->curr_write],
1174 0, copy->cancellable,
1175 write_done_cb, copy);
1181 copy_stream (GInputStream *in,
1183 GCancellable *cancellable)
1188 g_return_if_fail (in != NULL);
1189 g_return_if_fail (out != NULL);
1191 copy = g_new0 (CopyData, 1);
1192 copy->in = g_object_ref (in);
1193 copy->out = g_object_ref (out);
1195 if (cancellable != NULL)
1196 copy->cancellable = g_object_ref (cancellable);
1198 copy->cancellable = g_cancellable_new ();
1200 for (i = 0; i < N_BUFFERS; i++)
1201 copy->buff[i] = g_malloc (BUFFER_SIZE);
1203 schedule_next (copy);