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