No issues found
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
4 *
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.
9 *
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.
17 *
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.
22 *
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.
26 *
27 */
28
29 #include "config.h"
30
31 #include <lib/rb-chunk-loader.h>
32 #include <lib/rb-debug.h>
33
34 /**
35 * SECTION:rb-chunk-loader
36 * @short_description: simple utility for asynchronously fetching data by URL in chunks
37 *
38 */
39
40
41 static void rb_chunk_loader_class_init (RBChunkLoaderClass *klass);
42 static void rb_chunk_loader_init (RBChunkLoader *loader);
43
44 struct _RBChunkLoaderPrivate
45 {
46 char *uri;
47 gssize chunk_size;
48 guint8 *chunk;
49 GString chunk_string;
50 guint64 total;
51
52 GError *error;
53 GFile *file;
54 GFileInputStream *stream;
55 GCancellable *cancel;
56
57 RBChunkLoaderCallback callback;
58 gpointer callback_data;
59 GDestroyNotify destroy_data;
60 };
61
62 G_DEFINE_TYPE (RBChunkLoader, rb_chunk_loader, G_TYPE_OBJECT);
63
64 static void
65 stream_close_cb (GObject *obj, GAsyncResult *res, gpointer data)
66 {
67 GError *error = NULL;
68
69 g_input_stream_close_finish (G_INPUT_STREAM (obj), res, &error);
70
71 if (error != NULL) {
72 rb_debug ("unable to close input stream: %s", error->message);
73 g_clear_error (&error);
74 }
75 }
76
77 static void
78 cleanup (RBChunkLoader *loader)
79 {
80 g_input_stream_close_async (G_INPUT_STREAM (loader->priv->stream),
81 G_PRIORITY_DEFAULT,
82 loader->priv->cancel,
83 stream_close_cb,
84 loader);
85 }
86
87 static void
88 stream_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
89 {
90 RBChunkLoader *loader = RB_CHUNK_LOADER (data);
91 gssize done;
92
93 done = g_input_stream_read_finish (G_INPUT_STREAM (obj),
94 res,
95 &loader->priv->error);
96 if (done == -1) {
97 rb_debug ("error reading from stream: %s", loader->priv->error->message);
98 loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
99 cleanup (loader);
100 } else if (done == 0) {
101 rb_debug ("reached end up input stream");
102 loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
103 cleanup (loader);
104 } else {
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),
108 loader->priv->chunk,
109 loader->priv->chunk_size,
110 G_PRIORITY_DEFAULT,
111 loader->priv->cancel,
112 stream_read_async_cb,
113 loader);
114 }
115 }
116
117 static void
118 stream_info_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
119 {
120 RBChunkLoader *loader = RB_CHUNK_LOADER (data);
121 GFileInfo *info;
122 GError *error = NULL;
123
124 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (obj), res, &error);
125 if (info != NULL) {
126 loader->priv->total = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
127 } else {
128 loader->priv->total = 0;
129 rb_debug ("couldn't get size of source file: %s", error->message);
130 g_clear_error (&error);
131 }
132
133 g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
134 loader->priv->chunk,
135 loader->priv->chunk_size,
136 G_PRIORITY_DEFAULT,
137 loader->priv->cancel,
138 stream_read_async_cb,
139 loader);
140 }
141
142 static void
143 file_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
144 {
145 RBChunkLoader *loader = RB_CHUNK_LOADER (data);
146
147 loader->priv->stream = g_file_read_finish (G_FILE (obj),
148 res,
149 &loader->priv->error);
150 if (loader->priv->error != NULL) {
151 loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
152 return;
153 }
154
155 g_file_input_stream_query_info_async (loader->priv->stream,
156 G_FILE_ATTRIBUTE_STANDARD_SIZE,
157 G_PRIORITY_DEFAULT,
158 loader->priv->cancel,
159 stream_info_async_cb,
160 loader);
161
162
163 }
164
165 /**
166 * rb_chunk_loader_start:
167 * @loader: a #RBChunkLoader
168 * @uri: the uri to load
169 * @chunk_size: maximum chunk size
170 *
171 * Starts loading data from the specified URI, passing it in chunks
172 * of at most @chunk_size to the callback.
173 */
174 void
175 rb_chunk_loader_start (RBChunkLoader *loader, const char *uri, gssize chunk_size)
176 {
177 g_assert (loader->priv->uri == NULL);
178 g_assert (loader->priv->callback != NULL);
179
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;
186
187 loader->priv->cancel = g_cancellable_new ();
188
189 loader->priv->file = g_file_new_for_commandline_arg (loader->priv->uri);
190 g_file_read_async (loader->priv->file,
191 G_PRIORITY_DEFAULT,
192 loader->priv->cancel,
193 file_read_async_cb,
194 loader);
195 }
196
197 /**
198 * rb_chunk_loader_cancel:
199 * @loader: a #RBChunkLoader
200 *
201 * Cancels the loading operation, ensuring that the callback
202 * will not be called again.
203 */
204 void
205 rb_chunk_loader_cancel (RBChunkLoader *loader)
206 {
207 g_cancellable_cancel (loader->priv->cancel);
208 }
209
210 /**
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
216 *
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.
221 *
222 * This must be called before @rb_chunk_loader_start.
223 */
224 void
225 rb_chunk_loader_set_callback (RBChunkLoader *loader,
226 RBChunkLoaderCallback callback,
227 gpointer user_data,
228 GDestroyNotify destroy_data)
229 {
230 g_assert (loader->priv->callback == NULL);
231 g_assert (loader->priv->file == NULL);
232
233 loader->priv->callback = callback;
234 loader->priv->callback_data = user_data;
235 loader->priv->destroy_data = destroy_data;
236 }
237
238 /**
239 * rb_chunk_loader_get_error:
240 * @loader: a #RBChunkLoader
241 *
242 * If an error has occurred that prevents the loader from providing
243 * any further data, this function will return a #GError, otherwise
244 * NULL.
245 *
246 * Return value: loader error or NULL
247 */
248 GError *
249 rb_chunk_loader_get_error (RBChunkLoader *loader)
250 {
251 if (loader->priv->error)
252 return g_error_copy (loader->priv->error);
253 return NULL;
254 }
255
256 /**
257 * rb_chunk_loader_new:
258 *
259 * Creates and returns a new #RBChunkLoader instance.
260 *
261 * Return value: #RBChunkLoader instance
262 */
263 RBChunkLoader *
264 rb_chunk_loader_new (void)
265 {
266 return RB_CHUNK_LOADER (g_object_new (RB_TYPE_CHUNK_LOADER, NULL));
267 }
268
269 static void
270 impl_finalize (GObject *object)
271 {
272 RBChunkLoader *loader = RB_CHUNK_LOADER (object);
273
274 g_free (loader->priv->uri);
275 g_free (loader->priv->chunk);
276 g_clear_error (&loader->priv->error);
277
278 if (loader->priv->cancel) {
279 g_object_unref (loader->priv->cancel);
280 loader->priv->cancel = NULL;
281 }
282
283 if (loader->priv->file) {
284 g_object_unref (loader->priv->file);
285 loader->priv->file = NULL;
286 }
287
288 if (loader->priv->stream) {
289 g_object_unref (loader->priv->stream);
290 loader->priv->stream = NULL;
291 }
292
293 if (loader->priv->destroy_data) {
294 loader->priv->destroy_data (loader->priv->callback_data);
295 }
296
297 G_OBJECT_CLASS (rb_chunk_loader_parent_class)->finalize (object);
298 }
299
300 static void
301 rb_chunk_loader_init (RBChunkLoader *loader)
302 {
303 loader->priv = G_TYPE_INSTANCE_GET_PRIVATE (loader, RB_TYPE_CHUNK_LOADER, RBChunkLoaderPrivate);
304 }
305
306 static void
307 rb_chunk_loader_class_init (RBChunkLoaderClass *klass)
308 {
309 GObjectClass *object_class = G_OBJECT_CLASS (klass);
310
311 object_class->finalize = impl_finalize;
312
313 g_type_class_add_private (klass, sizeof (RBChunkLoaderPrivate));
314 }