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