public async Task <bool> SavePDF(List <ScannedImage> images, ISaveNotify notify) { if (images.Any()) { string savePath; var pdfSettings = pdfSettingsContainer.PdfSettings; if (pdfSettings.SkipSavePrompt && Path.IsPathRooted(pdfSettings.DefaultFileName)) { savePath = pdfSettings.DefaultFileName; } else { if (!dialogHelper.PromptToSavePdf(pdfSettings.DefaultFileName, out savePath)) { return(false); } } var subSavePath = fileNamePlaceholders.SubstitutePlaceholders(savePath, DateTime.Now); var changeToken = changeTracker.State; if (await ExportPDF(subSavePath, images, false, null)) { changeTracker.Saved(changeToken); notify?.PdfSaved(subSavePath); return(true); } } return(false); }
public bool Save(AutoSaveSettings settings, List <ScannedImage> images, ISaveNotify notify) { if (appConfigManager.Config.DisableAutoSave) { return(false); } try { bool ok = true; DateTime now = DateTime.Now; int i = 0; string firstFileSaved = null; var scans = SaveSeparatorHelper.SeparateScans(new[] { images }, settings.Separator).ToList(); foreach (var imageList in scans) { if (!SaveOneFile(settings, now, i++, imageList, scans.Count == 1 ? notify : null, ref firstFileSaved)) { ok = false; } } if (notify != null && scans.Count > 1 && ok) { // Can't just do images.Count because that includes patch codes int imageCount = scans.SelectMany(x => x).Count(); notify.ImagesSaved(imageCount, firstFileSaved); } return(ok); } catch (Exception ex) { Log.ErrorException(MiscResources.AutoSaveError, ex); errorOutput.DisplayError(MiscResources.AutoSaveError); return(false); } }
public async Task <bool> SavePdf(List <ScannedImage> images, ISaveNotify notify) { if (!images.Any()) { return(false); } string savePath; var pdfSettings = pdfSettingsContainer.PdfSettings; if (pdfSettings.SkipSavePrompt && Path.IsPathRooted(pdfSettings.DefaultFileName)) { savePath = pdfSettings.DefaultFileName; } else { if (!dialogHelper.PromptToSavePdf(pdfSettings.DefaultFileName, out savePath)) { return(false); } } var changeToken = changeTracker.State; var firstFileSaved = await ExportPdf(savePath, images, false, null); if (firstFileSaved == null) { return(false); } changeTracker.Saved(changeToken); notify?.PdfSaved(firstFileSaved); return(true); }
public bool Save(AutoSaveSettings settings, List<ScannedImage> images, ISaveNotify notify) { if (appConfigManager.Config.DisableAutoSave) { return false; } try { bool ok = true; DateTime now = DateTime.Now; int i = 0; string firstFileSaved = null; var scans = SaveSeparatorHelper.SeparateScans(new[] { images }, settings.Separator).ToList(); foreach (var imageList in scans) { if (!SaveOneFile(settings, now, i++, imageList, scans.Count == 1 ? notify : null, ref firstFileSaved)) { ok = false; } } if (notify != null && scans.Count > 1 && ok) { // Can't just do images.Count because that includes patch codes int imageCount = scans.SelectMany(x => x).Count(); notify.ImagesSaved(imageCount, firstFileSaved); } return ok; } catch (Exception ex) { Log.ErrorException(MiscResources.AutoSaveError, ex); errorOutput.DisplayError(MiscResources.AutoSaveError, ex); return false; } }
public bool SaveImages(List <ScannedImage> images, ISaveNotify notify) { if (images.Any()) { string savePath; var imageSettings = imageSettingsContainer.ImageSettings; if (imageSettings.SkipSavePrompt && Path.IsPathRooted(imageSettings.DefaultFileName)) { savePath = imageSettings.DefaultFileName; } else { if (!dialogHelper.PromptToSaveImage(imageSettings.DefaultFileName, out savePath)) { return(false); } } var op = operationFactory.Create <SaveImagesOperation>(); var progressForm = formFactory.Create <FProgress>(); progressForm.Operation = op; progressForm.Start = () => op.Start(savePath, DateTime.Now, images); progressForm.ShowDialog(); if (op.Status.Success) { changeTracker.HasUnsavedChanges = false; notify?.ImagesSaved(images.Count, op.FirstFileSaved); return(true); } } return(false); }
public async Task <bool> SaveImages(List <ScannedImage> images, ISaveNotify notify) { if (images.Any()) { string savePath; var imageSettings = imageSettingsContainer.ImageSettings; if (imageSettings.SkipSavePrompt && Path.IsPathRooted(imageSettings.DefaultFileName)) { savePath = imageSettings.DefaultFileName; } else { if (!dialogHelper.PromptToSaveImage(imageSettings.DefaultFileName, out savePath)) { return(false); } } var op = operationFactory.Create <SaveImagesOperation>(); var changeToken = changeTracker.State; if (op.Start(savePath, DateTime.Now, images)) { operationProgress.ShowProgress(op); } if (await op.Success) { changeTracker.Saved(changeToken); notify?.ImagesSaved(images.Count, op.FirstFileSaved); return(true); } } return(false); }
private bool SaveOneFile(AutoSaveSettings settings, DateTime now, int i, List<ScannedImage> images, ISaveNotify notify, ref string firstFileSaved) { if (images.Count == 0) { return true; } var form = formFactory.Create<FProgress>(); var subPath = fileNamePlaceholders.SubstitutePlaceholders(settings.FilePath, now, true, i); var extension = Path.GetExtension(subPath); if (extension != null && extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase)) { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); } var op = operationFactory.Create<SavePdfOperation>(); form.Operation = op; var ocrLanguageCode = userConfigManager.Config.EnableOcr ? userConfigManager.Config.OcrLanguageCode : null; if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrLanguageCode, false)) { form.ShowDialog(); } if (op.Status.Success && firstFileSaved == null) { firstFileSaved = subPath; } if (op.Status.Success && notify != null) { notify.PdfSaved(subPath); } return op.Status.Success; } else { var op = operationFactory.Create<SaveImagesOperation>(); form.Operation = op; if (op.Start(subPath, now, images)) { form.ShowDialog(); } if (op.Status.Success && firstFileSaved == null) { firstFileSaved = op.FirstFileSaved; } if (op.Status.Success && notify != null) { notify.ImagesSaved(images.Count, op.FirstFileSaved); } return op.Status.Success; } }
//TODO public bool AlfSavePDF(List <ScannedImage> images, ISaveNotify notify) { if (images.Any()) { var savePath = NAPS2_Alfresco.utils.FileUtils.GetTempPath() + "\\" + DateTime.Now.ToFileTime() + ".pdf"; if (ExportPDF(savePath, images, false)) { changeTracker.HasUnsavedChanges = false; NAPS2_Alfresco.AlfResult alfResult = SessionUtils.UploadFile(savePath); if (alfResult.status == NAPS2_Alfresco.AlfResult.STATUS_OK) { notify?.PdfSaved(savePath); return(true); } else { MessageBox.Show(alfResult.message, "Error " + alfResult.status, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } return(false); }
public async Task PerformScan(ScanProfile scanProfile, ScanParams scanParams, IWin32Window dialogParent, ISaveNotify notify, Action <ScannedImage> imageCallback, CancellationToken cancelToken = default) { var driver = driverFactory.Create(scanProfile.DriverName); driver.DialogParent = dialogParent; driver.ScanProfile = scanProfile; driver.ScanParams = scanParams; driver.CancelToken = cancelToken; try { if (scanProfile.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } if (appConfigManager.Config.AlwaysRememberDevice) { scanProfile.Device = device; profileManager.Save(); } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanProfile.Device; } // Start the scan int imageCount = 0; var source = driver.Scan().Then(img => imageCount++); bool doAutoSave = !scanParams.NoAutoSave && !appConfigManager.Config.DisableAutoSave && scanProfile.EnableAutoSave && scanProfile.AutoSaveSettings != null; if (doAutoSave) { if (scanProfile.AutoSaveSettings.ClearImagesAfterSaving) { // Auto save without piping images var images = await source.ToList(); if (await autoSave.Save(scanProfile.AutoSaveSettings, images, notify)) { foreach (ScannedImage img in images) { img.Dispose(); } } else { // Fallback in case auto save failed; pipe all the images back at once foreach (ScannedImage img in images) { imageCallback(img); } } } else { // Basic auto save, so keep track of images as we pipe them and try to auto save afterwards var images = new List <ScannedImage>(); await source.ForEach(scannedImage => { imageCallback(scannedImage); images.Add(scannedImage); }); await autoSave.Save(scanProfile.AutoSaveSettings, images, notify); } } else { // No auto save, so just pipe images back as we get them await source.ForEach(imageCallback); } if (imageCount > 0) { Log.Event(EventType.Scan, new Event { Name = MiscResources.Scan, Pages = imageCount, DeviceName = scanProfile.Device?.Name, ProfileName = scanProfile.DisplayName, BitDepth = scanProfile.BitDepth.Description() }); } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); errorOutput.DisplayError(e.Message, e); } else { errorOutput.DisplayError(e.Message); } } }
private bool SaveOneFile(AutoSaveSettings settings, DateTime now, int i, List <ScannedImage> images, ISaveNotify notify, ref string firstFileSaved) { if (images.Count == 0) { return(true); } var form = formFactory.Create <FProgress>(); var subPath = fileNamePlaceholders.SubstitutePlaceholders(settings.FilePath, now, true, i); var extension = Path.GetExtension(subPath); if (extension != null && extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase)) { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); } var op = operationFactory.Create <SavePdfOperation>(); form.Operation = op; var ocrLanguageCode = userConfigManager.Config.EnableOcr ? userConfigManager.Config.OcrLanguageCode : null; if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrLanguageCode, false)) { form.ShowDialog(); } if (op.Status.Success && firstFileSaved == null) { firstFileSaved = subPath; } if (op.Status.Success && notify != null) { notify.PdfSaved(subPath); } return(op.Status.Success); } else { var op = operationFactory.Create <SaveImagesOperation>(); form.Operation = op; if (op.Start(subPath, now, images)) { form.ShowDialog(); } if (op.Status.Success && firstFileSaved == null) { firstFileSaved = op.FirstFileSaved; } if (op.Status.Success && notify != null) { notify.ImagesSaved(images.Count, op.FirstFileSaved); } return(op.Status.Success); } }
public void PerformScan(ScanProfile scanProfile, ScanParams scanParams, IWin32Window dialogParent, ISaveNotify notify, Action <ScannedImage> imageCallback) { var driver = driverFactory.Create(scanProfile.DriverName); driver.DialogParent = dialogParent; driver.ScanProfile = scanProfile; driver.ScanParams = scanParams; try { if (scanProfile.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } if (appConfigManager.Config.AlwaysRememberDevice) { scanProfile.Device = device; profileManager.Save(); } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanProfile.Device; } bool doAutoSave = !scanParams.NoAutoSave && !appConfigManager.Config.DisableAutoSave && scanProfile.EnableAutoSave && scanProfile.AutoSaveSettings != null; if (doAutoSave) { if (scanProfile.AutoSaveSettings.ClearImagesAfterSaving) { // Auto save without piping images var images = driver.Scan().ToList(); if (autoSave.Save(scanProfile.AutoSaveSettings, images, notify)) { foreach (ScannedImage img in images) { img.Dispose(); } } else { // Fallback in case auto save failed; pipe all the images back at once foreach (ScannedImage img in images) { imageCallback(img); } } } else { // Basic auto save, so keep track of images as we pipe them and try to auto save afterwards var images = new List <ScannedImage>(); foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); images.Add(scannedImage); } autoSave.Save(scanProfile.AutoSaveSettings, images, notify); } } else { // No auto save, so just pipe images back as we get them foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); } } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); errorOutput.DisplayError(e.Message, e); } else { errorOutput.DisplayError(e.Message); } } }
public void PerformScan(ScanProfile scanProfile, ScanParams scanParams, IWin32Window dialogParent, ISaveNotify notify, Action<ScannedImage> imageCallback) { var driver = driverFactory.Create(scanProfile.DriverName); driver.DialogParent = dialogParent; driver.ScanProfile = scanProfile; driver.ScanParams = scanParams; try { if (scanProfile.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } if (appConfigManager.Config.AlwaysRememberDevice) { scanProfile.Device = device; profileManager.Save(); } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanProfile.Device; } bool doAutoSave = !scanParams.NoAutoSave && !appConfigManager.Config.DisableAutoSave && scanProfile.EnableAutoSave && scanProfile.AutoSaveSettings != null; if (doAutoSave) { if (scanProfile.AutoSaveSettings.ClearImagesAfterSaving) { // Auto save without piping images var images = driver.Scan().ToList(); if (autoSave.Save(scanProfile.AutoSaveSettings, images, notify)) { foreach (ScannedImage img in images) { img.Dispose(); } } else { // Fallback in case auto save failed; pipe all the images back at once foreach (ScannedImage img in images) { imageCallback(img); } } } else { // Basic auto save, so keep track of images as we pipe them and try to auto save afterwards var images = new List<ScannedImage>(); foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); images.Add(scannedImage); } autoSave.Save(scanProfile.AutoSaveSettings, images, notify); } } else { // No auto save, so just pipe images back as we get them foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); } } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); errorOutput.DisplayError(e.Message, e); } else { errorOutput.DisplayError(e.Message); } } }
private async Task <(bool, string)> SaveOneFile(AutoSaveSettings settings, DateTime now, int i, List <ScannedImage> images, ISaveNotify notify) { if (images.Count == 0) { return(true, null); } string subPath = fileNamePlaceholders.SubstitutePlaceholders(settings.FilePath, now, true, i); if (settings.PromptForFilePath) { if (dialogHelper.PromptToSavePdfOrImage(subPath, out string newPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(newPath, now, true, i); } } var extension = Path.GetExtension(subPath); if (extension != null && extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase)) { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); } var op = operationFactory.Create <SavePdfOperation>(); if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, false, null)) { operationProgress.ShowProgress(op); } bool success = await op.Success; if (success) { notify?.PdfSaved(subPath); } return(success, subPath); } else { var op = operationFactory.Create <SaveImagesOperation>(); if (op.Start(subPath, now, images)) { operationProgress.ShowProgress(op); } bool success = await op.Success; if (success) { notify?.ImagesSaved(images.Count, op.FirstFileSaved); } return(success, subPath); } }