gio/
app_info.rs

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