From 09c3054485052a39dfabdc13c41780ae65b7d409 Mon Sep 17 00:00:00 2001 From: Gerrit Edzards Date: Mon, 19 Aug 2024 16:00:55 +0200 Subject: [PATCH] Add the option for custom redirection --- .../GhostScriptRedirector.cs | 13 ++++----- .../AmagnoVirtualPrinter.Delivery/Program.cs | 10 ++++++- .../Redirector.cs | 28 ++++++------------- .../Interfaces/IExConfig.cs | 5 ++++ .../Model/RegistryConfig.cs | 1 + Common/AmagnoVirtualPrinter.Utils/Consts.cs | 3 +- .../RegistryRepository.cs | 22 ++++++++++++++- .../Script.cs | 4 +-- 8 files changed, 55 insertions(+), 31 deletions(-) diff --git a/Agent/AmagnoVirtualPrinter.Delivery/GhostScriptRedirector.cs b/Agent/AmagnoVirtualPrinter.Delivery/GhostScriptRedirector.cs index 39d479f..f969ac3 100644 --- a/Agent/AmagnoVirtualPrinter.Delivery/GhostScriptRedirector.cs +++ b/Agent/AmagnoVirtualPrinter.Delivery/GhostScriptRedirector.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using System.IO; -using System.Printing; using AmagnoVirtualPrinter.Logging; using AmagnoVirtualPrinter.Utils; using JetBrains.Annotations; @@ -17,16 +16,16 @@ public class GhostScriptRedirector private const string GsWin32 = "gswin32c.exe"; [NotNull] - private readonly PrintQueue _queue; + private readonly string _printerName; - public GhostScriptRedirector([NotNull]PrintQueue queue) + public GhostScriptRedirector([NotNull]string printerName) { - if (queue == null) + if (printerName == null) { - throw new ArgumentNullException(nameof(queue)); + throw new ArgumentNullException(nameof(printerName)); } - _queue = queue; + _printerName = printerName; } /// @@ -50,7 +49,7 @@ public void Redirect([NotNull]string file) } // More details about the arguments can be find at https://www.ghostscript.com/doc/current/Use.htm - var ghostScriptArguments = $"-dPrinted -dBATCH -dNOPAUSE -dNoCancel -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"%printer%{ _queue.FullName}\" \"{file}\""; + var ghostScriptArguments = $"-dPrinted -dBATCH -dNOPAUSE -dNoCancel -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"%printer%{ _printerName}\" \"{file}\""; logger.Info("Try to start the process {ghostScriptExe} with the following arguments: {ghostScriptArguments}.", ghostScriptExe, ghostScriptArguments); var processStartInfo = new ProcessStartInfo diff --git a/Agent/AmagnoVirtualPrinter.Delivery/Program.cs b/Agent/AmagnoVirtualPrinter.Delivery/Program.cs index 2e72b10..b5a895f 100644 --- a/Agent/AmagnoVirtualPrinter.Delivery/Program.cs +++ b/Agent/AmagnoVirtualPrinter.Delivery/Program.cs @@ -20,7 +20,15 @@ private static void Main([CanBeNull]string[] args) switch (args[0]) { case RedirectCmd: { - RedirectToPrinter(args[1], args[2]); + var filePath = args[1]; + var printerName = args[2]; + + if (!PrinterExists(printerName)) + { + Console.WriteLine($"Printer {printerName} does not exist!"); + } + + RedirectToPrinter(filePath, printerName); break; } default: { diff --git a/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs b/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs index 3574185..2262801 100644 --- a/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs +++ b/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs @@ -21,31 +21,21 @@ public static void RedirectToPrinter([NotNull]string filePath, [NotNull]string p } var file = Path.GetFullPath(filePath); + + var ghostScriptRedirector = new GhostScriptRedirector(printerName); + ghostScriptRedirector.Redirect(file); + } + + public static bool PrinterExists(string printerName) + { // e.g. "Dell C2665dnf Color MFP" using (var localSystem = new LocalPrintServer()) { - using (var queue = localSystem.GetPrintQueueSafe(printerName)) + using (localSystem.GetPrintQueue(printerName)) { - if (queue != null) - { - var ghostScriptRedirector = new GhostScriptRedirector(queue); - ghostScriptRedirector.Redirect(file); - } + return true; } } } - - [CanBeNull] - private static PrintQueue GetPrintQueueSafe(this PrintServer server, string name) - { - try - { - return server.GetPrintQueue(name); - } - catch - { - return null; - } - } } } \ No newline at end of file diff --git a/Common/AmagnoVirtualPrinter.Agent.Core/Interfaces/IExConfig.cs b/Common/AmagnoVirtualPrinter.Agent.Core/Interfaces/IExConfig.cs index 30e7d6a..1062ca3 100644 --- a/Common/AmagnoVirtualPrinter.Agent.Core/Interfaces/IExConfig.cs +++ b/Common/AmagnoVirtualPrinter.Agent.Core/Interfaces/IExConfig.cs @@ -24,5 +24,10 @@ public interface IExConfig : IConfig /// An intermediate format which is read by the printer or similar. /// IntermediateFormat IntermediateFormat { get; } + + /// + /// Is not empty if the redirection is performed by another application + /// + string CustomRedirection { get; } } } \ No newline at end of file diff --git a/Common/AmagnoVirtualPrinter.Agent.Core/Model/RegistryConfig.cs b/Common/AmagnoVirtualPrinter.Agent.Core/Model/RegistryConfig.cs index 37c4748..b6cc2a7 100644 --- a/Common/AmagnoVirtualPrinter.Agent.Core/Model/RegistryConfig.cs +++ b/Common/AmagnoVirtualPrinter.Agent.Core/Model/RegistryConfig.cs @@ -28,6 +28,7 @@ public Tuple ResolvedPostconverter } public IntermediateFormat IntermediateFormat { get; set; } + public string CustomRedirection { get; set; } [NotNull] private static Tuple GetResolvedArgs([NotNull]string text) diff --git a/Common/AmagnoVirtualPrinter.Utils/Consts.cs b/Common/AmagnoVirtualPrinter.Utils/Consts.cs index 27c0ba9..b490ea1 100644 --- a/Common/AmagnoVirtualPrinter.Utils/Consts.cs +++ b/Common/AmagnoVirtualPrinter.Utils/Consts.cs @@ -6,7 +6,7 @@ public struct Files public const string PRINTER_SERVICE_EXE = "AmagnoPrinterAgent.exe"; public const string SETUP_DRIVER_EXE = "setupdrv.exe"; public const string AGENT_PROGRESS_EXE = "AmagnoPrinterAgentProgress.exe"; - public const string DILIVERY_EXE = "delivery.exe"; + public const string DELIVERY_EXE = "delivery.exe"; public const string LICENCE_FILE = "LICENSE.rtf"; public const string PRE_CONVERTER = @"C:\Program Files (x86)\MyPreConverter.exe ARG"; public const string POST_CONVERTER = @"C:\Program Files (x86)\MyPostConverter.exe ARG"; @@ -26,6 +26,7 @@ public struct Keys public struct KeyNames { + public const string CUSTOM_REDIRECTION_KEY = "CustomRedirection"; public const string EXECUTABLE_FILE = "Executable File"; public const string INSTALLATION_DIR = "Installation Directory"; public const string THREADS = "Threads"; diff --git a/Common/AmagnoVirtualPrinter.Utils/RegistryRepository.cs b/Common/AmagnoVirtualPrinter.Utils/RegistryRepository.cs index b0f9663..565f909 100644 --- a/Common/AmagnoVirtualPrinter.Utils/RegistryRepository.cs +++ b/Common/AmagnoVirtualPrinter.Utils/RegistryRepository.cs @@ -70,6 +70,8 @@ public IExConfig GetRegistryConfig() { CheckForNull(driver, subKey); + registryConfig.CustomRedirection = GetRegKeyStringValue(driver, KeyNames.CUSTOM_REDIRECTION_KEY); + using(var key = driver.OpenSubKey(Keys.POSTCONVERTER_KEY)) { CheckForNull(key, Keys.POSTCONVERTER_KEY); @@ -96,6 +98,18 @@ public IExConfig GetRegistryConfig() return registryConfig; } + private static string GetRegKeyStringValue(RegistryKey key, string name) + { + var value = key.GetValue(name); + + if (value != null) + { + return value.ToString(); + } + + return string.Empty; + } + [ContractAnnotation("key:null => void")] private static void CheckForNull(RegistryKey key, string keyName) { @@ -108,7 +122,8 @@ private static void CheckForNull(RegistryKey key, string keyName) public IUserConfig GetUserRegistryConfig(string sid) { Logger.Trace("GetUserRegistryConfig for {sid}", sid); - + + var registryConfig = GetRegistryConfig(); var regView = GetRegistryView(); var userConfig = new UserRegistryConfig(); @@ -124,6 +139,11 @@ public IUserConfig GetUserRegistryConfig(string sid) CheckForNull(converter, Keys.CONVERTER_KEY); userConfig.OutputDirectory = converter.GetValue(KeyNames.OUTPUT_DIR).ToString(); + if (!string.IsNullOrEmpty(registryConfig.CustomRedirection)) + { + return userConfig; + } + subKey = "Redirect"; using (var redirect = converter.OpenSubKey(subKey)) { diff --git a/Installer/AmagnoVirtualPrinter.WixSharpInstaller/Script.cs b/Installer/AmagnoVirtualPrinter.WixSharpInstaller/Script.cs index cb6e0de..6ef90d8 100644 --- a/Installer/AmagnoVirtualPrinter.WixSharpInstaller/Script.cs +++ b/Installer/AmagnoVirtualPrinter.WixSharpInstaller/Script.cs @@ -141,8 +141,8 @@ File printerServiceFile ( @"%ProgramFiles%\AmagnoPrinterDriver\", new DirFiles(feature, _filesDir + @"\*", s => !s.EndsWith(".exe")), - new File(new Id(SetupDriverId), feature, Path.Combine(_filesDir, Utils.Files.SETUP_DRIVER_EXE)), - new File(feature, Path.Combine(_filesDir, Files.DILIVERY_EXE)), + new File(new Id(SetupDriverId), feature, Path.Combine(_filesDir, Files.SETUP_DRIVER_EXE)), + new File(feature, Path.Combine(_filesDir, Files.DELIVERY_EXE)), new File(feature, Path.Combine(_filesDir, Files.AGENT_PROGRESS_EXE)), printerServiceFile )