1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* The Rhythmbox authors hereby grant permission for non-GPL compatible
11
* GStreamer plugins to be used and distributed together with GStreamer
12
* and Rhythmbox. This permission is above and beyond the permissions granted
13
* by the GPL license by which Rhythmbox is covered. If you modify this code
14
* you may extend this exception to your version of the code, but you are not
15
* obligated to do so. If you do not wish to do so, delete this exception
16
* statement from your version.
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31
#include <lib/rb-chunk-loader.h>
32
#include <lib/rb-debug.h>
35
* SECTION:rb-chunk-loader
36
* @short_description: simple utility for asynchronously fetching data by URL in chunks
41
static void rb_chunk_loader_class_init (RBChunkLoaderClass *klass);
42
static void rb_chunk_loader_init (RBChunkLoader *loader);
44
struct _RBChunkLoaderPrivate
54
GFileInputStream *stream;
57
RBChunkLoaderCallback callback;
58
gpointer callback_data;
59
GDestroyNotify destroy_data;
62
G_DEFINE_TYPE (RBChunkLoader, rb_chunk_loader, G_TYPE_OBJECT);
65
stream_close_cb (GObject *obj, GAsyncResult *res, gpointer data)
69
g_input_stream_close_finish (G_INPUT_STREAM (obj), res, &error);
72
rb_debug ("unable to close input stream: %s", error->message);
73
g_clear_error (&error);
78
cleanup (RBChunkLoader *loader)
80
g_input_stream_close_async (G_INPUT_STREAM (loader->priv->stream),
88
stream_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
90
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
93
done = g_input_stream_read_finish (G_INPUT_STREAM (obj),
95
&loader->priv->error);
97
rb_debug ("error reading from stream: %s", loader->priv->error->message);
98
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
100
} else if (done == 0) {
101
rb_debug ("reached end up input stream");
102
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
105
loader->priv->chunk_string.len = done;
106
loader->priv->callback (loader, &loader->priv->chunk_string, loader->priv->total, loader->priv->callback_data);
107
g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
109
loader->priv->chunk_size,
111
loader->priv->cancel,
112
stream_read_async_cb,
118
stream_info_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
120
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
122
GError *error = NULL;
124
info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (obj), res, &error);
126
loader->priv->total = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
128
loader->priv->total = 0;
129
rb_debug ("couldn't get size of source file: %s", error->message);
130
g_clear_error (&error);
133
g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
135
loader->priv->chunk_size,
137
loader->priv->cancel,
138
stream_read_async_cb,
143
file_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
145
RBChunkLoader *loader = RB_CHUNK_LOADER (data);
147
loader->priv->stream = g_file_read_finish (G_FILE (obj),
149
&loader->priv->error);
150
if (loader->priv->error != NULL) {
151
loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
155
g_file_input_stream_query_info_async (loader->priv->stream,
156
G_FILE_ATTRIBUTE_STANDARD_SIZE,
158
loader->priv->cancel,
159
stream_info_async_cb,
166
* rb_chunk_loader_start:
167
* @loader: a #RBChunkLoader
168
* @uri: the uri to load
169
* @chunk_size: maximum chunk size
171
* Starts loading data from the specified URI, passing it in chunks
172
* of at most @chunk_size to the callback.
175
rb_chunk_loader_start (RBChunkLoader *loader, const char *uri, gssize chunk_size)
177
g_assert (loader->priv->uri == NULL);
178
g_assert (loader->priv->callback != NULL);
180
loader->priv->uri = g_strdup (uri);
181
loader->priv->chunk_size = chunk_size;
182
loader->priv->chunk = g_malloc0 (chunk_size+1);
183
loader->priv->chunk_string.str = (gchar *)loader->priv->chunk;
184
loader->priv->chunk_string.len = 0;
185
loader->priv->chunk_string.allocated_len = chunk_size;
187
loader->priv->cancel = g_cancellable_new ();
189
loader->priv->file = g_file_new_for_commandline_arg (loader->priv->uri);
190
g_file_read_async (loader->priv->file,
192
loader->priv->cancel,
198
* rb_chunk_loader_cancel:
199
* @loader: a #RBChunkLoader
201
* Cancels the loading operation, ensuring that the callback
202
* will not be called again.
205
rb_chunk_loader_cancel (RBChunkLoader *loader)
207
g_cancellable_cancel (loader->priv->cancel);
211
* rb_chunk_loader_set_callback:
212
* @loader: a #RBChunkLoader
213
* @callback: the data/error callback
214
* @user_data: data to pass to the callback
215
* @destroy_data: function to call to destroy user_data
217
* Sets the loader data callback. This will be called with each
218
* chunk of data read, or with NULL to indicate the end of the file
219
* or that an error has occurred. To determine which of these is
220
* the case, call @rb_chunk_loader_get_error.
222
* This must be called before @rb_chunk_loader_start.
225
rb_chunk_loader_set_callback (RBChunkLoader *loader,
226
RBChunkLoaderCallback callback,
228
GDestroyNotify destroy_data)
230
g_assert (loader->priv->callback == NULL);
231
g_assert (loader->priv->file == NULL);
233
loader->priv->callback = callback;
234
loader->priv->callback_data = user_data;
235
loader->priv->destroy_data = destroy_data;
239
* rb_chunk_loader_get_error:
240
* @loader: a #RBChunkLoader
242
* If an error has occurred that prevents the loader from providing
243
* any further data, this function will return a #GError, otherwise
246
* Return value: loader error or NULL
249
rb_chunk_loader_get_error (RBChunkLoader *loader)
251
if (loader->priv->error)
252
return g_error_copy (loader->priv->error);
257
* rb_chunk_loader_new:
259
* Creates and returns a new #RBChunkLoader instance.
261
* Return value: #RBChunkLoader instance
264
rb_chunk_loader_new (void)
266
return RB_CHUNK_LOADER (g_object_new (RB_TYPE_CHUNK_LOADER, NULL));
270
impl_finalize (GObject *object)
272
RBChunkLoader *loader = RB_CHUNK_LOADER (object);
274
g_free (loader->priv->uri);
275
g_free (loader->priv->chunk);
276
g_clear_error (&loader->priv->error);
278
if (loader->priv->cancel) {
279
g_object_unref (loader->priv->cancel);
280
loader->priv->cancel = NULL;
283
if (loader->priv->file) {
284
g_object_unref (loader->priv->file);
285
loader->priv->file = NULL;
288
if (loader->priv->stream) {
289
g_object_unref (loader->priv->stream);
290
loader->priv->stream = NULL;
293
if (loader->priv->destroy_data) {
294
loader->priv->destroy_data (loader->priv->callback_data);
297
G_OBJECT_CLASS (rb_chunk_loader_parent_class)->finalize (object);
301
rb_chunk_loader_init (RBChunkLoader *loader)
303
loader->priv = G_TYPE_INSTANCE_GET_PRIVATE (loader, RB_TYPE_CHUNK_LOADER, RBChunkLoaderPrivate);
307
rb_chunk_loader_class_init (RBChunkLoaderClass *klass)
309
GObjectClass *object_class = G_OBJECT_CLASS (klass);
311
object_class->finalize = impl_finalize;
313
g_type_class_add_private (klass, sizeof (RBChunkLoaderPrivate));