Skip to content

Commit 1524a0f

Browse files
committed
Add gio::File subclass
Signed-off-by: fbrouille <[email protected]>
1 parent 94c3909 commit 1524a0f

File tree

8 files changed

+7046
-115
lines changed

8 files changed

+7046
-115
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gio/Gir.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,10 @@ manual_traits = ["FileExtManual"]
796796
manual = true
797797
doc_trait_name = "FileExtManual"
798798
[[object.function]]
799+
name = "copy"
800+
# FnMut callback
801+
manual = true
802+
[[object.function]]
799803
name = "copy_async"
800804
# Multiple callbacks
801805
manual = true
@@ -805,6 +809,10 @@ manual_traits = ["FileExtManual"]
805809
# Manually implemented above
806810
ignore = true
807811
[[object.function]]
812+
name = "move"
813+
# FnMut callback
814+
manual = true
815+
[[object.function]]
808816
name = "move_async"
809817
# Multiple callbacks
810818
manual = true

gio/src/auto/file.rs

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -216,53 +216,6 @@ pub trait FileExt: IsA<File> + 'static {
216216
}
217217
}
218218

219-
#[doc(alias = "g_file_copy")]
220-
fn copy(
221-
&self,
222-
destination: &impl IsA<File>,
223-
flags: FileCopyFlags,
224-
cancellable: Option<&impl IsA<Cancellable>>,
225-
progress_callback: Option<&mut dyn FnMut(i64, i64)>,
226-
) -> Result<(), glib::Error> {
227-
let mut progress_callback_data: Option<&mut dyn FnMut(i64, i64)> = progress_callback;
228-
unsafe extern "C" fn progress_callback_func(
229-
current_num_bytes: i64,
230-
total_num_bytes: i64,
231-
data: glib::ffi::gpointer,
232-
) {
233-
let callback = data as *mut Option<&mut dyn FnMut(i64, i64)>;
234-
if let Some(ref mut callback) = *callback {
235-
callback(current_num_bytes, total_num_bytes)
236-
} else {
237-
panic!("cannot get closure...")
238-
}
239-
}
240-
let progress_callback = if progress_callback_data.is_some() {
241-
Some(progress_callback_func as _)
242-
} else {
243-
None
244-
};
245-
let super_callback0: &mut Option<&mut dyn FnMut(i64, i64)> = &mut progress_callback_data;
246-
unsafe {
247-
let mut error = std::ptr::null_mut();
248-
let is_ok = ffi::g_file_copy(
249-
self.as_ref().to_glib_none().0,
250-
destination.as_ref().to_glib_none().0,
251-
flags.into_glib(),
252-
cancellable.map(|p| p.as_ref()).to_glib_none().0,
253-
progress_callback,
254-
super_callback0 as *mut _ as *mut _,
255-
&mut error,
256-
);
257-
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
258-
if error.is_null() {
259-
Ok(())
260-
} else {
261-
Err(from_glib_full(error))
262-
}
263-
}
264-
}
265-
266219
#[doc(alias = "g_file_copy_attributes")]
267220
fn copy_attributes(
268221
&self,
@@ -1239,54 +1192,6 @@ pub trait FileExt: IsA<File> + 'static {
12391192
))
12401193
}
12411194

1242-
#[doc(alias = "g_file_move")]
1243-
#[doc(alias = "move")]
1244-
fn move_(
1245-
&self,
1246-
destination: &impl IsA<File>,
1247-
flags: FileCopyFlags,
1248-
cancellable: Option<&impl IsA<Cancellable>>,
1249-
progress_callback: Option<&mut dyn FnMut(i64, i64)>,
1250-
) -> Result<(), glib::Error> {
1251-
let mut progress_callback_data: Option<&mut dyn FnMut(i64, i64)> = progress_callback;
1252-
unsafe extern "C" fn progress_callback_func(
1253-
current_num_bytes: i64,
1254-
total_num_bytes: i64,
1255-
data: glib::ffi::gpointer,
1256-
) {
1257-
let callback = data as *mut Option<&mut dyn FnMut(i64, i64)>;
1258-
if let Some(ref mut callback) = *callback {
1259-
callback(current_num_bytes, total_num_bytes)
1260-
} else {
1261-
panic!("cannot get closure...")
1262-
}
1263-
}
1264-
let progress_callback = if progress_callback_data.is_some() {
1265-
Some(progress_callback_func as _)
1266-
} else {
1267-
None
1268-
};
1269-
let super_callback0: &mut Option<&mut dyn FnMut(i64, i64)> = &mut progress_callback_data;
1270-
unsafe {
1271-
let mut error = std::ptr::null_mut();
1272-
let is_ok = ffi::g_file_move(
1273-
self.as_ref().to_glib_none().0,
1274-
destination.as_ref().to_glib_none().0,
1275-
flags.into_glib(),
1276-
cancellable.map(|p| p.as_ref()).to_glib_none().0,
1277-
progress_callback,
1278-
super_callback0 as *mut _ as *mut _,
1279-
&mut error,
1280-
);
1281-
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1282-
if error.is_null() {
1283-
Ok(())
1284-
} else {
1285-
Err(from_glib_full(error))
1286-
}
1287-
}
1288-
}
1289-
12901195
#[doc(alias = "g_file_open_readwrite")]
12911196
fn open_readwrite(
12921197
&self,

gio/src/file.rs

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use glib::{prelude::*, translate::*};
77
#[cfg(feature = "v2_74")]
88
use crate::FileIOStream;
99
use crate::{
10-
ffi, Cancellable, File, FileAttributeValue, FileCreateFlags, FileEnumerator, FileQueryInfoFlags,
10+
ffi, Cancellable, File, FileAttributeValue, FileCopyFlags, FileCreateFlags, FileEnumerator,
11+
FileQueryInfoFlags,
1112
};
1213

1314
impl File {
@@ -357,6 +358,53 @@ pub trait FileExtManual: IsA<File> + Sized {
357358
))
358359
}
359360

361+
#[doc(alias = "g_file_copy")]
362+
fn copy(
363+
&self,
364+
destination: &impl IsA<File>,
365+
flags: FileCopyFlags,
366+
cancellable: Option<&impl IsA<Cancellable>>,
367+
progress_callback: Option<Box<dyn FnMut(i64, i64) + 'static>>,
368+
) -> Result<(), glib::Error> {
369+
let mut super_callback0 = progress_callback;
370+
let (progress_callback, progress_callback_data) =
371+
super_callback0
372+
.as_mut()
373+
.map_or((None, std::ptr::null_mut()), |progress_callback| {
374+
unsafe extern "C" fn progress_callback_trampoline(
375+
current_num_bytes: i64,
376+
total_num_bytes: i64,
377+
user_data: glib::ffi::gpointer,
378+
) {
379+
let progress_callback: &mut Box<dyn FnMut(i64, i64) + 'static> =
380+
&mut *(user_data as *mut _);
381+
progress_callback(current_num_bytes, total_num_bytes);
382+
}
383+
(
384+
Some(progress_callback_trampoline as _),
385+
progress_callback as *mut Box<dyn FnMut(i64, i64) + 'static> as *mut _,
386+
)
387+
});
388+
unsafe {
389+
let mut error = std::ptr::null_mut();
390+
let is_ok = ffi::g_file_copy(
391+
self.as_ref().to_glib_none().0,
392+
destination.as_ref().to_glib_none().0,
393+
flags.into_glib(),
394+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
395+
progress_callback,
396+
progress_callback_data,
397+
&mut error,
398+
);
399+
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
400+
if error.is_null() {
401+
Ok(())
402+
} else {
403+
Err(from_glib_full(error))
404+
}
405+
}
406+
}
407+
360408
#[doc(alias = "g_file_copy_async")]
361409
fn copy_async<Q: FnOnce(Result<(), glib::Error>) + 'static>(
362410
&self,
@@ -704,32 +752,28 @@ pub trait FileExtManual: IsA<File> + Sized {
704752
cancellable: Option<&impl IsA<Cancellable>>,
705753
progress_callback: Option<Box<dyn FnMut(bool, u64, u64, u64) + 'static>>,
706754
) -> Result<(u64, u64, u64), glib::Error> {
707-
let progress_callback_data: Box<
708-
Option<RefCell<Box<dyn FnMut(bool, u64, u64, u64) + 'static>>>,
709-
> = Box::new(progress_callback.map(RefCell::new));
710-
unsafe extern "C" fn progress_callback_func(
755+
let mut super_callback0 = progress_callback;
756+
unsafe extern "C" fn progress_callback_trampoline(
711757
reporting: glib::ffi::gboolean,
712758
current_size: u64,
713759
num_dirs: u64,
714760
num_files: u64,
715761
user_data: glib::ffi::gpointer,
716762
) {
763+
let progress_callback: &mut Box<dyn FnMut(bool, u64, u64, u64) + 'static> =
764+
&mut *(user_data as *mut _);
717765
let reporting = from_glib(reporting);
718-
let callback: &Option<RefCell<Box<dyn Fn(bool, u64, u64, u64) + 'static>>> =
719-
&*(user_data as *mut _);
720-
if let Some(ref callback) = *callback {
721-
(*callback.borrow_mut())(reporting, current_size, num_dirs, num_files)
722-
} else {
723-
panic!("cannot get closure...")
724-
};
766+
progress_callback(reporting, current_size, num_dirs, num_files);
725767
}
726-
let progress_callback = if progress_callback_data.is_some() {
727-
Some(progress_callback_func as _)
768+
let progress_callback = if super_callback0.is_some() {
769+
Some(progress_callback_trampoline as _)
728770
} else {
729771
None
730772
};
731-
let super_callback0: Box<Option<RefCell<Box<dyn FnMut(bool, u64, u64, u64) + 'static>>>> =
732-
progress_callback_data;
773+
let progress_callback_data = super_callback0
774+
.as_mut()
775+
.map_or(std::ptr::null_mut(), |data| data as *mut _)
776+
as *mut _;
733777
unsafe {
734778
let mut disk_usage = mem::MaybeUninit::uninit();
735779
let mut num_dirs = mem::MaybeUninit::uninit();
@@ -740,7 +784,7 @@ pub trait FileExtManual: IsA<File> + Sized {
740784
flags.into_glib(),
741785
cancellable.map(|p| p.as_ref()).to_glib_none().0,
742786
progress_callback,
743-
Box::into_raw(super_callback0) as *mut _,
787+
progress_callback_data,
744788
disk_usage.as_mut_ptr(),
745789
num_dirs.as_mut_ptr(),
746790
num_files.as_mut_ptr(),
@@ -910,6 +954,54 @@ pub trait FileExtManual: IsA<File> + Sized {
910954
(fut, Box::pin(receiver))
911955
}
912956

957+
#[doc(alias = "g_file_move")]
958+
#[doc(alias = "move")]
959+
fn move_(
960+
&self,
961+
destination: &impl IsA<File>,
962+
flags: FileCopyFlags,
963+
cancellable: Option<&impl IsA<Cancellable>>,
964+
progress_callback: Option<Box<dyn FnMut(i64, i64) + 'static>>,
965+
) -> Result<(), glib::Error> {
966+
let mut super_callback0 = progress_callback;
967+
let (progress_callback, progress_callback_data) =
968+
super_callback0
969+
.as_mut()
970+
.map_or((None, std::ptr::null_mut()), |progress_callback| {
971+
unsafe extern "C" fn progress_callback_trampoline(
972+
current_num_bytes: i64,
973+
total_num_bytes: i64,
974+
user_data: glib::ffi::gpointer,
975+
) {
976+
let progress_callback: &mut Box<dyn FnMut(i64, i64) + 'static> =
977+
&mut *(user_data as *mut _);
978+
progress_callback(current_num_bytes, total_num_bytes);
979+
}
980+
(
981+
Some(progress_callback_trampoline as _),
982+
progress_callback as *mut Box<dyn FnMut(i64, i64) + 'static> as *mut _,
983+
)
984+
});
985+
unsafe {
986+
let mut error = std::ptr::null_mut();
987+
let is_ok = ffi::g_file_move(
988+
self.as_ref().to_glib_none().0,
989+
destination.as_ref().to_glib_none().0,
990+
flags.into_glib(),
991+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
992+
progress_callback,
993+
progress_callback_data,
994+
&mut error,
995+
);
996+
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
997+
if error.is_null() {
998+
Ok(())
999+
} else {
1000+
Err(from_glib_full(error))
1001+
}
1002+
}
1003+
}
1004+
9131005
#[cfg(feature = "v2_72")]
9141006
#[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
9151007
#[doc(alias = "g_file_move_async")]

gio/src/file_attribute_value.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ impl FileAttributeValue<'_> {
7676
pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
7777
self.0.as_ptr()
7878
}
79+
80+
pub(crate) fn for_pointer(type_: FileAttributeType, value_p: *mut std::ffi::c_void) -> Self {
81+
Self(FileAttributeValueInner::Pointer(type_, value_p))
82+
}
7983
}
8084

8185
#[derive(Debug)]
8286
pub(crate) enum FileAttributeValueInner<'a> {
83-
#[allow(dead_code)] // TODO remove this allow attribute when Pointer will be used by this crate
8487
Pointer(FileAttributeType, glib::ffi::gpointer),
8588
String(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
8689
ByteString(&'a CStr),

0 commit comments

Comments
 (0)