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