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_;
47 @interface WBThemeTableViewCell : UITableViewCell {
53 @implementation WBThemeTableViewCell
58 @interface WBApplication : UIApplication <
59 UITableViewDataSource,
63 UITableView *themesTable_;
64 NSMutableArray *themesArray_;
69 @implementation WBApplication
73 [themesTable_ release];
74 [themesArray_ release];
78 - (void) applicationWillTerminate:(UIApplication *)application {
79 [settings_ writeToFile:plist_ atomically:YES];
80 system("killall SpringBoard");
83 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
84 UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
85 NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]];
86 cell.text = [theme objectForKey:@"Name"];
87 cell.hidesAccessoryWhenEditing = NO;
88 NSNumber *active = [theme objectForKey:@"Active"];
89 BOOL inactive = active == nil || ![active boolValue];
90 cell.accessoryType = inactive ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
94 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
95 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
96 NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]];
97 NSNumber *active = [theme objectForKey:@"Active"];
98 BOOL inactive = active == nil || ![active boolValue];
99 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
100 cell.accessoryType = inactive ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
101 [themesTable_ deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
104 - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
105 return [themesArray_ count];
108 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
109 return UITableViewCellEditingStyleNone;
112 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
113 NSUInteger fromIndex = [fromIndexPath row];
114 NSUInteger toIndex = [toIndexPath row];
115 if (fromIndex == toIndex)
117 NSMutableDictionary *theme = [[[themesArray_ objectAtIndex:fromIndex] retain] autorelease];
118 [themesArray_ removeObjectAtIndex:fromIndex];
119 [themesArray_ insertObject:theme atIndex:toIndex];
122 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
126 - (void) applicationDidFinishLaunching:(id)unused {
127 window_ = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
128 [window_ makeKeyAndVisible];
130 plist_ = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist",
134 settings_ = [[NSMutableDictionary alloc] initWithContentsOfFile:plist_];
136 themesArray_ = [settings_ objectForKey:@"Themes"];
137 if (themesArray_ == nil) {
138 if (NSString *theme = [settings_ objectForKey:@"Theme"]) {
139 themesArray_ = [[NSArray arrayWithObject:[[NSDictionary dictionaryWithObjectsAndKeys:
141 [NSNumber numberWithBool:YES], @"Active",
142 nil] mutableCopy]] mutableCopy];
144 [settings_ removeObjectForKey:@"Theme"];
147 if (themesArray_ == nil)
148 themesArray_ = [NSMutableArray arrayWithCapacity:16];
149 [settings_ setObject:themesArray_ forKey:@"Themes"];
152 themesArray_ = [themesArray_ retain];
154 NSMutableSet *themesSet = [NSMutableSet setWithCapacity:32];
155 for (NSMutableDictionary *theme in themesArray_)
156 if (NSString *name = [theme objectForKey:@"Name"])
157 [themesSet addObject:name];
159 NSFileManager *manager = [NSFileManager defaultManager];
161 NSMutableArray *themes = [NSMutableArray arrayWithCapacity:32];
162 [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]];
163 [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()] error:NULL]];
165 for (NSString *theme in themes) {
166 if ([theme hasSuffix:@".theme"])
167 theme = [theme substringWithRange:NSMakeRange(0, [theme length] - 6)];
168 if ([themesSet containsObject:theme])
170 [themesSet addObject:theme];
171 [themesArray_ addObject:[[NSDictionary dictionaryWithObjectsAndKeys:
173 [NSNumber numberWithBool:NO], @"Active",
177 themesTable_ = [[UITableView alloc] initWithFrame:window_.bounds];
178 [window_ addSubview:themesTable_];
180 [themesTable_ setDataSource:self];
181 [themesTable_ setDelegate:self];
183 [themesTable_ setEditing:YES animated:NO];
184 themesTable_.allowsSelectionDuringEditing = YES;
186 [themesTable_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
191 int main(int argc, char *argv[]) {
192 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
194 int value = UIApplicationMain(argc, argv, @"WBApplication", @"WBApplication");