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-lib.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
38 #include <telepathy-glib/proxy-subclass.h>
39 #include <telepathy-glib/util.h>
41 #include "empathy-tp-file.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
47 #include "extensions/extensions.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_FT
50 #include "empathy-debug.h"
53 * SECTION:empathy-tp-file
54 * @short_description: File channel
55 * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_send_file()
56 * @include: libempthy/empathy-tp-file.h
58 * The #EmpathyTpFile object represents a Telepathy file channel.
62 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
64 * Value used for the "size" or "estimated-size" properties when the size of
65 * the transferred file is unknown.
68 /* Functions to copy the content of a GInputStream to a GOutputStream */
71 #define BUFFER_SIZE 4096
76 GCancellable *cancellable;
77 char *buff[N_BUFFERS]; /* the temporary buffers */
78 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
79 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
80 gint curr_read; /* index of the buffer used for reading */
81 gint curr_write; /* index of the buffer used for writing */
82 gboolean is_reading; /* we are reading */
83 gboolean is_writing; /* we are writing */
84 guint n_closed; /* number of streams that have been closed */
88 static void schedule_next (CopyData *copy);
91 copy_data_unref (CopyData *copy)
93 if (--copy->ref_count == 0)
97 /* Free the data only if both the input and output streams have
100 if (copy->n_closed < 2)
103 if (copy->in != NULL)
104 g_object_unref (copy->in);
106 if (copy->out != NULL)
107 g_object_unref (copy->out);
109 for (i = 0; i < N_BUFFERS; i++)
110 g_free (copy->buff[i]);
112 g_object_unref (copy->cancellable);
118 io_error (CopyData *copy,
121 g_cancellable_cancel (copy->cancellable);
124 g_warning ("I/O error");
125 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
126 ; /* Ignore cancellations */
128 g_warning ("I/O error: %d: %s\n", error->code, error->message);
130 if (copy->in != NULL)
131 g_input_stream_close (copy->in, NULL, NULL);
133 if (copy->out != NULL)
134 g_output_stream_close (copy->out, NULL, NULL);
136 copy_data_unref (copy);
140 close_done (GObject *source_object,
144 CopyData *copy = user_data;
146 g_object_unref (source_object);
147 copy_data_unref (copy);
151 write_done_cb (GObject *source_object,
155 CopyData *copy = user_data;
157 GError *error = NULL;
159 count_write = g_output_stream_write_finish (copy->out, res, &error);
161 if (count_write <= 0)
163 io_error (copy, error);
164 g_error_free (error);
168 copy->is_full[copy->curr_write] = FALSE;
169 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
170 copy->is_writing = FALSE;
172 schedule_next (copy);
176 read_done_cb (GObject *source_object,
180 CopyData *copy = user_data;
182 GError *error = NULL;
184 count_read = g_input_stream_read_finish (copy->in, res, &error);
188 g_input_stream_close_async (copy->in, 0, copy->cancellable,
192 else if (count_read < 0)
194 io_error (copy, error);
195 g_error_free (error);
199 copy->count[copy->curr_read] = count_read;
200 copy->is_full[copy->curr_read] = TRUE;
201 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
202 copy->is_reading = FALSE;
204 schedule_next (copy);
208 schedule_next (CopyData *copy)
210 if (copy->in != NULL &&
212 !copy->is_full[copy->curr_read])
214 /* We are not reading and the current buffer is empty, so
215 * start an async read. */
216 copy->is_reading = TRUE;
217 g_input_stream_read_async (copy->in,
218 copy->buff[copy->curr_read],
219 BUFFER_SIZE, 0, copy->cancellable,
223 if (!copy->is_writing &&
224 copy->is_full[copy->curr_write])
226 if (copy->count[copy->curr_write] == 0)
228 /* The last read on the buffer read 0 bytes, this
229 * means that we got an EOF, so we can close
230 * the output channel. */
231 g_output_stream_close_async (copy->out, 0,
238 /* We are not writing and the current buffer contains
239 * data, so start an async write. */
240 copy->is_writing = TRUE;
241 g_output_stream_write_async (copy->out,
242 copy->buff[copy->curr_write],
243 copy->count[copy->curr_write],
244 0, copy->cancellable,
245 write_done_cb, copy);
251 copy_stream (GInputStream *in,
253 GCancellable *cancellable)
258 g_return_if_fail (in != NULL);
259 g_return_if_fail (out != NULL);
261 copy = g_new0 (CopyData, 1);
262 copy->in = g_object_ref (in);
263 copy->out = g_object_ref (out);
266 if (cancellable != NULL)
267 copy->cancellable = g_object_ref (cancellable);
269 copy->cancellable = g_cancellable_new ();
271 for (i = 0; i < N_BUFFERS; i++)
272 copy->buff[i] = g_malloc (BUFFER_SIZE);
274 schedule_next (copy);
277 /* EmpathyTpFile object */
279 struct _EmpathyTpFilePriv {
280 EmpathyContactFactory *factory;
284 EmpathyContact *contact;
285 GInputStream *in_stream;
286 GOutputStream *out_stream;
288 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
289 TpFileTransferState state;
293 TpFileHashType content_hash_type;
296 guint64 transferred_bytes;
299 TpFileTransferStateChangeReason state_change_reason;
301 GValue *socket_address;
302 GCancellable *cancellable;
313 PROP_TRANSFERRED_BYTES,
314 PROP_CONTENT_HASH_TYPE,
318 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
321 empathy_tp_file_init (EmpathyTpFile *tp_file)
323 EmpathyTpFilePriv *priv;
325 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
326 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
328 tp_file->priv = priv;
332 tp_file_invalidated_cb (TpProxy *proxy,
336 EmpathyTpFile *tp_file)
338 DEBUG ("Channel invalidated: %s", message);
340 if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
341 tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
343 /* The channel is not in a finished state, an error occured */
344 tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
345 tp_file->priv->state_change_reason =
346 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
347 g_object_notify (G_OBJECT (tp_file), "state");
352 tp_file_finalize (GObject *object)
354 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
356 if (tp_file->priv->channel)
358 g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
359 tp_file_invalidated_cb, object);
360 g_object_unref (tp_file->priv->channel);
361 tp_file->priv->channel = NULL;
364 if (tp_file->priv->factory)
366 g_object_unref (tp_file->priv->factory);
368 if (tp_file->priv->mc)
370 g_object_unref (tp_file->priv->mc);
373 g_free (tp_file->priv->filename);
374 if (tp_file->priv->socket_address != NULL)
375 tp_g_value_slice_free (tp_file->priv->socket_address);
376 g_free (tp_file->priv->description);
377 g_free (tp_file->priv->content_hash);
378 g_free (tp_file->priv->content_type);
380 if (tp_file->priv->in_stream)
381 g_object_unref (tp_file->priv->in_stream);
383 if (tp_file->priv->out_stream)
384 g_object_unref (tp_file->priv->out_stream);
386 if (tp_file->priv->contact)
387 g_object_unref (tp_file->priv->contact);
389 if (tp_file->priv->cancellable)
390 g_object_unref (tp_file->priv->cancellable);
392 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
396 tp_file_start_transfer (EmpathyTpFile *tp_file)
399 struct sockaddr_un addr;
402 fd = socket (PF_UNIX, SOCK_STREAM, 0);
405 DEBUG ("Failed to create socket, closing channel");
406 empathy_tp_file_cancel (tp_file);
410 array = g_value_get_boxed (tp_file->priv->socket_address);
412 memset (&addr, 0, sizeof (addr));
413 addr.sun_family = AF_UNIX;
414 strncpy (addr.sun_path, array->data, array->len);
416 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
418 DEBUG ("Failed to connect socket, closing channel");
419 empathy_tp_file_cancel (tp_file);
424 DEBUG ("Start the transfer");
426 tp_file->priv->start_time = empathy_time_get_current ();
427 tp_file->priv->cancellable = g_cancellable_new ();
428 if (tp_file->priv->incoming)
430 GInputStream *socket_stream;
432 socket_stream = g_unix_input_stream_new (fd, TRUE);
433 copy_stream (socket_stream, tp_file->priv->out_stream,
434 tp_file->priv->cancellable);
435 g_object_unref (socket_stream);
439 GOutputStream *socket_stream;
441 socket_stream = g_unix_output_stream_new (fd, TRUE);
442 copy_stream (tp_file->priv->in_stream, socket_stream,
443 tp_file->priv->cancellable);
444 g_object_unref (socket_stream);
449 tp_file_state_changed_cb (TpChannel *channel,
453 GObject *weak_object)
455 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
457 if (state == tp_file->priv->state)
460 DEBUG ("File transfer state changed:\n"
461 "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
462 "\tincoming = %s, in_stream = %s, out_stream = %s",
463 tp_file->priv->filename, tp_file->priv->state, state, reason,
464 tp_file->priv->incoming ? "yes" : "no",
465 tp_file->priv->in_stream ? "present" : "not present",
466 tp_file->priv->out_stream ? "present" : "not present");
468 /* If the channel is open AND we have the socket path, we can start the
469 * transfer. The socket path could be NULL if we are not doing the actual
470 * data transfer but are just an observer for the channel. */
471 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
472 tp_file->priv->socket_address != NULL)
473 tp_file_start_transfer (tp_file);
475 tp_file->priv->state = state;
476 tp_file->priv->state_change_reason = reason;
478 g_object_notify (G_OBJECT (tp_file), "state");
482 tp_file_transferred_bytes_changed_cb (TpChannel *channel,
485 GObject *weak_object)
487 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
489 if (tp_file->priv->transferred_bytes == count)
492 tp_file->priv->transferred_bytes = count;
493 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
497 tp_file_constructor (GType type,
499 GObjectConstructParam *props)
502 EmpathyTpFile *tp_file;
504 GHashTable *properties;
508 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
511 tp_file = EMPATHY_TP_FILE (file_obj);
513 tp_file->priv->factory = empathy_contact_factory_dup_singleton ();
514 tp_file->priv->mc = empathy_mission_control_dup_singleton ();
516 g_signal_connect (tp_file->priv->channel, "invalidated",
517 G_CALLBACK (tp_file_invalidated_cb), tp_file);
519 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
520 tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL,
521 G_OBJECT (tp_file), NULL);
523 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
524 tp_file->priv->channel, tp_file_transferred_bytes_changed_cb,
525 NULL, NULL, G_OBJECT (tp_file), NULL);
527 account = empathy_channel_get_account (tp_file->priv->channel);
529 handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
530 tp_file->priv->contact = empathy_contact_factory_get_from_handle (
531 tp_file->priv->factory, account, (guint) handle);
533 tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
534 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, &properties, NULL, NULL);
536 tp_cli_dbus_properties_run_get (tp_file->priv->channel,
537 -1, TP_IFACE_CHANNEL, "Requested", &requested, NULL, NULL);
539 tp_file->priv->size = g_value_get_uint64 (
540 g_hash_table_lookup (properties, "Size"));
542 tp_file->priv->state = g_value_get_uint (
543 g_hash_table_lookup (properties, "State"));
545 tp_file->priv->state_change_reason =
546 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
548 tp_file->priv->transferred_bytes = g_value_get_uint64 (
549 g_hash_table_lookup (properties, "TransferredBytes"));
551 tp_file->priv->filename = g_value_dup_string (
552 g_hash_table_lookup (properties, "Filename"));
554 tp_file->priv->content_hash = g_value_dup_string (
555 g_hash_table_lookup (properties, "ContentHash"));
557 tp_file->priv->content_hash_type = g_value_get_uint (
558 g_hash_table_lookup (properties, "ContentHashType"));
560 tp_file->priv->content_type = g_value_dup_string (
561 g_hash_table_lookup (properties, "ContentType"));
563 tp_file->priv->description = g_value_dup_string (
564 g_hash_table_lookup (properties, "Description"));
566 tp_file->priv->incoming = !g_value_get_boolean (requested);
568 g_hash_table_destroy (properties);
569 g_object_unref (account);
570 g_value_unset (requested);
576 tp_file_get_property (GObject *object,
581 EmpathyTpFile *tp_file;
583 tp_file = EMPATHY_TP_FILE (object);
588 g_value_set_object (value, tp_file->priv->channel);
591 g_value_set_boolean (value, tp_file->priv->incoming);
594 g_value_set_uint (value, tp_file->priv->state);
596 case PROP_CONTENT_TYPE:
597 g_value_set_string (value, tp_file->priv->content_type);
600 g_value_set_string (value, tp_file->priv->filename);
603 g_value_set_uint64 (value, tp_file->priv->size);
605 case PROP_CONTENT_HASH_TYPE:
606 g_value_set_uint (value, tp_file->priv->content_hash_type);
608 case PROP_CONTENT_HASH:
609 g_value_set_string (value, tp_file->priv->content_hash);
611 case PROP_TRANSFERRED_BYTES:
612 g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
621 tp_file_channel_set_dbus_property (gpointer proxy,
622 const gchar *property,
625 DEBUG ("Setting %s property", property);
626 tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
627 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
628 NULL, NULL, NULL, NULL);
632 tp_file_set_property (GObject *object,
637 EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
641 tp_file->priv->channel = g_object_ref (g_value_get_object (value));
644 tp_file->priv->state = g_value_get_uint (value);
647 tp_file->priv->incoming = g_value_get_boolean (value);
650 g_free (tp_file->priv->filename);
651 tp_file->priv->filename = g_value_dup_string (value);
652 tp_file_channel_set_dbus_property (tp_file->priv->channel,
656 tp_file->priv->size = g_value_get_uint64 (value);
657 tp_file_channel_set_dbus_property (tp_file->priv->channel,
660 case PROP_CONTENT_TYPE:
661 tp_file_channel_set_dbus_property (tp_file->priv->channel,
662 "ContentType", value);
663 g_free (tp_file->priv->content_type);
664 tp_file->priv->content_type = g_value_dup_string (value);
666 case PROP_CONTENT_HASH:
667 tp_file_channel_set_dbus_property (tp_file->priv->channel,
668 "ContentHash", value);
669 g_free (tp_file->priv->content_hash);
670 tp_file->priv->content_hash = g_value_dup_string (value);
673 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
678 static GHashTable *ft_table = NULL;
681 tp_file_weak_notify_cb (gpointer channel,
684 g_hash_table_remove (ft_table, channel);
688 * empathy_tp_file_new:
689 * @channel: a Telepathy channel
691 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
692 * existing #EmpathyTpFile for that channel.
694 * Returns: a new #EmpathyTpFile
697 empathy_tp_file_new (TpChannel *channel)
699 EmpathyTpFile *tp_file;
701 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
703 if (ft_table != NULL)
705 tp_file = g_hash_table_lookup (ft_table, channel);
706 if (tp_file != NULL) {
707 return g_object_ref (tp_file);
711 ft_table = g_hash_table_new_full (empathy_proxy_hash,
712 empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL);
714 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
718 g_hash_table_insert (ft_table, g_object_ref (channel), tp_file);
719 g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel);
725 * empathy_tp_file_get_channel
726 * @tp_file: an #EmpathyTpFile
728 * Returns the Telepathy file transfer channel
730 * Returns: the #TpChannel
733 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
735 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
737 return tp_file->priv->channel;
741 tp_file_method_cb (TpChannel *channel,
742 const GValue *address,
745 GObject *weak_object)
747 EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
752 DEBUG ("Error: %s", error->message);
753 empathy_tp_file_cancel (tp_file);
757 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
759 tp_file->priv->socket_address = tp_g_value_slice_dup (address);
761 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
763 /* Old bugged version of telepathy-salut used to store the address
764 * as a 's' instead of an 'ay' */
767 path = g_value_get_string (address);
768 array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path));
769 g_array_insert_vals (array, 0, path, strlen (path));
771 tp_file->priv->socket_address = tp_g_value_slice_new (
772 DBUS_TYPE_G_UCHAR_ARRAY);
773 g_value_set_boxed (tp_file->priv->socket_address, array);
775 g_array_free (array, TRUE);
779 DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address));
780 empathy_tp_file_cancel (tp_file);
784 array = g_value_get_boxed (tp_file->priv->socket_address);
785 DEBUG ("Got unix socket path: %s", array->data);
787 if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN)
788 tp_file_start_transfer (tp_file);
792 * empathy_tp_file_accept:
793 * @tp_file: an #EmpathyTpFile
794 * @offset: position where to start the transfer
795 * @gfile: a #GFile where to write transfered data
796 * @error: a #GError set if there is an error when opening @gfile
798 * Accepts a file transfer that's in the "local pending" state (i.e.
799 * TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
802 empathy_tp_file_accept (EmpathyTpFile *tp_file,
807 GValue nothing = { 0 };
809 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
810 g_return_if_fail (G_IS_FILE (gfile));
812 tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
813 FALSE, 0, NULL, error));
817 tp_file->priv->filename = g_file_get_basename (gfile);
818 g_object_notify (G_OBJECT (tp_file), "filename");
820 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
822 g_value_init (¬hing, G_TYPE_STRING);
823 g_value_set_static_string (¬hing, "");
825 tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
826 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
827 ¬hing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
831 * empathy_tp_file_offer:
832 * @tp_file: an #EmpathyTpFile
833 * @gfile: a #GFile where to read the data to transfer
834 * @error: a #GError set if there is an error when opening @gfile
836 * Offers a file transfer that's in the "not offered" state (i.e.
837 * TP_FILE_TRANSFER_STATE_NOT_OFFERED).
840 empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
842 GValue nothing = { 0 };
844 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
846 tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
850 g_value_init (¬hing, G_TYPE_STRING);
851 g_value_set_static_string (¬hing, "");
853 tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
854 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
855 ¬hing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
859 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
861 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
862 return tp_file->priv->contact;
866 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
868 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
869 return tp_file->priv->filename;
873 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
875 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
876 return tp_file->priv->incoming;
880 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
881 TpFileTransferStateChangeReason *reason)
883 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
884 TP_FILE_TRANSFER_STATE_NONE);
887 *reason = tp_file->priv->state_change_reason;
889 return tp_file->priv->state;
893 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
895 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
896 EMPATHY_TP_FILE_UNKNOWN_SIZE);
897 return tp_file->priv->size;
901 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
903 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
904 return tp_file->priv->transferred_bytes;
908 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
910 time_t curr_time, elapsed_time;
911 gdouble time_per_byte;
912 gdouble remaining_time;
914 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
916 if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
919 if (tp_file->priv->transferred_bytes == tp_file->priv->size)
922 curr_time = empathy_time_get_current ();
923 elapsed_time = curr_time - tp_file->priv->start_time;
924 time_per_byte = (gdouble) elapsed_time /
925 (gdouble) tp_file->priv->transferred_bytes;
926 remaining_time = time_per_byte * (tp_file->priv->size -
927 tp_file->priv->transferred_bytes);
929 return (gint) remaining_time;
933 empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
935 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
936 return tp_file->priv->content_type;
940 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
942 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
944 DEBUG ("Closing channel..");
945 tp_cli_channel_call_close (tp_file->priv->channel, -1,
946 NULL, NULL, NULL, NULL);
948 if (tp_file->priv->cancellable != NULL)
949 g_cancellable_cancel (tp_file->priv->cancellable);
953 empathy_tp_file_close (EmpathyTpFile *tp_file)
955 empathy_tp_file_cancel (tp_file);
959 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
961 GObjectClass *object_class = G_OBJECT_CLASS (klass);
963 object_class->finalize = tp_file_finalize;
964 object_class->constructor = tp_file_constructor;
965 object_class->get_property = tp_file_get_property;
966 object_class->set_property = tp_file_set_property;
968 /* Construct-only properties */
969 g_object_class_install_property (object_class,
971 g_param_spec_object ("channel",
973 "The file transfer channel",
976 G_PARAM_CONSTRUCT_ONLY));
978 g_object_class_install_property (object_class,
980 g_param_spec_uint ("state",
981 "state of the transfer",
982 "The file transfer state",
989 g_object_class_install_property (object_class,
991 g_param_spec_boolean ("incoming",
993 "Whether the transfer is incoming",
998 g_object_class_install_property (object_class,
1000 g_param_spec_string ("filename",
1001 "name of the transfer",
1002 "The file transfer filename",
1004 G_PARAM_READWRITE));
1006 g_object_class_install_property (object_class,
1008 g_param_spec_uint64 ("size",
1010 "The file transfer size",
1014 G_PARAM_READWRITE));
1016 g_object_class_install_property (object_class,
1018 g_param_spec_string ("content-type",
1019 "file transfer content-type",
1020 "The file transfer content-type",
1022 G_PARAM_READWRITE));
1024 g_object_class_install_property (object_class,
1025 PROP_CONTENT_HASH_TYPE,
1026 g_param_spec_uint ("content-hash-type",
1027 "file transfer hash type",
1028 "The type of the file transfer hash",
1032 G_PARAM_READWRITE));
1034 g_object_class_install_property (object_class,
1036 g_param_spec_string ("content-hash",
1037 "file transfer hash",
1038 "The hash of the transfer's contents",
1040 G_PARAM_READWRITE));
1042 g_object_class_install_property (object_class,
1043 PROP_TRANSFERRED_BYTES,
1044 g_param_spec_uint64 ("transferred-bytes",
1045 "bytes transferred",
1046 "The number of bytes transferred",
1050 G_PARAM_READWRITE));
1052 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));