Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // |
2 | // InAppSettingsReader.m |
||
3 | // InAppSettingsTestApp |
||
4 | // |
||
5 | // Created by David Keegan on 1/19/10. |
||
6 | // Copyright 2010 InScopeApps{+}. All rights reserved. |
||
7 | // |
||
8 | |||
9 | #import "InAppSettingsReader.h" |
||
10 | #import "InAppSettingsSpecifier.h" |
||
11 | #import "InAppSettingsConstants.h" |
||
12 | |||
13 | @implementation InAppSettingsReaderRegisterDefaults |
||
14 | |||
15 | @synthesize files; |
||
16 | @synthesize values; |
||
17 | |||
18 | - (void) loadFile:(NSString *)file { |
||
19 | // if the file is not in the files list we havn't read it yet |
||
20 | NSInteger fileIndex = [self.files indexOfObject:file]; |
||
21 | |||
22 | if (fileIndex == NSNotFound) { |
||
23 | [self.files addObject:file]; |
||
24 | |||
25 | // load plist |
||
26 | NSDictionary * settingsDictionary = [[NSDictionary alloc] initWithContentsOfFile:InAppSettingsFullPlistPath(file)]; |
||
27 | NSArray * preferenceSpecifiers = [settingsDictionary objectForKey:InAppSettingsPreferenceSpecifiers]; |
||
28 | NSString * stringsTable = [settingsDictionary objectForKey:InAppSettingsStringsTable]; |
||
29 | |||
30 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
||
31 | for (NSDictionary * eachSetting in preferenceSpecifiers) { |
||
32 | InAppSettingsSpecifier * setting = [[InAppSettingsSpecifier alloc] initWithDictionary:eachSetting andStringsTable:stringsTable]; |
||
33 | if ([setting isValid]) { |
||
34 | if ([setting isType:InAppSettingsPSChildPaneSpecifier]) { |
||
35 | [self loadFile:[setting valueForKey:InAppSettingsSpecifierFile]]; |
||
36 | } else if ([setting hasKey]) { |
||
37 | if ([setting valueForKey:InAppSettingsSpecifierDefaultValue]) { |
||
38 | [self.values |
||
39 | setObject:[setting valueForKey:InAppSettingsSpecifierDefaultValue] |
||
40 | forKey:[setting getKey]]; |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | [setting release]; |
||
45 | } |
||
46 | [pool drain]; |
||
47 | [settingsDictionary release]; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | - (id) init { |
||
52 | self = [super init]; |
||
53 | if (self != nil) { |
||
54 | self.files = [[NSMutableArray alloc] init]; |
||
55 | self.values = [[NSMutableDictionary alloc] init]; |
||
56 | [self loadFile:InAppSettingsRootFile]; |
||
57 | [[NSUserDefaults standardUserDefaults] registerDefaults:self.values]; |
||
58 | } |
||
59 | return self; |
||
60 | } |
||
61 | |||
62 | |||
63 | - (void) dealloc { |
||
64 | [files release]; |
||
65 | [values release]; |
||
66 | [super dealloc]; |
||
67 | } |
||
68 | |||
69 | @end |
||
70 | |||
71 | @implementation InAppSettingsReader |
||
72 | |||
73 | @synthesize file; |
||
74 | @synthesize headers, settings; |
||
75 | |||
76 | - (id) initWithFile:(NSString *)inputFile { |
||
77 | self = [super init]; |
||
78 | if (self != nil) { |
||
79 | self.file = inputFile; |
||
80 | |||
81 | // load plist |
||
82 | NSDictionary * settingsDictionary = [[NSDictionary alloc] initWithContentsOfFile:InAppSettingsFullPlistPath(self.file)]; |
||
83 | NSArray * preferenceSpecifiers = [settingsDictionary objectForKey:InAppSettingsPreferenceSpecifiers]; |
||
84 | NSString * stringsTable = [settingsDictionary objectForKey:InAppSettingsStringsTable]; |
||
85 | |||
86 | // initialize the arrays |
||
87 | headers = [[NSMutableArray alloc] init]; |
||
88 | settings = [[NSMutableArray alloc] init]; |
||
89 | |||
90 | // load the data |
||
91 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
||
92 | for (NSDictionary * eachSetting in preferenceSpecifiers) { |
||
93 | InAppSettingsSpecifier * setting = [[InAppSettingsSpecifier alloc] initWithDictionary:eachSetting andStringsTable:stringsTable]; |
||
94 | setting.dataSource = dataSource; |
||
95 | if ([setting isValid]) { |
||
96 | if ([setting isType:InAppSettingsPSGroupSpecifier]) { |
||
97 | [self.headers addObject:[setting localizedTitle]]; |
||
98 | [self.settings addObject:[NSMutableArray array]]; |
||
99 | } else { |
||
100 | // if there are no settings make an initial container |
||
101 | if ([self.settings count] < 1) { |
||
102 | [self.headers addObject:@""]; |
||
103 | [self.settings addObject:[NSMutableArray array]]; |
||
104 | } |
||
105 | [[self.settings lastObject] addObject:setting]; |
||
106 | } |
||
107 | } |
||
108 | [setting release]; |
||
109 | } |
||
110 | [pool drain]; |
||
111 | [settingsDictionary release]; |
||
112 | } |
||
113 | return self; |
||
114 | } |
||
115 | |||
116 | - (id) initWithFile:(NSString *)inputFile dataSource:(id<InAppSettingsDatasource>)aDataSource { |
||
117 | dataSource = aDataSource; |
||
118 | return [self initWithFile:inputFile]; |
||
119 | } |
||
120 | |||
121 | - (void) dealloc { |
||
122 | [file release]; |
||
123 | [headers release]; |
||
124 | [settings release]; |
||
125 | [super dealloc]; |
||
126 | } |
||
127 | |||
128 | @end |