1b694b2c1a7047544190589f429595a77d53ab63
[atutor.git] / mods / atsocial_iphone_app / InAppSettingsKit / Models / IASKSettingsReader.m
1 //
2 //  IASKSettingsReader.m
3 //  http://www.inappsettingskit.com
4 //
5 //  Copyright (c) 2009:
6 //  Luc Vandal, Edovia Inc., http://www.edovia.com
7 //  Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com
8 //  All rights reserved.
9 // 
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.
13 //
14 //  This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php
15 //
16
17 #import "IASKSettingsReader.h"
18 #import "IASKSpecifier.h"
19
20 @interface IASKSettingsReader (private)
21 - (void)_reinterpretBundle:(NSDictionary*)settingsBundle;
22 - (BOOL)_sectionHasHeading:(NSInteger)section;
23 @end
24
25 @implementation IASKSettingsReader
26
27 @synthesize path=_path,
28 bundleFolder=_bundleFolder,
29 settingsBundle=_settingsBundle, 
30 dataSource=_dataSource;
31
32 - (id)init {
33         return [self initWithFile:@"Root"];
34 }
35
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];
41                 
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]]];
49                         }
50                         if (self.settingsBundle)
51                                 break;
52                         [self setBundleFolder:kIASKBundleFolder];
53                         path = [self bundlePath];
54                 }
55         _bundle = [[NSBundle bundleWithPath:path] retain];
56         
57         if (_settingsBundle) {
58             [self _reinterpretBundle:_settingsBundle];
59         }
60     }
61     return self;
62 }
63
64 - (void)dealloc {
65     [_path release];
66     [_settingsBundle release];
67     [_dataSource release];
68     [_bundle release];
69     [super dealloc];
70 }
71
72 - (void)_reinterpretBundle:(NSDictionary*)settingsBundle {
73     NSArray *preferenceSpecifiers   = [settingsBundle objectForKey:kIASKPreferenceSpecifiers];
74     NSInteger sectionCount          = -1;
75     NSMutableArray *dataSource      = [[[NSMutableArray alloc] init] autorelease];
76     
77     for (NSDictionary *specifier in preferenceSpecifiers) {
78         if ([(NSString*)[specifier objectForKey:kIASKType] isEqualToString:kIASKPSGroupSpecifier]) {
79             NSMutableArray *newArray = [[NSMutableArray alloc] init];
80             
81             [newArray addObject:specifier];
82             [dataSource addObject:newArray];
83             [newArray release];
84             sectionCount++;
85         }
86         else {
87             if (sectionCount == -1) {
88                 NSMutableArray *newArray = [[NSMutableArray alloc] init];
89                                 [dataSource addObject:newArray];
90                                 [newArray release];
91                                 sectionCount++;
92                         }
93
94             IASKSpecifier *newSpecifier = [[IASKSpecifier alloc] initWithSpecifier:specifier];
95             [(NSMutableArray*)[dataSource objectAtIndex:sectionCount] addObject:newSpecifier];
96             [newSpecifier release];
97         }
98     }
99     [self setDataSource:dataSource];
100 }
101
102 - (BOOL)_sectionHasHeading:(NSInteger)section {
103     return [[[[self dataSource] objectAtIndex:section] objectAtIndex:0] isKindOfClass:[NSDictionary class]];
104 }
105
106 - (NSInteger)numberOfSections {
107     return [[self dataSource] count];
108 }
109
110 - (NSInteger)numberOfRowsForSection:(NSInteger)section {
111     int headingCorrection = [self _sectionHasHeading:section] ? 1 : 0;
112     return [(NSArray*)[[self dataSource] objectAtIndex:section] count] - headingCorrection;
113 }
114
115 - (IASKSpecifier*)specifierForIndexPath:(NSIndexPath*)indexPath {
116     int headingCorrection = [self _sectionHasHeading:indexPath.section] ? 1 : 0;
117     
118     IASKSpecifier *specifier = [[[self dataSource] objectAtIndex:indexPath.section] objectAtIndex:(indexPath.row+headingCorrection)];
119         specifier.settingsReader = self;
120         return specifier;
121 }
122
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]) {
128                     return sp;
129                 }
130             }
131         }
132     }
133     return nil;
134 }
135
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"];
140     }
141     return nil;
142 }
143
144 - (NSString*)titleForStringId:(NSString*)stringId {
145     return [_bundle localizedStringForKey:stringId value:stringId table:@"Root"];
146 }
147
148 - (NSString*)bundlePath {
149     NSString *libDirectory  = [[NSBundle mainBundle] bundlePath];
150     return [libDirectory stringByAppendingPathComponent:_bundleFolder];
151 }
152
153 - (NSString*)pathForImageNamed:(NSString*)image {
154     return [[self bundlePath] stringByAppendingPathComponent:image];
155 }
156
157 @end