/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
- * Copyright (C) 2007 Collabora Ltd.
- * @author: Xavier Claessens <xclaesse@gmail.com>
+ * Copyright (C) 2007-2008 Collabora Ltd.
* Copyright (C) 2007 Marco Barisione <marco@barisione.org>
*
* This library is free software; you can redistribute it and/or
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Marco Barisione <marco@barisione.org>
+ * Jonny Lamb <jonny.lamb@collabora.co.uk>
*/
#include <config.h>
#include <sys/socket.h>
#include <sys/un.h>
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
-#include <libtelepathy/tp-conn.h>
-#include <libtelepathy/tp-helpers.h>
-#include <libtelepathy/tp-props-iface.h>
-
#include <telepathy-glib/proxy-subclass.h>
+#include <telepathy-glib/util.h>
#include "empathy-tp-file.h"
-#include "empathy-contact-factory.h"
+#include "empathy-tp-contact-factory.h"
#include "empathy-marshal.h"
#include "empathy-time.h"
#include "empathy-utils.h"
+#include "extensions/extensions.h"
+
#define DEBUG_FLAG EMPATHY_DEBUG_FT
#include "empathy-debug.h"
/**
* SECTION:empathy-tp-file
* @short_description: File channel
- * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
+ * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_send_file()
* @include: libempthy/empathy-tp-file.h
*
* The #EmpathyTpFile object represents a Telepathy file channel.
* the transferred file is unknown.
*/
-static void empathy_tp_file_class_init (EmpathyTpFileClass *klass);
-static void empathy_tp_file_init (EmpathyTpFile *tp_file);
-static void tp_file_finalize (GObject *object);
-static GObject *tp_file_constructor (GType type, guint n_props,
- GObjectConstructParam *props);
-static void tp_file_get_property (GObject *object, guint param_id,
- GValue *value, GParamSpec *pspec);
-static void tp_file_set_property (GObject *object, guint param_id,
- const GValue *value, GParamSpec *pspec);
-static void tp_file_destroy_cb (TpChannel *file_chan, EmpathyTpFile *tp_file);
-static void tp_file_closed_cb (TpChannel *file_chan, EmpathyTpFile *tp_file,
- GObject *weak_object);
-static void tp_file_state_changed_cb (DBusGProxy *file_iface, guint state,
- guint reason, EmpathyTpFile *tp_file);
-static void tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
- guint64 count, EmpathyTpFile *tp_file, GObject *weak_object);
-static void copy_stream (GInputStream *in, GOutputStream *out,
- GCancellable *cancellable);
-
-/* EmpathyTpFile object */
-
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
- EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv))
-
-typedef struct _EmpathyTpFilePriv EmpathyTpFilePriv;
-
-struct _EmpathyTpFilePriv {
- EmpathyContactFactory *factory;
- McAccount *account;
- gchar *id;
- MissionControl *mc;
- TpChannel *channel;
+/* Functions to copy the content of a GInputStream to a GOutputStream */
- EmpathyTpFile *cached_empathy_file;
- EmpathyContact *contact;
- GInputStream *in_stream;
- GOutputStream *out_stream;
- gchar *filename;
- EmpFileTransferDirection direction;
- EmpFileTransferState state;
- EmpFileTransferStateChangeReason state_change_reason;
- guint64 size;
- guint64 transferred_bytes;
- gint64 start_time;
- gchar *unix_socket_path;
- gchar *content_md5;
- gchar *content_type;
- gchar *description;
- GCancellable *cancellable;
-};
+#define N_BUFFERS 2
+#define BUFFER_SIZE 4096
-enum {
- PROP_0,
- PROP_ACCOUNT,
- PROP_CHANNEL,
- PROP_STATE,
- PROP_DIRECTION,
- PROP_FILENAME,
- PROP_SIZE,
- PROP_CONTENT_TYPE,
- PROP_TRANSFERRED_BYTES,
- PROP_CONTENT_MD5,
- PROP_IN_STREAM,
-};
+typedef struct {
+ GInputStream *in;
+ GOutputStream *out;
+ GCancellable *cancellable;
+ char *buff[N_BUFFERS]; /* the temporary buffers */
+ gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
+ gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
+ gint curr_read; /* index of the buffer used for reading */
+ gint curr_write; /* index of the buffer used for writing */
+ gboolean is_reading; /* we are reading */
+ gboolean is_writing; /* we are writing */
+ guint n_closed; /* number of streams that have been closed */
+ gint ref_count;
+} CopyData;
-G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
+static void schedule_next (CopyData *copy);
static void
-empathy_tp_file_class_init (EmpathyTpFileClass *klass)
+copy_data_unref (CopyData *copy)
{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
- object_class->finalize = tp_file_finalize;
- object_class->constructor = tp_file_constructor;
- object_class->get_property = tp_file_get_property;
- object_class->set_property = tp_file_set_property;
-
- /* Construct-only properties */
- g_object_class_install_property (object_class,
- PROP_ACCOUNT,
- g_param_spec_object ("account",
- "channel Account",
- "The account associated with the channel",
- MC_TYPE_ACCOUNT,
- G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY));
+ if (--copy->ref_count == 0)
+ {
+ gint i;
- g_object_class_install_property (object_class,
- PROP_CHANNEL,
- g_param_spec_object ("channel",
- "telepathy channel",
- "The file transfer channel",
- TP_TYPE_CHANNEL,
- G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY));
+ /* Free the data only if both the input and output streams have
+ * been closed. */
+ copy->n_closed++;
+ if (copy->n_closed < 2)
+ return;
- g_object_class_install_property (object_class,
- PROP_STATE,
- g_param_spec_uint ("state",
- "state of the transfer",
- "The file transfer state",
- 0,
- G_MAXUINT,
- G_MAXUINT,
- G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT));
+ if (copy->in != NULL)
+ g_object_unref (copy->in);
- g_object_class_install_property (object_class,
- PROP_DIRECTION,
- g_param_spec_uint ("direction",
- "direction of the transfer",
- "The file transfer direction",
- 0,
- G_MAXUINT,
- G_MAXUINT,
- G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT));
+ if (copy->out != NULL)
+ g_object_unref (copy->out);
- g_object_class_install_property (object_class,
- PROP_FILENAME,
- g_param_spec_string ("filename",
- "name of the transfer",
- "The file transfer filename",
- "",
- G_PARAM_READWRITE));
+ for (i = 0; i < N_BUFFERS; i++)
+ g_free (copy->buff[i]);
- g_object_class_install_property (object_class,
- PROP_SIZE,
- g_param_spec_uint64 ("size",
- "size of the file",
- "The file transfer size",
- 0,
- G_MAXUINT64,
- G_MAXUINT64,
- G_PARAM_READWRITE));
+ g_object_unref (copy->cancellable);
+ g_free (copy);
+ }
+}
- g_object_class_install_property (object_class,
- PROP_CONTENT_TYPE,
- g_param_spec_string ("content-type",
- "file transfer content-type",
- "The file transfer content-type",
- "",
- G_PARAM_READWRITE));
+static void
+io_error (CopyData *copy,
+ GError *error)
+{
+ g_cancellable_cancel (copy->cancellable);
- g_object_class_install_property (object_class,
- PROP_CONTENT_MD5,
- g_param_spec_string ("content-md5",
- "file transfer md5sum",
- "The md5 sum of the transfer's contents",
- "",
- G_PARAM_READWRITE));
+ if (error == NULL)
+ g_warning ("I/O error");
+ else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
+ ; /* Ignore cancellations */
+ else
+ g_warning ("I/O error: %d: %s\n", error->code, error->message);
- g_object_class_install_property (object_class,
- PROP_TRANSFERRED_BYTES,
- g_param_spec_uint64 ("transferred-bytes",
- "bytes transferred",
- "The number of bytes transferred",
- 0,
- G_MAXUINT64,
- 0,
- G_PARAM_READWRITE));
+ if (copy->in != NULL)
+ g_input_stream_close (copy->in, NULL, NULL);
- g_object_class_install_property (object_class,
- PROP_IN_STREAM,
- g_param_spec_object ("in-stream",
- "transfer input stream",
- "The input stream for file transfer",
- G_TYPE_INPUT_STREAM,
- G_PARAM_READWRITE));
+ if (copy->out != NULL)
+ g_output_stream_close (copy->out, NULL, NULL);
- g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
+ copy_data_unref (copy);
}
static void
-empathy_tp_file_init (EmpathyTpFile *tp_file)
+close_done (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
{
+ CopyData *copy = user_data;
+
+ g_object_unref (source_object);
+ copy_data_unref (copy);
}
static void
-tp_file_finalize (GObject *object)
+write_done_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
{
- EmpathyTpFilePriv *priv;
- EmpathyTpFile *tp_file;
-
- tp_file = EMPATHY_TP_FILE (object);
- priv = GET_PRIV (tp_file);
+ CopyData *copy = user_data;
+ gssize count_write;
+ GError *error = NULL;
- if (priv->channel)
- {
- DEBUG ("Closing channel..");
- g_signal_handlers_disconnect_by_func (priv->channel,
- tp_file_destroy_cb, object);
- tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
- if (G_IS_OBJECT (priv->channel))
- g_object_unref (priv->channel);
- }
+ count_write = g_output_stream_write_finish (copy->out, res, &error);
- if (priv->factory)
- {
- g_object_unref (priv->factory);
- }
- if (priv->account)
- {
- g_object_unref (priv->account);
- }
- if (priv->mc)
+ if (count_write <= 0)
{
- g_object_unref (priv->mc);
+ io_error (copy, error);
+ g_error_free (error);
+ return;
}
- g_free (priv->id);
- g_free (priv->filename);
- g_free (priv->unix_socket_path);
- g_free (priv->description);
- g_free (priv->content_md5);
- g_free (priv->content_type);
-
- if (priv->in_stream)
- g_object_unref (priv->in_stream);
-
- if (priv->out_stream)
- g_object_unref (priv->out_stream);
-
- if (priv->contact)
- g_object_unref (priv->contact);
-
- if (priv->cancellable)
- g_object_unref (priv->cancellable);
+ copy->is_full[copy->curr_write] = FALSE;
+ copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
+ copy->is_writing = FALSE;
- G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
+ schedule_next (copy);
}
-static GObject *
-tp_file_constructor (GType type,
- guint n_props,
- GObjectConstructParam *props)
+static void
+read_done_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
{
- GObject *tp_file;
- EmpathyTpFilePriv *priv;
+ CopyData *copy = user_data;
+ gssize count_read;
GError *error = NULL;
- GHashTable *properties;
- TpHandle handle;
-
- tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
- n_props, props);
- priv = GET_PRIV (tp_file);
-
- priv->factory = empathy_contact_factory_new ();
- priv->mc = empathy_mission_control_new ();
-
- tp_cli_channel_connect_to_closed (priv->channel,
- (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
- tp_file,
- NULL, NULL, NULL);
-
- emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
- TP_PROXY (priv->channel),
- (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
- tp_file_state_changed_cb,
- tp_file,
- NULL, NULL, NULL);
-
- emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
- TP_PROXY (priv->channel),
- (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
- tp_file_transferred_bytes_changed_cb,
- tp_file,
- NULL, NULL, NULL);
-
-
- handle = tp_channel_get_handle (priv->channel, NULL);
- priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
- priv->account,
- (guint) handle);
+ count_read = g_input_stream_read_finish (copy->in, res, &error);
- if (!tp_cli_dbus_properties_run_get_all (priv->channel,
- -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
+ if (count_read == 0)
{
- DEBUG ("Failed to get properties: %s",
- error ? error->message : "No error given");
- g_clear_error (&error);
- return NULL;
+ g_input_stream_close_async (copy->in, 0, copy->cancellable,
+ close_done, copy);
+ copy->in = NULL;
+ }
+ else if (count_read < 0)
+ {
+ io_error (copy, error);
+ g_error_free (error);
+ return;
}
- priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
-
- priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
-
- /* Invalid reason, so empathy_file_get_state_change_reason() can give
- * a warning if called for a not closed file transfer. */
- priv->state_change_reason = -1;
-
- priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
- properties, "TransferredBytes"));
-
- priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
- "Filename"));
-
- priv->content_md5 = g_value_dup_string (g_hash_table_lookup (properties,
- "ContentMD5"));
-
- priv->description = g_value_dup_string (g_hash_table_lookup (properties,
- "Description"));
-
- priv->unix_socket_path = g_value_dup_string (g_hash_table_lookup (properties,
- "SocketPath"));
-
- priv->direction = g_value_get_uint (g_hash_table_lookup (properties,
- "Direction"));
-
- g_hash_table_destroy (properties);
+ copy->count[copy->curr_read] = count_read;
+ copy->is_full[copy->curr_read] = TRUE;
+ copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
+ copy->is_reading = FALSE;
- return tp_file;
+ schedule_next (copy);
}
static void
-tp_file_get_property (GObject *object,
- guint param_id,
- GValue *value,
- GParamSpec *pspec)
+schedule_next (CopyData *copy)
{
- EmpathyTpFilePriv *priv;
- EmpathyTpFile *tp_file;
-
- priv = GET_PRIV (object);
- tp_file = EMPATHY_TP_FILE (object);
-
- switch (param_id)
+ if (copy->in != NULL &&
+ !copy->is_reading &&
+ !copy->is_full[copy->curr_read])
{
- case PROP_ACCOUNT:
- g_value_set_object (value, priv->account);
- break;
- case PROP_CHANNEL:
- g_value_set_object (value, priv->channel);
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
-}
+ /* We are not reading and the current buffer is empty, so
+ * start an async read. */
+ copy->is_reading = TRUE;
+ g_input_stream_read_async (copy->in,
+ copy->buff[copy->curr_read],
+ BUFFER_SIZE, 0, copy->cancellable,
+ read_done_cb, copy);
+ }
-static void
-tp_file_channel_set_dbus_property (gpointer proxy,
- const gchar *property,
- const GValue *value)
-{
- DEBUG ("Setting %s property", property);
- tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
- -1,
- EMP_IFACE_CHANNEL_TYPE_FILE,
- property,
- value,
- NULL, NULL);
- DEBUG ("done");
+ if (!copy->is_writing &&
+ copy->is_full[copy->curr_write])
+ {
+ if (copy->count[copy->curr_write] == 0)
+ {
+ /* The last read on the buffer read 0 bytes, this
+ * means that we got an EOF, so we can close
+ * the output channel. */
+ g_output_stream_close_async (copy->out, 0,
+ copy->cancellable,
+ close_done, copy);
+ copy->out = NULL;
+ }
+ else
+ {
+ /* We are not writing and the current buffer contains
+ * data, so start an async write. */
+ copy->is_writing = TRUE;
+ g_output_stream_write_async (copy->out,
+ copy->buff[copy->curr_write],
+ copy->count[copy->curr_write],
+ 0, copy->cancellable,
+ write_done_cb, copy);
+ }
+ }
}
-
static void
-tp_file_set_property (GObject *object,
- guint param_id,
- const GValue *value,
- GParamSpec *pspec)
+copy_stream (GInputStream *in,
+ GOutputStream *out,
+ GCancellable *cancellable)
{
- EmpathyTpFilePriv *priv;
+ CopyData *copy;
+ gint i;
- priv = GET_PRIV (object);
+ g_return_if_fail (in != NULL);
+ g_return_if_fail (out != NULL);
- switch (param_id)
- {
- case PROP_ACCOUNT:
- priv->account = g_object_ref (g_value_get_object (value));
- break;
- case PROP_CHANNEL:
- priv->channel = g_object_ref (g_value_get_object (value));
- break;
- case PROP_STATE:
- priv->state = g_value_get_uint (value);
- break;
- case PROP_DIRECTION:
- priv->direction = g_value_get_uint (value);
- break;
- case PROP_FILENAME:
- g_free (priv->filename);
- priv->filename = g_value_dup_string (value);
- tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
- break;
- case PROP_SIZE:
- priv->size = g_value_get_uint64 (value);
- tp_file_channel_set_dbus_property (priv->channel, "Size", value);
- break;
- case PROP_CONTENT_TYPE:
- tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
- g_free (priv->content_type);
- priv->content_type = g_value_dup_string (value);
- break;
- case PROP_CONTENT_MD5:
- tp_file_channel_set_dbus_property (priv->channel, "ContentMD5", value);
- g_free (priv->content_md5);
- priv->content_md5 = g_value_dup_string (value);
- break;
- case PROP_IN_STREAM:
- if (priv->in_stream)
- g_object_unref (priv->in_stream);
- priv->in_stream = g_object_ref (g_value_get_object (value));
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
-}
+ copy = g_new0 (CopyData, 1);
+ copy->in = g_object_ref (in);
+ copy->out = g_object_ref (out);
+ copy->ref_count = 1;
-/**
- * empathy_tp_file_new:
- * @account: the #McAccount for the channel
- * @channel: a Telepathy channel
- *
- * Creates a new #EmpathyTpFile wrapping @channel.
- *
- * Returns: a new #EmpathyTpFile
- */
-EmpathyTpFile *
-empathy_tp_file_new (McAccount *account,
- TpChannel *channel)
-{
- return g_object_new (EMPATHY_TYPE_TP_FILE,
- "account", account,
- "channel", channel,
- NULL);
-}
+ if (cancellable != NULL)
+ copy->cancellable = g_object_ref (cancellable);
+ else
+ copy->cancellable = g_cancellable_new ();
-/**
- * empathy_tp_file_get_id:
- * @tp_file: an #EmpathyTpFile
- *
- * Returns the ID of @tp_file.
- *
- * Returns: the ID
- */
-const gchar *
-empathy_tp_file_get_id (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
+ for (i = 0; i < N_BUFFERS; i++)
+ copy->buff[i] = g_malloc (BUFFER_SIZE);
- g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+ schedule_next (copy);
+}
- priv = GET_PRIV (tp_file);
+/* EmpathyTpFile object */
- return priv->id;
-}
+struct _EmpathyTpFilePriv {
+ EmpathyTpContactFactory *factory;
+ MissionControl *mc;
+ TpChannel *channel;
+ gboolean ready;
-/**
- * empathy_tp_file_get_channel
- * @tp_file: an #EmpathyTpFile
- *
- * Returns the Telepathy file transfer channel
- *
- * Returns: the #TpChannel
- */
-TpChannel *
-empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
+ EmpathyContact *contact;
+ GInputStream *in_stream;
+ GOutputStream *out_stream;
- g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+ /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
+ TpFileTransferState state;
+ gchar *content_type;
+ gchar *filename;
+ guint64 size;
+ TpFileHashType content_hash_type;
+ gchar *content_hash;
+ gchar *description;
+ guint64 transferred_bytes;
- priv = GET_PRIV (tp_file);
+ gboolean incoming;
+ TpFileTransferStateChangeReason state_change_reason;
+ time_t start_time;
+ GValue *socket_address;
+ GCancellable *cancellable;
+};
- return priv->channel;
-}
+enum {
+ PROP_0,
+ PROP_CHANNEL,
+ PROP_STATE,
+ PROP_INCOMING,
+ PROP_READY,
+ PROP_FILENAME,
+ PROP_SIZE,
+ PROP_CONTENT_TYPE,
+ PROP_TRANSFERRED_BYTES,
+ PROP_CONTENT_HASH_TYPE,
+ PROP_CONTENT_HASH,
+};
+
+G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
static void
-tp_file_destroy_cb (TpChannel *file_channel,
- EmpathyTpFile *tp_file)
+empathy_tp_file_init (EmpathyTpFile *tp_file)
{
EmpathyTpFilePriv *priv;
- priv = GET_PRIV (tp_file);
+ priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
+ EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
- DEBUG ("Channel Closed or CM crashed");
-
- g_object_unref (priv->channel);
- priv->channel = NULL;
+ tp_file->priv = priv;
}
static void
-tp_file_closed_cb (TpChannel *file_channel,
- EmpathyTpFile *tp_file,
- GObject *weak_object)
+tp_file_invalidated_cb (TpProxy *proxy,
+ guint domain,
+ gint code,
+ gchar *message,
+ EmpathyTpFile *tp_file)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
+ DEBUG ("Channel invalidated: %s", message);
- /* The channel is closed, do just like if the proxy was destroyed */
- g_signal_handlers_disconnect_by_func (priv->channel,
- tp_file_destroy_cb,
- tp_file);
- tp_file_destroy_cb (file_channel, tp_file);
+ if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
+ tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+ {
+ /* The channel is not in a finished state, an error occured */
+ tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
+ tp_file->priv->state_change_reason =
+ TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
+ g_object_notify (G_OBJECT (tp_file), "state");
+ }
}
-static gint64
-get_time_msec (void)
+static void
+tp_file_finalize (GObject *object)
{
- GTimeVal tv;
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
- g_get_current_time (&tv);
- return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
-}
+ if (tp_file->priv->channel)
+ {
+ g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
+ tp_file_invalidated_cb, object);
+ g_object_unref (tp_file->priv->channel);
+ tp_file->priv->channel = NULL;
+ }
-static gint
-_get_local_socket (EmpathyTpFile *tp_file)
-{
- gint fd;
- size_t path_len;
- struct sockaddr_un addr;
- EmpathyTpFilePriv *priv;
- GValue *socket_path;
+ if (tp_file->priv->factory)
+ {
+ g_object_unref (tp_file->priv->factory);
+ }
+ if (tp_file->priv->mc)
+ {
+ g_object_unref (tp_file->priv->mc);
+ }
- priv = GET_PRIV (tp_file);
+ g_free (tp_file->priv->filename);
+ if (tp_file->priv->socket_address != NULL)
+ tp_g_value_slice_free (tp_file->priv->socket_address);
+ g_free (tp_file->priv->description);
+ g_free (tp_file->priv->content_hash);
+ g_free (tp_file->priv->content_type);
- /* TODO: This could probably be a little nicer. */
- tp_cli_dbus_properties_run_get (priv->channel,
- -1,
- EMP_IFACE_CHANNEL_TYPE_FILE,
- "SocketPath",
- &socket_path,
- NULL,
- NULL);
+ if (tp_file->priv->in_stream)
+ g_object_unref (tp_file->priv->in_stream);
- if (priv->unix_socket_path)
- g_free (priv->unix_socket_path);
+ if (tp_file->priv->out_stream)
+ g_object_unref (tp_file->priv->out_stream);
- priv->unix_socket_path = g_value_dup_string (socket_path);
- g_value_unset (socket_path);
+ if (tp_file->priv->contact)
+ g_object_unref (tp_file->priv->contact);
- if (G_STR_EMPTY (priv->unix_socket_path))
- return -1;
+ if (tp_file->priv->cancellable)
+ g_object_unref (tp_file->priv->cancellable);
+
+ G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
+}
+
+static void
+tp_file_start_transfer (EmpathyTpFile *tp_file)
+{
+ gint fd;
+ struct sockaddr_un addr;
+ GArray *array;
fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
- return -1;
+ {
+ DEBUG ("Failed to create socket, closing channel");
+ empathy_tp_file_cancel (tp_file);
+ return;
+ }
+
+ array = g_value_get_boxed (tp_file->priv->socket_address);
memset (&addr, 0, sizeof (addr));
addr.sun_family = AF_UNIX;
- path_len = strlen (priv->unix_socket_path);
- strncpy (addr.sun_path, priv->unix_socket_path, path_len);
+ strncpy (addr.sun_path, array->data, array->len);
- if (connect (fd, (struct sockaddr*) &addr,
- sizeof (addr)) < 0)
+ if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
{
+ DEBUG ("Failed to connect socket, closing channel");
+ empathy_tp_file_cancel (tp_file);
close (fd);
- return -1;
+ return;
}
- return fd;
-}
-
-/**
- * empathy_tp_file_accept:
- * @tp_file: an #EmpathyTpFile
- *
- * Accepts a file transfer that's in the "local pending" state (i.e.
- * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
- */
-void
-empathy_tp_file_accept (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
- GValue *address;
- GValue nothing = { 0 };
- GError *error = NULL;
+ DEBUG ("Start the transfer");
- g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+ tp_file->priv->start_time = empathy_time_get_current ();
+ tp_file->priv->cancellable = g_cancellable_new ();
+ if (tp_file->priv->incoming)
+ {
+ GInputStream *socket_stream;
- priv = GET_PRIV (tp_file);
+ socket_stream = g_unix_input_stream_new (fd, TRUE);
+ copy_stream (socket_stream, tp_file->priv->out_stream,
+ tp_file->priv->cancellable);
+ g_object_unref (socket_stream);
+ }
+ else
+ {
+ GOutputStream *socket_stream;
- g_return_if_fail (priv->out_stream != NULL);
+ socket_stream = g_unix_output_stream_new (fd, TRUE);
+ copy_stream (tp_file->priv->in_stream, socket_stream,
+ tp_file->priv->cancellable);
+ g_object_unref (socket_stream);
+ }
+}
- DEBUG ("Accepting file: filename=%s", priv->filename);
+static void
+tp_file_state_changed_cb (TpChannel *channel,
+ guint state,
+ guint reason,
+ gpointer user_data,
+ GObject *weak_object)
+{
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
- g_value_init (¬hing, G_TYPE_STRING);
- g_value_set_string (¬hing, "");
+ if (state == tp_file->priv->state)
+ return;
- if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
- -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
- ¬hing, &address, &error, NULL))
- {
- DEBUG ("Accept error: %s",
- error ? error->message : "No message given");
- g_clear_error (&error);
- }
+ DEBUG ("File transfer state changed:\n"
+ "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
+ "\tincoming = %s, in_stream = %s, out_stream = %s",
+ tp_file->priv->filename, tp_file->priv->state, state, reason,
+ tp_file->priv->incoming ? "yes" : "no",
+ tp_file->priv->in_stream ? "present" : "not present",
+ tp_file->priv->out_stream ? "present" : "not present");
- if (priv->unix_socket_path)
- g_free (priv->unix_socket_path);
+ /* If the channel is open AND we have the socket path, we can start the
+ * transfer. The socket path could be NULL if we are not doing the actual
+ * data transfer but are just an observer for the channel. */
+ if (state == TP_FILE_TRANSFER_STATE_OPEN &&
+ tp_file->priv->socket_address != NULL)
+ tp_file_start_transfer (tp_file);
- priv->unix_socket_path = g_value_dup_string (address);
- g_value_unset (address);
+ tp_file->priv->state = state;
+ tp_file->priv->state_change_reason = reason;
- DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
+ g_object_notify (G_OBJECT (tp_file), "state");
}
static void
-receive_tp_file (EmpathyTpFile *tp_file)
+tp_file_transferred_bytes_changed_cb (TpChannel *channel,
+ guint64 count,
+ gpointer user_data,
+ GObject *weak_object)
{
- EmpathyTpFilePriv *priv;
- GInputStream *socket_stream;
- gint socket_fd;
-
- priv = GET_PRIV (tp_file);
-
- socket_fd = _get_local_socket (tp_file);
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
- if (socket_fd < 0)
+ if (tp_file->priv->transferred_bytes == count)
return;
- socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
-
- priv->cancellable = g_cancellable_new ();
-
- copy_stream (socket_stream, priv->out_stream, priv->cancellable);
-
- g_object_unref (socket_stream);
+ tp_file->priv->transferred_bytes = count;
+ g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
}
-
static void
-send_tp_file (EmpathyTpFile *tp_file)
+tp_file_check_if_ready (EmpathyTpFile *tp_file)
{
- gint socket_fd;
- GOutputStream *socket_stream;
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
+ if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
+ tp_file->priv->state == 0)
+ return;
- DEBUG ("Sending file content: filename=%s",
- priv->filename);
+ tp_file->priv->ready = TRUE;
+ g_object_notify (G_OBJECT (tp_file), "ready");
+}
- g_return_if_fail (priv->in_stream);
+static void
+tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
+ EmpathyContact *contact,
+ const GError *error,
+ gpointer user_data,
+ GObject *weak_object)
+{
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
- socket_fd = _get_local_socket (tp_file);
- if (socket_fd < 0)
+ if (error)
{
- DEBUG ("failed to get local socket fd");
+ DEBUG ("Error: %s", error->message);
+ empathy_tp_file_close (tp_file);
return;
}
- DEBUG ("got local socket fd");
- socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
-
- priv->cancellable = g_cancellable_new ();
- copy_stream (priv->in_stream, socket_stream, priv->cancellable);
-
- g_object_unref (socket_stream);
+ tp_file->priv->contact = g_object_ref (contact);
+ tp_file_check_if_ready (tp_file);
}
static void
-tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
- EmpFileTransferState state,
- EmpFileTransferStateChangeReason reason,
- EmpathyTpFile *tp_file)
+tp_file_get_all_cb (TpProxy *proxy,
+ GHashTable *properties,
+ const GError *error,
+ gpointer user_data,
+ GObject *file_obj)
{
- EmpathyTpFilePriv *priv;
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
+
+ if (error)
+ {
+ DEBUG ("Error: %s", error->message);
+ tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
+ NULL);
+ return;
+ }
- priv = GET_PRIV (tp_file);
+ tp_file->priv->size = g_value_get_uint64 (
+ g_hash_table_lookup (properties, "Size"));
+ g_object_notify (file_obj, "size");
- DEBUG ("File transfer state changed: filename=%s, "
- "old state=%u, state=%u, reason=%u",
- priv->filename, priv->state, state, reason);
+ tp_file->priv->state = g_value_get_uint (
+ g_hash_table_lookup (properties, "State"));
+ g_object_notify (file_obj, "state");
- if (state == EMP_FILE_TRANSFER_STATE_OPEN)
- priv->start_time = get_time_msec ();
+ tp_file->priv->transferred_bytes = g_value_get_uint64 (
+ g_hash_table_lookup (properties, "TransferredBytes"));
+ g_object_notify (file_obj, "transferred-bytes");
- DEBUG ("state = %u, direction = %u, in_stream = %s, out_stream = %s",
- state, priv->direction,
- priv->in_stream ? "present" : "not present",
- priv->out_stream ? "present" : "not present");
+ tp_file->priv->filename = g_value_dup_string (
+ g_hash_table_lookup (properties, "Filename"));
+ g_object_notify (file_obj, "filename");
- if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
- priv->direction == EMP_FILE_TRANSFER_DIRECTION_OUTGOING &&
- priv->in_stream)
- send_tp_file (tp_file);
- else if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
- priv->direction == EMP_FILE_TRANSFER_DIRECTION_INCOMING &&
- priv->out_stream)
- receive_tp_file (tp_file);
+ tp_file->priv->content_hash = g_value_dup_string (
+ g_hash_table_lookup (properties, "ContentHash"));
+ g_object_notify (file_obj, "content-hash");
- priv->state = state;
- priv->state_change_reason = reason;
+ tp_file->priv->content_hash_type = g_value_get_uint (
+ g_hash_table_lookup (properties, "ContentHashType"));
+ g_object_notify (file_obj, "content-hash-type");
- g_object_notify (G_OBJECT (tp_file), "state");
+ tp_file->priv->content_type = g_value_dup_string (
+ g_hash_table_lookup (properties, "ContentType"));
+ g_object_notify (file_obj, "content-type");
+
+ tp_file->priv->description = g_value_dup_string (
+ g_hash_table_lookup (properties, "Description"));
+
+ tp_file_check_if_ready (tp_file);
}
static void
-tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
- guint64 count,
- EmpathyTpFile *tp_file,
- GObject *weak_object)
+tp_file_get_requested_cb (TpProxy *proxy,
+ const GValue *requested,
+ const GError *error,
+ gpointer user_data,
+ GObject *weak_object)
{
- EmpathyTpFilePriv *priv;
+ EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
- priv = GET_PRIV (tp_file);
-
- if (priv->transferred_bytes == count)
- return;
+ if (error)
+ {
+ DEBUG ("Error: %s", error->message);
+ tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
+ NULL);
+ return;
+ }
- priv->transferred_bytes = count;
+ tp_file->priv->incoming = !g_value_get_boolean (requested);
+ g_object_notify (G_OBJECT (tp_file), "incoming");
- g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
+ tp_file_check_if_ready (tp_file);
}
-EmpathyContact *
-empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
+static GObject *
+tp_file_constructor (GType type,
+ guint n_props,
+ GObjectConstructParam *props)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
+ GObject *file_obj;
+ EmpathyTpFile *tp_file;
+ TpHandle handle;
+ TpConnection *connection;
- return priv->contact;
-}
+ file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
+ n_props, props);
-GInputStream *
-empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
+ tp_file = EMPATHY_TP_FILE (file_obj);
- priv = GET_PRIV (tp_file);
+ connection = tp_channel_borrow_connection (tp_file->priv->channel);
+ tp_file->priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
+ tp_file->priv->mc = empathy_mission_control_dup_singleton ();
+ tp_file->priv->state_change_reason =
+ TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
- return priv->in_stream;
-}
+ g_signal_connect (tp_file->priv->channel, "invalidated",
+ G_CALLBACK (tp_file_invalidated_cb), tp_file);
-GOutputStream *
-empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
+ tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
+ tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL,
+ G_OBJECT (tp_file), NULL);
- priv = GET_PRIV (tp_file);
+ tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
+ tp_file->priv->channel, tp_file_transferred_bytes_changed_cb,
+ NULL, NULL, G_OBJECT (tp_file), NULL);
- return priv->out_stream;
-}
+ tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
+ TP_IFACE_CHANNEL, "Requested",
+ tp_file_get_requested_cb, NULL, NULL, file_obj);
-const gchar *
-empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
+ tp_cli_dbus_properties_call_get_all (tp_file->priv->channel, -1,
+ TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
+ tp_file_get_all_cb, NULL, NULL, file_obj);
- priv = GET_PRIV (tp_file);
+ handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
+ empathy_tp_contact_factory_get_from_handle (tp_file->priv->factory,
+ handle, tp_file_got_contact_cb, NULL, NULL, file_obj);
- return priv->filename;
+ return file_obj;
}
-EmpFileTransferDirection
-empathy_tp_file_get_direction (EmpathyTpFile *tp_file)
+static void
+tp_file_get_property (GObject *object,
+ guint param_id,
+ GValue *value,
+ GParamSpec *pspec)
{
- EmpathyTpFilePriv *priv;
+ EmpathyTpFile *tp_file;
- priv = GET_PRIV (tp_file);
+ tp_file = EMPATHY_TP_FILE (object);
- return priv->direction;
+ switch (param_id)
+ {
+ case PROP_CHANNEL:
+ g_value_set_object (value, tp_file->priv->channel);
+ break;
+ case PROP_INCOMING:
+ g_value_set_boolean (value, tp_file->priv->incoming);
+ break;
+ case PROP_STATE:
+ g_value_set_uint (value, tp_file->priv->state);
+ break;
+ case PROP_CONTENT_TYPE:
+ g_value_set_string (value, tp_file->priv->content_type);
+ break;
+ case PROP_FILENAME:
+ g_value_set_string (value, tp_file->priv->filename);
+ break;
+ case PROP_SIZE:
+ g_value_set_uint64 (value, tp_file->priv->size);
+ break;
+ case PROP_CONTENT_HASH_TYPE:
+ g_value_set_uint (value, tp_file->priv->content_hash_type);
+ break;
+ case PROP_CONTENT_HASH:
+ g_value_set_string (value, tp_file->priv->content_hash);
+ break;
+ case PROP_TRANSFERRED_BYTES:
+ g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
+ break;
+ case PROP_READY:
+ g_value_set_boolean (value, tp_file->priv->ready);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ };
}
-EmpFileTransferState
-empathy_tp_file_get_state (EmpathyTpFile *tp_file)
+static void
+tp_file_channel_set_dbus_property (gpointer proxy,
+ const gchar *property,
+ const GValue *value)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
-
- return priv->state;
+ DEBUG ("Setting %s property", property);
+ tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
+ TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
+ NULL, NULL, NULL, NULL);
}
-EmpFileTransferStateChangeReason
-empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
+static void
+tp_file_set_property (GObject *object,
+ guint param_id,
+ const GValue *value,
+ GParamSpec *pspec)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
-
- g_return_val_if_fail (priv->state_change_reason >= 0,
- EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
-
- return priv->state_change_reason;
+ EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
+ switch (param_id)
+ {
+ case PROP_CHANNEL:
+ tp_file->priv->channel = g_object_ref (g_value_get_object (value));
+ break;
+ case PROP_STATE:
+ tp_file->priv->state = g_value_get_uint (value);
+ break;
+ case PROP_INCOMING:
+ tp_file->priv->incoming = g_value_get_boolean (value);
+ break;
+ case PROP_FILENAME:
+ g_free (tp_file->priv->filename);
+ tp_file->priv->filename = g_value_dup_string (value);
+ tp_file_channel_set_dbus_property (tp_file->priv->channel,
+ "Filename", value);
+ break;
+ case PROP_SIZE:
+ tp_file->priv->size = g_value_get_uint64 (value);
+ tp_file_channel_set_dbus_property (tp_file->priv->channel,
+ "Size", value);
+ break;
+ case PROP_CONTENT_TYPE:
+ tp_file_channel_set_dbus_property (tp_file->priv->channel,
+ "ContentType", value);
+ g_free (tp_file->priv->content_type);
+ tp_file->priv->content_type = g_value_dup_string (value);
+ break;
+ case PROP_CONTENT_HASH:
+ tp_file_channel_set_dbus_property (tp_file->priv->channel,
+ "ContentHash", value);
+ g_free (tp_file->priv->content_hash);
+ tp_file->priv->content_hash = g_value_dup_string (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ };
}
-guint64
-empathy_tp_file_get_size (EmpathyTpFile *tp_file)
-{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
+static GHashTable *ft_table = NULL;
- return priv->size;
-}
-
-guint64
-empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
+static void
+tp_file_weak_notify_cb (gpointer channel,
+ GObject *tp_file)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
-
- return priv->transferred_bytes;
+ g_hash_table_remove (ft_table, channel);
}
-gint
-empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
+/**
+ * empathy_tp_file_new:
+ * @channel: a Telepathy channel
+ *
+ * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
+ * existing #EmpathyTpFile for that channel.
+ *
+ * Returns: a new #EmpathyTpFile
+ */
+EmpathyTpFile *
+empathy_tp_file_new (TpChannel *channel)
{
- EmpathyTpFilePriv *priv;
- gint64 curr_time, elapsed_time;
- gdouble time_per_byte;
- gdouble remaining_time;
+ EmpathyTpFile *tp_file;
- priv = GET_PRIV (tp_file);
+ g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
- if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
- return -1;
+ if (ft_table != NULL)
+ {
+ tp_file = g_hash_table_lookup (ft_table, channel);
+ if (tp_file != NULL) {
+ return g_object_ref (tp_file);
+ }
+ }
+ else
+ ft_table = g_hash_table_new_full (empathy_proxy_hash,
+ empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL);
- if (priv->transferred_bytes == priv->size)
- return 0;
+ tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
+ "channel", channel,
+ NULL);
- curr_time = get_time_msec ();
- elapsed_time = curr_time - priv->start_time;
- time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
- remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
+ g_hash_table_insert (ft_table, g_object_ref (channel), tp_file);
+ g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel);
- return (gint) (remaining_time + 0.5);
+ return tp_file;
}
-void
-empathy_tp_file_cancel (EmpathyTpFile *tp_file)
+/**
+ * empathy_tp_file_get_channel
+ * @tp_file: an #EmpathyTpFile
+ *
+ * Returns the Telepathy file transfer channel
+ *
+ * Returns: the #TpChannel
+ */
+TpChannel *
+empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
-
- tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
- g_cancellable_cancel (priv->cancellable);
+ return tp_file->priv->channel;
}
-void
-empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
- GInputStream *in_stream)
+static void
+tp_file_method_cb (TpChannel *channel,
+ const GValue *address,
+ const GError *error,
+ gpointer user_data,
+ GObject *weak_object)
{
- EmpathyTpFilePriv *priv;
+ EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
+ GArray *array;
- priv = GET_PRIV (tp_file);
+ if (error)
+ {
+ DEBUG ("Error: %s", error->message);
+ empathy_tp_file_cancel (tp_file);
+ return;
+ }
- if (priv->in_stream == in_stream)
- return;
+ if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
+ {
+ tp_file->priv->socket_address = tp_g_value_slice_dup (address);
+ }
+ else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
+ {
+ /* Old bugged version of telepathy-salut used to store the address
+ * as a 's' instead of an 'ay' */
+ const gchar *path;
- if (priv->direction == EMP_FILE_TRANSFER_DIRECTION_INCOMING)
- g_warning ("Setting an input stream for incoming file "
- "transfers is useless");
+ path = g_value_get_string (address);
+ array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path));
+ g_array_insert_vals (array, 0, path, strlen (path));
- if (priv->in_stream)
- g_object_unref (priv->in_stream);
+ tp_file->priv->socket_address = tp_g_value_slice_new (
+ DBUS_TYPE_G_UCHAR_ARRAY);
+ g_value_set_boxed (tp_file->priv->socket_address, array);
- if (in_stream)
- g_object_ref (in_stream);
+ g_array_free (array, TRUE);
+ }
+ else
+ {
+ DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address));
+ empathy_tp_file_cancel (tp_file);
+ return;
+ }
- priv->in_stream = in_stream;
+ array = g_value_get_boxed (tp_file->priv->socket_address);
+ DEBUG ("Got unix socket path: %s", array->data);
- g_object_notify (G_OBJECT (tp_file), "in-stream");
+ if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN)
+ tp_file_start_transfer (tp_file);
}
+/**
+ * empathy_tp_file_accept:
+ * @tp_file: an #EmpathyTpFile
+ * @offset: position where to start the transfer
+ * @gfile: a #GFile where to write transfered data
+ * @error: a #GError set if there is an error when opening @gfile
+ *
+ * Accepts a file transfer that's in the "local pending" state (i.e.
+ * TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
+ */
void
-empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
- GOutputStream *out_stream)
+empathy_tp_file_accept (EmpathyTpFile *tp_file,
+ guint64 offset,
+ GFile *gfile,
+ GError **error)
{
- EmpathyTpFilePriv *priv;
+ GValue nothing = { 0 };
- priv = GET_PRIV (tp_file);
+ g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+ g_return_if_fail (G_IS_FILE (gfile));
- if (priv->out_stream == out_stream)
+ tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
+ FALSE, 0, NULL, error));
+ if (error && *error)
return;
- if (priv->direction == EMP_FILE_TRANSFER_DIRECTION_OUTGOING)
- g_warning ("Setting an output stream for outgoing file "
- "transfers is useless");
+ g_free (tp_file->priv->filename);
+ tp_file->priv->filename = g_file_get_basename (gfile);
+ g_object_notify (G_OBJECT (tp_file), "filename");
- if (priv->out_stream)
- g_object_unref (priv->out_stream);
+ DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
- if (out_stream)
- g_object_ref (out_stream);
+ g_value_init (¬hing, G_TYPE_STRING);
+ g_value_set_static_string (¬hing, "");
- priv->out_stream = out_stream;
+ tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
+ -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+ ¬hing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
}
+/**
+ * empathy_tp_file_offer:
+ * @tp_file: an #EmpathyTpFile
+ * @gfile: a #GFile where to read the data to transfer
+ * @error: a #GError set if there is an error when opening @gfile
+ *
+ * Offers a file transfer that's in the "not offered" state (i.e.
+ * TP_FILE_TRANSFER_STATE_NOT_OFFERED).
+ */
void
-empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
- const gchar *filename)
+empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
{
- EmpathyTpFilePriv *priv;
-
- priv = GET_PRIV (tp_file);
+ GValue nothing = { 0 };
- g_return_if_fail (filename != NULL);
+ g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
- if (priv->filename && strcmp (filename, priv->filename) == 0)
- return;
+ tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
+ if (error && *error)
+ return;
- g_free (priv->filename);
- priv->filename = g_strdup (filename);
+ g_value_init (¬hing, G_TYPE_STRING);
+ g_value_set_static_string (¬hing, "");
- g_object_notify (G_OBJECT (tp_file), "filename");
+ tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
+ -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+ ¬hing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
}
-/* Functions to copy the content of a GInputStream to a GOutputStream */
-
-#define N_BUFFERS 2
-#define BUFFER_SIZE 4096
-
-typedef struct {
- GInputStream *in;
- GOutputStream *out;
- GCancellable *cancellable;
- char *buff[N_BUFFERS]; /* the temporary buffers */
- gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
- gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
- gint curr_read; /* index of the buffer used for reading */
- gint curr_write; /* index of the buffer used for writing */
- gboolean is_reading; /* we are reading */
- gboolean is_writing; /* we are writing */
- guint n_closed; /* number of streams that have been closed */
-} CopyData;
+EmpathyContact *
+empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+ return tp_file->priv->contact;
+}
-static void schedule_next (CopyData *copy);
+const gchar *
+empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+ return tp_file->priv->filename;
+}
-static void
-free_copy_data_if_closed (CopyData *copy)
+gboolean
+empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
{
- gint i;
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
+ return tp_file->priv->incoming;
+}
- /* Free the data only if both the input and output streams have
- * been closed. */
- copy->n_closed++;
- if (copy->n_closed < 2)
- return;
+TpFileTransferState
+empathy_tp_file_get_state (EmpathyTpFile *tp_file,
+ TpFileTransferStateChangeReason *reason)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
+ TP_FILE_TRANSFER_STATE_NONE);
- if (copy->in != NULL)
- g_object_unref (copy->in);
+ if (reason != NULL)
+ *reason = tp_file->priv->state_change_reason;
- if (copy->out != NULL)
- g_object_unref (copy->out);
+ return tp_file->priv->state;
+}
- for (i = 0; i < N_BUFFERS; i++)
- g_free (copy->buff[i]);
+guint64
+empathy_tp_file_get_size (EmpathyTpFile *tp_file)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
+ EMPATHY_TP_FILE_UNKNOWN_SIZE);
+ return tp_file->priv->size;
+}
- g_object_unref (copy->cancellable);
- g_free (copy);
+guint64
+empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
+ return tp_file->priv->transferred_bytes;
}
-static void
-io_error (CopyData *copy,
- GError *error)
+gint
+empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
{
- g_cancellable_cancel (copy->cancellable);
+ time_t curr_time, elapsed_time;
+ gdouble time_per_byte;
+ gdouble remaining_time;
- if (error == NULL)
- g_warning ("I/O error");
- else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
- ; /* Ignore cancellations */
- else
- g_warning ("I/O error: %d: %s\n", error->code, error->message);
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
- if (copy->in != NULL)
- g_input_stream_close (copy->in, NULL, NULL);
+ if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
+ return -1;
- if (copy->out != NULL)
- g_output_stream_close (copy->out, NULL, NULL);
+ if (tp_file->priv->transferred_bytes == tp_file->priv->size)
+ return 0;
- free_copy_data_if_closed (copy);
+ curr_time = empathy_time_get_current ();
+ elapsed_time = curr_time - tp_file->priv->start_time;
+ time_per_byte = (gdouble) elapsed_time /
+ (gdouble) tp_file->priv->transferred_bytes;
+ remaining_time = time_per_byte * (tp_file->priv->size -
+ tp_file->priv->transferred_bytes);
+
+ return (gint) remaining_time;
}
-static void
-close_done (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+const gchar *
+empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
{
- CopyData *copy = user_data;
-
- g_object_unref (source_object);
- free_copy_data_if_closed (copy);
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+ return tp_file->priv->content_type;
}
-static void
-write_done_cb (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+void
+empathy_tp_file_cancel (EmpathyTpFile *tp_file)
{
- CopyData *copy = user_data;
- gssize count_write;
- GError *error = NULL;
+ g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
- count_write = g_output_stream_write_finish (copy->out, res, &error);
+ DEBUG ("Closing channel..");
+ tp_cli_channel_call_close (tp_file->priv->channel, -1,
+ NULL, NULL, NULL, NULL);
- if (count_write <= 0)
- {
- io_error (copy, error);
- g_error_free (error);
- return;
- }
+ if (tp_file->priv->cancellable != NULL)
+ g_cancellable_cancel (tp_file->priv->cancellable);
+}
- copy->is_full[copy->curr_write] = FALSE;
- copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
- copy->is_writing = FALSE;
+void
+empathy_tp_file_close (EmpathyTpFile *tp_file)
+{
+ empathy_tp_file_cancel (tp_file);
+}
- schedule_next (copy);
+gboolean
+empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
+{
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
+
+ return tp_file->priv->ready;
}
static void
-read_done_cb (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+empathy_tp_file_class_init (EmpathyTpFileClass *klass)
{
- CopyData *copy = user_data;
- gssize count_read;
- GError *error = NULL;
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
- count_read = g_input_stream_read_finish (copy->in, res, &error);
+ object_class->finalize = tp_file_finalize;
+ object_class->constructor = tp_file_constructor;
+ object_class->get_property = tp_file_get_property;
+ object_class->set_property = tp_file_set_property;
- if (count_read == 0)
- {
- g_input_stream_close_async (copy->in, 0, copy->cancellable,
- close_done, copy);
- copy->in = NULL;
- }
- else if (count_read < 0)
- {
- io_error (copy, error);
- g_error_free (error);
- return;
- }
+ /* Construct-only properties */
+ g_object_class_install_property (object_class,
+ PROP_CHANNEL,
+ g_param_spec_object ("channel",
+ "telepathy channel",
+ "The file transfer channel",
+ TP_TYPE_CHANNEL,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY));
- copy->count[copy->curr_read] = count_read;
- copy->is_full[copy->curr_read] = TRUE;
- copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
- copy->is_reading = FALSE;
+ g_object_class_install_property (object_class,
+ PROP_STATE,
+ g_param_spec_uint ("state",
+ "state of the transfer",
+ "The file transfer state",
+ 0,
+ G_MAXUINT,
+ G_MAXUINT,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT));
- schedule_next (copy);
-}
+ g_object_class_install_property (object_class,
+ PROP_INCOMING,
+ g_param_spec_boolean ("incoming",
+ "incoming",
+ "Whether the transfer is incoming",
+ FALSE,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT));
-static void
-schedule_next (CopyData *copy)
-{
- if (copy->in != NULL &&
- !copy->is_reading &&
- !copy->is_full[copy->curr_read])
- {
- /* We are not reading and the current buffer is empty, so
- * start an async read. */
- copy->is_reading = TRUE;
- g_input_stream_read_async (copy->in,
- copy->buff[copy->curr_read],
- BUFFER_SIZE, 0, copy->cancellable,
- read_done_cb, copy);
- }
+ g_object_class_install_property (object_class,
+ PROP_READY,
+ g_param_spec_boolean ("ready",
+ "ready",
+ "Whether the object is ready",
+ FALSE,
+ G_PARAM_READABLE));
- if (!copy->is_writing &&
- copy->is_full[copy->curr_write])
- {
- if (copy->count[copy->curr_write] == 0)
- {
- /* The last read on the buffer read 0 bytes, this
- * means that we got an EOF, so we can close
- * the output channel. */
- g_output_stream_close_async (copy->out, 0,
- copy->cancellable,
- close_done, copy);
- copy->out = NULL;
- }
- else
- {
- /* We are not writing and the current buffer contains
- * data, so start an async write. */
- copy->is_writing = TRUE;
- g_output_stream_write_async (copy->out,
- copy->buff[copy->curr_write],
- copy->count[copy->curr_write],
- 0, copy->cancellable,
- write_done_cb, copy);
- }
- }
-}
+ g_object_class_install_property (object_class,
+ PROP_FILENAME,
+ g_param_spec_string ("filename",
+ "name of the transfer",
+ "The file transfer filename",
+ "",
+ G_PARAM_READWRITE));
-static void
-copy_stream (GInputStream *in,
- GOutputStream *out,
- GCancellable *cancellable)
-{
- CopyData *copy;
- gint i;
+ g_object_class_install_property (object_class,
+ PROP_SIZE,
+ g_param_spec_uint64 ("size",
+ "size of the file",
+ "The file transfer size",
+ 0,
+ G_MAXUINT64,
+ G_MAXUINT64,
+ G_PARAM_READWRITE));
- g_return_if_fail (in != NULL);
- g_return_if_fail (out != NULL);
+ g_object_class_install_property (object_class,
+ PROP_CONTENT_TYPE,
+ g_param_spec_string ("content-type",
+ "file transfer content-type",
+ "The file transfer content-type",
+ "",
+ G_PARAM_READWRITE));
- copy = g_new0 (CopyData, 1);
- copy->in = g_object_ref (in);
- copy->out = g_object_ref (out);
+ g_object_class_install_property (object_class,
+ PROP_CONTENT_HASH_TYPE,
+ g_param_spec_uint ("content-hash-type",
+ "file transfer hash type",
+ "The type of the file transfer hash",
+ 0,
+ G_MAXUINT,
+ 0,
+ G_PARAM_READWRITE));
- if (cancellable != NULL)
- copy->cancellable = g_object_ref (cancellable);
- else
- copy->cancellable = g_cancellable_new ();
+ g_object_class_install_property (object_class,
+ PROP_CONTENT_HASH,
+ g_param_spec_string ("content-hash",
+ "file transfer hash",
+ "The hash of the transfer's contents",
+ "",
+ G_PARAM_READWRITE));
- for (i = 0; i < N_BUFFERS; i++)
- copy->buff[i] = g_malloc (BUFFER_SIZE);
+ g_object_class_install_property (object_class,
+ PROP_TRANSFERRED_BYTES,
+ g_param_spec_uint64 ("transferred-bytes",
+ "bytes transferred",
+ "The number of bytes transferred",
+ 0,
+ G_MAXUINT64,
+ 0,
+ G_PARAM_READWRITE));
- schedule_next (copy);
+ g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
}
+