FileBrowser.m.svn-base
上传用户:kc0325
上传日期:2020-06-20
资源大小:204k
文件大小:7k
源码类别:

iPhone

开发平台:

Objective-C

  1. #import "FileBrowser.h"
  2. #define kToolbarHeight 80
  3. @implementation FileBrowser
  4. - (id)initWithRoot:(NSString*)_folder title:(NSString*)_titl server:(HTTPServer*)_server
  5. {
  6.     if (self = [super initWithStyle:UITableViewStylePlain]) 
  7. {
  8. folder = [_folder retain];
  9. server = [_server retain];
  10. if (_titl)
  11. self.title = _titl;
  12. else
  13. self.title = [folder lastPathComponent];
  14. current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
  15. UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
  16. self.navigationItem.rightBarButtonItem = editButton;
  17. [editButton release];
  18. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NewFileUploaded" object:nil];
  19. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayInfoUpdate:) name:@"LocalhostAdressesResolved" object:nil];
  20.     }
  21.     return self;
  22. }
  23. - (void)loadView 
  24. {
  25. [super loadView];
  26. UIToolbar *toolbar = [UIToolbar new];
  27. toolbar.barStyle = UIBarStyleDefault;
  28. CGRect mainViewBounds = self.navigationController.view.bounds;
  29. [toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
  30.  CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - kToolbarHeight + 2.0,
  31.  CGRectGetWidth(mainViewBounds),
  32.  kToolbarHeight)];
  33. [self.navigationController.view addSubview:toolbar];
  34. }
  35. - (void)refresh
  36. {
  37. current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
  38. [self.tableView reloadData];
  39. }
  40. - (void)editAction:(id)sender
  41. {
  42. if ([self.tableView isEditing])
  43. {
  44. [self.tableView setEditing:NO animated:YES];
  45. UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
  46. self.navigationItem.rightBarButtonItem = editButton;
  47. [editButton release];
  48. }
  49. else
  50. {
  51. [self.tableView setEditing:YES animated:YES];
  52. UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editAction:)];
  53. self.navigationItem.rightBarButtonItem = doneButton;
  54. [doneButton release];
  55. }
  56. }
  57. - (void)displayInfoUpdate:(NSNotification *) notification
  58. {
  59. NSLog(@"displayInfoUpdate: %@", [server name]);
  60. NSDictionary *addresses = [notification object];
  61. UILabel *displayInfo = [UILabel new];
  62. CGRect mainViewBounds = self.navigationController.view.bounds;
  63. [displayInfo setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
  64.  CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - kToolbarHeight + 2.0,
  65.  CGRectGetWidth(mainViewBounds),
  66.  kToolbarHeight)];
  67. displayInfo.textAlignment = UITextAlignmentCenter;
  68. displayInfo.backgroundColor = [UIColor clearColor];
  69. displayInfo.textColor = [UIColor whiteColor];
  70. displayInfo.numberOfLines = 4;
  71. NSString *info;
  72. UInt16 port = [server port];
  73. NSString *localIP = [addresses objectForKey:@"en0"];
  74. if (!localIP)
  75. info = @"Wifi: No Connection !n";
  76. else
  77. info = [NSString stringWithFormat:@"http://iphone.local:%d http://%@:%dn", port, localIP, port];
  78. NSString *wwwIP = [addresses objectForKey:@"www"];
  79. if (wwwIP)
  80. info = [info stringByAppendingFormat:@"Web: %@:%dn", wwwIP, port];
  81. else
  82. info = [info stringByAppendingString:@"Web: No Connectionn"];
  83. displayInfo.text = info;
  84. [self.navigationController.view addSubview:displayInfo];
  85. [displayInfo release];
  86. }
  87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  88. {
  89.     return 1;
  90. }
  91. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  92. {
  93. return [current count];
  94. }
  95. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  96.     
  97.     static NSString *CellIdentifier = @"Cell";
  98.     
  99.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  100.     if (cell == nil) {
  101.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  102.     }
  103.     NSDictionary *fileDict = [[NSFileManager defaultManager] fileAttributesAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] traverseLink:NO];
  104. //NSLog(@"fileDict: %@", fileDict);
  105. if (indexPath.row < [current count])
  106. {
  107. if ([[fileDict objectForKey:NSFileType] isEqualToString: @"NSFileTypeDirectory"])
  108. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  109. else
  110. cell.accessoryType = UITableViewCellAccessoryNone;
  111. cell.text = [current objectAtIndex:indexPath.row];
  112. }
  113.     return cell;
  114. }
  115. -(void)deselectCell:(NSTimer*)_timer
  116. {
  117. [self.tableView deselectRowAtIndexPath:_timer.userInfo animated:YES];
  118. }
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
  120. {
  121. NSDictionary *fileDict = [[NSFileManager defaultManager] fileAttributesAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] traverseLink:NO];
  122. //NSLog(@"fileDict: %@", fileDict);
  123. if ([[fileDict objectForKey:NSFileType] isEqualToString: @"NSFileTypeDirectory"])
  124. [self.navigationController pushViewController:[[FileBrowser alloc] initWithRoot:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] title:nil server:server] animated:YES];
  125. else
  126. [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(deselectCell:) userInfo:indexPath repeats:NO];
  127. }
  128. // Override to support editing the list
  129. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  130.     
  131.     if (editingStyle == UITableViewCellEditingStyleDelete) 
  132. {
  133.         // Delete the row from the data source
  134. [[NSFileManager defaultManager] removeItemAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] error:nil];
  135. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
  136. current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
  137. [tableView reloadData];
  138.     }   
  139. }
  140. /*
  141. - (void)viewWillAppear:(BOOL)animated {
  142.     [super viewWillAppear:animated];
  143. }
  144. */
  145. /*
  146. - (void)viewDidAppear:(BOOL)animated {
  147.     [super viewDidAppear:animated];
  148. }
  149. */
  150. /*
  151. - (void)viewWillDisappear:(BOOL)animated {
  152. }
  153. */
  154. /*
  155. - (void)viewDidDisappear:(BOOL)animated {
  156. }
  157. */
  158. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  159.     // Return YES for supported orientations
  160.     return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
  161. }
  162. - (void)didReceiveMemoryWarning {
  163.     [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  164.     // Release anything that's not essential, such as cached data
  165. }
  166. - (void)dealloc 
  167. {
  168.     [server release];
  169. [folder release];
  170. [current release];
  171. [super dealloc];
  172. }
  173. @end