-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDXPrefsManagerServer.m
100 lines (78 loc) · 3.65 KB
/
DXPrefsManagerServer.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
#import "common.h"
#import "DXPrefsManagerServer.h"
#import "DXPrefsManager.h"
@implementation DXPrefsManagerServer
+ (void)load {
@autoreleasepool {
NSArray *args = [[NSClassFromString(@"NSProcessInfo") processInfo] arguments];
if (args.count != 0) {
NSString *executablePath = args[0];
if (executablePath) {
NSString *processName = [executablePath lastPathComponent];
BOOL isSpringBoard = [processName isEqualToString:@"SpringBoard"];
if (isSpringBoard) {
[self sharedInstance];
}
}
}
}
}
+ (instancetype)sharedInstance {
static dispatch_once_t once = 0;
__strong static id sharedInstance = nil;
dispatch_once(&once, ^{
sharedInstance = [self new];
});
return sharedInstance;
}
- (instancetype)init {
self = [super init];
if (self) {
_messagingCenter = [CPDistributedMessagingCenter centerNamed:kIPCCenterPrefsManager];
rocketbootstrap_distributedmessagingcenter_apply(_messagingCenter);
[_messagingCenter runServerOnCurrentThread];
[_messagingCenter registerForMessageName:@"readPrefs" target:self selector:@selector(readPrefs:withUserInfo:)];
[_messagingCenter registerForMessageName:@"writePrefs" target:self selector:@selector(writePrefs:withUserInfo:)];
[_messagingCenter registerForMessageName:@"setValue" target:self selector:@selector(setValue:withUserInfo:)];
[_messagingCenter registerForMessageName:@"getValueForKey" target:self selector:@selector(getValueForKey:withUserInfo:)];
[_messagingCenter registerForMessageName:@"removeKey" target:self selector:@selector(removeKey:withUserInfo:)];
}
return self;
}
-(NSDictionary *)readPrefs:(NSString *)name withUserInfo:(NSDictionary *)userInfo{
CFStringRef appID = (CFStringRef)kIdentifier;
CFPreferencesAppSynchronize(appID);
CFArrayRef keyList = CFPreferencesCopyKeyList(appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (!keyList) {
return @{};
}
NSDictionary *dictionary = (NSDictionary *)CFBridgingRelease(CFPreferencesCopyMultiple(keyList, appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
CFRelease(keyList);
return dictionary;
}
-(NSDictionary *)writePrefs:(NSString *)name withUserInfo:(NSDictionary *)userInfo{
CFStringRef appID = (CFStringRef)kIdentifier;
CFPreferencesSetMultiple((__bridge CFDictionaryRef)userInfo, nil, appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesAppSynchronize(appID);
//[self postChangedNotification];
return nil;
}
-(NSDictionary *)setValue:(NSString *)name withUserInfo:(NSDictionary *)userInfo{
CFPreferencesSetAppValue((CFStringRef)userInfo[@"key"], (CFPropertyListRef)userInfo[@"value"], (CFStringRef)kIdentifier);
CFPreferencesAppSynchronize((CFStringRef)kIdentifier);
//[self postChangedNotification];
return nil;
}
-(NSDictionary *)getValueForKey:(NSString *)name withUserInfo:(NSDictionary *)userInfo{
CFStringRef appID = (CFStringRef)kIdentifier;
CFPreferencesAppSynchronize(appID);
return @{@"value":CFBridgingRelease(CFPreferencesCopyAppValue((CFStringRef)userInfo[@"key"], appID))};
}
-(NSDictionary *)removeKey:(NSString *)name withUserInfo:(NSDictionary *)userInfo{
[self setValue:nil withUserInfo:userInfo];
return nil;
}
-(void)postChangedNotification{
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)kPrefsChangedIdentifier, NULL, NULL, YES);
}
@end