Skip to content

Commit 6ec276f

Browse files
committed
Add gio::File subclass
Signed-off-by: fbrouille <fbrouille@users.noreply.github.com>
1 parent ea08f3e commit 6ec276f

File tree

6 files changed

+5347
-8
lines changed

6 files changed

+5347
-8
lines changed

gio/src/file.rs

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

gio/src/prelude.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub use crate::{
3535
action_map::ActionMapExtManual, application::ApplicationExtManual, auto::traits::*,
3636
cancellable::CancellableExtManual, converter::ConverterExtManual,
3737
data_input_stream::DataInputStreamExtManual, datagram_based::DatagramBasedExtManual,
38-
dbus_connection::DBusMethodCall, dbus_proxy::DBusProxyExtManual, file::FileExtManual,
39-
file_enumerator::FileEnumeratorExtManual, inet_address::InetAddressExtManual,
40-
input_stream::InputStreamExtManual, io_stream::IOStreamExtManual,
41-
list_model::ListModelExtManual, output_stream::OutputStreamExtManual,
42-
pollable_input_stream::PollableInputStreamExtManual,
38+
dbus_connection::DBusMethodCall, dbus_proxy::DBusProxyExtManual, file::FileAttributeTypeValue,
39+
file::FileExtManual, file_enumerator::FileEnumeratorExtManual,
40+
inet_address::InetAddressExtManual, input_stream::InputStreamExtManual,
41+
io_stream::IOStreamExtManual, list_model::ListModelExtManual,
42+
output_stream::OutputStreamExtManual, pollable_input_stream::PollableInputStreamExtManual,
4343
pollable_output_stream::PollableOutputStreamExtManual, settings::SettingsExtManual,
4444
simple_proxy_resolver::SimpleProxyResolverExtManual, socket::SocketExtManual,
4545
socket_control_message::SocketControlMessageExtManual,

0 commit comments

Comments
 (0)