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