5 // Created by J Osborne on 7/11/13.
9 #import "PersistantState.h"
10 #import <Foundation/Foundation.h>
12 @interface PersistantState()
13 -(NSURL*)urlForStorage;
16 @implementation PersistantState
18 -(NSURL*)urlForStorage
20 return [NSURL fileURLWithPath:@"/var/mobile/Library/Preferences/com.apple.security.CircleJoinRequested.plist" isDirectory:NO];
23 -(unsigned int)defaultPendingApplicationReminderAlertInterval
25 return 60 * 60 * 24 * 2;
28 +(instancetype)loadFromStorage
30 PersistantState *state = [[PersistantState alloc] init];
36 id plist = @{@"lastWritten": [NSDate distantPast]};
38 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
40 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
42 NSPropertyListFormat format;
43 id plistTmp = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
45 if (plistTmp == nil) {
46 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
52 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
53 state.lastWritten = plist[@"lastWritten"];
54 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"] ? plist[@"pendingApplicationReminder"] : [NSDate distantFuture];
55 state.applcationDate = plist[@"applcationDate"] ? plist[@"applcationDate"] : [NSDate distantPast];
56 state.debugShowLeftReason = plist[@"debugShowLeftReason"];
57 state.pendingApplicationReminderAlertInterval = plist[@"pendingApplicationReminderAlertInterval"] ? [plist[@"pendingApplicationReminderAlertInterval"] unsignedIntValue] : [state defaultPendingApplicationReminderAlertInterval];
58 state.absentCircleWithNoReason = plist[@"absentCircleWithNoReason"] ? [plist[@"absentCircleWithNoReason"] intValue] : NO;
65 NSDictionary *plist = @{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
66 @"lastWritten": [NSDate date],
67 @"pendingApplicationReminder": self.pendingApplicationReminder ? self.pendingApplicationReminder : [NSDate distantFuture],
68 @"applcationDate": self.applcationDate ? self.applcationDate : [NSDate distantPast],
69 @"pendingApplicationReminderAlertInterval": [NSNumber numberWithUnsignedInt:self.pendingApplicationReminderAlertInterval],
70 @"absentCircleWithNoReason": [NSNumber numberWithBool:self.absentCircleWithNoReason]
72 if (self.debugShowLeftReason) {
73 NSMutableDictionary *tmp = [plist mutableCopy];
74 tmp[@"debugShowLeftReason"] = self.debugShowLeftReason;
77 NSLog(@"writeToStorage plist=%@", plist);
80 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
82 NSLog(@"Can't serialize %@: %@", plist, error);
85 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
86 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);