1use crate::{ffi, File};
6use glib::{prelude::*, translate::*};
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10 #[doc(alias = "GVfs")]
11 pub struct Vfs(Object<ffi::GVfs, ffi::GVfsClass>);
12
13 match fn {
14 type_ => || ffi::g_vfs_get_type(),
15 }
16}
17
18impl Vfs {
19 pub const NONE: Option<&'static Vfs> = None;
20
21 #[doc(alias = "g_vfs_get_default")]
22 #[doc(alias = "get_default")]
23 #[allow(clippy::should_implement_trait)]
24 pub fn default() -> Vfs {
25 unsafe { from_glib_none(ffi::g_vfs_get_default()) }
26 }
27
28 #[doc(alias = "g_vfs_get_local")]
29 #[doc(alias = "get_local")]
30 pub fn local() -> Vfs {
31 unsafe { from_glib_none(ffi::g_vfs_get_local()) }
32 }
33}
34
35unsafe impl Send for Vfs {}
36unsafe impl Sync for Vfs {}
37
38pub trait VfsExt: IsA<Vfs> + 'static {
39 #[doc(alias = "g_vfs_get_file_for_path")]
40 #[doc(alias = "get_file_for_path")]
41 fn file_for_path(&self, path: impl AsRef<std::path::Path>) -> File {
42 unsafe {
43 from_glib_full(ffi::g_vfs_get_file_for_path(
44 self.as_ref().to_glib_none().0,
45 path.as_ref().to_glib_none().0,
46 ))
47 }
48 }
49
50 #[doc(alias = "g_vfs_get_file_for_uri")]
51 #[doc(alias = "get_file_for_uri")]
52 fn file_for_uri(&self, uri: &str) -> File {
53 unsafe {
54 from_glib_full(ffi::g_vfs_get_file_for_uri(
55 self.as_ref().to_glib_none().0,
56 uri.to_glib_none().0,
57 ))
58 }
59 }
60
61 #[doc(alias = "g_vfs_get_supported_uri_schemes")]
62 #[doc(alias = "get_supported_uri_schemes")]
63 fn supported_uri_schemes(&self) -> Vec<glib::GString> {
64 unsafe {
65 FromGlibPtrContainer::from_glib_none(ffi::g_vfs_get_supported_uri_schemes(
66 self.as_ref().to_glib_none().0,
67 ))
68 }
69 }
70
71 #[doc(alias = "g_vfs_is_active")]
72 fn is_active(&self) -> bool {
73 unsafe { from_glib(ffi::g_vfs_is_active(self.as_ref().to_glib_none().0)) }
74 }
75
76 #[doc(alias = "g_vfs_parse_name")]
77 fn parse_name(&self, parse_name: &str) -> File {
78 unsafe {
79 from_glib_full(ffi::g_vfs_parse_name(
80 self.as_ref().to_glib_none().0,
81 parse_name.to_glib_none().0,
82 ))
83 }
84 }
85
86 #[doc(alias = "g_vfs_register_uri_scheme")]
87 fn register_uri_scheme(
88 &self,
89 scheme: &str,
90 uri_func: Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
91 parse_name_func: Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
92 ) -> bool {
93 let uri_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
94 Box_::new(uri_func);
95 unsafe extern "C" fn uri_func_func(
96 vfs: *mut ffi::GVfs,
97 identifier: *const std::ffi::c_char,
98 user_data: glib::ffi::gpointer,
99 ) -> *mut ffi::GFile {
100 let vfs = from_glib_borrow(vfs);
101 let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
102 let callback =
103 &*(user_data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>);
104 if let Some(ref callback) = *callback {
105 callback(&vfs, identifier.as_str())
106 } else {
107 panic!("cannot get closure...")
108 }
109 .to_glib_full()
110 }
111 let uri_func = if uri_func_data.is_some() {
112 Some(uri_func_func as _)
113 } else {
114 None
115 };
116 let parse_name_func_data: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
117 Box_::new(parse_name_func);
118 unsafe extern "C" fn parse_name_func_func(
119 vfs: *mut ffi::GVfs,
120 identifier: *const std::ffi::c_char,
121 user_data: glib::ffi::gpointer,
122 ) -> *mut ffi::GFile {
123 let vfs = from_glib_borrow(vfs);
124 let identifier: Borrowed<glib::GString> = from_glib_borrow(identifier);
125 let callback =
126 &*(user_data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>);
127 if let Some(ref callback) = *callback {
128 callback(&vfs, identifier.as_str())
129 } else {
130 panic!("cannot get closure...")
131 }
132 .to_glib_full()
133 }
134 let parse_name_func = if parse_name_func_data.is_some() {
135 Some(parse_name_func_func as _)
136 } else {
137 None
138 };
139 unsafe extern "C" fn uri_destroy_func(data: glib::ffi::gpointer) {
140 let _callback = Box_::from_raw(
141 data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
142 );
143 }
144 let destroy_call4 = Some(uri_destroy_func as _);
145 unsafe extern "C" fn parse_name_destroy_func(data: glib::ffi::gpointer) {
146 let _callback = Box_::from_raw(
147 data as *mut Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>,
148 );
149 }
150 let destroy_call7 = Some(parse_name_destroy_func as _);
151 let super_callback0: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
152 uri_func_data;
153 let super_callback1: Box_<Option<Box_<dyn Fn(&Vfs, &str) -> Option<File> + 'static>>> =
154 parse_name_func_data;
155 unsafe {
156 from_glib(ffi::g_vfs_register_uri_scheme(
157 self.as_ref().to_glib_none().0,
158 scheme.to_glib_none().0,
159 uri_func,
160 Box_::into_raw(super_callback0) as *mut _,
161 destroy_call4,
162 parse_name_func,
163 Box_::into_raw(super_callback1) as *mut _,
164 destroy_call7,
165 ))
166 }
167 }
168
169 #[doc(alias = "g_vfs_unregister_uri_scheme")]
170 fn unregister_uri_scheme(&self, scheme: &str) -> bool {
171 unsafe {
172 from_glib(ffi::g_vfs_unregister_uri_scheme(
173 self.as_ref().to_glib_none().0,
174 scheme.to_glib_none().0,
175 ))
176 }
177 }
178}
179
180impl<O: IsA<Vfs>> VfsExt for O {}