1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::error::Error;
use crate::gstring::GString;
use crate::translate::*;
use crate::KeyFileFlags;
use std::mem;
use std::path;
use std::ptr;

use crate::KeyFile;

impl KeyFile {
    /// Writes the contents of `self` to `filename` using
    /// [`file_set_contents()`][crate::file_set_contents()]. If you need stricter guarantees about durability of
    /// the written file than are provided by [`file_set_contents()`][crate::file_set_contents()], use
    /// [`file_set_contents_full()`][crate::file_set_contents_full()] with the return value of [`to_data()`][Self::to_data()].
    ///
    /// This function can fail for any of the reasons that
    /// [`file_set_contents()`][crate::file_set_contents()] may fail.
    /// ## `filename`
    /// the name of the file to write to
    ///
    /// # Returns
    ///
    /// [`true`] if successful, else [`false`] with `error` set
    #[doc(alias = "g_key_file_save_to_file")]
    pub fn save_to_file<T: AsRef<std::path::Path>>(&self, filename: T) -> Result<(), Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ffi::g_key_file_save_to_file(
                self.to_glib_none().0,
                filename.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// This function looks for a key file named `file` in the paths
    /// returned from [`user_data_dir()`][crate::user_data_dir()] and [`system_data_dirs()`][crate::system_data_dirs()],
    /// loads the file into `self` and returns the file's full path in
    /// `full_path`. If the file could not be loaded then an `error` is
    /// set to either a `GFileError` or [`KeyFileError`][crate::KeyFileError].
    /// ## `file`
    /// a relative path to a filename to open and parse
    /// ## `flags`
    /// flags from [`KeyFileFlags`][crate::KeyFileFlags]
    ///
    /// # Returns
    ///
    /// [`true`] if a key file could be loaded, [`false`] otherwise
    ///
    /// ## `full_path`
    /// return location for a string containing the full path
    ///  of the file, or [`None`]
    #[doc(alias = "g_key_file_load_from_data_dirs")]
    pub fn load_from_data_dirs<T: AsRef<std::path::Path>>(
        &self,
        file: T,
        flags: KeyFileFlags,
    ) -> Result<path::PathBuf, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let mut full_path: *mut libc::c_char = ptr::null_mut();
            let _ = ffi::g_key_file_load_from_data_dirs(
                self.to_glib_none().0,
                file.as_ref().to_glib_none().0,
                &mut full_path,
                flags.into_glib(),
                &mut error,
            );
            if error.is_null() {
                let path: GString = from_glib_full(full_path);
                Ok(path::PathBuf::from(&path))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// This function looks for a key file named `file` in the paths
    /// specified in `search_dirs`, loads the file into `self` and
    /// returns the file's full path in `full_path`.
    ///
    /// If the file could not be found in any of the `search_dirs`,
    /// [`KeyFileError::NotFound`][crate::KeyFileError::NotFound] is returned. If
    /// the file is found but the OS returns an error when opening or reading the
    /// file, a `G_FILE_ERROR` is returned. If there is a problem parsing the file, a
    /// `G_KEY_FILE_ERROR` is returned.
    /// ## `file`
    /// a relative path to a filename to open and parse
    /// ## `search_dirs`
    /// [`None`]-terminated array of directories to search
    /// ## `flags`
    /// flags from [`KeyFileFlags`][crate::KeyFileFlags]
    ///
    /// # Returns
    ///
    /// [`true`] if a key file could be loaded, [`false`] otherwise
    ///
    /// ## `full_path`
    /// return location for a string containing the full path
    ///  of the file, or [`None`]
    #[doc(alias = "g_key_file_load_from_dirs")]
    pub fn 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> {
        unsafe {
            let search_dirs: Vec<&std::path::Path> =
                search_dirs.iter().map(AsRef::as_ref).collect();
            let mut error = ptr::null_mut();
            let mut full_path: *mut libc::c_char = ptr::null_mut();
            let _ = ffi::g_key_file_load_from_dirs(
                self.to_glib_none().0,
                file.as_ref().to_glib_none().0,
                search_dirs.to_glib_none().0,
                &mut full_path,
                flags.into_glib(),
                &mut error,
            );
            if error.is_null() {
                let path: GString = from_glib_full(full_path);
                Ok(path::PathBuf::from(&path))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// This function outputs `self` as a string.
    ///
    /// Note that this function never reports an error,
    /// so it is safe to pass [`None`] as `error`.
    ///
    /// # Returns
    ///
    /// a newly allocated string holding
    ///  the contents of the [`KeyFile`][crate::KeyFile]
    ///
    /// ## `length`
    /// return location for the length of the
    ///  returned string, or [`None`]
    #[doc(alias = "g_key_file_to_data")]
    pub fn to_data(&self) -> GString {
        unsafe {
            let ret =
                ffi::g_key_file_to_data(self.to_glib_none().0, ptr::null_mut(), ptr::null_mut());
            from_glib_full(ret)
        }
    }

    /// Returns the value associated with `key` under `group_name` as a
    /// boolean.
    ///
    /// If `key` cannot be found then [`false`] is returned and `error` is set
    /// to [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. Likewise, if the value
    /// associated with `key` cannot be interpreted as a boolean then [`false`]
    /// is returned and `error` is set to [`KeyFileError::InvalidValue`][crate::KeyFileError::InvalidValue].
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    ///
    /// # Returns
    ///
    /// the value associated with the key as a boolean,
    ///  or [`false`] if the key was not found or could not be parsed.
    #[doc(alias = "g_key_file_get_boolean")]
    #[doc(alias = "get_boolean")]
    pub fn boolean(&self, group_name: &str, key: &str) -> Result<bool, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_boolean(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Looks whether the key file has the key `key` in the group
    /// `group_name`.
    ///
    /// Note that this function does not follow the rules for [`Error`][crate::Error] strictly;
    /// the return value both carries meaning and signals an error. To use
    /// this function, you must pass a [`Error`][crate::Error] pointer in `error`, and check
    /// whether it is not [`None`] to see if an error occurred.
    ///
    /// Language bindings should use [`value()`][Self::value()] to test whether
    /// or not a key exists.
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key name
    ///
    /// # Returns
    ///
    /// [`true`] if `key` is a part of `group_name`, [`false`] otherwise
    #[doc(alias = "g_key_file_has_key")]
    pub fn has_key(&self, group_name: &str, key: &str) -> Result<bool, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_has_key(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Returns the values associated with `key` under `group_name` as
    /// booleans.
    ///
    /// If `key` cannot be found then [`None`] is returned and `error` is set to
    /// [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. Likewise, if the values associated
    /// with `key` cannot be interpreted as booleans then [`None`] is returned
    /// and `error` is set to [`KeyFileError::InvalidValue`][crate::KeyFileError::InvalidValue].
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    ///
    /// # Returns
    ///
    ///
    ///  the values associated with the key as a list of booleans, or [`None`] if the
    ///  key was not found or could not be parsed. The returned list of booleans
    ///  should be freed with `g_free()` when no longer needed.
    #[doc(alias = "g_key_file_get_boolean_list")]
    #[doc(alias = "get_boolean_list")]
    pub fn boolean_list(&self, group_name: &str, key: &str) -> Result<Vec<bool>, Error> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_boolean_list(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                length.as_mut_ptr(),
                &mut error,
            );
            if !error.is_null() {
                return Err(from_glib_full(error));
            }
            Ok(FromGlibContainer::from_glib_container_num(
                ret,
                length.assume_init() as usize,
            ))
        }
    }

    /// Returns the string value associated with `key` under `group_name`.
    /// Unlike [`value()`][Self::value()], this function handles escape sequences
    /// like \s.
    ///
    /// In the event the key cannot be found, [`None`] is returned and
    /// `error` is set to [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. In the
    /// event that the `group_name` cannot be found, [`None`] is returned
    /// and `error` is set to [`KeyFileError::GroupNotFound`][crate::KeyFileError::GroupNotFound].
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    ///
    /// # Returns
    ///
    /// a newly allocated string or [`None`] if the specified
    ///  key cannot be found.
    #[doc(alias = "g_key_file_get_string")]
    #[doc(alias = "get_string")]
    pub fn string(&self, group_name: &str, key: &str) -> Result<GString, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_string(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                if !ret.is_null() {
                    ffi::g_free(ret as *mut _);
                }
                Err(from_glib_full(error))
            }
        }
    }

    /// Returns the values associated with `key` under `group_name`.
    ///
    /// In the event the key cannot be found, [`None`] is returned and
    /// `error` is set to [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. In the
    /// event that the `group_name` cannot be found, [`None`] is returned
    /// and `error` is set to [`KeyFileError::GroupNotFound`][crate::KeyFileError::GroupNotFound].
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    ///
    /// # Returns
    ///
    ///
    ///  a [`None`]-terminated string array or [`None`] if the specified
    ///  key cannot be found. The array should be freed with `g_strfreev()`.
    #[doc(alias = "g_key_file_get_string_list")]
    #[doc(alias = "get_string_list")]
    pub fn string_list(&self, group_name: &str, key: &str) -> Result<Vec<GString>, Error> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_string_list(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                length.as_mut_ptr(),
                &mut error,
            );
            if error.is_null() {
                Ok(FromGlibContainer::from_glib_full_num(
                    ret,
                    length.assume_init() as usize,
                ))
            } else {
                if !ret.is_null() {
                    ffi::g_strfreev(ret);
                }
                Err(from_glib_full(error))
            }
        }
    }

    /// Returns the value associated with `key` under `group_name`
    /// translated in the given `locale` if available. If `locale` is
    /// [`None`] then the current locale is assumed.
    ///
    /// If `locale` is to be non-[`None`], or if the current locale will change over
    /// the lifetime of the [`KeyFile`][crate::KeyFile], it must be loaded with
    /// [`KeyFileFlags::KEEP_TRANSLATIONS`][crate::KeyFileFlags::KEEP_TRANSLATIONS] in order to load strings for all locales.
    ///
    /// If `key` cannot be found then [`None`] is returned and `error` is set
    /// to [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. If the value associated
    /// with `key` cannot be interpreted or no suitable translation can
    /// be found then the untranslated value is returned.
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    /// ## `locale`
    /// a locale identifier or [`None`]
    ///
    /// # Returns
    ///
    /// a newly allocated string or [`None`] if the specified
    ///  key cannot be found.
    #[doc(alias = "g_key_file_get_locale_string")]
    #[doc(alias = "get_locale_string")]
    pub fn locale_string(
        &self,
        group_name: &str,
        key: &str,
        locale: Option<&str>,
    ) -> Result<GString, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_locale_string(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                locale.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                if !ret.is_null() {
                    ffi::g_free(ret as *mut _);
                }
                Err(from_glib_full(error))
            }
        }
    }

    /// Returns the values associated with `key` under `group_name`
    /// translated in the given `locale` if available. If `locale` is
    /// [`None`] then the current locale is assumed.
    ///
    /// If `locale` is to be non-[`None`], or if the current locale will change over
    /// the lifetime of the [`KeyFile`][crate::KeyFile], it must be loaded with
    /// [`KeyFileFlags::KEEP_TRANSLATIONS`][crate::KeyFileFlags::KEEP_TRANSLATIONS] in order to load strings for all locales.
    ///
    /// If `key` cannot be found then [`None`] is returned and `error` is set
    /// to [`KeyFileError::KeyNotFound`][crate::KeyFileError::KeyNotFound]. If the values associated
    /// with `key` cannot be interpreted or no suitable translations
    /// can be found then the untranslated values are returned. The
    /// returned array is [`None`]-terminated, so `length` may optionally
    /// be [`None`].
    /// ## `group_name`
    /// a group name
    /// ## `key`
    /// a key
    /// ## `locale`
    /// a locale identifier or [`None`]
    ///
    /// # Returns
    ///
    /// a newly allocated [`None`]-terminated string array
    ///  or [`None`] if the key isn't found. The string array should be freed
    ///  with `g_strfreev()`.
    #[doc(alias = "g_key_file_get_locale_string_list")]
    #[doc(alias = "get_locale_string_list")]
    pub fn locale_string_list(
        &self,
        group_name: &str,
        key: &str,
        locale: Option<&str>,
    ) -> Result<Vec<GString>, Error> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let mut error = ptr::null_mut();
            let ret = ffi::g_key_file_get_locale_string_list(
                self.to_glib_none().0,
                group_name.to_glib_none().0,
                key.to_glib_none().0,
                locale.to_glib_none().0,
                length.as_mut_ptr(),
                &mut error,
            );
            if error.is_null() {
                Ok(FromGlibContainer::from_glib_full_num(
                    ret,
                    length.assume_init() as usize,
                ))
            } else {
                if !ret.is_null() {
                    ffi::g_strfreev(ret);
                }
                Err(from_glib_full(error))
            }
        }
    }
}