]> git.saurik.com Git - winterboard.git/blob - Application.mm
Fixed UIImages, fixed jitter aliasing, fixed Themes permissions, and massive code...
[winterboard.git] / Application.mm
1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2008 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
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
17 * distribution.
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.
21 *
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.
36 */
37
38 #import <Foundation/Foundation.h>
39 #import <CoreGraphics/CGGeometry.h>
40 #import <UIKit/UIKit.h>
41
42 #define _trace() NSLog(@"WE:_trace(%u)", __LINE__);
43
44 static NSString *plist_;
45 static NSMutableDictionary *settings_;
46 static BOOL changed_;
47
48 @interface WBThemeTableViewCell : UITableViewCell {
49 UILabel *label;
50 }
51
52 @end
53
54 @implementation WBThemeTableViewCell
55
56
57 @end
58
59 @interface WBApplication : UIApplication <
60 UITableViewDataSource,
61 UITableViewDelegate
62 > {
63 UIWindow *window_;
64 UITableView *themesTable_;
65 NSMutableArray *themesArray_;
66 }
67
68 @end
69
70 @implementation WBApplication
71
72 - (void) dealloc {
73 [window_ release];
74 [themesTable_ release];
75 [themesArray_ release];
76 [super dealloc];
77 }
78
79 - (void) applicationWillTerminate:(UIApplication *)application {
80 if (changed_) {
81 if (![settings_ writeToFile:plist_ atomically:YES])
82 NSLog(@"WB:Error:writeToFile");
83 system("killall SpringBoard");
84 }
85 }
86
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;
95 return cell;
96 }
97
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];
106 changed_ = YES;
107 }
108
109 - (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
110 return [themesArray_ count];
111 }
112
113 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
114 return UITableViewCellEditingStyleNone;
115 }
116
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)
121 return;
122 NSMutableDictionary *theme = [[[themesArray_ objectAtIndex:fromIndex] retain] autorelease];
123 [themesArray_ removeObjectAtIndex:fromIndex];
124 [themesArray_ insertObject:theme atIndex:toIndex];
125 changed_ = YES;
126 }
127
128 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
129 return YES;
130 }
131
132 - (void) applicationDidFinishLaunching:(id)unused {
133 window_ = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
134 [window_ makeKeyAndVisible];
135
136 plist_ = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist",
137 NSHomeDirectory()
138 ] retain];
139
140 settings_ = [[NSMutableDictionary alloc] initWithContentsOfFile:plist_];
141 if (settings_ == nil)
142 settings_ = [[NSMutableDictionary alloc] initWithCapacity:16];
143
144 themesArray_ = [settings_ objectForKey:@"Themes"];
145 if (themesArray_ == nil) {
146 if (NSString *theme = [settings_ objectForKey:@"Theme"]) {
147 themesArray_ = [[NSArray arrayWithObject:[[NSDictionary dictionaryWithObjectsAndKeys:
148 theme, @"Name",
149 [NSNumber numberWithBool:YES], @"Active",
150 nil] mutableCopy]] mutableCopy];
151
152 [settings_ removeObjectForKey:@"Theme"];
153 }
154
155 if (themesArray_ == nil)
156 themesArray_ = [NSMutableArray arrayWithCapacity:16];
157 [settings_ setObject:themesArray_ forKey:@"Themes"];
158 }
159
160 themesArray_ = [themesArray_ retain];
161
162 NSMutableSet *themesSet = [NSMutableSet setWithCapacity:32];
163 for (NSMutableDictionary *theme in themesArray_)
164 if (NSString *name = [theme objectForKey:@"Name"])
165 [themesSet addObject:name];
166
167 NSFileManager *manager = [NSFileManager defaultManager];
168
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]];
172
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)]];
177 }
178
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];
184 --i; --e;
185 }
186 }
187
188 for (NSString *theme in themes) {
189 if ([themesSet containsObject:theme])
190 continue;
191 [themesSet addObject:theme];
192 [themesArray_ addObject:[[NSDictionary dictionaryWithObjectsAndKeys:
193 theme, @"Name",
194 [NSNumber numberWithBool:NO], @"Active",
195 nil] mutableCopy]];
196 }
197
198 themesTable_ = [[UITableView alloc] initWithFrame:window_.bounds];
199 [window_ addSubview:themesTable_];
200
201 [themesTable_ setDataSource:self];
202 [themesTable_ setDelegate:self];
203
204 [themesTable_ setEditing:YES animated:NO];
205 themesTable_.allowsSelectionDuringEditing = YES;
206
207 [themesTable_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
208 }
209
210 @end
211
212 int main(int argc, char *argv[]) {
213 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
214
215 int value = UIApplicationMain(argc, argv, @"WBApplication", @"WBApplication");
216
217 [pool release];
218 return value;
219 }