FileBrowser.m.svn-base
上传用户:kc0325
上传日期:2020-06-20
资源大小:204k
文件大小:7k
- #import "FileBrowser.h"
- #define kToolbarHeight 80
- @implementation FileBrowser
- - (id)initWithRoot:(NSString*)_folder title:(NSString*)_titl server:(HTTPServer*)_server
- {
- if (self = [super initWithStyle:UITableViewStylePlain])
- {
- folder = [_folder retain];
- server = [_server retain];
- if (_titl)
- self.title = _titl;
- else
- self.title = [folder lastPathComponent];
-
- current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
-
- UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
- self.navigationItem.rightBarButtonItem = editButton;
- [editButton release];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NewFileUploaded" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayInfoUpdate:) name:@"LocalhostAdressesResolved" object:nil];
- }
- return self;
- }
- - (void)loadView
- {
- [super loadView];
-
- UIToolbar *toolbar = [UIToolbar new];
- toolbar.barStyle = UIBarStyleDefault;
- CGRect mainViewBounds = self.navigationController.view.bounds;
- [toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
- CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - kToolbarHeight + 2.0,
- CGRectGetWidth(mainViewBounds),
- kToolbarHeight)];
-
-
-
- [self.navigationController.view addSubview:toolbar];
-
- }
- - (void)refresh
- {
- current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
- [self.tableView reloadData];
- }
- - (void)editAction:(id)sender
- {
- if ([self.tableView isEditing])
- {
- [self.tableView setEditing:NO animated:YES];
- UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
- self.navigationItem.rightBarButtonItem = editButton;
- [editButton release];
- }
- else
- {
- [self.tableView setEditing:YES animated:YES];
- UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editAction:)];
- self.navigationItem.rightBarButtonItem = doneButton;
- [doneButton release];
- }
- }
- - (void)displayInfoUpdate:(NSNotification *) notification
- {
- NSLog(@"displayInfoUpdate: %@", [server name]);
- NSDictionary *addresses = [notification object];
-
- UILabel *displayInfo = [UILabel new];
- CGRect mainViewBounds = self.navigationController.view.bounds;
- [displayInfo setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
- CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - kToolbarHeight + 2.0,
- CGRectGetWidth(mainViewBounds),
- kToolbarHeight)];
- displayInfo.textAlignment = UITextAlignmentCenter;
- displayInfo.backgroundColor = [UIColor clearColor];
- displayInfo.textColor = [UIColor whiteColor];
- displayInfo.numberOfLines = 4;
-
- NSString *info;
- UInt16 port = [server port];
- NSString *localIP = [addresses objectForKey:@"en0"];
- if (!localIP)
- info = @"Wifi: No Connection !n";
- else
- info = [NSString stringWithFormat:@"http://iphone.local:%d http://%@:%dn", port, localIP, port];
- NSString *wwwIP = [addresses objectForKey:@"www"];
- if (wwwIP)
- info = [info stringByAppendingFormat:@"Web: %@:%dn", wwwIP, port];
- else
- info = [info stringByAppendingString:@"Web: No Connectionn"];
-
- displayInfo.text = info;
- [self.navigationController.view addSubview:displayInfo];
- [displayInfo release];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [current count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- static NSString *CellIdentifier = @"Cell";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
- }
- NSDictionary *fileDict = [[NSFileManager defaultManager] fileAttributesAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] traverseLink:NO];
- //NSLog(@"fileDict: %@", fileDict);
-
- if (indexPath.row < [current count])
- {
- if ([[fileDict objectForKey:NSFileType] isEqualToString: @"NSFileTypeDirectory"])
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- else
- cell.accessoryType = UITableViewCellAccessoryNone;
-
- cell.text = [current objectAtIndex:indexPath.row];
- }
- return cell;
- }
- -(void)deselectCell:(NSTimer*)_timer
- {
- [self.tableView deselectRowAtIndexPath:_timer.userInfo animated:YES];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSDictionary *fileDict = [[NSFileManager defaultManager] fileAttributesAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] traverseLink:NO];
- //NSLog(@"fileDict: %@", fileDict);
- if ([[fileDict objectForKey:NSFileType] isEqualToString: @"NSFileTypeDirectory"])
- [self.navigationController pushViewController:[[FileBrowser alloc] initWithRoot:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] title:nil server:server] animated:YES];
- else
- [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(deselectCell:) userInfo:indexPath repeats:NO];
- }
- // Override to support editing the list
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
-
- if (editingStyle == UITableViewCellEditingStyleDelete)
- {
- // Delete the row from the data source
- [[NSFileManager defaultManager] removeItemAtPath:[folder stringByAppendingPathComponent:[current objectAtIndex:indexPath.row]] error:nil];
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
- current = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil] copy];
- [tableView reloadData];
- }
- }
- /*
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- }
- */
- /*
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- }
- */
- /*
- - (void)viewWillDisappear:(BOOL)animated {
- }
- */
- /*
- - (void)viewDidDisappear:(BOOL)animated {
- }
- */
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
- // Release anything that's not essential, such as cached data
- }
- - (void)dealloc
- {
- [server release];
- [folder release];
- [current release];
- [super dealloc];
- }
- @end