1use crate::{ffi, ContentFormats};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::{boxed::Box as Box_, pin::Pin};
13
14glib::wrapper! {
15 #[doc(alias = "GdkContentProvider")]
16 pub struct ContentProvider(Object<ffi::GdkContentProvider, ffi::GdkContentProviderClass>);
17
18 match fn {
19 type_ => || ffi::gdk_content_provider_get_type(),
20 }
21}
22
23impl ContentProvider {
24 pub const NONE: Option<&'static ContentProvider> = None;
25
26 #[doc(alias = "gdk_content_provider_new_for_bytes")]
27 #[doc(alias = "new_for_bytes")]
28 pub fn for_bytes(mime_type: &str, bytes: &glib::Bytes) -> ContentProvider {
29 assert_initialized_main_thread!();
30 unsafe {
31 from_glib_full(ffi::gdk_content_provider_new_for_bytes(
32 mime_type.to_glib_none().0,
33 bytes.to_glib_none().0,
34 ))
35 }
36 }
37
38 #[doc(alias = "gdk_content_provider_new_for_value")]
39 #[doc(alias = "new_for_value")]
40 pub fn for_value(value: &glib::Value) -> ContentProvider {
41 assert_initialized_main_thread!();
42 unsafe {
43 from_glib_full(ffi::gdk_content_provider_new_for_value(
44 value.to_glib_none().0,
45 ))
46 }
47 }
48
49 #[doc(alias = "gdk_content_provider_new_union")]
50 pub fn new_union(providers: &[ContentProvider]) -> ContentProvider {
51 assert_initialized_main_thread!();
52 let n_providers = providers.len() as _;
53 unsafe {
54 from_glib_full(ffi::gdk_content_provider_new_union(
55 providers.to_glib_full(),
56 n_providers,
57 ))
58 }
59 }
60}
61
62pub trait ContentProviderExt: IsA<ContentProvider> + 'static {
63 #[doc(alias = "gdk_content_provider_content_changed")]
64 fn content_changed(&self) {
65 unsafe {
66 ffi::gdk_content_provider_content_changed(self.as_ref().to_glib_none().0);
67 }
68 }
69
70 #[doc(alias = "gdk_content_provider_ref_formats")]
71 #[doc(alias = "ref_formats")]
72 fn formats(&self) -> ContentFormats {
73 unsafe {
74 from_glib_full(ffi::gdk_content_provider_ref_formats(
75 self.as_ref().to_glib_none().0,
76 ))
77 }
78 }
79
80 #[doc(alias = "gdk_content_provider_ref_storable_formats")]
81 #[doc(alias = "ref_storable_formats")]
82 #[doc(alias = "storable-formats")]
83 fn storable_formats(&self) -> ContentFormats {
84 unsafe {
85 from_glib_full(ffi::gdk_content_provider_ref_storable_formats(
86 self.as_ref().to_glib_none().0,
87 ))
88 }
89 }
90
91 #[doc(alias = "gdk_content_provider_write_mime_type_async")]
92 fn write_mime_type_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
93 &self,
94 mime_type: &str,
95 stream: &impl IsA<gio::OutputStream>,
96 io_priority: glib::Priority,
97 cancellable: Option<&impl IsA<gio::Cancellable>>,
98 callback: P,
99 ) {
100 let main_context = glib::MainContext::ref_thread_default();
101 let is_main_context_owner = main_context.is_owner();
102 let has_acquired_main_context = (!is_main_context_owner)
103 .then(|| main_context.acquire().ok())
104 .flatten();
105 assert!(
106 is_main_context_owner || has_acquired_main_context.is_some(),
107 "Async operations only allowed if the thread is owning the MainContext"
108 );
109
110 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
111 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
112 unsafe extern "C" fn write_mime_type_async_trampoline<
113 P: FnOnce(Result<(), glib::Error>) + 'static,
114 >(
115 _source_object: *mut glib::gobject_ffi::GObject,
116 res: *mut gio::ffi::GAsyncResult,
117 user_data: glib::ffi::gpointer,
118 ) {
119 let mut error = std::ptr::null_mut();
120 ffi::gdk_content_provider_write_mime_type_finish(
121 _source_object as *mut _,
122 res,
123 &mut error,
124 );
125 let result = if error.is_null() {
126 Ok(())
127 } else {
128 Err(from_glib_full(error))
129 };
130 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
131 Box_::from_raw(user_data as *mut _);
132 let callback: P = callback.into_inner();
133 callback(result);
134 }
135 let callback = write_mime_type_async_trampoline::<P>;
136 unsafe {
137 ffi::gdk_content_provider_write_mime_type_async(
138 self.as_ref().to_glib_none().0,
139 mime_type.to_glib_none().0,
140 stream.as_ref().to_glib_none().0,
141 io_priority.into_glib(),
142 cancellable.map(|p| p.as_ref()).to_glib_none().0,
143 Some(callback),
144 Box_::into_raw(user_data) as *mut _,
145 );
146 }
147 }
148
149 fn write_mime_type_future(
150 &self,
151 mime_type: &str,
152 stream: &(impl IsA<gio::OutputStream> + Clone + 'static),
153 io_priority: glib::Priority,
154 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
155 let mime_type = String::from(mime_type);
156 let stream = stream.clone();
157 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
158 obj.write_mime_type_async(
159 &mime_type,
160 &stream,
161 io_priority,
162 Some(cancellable),
163 move |res| {
164 send.resolve(res);
165 },
166 );
167 }))
168 }
169
170 #[doc(alias = "content-changed")]
171 fn connect_content_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
172 unsafe extern "C" fn content_changed_trampoline<
173 P: IsA<ContentProvider>,
174 F: Fn(&P) + 'static,
175 >(
176 this: *mut ffi::GdkContentProvider,
177 f: glib::ffi::gpointer,
178 ) {
179 let f: &F = &*(f as *const F);
180 f(ContentProvider::from_glib_borrow(this).unsafe_cast_ref())
181 }
182 unsafe {
183 let f: Box_<F> = Box_::new(f);
184 connect_raw(
185 self.as_ptr() as *mut _,
186 c"content-changed".as_ptr() as *const _,
187 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
188 content_changed_trampoline::<Self, F> as *const (),
189 )),
190 Box_::into_raw(f),
191 )
192 }
193 }
194
195 #[doc(alias = "formats")]
196 fn connect_formats_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
197 unsafe extern "C" fn notify_formats_trampoline<
198 P: IsA<ContentProvider>,
199 F: Fn(&P) + 'static,
200 >(
201 this: *mut ffi::GdkContentProvider,
202 _param_spec: glib::ffi::gpointer,
203 f: glib::ffi::gpointer,
204 ) {
205 let f: &F = &*(f as *const F);
206 f(ContentProvider::from_glib_borrow(this).unsafe_cast_ref())
207 }
208 unsafe {
209 let f: Box_<F> = Box_::new(f);
210 connect_raw(
211 self.as_ptr() as *mut _,
212 c"notify::formats".as_ptr() as *const _,
213 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
214 notify_formats_trampoline::<Self, F> as *const (),
215 )),
216 Box_::into_raw(f),
217 )
218 }
219 }
220
221 #[doc(alias = "storable-formats")]
222 fn connect_storable_formats_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
223 unsafe extern "C" fn notify_storable_formats_trampoline<
224 P: IsA<ContentProvider>,
225 F: Fn(&P) + 'static,
226 >(
227 this: *mut ffi::GdkContentProvider,
228 _param_spec: glib::ffi::gpointer,
229 f: glib::ffi::gpointer,
230 ) {
231 let f: &F = &*(f as *const F);
232 f(ContentProvider::from_glib_borrow(this).unsafe_cast_ref())
233 }
234 unsafe {
235 let f: Box_<F> = Box_::new(f);
236 connect_raw(
237 self.as_ptr() as *mut _,
238 c"notify::storable-formats".as_ptr() as *const _,
239 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
240 notify_storable_formats_trampoline::<Self, F> as *const (),
241 )),
242 Box_::into_raw(f),
243 )
244 }
245 }
246}
247
248impl<O: IsA<ContentProvider>> ContentProviderExt for O {}