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