1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2008 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #import <Foundation/Foundation.h>
39 #import <CoreGraphics/CGGeometry.h>
40 #import <UIKit/UIKit.h>
42 #define _trace() NSLog(@"WE:_trace(%u)", __LINE__);
44 static NSString *plist_;
45 static NSMutableDictionary *settings_;
48 @interface WBThemeTableViewCell : UITableViewCell {
54 @implementation WBThemeTableViewCell
59 @interface WBApplication : UIApplication <
60 UITableViewDataSource,
64 UITableView *themesTable_;
65 NSMutableArray *themesArray_;
70 @implementation WBApplication
74 [themesTable_ release];
75 [themesArray_ release];
79 - (void) applicationWillTerminate:(UIApplication *)application {
81 if (![settings_ writeToFile:plist_ atomically:YES])
82 NSLog(@"WB:Error:writeToFile");
83 system("killall SpringBoard");
87 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
88 UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
89 NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]];
90 cell.text = [theme objectForKey:@"Name"];
91 cell.hidesAccessoryWhenEditing = NO;
92 NSNumber *active = [theme objectForKey:@"Active"];
93 BOOL inactive = active == nil || ![active boolValue];
94 cell.accessoryType = inactive ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
98 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
99 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
100 NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]];
101 NSNumber *active = [theme objectForKey:@"Active"];
102 BOOL inactive = active == nil || ![active boolValue];
103 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
104 cell.accessoryType = inactive ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
105 [themesTable_ deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
109 - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
110 return [themesArray_ count];
113 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
114 return UITableViewCellEditingStyleNone;
117 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
118 NSUInteger fromIndex = [fromIndexPath row];
119 NSUInteger toIndex = [toIndexPath row];
120 if (fromIndex == toIndex)
122 NSMutableDictionary *theme = [[[themesArray_ objectAtIndex:fromIndex] retain] autorelease];
123 [themesArray_ removeObjectAtIndex:fromIndex];
124 [themesArray_ insertObject:theme atIndex:toIndex];
128 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
132 - (void) applicationDidFinishLaunching:(id)unused {
133 window_ = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
134 [window_ makeKeyAndVisible];
136 plist_ = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist",
140 settings_ = [[NSMutableDictionary alloc] initWithContentsOfFile:plist_];
141 if (settings_ == nil)
142 settings_ = [[NSMutableDictionary alloc] initWithCapacity:16];
144 themesArray_ = [settings_ objectForKey:@"Themes"];
145 if (themesArray_ == nil) {
146 if (NSString *theme = [settings_ objectForKey:@"Theme"]) {
147 themesArray_ = [[NSArray arrayWithObject:[[NSDictionary dictionaryWithObjectsAndKeys:
149 [NSNumber numberWithBool:YES], @"Active",
150 nil] mutableCopy]] mutableCopy];
152 [settings_ removeObjectForKey:@"Theme"];
155 if (themesArray_ == nil)
156 themesArray_ = [NSMutableArray arrayWithCapacity:16];
157 [settings_ setObject:themesArray_ forKey:@"Themes"];
160 themesArray_ = [themesArray_ retain];
162 NSMutableSet *themesSet = [NSMutableSet setWithCapacity:32];
163 for (NSMutableDictionary *theme in themesArray_)
164 if (NSString *name = [theme objectForKey:@"Name"])
165 [themesSet addObject:name];
167 NSFileManager *manager = [NSFileManager defaultManager];
169 NSMutableArray *themes = [NSMutableArray arrayWithCapacity:32];
170 [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]];
171 [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()] error:NULL]];
173 for (NSUInteger i(0), e([themes count]); i != e; ++i) {
174 NSString *theme = [themes objectAtIndex:i];
175 if ([theme hasSuffix:@".theme"])
176 [themes replaceObjectAtIndex:i withObject:[theme substringWithRange:NSMakeRange(0, [theme length] - 6)]];
179 for (NSUInteger i(0), e([themesArray_ count]); i != e; ++i) {
180 NSMutableDictionary *theme = [themesArray_ objectAtIndex:i];
181 NSString *name = [theme objectForKey:@"Name"];
182 if (name == nil || ![themes containsObject:name]) {
183 [themesArray_ removeObjectAtIndex:i];
188 for (NSString *theme in themes) {
189 if ([themesSet containsObject:theme])
191 [themesSet addObject:theme];
192 [themesArray_ addObject:[[NSDictionary dictionaryWithObjectsAndKeys:
194 [NSNumber numberWithBool:NO], @"Active",
198 themesTable_ = [[UITableView alloc] initWithFrame:window_.bounds];
199 [window_ addSubview:themesTable_];
201 [themesTable_ setDataSource:self];
202 [themesTable_ setDelegate:self];
204 [themesTable_ setEditing:YES animated:NO];
205 themesTable_.allowsSelectionDuringEditing = YES;
207 [themesTable_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
212 int main(int argc, char *argv[]) {
213 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
215 int value = UIApplicationMain(argc, argv, @"WBApplication", @"WBApplication");