From a8d91b890734e8bd8d400dfe756c7c1dc6e7dfc6 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Mon, 26 Nov 2018 15:53:06 -0500 Subject: [PATCH] Remove FileNamePlaceholders as a dependency (use as a param instead) --- NAPS2.Sdk/Automation/AutomatedScanning.cs | 24 ++-- NAPS2.Sdk/ImportExport/AutoSave.cs | 20 ++-- .../ImportExport/FileNamePlaceholders.cs | 73 ------------ .../Images/SaveImagesOperation.cs | 14 +-- .../ImportExport/Pdf/SavePdfOperation.cs | 10 +- NAPS2.Sdk/ImportExport/Placeholders.cs | 107 ++++++++++++++++++ NAPS2.Sdk/NAPS2.Sdk.csproj | 2 +- NAPS2.Sdk/Scan/Batch/BatchScanPerformer.cs | 24 ++-- NAPS2.Sdk/WinForms/FPlaceholders.cs | 33 +++--- NAPS2.Sdk/WinForms/WinFormsExportHelper.cs | 12 +- 10 files changed, 168 insertions(+), 151 deletions(-) delete mode 100644 NAPS2.Sdk/ImportExport/FileNamePlaceholders.cs create mode 100644 NAPS2.Sdk/ImportExport/Placeholders.cs diff --git a/NAPS2.Sdk/Automation/AutomatedScanning.cs b/NAPS2.Sdk/Automation/AutomatedScanning.cs index 381f247fc..1dccaead3 100644 --- a/NAPS2.Sdk/Automation/AutomatedScanning.cs +++ b/NAPS2.Sdk/Automation/AutomatedScanning.cs @@ -31,7 +31,6 @@ namespace NAPS2.Automation private readonly IErrorOutput errorOutput; private readonly IScannedImageImporter scannedImageImporter; private readonly PdfSettingsContainer pdfSettingsContainer; - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly ImageSettingsContainer imageSettingsContainer; private readonly IOperationFactory operationFactory; private readonly OcrManager ocrManager; @@ -42,11 +41,11 @@ namespace NAPS2.Automation private List> scanList; private int pagesScanned; private int totalPagesScanned; - private DateTime startTime; + private Placeholders placeholders; private List actualOutputPaths; private OcrParams ocrParams; - public AutomatedScanning(AutomatedScanningOptions options, IProfileManager profileManager, IScanPerformer scanPerformer, IErrorOutput errorOutput, IEmailProviderFactory emailProviderFactory, IScannedImageImporter scannedImageImporter, PdfSettingsContainer pdfSettingsContainer, FileNamePlaceholders fileNamePlaceholders, ImageSettingsContainer imageSettingsContainer, IOperationFactory operationFactory, OcrManager ocrManager, IFormFactory formFactory, GhostscriptManager ghostscriptManager) + public AutomatedScanning(AutomatedScanningOptions options, IProfileManager profileManager, IScanPerformer scanPerformer, IErrorOutput errorOutput, IEmailProviderFactory emailProviderFactory, IScannedImageImporter scannedImageImporter, PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, IOperationFactory operationFactory, OcrManager ocrManager, IFormFactory formFactory, GhostscriptManager ghostscriptManager) { this.options = options; this.profileManager = profileManager; @@ -55,7 +54,6 @@ namespace NAPS2.Automation this.emailProviderFactory = emailProviderFactory; this.scannedImageImporter = scannedImageImporter; this.pdfSettingsContainer = pdfSettingsContainer; - this.fileNamePlaceholders = fileNamePlaceholders; this.imageSettingsContainer = imageSettingsContainer; this.operationFactory = operationFactory; this.ocrManager = ocrManager; @@ -82,7 +80,7 @@ namespace NAPS2.Automation return; } - startTime = DateTime.Now; + placeholders = Placeholders.All.WithDate(DateTime.Now); ConsoleOverwritePrompt.ForceOverwrite = options.ForceOverwrite; if (options.Install != null) @@ -246,7 +244,7 @@ namespace NAPS2.Automation // Email, so no check needed return true; } - var subPath = fileNamePlaceholders.SubstitutePlaceholders(options.OutputPath, startTime); + var subPath = placeholders.Substitute(options.OutputPath); if (IsPdfFile(subPath) && File.Exists(subPath) && !options.ForceOverwrite) @@ -302,8 +300,8 @@ namespace NAPS2.Automation var message = new EmailMessage { - Subject = fileNamePlaceholders.SubstitutePlaceholders(options.EmailSubject, startTime, false) ?? "", - BodyText = fileNamePlaceholders.SubstitutePlaceholders(options.EmailBody, startTime, false), + Subject = placeholders.Substitute(options.EmailSubject, false) ?? "", + BodyText = placeholders.Substitute(options.EmailBody, false), AutoSend = options.EmailAutoSend, SilentSend = options.EmailSilentSend }; @@ -327,7 +325,7 @@ namespace NAPS2.Automation int i = 0; foreach (var path in actualOutputPaths) { - string attachmentName = fileNamePlaceholders.SubstitutePlaceholders(options.EmailFileName, startTime, false, i++, scanList.Count > 1 ? digits : 0); + string attachmentName = placeholders.Substitute(options.EmailFileName, false, i++, scanList.Count > 1 ? digits : 0); message.Attachments.Add(new EmailAttachment { FilePath = path, @@ -434,7 +432,7 @@ namespace NAPS2.Automation private async Task ExportToImageFiles() { - var path = fileNamePlaceholders.SubstitutePlaceholders(options.OutputPath, startTime); + var path = placeholders.Substitute(options.OutputPath); await DoExportToImageFiles(options.OutputPath); OutputVerbose(ConsoleResources.FinishedSavingImages, Path.GetFullPath(path)); } @@ -460,7 +458,7 @@ namespace NAPS2.Automation i = op.Status.CurrentProgress; } }; - op.Start(outputPath, startTime, scan); + op.Start(outputPath, placeholders, scan); await op.Success; } } @@ -548,8 +546,8 @@ namespace NAPS2.Automation } }; int digits = (int)Math.Floor(Math.Log10(scanList.Count)) + 1; - string actualPath = fileNamePlaceholders.SubstitutePlaceholders(path, startTime, true, scanIndex++, scanList.Count > 1 ? digits : 0); - op.Start(actualPath, startTime, fileContents, pdfSettings, ocrParams, email, null); + string actualPath = placeholders.Substitute(path, true, scanIndex++, scanList.Count > 1 ? digits : 0); + op.Start(actualPath, placeholders, fileContents, pdfSettings, ocrParams, email, null); if (!await op.Success) { return false; diff --git a/NAPS2.Sdk/ImportExport/AutoSave.cs b/NAPS2.Sdk/ImportExport/AutoSave.cs index 0623cf7aa..a45ab8f6a 100644 --- a/NAPS2.Sdk/ImportExport/AutoSave.cs +++ b/NAPS2.Sdk/ImportExport/AutoSave.cs @@ -23,17 +23,15 @@ namespace NAPS2.ImportExport private readonly PdfSettingsContainer pdfSettingsContainer; private readonly OcrManager ocrManager; private readonly IErrorOutput errorOutput; - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly DialogHelper dialogHelper; private readonly IOperationProgress operationProgress; - public AutoSave(IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IErrorOutput errorOutput, FileNamePlaceholders fileNamePlaceholders, DialogHelper dialogHelper, IOperationProgress operationProgress) + public AutoSave(IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IErrorOutput errorOutput, DialogHelper dialogHelper, IOperationProgress operationProgress) { this.operationFactory = operationFactory; this.pdfSettingsContainer = pdfSettingsContainer; this.ocrManager = ocrManager; this.errorOutput = errorOutput; - this.fileNamePlaceholders = fileNamePlaceholders; this.dialogHelper = dialogHelper; this.operationProgress = operationProgress; } @@ -47,13 +45,13 @@ namespace NAPS2.ImportExport try { bool ok = true; - DateTime now = DateTime.Now; + var placeholders = Placeholders.All.WithDate(DateTime.Now); int i = 0; string firstFileSaved = null; var scans = SaveSeparatorHelper.SeparateScans(new[] { images }, settings.Separator).ToList(); foreach (var imageList in scans) { - (bool success, string filePath) = await SaveOneFile(settings, now, i++, imageList, scans.Count == 1 ? notify : null); + (bool success, string filePath) = await SaveOneFile(settings, placeholders, i++, imageList, scans.Count == 1 ? notify : null); if (!success) { ok = false; @@ -80,18 +78,18 @@ namespace NAPS2.ImportExport } } - private async Task<(bool, string)> SaveOneFile(AutoSaveSettings settings, DateTime now, int i, List images, ISaveNotify notify) + private async Task<(bool, string)> SaveOneFile(AutoSaveSettings settings, Placeholders placeholders, int i, List images, ISaveNotify notify) { if (images.Count == 0) { return (true, null); } - string subPath = fileNamePlaceholders.SubstitutePlaceholders(settings.FilePath, now, true, i); + string subPath = placeholders.Substitute(settings.FilePath, true, i); if (settings.PromptForFilePath) { if (dialogHelper.PromptToSavePdfOrImage(subPath, out string newPath)) { - subPath = fileNamePlaceholders.SubstitutePlaceholders(newPath, now, true, i); + subPath = placeholders.Substitute(newPath, true, i); } } var extension = Path.GetExtension(subPath); @@ -99,10 +97,10 @@ namespace NAPS2.ImportExport { if (File.Exists(subPath)) { - subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); + subPath = placeholders.Substitute(subPath, true, 0, 1); } var op = operationFactory.Create(); - if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, false, null)) + if (op.Start(subPath, placeholders, images, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, false, null)) { operationProgress.ShowProgress(op); } @@ -116,7 +114,7 @@ namespace NAPS2.ImportExport else { var op = operationFactory.Create(); - if (op.Start(subPath, now, images)) + if (op.Start(subPath, placeholders, images)) { operationProgress.ShowProgress(op); } diff --git a/NAPS2.Sdk/ImportExport/FileNamePlaceholders.cs b/NAPS2.Sdk/ImportExport/FileNamePlaceholders.cs deleted file mode 100644 index 62ee46181..000000000 --- a/NAPS2.Sdk/ImportExport/FileNamePlaceholders.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; - -namespace NAPS2.ImportExport -{ - public class FileNamePlaceholders - { - public const string YEAR_4_DIGITS = "$(YYYY)"; - public const string YEAR_2_DIGITS = "$(YY)"; - public const string MONTH_2_DIGITS = "$(MM)"; - public const string DAY_2_DIGITS = "$(DD)"; - public const string HOUR_24_CLOCK = "$(hh)"; - public const string MINUTE_2_DIGITS = "$(mm)"; - public const string SECOND_2_DIGITS = "$(ss)"; - public const string NUMBER_4_DIGITS = "$(nnnn)"; - public const string NUMBER_3_DIGITS = "$(nnn)"; - public const string NUMBER_2_DIGITS = "$(nn)"; - public const string NUMBER_1_DIGIT = "$(n)"; - - private static readonly Dictionary> Placeholders = new Dictionary> - { - { YEAR_4_DIGITS, dateTime => dateTime.ToString("yyyy") }, - { YEAR_2_DIGITS, dateTime => dateTime.ToString("yy") }, - { MONTH_2_DIGITS, dateTime => dateTime.ToString("MM") }, - { DAY_2_DIGITS, dateTime => dateTime.ToString("dd") }, - { HOUR_24_CLOCK, dateTime => dateTime.ToString("HH") }, - { MINUTE_2_DIGITS, dateTime => dateTime.ToString("mm") }, - { SECOND_2_DIGITS, dateTime => dateTime.ToString("ss") }, - }; - - private static readonly Regex NumberPlaceholderPattern = new Regex(@"\$\(n+\)"); - - public string SubstitutePlaceholders(string fileNameWithPath, DateTime dateTime, bool incrementIfExists = true, int numberSkip = 0, int autoNumberDigits = 0) - { - if (fileNameWithPath == null) - { - return null; - } - // Start with environment variables - string result = Environment.ExpandEnvironmentVariables(fileNameWithPath); - // Most placeholders don't need a special case - result = Placeholders.Aggregate(result, (current, ph) => current.Replace(ph.Key, ph.Value(dateTime))); - // One does, however - var match = NumberPlaceholderPattern.Match(result); - if (match.Success) - { - result = NumberPlaceholderPattern.Replace(result, ""); - result = SubstituteNumber(result, match.Index, match.Length - 3, numberSkip, true); - } - else if (autoNumberDigits > 0) - { - result = result.Insert(result.Length - Path.GetExtension(result).Length, "."); - result = SubstituteNumber(result, result.Length - Path.GetExtension(result).Length, autoNumberDigits, numberSkip, incrementIfExists); - } - return result; - } - - private string SubstituteNumber(string path, int insertionIndex, int minDigits, int skip, bool incrementIfExists) - { - string result; - int i = skip; - do - { - ++i; - result = path.Insert(insertionIndex, i.ToString("D" + minDigits)); - } while (incrementIfExists && File.Exists(result)); - return result; - } - } -} diff --git a/NAPS2.Sdk/ImportExport/Images/SaveImagesOperation.cs b/NAPS2.Sdk/ImportExport/Images/SaveImagesOperation.cs index 038944ab4..02de80cbf 100644 --- a/NAPS2.Sdk/ImportExport/Images/SaveImagesOperation.cs +++ b/NAPS2.Sdk/ImportExport/Images/SaveImagesOperation.cs @@ -18,15 +18,13 @@ namespace NAPS2.ImportExport.Images { public class SaveImagesOperation : OperationBase { - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly ImageSettingsContainer imageSettingsContainer; private readonly IOverwritePrompt overwritePrompt; private readonly ScannedImageRenderer scannedImageRenderer; private readonly TiffHelper tiffHelper; - public SaveImagesOperation(FileNamePlaceholders fileNamePlaceholders, ImageSettingsContainer imageSettingsContainer, IOverwritePrompt overwritePrompt, ScannedImageRenderer scannedImageRenderer, TiffHelper tiffHelper) + public SaveImagesOperation(ImageSettingsContainer imageSettingsContainer, IOverwritePrompt overwritePrompt, ScannedImageRenderer scannedImageRenderer, TiffHelper tiffHelper) { - this.fileNamePlaceholders = fileNamePlaceholders; this.imageSettingsContainer = imageSettingsContainer; this.overwritePrompt = overwritePrompt; this.scannedImageRenderer = scannedImageRenderer; @@ -44,10 +42,10 @@ namespace NAPS2.ImportExport.Images /// If multiple images are provided, they will be saved to files with numeric identifiers, e.g. img1.jpg, img2.jpg, etc.. /// /// The name of the file to save. For multiple images, this is modified by appending a number before the extension. - /// + /// /// The collection of images to save. /// - public bool Start(string fileName, DateTime dateTime, List images, bool batch = false) + public bool Start(string fileName, Placeholders placeholders, List images, bool batch = false) { Status = new OperationStatus { @@ -59,12 +57,12 @@ namespace NAPS2.ImportExport.Images { try { - var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, batch); + var subFileName = placeholders.Substitute(fileName, batch); if (Directory.Exists(subFileName)) { // Not supposed to be a directory, but ok... fileName = Path.Combine(subFileName, "$(n).jpg"); - subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, batch); + subFileName = placeholders.Substitute(fileName, batch); } ImageFormat format = GetImageFormat(subFileName); @@ -114,7 +112,7 @@ namespace NAPS2.ImportExport.Images } else { - var fileNameN = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, true, i, + var fileNameN = placeholders.Substitute(fileName, true, i, digits); Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(fileNameN)); InvokeStatusChanged(); diff --git a/NAPS2.Sdk/ImportExport/Pdf/SavePdfOperation.cs b/NAPS2.Sdk/ImportExport/Pdf/SavePdfOperation.cs index 72bee0dca..c49e134a2 100644 --- a/NAPS2.Sdk/ImportExport/Pdf/SavePdfOperation.cs +++ b/NAPS2.Sdk/ImportExport/Pdf/SavePdfOperation.cs @@ -16,14 +16,12 @@ namespace NAPS2.ImportExport.Pdf { public class SavePdfOperation : OperationBase { - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly IPdfExporter pdfExporter; private readonly IOverwritePrompt overwritePrompt; private readonly IEmailProviderFactory emailProviderFactory; - public SavePdfOperation(FileNamePlaceholders fileNamePlaceholders, IPdfExporter pdfExporter, IOverwritePrompt overwritePrompt, IEmailProviderFactory emailProviderFactory) + public SavePdfOperation(IPdfExporter pdfExporter, IOverwritePrompt overwritePrompt, IEmailProviderFactory emailProviderFactory) { - this.fileNamePlaceholders = fileNamePlaceholders; this.pdfExporter = pdfExporter; this.overwritePrompt = overwritePrompt; this.emailProviderFactory = emailProviderFactory; @@ -32,10 +30,10 @@ namespace NAPS2.ImportExport.Pdf AllowBackground = true; } - public bool Start(string fileName, DateTime dateTime, ICollection images, PdfSettings pdfSettings, OcrParams ocrParams, bool email, EmailMessage emailMessage) + public bool Start(string fileName, Placeholders placeholders, ICollection images, PdfSettings pdfSettings, OcrParams ocrParams, bool email, EmailMessage emailMessage) { ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress; - var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime); + var subFileName = placeholders.Substitute(fileName); Status = new OperationStatus { StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)), @@ -45,7 +43,7 @@ namespace NAPS2.ImportExport.Pdf if (Directory.Exists(subFileName)) { // Not supposed to be a directory, but ok... - subFileName = fileNamePlaceholders.SubstitutePlaceholders(Path.Combine(subFileName, "$(n).pdf"), dateTime); + subFileName = placeholders.Substitute(Path.Combine(subFileName, "$(n).pdf")); } if (File.Exists(subFileName)) { diff --git a/NAPS2.Sdk/ImportExport/Placeholders.cs b/NAPS2.Sdk/ImportExport/Placeholders.cs new file mode 100644 index 000000000..995649e85 --- /dev/null +++ b/NAPS2.Sdk/ImportExport/Placeholders.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; + +namespace NAPS2.ImportExport +{ + public abstract class Placeholders + { + public const string YEAR_4_DIGITS = "$(YYYY)"; + public const string YEAR_2_DIGITS = "$(YY)"; + public const string MONTH_2_DIGITS = "$(MM)"; + public const string DAY_2_DIGITS = "$(DD)"; + public const string HOUR_24_CLOCK = "$(hh)"; + public const string MINUTE_2_DIGITS = "$(mm)"; + public const string SECOND_2_DIGITS = "$(ss)"; + public const string NUMBER_4_DIGITS = "$(nnnn)"; + public const string NUMBER_3_DIGITS = "$(nnn)"; + public const string NUMBER_2_DIGITS = "$(nn)"; + public const string NUMBER_1_DIGIT = "$(n)"; + + public static DefaultPlaceholders All => new DefaultPlaceholders(); + + public static EnvironmentPlaceholders Env => new EnvironmentPlaceholders(); + + public static StubPlaceholders None => new StubPlaceholders(); + + public abstract string Substitute(string filePath, bool incrementIfExists = true, int numberSkip = 0, int autoNumberDigits = 0); + + public class StubPlaceholders : Placeholders + { + public override string Substitute(string filePath, bool incrementIfExists = true, int numberSkip = 0, int autoNumberDigits = 0) => filePath; + } + + public class EnvironmentPlaceholders : Placeholders + { + public override string Substitute(string filePath, bool incrementIfExists = true, int numberSkip = 0, int autoNumberDigits = 0) => Environment.ExpandEnvironmentVariables(filePath); + } + + public class DefaultPlaceholders : Placeholders + { + private static readonly Dictionary> Replacements = new Dictionary> + { + {YEAR_4_DIGITS, dateTime => dateTime.ToString("yyyy")}, + {YEAR_2_DIGITS, dateTime => dateTime.ToString("yy")}, + {MONTH_2_DIGITS, dateTime => dateTime.ToString("MM")}, + {DAY_2_DIGITS, dateTime => dateTime.ToString("dd")}, + {HOUR_24_CLOCK, dateTime => dateTime.ToString("HH")}, + {MINUTE_2_DIGITS, dateTime => dateTime.ToString("mm")}, + {SECOND_2_DIGITS, dateTime => dateTime.ToString("ss")}, + }; + + private static readonly Regex NumberPlaceholderPattern = new Regex(@"\$\(n+\)"); + + private readonly DateTime? dateTimeOverride; + + public DefaultPlaceholders(DateTime? dateTimeOverride = null) + { + this.dateTimeOverride = dateTimeOverride; + } + + public DefaultPlaceholders WithDate(DateTime dateTime) => new DefaultPlaceholders(dateTime); + + public override string Substitute(string filePath, bool incrementIfExists = true, int numberSkip = 0, int autoNumberDigits = 0) + { + if (filePath == null) + { + return null; + } + + var dateTime = dateTimeOverride ?? DateTime.Now; + // Start with environment variables + string result = Environment.ExpandEnvironmentVariables(filePath); + // Most placeholders don't need a special case + result = Replacements.Aggregate(result, (current, ph) => current.Replace(ph.Key, ph.Value(dateTime))); + // One does, however + var match = NumberPlaceholderPattern.Match(result); + if (match.Success) + { + result = NumberPlaceholderPattern.Replace(result, ""); + result = SubstituteNumber(result, match.Index, match.Length - 3, numberSkip, true); + } + else if (autoNumberDigits > 0) + { + result = result.Insert(result.Length - Path.GetExtension(result).Length, "."); + result = SubstituteNumber(result, result.Length - Path.GetExtension(result).Length, autoNumberDigits, numberSkip, incrementIfExists); + } + + return result; + } + + private string SubstituteNumber(string path, int insertionIndex, int minDigits, int skip, bool incrementIfExists) + { + string result; + int i = skip; + do + { + ++i; + result = path.Insert(insertionIndex, i.ToString("D" + minDigits)); + } while (incrementIfExists && File.Exists(result)); + + return result; + } + } + } +} diff --git a/NAPS2.Sdk/NAPS2.Sdk.csproj b/NAPS2.Sdk/NAPS2.Sdk.csproj index 9c1c1a33a..7d3db6868 100644 --- a/NAPS2.Sdk/NAPS2.Sdk.csproj +++ b/NAPS2.Sdk/NAPS2.Sdk.csproj @@ -360,7 +360,7 @@ - + diff --git a/NAPS2.Sdk/Scan/Batch/BatchScanPerformer.cs b/NAPS2.Sdk/Scan/Batch/BatchScanPerformer.cs index e019f0e96..707b87e0f 100644 --- a/NAPS2.Sdk/Scan/Batch/BatchScanPerformer.cs +++ b/NAPS2.Sdk/Scan/Batch/BatchScanPerformer.cs @@ -23,18 +23,16 @@ namespace NAPS2.Scan.Batch { private readonly IScanPerformer scanPerformer; private readonly IProfileManager profileManager; - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly IPdfExporter pdfExporter; private readonly IOperationFactory operationFactory; private readonly PdfSettingsContainer pdfSettingsContainer; private readonly OcrManager ocrManager; private readonly IFormFactory formFactory; - public BatchScanPerformer(IScanPerformer scanPerformer, IProfileManager profileManager, FileNamePlaceholders fileNamePlaceholders, IPdfExporter pdfExporter, IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IFormFactory formFactory) + public BatchScanPerformer(IScanPerformer scanPerformer, IProfileManager profileManager, IPdfExporter pdfExporter, IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IFormFactory formFactory) { this.scanPerformer = scanPerformer; this.profileManager = profileManager; - this.fileNamePlaceholders = fileNamePlaceholders; this.pdfExporter = pdfExporter; this.operationFactory = operationFactory; this.pdfSettingsContainer = pdfSettingsContainer; @@ -44,7 +42,7 @@ namespace NAPS2.Scan.Batch public async Task PerformBatchScan(BatchSettings settings, FormBase batchForm, Action imageCallback, Action progressCallback, CancellationToken cancelToken) { - var state = new BatchState(scanPerformer, profileManager, fileNamePlaceholders, pdfExporter, operationFactory, pdfSettingsContainer, ocrManager, formFactory) + var state = new BatchState(scanPerformer, profileManager, pdfExporter, operationFactory, pdfSettingsContainer, ocrManager, formFactory) { Settings = settings, ProgressCallback = progressCallback, @@ -59,7 +57,6 @@ namespace NAPS2.Scan.Batch { private readonly IScanPerformer scanPerformer; private readonly IProfileManager profileManager; - private readonly FileNamePlaceholders fileNamePlaceholders; private readonly IPdfExporter pdfExporter; private readonly IOperationFactory operationFactory; private readonly PdfSettingsContainer pdfSettingsContainer; @@ -70,11 +67,10 @@ namespace NAPS2.Scan.Batch private ScanParams scanParams; private List> scans; - public BatchState(IScanPerformer scanPerformer, IProfileManager profileManager, FileNamePlaceholders fileNamePlaceholders, IPdfExporter pdfExporter, IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IFormFactory formFactory) + public BatchState(IScanPerformer scanPerformer, IProfileManager profileManager, IPdfExporter pdfExporter, IOperationFactory operationFactory, PdfSettingsContainer pdfSettingsContainer, OcrManager ocrManager, IFormFactory formFactory) { this.scanPerformer = scanPerformer; this.profileManager = profileManager; - this.fileNamePlaceholders = fileNamePlaceholders; this.pdfExporter = pdfExporter; this.operationFactory = operationFactory; this.pdfSettingsContainer = pdfSettingsContainer; @@ -228,7 +224,7 @@ namespace NAPS2.Scan.Batch { ProgressCallback(MiscResources.BatchStatusSaving); - var now = DateTime.Now; + var placeholders = Placeholders.All.WithDate(DateTime.Now); var allImages = scans.SelectMany(x => x).ToList(); if (Settings.OutputType == BatchOutputType.Load) @@ -240,7 +236,7 @@ namespace NAPS2.Scan.Batch } else if (Settings.OutputType == BatchOutputType.SingleFile) { - await Save(now, 0, allImages); + await Save(placeholders, 0, allImages); foreach (var img in allImages) { img.Dispose(); @@ -251,7 +247,7 @@ namespace NAPS2.Scan.Batch int i = 0; foreach (var imageList in SaveSeparatorHelper.SeparateScans(scans, Settings.SaveSeparator)) { - await Save(now, i++, imageList); + await Save(placeholders, i++, imageList); foreach (var img in imageList) { img.Dispose(); @@ -260,18 +256,18 @@ namespace NAPS2.Scan.Batch } } - private async Task Save(DateTime now, int i, List images) + private async Task Save(Placeholders placeholders, int i, List images) { if (images.Count == 0) { return; } - var subPath = fileNamePlaceholders.SubstitutePlaceholders(Settings.SavePath, now, true, i); + var subPath = placeholders.Substitute(Settings.SavePath, true, i); if (GetSavePathExtension().ToLower() == ".pdf") { if (File.Exists(subPath)) { - subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); + subPath = placeholders.Substitute(subPath, true, 0, 1); } var snapshots = images.Select(x => x.Preserve()).ToList(); try @@ -286,7 +282,7 @@ namespace NAPS2.Scan.Batch else { var op = operationFactory.Create(); - op.Start(subPath, now, images, true); + op.Start(subPath, placeholders, images, true); await op.Success; } } diff --git a/NAPS2.Sdk/WinForms/FPlaceholders.cs b/NAPS2.Sdk/WinForms/FPlaceholders.cs index ca73257a4..8b3b4c378 100644 --- a/NAPS2.Sdk/WinForms/FPlaceholders.cs +++ b/NAPS2.Sdk/WinForms/FPlaceholders.cs @@ -8,11 +8,8 @@ namespace NAPS2.WinForms { public partial class FPlaceholders : FormBase { - private readonly FileNamePlaceholders fileNamePlaceholders; - - public FPlaceholders(FileNamePlaceholders fileNamePlaceholders) + public FPlaceholders() { - this.fileNamePlaceholders = fileNamePlaceholders; RestoreFormState = false; InitializeComponent(); AcceptButton = btnOK; @@ -29,19 +26,19 @@ namespace NAPS2.WinForms // Annoying, but oh well. var placeholders = new[] { - FileNamePlaceholders.YEAR_4_DIGITS, - FileNamePlaceholders.YEAR_2_DIGITS, - FileNamePlaceholders.MONTH_2_DIGITS, - FileNamePlaceholders.DAY_2_DIGITS, - FileNamePlaceholders.HOUR_24_CLOCK, - FileNamePlaceholders.MINUTE_2_DIGITS, - FileNamePlaceholders.SECOND_2_DIGITS, - FileNamePlaceholders.NUMBER_4_DIGITS, - FileNamePlaceholders.NUMBER_3_DIGITS, - FileNamePlaceholders.NUMBER_2_DIGITS, - FileNamePlaceholders.NUMBER_1_DIGIT, - $"{FileNamePlaceholders.YEAR_4_DIGITS}-{FileNamePlaceholders.MONTH_2_DIGITS}-{FileNamePlaceholders.DAY_2_DIGITS}", - $"{FileNamePlaceholders.HOUR_24_CLOCK}_{FileNamePlaceholders.MINUTE_2_DIGITS}_{FileNamePlaceholders.SECOND_2_DIGITS}", + Placeholders.YEAR_4_DIGITS, + Placeholders.YEAR_2_DIGITS, + Placeholders.MONTH_2_DIGITS, + Placeholders.DAY_2_DIGITS, + Placeholders.HOUR_24_CLOCK, + Placeholders.MINUTE_2_DIGITS, + Placeholders.SECOND_2_DIGITS, + Placeholders.NUMBER_4_DIGITS, + Placeholders.NUMBER_3_DIGITS, + Placeholders.NUMBER_2_DIGITS, + Placeholders.NUMBER_1_DIGIT, + $"{Placeholders.YEAR_4_DIGITS}-{Placeholders.MONTH_2_DIGITS}-{Placeholders.DAY_2_DIGITS}", + $"{Placeholders.HOUR_24_CLOCK}_{Placeholders.MINUTE_2_DIGITS}_{Placeholders.SECOND_2_DIGITS}", }; var buttons = gboxPlaceholders.Controls.OfType