public void PrintCommand_GivenValidPrintOnlyFile_HasCorrectStartInfo() { var tempFile = TempFileHelper.CreateTempFile("PrintCommand", "test.ini"); var printCommand = new PrintCommand(tempFile, _printerHelper.GetDefaultPrinter(), new FileAssoc(), _printerHelper, _timeOut); var factory = new MockProcessWrapperFactory(true); printCommand.ProcessWrapperFactory = factory; printCommand.Print(); Assert.IsTrue(printCommand.Successful); StringAssert.Contains(tempFile, factory.LastMock.StartInfo.Arguments); }
/// <summary> /// Prints the file. /// </summary> /// <param name="processTimeout">The timespan to wait for the process to finish</param> /// <returns>true, if printing was successful</returns> public bool Print(TimeSpan processTimeout) { if (CommandType == PrintType.Unprintable) { throw new InvalidOperationException("File is not printable"); } if (CommandType == PrintType.Print && Printer != _printerHelper.GetDefaultPrinter()) { throw new InvalidOperationException("The default printer needs to be set in order to print this file"); } var p = ProcessWrapperFactory.BuildProcessWrapper(new ProcessStartInfo()); var verb = SupportsPrintTo() ? "printto" : "print"; var command = GetCommand(verb); var arguments = SupportsPrintTo() ? command.GetReplacedCommandArgs(Filename, Printer) : command.GetReplacedCommandArgs(Filename); p.StartInfo.FileName = command.Command; p.StartInfo.Arguments = arguments; Logger.Debug($"Launching {verb} for \"{Filename}\": {command.Command} {arguments}"); try { p.Start(); p.WaitForExit(processTimeout); if (!p.HasExited) { Logger.Warn("Process was not finishing after {0} seconds, killing it now...", processTimeout.TotalSeconds); p.Kill(); } else { Successful = true; } } catch (Exception ex) { Logger.Error("Exception during printing" + "\r\nType: " + ex.GetType() + "\r\nMessage: " + ex.Message ); return(false); } return(Successful); }
/// <summary> /// Prints all files in the list. /// </summary> /// <returns>true, if all files could be printed</returns> public bool PrintAll() { if (string.IsNullOrEmpty(PdfCreatorPrinter)) { _logger.Error("No PDFCreator is installed."); return(false); } var requiresDefaultPrinter = _printCommands.RequiresDefaultPrinter; var defaultPrinter = _printerHelper.GetDefaultPrinter(); try { if (requiresDefaultPrinter && (defaultPrinter != PdfCreatorPrinter)) { _logger.Debug("Current default printer is " + defaultPrinter); _logger.Info("PDFCreator must be set temporarily as default printer"); if (_settingsProvider.Settings.CreatorAppSettings.AskSwitchDefaultPrinter) { if (!QuerySwitchDefaultPrinter()) { return(false); } } if (!_printerHelper.SetDefaultPrinter(PdfCreatorPrinter)) { _logger.Error("PDFCreator could not be set as default printer"); return(false); } _logger.Debug("PDFCreator set as default printer"); } return(_printCommands.PrintAll(_settingsProvider.Settings.ApplicationSettings.ConversionTimeout)); } finally { if (requiresDefaultPrinter) { _printerHelper.SetDefaultPrinter(defaultPrinter); _logger.Debug("Default printer set back to " + defaultPrinter); } _printCommands = new PrintCommandGroup(); } }
/// <summary> /// Prints all files in the list. /// </summary> /// <returns>true, if all files could be printed</returns> public bool PrintAll() { if (string.IsNullOrEmpty(PdfCreatorPrinter)) { _logger.Error("No PDFCreator is installed."); return(false); } var requiresDefaultPrinter = _printCommands.RequiresDefaultPrinter; var defaultPrinter = _printerHelper.GetDefaultPrinter(); try { if (requiresDefaultPrinter) { if (_settingsProvider.Settings.ApplicationSettings.AskSwitchDefaultPrinter) { if (!QuerySwitchDefaultPrinter()) { return(false); } } _printerHelper.SetDefaultPrinter(PdfCreatorPrinter); } return(_printCommands.PrintAll()); } finally { if (requiresDefaultPrinter) { _printerHelper.SetDefaultPrinter(defaultPrinter); } _printCommands = new PrintCommandGroup(); } }