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::{AsFd, 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))]
71pub trait DesktopAppInfoExtManual: IsA<DesktopAppInfo> {
72    #[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", unix))))]
73    #[doc(alias = "g_desktop_app_info_launch_uris_as_manager_with_fds")]
74    fn launch_uris_as_manager_with_fds<P: IsA<AppLaunchContext>>(
75        &self,
76        uris: &[&str],
77        launch_context: Option<&P>,
78        spawn_flags: glib::SpawnFlags,
79        user_setup: Option<Box_<dyn FnOnce() + 'static>>,
80        pid_callback: Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))>,
81        stdin_fd: Option<impl AsFd>,
82        stdout_fd: Option<impl AsFd>,
83        stderr_fd: Option<impl AsFd>,
84    ) -> Result<(), Error> {
85        let user_setup_data: Box_<Option<Box_<dyn FnOnce() + 'static>>> = Box_::new(user_setup);
86        unsafe extern "C" fn user_setup_func(user_data: glib::ffi::gpointer) {
87            let callback: Box_<Option<Box_<dyn FnOnce() + 'static>>> =
88                Box_::from_raw(user_data as *mut _);
89            let callback = (*callback).expect("cannot get closure...");
90            callback()
91        }
92        let user_setup = if user_setup_data.is_some() {
93            Some(user_setup_func as _)
94        } else {
95            None
96        };
97        let pid_callback_data: Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))> = pid_callback;
98        unsafe extern "C" fn pid_callback_func(
99            appinfo: *mut ffi::GDesktopAppInfo,
100            pid: glib::ffi::GPid,
101            user_data: glib::ffi::gpointer,
102        ) {
103            let appinfo = from_glib_borrow(appinfo);
104            let pid = from_glib(pid);
105            let callback = user_data as *mut Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))>;
106            if let Some(ref mut callback) = *callback {
107                callback(&appinfo, pid)
108            } else {
109                panic!("cannot get closure...")
110            };
111        }
112        let pid_callback = if pid_callback_data.is_some() {
113            Some(pid_callback_func as _)
114        } else {
115            None
116        };
117        let super_callback0: Box_<Option<Box_<dyn FnOnce() + 'static>>> = user_setup_data;
118        let super_callback1: &Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))> =
119            &pid_callback_data;
120
121        let stdin_raw_fd = stdin_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
122        let stdout_raw_fd = stdout_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
123        let stderr_raw_fd = stderr_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
124        unsafe {
125            let mut error = ptr::null_mut();
126            let _ = ffi::g_desktop_app_info_launch_uris_as_manager_with_fds(
127                self.as_ref().to_glib_none().0,
128                uris.to_glib_none().0,
129                launch_context.map(|p| p.as_ref()).to_glib_none().0,
130                spawn_flags.into_glib(),
131                user_setup,
132                Box_::into_raw(super_callback0) as *mut _,
133                pid_callback,
134                super_callback1 as *const _ as *mut _,
135                stdin_raw_fd,
136                stdout_raw_fd,
137                stderr_raw_fd,
138                &mut error,
139            );
140            if error.is_null() {
141                Ok(())
142            } else {
143                Err(from_glib_full(error))
144            }
145        }
146    }
147}
148
149#[cfg(all(feature = "v2_58", unix))]
150impl<O: IsA<DesktopAppInfo>> DesktopAppInfoExtManual for O {}