1use crate::{ffi, Window};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::{boxed::Box as Box_, pin::Pin};
12
13glib::wrapper! {
14 #[doc(alias = "GtkFileLauncher")]
49 pub struct FileLauncher(Object<ffi::GtkFileLauncher, ffi::GtkFileLauncherClass>);
50
51 match fn {
52 type_ => || ffi::gtk_file_launcher_get_type(),
53 }
54}
55
56impl FileLauncher {
57 #[doc(alias = "gtk_file_launcher_new")]
65 pub fn new(file: Option<&impl IsA<gio::File>>) -> FileLauncher {
66 assert_initialized_main_thread!();
67 unsafe {
68 from_glib_full(ffi::gtk_file_launcher_new(
69 file.map(|p| p.as_ref()).to_glib_none().0,
70 ))
71 }
72 }
73
74 #[cfg(feature = "v4_12")]
80 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
81 #[doc(alias = "gtk_file_launcher_get_always_ask")]
82 #[doc(alias = "get_always_ask")]
83 #[doc(alias = "always-ask")]
84 pub fn must_always_ask(&self) -> bool {
85 unsafe { from_glib(ffi::gtk_file_launcher_get_always_ask(self.to_glib_none().0)) }
86 }
87
88 #[doc(alias = "gtk_file_launcher_get_file")]
94 #[doc(alias = "get_file")]
95 pub fn file(&self) -> Option<gio::File> {
96 unsafe { from_glib_none(ffi::gtk_file_launcher_get_file(self.to_glib_none().0)) }
97 }
98
99 #[cfg(feature = "v4_14")]
105 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
106 #[doc(alias = "gtk_file_launcher_get_writable")]
107 #[doc(alias = "get_writable")]
108 #[doc(alias = "writable")]
109 pub fn is_writable(&self) -> bool {
110 unsafe { from_glib(ffi::gtk_file_launcher_get_writable(self.to_glib_none().0)) }
111 }
112
113 #[doc(alias = "gtk_file_launcher_launch")]
124 pub fn launch<P: FnOnce(Result<(), glib::Error>) + 'static>(
125 &self,
126 parent: Option<&impl IsA<Window>>,
127 cancellable: Option<&impl IsA<gio::Cancellable>>,
128 callback: P,
129 ) {
130 let main_context = glib::MainContext::ref_thread_default();
131 let is_main_context_owner = main_context.is_owner();
132 let has_acquired_main_context = (!is_main_context_owner)
133 .then(|| main_context.acquire().ok())
134 .flatten();
135 assert!(
136 is_main_context_owner || has_acquired_main_context.is_some(),
137 "Async operations only allowed if the thread is owning the MainContext"
138 );
139
140 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
141 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
142 unsafe extern "C" fn launch_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
143 _source_object: *mut glib::gobject_ffi::GObject,
144 res: *mut gio::ffi::GAsyncResult,
145 user_data: glib::ffi::gpointer,
146 ) {
147 let mut error = std::ptr::null_mut();
148 let _ = ffi::gtk_file_launcher_launch_finish(_source_object as *mut _, res, &mut error);
149 let result = if error.is_null() {
150 Ok(())
151 } else {
152 Err(from_glib_full(error))
153 };
154 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
155 Box_::from_raw(user_data as *mut _);
156 let callback: P = callback.into_inner();
157 callback(result);
158 }
159 let callback = launch_trampoline::<P>;
160 unsafe {
161 ffi::gtk_file_launcher_launch(
162 self.to_glib_none().0,
163 parent.map(|p| p.as_ref()).to_glib_none().0,
164 cancellable.map(|p| p.as_ref()).to_glib_none().0,
165 Some(callback),
166 Box_::into_raw(user_data) as *mut _,
167 );
168 }
169 }
170
171 pub fn launch_future(
172 &self,
173 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
174 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
175 let parent = parent.map(ToOwned::to_owned);
176 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
177 obj.launch(
178 parent.as_ref().map(::std::borrow::Borrow::borrow),
179 Some(cancellable),
180 move |res| {
181 send.resolve(res);
182 },
183 );
184 }))
185 }
186
187 #[doc(alias = "gtk_file_launcher_open_containing_folder")]
199 pub fn open_containing_folder<P: FnOnce(Result<(), glib::Error>) + 'static>(
200 &self,
201 parent: Option<&impl IsA<Window>>,
202 cancellable: Option<&impl IsA<gio::Cancellable>>,
203 callback: P,
204 ) {
205 let main_context = glib::MainContext::ref_thread_default();
206 let is_main_context_owner = main_context.is_owner();
207 let has_acquired_main_context = (!is_main_context_owner)
208 .then(|| main_context.acquire().ok())
209 .flatten();
210 assert!(
211 is_main_context_owner || has_acquired_main_context.is_some(),
212 "Async operations only allowed if the thread is owning the MainContext"
213 );
214
215 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
216 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
217 unsafe extern "C" fn open_containing_folder_trampoline<
218 P: FnOnce(Result<(), glib::Error>) + 'static,
219 >(
220 _source_object: *mut glib::gobject_ffi::GObject,
221 res: *mut gio::ffi::GAsyncResult,
222 user_data: glib::ffi::gpointer,
223 ) {
224 let mut error = std::ptr::null_mut();
225 let _ = ffi::gtk_file_launcher_open_containing_folder_finish(
226 _source_object as *mut _,
227 res,
228 &mut error,
229 );
230 let result = if error.is_null() {
231 Ok(())
232 } else {
233 Err(from_glib_full(error))
234 };
235 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
236 Box_::from_raw(user_data as *mut _);
237 let callback: P = callback.into_inner();
238 callback(result);
239 }
240 let callback = open_containing_folder_trampoline::<P>;
241 unsafe {
242 ffi::gtk_file_launcher_open_containing_folder(
243 self.to_glib_none().0,
244 parent.map(|p| p.as_ref()).to_glib_none().0,
245 cancellable.map(|p| p.as_ref()).to_glib_none().0,
246 Some(callback),
247 Box_::into_raw(user_data) as *mut _,
248 );
249 }
250 }
251
252 pub fn open_containing_folder_future(
253 &self,
254 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
255 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
256 let parent = parent.map(ToOwned::to_owned);
257 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
258 obj.open_containing_folder(
259 parent.as_ref().map(::std::borrow::Borrow::borrow),
260 Some(cancellable),
261 move |res| {
262 send.resolve(res);
263 },
264 );
265 }))
266 }
267
268 #[cfg(feature = "v4_12")]
275 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
276 #[doc(alias = "gtk_file_launcher_set_always_ask")]
277 #[doc(alias = "always-ask")]
278 pub fn set_always_ask(&self, always_ask: bool) {
279 unsafe {
280 ffi::gtk_file_launcher_set_always_ask(self.to_glib_none().0, always_ask.into_glib());
281 }
282 }
283
284 #[doc(alias = "gtk_file_launcher_set_file")]
288 #[doc(alias = "file")]
289 pub fn set_file(&self, file: Option<&impl IsA<gio::File>>) {
290 unsafe {
291 ffi::gtk_file_launcher_set_file(
292 self.to_glib_none().0,
293 file.map(|p| p.as_ref()).to_glib_none().0,
294 );
295 }
296 }
297
298 #[cfg(feature = "v4_14")]
302 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
303 #[doc(alias = "gtk_file_launcher_set_writable")]
304 #[doc(alias = "writable")]
305 pub fn set_writable(&self, writable: bool) {
306 unsafe {
307 ffi::gtk_file_launcher_set_writable(self.to_glib_none().0, writable.into_glib());
308 }
309 }
310
311 #[cfg(feature = "v4_12")]
312 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
313 #[doc(alias = "always-ask")]
314 pub fn connect_always_ask_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
315 unsafe extern "C" fn notify_always_ask_trampoline<F: Fn(&FileLauncher) + 'static>(
316 this: *mut ffi::GtkFileLauncher,
317 _param_spec: glib::ffi::gpointer,
318 f: glib::ffi::gpointer,
319 ) {
320 let f: &F = &*(f as *const F);
321 f(&from_glib_borrow(this))
322 }
323 unsafe {
324 let f: Box_<F> = Box_::new(f);
325 connect_raw(
326 self.as_ptr() as *mut _,
327 b"notify::always-ask\0".as_ptr() as *const _,
328 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
329 notify_always_ask_trampoline::<F> as *const (),
330 )),
331 Box_::into_raw(f),
332 )
333 }
334 }
335
336 #[cfg(feature = "v4_10")]
337 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
338 #[doc(alias = "file")]
339 pub fn connect_file_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
340 unsafe extern "C" fn notify_file_trampoline<F: Fn(&FileLauncher) + 'static>(
341 this: *mut ffi::GtkFileLauncher,
342 _param_spec: glib::ffi::gpointer,
343 f: glib::ffi::gpointer,
344 ) {
345 let f: &F = &*(f as *const F);
346 f(&from_glib_borrow(this))
347 }
348 unsafe {
349 let f: Box_<F> = Box_::new(f);
350 connect_raw(
351 self.as_ptr() as *mut _,
352 b"notify::file\0".as_ptr() as *const _,
353 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
354 notify_file_trampoline::<F> as *const (),
355 )),
356 Box_::into_raw(f),
357 )
358 }
359 }
360
361 #[cfg(feature = "v4_14")]
362 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
363 #[doc(alias = "writable")]
364 pub fn connect_writable_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
365 unsafe extern "C" fn notify_writable_trampoline<F: Fn(&FileLauncher) + 'static>(
366 this: *mut ffi::GtkFileLauncher,
367 _param_spec: glib::ffi::gpointer,
368 f: glib::ffi::gpointer,
369 ) {
370 let f: &F = &*(f as *const F);
371 f(&from_glib_borrow(this))
372 }
373 unsafe {
374 let f: Box_<F> = Box_::new(f);
375 connect_raw(
376 self.as_ptr() as *mut _,
377 b"notify::writable\0".as_ptr() as *const _,
378 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
379 notify_writable_trampoline::<F> as *const (),
380 )),
381 Box_::into_raw(f),
382 )
383 }
384 }
385}