//
//  FileOverviewViewController.m
//  FilesystemExplorer
//
//  Created by Chris Adamson on 11/4/08.
//  Copyright 2008 Subsequently and Furthermore, Inc.. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "FileOverviewViewController.h"
#import "FileContentsViewController.h"


@implementation FileOverviewViewController

@synthesize fileNameLabel, fileSizeLabel, fileModifiedLabel;

//START:code.FilesystemExplorer.overviewfilepathproperty
-(NSString*) filePath {
	return filePath;
}

-(void) setFilePath: (NSString*) p {
	[p retain];
	[filePath release];
	filePath = p;
	[self updateFileOverview];
	// also set title of nav controller with last path element
	NSString *pathTitle= [filePath lastPathComponent];
	self.title = pathTitle;
}
//END:code.FilesystemExplorer.overviewfilepathproperty


//START:code.FilesystemExplorer.file-attribute-format		
-(void) updateFileOverview {
	if (self.filePath != NULL) {
		NSString *fileName = [self.filePath lastPathComponent];
		fileNameLabel.text = fileName;
		
		NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
		[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
		[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
		
		NSNumberFormatter *numberFormatter =
		[[NSNumberFormatter alloc] init];
		[numberFormatter setPositiveFormat: @"#,##0.## bytes"];
		//END:code.FilesystemExplorer.file-attribute-format				
		
		//START:code.FilesystemExplorer.file-attribute-load
		NSDictionary *fileAttributes =
		[[NSFileManager defaultManager]
			fileAttributesAtPath: self.filePath
			traverseLink: YES]; 
		NSDate *modificationDate = (NSDate*)
			[fileAttributes objectForKey: NSFileModificationDate];
		NSNumber *fileSize = (NSNumber*)
			[fileAttributes objectForKey: NSFileSize];
		fileSizeLabel.text = 
			[numberFormatter stringFromNumber: fileSize];
		fileModifiedLabel.text =
			[dateFormatter stringFromDate: modificationDate];

		[numberFormatter release];
		[dateFormatter release];
		
	}
}
//END:code.FilesystemExplorer.file-attribute-load

//START:code.FilesystemExplorer.readfilecontents
- (IBAction) readFileContents {
	FileContentsViewController *fileContentsViewController =
		[[FileContentsViewController alloc]
			initWithNibName: @"FileContentsView"
			bundle:nil];
	fileContentsViewController.filePath = filePath;
	fileContentsViewController.title =
		[NSString stringWithFormat: @"%@ contents",
			[filePath lastPathComponent]];
	[[self navigationController] pushViewController:
		fileContentsViewController animated:YES];
	[fileContentsViewController release];
}
//END:code.FilesystemExplorer.readfilecontents


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (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 {
    [super dealloc];
}


@end
