Commit 391c9836 authored by Jonathan Matthew's avatar Jonathan Matthew

create GObjects implementing rb.ChunkLoader and Gio.File.copy_async

These currently don't work in python and we need them to get the
Magnatune plugin working again.  The async copy object may disappear
at some point in the future, but the chunk loader probably won't.
parent 3aa5da74
......@@ -17,8 +17,12 @@ rb_introspection_sources = \
backends/rb-player-gst-tee.c \
backends/rb-player.h \
backends/rb-player.c \
lib/rb-async-copy.h \
lib/rb-async-copy.c \
lib/rb-builder-helpers.h \
lib/rb-builder-helpers.c \
lib/rb-chunk-loader.h \
lib/rb-chunk-loader.c \
lib/rb-debug.h \
lib/rb-debug.c \
lib/rb-file-helpers.h \
......
......@@ -2,9 +2,11 @@
#include <glib-object.h>
#include <gtk/gtk.h>
#include "rb-async-copy.h"
#include "rb-auto-playlist-source.h"
#include "rb-cell-renderer-pixbuf.h"
#include "rb-cell-renderer-rating.h"
#include "rb-chunk-loader.h"
#include "rb-cut-and-paste-code.h"
#include "rb-debug.h"
#include "rb-device-source.h"
......
......@@ -37,7 +37,11 @@ librb_la_SOURCES = \
rb-gst-media-types.c \
rb-gst-media-types.h \
rb-missing-plugins.c \
rb-missing-plugins.h
rb-missing-plugins.h \
rb-async-copy.c \
rb-async-copy.h \
rb-chunk-loader.c \
rb-chunk-loader.h
INCLUDES = \
-DGNOMELOCALEDIR=\""$(datadir)/locale"\" \
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include <lib/rb-async-copy.h>
#include <lib/rb-debug.h>
/**
* SECTION:rb-async-copy
* @short_description: performs asynchronous file copies (like g_file_copy_async)
*
*/
static void rb_async_copy_class_init (RBAsyncCopyClass *klass);
static void rb_async_copy_init (RBAsyncCopy *copy);
struct _RBAsyncCopyPrivate
{
GError *error;
GCancellable *cancel;
GFile *src;
GFile *dest;
RBAsyncCopyCallback callback;
gpointer callback_data;
GDestroyNotify destroy_data;
RBAsyncCopyProgressCallback progress;
gpointer progress_data;
GDestroyNotify destroy_progress_data;
};
G_DEFINE_TYPE (RBAsyncCopy, rb_async_copy, G_TYPE_OBJECT);
static void
progress_cb (goffset current_num_bytes, goffset total_bytes, gpointer data)
{
RBAsyncCopy *copy = RB_ASYNC_COPY (data);
if (copy->priv->progress)
copy->priv->progress (copy, current_num_bytes, total_bytes, copy->priv->progress_data);
}
static void
copy_cb (GObject *src, GAsyncResult *res, gpointer data)
{
RBAsyncCopy *copy = RB_ASYNC_COPY (data);
gboolean result;
result = g_file_copy_finish (G_FILE (src), res, &copy->priv->error);
rb_debug ("copy finished: %s", (result == FALSE) ? copy->priv->error->message : "ok");
copy->priv->callback (copy, result, copy->priv->callback_data);
}
/**
* rb_async_copy_start:
* @copy: a #RBAsyncCopy
* @src: source URI
* @dest: destination URI
* @callback: completion callback
* @user_data: data for completion callback
* @destroy_data: destroy function for user_data
*
* Starts copying @src to @dest, calling @callback on completion or error.
*/
void
rb_async_copy_start (RBAsyncCopy *copy,
const char *src,
const char *dest,
RBAsyncCopyCallback callback,
gpointer user_data,
GDestroyNotify destroy_data)
{
g_assert (copy->priv->src == NULL);
copy->priv->cancel = g_cancellable_new ();
copy->priv->callback = callback;
copy->priv->callback_data = user_data;
copy->priv->destroy_data = destroy_data;
copy->priv->src = g_file_new_for_commandline_arg (src);
copy->priv->dest = g_file_new_for_commandline_arg (dest);
g_file_copy_async (copy->priv->src,
copy->priv->dest,
G_FILE_COPY_NONE,
G_PRIORITY_DEFAULT,
copy->priv->cancel,
progress_cb,
copy,
copy_cb,
copy);
}
/**
* rb_async_copy_cancel:
* @copy: a #RBAsyncCopy
*
* Cancels the loading operation, ensuring that the callback
* will not be called again.
*/
void
rb_async_copy_cancel (RBAsyncCopy *copy)
{
g_cancellable_cancel (copy->priv->cancel);
}
/**
* rb_async_copy_set_callback:
* @copy: a #RBAsyncCopy
* @callback: the progress callback
* @user_data: data to pass to the callback
* @destroy_data: function to call to destroy user_data
*
* Sets the progress callback for the copy. The callback will
* be called periodically while the copy is proceeding.
*/
void
rb_async_copy_set_progress (RBAsyncCopy *copy,
RBAsyncCopyProgressCallback callback,
gpointer user_data,
GDestroyNotify destroy_data)
{
g_assert (copy->priv->progress == NULL);
g_assert (copy->priv->src == NULL);
copy->priv->progress = callback;
copy->priv->progress_data = user_data;
copy->priv->destroy_progress_data = destroy_data;
}
/**
* rb_async_copy_get_error:
* @copy: a #RBAsyncCopy
*
* If an error has occurred that prevents the copy from proceeding,
* this function will return a #GError, otherwise NULL.
*
* Return value: copy error or NULL
*/
GError *
rb_async_copy_get_error (RBAsyncCopy *copy)
{
if (copy->priv->error)
return g_error_copy (copy->priv->error);
return NULL;
}
/**
* rb_async_copy_new:
*
* Creates and returns a new #RBAsyncCopy instance.
*
* Return value: #RBAsyncCopy instance
*/
RBAsyncCopy *
rb_async_copy_new (void)
{
return RB_ASYNC_COPY (g_object_new (RB_TYPE_ASYNC_COPY, NULL));
}
static void
impl_finalize (GObject *object)
{
RBAsyncCopy *copy = RB_ASYNC_COPY (object);
g_clear_error (&copy->priv->error);
if (copy->priv->cancel) {
g_object_unref (copy->priv->cancel);
copy->priv->cancel = NULL;
}
if (copy->priv->src) {
g_object_unref (copy->priv->src);
copy->priv->src = NULL;
}
if (copy->priv->dest) {
g_object_unref (copy->priv->dest);
copy->priv->dest = NULL;
}
if (copy->priv->destroy_data) {
copy->priv->destroy_data (copy->priv->callback_data);
}
if (copy->priv->destroy_progress_data) {
copy->priv->destroy_progress_data (copy->priv->progress_data);
}
G_OBJECT_CLASS (rb_async_copy_parent_class)->finalize (object);
}
static void
rb_async_copy_init (RBAsyncCopy *copy)
{
copy->priv = G_TYPE_INSTANCE_GET_PRIVATE (copy, RB_TYPE_ASYNC_COPY, RBAsyncCopyPrivate);
}
static void
rb_async_copy_class_init (RBAsyncCopyClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = impl_finalize;
g_type_class_add_private (klass, sizeof (RBAsyncCopyPrivate));
}
/*
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grants permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef __RB_ASYNC_COPY_H
#define __RB_ASYNC_COPY_H
#include <gio/gio.h>
G_BEGIN_DECLS
#define RB_TYPE_ASYNC_COPY (rb_async_copy_get_type ())
#define RB_ASYNC_COPY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_ASYNC_COPY, RBAsyncCopy))
#define RB_ASYNC_COPY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_ASYNC_COPY, RBAsyncCopyClass))
#define RB_IS_ASYNC_COPY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_ASYNC_COPY))
#define RB_IS_ASYNC_COPY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_ASYNC_COPY))
#define RB_ASYNC_COPY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_ASYNC_COPY, RBAsyncCopyClass))
typedef struct _RBAsyncCopy RBAsyncCopy;
typedef struct _RBAsyncCopyClass RBAsyncCopyClass;
typedef struct _RBAsyncCopyPrivate RBAsyncCopyPrivate;
typedef void (*RBAsyncCopyProgressCallback) (RBAsyncCopy *copy, goffset position, goffset total, gpointer data);
typedef void (*RBAsyncCopyCallback) (RBAsyncCopy *copy, gboolean success, gpointer data);
struct _RBAsyncCopy
{
GObject parent;
RBAsyncCopyPrivate *priv;
};
struct _RBAsyncCopyClass
{
GObjectClass parent_class;
};
GType rb_async_copy_get_type (void);
RBAsyncCopy * rb_async_copy_new (void);
void rb_async_copy_set_progress (RBAsyncCopy *copy,
RBAsyncCopyProgressCallback callback,
gpointer user_data,
GDestroyNotify destroy_data);
GError * rb_async_copy_get_error (RBAsyncCopy *copy);
void rb_async_copy_start (RBAsyncCopy *copy,
const char *src_uri,
const char *dest_uri,
RBAsyncCopyCallback callback,
gpointer user_data,
GDestroyNotify destroy_data);
void rb_async_copy_cancel (RBAsyncCopy *copy);
G_END_DECLS
#endif /* __RB_ASYNC_COPY_H */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include <lib/rb-chunk-loader.h>
#include <lib/rb-debug.h>
/**
* SECTION:rb-chunk-loader
* @short_description: simple utility for asynchronously fetching data by URL in chunks
*
*/
static void rb_chunk_loader_class_init (RBChunkLoaderClass *klass);
static void rb_chunk_loader_init (RBChunkLoader *loader);
struct _RBChunkLoaderPrivate
{
char *uri;
gssize chunk_size;
guint8 *chunk;
GString chunk_string;
guint64 total;
GError *error;
GFile *file;
GFileInputStream *stream;
GCancellable *cancel;
RBChunkLoaderCallback callback;
gpointer callback_data;
GDestroyNotify destroy_data;
};
G_DEFINE_TYPE (RBChunkLoader, rb_chunk_loader, G_TYPE_OBJECT);
static void
stream_close_cb (GObject *obj, GAsyncResult *res, gpointer data)
{
GError *error = NULL;
g_input_stream_close_finish (G_INPUT_STREAM (obj), res, &error);
if (error != NULL) {
rb_debug ("unable to close input stream: %s", error->message);
g_clear_error (&error);
}
}
static void
cleanup (RBChunkLoader *loader)
{
g_input_stream_close_async (G_INPUT_STREAM (loader->priv->stream),
G_PRIORITY_DEFAULT,
loader->priv->cancel,
stream_close_cb,
loader);
}
static void
stream_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
{
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
gssize done;
done = g_input_stream_read_finish (G_INPUT_STREAM (obj),
res,
&loader->priv->error);
if (done == -1) {
rb_debug ("error reading from stream: %s", loader->priv->error->message);
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
cleanup (loader);
} else if (done == 0) {
rb_debug ("reached end up input stream");
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
cleanup (loader);
} else {
loader->priv->chunk_string.len = done;
loader->priv->callback (loader, &loader->priv->chunk_string, loader->priv->total, loader->priv->callback_data);
g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
loader->priv->chunk,
loader->priv->chunk_size,
G_PRIORITY_DEFAULT,
loader->priv->cancel,
stream_read_async_cb,
loader);
}
}
static void
stream_info_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
{
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
GFileInfo *info;
GError *error = NULL;
info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (obj), res, &error);
if (info != NULL) {
loader->priv->total = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
} else {
loader->priv->total = 0;
rb_debug ("couldn't get size of source file: %s", error->message);
g_clear_error (&error);
}
g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
loader->priv->chunk,
loader->priv->chunk_size,
G_PRIORITY_DEFAULT,
loader->priv->cancel,
stream_read_async_cb,
loader);
}
static void
file_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
{
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
loader->priv->stream = g_file_read_finish (G_FILE (obj),
res,
&loader->priv->error);
if (loader->priv->error != NULL) {
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
return;
}
g_file_input_stream_query_info_async (loader->priv->stream,
G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_PRIORITY_DEFAULT,
loader->priv->cancel,
stream_info_async_cb,
loader);
}
/**
* rb_chunk_loader_start:
* @loader: a #RBChunkLoader
* @uri: the uri to load
* @chunk_size: maximum chunk size
*
* Starts loading data from the specified URI, passing it in chunks
* of at most @chunk_size to the callback.
*/
void
rb_chunk_loader_start (RBChunkLoader *loader, const char *uri, gssize chunk_size)
{
g_assert (loader->priv->uri == NULL);
g_assert (loader->priv->callback != NULL);
loader->priv->uri = g_strdup (uri);
loader->priv->chunk_size = chunk_size;
loader->priv->chunk = g_malloc0 (chunk_size+1);
loader->priv->chunk_string.str = (gchar *)loader->priv->chunk;
loader->priv->chunk_string.len = 0;
loader->priv->chunk_string.allocated_len = chunk_size;
loader->priv->cancel = g_cancellable_new ();
loader->priv->file = g_file_new_for_commandline_arg (loader->priv->uri);
g_file_read_async (loader->priv->file,
G_PRIORITY_DEFAULT,
loader->priv->cancel,
file_read_async_cb,
loader);
}
/**
* rb_chunk_loader_cancel:
* @loader: a #RBChunkLoader
*
* Cancels the loading operation, ensuring that the callback
* will not be called again.
*/
void
rb_chunk_loader_cancel (RBChunkLoader *loader)
{
g_cancellable_cancel (loader->priv->cancel);
}
/**
* rb_chunk_loader_set_callback:
* @loader: a #RBChunkLoader
* @callback: the data/error callback
* @user_data: data to pass to the callback
* @destroy_data: function to call to destroy user_data
*
* Sets the loader data callback. This will be called with each
* chunk of data read, or with NULL to indicate the end of the file
* or that an error has occurred. To determine which of these is
* the case, call @rb_chunk_loader_get_error.
*
* This must be called before @rb_chunk_loader_start.
*/
void
rb_chunk_loader_set_callback (RBChunkLoader *loader,
RBChunkLoaderCallback callback,
gpointer user_data,
GDestroyNotify destroy_data)
{
g_assert (loader->priv->callback == NULL);
g_assert (loader->priv->file == NULL);
loader->priv->callback = callback;
loader->priv->callback_data = user_data;
loader->priv->destroy_data = destroy_data;
}
/**
* rb_chunk_loader_get_error:
* @loader: a #RBChunkLoader
*
* If an error has occurred that prevents the loader from providing
* any further data, this function will return a #GError, otherwise
* NULL.
*
* Return value: loader error or NULL
*/
GError *
rb_chunk_loader_get_error (RBChunkLoader *loader)
{
if (loader->priv->error)
return g_error_copy (loader->priv->error);
return NULL;
}
/**
* rb_chunk_loader_new:
*
* Creates and returns a new #RBChunkLoader instance.
*
* Return value: #RBChunkLoader instance
*/
RBChunkLoader *
rb_chunk_loader_new (void)
{
return RB_CHUNK_LOADER (g_object_new (RB_TYPE_CHUNK_LOADER, NULL));
}
static void
impl_finalize (GObject *object)
{
RBChunkLoader *loader = RB_CHUNK_LOADER (object);
g_free (loader->priv->uri);
g_free (loader->priv->chunk);
g_clear_error (&loader->priv->error);
if (loader->priv->cancel) {
g_object_unref (loader->priv->cancel);
loader->priv->cancel = NULL;
}
if (loader->priv->file) {
g_object_unref (loader->priv->file);
loader->priv->file = NULL;
}
if (loader->priv->stream) {
g_object_unref (loader->priv->stream);
loader->priv->stream = NULL;
}
if (loader->priv->destroy_data) {
loader->priv->destroy_data (loader->priv->callback_data);
}
G_OBJECT_CLASS (rb_chunk_loader_parent_class)->finalize (object);
}
static void
rb_chunk_loader_init (RBChunkLoader *loader)
{
loader->priv = G_TYPE_INSTANCE_GET_PRIVATE (loader, RB_TYPE_CHUNK_LOADER, RBChunkLoaderPrivate);
}
static void
rb_chunk_loader_class_init (RBChunkLoaderClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = impl_finalize;
g_type_class_add_private (klass, sizeof (RBChunkLoaderPrivate));
}
/*
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grants permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef __RB_CHUNK_LOADER_H
#define __RB_CHUNK_LOADER_H
#include <gio/gio.h>
G_BEGIN_DECLS
#define RB_TYPE_CHUNK_LOADER (rb_chunk_loader_get_type ())
#define RB_CHUNK_LOADER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_CHUNK_LOADER, RBChunkLoader))
#define RB_CHUNK_LOADER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_CHUNK_LOADER, RBChunkLoaderClass))
#define RB_IS_CHUNK_LOADER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_CHUNK_LOADER))
#define RB_IS_CHUNK_LOADER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_CHUNK_LOADER))
#define RB_CHUNK_LOADER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_CHUNK_LOADER, RBChunkLoaderClass))
typedef struct _RBChunkLoader RBChunkLoader;
typedef struct _RBChunkLoaderClass RBChunkLoaderClass;
typedef struct _RBChunkLoaderPrivate RBChunkLoaderPrivate;
typedef void (*RBChunkLoaderCallback) (RBChunkLoader *loader, GString *data, goffset total, gpointer user_data);
struct _RBChunkLoader
{
GObject parent;
RBChunkLoaderPrivate *priv;
};
struct _RBChunkLoaderClass
{
GObjectClass parent_class;
};
GType rb_chunk_loader_get_type (void);
RBChunkLoader * rb_chunk_loader_new (void);
void rb_chunk_loader_set_callback (RBChunkLoader *loader,
RBChunkLoaderCallback callback,
gpointer user_data,
GDestroyNotify destroy_data);
GError * rb_chunk_loader_get_error (RBChunkLoader *loader);
void rb_chunk_loader_start (RBChunkLoader *loader,
const char *uri,
gssize chunk_size);
void rb_chunk_loader_cancel (RBChunkLoader *loader);
G_END_DECLS
#endif /* __RB_CHUNK_LOADER_H */