1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 use crate::error::Error; 4 use crate::gstring::GString; 5 use crate::translate::*; 6 use crate::KeyFileFlags; 7 use std::mem; 8 use std::path; 9 use std::ptr; 10 11 use crate::KeyFile; 12 13 impl KeyFile { 14 #[doc(alias = "g_key_file_save_to_file")] save_to_file<T: AsRef<std::path::Path>>(&self, filename: T) -> Result<(), Error>15 pub fn save_to_file<T: AsRef<std::path::Path>>(&self, filename: T) -> Result<(), Error> { 16 unsafe { 17 let mut error = ptr::null_mut(); 18 let _ = ffi::g_key_file_save_to_file( 19 self.to_glib_none().0, 20 filename.as_ref().to_glib_none().0, 21 &mut error, 22 ); 23 if error.is_null() { 24 Ok(()) 25 } else { 26 Err(from_glib_full(error)) 27 } 28 } 29 } 30 31 #[doc(alias = "g_key_file_load_from_data_dirs")] load_from_data_dirs<T: AsRef<std::path::Path>>( &self, file: T, flags: KeyFileFlags, ) -> Result<path::PathBuf, Error>32 pub fn load_from_data_dirs<T: AsRef<std::path::Path>>( 33 &self, 34 file: T, 35 flags: KeyFileFlags, 36 ) -> Result<path::PathBuf, Error> { 37 unsafe { 38 let mut error = ptr::null_mut(); 39 let mut full_path: *mut libc::c_char = ptr::null_mut(); 40 let _ = ffi::g_key_file_load_from_data_dirs( 41 self.to_glib_none().0, 42 file.as_ref().to_glib_none().0, 43 &mut full_path, 44 flags.into_glib(), 45 &mut error, 46 ); 47 if error.is_null() { 48 let path: GString = from_glib_full(full_path); 49 Ok(path::PathBuf::from(&path)) 50 } else { 51 Err(from_glib_full(error)) 52 } 53 } 54 } 55 56 #[doc(alias = "g_key_file_load_from_dirs")] load_from_dirs<T: AsRef<std::path::Path>, U: AsRef<std::path::Path>>( &self, file: T, search_dirs: &[U], flags: KeyFileFlags, ) -> Result<path::PathBuf, Error>57 pub fn load_from_dirs<T: AsRef<std::path::Path>, U: AsRef<std::path::Path>>( 58 &self, 59 file: T, 60 search_dirs: &[U], 61 flags: KeyFileFlags, 62 ) -> Result<path::PathBuf, Error> { 63 unsafe { 64 let search_dirs: Vec<&std::path::Path> = 65 search_dirs.iter().map(AsRef::as_ref).collect(); 66 let mut error = ptr::null_mut(); 67 let mut full_path: *mut libc::c_char = ptr::null_mut(); 68 let _ = ffi::g_key_file_load_from_dirs( 69 self.to_glib_none().0, 70 file.as_ref().to_glib_none().0, 71 search_dirs.to_glib_none().0, 72 &mut full_path, 73 flags.into_glib(), 74 &mut error, 75 ); 76 if error.is_null() { 77 let path: GString = from_glib_full(full_path); 78 Ok(path::PathBuf::from(&path)) 79 } else { 80 Err(from_glib_full(error)) 81 } 82 } 83 } 84 85 #[doc(alias = "g_key_file_to_data")] to_data(&self) -> GString86 pub fn to_data(&self) -> GString { 87 unsafe { 88 let ret = 89 ffi::g_key_file_to_data(self.to_glib_none().0, ptr::null_mut(), ptr::null_mut()); 90 from_glib_full(ret) 91 } 92 } 93 94 #[doc(alias = "g_key_file_get_boolean")] 95 #[doc(alias = "get_boolean")] boolean(&self, group_name: &str, key: &str) -> Result<bool, Error>96 pub fn boolean(&self, group_name: &str, key: &str) -> Result<bool, Error> { 97 unsafe { 98 let mut error = ptr::null_mut(); 99 let ret = ffi::g_key_file_get_boolean( 100 self.to_glib_none().0, 101 group_name.to_glib_none().0, 102 key.to_glib_none().0, 103 &mut error, 104 ); 105 if error.is_null() { 106 Ok(from_glib(ret)) 107 } else { 108 Err(from_glib_full(error)) 109 } 110 } 111 } 112 113 #[doc(alias = "g_key_file_has_key")] has_key(&self, group_name: &str, key: &str) -> Result<bool, Error>114 pub fn has_key(&self, group_name: &str, key: &str) -> Result<bool, Error> { 115 unsafe { 116 let mut error = ptr::null_mut(); 117 let ret = ffi::g_key_file_has_key( 118 self.to_glib_none().0, 119 group_name.to_glib_none().0, 120 key.to_glib_none().0, 121 &mut error, 122 ); 123 if error.is_null() { 124 Ok(from_glib(ret)) 125 } else { 126 Err(from_glib_full(error)) 127 } 128 } 129 } 130 131 #[doc(alias = "g_key_file_get_boolean_list")] 132 #[doc(alias = "get_boolean_list")] boolean_list(&self, group_name: &str, key: &str) -> Result<Vec<bool>, Error>133 pub fn boolean_list(&self, group_name: &str, key: &str) -> Result<Vec<bool>, Error> { 134 unsafe { 135 let mut length = mem::MaybeUninit::uninit(); 136 let mut error = ptr::null_mut(); 137 let ret = ffi::g_key_file_get_boolean_list( 138 self.to_glib_none().0, 139 group_name.to_glib_none().0, 140 key.to_glib_none().0, 141 length.as_mut_ptr(), 142 &mut error, 143 ); 144 if !error.is_null() { 145 return Err(from_glib_full(error)); 146 } 147 Ok(FromGlibContainer::from_glib_container_num( 148 ret, 149 length.assume_init() as usize, 150 )) 151 } 152 } 153 154 #[doc(alias = "g_key_file_get_string")] 155 #[doc(alias = "get_string")] string(&self, group_name: &str, key: &str) -> Result<GString, Error>156 pub fn string(&self, group_name: &str, key: &str) -> Result<GString, Error> { 157 unsafe { 158 let mut error = ptr::null_mut(); 159 let ret = ffi::g_key_file_get_string( 160 self.to_glib_none().0, 161 group_name.to_glib_none().0, 162 key.to_glib_none().0, 163 &mut error, 164 ); 165 if error.is_null() { 166 Ok(from_glib_full(ret)) 167 } else { 168 if !ret.is_null() { 169 ffi::g_free(ret as *mut _); 170 } 171 Err(from_glib_full(error)) 172 } 173 } 174 } 175 176 #[doc(alias = "g_key_file_get_string_list")] 177 #[doc(alias = "get_string_list")] string_list(&self, group_name: &str, key: &str) -> Result<Vec<GString>, Error>178 pub fn string_list(&self, group_name: &str, key: &str) -> Result<Vec<GString>, Error> { 179 unsafe { 180 let mut length = mem::MaybeUninit::uninit(); 181 let mut error = ptr::null_mut(); 182 let ret = ffi::g_key_file_get_string_list( 183 self.to_glib_none().0, 184 group_name.to_glib_none().0, 185 key.to_glib_none().0, 186 length.as_mut_ptr(), 187 &mut error, 188 ); 189 if error.is_null() { 190 Ok(FromGlibContainer::from_glib_full_num( 191 ret, 192 length.assume_init() as usize, 193 )) 194 } else { 195 if !ret.is_null() { 196 ffi::g_strfreev(ret); 197 } 198 Err(from_glib_full(error)) 199 } 200 } 201 } 202 203 #[doc(alias = "g_key_file_get_locale_string")] 204 #[doc(alias = "get_locale_string")] locale_string( &self, group_name: &str, key: &str, locale: Option<&str>, ) -> Result<GString, Error>205 pub fn locale_string( 206 &self, 207 group_name: &str, 208 key: &str, 209 locale: Option<&str>, 210 ) -> Result<GString, Error> { 211 unsafe { 212 let mut error = ptr::null_mut(); 213 let ret = ffi::g_key_file_get_locale_string( 214 self.to_glib_none().0, 215 group_name.to_glib_none().0, 216 key.to_glib_none().0, 217 locale.to_glib_none().0, 218 &mut error, 219 ); 220 if error.is_null() { 221 Ok(from_glib_full(ret)) 222 } else { 223 if !ret.is_null() { 224 ffi::g_free(ret as *mut _); 225 } 226 Err(from_glib_full(error)) 227 } 228 } 229 } 230 231 #[doc(alias = "g_key_file_get_locale_string_list")] 232 #[doc(alias = "get_locale_string_list")] locale_string_list( &self, group_name: &str, key: &str, locale: Option<&str>, ) -> Result<Vec<GString>, Error>233 pub fn locale_string_list( 234 &self, 235 group_name: &str, 236 key: &str, 237 locale: Option<&str>, 238 ) -> Result<Vec<GString>, Error> { 239 unsafe { 240 let mut length = mem::MaybeUninit::uninit(); 241 let mut error = ptr::null_mut(); 242 let ret = ffi::g_key_file_get_locale_string_list( 243 self.to_glib_none().0, 244 group_name.to_glib_none().0, 245 key.to_glib_none().0, 246 locale.to_glib_none().0, 247 length.as_mut_ptr(), 248 &mut error, 249 ); 250 if error.is_null() { 251 Ok(FromGlibContainer::from_glib_full_num( 252 ret, 253 length.assume_init() as usize, 254 )) 255 } else { 256 if !ret.is_null() { 257 ffi::g_strfreev(ret); 258 } 259 Err(from_glib_full(error)) 260 } 261 } 262 } 263 } 264