gio/auto/tls_interaction.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, TlsCertificateRequestFlags, TlsConnection, TlsInteractionResult,
7 TlsPassword, ffi,
8};
9use glib::{prelude::*, translate::*};
10use std::{boxed::Box as Box_, pin::Pin};
11
12glib::wrapper! {
13 /// d
14 /// like to support by overriding those virtual methods in their class
15 /// initialization function. Any interactions not implemented will return
16 /// `G_TLS_INTERACTION_UNHANDLED`. If a derived class implements an async method,
17 /// it must also implement the corresponding finish method.
18 ///
19 /// # Implements
20 ///
21 /// [`TlsInteractionExt`][trait@crate::prelude::TlsInteractionExt], [`trait@glib::ObjectExt`]
22 #[doc(alias = "GTlsInteraction")]
23 pub struct TlsInteraction(Object<ffi::GTlsInteraction, ffi::GTlsInteractionClass>);
24
25 match fn {
26 type_ => || ffi::g_tls_interaction_get_type(),
27 }
28}
29
30impl TlsInteraction {
31 pub const NONE: Option<&'static TlsInteraction> = None;
32}
33
34/// Trait containing all [`struct@TlsInteraction`] methods.
35///
36/// # Implementors
37///
38/// [`TlsInteraction`][struct@crate::TlsInteraction]
39pub trait TlsInteractionExt: IsA<TlsInteraction> + 'static {
40 /// Run synchronous interaction to ask the user for a password. In general,
41 /// g_tls_interaction_invoke_ask_password() should be used instead of this
42 /// function.
43 ///
44 /// Derived subclasses usually implement a password prompt, although they may
45 /// also choose to provide a password from elsewhere. The @password value will
46 /// be filled in and then @callback will be called. Alternatively the user may
47 /// abort this password request, which will usually abort the TLS connection.
48 ///
49 /// If the interaction is cancelled by the cancellation object, or by the
50 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
51 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
52 /// not support immediate cancellation.
53 /// ## `password`
54 /// a #GTlsPassword object
55 /// ## `cancellable`
56 /// an optional #GCancellable cancellation object
57 ///
58 /// # Returns
59 ///
60 /// The status of the ask password interaction.
61 #[doc(alias = "g_tls_interaction_ask_password")]
62 fn ask_password(
63 &self,
64 password: &impl IsA<TlsPassword>,
65 cancellable: Option<&impl IsA<Cancellable>>,
66 ) -> Result<TlsInteractionResult, glib::Error> {
67 unsafe {
68 let mut error = std::ptr::null_mut();
69 let ret = ffi::g_tls_interaction_ask_password(
70 self.as_ref().to_glib_none().0,
71 password.as_ref().to_glib_none().0,
72 cancellable.map(|p| p.as_ref()).to_glib_none().0,
73 &mut error,
74 );
75 if error.is_null() {
76 Ok(from_glib(ret))
77 } else {
78 Err(from_glib_full(error))
79 }
80 }
81 }
82
83 /// Run asynchronous interaction to ask the user for a password. In general,
84 /// g_tls_interaction_invoke_ask_password() should be used instead of this
85 /// function.
86 ///
87 /// Derived subclasses usually implement a password prompt, although they may
88 /// also choose to provide a password from elsewhere. The @password value will
89 /// be filled in and then @callback will be called. Alternatively the user may
90 /// abort this password request, which will usually abort the TLS connection.
91 ///
92 /// If the interaction is cancelled by the cancellation object, or by the
93 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
94 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
95 /// not support immediate cancellation.
96 ///
97 /// Certain implementations may not support immediate cancellation.
98 /// ## `password`
99 /// a #GTlsPassword object
100 /// ## `cancellable`
101 /// an optional #GCancellable cancellation object
102 /// ## `callback`
103 /// will be called when the interaction completes
104 #[doc(alias = "g_tls_interaction_ask_password_async")]
105 fn ask_password_async<P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static>(
106 &self,
107 password: &impl IsA<TlsPassword>,
108 cancellable: Option<&impl IsA<Cancellable>>,
109 callback: P,
110 ) {
111 let main_context = glib::MainContext::ref_thread_default();
112 let is_main_context_owner = main_context.is_owner();
113 let has_acquired_main_context = (!is_main_context_owner)
114 .then(|| main_context.acquire().ok())
115 .flatten();
116 assert!(
117 is_main_context_owner || has_acquired_main_context.is_some(),
118 "Async operations only allowed if the thread is owning the MainContext"
119 );
120
121 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
122 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
123 unsafe extern "C" fn ask_password_async_trampoline<
124 P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static,
125 >(
126 _source_object: *mut glib::gobject_ffi::GObject,
127 res: *mut crate::ffi::GAsyncResult,
128 user_data: glib::ffi::gpointer,
129 ) {
130 unsafe {
131 let mut error = std::ptr::null_mut();
132 let ret = ffi::g_tls_interaction_ask_password_finish(
133 _source_object as *mut _,
134 res,
135 &mut error,
136 );
137 let result = if error.is_null() {
138 Ok(from_glib(ret))
139 } else {
140 Err(from_glib_full(error))
141 };
142 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
143 Box_::from_raw(user_data as *mut _);
144 let callback: P = callback.into_inner();
145 callback(result);
146 }
147 }
148 let callback = ask_password_async_trampoline::<P>;
149 unsafe {
150 ffi::g_tls_interaction_ask_password_async(
151 self.as_ref().to_glib_none().0,
152 password.as_ref().to_glib_none().0,
153 cancellable.map(|p| p.as_ref()).to_glib_none().0,
154 Some(callback),
155 Box_::into_raw(user_data) as *mut _,
156 );
157 }
158 }
159
160 fn ask_password_future(
161 &self,
162 password: &(impl IsA<TlsPassword> + Clone + 'static),
163 ) -> Pin<
164 Box_<dyn std::future::Future<Output = Result<TlsInteractionResult, glib::Error>> + 'static>,
165 > {
166 let password = password.clone();
167 Box_::pin(crate::GioFuture::new(
168 self,
169 move |obj, cancellable, send| {
170 obj.ask_password_async(&password, Some(cancellable), move |res| {
171 send.resolve(res);
172 });
173 },
174 ))
175 }
176
177 /// Invoke the interaction to ask the user for a password. It invokes this
178 /// interaction in the main loop, specifically the #GMainContext returned by
179 /// g_main_context_get_thread_default() when the interaction is created. This
180 /// is called by called by #GTlsConnection or #GTlsDatabase to ask the user
181 /// for a password.
182 ///
183 /// Derived subclasses usually implement a password prompt, although they may
184 /// also choose to provide a password from elsewhere. The @password value will
185 /// be filled in and then @callback will be called. Alternatively the user may
186 /// abort this password request, which will usually abort the TLS connection.
187 ///
188 /// The implementation can either be a synchronous (eg: modal dialog) or an
189 /// asynchronous one (eg: modeless dialog). This function will take care of
190 /// calling which ever one correctly.
191 ///
192 /// If the interaction is cancelled by the cancellation object, or by the
193 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
194 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
195 /// not support immediate cancellation.
196 /// ## `password`
197 /// a #GTlsPassword object
198 /// ## `cancellable`
199 /// an optional #GCancellable cancellation object
200 ///
201 /// # Returns
202 ///
203 /// The status of the ask password interaction.
204 #[doc(alias = "g_tls_interaction_invoke_ask_password")]
205 fn invoke_ask_password(
206 &self,
207 password: &impl IsA<TlsPassword>,
208 cancellable: Option<&impl IsA<Cancellable>>,
209 ) -> Result<TlsInteractionResult, glib::Error> {
210 unsafe {
211 let mut error = std::ptr::null_mut();
212 let ret = ffi::g_tls_interaction_invoke_ask_password(
213 self.as_ref().to_glib_none().0,
214 password.as_ref().to_glib_none().0,
215 cancellable.map(|p| p.as_ref()).to_glib_none().0,
216 &mut error,
217 );
218 if error.is_null() {
219 Ok(from_glib(ret))
220 } else {
221 Err(from_glib_full(error))
222 }
223 }
224 }
225
226 /// Invoke the interaction to ask the user to choose a certificate to
227 /// use with the connection. It invokes this interaction in the main
228 /// loop, specifically the #GMainContext returned by
229 /// g_main_context_get_thread_default() when the interaction is
230 /// created. This is called by called by #GTlsConnection when the peer
231 /// requests a certificate during the handshake.
232 ///
233 /// Derived subclasses usually implement a certificate selector,
234 /// although they may also choose to provide a certificate from
235 /// elsewhere. Alternatively the user may abort this certificate
236 /// request, which may or may not abort the TLS connection.
237 ///
238 /// The implementation can either be a synchronous (eg: modal dialog) or an
239 /// asynchronous one (eg: modeless dialog). This function will take care of
240 /// calling which ever one correctly.
241 ///
242 /// If the interaction is cancelled by the cancellation object, or by the
243 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
244 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
245 /// not support immediate cancellation.
246 /// ## `connection`
247 /// a #GTlsConnection object
248 /// ## `flags`
249 /// flags providing more information about the request
250 /// ## `cancellable`
251 /// an optional #GCancellable cancellation object
252 ///
253 /// # Returns
254 ///
255 /// The status of the certificate request interaction.
256 #[doc(alias = "g_tls_interaction_invoke_request_certificate")]
257 fn invoke_request_certificate(
258 &self,
259 connection: &impl IsA<TlsConnection>,
260 flags: TlsCertificateRequestFlags,
261 cancellable: Option<&impl IsA<Cancellable>>,
262 ) -> Result<TlsInteractionResult, glib::Error> {
263 unsafe {
264 let mut error = std::ptr::null_mut();
265 let ret = ffi::g_tls_interaction_invoke_request_certificate(
266 self.as_ref().to_glib_none().0,
267 connection.as_ref().to_glib_none().0,
268 flags.into_glib(),
269 cancellable.map(|p| p.as_ref()).to_glib_none().0,
270 &mut error,
271 );
272 if error.is_null() {
273 Ok(from_glib(ret))
274 } else {
275 Err(from_glib_full(error))
276 }
277 }
278 }
279
280 /// Run synchronous interaction to ask the user to choose a certificate to use
281 /// with the connection. In general, g_tls_interaction_invoke_request_certificate()
282 /// should be used instead of this function.
283 ///
284 /// Derived subclasses usually implement a certificate selector, although they may
285 /// also choose to provide a certificate from elsewhere. Alternatively the user may
286 /// abort this certificate request, which will usually abort the TLS connection.
287 ///
288 /// If [`TlsInteractionResult::Handled`][crate::TlsInteractionResult::Handled] is returned, then the #GTlsConnection
289 /// passed to g_tls_interaction_request_certificate() will have had its
290 /// #GTlsConnection:certificate filled in.
291 ///
292 /// If the interaction is cancelled by the cancellation object, or by the
293 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
294 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
295 /// not support immediate cancellation.
296 /// ## `connection`
297 /// a #GTlsConnection object
298 /// ## `flags`
299 /// flags providing more information about the request
300 /// ## `cancellable`
301 /// an optional #GCancellable cancellation object
302 ///
303 /// # Returns
304 ///
305 /// The status of the request certificate interaction.
306 #[doc(alias = "g_tls_interaction_request_certificate")]
307 fn request_certificate(
308 &self,
309 connection: &impl IsA<TlsConnection>,
310 flags: TlsCertificateRequestFlags,
311 cancellable: Option<&impl IsA<Cancellable>>,
312 ) -> Result<TlsInteractionResult, glib::Error> {
313 unsafe {
314 let mut error = std::ptr::null_mut();
315 let ret = ffi::g_tls_interaction_request_certificate(
316 self.as_ref().to_glib_none().0,
317 connection.as_ref().to_glib_none().0,
318 flags.into_glib(),
319 cancellable.map(|p| p.as_ref()).to_glib_none().0,
320 &mut error,
321 );
322 if error.is_null() {
323 Ok(from_glib(ret))
324 } else {
325 Err(from_glib_full(error))
326 }
327 }
328 }
329
330 /// Run asynchronous interaction to ask the user for a certificate to use with
331 /// the connection. In general, g_tls_interaction_invoke_request_certificate() should
332 /// be used instead of this function.
333 ///
334 /// Derived subclasses usually implement a certificate selector, although they may
335 /// also choose to provide a certificate from elsewhere. @callback will be called
336 /// when the operation completes. Alternatively the user may abort this certificate
337 /// request, which will usually abort the TLS connection.
338 /// ## `connection`
339 /// a #GTlsConnection object
340 /// ## `flags`
341 /// flags providing more information about the request
342 /// ## `cancellable`
343 /// an optional #GCancellable cancellation object
344 /// ## `callback`
345 /// will be called when the interaction completes
346 #[doc(alias = "g_tls_interaction_request_certificate_async")]
347 fn request_certificate_async<P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static>(
348 &self,
349 connection: &impl IsA<TlsConnection>,
350 flags: TlsCertificateRequestFlags,
351 cancellable: Option<&impl IsA<Cancellable>>,
352 callback: P,
353 ) {
354 let main_context = glib::MainContext::ref_thread_default();
355 let is_main_context_owner = main_context.is_owner();
356 let has_acquired_main_context = (!is_main_context_owner)
357 .then(|| main_context.acquire().ok())
358 .flatten();
359 assert!(
360 is_main_context_owner || has_acquired_main_context.is_some(),
361 "Async operations only allowed if the thread is owning the MainContext"
362 );
363
364 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
365 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
366 unsafe extern "C" fn request_certificate_async_trampoline<
367 P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static,
368 >(
369 _source_object: *mut glib::gobject_ffi::GObject,
370 res: *mut crate::ffi::GAsyncResult,
371 user_data: glib::ffi::gpointer,
372 ) {
373 unsafe {
374 let mut error = std::ptr::null_mut();
375 let ret = ffi::g_tls_interaction_request_certificate_finish(
376 _source_object as *mut _,
377 res,
378 &mut error,
379 );
380 let result = if error.is_null() {
381 Ok(from_glib(ret))
382 } else {
383 Err(from_glib_full(error))
384 };
385 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
386 Box_::from_raw(user_data as *mut _);
387 let callback: P = callback.into_inner();
388 callback(result);
389 }
390 }
391 let callback = request_certificate_async_trampoline::<P>;
392 unsafe {
393 ffi::g_tls_interaction_request_certificate_async(
394 self.as_ref().to_glib_none().0,
395 connection.as_ref().to_glib_none().0,
396 flags.into_glib(),
397 cancellable.map(|p| p.as_ref()).to_glib_none().0,
398 Some(callback),
399 Box_::into_raw(user_data) as *mut _,
400 );
401 }
402 }
403
404 fn request_certificate_future(
405 &self,
406 connection: &(impl IsA<TlsConnection> + Clone + 'static),
407 flags: TlsCertificateRequestFlags,
408 ) -> Pin<
409 Box_<dyn std::future::Future<Output = Result<TlsInteractionResult, glib::Error>> + 'static>,
410 > {
411 let connection = connection.clone();
412 Box_::pin(crate::GioFuture::new(
413 self,
414 move |obj, cancellable, send| {
415 obj.request_certificate_async(&connection, flags, Some(cancellable), move |res| {
416 send.resolve(res);
417 });
418 },
419 ))
420 }
421}
422
423impl<O: IsA<TlsInteraction>> TlsInteractionExt for O {}