]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
DRY refactorization with silly abstraction for {request,URL}WithURL.
[cydia.git] / UICaboodle / BrowserView.mm
index d5496403674f386fc327e2ed969bada9ce108388..201e85415c385f7e4db48ea67eb0dc291fbd2d8f 100644 (file)
@@ -480,16 +480,6 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if (challenge_ != nil)
         [challenge_ release];
 
-    if (request_ != nil)
-        [request_ release];
-
-    if (custom_ != nil)
-        [custom_ release];
-    if (style_ != nil)
-        [style_ release];
-
-    if (function_ != nil)
-        [function_ release];
     if (closer_ != nil)
         [closer_ release];
 
@@ -508,22 +498,25 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [super dealloc];
 }
 
-- (void) setURL:(NSURL *)url {
-    _assert(request_ == nil);
+- (NSURL *) URLWithURL:(NSURL *)url {
+    return url;
+}
 
-    request_ = [[NSURLRequest
-        requestWithURL:url
-        cachePolicy:NSURLRequestUseProtocolCachePolicy
+- (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
+    return [NSURLRequest
+        requestWithURL:[self URLWithURL:url]
+        cachePolicy:policy
         timeoutInterval:DefaultTimeout_
-    ] retain];
+    ];
+}
+
+- (void) setURL:(NSURL *)url {
+    _assert(request_ == nil);
+    request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
 }
 
 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
-    [self loadRequest:[NSURLRequest
-        requestWithURL:url
-        cachePolicy:policy
-        timeoutInterval:DefaultTimeout_
-    ]];
+    [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
 }
 
 - (void) loadURL:(NSURL *)url {
@@ -548,8 +541,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     NSMutableURLRequest *request([request_ mutableCopy]);
     [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
 
-    [request_ autorelease];
-    request_ = [request retain];
+    request_ = request;
 
     if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
         [self loadRequest:request_];
@@ -579,52 +571,23 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 }
 
 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
-    if (custom_ != nil)
-        [custom_ autorelease];
-    if (button == nil)
-        custom_ = nil;
-    else
-        custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
-
-    if (style_ != nil)
-        [style_ autorelease];
-    if (style == nil)
-        style_ = nil;
-    else
-        style_ = [style retain];
-
-    if (function_ != nil)
-        [function_ autorelease];
-    if (function == nil)
-        function_ = nil;
-    else
-        function_ = [function retain];
+    custom_ = button;
+    style_ = style;
+    function_ = function;
 
     [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
-    if (custom_ != nil)
-        [custom_ autorelease];
-    if (button == nil)
-        custom_ = nil;
-    else
-        custom_ = [button retain];
+    custom_ = button;
+    style_ = style;
+    function_ = function;
 
-    if (style_ != nil)
-        [style_ autorelease];
-    if (style == nil)
-        style_ = nil;
-    else
-        style_ = [style retain];
-
-    if (function_ != nil)
-        [function_ autorelease];
-    if (function == nil)
-        function_ = nil;
-    else
-        function_ = [function retain];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
+}
 
+- (void) removeButton {
+    custom_ = [NSNull null];
     [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
@@ -637,6 +600,17 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         closer_ = [function retain];
 }
 
+- (void) scrollToBottomAnimated:(NSNumber *)animated {
+    CGSize size([scroller_ contentSize]);
+    CGPoint offset([scroller_ contentOffset]);
+    CGRect frame([scroller_ frame]);
+
+    if (size.height - offset.y < frame.size.height + 20.f) {
+        CGRect rect = {{0, size.height-1}, {size.width, 1}};
+        [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
+    }
+}
+
 - (void) _setViewportWidth {
     [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
 }
@@ -646,6 +620,14 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [self _setViewportWidth];
 }
 
+- (void) _setViewportWidthOnMainThread:(NSNumber *)width {
+    [self setViewportWidth:[width floatValue]];
+}
+
+- (void) setViewportWidthOnMainThread:(float)width {
+    [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
+}
+
 - (void) webViewUpdateViewSettings:(UIWebView *)view {
     [self _setViewportWidth];
 }
@@ -658,6 +640,24 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     return true;
 }
 
+- (bool) allowsNavigationAction {
+    return allowsNavigationAction_;
+}
+
+- (void) setAllowsNavigationAction:(bool)value {
+    allowsNavigationAction_ = value;
+}
+
+- (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
+    [self setAllowsNavigationAction:[value boolValue]];
+}
+
+- (void) popViewControllerWithNumber:(NSNumber *)value {
+    UINavigationController *navigation([self navigationController]);
+    if ([navigation topViewController] == self)
+        [navigation popViewControllerAnimated:[value boolValue]];
+}
+
 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
     [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
     [self _didFinishLoading];
@@ -675,45 +675,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     }
 }
 
-// CYWebViewDelegate {{{
-- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
-#if LogMessages
-    NSLog(@"addMessageToConsole:%@", message);
-#endif
-}
-
-- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
-#if LogBrowser
-    NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
-#endif
-
-    if ([frame parentFrame] == nil) {
-        if (!error_) {
-            if (request_ != nil)
-                [request_ autorelease];
-            if (request == nil)
-                request_ = nil;
-            else
-                request_ = [request retain];
-        }
-    }
-}
-
-- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
-#if LogBrowser
-    NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
-#endif
-
+- (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
     NSURL *url([request URL]);
-    if (url == nil)
-        return;
-
-    if ([frame isEqualToString:@"_open"])
-        [delegate_ openURL:url];
-
-    NSString *scheme([[url scheme] lowercaseString]);
-    if ([scheme isEqualToString:@"mailto"])
-        [self _openMailToURL:url];
 
     // XXX: filter to internal usage?
     CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
@@ -726,7 +689,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
     [page setDelegate:delegate_];
 
-    if (![frame isEqualToString:@"_popup"]) {
+    if (!pop) {
         [[self navigationItem] setTitle:title_];
 
         [[self navigationController] pushViewController:page animated:YES];
@@ -745,6 +708,53 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
         [[self navigationController] presentModalViewController:navigation animated:YES];
     }
+}
+
+// CYWebViewDelegate {{{
+- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
+#if LogMessages
+    NSLog(@"addMessageToConsole:%@", message);
+#endif
+}
+
+- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+#if LogBrowser
+    NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
+#endif
+
+    if ([frame parentFrame] == nil) {
+        if (!error_) {
+            NSURL *url(request == nil ? nil : [request URL]);
+
+            if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
+                request_ = request;
+            else {
+                if (url != nil)
+                    [self pushRequest:request asPop:NO];
+                [listener ignore];
+            }
+        }
+    }
+}
+
+- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+#if LogBrowser
+    NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
+#endif
+
+    NSURL *url([request URL]);
+    if (url == nil)
+        return;
+
+    if ([frame isEqualToString:@"_open"])
+        [delegate_ openURL:url];
+    else {
+        NSString *scheme([[url scheme] lowercaseString]);
+        if ([scheme isEqualToString:@"mailto"])
+            [self _openMailToURL:url];
+        else
+            [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
+    }
 
     [listener ignore];
 }
@@ -777,7 +787,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                 for (DOMHTMLBodyElement *body in (id) bodies) {
                     DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
 
-                    bool colored(false);
+                    UIColor *uic(nil);
 
                     if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
                         if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
@@ -788,10 +798,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                             float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
                             float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
 
-                            UIColor *uic(nil);
-
                             if (red == 0xc7 && green == 0xce && blue == 0xd5)
-                                uic = [UIColor groupTableViewBackgroundColor];
+                                uic = [UIColor pinStripeColor];
                             else if (alpha != 0)
                                 uic = [UIColor
                                     colorWithRed:(red / 255)
@@ -799,16 +807,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                                     blue:(blue / 255)
                                     alpha:alpha
                                 ];
-
-                            if (uic != nil) {
-                                colored = true;
-                                [scroller_ setBackgroundColor:uic];
-                            }
                         }
                     }
 
-                    if (!colored)
-                        [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
+                    [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
                     break;
                 }
     }
@@ -832,11 +834,13 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
     if ([frame parentFrame] == nil) {
         CYRelease(title_);
-        CYRelease(custom_);
-        CYRelease(style_);
-        CYRelease(function_);
+        custom_ = nil;
+        style_ = nil;
+        function_ = nil;
         CYRelease(closer_);
 
+        [self setHidesNavigationBar:NO];
+
         // XXX: do we still need to do this?
         [[self navigationItem] setTitle:nil];
     }
@@ -936,8 +940,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 }
 
 - (UIBarButtonItem *) customButton {
-    return [[[UIBarButtonItem alloc]
-        initWithTitle:custom_
+    return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
+        initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
         style:[self rightButtonStyle]
         target:self
         action:@selector(customButtonClicked)
@@ -1008,12 +1012,16 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
 - (id) initWithWidth:(float)width ofClass:(Class)_class {
     if ((self = [super init]) != nil) {
+        allowsNavigationAction_ = true;
+
         class_ = _class;
         loading_ = [[NSMutableSet alloc] initWithCapacity:5];
 
         indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
 
-        webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
+        CGRect bounds([[self view] bounds]);
+
+        webview_ = [[[CYWebView alloc] initWithFrame:bounds] autorelease];
         [webview_ setDelegate:self];
         [self setView:webview_];
 
@@ -1080,7 +1088,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         }
 
         [scroller_ setFixedBackgroundPattern:YES];
-        [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
+        [scroller_ setBackgroundColor:[UIColor clearColor]];
         [scroller_ setClipsSubviews:YES];
 
         [scroller_ setBounces:YES];
@@ -1106,6 +1114,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
         [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
 
+        UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
+        [webview_ insertSubview:table atIndex:0];
+
+        [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
     } return self;
@@ -1187,6 +1199,28 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     return 980;
 }
 
+- (void) setNavigationBarStyle:(NSString *)name {
+    UIBarStyle style;
+    if ([name isEqualToString:@"Black"])
+        style = UIBarStyleBlack;
+    else
+        style = UIBarStyleDefault;
+
+    [[[self navigationController] navigationBar] setBarStyle:style];
+}
+
+- (void) setNavigationBarTintColor:(UIColor *)color {
+    [[[self navigationController] navigationBar] setTintColor:color];
+}
+
+- (void) setHidesBackButton:(bool)value {
+    [[self navigationItem] setHidesBackButton:value];
+}
+
+- (void) setHidesBackButtonByNumber:(NSNumber *)value {
+    [self setHidesBackButton:[value boolValue]];
+}
+
 - (void) dispatchEvent:(NSString *)event {
     WebThreadLocked lock;
 
@@ -1210,24 +1244,54 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     }
 }
 
+- (bool) hidesNavigationBar {
+    return hidesNavigationBar_;
+}
+
+- (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
+    if (visible_)
+        [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
+}
+
+- (void) setHidesNavigationBar:(bool)value {
+    if (hidesNavigationBar_ != value) {
+        hidesNavigationBar_ = value;
+        [self _setHidesNavigationBar:YES animated:YES];
+    }
+}
+
+- (void) setHidesNavigationBarByNumber:(NSNumber *)value {
+    [self setHidesNavigationBar:[value boolValue]];
+}
+
 - (void) viewWillAppear:(BOOL)animated {
+    visible_ = true;
+
+    if ([self hidesNavigationBar])
+        [self _setHidesNavigationBar:YES animated:animated];
+
     [self dispatchEvent:@"CydiaViewWillAppear"];
     [super viewWillAppear:animated];
 }
 
 - (void) viewDidAppear:(BOOL)animated {
-    [self dispatchEvent:@"CydiaViewDidAppear"];
     [super viewDidAppear:animated];
+    [self dispatchEvent:@"CydiaViewDidAppear"];
 }
 
 - (void) viewWillDisappear:(BOOL)animated {
     [self dispatchEvent:@"CydiaViewWillDisappear"];
     [super viewWillDisappear:animated];
+
+    if ([self hidesNavigationBar])
+        [self _setHidesNavigationBar:NO animated:animated];
+
+    visible_ = false;
 }
 
 - (void) viewDidDisappear:(BOOL)animated {
-    [self dispatchEvent:@"CydiaViewDidDisappear"];
     [super viewDidDisappear:animated];
+    [self dispatchEvent:@"CydiaViewDidDisappear"];
 }
 
 @end