]>
Commit | Line | Data |
---|---|---|
d8f41ccd | 1 | // |
5c19dc3a | 2 | // PersistentState.m |
d8f41ccd A |
3 | // Security |
4 | // | |
5 | // Created by J Osborne on 7/11/13. | |
6 | // | |
7 | // | |
8 | ||
5c19dc3a | 9 | #import "PersistentState.h" |
d8f41ccd A |
10 | #import <Foundation/Foundation.h> |
11 | ||
5c19dc3a | 12 | @interface PersistentState() |
d8f41ccd A |
13 | -(NSURL*)urlForStorage; |
14 | @end | |
15 | ||
5c19dc3a | 16 | @implementation PersistentState |
d8f41ccd A |
17 | |
18 | -(NSURL*)urlForStorage | |
19 | { | |
20 | return [NSURL fileURLWithPath:@"/var/mobile/Library/Preferences/com.apple.security.CircleJoinRequested.plist" isDirectory:NO]; | |
21 | } | |
22 | ||
23 | -(unsigned int)defaultPendingApplicationReminderAlertInterval | |
24 | { | |
5c19dc3a | 25 | return 24 * 60 * 60; |
d8f41ccd A |
26 | } |
27 | ||
28 | +(instancetype)loadFromStorage | |
29 | { | |
5c19dc3a | 30 | PersistentState *state = [[PersistentState alloc] init]; |
d8f41ccd A |
31 | if (!state) { |
32 | return state; | |
33 | } | |
34 | ||
35 | NSError *error = nil; | |
36 | id plist = @{@"lastWritten": [NSDate distantPast]}; | |
37 | ||
38 | NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error]; | |
39 | if (!stateData) { | |
40 | NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error); | |
41 | } else { | |
42 | NSPropertyListFormat format; | |
43 | id plistTmp = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error]; | |
44 | ||
45 | if (plistTmp == nil) { | |
46 | NSLog(@"Can't deserialize %@, e=%@", stateData, error); | |
47 | } else { | |
48 | plist = plistTmp; | |
49 | } | |
50 | } | |
51 | ||
5c19dc3a A |
52 | state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent; |
53 | state.lastWritten = plist[@"lastWritten"]; | |
54 | state.pendingApplicationReminder = plist[@"pendingApplicationReminder"] ?: [NSDate distantFuture]; | |
55 | state.applicationDate = plist[@"applicationDate"] ?: [NSDate distantPast]; | |
56 | state.debugShowLeftReason = plist[@"debugShowLeftReason"]; | |
57 | state.pendingApplicationReminderAlertInterval = plist[@"pendingApplicationReminderAlertInterval"] ? | |
58 | [plist[@"pendingApplicationReminderAlertInterval"] unsignedIntValue] : | |
59 | [state defaultPendingApplicationReminderAlertInterval]; | |
60 | state.absentCircleWithNoReason = plist[@"absentCircleWithNoReason"] ? [plist[@"absentCircleWithNoReason"] intValue] : NO; | |
d8f41ccd A |
61 | |
62 | return state; | |
63 | } | |
64 | ||
65 | -(void)writeToStorage | |
66 | { | |
67 | NSDictionary *plist = @{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus], | |
68 | @"lastWritten": [NSDate date], | |
5c19dc3a A |
69 | @"pendingApplicationReminder": self.pendingApplicationReminder ?: [NSDate distantFuture], |
70 | @"applicationDate": self.applicationDate ?: [NSDate distantPast], | |
d8f41ccd | 71 | @"pendingApplicationReminderAlertInterval": [NSNumber numberWithUnsignedInt:self.pendingApplicationReminderAlertInterval], |
5c19dc3a | 72 | @"absentCircleWithNoReason": [NSNumber numberWithBool:self.absentCircleWithNoReason]}; |
d8f41ccd A |
73 | if (self.debugShowLeftReason) { |
74 | NSMutableDictionary *tmp = [plist mutableCopy]; | |
75 | tmp[@"debugShowLeftReason"] = self.debugShowLeftReason; | |
76 | plist =[tmp copy]; | |
77 | } | |
78 | NSLog(@"writeToStorage plist=%@", plist); | |
79 | ||
80 | NSError *error = nil; | |
81 | NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error]; | |
82 | if (!stateData) { | |
83 | NSLog(@"Can't serialize %@: %@", plist, error); | |
84 | return; | |
85 | } | |
86 | if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) { | |
87 | NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error); | |
88 | } | |
89 | } | |
90 | ||
91 | @end |