Skip to content

Commit 2deef12

Browse files
committed
Implement gio::File::set_attribute
Signed-off-by: fbrouille <[email protected]>
1 parent 39e634f commit 2deef12

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

gio/src/file.rs

Lines changed: 134 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,137 @@ 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(
1119+
&self,
1120+
attribute: &str,
1121+
type_value: FileAttributeTypeValue,
1122+
flags: FileQueryInfoFlags,
1123+
cancellable: Option<&impl IsA<Cancellable>>,
1124+
) -> Result<(), glib::Error> {
1125+
unsafe {
1126+
let mut error = std::ptr::null_mut();
1127+
let is_ok = ffi::g_file_set_attribute(
1128+
self.as_ref().to_glib_none().0,
1129+
attribute.to_glib_none().0,
1130+
type_value.type_().into_glib(),
1131+
type_value.to_glib_none().0,
1132+
flags.into_glib(),
1133+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
1134+
&mut error,
1135+
);
1136+
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1137+
if error.is_null() {
1138+
Ok(())
1139+
} else {
1140+
Err(from_glib_full(error))
1141+
}
1142+
}
1143+
}
11141144
}
11151145

11161146
impl<O: IsA<File>> FileExtManual for O {}
1147+
1148+
#[derive(Debug)]
1149+
// checker-ignore-item
1150+
pub enum FileAttributeTypeValue<'a> {
1151+
Pointer(FileAttributeType, glib::ffi::gpointer),
1152+
String(&'a str),
1153+
ByteString(&'a str),
1154+
Boolean(bool),
1155+
Uint32(u32),
1156+
Int32(i32),
1157+
Uint64(u64),
1158+
Int64(i64),
1159+
Object(&'a glib::Object),
1160+
Stringv(Vec<&'a str>),
1161+
}
1162+
1163+
impl FileAttributeTypeValue<'_> {
1164+
pub fn type_(&self) -> FileAttributeType {
1165+
match self {
1166+
Self::Pointer(type_, _) => *type_,
1167+
Self::String(_) => FileAttributeType::String,
1168+
Self::ByteString(_) => FileAttributeType::ByteString,
1169+
Self::Boolean(_) => FileAttributeType::Boolean,
1170+
Self::Uint32(_) => FileAttributeType::Uint32,
1171+
Self::Int32(_) => FileAttributeType::Int32,
1172+
Self::Uint64(_) => FileAttributeType::Uint64,
1173+
Self::Int64(_) => FileAttributeType::Int64,
1174+
Self::Object(_) => FileAttributeType::Object,
1175+
Self::Stringv(_) => FileAttributeType::Stringv,
1176+
}
1177+
}
1178+
}
1179+
1180+
impl<'a> ToGlibPtr<'a, glib::ffi::gpointer> for FileAttributeTypeValue<'a> {
1181+
type Storage = FileAttributeTypeValueStorage<'a>;
1182+
1183+
fn to_glib_none(&'a self) -> Stash<'a, glib::ffi::gpointer, Self> {
1184+
let s = match self {
1185+
Self::Pointer(_, pointer) => FileAttributeTypeValueStorage::Pointer(*pointer),
1186+
Self::String(value) => FileAttributeTypeValueStorage::String(
1187+
ToGlibPtr::<*mut libc::c_char>::to_glib_none(value).1,
1188+
),
1189+
Self::ByteString(value) => FileAttributeTypeValueStorage::ByteString(
1190+
ToGlibPtr::<*mut libc::c_char>::to_glib_none(value).1,
1191+
),
1192+
Self::Boolean(value) => FileAttributeTypeValueStorage::Boolean(value.into_glib()),
1193+
Self::Uint32(value) => FileAttributeTypeValueStorage::Uint32(value),
1194+
Self::Int32(value) => FileAttributeTypeValueStorage::Int32(value),
1195+
Self::Uint64(value) => FileAttributeTypeValueStorage::Uint64(value),
1196+
Self::Int64(value) => FileAttributeTypeValueStorage::Int64(value),
1197+
Self::Object(value) => {
1198+
FileAttributeTypeValueStorage::Object(ToGlibPtr::<
1199+
<glib::Object as glib::translate::GlibPtrDefault>::GlibType,
1200+
>::to_glib_none(value))
1201+
}
1202+
Self::Stringv(value) => FileAttributeTypeValueStorage::Stringv(
1203+
ToGlibContainerFromSlice::<*mut *mut libc::c_char>::to_glib_none_from_slice(value)
1204+
.1,
1205+
),
1206+
};
1207+
Stash(s.as_ptr(), s)
1208+
}
1209+
}
1210+
1211+
impl FromGlib<(crate::ffi::GFileAttributeType, glib::ffi::gpointer)>
1212+
for FileAttributeTypeValue<'_>
1213+
{
1214+
#[inline]
1215+
unsafe fn from_glib(value: (crate::ffi::GFileAttributeType, glib::ffi::gpointer)) -> Self {
1216+
FileAttributeTypeValue::Pointer(from_glib(value.0), value.1)
1217+
}
1218+
}
1219+
1220+
// checker-ignore-item
1221+
pub enum FileAttributeTypeValueStorage<'a> {
1222+
Pointer(glib::ffi::gpointer),
1223+
String(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
1224+
ByteString(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
1225+
Boolean(glib::ffi::gboolean),
1226+
Uint32(&'a u32),
1227+
Int32(&'a i32),
1228+
Uint64(&'a u64),
1229+
Int64(&'a i64),
1230+
Object(Stash<'a, <glib::Object as glib::translate::GlibPtrDefault>::GlibType, glib::Object>),
1231+
Stringv(<&'a str as ToGlibContainerFromSlice<'a, *mut *mut libc::c_char>>::Storage),
1232+
}
1233+
1234+
impl FileAttributeTypeValueStorage<'_> {
1235+
pub fn as_ptr(&self) -> glib::ffi::gpointer {
1236+
match self {
1237+
Self::Pointer(s) => *s,
1238+
Self::String(s) => s.as_ptr() as _,
1239+
Self::ByteString(s) => s.as_ptr() as _,
1240+
Self::Boolean(s) => s as *const _ as _,
1241+
Self::Uint32(s) => *s as *const _ as _,
1242+
Self::Int32(s) => *s as *const _ as _,
1243+
Self::Uint64(s) => *s as *const _ as _,
1244+
Self::Int64(s) => *s as *const _ as _,
1245+
Self::Object(s) => s.0 as _,
1246+
Self::Stringv(s) => s.1.as_ref().unwrap().as_ptr() as _,
1247+
}
1248+
}
1249+
}

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)