-- (void) dealloc {
- [packages_ release];
- [super dealloc];
-}
-
-- (id) initWithBook:(RVBook *)book database:(Database *)database {
- if ((self = [super initWithBook:book]) != nil) {
- database_ = database;
-
- packages_ = [[PackageTable alloc]
- initWithBook:book
- database:database
- title:nil
- filter:@selector(isInstalledAndVisible:)
- with:[NSNumber numberWithBool:YES]
- ];
-
- [self addSubview:packages_];
-
- [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
- [packages_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
- } return self;
-}
-
-- (void) resetViewAnimated:(BOOL)animated {
- [packages_ resetViewAnimated:animated];
-}
-
-- (void) reloadData {
- [packages_ reloadData];
-}
-
-- (void) _rightButtonClicked {
- [packages_ setObject:[NSNumber numberWithBool:expert_]];
- [packages_ reloadData];
- expert_ = !expert_;
- [book_ reloadButtonsForPage:self];
-}
-
-- (NSString *) title {
- return @"Installed";
-}
-
-- (NSString *) backButtonTitle {
- return @"Packages";
-}
-
-- (NSString *) rightButtonTitle {
- return Role_ != nil && [Role_ isEqualToString:@"Developer"] ? nil : expert_ ? @"Expert" : @"Simple";
-}
-
-- (UINavigationButtonStyle) rightButtonStyle {
- return expert_ ? UINavigationButtonStyleHighlighted : UINavigationButtonStyleNormal;
-}
-
-- (void) setDelegate:(id)delegate {
- [super setDelegate:delegate];
- [packages_ setDelegate:delegate];
-}
-
-@end
-/* }}} */
-
-/* Home View {{{ */
-@interface HomeView : BrowserView {
-}
-
-@end
-
-@implementation HomeView
-
-- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
- NSString *context([sheet context]);
-
- if ([context isEqualToString:@"about"])
- [sheet dismiss];
- else
- [super alertSheet:sheet buttonClicked:button];
-}
-
-- (void) _leftButtonClicked {
- UIActionSheet *sheet = [[[UIActionSheet alloc]
- initWithTitle:@"About Cydia Installer"
- buttons:[NSArray arrayWithObjects:@"Close", nil]
- defaultButtonIndex:0
- delegate:self
- context:@"about"
- ] autorelease];
-
- [sheet setBodyText:
- @"Copyright (C) 2008\n"
- "Jay Freeman (saurik)\n"
- "saurik@saurik.com\n"
- "http://www.saurik.com/\n"
- "\n"
- "The Okori Group\n"
- "http://www.theokorigroup.com/\n"
- "\n"
- "College of Creative Studies,\n"
- "University of California,\n"
- "Santa Barbara\n"
- "http://www.ccs.ucsb.edu/"
- ];
-
- [sheet popupAlertAnimated:YES];
-}
-
-- (NSString *) leftButtonTitle {
- return @"About";
-}
-
-@end
-/* }}} */
-/* Manage View {{{ */
-@interface ManageView : BrowserView {
-}
-
-@end
-
-@implementation ManageView
-
-- (NSString *) title {
- return @"Manage";
-}
-
-- (void) _leftButtonClicked {
- [delegate_ askForSettings];
-}
-
-- (NSString *) leftButtonTitle {
- return @"Settings";
-}
-
-#if !AlwaysReload
-- (NSString *) _rightButtonTitle {
- return nil;
-}
-#endif
-
-@end
-/* }}} */
-
-@interface WebView (Cydia)
-- (void) setScriptDebugDelegate:(id)delegate;
-- (void) _setFormDelegate:(id)delegate;
-- (void) _setUIKitDelegate:(id)delegate;
-- (void) setWebMailDelegate:(id)delegate;
-- (void) _setLayoutInterval:(float)interval;
-@end
-
-/* Indirect Delegate {{{ */
-@interface IndirectDelegate : NSProxy {
- _transient volatile id delegate_;
-}
-
-- (void) setDelegate:(id)delegate;
-- (id) initWithDelegate:(id)delegate;
-@end
-
-@implementation IndirectDelegate
-
-- (void) setDelegate:(id)delegate {
- delegate_ = delegate;
-}
-
-- (id) initWithDelegate:(id)delegate {
- delegate_ = delegate;
- return self;
-}
-
-- (NSMethodSignature*) methodSignatureForSelector:(SEL)sel {
- if (delegate_ != nil)
- if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
- return sig;
- // XXX: I fucking hate Apple so very very bad
- return [NSMethodSignature signatureWithObjCTypes:"v@:"];
-}
-
-- (void) forwardInvocation:(NSInvocation *)inv {
- SEL sel = [inv selector];
- if (delegate_ != nil && [delegate_ respondsToSelector:sel])
- [inv invokeWithTarget:delegate_];
-}
-
-@end
-/* }}} */
-/* Browser Implementation {{{ */
-@implementation BrowserView
-
-- (void) dealloc {
- if (challenge_ != nil)
- [challenge_ release];
-
- WebView *webview = [webview_ webView];
- [webview setFrameLoadDelegate:nil];
- [webview setResourceLoadDelegate:nil];
- [webview setUIDelegate:nil];
- [webview setScriptDebugDelegate:nil];
- [webview setPolicyDelegate:nil];
-
- [webview setDownloadDelegate:nil];
-
- [webview _setFormDelegate:nil];
- [webview _setUIKitDelegate:nil];
- [webview setWebMailDelegate:nil];
- [webview setEditingDelegate:nil];
-
- [webview_ setDelegate:nil];
- [webview_ setGestureDelegate:nil];
-
- //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
-
- [webview close];
-
-#if RecycleWebViews
- [webview_ removeFromSuperview];
- [Documents_ addObject:[webview_ autorelease]];
-#else
- [webview_ release];
-#endif
-
- [indirect_ setDelegate:nil];
- [indirect_ release];
-
- [scroller_ setDelegate:nil];
-
- [scroller_ release];
- [urls_ release];
- [indicator_ release];
- if (title_ != nil)
- [title_ release];
- [super dealloc];
-}
-
-- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
- [self loadRequest:[NSURLRequest
- requestWithURL:url
- cachePolicy:policy
- timeoutInterval:30.0
- ]];
-}
-
-- (void) loadURL:(NSURL *)url {
- [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
-}
-
-- (NSURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
- NSMutableURLRequest *copy = [request mutableCopy];
-
- if (Machine_ != NULL)
- [copy addValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
- if (UniqueID_ != nil)
- [copy addValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
-
- if (Role_ != nil)
- [copy addValue:Role_ forHTTPHeaderField:@"X-Role"];
-
- return copy;
-}
-
-- (void) loadRequest:(NSURLRequest *)request {
- pushed_ = true;
- [webview_ loadRequest:request];
-}
-
-- (void) reloadURL {
- if ([urls_ count] == 0)
- return;
- NSURL *url = [[[urls_ lastObject] retain] autorelease];
- [urls_ removeLastObject];
- [self loadURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData];
-}
-
-- (WebView *) webView {
- return [webview_ webView];
-}
-
-- (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
- [scroller_ setContentSize:frame.size];
-}
-
-- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
- [self view:sender didSetFrame:frame];
-}
-
-- (void) pushPage:(RVPage *)page {
- [self setBackButtonTitle:title_];
- [page setDelegate:delegate_];
- [book_ pushPage:page];
-}
-
-- (BOOL) getSpecial:(NSURL *)url {
- NSString *href([url absoluteString]);
- NSString *scheme([[url scheme] lowercaseString]);
-
- RVPage *page = nil;
-
- if ([href hasPrefix:@"apptapp://package/"])
- page = [delegate_ pageForPackage:[href substringFromIndex:18]];
- else if ([scheme isEqualToString:@"cydia"]) {
- page = [delegate_ pageForURL:url hasTag:NULL];
- if (page == nil)
- return false;
- } else if (![scheme isEqualToString:@"apptapp"])
- return false;
-
- if (page != nil)
- [self pushPage:page];
- return true;
-}
-
-- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
- [window setValue:delegate_ forKey:@"cydia"];
-}
-
-- (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)dictionary request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
- if (NSURL *url = [request URL]) {
- if (![self getSpecial:url]) {
- NSString *scheme([[url scheme] lowercaseString]);
- if ([scheme isEqualToString:@"mailto"])
- [delegate_ openMailToURL:url];
- else goto use;
- }
-
- [listener ignore];
- } else use:
- [listener use];
-}
-
-- (void) webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
- if ([WebView canShowMIMEType:type])
- [listener use];
- else {
- // XXX: handle more mime types!
- [listener ignore];
- if (frame == [webView mainFrame])
- [UIApp openURL:[request URL]];
- }
-}
-
-- (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
- NSURL *url([request URL]);
-
- if (url == nil) use: {
- [listener use];
- return;
- }
-
- const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
-
- if (
- [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
- [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
- ) {
- open:
- [UIApp openURL:url];
- ignore:
- [listener ignore];
- return;
- }
-
- int store(_not(int));
- if (NSURL *itms = [url itmsURL:&store]) {
- NSLog(@"itms#%@#%u#%@", url, store, itms);
- if (
- store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
- store == 2 && [capability containsObject:@"com.apple.AppStore"]
- ) {
- url = itms;
- goto open;
- }
- }
-
- NSString *scheme([[url scheme] lowercaseString]);
-
- if ([scheme isEqualToString:@"tel"]) {
- // XXX: intelligence
- goto open;
- }
-
- if ([scheme isEqualToString:@"mailto"]) {
- [delegate_ openMailToURL:url];
- goto ignore;
- }
-
- if ([self getSpecial:url])
- goto ignore;
- else if ([WebView _canHandleRequest:request])
- goto use;
- else if ([url isSpringboardHandledURL])
- goto open;
- else
- goto use;
-}
-
-- (void) webView:(WebView *)sender setStatusText:(NSString *)text {
- //lprintf("Status:%s\n", [text UTF8String]);
-}
-
-- (void) _pushPage {
- if (pushed_)
- return;
- pushed_ = true;
- [book_ pushPage:self];
-}
-
-- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
- NSString *context([sheet context]);
-
- if ([context isEqualToString:@"challenge"]) {
- id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
-
- switch (button) {
- case 1: {
- NSString *username([[sheet textFieldAtIndex:0] text]);
- NSString *password([[sheet textFieldAtIndex:1] text]);
-
- NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
-
- [sender useCredential:credential forAuthenticationChallenge:challenge_];
- } break;
-
- case 2:
- [sender cancelAuthenticationChallenge:challenge_];
- break;
-
- default:
- _assert(false);
- }
-
- [challenge_ release];
- challenge_ = nil;
-
- [sheet dismiss];
- }
-}
-
-- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
- challenge_ = [challenge retain];
-
- NSURLProtectionSpace *space([challenge protectionSpace]);
- NSString *realm([space realm]);
- if (realm == nil)
- realm = @"";
-
- UIActionSheet *sheet = [[[UIActionSheet alloc]
- initWithTitle:realm
- buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
- defaultButtonIndex:0
- delegate:self
- context:@"challenge"
- ] autorelease];
-
- [sheet setNumberOfRows:1];
-
- [sheet addTextFieldWithValue:@"" label:@"username"];
- [sheet addTextFieldWithValue:@"" label:@"password"];
-
- UITextField *username([sheet textFieldAtIndex:0]); {
- UITextInputTraits *traits([username textInputTraits]);
- [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
- [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
- [traits setKeyboardType:UIKeyboardTypeASCIICapable];
- [traits setReturnKeyType:UIReturnKeyNext];
- }
-
- UITextField *password([sheet textFieldAtIndex:1]); {
- UITextInputTraits *traits([password textInputTraits]);
- [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
- [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
- [traits setKeyboardType:UIKeyboardTypeASCIICapable];
- // XXX: UIReturnKeyDone
- [traits setReturnKeyType:UIReturnKeyNext];
- [traits setSecureTextEntry:YES];
- }
-
- [sheet popupAlertAnimated:YES];
-}
-
-- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
- NSURL *url = [request URL];
- if ([self getSpecial:url])
- return nil;
- [self _pushPage];
- return [self _addHeadersToRequest:request];
-}
-
-- (WebView *) _createWebViewWithRequest:(NSURLRequest *)request pushed:(BOOL)pushed {
- [self setBackButtonTitle:title_];
-
- BrowserView *browser = [[[BrowserView alloc] initWithBook:book_] autorelease];
- [browser setDelegate:delegate_];
-
- if (pushed) {
- [browser loadRequest:[self _addHeadersToRequest:request]];
- [book_ pushPage:browser];
- }
-
- return [browser webView];
-}
-
-- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
- return [self _createWebViewWithRequest:request pushed:(request != nil)];
-}
-
-- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
- return [self _createWebViewWithRequest:request pushed:YES];
-}
-
-- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
- if ([frame parentFrame] != nil)
- return;
-
- title_ = [title retain];
- [book_ reloadTitleForPage:self];
-}
-
-- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
- if ([frame parentFrame] != nil)
- return;
-
- reloading_ = false;
- loading_ = true;
- [indicator_ startAnimation];
- [self reloadButtons];
-
- if (title_ != nil) {
- [title_ release];
- title_ = nil;
- }
-
- [book_ reloadTitleForPage:self];