- #import "MyTable.h"
- #import "DeletableCell.h"
- #import "MainView.h"
- #import <dirent.h>
- @implementation MyTable
- -(id)initWithFrame : (struct CGRect)rect
- {
- if((self == [super initWithFrame: rect]) != nil){
- colFilename = [ [ UITableColumn alloc ]
- initWithTitle: @"Filename"
- identifier:@"filename"
- width: rect.size.width
- ];
- [self addTableColumn: colFilename];
- /*
- colType = [ [ UITableColumn alloc ]
- initWithTitle: @"Type"
- identifier:@"type"
- width: 75
- ];
- [self addTableColumn: colType];
- */
- [self setSeparatorStyle:1];
- [self setDelegate: self];
- [self setDataSource: self];
- [self setRowHeight: 64 ];
- fileList = [[NSMutableArray alloc] init];
- }
- return self;
- }
- -(void)dealloc
- {
- [ colFilename release ];
- [ colType release ];
- [ fileList release ];
- [super dealloc];
- }
- -(int)numberOfRowsInTable:(UITable *)_table
- {
- return [ fileList count ];
- }
- -(UITableCell *)table:(UITable *)table
- cellForRow:(int)row
- column:(UITableColumn *)col
- {
- //if (col == colFilename) {
- DeletableCell *cell = [ [ DeletableCell alloc ] init ];
- [ cell setTable: self ];
- NSString *imgpath;
- if([path isEqualToString:@"/Applications"])
- {
- imgpath = [[ NSString alloc ] initWithFormat: @"/Applications/%@/icon.png", [ fileList objectAtIndex: row ]];
- }
- else
- {
- NSString *filePath = [[ NSString alloc ] initWithFormat: @"%@/%@",path, [ fileList objectAtIndex: row ]];
- BOOL isdir = NO;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- [fileManager fileExistsAtPath:filePath isDirectory:&isdir];
- if(isdir == YES)
- {
- //NSLog(@"folder");
- imgpath = [[NSString alloc] initWithFormat: @"/Applications/FileManager.app/folder.png"];
- }
- else
- {
- if([filePath hasSuffix: @"png"] == YES)
- {
- imgpath = [[NSString alloc] initWithFormat: @"/Applications/FileManager.app/png.png"];
- }
- else if([filePath hasSuffix: @"JPG"] == YES)
- {
- imgpath = [[NSString alloc] initWithFormat: @"/Applications/FileManager.app/jpg.png"];
- }
- else if([filePath hasSuffix: @"plist"] == YES)
- {
- imgpath = [[NSString alloc] initWithFormat: @"/Applications/FileManager.app/xml.png"];
- }
- else
- {
- imgpath = [[NSString alloc] initWithFormat: @"/Applications/FileManager.app/file.png"];
- }
- //NSLog(@"file");
- }
- [filePath release];
- }
- UIImageView *image = [ [ UIImage alloc ]
- initWithContentsOfFile: imgpath];
- [ cell setTitle: [ [ fileList objectAtIndex: row ]
- stringByDeletingPathExtension ]];
- [ cell setImage: image ];
- [ cell setShowDisclosure: YES ];
- [ cell setDisclosureStyle: 3 ];
- return [ cell autorelease ];
- //}
- /*
- else if (col == colType) {
- DeletableCell *cell = [ [ DeletableCell alloc ] init ];
- [ cell setTable: self ];
- [ cell setTitle: extension ];
- return [ cell autorelease ];
- }
- */
- }
- -(int)swipe:(int)type withEvent:(struct __GSEvent *)event;
- {
- CGPoint point= GSEventGetLocationInWindow(event);
- CGPoint offset = _startOffset;
- if (point.x < 100 || point.x > 200) {
- point.x += offset.x;
- point.y += offset.y-48;
- int row = [ self rowAtPoint:point ];
- [ [ self visibleCellForRow: row column: 0 ]
- _showDeleteOrInsertion: YES
- withDisclosure: NO
- animated: YES
- isDelete: YES
- andRemoveConfirmation: YES
- ];
- return [ super swipe:type withEvent:event ];
- }
- //return 0;
- }
- -(void)_willDeleteRow:(int)row
- forTableCell:(id)cell
- viaEdge:(int)edge
- animateOthers:(BOOL)animate
- {
- [ fileList removeObjectAtIndex: row ];
- [ super _willDeleteRow: row forTableCell: cell viaEdge: edge
- animateOthers: animate ];
- }
- -(void)tableRowSelected:(NSNotification *)notification
- {
- NSString *fileName = [ fileList objectAtIndex: [ self selectedRow ] ];
- // file selected
- NSLog(fileName);
- //[self setPath: [path stringByAppendingPathComponent: fileName]];
- //[self reloadData];
- [(MainView *)mainView showSub:[path stringByAppendingPathComponent: fileName]];
- }
- -(void)setPath:(NSString *)_path
- {
- path = [ _path copy];
- }
- -(void)setView:(UIView *)_mainView
- {
- mainView = _mainView;
- }
- -(void)setExtension:(NSString *)_extension
- {
- extension = [ _extension copy ];
- }
- -(void)reloadData
- {
- NSFileManager *fileManager = [ NSFileManager defaultManager ];
- NSDirectoryEnumerator *dirEnum;
- NSString *file;
- BOOL isdir;
- if ([ fileManager fileExistsAtPath: path isDirectory:&isdir] == NO) {
- return;
- }
- if(!isdir)
- {
- return;
- }
- [ fileList removeAllObjects ];
- /* 会获取所有目录及子目录 */
- //dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath: path ];
- //while ((file = [ dirEnum nextObject ])) {
- //if ([ file hasSuffix: extension ] == YES) {
- // [ fileList addObject: file ];
- //}
- //}
- /* 会获取所有目录及子目录 */
- //NSArray *flist = [[ NSFileManager defaultManager ] subpathsAtPath: path];
- //int i=0;
- //NSEnumerator *enumerator = [flist objectEnumerator];
- //id obj;
- //while (obj = [enumerator nextObject]) {
- // file = (NSString*)obj;
- // [ fileList addObject: file ];
- //}
- NSArray *flist = [[NSFileManager defaultManager] directoryContentsAtPath: path];
- int i=0;
- NSEnumerator *enumerator = [flist objectEnumerator];
- id obj;
- while (obj = [enumerator nextObject]) {
- file = (NSString*)obj;
- [ fileList addObject: file ];
- }
- /*
- // 不支持中文
- DIR *dp;
- struct dirent *dirp;
- const char *fpath = [path UTF8String];
- dp=opendir(fpath);
- while((dirp=readdir(dp))!=NULL)
- {
- if(strcmp(".",dirp->d_name)==0 || strcmp("..",dirp->d_name)==0) continue;
- NSString *file = [Util GetNSString : dirp->d_name];
- [ fileList addObject: file ];
- }
- close(dp);
- */
- [ super reloadData ];
- }
- @end