1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::File;
use glib::object::IsA;
use glib::translate::*;
#[cfg(any(feature = "v2_50", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_50")))]
use std::boxed::Box as Box_;
use std::fmt;

glib::wrapper! {
    /// Entry point for using GIO functionality.
    ///
    /// # Implements
    ///
    /// [`VfsExt`][trait@crate::prelude::VfsExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GVfs")]
    pub struct Vfs(Object<ffi::GVfs, ffi::GVfsClass>);

    match fn {
        type_ => || ffi::g_vfs_get_type(),
    }
}

impl Vfs {
    /// Gets the default [`Vfs`][crate::Vfs] for the system.
    ///
    /// # Returns
    ///
    /// a [`Vfs`][crate::Vfs], which will be the local
    ///  file system [`Vfs`][crate::Vfs] if no other implementation is available.
    #[doc(alias = "g_vfs_get_default")]
    #[doc(alias = "get_default")]
    pub fn default() -> Vfs {
        unsafe { from_glib_none(ffi::g_vfs_get_default()) }
    }

    /// Gets the local [`Vfs`][crate::Vfs] for the system.
    ///
    /// # Returns
    ///
    /// a [`Vfs`][crate::Vfs].
    #[doc(alias = "g_vfs_get_local")]
    #[doc(alias = "get_local")]
    pub fn local() -> Vfs {
        unsafe { from_glib_none(ffi::g_vfs_get_local()) }
    }
}

unsafe impl Send for Vfs {}
unsafe impl Sync for Vfs {}

pub const NONE_VFS: Option<&Vfs> = None;

/// Trait containing all [`struct@Vfs`] methods.
///
/// # Implementors
///
/// [`Vfs`][struct@crate::Vfs]
pub trait VfsExt: 'static {
    /// Gets a [`File`][crate::File] for `path`.
    /// ## `path`
    /// a string containing a VFS path.
    ///
    /// # Returns
    ///
    /// a [`File`][crate::File].
    ///  Free the returned object with `g_object_unref()`.
    #[doc(alias = "g_vfs_get_file_for_path")]
    #[doc(alias = "get_file_for_path")]
    fn file_for_path(&self, path: &str) -> File;

    /// Gets a [`File`][crate::File] for `uri`.
    ///
    /// This operation never fails, but the returned object
    /// might not support any I/O operation if the URI
    /// is malformed or if the URI scheme is not supported.
    /// ## `uri`
    /// a string containing a URI
    ///
    /// # Returns
    ///
    /// a [`File`][crate::File].
    ///  Free the returned object with `g_object_unref()`.
    #[doc(alias = "g_vfs_get_file_for_uri")]
    #[doc(alias = "get_file_for_uri")]
    fn file_for_uri(&self, uri: &str) -> File;

    /// Gets a list of URI schemes supported by `self`.
    ///
    /// # Returns
    ///
    /// a [`None`]-terminated array of strings.
    ///  The returned array belongs to GIO and must
    ///  not be freed or modified.
    #[doc(alias = "g_vfs_get_supported_uri_schemes")]
    #[doc(alias = "get_supported_uri_schemes")]
    fn supported_uri_schemes(&self) -> Vec<glib::GString>;

    /// Checks if the VFS is active.
    ///
    /// # Returns
    ///
    /// [`true`] if construction of the `self` was successful
    ///  and it is now active.
    #[doc(alias = "g_vfs_is_active")]
    fn is_active(&self) -> bool;

    /// This operation never fails, but the returned object might
    /// not support any I/O operations if the `parse_name` cannot
    /// be parsed by the [`Vfs`][crate::Vfs] module.
    /// ## `parse_name`
    /// a string to be parsed by the VFS module.
    ///
    /// # Returns
    ///
    /// a [`File`][crate::File] for the given `parse_name`.
    ///  Free the returned object with `g_object_unref()`.
    #[doc(alias = "g_vfs_parse_name")]
    fn parse_name(&self, parse_name: &str) -> File;

    /// Registers `uri_func` and `parse_name_func` as the [`File`][crate::File] URI and parse name
    /// lookup functions for URIs with a scheme matching `scheme`.
    /// Note that `scheme` is registered only within the running application, as
    /// opposed to desktop-wide as it happens with GVfs backends.
    ///
    /// When a [`File`][crate::File] is requested with an URI containing `scheme` (e.g. through
    /// [`File::for_uri()`][crate::File::for_uri()]), `uri_func` will be called to allow a custom
    /// constructor. The implementation of `uri_func` should not be blocking, and
    /// must not call [`register_uri_scheme()`][Self::register_uri_scheme()] or [`unregister_uri_scheme()`][Self::unregister_uri_scheme()].
    ///
    /// When [`File::for_parse_name()`][crate::File::for_parse_name()] is called with a parse name obtained from such file,
    /// `parse_name_func` will be called to allow the [`File`][crate::File] to be created again. In
    /// that case, it's responsibility of `parse_name_func` to make sure the parse
    /// name matches what the custom [`File`][crate::File] implementation returned when
    /// [`FileExt::parse_name()`][crate::prelude::FileExt::parse_name()] was previously called. The implementation of
    /// `parse_name_func` should not be blocking, and must not call
    /// [`register_uri_scheme()`][Self::register_uri_scheme()] or [`unregister_uri_scheme()`][Self::unregister_uri_scheme()].
    ///
    /// It's an error to call this function twice with the same scheme. To unregister
    /// a custom URI scheme, use [`unregister_uri_scheme()`][Self::unregister_uri_scheme()].
    /// ## `scheme`
    /// an URI scheme, e.g. "http"
    /// ## `uri_func`
    /// a `GVfsFileLookupFunc`
    /// ## `uri_data`
    /// custom data passed to be passed to `uri_func`, or [`None`]
    /// ## `uri_destroy`
    /// function to be called when unregistering the
    ///  URI scheme, or when `self` is disposed, to free the resources used
    ///  by the URI lookup function
    /// ## `parse_name_func`
    /// a `GVfsFileLookupFunc`
    /// ## `parse_name_data`
    /// custom data passed to be passed to
    ///  `parse_name_func`, or [`None`]
    /// ## `parse_name_destroy`
    /// function to be called when unregistering the
    ///  URI scheme, or when `self` is disposed, to free the resources used
    ///  by the parse name lookup function
    ///
    /// # Returns
    ///
    /// [`true`] if `scheme` was successfully registered, or [`false`] if a handler
    ///  for `scheme` already exists.
    #[cfg(any(feature = "v2_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_50")))]
    #[doc(alias = "g_vfs_register_uri_scheme")]
    fn register_uri_scheme(
        &self,
        scheme: &str,
        uri_func: Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>,
        parse_name_func: Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>,
    ) -> bool;

    /// Unregisters the URI handler for `scheme` previously registered with
    /// [`register_uri_scheme()`][Self::register_uri_scheme()].
    /// ## `scheme`
    /// an URI scheme, e.g. "http"
    ///
    /// # Returns
    ///
    /// [`true`] if `scheme` was successfully unregistered, or [`false`] if a
    ///  handler for `scheme` does not exist.
    #[cfg(any(feature = "v2_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_50")))]
    #[doc(alias = "g_vfs_unregister_uri_scheme")]
    fn unregister_uri_scheme(&self, scheme: &str) -> bool;
}

impl<O: IsA<Vfs>> VfsExt for O {
    fn file_for_path(&self, path: &str) -> File {
        unsafe {
            from_glib_full(ffi::g_vfs_get_file_for_path(
                self.as_ref().to_glib_none().0,
                path.to_glib_none().0,
            ))
        }
    }

    fn file_for_uri(&self, uri: &str) -> File {
        unsafe {
            from_glib_full(ffi::g_vfs_get_file_for_uri(
                self.as_ref().to_glib_none().0,
                uri.to_glib_none().0,
            ))
        }
    }

    fn supported_uri_schemes(&self) -> Vec<glib::GString> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ffi::g_vfs_get_supported_uri_schemes(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn is_active(&self) -> bool {
        unsafe { from_glib(ffi::g_vfs_is_active(self.as_ref().to_glib_none().0)) }
    }

    fn parse_name(&self, parse_name: &str) -> File {
        unsafe {
            from_glib_full(ffi::g_vfs_parse_name(
                self.as_ref().to_glib_none().0,
                parse_name.to_glib_none().0,
            ))
        }
    }

    #[cfg(any(feature = "v2_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_50")))]
    fn register_uri_scheme(
        &self,
        scheme: &str,
        uri_func: Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>,
        parse_name_func: Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>,
    ) -> bool {
        let uri_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
            Box_::new(uri_func);
        unsafe extern "C" fn uri_func_func(
            vfs: *mut ffi::GVfs,
            identifier: *const libc::c_char,
            user_data: glib::ffi::gpointer,
        ) -> *mut ffi::GFile {
            let vfs = from_glib_borrow(vfs);
            let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
            let callback: &Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>> =
                &*(user_data as *mut _);
            let res = if let Some(ref callback) = *callback {
                callback(&vfs, identifier.as_str())
            } else {
                panic!("cannot get closure...")
            };
            res.to_glib_full()
        }
        let uri_func = if uri_func_data.is_some() {
            Some(uri_func_func as _)
        } else {
            None
        };
        let parse_name_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
            Box_::new(parse_name_func);
        unsafe extern "C" fn parse_name_func_func(
            vfs: *mut ffi::GVfs,
            identifier: *const libc::c_char,
            user_data: glib::ffi::gpointer,
        ) -> *mut ffi::GFile {
            let vfs = from_glib_borrow(vfs);
            let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
            let callback: &Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>> =
                &*(user_data as *mut _);
            let res = if let Some(ref callback) = *callback {
                callback(&vfs, identifier.as_str())
            } else {
                panic!("cannot get closure...")
            };
            res.to_glib_full()
        }
        let parse_name_func = if parse_name_func_data.is_some() {
            Some(parse_name_func_func as _)
        } else {
            None
        };
        unsafe extern "C" fn uri_destroy_func(data: glib::ffi::gpointer) {
            let _callback: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
                Box_::from_raw(data as *mut _);
        }
        let destroy_call4 = Some(uri_destroy_func as _);
        unsafe extern "C" fn parse_name_destroy_func(data: glib::ffi::gpointer) {
            let _callback: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
                Box_::from_raw(data as *mut _);
        }
        let destroy_call7 = Some(parse_name_destroy_func as _);
        let super_callback0: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
            uri_func_data;
        let super_callback1: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> File + 'static>>> =
            parse_name_func_data;
        unsafe {
            from_glib(ffi::g_vfs_register_uri_scheme(
                self.as_ref().to_glib_none().0,
                scheme.to_glib_none().0,
                uri_func,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call4,
                parse_name_func,
                Box_::into_raw(super_callback1) as *mut _,
                destroy_call7,
            ))
        }
    }

    #[cfg(any(feature = "v2_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_50")))]
    fn unregister_uri_scheme(&self, scheme: &str) -> bool {
        unsafe {
            from_glib(ffi::g_vfs_unregister_uri_scheme(
                self.as_ref().to_glib_none().0,
                scheme.to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for Vfs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Vfs")
    }
}