forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquirrelConfig.m
184 lines (159 loc) · 4.75 KB
/
SquirrelConfig.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
179
180
181
182
183
184
#import "SquirrelConfig.h"
#import <rime_api.h>
@implementation SquirrelConfig {
NSMutableDictionary *_cache;
RimeConfig _config;
NSString *_schemaId;
SquirrelConfig *_baseConfig;
BOOL _isOpen;
}
- (instancetype)init {
self = [super init];
if (self) {
_cache = [[NSMutableDictionary alloc] init];
}
return self;
}
- (BOOL)isOpen {
return _isOpen;
}
- (NSString *)schemaId {
return _schemaId;
}
- (BOOL)openBaseConfig {
[self close];
_isOpen = !!rime_get_api()->config_open("squirrel", &_config);
return _isOpen;
}
- (BOOL)openWithSchemaId:(NSString *)schemaId
baseConfig:(SquirrelConfig *)baseConfig {
[self close];
_isOpen = !!rime_get_api()->schema_open(schemaId.UTF8String, &_config);
if (_isOpen) {
_schemaId = schemaId;
_baseConfig = baseConfig;
}
return _isOpen;
}
- (void)close {
if (_isOpen) {
rime_get_api()->config_close(&_config);
_baseConfig = nil;
_isOpen = NO;
}
}
- (BOOL)hasSection:(NSString *)section {
if (_isOpen) {
RimeConfigIterator iterator = {0};
if (rime_get_api()->config_begin_map(&iterator, &_config, section.UTF8String)) {
rime_get_api()->config_end(&iterator);
return YES;
}
}
return NO;
}
- (BOOL)getBool:(NSString *)option {
return [self getOptionalBool:option].boolValue;
}
- (NSInteger)getInt:(NSString *)option {
return [self getOptionalInt:option].integerValue;
}
- (double)getDouble:(NSString *)option {
return [self getOptionalDouble:option].doubleValue;
}
- (NSNumber *)getOptionalBool:(NSString *)option {
NSNumber* cachedValue = [self cachedValueOfClass:[NSNumber class] forKey:option];
if (cachedValue) {
return cachedValue;
}
Bool value;
if (_isOpen && rime_get_api()->config_get_bool(&_config, option.UTF8String, &value)) {
return _cache[option] = @(!!value);
}
return [_baseConfig getOptionalBool:option];
}
- (NSNumber *)getOptionalInt:(NSString *)option {
NSNumber *cachedValue = [self cachedValueOfClass:[NSNumber class] forKey:option];
if (cachedValue) {
return cachedValue;
}
int value;
if (_isOpen && rime_get_api()->config_get_int(&_config, option.UTF8String, &value)) {
return _cache[option] = @(value);
}
return [_baseConfig getOptionalInt:option];
}
- (NSNumber *)getOptionalDouble:(NSString *)option {
NSNumber *cachedValue = [self cachedValueOfClass:[NSNumber class] forKey:option];
if (cachedValue) {
return cachedValue;
}
double value;
if (_isOpen && rime_get_api()->config_get_double(&_config, option.UTF8String, &value)) {
return _cache[option] = @(value);
}
return [_baseConfig getOptionalDouble:option];
}
- (NSString *)getString:(NSString *)option {
NSString *cachedValue = [self cachedValueOfClass:[NSString class] forKey:option];
if (cachedValue) {
return cachedValue;
}
const char *value =
_isOpen ? rime_get_api()->config_get_cstring(&_config, option.UTF8String) : NULL;
if (value) {
return _cache[option] = @(value);
}
return [_baseConfig getString:option];
}
- (NSColor *)getColor:(NSString *)option {
NSColor *cachedValue = [self cachedValueOfClass:[NSColor class] forKey:option];
if (cachedValue) {
return cachedValue;
}
NSColor *color = [[self class] colorFromString:[self getString:option]];
if (color) {
_cache[option] = color;
return color;
}
return [_baseConfig getColor:option];
}
- (SquirrelAppOptions *)getAppOptions:(NSString *)appName {
NSString * rootKey = [@"app_options/" stringByAppendingString:appName];
SquirrelMutableAppOptions* appOptions = [[SquirrelMutableAppOptions alloc] init];
RimeConfigIterator iterator;
rime_get_api()->config_begin_map(&iterator, &_config, rootKey.UTF8String);
while (rime_get_api()->config_next(&iterator)) {
//NSLog(@"DEBUG option[%d]: %s (%s)", iterator.index, iterator.key, iterator.path);
BOOL value = [self getBool:@(iterator.path)];
appOptions[@(iterator.key)] = @(value);
}
rime_get_api()->config_end(&iterator);
return [appOptions copy];
}
#pragma mark - Private methods
- (id)cachedValueOfClass:(Class)aClass forKey:(NSString *)key {
id value = [_cache objectForKey:key];
if (value && [value isKindOfClass:aClass]) {
return value;
}
return nil;
}
+ (NSColor *)colorFromString:(NSString *)string {
if (string == nil) {
return nil;
}
int r = 0, g = 0, b = 0, a = 0xff;
if (string.length == 10) {
// 0xffccbbaa
sscanf(string.UTF8String, "0x%02x%02x%02x%02x", &a, &b, &g, &r);
} else if (string.length == 8) {
// 0xccbbaa
sscanf(string.UTF8String, "0x%02x%02x%02x", &b, &g, &r);
}
return [NSColor colorWithDeviceRed:(CGFloat)r / 255.
green:(CGFloat)g / 255.
blue:(CGFloat)b / 255.
alpha:(CGFloat)a / 255.];
}
@end