glib/
key_file.rs

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