1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.Resource;
26 
27 private import gio.InputStream;
28 private import gio.c.functions;
29 public  import gio.c.types;
30 private import glib.Bytes;
31 private import glib.ConstructionException;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.Str;
35 private import glib.c.functions;
36 private import gobject.ObjectG;
37 private import gtkd.Loader;
38 
39 
40 /**
41  * Applications and libraries often contain binary or textual data that is
42  * really part of the application, rather than user data. For instance
43  * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files,
44  * icons, etc. These are often shipped as files in `$datadir/appname`, or
45  * manually included as literal strings in the code.
46  * 
47  * The #GResource API and the [glib-compile-resources][glib-compile-resources] program
48  * provide a convenient and efficient alternative to this which has some nice properties. You
49  * maintain the files as normal files, so its easy to edit them, but during the build the files
50  * are combined into a binary bundle that is linked into the executable. This means that loading
51  * the resource files are efficient (as they are already in memory, shared with other instances) and
52  * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
53  * also makes it easier to create relocatable applications.
54  * 
55  * Resource files can also be marked as compressed. Such files will be included in the resource bundle
56  * in a compressed form, but will be automatically uncompressed when the resource is used. This
57  * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
58  * 
59  * Resource files can also be marked to be preprocessed, by setting the value of the
60  * `preprocess` attribute to a comma-separated list of preprocessing options.
61  * The only options currently supported are:
62  * 
63  * `xml-stripblanks` which will use the xmllint command
64  * to strip ignorable whitespace from the XML file. For this to work,
65  * the `XMLLINT` environment variable must be set to the full path to
66  * the xmllint executable, or xmllint must be in the `PATH`; otherwise
67  * the preprocessing step is skipped.
68  * 
69  * `to-pixdata` (deprecated since gdk-pixbuf 2.32) which will use the
70  * `gdk-pixbuf-pixdata` command to convert images to the #GdkPixdata format,
71  * which allows you to create pixbufs directly using the data inside the
72  * resource file, rather than an (uncompressed) copy of it. For this, the
73  * `gdk-pixbuf-pixdata` program must be in the `PATH`, or the
74  * `GDK_PIXBUF_PIXDATA` environment variable must be set to the full path to the
75  * `gdk-pixbuf-pixdata` executable; otherwise the resource compiler will abort.
76  * `to-pixdata` has been deprecated since gdk-pixbuf 2.32, as #GResource
77  * supports embedding modern image formats just as well. Instead of using it,
78  * embed a PNG or SVG file in your #GResource.
79  * 
80  * `json-stripblanks` which will use the `json-glib-format` command to strip
81  * ignorable whitespace from the JSON file. For this to work, the
82  * `JSON_GLIB_FORMAT` environment variable must be set to the full path to the
83  * `json-glib-format` executable, or it must be in the `PATH`;
84  * otherwise the preprocessing step is skipped. In addition, at least version
85  * 1.6 of `json-glib-format` is required.
86  * 
87  * Resource files will be exported in the GResource namespace using the
88  * combination of the given `prefix` and the filename from the `file` element.
89  * The `alias` attribute can be used to alter the filename to expose them at a
90  * different location in the resource namespace. Typically, this is used to
91  * include files from a different source directory without exposing the source
92  * directory in the resource namespace, as in the example below.
93  * 
94  * Resource bundles are created by the [glib-compile-resources][glib-compile-resources] program
95  * which takes an XML file that describes the bundle, and a set of files that the XML references. These
96  * are combined into a binary resource bundle.
97  * 
98  * An example resource description:
99  * |[
100  * <?xml version="1.0" encoding="UTF-8"?>
101  * <gresources>
102  * <gresource prefix="/org/gtk/Example">
103  * <file>data/splashscreen.png</file>
104  * <file compressed="true">dialog.ui</file>
105  * <file preprocess="xml-stripblanks">menumarkup.xml</file>
106  * <file alias="example.css">data/example.css</file>
107  * </gresource>
108  * </gresources>
109  * ]|
110  * 
111  * This will create a resource bundle with the following files:
112  * |[
113  * /org/gtk/Example/data/splashscreen.png
114  * /org/gtk/Example/dialog.ui
115  * /org/gtk/Example/menumarkup.xml
116  * /org/gtk/Example/example.css
117  * ]|
118  * 
119  * Note that all resources in the process share the same namespace, so use Java-style
120  * path prefixes (like in the above example) to avoid conflicts.
121  * 
122  * You can then use [glib-compile-resources][glib-compile-resources] to compile the XML to a
123  * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
124  * --generate-header arguments to create a source file and header to link directly into your application.
125  * This will generate `get_resource()`, `register_resource()` and
126  * `unregister_resource()` functions, prefixed by the `--c-name` argument passed
127  * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns
128  * the generated #GResource object. The register and unregister functions
129  * register the resource so its files can be accessed using
130  * g_resources_lookup_data().
131  * 
132  * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
133  * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
134  * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
135  * the resource data.
136  * 
137  * Some higher-level APIs, such as #GtkApplication, will automatically load
138  * resources from certain well-known paths in the resource namespace as a
139  * convenience. See the documentation for those APIs for details.
140  * 
141  * There are two forms of the generated source, the default version uses the compiler support for constructor
142  * and destructor functions (where available) to automatically create and register the #GResource on startup
143  * or library load time. If you pass `--manual-register`, two functions to register/unregister the resource are created
144  * instead. This requires an explicit initialization call in your application/library, but it works on all platforms,
145  * even on the minor ones where constructors are not supported. (Constructor support is available for at least Win32, Mac OS and Linux.)
146  * 
147  * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
148  * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
149  * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
150  * are for your own resources, and resource data is often used once, during parsing, and then released.
151  * 
152  * When debugging a program or testing a change to an installed version, it is often useful to be able to
153  * replace resources in the program or library, without recompiling, for debugging or quick hacking and testing
154  * purposes. Since GLib 2.50, it is possible to use the `G_RESOURCE_OVERLAYS` environment variable to selectively overlay
155  * resources with replacements from the filesystem.  It is a %G_SEARCHPATH_SEPARATOR-separated list of substitutions to perform
156  * during resource lookups. It is ignored when running in a setuid process.
157  * 
158  * A substitution has the form
159  * 
160  * |[
161  * /org/gtk/libgtk=/home/desrt/gtk-overlay
162  * ]|
163  * 
164  * The part before the `=` is the resource subpath for which the overlay applies.  The part after is a
165  * filesystem path which contains files and subdirectories as you would like to be loaded as resources with the
166  * equivalent names.
167  * 
168  * In the example above, if an application tried to load a resource with the resource path
169  * `/org/gtk/libgtk/ui/gtkdialog.ui` then GResource would check the filesystem path
170  * `/home/desrt/gtk-overlay/ui/gtkdialog.ui`.  If a file was found there, it would be used instead.  This is an
171  * overlay, not an outright replacement, which means that if a file is not found at that path, the built-in
172  * version will be used instead.  Whiteouts are not currently supported.
173  * 
174  * Substitutions must start with a slash, and must not contain a trailing slash before the '='.  The path after
175  * the slash should ideally be absolute, but this is not strictly required.  It is possible to overlay the
176  * location of a single resource with an individual file.
177  *
178  * Since: 2.32
179  */
180 public class Resource
181 {
182 	/** the main Gtk struct */
183 	protected GResource* gResource;
184 	protected bool ownedRef;
185 
186 	/** Get the main Gtk struct */
187 	public GResource* getResourceStruct(bool transferOwnership = false)
188 	{
189 		if (transferOwnership)
190 			ownedRef = false;
191 		return gResource;
192 	}
193 
194 	/** the main Gtk struct as a void* */
195 	protected void* getStruct()
196 	{
197 		return cast(void*)gResource;
198 	}
199 
200 	/**
201 	 * Sets our main struct and passes it to the parent class.
202 	 */
203 	public this (GResource* gResource, bool ownedRef = false)
204 	{
205 		this.gResource = gResource;
206 		this.ownedRef = ownedRef;
207 	}
208 
209 	~this ()
210 	{
211 		if ( Linker.isLoaded(LIBRARY_GIO) && ownedRef )
212 			g_resource_unref(gResource);
213 	}
214 
215 
216 	/** */
217 	public static GType getType()
218 	{
219 		return g_resource_get_type();
220 	}
221 
222 	/**
223 	 * Creates a GResource from a reference to the binary resource bundle.
224 	 * This will keep a reference to @data while the resource lives, so
225 	 * the data should not be modified or freed.
226 	 *
227 	 * If you want to use this resource in the global resource namespace you need
228 	 * to register it with g_resources_register().
229 	 *
230 	 * Note: @data must be backed by memory that is at least pointer aligned.
231 	 * Otherwise this function will internally create a copy of the memory since
232 	 * GLib 2.56, or in older versions fail and exit the process.
233 	 *
234 	 * If @data is empty or corrupt, %G_RESOURCE_ERROR_INTERNAL will be returned.
235 	 *
236 	 * Params:
237 	 *     data = A #GBytes
238 	 *
239 	 * Returns: a new #GResource, or %NULL on error
240 	 *
241 	 * Since: 2.32
242 	 *
243 	 * Throws: GException on failure.
244 	 * Throws: ConstructionException GTK+ fails to create the object.
245 	 */
246 	public this(Bytes data)
247 	{
248 		GError* err = null;
249 
250 		auto __p = g_resource_new_from_data((data is null) ? null : data.getBytesStruct(), &err);
251 
252 		if (err !is null)
253 		{
254 			throw new GException( new ErrorG(err) );
255 		}
256 
257 		if(__p is null)
258 		{
259 			throw new ConstructionException("null returned by new_from_data");
260 		}
261 
262 		this(cast(GResource*) __p);
263 	}
264 
265 	/**
266 	 * Registers the resource with the process-global set of resources.
267 	 * Once a resource is registered the files in it can be accessed
268 	 * with the global resource lookup functions like g_resources_lookup_data().
269 	 *
270 	 * Params:
271 	 *     resource = A #GResource
272 	 *
273 	 * Since: 2.32
274 	 */
275 	public static void register(Resource resource)
276 	{
277 		g_resources_register((resource is null) ? null : resource.getResourceStruct());
278 	}
279 
280 	/**
281 	 * Unregisters the resource from the process-global set of resources.
282 	 *
283 	 * Params:
284 	 *     resource = A #GResource
285 	 *
286 	 * Since: 2.32
287 	 */
288 	public static void unregister(Resource resource)
289 	{
290 		g_resources_unregister((resource is null) ? null : resource.getResourceStruct());
291 	}
292 
293 	/**
294 	 * Returns all the names of children at the specified @path in the resource.
295 	 * The return result is a %NULL terminated list of strings which should
296 	 * be released with g_strfreev().
297 	 *
298 	 * If @path is invalid or does not exist in the #GResource,
299 	 * %G_RESOURCE_ERROR_NOT_FOUND will be returned.
300 	 *
301 	 * @lookup_flags controls the behaviour of the lookup.
302 	 *
303 	 * Params:
304 	 *     path = A pathname inside the resource
305 	 *     lookupFlags = A #GResourceLookupFlags
306 	 *
307 	 * Returns: an array of constant strings
308 	 *
309 	 * Since: 2.32
310 	 *
311 	 * Throws: GException on failure.
312 	 */
313 	public string[] enumerateChildren(string path, GResourceLookupFlags lookupFlags)
314 	{
315 		GError* err = null;
316 
317 		auto retStr = g_resource_enumerate_children(gResource, Str.toStringz(path), lookupFlags, &err);
318 
319 		if (err !is null)
320 		{
321 			throw new GException( new ErrorG(err) );
322 		}
323 
324 		scope(exit) Str.freeStringArray(retStr);
325 		return Str.toStringArray(retStr);
326 	}
327 
328 	/**
329 	 * Looks for a file at the specified @path in the resource and
330 	 * if found returns information about it.
331 	 *
332 	 * @lookup_flags controls the behaviour of the lookup.
333 	 *
334 	 * Params:
335 	 *     path = A pathname inside the resource
336 	 *     lookupFlags = A #GResourceLookupFlags
337 	 *     size = a location to place the length of the contents of the file,
338 	 *         or %NULL if the length is not needed
339 	 *     flags = a location to place the flags about the file,
340 	 *         or %NULL if the length is not needed
341 	 *
342 	 * Returns: %TRUE if the file was found. %FALSE if there were errors
343 	 *
344 	 * Since: 2.32
345 	 *
346 	 * Throws: GException on failure.
347 	 */
348 	public bool getInfo(string path, GResourceLookupFlags lookupFlags, out size_t size, out uint flags)
349 	{
350 		GError* err = null;
351 
352 		auto __p = g_resource_get_info(gResource, Str.toStringz(path), lookupFlags, &size, &flags, &err) != 0;
353 
354 		if (err !is null)
355 		{
356 			throw new GException( new ErrorG(err) );
357 		}
358 
359 		return __p;
360 	}
361 
362 	/**
363 	 * Looks for a file at the specified @path in the resource and
364 	 * returns a #GBytes that lets you directly access the data in
365 	 * memory.
366 	 *
367 	 * The data is always followed by a zero byte, so you
368 	 * can safely use the data as a C string. However, that byte
369 	 * is not included in the size of the GBytes.
370 	 *
371 	 * For uncompressed resource files this is a pointer directly into
372 	 * the resource bundle, which is typically in some readonly data section
373 	 * in the program binary. For compressed files we allocate memory on
374 	 * the heap and automatically uncompress the data.
375 	 *
376 	 * @lookup_flags controls the behaviour of the lookup.
377 	 *
378 	 * Params:
379 	 *     path = A pathname inside the resource
380 	 *     lookupFlags = A #GResourceLookupFlags
381 	 *
382 	 * Returns: #GBytes or %NULL on error.
383 	 *     Free the returned object with g_bytes_unref()
384 	 *
385 	 * Since: 2.32
386 	 *
387 	 * Throws: GException on failure.
388 	 */
389 	public Bytes lookupData(string path, GResourceLookupFlags lookupFlags)
390 	{
391 		GError* err = null;
392 
393 		auto __p = g_resource_lookup_data(gResource, Str.toStringz(path), lookupFlags, &err);
394 
395 		if (err !is null)
396 		{
397 			throw new GException( new ErrorG(err) );
398 		}
399 
400 		if(__p is null)
401 		{
402 			return null;
403 		}
404 
405 		return new Bytes(cast(GBytes*) __p, true);
406 	}
407 
408 	/**
409 	 * Looks for a file at the specified @path in the resource and
410 	 * returns a #GInputStream that lets you read the data.
411 	 *
412 	 * @lookup_flags controls the behaviour of the lookup.
413 	 *
414 	 * Params:
415 	 *     path = A pathname inside the resource
416 	 *     lookupFlags = A #GResourceLookupFlags
417 	 *
418 	 * Returns: #GInputStream or %NULL on error.
419 	 *     Free the returned object with g_object_unref()
420 	 *
421 	 * Since: 2.32
422 	 *
423 	 * Throws: GException on failure.
424 	 */
425 	public InputStream openStream(string path, GResourceLookupFlags lookupFlags)
426 	{
427 		GError* err = null;
428 
429 		auto __p = g_resource_open_stream(gResource, Str.toStringz(path), lookupFlags, &err);
430 
431 		if (err !is null)
432 		{
433 			throw new GException( new ErrorG(err) );
434 		}
435 
436 		if(__p is null)
437 		{
438 			return null;
439 		}
440 
441 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) __p, true);
442 	}
443 
444 	alias doref = ref_;
445 	/**
446 	 * Atomically increments the reference count of @resource by one. This
447 	 * function is MT-safe and may be called from any thread.
448 	 *
449 	 * Returns: The passed in #GResource
450 	 *
451 	 * Since: 2.32
452 	 */
453 	public Resource ref_()
454 	{
455 		auto __p = g_resource_ref(gResource);
456 
457 		if(__p is null)
458 		{
459 			return null;
460 		}
461 
462 		return ObjectG.getDObject!(Resource)(cast(GResource*) __p, true);
463 	}
464 
465 	/**
466 	 * Atomically decrements the reference count of @resource by one. If the
467 	 * reference count drops to 0, all memory allocated by the resource is
468 	 * released. This function is MT-safe and may be called from any
469 	 * thread.
470 	 *
471 	 * Since: 2.32
472 	 */
473 	public void unref()
474 	{
475 		g_resource_unref(gResource);
476 	}
477 
478 	/**
479 	 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
480 	 * you to query it for data.
481 	 *
482 	 * If you want to use this resource in the global resource namespace you need
483 	 * to register it with g_resources_register().
484 	 *
485 	 * If @filename is empty or the data in it is corrupt,
486 	 * %G_RESOURCE_ERROR_INTERNAL will be returned. If @filename doesn’t exist, or
487 	 * there is an error in reading it, an error from g_mapped_file_new() will be
488 	 * returned.
489 	 *
490 	 * Params:
491 	 *     filename = the path of a filename to load, in the GLib filename encoding
492 	 *
493 	 * Returns: a new #GResource, or %NULL on error
494 	 *
495 	 * Since: 2.32
496 	 *
497 	 * Throws: GException on failure.
498 	 */
499 	public static Resource load(string filename)
500 	{
501 		GError* err = null;
502 
503 		auto __p = g_resource_load(Str.toStringz(filename), &err);
504 
505 		if (err !is null)
506 		{
507 			throw new GException( new ErrorG(err) );
508 		}
509 
510 		if(__p is null)
511 		{
512 			return null;
513 		}
514 
515 		return ObjectG.getDObject!(Resource)(cast(GResource*) __p, true);
516 	}
517 
518 	/**
519 	 * Returns all the names of children at the specified @path in the set of
520 	 * globally registered resources.
521 	 * The return result is a %NULL terminated list of strings which should
522 	 * be released with g_strfreev().
523 	 *
524 	 * @lookup_flags controls the behaviour of the lookup.
525 	 *
526 	 * Params:
527 	 *     path = A pathname inside the resource
528 	 *     lookupFlags = A #GResourceLookupFlags
529 	 *
530 	 * Returns: an array of constant strings
531 	 *
532 	 * Since: 2.32
533 	 *
534 	 * Throws: GException on failure.
535 	 */
536 	public static string[] resourcesEnumerateChildren(string path, GResourceLookupFlags lookupFlags)
537 	{
538 		GError* err = null;
539 
540 		auto retStr = g_resources_enumerate_children(Str.toStringz(path), lookupFlags, &err);
541 
542 		if (err !is null)
543 		{
544 			throw new GException( new ErrorG(err) );
545 		}
546 
547 		scope(exit) Str.freeStringArray(retStr);
548 		return Str.toStringArray(retStr);
549 	}
550 
551 	/**
552 	 * Looks for a file at the specified @path in the set of
553 	 * globally registered resources and if found returns information about it.
554 	 *
555 	 * @lookup_flags controls the behaviour of the lookup.
556 	 *
557 	 * Params:
558 	 *     path = A pathname inside the resource
559 	 *     lookupFlags = A #GResourceLookupFlags
560 	 *     size = a location to place the length of the contents of the file,
561 	 *         or %NULL if the length is not needed
562 	 *     flags = a location to place the #GResourceFlags about the file,
563 	 *         or %NULL if the flags are not needed
564 	 *
565 	 * Returns: %TRUE if the file was found. %FALSE if there were errors
566 	 *
567 	 * Since: 2.32
568 	 *
569 	 * Throws: GException on failure.
570 	 */
571 	public static bool resourcesGetInfo(string path, GResourceLookupFlags lookupFlags, out size_t size, out uint flags)
572 	{
573 		GError* err = null;
574 
575 		auto __p = g_resources_get_info(Str.toStringz(path), lookupFlags, &size, &flags, &err) != 0;
576 
577 		if (err !is null)
578 		{
579 			throw new GException( new ErrorG(err) );
580 		}
581 
582 		return __p;
583 	}
584 
585 	/**
586 	 * Looks for a file at the specified @path in the set of
587 	 * globally registered resources and returns a #GBytes that
588 	 * lets you directly access the data in memory.
589 	 *
590 	 * The data is always followed by a zero byte, so you
591 	 * can safely use the data as a C string. However, that byte
592 	 * is not included in the size of the GBytes.
593 	 *
594 	 * For uncompressed resource files this is a pointer directly into
595 	 * the resource bundle, which is typically in some readonly data section
596 	 * in the program binary. For compressed files we allocate memory on
597 	 * the heap and automatically uncompress the data.
598 	 *
599 	 * @lookup_flags controls the behaviour of the lookup.
600 	 *
601 	 * Params:
602 	 *     path = A pathname inside the resource
603 	 *     lookupFlags = A #GResourceLookupFlags
604 	 *
605 	 * Returns: #GBytes or %NULL on error.
606 	 *     Free the returned object with g_bytes_unref()
607 	 *
608 	 * Since: 2.32
609 	 *
610 	 * Throws: GException on failure.
611 	 */
612 	public static Bytes resourcesLookupData(string path, GResourceLookupFlags lookupFlags)
613 	{
614 		GError* err = null;
615 
616 		auto __p = g_resources_lookup_data(Str.toStringz(path), lookupFlags, &err);
617 
618 		if (err !is null)
619 		{
620 			throw new GException( new ErrorG(err) );
621 		}
622 
623 		if(__p is null)
624 		{
625 			return null;
626 		}
627 
628 		return new Bytes(cast(GBytes*) __p, true);
629 	}
630 
631 	/**
632 	 * Looks for a file at the specified @path in the set of
633 	 * globally registered resources and returns a #GInputStream
634 	 * that lets you read the data.
635 	 *
636 	 * @lookup_flags controls the behaviour of the lookup.
637 	 *
638 	 * Params:
639 	 *     path = A pathname inside the resource
640 	 *     lookupFlags = A #GResourceLookupFlags
641 	 *
642 	 * Returns: #GInputStream or %NULL on error.
643 	 *     Free the returned object with g_object_unref()
644 	 *
645 	 * Since: 2.32
646 	 *
647 	 * Throws: GException on failure.
648 	 */
649 	public static InputStream resourcesOpenStream(string path, GResourceLookupFlags lookupFlags)
650 	{
651 		GError* err = null;
652 
653 		auto __p = g_resources_open_stream(Str.toStringz(path), lookupFlags, &err);
654 
655 		if (err !is null)
656 		{
657 			throw new GException( new ErrorG(err) );
658 		}
659 
660 		if(__p is null)
661 		{
662 			return null;
663 		}
664 
665 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) __p, true);
666 	}
667 }