]> git.saurik.com Git - cydia.git/blame - UICaboodle/BrowserView.m
Upgraded stylesheet and tweaked ratings position.
[cydia.git] / UICaboodle / BrowserView.m
CommitLineData
bde2d79b
JF
1#include <BrowserView.h>
2
04700693
JF
3/* Indirect Delegate {{{ */
4@interface IndirectDelegate : NSProxy {
5 _transient volatile id delegate_;
6}
7
8- (void) setDelegate:(id)delegate;
9- (id) initWithDelegate:(id)delegate;
10@end
11
12@implementation IndirectDelegate
13
14- (void) setDelegate:(id)delegate {
15 delegate_ = delegate;
16}
17
18- (id) initWithDelegate:(id)delegate {
19 delegate_ = delegate;
20 return self;
21}
22
23- (BOOL) respondsToSelector:(SEL)sel {
24 return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel];
25}
26
27- (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
28 if (delegate_ != nil)
29 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
30 return sig;
31 // XXX: I fucking hate Apple so very very bad
32 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
33}
34
35- (void) forwardInvocation:(NSInvocation *)inv {
36 SEL sel = [inv selector];
37 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
38 [inv invokeWithTarget:delegate_];
39}
40
41@end
42/* }}} */
43
bde2d79b
JF
44@interface WebView (Cydia)
45- (void) setScriptDebugDelegate:(id)delegate;
46- (void) _setFormDelegate:(id)delegate;
47- (void) _setUIKitDelegate:(id)delegate;
48- (void) setWebMailDelegate:(id)delegate;
49- (void) _setLayoutInterval:(float)interval;
50@end
51
45f88d41
JF
52/* Web Scripting {{{ */
53@interface CydiaObject : NSObject {
54 id indirect_;
55}
56
57- (id) initWithDelegate:(IndirectDelegate *)indirect;
58@end
59
60@implementation CydiaObject
61
62- (void) dealloc {
63 [indirect_ release];
64 [super dealloc];
65}
66
67- (id) initWithDelegate:(IndirectDelegate *)indirect {
68 if ((self = [super init]) != nil) {
69 indirect_ = [indirect retain];
70 } return self;
71}
72
73+ (NSString *) webScriptNameForSelector:(SEL)selector {
74 if (selector == @selector(getPackageById:))
75 return @"getPackageById";
7b00c562
JF
76 else if (selector == @selector(setAutoPopup:))
77 return @"setAutoPopup";
45f88d41
JF
78 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
79 return @"setButtonImage";
80 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
81 return @"setButtonTitle";
dcc55b8b
JF
82 else if (selector == @selector(setViewportWidth:))
83 return @"setViewportWidth";
45f88d41
JF
84 else if (selector == @selector(supports:))
85 return @"supports";
86 else if (selector == @selector(du:))
87 return @"du";
88 else if (selector == @selector(statfs:))
89 return @"statfs";
90 else
91 return nil;
92}
93
94+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
95 return [self webScriptNameForSelector:selector] == nil;
96}
97
98- (BOOL) supports:(NSString *)feature {
99 return [feature isEqualToString:@"window.open"];
100}
101
102- (Package *) getPackageById:(NSString *)id {
103 return [[Database sharedInstance] packageWithName:id];
104}
105
106- (NSArray *) statfs:(NSString *)path {
107 struct statfs stat;
108
109 if (path == nil || statfs([path UTF8String], &stat) == -1)
110 return nil;
111
112 return [NSArray arrayWithObjects:
113 [NSNumber numberWithUnsignedLong:stat.f_bsize],
114 [NSNumber numberWithUnsignedLong:stat.f_blocks],
115 [NSNumber numberWithUnsignedLong:stat.f_bfree],
116 nil];
117}
118
119- (NSNumber *) du:(NSString *)path {
120 NSNumber *value(nil);
121
122 int fds[2];
123 _assert(pipe(fds) != -1);
124
125 pid_t pid(ExecFork());
126 if (pid == 0) {
127 _assert(dup2(fds[1], 1) != -1);
128 _assert(close(fds[0]) != -1);
129 _assert(close(fds[1]) != -1);
130 execlp("du", "du", "-s", [path UTF8String], NULL);
131 exit(1);
132 _assert(false);
133 }
134
135 _assert(close(fds[1]) != -1);
136
137 if (FILE *du = fdopen(fds[0], "r")) {
138 char line[1024];
139 while (fgets(line, sizeof(line), du) != NULL) {
140 size_t length(strlen(line));
141 while (length != 0 && line[length - 1] == '\n')
142 line[--length] = '\0';
143 if (char *tab = strchr(line, '\t')) {
144 *tab = '\0';
145 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
146 }
147 }
148
149 fclose(du);
150 } else _assert(close(fds[0]));
151
152 int status;
153 wait:
154 if (waitpid(pid, &status, 0) == -1)
155 if (errno == EINTR)
156 goto wait;
157 else _assert(false);
158
159 return value;
160}
161
7b00c562
JF
162- (void) setAutoPopup:(BOOL)popup {
163 [indirect_ setAutoPopup:popup];
164}
165
45f88d41
JF
166- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
167 [indirect_ setButtonImage:button withStyle:style toFunction:function];
168}
169
170- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
171 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
172}
173
dcc55b8b
JF
174- (void) setViewportWidth:(float)width {
175 [indirect_ setViewportWidth:width];
176}
177
45f88d41
JF
178@end
179/* }}} */
180
bde2d79b
JF
181@implementation BrowserView
182
5a5c1e4b 183#if ShowInternals
bde2d79b
JF
184#include "Internals.h"
185#endif
186
187- (void) dealloc {
7b00c562
JF
188#if ForSaurik
189 NSLog(@"[BrowserView dealloc]");
190#endif
5a5c1e4b 191
bde2d79b
JF
192 if (challenge_ != nil)
193 [challenge_ release];
194
195 WebView *webview = [webview_ webView];
196 [webview setFrameLoadDelegate:nil];
197 [webview setResourceLoadDelegate:nil];
198 [webview setUIDelegate:nil];
199 [webview setScriptDebugDelegate:nil];
200 [webview setPolicyDelegate:nil];
201
202 [webview setDownloadDelegate:nil];
203
204 [webview _setFormDelegate:nil];
205 [webview _setUIKitDelegate:nil];
206 [webview setWebMailDelegate:nil];
207 [webview setEditingDelegate:nil];
208
209 [webview_ setDelegate:nil];
210 [webview_ setGestureDelegate:nil];
dcc55b8b
JF
211 [webview_ setFormEditingDelegate:nil];
212 [webview_ setInteractionDelegate:nil];
bde2d79b
JF
213
214 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
215
216 [webview close];
217
218#if RecycleWebViews
219 [webview_ removeFromSuperview];
220 [Documents_ addObject:[webview_ autorelease]];
221#else
222 [webview_ release];
223#endif
224
225 [indirect_ setDelegate:nil];
226 [indirect_ release];
227
45f88d41
JF
228 [cydia_ release];
229
bde2d79b
JF
230 [scroller_ setDelegate:nil];
231
232 if (button_ != nil)
233 [button_ release];
234 if (style_ != nil)
235 [style_ release];
236 if (function_ != nil)
237 [function_ release];
238
239 [scroller_ release];
240 [indicator_ release];
241 if (confirm_ != nil)
242 [confirm_ release];
243 if (title_ != nil)
244 [title_ release];
245 [super dealloc];
246}
247
248- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
249 [self loadRequest:[NSURLRequest
250 requestWithURL:url
251 cachePolicy:policy
252 timeoutInterval:30.0
253 ]];
254}
255
256- (void) loadURL:(NSURL *)url {
257 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
258}
259
260- (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
261 NSMutableURLRequest *copy = [request mutableCopy];
262
263 if (Machine_ != NULL)
264 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
265 if (UniqueID_ != nil)
266 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
267
268 if (Role_ != nil)
269 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
270
271 return copy;
272}
273
274- (void) loadRequest:(NSURLRequest *)request {
275 pushed_ = true;
f4772bd1 276 error_ = false;
bde2d79b
JF
277 [webview_ loadRequest:request];
278}
279
280- (void) reloadURL {
bde2d79b
JF
281 if (request_ == nil)
282 return;
283
284 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
f4772bd1 285 [self loadRequest:request_];
bde2d79b
JF
286 else {
287 UIActionSheet *sheet = [[[UIActionSheet alloc]
288 initWithTitle:@"Are you sure you want to submit this form again?"
289 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
290 defaultButtonIndex:0
291 delegate:self
292 context:@"submit"
293 ] autorelease];
294
295 [sheet setNumberOfRows:1];
296 [sheet popupAlertAnimated:YES];
297 }
298}
299
300- (WebView *) webView {
301 return [webview_ webView];
302}
303
5a5c1e4b
JF
304- (UIWebDocumentView *) documentView {
305 return webview_;
306}
307
dcc55b8b 308- (void) _fixScroller {
5889429e
JF
309 CGRect bounds([webview_ documentBounds]);
310#if ForSaurik
311 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
312#endif
313
dcc55b8b
JF
314 float extra;
315 if (!editing_)
316 extra = 0;
317 else {
318 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
319 CGRect peripheral([assistant peripheralFrame]);
5889429e 320#if ForSaurik
dcc55b8b 321 NSLog(@"per:%f", peripheral.size.height);
5889429e 322#endif
dcc55b8b
JF
323 extra = peripheral.size.height;
324 }
325
326 CGRect subrect([scroller_ frame]);
327 subrect.size.height -= extra;
328 [scroller_ setScrollerIndicatorSubrect:subrect];
329
5889429e
JF
330 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
331 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
332
dcc55b8b
JF
333 CGSize size(size_);
334 size.height += extra;
335 [scroller_ setContentSize:size];
5889429e
JF
336
337 [scroller_ releaseRubberBandIfNecessary];
dcc55b8b
JF
338}
339
bde2d79b 340- (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
dcc55b8b 341 size_ = frame.size;
5889429e
JF
342#if ForSaurik
343 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
344#endif
dcc55b8b 345 [self _fixScroller];
bde2d79b
JF
346}
347
348- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
349 [self view:sender didSetFrame:frame];
350}
351
352- (void) pushPage:(RVPage *)page {
bde2d79b 353 [page setDelegate:delegate_];
2517c95f 354 [self setBackButtonTitle:title_];
5a5c1e4b 355 [book_ pushPage:page];
bde2d79b
JF
356}
357
a55054a2
JF
358- (void) _pushPage {
359 if (pushed_)
360 return;
361 [self autorelease];
362 pushed_ = true;
363 [book_ pushPage:self];
364}
365
5889429e
JF
366- (void) swapPage:(RVPage *)page {
367 [page setDelegate:delegate_];
368 if (pushed_)
369 [book_ swapPage:page];
370 else
371 [book_ pushPage:page];
372}
373
bde2d79b 374- (BOOL) getSpecial:(NSURL *)url {
a55054a2
JF
375#if ForSaurik
376 NSLog(@"getSpecial:%@", url);
377#endif
378
bde2d79b
JF
379 NSString *href([url absoluteString]);
380 NSString *scheme([[url scheme] lowercaseString]);
381
382 RVPage *page = nil;
383
384 if ([href hasPrefix:@"apptapp://package/"])
385 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
386 else if ([scheme isEqualToString:@"cydia"]) {
387 page = [delegate_ pageForURL:url hasTag:NULL];
388 if (page == nil)
389 return false;
390 } else if (![scheme isEqualToString:@"apptapp"])
391 return false;
392
393 if (page != nil)
5889429e 394 [self swapPage:page];
bde2d79b
JF
395 return true;
396}
397
5a5c1e4b 398- (void) webViewShow:(WebView *)sender {
5b2d5cf3 399 /* XXX: this is where I cry myself to sleep */
5a5c1e4b
JF
400}
401
7b00c562
JF
402- (bool) _allowJavaScriptPanel {
403 return true;
404}
405
bde2d79b 406- (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
263b91b4 407 if (![self _allowJavaScriptPanel])
7b00c562
JF
408 return;
409
b456f1c9
JF
410 [self retain];
411
bde2d79b
JF
412 UIActionSheet *sheet = [[[UIActionSheet alloc]
413 initWithTitle:nil
414 buttons:[NSArray arrayWithObjects:@"OK", nil]
415 defaultButtonIndex:0
416 delegate:self
417 context:@"alert"
418 ] autorelease];
419
420 [sheet setBodyText:message];
421 [sheet popupAlertAnimated:YES];
422}
423
424- (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
7b00c562
JF
425 if (![self _allowJavaScriptPanel])
426 return NO;
427
bde2d79b
JF
428 UIActionSheet *sheet = [[[UIActionSheet alloc]
429 initWithTitle:nil
430 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
431 defaultButtonIndex:0
b456f1c9 432 delegate:indirect_
bde2d79b
JF
433 context:@"confirm"
434 ] autorelease];
435
436 [sheet setNumberOfRows:1];
437 [sheet setBodyText:message];
438 [sheet popupAlertAnimated:YES];
439
440 NSRunLoop *loop([NSRunLoop currentRunLoop]);
441 NSDate *future([NSDate distantFuture]);
442
443 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
444
445 NSNumber *confirm([confirm_ autorelease]);
446 confirm_ = nil;
447 return [confirm boolValue];
448}
449
7b00c562
JF
450- (void) setAutoPopup:(BOOL)popup {
451 popup_ = popup;
452}
453
bde2d79b
JF
454- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
455 if (button_ != nil)
456 [button_ autorelease];
457 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
458
459 if (style_ != nil)
460 [style_ autorelease];
461 style_ = style == nil ? nil : [style retain];
462
463 if (function_ != nil)
464 [function_ autorelease];
465 function_ = function == nil ? nil : [function retain];
466}
467
468- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
469 if (button_ != nil)
470 [button_ autorelease];
471 button_ = button == nil ? nil : [button retain];
472
473 if (style_ != nil)
474 [style_ autorelease];
475 style_ = style == nil ? nil : [style retain];
476
477 if (function_ != nil)
478 [function_ autorelease];
479 function_ = function == nil ? nil : [function retain];
480}
bde2d79b 481
dcc55b8b
JF
482- (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
483 editing_ = true;
484}
485
486- (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
487 [self _fixScroller];
488}
489
490- (void) webViewDidEndEditingFormElements:(WebView *)sender {
491 editing_ = false;
492 [self _fixScroller];
493}
494
04700693
JF
495- (void) webViewClose:(WebView *)sender {
496 [book_ close];
497}
498
bde2d79b 499- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
45f88d41 500 [window setValue:cydia_ forKey:@"cydia"];
bde2d79b
JF
501}
502
503- (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
504 NSLog(@"err:%@", error);
505}
506
507- (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
7b00c562 508#if ForSaurik
5a5c1e4b 509 NSLog(@"nwa:%@", name);
7b00c562 510#endif
5a5c1e4b 511
bde2d79b 512 if (NSURL *url = [request URL]) {
04700693 513 if (name == nil) unknown: {
04700693
JF
514 if (![self getSpecial:url]) {
515 NSString *scheme([[url scheme] lowercaseString]);
516 if ([scheme isEqualToString:@"mailto"])
517 [delegate_ openMailToURL:url];
518 else goto use;
519 }
520 } else if ([name isEqualToString:@"_open"])
bde2d79b 521 [delegate_ openURL:url];
04700693
JF
522 else if ([name isEqualToString:@"_popup"]) {
523 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
04700693
JF
524
525 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
526 if (page == nil) {
7b00c562 527 /* XXX: call createWebViewWithRequest instead? */
5a5c1e4b
JF
528
529 [self setBackButtonTitle:title_];
530
04700693
JF
531 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
532 [browser loadURL:url];
533 page = browser;
534 }
bde2d79b 535
0a7e5478
JF
536 [book setDelegate:delegate_];
537 [page setDelegate:delegate_];
538
04700693
JF
539 [book setPage:page];
540 [book_ pushBook:book];
541 } else goto unknown;
bde2d79b
JF
542
543 [listener ignore];
544 } else use:
545 [listener use];
546}
547
5889429e 548- (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
bde2d79b
JF
549 if ([WebView canShowMIMEType:type])
550 [listener use];
551 else {
552 // XXX: handle more mime types!
553 [listener ignore];
5889429e
JF
554
555 WebView *webview([webview_ webView]);
556 if (frame == [webview mainFrame])
bde2d79b
JF
557 [UIApp openURL:[request URL]];
558 }
559}
560
561- (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
562 if (request == nil) ignore: {
563 [listener ignore];
564 return;
565 }
566
567 NSURL *url([request URL]);
568
569 if (url == nil) use: {
f4772bd1 570 if (!error_ && [frame parentFrame] == nil) {
bde2d79b
JF
571 if (request_ != nil)
572 [request_ autorelease];
573 request_ = [request retain];
04700693 574#if ForSaurik
bde2d79b 575 NSLog(@"dpn:%@", request_);
04700693 576#endif
bde2d79b
JF
577 }
578
579 [listener use];
5889429e
JF
580
581 WebView *webview([webview_ webView]);
582 if (frame == [webview mainFrame])
583 [self _pushPage];
bde2d79b
JF
584 return;
585 }
586#if ForSaurik
587 else NSLog(@"nav:%@:%@", url, [action description]);
588#endif
589
590 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
591
592 if (
593 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
594 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
595 ) {
596 open:
597 [UIApp openURL:url];
598 goto ignore;
599 }
600
601 int store(_not(int));
602 if (NSURL *itms = [url itmsURL:&store]) {
7b00c562 603#if ForSaurik
bde2d79b 604 NSLog(@"itms#%@#%u#%@", url, store, itms);
7b00c562
JF
605#endif
606
bde2d79b
JF
607 if (
608 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
609 store == 2 && [capability containsObject:@"com.apple.AppStore"]
610 ) {
611 url = itms;
612 goto open;
613 }
614 }
615
616 NSString *scheme([[url scheme] lowercaseString]);
617
618 if ([scheme isEqualToString:@"tel"]) {
619 // XXX: intelligence
620 goto open;
621 }
622
623 if ([scheme isEqualToString:@"mailto"]) {
624 [delegate_ openMailToURL:url];
625 goto ignore;
626 }
627
628 if ([self getSpecial:url])
629 goto ignore;
630 else if ([WebView _canHandleRequest:request])
631 goto use;
632 else if ([url isSpringboardHandledURL])
633 goto open;
634 else
635 goto use;
636}
637
638- (void) webView:(WebView *)sender setStatusText:(NSString *)text {
639 //lprintf("Status:%s\n", [text UTF8String]);
640}
641
bde2d79b
JF
642- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
643 NSString *context([sheet context]);
644
b456f1c9
JF
645 if ([context isEqualToString:@"alert"]) {
646 [self autorelease];
bde2d79b 647 [sheet dismiss];
b456f1c9 648 } else if ([context isEqualToString:@"confirm"]) {
bde2d79b
JF
649 switch (button) {
650 case 1:
651 confirm_ = [NSNumber numberWithBool:YES];
652 break;
653
654 case 2:
655 confirm_ = [NSNumber numberWithBool:NO];
656 break;
657 }
658
659 [sheet dismiss];
660 } else if ([context isEqualToString:@"challenge"]) {
661 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
662
663 switch (button) {
664 case 1: {
665 NSString *username([[sheet textFieldAtIndex:0] text]);
666 NSString *password([[sheet textFieldAtIndex:1] text]);
667
668 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
669
670 [sender useCredential:credential forAuthenticationChallenge:challenge_];
671 } break;
672
673 case 2:
674 [sender cancelAuthenticationChallenge:challenge_];
675 break;
676
677 default:
678 _assert(false);
679 }
680
681 [challenge_ release];
682 challenge_ = nil;
683
684 [sheet dismiss];
685 } else if ([context isEqualToString:@"submit"]) {
686 switch (button) {
687 case 1:
688 break;
689
690 case 2:
691 if (request_ != nil)
692 [webview_ loadRequest:request_];
693 break;
694
695 default:
696 _assert(false);
697 }
698
699 [sheet dismiss];
700 }
701}
702
703- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
704 challenge_ = [challenge retain];
705
706 NSURLProtectionSpace *space([challenge protectionSpace]);
707 NSString *realm([space realm]);
708 if (realm == nil)
709 realm = @"";
710
711 UIActionSheet *sheet = [[[UIActionSheet alloc]
712 initWithTitle:realm
713 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
714 defaultButtonIndex:0
715 delegate:self
716 context:@"challenge"
717 ] autorelease];
718
719 [sheet setNumberOfRows:1];
720
721 [sheet addTextFieldWithValue:@"" label:@"username"];
722 [sheet addTextFieldWithValue:@"" label:@"password"];
723
724 UITextField *username([sheet textFieldAtIndex:0]); {
725 UITextInputTraits *traits([username textInputTraits]);
726 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
727 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
728 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
729 [traits setReturnKeyType:UIReturnKeyNext];
730 }
731
732 UITextField *password([sheet textFieldAtIndex:1]); {
733 UITextInputTraits *traits([password textInputTraits]);
734 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
735 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
736 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
737 // XXX: UIReturnKeyDone
738 [traits setReturnKeyType:UIReturnKeyNext];
739 [traits setSecureTextEntry:YES];
740 }
741
742 [sheet popupAlertAnimated:YES];
743}
744
745- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
bde2d79b
JF
746 return [self _addHeadersToRequest:request];
747}
748
5a5c1e4b 749- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
7b00c562 750//- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
5a5c1e4b 751#if ForSaurik
7b00c562
JF
752 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
753 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
5a5c1e4b 754#endif
bde2d79b 755
7b00c562 756 NSNumber *value([features objectForKey:@"width"]);
5889429e 757 float width(value == nil ? 0 : [value floatValue]);
7b00c562
JF
758
759 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
760
761 /* XXX: deal with cydia:// pages */
762 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
763
b456f1c9 764 if (features != nil && popup_) {
7b00c562
JF
765 [book setDelegate:delegate_];
766 [browser setDelegate:delegate_];
767
768 [browser loadRequest:request];
a55054a2 769
7b00c562
JF
770 [book setPage:browser];
771 [book_ pushBook:book];
772 } else if (request == nil) {
a55054a2
JF
773 [self setBackButtonTitle:title_];
774 [browser setDelegate:delegate_];
775 [browser retain];
776 } else {
777 [self pushPage:browser];
778 [browser loadRequest:request];
779 }
780
bde2d79b
JF
781 return [browser webView];
782}
783
784- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
5a5c1e4b 785 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
7b00c562 786 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
bde2d79b
JF
787}
788
789- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
790 if ([frame parentFrame] != nil)
791 return;
792
793 title_ = [title retain];
794 [book_ reloadTitleForPage:self];
795}
796
797- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
798 if ([frame parentFrame] != nil)
799 return;
800
5889429e
JF
801 [webview_ resignFirstResponder];
802
bde2d79b
JF
803 reloading_ = false;
804 loading_ = true;
805 [self reloadButtons];
806
807 if (title_ != nil) {
808 [title_ release];
809 title_ = nil;
810 }
811
812 if (button_ != nil) {
813 [button_ release];
814 button_ = nil;
815 }
816
817 if (style_ != nil) {
818 [style_ release];
819 style_ = nil;
820 }
821
822 if (function_ != nil) {
823 [function_ release];
824 function_ = nil;
825 }
826
827 [book_ reloadTitleForPage:self];
828
829 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
5889429e 830 [scroller_ setZoomScale:1 duration:0];
bde2d79b
JF
831
832 CGRect webrect = [scroller_ bounds];
833 webrect.size.height = 0;
834 [webview_ setFrame:webrect];
835}
836
837- (void) _finishLoading {
838 if (!reloading_) {
839 loading_ = false;
840 [self reloadButtons];
841 }
842}
843
844- (bool) _loading {
845 return loading_;
846}
847
848- (void) reloadButtons {
849 if ([self _loading])
850 [indicator_ startAnimation];
851 else
852 [indicator_ stopAnimation];
853 [super reloadButtons];
854}
855
856- (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
857 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
858}
859
860- (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
861 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
862}
863
864- (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
865 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
866}
867
868- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
10c3a9c1 869 [self _pushPage];
bde2d79b
JF
870 return [webview_ webView:sender didCommitLoadForFrame:frame];
871}
872
873- (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
874 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
875}
876
877- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
878 if ([frame parentFrame] == nil) {
879 [self _finishLoading];
880
881 if (DOMDocument *document = [frame DOMDocument])
882 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
883 for (DOMHTMLBodyElement *body in bodies) {
884 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
885
886 bool colored(false);
887
888 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
f4772bd1
JF
889 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
890 DOMRGBColor *rgb([color getRGBColorValue]);
891
892 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
893 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
894 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
895 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
896
897 UIColor *uic(nil);
898
899 if (red == 0xc7 && green == 0xce && blue == 0xd5)
900 uic = [UIColor pinStripeColor];
901 else if (alpha != 0)
902 uic = [UIColor
903 colorWithRed:(red / 255)
904 green:(green / 255)
905 blue:(blue / 255)
906 alpha:alpha
907 ];
908
909 if (uic != nil) {
910 colored = true;
911 [scroller_ setBackgroundColor:uic];
912 }
bde2d79b
JF
913 }
914 }
915
916 if (!colored)
917 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
918 break;
919 }
920 }
921
922 return [webview_ webView:sender didFinishLoadForFrame:frame];
923}
924
925- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
926 if ([frame parentFrame] != nil)
927 return;
2517c95f
JF
928 if (reloading_)
929 return;
bde2d79b
JF
930 [self _finishLoading];
931
932 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
933 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
934 [[error localizedDescription] stringByAddingPercentEscapes]
935 ]]];
f4772bd1
JF
936
937 error_ = true;
bde2d79b
JF
938}
939
940- (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
941#if ForSaurik
942 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
943#endif
944}
945
dcc55b8b 946- (void) setViewportWidth:(float)width {
5889429e 947 width_ = width ? width != 0 : [[self class] defaultWidth];
dcc55b8b
JF
948 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
949}
950
7b00c562 951- (id) initWithBook:(RVBook *)book forWidth:(float)width {
bde2d79b
JF
952 if ((self = [super initWithBook:book]) != nil) {
953 loading_ = false;
7b00c562 954 popup_ = false;
bde2d79b
JF
955
956 struct CGRect bounds = [self bounds];
957
958 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
959 [self addSubview:scroller_];
960
961 [scroller_ setShowBackgroundShadow:NO];
962 [scroller_ setFixedBackgroundPattern:YES];
963 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
964
965 [scroller_ setScrollingEnabled:YES];
966 [scroller_ setAdjustForContentSizeChange:YES];
967 [scroller_ setClipsSubviews:YES];
968 [scroller_ setAllowsRubberBanding:YES];
969 [scroller_ setScrollDecelerationFactor:0.99];
970 [scroller_ setDelegate:self];
971
972 CGRect webrect = [scroller_ bounds];
973 webrect.size.height = 0;
974
975 WebView *webview;
976
977#if RecycleWebViews
978 webview_ = [Documents_ lastObject];
979 if (webview_ != nil) {
980 webview_ = [webview_ retain];
981 webview = [webview_ webView];
982 [Documents_ removeLastObject];
983 [webview_ setFrame:webrect];
984 } else {
985#else
986 if (true) {
987#endif
988 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
989 webview = [webview_ webView];
990
991 // XXX: this is terribly (too?) expensive
992 //[webview_ setDrawsBackground:NO];
993 [webview setPreferencesIdentifier:@"Cydia"];
994
995 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
996
997 [webview_ setAllowsMessaging:YES];
998
999 [webview_ setTilingEnabled:YES];
1000 [webview_ setDrawsGrid:NO];
1001 [webview_ setLogsTilingChanges:NO];
1002 [webview_ setTileMinificationFilter:kCAFilterNearest];
1003 [webview_ setDetectsPhoneNumbers:NO];
1004 [webview_ setAutoresizes:YES];
1005
1006 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
7b00c562 1007 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
bde2d79b 1008 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
7b00c562 1009 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
bde2d79b
JF
1010
1011 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1012
7b00c562 1013 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
bde2d79b
JF
1014 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1015 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1016
1017 [webview_ _setDocumentType:0x4];
1018
1019 [webview_ setZoomsFocusedFormControl:YES];
1020 [webview_ setContentsPosition:7];
1021 [webview_ setEnabledGestures:0xa];
1022 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1023 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1024
1025 [webview_ setSmoothsFonts:YES];
7b00c562 1026 [webview_ setAllowsImageSheet:YES];
bde2d79b 1027 [webview _setUsesLoaderCache:YES];
7b00c562
JF
1028
1029 [webview setGroupName:@"CydiaGroup"];
bde2d79b
JF
1030 [webview _setLayoutInterval:0];
1031 }
1032
dcc55b8b 1033 [self setViewportWidth:width];
7b00c562 1034
bde2d79b
JF
1035 [webview_ setDelegate:self];
1036 [webview_ setGestureDelegate:self];
dcc55b8b
JF
1037 [webview_ setFormEditingDelegate:self];
1038 [webview_ setInteractionDelegate:self];
1039
bde2d79b
JF
1040 [scroller_ addSubview:webview_];
1041
1042 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1043
1044 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1045 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1046 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1047
1048 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1049 NSString *application = package == nil ? @"Cydia" : [NSString
1050 stringWithFormat:@"Cydia/%@",
1051 [package installed]
68c05606
JF
1052 ];
1053
7b00c562
JF
1054 if (Product_ != nil)
1055 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
68c05606 1056 if (Build_ != nil)
7b00c562
JF
1057 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1058 if (Safari_ != nil)
1059 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
68c05606
JF
1060
1061 /* XXX: lookup application directory? */
1062 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1063 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1064 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1065
1066 [webview setApplicationNameForUserAgent:application];
bde2d79b
JF
1067
1068 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
45f88d41 1069 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
bde2d79b
JF
1070
1071 [webview setFrameLoadDelegate:self];
1072 [webview setResourceLoadDelegate:indirect_];
1073 [webview setUIDelegate:self];
1074 [webview setScriptDebugDelegate:self];
1075 [webview setPolicyDelegate:self];
1076
1077 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1078 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1079 } return self;
1080}
1081
7b00c562 1082- (id) initWithBook:(RVBook *)book {
5889429e 1083 return [self initWithBook:book forWidth:0];
7b00c562
JF
1084}
1085
bde2d79b
JF
1086- (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1087 [webview_ redrawScaledDocument];
1088}
1089
1090- (void) _rightButtonClicked {
1091 if (function_ == nil) {
1092 reloading_ = true;
1093 [self reloadURL];
1094 } else {
1095 WebView *webview([webview_ webView]);
1096 WebFrame *frame([webview mainFrame]);
1097
1098 id _private(MSHookIvar<id>(webview, "_private"));
1099 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1100 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1101
1102 bool no;
1103 if (settings == NULL)
1104 no = 0;
1105 else {
1106 no = settings->JavaScriptCanOpenWindowsAutomatically();
1107 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1108 }
1109
1110 [delegate_ clearFirstResponder];
1111 JSObjectRef function([function_ JSObject]);
1112 JSGlobalContextRef context([frame globalContext]);
1113 JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
1114
1115 if (settings != NULL)
1116 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1117 }
1118}
1119
1120- (id) _rightButtonTitle {
1121 return button_ != nil ? button_ : @"Reload";
1122}
1123
1124- (id) rightButtonTitle {
1125 return [self _loading] ? @"" : [self _rightButtonTitle];
1126}
1127
1128- (UINavigationButtonStyle) rightButtonStyle {
1129 if (style_ == nil) normal:
1130 return UINavigationButtonStyleNormal;
1131 else if ([style_ isEqualToString:@"Normal"])
1132 return UINavigationButtonStyleNormal;
1133 else if ([style_ isEqualToString:@"Back"])
1134 return UINavigationButtonStyleBack;
1135 else if ([style_ isEqualToString:@"Highlighted"])
1136 return UINavigationButtonStyleHighlighted;
1137 else if ([style_ isEqualToString:@"Destructive"])
1138 return UINavigationButtonStyleDestructive;
1139 else goto normal;
1140}
1141
1142- (NSString *) title {
1143 return title_ == nil ? @"Loading" : title_;
1144}
1145
1146- (NSString *) backButtonTitle {
1147 return @"Browser";
1148}
1149
1150- (void) setPageActive:(BOOL)active {
1151 if (!active)
1152 [indicator_ removeFromSuperview];
1153 else
1154 [[book_ navigationBar] addSubview:indicator_];
1155}
1156
1157- (void) resetViewAnimated:(BOOL)animated {
1158}
1159
1160- (void) setPushed:(bool)pushed {
1161 pushed_ = pushed;
1162}
1163
7b00c562
JF
1164+ (float) defaultWidth {
1165 return 980;
1166}
1167
bde2d79b 1168@end