Skip to content

Commit 40d63f7

Browse files
committed
Add gio::File subclass
Signed-off-by: fbrouille <fbrouille@users.noreply.github.com>
1 parent 5b6ade1 commit 40d63f7

File tree

6 files changed

+5707
-3
lines changed

6 files changed

+5707
-3
lines changed

gio/src/file.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use glib::{prelude::*, translate::*};
66

77
#[cfg(feature = "v2_74")]
88
use crate::FileIOStream;
9-
use crate::{ffi, Cancellable, File, FileCreateFlags, FileEnumerator, FileQueryInfoFlags};
9+
use crate::{
10+
ffi, Cancellable, File, FileAttributeType, FileCreateFlags, FileEnumerator, FileQueryInfoFlags,
11+
};
1012

1113
impl File {
1214
#[cfg(feature = "v2_74")]
@@ -1111,6 +1113,99 @@ pub trait FileExtManual: IsA<File> + Sized {
11111113

11121114
(fut, Box::pin(receiver))
11131115
}
1116+
1117+
#[doc(alias = "g_file_set_attribute")]
1118+
fn set_attribute<T>(
1119+
&self,
1120+
attribute: &str,
1121+
type_: FileAttributeType,
1122+
value: FileAttributeTypeValue<T>,
1123+
flags: FileQueryInfoFlags,
1124+
cancellable: Option<&impl IsA<Cancellable>>,
1125+
) -> Result<(), glib::Error> {
1126+
unsafe {
1127+
let mut error = std::ptr::null_mut();
1128+
let is_ok = ffi::g_file_set_attribute(
1129+
self.as_ref().to_glib_none().0,
1130+
attribute.to_glib_none().0,
1131+
type_.into_glib(),
1132+
value.0,
1133+
flags.into_glib(),
1134+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
1135+
&mut error,
1136+
);
1137+
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1138+
if error.is_null() {
1139+
Ok(())
1140+
} else {
1141+
Err(from_glib_full(error))
1142+
}
1143+
}
1144+
}
11141145
}
11151146

11161147
impl<O: IsA<File>> FileExtManual for O {}
1148+
1149+
// checker-ignore-item
1150+
pub struct FileAttributeTypeValue<T>(pub glib::ffi::gpointer, pub T);
1151+
1152+
impl<T> From<*const T> for FileAttributeTypeValue<*const T> {
1153+
fn from(value: *const T) -> Self {
1154+
Self(value as _, value)
1155+
}
1156+
}
1157+
1158+
impl<T> From<*mut T> for FileAttributeTypeValue<*mut T> {
1159+
fn from(value: *mut T) -> Self {
1160+
Self(value as _, value)
1161+
}
1162+
}
1163+
1164+
impl<'a> From<&'a str>
1165+
for FileAttributeTypeValue<<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage>
1166+
{
1167+
fn from(value: &'a str) -> Self {
1168+
let s = ToGlibPtr::<*mut libc::c_char>::to_glib_none(value);
1169+
Self(s.0 as _, s.1)
1170+
}
1171+
}
1172+
1173+
impl<T: Copy> From<&T> for FileAttributeTypeValue<T>
1174+
where
1175+
FileAttributeTypeValue<T>: From<T>,
1176+
{
1177+
fn from(value: &T) -> Self {
1178+
(*value).into()
1179+
}
1180+
}
1181+
1182+
impl From<bool> for FileAttributeTypeValue<glib::ffi::gboolean> {
1183+
fn from(value: bool) -> Self {
1184+
let b = value.into_glib();
1185+
Self(&b as *const _ as _, b)
1186+
}
1187+
}
1188+
1189+
impl From<u32> for FileAttributeTypeValue<u32> {
1190+
fn from(value: u32) -> Self {
1191+
Self(&value as *const _ as _, value)
1192+
}
1193+
}
1194+
1195+
impl From<i32> for FileAttributeTypeValue<i32> {
1196+
fn from(value: i32) -> Self {
1197+
Self(&value as *const _ as _, value)
1198+
}
1199+
}
1200+
1201+
impl From<u64> for FileAttributeTypeValue<u64> {
1202+
fn from(value: u64) -> Self {
1203+
Self(&value as *const _ as _, value)
1204+
}
1205+
}
1206+
1207+
impl From<i64> for FileAttributeTypeValue<i64> {
1208+
fn from(value: i64) -> Self {
1209+
Self(&value as *const _ as _, value)
1210+
}
1211+
}

gio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod debug_controller_dbus;
4343
mod desktop_app_info;
4444
mod error;
4545
mod file;
46+
pub use file::FileAttributeTypeValue;
4647
mod file_attribute_info;
4748
pub use crate::file_attribute_info::FileAttributeInfo;
4849
mod file_attribute_info_list;

0 commit comments

Comments
 (0)