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