Skip to content

Commit 368db5c

Browse files
committed
Merge branch 'add-preferences-abstraction'
2 parents 1356ce1 + 55477ed commit 368db5c

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

system-configuration/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ extern crate core_foundation;
2424
extern crate system_configuration_sys;
2525

2626
pub mod dynamic_store;
27+
pub mod preferences;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
15+
16+
use core_foundation::base::{CFAllocator, TCFType};
17+
use core_foundation::string::CFString;
18+
19+
pub use system_configuration_sys::preferences::*;
20+
21+
use std::ptr;
22+
23+
24+
declare_TCFType!{
25+
/// The handle to an open preferences session for accessing system configuration preferences.
26+
SCPreferences, SCPreferencesRef
27+
}
28+
29+
impl_TCFType!(SCPreferences, SCPreferencesRef, SCPreferencesGetTypeID);
30+
31+
32+
impl SCPreferences {
33+
/// Initiates access to the default system preferences using the default allocator.
34+
pub fn default(calling_process_name: &CFString) -> Self {
35+
Self::new(None, calling_process_name, None)
36+
}
37+
38+
/// Initiates access to the given (`prefs_id`) group of configuration preferences using the
39+
/// default allocator. To access the default system preferences, use the [`default`]
40+
/// constructor.
41+
///
42+
/// [`default`]: #method.default
43+
pub fn group(calling_process_name: &CFString, prefs_id: &CFString) -> Self {
44+
Self::new(None, calling_process_name, Some(prefs_id))
45+
}
46+
47+
/// Initiates access to the per-system set of configuration preferences with a given
48+
/// allocator and preference group to access. See the underlying [SCPreferencesCreate] function
49+
/// documentation for details. Use the helper constructors [`default`] and [`group`] to easier
50+
/// create an instance using the default allocator.
51+
///
52+
/// [SCPreferencesCreate]: https://developer.apple.com/documentation/systemconfiguration/1516807-scpreferencescreate?language=objc
53+
/// [`default`]: #method.default
54+
/// [`group`]: #method.group
55+
pub fn new(
56+
allocator: Option<&CFAllocator>,
57+
calling_process_name: &CFString,
58+
prefs_id: Option<&CFString>,
59+
) -> Self {
60+
let allocator_ref = match allocator {
61+
Some(allocator) => allocator.as_concrete_TypeRef(),
62+
None => ptr::null(),
63+
};
64+
let prefs_id_ref = match prefs_id {
65+
Some(prefs_id) => prefs_id.as_concrete_TypeRef(),
66+
None => ptr::null(),
67+
};
68+
69+
unsafe {
70+
SCPreferences::wrap_under_create_rule(SCPreferencesCreate(
71+
allocator_ref,
72+
calling_process_name.as_concrete_TypeRef(),
73+
prefs_id_ref,
74+
))
75+
}
76+
}
77+
}
78+
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn retain_count() {
86+
let preferences = SCPreferences::default(&CFString::new("test"));
87+
assert_eq!(preferences.retain_count(), 1);
88+
}
89+
}

0 commit comments

Comments
 (0)