1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2009-2010 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/PSRootController.h>
41 #import <Preferences/PSViewController.h>
42 #import <Preferences/PSListController.h>
43 #import <Preferences/PSSpecifier.h>
44 #import <Preferences/PSTableCell.h>
45 #import <UIKit/UINavigationButton.h>
48 #include <objc/runtime.h>
50 static BOOL (*IsIconHiddenDisplayId)(NSString *);
51 static BOOL (*HideIconViaDisplayId)(NSString *);
52 static BOOL (*UnHideIconViaDisplayId)(NSString *);
54 static const NSString *WinterBoardDisplayID = @"com.saurik.WinterBoard";
56 extern NSString *PSTableCellKey;
57 extern "C" UIImage *_UIImageWithName(NSString *);
59 static UIImage *checkImage;
60 static UIImage *uncheckedImage;
62 static BOOL settingsChanged;
63 static NSMutableDictionary *_settings;
64 static NSString *_plist;
66 /* [NSObject yieldToSelector:(withObject:)] {{{*/
67 @interface NSObject (wb$yieldToSelector)
68 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object;
69 - (id) wb$yieldToSelector:(SEL)selector;
72 @implementation NSObject (Cydia)
74 - (void) wb$doNothing {
77 - (void) wb$_yieldToContext:(NSMutableArray *)context {
78 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
80 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
81 id object([[context objectAtIndex:1] nonretainedObjectValue]);
82 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
84 /* XXX: deal with exceptions */
85 id value([self performSelector:selector withObject:object]);
87 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
88 [context removeAllObjects];
89 if ([signature methodReturnLength] != 0 && value != nil)
90 [context addObject:value];
95 performSelectorOnMainThread:@selector(wb$doNothing)
103 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object {
104 /*return [self performSelector:selector withObject:object];*/
106 volatile bool stopped(false);
108 NSMutableArray *context([NSMutableArray arrayWithObjects:
109 [NSValue valueWithPointer:selector],
110 [NSValue valueWithNonretainedObject:object],
111 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
114 NSThread *thread([[[NSThread alloc]
116 selector:@selector(wb$_yieldToContext:)
122 NSRunLoop *loop([NSRunLoop currentRunLoop]);
123 NSDate *future([NSDate distantFuture]);
125 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
127 return [context count] == 0 ? nil : [context objectAtIndex:0];
130 - (id) wb$yieldToSelector:(SEL)selector {
131 return [self wb$yieldToSelector:selector withObject:nil];
137 /* Theme Settings Controller {{{ */
138 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
139 UITableView *_tableView;
140 NSMutableArray *_themes;
143 @property (nonatomic, retain) NSMutableArray *themes;
147 - (id) initForContentSize:(CGSize)size;
149 - (id) navigationTitle;
150 - (void) themesChanged;
152 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
153 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
154 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
155 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
156 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
157 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
158 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
159 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
160 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
164 @implementation WBSThemesController
166 @synthesize themes = _themes;
169 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
170 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
171 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
175 - (id) initForContentSize:(CGSize)size {
176 if ((self = [super initForContentSize:size]) != nil) {
177 self.themes = [_settings objectForKey:@"Themes"];
179 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
180 self.themes = [NSMutableArray arrayWithObject:
181 [NSMutableDictionary dictionaryWithObjectsAndKeys:
183 [NSNumber numberWithBool:YES], @"Active", nil]];
184 [_settings removeObjectForKey:@"Theme"];
187 self.themes = [NSMutableArray array];
188 [_settings setObject:_themes forKey:@"Themes"];
191 NSMutableArray *themesOnDisk([NSMutableArray array]);
194 addObjectsFromArray:[[NSFileManager defaultManager]
195 contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]
198 [themesOnDisk addObjectsFromArray:[[NSFileManager defaultManager]
199 contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]
203 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
204 NSString *theme = [themesOnDisk objectAtIndex:i];
205 if ([theme hasSuffix:@".theme"])
206 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
209 NSMutableSet *themesSet([NSMutableSet set]);
211 for (int i = 0, count = [_themes count]; i < count; i++) {
212 NSDictionary *theme([_themes objectAtIndex:i]);
213 NSString *name([theme objectForKey:@"Name"]);
215 if (!name || ![themesOnDisk containsObject:name]) {
216 [_themes removeObjectAtIndex:i];
220 [themesSet addObject:name];
224 for (NSString *theme in themesOnDisk) {
225 if ([themesSet containsObject:theme])
227 [themesSet addObject:theme];
229 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
231 [NSNumber numberWithBool:NO], @"Active",
235 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
236 [_tableView setDataSource:self];
237 [_tableView setDelegate:self];
238 [_tableView setEditing:YES];
239 [_tableView setAllowsSelectionDuringEditing:YES];
240 if ([self respondsToSelector:@selector(setView:)])
241 [self setView:_tableView];
247 [_tableView release];
252 - (id) navigationTitle {
260 - (void) themesChanged {
261 settingsChanged = YES;
264 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
265 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
269 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
273 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
274 return _themes.count;
277 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
278 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
280 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
281 //[cell setTableViewStyle:UITableViewCellStyleDefault];
284 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
285 cell.text = [theme objectForKey:@"Name"];
286 cell.hidesAccessoryWhenEditing = NO;
287 NSNumber *active([theme objectForKey:@"Active"]);
288 BOOL inactive(active == nil || ![active boolValue]);
289 [cell setImage:(inactive ? uncheckedImage : checkImage)];
293 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
294 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
295 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
296 NSNumber *active = [theme objectForKey:@"Active"];
297 BOOL inactive = active == nil || ![active boolValue];
298 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
299 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
300 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
301 [self themesChanged];
304 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
305 NSUInteger fromIndex = [fromIndexPath row];
306 NSUInteger toIndex = [toIndexPath row];
307 if (fromIndex == toIndex)
309 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
310 [_themes removeObjectAtIndex:fromIndex];
311 [_themes insertObject:theme atIndex:toIndex];
312 [self themesChanged];
315 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
316 return UITableViewCellEditingStyleNone;
319 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
323 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
330 @interface WBSettingsController: PSListController {
333 - (id) initForContentSize:(CGSize)size;
336 - (void) navigationBarButtonClicked:(int)buttonIndex;
337 - (void) viewWillRedisplay;
338 - (void) pushController:(id)controller;
340 - (void) settingsChanged;
341 - (NSString *) title;
342 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
343 - (id) readPreferenceValue:(PSSpecifier *)spec;
347 @implementation WBSettingsController
350 void *libhide(dlopen("/usr/lib/hide.dylib", RTLD_LAZY));
351 IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId"));
352 HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId"));
353 UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId"));
356 - (id) initForContentSize:(CGSize)size {
357 if ((self = [super initForContentSize:size]) != nil) {
358 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
359 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
361 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
371 - (void) __optimizeThemes {
372 system("/usr/libexec/winterboard/Optimize");
375 - (void) optimizeThemes {
376 UIActionSheet *sheet([[[UIActionSheet alloc]
377 initWithTitle:@"Optimize Themes"
378 buttons:[NSArray arrayWithObjects:@"Optimize", @"Cancel", nil]
384 [sheet setBodyText:@"Please note that this setting /replaces/ the PNG files that came with the theme. PNG files that have been iPhone-optimized cannot be viewed on a normal computer unless they are first deoptimized. You can use Cydia to reinstall themes that have been optimized in order to revert to the original PNG files."];
385 [sheet setNumberOfRows:1];
386 [sheet setDestructiveButtonIndex:0];
387 [sheet popupAlertAnimated:YES];
390 - (void) _optimizeThemes {
391 UIView *view([self view]);
392 UIWindow *window([view window]);
394 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
395 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
397 [window setUserInteractionEnabled:NO];
399 [window addSubview:hud];
401 [self wb$yieldToSelector:@selector(__optimizeThemes)];
402 [hud removeFromSuperview];
404 [window setUserInteractionEnabled:YES];
406 [self settingsChanged];
409 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
410 NSString *context([sheet context]);
412 if ([context isEqualToString:@"optimize"]) {
415 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
421 [super alertSheet:sheet buttonClicked:button];
426 if (!settingsChanged)
429 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
432 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
435 ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
437 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
438 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
439 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
440 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
442 unlink("/User/Library/Cache/com.apple.SpringBoard.folderSwitcherLinen");
443 unlink("/User/Library/Cache/com.apple.SpringBoard.notificationCenterLinen");
445 unlink("/User/Library/Cache/com.apple.SpringBoard.folderSwitcherLinen.0");
446 unlink("/User/Library/Cache/com.apple.SpringBoard.folderSwitcherLinen.1");
447 unlink("/User/Library/Cache/com.apple.SpringBoard.folderSwitcherLinen.2");
448 unlink("/User/Library/Cache/com.apple.SpringBoard.folderSwitcherLinen.3");
450 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
451 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
452 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
453 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
455 system("killall lsd SpringBoard");
458 - (void) cancelChanges {
461 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
462 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
464 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
465 [self reloadSpecifiers];
466 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
467 [[self navigationItem] setLeftBarButtonItem:nil];
468 [[self navigationItem] setRightBarButtonItem:nil];
470 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
472 settingsChanged = NO;
475 - (void) navigationBarButtonClicked:(int)buttonIndex {
476 if (!settingsChanged) {
477 [super navigationBarButtonClicked:buttonIndex];
481 if (buttonIndex == 0) {
482 [self cancelChanges];
487 [self.rootController popController];
490 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
491 [self navigationBarButtonClicked:button.tag];
494 - (void) viewWillRedisplay {
496 [self settingsChanged];
497 [super viewWillRedisplay];
500 - (void) viewWillAppear:(BOOL)animated {
502 [self settingsChanged];
503 if ([super respondsToSelector:@selector(viewWillAppear:)])
504 [super viewWillAppear:animated];
507 - (void) pushController:(id)controller {
508 [self hideNavigationBarButtons];
509 [super pushController:controller];
514 _specifiers = [[self loadSpecifiersFromPlistName:@"WinterBoard" target:self] retain];
518 - (void) settingsChanged {
519 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
520 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
521 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
522 cancelButton.tag = 0;
523 respringButton.tag = 1;
524 [[self navigationItem] setLeftBarButtonItem:respringButton];
525 [[self navigationItem] setRightBarButtonItem:cancelButton];
526 [respringButton release];
527 [cancelButton release];
529 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
531 settingsChanged = YES;
534 - (NSString *) title {
535 return @"WinterBoard";
538 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
539 NSString *key([spec propertyForKey:@"key"]);
540 if ([[spec propertyForKey:@"negate"] boolValue])
541 value = [NSNumber numberWithBool:(![value boolValue])];
542 [_settings setValue:value forKey:key];
543 [self settingsChanged];
546 - (id) readPreferenceValue:(PSSpecifier *)spec {
547 NSString *key([spec propertyForKey:@"key"]);
548 id defaultValue([spec propertyForKey:@"default"]);
549 id plistValue([_settings objectForKey:key]);
552 if ([[spec propertyForKey:@"negate"] boolValue])
553 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
559 #define WBSAddMethod(_class, _sel, _imp, _type) \
560 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
561 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
562 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
563 [self popViewControllerAnimated:YES];
566 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
569 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
573 static __attribute__((constructor)) void __wbsInit() {
574 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
575 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
576 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");