| 1 |
// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 |
|
| 3 |
use std::boxed::Box as Box_; |
| 4 |
use std::pin::Pin; |
| 5 |
use std::ptr; |
| 6 |
|
| 7 |
use glib::prelude::*; |
| 8 |
use glib::translate::*; |
| 9 |
|
| 10 |
use crate::{AppInfo, AppLaunchContext, Cancellable, ffi}; |
| 11 |
|
| 12 |
pub trait AppInfoExtManual: IsA<AppInfo> + 'static { |
| 13 |
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))] |
| 14 |
#[doc(alias = "g_app_info_launch_uris_async")] |
| 15 |
fn launch_uris_async< |
| 16 |
P: IsA<AppLaunchContext>, |
| 17 |
Q: IsA<Cancellable>, |
| 18 |
R: FnOnce(Result<(), glib::Error>) + 'static, |
| 19 |
>( |
| 20 |
&self, |
| 21 |
uris: &[&str], |
| 22 |
context: Option<&P>, |
| 23 |
cancellable: Option<&Q>, |
| 24 |
callback: R, |
| 25 |
) { |
| 26 |
let main_context = glib::MainContext::ref_thread_default(); |
| 27 |
let is_main_context_owner = main_context.is_owner(); |
| 28 |
let has_acquired_main_context = (!is_main_context_owner) |
| 29 |
.then(|| main_context.acquire().ok()) |
| 30 |
.flatten(); |
| 31 |
assert!( |
| 32 |
is_main_context_owner || has_acquired_main_context.is_some(), |
| 33 |
"Async operations only allowed if the thread is owning the MainContext" |
| 34 |
); |
| 35 |
|
| 36 |
let user_data: Box_<(glib::thread_guard::ThreadGuard<R>, *mut *mut libc::c_char)> = |
| 37 |
Box_::new(( |
| 38 |
glib::thread_guard::ThreadGuard::new(callback), |
| 39 |
uris.to_glib_full(), |
| 40 |
)); |
| 41 |
unsafe extern "C" fn launch_uris_async_trampoline< |
| 42 |
R: FnOnce(Result<(), glib::Error>) + 'static, |
| 43 |
>( |
| 44 |
_source_object: *mut glib::gobject_ffi::GObject, |
| 45 |
res: *mut ffi::GAsyncResult, |
| 46 |
user_data: glib::ffi::gpointer, |
| 47 |
) { |
| 48 |
unsafe { |
| 49 |
let mut error = ptr::null_mut(); |
| 50 |
let _ = |
| 51 |
ffi::g_app_info_launch_uris_finish(_source_object as *mut _, res, &mut error); |
| 52 |
let result = if error.is_null() { |
| 53 |
Ok(()) |
| 54 |
} else { |
| 55 |
Err(from_glib_full(error)) |
| 56 |
}; |
| 57 |
let callback: Box_<(glib::thread_guard::ThreadGuard<R>, *mut *mut libc::c_char)> = |
| 58 |
Box_::from_raw(user_data as *mut _); |
| 59 |
let (callback, uris) = *callback; |
| 60 |
let callback = callback.into_inner(); |
| 61 |
callback(result); |
| 62 |
glib::ffi::g_strfreev(uris); |
| 63 |
} |
| 64 |
} |
| 65 |
let callback = launch_uris_async_trampoline::<R>; |
| 66 |
unsafe { |
| 67 |
ffi::g_app_info_launch_uris_async( |
| 68 |
self.as_ref().to_glib_none().0, |
| 69 |
uris.to_glib_none().0, |
| 70 |
context.map(|p| p.as_ref()).to_glib_none().0, |
| 71 |
cancellable.map(|p| p.as_ref()).to_glib_none().0, |
| 72 |
Some(callback), |
| 73 |
Box_::into_raw(user_data) as *mut _, |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))] |
| 79 |
fn launch_uris_future<P: IsA<AppLaunchContext> + Clone + 'static>( |
| 80 |
&self, |
| 81 |
uris: &[&str], |
| 82 |
context: Option<&P>, |
| 83 |
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> { |
| 84 |
let uris = uris.iter().copied().map(String::from).collect::<Vec<_>>(); |
| 85 |
let context = context.map(ToOwned::to_owned); |
| 86 |
Box_::pin(crate::GioFuture::new( |
| 87 |
self, |
| 88 |
move |obj, cancellable, send| { |
| 89 |
let uris = uris |
| 90 |
.iter() |
| 91 |
.map(::std::borrow::Borrow::borrow) |
| 92 |
.collect::<Vec<_>>(); |
| 93 |
obj.launch_uris_async( |
| 94 |
uris.as_ref(), |
| 95 |
context.as_ref().map(::std::borrow::Borrow::borrow), |
| 96 |
Some(cancellable), |
| 97 |
move |res| { |
| 98 |
send.resolve(res); |
| 99 |
}, |
| 100 |
); |
| 101 |
}, |
| 102 |
)) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl<O: IsA<AppInfo>> AppInfoExtManual for O {} |