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