MainView.m
上传用户:xy800802
上传日期:2020-12-29
资源大小:194k
文件大小:12k
- #import "MainView.h"
- @implementation MainView
- - (id)initWithFrame:(CGRect)rect {
- if ((self == [ super initWithFrame: rect ]) != nil) {
- /* -------------------------------------- exploreView -------------------------------------- */
- NSLog(@"exploreView Init");
- exploreView = [[UITextView alloc] initWithFrame: rect];
- [aboutView setTextSize: 18];
- [aboutView setMarginTop: 2 ];
- /* -------------------------------------- fileView -------------------------------------- */
- NSLog(@"fileView Init");
- fileView = [[UITextView alloc] initWithFrame: rect];
- [aboutView setTextSize: 18];
- [aboutView setMarginTop: 2 ];
- /* -------------------------------------- htmlView -------------------------------------- */
- //NSLog(@"htmlView Init");
- //htmlView = [[UIWebView alloc] initWithFrame: rect];
- //UIScroller *scroller = [[UIScroller alloc] initWithFrame: rect];
- //[htmlView addSubview: scroller];
-
- /* -------------------------------------- NavigationBar -------------------------------------- */
- NSLog(@"NavigationBar Init");
- navBar = [ [UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, 320, 48)];
- [ navBar setDelegate: self ];
- [ navBar enableAnimation ];
- [ navBar setBarStyle: 0 ];
- navTitle = [ [ UINavigationItem alloc ] initWithTitle:@"/" ];
- [ navBar pushNavigationItem: navTitle ];
- [ self addSubview: navBar ];
-
- /* -------------------------------------- UITransitionView -------------------------------------- */
- NSLog(@"UITransitionView Init");
-
- CGRect tranrect = CGRectMake(rect.origin.x, rect.origin.y + 48.0,
- rect.size.width, rect.size.height - 48.0);
- transitionView = [ [ UITransitionView alloc ]initWithFrame: tranrect ];
-
- [ self addSubview: transitionView ];
- /* -------------------------------------- UIAlertSheet -------------------------------------- */
- NSLog(@"UIAlertSheet Init");
- alertSheet = [[UIAlertSheet alloc]
- initWithFrame : CGRectMake(0,240,320,240)];
- [alertSheet setTitle:@"Alert Title"];
- [alertSheet setBodyText:@"This is an alert."];
- [alertSheet setDestructiveButton :
- [alertSheet addButtonWithTitle:@"Yes"]];
- [alertSheet addButtonWithTitle:@"No"];
- [alertSheet setDelegate: self];
- //[alertSheet popupAlertAnimated:YES];
- [self initTable];
- [self showrootdir];
- /* Allocate initial resources here */
- }
- return self;
- }
- - (void)dealloc
- {
- NSLog(@"MainView Release");
- /* Deallocate any resources here */
-
- [ aboutView dealloc];
- [ exploreView dealloc];
- [ fileView dealloc];
- [ transitionView dealloc];
-
- [ navBar release];
- [ self dealloc ];
- [ super dealloc ];
- }
- -(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
- {
- if ( button == 1 )
- {
- NSLog(@"Yes");
- [Util RestartSpringboard];
- }
- else if ( button == 2 )
- NSLog(@"No");
- [sheet dismiss];
- }
- -(void)initTable
- {
- CGRect rect = CGRectMake(0, 0, 320.0, 480.0 - 64);
- exploreTable = [ [ MyTable alloc ] initWithFrame: rect ];
- //[ exploreTable setPath: @"/Applications" ];
- //[ exploreTable setExtension: @"" ];
- //[ exploreTable reloadData ];
- [exploreTable setView:self];
- [exploreView addSubview:exploreTable];
- }
- -(void)showabout
- {
- if(aboutView == nil)
- {
- CGRect rect = [ UIHardware fullScreenApplicationContentRect ];
- rect.origin.x = 0;
- rect.origin.y = 0;
- //rect.size.height -= 48.0;
- aboutView = [[UITextView alloc] initWithFrame: rect];
- [aboutView setTextSize: 18];
- [aboutView setMarginTop: 2 ];
-
- CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
- float opaqueRed[4] = { 1.0, 0.0, 0.0, 1.0 };
- CGColorRef red = CGColorCreate(colorSpace, opaqueRed);
- [ aboutView setTextColor: red ];
- NSString *str = [NSString stringWithFormat:@"file:///Applications/FileManager.app/about.html"];
- NSURL *url = [NSURL URLWithString:str];
- NSMutableString *aboutContent = [NSMutableString stringWithCapacity:10240];
- if(url){
- NSError *err;
- NSString *fname = [[NSString alloc] initWithContentsOfURL:url encoding: NSUTF8StringEncoding error:NULL];
- NSLog(fname);
- if(fname && [fname length] > 0){
- [aboutContent appendString:fname];
- }
- }
- [aboutView setTextSize:16];
- [aboutView setHTML:aboutContent];
- [aboutView setEditable:NO];
- [aboutView setContentSize:CGSizeMake(320,800)];
- [aboutView setAllowsRubberBanding: YES];
- /*
- // 不能处理中文
- FILE *file;
- char buffer[262144], buf[1024];
- file = fopen("/Applications/FileManager.app/about.html","r");
- if(!file)
- {
- [aboutView setText:@"Hello,World!"];
- [aboutView setEditable:YES];
- }
- else
- {
- buffer[0] = 0;
- while((fgets(buf,sizeof(buf),file))!=NULL)
- {
- strlcat(buffer,buf,sizeof(buffer));
- }
- fclose(file);
- [ aboutView setHTML: [[NSString alloc] initWithCString: buffer]];
- }
- */
- }
- [ transitionView transition: pages_scroll_right toView: aboutView ];
- [ self setNavBar:0 withTitle:@"about"];
- }
- -(void)showparent
- {
- depth--;
- [self showpath: [currentPath stringByDeletingLastPathComponent]];
- }
- -(void)showrootdir
- {
- depth=0;
- [self showpath: @"/"];
- }
- -(void)showSub:(NSString*)path
- {
- depth++;
- [self showpath:path];
- }
- -(void)showpath:(NSString*)path
- {
- BOOL isdir = NO;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- [fileManager fileExistsAtPath:path isDirectory:&isdir];
- currentPath = path;
- if(isdir)
- {
- [exploreTable setPath: currentPath];
- [exploreTable setExtension: @"" ];
- [exploreTable reloadData];
- [ transitionView transition: pages_scroll_left toView: exploreView ];
- [self setNavBar:1 withTitle:path];
- }
- else
- {
- [self showfile:path];
- [self setNavBar:2 withTitle:path];
- }
- NSLog(@"%d",depth);
- }
- -(void)showfile:(NSString*)file
- {
- // load file
- if([file hasSuffix: @"png"] == YES ||
- [file hasSuffix: @"JPG"] == YES )
- {
- if(imageView!=nil)
- {
- [imageView dealloc];
- }
- CGRect rect = [ UIHardware fullScreenApplicationContentRect ];
- rect.origin.x = 0;
- rect.origin.y = 0;
- imageView = [[UIAutocorrectImageView alloc] initWithFrame: rect image: [UIImage imageAtPath: file]];
- [transitionView transition: pages_scroll_left toView: imageView ];
- }
- else if([file hasSuffix: @"txt"] == YES)
- {
- FILE *txt_file;
- char buffer[262144], buf[1024];
- txt_file = fopen([file UTF8String],"r");
- if(txt_file)
- {
- buffer[0] = 0;
- while((fgets(buf,sizeof(buf),txt_file))!=NULL)
- {
- strlcat(buffer,buf,sizeof(buffer));
- }
- fclose(txt_file);
- [fileView setText: [[NSString alloc] initWithCString: buffer]]; // encoding: NSASCIIStringEncoding / NSUTF8StringEncoding
- }
- [ transitionView transition: pages_scroll_left toView: fileView ];
- }
- else if([file hasSuffix: @"html"] == YES )
- {
- /*
- FILE *htm_file;
- char buffer[262144], buf[1024];
- htm_file = fopen([file UTF8String],"r");
- if(htm_file)
- {
- buffer[0] = 0;
- while((fgets(buf,sizeof(buf),htm_file))!=NULL)
- {
- strlcat(buffer,buf,sizeof(buffer));
- }
- fclose(htm_file);
- [fileView setHTML: [[NSString alloc] initWithCString: buffer]];
- }
- [ transitionView transition: pages_scroll_left toView: fileView ];
- */
- /*
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:
- [NSURL URLWithString: [[NSString alloc] initWithFormat: @"file:///%@",file]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
- [htmlView loadRequest: theRequest];
- [ transitionView transition: pages_scroll_left toView: htmlView ];
- */
- NSString *str = [NSString stringWithFormat:@"file:///%@",file];
- NSURL *url = [NSURL URLWithString:str];
- NSMutableString *aboutContent = [NSMutableString stringWithCapacity:10240];
- if(url){
- NSString *fname = [[NSString alloc] initWithContentsOfURL:url encoding: NSUTF8StringEncoding error:NULL];
- if(fname && [fname length] > 0){
- [aboutContent appendString:fname];
- }
- }
- [fileView setTextSize:16];
- [fileView setHTML:aboutContent];
- [fileView setEditable:NO];
- [fileView setContentSize:CGSizeMake(320,800)];
- [fileView setAllowsRubberBanding: YES];
- [ transitionView transition: pages_scroll_left toView: fileView ];
- }
- else if([file hasSuffix: @"plist"]==YES)
- {
- NSMutableString *plistContent = [NSMutableString stringWithCapacity:10240];
- NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:file];
- //NSEnumerator *keyenum = [dic keyEnumerator];
- /*
- id key;
- while(key = [keyenum nextObject])
- {
- NSString *pkey = [[NSString alloc] initWithString : (NSString*)key];
- NSString *pvalue = [[NSString alloc] initWithFormat : @"%@", [dic valueForKey: pkey]];
- NSString *pkeyvalue = [[NSString alloc] initWithFormat: @"%@ : %@nn", pkey, pvalue];
- //[plistContent appendString: pkeyvalue];
- [plistContent insertString: pkeyvalue atIndex:0];
- }
- */
- NSString *pkeyvalue = [[NSString alloc] initWithFormat : @"%@", dic];
- [plistContent appendString : pkeyvalue];
- [fileView setTextSize:16];
- [fileView setText:plistContent];
- [fileView setEditable:NO];
- [fileView setContentSize:CGSizeMake(320,800)];
- [fileView setAllowsRubberBanding: YES];
- [ transitionView transition: pages_scroll_left toView: fileView ];
- }
- else if([file hasSuffix: @"restar"]==YES)
- {
- [self showmsg:@"Message" content:@"Do you wanna restar now?"];
- }
- else
- {
- [fileView setText:@"can't read."];
- [ transitionView transition: pages_scroll_left toView: fileView ];
- }
- }
- -(void)setNavBar:(int)state withTitle:(NSString*)title
- {
- switch(state)
- {
- case 0:
- [ navBar showLeftButton:nil withStyle: 0 rightButton:@"back" withStyle: 0 ];
- break;
- case 1:
- if(depth<=0)
- {
- [ navBar showLeftButton:@"about" withStyle: 0 rightButton:nil withStyle: 0 ];
- }
- else
- {
- [ navBar showLeftButton:@"back" withStyle: 0 rightButton:nil withStyle: 0 ];
- }
- break;
- case 2:
- [ navBar showLeftButton:nil withStyle: 0 rightButton:@"close" withStyle: 0 ];
- break;
- }
- currentPage = state;
- [ navTitle setTitle: title ];
- }
- - (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button
- {
- switch(button)
- {
- case 1:
- if(depth==0)
- {
- [self showabout];
- }
- else
- {
- [self showparent];
- }
- break;
- case 0:
- if(currentPage == 0)
- {
- [self showrootdir];
- }
- else
- {
- [self showparent];
- }
- break;
- }
- }
- -(void)showmsg:(NSString*)title content:(NSString*)body
- {
- [alertSheet setTitle: title];
- [alertSheet setBodyText: body];
- [alertSheet popupAlertAnimated:YES];
- }
- @end