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  * Conversion parameters:
26  * inFile  = gio-GResource.html
27  * outPack = gio
28  * outFile = Resource
29  * strct   = GResource
30  * realStrct=
31  * ctorStrct=
32  * clss    = Resource
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_permission_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.ErrorG
48  * 	- glib.GException
49  * 	- glib.Bytes
50  * 	- gio.InputStream
51  * 	- gtkc.Loader
52  * 	- gtkc.paths
53  * structWrap:
54  * 	- GBytes* -> Bytes
55  * 	- GInputStream* -> InputStream
56  * 	- GResource* -> Resource
57  * module aliases:
58  * local aliases:
59  * overrides:
60  */
61 
62 module gio.Resource;
63 
64 public  import gtkc.giotypes;
65 
66 private import gtkc.gio;
67 private import glib.ConstructionException;
68 private import gobject.ObjectG;
69 
70 private import glib.Str;
71 private import glib.ErrorG;
72 private import glib.GException;
73 private import glib.Bytes;
74 private import gio.InputStream;
75 private import gtkc.Loader;
76 private import gtkc.paths;
77 
78 
79 
80 /**
81  * Applications and libraries often contain binary or textual data that is really part of the
82  * application, rather than user data. For instance GtkBuilder .ui files, splashscreen images,
83  * GMenu markup xml, CSS files, icons, etc. These are often shipped as files in $datadir/appname, or
84  * manually included as literal strings in the code.
85  *
86  * The GResource API and the glib-compile-resources program
87  * provide a convenient and efficient alternative to this which has some nice properties. You
88  * maintain the files as normal files, so its easy to edit them, but during the build the files
89  * are combined into a binary bundle that is linked into the executable. This means that loading
90  * the resource files are efficient (as they are already in memory, shared with other instances) and
91  * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
92  * also makes it easier to create relocatable applications.
93  *
94  * Resource files can also be marked as compressed. Such files will be included in the resource bundle
95  * in a compressed form, but will be automatically uncompressed when the resource is used. This
96  * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
97  *
98  * Resource files can also be marked to be preprocessed, by setting the value of the
99  * preprocess attribute to a comma-separated list of preprocessing options.
100  * The only options currently supported are:
101  *
102  * xml-stripblanks which will use xmllint to strip
103  * ignorable whitespace from the xml file. For this to work, the XMLLINT
104  * environment variable must be set to the full path to the xmllint executable, or xmllint
105  * must be in the PATH; otherwise the preprocessing step is skipped.
106  *
107  * to-pixdata which will use gdk-pixbuf-pixdata to convert
108  * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside
109  * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata
110  * program must be in the PATH, or the GDK_PIXBUF_PIXDATA environment variable must be
111  * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will
112  * abort.
113  *
114  * Resource bundles are created by the glib-compile-resources program
115  * which takes an xml file that describes the bundle, and a set of files that the xml references. These
116  * are combined into a binary resource bundle.
117  *
118  * $(DDOC_COMMENT example)
119  *
120  * This will create a resource bundle with the following files:
121  *
122  * /org/gtk/Example/data/splashscreen.png
123  * /org/gtk/Example/dialog.ui
124  * /org/gtk/Example/menumarkup.xml
125  *
126  * Note that all resources in the process share the same namespace, so use java-style
127  * path prefixes (like in the above example) to avoid conflicts.
128  *
129  * You can then use glib-compile-resources to compile the xml to a
130  * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
131  * --generate-header arguments to create a source file and header to link directly into your application.
132  *
133  * Once a GResource has been created and registered all the data in it can be accessed globally in the process by
134  * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
135  * to the data. You can also use uris like "resource:///org/gtk/Example/data/splashscreen.png" with GFile to access
136  * the resource data.
137  *
138  * There are two forms of the generated source, the default version uses the compiler support for constructor
139  * and destructor functions (where available) to automatically create and register the GResource on startup
140  * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
141  * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
142  * even on the minor ones where this is not available. (Constructor support is available for at least Win32, MacOS and Linux.)
143  *
144  * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
145  * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
146  * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
147  * is for your own resources, and resource data is often used once, during parsing, and then released.
148  */
149 public class Resource
150 {
151 	
152 	/** the main Gtk struct */
153 	protected GResource* gResource;
154 	
155 	
156 	/** Get the main Gtk struct */
157 	public GResource* getResourceStruct()
158 	{
159 		return gResource;
160 	}
161 	
162 	
163 	/** the main Gtk struct as a void* */
164 	protected void* getStruct()
165 	{
166 		return cast(void*)gResource;
167 	}
168 	
169 	/**
170 	 * Sets our main struct and passes it to the parent class
171 	 */
172 	public this (GResource* gResource)
173 	{
174 		this.gResource = gResource;
175 	}
176 	
177 	~this()
178 	{
179 		if ( Linker.isLoaded(LIBRARY.GIO) && gResource != null)
180 		{
181 			g_resource_unref(gResource);
182 		}
183 	}
184 	
185 	/**
186 	 */
187 	
188 	/**
189 	 * Loads a binary resource bundle and creates a GResource representation of it, allowing
190 	 * you to query it for data.
191 	 * If you want to use this resource in the global resource namespace you need
192 	 * to register it with g_resources_register().
193 	 * Since 2.32
194 	 * Params:
195 	 * filename = the path of a filename to load, in the GLib filename encoding. [type filename]
196 	 * Returns: a new GResource, or NULL on error. [transfer full]
197 	 * Throws: GException on failure.
198 	 */
199 	public static Resource gResourceLoad(string filename)
200 	{
201 		// GResource * g_resource_load (const gchar *filename,  GError **error);
202 		GError* err = null;
203 		
204 		auto p = g_resource_load(Str.toStringz(filename), &err);
205 		
206 		if (err !is null)
207 		{
208 			throw new GException( new ErrorG(err) );
209 		}
210 		
211 		
212 		if(p is null)
213 		{
214 			return null;
215 		}
216 		
217 		return ObjectG.getDObject!(Resource)(cast(GResource*) p);
218 	}
219 	
220 	/**
221 	 * Creates a GResource from a reference to the binary resource bundle.
222 	 * This will keep a reference to data while the resource lives, so
223 	 * the data should not be modified or freed.
224 	 * If you want to use this resource in the global resource namespace you need
225 	 * to register it with g_resources_register().
226 	 * Since 2.32
227 	 * Params:
228 	 * data = A GBytes
229 	 * Returns: a new GResource, or NULL on error. [transfer full]
230 	 * Throws: GException on failure.
231 	 */
232 	public static Resource gResourceNewFromData(Bytes data)
233 	{
234 		// GResource * g_resource_new_from_data (GBytes *data,  GError **error);
235 		GError* err = null;
236 		
237 		auto p = g_resource_new_from_data((data is null) ? null : data.getBytesStruct(), &err);
238 		
239 		if (err !is null)
240 		{
241 			throw new GException( new ErrorG(err) );
242 		}
243 		
244 		
245 		if(p is null)
246 		{
247 			return null;
248 		}
249 		
250 		return ObjectG.getDObject!(Resource)(cast(GResource*) p);
251 	}
252 	
253 	/**
254 	 * Atomically increments the reference count of array by one. This
255 	 * function is MT-safe and may be called from any thread.
256 	 * Since 2.32
257 	 * Returns: The passed in GResource
258 	 */
259 	public Resource gResourceRef()
260 	{
261 		// GResource * g_resource_ref (GResource *resource);
262 		auto p = g_resource_ref(gResource);
263 		
264 		if(p is null)
265 		{
266 			return null;
267 		}
268 		
269 		return ObjectG.getDObject!(Resource)(cast(GResource*) p);
270 	}
271 	
272 	/**
273 	 * Atomically decrements the reference count of resource by one. If the
274 	 * reference count drops to 0, all memory allocated by the array is
275 	 * released. This function is MT-safe and may be called from any
276 	 * thread.
277 	 * Since 2.32
278 	 */
279 	public void gResourceUnref()
280 	{
281 		// void g_resource_unref (GResource *resource);
282 		g_resource_unref(gResource);
283 	}
284 	
285 	/**
286 	 * Looks for a file at the specified path in the resource and
287 	 * returns a GBytes that lets you directly access the data in
288 	 * memory.
289 	 * The data is always followed by a zero byte, so you
290 	 * can safely use the data as a C string. However, that byte
291 	 * is not included in the size of the GBytes.
292 	 * For uncompressed resource files this is a pointer directly into
293 	 * the resource bundle, which is typically in some readonly data section
294 	 * in the program binary. For compressed files we allocate memory on
295 	 * the heap and automatically uncompress the data.
296 	 * lookup_flags controls the behaviour of the lookup.
297 	 * Since 2.32
298 	 * Params:
299 	 * path = A pathname inside the resource
300 	 * lookupFlags = A GResourceLookupFlags
301 	 * Returns: GBytes or NULL on error. Free the returned object with g_bytes_unref(). [transfer full]
302 	 * Throws: GException on failure.
303 	 */
304 	public Bytes gResourceLookupData(string path, GResourceLookupFlags lookupFlags)
305 	{
306 		// GBytes * g_resource_lookup_data (GResource *resource,  const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
307 		GError* err = null;
308 		
309 		auto p = g_resource_lookup_data(gResource, Str.toStringz(path), lookupFlags, &err);
310 		
311 		if (err !is null)
312 		{
313 			throw new GException( new ErrorG(err) );
314 		}
315 		
316 		
317 		if(p is null)
318 		{
319 			return null;
320 		}
321 		
322 		return ObjectG.getDObject!(Bytes)(cast(GBytes*) p);
323 	}
324 	
325 	/**
326 	 * Looks for a file at the specified path in the resource and
327 	 * returns a GInputStream that lets you read the data.
328 	 * lookup_flags controls the behaviour of the lookup.
329 	 * Since 2.32
330 	 * Params:
331 	 * path = A pathname inside the resource
332 	 * lookupFlags = A GResourceLookupFlags
333 	 * Returns: GInputStream or NULL on error. Free the returned object with g_object_unref(). [transfer full]
334 	 * Throws: GException on failure.
335 	 */
336 	public InputStream gResourceOpenStream(string path, GResourceLookupFlags lookupFlags)
337 	{
338 		// GInputStream * g_resource_open_stream (GResource *resource,  const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
339 		GError* err = null;
340 		
341 		auto p = g_resource_open_stream(gResource, Str.toStringz(path), lookupFlags, &err);
342 		
343 		if (err !is null)
344 		{
345 			throw new GException( new ErrorG(err) );
346 		}
347 		
348 		
349 		if(p is null)
350 		{
351 			return null;
352 		}
353 		
354 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) p);
355 	}
356 	
357 	/**
358 	 * Returns all the names of children at the specified path in the resource.
359 	 * The return result is a NULL terminated list of strings which should
360 	 * be released with g_strfreev().
361 	 * lookup_flags controls the behaviour of the lookup.
362 	 * Since 2.32
363 	 * Params:
364 	 * path = A pathname inside the resource
365 	 * lookupFlags = A GResourceLookupFlags
366 	 * Returns: an array of constant strings. [array zero-terminated=1][transfer full]
367 	 * Throws: GException on failure.
368 	 */
369 	public string[] gResourceEnumerateChildren(string path, GResourceLookupFlags lookupFlags)
370 	{
371 		// char ** g_resource_enumerate_children (GResource *resource,  const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
372 		GError* err = null;
373 		
374 		auto p = g_resource_enumerate_children(gResource, Str.toStringz(path), lookupFlags, &err);
375 		
376 		if (err !is null)
377 		{
378 			throw new GException( new ErrorG(err) );
379 		}
380 		
381 		return Str.toStringArray(p);
382 	}
383 	
384 	/**
385 	 * Looks for a file at the specified path in the resource and
386 	 * if found returns information about it.
387 	 * lookup_flags controls the behaviour of the lookup.
388 	 * Since 2.32
389 	 * Params:
390 	 * path = A pathname inside the resource
391 	 * lookupFlags = A GResourceLookupFlags
392 	 * size = a location to place the length of the contents of the file,
393 	 * or NULL if the length is not needed. [out][allow-none]
394 	 * flags = a location to place the flags about the file,
395 	 * or NULL if the length is not needed. [out][allow-none]
396 	 * Returns: TRUE if the file was found. FALSE if there were errors
397 	 * Throws: GException on failure.
398 	 */
399 	public int gResourceGetInfo(string path, GResourceLookupFlags lookupFlags, out gsize size, out uint flags)
400 	{
401 		// gboolean g_resource_get_info (GResource *resource,  const char *path,  GResourceLookupFlags lookup_flags,  gsize *size,  guint32 *flags,  GError **error);
402 		GError* err = null;
403 		
404 		auto p = g_resource_get_info(gResource, Str.toStringz(path), lookupFlags, &size, &flags, &err);
405 		
406 		if (err !is null)
407 		{
408 			throw new GException( new ErrorG(err) );
409 		}
410 		
411 		return p;
412 	}
413 	
414 	/**
415 	 * Initializes a GResource from static data using a
416 	 * GStaticResource.
417 	 * This is normally used by code generated by
418 	 * glib-compile-resources
419 	 * and is not typically used by other code.
420 	 * Since 2.32
421 	 * Params:
422 	 * staticResource = pointer to a static GStaticResource
423 	 */
424 	public static void gStaticResourceInit(GStaticResource* staticResource)
425 	{
426 		// void g_static_resource_init (GStaticResource *static_resource);
427 		g_static_resource_init(staticResource);
428 	}
429 	
430 	/**
431 	 * Finalized a GResource initialized by g_static_resource_init().
432 	 * This is normally used by code generated by
433 	 * glib-compile-resources
434 	 * and is not typically used by other code.
435 	 * Since 2.32
436 	 * Params:
437 	 * staticResource = pointer to a static GStaticResource
438 	 */
439 	public static void gStaticResourceFini(GStaticResource* staticResource)
440 	{
441 		// void g_static_resource_fini (GStaticResource *static_resource);
442 		g_static_resource_fini(staticResource);
443 	}
444 	
445 	/**
446 	 * Gets the GResource that was registered by a call to g_static_resource_init().
447 	 * This is normally used by code generated by
448 	 * glib-compile-resources
449 	 * and is not typically used by other code.
450 	 * Since 2.32
451 	 * Params:
452 	 * staticResource = pointer to a static GStaticResource
453 	 * Returns: a GResource. [transfer none]
454 	 */
455 	public static Resource gStaticResourceGetResource(GStaticResource* staticResource)
456 	{
457 		// GResource * g_static_resource_get_resource (GStaticResource *static_resource);
458 		auto p = g_static_resource_get_resource(staticResource);
459 		
460 		if(p is null)
461 		{
462 			return null;
463 		}
464 		
465 		return ObjectG.getDObject!(Resource)(cast(GResource*) p);
466 	}
467 	
468 	/**
469 	 * Registers the resource with the process-global set of resources.
470 	 * Once a resource is registered the files in it can be accessed
471 	 * with the global resource lookup functions like g_resources_lookup_data().
472 	 * Since 2.32
473 	 */
474 	public void gResourcesRegister()
475 	{
476 		// void g_resources_register (GResource *resource);
477 		g_resources_register(gResource);
478 	}
479 	
480 	/**
481 	 * Unregisters the resource from the process-global set of resources.
482 	 * Since 2.32
483 	 */
484 	public void gResourcesUnregister()
485 	{
486 		// void g_resources_unregister (GResource *resource);
487 		g_resources_unregister(gResource);
488 	}
489 	
490 	/**
491 	 * Looks for a file at the specified path in the set of
492 	 * globally registered resources and returns a GBytes that
493 	 * lets you directly access the data in memory.
494 	 * The data is always followed by a zero byte, so you
495 	 * can safely use the data as a C string. However, that byte
496 	 * is not included in the size of the GBytes.
497 	 * For uncompressed resource files this is a pointer directly into
498 	 * the resource bundle, which is typically in some readonly data section
499 	 * in the program binary. For compressed files we allocate memory on
500 	 * the heap and automatically uncompress the data.
501 	 * lookup_flags controls the behaviour of the lookup.
502 	 * Since 2.32
503 	 * Params:
504 	 * path = A pathname inside the resource
505 	 * lookupFlags = A GResourceLookupFlags
506 	 * Returns: GBytes or NULL on error. Free the returned object with g_bytes_unref(). [transfer full]
507 	 * Throws: GException on failure.
508 	 */
509 	public static Bytes gResourcesLookupData(string path, GResourceLookupFlags lookupFlags)
510 	{
511 		// GBytes * g_resources_lookup_data (const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
512 		GError* err = null;
513 		
514 		auto p = g_resources_lookup_data(Str.toStringz(path), lookupFlags, &err);
515 		
516 		if (err !is null)
517 		{
518 			throw new GException( new ErrorG(err) );
519 		}
520 		
521 		
522 		if(p is null)
523 		{
524 			return null;
525 		}
526 		
527 		return ObjectG.getDObject!(Bytes)(cast(GBytes*) p);
528 	}
529 	
530 	/**
531 	 * Looks for a file at the specified path in the set of
532 	 * globally registered resources and returns a GInputStream
533 	 * that lets you read the data.
534 	 * lookup_flags controls the behaviour of the lookup.
535 	 * Since 2.32
536 	 * Params:
537 	 * path = A pathname inside the resource
538 	 * lookupFlags = A GResourceLookupFlags
539 	 * Returns: GInputStream or NULL on error. Free the returned object with g_object_unref(). [transfer full]
540 	 * Throws: GException on failure.
541 	 */
542 	public static InputStream gResourcesOpenStream(string path, GResourceLookupFlags lookupFlags)
543 	{
544 		// GInputStream * g_resources_open_stream (const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
545 		GError* err = null;
546 		
547 		auto p = g_resources_open_stream(Str.toStringz(path), lookupFlags, &err);
548 		
549 		if (err !is null)
550 		{
551 			throw new GException( new ErrorG(err) );
552 		}
553 		
554 		
555 		if(p is null)
556 		{
557 			return null;
558 		}
559 		
560 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) p);
561 	}
562 	
563 	/**
564 	 * Returns all the names of children at the specified path in the set of
565 	 * globally registered resources.
566 	 * The return result is a NULL terminated list of strings which should
567 	 * be released with g_strfreev().
568 	 * lookup_flags controls the behaviour of the lookup.
569 	 * Since 2.32
570 	 * Params:
571 	 * path = A pathname inside the resource
572 	 * lookupFlags = A GResourceLookupFlags
573 	 * Returns: an array of constant strings. [array zero-terminated=1][transfer full]
574 	 * Throws: GException on failure.
575 	 */
576 	public static string[] gResourcesEnumerateChildren(string path, GResourceLookupFlags lookupFlags)
577 	{
578 		// char ** g_resources_enumerate_children (const char *path,  GResourceLookupFlags lookup_flags,  GError **error);
579 		GError* err = null;
580 		
581 		auto p = g_resources_enumerate_children(Str.toStringz(path), lookupFlags, &err);
582 		
583 		if (err !is null)
584 		{
585 			throw new GException( new ErrorG(err) );
586 		}
587 		
588 		return Str.toStringArray(p);
589 	}
590 	
591 	/**
592 	 * Looks for a file at the specified path in the set of
593 	 * globally registered resources and if found returns information about it.
594 	 * lookup_flags controls the behaviour of the lookup.
595 	 * Since 2.32
596 	 * Params:
597 	 * path = A pathname inside the resource
598 	 * lookupFlags = A GResourceLookupFlags
599 	 * size = a location to place the length of the contents of the file,
600 	 * or NULL if the length is not needed. [out][allow-none]
601 	 * flags = a location to place the flags about the file,
602 	 * or NULL if the length is not needed. [out][allow-none]
603 	 * Returns: TRUE if the file was found. FALSE if there were errors
604 	 * Throws: GException on failure.
605 	 */
606 	public static int gResourcesGetInfo(string path, GResourceLookupFlags lookupFlags, gsize* size, uint* flags)
607 	{
608 		// gboolean g_resources_get_info (const char *path,  GResourceLookupFlags lookup_flags,  gsize *size,  guint32 *flags,  GError **error);
609 		GError* err = null;
610 		
611 		auto p = g_resources_get_info(Str.toStringz(path), lookupFlags, size, flags, &err);
612 		
613 		if (err !is null)
614 		{
615 			throw new GException( new ErrorG(err) );
616 		}
617 		
618 		return p;
619 	}
620 }