3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "BonjourSCStore.h"
19 #import <Foundation/Foundation.h>
20 #import <AssertMacros.h>
22 @implementation BonjourSCStore
24 + (NSArray * _Nullable)objectForKey:(NSString * _Nonnull)key
26 NSArray * result = nil;
27 SCPreferencesRef store;
29 NSDictionary * origDict;
31 store = SCPreferencesCreateWithAuthorization(kCFAllocatorDefault, SC_DYNDNS_PREFS_KEY, NULL, NULL);
32 require_action(store != NULL, SysConfigErr, err = SCError());
33 require_action(true == SCPreferencesLock(store, true), LockFailed, err = SCError());
35 origDict = (__bridge NSDictionary *)SCPreferencesPathGetValue(store, SC_DYNDNS_SYSTEM_KEY);
36 if (origDict) origDict = [NSDictionary dictionaryWithDictionary: origDict];
38 result = [origDict objectForKey: key];
40 SCPreferencesUnlock(store);
48 + (void)setObject:(NSArray * _Nullable)value forKey:(NSString * _Nonnull)key
50 SCPreferencesRef store;
52 NSMutableDictionary * origDict;
55 store = SCPreferencesCreateWithAuthorization(kCFAllocatorDefault, SC_DYNDNS_PREFS_KEY, NULL, NULL);
56 require_action(store != NULL, SysConfigErr, err = SCError());
57 require_action(true == SCPreferencesLock(store, true), LockFailed, err = SCError());
59 origDict = (__bridge NSMutableDictionary *)SCPreferencesPathGetValue(store, SC_DYNDNS_SYSTEM_KEY);
60 if (!origDict) origDict = [NSMutableDictionary dictionary];
61 else origDict = [NSMutableDictionary dictionaryWithDictionary: origDict];
63 if (value.count) [origDict setObject: value forKey: key];
64 else [origDict removeObjectForKey: key];
66 success = SCPreferencesPathSetValue(store, SC_DYNDNS_SYSTEM_KEY, (__bridge CFDictionaryRef)origDict);
67 require_action(success, SCError, err = SCError(););
69 success = SCPreferencesCommitChanges(store);
70 require_action(success, SCError, err = SCError());
71 success = SCPreferencesApplyChanges(store);
72 require_action(success, SCError, err = SCError());
75 SCPreferencesUnlock(store);