system_configuration/
preferences.rs

1// Copyright 2017 Amagicom AB.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Bindings to [`SCPreferences`].
10//!
11//! See the examples directory for examples how to use this module.
12//!
13//! [`SCPreferences`]: https://developer.apple.com/documentation/systemconfiguration/scpreferences-ft8
14
15use crate::sys::preferences::{SCPreferencesCreate, SCPreferencesGetTypeID, SCPreferencesRef};
16use core_foundation::base::{CFAllocator, TCFType};
17use core_foundation::string::CFString;
18use std::ptr;
19
20declare_TCFType! {
21    /// The handle to an open preferences session for accessing system configuration preferences.
22    SCPreferences, SCPreferencesRef
23}
24
25impl_TCFType!(SCPreferences, SCPreferencesRef, SCPreferencesGetTypeID);
26
27impl SCPreferences {
28    /// Initiates access to the default system preferences using the default allocator.
29    pub fn default(calling_process_name: &CFString) -> Self {
30        Self::new(None, calling_process_name, None)
31    }
32
33    /// Initiates access to the given (`prefs_id`) group of configuration preferences using the
34    /// default allocator. To access the default system preferences, use the [`default`]
35    /// constructor.
36    ///
37    /// [`default`]: #method.default
38    pub fn group(calling_process_name: &CFString, prefs_id: &CFString) -> Self {
39        Self::new(None, calling_process_name, Some(prefs_id))
40    }
41
42    /// Initiates access to the per-system set of configuration preferences with a given
43    /// allocator and preference group to access. See the underlying [SCPreferencesCreate] function
44    /// documentation for details. Use the helper constructors [`default`] and [`group`] to easier
45    /// create an instance using the default allocator.
46    ///
47    /// [SCPreferencesCreate]: https://developer.apple.com/documentation/systemconfiguration/1516807-scpreferencescreate?language=objc
48    /// [`default`]: #method.default
49    /// [`group`]: #method.group
50    pub fn new(
51        allocator: Option<&CFAllocator>,
52        calling_process_name: &CFString,
53        prefs_id: Option<&CFString>,
54    ) -> Self {
55        let allocator_ref = match allocator {
56            Some(allocator) => allocator.as_concrete_TypeRef(),
57            None => ptr::null(),
58        };
59        let prefs_id_ref = match prefs_id {
60            Some(prefs_id) => prefs_id.as_concrete_TypeRef(),
61            None => ptr::null(),
62        };
63
64        unsafe {
65            SCPreferences::wrap_under_create_rule(SCPreferencesCreate(
66                allocator_ref,
67                calling_process_name.as_concrete_TypeRef(),
68                prefs_id_ref,
69            ))
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn retain_count() {
80        let preferences = SCPreferences::default(&CFString::new("test"));
81        assert_eq!(preferences.retain_count(), 1);
82    }
83}