X-Git-Url: https://git.saurik.com/winterboard.git/blobdiff_plain/b8fd9f3068ec15e2527cce7957d285f2f2e16ad4..8e699a8292b89f59ae0d4effd986307e16f2379c:/Settings.mm diff --git a/Settings.mm b/Settings.mm index 19fea78..59c35e5 100644 --- a/Settings.mm +++ b/Settings.mm @@ -1,5 +1,5 @@ /* WinterBoard - Theme Manager for the iPhone - * Copyright (C) 2009 Jay Freeman (saurik) + * Copyright (C) 2009-2010 Jay Freeman (saurik) */ /* @@ -37,12 +37,15 @@ #import #import +#import +#import #import #import #import #import #include +#include static BOOL (*IsIconHiddenDisplayId)(NSString *); static BOOL (*HideIconViaDisplayId)(NSString *); @@ -60,6 +63,77 @@ static BOOL settingsChanged; static NSMutableDictionary *_settings; static NSString *_plist; +/* [NSObject yieldToSelector:(withObject:)] {{{*/ +@interface NSObject (wb$yieldToSelector) +- (id) wb$yieldToSelector:(SEL)selector withObject:(id)object; +- (id) wb$yieldToSelector:(SEL)selector; +@end + +@implementation NSObject (Cydia) + +- (void) wb$doNothing { +} + +- (void) wb$_yieldToContext:(NSMutableArray *)context { + NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); + + SEL selector(reinterpret_cast([[context objectAtIndex:0] pointerValue])); + id object([[context objectAtIndex:1] nonretainedObjectValue]); + volatile bool &stopped(*reinterpret_cast([[context objectAtIndex:2] pointerValue])); + + /* XXX: deal with exceptions */ + id value([self performSelector:selector withObject:object]); + + NSMethodSignature *signature([self methodSignatureForSelector:selector]); + [context removeAllObjects]; + if ([signature methodReturnLength] != 0 && value != nil) + [context addObject:value]; + + stopped = true; + + [self + performSelectorOnMainThread:@selector(wb$doNothing) + withObject:nil + waitUntilDone:NO + ]; + + [pool release]; +} + +- (id) wb$yieldToSelector:(SEL)selector withObject:(id)object { + /*return [self performSelector:selector withObject:object];*/ + + volatile bool stopped(false); + + NSMutableArray *context([NSMutableArray arrayWithObjects: + [NSValue valueWithPointer:selector], + [NSValue valueWithNonretainedObject:object], + [NSValue valueWithPointer:const_cast(&stopped)], + nil]); + + NSThread *thread([[[NSThread alloc] + initWithTarget:self + selector:@selector(wb$_yieldToContext:) + object:context + ] autorelease]); + + [thread start]; + + NSRunLoop *loop([NSRunLoop currentRunLoop]); + NSDate *future([NSDate distantFuture]); + + while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); + + return [context count] == 0 ? nil : [context objectAtIndex:0]; +} + +- (id) wb$yieldToSelector:(SEL)selector { + return [self wb$yieldToSelector:selector withObject:nil]; +} + +@end +/* }}} */ + /* Theme Settings Controller {{{ */ @interface WBSThemesController: PSViewController { UITableView *_tableView; @@ -163,7 +237,8 @@ static NSString *_plist; [_tableView setDelegate:self]; [_tableView setEditing:YES]; [_tableView setAllowsSelectionDuringEditing:YES]; - [self showLeftButton:@"WinterBoard" withStyle:1 rightButton:nil withStyle:0]; + if ([self respondsToSelector:@selector(setView:)]) + [self setView:_tableView]; } return self; } @@ -293,6 +368,60 @@ static NSString *_plist; [super dealloc]; } +- (void) __optimizeThemes { + system("/usr/libexec/winterboard/Optimize"); +} + +- (void) optimizeThemes { + UIActionSheet *sheet([[[UIActionSheet alloc] + initWithTitle:@"Optimize Themes" + buttons:[NSArray arrayWithObjects:@"Optimize", @"Cancel", nil] + defaultButtonIndex:1 + delegate:self + context:@"optimize" + ] autorelease]); + + [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."]; + [sheet setNumberOfRows:1]; + [sheet setDestructiveButtonIndex:0]; + [sheet popupAlertAnimated:YES]; +} + +- (void) _optimizeThemes { + UIView *view([self view]); + UIWindow *window([view window]); + + UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]); + [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"]; + + [window setUserInteractionEnabled:NO]; + + [window addSubview:hud]; + [hud show:YES]; + [self wb$yieldToSelector:@selector(__optimizeThemes)]; + [hud removeFromSuperview]; + + [window setUserInteractionEnabled:YES]; + + [self settingsChanged]; +} + +- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button { + NSString *context([sheet context]); + + if ([context isEqualToString:@"optimize"]) { + switch (button) { + case 1: + [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0]; + break; + } + + [sheet dismiss]; + } else + [super alertSheet:sheet buttonClicked:button]; + +} + - (void) suspend { if (!settingsChanged) return; @@ -310,10 +439,38 @@ static NSString *_plist; unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons"); unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist"); + unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen"); + unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen"); + + unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0"); + unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1"); + unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2"); + unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3"); + system("rm -rf /User/Library/Caches/SpringBoardIconCache"); system("rm -rf /User/Library/Caches/SpringBoardIconCache-small"); + system("rm -rf /User/Library/Caches/com.apple.IconsCache"); + system("rm -rf /User/Library/Caches/com.apple.newsstand"); + system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache"); - system("killall SpringBoard"); + system("killall lsd SpringBoard"); +} + +- (void) cancelChanges { + [_settings release]; + [_plist release]; + _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain]; + _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain]; + + [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"]; + [self reloadSpecifiers]; + if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) { + [[self navigationItem] setLeftBarButtonItem:nil]; + [[self navigationItem] setRightBarButtonItem:nil]; + } else { + [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0]; + } + settingsChanged = NO; } - (void) navigationBarButtonClicked:(int)buttonIndex { @@ -322,19 +479,32 @@ static NSString *_plist; return; } - if (buttonIndex == 0) - settingsChanged = NO; + if (buttonIndex == 0) { + [self cancelChanges]; + return; + } [self suspend]; [self.rootController popController]; } +- (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button { + [self navigationBarButtonClicked:button.tag]; +} + - (void) viewWillRedisplay { if (settingsChanged) [self settingsChanged]; [super viewWillRedisplay]; } +- (void) viewWillAppear:(BOOL)animated { + if (settingsChanged) + [self settingsChanged]; + if ([super respondsToSelector:@selector(viewWillAppear:)]) + [super viewWillAppear:animated]; +} + - (void) pushController:(id)controller { [self hideNavigationBarButtons]; [super pushController:controller]; @@ -347,7 +517,18 @@ static NSString *_plist; } - (void) settingsChanged { - [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0]; + if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) { + UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]); + UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]); + cancelButton.tag = 0; + respringButton.tag = 1; + [[self navigationItem] setLeftBarButtonItem:respringButton]; + [[self navigationItem] setRightBarButtonItem:cancelButton]; + [respringButton release]; + [cancelButton release]; + } else { + [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0]; + } settingsChanged = YES; } @@ -375,3 +556,23 @@ static NSString *_plist; } @end + +#define WBSAddMethod(_class, _sel, _imp, _type) \ + if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \ + class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type) +void $PSRootController$popController(PSRootController *self, SEL _cmd) { + [self popViewControllerAnimated:YES]; +} + +void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) { +} + +id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) { + return [self init]; +} + +static __attribute__((constructor)) void __wbsInit() { + WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:"); + WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:"); + WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}"); +}