-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathFSPreferenceSwitchDataSource.m
93 lines (80 loc) · 3.06 KB
/
FSPreferenceSwitchDataSource.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
#import "FSPreferenceSwitchDataSource.h"
#import "FSSwitchPanel.h"
#import <notify.h>
@implementation FSPreferenceSwitchDataSource
- (id)initWithBundle:(NSBundle *)_bundle
{
if ((self = [super init])) {
bundle = [_bundle retain];
}
return self;
}
- (void)dealloc
{
[bundle release];
[switchIdentifier_ release];
[super dealloc];
}
- (NSBundle *)bundleForSwitchIdentifier:(NSString *)switchIdentifier
{
return bundle;
}
- (FSSwitchState)stateForSwitchIdentifier:(NSString *)switchIdentifier
{
NSString *key = [bundle objectForInfoDictionaryKey:@"key"];
NSString *defaults = [bundle objectForInfoDictionaryKey:@"defaults"] ?: bundle.bundleIdentifier;
CFPropertyListRef value = CFPreferencesCopyAppValue((CFStringRef)key, (CFStringRef)defaults);
BOOL result = [(id)value ?: [bundle objectForInfoDictionaryKey:@"default"] boolValue];
if (value)
CFRelease(value);
if ([[bundle objectForInfoDictionaryKey:@"negate"] boolValue])
result = !result;
return result;
}
- (void)applyState:(FSSwitchState)newState forSwitchIdentifier:(NSString *)switchIdentifier
{
if (newState == FSSwitchStateIndeterminate)
return;
NSString *key = [bundle objectForInfoDictionaryKey:@"key"];
NSString *defaults = [bundle objectForInfoDictionaryKey:@"defaults"] ?: bundle.bundleIdentifier;
if ([[bundle objectForInfoDictionaryKey:@"negate"] boolValue])
newState = !newState;
CFPreferencesSetAppValue((CFStringRef)key, newState ? kCFBooleanTrue : kCFBooleanFalse, (CFStringRef)defaults);
CFPreferencesAppSynchronize((CFStringRef)defaults);
NSString *notification = [bundle objectForInfoDictionaryKey:@"PostNotification"];
if (notification) {
notify_post([notification UTF8String]);
}
}
- (void)_preferenceChanged
{
NSString *defaults = [bundle objectForInfoDictionaryKey:@"defaults"] ?: bundle.bundleIdentifier;
if (defaults) {
CFPreferencesAppSynchronize((CFStringRef)defaults);
}
[[FSSwitchPanel sharedPanel] stateDidChangeForSwitchIdentifier: switchIdentifier_];
}
static void FSPreferenceSwitchDataSourceChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
FSPreferenceSwitchDataSource *self = observer;
[self _preferenceChanged];
}
- (void)switchWasRegisteredForIdentifier:(NSString *)switchIdentifier
{
[switchIdentifier_ release];
switchIdentifier_ = [switchIdentifier copy];
NSString *notification = [bundle objectForInfoDictionaryKey:@"PostNotification"];
if ([notification isKindOfClass:[NSString class]]) {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), self, FSPreferenceSwitchDataSourceChangedCallback, (CFStringRef)notification, NULL, CFNotificationSuspensionBehaviorCoalesce);
}
}
- (void)switchWasUnregisteredForIdentifier:(NSString *)switchIdentifier
{
NSString *notification = [bundle objectForInfoDictionaryKey:@"PostNotification"];
if ([notification isKindOfClass:[NSString class]]) {
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), self, (CFStringRef)notification, NULL);
}
[switchIdentifier_ release];
switchIdentifier_ = nil;
}
@end