2 // IASKSettingsReader.m
3 // http://www.inappsettingskit.com
6 // Luc Vandal, Edovia Inc., http://www.edovia.com
7 // Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com
8 // All rights reserved.
10 // It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz,
11 // as the original authors of this code. You can give credit in a blog post, a tweet or on
12 // a info page of your app. Also, the original authors appreciate letting them know if you use this code.
14 // This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php
17 #import "IASKSettingsReader.h"
18 #import "IASKSpecifier.h"
20 @interface IASKSettingsReader (private)
21 - (void)_reinterpretBundle:(NSDictionary*)settingsBundle;
22 - (BOOL)_sectionHasHeading:(NSInteger)section;
25 @implementation IASKSettingsReader
27 @synthesize path=_path,
28 bundleFolder=_bundleFolder,
29 settingsBundle=_settingsBundle,
30 dataSource=_dataSource;
33 return [self initWithFile:@"Root"];
36 - (id)initWithFile:(NSString*)file {
37 if ((self=[super init])) {
38 [self setBundleFolder:kIASKBundleFolderAlt];
39 // Generate the settings bundle path
40 NSString *path = [self bundlePath];
42 // Try both bundle folders
43 for (int i=0;i<2;i++) {
44 [self setPath:[path stringByAppendingPathComponent:[file stringByAppendingString:@".inApp.plist"]]];
45 [self setSettingsBundle:[NSDictionary dictionaryWithContentsOfFile:[self path]]];
46 if (!self.settingsBundle) {
47 [self setPath:[path stringByAppendingPathComponent:[file stringByAppendingString:@".plist"]]];
48 [self setSettingsBundle:[NSDictionary dictionaryWithContentsOfFile:[self path]]];
50 if (self.settingsBundle)
52 [self setBundleFolder:kIASKBundleFolder];
53 path = [self bundlePath];
55 _bundle = [[NSBundle bundleWithPath:path] retain];
57 if (_settingsBundle) {
58 [self _reinterpretBundle:_settingsBundle];
66 [_settingsBundle release];
67 [_dataSource release];
72 - (void)_reinterpretBundle:(NSDictionary*)settingsBundle {
73 NSArray *preferenceSpecifiers = [settingsBundle objectForKey:kIASKPreferenceSpecifiers];
74 NSInteger sectionCount = -1;
75 NSMutableArray *dataSource = [[[NSMutableArray alloc] init] autorelease];
77 for (NSDictionary *specifier in preferenceSpecifiers) {
78 if ([(NSString*)[specifier objectForKey:kIASKType] isEqualToString:kIASKPSGroupSpecifier]) {
79 NSMutableArray *newArray = [[NSMutableArray alloc] init];
81 [newArray addObject:specifier];
82 [dataSource addObject:newArray];
87 if (sectionCount == -1) {
88 NSMutableArray *newArray = [[NSMutableArray alloc] init];
89 [dataSource addObject:newArray];
94 IASKSpecifier *newSpecifier = [[IASKSpecifier alloc] initWithSpecifier:specifier];
95 [(NSMutableArray*)[dataSource objectAtIndex:sectionCount] addObject:newSpecifier];
96 [newSpecifier release];
99 [self setDataSource:dataSource];
102 - (BOOL)_sectionHasHeading:(NSInteger)section {
103 return [[[[self dataSource] objectAtIndex:section] objectAtIndex:0] isKindOfClass:[NSDictionary class]];
106 - (NSInteger)numberOfSections {
107 return [[self dataSource] count];
110 - (NSInteger)numberOfRowsForSection:(NSInteger)section {
111 int headingCorrection = [self _sectionHasHeading:section] ? 1 : 0;
112 return [(NSArray*)[[self dataSource] objectAtIndex:section] count] - headingCorrection;
115 - (IASKSpecifier*)specifierForIndexPath:(NSIndexPath*)indexPath {
116 int headingCorrection = [self _sectionHasHeading:indexPath.section] ? 1 : 0;
118 IASKSpecifier *specifier = [[[self dataSource] objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row+headingCorrection)];
119 specifier.settingsReader = self;
123 - (IASKSpecifier*)specifierForKey:(NSString*)key {
124 for (NSArray *specifiers in _dataSource) {
125 for (id sp in specifiers) {
126 if ([sp isKindOfClass:[IASKSpecifier class]]) {
127 if ([[sp key] isEqualToString:key]) {
136 - (NSString*)titleForSection:(NSInteger)section {
137 if ([self _sectionHasHeading:section]) {
138 NSDictionary *dict = [[[self dataSource] objectAtIndex:section] objectAtIndex:kIASKSectionHeaderIndex];
139 return [_bundle localizedStringForKey:[dict objectForKey:kIASKTitle] value:[dict objectForKey:kIASKTitle] table:@"Root"];
144 - (NSString*)titleForStringId:(NSString*)stringId {
145 return [_bundle localizedStringForKey:stringId value:stringId table:@"Root"];
148 - (NSString*)bundlePath {
149 NSString *libDirectory = [[NSBundle mainBundle] bundlePath];
150 return [libDirectory stringByAppendingPathComponent:_bundleFolder];
153 - (NSString*)pathForImageNamed:(NSString*)image {
154 return [[self bundlePath] stringByAppendingPathComponent:image];