glib/auto/key_file.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*, Bytes, Error, KeyFileFlags};
6
7crate::wrapper! {
8 /// `GKeyFile` parses .ini-like config files.
9 ///
10 /// `GKeyFile` lets you parse, edit or create files containing groups of
11 /// key-value pairs, which we call ‘key files’ for lack of a better name.
12 /// Several freedesktop.org specifications use key files. For example, the
13 /// [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/)
14 /// and the [Icon Theme Specification](https://specifications.freedesktop.org/icon-theme-spec/latest/).
15 ///
16 /// The syntax of key files is described in detail in the
17 /// [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/),
18 /// here is a quick summary: Key files consists of groups of key-value pairs, interspersed
19 /// with comments.
20 ///
21 /// **⚠️ The following code is in txt ⚠️**
22 ///
23 /// ```txt
24 /// # this is just an example
25 /// # there can be comments before the first group
26 ///
27 /// [First Group]
28 ///
29 /// Name=Key File Example\tthis value shows\nescaping
30 ///
31 /// # localized strings are stored in multiple key-value pairs
32 /// Welcome=Hello
33 /// Welcome[de]=Hallo
34 /// Welcome[fr_FR]=Bonjour
35 /// Welcome[it]=Ciao
36 ///
37 /// [Another Group]
38 ///
39 /// Numbers=2;20;-200;0
40 ///
41 /// Booleans=true;false;true;true
42 /// ```
43 ///
44 /// Lines beginning with a `#` and blank lines are considered comments.
45 ///
46 /// Groups are started by a header line containing the group name enclosed
47 /// in `[` and `]`, and ended implicitly by the start of the next group or
48 /// the end of the file. Each key-value pair must be contained in a group.
49 ///
50 /// Key-value pairs generally have the form `key=value`, with the exception
51 /// of localized strings, which have the form `key[locale]=value`, with a
52 /// locale identifier of the form `lang_COUNTRY@MODIFIER` where `COUNTRY`
53 /// and `MODIFIER` are optional. As a special case, the locale `C` is associated
54 /// with the untranslated pair `key=value` (since GLib 2.84). Space before and
55 /// after the `=` character is ignored. Newline, tab, carriage return and
56 /// backslash characters in value are escaped as `\n`, `\t`, `\r`, and `\\\\`,
57 /// respectively. To preserve leading spaces in values, these can also be escaped
58 /// as `\s`.
59 ///
60 /// Key files can store strings (possibly with localized variants), integers,
61 /// booleans and lists of these. Lists are separated by a separator character,
62 /// typically `;` or `,`. To use the list separator character in a value in
63 /// a list, it has to be escaped by prefixing it with a backslash.
64 ///
65 /// This syntax is obviously inspired by the .ini files commonly met
66 /// on Windows, but there are some important differences:
67 ///
68 /// - .ini files use the `;` character to begin comments,
69 /// key files use the `#` character.
70 ///
71 /// - Key files do not allow for ungrouped keys meaning only
72 /// comments can precede the first group.
73 ///
74 /// - Key files are always encoded in UTF-8.
75 ///
76 /// - Key and Group names are case-sensitive. For example, a group called
77 /// `[GROUP]` is a different from `[group]`.
78 ///
79 /// - .ini files don’t have a strongly typed boolean entry type,
80 /// they only have `GetProfileInt()`. In key files, only
81 /// `true` and `false` (in lower case) are allowed.
82 ///
83 /// Note that in contrast to the
84 /// [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/),
85 /// groups in key files may contain the same key multiple times; the last entry wins.
86 /// Key files may also contain multiple groups with the same name; they are merged
87 /// together. Another difference is that keys and group names in key files are not
88 /// restricted to ASCII characters.
89 ///
90 /// Here is an example of loading a key file and reading a value:
91 ///
92 /// **⚠️ The following code is in c ⚠️**
93 ///
94 /// ```c
95 /// g_autoptr(GError) error = NULL;
96 /// g_autoptr(GKeyFile) key_file = g_key_file_new ();
97 ///
98 /// if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
99 /// {
100 /// if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
101 /// g_warning ("Error loading key file: %s", error->message);
102 /// return;
103 /// }
104 ///
105 /// g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
106 /// if (val == NULL &&
107 /// !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
108 /// {
109 /// g_warning ("Error finding key in key file: %s", error->message);
110 /// return;
111 /// }
112 /// else if (val == NULL)
113 /// {
114 /// // Fall back to a default value.
115 /// val = g_strdup ("default-value");
116 /// }
117 /// ```
118 ///
119 /// Here is an example of creating and saving a key file:
120 ///
121 /// **⚠️ The following code is in c ⚠️**
122 ///
123 /// ```c
124 /// g_autoptr(GKeyFile) key_file = g_key_file_new ();
125 /// const gchar *val = …;
126 /// g_autoptr(GError) error = NULL;
127 ///
128 /// g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
129 ///
130 /// // Save as a file.
131 /// if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
132 /// {
133 /// g_warning ("Error saving key file: %s", error->message);
134 /// return;
135 /// }
136 ///
137 /// // Or store to a GBytes for use elsewhere.
138 /// gsize data_len;
139 /// g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
140 /// if (data == NULL)
141 /// {
142 /// g_warning ("Error saving key file: %s", error->message);
143 /// return;
144 /// }
145 /// g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
146 /// ```
147 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
148 pub struct KeyFile(Shared<ffi::GKeyFile>);
149
150 match fn {
151 ref => |ptr| ffi::g_key_file_ref(ptr),
152 unref => |ptr| ffi::g_key_file_unref(ptr),
153 type_ => || ffi::g_key_file_get_type(),
154 }
155}
156
157impl KeyFile {
158 /// Creates a new empty [`KeyFile`][crate::KeyFile] object.
159 ///
160 /// Use [`load_from_file()`][Self::load_from_file()],
161 /// [`load_from_data()`][Self::load_from_data()], [`load_from_dirs()`][Self::load_from_dirs()] or
162 /// [`load_from_data_dirs()`][Self::load_from_data_dirs()] to
163 /// read an existing key file.
164 ///
165 /// # Returns
166 ///
167 /// an empty [`KeyFile`][crate::KeyFile].
168 #[doc(alias = "g_key_file_new")]
169 pub fn new() -> KeyFile {
170 unsafe { from_glib_full(ffi::g_key_file_new()) }
171 }
172
173 /// Retrieves a comment above @key from @group_name.
174 ///
175 /// If @key is `NULL` then @comment will be read from above
176 /// @group_name. If both @key and @group_name are `NULL`, then
177 /// @comment will be read from above the first group in the file.
178 ///
179 /// Note that the returned string does not include the `#` comment markers,
180 /// but does include any whitespace after them (on each line). It includes
181 /// the line breaks between lines, but does not include the final line break.
182 /// ## `group_name`
183 /// a group name, or `NULL` to get a top-level comment
184 /// ## `key`
185 /// a key, or `NULL` to get a group comment
186 ///
187 /// # Returns
188 ///
189 /// a comment that should be freed with `free()`
190 #[doc(alias = "g_key_file_get_comment")]
191 #[doc(alias = "get_comment")]
192 pub fn comment(
193 &self,
194 group_name: Option<&str>,
195 key: Option<&str>,
196 ) -> Result<crate::GString, crate::Error> {
197 unsafe {
198 let mut error = std::ptr::null_mut();
199 let ret = ffi::g_key_file_get_comment(
200 self.to_glib_none().0,
201 group_name.to_glib_none().0,
202 key.to_glib_none().0,
203 &mut error,
204 );
205 if error.is_null() {
206 Ok(from_glib_full(ret))
207 } else {
208 Err(from_glib_full(error))
209 }
210 }
211 }
212
213 /// Returns the value associated with @key under @group_name as a double.
214 ///
215 /// If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
216 /// returned. Likewise, if the value associated with @key cannot be interpreted
217 /// as a double then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
218 /// ## `group_name`
219 /// a group name
220 /// ## `key`
221 /// a key
222 ///
223 /// # Returns
224 ///
225 /// the value associated with the key as a double, or
226 /// `0.0` if the key was not found or could not be parsed.
227 #[doc(alias = "g_key_file_get_double")]
228 #[doc(alias = "get_double")]
229 pub fn double(&self, group_name: &str, key: &str) -> Result<f64, crate::Error> {
230 unsafe {
231 let mut error = std::ptr::null_mut();
232 let ret = ffi::g_key_file_get_double(
233 self.to_glib_none().0,
234 group_name.to_glib_none().0,
235 key.to_glib_none().0,
236 &mut error,
237 );
238 if error.is_null() {
239 Ok(ret)
240 } else {
241 Err(from_glib_full(error))
242 }
243 }
244 }
245
246 /// Returns the values associated with @key under @group_name as
247 /// doubles.
248 ///
249 /// If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
250 /// returned. Likewise, if the values associated with @key cannot be interpreted
251 /// as doubles then [error@GLib.KeyFileError.INVALID_VALUE] is returned.
252 /// ## `group_name`
253 /// a group name
254 /// ## `key`
255 /// a key
256 ///
257 /// # Returns
258 ///
259 ///
260 /// the values associated with the key as a list of doubles, or `NULL` if the
261 /// key was not found or could not be parsed. The returned list of doubles
262 /// should be freed with `free()` when no longer needed.
263 #[doc(alias = "g_key_file_get_double_list")]
264 #[doc(alias = "get_double_list")]
265 pub fn double_list(&self, group_name: &str, key: &str) -> Result<Vec<f64>, crate::Error> {
266 unsafe {
267 let mut length = std::mem::MaybeUninit::uninit();
268 let mut error = std::ptr::null_mut();
269 let ret = ffi::g_key_file_get_double_list(
270 self.to_glib_none().0,
271 group_name.to_glib_none().0,
272 key.to_glib_none().0,
273 length.as_mut_ptr(),
274 &mut error,
275 );
276 if error.is_null() {
277 Ok(FromGlibContainer::from_glib_container_num(
278 ret,
279 length.assume_init() as _,
280 ))
281 } else {
282 Err(from_glib_full(error))
283 }
284 }
285 }
286
287 /// Returns the value associated with @key under @group_name as a signed
288 /// 64-bit integer.
289 ///
290 /// This is similar to [`integer()`][Self::integer()] but can return
291 /// 64-bit results without truncation.
292 /// ## `group_name`
293 /// a group name
294 /// ## `key`
295 /// a key
296 ///
297 /// # Returns
298 ///
299 /// the value associated with the key as a signed 64-bit integer, or
300 /// `0` if the key was not found or could not be parsed.
301 #[doc(alias = "g_key_file_get_int64")]
302 #[doc(alias = "get_int64")]
303 pub fn int64(&self, group_name: &str, key: &str) -> Result<i64, crate::Error> {
304 unsafe {
305 let mut error = std::ptr::null_mut();
306 let ret = ffi::g_key_file_get_int64(
307 self.to_glib_none().0,
308 group_name.to_glib_none().0,
309 key.to_glib_none().0,
310 &mut error,
311 );
312 if error.is_null() {
313 Ok(ret)
314 } else {
315 Err(from_glib_full(error))
316 }
317 }
318 }
319
320 /// Returns the value associated with @key under @group_name as an
321 /// integer.
322 ///
323 /// If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
324 /// returned. Likewise, if the value associated with @key cannot be interpreted
325 /// as an integer, or is out of range for a `gint`, then
326 /// [error@GLib.KeyFileError.INVALID_VALUE] is returned.
327 /// ## `group_name`
328 /// a group name
329 /// ## `key`
330 /// a key
331 ///
332 /// # Returns
333 ///
334 /// the value associated with the key as an integer, or
335 /// `0` if the key was not found or could not be parsed.
336 #[doc(alias = "g_key_file_get_integer")]
337 #[doc(alias = "get_integer")]
338 pub fn integer(&self, group_name: &str, key: &str) -> Result<i32, crate::Error> {
339 unsafe {
340 let mut error = std::ptr::null_mut();
341 let ret = ffi::g_key_file_get_integer(
342 self.to_glib_none().0,
343 group_name.to_glib_none().0,
344 key.to_glib_none().0,
345 &mut error,
346 );
347 if error.is_null() {
348 Ok(ret)
349 } else {
350 Err(from_glib_full(error))
351 }
352 }
353 }
354
355 /// Returns the values associated with @key under @group_name as
356 /// integers.
357 ///
358 /// If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is
359 /// returned. Likewise, if the values associated with @key cannot be interpreted
360 /// as integers, or are out of range for `gint`, then
361 /// [error@GLib.KeyFileError.INVALID_VALUE] is returned.
362 /// ## `group_name`
363 /// a group name
364 /// ## `key`
365 /// a key
366 ///
367 /// # Returns
368 ///
369 ///
370 /// the values associated with the key as a list of integers, or `NULL` if
371 /// the key was not found or could not be parsed. The returned list of
372 /// integers should be freed with `free()` when no longer needed.
373 #[doc(alias = "g_key_file_get_integer_list")]
374 #[doc(alias = "get_integer_list")]
375 pub fn integer_list(&self, group_name: &str, key: &str) -> Result<Vec<i32>, crate::Error> {
376 unsafe {
377 let mut length = std::mem::MaybeUninit::uninit();
378 let mut error = std::ptr::null_mut();
379 let ret = ffi::g_key_file_get_integer_list(
380 self.to_glib_none().0,
381 group_name.to_glib_none().0,
382 key.to_glib_none().0,
383 length.as_mut_ptr(),
384 &mut error,
385 );
386 if error.is_null() {
387 Ok(FromGlibContainer::from_glib_container_num(
388 ret,
389 length.assume_init() as _,
390 ))
391 } else {
392 Err(from_glib_full(error))
393 }
394 }
395 }
396
397 /// Returns the actual locale which the result of
398 /// [`locale_string()`][Self::locale_string()] or
399 /// [`locale_string_list()`][Self::locale_string_list()] came from.
400 ///
401 /// If calling [`locale_string()`][Self::locale_string()] or
402 /// [`locale_string_list()`][Self::locale_string_list()] with exactly the same @self,
403 /// @group_name, @key and @locale, the result of those functions will
404 /// have originally been tagged with the locale that is the result of
405 /// this function.
406 /// ## `group_name`
407 /// a group name
408 /// ## `key`
409 /// a key
410 /// ## `locale`
411 /// a locale identifier or `NULL` to use the current locale
412 ///
413 /// # Returns
414 ///
415 /// the locale from the file, or `NULL` if the key was not
416 /// found or the entry in the file was was untranslated
417 #[doc(alias = "g_key_file_get_locale_for_key")]
418 #[doc(alias = "get_locale_for_key")]
419 pub fn locale_for_key(
420 &self,
421 group_name: &str,
422 key: &str,
423 locale: Option<&str>,
424 ) -> Option<crate::GString> {
425 unsafe {
426 from_glib_full(ffi::g_key_file_get_locale_for_key(
427 self.to_glib_none().0,
428 group_name.to_glib_none().0,
429 key.to_glib_none().0,
430 locale.to_glib_none().0,
431 ))
432 }
433 }
434
435 /// Returns the name of the start group of the file.
436 ///
437 /// # Returns
438 ///
439 /// The start group of the key file.
440 #[doc(alias = "g_key_file_get_start_group")]
441 #[doc(alias = "get_start_group")]
442 pub fn start_group(&self) -> Option<crate::GString> {
443 unsafe { from_glib_full(ffi::g_key_file_get_start_group(self.to_glib_none().0)) }
444 }
445
446 /// Returns the value associated with @key under @group_name as an unsigned
447 /// 64-bit integer.
448 ///
449 /// This is similar to [`integer()`][Self::integer()] but can return
450 /// large positive results without truncation.
451 /// ## `group_name`
452 /// a group name
453 /// ## `key`
454 /// a key
455 ///
456 /// # Returns
457 ///
458 /// the value associated with the key as an unsigned 64-bit integer,
459 /// or `0` if the key was not found or could not be parsed.
460 #[doc(alias = "g_key_file_get_uint64")]
461 #[doc(alias = "get_uint64")]
462 pub fn uint64(&self, group_name: &str, key: &str) -> Result<u64, crate::Error> {
463 unsafe {
464 let mut error = std::ptr::null_mut();
465 let ret = ffi::g_key_file_get_uint64(
466 self.to_glib_none().0,
467 group_name.to_glib_none().0,
468 key.to_glib_none().0,
469 &mut error,
470 );
471 if error.is_null() {
472 Ok(ret)
473 } else {
474 Err(from_glib_full(error))
475 }
476 }
477 }
478
479 /// Returns the raw value associated with @key under @group_name.
480 ///
481 /// Use [`string()`][Self::string()] to retrieve an unescaped UTF-8 string.
482 ///
483 /// If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND]
484 /// is returned. If the @group_name cannot be found,
485 /// [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.
486 /// ## `group_name`
487 /// a group name
488 /// ## `key`
489 /// a key
490 ///
491 /// # Returns
492 ///
493 /// a newly allocated string or `NULL` if the specified
494 /// key cannot be found.
495 #[doc(alias = "g_key_file_get_value")]
496 #[doc(alias = "get_value")]
497 pub fn value(&self, group_name: &str, key: &str) -> Result<crate::GString, crate::Error> {
498 unsafe {
499 let mut error = std::ptr::null_mut();
500 let ret = ffi::g_key_file_get_value(
501 self.to_glib_none().0,
502 group_name.to_glib_none().0,
503 key.to_glib_none().0,
504 &mut error,
505 );
506 if error.is_null() {
507 Ok(from_glib_full(ret))
508 } else {
509 Err(from_glib_full(error))
510 }
511 }
512 }
513
514 /// Looks whether the key file has the group @group_name.
515 /// ## `group_name`
516 /// a group name
517 ///
518 /// # Returns
519 ///
520 /// true if @group_name is a part of @self, false otherwise.
521 #[doc(alias = "g_key_file_has_group")]
522 pub fn has_group(&self, group_name: &str) -> bool {
523 unsafe {
524 from_glib(ffi::g_key_file_has_group(
525 self.to_glib_none().0,
526 group_name.to_glib_none().0,
527 ))
528 }
529 }
530
531 /// Loads a key file from the data in @bytes into an empty [`KeyFile`][crate::KeyFile]
532 /// structure.
533 ///
534 /// If the object cannot be created then a [`KeyFileError`][crate::KeyFileError] is returned.
535 /// ## `bytes`
536 /// a [`Bytes`][crate::Bytes]
537 /// ## `flags`
538 /// flags from [`KeyFileFlags`][crate::KeyFileFlags]
539 ///
540 /// # Returns
541 ///
542 /// true if a key file could be loaded, false otherwise
543 #[doc(alias = "g_key_file_load_from_bytes")]
544 pub fn load_from_bytes(&self, bytes: &Bytes, flags: KeyFileFlags) -> Result<(), crate::Error> {
545 unsafe {
546 let mut error = std::ptr::null_mut();
547 let is_ok = ffi::g_key_file_load_from_bytes(
548 self.to_glib_none().0,
549 bytes.to_glib_none().0,
550 flags.into_glib(),
551 &mut error,
552 );
553 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
554 if error.is_null() {
555 Ok(())
556 } else {
557 Err(from_glib_full(error))
558 }
559 }
560 }
561
562 /// Loads a key file from memory into an empty [`KeyFile`][crate::KeyFile] structure.
563 ///
564 /// If the object cannot be created then a [`KeyFileError`][crate::KeyFileError] is returned.
565 /// ## `data`
566 /// key file loaded in memory
567 /// ## `length`
568 /// the length of @data in bytes (or `(gsize)-1` if data is nul-terminated)
569 /// ## `flags`
570 /// flags from [`KeyFileFlags`][crate::KeyFileFlags]
571 ///
572 /// # Returns
573 ///
574 /// true if a key file could be loaded, false otherwise
575 #[doc(alias = "g_key_file_load_from_data")]
576 pub fn load_from_data(&self, data: &str, flags: KeyFileFlags) -> Result<(), crate::Error> {
577 let length = data.len() as _;
578 unsafe {
579 let mut error = std::ptr::null_mut();
580 let is_ok = ffi::g_key_file_load_from_data(
581 self.to_glib_none().0,
582 data.to_glib_none().0,
583 length,
584 flags.into_glib(),
585 &mut error,
586 );
587 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
588 if error.is_null() {
589 Ok(())
590 } else {
591 Err(from_glib_full(error))
592 }
593 }
594 }
595
596 /// Loads a key file into an empty [`KeyFile`][crate::KeyFile] structure.
597 ///
598 /// If the OS returns an error when opening or reading the file, a
599 /// [`FileError`][crate::FileError] is returned. If there is a problem parsing the file,
600 /// a [`KeyFileError`][crate::KeyFileError] is returned.
601 ///
602 /// This function will never return a [error@GLib.KeyFileError.NOT_FOUND]
603 /// error. If the @file is not found, [error@GLib.FileError.NOENT] is returned.
604 /// ## `file`
605 /// the path of a filename to load, in the GLib filename encoding
606 /// ## `flags`
607 /// flags from [`KeyFileFlags`][crate::KeyFileFlags]
608 ///
609 /// # Returns
610 ///
611 /// true if a key file could be loaded, false otherwise
612 #[doc(alias = "g_key_file_load_from_file")]
613 pub fn load_from_file(
614 &self,
615 file: impl AsRef<std::path::Path>,
616 flags: KeyFileFlags,
617 ) -> Result<(), crate::Error> {
618 unsafe {
619 let mut error = std::ptr::null_mut();
620 let is_ok = ffi::g_key_file_load_from_file(
621 self.to_glib_none().0,
622 file.as_ref().to_glib_none().0,
623 flags.into_glib(),
624 &mut error,
625 );
626 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
627 if error.is_null() {
628 Ok(())
629 } else {
630 Err(from_glib_full(error))
631 }
632 }
633 }
634
635 /// Removes a comment above @key from @group_name.
636 ///
637 /// If @key is `NULL` then @comment will be removed above @group_name.
638 /// If both @key and @group_name are `NULL`, then @comment will
639 /// be removed above the first group in the file.
640 /// ## `group_name`
641 /// a group name, or `NULL` to get a top-level comment
642 /// ## `key`
643 /// a key, or `NULL` to get a group comment
644 ///
645 /// # Returns
646 ///
647 /// true if the comment was removed, false otherwise
648 #[doc(alias = "g_key_file_remove_comment")]
649 pub fn remove_comment(
650 &self,
651 group_name: Option<&str>,
652 key: Option<&str>,
653 ) -> Result<(), crate::Error> {
654 unsafe {
655 let mut error = std::ptr::null_mut();
656 let is_ok = ffi::g_key_file_remove_comment(
657 self.to_glib_none().0,
658 group_name.to_glib_none().0,
659 key.to_glib_none().0,
660 &mut error,
661 );
662 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
663 if error.is_null() {
664 Ok(())
665 } else {
666 Err(from_glib_full(error))
667 }
668 }
669 }
670
671 /// Removes the specified group, @group_name,
672 /// from the key file.
673 /// ## `group_name`
674 /// a group name
675 ///
676 /// # Returns
677 ///
678 /// true if the group was removed, false otherwise
679 #[doc(alias = "g_key_file_remove_group")]
680 pub fn remove_group(&self, group_name: &str) -> Result<(), crate::Error> {
681 unsafe {
682 let mut error = std::ptr::null_mut();
683 let is_ok = ffi::g_key_file_remove_group(
684 self.to_glib_none().0,
685 group_name.to_glib_none().0,
686 &mut error,
687 );
688 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
689 if error.is_null() {
690 Ok(())
691 } else {
692 Err(from_glib_full(error))
693 }
694 }
695 }
696
697 /// Removes @key in @group_name from the key file.
698 /// ## `group_name`
699 /// a group name
700 /// ## `key`
701 /// a key name to remove
702 ///
703 /// # Returns
704 ///
705 /// true if the key was removed, false otherwise
706 #[doc(alias = "g_key_file_remove_key")]
707 pub fn remove_key(&self, group_name: &str, key: &str) -> Result<(), crate::Error> {
708 unsafe {
709 let mut error = std::ptr::null_mut();
710 let is_ok = ffi::g_key_file_remove_key(
711 self.to_glib_none().0,
712 group_name.to_glib_none().0,
713 key.to_glib_none().0,
714 &mut error,
715 );
716 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
717 if error.is_null() {
718 Ok(())
719 } else {
720 Err(from_glib_full(error))
721 }
722 }
723 }
724
725 /// Associates a new boolean value with @key under @group_name.
726 ///
727 /// If @key cannot be found then it is created.
728 /// ## `group_name`
729 /// a group name
730 /// ## `key`
731 /// a key
732 /// ## `value`
733 /// true or false
734 #[doc(alias = "g_key_file_set_boolean")]
735 pub fn set_boolean(&self, group_name: &str, key: &str, value: bool) {
736 unsafe {
737 ffi::g_key_file_set_boolean(
738 self.to_glib_none().0,
739 group_name.to_glib_none().0,
740 key.to_glib_none().0,
741 value.into_glib(),
742 );
743 }
744 }
745
746 //#[doc(alias = "g_key_file_set_boolean_list")]
747 //pub fn set_boolean_list(&self, group_name: &str, key: &str, list: /*Unimplemented*/&CArray TypeId { ns_id: 0, id: 1 }) {
748 // unsafe { TODO: call ffi:g_key_file_set_boolean_list() }
749 //}
750
751 /// Places a comment above @key from @group_name.
752 ///
753 /// If @key is `NULL` then @comment will be written above @group_name.
754 /// If both @key and @group_name are `NULL`, then @comment will be
755 /// written above the first group in the file.
756 ///
757 /// Note that this function prepends a `#` comment marker to
758 /// each line of @comment.
759 /// ## `group_name`
760 /// a group name, or `NULL` to write a top-level comment
761 /// ## `key`
762 /// a key, or `NULL` to write a group comment
763 /// ## `comment`
764 /// a comment
765 ///
766 /// # Returns
767 ///
768 /// true if the comment was written, false otherwise
769 #[doc(alias = "g_key_file_set_comment")]
770 pub fn set_comment(
771 &self,
772 group_name: Option<&str>,
773 key: Option<&str>,
774 comment: &str,
775 ) -> Result<(), crate::Error> {
776 unsafe {
777 let mut error = std::ptr::null_mut();
778 let is_ok = ffi::g_key_file_set_comment(
779 self.to_glib_none().0,
780 group_name.to_glib_none().0,
781 key.to_glib_none().0,
782 comment.to_glib_none().0,
783 &mut error,
784 );
785 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
786 if error.is_null() {
787 Ok(())
788 } else {
789 Err(from_glib_full(error))
790 }
791 }
792 }
793
794 /// Associates a new double value with @key under @group_name.
795 ///
796 /// If @key cannot be found then it is created.
797 /// ## `group_name`
798 /// a group name
799 /// ## `key`
800 /// a key
801 /// ## `value`
802 /// a double value
803 #[doc(alias = "g_key_file_set_double")]
804 pub fn set_double(&self, group_name: &str, key: &str, value: f64) {
805 unsafe {
806 ffi::g_key_file_set_double(
807 self.to_glib_none().0,
808 group_name.to_glib_none().0,
809 key.to_glib_none().0,
810 value,
811 );
812 }
813 }
814
815 /// Associates a new integer value with @key under @group_name.
816 ///
817 /// If @key cannot be found then it is created.
818 /// ## `group_name`
819 /// a group name
820 /// ## `key`
821 /// a key
822 /// ## `value`
823 /// an integer value
824 #[doc(alias = "g_key_file_set_int64")]
825 pub fn set_int64(&self, group_name: &str, key: &str, value: i64) {
826 unsafe {
827 ffi::g_key_file_set_int64(
828 self.to_glib_none().0,
829 group_name.to_glib_none().0,
830 key.to_glib_none().0,
831 value,
832 );
833 }
834 }
835
836 /// Associates a new integer value with @key under @group_name.
837 ///
838 /// If @key cannot be found then it is created.
839 /// ## `group_name`
840 /// a group name
841 /// ## `key`
842 /// a key
843 /// ## `value`
844 /// an integer value
845 #[doc(alias = "g_key_file_set_integer")]
846 pub fn set_integer(&self, group_name: &str, key: &str, value: i32) {
847 unsafe {
848 ffi::g_key_file_set_integer(
849 self.to_glib_none().0,
850 group_name.to_glib_none().0,
851 key.to_glib_none().0,
852 value,
853 );
854 }
855 }
856
857 /// Sets the character which is used to separate values in lists.
858 ///
859 /// Typically `;` or `,` are used as separators. The default list separator
860 /// is `;`.
861 /// ## `separator`
862 /// the separator
863 #[doc(alias = "g_key_file_set_list_separator")]
864 pub fn set_list_separator(&self, separator: crate::Char) {
865 unsafe {
866 ffi::g_key_file_set_list_separator(self.to_glib_none().0, separator.into_glib());
867 }
868 }
869
870 /// Associates a string value for @key and @locale under @group_name.
871 ///
872 /// If the translation for @key cannot be found then it is created.
873 ///
874 /// If @locale is `C` then the untranslated value is set (since GLib 2.84).
875 /// ## `group_name`
876 /// a group name
877 /// ## `key`
878 /// a key
879 /// ## `locale`
880 /// a locale identifier
881 /// ## `string`
882 /// a string
883 #[doc(alias = "g_key_file_set_locale_string")]
884 pub fn set_locale_string(&self, group_name: &str, key: &str, locale: &str, string: &str) {
885 unsafe {
886 ffi::g_key_file_set_locale_string(
887 self.to_glib_none().0,
888 group_name.to_glib_none().0,
889 key.to_glib_none().0,
890 locale.to_glib_none().0,
891 string.to_glib_none().0,
892 );
893 }
894 }
895
896 /// Associates a new string value with @key under @group_name.
897 ///
898 /// If @key cannot be found then it is created.
899 /// If @group_name cannot be found then it is created.
900 /// Unlike [`set_value()`][Self::set_value()], this function handles characters
901 /// that need escaping, such as newlines.
902 /// ## `group_name`
903 /// a group name
904 /// ## `key`
905 /// a key
906 /// ## `string`
907 /// a string
908 #[doc(alias = "g_key_file_set_string")]
909 pub fn set_string(&self, group_name: &str, key: &str, string: &str) {
910 unsafe {
911 ffi::g_key_file_set_string(
912 self.to_glib_none().0,
913 group_name.to_glib_none().0,
914 key.to_glib_none().0,
915 string.to_glib_none().0,
916 );
917 }
918 }
919
920 /// Associates a new integer value with @key under @group_name.
921 ///
922 /// If @key cannot be found then it is created.
923 /// ## `group_name`
924 /// a group name
925 /// ## `key`
926 /// a key
927 /// ## `value`
928 /// an integer value
929 #[doc(alias = "g_key_file_set_uint64")]
930 pub fn set_uint64(&self, group_name: &str, key: &str, value: u64) {
931 unsafe {
932 ffi::g_key_file_set_uint64(
933 self.to_glib_none().0,
934 group_name.to_glib_none().0,
935 key.to_glib_none().0,
936 value,
937 );
938 }
939 }
940
941 /// Associates a new value with @key under @group_name.
942 ///
943 /// If @key cannot be found then it is created. If @group_name cannot
944 /// be found then it is created. To set an UTF-8 string which may contain
945 /// characters that need escaping (such as newlines or spaces), use
946 /// [`set_string()`][Self::set_string()].
947 /// ## `group_name`
948 /// a group name
949 /// ## `key`
950 /// a key
951 /// ## `value`
952 /// a string
953 #[doc(alias = "g_key_file_set_value")]
954 pub fn set_value(&self, group_name: &str, key: &str, value: &str) {
955 unsafe {
956 ffi::g_key_file_set_value(
957 self.to_glib_none().0,
958 group_name.to_glib_none().0,
959 key.to_glib_none().0,
960 value.to_glib_none().0,
961 );
962 }
963 }
964}
965
966impl Default for KeyFile {
967 fn default() -> Self {
968 Self::new()
969 }
970}