]> git.saurik.com Git - apple/security.git/blob - CircleJoinRequested/PersistentState.m
Security-58286.260.20.tar.gz
[apple/security.git] / CircleJoinRequested / PersistentState.m
1 //
2 // PersistentState.m
3 // Security
4 //
5 // Created by J Osborne on 7/11/13.
6 //
7 //
8
9 #import "PersistentState.h"
10 #import <Foundation/Foundation.h>
11
12 @interface PersistentState()
13 -(NSURL*)urlForStorage;
14 @end
15
16 @implementation PersistentState
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 {
25 return 24 * 60 * 60;
26 }
27
28 +(instancetype)loadFromStorage
29 {
30 PersistentState *state = [[PersistentState alloc] init];
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
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;
61
62 return state;
63 }
64
65 -(void)writeToStorage
66 {
67 NSDictionary *plist = @{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
68 @"lastWritten": [NSDate date],
69 @"pendingApplicationReminder": self.pendingApplicationReminder ?: [NSDate distantFuture],
70 @"applicationDate": self.applicationDate ?: [NSDate distantPast],
71 @"pendingApplicationReminderAlertInterval": [NSNumber numberWithUnsignedInt:self.pendingApplicationReminderAlertInterval],
72 @"absentCircleWithNoReason": [NSNumber numberWithBool:self.absentCircleWithNoReason]};
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