-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEcUserDefaults.m
501 lines (426 loc) · 10.6 KB
/
EcUserDefaults.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSMethodSignature.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSProxy.h>
#import <Foundation/NSString.h>
#import "EcUserDefaults.h"
static NSUserDefaults *latest = nil;
static NSLock *lock = nil;
static NSMutableDictionary *lifetimes = nil;
static NSMutableDictionary *overrides = nil;
static NSTimeInterval defaultDuration = (72.0 * 60.0 * 60.0);
@interface EcUserDefaults : NSProxy
{
NSUserDefaults *defs;
NSString *prefix;
}
- (id) initWithPrefix: (NSString*)p;
- (NSString*) _getKey: (NSString*)baseKey;
@end
@implementation EcUserDefaults
+ (void) initialize
{
if (nil == lock)
{
lock = [NSLock new];
lifetimes = [NSMutableDictionary new];
overrides = [NSMutableDictionary new];
[[NSObject leakAt: &lock] release];
[[NSObject leakAt: &latest] release];
[[NSObject leakAt: &lifetimes] release];
[[NSObject leakAt: &overrides] release];
}
}
- (NSArray*) arrayForKey: (NSString*)aKey
{
return [defs arrayForKey: [self _getKey: aKey]];
}
- (BOOL) boolForKey: (NSString*)aKey
{
return [defs boolForKey: [self _getKey: aKey]];
}
- (NSDictionary*) commandExpiries
{
return [defs commandExpiries];
}
- (id) commandObjectForKey: (NSString*)aKey
{
return [defs commandObjectForKey: aKey];
}
- (NSData*) dataForKey: (NSString*)aKey
{
return [defs dataForKey: [self _getKey: aKey]];
}
- (void) dealloc
{
[lock lock];
if (latest == (NSUserDefaults*)self)
{
latest = nil;
}
[lock unlock];
[prefix release];
[defs release];
[super dealloc];
}
- (NSString*) defaultsPrefix
{
return prefix;
}
- (NSDictionary*) dictionaryForKey: (NSString*)aKey
{
return [defs dictionaryForKey: [self _getKey: aKey]];
}
- (double) doubleForKey: (NSString*)aKey
{
return [defs doubleForKey: [self _getKey: aKey]];
}
- (float) floatForKey: (NSString*)aKey
{
return [defs floatForKey: [self _getKey: aKey]];
}
- (void) forwardInvocation: (NSInvocation*)anInvocation
{
[anInvocation invokeWithTarget: defs];
}
- (NSString*) _getKey: (NSString*)aKey
{
/* Make sure we have the prefix.
*/
if (nil != prefix)
{
if (NO == [aKey hasPrefix: prefix])
{
aKey = [prefix stringByAppendingString: aKey];
}
if (nil == [defs objectForKey: aKey])
{
/* Nothing found for key ... try without the prefix.
*/
aKey = [aKey substringFromIndex: [prefix length]];
}
}
return aKey;
}
- (id) init
{
[self release];
return nil;
}
- (id) initWithPrefix: (NSString*)p
{
NSMutableArray *list;
[lock lock];
defs = [[NSUserDefaults standardUserDefaults] retain];
if (0 == [p length])
{
p = [defs stringForKey: @"EcUserDefaultsPrefix"];
if (0 == [p length])
{
p = nil;
}
}
prefix = [p copy];
/* Make sure the defaults database has our special domains at the start
* of the search list and in the correct order.
*/
list = [[defs searchList] mutableCopy];
[list removeObject: @"EcCommand"];
[list removeObject: @"EcConfiguration"];
[list insertObject: @"EcCommand" atIndex: 0];
[list insertObject: @"EcConfiguration" atIndex: 1];
[defs setSearchList: list];
[list release];
ASSIGN(latest, (NSUserDefaults*)self);
[lock unlock];
return self;
}
- (NSInteger) integerForKey: (NSString*)aKey
{
return [defs integerForKey: [self _getKey: aKey]];
}
- (NSString*) key: (NSString*)aKey
{
/* Make sure we have the prefix.
*/
if (nil != prefix && NO == [aKey hasPrefix: prefix])
{
aKey = [prefix stringByAppendingString: aKey];
}
return aKey;
}
- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector
{
if (class_respondsToSelector(object_getClass(self), aSelector))
{
return [super methodSignatureForSelector: aSelector];
}
return [defs methodSignatureForSelector: aSelector];
}
- (id) objectForKey: (NSString*)aKey
{
return [defs objectForKey: [self _getKey: aKey]];
}
- (void) purgeSettings
{
[defs purgeSettings];
}
- (void) removeObjectForKey: (NSString*)aKey
{
return [defs removeObjectForKey: [self _getKey: aKey]];
}
- (void) revertSettings
{
[defs revertSettings];
}
- (void) setBool: (BOOL)value forKey: (NSString*)aKey
{
[defs setBool: value forKey: aKey];
}
- (BOOL) setCommand: (id)val forKey: (NSString*)key
{
return [defs setCommand: val forKey: key];
}
- (BOOL) setCommand: (id)val
forKey: (NSString*)key
lifetime: (NSTimeInterval)t
{
return [defs setCommand: val forKey: key lifetime: t];
}
- (void) setDouble: (double)value forKey: (NSString*)aKey
{
[defs setDouble: value forKey: aKey];
}
- (void) setFloat: (float)value forKey: (NSString*)aKey
{
[defs setFloat: value forKey: aKey];
}
- (void) setInteger: (NSInteger)value forKey: (NSString*)aKey
{
[defs setInteger: value forKey: aKey];
}
- (void) setObject: (id)anObject forKey: (NSString*)aKey
{
[defs setObject: anObject forKey: aKey];
}
- (NSArray*) stringArrayForKey: (NSString*)aKey
{
return [defs stringArrayForKey: [self _getKey: aKey]];
}
- (NSString*) stringForKey: (NSString*)aKey
{
return [defs stringForKey: [self _getKey: aKey]];
}
- (NSUserDefaults*) target
{
return defs;
}
@end
@implementation NSUserDefaults (EcUserDefaults)
+ (NSUserDefaults*) prefixedDefaults
{
NSUserDefaults *defs = nil;
if (Nil != [EcUserDefaults class])
{
[lock lock];
defs = [latest retain];
[lock unlock];
}
return [defs autorelease];
}
+ (void) setDefaultLifetime: (NSTimeInterval)t
{
if (t != t || t <= 0)
{
t = 72.0 * 60.0 * 60.0;
}
defaultDuration = t;
}
+ (NSUserDefaults*) userDefaultsWithPrefix: (NSString*)aPrefix
{
return (NSUserDefaults*)[[[EcUserDefaults alloc]
initWithPrefix: aPrefix] autorelease];
}
- (NSDictionary*) commandExpiries
{
NSDictionary *d;
[lock lock];
d = [lifetimes copy];
[lock unlock];
return [d autorelease];
}
- (id) commandObjectForKey: (NSString*)aKey
{
id val;
[lock lock];
val = [[overrides objectForKey: aKey] retain];
[lock unlock];
return [val autorelease];
}
- (NSDictionary*) configuration
{
return [self volatileDomainForName: @"EcConfiguration"];
}
- (NSString*) defaultsPrefix
{
return nil; // No prefix in use ... this is not a proxy
}
- (NSString*) key: (NSString*)aKey
{
NSString *prefix = [self defaultsPrefix];
/* Make sure we have the prefix.
*/
if (nil != prefix && NO == [aKey hasPrefix: prefix])
{
aKey = [prefix stringByAppendingString: aKey];
}
return aKey;
}
- (void) purgeSettings
{
NSDictionary *new = nil;
BOOL changed = NO;
NSEnumerator *enumerator;
NSString *key;
NSDate *now = [NSDate date];
[lock lock];
enumerator = [[lifetimes allKeys] objectEnumerator];
while (nil != (key = [enumerator nextObject]))
{
NSDate *expires = [lifetimes objectForKey: key];
if ([expires laterDate: now] != expires)
{
[lifetimes removeObjectForKey: key];
[overrides removeObjectForKey: key];
changed = YES;
}
}
if (YES == changed && [overrides count] > 0)
{
new = [overrides copy];
}
[lock unlock];
if (YES == changed)
{
[self removeVolatileDomainForName: @"EcCommand"];
if (new != nil)
{
[self setVolatileDomain: new forName: @"EcCommand"];
[new release];
}
[[NSNotificationCenter defaultCenter] postNotificationName:
NSUserDefaultsDidChangeNotification object: self];
}
}
- (void) revertSettings
{
BOOL changed = NO;
[lock lock];
if (YES == (changed = [overrides count] > 0 ? YES : NO))
{
[lifetimes removeAllObjects];
[overrides removeAllObjects];
}
[lock unlock];
if (YES == changed)
{
[self removeVolatileDomainForName: @"EcCommand"];
[[NSNotificationCenter defaultCenter] postNotificationName:
NSUserDefaultsDidChangeNotification object: self];
}
}
- (BOOL) setCommand: (id)val forKey: (NSString*)key
{
return [self setCommand: val forKey: key lifetime: defaultDuration];
}
- (BOOL) setCommand: (id)val forKey: (NSString*)key lifetime: (NSTimeInterval)t
{
NSDictionary *new = nil;
BOOL changed = NO;
id old;
[lock lock];
old = [overrides objectForKey: key];
if (old != val && NO == [old isEqual: val])
{
if (nil == val)
{
[overrides removeObjectForKey: key];
[lifetimes removeObjectForKey: key];
}
else
{
[overrides setObject: val forKey: key];
[lifetimes setObject: [NSDate dateWithTimeIntervalSinceNow: t]
forKey: key];
}
if ([overrides count] > 0)
{
new = [overrides copy];
}
changed = YES;
}
else if (old != nil)
{
/* No change to the value, only to its expiry time.
*/
[lifetimes setObject: [NSDate dateWithTimeIntervalSinceNow: t]
forKey: key];
}
[lock unlock];
if (YES == changed)
{
[self removeVolatileDomainForName: @"EcCommand"];
if (nil != new)
{
[self setVolatileDomain: new forName: @"EcCommand"];
[new release];
}
[[NSNotificationCenter defaultCenter] postNotificationName:
NSUserDefaultsDidChangeNotification object: self];
}
return changed;
}
- (BOOL) setConfiguration: (NSDictionary*)config
{
NSDictionary *old = [self volatileDomainForName: @"EcConfiguration"];
if (old != config && NO == [old isEqual: config])
{
if (nil != old)
{
[self removeVolatileDomainForName: @"EcConfiguration"];
}
if (nil != config)
{
[self setVolatileDomain: config forName: @"EcConfiguration"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:
NSUserDefaultsDidChangeNotification
object: self];
return YES;
}
return NO;
}
@end