polkit_rs/auto/
permission.rs

1// Generated by gir (https://github.com/gtk-rs/gir @ b2a1c6f9b362)
2// from ../misc (@ ???)
3// DO NOT EDIT
4
5use crate::{Subject, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    #[doc(alias = "PolkitPermission")]
11    pub struct Permission(Object<ffi::PolkitPermission>) @extends gio::Permission;
12
13    match fn {
14        type_ => || ffi::polkit_permission_get_type(),
15    }
16}
17
18impl Permission {
19    #[doc(alias = "polkit_permission_new_sync")]
20    pub fn new_sync(
21        action_id: &str,
22        subject: Option<&impl IsA<Subject>>,
23        cancellable: Option<&impl IsA<gio::Cancellable>>,
24    ) -> Result<Self, glib::Error> {
25        unsafe {
26            let mut error = std::ptr::null_mut();
27            let ret = ffi::polkit_permission_new_sync(
28                action_id.to_glib_none().0,
29                subject.map(|p| p.as_ref()).to_glib_none().0,
30                cancellable.map(|p| p.as_ref()).to_glib_none().0,
31                &mut error,
32            );
33            if error.is_null() {
34                Ok(gio::Permission::from_glib_full(ret).unsafe_cast())
35            } else {
36                Err(from_glib_full(error))
37            }
38        }
39    }
40
41    #[doc(alias = "polkit_permission_get_action_id")]
42    #[doc(alias = "get_action_id")]
43    #[doc(alias = "action-id")]
44    pub fn action_id(&self) -> glib::GString {
45        unsafe { from_glib_none(ffi::polkit_permission_get_action_id(self.to_glib_none().0)) }
46    }
47
48    #[doc(alias = "polkit_permission_get_subject")]
49    #[doc(alias = "get_subject")]
50    pub fn subject(&self) -> Subject {
51        unsafe { from_glib_none(ffi::polkit_permission_get_subject(self.to_glib_none().0)) }
52    }
53
54    #[allow(clippy::new_ret_no_self)]
55    #[doc(alias = "polkit_permission_new")]
56    pub fn new_with_callback<P: FnOnce(Result<Self, glib::Error>) + 'static>(
57        action_id: &str,
58        subject: Option<&impl IsA<Subject>>,
59        cancellable: Option<&impl IsA<gio::Cancellable>>,
60        callback: P,
61    ) {
62        let main_context = glib::MainContext::ref_thread_default();
63        let is_main_context_owner = main_context.is_owner();
64        let has_acquired_main_context = (!is_main_context_owner)
65            .then(|| main_context.acquire().ok())
66            .flatten();
67        assert!(
68            is_main_context_owner || has_acquired_main_context.is_some(),
69            "Async operations only allowed if the thread is owning the MainContext"
70        );
71
72        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
73            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
74        unsafe extern "C" fn new_trampoline<
75            P: FnOnce(Result<Permission, glib::Error>) + 'static,
76        >(
77            _source_object: *mut glib::gobject_ffi::GObject,
78            res: *mut gio::ffi::GAsyncResult,
79            user_data: glib::ffi::gpointer,
80        ) {
81            unsafe {
82                let mut error = std::ptr::null_mut();
83                let ret = ffi::polkit_permission_new_finish(res, &mut error);
84                let result = if error.is_null() {
85                    let permission: gio::Permission = from_glib_full(ret);
86                    Ok(permission.unsafe_cast())
87                } else {
88                    Err(from_glib_full(error))
89                };
90                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
91                    Box_::from_raw(user_data as *mut _);
92                let callback: P = callback.into_inner();
93                callback(result);
94            }
95        }
96        let callback = new_trampoline::<P>;
97        unsafe {
98            ffi::polkit_permission_new(
99                action_id.to_glib_none().0,
100                subject.map(|p| p.as_ref()).to_glib_none().0,
101                cancellable.map(|p| p.as_ref()).to_glib_none().0,
102                Some(callback),
103                Box_::into_raw(user_data) as *mut _,
104            );
105        }
106    }
107
108    pub fn new_future(
109        action_id: &str,
110        subject: Option<&(impl IsA<Subject> + Clone + 'static)>,
111    ) -> Pin<Box_<dyn std::future::Future<Output = Result<Self, glib::Error>> + 'static>> {
112        let action_id = String::from(action_id);
113        let subject = subject.map(ToOwned::to_owned);
114        Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
115            Self::new_with_callback(
116                &action_id,
117                subject.as_ref().map(::std::borrow::Borrow::borrow),
118                Some(cancellable),
119                move |res| {
120                    send.resolve(res);
121                },
122            );
123        }))
124    }
125}