1use std::boxed::Box as Box_;
4use std::pin::Pin;
5use std::ptr;
6
7use glib::prelude::*;
8use glib::translate::*;
9
10use crate::{AppInfo, AppLaunchContext, Cancellable, ffi};
11
12pub trait AppInfoExtManual: IsA<AppInfo> + 'static {
13 #[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
26 #[doc(alias = "g_app_info_launch_uris_async")]
27 fn launch_uris_async<
28 P: IsA<AppLaunchContext>,
29 Q: IsA<Cancellable>,
30 R: FnOnce(Result<(), glib::Error>) + 'static,
31 >(
32 &self,
33 uris: &[&str],
34 context: Option<&P>,
35 cancellable: Option<&Q>,
36 callback: R,
37 ) {
38 let main_context = glib::MainContext::ref_thread_default();
39 let is_main_context_owner = main_context.is_owner();
40 let has_acquired_main_context = (!is_main_context_owner)
41 .then(|| main_context.acquire().ok())
42 .flatten();
43 assert!(
44 is_main_context_owner || has_acquired_main_context.is_some(),
45 "Async operations only allowed if the thread is owning the MainContext"
46 );
47
48 let user_data: Box_<(glib::thread_guard::ThreadGuard<R>, *mut *mut libc::c_char)> =
49 Box_::new((
50 glib::thread_guard::ThreadGuard::new(callback),
51 uris.to_glib_full(),
52 ));
53 unsafe extern "C" fn launch_uris_async_trampoline<
54 R: FnOnce(Result<(), glib::Error>) + 'static,
55 >(
56 _source_object: *mut glib::gobject_ffi::GObject,
57 res: *mut ffi::GAsyncResult,
58 user_data: glib::ffi::gpointer,
59 ) {
60 unsafe {
61 let mut error = ptr::null_mut();
62 let _ =
63 ffi::g_app_info_launch_uris_finish(_source_object as *mut _, res, &mut error);
64 let result = if error.is_null() {
65 Ok(())
66 } else {
67 Err(from_glib_full(error))
68 };
69 let callback: Box_<(glib::thread_guard::ThreadGuard<R>, *mut *mut libc::c_char)> =
70 Box_::from_raw(user_data as *mut _);
71 let (callback, uris) = *callback;
72 let callback = callback.into_inner();
73 callback(result);
74 glib::ffi::g_strfreev(uris);
75 }
76 }
77 let callback = launch_uris_async_trampoline::<R>;
78 unsafe {
79 ffi::g_app_info_launch_uris_async(
80 self.as_ref().to_glib_none().0,
81 uris.to_glib_none().0,
82 context.map(|p| p.as_ref()).to_glib_none().0,
83 cancellable.map(|p| p.as_ref()).to_glib_none().0,
84 Some(callback),
85 Box_::into_raw(user_data) as *mut _,
86 );
87 }
88 }
89
90 #[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
91 fn launch_uris_future<P: IsA<AppLaunchContext> + Clone + 'static>(
92 &self,
93 uris: &[&str],
94 context: Option<&P>,
95 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
96 let uris = uris.iter().copied().map(String::from).collect::<Vec<_>>();
97 let context = context.map(ToOwned::to_owned);
98 Box_::pin(crate::GioFuture::new(
99 self,
100 move |obj, cancellable, send| {
101 let uris = uris
102 .iter()
103 .map(::std::borrow::Borrow::borrow)
104 .collect::<Vec<_>>();
105 obj.launch_uris_async(
106 uris.as_ref(),
107 context.as_ref().map(::std::borrow::Borrow::borrow),
108 Some(cancellable),
109 move |res| {
110 send.resolve(res);
111 },
112 );
113 },
114 ))
115 }
116}
117
118impl<O: IsA<AppInfo>> AppInfoExtManual for O {}