X-Git-Url: https://git.saurik.com/winterboard.git/blobdiff_plain/f22a89893de5b47d242318375498ca9f02b6eba8..24ba43eea737d1c6345d6f6a46169a90a9265e0c:/Application.mm diff --git a/Application.mm b/Application.mm index 36dc111..cd383d3 100644 --- a/Application.mm +++ b/Application.mm @@ -1,5 +1,5 @@ /* WinterBoard - Theme Manager for the iPhone - * Copyright (C) 2008 Jay Freeman (saurik) + * Copyright (C) 2008-2009 Jay Freeman (saurik) */ /* @@ -39,172 +39,79 @@ #import <CoreGraphics/CGGeometry.h> #import <UIKit/UIKit.h> -#define _trace() NSLog(@"WE:_trace(%u)", __LINE__); +#import <Preferences/PSRootController.h> +#import <Preferences/PSViewController.h> +#import <Preferences/PSSpecifier.h> -static NSString *plist_; -static NSMutableDictionary *settings_; -static BOOL changed_; +@interface UIApplication (Private) +- (void) terminateWithSuccess; +@end -@interface WBThemeTableViewCell : UITableViewCell { - UILabel *label; +@interface WBRootController : PSRootController { + PSViewController *_firstViewController; } -@end - -@implementation WBThemeTableViewCell +@property (readonly) PSViewController *firstViewController; +- (void) setupRootListForSize:(CGSize)size; +- (BOOL) popController; @end -@interface WBApplication : UIApplication < - UITableViewDataSource, - UITableViewDelegate -> { - UIWindow *window_; - UITableView *themesTable_; - NSMutableArray *themesArray_; -} +@implementation WBRootController -@end +@synthesize firstViewController = _firstViewController; -@implementation WBApplication +- (void) setupRootListForSize:(CGSize)size { + PSSpecifier *spec = [[PSSpecifier alloc] init]; + [spec setTarget:self]; + spec.name = @"WinterBoard"; -- (void) dealloc { - [window_ release]; - [themesTable_ release]; - [themesArray_ release]; - [super dealloc]; -} + NSBundle *wbSettingsBundle = [NSBundle bundleWithPath:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle"]; + [wbSettingsBundle load]; -- (void) applicationWillTerminate:(UIApplication *)application { - if (changed_) { - if (![settings_ writeToFile:plist_ atomically:YES]) - NSLog(@"WB:Error:writeToFile"); - system("killall SpringBoard"); - } -} + _firstViewController = [[[wbSettingsBundle principalClass] alloc] initForContentSize:size]; + _firstViewController.rootController = self; + _firstViewController.parentController = self; + [_firstViewController viewWillBecomeVisible:spec]; -- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease]; - NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]]; - cell.text = [theme objectForKey:@"Name"]; - cell.hidesAccessoryWhenEditing = NO; - NSNumber *active = [theme objectForKey:@"Active"]; - BOOL inactive = active == nil || ![active boolValue]; - cell.accessoryType = inactive ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark; - return cell; -} + [spec release]; -- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; - NSMutableDictionary *theme = [themesArray_ objectAtIndex:[indexPath row]]; - NSNumber *active = [theme objectForKey:@"Active"]; - BOOL inactive = active == nil || ![active boolValue]; - [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"]; - cell.accessoryType = inactive ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; - [themesTable_ deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES]; - changed_ = YES; + [self pushController:_firstViewController]; } -- (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { - return [themesArray_ count]; +- (BOOL) popController { + // Pop the last controller = exit the application. + // The only time the last controller should pop is when the user taps Respring/Cancel. + // Which only gets displayed if the user has made changes. + if([self lastController] == _firstViewController) + [[UIApplication sharedApplication] terminateWithSuccess]; + return [super popController]; } +@end -- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { - return UITableViewCellEditingStyleNone; +@interface WBApplication : UIApplication { + WBRootController *_rootController; } -- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { - NSUInteger fromIndex = [fromIndexPath row]; - NSUInteger toIndex = [toIndexPath row]; - if (fromIndex == toIndex) - return; - NSMutableDictionary *theme = [[[themesArray_ objectAtIndex:fromIndex] retain] autorelease]; - [themesArray_ removeObjectAtIndex:fromIndex]; - [themesArray_ insertObject:theme atIndex:toIndex]; - changed_ = YES; +@end + +@implementation WBApplication + +- (void) dealloc { + [_rootController release]; + [super dealloc]; } -- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { - return YES; +- (void) applicationWillTerminate:(UIApplication *)application { + [_rootController.firstViewController suspend]; } - (void) applicationDidFinishLaunching:(id)unused { - window_ = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; - [window_ makeKeyAndVisible]; - - plist_ = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", - NSHomeDirectory() - ] retain]; - - settings_ = [[NSMutableDictionary alloc] initWithContentsOfFile:plist_]; - if (settings_ == nil) - settings_ = [[NSMutableDictionary alloc] initWithCapacity:16]; - - themesArray_ = [settings_ objectForKey:@"Themes"]; - if (themesArray_ == nil) { - if (NSString *theme = [settings_ objectForKey:@"Theme"]) { - themesArray_ = [[NSArray arrayWithObject:[[NSDictionary dictionaryWithObjectsAndKeys: - theme, @"Name", - [NSNumber numberWithBool:YES], @"Active", - nil] mutableCopy]] mutableCopy]; - - [settings_ removeObjectForKey:@"Theme"]; - } - - if (themesArray_ == nil) - themesArray_ = [NSMutableArray arrayWithCapacity:16]; - [settings_ setObject:themesArray_ forKey:@"Themes"]; - } - - themesArray_ = [themesArray_ retain]; - - NSMutableSet *themesSet = [NSMutableSet setWithCapacity:32]; - for (NSMutableDictionary *theme in themesArray_) - if (NSString *name = [theme objectForKey:@"Name"]) - [themesSet addObject:name]; - - NSFileManager *manager = [NSFileManager defaultManager]; - - NSMutableArray *themes = [NSMutableArray arrayWithCapacity:32]; - [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]]; - [themes addObjectsFromArray:[manager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()] error:NULL]]; - - for (NSUInteger i(0), e([themes count]); i != e; ++i) { - NSString *theme = [themes objectAtIndex:i]; - if ([theme hasSuffix:@".theme"]) - [themes replaceObjectAtIndex:i withObject:[theme substringWithRange:NSMakeRange(0, [theme length] - 6)]]; - } - - for (NSUInteger i(0), e([themesArray_ count]); i != e; ++i) { - NSMutableDictionary *theme = [themesArray_ objectAtIndex:i]; - NSString *name = [theme objectForKey:@"Name"]; - if (name == nil || ![themes containsObject:name]) { - [themesArray_ removeObjectAtIndex:i]; - --i; --e; - } - } - - for (NSString *theme in themes) { - if ([themesSet containsObject:theme]) - continue; - [themesSet addObject:theme]; - [themesArray_ addObject:[[NSDictionary dictionaryWithObjectsAndKeys: - theme, @"Name", - [NSNumber numberWithBool:NO], @"Active", - nil] mutableCopy]]; - } - - themesTable_ = [[UITableView alloc] initWithFrame:window_.bounds]; - [window_ addSubview:themesTable_]; - - [themesTable_ setDataSource:self]; - [themesTable_ setDelegate:self]; - - [themesTable_ setEditing:YES animated:NO]; - themesTable_.allowsSelectionDuringEditing = YES; - - [themesTable_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; + UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; + _rootController = [[WBRootController alloc] initWithTitle:@"WinterBoard" identifier:[[NSBundle mainBundle] bundleIdentifier]]; + [window addSubview:_rootController.contentView]; + [window makeKeyAndVisible]; } @end