1use crate::{
6 ffi, AsyncResult, Cancellable, Drive, File, Icon, MountMountFlags, MountOperation,
7 MountUnmountFlags, Volume,
8};
9use glib::{
10 object::ObjectType as _,
11 prelude::*,
12 signal::{connect_raw, SignalHandlerId},
13 translate::*,
14};
15use std::{boxed::Box as Box_, pin::Pin};
16
17glib::wrapper! {
18 #[doc(alias = "GMount")]
19 pub struct Mount(Interface<ffi::GMount, ffi::GMountIface>);
20
21 match fn {
22 type_ => || ffi::g_mount_get_type(),
23 }
24}
25
26impl Mount {
27 pub const NONE: Option<&'static Mount> = None;
28}
29
30pub trait MountExt: IsA<Mount> + 'static {
31 #[doc(alias = "g_mount_can_eject")]
32 fn can_eject(&self) -> bool {
33 unsafe { from_glib(ffi::g_mount_can_eject(self.as_ref().to_glib_none().0)) }
34 }
35
36 #[doc(alias = "g_mount_can_unmount")]
37 fn can_unmount(&self) -> bool {
38 unsafe { from_glib(ffi::g_mount_can_unmount(self.as_ref().to_glib_none().0)) }
39 }
40
41 #[doc(alias = "g_mount_eject_with_operation")]
42 fn eject_with_operation<P: FnOnce(Result<(), glib::Error>) + 'static>(
43 &self,
44 flags: MountUnmountFlags,
45 mount_operation: Option<&impl IsA<MountOperation>>,
46 cancellable: Option<&impl IsA<Cancellable>>,
47 callback: P,
48 ) {
49 let main_context = glib::MainContext::ref_thread_default();
50 let is_main_context_owner = main_context.is_owner();
51 let has_acquired_main_context = (!is_main_context_owner)
52 .then(|| main_context.acquire().ok())
53 .flatten();
54 assert!(
55 is_main_context_owner || has_acquired_main_context.is_some(),
56 "Async operations only allowed if the thread is owning the MainContext"
57 );
58
59 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
60 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
61 unsafe extern "C" fn eject_with_operation_trampoline<
62 P: FnOnce(Result<(), glib::Error>) + 'static,
63 >(
64 _source_object: *mut glib::gobject_ffi::GObject,
65 res: *mut crate::ffi::GAsyncResult,
66 user_data: glib::ffi::gpointer,
67 ) {
68 let mut error = std::ptr::null_mut();
69 ffi::g_mount_eject_with_operation_finish(_source_object as *mut _, res, &mut error);
70 let result = if error.is_null() {
71 Ok(())
72 } else {
73 Err(from_glib_full(error))
74 };
75 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
76 Box_::from_raw(user_data as *mut _);
77 let callback: P = callback.into_inner();
78 callback(result);
79 }
80 let callback = eject_with_operation_trampoline::<P>;
81 unsafe {
82 ffi::g_mount_eject_with_operation(
83 self.as_ref().to_glib_none().0,
84 flags.into_glib(),
85 mount_operation.map(|p| p.as_ref()).to_glib_none().0,
86 cancellable.map(|p| p.as_ref()).to_glib_none().0,
87 Some(callback),
88 Box_::into_raw(user_data) as *mut _,
89 );
90 }
91 }
92
93 fn eject_with_operation_future(
94 &self,
95 flags: MountUnmountFlags,
96 mount_operation: Option<&(impl IsA<MountOperation> + Clone + 'static)>,
97 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
98 let mount_operation = mount_operation.map(ToOwned::to_owned);
99 Box_::pin(crate::GioFuture::new(
100 self,
101 move |obj, cancellable, send| {
102 obj.eject_with_operation(
103 flags,
104 mount_operation.as_ref().map(::std::borrow::Borrow::borrow),
105 Some(cancellable),
106 move |res| {
107 send.resolve(res);
108 },
109 );
110 },
111 ))
112 }
113
114 #[doc(alias = "g_mount_get_default_location")]
115 #[doc(alias = "get_default_location")]
116 fn default_location(&self) -> File {
117 unsafe {
118 from_glib_full(ffi::g_mount_get_default_location(
119 self.as_ref().to_glib_none().0,
120 ))
121 }
122 }
123
124 #[doc(alias = "g_mount_get_drive")]
125 #[doc(alias = "get_drive")]
126 fn drive(&self) -> Option<Drive> {
127 unsafe { from_glib_full(ffi::g_mount_get_drive(self.as_ref().to_glib_none().0)) }
128 }
129
130 #[doc(alias = "g_mount_get_icon")]
131 #[doc(alias = "get_icon")]
132 fn icon(&self) -> Icon {
133 unsafe { from_glib_full(ffi::g_mount_get_icon(self.as_ref().to_glib_none().0)) }
134 }
135
136 #[doc(alias = "g_mount_get_name")]
137 #[doc(alias = "get_name")]
138 fn name(&self) -> glib::GString {
139 unsafe { from_glib_full(ffi::g_mount_get_name(self.as_ref().to_glib_none().0)) }
140 }
141
142 #[doc(alias = "g_mount_get_root")]
143 #[doc(alias = "get_root")]
144 fn root(&self) -> File {
145 unsafe { from_glib_full(ffi::g_mount_get_root(self.as_ref().to_glib_none().0)) }
146 }
147
148 #[doc(alias = "g_mount_get_sort_key")]
149 #[doc(alias = "get_sort_key")]
150 fn sort_key(&self) -> Option<glib::GString> {
151 unsafe { from_glib_none(ffi::g_mount_get_sort_key(self.as_ref().to_glib_none().0)) }
152 }
153
154 #[doc(alias = "g_mount_get_symbolic_icon")]
155 #[doc(alias = "get_symbolic_icon")]
156 fn symbolic_icon(&self) -> Icon {
157 unsafe {
158 from_glib_full(ffi::g_mount_get_symbolic_icon(
159 self.as_ref().to_glib_none().0,
160 ))
161 }
162 }
163
164 #[doc(alias = "g_mount_get_uuid")]
165 #[doc(alias = "get_uuid")]
166 fn uuid(&self) -> Option<glib::GString> {
167 unsafe { from_glib_full(ffi::g_mount_get_uuid(self.as_ref().to_glib_none().0)) }
168 }
169
170 #[doc(alias = "g_mount_get_volume")]
171 #[doc(alias = "get_volume")]
172 fn volume(&self) -> Option<Volume> {
173 unsafe { from_glib_full(ffi::g_mount_get_volume(self.as_ref().to_glib_none().0)) }
174 }
175
176 #[doc(alias = "g_mount_guess_content_type")]
177 fn guess_content_type<P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static>(
178 &self,
179 force_rescan: bool,
180 cancellable: Option<&impl IsA<Cancellable>>,
181 callback: P,
182 ) {
183 let main_context = glib::MainContext::ref_thread_default();
184 let is_main_context_owner = main_context.is_owner();
185 let has_acquired_main_context = (!is_main_context_owner)
186 .then(|| main_context.acquire().ok())
187 .flatten();
188 assert!(
189 is_main_context_owner || has_acquired_main_context.is_some(),
190 "Async operations only allowed if the thread is owning the MainContext"
191 );
192
193 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
194 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
195 unsafe extern "C" fn guess_content_type_trampoline<
196 P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static,
197 >(
198 _source_object: *mut glib::gobject_ffi::GObject,
199 res: *mut crate::ffi::GAsyncResult,
200 user_data: glib::ffi::gpointer,
201 ) {
202 let mut error = std::ptr::null_mut();
203 let ret =
204 ffi::g_mount_guess_content_type_finish(_source_object as *mut _, res, &mut error);
205 let result = if error.is_null() {
206 Ok(FromGlibPtrContainer::from_glib_full(ret))
207 } else {
208 Err(from_glib_full(error))
209 };
210 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
211 Box_::from_raw(user_data as *mut _);
212 let callback: P = callback.into_inner();
213 callback(result);
214 }
215 let callback = guess_content_type_trampoline::<P>;
216 unsafe {
217 ffi::g_mount_guess_content_type(
218 self.as_ref().to_glib_none().0,
219 force_rescan.into_glib(),
220 cancellable.map(|p| p.as_ref()).to_glib_none().0,
221 Some(callback),
222 Box_::into_raw(user_data) as *mut _,
223 );
224 }
225 }
226
227 fn guess_content_type_future(
228 &self,
229 force_rescan: bool,
230 ) -> Pin<
231 Box_<dyn std::future::Future<Output = Result<Vec<glib::GString>, glib::Error>> + 'static>,
232 > {
233 Box_::pin(crate::GioFuture::new(
234 self,
235 move |obj, cancellable, send| {
236 obj.guess_content_type(force_rescan, Some(cancellable), move |res| {
237 send.resolve(res);
238 });
239 },
240 ))
241 }
242
243 #[doc(alias = "g_mount_guess_content_type_sync")]
244 fn guess_content_type_sync(
245 &self,
246 force_rescan: bool,
247 cancellable: Option<&impl IsA<Cancellable>>,
248 ) -> Result<Vec<glib::GString>, glib::Error> {
249 unsafe {
250 let mut error = std::ptr::null_mut();
251 let ret = ffi::g_mount_guess_content_type_sync(
252 self.as_ref().to_glib_none().0,
253 force_rescan.into_glib(),
254 cancellable.map(|p| p.as_ref()).to_glib_none().0,
255 &mut error,
256 );
257 if error.is_null() {
258 Ok(FromGlibPtrContainer::from_glib_full(ret))
259 } else {
260 Err(from_glib_full(error))
261 }
262 }
263 }
264
265 #[doc(alias = "g_mount_is_shadowed")]
266 fn is_shadowed(&self) -> bool {
267 unsafe { from_glib(ffi::g_mount_is_shadowed(self.as_ref().to_glib_none().0)) }
268 }
269
270 #[doc(alias = "g_mount_remount")]
271 fn remount<P: FnOnce(Result<(), glib::Error>) + 'static>(
272 &self,
273 flags: MountMountFlags,
274 mount_operation: Option<&impl IsA<MountOperation>>,
275 cancellable: Option<&impl IsA<Cancellable>>,
276 callback: P,
277 ) {
278 let main_context = glib::MainContext::ref_thread_default();
279 let is_main_context_owner = main_context.is_owner();
280 let has_acquired_main_context = (!is_main_context_owner)
281 .then(|| main_context.acquire().ok())
282 .flatten();
283 assert!(
284 is_main_context_owner || has_acquired_main_context.is_some(),
285 "Async operations only allowed if the thread is owning the MainContext"
286 );
287
288 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
289 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
290 unsafe extern "C" fn remount_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
291 _source_object: *mut glib::gobject_ffi::GObject,
292 res: *mut crate::ffi::GAsyncResult,
293 user_data: glib::ffi::gpointer,
294 ) {
295 let mut error = std::ptr::null_mut();
296 ffi::g_mount_remount_finish(_source_object as *mut _, res, &mut error);
297 let result = if error.is_null() {
298 Ok(())
299 } else {
300 Err(from_glib_full(error))
301 };
302 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
303 Box_::from_raw(user_data as *mut _);
304 let callback: P = callback.into_inner();
305 callback(result);
306 }
307 let callback = remount_trampoline::<P>;
308 unsafe {
309 ffi::g_mount_remount(
310 self.as_ref().to_glib_none().0,
311 flags.into_glib(),
312 mount_operation.map(|p| p.as_ref()).to_glib_none().0,
313 cancellable.map(|p| p.as_ref()).to_glib_none().0,
314 Some(callback),
315 Box_::into_raw(user_data) as *mut _,
316 );
317 }
318 }
319
320 fn remount_future(
321 &self,
322 flags: MountMountFlags,
323 mount_operation: Option<&(impl IsA<MountOperation> + Clone + 'static)>,
324 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
325 let mount_operation = mount_operation.map(ToOwned::to_owned);
326 Box_::pin(crate::GioFuture::new(
327 self,
328 move |obj, cancellable, send| {
329 obj.remount(
330 flags,
331 mount_operation.as_ref().map(::std::borrow::Borrow::borrow),
332 Some(cancellable),
333 move |res| {
334 send.resolve(res);
335 },
336 );
337 },
338 ))
339 }
340
341 #[doc(alias = "g_mount_shadow")]
342 fn shadow(&self) {
343 unsafe {
344 ffi::g_mount_shadow(self.as_ref().to_glib_none().0);
345 }
346 }
347
348 #[doc(alias = "g_mount_unmount_with_operation")]
349 fn unmount_with_operation<P: FnOnce(Result<(), glib::Error>) + 'static>(
350 &self,
351 flags: MountUnmountFlags,
352 mount_operation: Option<&impl IsA<MountOperation>>,
353 cancellable: Option<&impl IsA<Cancellable>>,
354 callback: P,
355 ) {
356 let main_context = glib::MainContext::ref_thread_default();
357 let is_main_context_owner = main_context.is_owner();
358 let has_acquired_main_context = (!is_main_context_owner)
359 .then(|| main_context.acquire().ok())
360 .flatten();
361 assert!(
362 is_main_context_owner || has_acquired_main_context.is_some(),
363 "Async operations only allowed if the thread is owning the MainContext"
364 );
365
366 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
367 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
368 unsafe extern "C" fn unmount_with_operation_trampoline<
369 P: FnOnce(Result<(), glib::Error>) + 'static,
370 >(
371 _source_object: *mut glib::gobject_ffi::GObject,
372 res: *mut crate::ffi::GAsyncResult,
373 user_data: glib::ffi::gpointer,
374 ) {
375 let mut error = std::ptr::null_mut();
376 ffi::g_mount_unmount_with_operation_finish(_source_object as *mut _, res, &mut error);
377 let result = if error.is_null() {
378 Ok(())
379 } else {
380 Err(from_glib_full(error))
381 };
382 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
383 Box_::from_raw(user_data as *mut _);
384 let callback: P = callback.into_inner();
385 callback(result);
386 }
387 let callback = unmount_with_operation_trampoline::<P>;
388 unsafe {
389 ffi::g_mount_unmount_with_operation(
390 self.as_ref().to_glib_none().0,
391 flags.into_glib(),
392 mount_operation.map(|p| p.as_ref()).to_glib_none().0,
393 cancellable.map(|p| p.as_ref()).to_glib_none().0,
394 Some(callback),
395 Box_::into_raw(user_data) as *mut _,
396 );
397 }
398 }
399
400 fn unmount_with_operation_future(
401 &self,
402 flags: MountUnmountFlags,
403 mount_operation: Option<&(impl IsA<MountOperation> + Clone + 'static)>,
404 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
405 let mount_operation = mount_operation.map(ToOwned::to_owned);
406 Box_::pin(crate::GioFuture::new(
407 self,
408 move |obj, cancellable, send| {
409 obj.unmount_with_operation(
410 flags,
411 mount_operation.as_ref().map(::std::borrow::Borrow::borrow),
412 Some(cancellable),
413 move |res| {
414 send.resolve(res);
415 },
416 );
417 },
418 ))
419 }
420
421 #[doc(alias = "g_mount_unshadow")]
422 fn unshadow(&self) {
423 unsafe {
424 ffi::g_mount_unshadow(self.as_ref().to_glib_none().0);
425 }
426 }
427
428 #[doc(alias = "changed")]
429 fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
430 unsafe extern "C" fn changed_trampoline<P: IsA<Mount>, F: Fn(&P) + 'static>(
431 this: *mut ffi::GMount,
432 f: glib::ffi::gpointer,
433 ) {
434 let f: &F = &*(f as *const F);
435 f(Mount::from_glib_borrow(this).unsafe_cast_ref())
436 }
437 unsafe {
438 let f: Box_<F> = Box_::new(f);
439 connect_raw(
440 self.as_ptr() as *mut _,
441 c"changed".as_ptr() as *const _,
442 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
443 changed_trampoline::<Self, F> as *const (),
444 )),
445 Box_::into_raw(f),
446 )
447 }
448 }
449
450 #[doc(alias = "pre-unmount")]
451 fn connect_pre_unmount<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
452 unsafe extern "C" fn pre_unmount_trampoline<P: IsA<Mount>, F: Fn(&P) + 'static>(
453 this: *mut ffi::GMount,
454 f: glib::ffi::gpointer,
455 ) {
456 let f: &F = &*(f as *const F);
457 f(Mount::from_glib_borrow(this).unsafe_cast_ref())
458 }
459 unsafe {
460 let f: Box_<F> = Box_::new(f);
461 connect_raw(
462 self.as_ptr() as *mut _,
463 c"pre-unmount".as_ptr() as *const _,
464 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
465 pre_unmount_trampoline::<Self, F> as *const (),
466 )),
467 Box_::into_raw(f),
468 )
469 }
470 }
471
472 #[doc(alias = "unmounted")]
473 fn connect_unmounted<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
474 unsafe extern "C" fn unmounted_trampoline<P: IsA<Mount>, F: Fn(&P) + 'static>(
475 this: *mut ffi::GMount,
476 f: glib::ffi::gpointer,
477 ) {
478 let f: &F = &*(f as *const F);
479 f(Mount::from_glib_borrow(this).unsafe_cast_ref())
480 }
481 unsafe {
482 let f: Box_<F> = Box_::new(f);
483 connect_raw(
484 self.as_ptr() as *mut _,
485 c"unmounted".as_ptr() as *const _,
486 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
487 unmounted_trampoline::<Self, F> as *const (),
488 )),
489 Box_::into_raw(f),
490 )
491 }
492 }
493}
494
495impl<O: IsA<Mount>> MountExt for O {}