1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2009 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 <UIKit/UIKit.h>
40 #import <Preferences/PSListController.h>
41 #import <Preferences/PSSpecifier.h>
42 #import <Preferences/PSTableCell.h>
43 #import <UIKit/UINavigationButton.h>
45 extern NSString *PSTableCellKey;
46 extern "C" UIImage *_UIImageWithName(NSString *);
48 static UIImage *checkImage;
49 static UIImage *uncheckedImage;
51 static BOOL settingsChanged;
52 static NSMutableDictionary *_settings;
53 static NSString *_plist;
55 /* Theme Settings Controller {{{ */
56 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
57 UITableView *_tableView;
58 NSMutableArray *_themes;
61 @property (nonatomic, retain) NSMutableArray *themes;
65 - (id) initForContentSize:(CGSize)size;
67 - (id) navigationTitle;
68 - (void) themesChanged;
70 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
71 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
72 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
73 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
74 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
75 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
76 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
77 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
78 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
82 @implementation WBSThemesController
84 @synthesize themes = _themes;
87 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
88 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
89 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
93 - (id) initForContentSize:(CGSize)size {
94 if ((self = [super initForContentSize:size]) != nil) {
95 self.themes = [_settings objectForKey:@"Themes"];
97 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
98 self.themes = [NSMutableArray arrayWithObject:
99 [NSMutableDictionary dictionaryWithObjectsAndKeys:
101 [NSNumber numberWithBool:YES], @"Active", nil]];
102 [_settings removeObjectForKey:@"Theme"];
105 self.themes = [NSMutableArray array];
106 [_settings setObject:_themes forKey:@"Themes"];
109 NSMutableArray *themesOnDisk([NSMutableArray array]);
112 addObjectsFromArray:[[NSFileManager defaultManager]
113 contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]
116 [themesOnDisk addObjectsFromArray:[[NSFileManager defaultManager]
117 contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]
121 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
122 NSString *theme = [themesOnDisk objectAtIndex:i];
123 if ([theme hasSuffix:@".theme"])
124 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
127 NSMutableSet *themesSet([NSMutableSet set]);
129 for (int i = 0, count = [_themes count]; i < count; i++) {
130 NSDictionary *theme([_themes objectAtIndex:i]);
131 NSString *name([theme objectForKey:@"Name"]);
133 if (!name || ![themesOnDisk containsObject:name]) {
134 [_themes removeObjectAtIndex:i];
138 [themesSet addObject:name];
142 for (NSString *theme in themesOnDisk) {
143 if ([themesSet containsObject:theme])
145 [themesSet addObject:theme];
147 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
149 [NSNumber numberWithBool:NO], @"Active",
153 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
154 [_tableView setDataSource:self];
155 [_tableView setDelegate:self];
156 [_tableView setEditing:YES];
157 [_tableView setAllowsSelectionDuringEditing:YES];
158 [self showLeftButton:@"WinterBoard" withStyle:1 rightButton:nil withStyle:0];
164 [_tableView release];
169 - (id) navigationTitle {
177 - (void) themesChanged {
178 settingsChanged = YES;
181 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
182 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
186 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
190 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
191 return _themes.count;
194 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
195 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
197 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
198 //[cell setTableViewStyle:UITableViewCellStyleDefault];
201 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
202 cell.text = [theme objectForKey:@"Name"];
203 cell.hidesAccessoryWhenEditing = NO;
204 NSNumber *active([theme objectForKey:@"Active"]);
205 BOOL inactive(active == nil || ![active boolValue]);
206 [cell setImage:(inactive ? uncheckedImage : checkImage)];
210 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
211 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
212 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
213 NSNumber *active = [theme objectForKey:@"Active"];
214 BOOL inactive = active == nil || ![active boolValue];
215 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
216 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
217 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
218 [self themesChanged];
221 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
222 NSUInteger fromIndex = [fromIndexPath row];
223 NSUInteger toIndex = [toIndexPath row];
224 if (fromIndex == toIndex)
226 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
227 [_themes removeObjectAtIndex:fromIndex];
228 [_themes insertObject:theme atIndex:toIndex];
229 [self themesChanged];
232 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
233 return UITableViewCellEditingStyleNone;
236 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
240 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
247 @interface WBSettingsController: PSListController {
250 - (id) initForContentSize:(CGSize)size;
253 - (void) navigationBarButtonClicked:(int)buttonIndex;
254 - (void) viewWillRedisplay;
255 - (void) pushController:(id)controller;
257 - (void) settingsChanged;
258 - (NSString *) title;
259 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
260 - (id) readPreferenceValue:(PSSpecifier *)spec;
264 @implementation WBSettingsController
266 - (id) initForContentSize:(CGSize)size {
267 if ((self = [super initForContentSize:size]) != nil) {
268 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
269 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
280 if (!settingsChanged)
283 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
286 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
289 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
290 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
291 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
292 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
293 // XXX: recursively delete directory!!
294 unlink("/User/Library/Caches/SpringBoardIconCache");
295 unlink("/User/Library/Caches/SpringBoardIconCache-small");
296 system("killall SpringBoard");
299 - (void) navigationBarButtonClicked:(int)buttonIndex {
300 if (!settingsChanged) {
301 [super navigationBarButtonClicked:buttonIndex];
305 if (buttonIndex == 0)
306 settingsChanged = NO;
309 [self.rootController popController];
312 - (void) viewWillRedisplay {
314 [self settingsChanged];
315 [super viewWillRedisplay];
318 - (void) pushController:(id)controller {
319 [self hideNavigationBarButtons];
320 [super pushController:controller];
325 _specifiers = [[self loadSpecifiersFromPlistName:@"WinterBoard" target:self] retain];
329 - (void) settingsChanged {
330 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
331 settingsChanged = YES;
334 - (NSString *) title {
335 return @"WinterBoard";
338 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
339 if ([[spec propertyForKey:@"negate"] boolValue])
340 value = [NSNumber numberWithBool:(![value boolValue])];
341 [_settings setValue:value forKey:[spec propertyForKey:@"key"]];
342 [self settingsChanged];
345 - (id) readPreferenceValue:(PSSpecifier *)spec {
346 NSString *key([spec propertyForKey:@"key"]);
347 id defaultValue([spec propertyForKey:@"default"]);
348 id plistValue([_settings objectForKey:key]);
351 if ([[spec propertyForKey:@"negate"] boolValue])
352 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];