Add a reusable "GetDeviceList" method to IScanDriver (credit: Daniel Gómez Didier)

This commit is contained in:
Ben Olden-Cooligan 2016-06-04 21:53:26 -04:00
parent 11cfbbdd2a
commit 0b27c52d5b
6 changed files with 62 additions and 7 deletions

View File

@ -68,6 +68,13 @@ namespace NAPS2.Scan
/// <exception cref="InvalidOperationException">Throws an InvalidOperationException if DialogParent has not been set.</exception>
ScanDevice PromptForDevice();
/// <summary>
/// Gets a list of available scanning devices.
/// </summary>
/// <returns>The list of devices.</returns>
/// <exception cref="ScanDriverException">Throws a ScanDriverException if an error occurs when reading the available devices.</exception>
List<ScanDevice> GetDeviceList();
/// <summary>
/// Scans one or more images, interacting with the user as necessary.
/// </summary>

View File

@ -61,6 +61,24 @@ namespace NAPS2.Scan
protected abstract ScanDevice PromptForDeviceInternal();
public List<ScanDevice> GetDeviceList()
{
try
{
return GetDeviceListInternal();
}
catch (ScanDriverException)
{
throw;
}
catch (Exception e)
{
throw new ScanDriverUnknownException(e);
}
}
protected abstract List<ScanDevice> GetDeviceListInternal();
public IEnumerable<ScannedImage> Scan()
{
if (ScanProfile == null)

View File

@ -50,6 +50,14 @@ namespace NAPS2.Scan.Stub
return new ScanDevice("test", "Test Scanner");
}
public List<ScanDevice> GetDeviceList()
{
return new List<ScanDevice>
{
new ScanDevice("test", "Test Scanner")
};
}
public IEnumerable<ScannedImage> Scan()
{
for (int i = 0; i < ImageCount; i++)

View File

@ -56,10 +56,7 @@ namespace NAPS2.Scan.Twain
protected override ScanDevice PromptForDeviceInternal()
{
var twainImpl = ScanProfile != null ? ScanProfile.TwainImpl : TwainImpl.Default;
// Exclude WIA proxy devices since NAPS2 already supports WIA
var deviceList = GetDeviceList(twainImpl).Where(x => !x.ID.StartsWith("WIA-")).ToList();
var deviceList = GetDeviceList();
if (!deviceList.Any())
{
@ -72,8 +69,15 @@ namespace NAPS2.Scan.Twain
return form.SelectedDevice;
}
private IEnumerable<ScanDevice> GetDeviceList(TwainImpl twainImpl)
protected override List<ScanDevice> GetDeviceListInternal()
{
// Exclude WIA proxy devices since NAPS2 already supports WIA
return GetFullDeviceList().Where(x => !x.ID.StartsWith("WIA-")).ToList();
}
private IEnumerable<ScanDevice> GetFullDeviceList()
{
var twainImpl = ScanProfile != null ? ScanProfile.TwainImpl : TwainImpl.Default;
if (UseHostService)
{
return x86HostServiceFactory.Create().TwainGetDeviceList(twainImpl);

View File

@ -97,7 +97,7 @@ namespace NAPS2.Scan.Wia
#region Device/Item Management
public static ScanDevice PromptForDevice()
public static ScanDevice PromptForScanDevice()
{
var wiaCommonDialog = new CommonDialogClass();
try
@ -116,6 +116,19 @@ namespace NAPS2.Scan.Wia
}
}
public static List<ScanDevice> GetScanDeviceList()
{
DeviceManager manager = new DeviceManagerClass();
var devices = new List<ScanDevice>();
foreach (DeviceInfo info in manager.DeviceInfos)
{
Device device = info.Connect();
devices.Add(new ScanDevice(info.DeviceID, GetDeviceName(device)));
}
return devices;
}
public static Device GetDevice(ScanDevice scanDevice)
{
DeviceManager manager = new DeviceManagerClass();

View File

@ -54,7 +54,12 @@ namespace NAPS2.Scan.Wia
protected override ScanDevice PromptForDeviceInternal()
{
return WiaApi.PromptForDevice();
return WiaApi.PromptForScanDevice();
}
protected override List<ScanDevice> GetDeviceListInternal()
{
return WiaApi.GetScanDeviceList();
}
protected override IEnumerable<ScannedImage> ScanInternal()