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    /// Async version of [`AppInfoExt::launch_uris()`][crate::prelude::AppInfoExt::launch_uris()].
14    ///
15    /// The @callback is invoked immediately after the application launch, but it
16    /// waits for activation in case of D-Bus–activated applications and also provides
17    /// extended error information for sandboxed applications, see notes for
18    /// [`AppInfo::launch_default_for_uri_async()`][crate::AppInfo::launch_default_for_uri_async()].
19    /// ## `uris`
20    /// a list of URIs to launch.
21    /// ## `context`
22    /// the launch context
23    /// ## `cancellable`
24    /// a [`Cancellable`][crate::Cancellable]
25    /// ## `callback`
26    /// a [type@Gio.AsyncReadyCallback] to call
27    ///   when the request is done
28    #[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            let mut error = ptr::null_mut();
64            let _ = ffi::g_app_info_launch_uris_finish(_source_object as *mut _, res, &mut error);
65            let result = if error.is_null() {
66                Ok(())
67            } else {
68                Err(from_glib_full(error))
69            };
70            let callback: Box_<(glib::thread_guard::ThreadGuard<R>, *mut *mut libc::c_char)> =
71                Box_::from_raw(user_data as *mut _);
72            let (callback, uris) = *callback;
73            let callback = callback.into_inner();
74            callback(result);
75            glib::ffi::g_strfreev(uris);
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 {}