gio/auto/
permission.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::{ffi, AsyncResult, Cancellable};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::{boxed::Box as Box_, pin::Pin};
12
13glib::wrapper! {
14    /// A `GPermission` represents the status of the caller’s permission to
15    /// perform a certain action.
16    ///
17    /// You can query if the action is currently allowed and if it is
18    /// possible to acquire the permission so that the action will be allowed
19    /// in the future.
20    ///
21    /// There is also an API to actually acquire the permission and one to
22    /// release it.
23    ///
24    /// As an example, a `GPermission` might represent the ability for the
25    /// user to write to a [`Settings`][crate::Settings] object.  This `GPermission` object
26    /// could then be used to decide if it is appropriate to show a “Click here to
27    /// unlock” button in a dialog and to provide the mechanism to invoke
28    /// when that button is clicked.
29    ///
30    /// This is an Abstract Base Class, you cannot instantiate it.
31    ///
32    /// ## Properties
33    ///
34    ///
35    /// #### `allowed`
36    ///  [`true`] if the caller currently has permission to perform the action that
37    /// @permission represents the permission to perform.
38    ///
39    /// Readable
40    ///
41    ///
42    /// #### `can-acquire`
43    ///  [`true`] if it is generally possible to acquire the permission by calling
44    /// g_permission_acquire().
45    ///
46    /// Readable
47    ///
48    ///
49    /// #### `can-release`
50    ///  [`true`] if it is generally possible to release the permission by calling
51    /// g_permission_release().
52    ///
53    /// Readable
54    ///
55    /// # Implements
56    ///
57    /// [`PermissionExt`][trait@crate::prelude::PermissionExt], [`trait@glib::ObjectExt`]
58    #[doc(alias = "GPermission")]
59    pub struct Permission(Object<ffi::GPermission, ffi::GPermissionClass>);
60
61    match fn {
62        type_ => || ffi::g_permission_get_type(),
63    }
64}
65
66impl Permission {
67    pub const NONE: Option<&'static Permission> = None;
68}
69
70/// Trait containing all [`struct@Permission`] methods.
71///
72/// # Implementors
73///
74/// [`Permission`][struct@crate::Permission], [`SimplePermission`][struct@crate::SimplePermission]
75pub trait PermissionExt: IsA<Permission> + 'static {
76    /// Attempts to acquire the permission represented by @self.
77    ///
78    /// The precise method by which this happens depends on the permission
79    /// and the underlying authentication mechanism.  A simple example is
80    /// that a dialog may appear asking the user to enter their password.
81    ///
82    /// You should check with g_permission_get_can_acquire() before calling
83    /// this function.
84    ///
85    /// If the permission is acquired then [`true`] is returned.  Otherwise,
86    /// [`false`] is returned and @error is set appropriately.
87    ///
88    /// This call is blocking, likely for a very long time (in the case that
89    /// user interaction is required).  See g_permission_acquire_async() for
90    /// the non-blocking version.
91    /// ## `cancellable`
92    /// a #GCancellable, or [`None`]
93    ///
94    /// # Returns
95    ///
96    /// [`true`] if the permission was successfully acquired
97    #[doc(alias = "g_permission_acquire")]
98    fn acquire(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<(), glib::Error> {
99        unsafe {
100            let mut error = std::ptr::null_mut();
101            let is_ok = ffi::g_permission_acquire(
102                self.as_ref().to_glib_none().0,
103                cancellable.map(|p| p.as_ref()).to_glib_none().0,
104                &mut error,
105            );
106            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
107            if error.is_null() {
108                Ok(())
109            } else {
110                Err(from_glib_full(error))
111            }
112        }
113    }
114
115    /// Attempts to acquire the permission represented by @self.
116    ///
117    /// This is the first half of the asynchronous version of
118    /// g_permission_acquire().
119    /// ## `cancellable`
120    /// a #GCancellable, or [`None`]
121    /// ## `callback`
122    /// the #GAsyncReadyCallback to call when done
123    #[doc(alias = "g_permission_acquire_async")]
124    fn acquire_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
125        &self,
126        cancellable: Option<&impl IsA<Cancellable>>,
127        callback: P,
128    ) {
129        let main_context = glib::MainContext::ref_thread_default();
130        let is_main_context_owner = main_context.is_owner();
131        let has_acquired_main_context = (!is_main_context_owner)
132            .then(|| main_context.acquire().ok())
133            .flatten();
134        assert!(
135            is_main_context_owner || has_acquired_main_context.is_some(),
136            "Async operations only allowed if the thread is owning the MainContext"
137        );
138
139        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
140            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
141        unsafe extern "C" fn acquire_async_trampoline<
142            P: FnOnce(Result<(), glib::Error>) + 'static,
143        >(
144            _source_object: *mut glib::gobject_ffi::GObject,
145            res: *mut crate::ffi::GAsyncResult,
146            user_data: glib::ffi::gpointer,
147        ) {
148            let mut error = std::ptr::null_mut();
149            ffi::g_permission_acquire_finish(_source_object as *mut _, res, &mut error);
150            let result = if error.is_null() {
151                Ok(())
152            } else {
153                Err(from_glib_full(error))
154            };
155            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
156                Box_::from_raw(user_data as *mut _);
157            let callback: P = callback.into_inner();
158            callback(result);
159        }
160        let callback = acquire_async_trampoline::<P>;
161        unsafe {
162            ffi::g_permission_acquire_async(
163                self.as_ref().to_glib_none().0,
164                cancellable.map(|p| p.as_ref()).to_glib_none().0,
165                Some(callback),
166                Box_::into_raw(user_data) as *mut _,
167            );
168        }
169    }
170
171    fn acquire_future(
172        &self,
173    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
174        Box_::pin(crate::GioFuture::new(
175            self,
176            move |obj, cancellable, send| {
177                obj.acquire_async(Some(cancellable), move |res| {
178                    send.resolve(res);
179                });
180            },
181        ))
182    }
183
184    /// Gets the value of the 'allowed' property.  This property is [`true`] if
185    /// the caller currently has permission to perform the action that
186    /// @self represents the permission to perform.
187    ///
188    /// # Returns
189    ///
190    /// the value of the 'allowed' property
191    #[doc(alias = "g_permission_get_allowed")]
192    #[doc(alias = "get_allowed")]
193    #[doc(alias = "allowed")]
194    fn is_allowed(&self) -> bool {
195        unsafe {
196            from_glib(ffi::g_permission_get_allowed(
197                self.as_ref().to_glib_none().0,
198            ))
199        }
200    }
201
202    /// Gets the value of the 'can-acquire' property.  This property is [`true`]
203    /// if it is generally possible to acquire the permission by calling
204    /// g_permission_acquire().
205    ///
206    /// # Returns
207    ///
208    /// the value of the 'can-acquire' property
209    #[doc(alias = "g_permission_get_can_acquire")]
210    #[doc(alias = "get_can_acquire")]
211    #[doc(alias = "can-acquire")]
212    fn can_acquire(&self) -> bool {
213        unsafe {
214            from_glib(ffi::g_permission_get_can_acquire(
215                self.as_ref().to_glib_none().0,
216            ))
217        }
218    }
219
220    /// Gets the value of the 'can-release' property.  This property is [`true`]
221    /// if it is generally possible to release the permission by calling
222    /// g_permission_release().
223    ///
224    /// # Returns
225    ///
226    /// the value of the 'can-release' property
227    #[doc(alias = "g_permission_get_can_release")]
228    #[doc(alias = "get_can_release")]
229    #[doc(alias = "can-release")]
230    fn can_release(&self) -> bool {
231        unsafe {
232            from_glib(ffi::g_permission_get_can_release(
233                self.as_ref().to_glib_none().0,
234            ))
235        }
236    }
237
238    /// This function is called by the #GPermission implementation to update
239    /// the properties of the permission.  You should never call this
240    /// function except from a #GPermission implementation.
241    ///
242    /// GObject notify signals are generated, as appropriate.
243    /// ## `allowed`
244    /// the new value for the 'allowed' property
245    /// ## `can_acquire`
246    /// the new value for the 'can-acquire' property
247    /// ## `can_release`
248    /// the new value for the 'can-release' property
249    #[doc(alias = "g_permission_impl_update")]
250    fn impl_update(&self, allowed: bool, can_acquire: bool, can_release: bool) {
251        unsafe {
252            ffi::g_permission_impl_update(
253                self.as_ref().to_glib_none().0,
254                allowed.into_glib(),
255                can_acquire.into_glib(),
256                can_release.into_glib(),
257            );
258        }
259    }
260
261    /// Attempts to release the permission represented by @self.
262    ///
263    /// The precise method by which this happens depends on the permission
264    /// and the underlying authentication mechanism.  In most cases the
265    /// permission will be dropped immediately without further action.
266    ///
267    /// You should check with g_permission_get_can_release() before calling
268    /// this function.
269    ///
270    /// If the permission is released then [`true`] is returned.  Otherwise,
271    /// [`false`] is returned and @error is set appropriately.
272    ///
273    /// This call is blocking, likely for a very long time (in the case that
274    /// user interaction is required).  See g_permission_release_async() for
275    /// the non-blocking version.
276    /// ## `cancellable`
277    /// a #GCancellable, or [`None`]
278    ///
279    /// # Returns
280    ///
281    /// [`true`] if the permission was successfully released
282    #[doc(alias = "g_permission_release")]
283    fn release(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<(), glib::Error> {
284        unsafe {
285            let mut error = std::ptr::null_mut();
286            let is_ok = ffi::g_permission_release(
287                self.as_ref().to_glib_none().0,
288                cancellable.map(|p| p.as_ref()).to_glib_none().0,
289                &mut error,
290            );
291            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
292            if error.is_null() {
293                Ok(())
294            } else {
295                Err(from_glib_full(error))
296            }
297        }
298    }
299
300    /// Attempts to release the permission represented by @self.
301    ///
302    /// This is the first half of the asynchronous version of
303    /// g_permission_release().
304    /// ## `cancellable`
305    /// a #GCancellable, or [`None`]
306    /// ## `callback`
307    /// the #GAsyncReadyCallback to call when done
308    #[doc(alias = "g_permission_release_async")]
309    fn release_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
310        &self,
311        cancellable: Option<&impl IsA<Cancellable>>,
312        callback: P,
313    ) {
314        let main_context = glib::MainContext::ref_thread_default();
315        let is_main_context_owner = main_context.is_owner();
316        let has_acquired_main_context = (!is_main_context_owner)
317            .then(|| main_context.acquire().ok())
318            .flatten();
319        assert!(
320            is_main_context_owner || has_acquired_main_context.is_some(),
321            "Async operations only allowed if the thread is owning the MainContext"
322        );
323
324        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
325            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
326        unsafe extern "C" fn release_async_trampoline<
327            P: FnOnce(Result<(), glib::Error>) + 'static,
328        >(
329            _source_object: *mut glib::gobject_ffi::GObject,
330            res: *mut crate::ffi::GAsyncResult,
331            user_data: glib::ffi::gpointer,
332        ) {
333            let mut error = std::ptr::null_mut();
334            ffi::g_permission_release_finish(_source_object as *mut _, res, &mut error);
335            let result = if error.is_null() {
336                Ok(())
337            } else {
338                Err(from_glib_full(error))
339            };
340            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
341                Box_::from_raw(user_data as *mut _);
342            let callback: P = callback.into_inner();
343            callback(result);
344        }
345        let callback = release_async_trampoline::<P>;
346        unsafe {
347            ffi::g_permission_release_async(
348                self.as_ref().to_glib_none().0,
349                cancellable.map(|p| p.as_ref()).to_glib_none().0,
350                Some(callback),
351                Box_::into_raw(user_data) as *mut _,
352            );
353        }
354    }
355
356    fn release_future(
357        &self,
358    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
359        Box_::pin(crate::GioFuture::new(
360            self,
361            move |obj, cancellable, send| {
362                obj.release_async(Some(cancellable), move |res| {
363                    send.resolve(res);
364                });
365            },
366        ))
367    }
368
369    #[doc(alias = "allowed")]
370    fn connect_allowed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
371        unsafe extern "C" fn notify_allowed_trampoline<P: IsA<Permission>, F: Fn(&P) + 'static>(
372            this: *mut ffi::GPermission,
373            _param_spec: glib::ffi::gpointer,
374            f: glib::ffi::gpointer,
375        ) {
376            let f: &F = &*(f as *const F);
377            f(Permission::from_glib_borrow(this).unsafe_cast_ref())
378        }
379        unsafe {
380            let f: Box_<F> = Box_::new(f);
381            connect_raw(
382                self.as_ptr() as *mut _,
383                c"notify::allowed".as_ptr() as *const _,
384                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
385                    notify_allowed_trampoline::<Self, F> as *const (),
386                )),
387                Box_::into_raw(f),
388            )
389        }
390    }
391
392    #[doc(alias = "can-acquire")]
393    fn connect_can_acquire_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
394        unsafe extern "C" fn notify_can_acquire_trampoline<
395            P: IsA<Permission>,
396            F: Fn(&P) + 'static,
397        >(
398            this: *mut ffi::GPermission,
399            _param_spec: glib::ffi::gpointer,
400            f: glib::ffi::gpointer,
401        ) {
402            let f: &F = &*(f as *const F);
403            f(Permission::from_glib_borrow(this).unsafe_cast_ref())
404        }
405        unsafe {
406            let f: Box_<F> = Box_::new(f);
407            connect_raw(
408                self.as_ptr() as *mut _,
409                c"notify::can-acquire".as_ptr() as *const _,
410                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
411                    notify_can_acquire_trampoline::<Self, F> as *const (),
412                )),
413                Box_::into_raw(f),
414            )
415        }
416    }
417
418    #[doc(alias = "can-release")]
419    fn connect_can_release_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
420        unsafe extern "C" fn notify_can_release_trampoline<
421            P: IsA<Permission>,
422            F: Fn(&P) + 'static,
423        >(
424            this: *mut ffi::GPermission,
425            _param_spec: glib::ffi::gpointer,
426            f: glib::ffi::gpointer,
427        ) {
428            let f: &F = &*(f as *const F);
429            f(Permission::from_glib_borrow(this).unsafe_cast_ref())
430        }
431        unsafe {
432            let f: Box_<F> = Box_::new(f);
433            connect_raw(
434                self.as_ptr() as *mut _,
435                c"notify::can-release".as_ptr() as *const _,
436                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
437                    notify_can_release_trampoline::<Self, F> as *const (),
438                )),
439                Box_::into_raw(f),
440            )
441        }
442    }
443}
444
445impl<O: IsA<Permission>> PermissionExt for O {}