gdk4/
drop.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{future, pin::Pin, ptr};
4
5use glib::{translate::*, GString};
6
7use crate::{ffi, prelude::*, Drop};
8
9impl Drop {
10    /// Asynchronously read the dropped data from a [`Drop`][crate::Drop]
11    /// in a format that complies with one of the mime types.
12    /// ## `mime_types`
13    ///
14    ///   pointer to an array of mime types
15    /// ## `io_priority`
16    /// the I/O priority for the read operation
17    /// ## `cancellable`
18    /// optional `GCancellable` object
19    /// ## `callback`
20    /// a `GAsyncReadyCallback` to call when
21    ///   the request is satisfied
22    #[doc(alias = "gdk_drop_read_async")]
23    pub fn read_async<Q: FnOnce(Result<(gio::InputStream, GString), glib::Error>) + 'static>(
24        &self,
25        mime_types: &[&str],
26        io_priority: glib::Priority,
27        cancellable: Option<&impl IsA<gio::Cancellable>>,
28        callback: Q,
29    ) {
30        let main_context = glib::MainContext::ref_thread_default();
31        let is_main_context_owner = main_context.is_owner();
32        let has_acquired_main_context = (!is_main_context_owner)
33            .then(|| main_context.acquire().ok())
34            .flatten();
35        assert!(
36            is_main_context_owner || has_acquired_main_context.is_some(),
37            "Async operations only allowed if the thread is owning the MainContext"
38        );
39
40        let user_data: Box<glib::thread_guard::ThreadGuard<Q>> =
41            Box::new(glib::thread_guard::ThreadGuard::new(callback));
42        unsafe extern "C" fn read_async_trampoline<
43            Q: FnOnce(Result<(gio::InputStream, GString), glib::Error>) + 'static,
44        >(
45            _source_object: *mut glib::gobject_ffi::GObject,
46            res: *mut gio::ffi::GAsyncResult,
47            user_data: glib::ffi::gpointer,
48        ) {
49            let mut error = ptr::null_mut();
50            let mut out_mime_type = ptr::null();
51            let ret = ffi::gdk_drop_read_finish(
52                _source_object as *mut _,
53                res,
54                &mut out_mime_type,
55                &mut error,
56            );
57            let result = if error.is_null() {
58                Ok((from_glib_full(ret), from_glib_none(out_mime_type)))
59            } else {
60                Err(from_glib_full(error))
61            };
62            let callback: Box<glib::thread_guard::ThreadGuard<Q>> =
63                Box::from_raw(user_data as *mut _);
64            let callback = callback.into_inner();
65            callback(result);
66        }
67        let callback = read_async_trampoline::<Q>;
68        unsafe {
69            ffi::gdk_drop_read_async(
70                self.to_glib_none().0,
71                mime_types.to_glib_none().0,
72                io_priority.into_glib(),
73                cancellable.map(|p| p.as_ref()).to_glib_none().0,
74                Some(callback),
75                Box::into_raw(user_data) as *mut _,
76            );
77        }
78    }
79
80    #[allow(clippy::type_complexity)]
81    pub fn read_future(
82        &self,
83        mime_types: &[&str],
84        io_priority: glib::Priority,
85    ) -> Pin<
86        Box<
87            dyn future::Future<Output = Result<(gio::InputStream, GString), glib::Error>> + 'static,
88        >,
89    > {
90        let mime_types = mime_types
91            .iter()
92            .copied()
93            .map(String::from)
94            .collect::<Vec<_>>();
95        Box::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
96            let mime_types = mime_types.iter().map(|s| s.as_str()).collect::<Vec<_>>();
97            obj.read_async(&mime_types, io_priority, Some(cancellable), move |res| {
98                send.resolve(res);
99            });
100        }))
101    }
102}