mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-21 04:37:47 +03:00
b62100fb07
No longer keeping System, System.Collections.Generic, System.Linq
68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using NAPS2.Lang.Resources;
|
|
using NAPS2.Logging;
|
|
using NAPS2.Operation;
|
|
using NAPS2.Images;
|
|
using NAPS2.Util;
|
|
|
|
namespace NAPS2.ImportExport
|
|
{
|
|
public class ImportOperation : OperationBase
|
|
{
|
|
private readonly IScannedImageImporter scannedImageImporter;
|
|
|
|
public ImportOperation(IScannedImageImporter scannedImageImporter)
|
|
{
|
|
this.scannedImageImporter = scannedImageImporter;
|
|
|
|
ProgressTitle = MiscResources.ImportProgress;
|
|
AllowCancel = true;
|
|
AllowBackground = true;
|
|
}
|
|
|
|
public bool Start(List<string> filesToImport, Action<ScannedImage> imageCallback, ImportParams importParams)
|
|
{
|
|
bool oneFile = filesToImport.Count == 1;
|
|
Status = new OperationStatus
|
|
{
|
|
MaxProgress = oneFile ? 0 : filesToImport.Count
|
|
};
|
|
|
|
RunAsync(async () =>
|
|
{
|
|
try
|
|
{
|
|
foreach (var fileName in filesToImport)
|
|
{
|
|
try
|
|
{
|
|
Status.StatusText = string.Format(MiscResources.ImportingFormat, Path.GetFileName(fileName));
|
|
InvokeStatusChanged();
|
|
var imageSrc = scannedImageImporter.Import(fileName, importParams, oneFile ? OnProgress : new ProgressHandler((j, k) => { }), CancelToken);
|
|
await imageSrc.ForEach(imageCallback);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.ErrorException(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(fileName)), ex);
|
|
InvokeError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(fileName)), ex);
|
|
}
|
|
if (!oneFile)
|
|
{
|
|
Status.CurrentProgress++;
|
|
InvokeStatusChanged();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
finally
|
|
{
|
|
GC.Collect();
|
|
}
|
|
});
|
|
return true;
|
|
}
|
|
}
|
|
}
|