gio/
desktop_app_info.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(all(unix, feature = "v2_58"))]
4use std::boxed::Box as Box_;
5#[cfg(all(unix, feature = "v2_58"))]
6use std::os::unix::io::AsRawFd;
7#[cfg(all(unix, feature = "v2_58"))]
8use std::ptr;
9
10#[cfg(all(feature = "v2_58", unix))]
11use glib::{prelude::*, Error};
12use glib::{translate::*, GString};
13
14#[cfg(all(feature = "v2_58", unix))]
15use crate::AppLaunchContext;
16use crate::{ffi, DesktopAppInfo};
17
18impl DesktopAppInfo {
19    /// Searches desktop files for ones that match @search_string.
20    ///
21    /// The return value is an array of strvs.  Each strv contains a list of
22    /// applications that matched @search_string with an equal score.  The
23    /// outer list is sorted by score so that the first strv contains the
24    /// best-matching applications, and so on.
25    /// The algorithm for determining matches is undefined and may change at
26    /// any time.
27    ///
28    /// None of the search results are subjected to the normal validation
29    /// checks performed by [`new()`][Self::new()] (for example, checking that
30    /// the executable referenced by a result exists), and so it is possible for
31    /// [`new()`][Self::new()] to return `NULL` when passed an app ID returned
32    /// by this function. It is expected that calling code will do this when
33    /// subsequently creating a [`DesktopAppInfo`][crate::DesktopAppInfo] for each result.
34    /// ## `search_string`
35    /// the search string to use
36    ///
37    /// # Returns
38    ///
39    /// a
40    ///   list of strvs.  Free each item with `strfreev()` and free the outer
41    ///   list with `free()`.
42    #[doc(alias = "g_desktop_app_info_search")]
43    pub fn search(search_string: &str) -> Vec<Vec<GString>> {
44        unsafe {
45            let out = ffi::g_desktop_app_info_search(search_string.to_glib_none().0);
46
47            if out.is_null() {
48                return Vec::new();
49            }
50
51            let mut ret = Vec::new();
52            let mut it = 0;
53            loop {
54                let tmp: *mut *mut libc::c_char = *out.offset(it);
55
56                if tmp.is_null() {
57                    break;
58                }
59                let v: Vec<GString> = FromGlibPtrContainer::from_glib_full(tmp);
60                ret.push(v);
61                it += 1;
62            }
63
64            glib::ffi::g_free(out as *mut libc::c_void);
65            ret
66        }
67    }
68}
69
70#[cfg(all(feature = "v2_58", unix))]
71mod sealed {
72    pub trait Sealed {}
73    impl<T: super::IsA<super::DesktopAppInfo>> Sealed for T {}
74}
75
76#[cfg(all(feature = "v2_58", unix))]
77pub trait DesktopAppInfoExtManual: sealed::Sealed + IsA<DesktopAppInfo> {
78    #[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", unix))))]
79    #[doc(alias = "g_desktop_app_info_launch_uris_as_manager_with_fds")]
80    fn launch_uris_as_manager_with_fds<
81        P: IsA<AppLaunchContext>,
82        T: AsRawFd,
83        U: AsRawFd,
84        V: AsRawFd,
85    >(
86        &self,
87        uris: &[&str],
88        launch_context: Option<&P>,
89        spawn_flags: glib::SpawnFlags,
90        user_setup: Option<Box_<dyn FnOnce() + 'static>>,
91        pid_callback: Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))>,
92        stdin_fd: &mut T,
93        stdout_fd: &mut U,
94        stderr_fd: &mut V,
95    ) -> Result<(), Error> {
96        let user_setup_data: Box_<Option<Box_<dyn FnOnce() + 'static>>> = Box_::new(user_setup);
97        unsafe extern "C" fn user_setup_func(user_data: glib::ffi::gpointer) {
98            let callback: Box_<Option<Box_<dyn FnOnce() + 'static>>> =
99                Box_::from_raw(user_data as *mut _);
100            let callback = (*callback).expect("cannot get closure...");
101            callback()
102        }
103        let user_setup = if user_setup_data.is_some() {
104            Some(user_setup_func as _)
105        } else {
106            None
107        };
108        let pid_callback_data: Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))> = pid_callback;
109        unsafe extern "C" fn pid_callback_func(
110            appinfo: *mut ffi::GDesktopAppInfo,
111            pid: glib::ffi::GPid,
112            user_data: glib::ffi::gpointer,
113        ) {
114            let appinfo = from_glib_borrow(appinfo);
115            let pid = from_glib(pid);
116            let callback = user_data as *mut Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))>;
117            if let Some(ref mut callback) = *callback {
118                callback(&appinfo, pid)
119            } else {
120                panic!("cannot get closure...")
121            };
122        }
123        let pid_callback = if pid_callback_data.is_some() {
124            Some(pid_callback_func as _)
125        } else {
126            None
127        };
128        let super_callback0: Box_<Option<Box_<dyn FnOnce() + 'static>>> = user_setup_data;
129        let super_callback1: &Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))> =
130            &pid_callback_data;
131        unsafe {
132            let mut error = ptr::null_mut();
133            let _ = ffi::g_desktop_app_info_launch_uris_as_manager_with_fds(
134                self.as_ref().to_glib_none().0,
135                uris.to_glib_none().0,
136                launch_context.map(|p| p.as_ref()).to_glib_none().0,
137                spawn_flags.into_glib(),
138                user_setup,
139                Box_::into_raw(super_callback0) as *mut _,
140                pid_callback,
141                super_callback1 as *const _ as *mut _,
142                stdin_fd.as_raw_fd(),
143                stdout_fd.as_raw_fd(),
144                stderr_fd.as_raw_fd(),
145                &mut error,
146            );
147            if error.is_null() {
148                Ok(())
149            } else {
150                Err(from_glib_full(error))
151            }
152        }
153    }
154}
155
156#[cfg(all(feature = "v2_58", unix))]
157impl<O: IsA<DesktopAppInfo>> DesktopAppInfoExtManual for O {}