gio/auto/
subprocess_launcher.rs1use gio_sys;
6use glib::translate::*;
7use std;
8use std::boxed::Box as Box_;
9use std::fmt;
10use std::ptr;
11use Error;
12use Subprocess;
13use SubprocessFlags;
14
15glib_wrapper! {
16 pub struct SubprocessLauncher(Object<gio_sys::GSubprocessLauncher, SubprocessLauncherClass>);
17
18 match fn {
19 get_type => || gio_sys::g_subprocess_launcher_get_type(),
20 }
21}
22
23impl SubprocessLauncher {
24 pub fn new(flags: SubprocessFlags) -> SubprocessLauncher {
25 unsafe { from_glib_full(gio_sys::g_subprocess_launcher_new(flags.to_glib())) }
26 }
27
28 pub fn getenv<P: AsRef<std::path::Path>>(&self, variable: P) -> Option<std::path::PathBuf> {
29 unsafe {
30 from_glib_none(gio_sys::g_subprocess_launcher_getenv(
31 self.to_glib_none().0,
32 variable.as_ref().to_glib_none().0,
33 ))
34 }
35 }
36
37 #[cfg(any(unix, feature = "dox"))]
38 pub fn set_child_setup<P: Fn() + 'static>(&self, child_setup: P) {
39 let child_setup_data: Box_<P> = Box::new(child_setup);
40 unsafe extern "C" fn child_setup_func<P: Fn() + 'static>(user_data: glib_sys::gpointer) {
41 let callback: &P = &*(user_data as *mut _);
42 (*callback)();
43 }
44 let child_setup = Some(child_setup_func::<P> as _);
45 unsafe extern "C" fn destroy_notify_func<P: Fn() + 'static>(data: glib_sys::gpointer) {
46 let _callback: Box_<P> = Box_::from_raw(data as *mut _);
47 }
48 let destroy_call3 = Some(destroy_notify_func::<P> as _);
49 let super_callback0: Box_<P> = child_setup_data;
50 unsafe {
51 gio_sys::g_subprocess_launcher_set_child_setup(
52 self.to_glib_none().0,
53 child_setup,
54 Box::into_raw(super_callback0) as *mut _,
55 destroy_call3,
56 );
57 }
58 }
59
60 pub fn set_cwd<P: AsRef<std::path::Path>>(&self, cwd: P) {
61 unsafe {
62 gio_sys::g_subprocess_launcher_set_cwd(
63 self.to_glib_none().0,
64 cwd.as_ref().to_glib_none().0,
65 );
66 }
67 }
68
69 pub fn set_environ(&self, env: &[&std::path::Path]) {
70 unsafe {
71 gio_sys::g_subprocess_launcher_set_environ(self.to_glib_none().0, env.to_glib_none().0);
72 }
73 }
74
75 pub fn set_flags(&self, flags: SubprocessFlags) {
76 unsafe {
77 gio_sys::g_subprocess_launcher_set_flags(self.to_glib_none().0, flags.to_glib());
78 }
79 }
80
81 #[cfg(any(unix, feature = "dox"))]
82 pub fn set_stderr_file_path<P: AsRef<std::path::Path>>(&self, path: P) {
83 unsafe {
84 gio_sys::g_subprocess_launcher_set_stderr_file_path(
85 self.to_glib_none().0,
86 path.as_ref().to_glib_none().0,
87 );
88 }
89 }
90
91 #[cfg(any(unix, feature = "dox"))]
92 pub fn set_stdin_file_path(&self, path: &str) {
93 unsafe {
94 gio_sys::g_subprocess_launcher_set_stdin_file_path(
95 self.to_glib_none().0,
96 path.to_glib_none().0,
97 );
98 }
99 }
100
101 #[cfg(any(unix, feature = "dox"))]
102 pub fn set_stdout_file_path<P: AsRef<std::path::Path>>(&self, path: P) {
103 unsafe {
104 gio_sys::g_subprocess_launcher_set_stdout_file_path(
105 self.to_glib_none().0,
106 path.as_ref().to_glib_none().0,
107 );
108 }
109 }
110
111 pub fn setenv<P: AsRef<std::ffi::OsStr>, Q: AsRef<std::ffi::OsStr>>(
112 &self,
113 variable: P,
114 value: Q,
115 overwrite: bool,
116 ) {
117 unsafe {
118 gio_sys::g_subprocess_launcher_setenv(
119 self.to_glib_none().0,
120 variable.as_ref().to_glib_none().0,
121 value.as_ref().to_glib_none().0,
122 overwrite.to_glib(),
123 );
124 }
125 }
126
127 pub fn spawnv(&self, argv: &[&std::ffi::OsStr]) -> Result<Subprocess, Error> {
132 unsafe {
133 let mut error = ptr::null_mut();
134 let ret = gio_sys::g_subprocess_launcher_spawnv(
135 self.to_glib_none().0,
136 argv.to_glib_none().0,
137 &mut error,
138 );
139 if error.is_null() {
140 Ok(from_glib_full(ret))
141 } else {
142 Err(from_glib_full(error))
143 }
144 }
145 }
146
147 pub fn unsetenv<P: AsRef<std::ffi::OsStr>>(&self, variable: P) {
148 unsafe {
149 gio_sys::g_subprocess_launcher_unsetenv(
150 self.to_glib_none().0,
151 variable.as_ref().to_glib_none().0,
152 );
153 }
154 }
155}
156
157impl fmt::Display for SubprocessLauncher {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 write!(f, "SubprocessLauncher")
160 }
161}