]> git.saurik.com Git - winterboard.git/blobdiff - Settings.mm
In the last couple decades I learned BSD is wrong.
[winterboard.git] / Settings.mm
index 38aa3c3a1bb402ffdcb87fe14dcec44b4d06932a..614a0b7befe37898b7c41638f5ec09faf2c819ad 100644 (file)
@@ -1,47 +1,42 @@
 /* WinterBoard - Theme Manager for the iPhone
- * Copyright (C) 2009  Jay Freeman (saurik)
+ * Copyright (C) 2008-2014  Jay Freeman (saurik)
 */
 
+/* GNU Lesser General Public License, Version 3 {{{ */
 /*
- *        Redistribution and use in source and binary
- * forms, with or without modification, are permitted
- * provided that the following conditions are met:
+ * WinterBoard is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
  *
- * 1. Redistributions of source code must retain the
- *    above copyright notice, this list of conditions
- *    and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the
- *    above copyright notice, this list of conditions
- *    and the following disclaimer in the documentation
- *    and/or other materials provided with the
- *    distribution.
- * 3. The name of the author may not be used to endorse
- *    or promote products derived from this software
- *    without specific prior written permission.
+ * WinterBoard is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
  *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
- * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
- * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with WinterBoard.  If not, see <http://www.gnu.org/licenses/>.
+**/
+/* }}} */
 
 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
+#import <Preferences/PSRootController.h>
+#import <Preferences/PSViewController.h>
 #import <Preferences/PSListController.h>
 #import <Preferences/PSSpecifier.h>
 #import <Preferences/PSTableCell.h>
 #import <UIKit/UINavigationButton.h>
 
+#include <dlfcn.h>
+#include <objc/runtime.h>
+
+static BOOL (*IsIconHiddenDisplayId)(NSString *);
+static BOOL (*HideIconViaDisplayId)(NSString *);
+static BOOL (*UnHideIconViaDisplayId)(NSString *);
+
+static NSString *WinterBoardDisplayID = @"com.saurik.WinterBoard";
+
 extern NSString *PSTableCellKey;
 extern "C" UIImage *_UIImageWithName(NSString *);
 
@@ -52,6 +47,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<SEL>([[context objectAtIndex:0] pointerValue]));
+    id object([[context objectAtIndex:1] nonretainedObjectValue]);
+    volatile bool &stopped(*reinterpret_cast<bool *>([[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<bool *>(&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 <UITableViewDelegate, UITableViewDataSource> {
     UITableView *_tableView;
@@ -76,6 +142,7 @@ static NSString *_plist;
 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
+
 @end
 
 @implementation WBSThemesController
@@ -154,7 +221,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;
 }
@@ -243,6 +311,98 @@ static NSString *_plist;
 @end
 /* }}} */
 
+@interface WBAdvancedController: PSListController {
+}
+
+- (id) specifiers;
+- (void) settingsChanged;
+
+@end
+
+@implementation WBAdvancedController
+
+- (id) specifiers {
+    if (!_specifiers)
+        _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
+    return _specifiers;
+}
+
+- (void) settingsChanged {
+    settingsChanged = YES;
+}
+
+- (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
+    NSString *key([spec propertyForKey:@"key"]);
+    if ([[spec propertyForKey:@"negate"] boolValue])
+        value = [NSNumber numberWithBool:(![value boolValue])];
+    [_settings setValue:value forKey:key];
+    [self settingsChanged];
+}
+
+- (id) readPreferenceValue:(PSSpecifier *)spec {
+    NSString *key([spec propertyForKey:@"key"]);
+    id defaultValue([spec propertyForKey:@"default"]);
+    id plistValue([_settings objectForKey:key]);
+    if (!plistValue)
+        return defaultValue;
+    if ([[spec propertyForKey:@"negate"] boolValue])
+        plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
+    return plistValue;
+}
+
+- (void) __optimizeThemes {
+    system("/usr/libexec/winterboard/Optimize");
+}
+
+- (void) optimizeThemes {
+    UIAlertView *alert([[[UIAlertView alloc]
+        initWithTitle:@"Optimize Themes"
+        message:@"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."
+        delegate:self
+        cancelButtonTitle:@"Cancel"
+        otherButtonTitles:@"Optimize", nil
+    ] autorelease]);
+
+    [alert setContext:@"optimize"];
+    [alert setNumberOfRows:1];
+    [alert show];
+}
+
+- (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) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
+    NSString *context([alert context]);
+
+    if ([context isEqualToString:@"optimize"]) {
+        if (button == [alert firstOtherButtonIndex]) {
+            [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
+        }
+
+        [alert dismissWithClickedButtonIndex:-1 animated:YES];
+    }
+    /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
+        [super alertView:alert clickedButtonAtIndex:button];*/
+}
+
+@end
+
 @interface WBSettingsController: PSListController {
 }
 
@@ -262,10 +422,36 @@ static NSString *_plist;
 
 @implementation WBSettingsController
 
++ (void) load {
+    void *libhide(dlopen("/usr/lib/hide.dylib", RTLD_LAZY));
+    IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId"));
+    HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId"));
+    UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId"));
+}
+
+- (void) _wb$loadSettings {
+    _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
+    _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
+
+    bool set;
+    if (_settings != nil)
+        set = true;
+    else {
+        set = false;
+        _settings = [NSMutableDictionary dictionary];
+    }
+
+    _settings = [_settings retain];
+
+    if ([_settings objectForKey:@"SummerBoard"] == nil)
+        [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
+
+    [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
+}
+
 - (id) initForContentSize:(CGSize)size {
     if ((self = [super initForContentSize:size]) != nil) {
-        _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
-        _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
+        [self _wb$loadSettings];
     } return self;
 }
 
@@ -285,11 +471,49 @@ static NSString *_plist;
     if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
         return;
 
+    ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
+
     unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
     unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
     unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
     unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
-    system("killall SpringBoard");
+
+    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 -9 lsd");
+
+    if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
+        system("killall backboardd");
+    else
+        system("killall SpringBoard");
+}
+
+- (void) cancelChanges {
+    [_settings release];
+    [_plist release];
+
+    [self _wb$loadSettings];
+
+    [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 {
@@ -298,19 +522,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];
@@ -323,7 +560,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;
 }
 
@@ -332,9 +580,10 @@ static NSString *_plist;
 }
 
 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
+    NSString *key([spec propertyForKey:@"key"]);
     if ([[spec propertyForKey:@"negate"] boolValue])
         value = [NSNumber numberWithBool:(![value boolValue])];
-    [_settings setValue:value forKey:[spec propertyForKey:@"key"]];
+    [_settings setValue:value forKey:key];
     [self settingsChanged];
 }
 
@@ -350,3 +599,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}");
+}