MainView.m
上传用户:xy800802
上传日期:2020-12-29
资源大小:194k
文件大小:12k
源码类别:

iPhone

开发平台:

Objective-C

  1. #import "MainView.h"
  2. @implementation MainView
  3. - (id)initWithFrame:(CGRect)rect {
  4.     if ((self == [ super initWithFrame: rect ]) != nil) {
  5.         /* -------------------------------------- exploreView -------------------------------------- */
  6.         NSLog(@"exploreView Init");
  7.         exploreView = [[UITextView alloc] initWithFrame: rect];
  8.         [aboutView setTextSize: 18];
  9. [aboutView setMarginTop: 2 ];
  10.         /* -------------------------------------- fileView -------------------------------------- */
  11.         NSLog(@"fileView Init");
  12.         fileView = [[UITextView alloc] initWithFrame: rect];
  13.         [aboutView setTextSize: 18];
  14. [aboutView setMarginTop: 2 ];
  15.         /* -------------------------------------- htmlView -------------------------------------- */
  16.         //NSLog(@"htmlView Init");
  17.         //htmlView = [[UIWebView alloc] initWithFrame: rect];
  18.         //UIScroller *scroller = [[UIScroller alloc] initWithFrame: rect];
  19.         //[htmlView addSubview: scroller];
  20.         
  21. /* -------------------------------------- NavigationBar -------------------------------------- */
  22. NSLog(@"NavigationBar Init");
  23. navBar = [ [UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, 320, 48)];
  24. [ navBar setDelegate: self ];
  25. [ navBar enableAnimation ];
  26. [ navBar setBarStyle: 0 ];
  27.         navTitle = [ [ UINavigationItem alloc ] initWithTitle:@"/" ];
  28.         [ navBar pushNavigationItem: navTitle ];
  29. [ self addSubview: navBar ];
  30. /* -------------------------------------- UITransitionView -------------------------------------- */
  31. NSLog(@"UITransitionView Init");
  32. CGRect tranrect = CGRectMake(rect.origin.x, rect.origin.y + 48.0,
  33.             rect.size.width, rect.size.height - 48.0);
  34. transitionView = [ [ UITransitionView alloc ]initWithFrame: tranrect ];
  35. [ self addSubview: transitionView ];
  36. /* -------------------------------------- UIAlertSheet -------------------------------------- */
  37.         NSLog(@"UIAlertSheet Init");
  38. alertSheet = [[UIAlertSheet alloc] 
  39. initWithFrame : CGRectMake(0,240,320,240)];
  40. [alertSheet setTitle:@"Alert Title"];
  41. [alertSheet setBodyText:@"This is an alert."];
  42. [alertSheet setDestructiveButton :
  43. [alertSheet addButtonWithTitle:@"Yes"]];
  44. [alertSheet addButtonWithTitle:@"No"];
  45. [alertSheet setDelegate: self];
  46.         //[alertSheet popupAlertAnimated:YES];
  47.         [self initTable];
  48. [self showrootdir];
  49.         /* Allocate initial resources here */
  50.     }
  51.     return self;
  52. }
  53. - (void)dealloc
  54. {
  55. NSLog(@"MainView Release");
  56.     /* Deallocate any resources here */
  57. [ aboutView dealloc];
  58.     [ exploreView dealloc];
  59.     [ fileView dealloc];
  60. [ transitionView dealloc];
  61. [ navBar release];
  62.     [ self dealloc ];
  63.     [ super dealloc ];
  64. }
  65. -(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
  66. {
  67.   if ( button == 1 )
  68.   {
  69.       NSLog(@"Yes");
  70.       [Util RestartSpringboard];
  71.   }    
  72.   else if ( button == 2 )
  73.     NSLog(@"No");
  74.   [sheet dismiss];
  75. }
  76. -(void)initTable
  77. {
  78. CGRect rect = CGRectMake(0, 0, 320.0, 480.0 - 64);
  79. exploreTable = [ [ MyTable alloc ] initWithFrame: rect ];
  80. //[ exploreTable setPath: @"/Applications" ];
  81. //[ exploreTable setExtension: @"" ];
  82. //[ exploreTable reloadData ];
  83.     [exploreTable setView:self];
  84. [exploreView addSubview:exploreTable];
  85. }
  86. -(void)showabout
  87. {
  88.     if(aboutView == nil)
  89.     {
  90.         CGRect rect = [ UIHardware fullScreenApplicationContentRect ];
  91.         rect.origin.x = 0;
  92.         rect.origin.y = 0;
  93.         //rect.size.height -= 48.0;
  94.         aboutView = [[UITextView alloc] initWithFrame: rect];
  95.         [aboutView setTextSize: 18];
  96. [aboutView setMarginTop: 2 ];
  97. CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
  98. float opaqueRed[4] = { 1.0, 0.0, 0.0, 1.0 };
  99. CGColorRef red = CGColorCreate(colorSpace, opaqueRed);
  100. [ aboutView setTextColor: red ];
  101.         NSString *str = [NSString stringWithFormat:@"file:///Applications/FileManager.app/about.html"];
  102.         NSURL *url = [NSURL URLWithString:str];
  103.         NSMutableString *aboutContent = [NSMutableString stringWithCapacity:10240];
  104.         if(url){
  105.             NSError *err;
  106.             NSString *fname = [[NSString alloc] initWithContentsOfURL:url encoding: NSUTF8StringEncoding error:NULL];
  107.             NSLog(fname);
  108.             if(fname && [fname length] > 0){
  109.                 [aboutContent appendString:fname];
  110.             }
  111.         }        
  112.         [aboutView setTextSize:16];
  113.         [aboutView setHTML:aboutContent];
  114.         [aboutView setEditable:NO];
  115.         [aboutView setContentSize:CGSizeMake(320,800)];
  116.         [aboutView setAllowsRubberBanding: YES];
  117.         /*
  118.         // 不能处理中文
  119. FILE *file;
  120. char buffer[262144], buf[1024];
  121. file = fopen("/Applications/FileManager.app/about.html","r");
  122. if(!file)
  123. {
  124. [aboutView setText:@"Hello,World!"];
  125. [aboutView setEditable:YES];
  126. }
  127. else
  128. {
  129. buffer[0] = 0;
  130. while((fgets(buf,sizeof(buf),file))!=NULL)
  131. {
  132. strlcat(buffer,buf,sizeof(buffer));
  133. }
  134. fclose(file);
  135.             [ aboutView setHTML: [[NSString alloc] initWithCString: buffer]];
  136. }
  137.         */
  138.     }
  139.     [ transitionView transition: pages_scroll_right toView: aboutView ];
  140.     [ self setNavBar:0 withTitle:@"about"];
  141. }
  142. -(void)showparent
  143. {
  144.     depth--;
  145.     [self showpath: [currentPath stringByDeletingLastPathComponent]];
  146. }
  147. -(void)showrootdir
  148. {
  149.     depth=0;
  150.     [self showpath: @"/"];    
  151. }
  152. -(void)showSub:(NSString*)path
  153. {
  154.     depth++;
  155.     [self showpath:path];    
  156. }
  157. -(void)showpath:(NSString*)path
  158. {
  159.     BOOL isdir = NO;
  160.     NSFileManager *fileManager = [NSFileManager defaultManager]; 
  161.     [fileManager fileExistsAtPath:path isDirectory:&isdir];
  162.     currentPath = path;
  163.     if(isdir)
  164.     {        
  165.         [exploreTable setPath: currentPath];
  166.         [exploreTable setExtension: @"" ];
  167.         [exploreTable reloadData];
  168.         [ transitionView transition: pages_scroll_left toView: exploreView ];
  169.         [self setNavBar:1 withTitle:path];
  170.     }
  171.     else
  172.     {
  173.         [self showfile:path];
  174.         [self setNavBar:2 withTitle:path];
  175.     }
  176.     NSLog(@"%d",depth);
  177. }
  178. -(void)showfile:(NSString*)file
  179. {
  180.     // load file
  181.     if([file hasSuffix: @"png"] == YES || 
  182.        [file hasSuffix: @"JPG"] == YES )
  183.     {
  184.         if(imageView!=nil)
  185.         {
  186.             [imageView dealloc];
  187.         }
  188.         CGRect rect = [ UIHardware fullScreenApplicationContentRect ];
  189.         rect.origin.x = 0;
  190.         rect.origin.y = 0;
  191.         imageView = [[UIAutocorrectImageView alloc] initWithFrame: rect image: [UIImage imageAtPath: file]];
  192.         [transitionView transition: pages_scroll_left toView: imageView ];
  193.     }
  194.     else if([file hasSuffix: @"txt"] == YES)
  195.     {
  196.         FILE *txt_file;
  197. char buffer[262144], buf[1024];
  198. txt_file = fopen([file UTF8String],"r");
  199. if(txt_file)
  200. {
  201. buffer[0] = 0;
  202. while((fgets(buf,sizeof(buf),txt_file))!=NULL)
  203. {
  204. strlcat(buffer,buf,sizeof(buffer));
  205. }
  206. fclose(txt_file);
  207.             [fileView setText: [[NSString alloc] initWithCString: buffer]]; // encoding: NSASCIIStringEncoding / NSUTF8StringEncoding
  208. }
  209.         [ transitionView transition: pages_scroll_left toView: fileView ];
  210.     }
  211.     else if([file hasSuffix: @"html"] == YES )
  212.     {
  213.         /*
  214.         FILE *htm_file;
  215. char buffer[262144], buf[1024];
  216. htm_file = fopen([file UTF8String],"r");
  217. if(htm_file)
  218. {
  219. buffer[0] = 0;
  220. while((fgets(buf,sizeof(buf),htm_file))!=NULL)
  221. {
  222. strlcat(buffer,buf,sizeof(buffer));
  223. }
  224. fclose(htm_file);
  225.             [fileView setHTML: [[NSString alloc] initWithCString: buffer]];
  226. }
  227.         [ transitionView transition: pages_scroll_left toView: fileView ];
  228.         */
  229.         /*
  230.         NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL: 
  231.             [NSURL URLWithString: [[NSString alloc] initWithFormat: @"file:///%@",file]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
  232.         [htmlView loadRequest: theRequest];
  233.         [ transitionView transition: pages_scroll_left toView: htmlView ];
  234.         */
  235.         NSString *str = [NSString stringWithFormat:@"file:///%@",file];
  236.         NSURL *url = [NSURL URLWithString:str];
  237.         NSMutableString *aboutContent = [NSMutableString stringWithCapacity:10240];
  238.         if(url){
  239.             NSString *fname = [[NSString alloc] initWithContentsOfURL:url encoding: NSUTF8StringEncoding error:NULL];
  240.             if(fname && [fname length] > 0){
  241.                 [aboutContent appendString:fname];
  242.             }
  243.         }
  244.         [fileView setTextSize:16];
  245.         [fileView setHTML:aboutContent];
  246.         [fileView setEditable:NO];
  247.         [fileView setContentSize:CGSizeMake(320,800)];
  248.         [fileView setAllowsRubberBanding: YES];
  249.         [ transitionView transition: pages_scroll_left toView: fileView ];
  250.     }
  251.     else if([file hasSuffix: @"plist"]==YES)
  252.     {
  253.         NSMutableString *plistContent = [NSMutableString stringWithCapacity:10240];
  254.         NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:file];
  255.         //NSEnumerator *keyenum = [dic keyEnumerator];
  256.         /*
  257.         id key;
  258.         while(key = [keyenum nextObject])
  259.         {
  260.             NSString *pkey = [[NSString alloc] initWithString : (NSString*)key];
  261.             NSString *pvalue = [[NSString alloc] initWithFormat : @"%@", [dic valueForKey: pkey]];
  262.             NSString *pkeyvalue = [[NSString alloc] initWithFormat: @"%@ : %@nn", pkey, pvalue];
  263.             //[plistContent appendString: pkeyvalue];
  264.             [plistContent insertString: pkeyvalue atIndex:0];
  265.         }
  266.         */
  267.         NSString *pkeyvalue = [[NSString alloc] initWithFormat : @"%@", dic];
  268.         [plistContent appendString : pkeyvalue];
  269.         [fileView setTextSize:16];
  270.         [fileView setText:plistContent];
  271.         [fileView setEditable:NO];
  272.         [fileView setContentSize:CGSizeMake(320,800)];
  273.         [fileView setAllowsRubberBanding: YES];
  274.         [ transitionView transition: pages_scroll_left toView: fileView ];
  275.     }
  276.     else if([file hasSuffix: @"restar"]==YES)
  277.     {
  278.         [self showmsg:@"Message" content:@"Do you wanna restar now?"];
  279.     }
  280.     else
  281.     {
  282.         [fileView setText:@"can't read."];
  283.         [ transitionView transition: pages_scroll_left toView: fileView ];
  284.     }
  285. }
  286. -(void)setNavBar:(int)state withTitle:(NSString*)title
  287. {
  288. switch(state)
  289.     {
  290.     case 0:
  291.         [ navBar showLeftButton:nil withStyle: 0 rightButton:@"back" withStyle: 0 ];
  292.         break;
  293.     case 1:
  294.         if(depth<=0)
  295.         {
  296.             [ navBar showLeftButton:@"about" withStyle: 0 rightButton:nil withStyle: 0 ];
  297.         }
  298.         else
  299.         {
  300.             [ navBar showLeftButton:@"back" withStyle: 0 rightButton:nil withStyle: 0 ];
  301.         }        
  302.         break;
  303.     case 2:
  304.             [ navBar showLeftButton:nil withStyle: 0 rightButton:@"close" withStyle: 0 ];
  305.         break;
  306.     }
  307.     currentPage = state;
  308.     [ navTitle setTitle: title ];
  309. }
  310. - (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button
  311. {
  312. switch(button)
  313. {
  314. case 1:
  315. if(depth==0)
  316.             {
  317.                 [self showabout];
  318.             }
  319.             else
  320.             {
  321.                 [self showparent];
  322.             }
  323. break;
  324. case 0:
  325. if(currentPage == 0)
  326.             {
  327.                 [self showrootdir];
  328.             }
  329.             else
  330.             {
  331.                 [self showparent];
  332.             }
  333. break;
  334. }
  335. }
  336. -(void)showmsg:(NSString*)title content:(NSString*)body
  337. {
  338.     [alertSheet setTitle: title];
  339. [alertSheet setBodyText: body];
  340.     [alertSheet popupAlertAnimated:YES];
  341. }
  342. @end