gio/auto/resource.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{InputStream, ResourceLookupFlags, ffi};
6use glib::translate::*;
7
8glib::wrapper! {
9 ///
10 /// ```text
11 ///
12 /// This will create a resource bundle with the following files:
13 /// ```
14 /// /org/gtk/Example/data/splashscreen.png
15 /// /org/gtk/Example/dialog.ui
16 /// /org/gtk/Example/menumarkup.xml
17 /// /org/gtk/Example/example.css
18 /// ```text
19 ///
20 /// Note that all resources in the process share the same namespace, so use
21 /// Java-style path prefixes (like in the above example) to avoid conflicts.
22 ///
23 /// You can then use [`glib-compile-resources`](glib-compile-resources.html) to
24 /// compile the XML to a binary bundle that you can load with
25 /// [func@Gio.Resource.load]. However, it’s more common to use the
26 /// `--generate-source` and `--generate-header` arguments to create a source file
27 /// and header to link directly into your application.
28 /// This will generate `get_resource()`, `register_resource()` and
29 /// `unregister_resource()` functions, prefixed by the `--c-name` argument passed
30 /// to [`glib-compile-resources`](glib-compile-resources.html). `get_resource()`
31 /// returns the generated `GResource` object. The register and unregister
32 /// functions register the resource so its files can be accessed using
33 /// [func@Gio.resources_lookup_data].
34 ///
35 /// Once a `GResource` has been created and registered all the data in it can be
36 /// accessed globally in the process by using API calls like
37 /// [func@Gio.resources_open_stream] to stream the data or
38 /// [func@Gio.resources_lookup_data] to get a direct pointer to the data. You can
39 /// also use URIs like `resource:///org/gtk/Example/data/splashscreen.png` with
40 /// [iface@Gio.File] to access the resource data.
41 ///
42 /// Some higher-level APIs, such as [`GtkApplication`](https://docs.gtk.org/gtk4/class.Application.html),
43 /// will automatically load resources from certain well-known paths in the
44 /// resource namespace as a convenience. See the documentation for those APIs
45 /// for details.
46 ///
47 /// There are two forms of the generated source, the default version uses the
48 /// compiler support for constructor and destructor functions (where available)
49 /// to automatically create and register the `GResource` on startup or library
50 /// load time. If you pass `--manual-register`, two functions to
51 /// register/unregister the resource are created instead. This requires an
52 /// explicit initialization call in your application/library, but it works on all
53 /// platforms, even on the minor ones where constructors are not supported.
54 /// (Constructor support is available for at least Win32, Mac OS and Linux.)
55 ///
56 /// Note that resource data can point directly into the data segment of e.g. a
57 /// library, so if you are unloading libraries during runtime you need to be very
58 /// careful with keeping around pointers to data from a resource, as this goes
59 /// away when the library is unloaded. However, in practice this is not generally
60 /// a problem, since most resource accesses are for your own resources, and
61 /// resource data is often used once, during parsing, and then released.
62 ///
63 /// # Overlays
64 ///
65 /// When debugging a program or testing a change to an installed version, it is
66 /// often useful to be able to replace resources in the program or library,
67 /// without recompiling, for debugging or quick hacking and testing purposes.
68 /// Since GLib 2.50, it is possible to use the `G_RESOURCE_OVERLAYS` environment
69 /// variable to selectively overlay resources with replacements from the
70 /// filesystem. It is a `G_SEARCHPATH_SEPARATOR`-separated list of substitutions
71 /// to perform during resource lookups. It is ignored when running in a setuid
72 /// process.
73 ///
74 /// A substitution has the form
75 ///
76 /// ```
77 /// /org/gtk/libgtk=/home/desrt/gtk-overlay
78 /// ```text
79 ///
80 /// The part before the `=` is the resource subpath for which the overlay
81 /// applies. The part after is a filesystem path which contains files and
82 /// subdirectories as you would like to be loaded as resources with the
83 /// equivalent names.
84 ///
85 /// In the example above, if an application tried to load a resource with the
86 /// resource path `/org/gtk/libgtk/ui/gtkdialog.ui` then `GResource` would check
87 /// the filesystem path `/home/desrt/gtk-overlay/ui/gtkdialog.ui`. If a file was
88 /// found there, it would be used instead. This is an overlay, not an outright
89 /// replacement, which means that if a file is not found at that path, the
90 /// built-in version will be used instead. Whiteouts are not currently
91 /// supported.
92 ///
93 /// Substitutions must start with a slash, and must not contain a trailing slash
94 /// before the `=`. The filesystem path after the `=` should ideally be absolute,
95 /// but this is not strictly required. It is possible to overlay the location of
96 /// a single resource with an individual file.
97 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
98 pub struct Resource(Shared<ffi::GResource>);
99
100 match fn {
101 ref => |ptr| ffi::g_resource_ref(ptr),
102 unref => |ptr| ffi::g_resource_unref(ptr),
103 type_ => || ffi::g_resource_get_type(),
104 }
105}
106
107impl Resource {
108 /// Returns all the names of children at the specified @path in the resource.
109 ///
110 /// The return result is a `NULL` terminated list of strings which should
111 /// be released with `strfreev()`.
112 ///
113 /// If @path is invalid or does not exist in the [`Resource`][crate::Resource],
114 /// [`ResourceError::NotFound`][crate::ResourceError::NotFound] will be returned.
115 ///
116 /// @lookup_flags controls the behaviour of the lookup.
117 /// ## `path`
118 /// A path name inside the resource
119 /// ## `lookup_flags`
120 /// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
121 ///
122 /// # Returns
123 ///
124 /// an array of constant strings
125 #[doc(alias = "g_resource_enumerate_children")]
126 pub fn enumerate_children(
127 &self,
128 path: &str,
129 lookup_flags: ResourceLookupFlags,
130 ) -> Result<Vec<glib::GString>, glib::Error> {
131 unsafe {
132 let mut error = std::ptr::null_mut();
133 let ret = ffi::g_resource_enumerate_children(
134 self.to_glib_none().0,
135 path.to_glib_none().0,
136 lookup_flags.into_glib(),
137 &mut error,
138 );
139 if error.is_null() {
140 Ok(FromGlibPtrContainer::from_glib_full(ret))
141 } else {
142 Err(from_glib_full(error))
143 }
144 }
145 }
146
147 /// Looks for a file at the specified @path in the resource and
148 /// if found returns information about it.
149 ///
150 /// @lookup_flags controls the behaviour of the lookup.
151 ///
152 /// The only error this can return is [`ResourceError::NotFound`][crate::ResourceError::NotFound], if @path was
153 /// not found in @self.
154 /// ## `path`
155 /// A path name inside the resource
156 /// ## `lookup_flags`
157 /// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
158 ///
159 /// # Returns
160 ///
161 /// `TRUE` if the file was found, `FALSE` if there were errors
162 ///
163 /// ## `size`
164 /// a location to place the length of the contents of the file,
165 /// or `NULL` if the length is not needed
166 ///
167 /// ## `flags`
168 /// a location to place the flags about the file,
169 /// or `NULL` if the length is not needed
170 #[doc(alias = "g_resource_get_info")]
171 #[doc(alias = "get_info")]
172 pub fn info(
173 &self,
174 path: &str,
175 lookup_flags: ResourceLookupFlags,
176 ) -> Result<(usize, u32), glib::Error> {
177 unsafe {
178 let mut size = std::mem::MaybeUninit::uninit();
179 let mut flags = std::mem::MaybeUninit::uninit();
180 let mut error = std::ptr::null_mut();
181 let is_ok = ffi::g_resource_get_info(
182 self.to_glib_none().0,
183 path.to_glib_none().0,
184 lookup_flags.into_glib(),
185 size.as_mut_ptr(),
186 flags.as_mut_ptr(),
187 &mut error,
188 );
189 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
190 if error.is_null() {
191 Ok((size.assume_init(), flags.assume_init()))
192 } else {
193 Err(from_glib_full(error))
194 }
195 }
196 }
197
198 /// Returns whether the specified @path in the resource
199 /// has children.
200 /// ## `path`
201 /// A pathname inside the resource
202 ///
203 /// # Returns
204 ///
205 /// [`true`] if @path has children
206 #[cfg(feature = "v2_84")]
207 #[cfg_attr(docsrs, doc(cfg(feature = "v2_84")))]
208 #[doc(alias = "g_resource_has_children")]
209 pub fn has_children(&self, path: &str) -> bool {
210 unsafe {
211 from_glib(ffi::g_resource_has_children(
212 self.to_glib_none().0,
213 path.to_glib_none().0,
214 ))
215 }
216 }
217
218 /// Looks for a file at the specified @path in the resource and
219 /// returns a [`glib::Bytes`][crate::glib::Bytes] that lets you directly access the data in
220 /// memory.
221 ///
222 /// The data is always followed by a zero byte, so you
223 /// can safely use the data as a C string. However, that byte
224 /// is not included in the size of the [`glib::Bytes`][crate::glib::Bytes].
225 ///
226 /// For uncompressed resource files this is a pointer directly into
227 /// the resource bundle, which is typically in some read-only data section
228 /// in the program binary. For compressed files, memory is allocated on
229 /// the heap and the data is automatically uncompressed.
230 ///
231 /// @lookup_flags controls the behaviour of the lookup.
232 ///
233 /// This can return error [`ResourceError::NotFound`][crate::ResourceError::NotFound] if @path was not found in
234 /// @self, or [`ResourceError::Internal`][crate::ResourceError::Internal] if decompression of a compressed
235 /// resource failed.
236 /// ## `path`
237 /// A path name inside the resource
238 /// ## `lookup_flags`
239 /// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
240 ///
241 /// # Returns
242 ///
243 /// [`glib::Bytes`][crate::glib::Bytes] or `NULL` on error
244 #[doc(alias = "g_resource_lookup_data")]
245 pub fn lookup_data(
246 &self,
247 path: &str,
248 lookup_flags: ResourceLookupFlags,
249 ) -> Result<glib::Bytes, glib::Error> {
250 unsafe {
251 let mut error = std::ptr::null_mut();
252 let ret = ffi::g_resource_lookup_data(
253 self.to_glib_none().0,
254 path.to_glib_none().0,
255 lookup_flags.into_glib(),
256 &mut error,
257 );
258 if error.is_null() {
259 Ok(from_glib_full(ret))
260 } else {
261 Err(from_glib_full(error))
262 }
263 }
264 }
265
266 /// Looks for a file at the specified @path in the resource and
267 /// returns a [`InputStream`][crate::InputStream] that lets you read the data.
268 ///
269 /// @lookup_flags controls the behaviour of the lookup.
270 ///
271 /// The only error this can return is [`ResourceError::NotFound`][crate::ResourceError::NotFound], if @path was
272 /// not found in @self.
273 /// ## `path`
274 /// A path name inside the resource
275 /// ## `lookup_flags`
276 /// A [`ResourceLookupFlags`][crate::ResourceLookupFlags]
277 ///
278 /// # Returns
279 ///
280 /// [`InputStream`][crate::InputStream] or `NULL` on error
281 #[doc(alias = "g_resource_open_stream")]
282 pub fn open_stream(
283 &self,
284 path: &str,
285 lookup_flags: ResourceLookupFlags,
286 ) -> Result<InputStream, glib::Error> {
287 unsafe {
288 let mut error = std::ptr::null_mut();
289 let ret = ffi::g_resource_open_stream(
290 self.to_glib_none().0,
291 path.to_glib_none().0,
292 lookup_flags.into_glib(),
293 &mut error,
294 );
295 if error.is_null() {
296 Ok(from_glib_full(ret))
297 } else {
298 Err(from_glib_full(error))
299 }
300 }
301 }
302
303 /// Loads a binary resource bundle and creates a [`Resource`][crate::Resource]
304 /// representation of it, allowing you to query it for data.
305 ///
306 /// If you want to use this resource in the global resource namespace you need
307 /// to register it with [`resources_register()`][crate::resources_register()].
308 ///
309 /// If @filename is empty or the data in it is corrupt,
310 /// [`ResourceError::Internal`][crate::ResourceError::Internal] will be returned. If @filename doesn’t exist, or
311 /// there is an error in reading it, an error from `GLib::MappedFile::new()`
312 /// will be returned.
313 /// ## `filename`
314 /// the path of a filename to load, in the GLib filename encoding
315 ///
316 /// # Returns
317 ///
318 /// a new [`Resource`][crate::Resource], or `NULL` on error
319 #[doc(alias = "g_resource_load")]
320 pub fn load(filename: impl AsRef<std::path::Path>) -> Result<Resource, glib::Error> {
321 unsafe {
322 let mut error = std::ptr::null_mut();
323 let ret = ffi::g_resource_load(filename.as_ref().to_glib_none().0, &mut error);
324 if error.is_null() {
325 Ok(from_glib_full(ret))
326 } else {
327 Err(from_glib_full(error))
328 }
329 }
330 }
331}