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_call_close (priv->channel, -1, NULL, NULL, 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_get_all_cb (TpProxy *proxy,
313 GHashTable *properties,
316 GObject *weak_object)
318 EmpathyTpFilePriv *priv = (EmpathyTpFilePriv *) user_data;
322 DEBUG ("Failed to get properties: %s", error->message);
326 priv->size = g_value_get_uint64 (
327 g_hash_table_lookup (properties, "Size"));
329 priv->state = g_value_get_uint (
330 g_hash_table_lookup (properties, "State"));
332 /* Invalid reason, so empathy_file_get_state_change_reason() can give
333 * a warning if called for a not closed file transfer. */
334 priv->state_change_reason = -1;
336 priv->transferred_bytes = g_value_get_uint64 (
337 g_hash_table_lookup (properties, "TransferredBytes"));
339 priv->filename = g_value_dup_string (
340 g_hash_table_lookup (properties, "Filename"));
342 priv->content_hash = g_value_dup_string (
343 g_hash_table_lookup (properties, "ContentHash"));
345 priv->description = g_value_dup_string (
346 g_hash_table_lookup (properties, "Description"));
348 if (priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
349 priv->incoming = TRUE;
351 g_hash_table_destroy (properties);
355 tp_file_constructor (GType type,
357 GObjectConstructParam *props)
360 EmpathyTpFilePriv *priv;
363 tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
366 priv = GET_PRIV (tp_file);
368 priv->factory = empathy_contact_factory_new ();
369 priv->mc = empathy_mission_control_new ();
371 tp_cli_channel_connect_to_closed (priv->channel,
372 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
376 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
377 TP_PROXY (priv->channel),
378 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
379 tp_file_state_changed_cb,
383 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
384 TP_PROXY (priv->channel),
385 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
386 tp_file_transferred_bytes_changed_cb,
391 handle = tp_channel_get_handle (priv->channel, NULL);
392 priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
396 tp_cli_dbus_properties_call_get_all (priv->channel,
397 -1, EMP_IFACE_CHANNEL_TYPE_FILE, tp_file_get_all_cb, priv, NULL, NULL);
403 tp_file_get_property (GObject *object,
408 EmpathyTpFilePriv *priv;
409 EmpathyTpFile *tp_file;
411 priv = GET_PRIV (object);
412 tp_file = EMPATHY_TP_FILE (object);
417 g_value_set_object (value, priv->account);
420 g_value_set_object (value, priv->channel);
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
429 tp_file_channel_set_dbus_property (gpointer proxy,
430 const gchar *property,
433 DEBUG ("Setting %s property", property);
434 tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
435 EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
436 NULL, NULL, NULL, NULL);
441 tp_file_set_property (GObject *object,
446 EmpathyTpFilePriv *priv;
448 priv = GET_PRIV (object);
453 priv->account = g_object_ref (g_value_get_object (value));
456 priv->channel = g_object_ref (g_value_get_object (value));
459 priv->state = g_value_get_uint (value);
462 priv->incoming = g_value_get_boolean (value);
465 g_free (priv->filename);
466 priv->filename = g_value_dup_string (value);
467 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
470 priv->size = g_value_get_uint64 (value);
471 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
473 case PROP_CONTENT_TYPE:
474 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
475 g_free (priv->content_type);
476 priv->content_type = g_value_dup_string (value);
478 case PROP_CONTENT_HASH:
479 tp_file_channel_set_dbus_property (priv->channel, "ContentHash", value);
480 g_free (priv->content_hash);
481 priv->content_hash = g_value_dup_string (value);
485 g_object_unref (priv->in_stream);
486 priv->in_stream = g_object_ref (g_value_get_object (value));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
495 * empathy_tp_file_new:
496 * @account: the #McAccount for the channel
497 * @channel: a Telepathy channel
499 * Creates a new #EmpathyTpFile wrapping @channel.
501 * Returns: a new #EmpathyTpFile
504 empathy_tp_file_new (McAccount *account,
507 return g_object_new (EMPATHY_TYPE_TP_FILE,
514 * empathy_tp_file_get_id:
515 * @tp_file: an #EmpathyTpFile
517 * Returns the ID of @tp_file.
522 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
524 EmpathyTpFilePriv *priv;
526 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
528 priv = GET_PRIV (tp_file);
534 * empathy_tp_file_get_channel
535 * @tp_file: an #EmpathyTpFile
537 * Returns the Telepathy file transfer channel
539 * Returns: the #TpChannel
542 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
544 EmpathyTpFilePriv *priv;
546 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
548 priv = GET_PRIV (tp_file);
550 return priv->channel;
554 tp_file_destroy_cb (TpChannel *file_channel,
555 EmpathyTpFile *tp_file)
557 EmpathyTpFilePriv *priv;
559 priv = GET_PRIV (tp_file);
561 DEBUG ("Channel Closed or CM crashed");
563 g_object_unref (priv->channel);
564 priv->channel = NULL;
568 tp_file_closed_cb (TpChannel *file_channel,
569 EmpathyTpFile *tp_file,
570 GObject *weak_object)
572 EmpathyTpFilePriv *priv;
574 priv = GET_PRIV (tp_file);
576 /* The channel is closed, do just like if the proxy was destroyed */
577 g_signal_handlers_disconnect_by_func (priv->channel,
580 tp_file_destroy_cb (file_channel, tp_file);
588 g_get_current_time (&tv);
589 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
593 _get_local_socket (EmpathyTpFile *tp_file)
597 struct sockaddr_un addr;
598 EmpathyTpFilePriv *priv;
600 priv = GET_PRIV (tp_file);
602 if (G_STR_EMPTY (priv->unix_socket_path))
605 fd = socket (PF_UNIX, SOCK_STREAM, 0);
609 memset (&addr, 0, sizeof (addr));
610 addr.sun_family = AF_UNIX;
611 path_len = strlen (priv->unix_socket_path);
612 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
614 if (connect (fd, (struct sockaddr*) &addr,
625 tp_file_method_cb (TpProxy *proxy,
626 const GValue *address,
629 GObject *weak_object)
631 EmpathyTpFilePriv *priv = (EmpathyTpFilePriv *) user_data;
635 DEBUG ("Error: %s", error->message);
639 if (priv->unix_socket_path)
640 g_free (priv->unix_socket_path);
642 priv->unix_socket_path = g_value_dup_string (address);
644 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
649 * empathy_tp_file_accept:
650 * @tp_file: an #EmpathyTpFile
652 * Accepts a file transfer that's in the "local pending" state (i.e.
653 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
656 empathy_tp_file_accept (EmpathyTpFile *tp_file,
659 EmpathyTpFilePriv *priv;
660 GValue nothing = { 0 };
662 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
664 priv = GET_PRIV (tp_file);
666 g_return_if_fail (priv->out_stream != NULL);
668 DEBUG ("Accepting file: filename=%s", priv->filename);
670 g_value_init (¬hing, G_TYPE_STRING);
671 g_value_set_string (¬hing, "");
673 emp_cli_channel_type_file_call_accept_file (TP_PROXY (priv->channel),
674 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
675 ¬hing, offset, tp_file_method_cb, priv, NULL, NULL);
679 * empathy_tp_file_offer:
680 * @tp_file: an #EmpathyTpFile
682 * Offers a file transfer that's in the "not offered" state (i.e.
683 * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
686 empathy_tp_file_offer (EmpathyTpFile *tp_file)
688 EmpathyTpFilePriv *priv;
689 GValue nothing = { 0 };
691 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
693 priv = GET_PRIV (tp_file);
695 g_value_init (¬hing, G_TYPE_STRING);
696 g_value_set_string (¬hing, "");
698 emp_cli_channel_type_file_call_offer_file (TP_PROXY (priv->channel),
699 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
700 ¬hing, tp_file_method_cb, priv, NULL, NULL);
704 receive_tp_file (EmpathyTpFile *tp_file)
706 EmpathyTpFilePriv *priv;
707 GInputStream *socket_stream;
710 priv = GET_PRIV (tp_file);
712 socket_fd = _get_local_socket (tp_file);
717 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
719 priv->cancellable = g_cancellable_new ();
721 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
723 g_object_unref (socket_stream);
727 send_tp_file (EmpathyTpFile *tp_file)
730 GOutputStream *socket_stream;
731 EmpathyTpFilePriv *priv;
733 priv = GET_PRIV (tp_file);
735 DEBUG ("Sending file content: filename=%s",
738 g_return_if_fail (priv->in_stream);
740 socket_fd = _get_local_socket (tp_file);
743 DEBUG ("failed to get local socket fd");
746 DEBUG ("got local socket fd");
747 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
749 priv->cancellable = g_cancellable_new ();
751 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
753 g_object_unref (socket_stream);
757 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
758 EmpFileTransferState state,
759 EmpFileTransferStateChangeReason reason,
760 EmpathyTpFile *tp_file)
762 EmpathyTpFilePriv *priv;
764 priv = GET_PRIV (tp_file);
766 DEBUG ("File transfer state changed: filename=%s, "
767 "old state=%u, state=%u, reason=%u",
768 priv->filename, priv->state, state, reason);
770 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
771 priv->start_time = get_time_msec ();
773 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
774 state, priv->incoming ? "yes" : "no",
775 priv->in_stream ? "present" : "not present",
776 priv->out_stream ? "present" : "not present");
778 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
780 send_tp_file (tp_file);
781 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
783 receive_tp_file (tp_file);
786 priv->state_change_reason = reason;
788 g_object_notify (G_OBJECT (tp_file), "state");
792 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
794 EmpathyTpFile *tp_file,
795 GObject *weak_object)
797 EmpathyTpFilePriv *priv;
799 priv = GET_PRIV (tp_file);
801 if (priv->transferred_bytes == count)
804 priv->transferred_bytes = count;
806 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
810 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
812 EmpathyTpFilePriv *priv;
814 priv = GET_PRIV (tp_file);
816 return priv->contact;
820 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
822 EmpathyTpFilePriv *priv;
824 priv = GET_PRIV (tp_file);
826 return priv->in_stream;
830 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
832 EmpathyTpFilePriv *priv;
834 priv = GET_PRIV (tp_file);
836 return priv->out_stream;
840 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
842 EmpathyTpFilePriv *priv;
844 priv = GET_PRIV (tp_file);
846 return priv->filename;
850 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
852 EmpathyTpFilePriv *priv;
854 priv = GET_PRIV (tp_file);
856 return priv->incoming;
860 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
862 EmpathyTpFilePriv *priv;
864 priv = GET_PRIV (tp_file);
869 EmpFileTransferStateChangeReason
870 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
872 EmpathyTpFilePriv *priv;
874 priv = GET_PRIV (tp_file);
876 g_return_val_if_fail (priv->state_change_reason >= 0,
877 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
879 return priv->state_change_reason;
883 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
885 EmpathyTpFilePriv *priv;
887 priv = GET_PRIV (tp_file);
893 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
895 EmpathyTpFilePriv *priv;
897 priv = GET_PRIV (tp_file);
899 return priv->transferred_bytes;
903 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
905 EmpathyTpFilePriv *priv;
906 gint64 curr_time, elapsed_time;
907 gdouble time_per_byte;
908 gdouble remaining_time;
910 priv = GET_PRIV (tp_file);
912 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
915 if (priv->transferred_bytes == priv->size)
918 curr_time = get_time_msec ();
919 elapsed_time = curr_time - priv->start_time;
920 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
921 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
923 return (gint) (remaining_time + 0.5);
927 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
929 EmpathyTpFilePriv *priv;
931 priv = GET_PRIV (tp_file);
933 tp_cli_channel_call_close (priv->channel, -1, NULL, NULL, NULL, NULL);
935 g_cancellable_cancel (priv->cancellable);
939 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
940 GInputStream *in_stream)
942 EmpathyTpFilePriv *priv;
944 priv = GET_PRIV (tp_file);
946 if (priv->in_stream == in_stream)
950 g_warning ("Setting an input stream for incoming file "
951 "transfers is useless");
954 g_object_unref (priv->in_stream);
957 g_object_ref (in_stream);
959 priv->in_stream = in_stream;
961 g_object_notify (G_OBJECT (tp_file), "in-stream");
965 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
966 GOutputStream *out_stream)
968 EmpathyTpFilePriv *priv;
970 priv = GET_PRIV (tp_file);
972 if (priv->out_stream == out_stream)
976 g_warning ("Setting an output stream for outgoing file "
977 "transfers is useless");
979 if (priv->out_stream)
980 g_object_unref (priv->out_stream);
983 g_object_ref (out_stream);
985 priv->out_stream = out_stream;
989 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
990 const gchar *filename)
992 EmpathyTpFilePriv *priv;
994 priv = GET_PRIV (tp_file);
996 g_return_if_fail (filename != NULL);
998 if (priv->filename && strcmp (filename, priv->filename) == 0)
1001 g_free (priv->filename);
1002 priv->filename = g_strdup (filename);
1004 g_object_notify (G_OBJECT (tp_file), "filename");
1007 /* Functions to copy the content of a GInputStream to a GOutputStream */
1010 #define BUFFER_SIZE 4096
1015 GCancellable *cancellable;
1016 char *buff[N_BUFFERS]; /* the temporary buffers */
1017 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
1018 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
1019 gint curr_read; /* index of the buffer used for reading */
1020 gint curr_write; /* index of the buffer used for writing */
1021 gboolean is_reading; /* we are reading */
1022 gboolean is_writing; /* we are writing */
1023 guint n_closed; /* number of streams that have been closed */
1026 static void schedule_next (CopyData *copy);
1029 free_copy_data_if_closed (CopyData *copy)
1033 /* Free the data only if both the input and output streams have
1036 if (copy->n_closed < 2)
1039 if (copy->in != NULL)
1040 g_object_unref (copy->in);
1042 if (copy->out != NULL)
1043 g_object_unref (copy->out);
1045 for (i = 0; i < N_BUFFERS; i++)
1046 g_free (copy->buff[i]);
1048 g_object_unref (copy->cancellable);
1053 io_error (CopyData *copy,
1056 g_cancellable_cancel (copy->cancellable);
1059 g_warning ("I/O error");
1060 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1061 ; /* Ignore cancellations */
1063 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1065 if (copy->in != NULL)
1066 g_input_stream_close (copy->in, NULL, NULL);
1068 if (copy->out != NULL)
1069 g_output_stream_close (copy->out, NULL, NULL);
1071 free_copy_data_if_closed (copy);
1075 close_done (GObject *source_object,
1079 CopyData *copy = user_data;
1081 g_object_unref (source_object);
1082 free_copy_data_if_closed (copy);
1086 write_done_cb (GObject *source_object,
1090 CopyData *copy = user_data;
1092 GError *error = NULL;
1094 count_write = g_output_stream_write_finish (copy->out, res, &error);
1096 if (count_write <= 0)
1098 io_error (copy, error);
1099 g_error_free (error);
1103 copy->is_full[copy->curr_write] = FALSE;
1104 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1105 copy->is_writing = FALSE;
1107 schedule_next (copy);
1111 read_done_cb (GObject *source_object,
1115 CopyData *copy = user_data;
1117 GError *error = NULL;
1119 count_read = g_input_stream_read_finish (copy->in, res, &error);
1121 if (count_read == 0)
1123 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1127 else if (count_read < 0)
1129 io_error (copy, error);
1130 g_error_free (error);
1134 copy->count[copy->curr_read] = count_read;
1135 copy->is_full[copy->curr_read] = TRUE;
1136 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1137 copy->is_reading = FALSE;
1139 schedule_next (copy);
1143 schedule_next (CopyData *copy)
1145 if (copy->in != NULL &&
1146 !copy->is_reading &&
1147 !copy->is_full[copy->curr_read])
1149 /* We are not reading and the current buffer is empty, so
1150 * start an async read. */
1151 copy->is_reading = TRUE;
1152 g_input_stream_read_async (copy->in,
1153 copy->buff[copy->curr_read],
1154 BUFFER_SIZE, 0, copy->cancellable,
1155 read_done_cb, copy);
1158 if (!copy->is_writing &&
1159 copy->is_full[copy->curr_write])
1161 if (copy->count[copy->curr_write] == 0)
1163 /* The last read on the buffer read 0 bytes, this
1164 * means that we got an EOF, so we can close
1165 * the output channel. */
1166 g_output_stream_close_async (copy->out, 0,
1173 /* We are not writing and the current buffer contains
1174 * data, so start an async write. */
1175 copy->is_writing = TRUE;
1176 g_output_stream_write_async (copy->out,
1177 copy->buff[copy->curr_write],
1178 copy->count[copy->curr_write],
1179 0, copy->cancellable,
1180 write_done_cb, copy);
1186 copy_stream (GInputStream *in,
1188 GCancellable *cancellable)
1193 g_return_if_fail (in != NULL);
1194 g_return_if_fail (out != NULL);
1196 copy = g_new0 (CopyData, 1);
1197 copy->in = g_object_ref (in);
1198 copy->out = g_object_ref (out);
1200 if (cancellable != NULL)
1201 copy->cancellable = g_object_ref (cancellable);
1203 copy->cancellable = g_cancellable_new ();
1205 for (i = 0; i < N_BUFFERS; i++)
1206 copy->buff[i] = g_malloc (BUFFER_SIZE);
1208 schedule_next (copy);