forked from btrask/EasyCapViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTUserDefaults.m
178 lines (157 loc) · 6.1 KB
/
BTUserDefaults.m
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
/* Copyright (c) 2011, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "BTUserDefaults.h"
// Other Sources
#import "ECVFoundationAdditions.h"
static NSTimeInterval const BTSynchronizationDelay = 5.0;
@implementation BTUserDefaults
#pragma mark -BTUserDefaults
- (id)initWithSuite:(NSString *)suite
{
return [self initWithSuites:[NSArray arrayWithObject:suite]];
}
- (id)initWithSuites:(NSArray *)suites
{
return [self initWithSuites:suites user:(NSString *)kCFPreferencesCurrentUser host:(NSString *)kCFPreferencesAnyHost];
}
- (id)initWithSuites:(NSArray *)suites user:(NSString *)username host:(NSString *)hostname
{
if((self = [super init])) {
_suites = [suites copy];
_host = [hostname copy];
_user = [username copy];
_defaults = [[NSMutableDictionary alloc] init];
[NSApp ECV_addObserver:self selector:@selector(synchronizeNow) name:NSApplicationWillTerminateNotification];
[NSApp ECV_addObserver:self selector:@selector(synchronizeNow) name:NSApplicationDidResignActiveNotification]; // Also works when hiding.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(synchronizeNow) name:NSWorkspaceWillSleepNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(synchronizeNow) name:NSWorkspaceWillPowerOffNotification object:[NSWorkspace sharedWorkspace]];
}
return self;
}
#pragma mark -
- (id)objectForKey:(NSString *)defaultName
{
id val = nil;
for(NSString *const suite in _suites) {
val = [(id)CFPreferencesCopyValue((CFStringRef)defaultName, (CFStringRef)suite, (CFStringRef)_user, (CFStringRef)_host) autorelease];
if(val) break;
}
if(!val) val = [[[_defaults objectForKey:defaultName] copy] autorelease];
return val;
}
- (void)setObject:(id)value forKey:(NSString *)defaultName
{
NSAssert([_suites count], @"BTUserDefaults object must have at least one suite before values can be set");
CFPreferencesSetValue((CFStringRef)defaultName, (CFPropertyListRef)value, (CFStringRef)[_suites objectAtIndex:0], (CFStringRef)_user, (CFStringRef)_host);
[self synchronizeAfterDelay];
}
- (void)removeObjectForKey:(NSString *)defaultName
{
NSAssert([_suites count], @"BTUserDefaults object must have at least one suite before values can be removed");
CFPreferencesSetValue((CFStringRef)defaultName, NULL, (CFStringRef)[_suites objectAtIndex:0], (CFStringRef)_user, (CFStringRef)_host);
[self synchronizeAfterDelay];
}
#pragma mark -
- (NSInteger)integerForKey:(NSString *)defaultName
{
NSNumber *const obj = [self objectForKey:defaultName];
return obj ? [obj integerValue] : 0;
}
- (float)floatForKey:(NSString *)defaultName
{
NSNumber *const obj = [self objectForKey:defaultName];
return obj ? [obj floatValue] : 0.0f;
}
- (double)doubleForKey:(NSString *)defaultName
{
NSNumber *const obj = [self objectForKey:defaultName];
return obj ? [obj doubleValue] : 0.0;
}
- (BOOL)boolForKey:(NSString *)defaultName
{
NSNumber *const obj = [self objectForKey:defaultName];
return obj ? [obj boolValue] : NO;
}
#pragma mark -
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName
{
[self setObject:[NSNumber numberWithInteger:value] forKey:defaultName];
}
- (void)setFloat:(float)value forKey:(NSString *)defaultName
{
[self setObject:[NSNumber numberWithFloat:value] forKey:defaultName];
}
- (void)setDouble:(double)value forKey:(NSString *)defaultName
{
[self setObject:[NSNumber numberWithDouble:value] forKey:defaultName];
}
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName
{
[self setObject:[NSNumber numberWithBool:value] forKey:defaultName];
}
#pragma mark -
- (void)registerDefaults:(NSDictionary *)registrationDictionary
{
[_defaults addEntriesFromDictionary:registrationDictionary];
}
#pragma mark -
- (void)synchronizeAfterDelay
{
if(_syncTimer) return;
if(BTSynchronizationDelay > 0.0) {
[[NSProcessInfo processInfo] ECV_disableSuddenTermination];
_syncTimer = [[NSTimer timerWithTimeInterval:BTSynchronizationDelay target:self selector:@selector(synchronizeNow) userInfo:nil repeats:NO] retain];
[[NSRunLoop mainRunLoop] addTimer:_syncTimer forMode:NSRunLoopCommonModes];
} else {
[self synchronizeNow];
}
}
- (BOOL)synchronizeNow
{
BOOL success = YES;
if(_syncTimer) {
for(NSString *const suite in _suites) {
if(!CFPreferencesSynchronize((CFStringRef)suite, (CFStringRef)_user, (CFStringRef)_host)) success = NO;
}
[[NSProcessInfo processInfo] ECV_enableSuddenTermination];
[_syncTimer invalidate];
[_syncTimer release];
_syncTimer = nil;
if(!success) [self synchronizeAfterDelay];
}
return success;
}
#pragma mark -NSObject
- (id)init
{
return [self initWithSuite:[[NSBundle mainBundle] bundleIdentifier]];
}
- (void)dealloc
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self synchronizeNow];
[_suites release];
[_host release];
[_user release];
[_defaults release];
[super dealloc];
}
@end