Skip to main content

gio/auto/
volume.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::{
6    AsyncResult, Cancellable, Drive, File, Icon, Mount, MountMountFlags, MountOperation,
7    MountUnmountFlags, ffi,
8};
9use glib::{
10    object::ObjectType as _,
11    prelude::*,
12    signal::{SignalHandlerId, connect_raw},
13    translate::*,
14};
15use std::{boxed::Box as Box_, pin::Pin};
16
17glib::wrapper! {
18    ///  for the volume. There can be
19    /// different kinds of identifiers, such as Hal UDIs, filesystem labels,
20    /// traditional Unix devices (e.g. `/dev/sda2`), UUIDs. GIO uses predefined
21    /// strings as names for the different kinds of identifiers:
22    /// `G_VOLUME_IDENTIFIER_KIND_UUID`, `G_VOLUME_IDENTIFIER_KIND_LABEL`, etc.
23    /// Use [`VolumeExt::identifier()`][crate::prelude::VolumeExt::identifier()] to obtain an identifier for a volume.
24    ///
25    /// Note that `G_VOLUME_IDENTIFIER_KIND_HAL_UDI` will only be available
26    /// when the GVFS hal volume monitor is in use. Other volume monitors
27    /// will generally be able to provide the `G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE`
28    /// identifier, which can be used to obtain a hal device by means of
29    /// `libhal_manager_find_device_string_match()`.
30    ///
31    /// ## Signals
32    ///
33    ///
34    /// #### `changed`
35    ///  Emitted when the volume has been changed.
36    ///
37    ///
38    ///
39    ///
40    /// #### `removed`
41    ///  This signal is emitted when the #GVolume have been removed. If
42    /// the recipient is holding references to the object they should
43    /// release them so the object can be finalized.
44    ///
45    ///
46    ///
47    /// # Implements
48    ///
49    /// [`VolumeExt`][trait@crate::prelude::VolumeExt]
50    #[doc(alias = "GVolume")]
51    pub struct Volume(Interface<ffi::GVolume, ffi::GVolumeIface>);
52
53    match fn {
54        type_ => || ffi::g_volume_get_type(),
55    }
56}
57
58impl Volume {
59    pub const NONE: Option<&'static Volume> = None;
60}
61
62/// Trait containing all [`struct@Volume`] methods.
63///
64/// # Implementors
65///
66/// [`Volume`][struct@crate::Volume]
67pub trait VolumeExt: IsA<Volume> + 'static {
68    /// Checks if a volume can be ejected.
69    ///
70    /// # Returns
71    ///
72    /// [`true`] if the @self can be ejected. [`false`] otherwise
73    #[doc(alias = "g_volume_can_eject")]
74    fn can_eject(&self) -> bool {
75        unsafe { from_glib(ffi::g_volume_can_eject(self.as_ref().to_glib_none().0)) }
76    }
77
78    /// Checks if a volume can be mounted.
79    ///
80    /// # Returns
81    ///
82    /// [`true`] if the @self can be mounted. [`false`] otherwise
83    #[doc(alias = "g_volume_can_mount")]
84    fn can_mount(&self) -> bool {
85        unsafe { from_glib(ffi::g_volume_can_mount(self.as_ref().to_glib_none().0)) }
86    }
87
88    /// Ejects a volume. This is an asynchronous operation, and is
89    /// finished by calling g_volume_eject_with_operation_finish() with the @self
90    /// and #GAsyncResult data returned in the @callback.
91    /// ## `flags`
92    /// flags affecting the unmount if required for eject
93    /// ## `mount_operation`
94    /// a #GMountOperation or [`None`] to
95    ///     avoid user interaction
96    /// ## `cancellable`
97    /// optional #GCancellable object, [`None`] to ignore
98    /// ## `callback`
99    /// a #GAsyncReadyCallback, or [`None`]
100    #[doc(alias = "g_volume_eject_with_operation")]
101    fn eject_with_operation<P: FnOnce(Result<(), glib::Error>) + 'static>(
102        &self,
103        flags: MountUnmountFlags,
104        mount_operation: Option<&impl IsA<MountOperation>>,
105        cancellable: Option<&impl IsA<Cancellable>>,
106        callback: P,
107    ) {
108        let main_context = glib::MainContext::ref_thread_default();
109        let is_main_context_owner = main_context.is_owner();
110        let has_acquired_main_context = (!is_main_context_owner)
111            .then(|| main_context.acquire().ok())
112            .flatten();
113        assert!(
114            is_main_context_owner || has_acquired_main_context.is_some(),
115            "Async operations only allowed if the thread is owning the MainContext"
116        );
117
118        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
119            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
120        unsafe extern "C" fn eject_with_operation_trampoline<
121            P: FnOnce(Result<(), glib::Error>) + 'static,
122        >(
123            _source_object: *mut glib::gobject_ffi::GObject,
124            res: *mut crate::ffi::GAsyncResult,
125            user_data: glib::ffi::gpointer,
126        ) {
127            unsafe {
128                let mut error = std::ptr::null_mut();
129                ffi::g_volume_eject_with_operation_finish(
130                    _source_object as *mut _,
131                    res,
132                    &mut error,
133                );
134                let result = if error.is_null() {
135                    Ok(())
136                } else {
137                    Err(from_glib_full(error))
138                };
139                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
140                    Box_::from_raw(user_data as *mut _);
141                let callback: P = callback.into_inner();
142                callback(result);
143            }
144        }
145        let callback = eject_with_operation_trampoline::<P>;
146        unsafe {
147            ffi::g_volume_eject_with_operation(
148                self.as_ref().to_glib_none().0,
149                flags.into_glib(),
150                mount_operation.map(|p| p.as_ref()).to_glib_none().0,
151                cancellable.map(|p| p.as_ref()).to_glib_none().0,
152                Some(callback),
153                Box_::into_raw(user_data) as *mut _,
154            );
155        }
156    }
157
158    fn eject_with_operation_future(
159        &self,
160        flags: MountUnmountFlags,
161        mount_operation: Option<&(impl IsA<MountOperation> + Clone + 'static)>,
162    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
163        let mount_operation = mount_operation.map(ToOwned::to_owned);
164        Box_::pin(crate::GioFuture::new(
165            self,
166            move |obj, cancellable, send| {
167                obj.eject_with_operation(
168                    flags,
169                    mount_operation.as_ref().map(::std::borrow::Borrow::borrow),
170                    Some(cancellable),
171                    move |res| {
172                        send.resolve(res);
173                    },
174                );
175            },
176        ))
177    }
178
179    /// Gets the kinds of [identifiers](#volume-identifiers) that @self has.
180    /// Use g_volume_get_identifier() to obtain the identifiers themselves.
181    ///
182    /// # Returns
183    ///
184    /// a [`None`]-terminated array
185    ///   of strings containing kinds of identifiers. Use g_strfreev() to free.
186    #[doc(alias = "g_volume_enumerate_identifiers")]
187    fn enumerate_identifiers(&self) -> Vec<glib::GString> {
188        unsafe {
189            FromGlibPtrContainer::from_glib_full(ffi::g_volume_enumerate_identifiers(
190                self.as_ref().to_glib_none().0,
191            ))
192        }
193    }
194
195    ///
196    ///   (g_file_has_prefix (volume_activation_root, mount_root) ||
197    ///    g_file_equal (volume_activation_root, mount_root))
198    /// ]|
199    /// will always be [`true`].
200    ///
201    /// Activation roots are typically used in #GVolumeMonitor
202    /// implementations to find the underlying mount to shadow, see
203    /// g_mount_is_shadowed() for more details.
204    ///
205    /// # Returns
206    ///
207    /// the activation root of @self
208    ///     or [`None`]. Use g_object_unref() to free.
209    #[doc(alias = "g_volume_get_activation_root")]
210    #[doc(alias = "get_activation_root")]
211    fn activation_root(&self) -> Option<File> {
212        unsafe {
213            from_glib_full(ffi::g_volume_get_activation_root(
214                self.as_ref().to_glib_none().0,
215            ))
216        }
217    }
218
219    /// Gets the drive for the @self.
220    ///
221    /// # Returns
222    ///
223    /// a #GDrive or [`None`] if @self is not
224    ///     associated with a drive. The returned object should be unreffed
225    ///     with g_object_unref() when no longer needed.
226    #[doc(alias = "g_volume_get_drive")]
227    #[doc(alias = "get_drive")]
228    fn drive(&self) -> Option<Drive> {
229        unsafe { from_glib_full(ffi::g_volume_get_drive(self.as_ref().to_glib_none().0)) }
230    }
231
232    /// Gets the icon for @self.
233    ///
234    /// # Returns
235    ///
236    /// a #GIcon.
237    ///     The returned object should be unreffed with g_object_unref()
238    ///     when no longer needed.
239    #[doc(alias = "g_volume_get_icon")]
240    #[doc(alias = "get_icon")]
241    fn icon(&self) -> Icon {
242        unsafe { from_glib_full(ffi::g_volume_get_icon(self.as_ref().to_glib_none().0)) }
243    }
244
245    /// Gets the identifier of the given kind for @self.
246    /// See the [introduction](#volume-identifiers) for more
247    /// information about volume identifiers.
248    /// ## `kind`
249    /// the kind of identifier to return
250    ///
251    /// # Returns
252    ///
253    /// a newly allocated string containing the
254    ///     requested identifier, or [`None`] if the #GVolume
255    ///     doesn't have this kind of identifier
256    #[doc(alias = "g_volume_get_identifier")]
257    #[doc(alias = "get_identifier")]
258    fn identifier(&self, kind: &str) -> Option<glib::GString> {
259        unsafe {
260            from_glib_full(ffi::g_volume_get_identifier(
261                self.as_ref().to_glib_none().0,
262                kind.to_glib_none().0,
263            ))
264        }
265    }
266
267    /// Gets the mount for the @self.
268    ///
269    /// # Returns
270    ///
271    /// a #GMount or [`None`] if @self isn't mounted.
272    ///     The returned object should be unreffed with g_object_unref()
273    ///     when no longer needed.
274    #[doc(alias = "g_volume_get_mount")]
275    fn get_mount(&self) -> Option<Mount> {
276        unsafe { from_glib_full(ffi::g_volume_get_mount(self.as_ref().to_glib_none().0)) }
277    }
278
279    /// Gets the name of @self.
280    ///
281    /// # Returns
282    ///
283    /// the name for the given @self. The returned string should
284    ///     be freed with g_free() when no longer needed.
285    #[doc(alias = "g_volume_get_name")]
286    #[doc(alias = "get_name")]
287    fn name(&self) -> glib::GString {
288        unsafe { from_glib_full(ffi::g_volume_get_name(self.as_ref().to_glib_none().0)) }
289    }
290
291    /// Gets the sort key for @self, if any.
292    ///
293    /// # Returns
294    ///
295    /// Sorting key for @self or [`None`] if no such key is available
296    #[doc(alias = "g_volume_get_sort_key")]
297    #[doc(alias = "get_sort_key")]
298    fn sort_key(&self) -> Option<glib::GString> {
299        unsafe { from_glib_none(ffi::g_volume_get_sort_key(self.as_ref().to_glib_none().0)) }
300    }
301
302    /// Gets the symbolic icon for @self.
303    ///
304    /// # Returns
305    ///
306    /// a #GIcon.
307    ///     The returned object should be unreffed with g_object_unref()
308    ///     when no longer needed.
309    #[doc(alias = "g_volume_get_symbolic_icon")]
310    #[doc(alias = "get_symbolic_icon")]
311    fn symbolic_icon(&self) -> Icon {
312        unsafe {
313            from_glib_full(ffi::g_volume_get_symbolic_icon(
314                self.as_ref().to_glib_none().0,
315            ))
316        }
317    }
318
319    /// Gets the UUID for the @self. The reference is typically based on
320    /// the file system UUID for the volume in question and should be
321    /// considered an opaque string. Returns [`None`] if there is no UUID
322    /// available.
323    ///
324    /// # Returns
325    ///
326    /// the UUID for @self or [`None`] if no UUID
327    ///     can be computed.
328    ///     The returned string should be freed with g_free()
329    ///     when no longer needed.
330    #[doc(alias = "g_volume_get_uuid")]
331    #[doc(alias = "get_uuid")]
332    fn uuid(&self) -> Option<glib::GString> {
333        unsafe { from_glib_full(ffi::g_volume_get_uuid(self.as_ref().to_glib_none().0)) }
334    }
335
336    /// Mounts a volume. This is an asynchronous operation, and is
337    /// finished by calling g_volume_mount_finish() with the @self
338    /// and #GAsyncResult returned in the @callback.
339    /// ## `flags`
340    /// flags affecting the operation
341    /// ## `mount_operation`
342    /// a #GMountOperation or [`None`] to avoid user interaction
343    /// ## `cancellable`
344    /// optional #GCancellable object, [`None`] to ignore
345    /// ## `callback`
346    /// a #GAsyncReadyCallback, or [`None`]
347    #[doc(alias = "g_volume_mount")]
348    fn mount<P: FnOnce(Result<(), glib::Error>) + 'static>(
349        &self,
350        flags: MountMountFlags,
351        mount_operation: Option<&impl IsA<MountOperation>>,
352        cancellable: Option<&impl IsA<Cancellable>>,
353        callback: P,
354    ) {
355        let main_context = glib::MainContext::ref_thread_default();
356        let is_main_context_owner = main_context.is_owner();
357        let has_acquired_main_context = (!is_main_context_owner)
358            .then(|| main_context.acquire().ok())
359            .flatten();
360        assert!(
361            is_main_context_owner || has_acquired_main_context.is_some(),
362            "Async operations only allowed if the thread is owning the MainContext"
363        );
364
365        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
366            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
367        unsafe extern "C" fn mount_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
368            _source_object: *mut glib::gobject_ffi::GObject,
369            res: *mut crate::ffi::GAsyncResult,
370            user_data: glib::ffi::gpointer,
371        ) {
372            unsafe {
373                let mut error = std::ptr::null_mut();
374                ffi::g_volume_mount_finish(_source_object as *mut _, res, &mut error);
375                let result = if error.is_null() {
376                    Ok(())
377                } else {
378                    Err(from_glib_full(error))
379                };
380                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
381                    Box_::from_raw(user_data as *mut _);
382                let callback: P = callback.into_inner();
383                callback(result);
384            }
385        }
386        let callback = mount_trampoline::<P>;
387        unsafe {
388            ffi::g_volume_mount(
389                self.as_ref().to_glib_none().0,
390                flags.into_glib(),
391                mount_operation.map(|p| p.as_ref()).to_glib_none().0,
392                cancellable.map(|p| p.as_ref()).to_glib_none().0,
393                Some(callback),
394                Box_::into_raw(user_data) as *mut _,
395            );
396        }
397    }
398
399    fn mount_future(
400        &self,
401        flags: MountMountFlags,
402        mount_operation: Option<&(impl IsA<MountOperation> + Clone + 'static)>,
403    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
404        let mount_operation = mount_operation.map(ToOwned::to_owned);
405        Box_::pin(crate::GioFuture::new(
406            self,
407            move |obj, cancellable, send| {
408                obj.mount(
409                    flags,
410                    mount_operation.as_ref().map(::std::borrow::Borrow::borrow),
411                    Some(cancellable),
412                    move |res| {
413                        send.resolve(res);
414                    },
415                );
416            },
417        ))
418    }
419
420    /// Returns whether the volume should be automatically mounted.
421    ///
422    /// # Returns
423    ///
424    /// [`true`] if the volume should be automatically mounted
425    #[doc(alias = "g_volume_should_automount")]
426    fn should_automount(&self) -> bool {
427        unsafe {
428            from_glib(ffi::g_volume_should_automount(
429                self.as_ref().to_glib_none().0,
430            ))
431        }
432    }
433
434    /// Emitted when the volume has been changed.
435    #[doc(alias = "changed")]
436    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
437        unsafe extern "C" fn changed_trampoline<P: IsA<Volume>, F: Fn(&P) + 'static>(
438            this: *mut ffi::GVolume,
439            f: glib::ffi::gpointer,
440        ) {
441            unsafe {
442                let f: &F = &*(f as *const F);
443                f(Volume::from_glib_borrow(this).unsafe_cast_ref())
444            }
445        }
446        unsafe {
447            let f: Box_<F> = Box_::new(f);
448            connect_raw(
449                self.as_ptr() as *mut _,
450                c"changed".as_ptr(),
451                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
452                    changed_trampoline::<Self, F> as *const (),
453                )),
454                Box_::into_raw(f),
455            )
456        }
457    }
458
459    /// This signal is emitted when the #GVolume have been removed. If
460    /// the recipient is holding references to the object they should
461    /// release them so the object can be finalized.
462    #[doc(alias = "removed")]
463    fn connect_removed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
464        unsafe extern "C" fn removed_trampoline<P: IsA<Volume>, F: Fn(&P) + 'static>(
465            this: *mut ffi::GVolume,
466            f: glib::ffi::gpointer,
467        ) {
468            unsafe {
469                let f: &F = &*(f as *const F);
470                f(Volume::from_glib_borrow(this).unsafe_cast_ref())
471            }
472        }
473        unsafe {
474            let f: Box_<F> = Box_::new(f);
475            connect_raw(
476                self.as_ptr() as *mut _,
477                c"removed".as_ptr(),
478                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
479                    removed_trampoline::<Self, F> as *const (),
480                )),
481                Box_::into_raw(f),
482            )
483        }
484    }
485}
486
487impl<O: IsA<Volume>> VolumeExt for O {}