2016-01-08 05:21:47 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2016-01-08 07:09:02 +03:00
|
|
|
|
using NAPS2.Config;
|
2016-01-08 05:21:47 +03:00
|
|
|
|
using NAPS2.Lang.Resources;
|
|
|
|
|
using NAPS2.Operation;
|
|
|
|
|
using NAPS2.Scan.Images;
|
|
|
|
|
using NAPS2.Util;
|
|
|
|
|
|
|
|
|
|
namespace NAPS2.ImportExport
|
|
|
|
|
{
|
2016-01-08 06:00:22 +03:00
|
|
|
|
public class DirectImportOperation : OperationBase
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
private readonly IUserConfigManager userConfigManager;
|
2016-01-08 05:21:47 +03:00
|
|
|
|
private readonly ThreadFactory threadFactory;
|
|
|
|
|
|
|
|
|
|
private bool cancel;
|
|
|
|
|
private Thread thread;
|
|
|
|
|
|
2016-01-11 05:37:59 +03:00
|
|
|
|
public DirectImportOperation(IUserConfigManager userConfigManager, ThreadFactory threadFactory)
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
this.userConfigManager = userConfigManager;
|
2016-01-08 05:21:47 +03:00
|
|
|
|
this.threadFactory = threadFactory;
|
|
|
|
|
|
|
|
|
|
AllowCancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 05:37:59 +03:00
|
|
|
|
public bool Start(DirectImageTransfer data, bool copy, Action<ScannedImage> imageCallback)
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
ProgressTitle = copy ? MiscResources.CopyProgress : MiscResources.ImportProgress;
|
2016-01-08 05:21:47 +03:00
|
|
|
|
Status = new OperationStatus
|
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
StatusText = copy ? MiscResources.Copying : MiscResources.Importing,
|
2016-01-08 06:00:22 +03:00
|
|
|
|
MaxProgress = data.ImageRecovery.Length
|
2016-01-08 05:21:47 +03:00
|
|
|
|
};
|
|
|
|
|
cancel = false;
|
|
|
|
|
|
|
|
|
|
thread = threadFactory.StartThread(() =>
|
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
Exception error = null;
|
|
|
|
|
foreach (var ir in data.ImageRecovery)
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
try
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 06:00:22 +03:00
|
|
|
|
using (var bitmap = new Bitmap(Path.Combine(data.RecoveryFolder, ir.FileName)))
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-11 05:37:59 +03:00
|
|
|
|
var img = new ScannedImage(bitmap, ir.BitDepth, ir.HighQuality, -1);
|
2016-01-08 07:09:02 +03:00
|
|
|
|
foreach (var transform in ir.TransformList)
|
|
|
|
|
{
|
|
|
|
|
img.AddTransform(transform);
|
|
|
|
|
}
|
|
|
|
|
img.SetThumbnail(img.RenderThumbnail(userConfigManager.Config.ThumbnailSize));
|
2016-01-08 05:21:47 +03:00
|
|
|
|
imageCallback(img);
|
|
|
|
|
|
|
|
|
|
Status.CurrentProgress++;
|
|
|
|
|
InvokeStatusChanged();
|
|
|
|
|
if (cancel)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-08 07:09:02 +03:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
error = ex;
|
|
|
|
|
}
|
2016-01-08 05:21:47 +03:00
|
|
|
|
}
|
2016-01-08 07:09:02 +03:00
|
|
|
|
if (error != null)
|
2016-01-08 05:21:47 +03:00
|
|
|
|
{
|
2016-01-08 07:09:02 +03:00
|
|
|
|
Log.ErrorException(string.Format(MiscResources.ImportErrorCouldNot, data.RecoveryFolder), error);
|
2016-01-08 05:21:47 +03:00
|
|
|
|
}
|
2016-01-08 07:09:02 +03:00
|
|
|
|
Status.Success = true;
|
2016-01-08 05:21:47 +03:00
|
|
|
|
InvokeFinished();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WaitUntilFinished()
|
|
|
|
|
{
|
|
|
|
|
thread.Join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Cancel()
|
|
|
|
|
{
|
|
|
|
|
cancel = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|