1use crate::{Bytes, Error, KeyFileFlags, ffi, translate::*};
6
7crate::wrapper! {
8 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9 pub struct KeyFile(Shared<ffi::GKeyFile>);
10
11 match fn {
12 ref => |ptr| ffi::g_key_file_ref(ptr),
13 unref => |ptr| ffi::g_key_file_unref(ptr),
14 type_ => || ffi::g_key_file_get_type(),
15 }
16}
17
18impl KeyFile {
19 #[doc(alias = "g_key_file_new")]
20 pub fn new() -> KeyFile {
21 unsafe { from_glib_full(ffi::g_key_file_new()) }
22 }
23
24 #[doc(alias = "g_key_file_get_comment")]
25 #[doc(alias = "get_comment")]
26 pub fn comment(
27 &self,
28 group_name: Option<&str>,
29 key: Option<&str>,
30 ) -> Result<crate::GString, crate::Error> {
31 unsafe {
32 let mut error = std::ptr::null_mut();
33 let ret = ffi::g_key_file_get_comment(
34 self.to_glib_none().0,
35 group_name.to_glib_none().0,
36 key.to_glib_none().0,
37 &mut error,
38 );
39 if error.is_null() {
40 Ok(from_glib_full(ret))
41 } else {
42 Err(from_glib_full(error))
43 }
44 }
45 }
46
47 #[doc(alias = "g_key_file_get_double")]
48 #[doc(alias = "get_double")]
49 pub fn double(&self, group_name: &str, key: &str) -> Result<f64, crate::Error> {
50 unsafe {
51 let mut error = std::ptr::null_mut();
52 let ret = ffi::g_key_file_get_double(
53 self.to_glib_none().0,
54 group_name.to_glib_none().0,
55 key.to_glib_none().0,
56 &mut error,
57 );
58 if error.is_null() {
59 Ok(ret)
60 } else {
61 Err(from_glib_full(error))
62 }
63 }
64 }
65
66 #[doc(alias = "g_key_file_get_double_list")]
67 #[doc(alias = "get_double_list")]
68 pub fn double_list(&self, group_name: &str, key: &str) -> Result<Vec<f64>, crate::Error> {
69 unsafe {
70 let mut length = std::mem::MaybeUninit::uninit();
71 let mut error = std::ptr::null_mut();
72 let ret = ffi::g_key_file_get_double_list(
73 self.to_glib_none().0,
74 group_name.to_glib_none().0,
75 key.to_glib_none().0,
76 length.as_mut_ptr(),
77 &mut error,
78 );
79 if error.is_null() {
80 Ok(FromGlibContainer::from_glib_container_num(
81 ret,
82 length.assume_init() as _,
83 ))
84 } else {
85 Err(from_glib_full(error))
86 }
87 }
88 }
89
90 #[doc(alias = "g_key_file_get_int64")]
91 #[doc(alias = "get_int64")]
92 pub fn int64(&self, group_name: &str, key: &str) -> Result<i64, crate::Error> {
93 unsafe {
94 let mut error = std::ptr::null_mut();
95 let ret = ffi::g_key_file_get_int64(
96 self.to_glib_none().0,
97 group_name.to_glib_none().0,
98 key.to_glib_none().0,
99 &mut error,
100 );
101 if error.is_null() {
102 Ok(ret)
103 } else {
104 Err(from_glib_full(error))
105 }
106 }
107 }
108
109 #[doc(alias = "g_key_file_get_integer")]
110 #[doc(alias = "get_integer")]
111 pub fn integer(&self, group_name: &str, key: &str) -> Result<i32, crate::Error> {
112 unsafe {
113 let mut error = std::ptr::null_mut();
114 let ret = ffi::g_key_file_get_integer(
115 self.to_glib_none().0,
116 group_name.to_glib_none().0,
117 key.to_glib_none().0,
118 &mut error,
119 );
120 if error.is_null() {
121 Ok(ret)
122 } else {
123 Err(from_glib_full(error))
124 }
125 }
126 }
127
128 #[doc(alias = "g_key_file_get_integer_list")]
129 #[doc(alias = "get_integer_list")]
130 pub fn integer_list(&self, group_name: &str, key: &str) -> Result<Vec<i32>, crate::Error> {
131 unsafe {
132 let mut length = std::mem::MaybeUninit::uninit();
133 let mut error = std::ptr::null_mut();
134 let ret = ffi::g_key_file_get_integer_list(
135 self.to_glib_none().0,
136 group_name.to_glib_none().0,
137 key.to_glib_none().0,
138 length.as_mut_ptr(),
139 &mut error,
140 );
141 if error.is_null() {
142 Ok(FromGlibContainer::from_glib_container_num(
143 ret,
144 length.assume_init() as _,
145 ))
146 } else {
147 Err(from_glib_full(error))
148 }
149 }
150 }
151
152 #[doc(alias = "g_key_file_get_locale_for_key")]
153 #[doc(alias = "get_locale_for_key")]
154 pub fn locale_for_key(
155 &self,
156 group_name: &str,
157 key: &str,
158 locale: Option<&str>,
159 ) -> Option<crate::GString> {
160 unsafe {
161 from_glib_full(ffi::g_key_file_get_locale_for_key(
162 self.to_glib_none().0,
163 group_name.to_glib_none().0,
164 key.to_glib_none().0,
165 locale.to_glib_none().0,
166 ))
167 }
168 }
169
170 #[doc(alias = "g_key_file_get_start_group")]
171 #[doc(alias = "get_start_group")]
172 pub fn start_group(&self) -> Option<crate::GString> {
173 unsafe { from_glib_full(ffi::g_key_file_get_start_group(self.to_glib_none().0)) }
174 }
175
176 #[doc(alias = "g_key_file_get_uint64")]
177 #[doc(alias = "get_uint64")]
178 pub fn uint64(&self, group_name: &str, key: &str) -> Result<u64, crate::Error> {
179 unsafe {
180 let mut error = std::ptr::null_mut();
181 let ret = ffi::g_key_file_get_uint64(
182 self.to_glib_none().0,
183 group_name.to_glib_none().0,
184 key.to_glib_none().0,
185 &mut error,
186 );
187 if error.is_null() {
188 Ok(ret)
189 } else {
190 Err(from_glib_full(error))
191 }
192 }
193 }
194
195 #[doc(alias = "g_key_file_get_value")]
196 #[doc(alias = "get_value")]
197 pub fn value(&self, group_name: &str, key: &str) -> Result<crate::GString, crate::Error> {
198 unsafe {
199 let mut error = std::ptr::null_mut();
200 let ret = ffi::g_key_file_get_value(
201 self.to_glib_none().0,
202 group_name.to_glib_none().0,
203 key.to_glib_none().0,
204 &mut error,
205 );
206 if error.is_null() {
207 Ok(from_glib_full(ret))
208 } else {
209 Err(from_glib_full(error))
210 }
211 }
212 }
213
214 #[doc(alias = "g_key_file_has_group")]
215 pub fn has_group(&self, group_name: &str) -> bool {
216 unsafe {
217 from_glib(ffi::g_key_file_has_group(
218 self.to_glib_none().0,
219 group_name.to_glib_none().0,
220 ))
221 }
222 }
223
224 #[doc(alias = "g_key_file_load_from_bytes")]
225 pub fn load_from_bytes(&self, bytes: &Bytes, flags: KeyFileFlags) -> Result<(), crate::Error> {
226 unsafe {
227 let mut error = std::ptr::null_mut();
228 let is_ok = ffi::g_key_file_load_from_bytes(
229 self.to_glib_none().0,
230 bytes.to_glib_none().0,
231 flags.into_glib(),
232 &mut error,
233 );
234 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
235 if error.is_null() {
236 Ok(())
237 } else {
238 Err(from_glib_full(error))
239 }
240 }
241 }
242
243 #[doc(alias = "g_key_file_load_from_data")]
244 pub fn load_from_data(&self, data: &str, flags: KeyFileFlags) -> Result<(), crate::Error> {
245 let length = data.len() as _;
246 unsafe {
247 let mut error = std::ptr::null_mut();
248 let is_ok = ffi::g_key_file_load_from_data(
249 self.to_glib_none().0,
250 data.to_glib_none().0,
251 length,
252 flags.into_glib(),
253 &mut error,
254 );
255 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
256 if error.is_null() {
257 Ok(())
258 } else {
259 Err(from_glib_full(error))
260 }
261 }
262 }
263
264 #[doc(alias = "g_key_file_load_from_file")]
265 pub fn load_from_file(
266 &self,
267 file: impl AsRef<std::path::Path>,
268 flags: KeyFileFlags,
269 ) -> Result<(), crate::Error> {
270 unsafe {
271 let mut error = std::ptr::null_mut();
272 let is_ok = ffi::g_key_file_load_from_file(
273 self.to_glib_none().0,
274 file.as_ref().to_glib_none().0,
275 flags.into_glib(),
276 &mut error,
277 );
278 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
279 if error.is_null() {
280 Ok(())
281 } else {
282 Err(from_glib_full(error))
283 }
284 }
285 }
286
287 #[cfg(feature = "v2_90")]
288 #[cfg_attr(docsrs, doc(cfg(feature = "v2_90")))]
289 #[doc(alias = "g_key_file_load_unix_configurations")]
290 pub fn load_unix_configurations(
291 &self,
292 project: Option<&str>,
293 etc_subdir: Option<impl AsRef<std::path::Path>>,
294 run_subdir: Option<impl AsRef<std::path::Path>>,
295 usr_subdir: Option<impl AsRef<std::path::Path>>,
296 config_name: impl AsRef<std::path::Path>,
297 config_suffix: Option<impl AsRef<std::path::Path>>,
298 flags: KeyFileFlags,
299 ) -> Result<(), crate::Error> {
300 unsafe {
301 let mut error = std::ptr::null_mut();
302 let is_ok = ffi::g_key_file_load_unix_configurations(
303 self.to_glib_none().0,
304 project.to_glib_none().0,
305 etc_subdir.as_ref().map(|p| p.as_ref()).to_glib_none().0,
306 run_subdir.as_ref().map(|p| p.as_ref()).to_glib_none().0,
307 usr_subdir.as_ref().map(|p| p.as_ref()).to_glib_none().0,
308 config_name.as_ref().to_glib_none().0,
309 config_suffix.as_ref().map(|p| p.as_ref()).to_glib_none().0,
310 flags.into_glib(),
311 &mut error,
312 );
313 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
314 if error.is_null() {
315 Ok(())
316 } else {
317 Err(from_glib_full(error))
318 }
319 }
320 }
321
322 #[doc(alias = "g_key_file_remove_comment")]
323 pub fn remove_comment(
324 &self,
325 group_name: Option<&str>,
326 key: Option<&str>,
327 ) -> Result<(), crate::Error> {
328 unsafe {
329 let mut error = std::ptr::null_mut();
330 let is_ok = ffi::g_key_file_remove_comment(
331 self.to_glib_none().0,
332 group_name.to_glib_none().0,
333 key.to_glib_none().0,
334 &mut error,
335 );
336 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
337 if error.is_null() {
338 Ok(())
339 } else {
340 Err(from_glib_full(error))
341 }
342 }
343 }
344
345 #[doc(alias = "g_key_file_remove_group")]
346 pub fn remove_group(&self, group_name: &str) -> Result<(), crate::Error> {
347 unsafe {
348 let mut error = std::ptr::null_mut();
349 let is_ok = ffi::g_key_file_remove_group(
350 self.to_glib_none().0,
351 group_name.to_glib_none().0,
352 &mut error,
353 );
354 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
355 if error.is_null() {
356 Ok(())
357 } else {
358 Err(from_glib_full(error))
359 }
360 }
361 }
362
363 #[doc(alias = "g_key_file_remove_key")]
364 pub fn remove_key(&self, group_name: &str, key: &str) -> Result<(), crate::Error> {
365 unsafe {
366 let mut error = std::ptr::null_mut();
367 let is_ok = ffi::g_key_file_remove_key(
368 self.to_glib_none().0,
369 group_name.to_glib_none().0,
370 key.to_glib_none().0,
371 &mut error,
372 );
373 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
374 if error.is_null() {
375 Ok(())
376 } else {
377 Err(from_glib_full(error))
378 }
379 }
380 }
381
382 #[doc(alias = "g_key_file_set_boolean")]
383 pub fn set_boolean(&self, group_name: &str, key: &str, value: bool) {
384 unsafe {
385 ffi::g_key_file_set_boolean(
386 self.to_glib_none().0,
387 group_name.to_glib_none().0,
388 key.to_glib_none().0,
389 value.into_glib(),
390 );
391 }
392 }
393
394 #[doc(alias = "g_key_file_set_comment")]
400 pub fn set_comment(
401 &self,
402 group_name: Option<&str>,
403 key: Option<&str>,
404 comment: &str,
405 ) -> Result<(), crate::Error> {
406 unsafe {
407 let mut error = std::ptr::null_mut();
408 let is_ok = ffi::g_key_file_set_comment(
409 self.to_glib_none().0,
410 group_name.to_glib_none().0,
411 key.to_glib_none().0,
412 comment.to_glib_none().0,
413 &mut error,
414 );
415 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
416 if error.is_null() {
417 Ok(())
418 } else {
419 Err(from_glib_full(error))
420 }
421 }
422 }
423
424 #[doc(alias = "g_key_file_set_double")]
425 pub fn set_double(&self, group_name: &str, key: &str, value: f64) {
426 unsafe {
427 ffi::g_key_file_set_double(
428 self.to_glib_none().0,
429 group_name.to_glib_none().0,
430 key.to_glib_none().0,
431 value,
432 );
433 }
434 }
435
436 #[doc(alias = "g_key_file_set_int64")]
437 pub fn set_int64(&self, group_name: &str, key: &str, value: i64) {
438 unsafe {
439 ffi::g_key_file_set_int64(
440 self.to_glib_none().0,
441 group_name.to_glib_none().0,
442 key.to_glib_none().0,
443 value,
444 );
445 }
446 }
447
448 #[doc(alias = "g_key_file_set_integer")]
449 pub fn set_integer(&self, group_name: &str, key: &str, value: i32) {
450 unsafe {
451 ffi::g_key_file_set_integer(
452 self.to_glib_none().0,
453 group_name.to_glib_none().0,
454 key.to_glib_none().0,
455 value,
456 );
457 }
458 }
459
460 #[doc(alias = "g_key_file_set_list_separator")]
461 pub fn set_list_separator(&self, separator: crate::Char) {
462 unsafe {
463 ffi::g_key_file_set_list_separator(self.to_glib_none().0, separator.into_glib());
464 }
465 }
466
467 #[doc(alias = "g_key_file_set_locale_string")]
468 pub fn set_locale_string(&self, group_name: &str, key: &str, locale: &str, string: &str) {
469 unsafe {
470 ffi::g_key_file_set_locale_string(
471 self.to_glib_none().0,
472 group_name.to_glib_none().0,
473 key.to_glib_none().0,
474 locale.to_glib_none().0,
475 string.to_glib_none().0,
476 );
477 }
478 }
479
480 #[doc(alias = "g_key_file_set_string")]
481 pub fn set_string(&self, group_name: &str, key: &str, string: &str) {
482 unsafe {
483 ffi::g_key_file_set_string(
484 self.to_glib_none().0,
485 group_name.to_glib_none().0,
486 key.to_glib_none().0,
487 string.to_glib_none().0,
488 );
489 }
490 }
491
492 #[doc(alias = "g_key_file_set_uint64")]
493 pub fn set_uint64(&self, group_name: &str, key: &str, value: u64) {
494 unsafe {
495 ffi::g_key_file_set_uint64(
496 self.to_glib_none().0,
497 group_name.to_glib_none().0,
498 key.to_glib_none().0,
499 value,
500 );
501 }
502 }
503
504 #[doc(alias = "g_key_file_set_value")]
505 pub fn set_value(&self, group_name: &str, key: &str, value: &str) {
506 unsafe {
507 ffi::g_key_file_set_value(
508 self.to_glib_none().0,
509 group_name.to_glib_none().0,
510 key.to_glib_none().0,
511 value.to_glib_none().0,
512 );
513 }
514 }
515}
516
517impl Default for KeyFile {
518 fn default() -> Self {
519 Self::new()
520 }
521}