Skip to main content

gio/auto/
dbus_auth_observer.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::{Credentials, IOStream, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// s equivalent
16    /// to the following signal handler:
17    ///
18    /// **⚠️ The following code is in c ⚠️**
19    ///
20    /// ```c
21    /// static gboolean
22    /// on_authorize_authenticated_peer (GDBusAuthObserver *observer,
23    ///                                  GIOStream         *stream,
24    ///                                  GCredentials      *credentials,
25    ///                                  gpointer           user_data)
26    /// {
27    ///   gboolean authorized;
28    ///
29    ///   authorized = FALSE;
30    ///   if (credentials != NULL)
31    ///     {
32    ///       GCredentials *own_credentials;
33    ///       own_credentials = g_credentials_new ();
34    ///       if (g_credentials_is_same_user (credentials, own_credentials, NULL))
35    ///         authorized = TRUE;
36    ///       g_object_unref (own_credentials);
37    ///     }
38    ///
39    ///   return authorized;
40    /// }
41    /// ```
42    ///
43    /// ## Signals
44    ///
45    ///
46    /// #### `allow-mechanism`
47    ///  Emitted to check if @mechanism is allowed to be used.
48    ///
49    ///
50    ///
51    ///
52    /// #### `authorize-authenticated-peer`
53    ///  Emitted to check if a peer that is successfully authenticated
54    /// is authorized.
55    ///
56    ///
57    ///
58    /// # Implements
59    ///
60    /// [`trait@glib::ObjectExt`]
61    #[doc(alias = "GDBusAuthObserver")]
62    pub struct DBusAuthObserver(Object<ffi::GDBusAuthObserver>);
63
64    match fn {
65        type_ => || ffi::g_dbus_auth_observer_get_type(),
66    }
67}
68
69impl DBusAuthObserver {
70    /// Creates a new #GDBusAuthObserver object.
71    ///
72    /// # Returns
73    ///
74    /// A #GDBusAuthObserver. Free with g_object_unref().
75    #[doc(alias = "g_dbus_auth_observer_new")]
76    pub fn new() -> DBusAuthObserver {
77        unsafe { from_glib_full(ffi::g_dbus_auth_observer_new()) }
78    }
79
80    /// Emits the #GDBusAuthObserver::allow-mechanism signal on @self.
81    /// ## `mechanism`
82    /// The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
83    ///
84    /// # Returns
85    ///
86    /// [`true`] if @mechanism can be used to authenticate the other peer, [`false`] if not.
87    #[doc(alias = "g_dbus_auth_observer_allow_mechanism")]
88    pub fn allow_mechanism(&self, mechanism: &str) -> bool {
89        unsafe {
90            from_glib(ffi::g_dbus_auth_observer_allow_mechanism(
91                self.to_glib_none().0,
92                mechanism.to_glib_none().0,
93            ))
94        }
95    }
96
97    /// Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @self.
98    /// ## `stream`
99    /// A #GIOStream for the #GDBusConnection.
100    /// ## `credentials`
101    /// Credentials received from the peer or [`None`].
102    ///
103    /// # Returns
104    ///
105    /// [`true`] if the peer is authorized, [`false`] if not.
106    #[doc(alias = "g_dbus_auth_observer_authorize_authenticated_peer")]
107    pub fn authorize_authenticated_peer(
108        &self,
109        stream: &impl IsA<IOStream>,
110        credentials: Option<&Credentials>,
111    ) -> bool {
112        unsafe {
113            from_glib(ffi::g_dbus_auth_observer_authorize_authenticated_peer(
114                self.to_glib_none().0,
115                stream.as_ref().to_glib_none().0,
116                credentials.to_glib_none().0,
117            ))
118        }
119    }
120
121    /// Emitted to check if @mechanism is allowed to be used.
122    /// ## `mechanism`
123    /// The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
124    ///
125    /// # Returns
126    ///
127    /// [`true`] if @mechanism can be used to authenticate the other peer, [`false`] if not.
128    #[doc(alias = "allow-mechanism")]
129    pub fn connect_allow_mechanism<F: Fn(&Self, &str) -> bool + 'static>(
130        &self,
131        f: F,
132    ) -> SignalHandlerId {
133        unsafe extern "C" fn allow_mechanism_trampoline<
134            F: Fn(&DBusAuthObserver, &str) -> bool + 'static,
135        >(
136            this: *mut ffi::GDBusAuthObserver,
137            mechanism: *mut std::ffi::c_char,
138            f: glib::ffi::gpointer,
139        ) -> glib::ffi::gboolean {
140            unsafe {
141                let f: &F = &*(f as *const F);
142                f(
143                    &from_glib_borrow(this),
144                    &glib::GString::from_glib_borrow(mechanism),
145                )
146                .into_glib()
147            }
148        }
149        unsafe {
150            let f: Box_<F> = Box_::new(f);
151            connect_raw(
152                self.as_ptr() as *mut _,
153                c"allow-mechanism".as_ptr(),
154                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
155                    allow_mechanism_trampoline::<F> as *const (),
156                )),
157                Box_::into_raw(f),
158            )
159        }
160    }
161
162    /// Emitted to check if a peer that is successfully authenticated
163    /// is authorized.
164    /// ## `stream`
165    /// A #GIOStream for the #GDBusConnection.
166    /// ## `credentials`
167    /// Credentials received from the peer or [`None`].
168    ///
169    /// # Returns
170    ///
171    /// [`true`] if the peer is authorized, [`false`] if not.
172    #[doc(alias = "authorize-authenticated-peer")]
173    pub fn connect_authorize_authenticated_peer<
174        F: Fn(&Self, &IOStream, Option<&Credentials>) -> bool + 'static,
175    >(
176        &self,
177        f: F,
178    ) -> SignalHandlerId {
179        unsafe extern "C" fn authorize_authenticated_peer_trampoline<
180            F: Fn(&DBusAuthObserver, &IOStream, Option<&Credentials>) -> bool + 'static,
181        >(
182            this: *mut ffi::GDBusAuthObserver,
183            stream: *mut ffi::GIOStream,
184            credentials: *mut ffi::GCredentials,
185            f: glib::ffi::gpointer,
186        ) -> glib::ffi::gboolean {
187            unsafe {
188                let f: &F = &*(f as *const F);
189                f(
190                    &from_glib_borrow(this),
191                    &from_glib_borrow(stream),
192                    Option::<Credentials>::from_glib_borrow(credentials)
193                        .as_ref()
194                        .as_ref(),
195                )
196                .into_glib()
197            }
198        }
199        unsafe {
200            let f: Box_<F> = Box_::new(f);
201            connect_raw(
202                self.as_ptr() as *mut _,
203                c"authorize-authenticated-peer".as_ptr(),
204                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
205                    authorize_authenticated_peer_trampoline::<F> as *const (),
206                )),
207                Box_::into_raw(f),
208            )
209        }
210    }
211}
212
213impl Default for DBusAuthObserver {
214    fn default() -> Self {
215        Self::new()
216    }
217}