gio/auto/vfs.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::{ffi, File};
6use glib::{prelude::*, translate::*};
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10 /// Entry point for using GIO functionality.
11 ///
12 /// # Implements
13 ///
14 /// [`VfsExt`][trait@crate::prelude::VfsExt], [`trait@glib::ObjectExt`]
15 #[doc(alias = "GVfs")]
16 pub struct Vfs(Object<ffi::GVfs, ffi::GVfsClass>);
17
18 match fn {
19 type_ => || ffi::g_vfs_get_type(),
20 }
21}
22
23impl Vfs {
24 pub const NONE: Option<&'static Vfs> = None;
25
26 /// Gets the default #GVfs for the system.
27 ///
28 /// # Returns
29 ///
30 /// a #GVfs, which will be the local
31 /// file system #GVfs if no other implementation is available.
32 #[doc(alias = "g_vfs_get_default")]
33 #[doc(alias = "get_default")]
34 #[allow(clippy::should_implement_trait)]
35 pub fn default() -> Vfs {
36 unsafe { from_glib_none(ffi::g_vfs_get_default()) }
37 }
38
39 /// Gets the local #GVfs for the system.
40 ///
41 /// # Returns
42 ///
43 /// a #GVfs.
44 #[doc(alias = "g_vfs_get_local")]
45 #[doc(alias = "get_local")]
46 pub fn local() -> Vfs {
47 unsafe { from_glib_none(ffi::g_vfs_get_local()) }
48 }
49}
50
51unsafe impl Send for Vfs {}
52unsafe impl Sync for Vfs {}
53
54mod sealed {
55 pub trait Sealed {}
56 impl<T: super::IsA<super::Vfs>> Sealed for T {}
57}
58
59/// Trait containing all [`struct@Vfs`] methods.
60///
61/// # Implementors
62///
63/// [`Vfs`][struct@crate::Vfs]
64pub trait VfsExt: IsA<Vfs> + sealed::Sealed + 'static {
65 /// Gets a #GFile for @path.
66 /// ## `path`
67 /// a string containing a VFS path.
68 ///
69 /// # Returns
70 ///
71 /// a #GFile.
72 /// Free the returned object with g_object_unref().
73 #[doc(alias = "g_vfs_get_file_for_path")]
74 #[doc(alias = "get_file_for_path")]
75 fn file_for_path(&self, path: &str) -> File {
76 unsafe {
77 from_glib_full(ffi::g_vfs_get_file_for_path(
78 self.as_ref().to_glib_none().0,
79 path.to_glib_none().0,
80 ))
81 }
82 }
83
84 /// Gets a #GFile for @uri.
85 ///
86 /// This operation never fails, but the returned object
87 /// might not support any I/O operation if the URI
88 /// is malformed or if the URI scheme is not supported.
89 /// ## `uri`
90 /// a string containing a URI
91 ///
92 /// # Returns
93 ///
94 /// a #GFile.
95 /// Free the returned object with g_object_unref().
96 #[doc(alias = "g_vfs_get_file_for_uri")]
97 #[doc(alias = "get_file_for_uri")]
98 fn file_for_uri(&self, uri: &str) -> File {
99 unsafe {
100 from_glib_full(ffi::g_vfs_get_file_for_uri(
101 self.as_ref().to_glib_none().0,
102 uri.to_glib_none().0,
103 ))
104 }
105 }
106
107 /// Gets a list of URI schemes supported by @self.
108 ///
109 /// # Returns
110 ///
111 /// a [`None`]-terminated array of strings.
112 /// The returned array belongs to GIO and must
113 /// not be freed or modified.
114 #[doc(alias = "g_vfs_get_supported_uri_schemes")]
115 #[doc(alias = "get_supported_uri_schemes")]
116 fn supported_uri_schemes(&self) -> Vec<glib::GString> {
117 unsafe {
118 FromGlibPtrContainer::from_glib_none(ffi::g_vfs_get_supported_uri_schemes(
119 self.as_ref().to_glib_none().0,
120 ))
121 }
122 }
123
124 /// Checks if the VFS is active.
125 ///
126 /// # Returns
127 ///
128 /// [`true`] if construction of the @self was successful
129 /// and it is now active.
130 #[doc(alias = "g_vfs_is_active")]
131 fn is_active(&self) -> bool {
132 unsafe { from_glib(ffi::g_vfs_is_active(self.as_ref().to_glib_none().0)) }
133 }
134
135 /// This operation never fails, but the returned object might
136 /// not support any I/O operations if the @parse_name cannot
137 /// be parsed by the #GVfs module.
138 /// ## `parse_name`
139 /// a string to be parsed by the VFS module.
140 ///
141 /// # Returns
142 ///
143 /// a #GFile for the given @parse_name.
144 /// Free the returned object with g_object_unref().
145 #[doc(alias = "g_vfs_parse_name")]
146 fn parse_name(&self, parse_name: &str) -> File {
147 unsafe {
148 from_glib_full(ffi::g_vfs_parse_name(
149 self.as_ref().to_glib_none().0,
150 parse_name.to_glib_none().0,
151 ))
152 }
153 }
154
155 /// Registers @uri_func and @parse_name_func as the #GFile URI and parse name
156 /// lookup functions for URIs with a scheme matching @scheme.
157 /// Note that @scheme is registered only within the running application, as
158 /// opposed to desktop-wide as it happens with GVfs backends.
159 ///
160 /// When a #GFile is requested with an URI containing @scheme (e.g. through
161 /// g_file_new_for_uri()), @uri_func will be called to allow a custom
162 /// constructor. The implementation of @uri_func should not be blocking, and
163 /// must not call g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme().
164 ///
165 /// When g_file_parse_name() is called with a parse name obtained from such file,
166 /// @parse_name_func will be called to allow the #GFile to be created again. In
167 /// that case, it's responsibility of @parse_name_func to make sure the parse
168 /// name matches what the custom #GFile implementation returned when
169 /// g_file_get_parse_name() was previously called. The implementation of
170 /// @parse_name_func should not be blocking, and must not call
171 /// g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme().
172 ///
173 /// It's an error to call this function twice with the same scheme. To unregister
174 /// a custom URI scheme, use g_vfs_unregister_uri_scheme().
175 /// ## `scheme`
176 /// an URI scheme, e.g. "http"
177 /// ## `uri_func`
178 /// a #GVfsFileLookupFunc
179 /// ## `uri_data`
180 /// custom data passed to be passed to @uri_func, or [`None`]
181 /// ## `uri_destroy`
182 /// function to be called when unregistering the
183 /// URI scheme, or when @self is disposed, to free the resources used
184 /// by the URI lookup function
185 /// ## `parse_name_func`
186 /// a #GVfsFileLookupFunc
187 /// ## `parse_name_data`
188 /// custom data passed to be passed to
189 /// @parse_name_func, or [`None`]
190 /// ## `parse_name_destroy`
191 /// function to be called when unregistering the
192 /// URI scheme, or when @self is disposed, to free the resources used
193 /// by the parse name lookup function
194 ///
195 /// # Returns
196 ///
197 /// [`true`] if @scheme was successfully registered, or [`false`] if a handler
198 /// for @scheme already exists.
199 #[doc(alias = "g_vfs_register_uri_scheme")]
200 fn register_uri_scheme(
201 &self,
202 scheme: &str,
203 uri_func: Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
204 parse_name_func: Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
205 ) -> bool {
206 let uri_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
207 Box_::new(uri_func);
208 unsafe extern "C" fn uri_func_func(
209 vfs: *mut ffi::GVfs,
210 identifier: *const std::ffi::c_char,
211 user_data: glib::ffi::gpointer,
212 ) -> *mut ffi::GFile {
213 let vfs = from_glib_borrow(vfs);
214 let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
215 let callback =
216 &*(user_data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>);
217 if let Some(ref callback) = *callback {
218 callback(&vfs, identifier.as_str())
219 } else {
220 panic!("cannot get closure...")
221 }
222 .to_glib_full()
223 }
224 let uri_func = if uri_func_data.is_some() {
225 Some(uri_func_func as _)
226 } else {
227 None
228 };
229 let parse_name_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
230 Box_::new(parse_name_func);
231 unsafe extern "C" fn parse_name_func_func(
232 vfs: *mut ffi::GVfs,
233 identifier: *const std::ffi::c_char,
234 user_data: glib::ffi::gpointer,
235 ) -> *mut ffi::GFile {
236 let vfs = from_glib_borrow(vfs);
237 let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
238 let callback =
239 &*(user_data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>);
240 if let Some(ref callback) = *callback {
241 callback(&vfs, identifier.as_str())
242 } else {
243 panic!("cannot get closure...")
244 }
245 .to_glib_full()
246 }
247 let parse_name_func = if parse_name_func_data.is_some() {
248 Some(parse_name_func_func as _)
249 } else {
250 None
251 };
252 unsafe extern "C" fn uri_destroy_func(data: glib::ffi::gpointer) {
253 let _callback = Box_::from_raw(
254 data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
255 );
256 }
257 let destroy_call4 = Some(uri_destroy_func as _);
258 unsafe extern "C" fn parse_name_destroy_func(data: glib::ffi::gpointer) {
259 let _callback = Box_::from_raw(
260 data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
261 );
262 }
263 let destroy_call7 = Some(parse_name_destroy_func as _);
264 let super_callback0: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
265 uri_func_data;
266 let super_callback1: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
267 parse_name_func_data;
268 unsafe {
269 from_glib(ffi::g_vfs_register_uri_scheme(
270 self.as_ref().to_glib_none().0,
271 scheme.to_glib_none().0,
272 uri_func,
273 Box_::into_raw(super_callback0) as *mut _,
274 destroy_call4,
275 parse_name_func,
276 Box_::into_raw(super_callback1) as *mut _,
277 destroy_call7,
278 ))
279 }
280 }
281
282 /// Unregisters the URI handler for @scheme previously registered with
283 /// g_vfs_register_uri_scheme().
284 /// ## `scheme`
285 /// an URI scheme, e.g. "http"
286 ///
287 /// # Returns
288 ///
289 /// [`true`] if @scheme was successfully unregistered, or [`false`] if a
290 /// handler for @scheme does not exist.
291 #[doc(alias = "g_vfs_unregister_uri_scheme")]
292 fn unregister_uri_scheme(&self, scheme: &str) -> bool {
293 unsafe {
294 from_glib(ffi::g_vfs_unregister_uri_scheme(
295 self.as_ref().to_glib_none().0,
296 scheme.to_glib_none().0,
297 ))
298 }
299 }
300}
301
302impl<O: IsA<Vfs>> VfsExt for O {}