gio/auto/file_enumerator.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{AsyncResult, Cancellable, File, FileInfo, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 /// `GFileEnumerator` allows you to operate on a set of [`File`][crate::File] objects,
11 /// returning a [`FileInfo`][crate::FileInfo] structure for each file enumerated (e.g.
12 /// [`FileExt::enumerate_children()`][crate::prelude::FileExt::enumerate_children()] will return a `GFileEnumerator` for each
13 /// of the children within a directory).
14 ///
15 /// To get the next file's information from a `GFileEnumerator`, use
16 /// [`FileEnumeratorExt::next_file()`][crate::prelude::FileEnumeratorExt::next_file()] or its asynchronous version,
17 /// [`FileEnumeratorExt::next_files_async()`][crate::prelude::FileEnumeratorExt::next_files_async()]. Note that the asynchronous
18 /// version will return a list of [`FileInfo`][crate::FileInfo] objects, whereas the
19 /// synchronous will only return the next file in the enumerator.
20 ///
21 /// The ordering of returned files is unspecified for non-Unix
22 /// platforms; for more information, see `GLib::Dir::read_name()`. On Unix,
23 /// when operating on local files, returned files will be sorted by
24 /// inode number. Effectively you can assume that the ordering of
25 /// returned files will be stable between successive calls (and
26 /// applications) assuming the directory is unchanged.
27 ///
28 /// If your application needs a specific ordering, such as by name or
29 /// modification time, you will have to implement that in your
30 /// application code.
31 ///
32 /// To close a `GFileEnumerator`, use [`FileEnumeratorExtManual::close()`][crate::prelude::FileEnumeratorExtManual::close()], or
33 /// its asynchronous version, [`FileEnumeratorExt::close_async()`][crate::prelude::FileEnumeratorExt::close_async()]. Once
34 /// a `GFileEnumerator` is closed, no further actions may be performed
35 /// on it, and it should be freed with `GObject::Object::unref()`.
36 ///
37 /// ## Properties
38 ///
39 ///
40 /// #### `container`
41 /// The container that is being enumerated.
42 ///
43 /// Writable | Construct Only
44 ///
45 /// # Implements
46 ///
47 /// [`FileEnumeratorExt`][trait@crate::prelude::FileEnumeratorExt], [`trait@glib::ObjectExt`], [`FileEnumeratorExtManual`][trait@crate::prelude::FileEnumeratorExtManual]
48 #[doc(alias = "GFileEnumerator")]
49 pub struct FileEnumerator(Object<ffi::GFileEnumerator, ffi::GFileEnumeratorClass>);
50
51 match fn {
52 type_ => || ffi::g_file_enumerator_get_type(),
53 }
54}
55
56impl FileEnumerator {
57 pub const NONE: Option<&'static FileEnumerator> = None;
58}
59
60/// Trait containing all [`struct@FileEnumerator`] methods.
61///
62/// # Implementors
63///
64/// [`FileEnumerator`][struct@crate::FileEnumerator]
65pub trait FileEnumeratorExt: IsA<FileEnumerator> + 'static {
66 /// Asynchronously closes the file enumerator.
67 ///
68 /// If @cancellable is not [`None`], then the operation can be cancelled by
69 /// triggering the cancellable object from another thread. If the operation
70 /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned in
71 /// g_file_enumerator_close_finish().
72 /// ## `io_priority`
73 /// the [I/O priority](iface.AsyncResult.html#io-priority) of the request
74 /// ## `cancellable`
75 /// optional #GCancellable object, [`None`] to ignore.
76 /// ## `callback`
77 /// a #GAsyncReadyCallback
78 /// to call when the request is satisfied
79 #[doc(alias = "g_file_enumerator_close_async")]
80 fn close_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
81 &self,
82 io_priority: glib::Priority,
83 cancellable: Option<&impl IsA<Cancellable>>,
84 callback: P,
85 ) {
86 let main_context = glib::MainContext::ref_thread_default();
87 let is_main_context_owner = main_context.is_owner();
88 let has_acquired_main_context = (!is_main_context_owner)
89 .then(|| main_context.acquire().ok())
90 .flatten();
91 assert!(
92 is_main_context_owner || has_acquired_main_context.is_some(),
93 "Async operations only allowed if the thread is owning the MainContext"
94 );
95
96 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
97 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
98 unsafe extern "C" fn close_async_trampoline<
99 P: FnOnce(Result<(), glib::Error>) + 'static,
100 >(
101 _source_object: *mut glib::gobject_ffi::GObject,
102 res: *mut crate::ffi::GAsyncResult,
103 user_data: glib::ffi::gpointer,
104 ) {
105 unsafe {
106 let mut error = std::ptr::null_mut();
107 ffi::g_file_enumerator_close_finish(_source_object as *mut _, res, &mut error);
108 let result = if error.is_null() {
109 Ok(())
110 } else {
111 Err(from_glib_full(error))
112 };
113 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
114 Box_::from_raw(user_data as *mut _);
115 let callback: P = callback.into_inner();
116 callback(result);
117 }
118 }
119 let callback = close_async_trampoline::<P>;
120 unsafe {
121 ffi::g_file_enumerator_close_async(
122 self.as_ref().to_glib_none().0,
123 io_priority.into_glib(),
124 cancellable.map(|p| p.as_ref()).to_glib_none().0,
125 Some(callback),
126 Box_::into_raw(user_data) as *mut _,
127 );
128 }
129 }
130
131 fn close_future(
132 &self,
133 io_priority: glib::Priority,
134 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
135 Box_::pin(crate::GioFuture::new(
136 self,
137 move |obj, cancellable, send| {
138 obj.close_async(io_priority, Some(cancellable), move |res| {
139 send.resolve(res);
140 });
141 },
142 ))
143 }
144
145 ///
146 /// gchar *name = g_file_info_get_name (info);
147 /// GFile *child = g_file_get_child (g_file_enumerator_get_container (enumr),
148 /// name);
149 /// ]|
150 /// ## `info`
151 /// a #GFileInfo gotten from g_file_enumerator_next_file()
152 /// or the async equivalents.
153 ///
154 /// # Returns
155 ///
156 /// a #GFile for the #GFileInfo passed it.
157 #[doc(alias = "g_file_enumerator_get_child")]
158 #[doc(alias = "get_child")]
159 fn child(&self, info: &FileInfo) -> File {
160 unsafe {
161 from_glib_full(ffi::g_file_enumerator_get_child(
162 self.as_ref().to_glib_none().0,
163 info.to_glib_none().0,
164 ))
165 }
166 }
167
168 /// Get the #GFile container which is being enumerated.
169 ///
170 /// # Returns
171 ///
172 /// the #GFile which is being enumerated.
173 #[doc(alias = "g_file_enumerator_get_container")]
174 #[doc(alias = "get_container")]
175 fn container(&self) -> File {
176 unsafe {
177 from_glib_none(ffi::g_file_enumerator_get_container(
178 self.as_ref().to_glib_none().0,
179 ))
180 }
181 }
182
183 /// Checks if the file enumerator has pending operations.
184 ///
185 /// # Returns
186 ///
187 /// [`true`] if the @self has pending operations.
188 #[doc(alias = "g_file_enumerator_has_pending")]
189 fn has_pending(&self) -> bool {
190 unsafe {
191 from_glib(ffi::g_file_enumerator_has_pending(
192 self.as_ref().to_glib_none().0,
193 ))
194 }
195 }
196
197 /// Checks if the file enumerator has been closed.
198 ///
199 /// # Returns
200 ///
201 /// [`true`] if the @self is closed.
202 #[doc(alias = "g_file_enumerator_is_closed")]
203 fn is_closed(&self) -> bool {
204 unsafe {
205 from_glib(ffi::g_file_enumerator_is_closed(
206 self.as_ref().to_glib_none().0,
207 ))
208 }
209 }
210
211 /// Returns information for the next file in the enumerated object.
212 /// Will block until the information is available. The #GFileInfo
213 /// returned from this function will contain attributes that match the
214 /// attribute string that was passed when the #GFileEnumerator was created.
215 ///
216 /// See the documentation of #GFileEnumerator for information about the
217 /// order of returned files.
218 ///
219 /// On error, returns [`None`] and sets @error to the error. If the
220 /// enumerator is at the end, [`None`] will be returned and @error will
221 /// be unset.
222 /// ## `cancellable`
223 /// optional #GCancellable object, [`None`] to ignore.
224 ///
225 /// # Returns
226 ///
227 /// A #GFileInfo or [`None`] on error
228 /// or end of enumerator. Free the returned object with
229 /// g_object_unref() when no longer needed.
230 #[doc(alias = "g_file_enumerator_next_file")]
231 fn next_file(
232 &self,
233 cancellable: Option<&impl IsA<Cancellable>>,
234 ) -> Result<Option<FileInfo>, glib::Error> {
235 unsafe {
236 let mut error = std::ptr::null_mut();
237 let ret = ffi::g_file_enumerator_next_file(
238 self.as_ref().to_glib_none().0,
239 cancellable.map(|p| p.as_ref()).to_glib_none().0,
240 &mut error,
241 );
242 if error.is_null() {
243 Ok(from_glib_full(ret))
244 } else {
245 Err(from_glib_full(error))
246 }
247 }
248 }
249
250 /// message);
251 /// ]|
252 ///
253 /// During an async request no other sync and async calls are allowed, and will
254 /// result in [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending] errors.
255 ///
256 /// Any outstanding I/O request with higher priority (lower numerical value) will
257 /// be executed before an outstanding request with lower priority. Default
258 /// priority is `G_PRIORITY_DEFAULT`.
259 /// ## `num_files`
260 /// the number of file info objects to request
261 /// ## `io_priority`
262 /// the [I/O priority](iface.AsyncResult.html#io-priority) of the request
263 /// ## `cancellable`
264 /// optional #GCancellable object, [`None`] to ignore.
265 /// ## `callback`
266 /// a #GAsyncReadyCallback
267 /// to call when the request is satisfied
268 #[doc(alias = "g_file_enumerator_next_files_async")]
269 fn next_files_async<P: FnOnce(Result<Vec<FileInfo>, glib::Error>) + 'static>(
270 &self,
271 num_files: i32,
272 io_priority: glib::Priority,
273 cancellable: Option<&impl IsA<Cancellable>>,
274 callback: P,
275 ) {
276 let main_context = glib::MainContext::ref_thread_default();
277 let is_main_context_owner = main_context.is_owner();
278 let has_acquired_main_context = (!is_main_context_owner)
279 .then(|| main_context.acquire().ok())
280 .flatten();
281 assert!(
282 is_main_context_owner || has_acquired_main_context.is_some(),
283 "Async operations only allowed if the thread is owning the MainContext"
284 );
285
286 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
287 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
288 unsafe extern "C" fn next_files_async_trampoline<
289 P: FnOnce(Result<Vec<FileInfo>, glib::Error>) + 'static,
290 >(
291 _source_object: *mut glib::gobject_ffi::GObject,
292 res: *mut crate::ffi::GAsyncResult,
293 user_data: glib::ffi::gpointer,
294 ) {
295 unsafe {
296 let mut error = std::ptr::null_mut();
297 let ret = ffi::g_file_enumerator_next_files_finish(
298 _source_object as *mut _,
299 res,
300 &mut error,
301 );
302 let result = if error.is_null() {
303 Ok(FromGlibPtrContainer::from_glib_full(ret))
304 } else {
305 Err(from_glib_full(error))
306 };
307 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
308 Box_::from_raw(user_data as *mut _);
309 let callback: P = callback.into_inner();
310 callback(result);
311 }
312 }
313 let callback = next_files_async_trampoline::<P>;
314 unsafe {
315 ffi::g_file_enumerator_next_files_async(
316 self.as_ref().to_glib_none().0,
317 num_files,
318 io_priority.into_glib(),
319 cancellable.map(|p| p.as_ref()).to_glib_none().0,
320 Some(callback),
321 Box_::into_raw(user_data) as *mut _,
322 );
323 }
324 }
325
326 fn next_files_future(
327 &self,
328 num_files: i32,
329 io_priority: glib::Priority,
330 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<FileInfo>, glib::Error>> + 'static>>
331 {
332 Box_::pin(crate::GioFuture::new(
333 self,
334 move |obj, cancellable, send| {
335 obj.next_files_async(num_files, io_priority, Some(cancellable), move |res| {
336 send.resolve(res);
337 });
338 },
339 ))
340 }
341
342 /// Sets the file enumerator as having pending operations.
343 /// ## `pending`
344 /// a boolean value.
345 #[doc(alias = "g_file_enumerator_set_pending")]
346 fn set_pending(&self, pending: bool) {
347 unsafe {
348 ffi::g_file_enumerator_set_pending(self.as_ref().to_glib_none().0, pending.into_glib());
349 }
350 }
351}
352
353impl<O: IsA<FileEnumerator>> FileEnumeratorExt for O {}