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 if (priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
383 priv->incoming = TRUE;
385 g_hash_table_destroy (properties);
391 tp_file_get_property (GObject *object,
396 EmpathyTpFilePriv *priv;
397 EmpathyTpFile *tp_file;
399 priv = GET_PRIV (object);
400 tp_file = EMPATHY_TP_FILE (object);
405 g_value_set_object (value, priv->account);
408 g_value_set_object (value, priv->channel);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
417 tp_file_channel_set_dbus_property (gpointer proxy,
418 const gchar *property,
421 DEBUG ("Setting %s property", property);
422 tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
424 EMP_IFACE_CHANNEL_TYPE_FILE,
433 tp_file_set_property (GObject *object,
438 EmpathyTpFilePriv *priv;
440 priv = GET_PRIV (object);
445 priv->account = g_object_ref (g_value_get_object (value));
448 priv->channel = g_object_ref (g_value_get_object (value));
451 priv->state = g_value_get_uint (value);
454 priv->incoming = g_value_get_boolean (value);
457 g_free (priv->filename);
458 priv->filename = g_value_dup_string (value);
459 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
462 priv->size = g_value_get_uint64 (value);
463 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
465 case PROP_CONTENT_TYPE:
466 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
467 g_free (priv->content_type);
468 priv->content_type = g_value_dup_string (value);
470 case PROP_CONTENT_HASH:
471 tp_file_channel_set_dbus_property (priv->channel, "ContentHash", value);
472 g_free (priv->content_hash);
473 priv->content_hash = g_value_dup_string (value);
477 g_object_unref (priv->in_stream);
478 priv->in_stream = g_object_ref (g_value_get_object (value));
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
487 * empathy_tp_file_new:
488 * @account: the #McAccount for the channel
489 * @channel: a Telepathy channel
491 * Creates a new #EmpathyTpFile wrapping @channel.
493 * Returns: a new #EmpathyTpFile
496 empathy_tp_file_new (McAccount *account,
499 return g_object_new (EMPATHY_TYPE_TP_FILE,
506 * empathy_tp_file_get_id:
507 * @tp_file: an #EmpathyTpFile
509 * Returns the ID of @tp_file.
514 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
516 EmpathyTpFilePriv *priv;
518 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
520 priv = GET_PRIV (tp_file);
526 * empathy_tp_file_get_channel
527 * @tp_file: an #EmpathyTpFile
529 * Returns the Telepathy file transfer channel
531 * Returns: the #TpChannel
534 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
536 EmpathyTpFilePriv *priv;
538 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
540 priv = GET_PRIV (tp_file);
542 return priv->channel;
546 tp_file_destroy_cb (TpChannel *file_channel,
547 EmpathyTpFile *tp_file)
549 EmpathyTpFilePriv *priv;
551 priv = GET_PRIV (tp_file);
553 DEBUG ("Channel Closed or CM crashed");
555 g_object_unref (priv->channel);
556 priv->channel = NULL;
560 tp_file_closed_cb (TpChannel *file_channel,
561 EmpathyTpFile *tp_file,
562 GObject *weak_object)
564 EmpathyTpFilePriv *priv;
566 priv = GET_PRIV (tp_file);
568 /* The channel is closed, do just like if the proxy was destroyed */
569 g_signal_handlers_disconnect_by_func (priv->channel,
572 tp_file_destroy_cb (file_channel, tp_file);
580 g_get_current_time (&tv);
581 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
585 _get_local_socket (EmpathyTpFile *tp_file)
589 struct sockaddr_un addr;
590 EmpathyTpFilePriv *priv;
592 priv = GET_PRIV (tp_file);
594 if (G_STR_EMPTY (priv->unix_socket_path))
597 fd = socket (PF_UNIX, SOCK_STREAM, 0);
601 memset (&addr, 0, sizeof (addr));
602 addr.sun_family = AF_UNIX;
603 path_len = strlen (priv->unix_socket_path);
604 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
606 if (connect (fd, (struct sockaddr*) &addr,
617 * empathy_tp_file_accept:
618 * @tp_file: an #EmpathyTpFile
620 * Accepts a file transfer that's in the "local pending" state (i.e.
621 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
624 empathy_tp_file_accept (EmpathyTpFile *tp_file,
627 EmpathyTpFilePriv *priv;
629 GValue nothing = { 0 };
630 GError *error = NULL;
632 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
634 priv = GET_PRIV (tp_file);
636 g_return_if_fail (priv->out_stream != NULL);
638 DEBUG ("Accepting file: filename=%s", priv->filename);
640 g_value_init (¬hing, G_TYPE_STRING);
641 g_value_set_string (¬hing, "");
643 if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
644 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
645 ¬hing, offset, &address, &error, NULL))
647 DEBUG ("Accept error: %s",
648 error ? error->message : "No message given");
649 g_clear_error (&error);
653 if (priv->unix_socket_path)
654 g_free (priv->unix_socket_path);
656 priv->unix_socket_path = g_value_dup_string (address);
657 g_value_unset (address);
659 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
663 empathy_tp_file_offer (EmpathyTpFile *tp_file)
665 EmpathyTpFilePriv *priv;
667 GError *error = NULL;
668 GValue nothing = { 0 };
670 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
672 priv = GET_PRIV (tp_file);
674 g_value_init (¬hing, G_TYPE_STRING);
675 g_value_set_string (¬hing, "");
677 if (!emp_cli_channel_type_file_run_offer_file (TP_PROXY (priv->channel),
678 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
679 ¬hing, &address, &error, NULL))
681 DEBUG ("OfferFile error: %s",
682 error ? error->message : "No message given");
683 g_clear_error (&error);
687 if (priv->unix_socket_path)
688 g_free (priv->unix_socket_path);
690 priv->unix_socket_path = g_value_dup_string (address);
691 g_value_unset (address);
693 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
697 receive_tp_file (EmpathyTpFile *tp_file)
699 EmpathyTpFilePriv *priv;
700 GInputStream *socket_stream;
703 priv = GET_PRIV (tp_file);
705 socket_fd = _get_local_socket (tp_file);
710 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
712 priv->cancellable = g_cancellable_new ();
714 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
716 g_object_unref (socket_stream);
720 send_tp_file (EmpathyTpFile *tp_file)
723 GOutputStream *socket_stream;
724 EmpathyTpFilePriv *priv;
726 priv = GET_PRIV (tp_file);
728 DEBUG ("Sending file content: filename=%s",
731 g_return_if_fail (priv->in_stream);
733 socket_fd = _get_local_socket (tp_file);
736 DEBUG ("failed to get local socket fd");
739 DEBUG ("got local socket fd");
740 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
742 priv->cancellable = g_cancellable_new ();
744 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
746 g_object_unref (socket_stream);
750 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
751 EmpFileTransferState state,
752 EmpFileTransferStateChangeReason reason,
753 EmpathyTpFile *tp_file)
755 EmpathyTpFilePriv *priv;
757 priv = GET_PRIV (tp_file);
759 DEBUG ("File transfer state changed: filename=%s, "
760 "old state=%u, state=%u, reason=%u",
761 priv->filename, priv->state, state, reason);
763 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
764 priv->start_time = get_time_msec ();
766 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
767 state, priv->incoming ? "yes" : "no",
768 priv->in_stream ? "present" : "not present",
769 priv->out_stream ? "present" : "not present");
771 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
773 send_tp_file (tp_file);
774 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
776 receive_tp_file (tp_file);
779 priv->state_change_reason = reason;
781 g_object_notify (G_OBJECT (tp_file), "state");
785 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
787 EmpathyTpFile *tp_file,
788 GObject *weak_object)
790 EmpathyTpFilePriv *priv;
792 priv = GET_PRIV (tp_file);
794 if (priv->transferred_bytes == count)
797 priv->transferred_bytes = count;
799 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
803 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
805 EmpathyTpFilePriv *priv;
807 priv = GET_PRIV (tp_file);
809 return priv->contact;
813 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
815 EmpathyTpFilePriv *priv;
817 priv = GET_PRIV (tp_file);
819 return priv->in_stream;
823 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
825 EmpathyTpFilePriv *priv;
827 priv = GET_PRIV (tp_file);
829 return priv->out_stream;
833 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
835 EmpathyTpFilePriv *priv;
837 priv = GET_PRIV (tp_file);
839 return priv->filename;
843 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
845 EmpathyTpFilePriv *priv;
847 priv = GET_PRIV (tp_file);
849 return priv->incoming;
853 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
855 EmpathyTpFilePriv *priv;
857 priv = GET_PRIV (tp_file);
862 EmpFileTransferStateChangeReason
863 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
865 EmpathyTpFilePriv *priv;
867 priv = GET_PRIV (tp_file);
869 g_return_val_if_fail (priv->state_change_reason >= 0,
870 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
872 return priv->state_change_reason;
876 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
878 EmpathyTpFilePriv *priv;
880 priv = GET_PRIV (tp_file);
886 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
888 EmpathyTpFilePriv *priv;
890 priv = GET_PRIV (tp_file);
892 return priv->transferred_bytes;
896 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
898 EmpathyTpFilePriv *priv;
899 gint64 curr_time, elapsed_time;
900 gdouble time_per_byte;
901 gdouble remaining_time;
903 priv = GET_PRIV (tp_file);
905 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
908 if (priv->transferred_bytes == priv->size)
911 curr_time = get_time_msec ();
912 elapsed_time = curr_time - priv->start_time;
913 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
914 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
916 return (gint) (remaining_time + 0.5);
920 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
922 EmpathyTpFilePriv *priv;
924 priv = GET_PRIV (tp_file);
926 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
928 g_cancellable_cancel (priv->cancellable);
932 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
933 GInputStream *in_stream)
935 EmpathyTpFilePriv *priv;
937 priv = GET_PRIV (tp_file);
939 if (priv->in_stream == in_stream)
943 g_warning ("Setting an input stream for incoming file "
944 "transfers is useless");
947 g_object_unref (priv->in_stream);
950 g_object_ref (in_stream);
952 priv->in_stream = in_stream;
954 g_object_notify (G_OBJECT (tp_file), "in-stream");
958 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
959 GOutputStream *out_stream)
961 EmpathyTpFilePriv *priv;
963 priv = GET_PRIV (tp_file);
965 if (priv->out_stream == out_stream)
969 g_warning ("Setting an output stream for outgoing file "
970 "transfers is useless");
972 if (priv->out_stream)
973 g_object_unref (priv->out_stream);
976 g_object_ref (out_stream);
978 priv->out_stream = out_stream;
982 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
983 const gchar *filename)
985 EmpathyTpFilePriv *priv;
987 priv = GET_PRIV (tp_file);
989 g_return_if_fail (filename != NULL);
991 if (priv->filename && strcmp (filename, priv->filename) == 0)
994 g_free (priv->filename);
995 priv->filename = g_strdup (filename);
997 g_object_notify (G_OBJECT (tp_file), "filename");
1000 /* Functions to copy the content of a GInputStream to a GOutputStream */
1003 #define BUFFER_SIZE 4096
1008 GCancellable *cancellable;
1009 char *buff[N_BUFFERS]; /* the temporary buffers */
1010 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
1011 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
1012 gint curr_read; /* index of the buffer used for reading */
1013 gint curr_write; /* index of the buffer used for writing */
1014 gboolean is_reading; /* we are reading */
1015 gboolean is_writing; /* we are writing */
1016 guint n_closed; /* number of streams that have been closed */
1019 static void schedule_next (CopyData *copy);
1022 free_copy_data_if_closed (CopyData *copy)
1026 /* Free the data only if both the input and output streams have
1029 if (copy->n_closed < 2)
1032 if (copy->in != NULL)
1033 g_object_unref (copy->in);
1035 if (copy->out != NULL)
1036 g_object_unref (copy->out);
1038 for (i = 0; i < N_BUFFERS; i++)
1039 g_free (copy->buff[i]);
1041 g_object_unref (copy->cancellable);
1046 io_error (CopyData *copy,
1049 g_cancellable_cancel (copy->cancellable);
1052 g_warning ("I/O error");
1053 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1054 ; /* Ignore cancellations */
1056 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1058 if (copy->in != NULL)
1059 g_input_stream_close (copy->in, NULL, NULL);
1061 if (copy->out != NULL)
1062 g_output_stream_close (copy->out, NULL, NULL);
1064 free_copy_data_if_closed (copy);
1068 close_done (GObject *source_object,
1072 CopyData *copy = user_data;
1074 g_object_unref (source_object);
1075 free_copy_data_if_closed (copy);
1079 write_done_cb (GObject *source_object,
1083 CopyData *copy = user_data;
1085 GError *error = NULL;
1087 count_write = g_output_stream_write_finish (copy->out, res, &error);
1089 if (count_write <= 0)
1091 io_error (copy, error);
1092 g_error_free (error);
1096 copy->is_full[copy->curr_write] = FALSE;
1097 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1098 copy->is_writing = FALSE;
1100 schedule_next (copy);
1104 read_done_cb (GObject *source_object,
1108 CopyData *copy = user_data;
1110 GError *error = NULL;
1112 count_read = g_input_stream_read_finish (copy->in, res, &error);
1114 if (count_read == 0)
1116 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1120 else if (count_read < 0)
1122 io_error (copy, error);
1123 g_error_free (error);
1127 copy->count[copy->curr_read] = count_read;
1128 copy->is_full[copy->curr_read] = TRUE;
1129 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1130 copy->is_reading = FALSE;
1132 schedule_next (copy);
1136 schedule_next (CopyData *copy)
1138 if (copy->in != NULL &&
1139 !copy->is_reading &&
1140 !copy->is_full[copy->curr_read])
1142 /* We are not reading and the current buffer is empty, so
1143 * start an async read. */
1144 copy->is_reading = TRUE;
1145 g_input_stream_read_async (copy->in,
1146 copy->buff[copy->curr_read],
1147 BUFFER_SIZE, 0, copy->cancellable,
1148 read_done_cb, copy);
1151 if (!copy->is_writing &&
1152 copy->is_full[copy->curr_write])
1154 if (copy->count[copy->curr_write] == 0)
1156 /* The last read on the buffer read 0 bytes, this
1157 * means that we got an EOF, so we can close
1158 * the output channel. */
1159 g_output_stream_close_async (copy->out, 0,
1166 /* We are not writing and the current buffer contains
1167 * data, so start an async write. */
1168 copy->is_writing = TRUE;
1169 g_output_stream_write_async (copy->out,
1170 copy->buff[copy->curr_write],
1171 copy->count[copy->curr_write],
1172 0, copy->cancellable,
1173 write_done_cb, copy);
1179 copy_stream (GInputStream *in,
1181 GCancellable *cancellable)
1186 g_return_if_fail (in != NULL);
1187 g_return_if_fail (out != NULL);
1189 copy = g_new0 (CopyData, 1);
1190 copy->in = g_object_ref (in);
1191 copy->out = g_object_ref (out);
1193 if (cancellable != NULL)
1194 copy->cancellable = g_object_ref (cancellable);
1196 copy->cancellable = g_cancellable_new ();
1198 for (i = 0; i < N_BUFFERS; i++)
1199 copy->buff[i] = g_malloc (BUFFER_SIZE);
1201 schedule_next (copy);