]> git.saurik.com Git - cydget.git/blobdiff - CydgetSettings.mm
Support application URL schemes.
[cydget.git] / CydgetSettings.mm
index dbe5ab74fef996f801c8add6d67be439edbc7310..7726b4f03cc1adadc3bd788c2e6cbcac5f5e554d 100644 (file)
@@ -1,5 +1,5 @@
-/* CydgetScript - open-source IntelliDial replacement
- * Copyright (C) 2009  Jay Freeman (saurik)
+/* CydgetScript - open-source lock screen multiplexer
+ * Copyright (C) 2009-2011  Jay Freeman (saurik)
 */
 
 /*
 
 #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 *);
+
 extern NSString *PSTableCellKey;
 extern "C" UIImage *_UIImageWithName(NSString *);
 
@@ -52,6 +61,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 CydgetOrderController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
     UITableView *_tableView;
@@ -95,9 +175,9 @@ static NSString *_plist;
         self.themes = [_settings objectForKey:@"LockCydgets"];
         if (!_themes) {
             self.themes = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:
-                @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
+                @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
             ], [NSMutableDictionary dictionaryWithObjectsAndKeys:
-                @"CydgetCentral", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
+                @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
             ], nil];
 
             [_settings setObject:_themes forKey:@"LockCydgets"];
@@ -139,7 +219,8 @@ static NSString *_plist;
         [_tableView setDelegate:self];
         [_tableView setEditing:YES];
         [_tableView setAllowsSelectionDuringEditing:YES];
-        [self showLeftButton:@"Cydget" withStyle:1 rightButton:nil withStyle:0];
+        if ([self respondsToSelector:@selector(setView:)])
+            [self setView:_tableView];
     }
     return self;
 }
@@ -269,36 +350,57 @@ static NSString *_plist;
         return;
     if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
         return;
-
-    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");
-    // XXX: recursively delete directory!!
-    unlink("/User/Library/Caches/SpringBoardIconCache");
-    unlink("/User/Library/Caches/SpringBoardIconCache-small");
     system("killall SpringBoard");
 }
 
+- (void) cancelChanges {
+    [_settings release];
+    [_plist release];
+    _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
+    _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
+
+    [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 {
     if (!settingsChanged) {
         [super navigationBarButtonClicked:buttonIndex];
         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];
@@ -311,7 +413,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;
 }
 
@@ -320,9 +433,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];
 }
 
@@ -338,3 +452,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}");
+}