#import "AppController.h" NSString* LAST_DEST_DIR = @"LastDestDir"; NSString* LAST_SOURCE_FILE = @"LastSourceFile"; NSString* LAST_SEPARATOR = @"LastSeparator"; @implementation AppController - (id)init { [super init]; typesArray = [[NSArray arrayWithObjects: @"txt", @"pdf", nil] retain]; chunker = [[TextChunker alloc] init]; fileManager = [[NSFileManager alloc] init]; userDefaults = [[NSUserDefaults standardUserDefaults] retain]; return self; } - (void)awakeFromNib { //the text fields are set up to be blank by default in the nib if ((nil != [userDefaults objectForKey:LAST_DEST_DIR]) && [fileManager fileExistsAtPath:[userDefaults objectForKey:LAST_DEST_DIR]]) { [destDir setStringValue:[userDefaults stringForKey:LAST_DEST_DIR]]; } if ((nil != [userDefaults objectForKey:LAST_SOURCE_FILE]) && [fileManager fileExistsAtPath:[userDefaults objectForKey:LAST_SOURCE_FILE]]) { [sourceFile setStringValue:[userDefaults stringForKey:LAST_SOURCE_FILE]]; } if (nil != [userDefaults objectForKey:LAST_SEPARATOR]) { [separatorValue setStringValue:[userDefaults stringForKey:LAST_SEPARATOR]]; } //the copy button is set to be disabled by default in the nib if (! (([[sourceFile stringValue] isEqualToString:@""]) || ([[destDir stringValue] isEqualToString:@""])) ) { [copyButton setEnabled:YES]; } } - (IBAction)copyIt:(id)sender { NSString* fileName = [[sourceFile stringValue] stringByExpandingTildeInPath]; [progressIndicator startAnimation:nil]; if ([[fileName pathExtension] isEqualToString:@"pdf"]) { NSLog(@"Trying to open a PDF File...next time"); } else if ([[fileName pathExtension] isEqualToString:@"txt"]) { [chunker chunkIt:fileName toDir:[[destDir stringValue] stringByExpandingTildeInPath] withSeparator:[separatorValue stringValue]]; } else {//this should never happen if users are forced //to select files with the open pane and all values //in typesArray are handled in this event loop NSLog(@"Unrecognized file type"); } [progressIndicator stopAnimation:nil]; [userDefaults setObject:[separatorValue stringValue] forKey:LAST_SEPARATOR]; } - (IBAction)openFileDialog:(id)sender { NSOpenPanel* openPanel; if(0 == [typesArray count]) { typesArray = nil; //allow any type } //configure the open panel openPanel = [NSOpenPanel openPanel]; [openPanel setAllowsMultipleSelection:NO]; [openPanel setTreatsFilePackagesAsDirectories:NO]; [openPanel setResolvesAliases:YES]; if ([sender isEqualTo:sourceButton]) { [openPanel setCanChooseDirectories:NO]; [openPanel setCanChooseFiles:YES]; [openPanel setTitle:@"Source File"]; if ((nil != [userDefaults objectForKey:LAST_SOURCE_FILE]) && [fileManager fileExistsAtPath:[userDefaults objectForKey:LAST_SOURCE_FILE]]) { [openPanel setDirectory:[[userDefaults stringForKey:LAST_SOURCE_FILE] stringByDeletingLastPathComponent]]; } else { [openPanel setDirectory:[@"~" stringByExpandingTildeInPath]]; } } else { [openPanel setCanChooseDirectories:YES]; [openPanel setCanCreateDirectories:YES]; [openPanel setCanChooseFiles:NO]; [openPanel setTitle:@"Destination Directory"]; if ((nil != [userDefaults objectForKey:LAST_DEST_DIR]) && [fileManager fileExistsAtPath:[userDefaults objectForKey:LAST_DEST_DIR]]) { [openPanel setDirectory:[userDefaults stringForKey:LAST_DEST_DIR]]; } else { [openPanel setDirectory:@"/Volumes"]; } } if (NSOKButton == [openPanel runModalForTypes:typesArray]) { NSArray* selection = [openPanel filenames]; if ([sender isEqualTo:sourceButton]) { [sourceFile setStringValue:[selection lastObject]]; } else { [destDir setStringValue:[selection lastObject]]; } } if (! (([[sourceFile stringValue] isEqualToString:@""]) || ([[destDir stringValue] isEqualToString:@""])) ) { [copyButton setEnabled:YES]; } [userDefaults setObject:[destDir stringValue] forKey:LAST_DEST_DIR]; [userDefaults setObject:[sourceFile stringValue] forKey:LAST_SOURCE_FILE]; } - (void)dealloc { [userDefaults synchronize]; [chunker release]; [typesArray release]; [userDefaults release]; [fileManager release]; [super dealloc]; } @end