2 * Copyright (C) 2007-2008 Collabora Ltd.
3 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Marco Barisione <marco@barisione.org>
20 * Jonny Lamb <jonny.lamb@collabora.co.uk>
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <glib/gi18n-lib.h>
34 #include <gio/gunixinputstream.h>
35 #include <gio/gunixoutputstream.h>
37 #include <telepathy-glib/proxy-subclass.h>
38 #include <telepathy-glib/util.h>
40 #include "empathy-tp-file.h"
41 #include "empathy-tp-contact-factory.h"
42 #include "empathy-marshal.h"
43 #include "empathy-time.h"
44 #include "empathy-utils.h"
46 #include "extensions/extensions.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_FT
49 #include "empathy-debug.h"
52 * SECTION:empathy-tp-file
53 * @title: EmpathyTpFile
54 * @short_description: Object which represents a Telepathy file channel
55 * @include: libempathy/empathy-tp-file.h
57 * #EmpathyTpFile is an object which represents a Telepathy file channel.
62 * @parent: parent object
64 * Object which represents a Telepathy file channel.
68 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
70 * Value used for the "size" or "estimated-size" properties when the size of
71 * the transferred file is unknown.
74 /* Functions to copy the content of a GInputStream to a GOutputStream */
77 #define BUFFER_SIZE 4096
78 #define STALLED_TIMEOUT 5
83 GCancellable *cancellable;
84 char *buff[N_BUFFERS]; /* the temporary buffers */
85 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
86 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
87 gint curr_read; /* index of the buffer used for reading */
88 gint curr_write; /* index of the buffer used for writing */
89 gboolean is_reading; /* we are reading */
90 gboolean is_writing; /* we are writing */
91 guint n_closed; /* number of streams that have been closed */
95 static void schedule_next (CopyData *copy);
98 copy_data_unref (CopyData *copy)
100 if (--copy->ref_count == 0)
104 /* Free the data only if both the input and output streams have
107 if (copy->n_closed < 2)
110 if (copy->in != NULL)
111 g_object_unref (copy->in);
113 if (copy->out != NULL)
114 g_object_unref (copy->out);
116 for (i = 0; i < N_BUFFERS; i++)
117 g_free (copy->buff[i]);
119 g_object_unref (copy->cancellable);
125 io_error (CopyData *copy,
128 g_cancellable_cancel (copy->cancellable);
131 g_warning ("I/O error");
132 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
133 ; /* Ignore cancellations */
135 g_warning ("I/O error: %d: %s\n", error->code, error->message);
137 if (copy->in != NULL)
138 g_input_stream_close (copy->in, NULL, NULL);
140 if (copy->out != NULL)
141 g_output_stream_close (copy->out, NULL, NULL);
143 copy_data_unref (copy);
147 close_done (GObject *source_object,
151 CopyData *copy = user_data;
153 g_object_unref (source_object);
154 copy_data_unref (copy);
158 write_done_cb (GObject *source_object,
162 CopyData *copy = user_data;
164 GError *error = NULL;
166 count_write = g_output_stream_write_finish (copy->out, res, &error);
168 if (count_write <= 0)
170 io_error (copy, error);
171 g_error_free (error);
175 copy->is_full[copy->curr_write] = FALSE;
176 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
177 copy->is_writing = FALSE;
179 schedule_next (copy);
183 read_done_cb (GObject *source_object,
187 CopyData *copy = user_data;
189 GError *error = NULL;
191 count_read = g_input_stream_read_finish (copy->in, res, &error);
195 g_input_stream_close_async (copy->in, 0, copy->cancellable,
199 else if (count_read < 0)
201 io_error (copy, error);
202 g_error_free (error);
206 copy->count[copy->curr_read] = count_read;
207 copy->is_full[copy->curr_read] = TRUE;
208 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
209 copy->is_reading = FALSE;
211 schedule_next (copy);
215 schedule_next (CopyData *copy)
217 if (copy->in != NULL &&
219 !copy->is_full[copy->curr_read])
221 /* We are not reading and the current buffer is empty, so
222 * start an async read. */
223 copy->is_reading = TRUE;
224 g_input_stream_read_async (copy->in,
225 copy->buff[copy->curr_read],
226 BUFFER_SIZE, 0, copy->cancellable,
230 if (!copy->is_writing &&
231 copy->is_full[copy->curr_write])
233 if (copy->count[copy->curr_write] == 0)
235 /* The last read on the buffer read 0 bytes, this
236 * means that we got an EOF, so we can close
237 * the output channel. */
238 g_output_stream_close_async (copy->out, 0,
245 /* We are not writing and the current buffer contains
246 * data, so start an async write. */
247 copy->is_writing = TRUE;
248 g_output_stream_write_async (copy->out,
249 copy->buff[copy->curr_write],
250 copy->count[copy->curr_write],
251 0, copy->cancellable,
252 write_done_cb, copy);
258 copy_stream (GInputStream *in,
260 GCancellable *cancellable)
265 g_return_if_fail (in != NULL);
266 g_return_if_fail (out != NULL);
268 copy = g_new0 (CopyData, 1);
269 copy->in = g_object_ref (in);
270 copy->out = g_object_ref (out);
273 if (cancellable != NULL)
274 copy->cancellable = g_object_ref (cancellable);
276 copy->cancellable = g_cancellable_new ();
278 for (i = 0; i < N_BUFFERS; i++)
279 copy->buff[i] = g_malloc (BUFFER_SIZE);
281 schedule_next (copy);
284 /* EmpathyTpFile object */
286 struct _EmpathyTpFilePriv {
287 EmpathyTpContactFactory *factory;
292 EmpathyContact *contact;
293 GInputStream *in_stream;
294 GOutputStream *out_stream;
296 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
297 TpFileTransferState state;
301 TpFileHashType content_hash_type;
304 guint64 transferred_bytes;
307 TpFileTransferStateChangeReason state_change_reason;
308 time_t last_update_time;
309 guint64 last_update_transferred_bytes;
313 GValue *socket_address;
314 GCancellable *cancellable;
326 PROP_TRANSFERRED_BYTES,
327 PROP_CONTENT_HASH_TYPE,
336 static guint signals[LAST_SIGNAL];
338 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
341 empathy_tp_file_init (EmpathyTpFile *tp_file)
343 EmpathyTpFilePriv *priv;
345 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
346 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
348 tp_file->priv = priv;
352 tp_file_invalidated_cb (TpProxy *proxy,
356 EmpathyTpFile *tp_file)
358 DEBUG ("Channel invalidated: %s", message);
360 if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
361 tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
363 /* The channel is not in a finished state, an error occured */
364 tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
365 tp_file->priv->state_change_reason =
366 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
367 g_object_notify (G_OBJECT (tp_file), "state");
372 tp_file_finalize (GObject *object)
374 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
376 if (tp_file->priv->channel)
378 g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
379 tp_file_invalidated_cb, object);
380 g_object_unref (tp_file->priv->channel);
381 tp_file->priv->channel = NULL;
384 if (tp_file->priv->factory)
386 g_object_unref (tp_file->priv->factory);
388 if (tp_file->priv->mc)
390 g_object_unref (tp_file->priv->mc);
393 g_free (tp_file->priv->filename);
394 if (tp_file->priv->socket_address != NULL)
395 tp_g_value_slice_free (tp_file->priv->socket_address);
396 g_free (tp_file->priv->description);
397 g_free (tp_file->priv->content_hash);
398 g_free (tp_file->priv->content_type);
400 if (tp_file->priv->in_stream)
401 g_object_unref (tp_file->priv->in_stream);
403 if (tp_file->priv->out_stream)
404 g_object_unref (tp_file->priv->out_stream);
406 if (tp_file->priv->contact)
407 g_object_unref (tp_file->priv->contact);
409 if (tp_file->priv->cancellable)
410 g_object_unref (tp_file->priv->cancellable);
412 if (tp_file->priv->stalled_id != 0)
413 g_source_remove (tp_file->priv->stalled_id);
415 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
419 tp_file_stalled_cb (EmpathyTpFile *tp_file)
421 /* We didn't get transferred bytes update for a while, the transfer is
424 tp_file->priv->speed = 0;
425 tp_file->priv->remaining_time = -1;
426 g_signal_emit (tp_file, signals[REFRESH], 0);
432 tp_file_start_transfer (EmpathyTpFile *tp_file)
435 struct sockaddr_un addr;
438 fd = socket (PF_UNIX, SOCK_STREAM, 0);
441 DEBUG ("Failed to create socket, closing channel");
442 empathy_tp_file_cancel (tp_file);
446 array = g_value_get_boxed (tp_file->priv->socket_address);
448 memset (&addr, 0, sizeof (addr));
449 addr.sun_family = AF_UNIX;
450 strncpy (addr.sun_path, array->data, array->len);
452 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
454 DEBUG ("Failed to connect socket, closing channel");
455 empathy_tp_file_cancel (tp_file);
460 DEBUG ("Start the transfer");
462 tp_file->priv->last_update_time = empathy_time_get_current ();
463 tp_file->priv->last_update_transferred_bytes = tp_file->priv->transferred_bytes;
464 tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT,
465 (GSourceFunc) tp_file_stalled_cb, tp_file);
467 tp_file->priv->cancellable = g_cancellable_new ();
468 if (tp_file->priv->incoming)
470 GInputStream *socket_stream;
472 socket_stream = g_unix_input_stream_new (fd, TRUE);
473 copy_stream (socket_stream, tp_file->priv->out_stream,
474 tp_file->priv->cancellable);
475 g_object_unref (socket_stream);
479 GOutputStream *socket_stream;
481 socket_stream = g_unix_output_stream_new (fd, TRUE);
482 copy_stream (tp_file->priv->in_stream, socket_stream,
483 tp_file->priv->cancellable);
484 g_object_unref (socket_stream);
489 tp_file_state_changed_cb (TpChannel *channel,
493 GObject *weak_object)
495 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
497 if (state == tp_file->priv->state)
500 DEBUG ("File transfer state changed:\n"
501 "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
502 "\tincoming = %s, in_stream = %s, out_stream = %s",
503 tp_file->priv->filename, tp_file->priv->state, state, reason,
504 tp_file->priv->incoming ? "yes" : "no",
505 tp_file->priv->in_stream ? "present" : "not present",
506 tp_file->priv->out_stream ? "present" : "not present");
508 /* If the channel is open AND we have the socket path, we can start the
509 * transfer. The socket path could be NULL if we are not doing the actual
510 * data transfer but are just an observer for the channel. */
511 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
512 tp_file->priv->socket_address != NULL)
513 tp_file_start_transfer (tp_file);
515 tp_file->priv->state = state;
516 tp_file->priv->state_change_reason = reason;
518 g_object_notify (G_OBJECT (tp_file), "state");
522 tp_file_transferred_bytes_changed_cb (TpChannel *channel,
525 GObject *weak_object)
527 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
528 time_t curr_time, elapsed_time;
529 guint64 transferred_bytes;
531 /* If we didn't progress since last update, return */
532 if (tp_file->priv->transferred_bytes == count)
535 /* Update the transferred bytes count */
536 tp_file->priv->transferred_bytes = count;
537 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
539 /* We got a progress, reset the stalled timeout */
540 if (tp_file->priv->stalled_id != 0)
541 g_source_remove (tp_file->priv->stalled_id);
542 tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT,
543 (GSourceFunc) tp_file_stalled_cb, tp_file);
545 /* Calculate the transfer speed and remaining time estimation. We recalculate
546 * that each second to get more dynamic values that react faster to network
547 * changes. This is better than calculating the average from the begining of
548 * the transfer, I think. */
549 curr_time = empathy_time_get_current ();
550 elapsed_time = curr_time - tp_file->priv->last_update_time;
551 if (elapsed_time >= 1)
553 transferred_bytes = count - tp_file->priv->last_update_transferred_bytes;
554 tp_file->priv->speed = (gdouble) transferred_bytes / (gdouble) elapsed_time;
555 tp_file->priv->remaining_time = (tp_file->priv->size - count) /
556 tp_file->priv->speed;
557 tp_file->priv->last_update_transferred_bytes = count;
558 tp_file->priv->last_update_time = curr_time;
560 g_signal_emit (tp_file, signals[REFRESH], 0);
565 tp_file_check_if_ready (EmpathyTpFile *tp_file)
567 if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
568 tp_file->priv->state == TP_FILE_TRANSFER_STATE_NONE)
571 tp_file->priv->ready = TRUE;
572 g_object_notify (G_OBJECT (tp_file), "ready");
576 tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
577 EmpathyContact *contact,
580 GObject *weak_object)
582 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
586 DEBUG ("Error: %s", error->message);
587 empathy_tp_file_cancel (tp_file);
591 tp_file->priv->contact = g_object_ref (contact);
592 tp_file_check_if_ready (tp_file);
596 tp_file_get_all_cb (TpProxy *proxy,
597 GHashTable *properties,
602 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
606 DEBUG ("Error: %s", error->message);
607 tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
612 tp_file->priv->size = g_value_get_uint64 (
613 g_hash_table_lookup (properties, "Size"));
614 g_object_notify (file_obj, "size");
616 tp_file->priv->state = g_value_get_uint (
617 g_hash_table_lookup (properties, "State"));
618 g_object_notify (file_obj, "state");
620 tp_file->priv->transferred_bytes = g_value_get_uint64 (
621 g_hash_table_lookup (properties, "TransferredBytes"));
622 g_object_notify (file_obj, "transferred-bytes");
624 tp_file->priv->filename = g_value_dup_string (
625 g_hash_table_lookup (properties, "Filename"));
626 g_object_notify (file_obj, "filename");
628 tp_file->priv->content_hash = g_value_dup_string (
629 g_hash_table_lookup (properties, "ContentHash"));
630 g_object_notify (file_obj, "content-hash");
632 tp_file->priv->content_hash_type = g_value_get_uint (
633 g_hash_table_lookup (properties, "ContentHashType"));
634 g_object_notify (file_obj, "content-hash-type");
636 tp_file->priv->content_type = g_value_dup_string (
637 g_hash_table_lookup (properties, "ContentType"));
638 g_object_notify (file_obj, "content-type");
640 tp_file->priv->description = g_value_dup_string (
641 g_hash_table_lookup (properties, "Description"));
643 tp_file_check_if_ready (tp_file);
647 tp_file_get_requested_cb (TpProxy *proxy,
648 const GValue *requested,
651 GObject *weak_object)
653 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
657 DEBUG ("Error: %s", error->message);
658 tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
663 tp_file->priv->incoming = !g_value_get_boolean (requested);
664 g_object_notify (G_OBJECT (tp_file), "incoming");
666 tp_file_check_if_ready (tp_file);
670 tp_file_constructor (GType type,
672 GObjectConstructParam *props)
675 EmpathyTpFile *tp_file;
677 TpConnection *connection;
679 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
682 tp_file = EMPATHY_TP_FILE (file_obj);
684 connection = tp_channel_borrow_connection (tp_file->priv->channel);
685 tp_file->priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
686 tp_file->priv->mc = empathy_mission_control_dup_singleton ();
687 tp_file->priv->state_change_reason =
688 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
690 g_signal_connect (tp_file->priv->channel, "invalidated",
691 G_CALLBACK (tp_file_invalidated_cb), tp_file);
693 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
694 tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL,
695 G_OBJECT (tp_file), NULL);
697 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
698 tp_file->priv->channel, tp_file_transferred_bytes_changed_cb,
699 NULL, NULL, G_OBJECT (tp_file), NULL);
701 tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
702 TP_IFACE_CHANNEL, "Requested",
703 tp_file_get_requested_cb, NULL, NULL, file_obj);
705 tp_cli_dbus_properties_call_get_all (tp_file->priv->channel, -1,
706 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
707 tp_file_get_all_cb, NULL, NULL, file_obj);
709 handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
710 empathy_tp_contact_factory_get_from_handle (tp_file->priv->factory,
711 handle, tp_file_got_contact_cb, NULL, NULL, file_obj);
717 tp_file_get_property (GObject *object,
722 EmpathyTpFile *tp_file;
724 tp_file = EMPATHY_TP_FILE (object);
729 g_value_set_object (value, tp_file->priv->channel);
732 g_value_set_boolean (value, tp_file->priv->incoming);
735 g_value_set_uint (value, tp_file->priv->state);
737 case PROP_CONTENT_TYPE:
738 g_value_set_string (value, tp_file->priv->content_type);
741 g_value_set_string (value, tp_file->priv->filename);
744 g_value_set_uint64 (value, tp_file->priv->size);
746 case PROP_CONTENT_HASH_TYPE:
747 g_value_set_uint (value, tp_file->priv->content_hash_type);
749 case PROP_CONTENT_HASH:
750 g_value_set_string (value, tp_file->priv->content_hash);
752 case PROP_TRANSFERRED_BYTES:
753 g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
756 g_value_set_boolean (value, tp_file->priv->ready);
759 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
765 tp_file_channel_set_dbus_property (gpointer proxy,
766 const gchar *property,
769 DEBUG ("Setting %s property", property);
770 tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
771 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
772 NULL, NULL, NULL, NULL);
776 tp_file_set_property (GObject *object,
781 EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
785 tp_file->priv->channel = g_object_ref (g_value_get_object (value));
788 tp_file->priv->state = g_value_get_uint (value);
791 tp_file->priv->incoming = g_value_get_boolean (value);
794 g_free (tp_file->priv->filename);
795 tp_file->priv->filename = g_value_dup_string (value);
796 tp_file_channel_set_dbus_property (tp_file->priv->channel,
800 tp_file->priv->size = g_value_get_uint64 (value);
801 tp_file_channel_set_dbus_property (tp_file->priv->channel,
804 case PROP_CONTENT_TYPE:
805 tp_file_channel_set_dbus_property (tp_file->priv->channel,
806 "ContentType", value);
807 g_free (tp_file->priv->content_type);
808 tp_file->priv->content_type = g_value_dup_string (value);
810 case PROP_CONTENT_HASH:
811 tp_file_channel_set_dbus_property (tp_file->priv->channel,
812 "ContentHash", value);
813 g_free (tp_file->priv->content_hash);
814 tp_file->priv->content_hash = g_value_dup_string (value);
817 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
822 static GHashTable *ft_table = NULL;
825 tp_file_weak_notify_cb (gpointer channel,
828 g_hash_table_remove (ft_table, channel);
832 * empathy_tp_file_new:
833 * @channel: a #TpChannel
835 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
836 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
837 * should be unrefed with g_object_unref() when finished with.
839 * Return value: a new #EmpathyTpFile
842 empathy_tp_file_new (TpChannel *channel)
844 EmpathyTpFile *tp_file;
846 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
848 if (ft_table != NULL)
850 tp_file = g_hash_table_lookup (ft_table, channel);
851 if (tp_file != NULL) {
852 return g_object_ref (tp_file);
856 ft_table = g_hash_table_new_full (empathy_proxy_hash,
857 empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL);
859 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
863 g_hash_table_insert (ft_table, g_object_ref (channel), tp_file);
864 g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel);
870 * empathy_tp_file_get_channel
871 * @tp_file: an #EmpathyTpFile
873 * Returns the #TpChannel associated with @tp_file.
875 * Returns: the #TpChannel associated with @tp_file
878 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
880 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
882 return tp_file->priv->channel;
886 tp_file_method_cb (TpChannel *channel,
887 const GValue *address,
890 GObject *weak_object)
892 EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
897 DEBUG ("Error: %s", error->message);
898 empathy_tp_file_cancel (tp_file);
902 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
904 tp_file->priv->socket_address = tp_g_value_slice_dup (address);
906 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
908 /* Old bugged version of telepathy-salut used to store the address
909 * as a 's' instead of an 'ay' */
912 path = g_value_get_string (address);
913 array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path));
914 g_array_insert_vals (array, 0, path, strlen (path));
916 tp_file->priv->socket_address = tp_g_value_slice_new (
917 DBUS_TYPE_G_UCHAR_ARRAY);
918 g_value_set_boxed (tp_file->priv->socket_address, array);
920 g_array_free (array, TRUE);
924 DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address));
925 empathy_tp_file_cancel (tp_file);
929 array = g_value_get_boxed (tp_file->priv->socket_address);
930 DEBUG ("Got unix socket path: %s", array->data);
932 if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN)
933 tp_file_start_transfer (tp_file);
937 * empathy_tp_file_accept:
938 * @tp_file: an #EmpathyTpFile
939 * @offset: position where to start the transfer
940 * @gfile: a #GFile where to write transfered data
941 * @error: a #GError set if there is an error when opening @gfile
943 * Accepts a file transfer that's in the "local pending" state (i.e.
944 * %TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
947 empathy_tp_file_accept (EmpathyTpFile *tp_file,
952 GValue nothing = { 0 };
954 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
955 g_return_if_fail (G_IS_FILE (gfile));
957 tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
958 FALSE, 0, NULL, error));
962 g_free (tp_file->priv->filename);
963 tp_file->priv->filename = g_file_get_basename (gfile);
964 g_object_notify (G_OBJECT (tp_file), "filename");
966 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
968 g_value_init (¬hing, G_TYPE_STRING);
969 g_value_set_static_string (¬hing, "");
971 tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
972 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
973 ¬hing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
977 * empathy_tp_file_offer:
978 * @tp_file: an #EmpathyTpFile
979 * @gfile: a #GFile where to read the data to transfer
980 * @error: a #GError set if there is an error when opening @gfile
982 * Offers a file transfer that's in the "not offered" state (i.e.
983 * %TP_FILE_TRANSFER_STATE_NOT_OFFERED).
986 empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
988 GValue nothing = { 0 };
990 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
992 tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
996 g_value_init (¬hing, G_TYPE_STRING);
997 g_value_set_static_string (¬hing, "");
999 tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
1000 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
1001 ¬hing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
1005 * empathy_tp_file_get_contact:
1006 * @tp_file: an #EmpathyTpFile
1008 * Returns the #EmpathyContact that @tp_file is open with.
1010 * Return value: the #EmpathyContact that @tp_file is open with.
1013 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
1015 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
1016 return tp_file->priv->contact;
1020 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
1022 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
1023 return tp_file->priv->filename;
1027 * empathy_tp_file_is_incoming:
1028 * @tp_file: an #EmpathyTpFile
1030 * Returns whether @tp_file is incoming.
1032 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
1035 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
1037 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
1038 return tp_file->priv->incoming;
1042 * empathy_tp_file_get_state:
1043 * @tp_file: an #EmpathyTpFile
1044 * @reason: return location for state change reason, or %NULL
1046 * Gets the current state of @tp_file. If @reason is not %NULL, then
1047 * it is set to the reason of the last state change.
1049 * Return value: a #TpFileTransferState
1052 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
1053 TpFileTransferStateChangeReason *reason)
1055 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
1056 TP_FILE_TRANSFER_STATE_NONE);
1059 *reason = tp_file->priv->state_change_reason;
1061 return tp_file->priv->state;
1065 * empathy_tp_file_get_size:
1066 * @tp_file: an #EmpathyTpFile
1068 * Gets the size of the file being transferred over @tp_file, in bytes.
1070 * Return value: the size of the file being transferred, in bytes
1073 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
1075 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
1076 EMPATHY_TP_FILE_UNKNOWN_SIZE);
1077 return tp_file->priv->size;
1081 * empathy_tp_file_get_transferred_bytes:
1082 * @tp_file: an #EmpathyTpFile
1084 * Gets the number of transferred bytes of @tp_file so far, in bytes.
1086 * Return value: number of transferred bytes of @tp_file, in bytes
1089 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
1091 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
1092 return tp_file->priv->transferred_bytes;
1096 * empathy_tp_file_get_remaining_time:
1097 * @tp_file: a #EmpathyTpFile
1099 * Gets the estimated time remaining of @tp_file, in seconds.
1101 * Return value: the estimated time remaining of @tp_file, in seconds
1104 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
1106 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
1108 if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
1111 if (tp_file->priv->transferred_bytes == tp_file->priv->size)
1114 return tp_file->priv->remaining_time;
1118 * empathy_tp_file_get_speed:
1119 * @tp_file: an #EmpathyTpFile
1121 * Gets the current speed of the transfer @tp_file, in bytes per
1124 * Return value: the current speed of the transfer @tp_file, in
1128 empathy_tp_file_get_speed (EmpathyTpFile *tp_file)
1130 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
1132 if (tp_file->priv->transferred_bytes == tp_file->priv->size)
1135 return tp_file->priv->speed;
1139 empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
1141 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
1142 return tp_file->priv->content_type;
1146 * empathy_tp_file_cancel:
1147 * @tp_file: an #EmpathyTpFile
1149 * Cancels the file transfer, @tp_file.
1152 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1154 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1156 DEBUG ("Closing channel..");
1157 tp_cli_channel_call_close (tp_file->priv->channel, -1,
1158 NULL, NULL, NULL, NULL);
1160 if (tp_file->priv->cancellable != NULL)
1161 g_cancellable_cancel (tp_file->priv->cancellable);
1165 * empathy_tp_file_is_ready:
1166 * @tp_file: an #EmpathyTpFile
1168 * Returns whether the file channel @tp_file is ready for use.
1170 * @tp_file is classed as ready if its state is no longer
1171 * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
1172 * contact have been fully received.
1174 * Return value: %TRUE if @tp_file is ready for use
1177 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
1179 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
1181 return tp_file->priv->ready;
1185 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
1187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1189 object_class->finalize = tp_file_finalize;
1190 object_class->constructor = tp_file_constructor;
1191 object_class->get_property = tp_file_get_property;
1192 object_class->set_property = tp_file_set_property;
1194 /* Construct-only properties */
1197 * EmpathyTpFile:channel:
1199 * The #TpChannel associated with the #EmpathyTpFile.
1201 g_object_class_install_property (object_class,
1203 g_param_spec_object ("channel",
1204 "telepathy channel",
1205 "The file transfer channel",
1208 G_PARAM_CONSTRUCT_ONLY));
1211 * EmpathyTpFile:state:
1213 * The #TpFileTransferState of the #EmpathyTpFile.
1215 g_object_class_install_property (object_class,
1217 g_param_spec_uint ("state",
1218 "state of the transfer",
1219 "The file transfer state",
1224 G_PARAM_CONSTRUCT));
1227 * EmpathyTpFile:incoming:
1229 * Whether the #EmpathyTpFile is incoming.
1231 g_object_class_install_property (object_class,
1233 g_param_spec_boolean ("incoming",
1235 "Whether the transfer is incoming",
1238 G_PARAM_CONSTRUCT));
1241 * EmpathyTpFile:ready:
1243 * Whether the #EmpathyTpFile is ready to use.
1245 g_object_class_install_property (object_class,
1247 g_param_spec_boolean ("ready",
1249 "Whether the object is ready",
1254 * EmpathyTpFile:filename:
1256 * The name of the file being transferred.
1258 g_object_class_install_property (object_class,
1260 g_param_spec_string ("filename",
1261 "name of the transfer",
1262 "The file transfer filename",
1264 G_PARAM_READWRITE));
1267 * EmpathyTpFile:size:
1269 * The size of the file being transferred.
1271 g_object_class_install_property (object_class,
1273 g_param_spec_uint64 ("size",
1275 "The file transfer size",
1279 G_PARAM_READWRITE));
1282 * EmpathyTpFile:content-type:
1284 * The content-type of the file being transferred.
1286 g_object_class_install_property (object_class,
1288 g_param_spec_string ("content-type",
1289 "file transfer content-type",
1290 "The file transfer content-type",
1292 G_PARAM_READWRITE));
1295 * EmpathyTpFile:content-hash-type:
1297 * The type of hash type stored in #EmpathyTpFile:content-hash,
1298 * from #TpFileHashType.
1300 g_object_class_install_property (object_class,
1301 PROP_CONTENT_HASH_TYPE,
1302 g_param_spec_uint ("content-hash-type",
1303 "file transfer hash type",
1304 "The type of the file transfer hash",
1308 G_PARAM_READWRITE));
1311 * EmpathyTpFile:content-hash:
1313 * A hash of the contents of the file being transferred.
1315 g_object_class_install_property (object_class,
1317 g_param_spec_string ("content-hash",
1318 "file transfer hash",
1319 "The hash of the transfer's contents",
1321 G_PARAM_READWRITE));
1324 * EmpathyTpFile:transferred-bytes:
1326 * The number of bytes transferred in the #EmpathyTpFile.
1328 g_object_class_install_property (object_class,
1329 PROP_TRANSFERRED_BYTES,
1330 g_param_spec_uint64 ("transferred-bytes",
1331 "bytes transferred",
1332 "The number of bytes transferred",
1336 G_PARAM_READWRITE));
1339 * EmpathyTpFile::refresh:
1340 * @tp_file: the #EmpathyTpFile
1342 * The progress of @tp_file has changed. This can either be an update
1343 * in the number of bytes transferred, or it can be to inform of the
1344 * transfer stalling.
1346 * This signal is designed for clients to provide more user feedback
1347 * when something to do with @tp_file changes. To avoid emitting this
1348 * signal too much, it is guaranteed that it will only ever be fired
1349 * at least every two seconds.
1351 signals[REFRESH] = g_signal_new ("refresh", G_TYPE_FROM_CLASS (klass),
1352 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1353 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1355 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));